blob: 7c05c6e1abc7de001ec26fb1ed9535347b77a337 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * The UBI Eraseblock Association (EBA) unit.
23 *
24 * This unit is responsible for I/O to/from logical eraseblock.
25 *
26 * Although in this implementation the EBA table is fully kept and managed in
27 * RAM, which assumes poor scalability, it might be (partially) maintained on
28 * flash in future implementations.
29 *
30 * The EBA unit implements per-logical eraseblock locking. Before accessing a
31 * logical eraseblock it is locked for reading or writing. The per-logical
32 * eraseblock locking is implemented by means of the lock tree. The lock tree
33 * is an RB-tree which refers all the currently locked logical eraseblocks. The
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +020034 * lock tree elements are &struct ubi_ltree_entry objects. They are indexed by
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040035 * (@vol_id, @lnum) pairs.
36 *
37 * EBA also maintains the global sequence counter which is incremented each
38 * time a logical eraseblock is mapped to a physical eraseblock and it is
39 * stored in the volume identifier header. This means that each VID header has
40 * a unique sequence number. The sequence number is only increased an we assume
41 * 64 bits is enough to never overflow.
42 */
43
44#include <linux/slab.h>
45#include <linux/crc32.h>
46#include <linux/err.h>
47#include "ubi.h"
48
Artem Bityutskiye8823bd2007-09-13 14:28:14 +030049/* Number of physical eraseblocks reserved for atomic LEB change operation */
50#define EBA_RESERVED_PEBS 1
51
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040052/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040053 * next_sqnum - get next sequence number.
54 * @ubi: UBI device description object
55 *
56 * This function returns next sequence number to use, which is just the current
57 * global sequence counter value. It also increases the global sequence
58 * counter.
59 */
60static unsigned long long next_sqnum(struct ubi_device *ubi)
61{
62 unsigned long long sqnum;
63
64 spin_lock(&ubi->ltree_lock);
65 sqnum = ubi->global_sqnum++;
66 spin_unlock(&ubi->ltree_lock);
67
68 return sqnum;
69}
70
71/**
72 * ubi_get_compat - get compatibility flags of a volume.
73 * @ubi: UBI device description object
74 * @vol_id: volume ID
75 *
76 * This function returns compatibility flags for an internal volume. User
77 * volumes have no compatibility flags, so %0 is returned.
78 */
79static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
80{
81 if (vol_id == UBI_LAYOUT_VOL_ID)
82 return UBI_LAYOUT_VOLUME_COMPAT;
83 return 0;
84}
85
86/**
87 * ltree_lookup - look up the lock tree.
88 * @ubi: UBI device description object
89 * @vol_id: volume ID
90 * @lnum: logical eraseblock number
91 *
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +020092 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040093 * object if the logical eraseblock is locked and %NULL if it is not.
94 * @ubi->ltree_lock has to be locked.
95 */
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +020096static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
97 int lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040098{
99 struct rb_node *p;
100
101 p = ubi->ltree.rb_node;
102 while (p) {
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200103 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400104
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200105 le = rb_entry(p, struct ubi_ltree_entry, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400106
107 if (vol_id < le->vol_id)
108 p = p->rb_left;
109 else if (vol_id > le->vol_id)
110 p = p->rb_right;
111 else {
112 if (lnum < le->lnum)
113 p = p->rb_left;
114 else if (lnum > le->lnum)
115 p = p->rb_right;
116 else
117 return le;
118 }
119 }
120
121 return NULL;
122}
123
124/**
125 * ltree_add_entry - add new entry to the lock tree.
126 * @ubi: UBI device description object
127 * @vol_id: volume ID
128 * @lnum: logical eraseblock number
129 *
130 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
131 * lock tree. If such entry is already there, its usage counter is increased.
132 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
133 * failed.
134 */
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200135static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
136 int vol_id, int lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400137{
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200138 struct ubi_ltree_entry *le, *le1, *le_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400139
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200140 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400141 if (!le)
142 return ERR_PTR(-ENOMEM);
143
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200144 le->users = 0;
145 init_rwsem(&le->mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146 le->vol_id = vol_id;
147 le->lnum = lnum;
148
149 spin_lock(&ubi->ltree_lock);
150 le1 = ltree_lookup(ubi, vol_id, lnum);
151
152 if (le1) {
153 /*
154 * This logical eraseblock is already locked. The newly
155 * allocated lock entry is not needed.
156 */
157 le_free = le;
158 le = le1;
159 } else {
160 struct rb_node **p, *parent = NULL;
161
162 /*
163 * No lock entry, add the newly allocated one to the
164 * @ubi->ltree RB-tree.
165 */
166 le_free = NULL;
167
168 p = &ubi->ltree.rb_node;
169 while (*p) {
170 parent = *p;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200171 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400172
173 if (vol_id < le1->vol_id)
174 p = &(*p)->rb_left;
175 else if (vol_id > le1->vol_id)
176 p = &(*p)->rb_right;
177 else {
178 ubi_assert(lnum != le1->lnum);
179 if (lnum < le1->lnum)
180 p = &(*p)->rb_left;
181 else
182 p = &(*p)->rb_right;
183 }
184 }
185
186 rb_link_node(&le->rb, parent, p);
187 rb_insert_color(&le->rb, &ubi->ltree);
188 }
189 le->users += 1;
190 spin_unlock(&ubi->ltree_lock);
191
192 if (le_free)
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200193 kfree(le_free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194
195 return le;
196}
197
198/**
199 * leb_read_lock - lock logical eraseblock for reading.
200 * @ubi: UBI device description object
201 * @vol_id: volume ID
202 * @lnum: logical eraseblock number
203 *
204 * This function locks a logical eraseblock for reading. Returns zero in case
205 * of success and a negative error code in case of failure.
206 */
207static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
208{
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200209 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400210
211 le = ltree_add_entry(ubi, vol_id, lnum);
212 if (IS_ERR(le))
213 return PTR_ERR(le);
214 down_read(&le->mutex);
215 return 0;
216}
217
218/**
219 * leb_read_unlock - unlock logical eraseblock.
220 * @ubi: UBI device description object
221 * @vol_id: volume ID
222 * @lnum: logical eraseblock number
223 */
224static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
225{
226 int free = 0;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200227 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400228
229 spin_lock(&ubi->ltree_lock);
230 le = ltree_lookup(ubi, vol_id, lnum);
231 le->users -= 1;
232 ubi_assert(le->users >= 0);
233 if (le->users == 0) {
234 rb_erase(&le->rb, &ubi->ltree);
235 free = 1;
236 }
237 spin_unlock(&ubi->ltree_lock);
238
239 up_read(&le->mutex);
240 if (free)
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200241 kfree(le);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400242}
243
244/**
245 * leb_write_lock - lock logical eraseblock for writing.
246 * @ubi: UBI device description object
247 * @vol_id: volume ID
248 * @lnum: logical eraseblock number
249 *
250 * This function locks a logical eraseblock for writing. Returns zero in case
251 * of success and a negative error code in case of failure.
252 */
253static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
254{
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200255 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400256
257 le = ltree_add_entry(ubi, vol_id, lnum);
258 if (IS_ERR(le))
259 return PTR_ERR(le);
260 down_write(&le->mutex);
261 return 0;
262}
263
264/**
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200265 * leb_write_lock - lock logical eraseblock for writing.
266 * @ubi: UBI device description object
267 * @vol_id: volume ID
268 * @lnum: logical eraseblock number
269 *
270 * This function locks a logical eraseblock for writing if there is no
271 * contention and does nothing if there is contention. Returns %0 in case of
272 * success, %1 in case of contention, and and a negative error code in case of
273 * failure.
274 */
275static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
276{
277 int free;
278 struct ubi_ltree_entry *le;
279
280 le = ltree_add_entry(ubi, vol_id, lnum);
281 if (IS_ERR(le))
282 return PTR_ERR(le);
283 if (down_write_trylock(&le->mutex))
284 return 0;
285
286 /* Contention, cancel */
287 spin_lock(&ubi->ltree_lock);
288 le->users -= 1;
289 ubi_assert(le->users >= 0);
290 if (le->users == 0) {
291 rb_erase(&le->rb, &ubi->ltree);
292 free = 1;
293 } else
294 free = 0;
295 spin_unlock(&ubi->ltree_lock);
296 if (free)
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200297 kfree(le);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200298
299 return 1;
300}
301
302/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400303 * leb_write_unlock - unlock logical eraseblock.
304 * @ubi: UBI device description object
305 * @vol_id: volume ID
306 * @lnum: logical eraseblock number
307 */
308static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
309{
310 int free;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200311 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400312
313 spin_lock(&ubi->ltree_lock);
314 le = ltree_lookup(ubi, vol_id, lnum);
315 le->users -= 1;
316 ubi_assert(le->users >= 0);
317 if (le->users == 0) {
318 rb_erase(&le->rb, &ubi->ltree);
319 free = 1;
320 } else
321 free = 0;
322 spin_unlock(&ubi->ltree_lock);
323
324 up_write(&le->mutex);
325 if (free)
Artem Bityutskiyb9a06622008-01-16 12:11:54 +0200326 kfree(le);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400327}
328
329/**
330 * ubi_eba_unmap_leb - un-map logical eraseblock.
331 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200332 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400333 * @lnum: logical eraseblock number
334 *
335 * This function un-maps logical eraseblock @lnum and schedules corresponding
336 * physical eraseblock for erasure. Returns zero in case of success and a
337 * negative error code in case of failure.
338 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200339int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
340 int lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400341{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200342 int err, pnum, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400343
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200344 ubi_assert(ubi->ref_count > 0);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200345 ubi_assert(vol->ref_count > 0);
346
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347 if (ubi->ro_mode)
348 return -EROFS;
349
350 err = leb_write_lock(ubi, vol_id, lnum);
351 if (err)
352 return err;
353
354 pnum = vol->eba_tbl[lnum];
355 if (pnum < 0)
356 /* This logical eraseblock is already unmapped */
357 goto out_unlock;
358
359 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
360
361 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
362 err = ubi_wl_put_peb(ubi, pnum, 0);
363
364out_unlock:
365 leb_write_unlock(ubi, vol_id, lnum);
366 return err;
367}
368
369/**
370 * ubi_eba_read_leb - read data.
371 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200372 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373 * @lnum: logical eraseblock number
374 * @buf: buffer to store the read data
375 * @offset: offset from where to read
376 * @len: how many bytes to read
377 * @check: data CRC check flag
378 *
379 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
380 * bytes. The @check flag only makes sense for static volumes and forces
381 * eraseblock data CRC checking.
382 *
383 * In case of success this function returns zero. In case of a static volume,
384 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
385 * returned for any volume type if an ECC error was detected by the MTD device
386 * driver. Other negative error cored may be returned in case of other errors.
387 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200388int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
389 void *buf, int offset, int len, int check)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200391 int err, pnum, scrub = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400392 struct ubi_vid_hdr *vid_hdr;
Jeff Garzika6343af2007-07-17 05:39:58 -0400393 uint32_t uninitialized_var(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400394
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200395 ubi_assert(ubi->ref_count > 0);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200396 ubi_assert(vol->ref_count > 0);
397
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 err = leb_read_lock(ubi, vol_id, lnum);
399 if (err)
400 return err;
401
402 pnum = vol->eba_tbl[lnum];
403 if (pnum < 0) {
404 /*
405 * The logical eraseblock is not mapped, fill the whole buffer
406 * with 0xFF bytes. The exception is static volumes for which
407 * it is an error to read unmapped logical eraseblocks.
408 */
409 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
410 len, offset, vol_id, lnum);
411 leb_read_unlock(ubi, vol_id, lnum);
412 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
413 memset(buf, 0xFF, len);
414 return 0;
415 }
416
417 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
418 len, offset, vol_id, lnum, pnum);
419
420 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
421 check = 0;
422
423retry:
424 if (check) {
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300425 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400426 if (!vid_hdr) {
427 err = -ENOMEM;
428 goto out_unlock;
429 }
430
431 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
432 if (err && err != UBI_IO_BITFLIPS) {
433 if (err > 0) {
434 /*
435 * The header is either absent or corrupted.
436 * The former case means there is a bug -
437 * switch to read-only mode just in case.
438 * The latter case means a real corruption - we
439 * may try to recover data. FIXME: but this is
440 * not implemented.
441 */
442 if (err == UBI_IO_BAD_VID_HDR) {
443 ubi_warn("bad VID header at PEB %d, LEB"
444 "%d:%d", pnum, vol_id, lnum);
445 err = -EBADMSG;
446 } else
447 ubi_ro_mode(ubi);
448 }
449 goto out_free;
450 } else if (err == UBI_IO_BITFLIPS)
451 scrub = 1;
452
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300453 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
454 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400455
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300456 crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400457 ubi_free_vid_hdr(ubi, vid_hdr);
458 }
459
460 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
461 if (err) {
462 if (err == UBI_IO_BITFLIPS) {
463 scrub = 1;
464 err = 0;
465 } else if (err == -EBADMSG) {
466 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
467 goto out_unlock;
468 scrub = 1;
469 if (!check) {
470 ubi_msg("force data checking");
471 check = 1;
472 goto retry;
473 }
474 } else
475 goto out_unlock;
476 }
477
478 if (check) {
Jeff Garzik2ab934b2007-07-17 01:49:56 -0400479 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400480 if (crc1 != crc) {
481 ubi_warn("CRC error: calculated %#08x, must be %#08x",
482 crc1, crc);
483 err = -EBADMSG;
484 goto out_unlock;
485 }
486 }
487
488 if (scrub)
489 err = ubi_wl_scrub_peb(ubi, pnum);
490
491 leb_read_unlock(ubi, vol_id, lnum);
492 return err;
493
494out_free:
495 ubi_free_vid_hdr(ubi, vid_hdr);
496out_unlock:
497 leb_read_unlock(ubi, vol_id, lnum);
498 return err;
499}
500
501/**
502 * recover_peb - recover from write failure.
503 * @ubi: UBI device description object
504 * @pnum: the physical eraseblock to recover
505 * @vol_id: volume ID
506 * @lnum: logical eraseblock number
507 * @buf: data which was not written because of the write failure
508 * @offset: offset of the failed write
509 * @len: how many bytes should have been written
510 *
511 * This function is called in case of a write failure and moves all good data
512 * from the potentially bad physical eraseblock to a good physical eraseblock.
513 * This function also writes the data which was not written due to the failure.
514 * Returns new physical eraseblock number in case of success, and a negative
515 * error code in case of failure.
516 */
517static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
518 const void *buf, int offset, int len)
519{
520 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
521 struct ubi_volume *vol = ubi->volumes[idx];
522 struct ubi_vid_hdr *vid_hdr;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400523
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300524 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400525 if (!vid_hdr) {
526 return -ENOMEM;
527 }
528
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300529 mutex_lock(&ubi->buf_mutex);
530
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400531retry:
532 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
533 if (new_pnum < 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300534 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400535 ubi_free_vid_hdr(ubi, vid_hdr);
536 return new_pnum;
537 }
538
539 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
540
541 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
542 if (err && err != UBI_IO_BITFLIPS) {
543 if (err > 0)
544 err = -EIO;
545 goto out_put;
546 }
547
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300548 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400549 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
550 if (err)
551 goto write_error;
552
553 data_size = offset + len;
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300554 memset(ubi->peb_buf1 + offset, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400555
556 /* Read everything before the area where the write failure happened */
557 if (offset > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300558 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
559 if (err && err != UBI_IO_BITFLIPS)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560 goto out_put;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400561 }
562
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300563 memcpy(ubi->peb_buf1 + offset, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400564
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300565 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
566 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400567 goto write_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300569 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400570 ubi_free_vid_hdr(ubi, vid_hdr);
571
572 vol->eba_tbl[lnum] = new_pnum;
573 ubi_wl_put_peb(ubi, pnum, 1);
574
575 ubi_msg("data was successfully recovered");
576 return 0;
577
578out_put:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300579 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400580 ubi_wl_put_peb(ubi, new_pnum, 1);
581 ubi_free_vid_hdr(ubi, vid_hdr);
582 return err;
583
584write_error:
585 /*
586 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
587 * get another one.
588 */
589 ubi_warn("failed to write to PEB %d", new_pnum);
590 ubi_wl_put_peb(ubi, new_pnum, 1);
591 if (++tries > UBI_IO_RETRIES) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300592 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400593 ubi_free_vid_hdr(ubi, vid_hdr);
594 return err;
595 }
596 ubi_msg("try again");
597 goto retry;
598}
599
600/**
601 * ubi_eba_write_leb - write data to dynamic volume.
602 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200603 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400604 * @lnum: logical eraseblock number
605 * @buf: the data to write
606 * @offset: offset within the logical eraseblock where to write
607 * @len: how many bytes to write
608 * @dtype: data type
609 *
610 * This function writes data to logical eraseblock @lnum of a dynamic volume
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200611 * @vol. Returns zero in case of success and a negative error code in case
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400612 * of failure. In case of error, it is possible that something was still
613 * written to the flash media, but may be some garbage.
614 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200615int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616 const void *buf, int offset, int len, int dtype)
617{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200618 int err, pnum, tries = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400619 struct ubi_vid_hdr *vid_hdr;
620
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200621 ubi_assert(ubi->ref_count > 0);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200622 ubi_assert(vol->ref_count > 0);
623
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400624 if (ubi->ro_mode)
625 return -EROFS;
626
627 err = leb_write_lock(ubi, vol_id, lnum);
628 if (err)
629 return err;
630
631 pnum = vol->eba_tbl[lnum];
632 if (pnum >= 0) {
633 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
634 len, offset, vol_id, lnum, pnum);
635
636 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
637 if (err) {
638 ubi_warn("failed to write data to PEB %d", pnum);
639 if (err == -EIO && ubi->bad_allowed)
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200640 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
641 offset, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400642 if (err)
643 ubi_ro_mode(ubi);
644 }
645 leb_write_unlock(ubi, vol_id, lnum);
646 return err;
647 }
648
649 /*
650 * The logical eraseblock is not mapped. We have to get a free physical
651 * eraseblock and write the volume identifier header there first.
652 */
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300653 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654 if (!vid_hdr) {
655 leb_write_unlock(ubi, vol_id, lnum);
656 return -ENOMEM;
657 }
658
659 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300660 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
661 vid_hdr->vol_id = cpu_to_be32(vol_id);
662 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300664 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400665
666retry:
667 pnum = ubi_wl_get_peb(ubi, dtype);
668 if (pnum < 0) {
669 ubi_free_vid_hdr(ubi, vid_hdr);
670 leb_write_unlock(ubi, vol_id, lnum);
671 return pnum;
672 }
673
674 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
675 len, offset, vol_id, lnum, pnum);
676
677 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
678 if (err) {
679 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
680 vol_id, lnum, pnum);
681 goto write_error;
682 }
683
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200684 if (len) {
685 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
686 if (err) {
687 ubi_warn("failed to write %d bytes at offset %d of "
688 "LEB %d:%d, PEB %d", len, offset, vol_id,
689 lnum, pnum);
690 goto write_error;
691 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400692 }
693
694 vol->eba_tbl[lnum] = pnum;
695
696 leb_write_unlock(ubi, vol_id, lnum);
697 ubi_free_vid_hdr(ubi, vid_hdr);
698 return 0;
699
700write_error:
701 if (err != -EIO || !ubi->bad_allowed) {
702 ubi_ro_mode(ubi);
703 leb_write_unlock(ubi, vol_id, lnum);
704 ubi_free_vid_hdr(ubi, vid_hdr);
705 return err;
706 }
707
708 /*
709 * Fortunately, this is the first write operation to this physical
710 * eraseblock, so just put it and request a new one. We assume that if
711 * this physical eraseblock went bad, the erase code will handle that.
712 */
713 err = ubi_wl_put_peb(ubi, pnum, 1);
714 if (err || ++tries > UBI_IO_RETRIES) {
715 ubi_ro_mode(ubi);
716 leb_write_unlock(ubi, vol_id, lnum);
717 ubi_free_vid_hdr(ubi, vid_hdr);
718 return err;
719 }
720
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300721 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400722 ubi_msg("try another PEB");
723 goto retry;
724}
725
726/**
727 * ubi_eba_write_leb_st - write data to static volume.
728 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200729 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400730 * @lnum: logical eraseblock number
731 * @buf: data to write
732 * @len: how many bytes to write
733 * @dtype: data type
734 * @used_ebs: how many logical eraseblocks will this volume contain
735 *
736 * This function writes data to logical eraseblock @lnum of static volume
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200737 * @vol. The @used_ebs argument should contain total number of logical
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400738 * eraseblock in this static volume.
739 *
740 * When writing to the last logical eraseblock, the @len argument doesn't have
741 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
742 * to the real data size, although the @buf buffer has to contain the
743 * alignment. In all other cases, @len has to be aligned.
744 *
745 * It is prohibited to write more then once to logical eraseblocks of static
746 * volumes. This function returns zero in case of success and a negative error
747 * code in case of failure.
748 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200749int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
750 int lnum, const void *buf, int len, int dtype,
751 int used_ebs)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400752{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200753 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400754 struct ubi_vid_hdr *vid_hdr;
755 uint32_t crc;
756
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200757 ubi_assert(ubi->ref_count > 0);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200758 ubi_assert(vol->ref_count > 0);
759
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400760 if (ubi->ro_mode)
761 return -EROFS;
762
763 if (lnum == used_ebs - 1)
764 /* If this is the last LEB @len may be unaligned */
765 len = ALIGN(data_size, ubi->min_io_size);
766 else
767 ubi_assert(len % ubi->min_io_size == 0);
768
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300769 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400770 if (!vid_hdr)
771 return -ENOMEM;
772
773 err = leb_write_lock(ubi, vol_id, lnum);
774 if (err) {
775 ubi_free_vid_hdr(ubi, vid_hdr);
776 return err;
777 }
778
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300779 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
780 vid_hdr->vol_id = cpu_to_be32(vol_id);
781 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400782 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300783 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400784
785 crc = crc32(UBI_CRC32_INIT, buf, data_size);
786 vid_hdr->vol_type = UBI_VID_STATIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300787 vid_hdr->data_size = cpu_to_be32(data_size);
788 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
789 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400790
791retry:
792 pnum = ubi_wl_get_peb(ubi, dtype);
793 if (pnum < 0) {
794 ubi_free_vid_hdr(ubi, vid_hdr);
795 leb_write_unlock(ubi, vol_id, lnum);
796 return pnum;
797 }
798
799 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
800 len, vol_id, lnum, pnum, used_ebs);
801
802 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
803 if (err) {
804 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
805 vol_id, lnum, pnum);
806 goto write_error;
807 }
808
809 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
810 if (err) {
811 ubi_warn("failed to write %d bytes of data to PEB %d",
812 len, pnum);
813 goto write_error;
814 }
815
816 ubi_assert(vol->eba_tbl[lnum] < 0);
817 vol->eba_tbl[lnum] = pnum;
818
819 leb_write_unlock(ubi, vol_id, lnum);
820 ubi_free_vid_hdr(ubi, vid_hdr);
821 return 0;
822
823write_error:
824 if (err != -EIO || !ubi->bad_allowed) {
825 /*
826 * This flash device does not admit of bad eraseblocks or
827 * something nasty and unexpected happened. Switch to read-only
828 * mode just in case.
829 */
830 ubi_ro_mode(ubi);
831 leb_write_unlock(ubi, vol_id, lnum);
832 ubi_free_vid_hdr(ubi, vid_hdr);
833 return err;
834 }
835
836 err = ubi_wl_put_peb(ubi, pnum, 1);
837 if (err || ++tries > UBI_IO_RETRIES) {
838 ubi_ro_mode(ubi);
839 leb_write_unlock(ubi, vol_id, lnum);
840 ubi_free_vid_hdr(ubi, vid_hdr);
841 return err;
842 }
843
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300844 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400845 ubi_msg("try another PEB");
846 goto retry;
847}
848
849/*
850 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
851 * @ubi: UBI device description object
Artem Bityutskiyc63a4912007-12-17 13:21:07 +0200852 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400853 * @lnum: logical eraseblock number
854 * @buf: data to write
855 * @len: how many bytes to write
856 * @dtype: data type
857 *
858 * This function changes the contents of a logical eraseblock atomically. @buf
859 * has to contain new logical eraseblock data, and @len - the length of the
860 * data, which has to be aligned. This function guarantees that in case of an
861 * unclean reboot the old contents is preserved. Returns zero in case of
862 * success and a negative error code in case of failure.
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300863 *
864 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
865 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400866 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200867int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
868 int lnum, const void *buf, int len, int dtype)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400869{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200870 int err, pnum, tries = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400871 struct ubi_vid_hdr *vid_hdr;
872 uint32_t crc;
873
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200874 ubi_assert(ubi->ref_count > 0);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200875 ubi_assert(vol->ref_count > 0);
876
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400877 if (ubi->ro_mode)
878 return -EROFS;
879
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300880 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400881 if (!vid_hdr)
882 return -ENOMEM;
883
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300884 mutex_lock(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400885 err = leb_write_lock(ubi, vol_id, lnum);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300886 if (err)
887 goto out_mutex;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400888
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300889 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
890 vid_hdr->vol_id = cpu_to_be32(vol_id);
891 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400892 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300893 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400894
895 crc = crc32(UBI_CRC32_INIT, buf, len);
Artem Bityutskiy84a92582007-07-04 16:16:51 +0300896 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300897 vid_hdr->data_size = cpu_to_be32(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400898 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300899 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400900
901retry:
902 pnum = ubi_wl_get_peb(ubi, dtype);
903 if (pnum < 0) {
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300904 err = pnum;
905 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400906 }
907
908 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
909 vol_id, lnum, vol->eba_tbl[lnum], pnum);
910
911 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
912 if (err) {
913 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
914 vol_id, lnum, pnum);
915 goto write_error;
916 }
917
918 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
919 if (err) {
920 ubi_warn("failed to write %d bytes of data to PEB %d",
921 len, pnum);
922 goto write_error;
923 }
924
Artem Bityutskiya443db42007-05-21 20:26:05 +0300925 if (vol->eba_tbl[lnum] >= 0) {
926 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300927 if (err)
928 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400929 }
930
931 vol->eba_tbl[lnum] = pnum;
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300932
933out_leb_unlock:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400934 leb_write_unlock(ubi, vol_id, lnum);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300935out_mutex:
936 mutex_unlock(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400937 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300938 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400939
940write_error:
941 if (err != -EIO || !ubi->bad_allowed) {
942 /*
943 * This flash device does not admit of bad eraseblocks or
944 * something nasty and unexpected happened. Switch to read-only
945 * mode just in case.
946 */
947 ubi_ro_mode(ubi);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300948 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400949 }
950
951 err = ubi_wl_put_peb(ubi, pnum, 1);
952 if (err || ++tries > UBI_IO_RETRIES) {
953 ubi_ro_mode(ubi);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300954 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400955 }
956
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300957 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400958 ubi_msg("try another PEB");
959 goto retry;
960}
961
962/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400963 * ubi_eba_copy_leb - copy logical eraseblock.
964 * @ubi: UBI device description object
965 * @from: physical eraseblock number from where to copy
966 * @to: physical eraseblock number where to copy
967 * @vid_hdr: VID header of the @from physical eraseblock
968 *
969 * This function copies logical eraseblock from physical eraseblock @from to
970 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200971 * function. Returns:
972 * o %0 in case of success;
973 * o %1 if the operation was canceled and should be tried later (e.g.,
974 * because a bit-flip was detected at the target PEB);
975 * o %2 if the volume is being deleted and this LEB should not be moved.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400976 */
977int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
978 struct ubi_vid_hdr *vid_hdr)
979{
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200980 int err, vol_id, lnum, data_size, aldata_size, idx;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400981 struct ubi_volume *vol;
982 uint32_t crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400983
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300984 vol_id = be32_to_cpu(vid_hdr->vol_id);
985 lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986
987 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
988
989 if (vid_hdr->vol_type == UBI_VID_STATIC) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300990 data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400991 aldata_size = ALIGN(data_size, ubi->min_io_size);
992 } else
993 data_size = aldata_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300994 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400995
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400996 idx = vol_id2idx(ubi, vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400997 spin_lock(&ubi->volumes_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200998 /*
999 * Note, we may race with volume deletion, which means that the volume
1000 * this logical eraseblock belongs to might be being deleted. Since the
1001 * volume deletion unmaps all the volume's logical eraseblocks, it will
1002 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1003 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001004 vol = ubi->volumes[idx];
1005 if (!vol) {
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001006 /* No need to do further work, cancel */
1007 dbg_eba("volume %d is being removed, cancel", vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001008 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001009 return 2;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001010 }
1011 spin_unlock(&ubi->volumes_lock);
1012
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001013 /*
1014 * We do not want anybody to write to this logical eraseblock while we
1015 * are moving it, so lock it.
1016 *
1017 * Note, we are using non-waiting locking here, because we cannot sleep
1018 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1019 * unmapping the LEB which is mapped to the PEB we are going to move
1020 * (@from). This task locks the LEB and goes sleep in the
1021 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1022 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1023 * LEB is already locked, we just do not move it and return %1.
1024 */
1025 err = leb_write_trylock(ubi, vol_id, lnum);
1026 if (err) {
1027 dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
1028 return err;
1029 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001030
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001031 /*
1032 * The LEB might have been put meanwhile, and the task which put it is
1033 * probably waiting on @ubi->move_mutex. No need to continue the work,
1034 * cancel it.
1035 */
1036 if (vol->eba_tbl[lnum] != from) {
1037 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1038 "PEB %d, cancel", vol_id, lnum, from,
1039 vol->eba_tbl[lnum]);
1040 err = 1;
1041 goto out_unlock_leb;
1042 }
1043
1044 /*
1045 * OK, now the LEB is locked and we can safely start moving iy. Since
1046 * this function utilizes thie @ubi->peb1_buf buffer which is shared
1047 * with some other functions, so lock the buffer by taking the
1048 * @ubi->buf_mutex.
1049 */
1050 mutex_lock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001051 dbg_eba("read %d bytes of data", aldata_size);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001052 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001053 if (err && err != UBI_IO_BITFLIPS) {
1054 ubi_warn("error %d while reading data from PEB %d",
1055 err, from);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001056 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001057 }
1058
1059 /*
1060 * Now we have got to calculate how much data we have to to copy. In
1061 * case of a static volume it is fairly easy - the VID header contains
1062 * the data size. In case of a dynamic volume it is more difficult - we
1063 * have to read the contents, cut 0xFF bytes from the end and copy only
1064 * the first part. We must do this to avoid writing 0xFF bytes as it
1065 * may have some side-effects. And not only this. It is important not
1066 * to include those 0xFFs to CRC because later the they may be filled
1067 * by data.
1068 */
1069 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1070 aldata_size = data_size =
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001071 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001072
1073 cond_resched();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001074 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001075 cond_resched();
1076
1077 /*
1078 * It may turn out to me that the whole @from physical eraseblock
1079 * contains only 0xFF bytes. Then we have to only write the VID header
1080 * and do not write any data. This also means we should not set
1081 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1082 */
1083 if (data_size > 0) {
1084 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001085 vid_hdr->data_size = cpu_to_be32(data_size);
1086 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001087 }
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001088 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001089
1090 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1091 if (err)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001092 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001093
1094 cond_resched();
1095
1096 /* Read the VID header back and check if it was written correctly */
1097 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1098 if (err) {
1099 if (err != UBI_IO_BITFLIPS)
1100 ubi_warn("cannot read VID header back from PEB %d", to);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001101 else
1102 err = 1;
1103 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001104 }
1105
1106 if (data_size > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001107 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001108 if (err)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001109 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001110
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001111 cond_resched();
1112
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001113 /*
1114 * We've written the data and are going to read it back to make
1115 * sure it was written correctly.
1116 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001117
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001118 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001119 if (err) {
1120 if (err != UBI_IO_BITFLIPS)
1121 ubi_warn("cannot read data back from PEB %d",
1122 to);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001123 else
1124 err = 1;
1125 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001126 }
1127
1128 cond_resched();
1129
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001130 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001131 ubi_warn("read data back from PEB %d - it is different",
1132 to);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001133 goto out_unlock_buf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001134 }
1135 }
1136
1137 ubi_assert(vol->eba_tbl[lnum] == from);
1138 vol->eba_tbl[lnum] = to;
1139
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001140out_unlock_buf:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001141 mutex_unlock(&ubi->buf_mutex);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001142out_unlock_leb:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001143 leb_write_unlock(ubi, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001144 return err;
1145}
1146
1147/**
1148 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1149 * @ubi: UBI device description object
1150 * @si: scanning information
1151 *
1152 * This function returns zero in case of success and a negative error code in
1153 * case of failure.
1154 */
1155int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1156{
1157 int i, j, err, num_volumes;
1158 struct ubi_scan_volume *sv;
1159 struct ubi_volume *vol;
1160 struct ubi_scan_leb *seb;
1161 struct rb_node *rb;
1162
1163 dbg_eba("initialize EBA unit");
1164
1165 spin_lock_init(&ubi->ltree_lock);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +03001166 mutex_init(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001167 ubi->ltree = RB_ROOT;
1168
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001169 ubi->global_sqnum = si->max_sqnum + 1;
1170 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1171
1172 for (i = 0; i < num_volumes; i++) {
1173 vol = ubi->volumes[i];
1174 if (!vol)
1175 continue;
1176
1177 cond_resched();
1178
1179 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1180 GFP_KERNEL);
1181 if (!vol->eba_tbl) {
1182 err = -ENOMEM;
1183 goto out_free;
1184 }
1185
1186 for (j = 0; j < vol->reserved_pebs; j++)
1187 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1188
1189 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1190 if (!sv)
1191 continue;
1192
1193 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1194 if (seb->lnum >= vol->reserved_pebs)
1195 /*
1196 * This may happen in case of an unclean reboot
1197 * during re-size.
1198 */
1199 ubi_scan_move_to_list(sv, seb, &si->erase);
1200 vol->eba_tbl[seb->lnum] = seb->pnum;
1201 }
1202 }
1203
Artem Bityutskiy94780d42007-12-04 21:36:12 +02001204 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1205 ubi_err("no enough physical eraseblocks (%d, need %d)",
1206 ubi->avail_pebs, EBA_RESERVED_PEBS);
1207 err = -ENOSPC;
1208 goto out_free;
1209 }
1210 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1211 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1212
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001213 if (ubi->bad_allowed) {
1214 ubi_calculate_reserved(ubi);
1215
1216 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1217 /* No enough free physical eraseblocks */
1218 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1219 ubi_warn("cannot reserve enough PEBs for bad PEB "
1220 "handling, reserved %d, need %d",
1221 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1222 } else
1223 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1224
1225 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1226 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1227 }
1228
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001229 dbg_eba("EBA unit is initialized");
1230 return 0;
1231
1232out_free:
1233 for (i = 0; i < num_volumes; i++) {
1234 if (!ubi->volumes[i])
1235 continue;
1236 kfree(ubi->volumes[i]->eba_tbl);
1237 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001238 return err;
1239}
1240
1241/**
1242 * ubi_eba_close - close EBA unit.
1243 * @ubi: UBI device description object
1244 */
1245void ubi_eba_close(const struct ubi_device *ubi)
1246{
1247 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1248
1249 dbg_eba("close EBA unit");
1250
1251 for (i = 0; i < num_volumes; i++) {
1252 if (!ubi->volumes[i])
1253 continue;
1254 kfree(ubi->volumes[i]->eba_tbl);
1255 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001256}