blob: 2ff34923e51d74d91d319a52da06e041cae6ef3b [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 Bityutskiy3a8d4642007-12-16 12:32:51 +0200140 le = kmem_cache_alloc(ubi_ltree_slab, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400141 if (!le)
142 return ERR_PTR(-ENOMEM);
143
144 le->vol_id = vol_id;
145 le->lnum = lnum;
146
147 spin_lock(&ubi->ltree_lock);
148 le1 = ltree_lookup(ubi, vol_id, lnum);
149
150 if (le1) {
151 /*
152 * This logical eraseblock is already locked. The newly
153 * allocated lock entry is not needed.
154 */
155 le_free = le;
156 le = le1;
157 } else {
158 struct rb_node **p, *parent = NULL;
159
160 /*
161 * No lock entry, add the newly allocated one to the
162 * @ubi->ltree RB-tree.
163 */
164 le_free = NULL;
165
166 p = &ubi->ltree.rb_node;
167 while (*p) {
168 parent = *p;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200169 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400170
171 if (vol_id < le1->vol_id)
172 p = &(*p)->rb_left;
173 else if (vol_id > le1->vol_id)
174 p = &(*p)->rb_right;
175 else {
176 ubi_assert(lnum != le1->lnum);
177 if (lnum < le1->lnum)
178 p = &(*p)->rb_left;
179 else
180 p = &(*p)->rb_right;
181 }
182 }
183
184 rb_link_node(&le->rb, parent, p);
185 rb_insert_color(&le->rb, &ubi->ltree);
186 }
187 le->users += 1;
188 spin_unlock(&ubi->ltree_lock);
189
190 if (le_free)
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200191 kmem_cache_free(ubi_ltree_slab, le_free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400192
193 return le;
194}
195
196/**
197 * leb_read_lock - lock logical eraseblock for reading.
198 * @ubi: UBI device description object
199 * @vol_id: volume ID
200 * @lnum: logical eraseblock number
201 *
202 * This function locks a logical eraseblock for reading. Returns zero in case
203 * of success and a negative error code in case of failure.
204 */
205static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
206{
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200207 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208
209 le = ltree_add_entry(ubi, vol_id, lnum);
210 if (IS_ERR(le))
211 return PTR_ERR(le);
212 down_read(&le->mutex);
213 return 0;
214}
215
216/**
217 * leb_read_unlock - unlock logical eraseblock.
218 * @ubi: UBI device description object
219 * @vol_id: volume ID
220 * @lnum: logical eraseblock number
221 */
222static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
223{
224 int free = 0;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200225 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400226
227 spin_lock(&ubi->ltree_lock);
228 le = ltree_lookup(ubi, vol_id, lnum);
229 le->users -= 1;
230 ubi_assert(le->users >= 0);
231 if (le->users == 0) {
232 rb_erase(&le->rb, &ubi->ltree);
233 free = 1;
234 }
235 spin_unlock(&ubi->ltree_lock);
236
237 up_read(&le->mutex);
238 if (free)
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200239 kmem_cache_free(ubi_ltree_slab, le);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400240}
241
242/**
243 * leb_write_lock - lock logical eraseblock for writing.
244 * @ubi: UBI device description object
245 * @vol_id: volume ID
246 * @lnum: logical eraseblock number
247 *
248 * This function locks a logical eraseblock for writing. Returns zero in case
249 * of success and a negative error code in case of failure.
250 */
251static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
252{
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200253 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400254
255 le = ltree_add_entry(ubi, vol_id, lnum);
256 if (IS_ERR(le))
257 return PTR_ERR(le);
258 down_write(&le->mutex);
259 return 0;
260}
261
262/**
263 * leb_write_unlock - unlock logical eraseblock.
264 * @ubi: UBI device description object
265 * @vol_id: volume ID
266 * @lnum: logical eraseblock number
267 */
268static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
269{
270 int free;
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200271 struct ubi_ltree_entry *le;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272
273 spin_lock(&ubi->ltree_lock);
274 le = ltree_lookup(ubi, vol_id, lnum);
275 le->users -= 1;
276 ubi_assert(le->users >= 0);
277 if (le->users == 0) {
278 rb_erase(&le->rb, &ubi->ltree);
279 free = 1;
280 } else
281 free = 0;
282 spin_unlock(&ubi->ltree_lock);
283
284 up_write(&le->mutex);
285 if (free)
Artem Bityutskiy3a8d4642007-12-16 12:32:51 +0200286 kmem_cache_free(ubi_ltree_slab, le);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400287}
288
289/**
290 * ubi_eba_unmap_leb - un-map logical eraseblock.
291 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200292 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400293 * @lnum: logical eraseblock number
294 *
295 * This function un-maps logical eraseblock @lnum and schedules corresponding
296 * physical eraseblock for erasure. Returns zero in case of success and a
297 * negative error code in case of failure.
298 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200299int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
300 int lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400301{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200302 int err, pnum, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400303
304 if (ubi->ro_mode)
305 return -EROFS;
306
307 err = leb_write_lock(ubi, vol_id, lnum);
308 if (err)
309 return err;
310
311 pnum = vol->eba_tbl[lnum];
312 if (pnum < 0)
313 /* This logical eraseblock is already unmapped */
314 goto out_unlock;
315
316 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
317
318 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
319 err = ubi_wl_put_peb(ubi, pnum, 0);
320
321out_unlock:
322 leb_write_unlock(ubi, vol_id, lnum);
323 return err;
324}
325
326/**
327 * ubi_eba_read_leb - read data.
328 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200329 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400330 * @lnum: logical eraseblock number
331 * @buf: buffer to store the read data
332 * @offset: offset from where to read
333 * @len: how many bytes to read
334 * @check: data CRC check flag
335 *
336 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
337 * bytes. The @check flag only makes sense for static volumes and forces
338 * eraseblock data CRC checking.
339 *
340 * In case of success this function returns zero. In case of a static volume,
341 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
342 * returned for any volume type if an ECC error was detected by the MTD device
343 * driver. Other negative error cored may be returned in case of other errors.
344 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200345int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
346 void *buf, int offset, int len, int check)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200348 int err, pnum, scrub = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400349 struct ubi_vid_hdr *vid_hdr;
Jeff Garzika6343af2007-07-17 05:39:58 -0400350 uint32_t uninitialized_var(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400351
352 err = leb_read_lock(ubi, vol_id, lnum);
353 if (err)
354 return err;
355
356 pnum = vol->eba_tbl[lnum];
357 if (pnum < 0) {
358 /*
359 * The logical eraseblock is not mapped, fill the whole buffer
360 * with 0xFF bytes. The exception is static volumes for which
361 * it is an error to read unmapped logical eraseblocks.
362 */
363 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
364 len, offset, vol_id, lnum);
365 leb_read_unlock(ubi, vol_id, lnum);
366 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
367 memset(buf, 0xFF, len);
368 return 0;
369 }
370
371 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
372 len, offset, vol_id, lnum, pnum);
373
374 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
375 check = 0;
376
377retry:
378 if (check) {
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300379 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380 if (!vid_hdr) {
381 err = -ENOMEM;
382 goto out_unlock;
383 }
384
385 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
386 if (err && err != UBI_IO_BITFLIPS) {
387 if (err > 0) {
388 /*
389 * The header is either absent or corrupted.
390 * The former case means there is a bug -
391 * switch to read-only mode just in case.
392 * The latter case means a real corruption - we
393 * may try to recover data. FIXME: but this is
394 * not implemented.
395 */
396 if (err == UBI_IO_BAD_VID_HDR) {
397 ubi_warn("bad VID header at PEB %d, LEB"
398 "%d:%d", pnum, vol_id, lnum);
399 err = -EBADMSG;
400 } else
401 ubi_ro_mode(ubi);
402 }
403 goto out_free;
404 } else if (err == UBI_IO_BITFLIPS)
405 scrub = 1;
406
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300407 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
408 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300410 crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400411 ubi_free_vid_hdr(ubi, vid_hdr);
412 }
413
414 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
415 if (err) {
416 if (err == UBI_IO_BITFLIPS) {
417 scrub = 1;
418 err = 0;
419 } else if (err == -EBADMSG) {
420 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
421 goto out_unlock;
422 scrub = 1;
423 if (!check) {
424 ubi_msg("force data checking");
425 check = 1;
426 goto retry;
427 }
428 } else
429 goto out_unlock;
430 }
431
432 if (check) {
Jeff Garzik2ab934b2007-07-17 01:49:56 -0400433 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400434 if (crc1 != crc) {
435 ubi_warn("CRC error: calculated %#08x, must be %#08x",
436 crc1, crc);
437 err = -EBADMSG;
438 goto out_unlock;
439 }
440 }
441
442 if (scrub)
443 err = ubi_wl_scrub_peb(ubi, pnum);
444
445 leb_read_unlock(ubi, vol_id, lnum);
446 return err;
447
448out_free:
449 ubi_free_vid_hdr(ubi, vid_hdr);
450out_unlock:
451 leb_read_unlock(ubi, vol_id, lnum);
452 return err;
453}
454
455/**
456 * recover_peb - recover from write failure.
457 * @ubi: UBI device description object
458 * @pnum: the physical eraseblock to recover
459 * @vol_id: volume ID
460 * @lnum: logical eraseblock number
461 * @buf: data which was not written because of the write failure
462 * @offset: offset of the failed write
463 * @len: how many bytes should have been written
464 *
465 * This function is called in case of a write failure and moves all good data
466 * from the potentially bad physical eraseblock to a good physical eraseblock.
467 * This function also writes the data which was not written due to the failure.
468 * Returns new physical eraseblock number in case of success, and a negative
469 * error code in case of failure.
470 */
471static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
472 const void *buf, int offset, int len)
473{
474 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
475 struct ubi_volume *vol = ubi->volumes[idx];
476 struct ubi_vid_hdr *vid_hdr;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400477
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300478 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400479 if (!vid_hdr) {
480 return -ENOMEM;
481 }
482
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300483 mutex_lock(&ubi->buf_mutex);
484
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400485retry:
486 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
487 if (new_pnum < 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300488 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400489 ubi_free_vid_hdr(ubi, vid_hdr);
490 return new_pnum;
491 }
492
493 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
494
495 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
496 if (err && err != UBI_IO_BITFLIPS) {
497 if (err > 0)
498 err = -EIO;
499 goto out_put;
500 }
501
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300502 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400503 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
504 if (err)
505 goto write_error;
506
507 data_size = offset + len;
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300508 memset(ubi->peb_buf1 + offset, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400509
510 /* Read everything before the area where the write failure happened */
511 if (offset > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300512 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
513 if (err && err != UBI_IO_BITFLIPS)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400514 goto out_put;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 }
516
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300517 memcpy(ubi->peb_buf1 + offset, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400518
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300519 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
520 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400521 goto write_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400522
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300523 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400524 ubi_free_vid_hdr(ubi, vid_hdr);
525
526 vol->eba_tbl[lnum] = new_pnum;
527 ubi_wl_put_peb(ubi, pnum, 1);
528
529 ubi_msg("data was successfully recovered");
530 return 0;
531
532out_put:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300533 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400534 ubi_wl_put_peb(ubi, new_pnum, 1);
535 ubi_free_vid_hdr(ubi, vid_hdr);
536 return err;
537
538write_error:
539 /*
540 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
541 * get another one.
542 */
543 ubi_warn("failed to write to PEB %d", new_pnum);
544 ubi_wl_put_peb(ubi, new_pnum, 1);
545 if (++tries > UBI_IO_RETRIES) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300546 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400547 ubi_free_vid_hdr(ubi, vid_hdr);
548 return err;
549 }
550 ubi_msg("try again");
551 goto retry;
552}
553
554/**
555 * ubi_eba_write_leb - write data to dynamic volume.
556 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200557 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400558 * @lnum: logical eraseblock number
559 * @buf: the data to write
560 * @offset: offset within the logical eraseblock where to write
561 * @len: how many bytes to write
562 * @dtype: data type
563 *
564 * This function writes data to logical eraseblock @lnum of a dynamic volume
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200565 * @vol. Returns zero in case of success and a negative error code in case
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400566 * of failure. In case of error, it is possible that something was still
567 * written to the flash media, but may be some garbage.
568 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200569int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400570 const void *buf, int offset, int len, int dtype)
571{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200572 int err, pnum, tries = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400573 struct ubi_vid_hdr *vid_hdr;
574
575 if (ubi->ro_mode)
576 return -EROFS;
577
578 err = leb_write_lock(ubi, vol_id, lnum);
579 if (err)
580 return err;
581
582 pnum = vol->eba_tbl[lnum];
583 if (pnum >= 0) {
584 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
585 len, offset, vol_id, lnum, pnum);
586
587 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
588 if (err) {
589 ubi_warn("failed to write data to PEB %d", pnum);
590 if (err == -EIO && ubi->bad_allowed)
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200591 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
592 offset, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400593 if (err)
594 ubi_ro_mode(ubi);
595 }
596 leb_write_unlock(ubi, vol_id, lnum);
597 return err;
598 }
599
600 /*
601 * The logical eraseblock is not mapped. We have to get a free physical
602 * eraseblock and write the volume identifier header there first.
603 */
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300604 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400605 if (!vid_hdr) {
606 leb_write_unlock(ubi, vol_id, lnum);
607 return -ENOMEM;
608 }
609
610 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300611 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
612 vid_hdr->vol_id = cpu_to_be32(vol_id);
613 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400614 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300615 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616
617retry:
618 pnum = ubi_wl_get_peb(ubi, dtype);
619 if (pnum < 0) {
620 ubi_free_vid_hdr(ubi, vid_hdr);
621 leb_write_unlock(ubi, vol_id, lnum);
622 return pnum;
623 }
624
625 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
626 len, offset, vol_id, lnum, pnum);
627
628 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
629 if (err) {
630 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
631 vol_id, lnum, pnum);
632 goto write_error;
633 }
634
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200635 if (len) {
636 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
637 if (err) {
638 ubi_warn("failed to write %d bytes at offset %d of "
639 "LEB %d:%d, PEB %d", len, offset, vol_id,
640 lnum, pnum);
641 goto write_error;
642 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400643 }
644
645 vol->eba_tbl[lnum] = pnum;
646
647 leb_write_unlock(ubi, vol_id, lnum);
648 ubi_free_vid_hdr(ubi, vid_hdr);
649 return 0;
650
651write_error:
652 if (err != -EIO || !ubi->bad_allowed) {
653 ubi_ro_mode(ubi);
654 leb_write_unlock(ubi, vol_id, lnum);
655 ubi_free_vid_hdr(ubi, vid_hdr);
656 return err;
657 }
658
659 /*
660 * Fortunately, this is the first write operation to this physical
661 * eraseblock, so just put it and request a new one. We assume that if
662 * this physical eraseblock went bad, the erase code will handle that.
663 */
664 err = ubi_wl_put_peb(ubi, pnum, 1);
665 if (err || ++tries > UBI_IO_RETRIES) {
666 ubi_ro_mode(ubi);
667 leb_write_unlock(ubi, vol_id, lnum);
668 ubi_free_vid_hdr(ubi, vid_hdr);
669 return err;
670 }
671
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300672 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400673 ubi_msg("try another PEB");
674 goto retry;
675}
676
677/**
678 * ubi_eba_write_leb_st - write data to static volume.
679 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200680 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681 * @lnum: logical eraseblock number
682 * @buf: data to write
683 * @len: how many bytes to write
684 * @dtype: data type
685 * @used_ebs: how many logical eraseblocks will this volume contain
686 *
687 * This function writes data to logical eraseblock @lnum of static volume
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200688 * @vol. The @used_ebs argument should contain total number of logical
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400689 * eraseblock in this static volume.
690 *
691 * When writing to the last logical eraseblock, the @len argument doesn't have
692 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
693 * to the real data size, although the @buf buffer has to contain the
694 * alignment. In all other cases, @len has to be aligned.
695 *
696 * It is prohibited to write more then once to logical eraseblocks of static
697 * volumes. This function returns zero in case of success and a negative error
698 * code in case of failure.
699 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200700int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
701 int lnum, const void *buf, int len, int dtype,
702 int used_ebs)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400703{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200704 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400705 struct ubi_vid_hdr *vid_hdr;
706 uint32_t crc;
707
708 if (ubi->ro_mode)
709 return -EROFS;
710
711 if (lnum == used_ebs - 1)
712 /* If this is the last LEB @len may be unaligned */
713 len = ALIGN(data_size, ubi->min_io_size);
714 else
715 ubi_assert(len % ubi->min_io_size == 0);
716
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300717 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400718 if (!vid_hdr)
719 return -ENOMEM;
720
721 err = leb_write_lock(ubi, vol_id, lnum);
722 if (err) {
723 ubi_free_vid_hdr(ubi, vid_hdr);
724 return err;
725 }
726
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300727 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
728 vid_hdr->vol_id = cpu_to_be32(vol_id);
729 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400730 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300731 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400732
733 crc = crc32(UBI_CRC32_INIT, buf, data_size);
734 vid_hdr->vol_type = UBI_VID_STATIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300735 vid_hdr->data_size = cpu_to_be32(data_size);
736 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
737 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400738
739retry:
740 pnum = ubi_wl_get_peb(ubi, dtype);
741 if (pnum < 0) {
742 ubi_free_vid_hdr(ubi, vid_hdr);
743 leb_write_unlock(ubi, vol_id, lnum);
744 return pnum;
745 }
746
747 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
748 len, vol_id, lnum, pnum, used_ebs);
749
750 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
751 if (err) {
752 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
753 vol_id, lnum, pnum);
754 goto write_error;
755 }
756
757 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
758 if (err) {
759 ubi_warn("failed to write %d bytes of data to PEB %d",
760 len, pnum);
761 goto write_error;
762 }
763
764 ubi_assert(vol->eba_tbl[lnum] < 0);
765 vol->eba_tbl[lnum] = pnum;
766
767 leb_write_unlock(ubi, vol_id, lnum);
768 ubi_free_vid_hdr(ubi, vid_hdr);
769 return 0;
770
771write_error:
772 if (err != -EIO || !ubi->bad_allowed) {
773 /*
774 * This flash device does not admit of bad eraseblocks or
775 * something nasty and unexpected happened. Switch to read-only
776 * mode just in case.
777 */
778 ubi_ro_mode(ubi);
779 leb_write_unlock(ubi, vol_id, lnum);
780 ubi_free_vid_hdr(ubi, vid_hdr);
781 return err;
782 }
783
784 err = ubi_wl_put_peb(ubi, pnum, 1);
785 if (err || ++tries > UBI_IO_RETRIES) {
786 ubi_ro_mode(ubi);
787 leb_write_unlock(ubi, vol_id, lnum);
788 ubi_free_vid_hdr(ubi, vid_hdr);
789 return err;
790 }
791
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300792 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400793 ubi_msg("try another PEB");
794 goto retry;
795}
796
797/*
798 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
799 * @ubi: UBI device description object
Artem Bityutskiyc63a4912007-12-17 13:21:07 +0200800 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400801 * @lnum: logical eraseblock number
802 * @buf: data to write
803 * @len: how many bytes to write
804 * @dtype: data type
805 *
806 * This function changes the contents of a logical eraseblock atomically. @buf
807 * has to contain new logical eraseblock data, and @len - the length of the
808 * data, which has to be aligned. This function guarantees that in case of an
809 * unclean reboot the old contents is preserved. Returns zero in case of
810 * success and a negative error code in case of failure.
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300811 *
812 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
813 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400814 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200815int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
816 int lnum, const void *buf, int len, int dtype)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400817{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200818 int err, pnum, tries = 0, vol_id = vol->vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400819 struct ubi_vid_hdr *vid_hdr;
820 uint32_t crc;
821
822 if (ubi->ro_mode)
823 return -EROFS;
824
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300825 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400826 if (!vid_hdr)
827 return -ENOMEM;
828
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300829 mutex_lock(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400830 err = leb_write_lock(ubi, vol_id, lnum);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300831 if (err)
832 goto out_mutex;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300834 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
835 vid_hdr->vol_id = cpu_to_be32(vol_id);
836 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400837 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300838 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400839
840 crc = crc32(UBI_CRC32_INIT, buf, len);
Artem Bityutskiy84a92582007-07-04 16:16:51 +0300841 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300842 vid_hdr->data_size = cpu_to_be32(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400843 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300844 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400845
846retry:
847 pnum = ubi_wl_get_peb(ubi, dtype);
848 if (pnum < 0) {
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300849 err = pnum;
850 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400851 }
852
853 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
854 vol_id, lnum, vol->eba_tbl[lnum], pnum);
855
856 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
857 if (err) {
858 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
859 vol_id, lnum, pnum);
860 goto write_error;
861 }
862
863 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
864 if (err) {
865 ubi_warn("failed to write %d bytes of data to PEB %d",
866 len, pnum);
867 goto write_error;
868 }
869
Artem Bityutskiya443db42007-05-21 20:26:05 +0300870 if (vol->eba_tbl[lnum] >= 0) {
871 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300872 if (err)
873 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400874 }
875
876 vol->eba_tbl[lnum] = pnum;
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300877
878out_leb_unlock:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400879 leb_write_unlock(ubi, vol_id, lnum);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300880out_mutex:
881 mutex_unlock(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400882 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300883 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400884
885write_error:
886 if (err != -EIO || !ubi->bad_allowed) {
887 /*
888 * This flash device does not admit of bad eraseblocks or
889 * something nasty and unexpected happened. Switch to read-only
890 * mode just in case.
891 */
892 ubi_ro_mode(ubi);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300893 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400894 }
895
896 err = ubi_wl_put_peb(ubi, pnum, 1);
897 if (err || ++tries > UBI_IO_RETRIES) {
898 ubi_ro_mode(ubi);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +0300899 goto out_leb_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400900 }
901
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300902 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400903 ubi_msg("try another PEB");
904 goto retry;
905}
906
907/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400908 * ubi_eba_copy_leb - copy logical eraseblock.
909 * @ubi: UBI device description object
910 * @from: physical eraseblock number from where to copy
911 * @to: physical eraseblock number where to copy
912 * @vid_hdr: VID header of the @from physical eraseblock
913 *
914 * This function copies logical eraseblock from physical eraseblock @from to
915 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
916 * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
917 * was canceled because bit-flips were detected at the target PEB, and a
918 * negative error code in case of failure.
919 */
920int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
921 struct ubi_vid_hdr *vid_hdr)
922{
923 int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
924 struct ubi_volume *vol;
925 uint32_t crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400926
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300927 vol_id = be32_to_cpu(vid_hdr->vol_id);
928 lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400929
930 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
931
932 if (vid_hdr->vol_type == UBI_VID_STATIC) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300933 data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400934 aldata_size = ALIGN(data_size, ubi->min_io_size);
935 } else
936 data_size = aldata_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300937 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400938
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400939 /*
940 * We do not want anybody to write to this logical eraseblock while we
941 * are moving it, so we lock it.
942 */
943 err = leb_write_lock(ubi, vol_id, lnum);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300944 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400945 return err;
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300946
947 mutex_lock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400948
949 /*
950 * But the logical eraseblock might have been put by this time.
951 * Cancel if it is true.
952 */
953 idx = vol_id2idx(ubi, vol_id);
954
955 /*
956 * We may race with volume deletion/re-size, so we have to hold
957 * @ubi->volumes_lock.
Artem Bityutskiyc63a4912007-12-17 13:21:07 +0200958 *
959 * Note, it is not a problem if we race with volume deletion or re-size
960 * here. If the volume is deleted or re-sized while we are moving an
961 * eraseblock which belongs to this volume, we'll end up with finding
962 * out that this LEB was unmapped at the end (see WL), and drop this
963 * PEB.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400964 */
965 spin_lock(&ubi->volumes_lock);
966 vol = ubi->volumes[idx];
967 if (!vol) {
968 dbg_eba("volume %d was removed meanwhile", vol_id);
969 spin_unlock(&ubi->volumes_lock);
970 goto out_unlock;
971 }
972
973 pnum = vol->eba_tbl[lnum];
974 if (pnum != from) {
975 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
976 "PEB %d, cancel", vol_id, lnum, from, pnum);
977 spin_unlock(&ubi->volumes_lock);
978 goto out_unlock;
979 }
980 spin_unlock(&ubi->volumes_lock);
981
982 /* OK, now the LEB is locked and we can safely start moving it */
983
984 dbg_eba("read %d bytes of data", aldata_size);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300985 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986 if (err && err != UBI_IO_BITFLIPS) {
987 ubi_warn("error %d while reading data from PEB %d",
988 err, from);
989 goto out_unlock;
990 }
991
992 /*
993 * Now we have got to calculate how much data we have to to copy. In
994 * case of a static volume it is fairly easy - the VID header contains
995 * the data size. In case of a dynamic volume it is more difficult - we
996 * have to read the contents, cut 0xFF bytes from the end and copy only
997 * the first part. We must do this to avoid writing 0xFF bytes as it
998 * may have some side-effects. And not only this. It is important not
999 * to include those 0xFFs to CRC because later the they may be filled
1000 * by data.
1001 */
1002 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1003 aldata_size = data_size =
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001004 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001005
1006 cond_resched();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001007 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001008 cond_resched();
1009
1010 /*
1011 * It may turn out to me that the whole @from physical eraseblock
1012 * contains only 0xFF bytes. Then we have to only write the VID header
1013 * and do not write any data. This also means we should not set
1014 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1015 */
1016 if (data_size > 0) {
1017 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001018 vid_hdr->data_size = cpu_to_be32(data_size);
1019 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001020 }
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001021 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001022
1023 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1024 if (err)
1025 goto out_unlock;
1026
1027 cond_resched();
1028
1029 /* Read the VID header back and check if it was written correctly */
1030 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1031 if (err) {
1032 if (err != UBI_IO_BITFLIPS)
1033 ubi_warn("cannot read VID header back from PEB %d", to);
1034 goto out_unlock;
1035 }
1036
1037 if (data_size > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001038 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001039 if (err)
1040 goto out_unlock;
1041
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001042 cond_resched();
1043
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001044 /*
1045 * We've written the data and are going to read it back to make
1046 * sure it was written correctly.
1047 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001048
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001049 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001050 if (err) {
1051 if (err != UBI_IO_BITFLIPS)
1052 ubi_warn("cannot read data back from PEB %d",
1053 to);
1054 goto out_unlock;
1055 }
1056
1057 cond_resched();
1058
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001059 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001060 ubi_warn("read data back from PEB %d - it is different",
1061 to);
1062 goto out_unlock;
1063 }
1064 }
1065
1066 ubi_assert(vol->eba_tbl[lnum] == from);
1067 vol->eba_tbl[lnum] = to;
1068
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001069out_unlock:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001070 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001071 leb_write_unlock(ubi, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001072 return err;
1073}
1074
1075/**
1076 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1077 * @ubi: UBI device description object
1078 * @si: scanning information
1079 *
1080 * This function returns zero in case of success and a negative error code in
1081 * case of failure.
1082 */
1083int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1084{
1085 int i, j, err, num_volumes;
1086 struct ubi_scan_volume *sv;
1087 struct ubi_volume *vol;
1088 struct ubi_scan_leb *seb;
1089 struct rb_node *rb;
1090
1091 dbg_eba("initialize EBA unit");
1092
1093 spin_lock_init(&ubi->ltree_lock);
Artem Bityutskiye8823bd2007-09-13 14:28:14 +03001094 mutex_init(&ubi->alc_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001095 ubi->ltree = RB_ROOT;
1096
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001097 ubi->global_sqnum = si->max_sqnum + 1;
1098 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1099
1100 for (i = 0; i < num_volumes; i++) {
1101 vol = ubi->volumes[i];
1102 if (!vol)
1103 continue;
1104
1105 cond_resched();
1106
1107 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1108 GFP_KERNEL);
1109 if (!vol->eba_tbl) {
1110 err = -ENOMEM;
1111 goto out_free;
1112 }
1113
1114 for (j = 0; j < vol->reserved_pebs; j++)
1115 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1116
1117 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1118 if (!sv)
1119 continue;
1120
1121 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1122 if (seb->lnum >= vol->reserved_pebs)
1123 /*
1124 * This may happen in case of an unclean reboot
1125 * during re-size.
1126 */
1127 ubi_scan_move_to_list(sv, seb, &si->erase);
1128 vol->eba_tbl[seb->lnum] = seb->pnum;
1129 }
1130 }
1131
Artem Bityutskiy94780d42007-12-04 21:36:12 +02001132 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1133 ubi_err("no enough physical eraseblocks (%d, need %d)",
1134 ubi->avail_pebs, EBA_RESERVED_PEBS);
1135 err = -ENOSPC;
1136 goto out_free;
1137 }
1138 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1139 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1140
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001141 if (ubi->bad_allowed) {
1142 ubi_calculate_reserved(ubi);
1143
1144 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1145 /* No enough free physical eraseblocks */
1146 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1147 ubi_warn("cannot reserve enough PEBs for bad PEB "
1148 "handling, reserved %d, need %d",
1149 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1150 } else
1151 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1152
1153 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1154 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1155 }
1156
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001157 dbg_eba("EBA unit is initialized");
1158 return 0;
1159
1160out_free:
1161 for (i = 0; i < num_volumes; i++) {
1162 if (!ubi->volumes[i])
1163 continue;
1164 kfree(ubi->volumes[i]->eba_tbl);
1165 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001166 return err;
1167}
1168
1169/**
1170 * ubi_eba_close - close EBA unit.
1171 * @ubi: UBI device description object
1172 */
1173void ubi_eba_close(const struct ubi_device *ubi)
1174{
1175 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1176
1177 dbg_eba("close EBA unit");
1178
1179 for (i = 0; i < num_volumes; i++) {
1180 if (!ubi->volumes[i])
1181 continue;
1182 kfree(ubi->volumes[i]->eba_tbl);
1183 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001184}