blob: bdb41689837c11f229f53433ee333715a3756ad5 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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) sub-system.
23 *
24 * This sub-system 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 sub-system implements per-logical eraseblock locking. Before
31 * accessing a logical eraseblock it is locked for reading or writing. The
32 * per-logical eraseblock locking is implemented by means of the lock tree. The
33 * lock tree is an RB-tree which refers all the currently locked logical
34 * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
35 * They are indexed by (@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
49/* Number of physical eraseblocks reserved for atomic LEB change operation */
50#define EBA_RESERVED_PEBS 1
51
52/**
53 * 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_VOLUME_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 *
92 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
93 * object if the logical eraseblock is locked and %NULL if it is not.
94 * @ubi->ltree_lock has to be locked.
95 */
96static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
97 int lnum)
98{
99 struct rb_node *p;
100
101 p = ubi->ltree.rb_node;
102 while (p) {
103 struct ubi_ltree_entry *le;
104
105 le = rb_entry(p, struct ubi_ltree_entry, rb);
106
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 */
135static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
136 int vol_id, int lnum)
137{
138 struct ubi_ltree_entry *le, *le1, *le_free;
139
140 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
141 if (!le)
142 return ERR_PTR(-ENOMEM);
143
144 le->users = 0;
145 init_rwsem(&le->mutex);
146 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;
171 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
172
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 kfree(le_free);
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{
207 struct ubi_ltree_entry *le;
208
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 struct ubi_ltree_entry *le;
225
226 spin_lock(&ubi->ltree_lock);
227 le = ltree_lookup(ubi, vol_id, lnum);
228 if(le == NULL){
229 spin_unlock(&ubi->ltree_lock);
230 return;
231 }
232 le->users -= 1;
233 ubi_assert(le->users >= 0);
234 up_read(&le->mutex);
235 if (le->users == 0) {
236 rb_erase(&le->rb, &ubi->ltree);
237 kfree(le);
238 }
239 spin_unlock(&ubi->ltree_lock);
240}
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{
253 struct ubi_ltree_entry *le;
254
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_lock - lock logical eraseblock for writing.
264 * @ubi: UBI device description object
265 * @vol_id: volume ID
266 * @lnum: logical eraseblock number
267 *
268 * This function locks a logical eraseblock for writing if there is no
269 * contention and does nothing if there is contention. Returns %0 in case of
270 * success, %1 in case of contention, and and a negative error code in case of
271 * failure.
272 */
273static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
274{
275 struct ubi_ltree_entry *le;
276
277 le = ltree_add_entry(ubi, vol_id, lnum);
278 if (IS_ERR(le))
279 return PTR_ERR(le);
280 if (down_write_trylock(&le->mutex))
281 return 0;
282
283 /* Contention, cancel */
284 spin_lock(&ubi->ltree_lock);
285 le->users -= 1;
286 ubi_assert(le->users >= 0);
287 if (le->users == 0) {
288 rb_erase(&le->rb, &ubi->ltree);
289 kfree(le);
290 }
291 spin_unlock(&ubi->ltree_lock);
292
293 return 1;
294}
295
296/**
297 * leb_write_unlock - unlock logical eraseblock.
298 * @ubi: UBI device description object
299 * @vol_id: volume ID
300 * @lnum: logical eraseblock number
301 */
302static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
303{
304 struct ubi_ltree_entry *le;
305
306 spin_lock(&ubi->ltree_lock);
307 le = ltree_lookup(ubi, vol_id, lnum);
308 if(le == NULL)
309 {
310 spin_unlock(&ubi->ltree_lock);
311 return;
312 }
313 le->users -= 1;
314 ubi_assert(le->users >= 0);
315 up_write(&le->mutex);
316 if (le->users == 0) {
317 rb_erase(&le->rb, &ubi->ltree);
318 kfree(le);
319 }
320 spin_unlock(&ubi->ltree_lock);
321}
322
323/**
324 * ubi_eba_unmap_leb - un-map logical eraseblock.
325 * @ubi: UBI device description object
326 * @vol: volume description object
327 * @lnum: logical eraseblock number
328 *
329 * This function un-maps logical eraseblock @lnum and schedules corresponding
330 * physical eraseblock for erasure. Returns zero in case of success and a
331 * negative error code in case of failure.
332 */
333int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
334 int lnum)
335{
336 int err, pnum, vol_id = vol->vol_id;
337
338 if (ubi->ro_mode)
339 return -EROFS;
340
341 err = leb_write_lock(ubi, vol_id, lnum);
342 if (err)
343 return err;
344
345 pnum = vol->eba_tbl[lnum];
346 if (pnum < 0)
347 /* This logical eraseblock is already unmapped */
348 goto out_unlock;
349
350 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
351
352 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
353 err = ubi_wl_put_peb(ubi, pnum, 0);
354
355out_unlock:
356 leb_write_unlock(ubi, vol_id, lnum);
357 return err;
358}
359
360/**
361 * ubi_eba_read_leb - read data.
362 * @ubi: UBI device description object
363 * @vol: volume description object
364 * @lnum: logical eraseblock number
365 * @buf: buffer to store the read data
366 * @offset: offset from where to read
367 * @len: how many bytes to read
368 * @check: data CRC check flag
369 *
370 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
371 * bytes. The @check flag only makes sense for static volumes and forces
372 * eraseblock data CRC checking.
373 *
374 * In case of success this function returns zero. In case of a static volume,
375 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
376 * returned for any volume type if an ECC error was detected by the MTD device
377 * driver. Other negative error cored may be returned in case of other errors.
378 */
379int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
380 void *buf, int offset, int len, int check)
381{
382 int err, pnum, scrub = 0, vol_id = vol->vol_id;
383 struct ubi_vid_hdr *vid_hdr;
384 uint32_t uninitialized_var(crc);
385
386 err = leb_read_lock(ubi, vol_id, lnum);
387 if (err)
388 return err;
389
390 pnum = vol->eba_tbl[lnum];
391 if (pnum < 0) {
392 /*
393 * The logical eraseblock is not mapped, fill the whole buffer
394 * with 0xFF bytes. The exception is static volumes for which
395 * it is an error to read unmapped logical eraseblocks.
396 */
397 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
398 len, offset, vol_id, lnum);
399 leb_read_unlock(ubi, vol_id, lnum);
400 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
401 memset(buf, 0xFF, len);
402 return 0;
403 }
404
405 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
406 len, offset, vol_id, lnum, pnum);
407
408 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
409 check = 0;
410
411retry:
412 if (check) {
413 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
414 if (!vid_hdr) {
415 err = -ENOMEM;
416 goto out_unlock;
417 }
418
419 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
420 if (err && err != UBI_IO_BITFLIPS) {
421 if (err > 0) {
422 /*
423 * The header is either absent or corrupted.
424 * The former case means there is a bug -
425 * switch to read-only mode just in case.
426 * The latter case means a real corruption - we
427 * may try to recover data. FIXME: but this is
428 * not implemented.
429 */
430 if (err == UBI_IO_BAD_HDR_EBADMSG ||
431 err == UBI_IO_BAD_HDR) {
432 ubi_warn("corrupted VID header at PEB "
433 "%d, LEB %d:%d", pnum, vol_id,
434 lnum);
435 err = -EBADMSG;
436 } else
437 ubi_ro_mode(ubi);
438 }
439 goto out_free;
440 } else if (err == UBI_IO_BITFLIPS)
441 scrub = 1;
442
443 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
444 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
445
446 crc = be32_to_cpu(vid_hdr->data_crc);
447 ubi_free_vid_hdr(ubi, vid_hdr);
448 }
449
450 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
451 if (err) {
452 if (err == UBI_IO_BITFLIPS) {
453 scrub = 1;
454 err = 0;
455 } else if (mtd_is_eccerr(err)) {
456 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
457 goto out_unlock;
458 scrub = 1;
459 if (!check) {
460 ubi_msg("force data checking");
461 check = 1;
462 goto retry;
463 }
464 } else
465 goto out_unlock;
466 }
467
468 if (check) {
469 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
470 if (crc1 != crc) {
471 ubi_warn("CRC error: calculated %#08x, must be %#08x",
472 crc1, crc);
473 err = -EBADMSG;
474 goto out_unlock;
475 }
476 }
477
478 if (scrub)
479 err = ubi_wl_scrub_peb(ubi, pnum);
480
481 leb_read_unlock(ubi, vol_id, lnum);
482 return err;
483
484out_free:
485 ubi_free_vid_hdr(ubi, vid_hdr);
486out_unlock:
487 leb_read_unlock(ubi, vol_id, lnum);
488 return err;
489}
490
491/**
492 * recover_peb - recover from write failure.
493 * @ubi: UBI device description object
494 * @pnum: the physical eraseblock to recover
495 * @vol_id: volume ID
496 * @lnum: logical eraseblock number
497 * @buf: data which was not written because of the write failure
498 * @offset: offset of the failed write
499 * @len: how many bytes should have been written
500 *
501 * This function is called in case of a write failure and moves all good data
502 * from the potentially bad physical eraseblock to a good physical eraseblock.
503 * This function also writes the data which was not written due to the failure.
504 * Returns new physical eraseblock number in case of success, and a negative
505 * error code in case of failure.
506 */
507static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
508 const void *buf, int offset, int len)
509{
510 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
511 struct ubi_volume *vol = ubi->volumes[idx];
512 struct ubi_vid_hdr *vid_hdr;
513
514 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
515 if (!vid_hdr)
516 return -ENOMEM;
517
518retry:
519 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
520 if (new_pnum < 0) {
521 ubi_free_vid_hdr(ubi, vid_hdr);
522 return new_pnum;
523 }
524
525 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
526
527 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
528 if (err && err != UBI_IO_BITFLIPS) {
529 if (err > 0)
530 err = -EIO;
531 goto out_put;
532 }
533
534 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
535 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
536 if (err)
537 goto write_error;
538
539 data_size = offset + len;
540 mutex_lock(&ubi->buf_mutex);
541 memset(ubi->peb_buf + offset, 0xFF, len);
542
543 /* Read everything before the area where the write failure happened */
544 if (offset > 0) {
545 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
546 if (err && err != UBI_IO_BITFLIPS)
547 goto out_unlock;
548 }
549
550 memcpy(ubi->peb_buf + offset, buf, len);
551
552 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
553 if (err) {
554 mutex_unlock(&ubi->buf_mutex);
555 goto write_error;
556 }
557
558 mutex_unlock(&ubi->buf_mutex);
559 ubi_free_vid_hdr(ubi, vid_hdr);
560
561 vol->eba_tbl[lnum] = new_pnum;
562 ubi_wl_put_peb(ubi, pnum, 1);
563
564 ubi_msg("data was successfully recovered");
565 return 0;
566
567out_unlock:
568 mutex_unlock(&ubi->buf_mutex);
569out_put:
570 ubi_wl_put_peb(ubi, new_pnum, 1);
571 ubi_free_vid_hdr(ubi, vid_hdr);
572 return err;
573
574write_error:
575 /*
576 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
577 * get another one.
578 */
579 ubi_warn("failed to write to PEB %d", new_pnum);
580 ubi_wl_put_peb(ubi, new_pnum, 1);
581 if (++tries > UBI_IO_RETRIES) {
582 ubi_free_vid_hdr(ubi, vid_hdr);
583 return err;
584 }
585 ubi_msg("try again");
586 goto retry;
587}
588
589/**
590 * ubi_eba_write_leb - write data to dynamic volume.
591 * @ubi: UBI device description object
592 * @vol: volume description object
593 * @lnum: logical eraseblock number
594 * @buf: the data to write
595 * @offset: offset within the logical eraseblock where to write
596 * @len: how many bytes to write
597 * @dtype: data type
598 *
599 * This function writes data to logical eraseblock @lnum of a dynamic volume
600 * @vol. Returns zero in case of success and a negative error code in case
601 * of failure. In case of error, it is possible that something was still
602 * written to the flash media, but may be some garbage.
603 */
604int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
605 const void *buf, int offset, int len, int dtype)
606{
607 int err, pnum, tries = 0, vol_id = vol->vol_id;
608 struct ubi_vid_hdr *vid_hdr;
609
610 if (ubi->ro_mode)
611 return -EROFS;
612
613 err = leb_write_lock(ubi, vol_id, lnum);
614 if (err)
615 return err;
616
617 pnum = vol->eba_tbl[lnum];
618 if (pnum >= 0) {
619 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
620 len, offset, vol_id, lnum, pnum);
621
622 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
623 if (err) {
624 ubi_warn("failed to write data to PEB %d", pnum);
625 if (err == -EIO && ubi->bad_allowed)
626 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
627 offset, len);
628 if (err)
629 ubi_ro_mode(ubi);
630 }
631 leb_write_unlock(ubi, vol_id, lnum);
632 return err;
633 }
634
635 /*
636 * The logical eraseblock is not mapped. We have to get a free physical
637 * eraseblock and write the volume identifier header there first.
638 */
639 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
640 if (!vid_hdr) {
641 leb_write_unlock(ubi, vol_id, lnum);
642 return -ENOMEM;
643 }
644
645 vid_hdr->vol_type = UBI_VID_DYNAMIC;
646 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
647 vid_hdr->vol_id = cpu_to_be32(vol_id);
648 vid_hdr->lnum = cpu_to_be32(lnum);
649 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
650 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
651
652retry:
653 pnum = ubi_wl_get_peb(ubi, dtype);
654 if (pnum < 0) {
655 ubi_free_vid_hdr(ubi, vid_hdr);
656 leb_write_unlock(ubi, vol_id, lnum);
657 return pnum;
658 }
659
660 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
661 len, offset, vol_id, lnum, pnum);
662
663 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
664 if (err) {
665 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
666 vol_id, lnum, pnum);
667 goto write_error;
668 }
669
670 if (len) {
671 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
672 if (err) {
673 ubi_warn("failed to write %d bytes at offset %d of "
674 "LEB %d:%d, PEB %d", len, offset, vol_id,
675 lnum, pnum);
676 goto write_error;
677 }
678 }
679
680 vol->eba_tbl[lnum] = pnum;
681
682 leb_write_unlock(ubi, vol_id, lnum);
683 ubi_free_vid_hdr(ubi, vid_hdr);
684 return 0;
685
686write_error:
687 if (err != -EIO || !ubi->bad_allowed) {
688 ubi_ro_mode(ubi);
689 leb_write_unlock(ubi, vol_id, lnum);
690 ubi_free_vid_hdr(ubi, vid_hdr);
691 return err;
692 }
693
694 /*
695 * Fortunately, this is the first write operation to this physical
696 * eraseblock, so just put it and request a new one. We assume that if
697 * this physical eraseblock went bad, the erase code will handle that.
698 */
699 err = ubi_wl_put_peb(ubi, pnum, 1);
700 if (err || ++tries > UBI_IO_RETRIES) {
701 ubi_ro_mode(ubi);
702 leb_write_unlock(ubi, vol_id, lnum);
703 ubi_free_vid_hdr(ubi, vid_hdr);
704 return err;
705 }
706
707 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
708 ubi_msg("try another PEB");
709 goto retry;
710}
711
712/**
713 * ubi_eba_write_leb_st - write data to static volume.
714 * @ubi: UBI device description object
715 * @vol: volume description object
716 * @lnum: logical eraseblock number
717 * @buf: data to write
718 * @len: how many bytes to write
719 * @dtype: data type
720 * @used_ebs: how many logical eraseblocks will this volume contain
721 *
722 * This function writes data to logical eraseblock @lnum of static volume
723 * @vol. The @used_ebs argument should contain total number of logical
724 * eraseblock in this static volume.
725 *
726 * When writing to the last logical eraseblock, the @len argument doesn't have
727 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
728 * to the real data size, although the @buf buffer has to contain the
729 * alignment. In all other cases, @len has to be aligned.
730 *
731 * It is prohibited to write more than once to logical eraseblocks of static
732 * volumes. This function returns zero in case of success and a negative error
733 * code in case of failure.
734 */
735int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
736 int lnum, const void *buf, int len, int dtype,
737 int used_ebs)
738{
739 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
740 struct ubi_vid_hdr *vid_hdr;
741 uint32_t crc;
742
743 if (ubi->ro_mode)
744 return -EROFS;
745
746 if (lnum == used_ebs - 1)
747 /* If this is the last LEB @len may be unaligned */
748 len = ALIGN(data_size, ubi->min_io_size);
749 else
750 ubi_assert(!(len & (ubi->min_io_size - 1)));
751
752 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
753 if (!vid_hdr)
754 return -ENOMEM;
755
756 err = leb_write_lock(ubi, vol_id, lnum);
757 if (err) {
758 ubi_free_vid_hdr(ubi, vid_hdr);
759 return err;
760 }
761
762 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
763 vid_hdr->vol_id = cpu_to_be32(vol_id);
764 vid_hdr->lnum = cpu_to_be32(lnum);
765 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
766 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
767
768 crc = crc32(UBI_CRC32_INIT, buf, data_size);
769 vid_hdr->vol_type = UBI_VID_STATIC;
770 vid_hdr->data_size = cpu_to_be32(data_size);
771 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
772 vid_hdr->data_crc = cpu_to_be32(crc);
773
774retry:
775 pnum = ubi_wl_get_peb(ubi, dtype);
776 if (pnum < 0) {
777 ubi_free_vid_hdr(ubi, vid_hdr);
778 leb_write_unlock(ubi, vol_id, lnum);
779 return pnum;
780 }
781
782 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
783 len, vol_id, lnum, pnum, used_ebs);
784
785 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
786 if (err) {
787 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
788 vol_id, lnum, pnum);
789 goto write_error;
790 }
791
792 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
793 if (err) {
794 ubi_warn("failed to write %d bytes of data to PEB %d",
795 len, pnum);
796 goto write_error;
797 }
798
799 ubi_assert(vol->eba_tbl[lnum] < 0);
800 vol->eba_tbl[lnum] = pnum;
801
802 leb_write_unlock(ubi, vol_id, lnum);
803 ubi_free_vid_hdr(ubi, vid_hdr);
804 return 0;
805
806write_error:
807 if (err != -EIO || !ubi->bad_allowed) {
808 /*
809 * This flash device does not admit of bad eraseblocks or
810 * something nasty and unexpected happened. Switch to read-only
811 * mode just in case.
812 */
813 ubi_ro_mode(ubi);
814 leb_write_unlock(ubi, vol_id, lnum);
815 ubi_free_vid_hdr(ubi, vid_hdr);
816 return err;
817 }
818
819 err = ubi_wl_put_peb(ubi, pnum, 1);
820 if (err || ++tries > UBI_IO_RETRIES) {
821 ubi_ro_mode(ubi);
822 leb_write_unlock(ubi, vol_id, lnum);
823 ubi_free_vid_hdr(ubi, vid_hdr);
824 return err;
825 }
826
827 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
828 ubi_msg("try another PEB");
829 goto retry;
830}
831
832/*
833 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
834 * @ubi: UBI device description object
835 * @vol: volume description object
836 * @lnum: logical eraseblock number
837 * @buf: data to write
838 * @len: how many bytes to write
839 * @dtype: data type
840 *
841 * This function changes the contents of a logical eraseblock atomically. @buf
842 * has to contain new logical eraseblock data, and @len - the length of the
843 * data, which has to be aligned. This function guarantees that in case of an
844 * unclean reboot the old contents is preserved. Returns zero in case of
845 * success and a negative error code in case of failure.
846 *
847 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
848 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
849 */
850int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
851 int lnum, const void *buf, int len, int dtype)
852{
853 int err, pnum, tries = 0, vol_id = vol->vol_id;
854 struct ubi_vid_hdr *vid_hdr;
855 uint32_t crc;
856
857 if (ubi->ro_mode)
858 return -EROFS;
859
860 if (len == 0) {
861 /*
862 * Special case when data length is zero. In this case the LEB
863 * has to be unmapped and mapped somewhere else.
864 */
865 err = ubi_eba_unmap_leb(ubi, vol, lnum);
866 if (err)
867 return err;
868 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
869 }
870
871 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
872 if (!vid_hdr)
873 return -ENOMEM;
874
875 mutex_lock(&ubi->alc_mutex);
876 err = leb_write_lock(ubi, vol_id, lnum);
877 if (err)
878 goto out_mutex;
879
880 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
881 vid_hdr->vol_id = cpu_to_be32(vol_id);
882 vid_hdr->lnum = cpu_to_be32(lnum);
883 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
884 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
885
886 crc = crc32(UBI_CRC32_INIT, buf, len);
887 vid_hdr->vol_type = UBI_VID_DYNAMIC;
888 vid_hdr->data_size = cpu_to_be32(len);
889 vid_hdr->copy_flag = 1;
890 vid_hdr->data_crc = cpu_to_be32(crc);
891
892retry:
893 pnum = ubi_wl_get_peb(ubi, dtype);
894 if (pnum < 0) {
895 err = pnum;
896 goto out_leb_unlock;
897 }
898
899 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
900 vol_id, lnum, vol->eba_tbl[lnum], pnum);
901
902 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
903 if (err) {
904 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
905 vol_id, lnum, pnum);
906 goto write_error;
907 }
908
909 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
910 if (err) {
911 ubi_warn("failed to write %d bytes of data to PEB %d",
912 len, pnum);
913 goto write_error;
914 }
915
916 if (vol->eba_tbl[lnum] >= 0) {
917 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 0);
918 if (err)
919 goto out_leb_unlock;
920 }
921
922 vol->eba_tbl[lnum] = pnum;
923
924out_leb_unlock:
925 leb_write_unlock(ubi, vol_id, lnum);
926out_mutex:
927 mutex_unlock(&ubi->alc_mutex);
928 ubi_free_vid_hdr(ubi, vid_hdr);
929 return err;
930
931write_error:
932 if (err != -EIO || !ubi->bad_allowed) {
933 /*
934 * This flash device does not admit of bad eraseblocks or
935 * something nasty and unexpected happened. Switch to read-only
936 * mode just in case.
937 */
938 ubi_ro_mode(ubi);
939 goto out_leb_unlock;
940 }
941
942 err = ubi_wl_put_peb(ubi, pnum, 1);
943 if (err || ++tries > UBI_IO_RETRIES) {
944 ubi_ro_mode(ubi);
945 goto out_leb_unlock;
946 }
947
948 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
949 ubi_msg("try another PEB");
950 goto retry;
951}
952
953/**
954 * is_error_sane - check whether a read error is sane.
955 * @err: code of the error happened during reading
956 *
957 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
958 * cannot read data from the target PEB (an error @err happened). If the error
959 * code is sane, then we treat this error as non-fatal. Otherwise the error is
960 * fatal and UBI will be switched to R/O mode later.
961 *
962 * The idea is that we try not to switch to R/O mode if the read error is
963 * something which suggests there was a real read problem. E.g., %-EIO. Or a
964 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
965 * mode, simply because we do not know what happened at the MTD level, and we
966 * cannot handle this. E.g., the underlying driver may have become crazy, and
967 * it is safer to switch to R/O mode to preserve the data.
968 *
969 * And bear in mind, this is about reading from the target PEB, i.e. the PEB
970 * which we have just written.
971 */
972static int is_error_sane(int err)
973{
974 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
975 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
976 return 0;
977 return 1;
978}
979
980/**
981 * ubi_eba_copy_leb - copy logical eraseblock.
982 * @ubi: UBI device description object
983 * @from: physical eraseblock number from where to copy
984 * @to: physical eraseblock number where to copy
985 * @vid_hdr: VID header of the @from physical eraseblock
986 *
987 * This function copies logical eraseblock from physical eraseblock @from to
988 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
989 * function. Returns:
990 * o %0 in case of success;
991 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
992 * o a negative error code in case of failure.
993 */
994int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
995 struct ubi_vid_hdr *vid_hdr)
996{
997 int err, vol_id, lnum, data_size, aldata_size, idx;
998 struct ubi_volume *vol;
999 uint32_t crc;
1000
1001 vol_id = be32_to_cpu(vid_hdr->vol_id);
1002 lnum = be32_to_cpu(vid_hdr->lnum);
1003
1004 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1005
1006 if (vid_hdr->vol_type == UBI_VID_STATIC) {
1007 data_size = be32_to_cpu(vid_hdr->data_size);
1008 aldata_size = ALIGN(data_size, ubi->min_io_size);
1009 } else
1010 data_size = aldata_size =
1011 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1012
1013 idx = vol_id2idx(ubi, vol_id);
1014 spin_lock(&ubi->volumes_lock);
1015 /*
1016 * Note, we may race with volume deletion, which means that the volume
1017 * this logical eraseblock belongs to might be being deleted. Since the
1018 * volume deletion un-maps all the volume's logical eraseblocks, it will
1019 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1020 */
1021 vol = ubi->volumes[idx];
1022 spin_unlock(&ubi->volumes_lock);
1023 if (!vol) {
1024 /* No need to do further work, cancel */
1025 dbg_wl("volume %d is being removed, cancel", vol_id);
1026 return MOVE_CANCEL_RACE;
1027 }
1028
1029 /*
1030 * We do not want anybody to write to this logical eraseblock while we
1031 * are moving it, so lock it.
1032 *
1033 * Note, we are using non-waiting locking here, because we cannot sleep
1034 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1035 * unmapping the LEB which is mapped to the PEB we are going to move
1036 * (@from). This task locks the LEB and goes sleep in the
1037 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1038 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1039 * LEB is already locked, we just do not move it and return
1040 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
1041 * we do not know the reasons of the contention - it may be just a
1042 * normal I/O on this LEB, so we want to re-try.
1043 */
1044 err = leb_write_trylock(ubi, vol_id, lnum);
1045 if (err) {
1046 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1047 return MOVE_RETRY;
1048 }
1049
1050 /*
1051 * The LEB might have been put meanwhile, and the task which put it is
1052 * probably waiting on @ubi->move_mutex. No need to continue the work,
1053 * cancel it.
1054 */
1055 if (vol->eba_tbl[lnum] != from) {
1056 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1057 "PEB %d, cancel", vol_id, lnum, from,
1058 vol->eba_tbl[lnum]);
1059 err = MOVE_CANCEL_RACE;
1060 goto out_unlock_leb;
1061 }
1062
1063 /*
1064 * OK, now the LEB is locked and we can safely start moving it. Since
1065 * this function utilizes the @ubi->peb_buf buffer which is shared
1066 * with some other functions - we lock the buffer by taking the
1067 * @ubi->buf_mutex.
1068 */
1069 mutex_lock(&ubi->buf_mutex);
1070 dbg_wl("read %d bytes of data", aldata_size);
1071 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
1072 if (err && err != UBI_IO_BITFLIPS) {
1073 ubi_warn("error %d while reading data from PEB %d",
1074 err, from);
1075 err = MOVE_SOURCE_RD_ERR;
1076 goto out_unlock_buf;
1077 }
1078
1079 /*
1080 * Now we have got to calculate how much data we have to copy. In
1081 * case of a static volume it is fairly easy - the VID header contains
1082 * the data size. In case of a dynamic volume it is more difficult - we
1083 * have to read the contents, cut 0xFF bytes from the end and copy only
1084 * the first part. We must do this to avoid writing 0xFF bytes as it
1085 * may have some side-effects. And not only this. It is important not
1086 * to include those 0xFFs to CRC because later the they may be filled
1087 * by data.
1088 */
1089 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1090 aldata_size = data_size =
1091 ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
1092
1093 cond_resched();
1094 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
1095 cond_resched();
1096
1097 /*
1098 * It may turn out to be that the whole @from physical eraseblock
1099 * contains only 0xFF bytes. Then we have to only write the VID header
1100 * and do not write any data. This also means we should not set
1101 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1102 */
1103 if (data_size > 0) {
1104 vid_hdr->copy_flag = 1;
1105 vid_hdr->data_size = cpu_to_be32(data_size);
1106 vid_hdr->data_crc = cpu_to_be32(crc);
1107 }
1108 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1109
1110 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1111 if (err) {
1112 if (err == -EIO)
1113 err = MOVE_TARGET_WR_ERR;
1114 goto out_unlock_buf;
1115 }
1116
1117 cond_resched();
1118
1119 /* Read the VID header back and check if it was written correctly */
1120 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1121 if (err) {
1122 if (err != UBI_IO_BITFLIPS) {
1123 ubi_warn("error %d while reading VID header back from "
1124 "PEB %d", err, to);
1125 if (is_error_sane(err))
1126 err = MOVE_TARGET_RD_ERR;
1127 } else
1128 err = MOVE_TARGET_BITFLIPS;
1129 goto out_unlock_buf;
1130 }
1131
1132 if (data_size > 0) {
1133 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1134 if (err) {
1135 if (err == -EIO)
1136 err = MOVE_TARGET_WR_ERR;
1137 goto out_unlock_buf;
1138 }
1139
1140 cond_resched();
1141
1142 /*
1143 * We've written the data and are going to read it back to make
1144 * sure it was written correctly.
1145 */
1146 memset(ubi->peb_buf, 0xFF, aldata_size);
1147 err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1148 if (err) {
1149 if (err != UBI_IO_BITFLIPS) {
1150 ubi_warn("error %d while reading data back "
1151 "from PEB %d", err, to);
1152 if (is_error_sane(err))
1153 err = MOVE_TARGET_RD_ERR;
1154 } else
1155 err = MOVE_TARGET_BITFLIPS;
1156 goto out_unlock_buf;
1157 }
1158
1159 cond_resched();
1160
1161 if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1162 ubi_warn("read data back from PEB %d and it is "
1163 "different", to);
1164 err = -EINVAL;
1165 goto out_unlock_buf;
1166 }
1167 }
1168
1169 ubi_assert(vol->eba_tbl[lnum] == from);
1170 vol->eba_tbl[lnum] = to;
1171
1172out_unlock_buf:
1173 mutex_unlock(&ubi->buf_mutex);
1174out_unlock_leb:
1175 leb_write_unlock(ubi, vol_id, lnum);
1176 return err;
1177}
1178
1179/**
1180 * print_rsvd_warning - warn about not having enough reserved PEBs.
1181 * @ubi: UBI device description object
1182 *
1183 * This is a helper function for 'ubi_eba_init_scan()' which is called when UBI
1184 * cannot reserve enough PEBs for bad block handling. This function makes a
1185 * decision whether we have to print a warning or not. The algorithm is as
1186 * follows:
1187 * o if this is a new UBI image, then just print the warning
1188 * o if this is an UBI image which has already been used for some time, print
1189 * a warning only if we can reserve less than 10% of the expected amount of
1190 * the reserved PEB.
1191 *
1192 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1193 * of PEBs becomes smaller, which is normal and we do not want to scare users
1194 * with a warning every time they attach the MTD device. This was an issue
1195 * reported by real users.
1196 */
1197static void print_rsvd_warning(struct ubi_device *ubi,
1198 struct ubi_scan_info *si)
1199{
1200 /*
1201 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1202 * large number to distinguish between newly flashed and used images.
1203 */
1204 if (si->max_sqnum > (1 << 18)) {
1205 int min = ubi->beb_rsvd_level / 10;
1206
1207 if (!min)
1208 min = 1;
1209 if (ubi->beb_rsvd_pebs > min)
1210 return;
1211 }
1212
1213 ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d,"
1214 " need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1215 if (ubi->corr_peb_count)
1216 ubi_warn("%d PEBs are corrupted and not used",
1217 ubi->corr_peb_count);
1218}
1219
1220/**
1221 * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
1222 * @ubi: UBI device description object
1223 * @si: scanning information
1224 *
1225 * This function returns zero in case of success and a negative error code in
1226 * case of failure.
1227 */
1228int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1229{
1230 int i, j, err, num_volumes;
1231 struct ubi_scan_volume *sv;
1232 struct ubi_volume *vol;
1233 struct ubi_scan_leb *seb;
1234 struct rb_node *rb;
1235
1236 dbg_eba("initialize EBA sub-system");
1237
1238 spin_lock_init(&ubi->ltree_lock);
1239 mutex_init(&ubi->alc_mutex);
1240 ubi->ltree = RB_ROOT;
1241
1242 ubi->global_sqnum = si->max_sqnum + 1;
1243 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1244
1245 for (i = 0; i < num_volumes; i++) {
1246 vol = ubi->volumes[i];
1247 if (!vol)
1248 continue;
1249
1250 cond_resched();
1251
1252 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1253 GFP_KERNEL);
1254 if (!vol->eba_tbl) {
1255 err = -ENOMEM;
1256 goto out_free;
1257 }
1258
1259 for (j = 0; j < vol->reserved_pebs; j++)
1260 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1261
1262 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1263 if (!sv)
1264 continue;
1265
1266 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1267 if (seb->lnum >= vol->reserved_pebs)
1268 /*
1269 * This may happen in case of an unclean reboot
1270 * during re-size.
1271 */
1272 ubi_scan_move_to_list(sv, seb, &si->erase);
1273 else
1274 vol->eba_tbl[seb->lnum] = seb->pnum;
1275 }
1276 }
1277
1278 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1279 ubi_err("no enough physical eraseblocks (%d, need %d)",
1280 ubi->avail_pebs, EBA_RESERVED_PEBS);
1281 if (ubi->corr_peb_count)
1282 ubi_err("%d PEBs are corrupted and not used",
1283 ubi->corr_peb_count);
1284 err = -ENOSPC;
1285 goto out_free;
1286 }
1287 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1288 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1289
1290 if (ubi->bad_allowed) {
1291 ubi_calculate_reserved(ubi);
1292
1293 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1294 /* No enough free physical eraseblocks */
1295 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1296 print_rsvd_warning(ubi, si);
1297 } else
1298 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1299
1300 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1301 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1302 }
1303
1304 dbg_eba("EBA sub-system is initialized");
1305 return 0;
1306
1307out_free:
1308 for (i = 0; i < num_volumes; i++) {
1309 if (!ubi->volumes[i])
1310 continue;
1311 kfree(ubi->volumes[i]->eba_tbl);
1312 ubi->volumes[i]->eba_tbl = NULL;
1313 }
1314 return err;
1315}