blob: 8428278e215fdf3ddd431b92381d2190d7c3cf5b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park961df832008-11-19 16:25:44 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Park961df832008-11-19 16:25:44 +01005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
8/*
Heiko Schocherff94bc42014-06-24 10:10:04 +02009 * The UBI Eraseblock Association (EBA) sub-system.
Kyungmin Park961df832008-11-19 16:25:44 +010010 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020011 * This sub-system is responsible for I/O to/from logical eraseblock.
Kyungmin Park961df832008-11-19 16:25:44 +010012 *
13 * Although in this implementation the EBA table is fully kept and managed in
14 * RAM, which assumes poor scalability, it might be (partially) maintained on
15 * flash in future implementations.
16 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020017 * The EBA sub-system implements per-logical eraseblock locking. Before
18 * accessing a logical eraseblock it is locked for reading or writing. The
19 * per-logical eraseblock locking is implemented by means of the lock tree. The
20 * lock tree is an RB-tree which refers all the currently locked logical
21 * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
22 * They are indexed by (@vol_id, @lnum) pairs.
Kyungmin Park961df832008-11-19 16:25:44 +010023 *
24 * EBA also maintains the global sequence counter which is incremented each
25 * time a logical eraseblock is mapped to a physical eraseblock and it is
26 * stored in the volume identifier header. This means that each VID header has
27 * a unique sequence number. The sequence number is only increased an we assume
28 * 64 bits is enough to never overflow.
29 */
30
Heiko Schocherff94bc42014-06-24 10:10:04 +020031#ifndef __UBOOT__
Simon Glass61b29b82020-02-03 07:36:15 -070032#include <dm/devres.h>
Kyungmin Park961df832008-11-19 16:25:44 +010033#include <linux/slab.h>
34#include <linux/crc32.h>
Simon Glass3db71102019-11-14 12:57:16 -070035#include <u-boot/crc.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020036#else
37#include <ubi_uboot.h>
Kyungmin Park961df832008-11-19 16:25:44 +010038#endif
39
Heiko Schocherff94bc42014-06-24 10:10:04 +020040#include <linux/err.h>
Kyungmin Park961df832008-11-19 16:25:44 +010041#include "ubi.h"
42
43/* Number of physical eraseblocks reserved for atomic LEB change operation */
44#define EBA_RESERVED_PEBS 1
45
46/**
47 * next_sqnum - get next sequence number.
48 * @ubi: UBI device description object
49 *
50 * This function returns next sequence number to use, which is just the current
51 * global sequence counter value. It also increases the global sequence
52 * counter.
53 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020054unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
Kyungmin Park961df832008-11-19 16:25:44 +010055{
56 unsigned long long sqnum;
57
58 spin_lock(&ubi->ltree_lock);
59 sqnum = ubi->global_sqnum++;
60 spin_unlock(&ubi->ltree_lock);
61
62 return sqnum;
63}
64
65/**
66 * ubi_get_compat - get compatibility flags of a volume.
67 * @ubi: UBI device description object
68 * @vol_id: volume ID
69 *
70 * This function returns compatibility flags for an internal volume. User
71 * volumes have no compatibility flags, so %0 is returned.
72 */
73static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
74{
75 if (vol_id == UBI_LAYOUT_VOLUME_ID)
76 return UBI_LAYOUT_VOLUME_COMPAT;
77 return 0;
78}
79
80/**
81 * ltree_lookup - look up the lock tree.
82 * @ubi: UBI device description object
83 * @vol_id: volume ID
84 * @lnum: logical eraseblock number
85 *
86 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
87 * object if the logical eraseblock is locked and %NULL if it is not.
88 * @ubi->ltree_lock has to be locked.
89 */
90static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
91 int lnum)
92{
93 struct rb_node *p;
94
95 p = ubi->ltree.rb_node;
96 while (p) {
97 struct ubi_ltree_entry *le;
98
99 le = rb_entry(p, struct ubi_ltree_entry, rb);
100
101 if (vol_id < le->vol_id)
102 p = p->rb_left;
103 else if (vol_id > le->vol_id)
104 p = p->rb_right;
105 else {
106 if (lnum < le->lnum)
107 p = p->rb_left;
108 else if (lnum > le->lnum)
109 p = p->rb_right;
110 else
111 return le;
112 }
113 }
114
115 return NULL;
116}
117
118/**
119 * ltree_add_entry - add new entry to the lock tree.
120 * @ubi: UBI device description object
121 * @vol_id: volume ID
122 * @lnum: logical eraseblock number
123 *
124 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
125 * lock tree. If such entry is already there, its usage counter is increased.
126 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
127 * failed.
128 */
129static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
130 int vol_id, int lnum)
131{
132 struct ubi_ltree_entry *le, *le1, *le_free;
133
134 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
135 if (!le)
136 return ERR_PTR(-ENOMEM);
137
138 le->users = 0;
139 init_rwsem(&le->mutex);
140 le->vol_id = vol_id;
141 le->lnum = lnum;
142
143 spin_lock(&ubi->ltree_lock);
144 le1 = ltree_lookup(ubi, vol_id, lnum);
145
146 if (le1) {
147 /*
148 * This logical eraseblock is already locked. The newly
149 * allocated lock entry is not needed.
150 */
151 le_free = le;
152 le = le1;
153 } else {
154 struct rb_node **p, *parent = NULL;
155
156 /*
157 * No lock entry, add the newly allocated one to the
158 * @ubi->ltree RB-tree.
159 */
160 le_free = NULL;
161
162 p = &ubi->ltree.rb_node;
163 while (*p) {
164 parent = *p;
165 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
166
167 if (vol_id < le1->vol_id)
168 p = &(*p)->rb_left;
169 else if (vol_id > le1->vol_id)
170 p = &(*p)->rb_right;
171 else {
172 ubi_assert(lnum != le1->lnum);
173 if (lnum < le1->lnum)
174 p = &(*p)->rb_left;
175 else
176 p = &(*p)->rb_right;
177 }
178 }
179
180 rb_link_node(&le->rb, parent, p);
181 rb_insert_color(&le->rb, &ubi->ltree);
182 }
183 le->users += 1;
184 spin_unlock(&ubi->ltree_lock);
185
Heiko Schocherff94bc42014-06-24 10:10:04 +0200186 kfree(le_free);
Kyungmin Park961df832008-11-19 16:25:44 +0100187 return le;
188}
189
190/**
191 * leb_read_lock - lock logical eraseblock for reading.
192 * @ubi: UBI device description object
193 * @vol_id: volume ID
194 * @lnum: logical eraseblock number
195 *
196 * This function locks a logical eraseblock for reading. Returns zero in case
197 * of success and a negative error code in case of failure.
198 */
199static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
200{
201 struct ubi_ltree_entry *le;
202
203 le = ltree_add_entry(ubi, vol_id, lnum);
204 if (IS_ERR(le))
205 return PTR_ERR(le);
206 down_read(&le->mutex);
207 return 0;
208}
209
210/**
211 * leb_read_unlock - unlock logical eraseblock.
212 * @ubi: UBI device description object
213 * @vol_id: volume ID
214 * @lnum: logical eraseblock number
215 */
216static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
217{
Kyungmin Park961df832008-11-19 16:25:44 +0100218 struct ubi_ltree_entry *le;
219
220 spin_lock(&ubi->ltree_lock);
221 le = ltree_lookup(ubi, vol_id, lnum);
222 le->users -= 1;
223 ubi_assert(le->users >= 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200224 up_read(&le->mutex);
Kyungmin Park961df832008-11-19 16:25:44 +0100225 if (le->users == 0) {
226 rb_erase(&le->rb, &ubi->ltree);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200227 kfree(le);
Kyungmin Park961df832008-11-19 16:25:44 +0100228 }
229 spin_unlock(&ubi->ltree_lock);
Kyungmin Park961df832008-11-19 16:25:44 +0100230}
231
232/**
233 * leb_write_lock - lock logical eraseblock for writing.
234 * @ubi: UBI device description object
235 * @vol_id: volume ID
236 * @lnum: logical eraseblock number
237 *
238 * This function locks a logical eraseblock for writing. Returns zero in case
239 * of success and a negative error code in case of failure.
240 */
241static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
242{
243 struct ubi_ltree_entry *le;
244
245 le = ltree_add_entry(ubi, vol_id, lnum);
246 if (IS_ERR(le))
247 return PTR_ERR(le);
248 down_write(&le->mutex);
249 return 0;
250}
251
252/**
253 * leb_write_lock - lock logical eraseblock for writing.
254 * @ubi: UBI device description object
255 * @vol_id: volume ID
256 * @lnum: logical eraseblock number
257 *
258 * This function locks a logical eraseblock for writing if there is no
259 * contention and does nothing if there is contention. Returns %0 in case of
260 * success, %1 in case of contention, and and a negative error code in case of
261 * failure.
262 */
263static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
264{
Kyungmin Park961df832008-11-19 16:25:44 +0100265 struct ubi_ltree_entry *le;
266
267 le = ltree_add_entry(ubi, vol_id, lnum);
268 if (IS_ERR(le))
269 return PTR_ERR(le);
270 if (down_write_trylock(&le->mutex))
271 return 0;
272
273 /* Contention, cancel */
274 spin_lock(&ubi->ltree_lock);
275 le->users -= 1;
276 ubi_assert(le->users >= 0);
277 if (le->users == 0) {
278 rb_erase(&le->rb, &ubi->ltree);
Kyungmin Park961df832008-11-19 16:25:44 +0100279 kfree(le);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200280 }
281 spin_unlock(&ubi->ltree_lock);
Kyungmin Park961df832008-11-19 16:25:44 +0100282
283 return 1;
284}
285
286/**
287 * leb_write_unlock - unlock logical eraseblock.
288 * @ubi: UBI device description object
289 * @vol_id: volume ID
290 * @lnum: logical eraseblock number
291 */
292static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
293{
Kyungmin Park961df832008-11-19 16:25:44 +0100294 struct ubi_ltree_entry *le;
295
296 spin_lock(&ubi->ltree_lock);
297 le = ltree_lookup(ubi, vol_id, lnum);
298 le->users -= 1;
299 ubi_assert(le->users >= 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200300 up_write(&le->mutex);
Kyungmin Park961df832008-11-19 16:25:44 +0100301 if (le->users == 0) {
302 rb_erase(&le->rb, &ubi->ltree);
Kyungmin Park961df832008-11-19 16:25:44 +0100303 kfree(le);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200304 }
305 spin_unlock(&ubi->ltree_lock);
Kyungmin Park961df832008-11-19 16:25:44 +0100306}
307
308/**
309 * ubi_eba_unmap_leb - un-map logical eraseblock.
310 * @ubi: UBI device description object
311 * @vol: volume description object
312 * @lnum: logical eraseblock number
313 *
314 * This function un-maps logical eraseblock @lnum and schedules corresponding
315 * physical eraseblock for erasure. Returns zero in case of success and a
316 * negative error code in case of failure.
317 */
318int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
319 int lnum)
320{
321 int err, pnum, vol_id = vol->vol_id;
322
323 if (ubi->ro_mode)
324 return -EROFS;
325
326 err = leb_write_lock(ubi, vol_id, lnum);
327 if (err)
328 return err;
329
330 pnum = vol->eba_tbl[lnum];
331 if (pnum < 0)
332 /* This logical eraseblock is already unmapped */
333 goto out_unlock;
334
335 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
336
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200337 down_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100338 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200339 up_read(&ubi->fm_eba_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200340 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
Kyungmin Park961df832008-11-19 16:25:44 +0100341
342out_unlock:
343 leb_write_unlock(ubi, vol_id, lnum);
344 return err;
345}
346
347/**
348 * ubi_eba_read_leb - read data.
349 * @ubi: UBI device description object
350 * @vol: volume description object
351 * @lnum: logical eraseblock number
352 * @buf: buffer to store the read data
353 * @offset: offset from where to read
354 * @len: how many bytes to read
355 * @check: data CRC check flag
356 *
357 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
358 * bytes. The @check flag only makes sense for static volumes and forces
359 * eraseblock data CRC checking.
360 *
361 * In case of success this function returns zero. In case of a static volume,
362 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
363 * returned for any volume type if an ECC error was detected by the MTD device
364 * driver. Other negative error cored may be returned in case of other errors.
365 */
366int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
367 void *buf, int offset, int len, int check)
368{
369 int err, pnum, scrub = 0, vol_id = vol->vol_id;
370 struct ubi_vid_hdr *vid_hdr;
371 uint32_t uninitialized_var(crc);
372
373 err = leb_read_lock(ubi, vol_id, lnum);
374 if (err)
375 return err;
376
377 pnum = vol->eba_tbl[lnum];
378 if (pnum < 0) {
379 /*
380 * The logical eraseblock is not mapped, fill the whole buffer
381 * with 0xFF bytes. The exception is static volumes for which
382 * it is an error to read unmapped logical eraseblocks.
383 */
384 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
385 len, offset, vol_id, lnum);
386 leb_read_unlock(ubi, vol_id, lnum);
387 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
388 memset(buf, 0xFF, len);
389 return 0;
390 }
391
392 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
393 len, offset, vol_id, lnum, pnum);
394
395 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
396 check = 0;
397
398retry:
399 if (check) {
400 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
401 if (!vid_hdr) {
402 err = -ENOMEM;
403 goto out_unlock;
404 }
405
406 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
407 if (err && err != UBI_IO_BITFLIPS) {
408 if (err > 0) {
409 /*
410 * The header is either absent or corrupted.
411 * The former case means there is a bug -
412 * switch to read-only mode just in case.
413 * The latter case means a real corruption - we
414 * may try to recover data. FIXME: but this is
415 * not implemented.
416 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200417 if (err == UBI_IO_BAD_HDR_EBADMSG ||
418 err == UBI_IO_BAD_HDR) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200419 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200420 pnum, vol_id, lnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100421 err = -EBADMSG;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200422 } else {
423 err = -EINVAL;
Kyungmin Park961df832008-11-19 16:25:44 +0100424 ubi_ro_mode(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200425 }
Kyungmin Park961df832008-11-19 16:25:44 +0100426 }
427 goto out_free;
428 } else if (err == UBI_IO_BITFLIPS)
429 scrub = 1;
430
431 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
432 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
433
434 crc = be32_to_cpu(vid_hdr->data_crc);
435 ubi_free_vid_hdr(ubi, vid_hdr);
436 }
437
438 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
439 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200440 if (err == UBI_IO_BITFLIPS)
Kyungmin Park961df832008-11-19 16:25:44 +0100441 scrub = 1;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200442 else if (mtd_is_eccerr(err)) {
Kyungmin Park961df832008-11-19 16:25:44 +0100443 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
444 goto out_unlock;
445 scrub = 1;
446 if (!check) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200447 ubi_msg(ubi, "force data checking");
Kyungmin Park961df832008-11-19 16:25:44 +0100448 check = 1;
449 goto retry;
450 }
451 } else
452 goto out_unlock;
453 }
454
455 if (check) {
456 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
457 if (crc1 != crc) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200458 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
Kyungmin Park961df832008-11-19 16:25:44 +0100459 crc1, crc);
460 err = -EBADMSG;
461 goto out_unlock;
462 }
463 }
464
465 if (scrub)
466 err = ubi_wl_scrub_peb(ubi, pnum);
467
468 leb_read_unlock(ubi, vol_id, lnum);
469 return err;
470
471out_free:
472 ubi_free_vid_hdr(ubi, vid_hdr);
473out_unlock:
474 leb_read_unlock(ubi, vol_id, lnum);
475 return err;
476}
477
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200478#ifndef __UBOOT__
479/**
480 * ubi_eba_read_leb_sg - read data into a scatter gather list.
481 * @ubi: UBI device description object
482 * @vol: volume description object
483 * @lnum: logical eraseblock number
484 * @sgl: UBI scatter gather list to store the read data
485 * @offset: offset from where to read
486 * @len: how many bytes to read
487 * @check: data CRC check flag
488 *
489 * This function works exactly like ubi_eba_read_leb(). But instead of
490 * storing the read data into a buffer it writes to an UBI scatter gather
491 * list.
492 */
493int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
494 struct ubi_sgl *sgl, int lnum, int offset, int len,
495 int check)
496{
497 int to_read;
498 int ret;
499 struct scatterlist *sg;
500
501 for (;;) {
502 ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
503 sg = &sgl->sg[sgl->list_pos];
504 if (len < sg->length - sgl->page_pos)
505 to_read = len;
506 else
507 to_read = sg->length - sgl->page_pos;
508
509 ret = ubi_eba_read_leb(ubi, vol, lnum,
510 sg_virt(sg) + sgl->page_pos, offset,
511 to_read, check);
512 if (ret < 0)
513 return ret;
514
515 offset += to_read;
516 len -= to_read;
517 if (!len) {
518 sgl->page_pos += to_read;
519 if (sgl->page_pos == sg->length) {
520 sgl->list_pos++;
521 sgl->page_pos = 0;
522 }
523
524 break;
525 }
526
527 sgl->list_pos++;
528 sgl->page_pos = 0;
529 }
530
531 return ret;
532}
533#endif
534
Kyungmin Park961df832008-11-19 16:25:44 +0100535/**
536 * recover_peb - recover from write failure.
537 * @ubi: UBI device description object
538 * @pnum: the physical eraseblock to recover
539 * @vol_id: volume ID
540 * @lnum: logical eraseblock number
541 * @buf: data which was not written because of the write failure
542 * @offset: offset of the failed write
543 * @len: how many bytes should have been written
544 *
545 * This function is called in case of a write failure and moves all good data
546 * from the potentially bad physical eraseblock to a good physical eraseblock.
547 * This function also writes the data which was not written due to the failure.
548 * Returns new physical eraseblock number in case of success, and a negative
549 * error code in case of failure.
550 */
551static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
552 const void *buf, int offset, int len)
553{
554 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
555 struct ubi_volume *vol = ubi->volumes[idx];
556 struct ubi_vid_hdr *vid_hdr;
557
558 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200559 if (!vid_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +0100560 return -ENOMEM;
Kyungmin Park961df832008-11-19 16:25:44 +0100561
562retry:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200563 new_pnum = ubi_wl_get_peb(ubi);
Kyungmin Park961df832008-11-19 16:25:44 +0100564 if (new_pnum < 0) {
Kyungmin Park961df832008-11-19 16:25:44 +0100565 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200566 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100567 return new_pnum;
568 }
569
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200570 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
571 pnum, new_pnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100572
573 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
574 if (err && err != UBI_IO_BITFLIPS) {
575 if (err > 0)
576 err = -EIO;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200577 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100578 goto out_put;
579 }
580
Heiko Schocherff94bc42014-06-24 10:10:04 +0200581 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Kyungmin Park961df832008-11-19 16:25:44 +0100582 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200583 if (err) {
584 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100585 goto write_error;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200586 }
Kyungmin Park961df832008-11-19 16:25:44 +0100587
588 data_size = offset + len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200589 mutex_lock(&ubi->buf_mutex);
590 memset(ubi->peb_buf + offset, 0xFF, len);
Kyungmin Park961df832008-11-19 16:25:44 +0100591
592 /* Read everything before the area where the write failure happened */
593 if (offset > 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200594 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200595 if (err && err != UBI_IO_BITFLIPS) {
596 up_read(&ubi->fm_eba_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200597 goto out_unlock;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200598 }
Kyungmin Park961df832008-11-19 16:25:44 +0100599 }
600
Heiko Schocherff94bc42014-06-24 10:10:04 +0200601 memcpy(ubi->peb_buf + offset, buf, len);
Kyungmin Park961df832008-11-19 16:25:44 +0100602
Heiko Schocherff94bc42014-06-24 10:10:04 +0200603 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
604 if (err) {
605 mutex_unlock(&ubi->buf_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200606 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100607 goto write_error;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200608 }
Kyungmin Park961df832008-11-19 16:25:44 +0100609
610 mutex_unlock(&ubi->buf_mutex);
611 ubi_free_vid_hdr(ubi, vid_hdr);
612
613 vol->eba_tbl[lnum] = new_pnum;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200614 up_read(&ubi->fm_eba_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200615 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +0100616
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200617 ubi_msg(ubi, "data was successfully recovered");
Kyungmin Park961df832008-11-19 16:25:44 +0100618 return 0;
619
Heiko Schocherff94bc42014-06-24 10:10:04 +0200620out_unlock:
Kyungmin Park961df832008-11-19 16:25:44 +0100621 mutex_unlock(&ubi->buf_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200622out_put:
623 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +0100624 ubi_free_vid_hdr(ubi, vid_hdr);
625 return err;
626
627write_error:
628 /*
629 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
630 * get another one.
631 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200632 ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200633 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +0100634 if (++tries > UBI_IO_RETRIES) {
Kyungmin Park961df832008-11-19 16:25:44 +0100635 ubi_free_vid_hdr(ubi, vid_hdr);
636 return err;
637 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200638 ubi_msg(ubi, "try again");
Kyungmin Park961df832008-11-19 16:25:44 +0100639 goto retry;
640}
641
642/**
643 * ubi_eba_write_leb - write data to dynamic volume.
644 * @ubi: UBI device description object
645 * @vol: volume description object
646 * @lnum: logical eraseblock number
647 * @buf: the data to write
648 * @offset: offset within the logical eraseblock where to write
649 * @len: how many bytes to write
Kyungmin Park961df832008-11-19 16:25:44 +0100650 *
651 * This function writes data to logical eraseblock @lnum of a dynamic volume
652 * @vol. Returns zero in case of success and a negative error code in case
653 * of failure. In case of error, it is possible that something was still
654 * written to the flash media, but may be some garbage.
655 */
656int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657 const void *buf, int offset, int len)
Kyungmin Park961df832008-11-19 16:25:44 +0100658{
659 int err, pnum, tries = 0, vol_id = vol->vol_id;
660 struct ubi_vid_hdr *vid_hdr;
661
662 if (ubi->ro_mode)
663 return -EROFS;
664
665 err = leb_write_lock(ubi, vol_id, lnum);
666 if (err)
667 return err;
668
669 pnum = vol->eba_tbl[lnum];
670 if (pnum >= 0) {
671 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
672 len, offset, vol_id, lnum, pnum);
673
674 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
675 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200676 ubi_warn(ubi, "failed to write data to PEB %d", pnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100677 if (err == -EIO && ubi->bad_allowed)
678 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
679 offset, len);
680 if (err)
681 ubi_ro_mode(ubi);
682 }
683 leb_write_unlock(ubi, vol_id, lnum);
684 return err;
685 }
686
687 /*
688 * The logical eraseblock is not mapped. We have to get a free physical
689 * eraseblock and write the volume identifier header there first.
690 */
691 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
692 if (!vid_hdr) {
693 leb_write_unlock(ubi, vol_id, lnum);
694 return -ENOMEM;
695 }
696
697 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200698 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Kyungmin Park961df832008-11-19 16:25:44 +0100699 vid_hdr->vol_id = cpu_to_be32(vol_id);
700 vid_hdr->lnum = cpu_to_be32(lnum);
701 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
702 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
703
704retry:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200705 pnum = ubi_wl_get_peb(ubi);
Kyungmin Park961df832008-11-19 16:25:44 +0100706 if (pnum < 0) {
707 ubi_free_vid_hdr(ubi, vid_hdr);
708 leb_write_unlock(ubi, vol_id, lnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200709 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100710 return pnum;
711 }
712
713 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
714 len, offset, vol_id, lnum, pnum);
715
716 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
717 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200718 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +0100719 vol_id, lnum, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200720 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100721 goto write_error;
722 }
723
724 if (len) {
725 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
726 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200727 ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200728 len, offset, vol_id, lnum, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200729 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100730 goto write_error;
731 }
732 }
733
734 vol->eba_tbl[lnum] = pnum;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200735 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100736
737 leb_write_unlock(ubi, vol_id, lnum);
738 ubi_free_vid_hdr(ubi, vid_hdr);
739 return 0;
740
741write_error:
742 if (err != -EIO || !ubi->bad_allowed) {
743 ubi_ro_mode(ubi);
744 leb_write_unlock(ubi, vol_id, lnum);
745 ubi_free_vid_hdr(ubi, vid_hdr);
746 return err;
747 }
748
749 /*
750 * Fortunately, this is the first write operation to this physical
751 * eraseblock, so just put it and request a new one. We assume that if
752 * this physical eraseblock went bad, the erase code will handle that.
753 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200754 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +0100755 if (err || ++tries > UBI_IO_RETRIES) {
756 ubi_ro_mode(ubi);
757 leb_write_unlock(ubi, vol_id, lnum);
758 ubi_free_vid_hdr(ubi, vid_hdr);
759 return err;
760 }
761
Heiko Schocherff94bc42014-06-24 10:10:04 +0200762 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200763 ubi_msg(ubi, "try another PEB");
Kyungmin Park961df832008-11-19 16:25:44 +0100764 goto retry;
765}
766
767/**
768 * ubi_eba_write_leb_st - write data to static volume.
769 * @ubi: UBI device description object
770 * @vol: volume description object
771 * @lnum: logical eraseblock number
772 * @buf: data to write
773 * @len: how many bytes to write
Kyungmin Park961df832008-11-19 16:25:44 +0100774 * @used_ebs: how many logical eraseblocks will this volume contain
775 *
776 * This function writes data to logical eraseblock @lnum of static volume
777 * @vol. The @used_ebs argument should contain total number of logical
778 * eraseblock in this static volume.
779 *
780 * When writing to the last logical eraseblock, the @len argument doesn't have
781 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
782 * to the real data size, although the @buf buffer has to contain the
783 * alignment. In all other cases, @len has to be aligned.
784 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200785 * It is prohibited to write more than once to logical eraseblocks of static
Kyungmin Park961df832008-11-19 16:25:44 +0100786 * volumes. This function returns zero in case of success and a negative error
787 * code in case of failure.
788 */
789int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790 int lnum, const void *buf, int len, int used_ebs)
Kyungmin Park961df832008-11-19 16:25:44 +0100791{
792 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
793 struct ubi_vid_hdr *vid_hdr;
794 uint32_t crc;
795
796 if (ubi->ro_mode)
797 return -EROFS;
798
799 if (lnum == used_ebs - 1)
800 /* If this is the last LEB @len may be unaligned */
801 len = ALIGN(data_size, ubi->min_io_size);
802 else
803 ubi_assert(!(len & (ubi->min_io_size - 1)));
804
805 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
806 if (!vid_hdr)
807 return -ENOMEM;
808
809 err = leb_write_lock(ubi, vol_id, lnum);
810 if (err) {
811 ubi_free_vid_hdr(ubi, vid_hdr);
812 return err;
813 }
814
Heiko Schocherff94bc42014-06-24 10:10:04 +0200815 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Kyungmin Park961df832008-11-19 16:25:44 +0100816 vid_hdr->vol_id = cpu_to_be32(vol_id);
817 vid_hdr->lnum = cpu_to_be32(lnum);
818 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
819 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
820
821 crc = crc32(UBI_CRC32_INIT, buf, data_size);
822 vid_hdr->vol_type = UBI_VID_STATIC;
823 vid_hdr->data_size = cpu_to_be32(data_size);
824 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
825 vid_hdr->data_crc = cpu_to_be32(crc);
826
827retry:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200828 pnum = ubi_wl_get_peb(ubi);
Kyungmin Park961df832008-11-19 16:25:44 +0100829 if (pnum < 0) {
830 ubi_free_vid_hdr(ubi, vid_hdr);
831 leb_write_unlock(ubi, vol_id, lnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200832 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100833 return pnum;
834 }
835
836 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
837 len, vol_id, lnum, pnum, used_ebs);
838
839 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
840 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200841 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +0100842 vol_id, lnum, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200843 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100844 goto write_error;
845 }
846
847 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
848 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200849 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +0100850 len, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200851 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100852 goto write_error;
853 }
854
855 ubi_assert(vol->eba_tbl[lnum] < 0);
856 vol->eba_tbl[lnum] = pnum;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200857 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100858
859 leb_write_unlock(ubi, vol_id, lnum);
860 ubi_free_vid_hdr(ubi, vid_hdr);
861 return 0;
862
863write_error:
864 if (err != -EIO || !ubi->bad_allowed) {
865 /*
866 * This flash device does not admit of bad eraseblocks or
867 * something nasty and unexpected happened. Switch to read-only
868 * mode just in case.
869 */
870 ubi_ro_mode(ubi);
871 leb_write_unlock(ubi, vol_id, lnum);
872 ubi_free_vid_hdr(ubi, vid_hdr);
873 return err;
874 }
875
Heiko Schocherff94bc42014-06-24 10:10:04 +0200876 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +0100877 if (err || ++tries > UBI_IO_RETRIES) {
878 ubi_ro_mode(ubi);
879 leb_write_unlock(ubi, vol_id, lnum);
880 ubi_free_vid_hdr(ubi, vid_hdr);
881 return err;
882 }
883
Heiko Schocherff94bc42014-06-24 10:10:04 +0200884 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200885 ubi_msg(ubi, "try another PEB");
Kyungmin Park961df832008-11-19 16:25:44 +0100886 goto retry;
887}
888
889/*
890 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
891 * @ubi: UBI device description object
892 * @vol: volume description object
893 * @lnum: logical eraseblock number
894 * @buf: data to write
895 * @len: how many bytes to write
Kyungmin Park961df832008-11-19 16:25:44 +0100896 *
897 * This function changes the contents of a logical eraseblock atomically. @buf
898 * has to contain new logical eraseblock data, and @len - the length of the
899 * data, which has to be aligned. This function guarantees that in case of an
900 * unclean reboot the old contents is preserved. Returns zero in case of
901 * success and a negative error code in case of failure.
902 *
903 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
904 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
905 */
906int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200907 int lnum, const void *buf, int len)
Kyungmin Park961df832008-11-19 16:25:44 +0100908{
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200909 int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
Kyungmin Park961df832008-11-19 16:25:44 +0100910 struct ubi_vid_hdr *vid_hdr;
911 uint32_t crc;
912
913 if (ubi->ro_mode)
914 return -EROFS;
915
916 if (len == 0) {
917 /*
918 * Special case when data length is zero. In this case the LEB
919 * has to be unmapped and mapped somewhere else.
920 */
921 err = ubi_eba_unmap_leb(ubi, vol, lnum);
922 if (err)
923 return err;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200924 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
Kyungmin Park961df832008-11-19 16:25:44 +0100925 }
926
927 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
928 if (!vid_hdr)
929 return -ENOMEM;
930
931 mutex_lock(&ubi->alc_mutex);
932 err = leb_write_lock(ubi, vol_id, lnum);
933 if (err)
934 goto out_mutex;
935
Heiko Schocherff94bc42014-06-24 10:10:04 +0200936 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Kyungmin Park961df832008-11-19 16:25:44 +0100937 vid_hdr->vol_id = cpu_to_be32(vol_id);
938 vid_hdr->lnum = cpu_to_be32(lnum);
939 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
940 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
941
942 crc = crc32(UBI_CRC32_INIT, buf, len);
943 vid_hdr->vol_type = UBI_VID_DYNAMIC;
944 vid_hdr->data_size = cpu_to_be32(len);
945 vid_hdr->copy_flag = 1;
946 vid_hdr->data_crc = cpu_to_be32(crc);
947
948retry:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200949 pnum = ubi_wl_get_peb(ubi);
Kyungmin Park961df832008-11-19 16:25:44 +0100950 if (pnum < 0) {
951 err = pnum;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200952 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100953 goto out_leb_unlock;
954 }
955
956 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
957 vol_id, lnum, vol->eba_tbl[lnum], pnum);
958
959 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
960 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200961 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +0100962 vol_id, lnum, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200963 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100964 goto write_error;
965 }
966
967 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
968 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200969 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +0100970 len, pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200971 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +0100972 goto write_error;
973 }
974
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200975 old_pnum = vol->eba_tbl[lnum];
976 vol->eba_tbl[lnum] = pnum;
977 up_read(&ubi->fm_eba_sem);
978
979 if (old_pnum >= 0) {
980 err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
Kyungmin Park961df832008-11-19 16:25:44 +0100981 if (err)
982 goto out_leb_unlock;
983 }
984
Kyungmin Park961df832008-11-19 16:25:44 +0100985out_leb_unlock:
986 leb_write_unlock(ubi, vol_id, lnum);
987out_mutex:
988 mutex_unlock(&ubi->alc_mutex);
989 ubi_free_vid_hdr(ubi, vid_hdr);
990 return err;
991
992write_error:
993 if (err != -EIO || !ubi->bad_allowed) {
994 /*
995 * This flash device does not admit of bad eraseblocks or
996 * something nasty and unexpected happened. Switch to read-only
997 * mode just in case.
998 */
999 ubi_ro_mode(ubi);
1000 goto out_leb_unlock;
1001 }
1002
Heiko Schocherff94bc42014-06-24 10:10:04 +02001003 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
Kyungmin Park961df832008-11-19 16:25:44 +01001004 if (err || ++tries > UBI_IO_RETRIES) {
1005 ubi_ro_mode(ubi);
1006 goto out_leb_unlock;
1007 }
1008
Heiko Schocherff94bc42014-06-24 10:10:04 +02001009 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001010 ubi_msg(ubi, "try another PEB");
Kyungmin Park961df832008-11-19 16:25:44 +01001011 goto retry;
1012}
1013
1014/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001015 * is_error_sane - check whether a read error is sane.
1016 * @err: code of the error happened during reading
1017 *
1018 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
1019 * cannot read data from the target PEB (an error @err happened). If the error
1020 * code is sane, then we treat this error as non-fatal. Otherwise the error is
1021 * fatal and UBI will be switched to R/O mode later.
1022 *
1023 * The idea is that we try not to switch to R/O mode if the read error is
1024 * something which suggests there was a real read problem. E.g., %-EIO. Or a
1025 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
1026 * mode, simply because we do not know what happened at the MTD level, and we
1027 * cannot handle this. E.g., the underlying driver may have become crazy, and
1028 * it is safer to switch to R/O mode to preserve the data.
1029 *
1030 * And bear in mind, this is about reading from the target PEB, i.e. the PEB
1031 * which we have just written.
1032 */
1033static int is_error_sane(int err)
1034{
1035 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
1036 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1037 return 0;
1038 return 1;
1039}
1040
1041/**
Kyungmin Park961df832008-11-19 16:25:44 +01001042 * ubi_eba_copy_leb - copy logical eraseblock.
1043 * @ubi: UBI device description object
1044 * @from: physical eraseblock number from where to copy
1045 * @to: physical eraseblock number where to copy
1046 * @vid_hdr: VID header of the @from physical eraseblock
1047 *
1048 * This function copies logical eraseblock from physical eraseblock @from to
1049 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1050 * function. Returns:
Heiko Schocherff94bc42014-06-24 10:10:04 +02001051 * o %0 in case of success;
1052 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1053 * o a negative error code in case of failure.
Kyungmin Park961df832008-11-19 16:25:44 +01001054 */
1055int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1056 struct ubi_vid_hdr *vid_hdr)
1057{
1058 int err, vol_id, lnum, data_size, aldata_size, idx;
1059 struct ubi_volume *vol;
1060 uint32_t crc;
1061
1062 vol_id = be32_to_cpu(vid_hdr->vol_id);
1063 lnum = be32_to_cpu(vid_hdr->lnum);
1064
Heiko Schocherff94bc42014-06-24 10:10:04 +02001065 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
Kyungmin Park961df832008-11-19 16:25:44 +01001066
1067 if (vid_hdr->vol_type == UBI_VID_STATIC) {
1068 data_size = be32_to_cpu(vid_hdr->data_size);
1069 aldata_size = ALIGN(data_size, ubi->min_io_size);
1070 } else
1071 data_size = aldata_size =
1072 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1073
1074 idx = vol_id2idx(ubi, vol_id);
1075 spin_lock(&ubi->volumes_lock);
1076 /*
1077 * Note, we may race with volume deletion, which means that the volume
1078 * this logical eraseblock belongs to might be being deleted. Since the
Heiko Schocherff94bc42014-06-24 10:10:04 +02001079 * volume deletion un-maps all the volume's logical eraseblocks, it will
Kyungmin Park961df832008-11-19 16:25:44 +01001080 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1081 */
1082 vol = ubi->volumes[idx];
Heiko Schocherff94bc42014-06-24 10:10:04 +02001083 spin_unlock(&ubi->volumes_lock);
Kyungmin Park961df832008-11-19 16:25:44 +01001084 if (!vol) {
1085 /* No need to do further work, cancel */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001086 dbg_wl("volume %d is being removed, cancel", vol_id);
1087 return MOVE_CANCEL_RACE;
Kyungmin Park961df832008-11-19 16:25:44 +01001088 }
Kyungmin Park961df832008-11-19 16:25:44 +01001089
1090 /*
1091 * We do not want anybody to write to this logical eraseblock while we
1092 * are moving it, so lock it.
1093 *
1094 * Note, we are using non-waiting locking here, because we cannot sleep
1095 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1096 * unmapping the LEB which is mapped to the PEB we are going to move
1097 * (@from). This task locks the LEB and goes sleep in the
1098 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1099 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
Heiko Schocherff94bc42014-06-24 10:10:04 +02001100 * LEB is already locked, we just do not move it and return
1101 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
1102 * we do not know the reasons of the contention - it may be just a
1103 * normal I/O on this LEB, so we want to re-try.
Kyungmin Park961df832008-11-19 16:25:44 +01001104 */
1105 err = leb_write_trylock(ubi, vol_id, lnum);
1106 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001107 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1108 return MOVE_RETRY;
Kyungmin Park961df832008-11-19 16:25:44 +01001109 }
1110
1111 /*
1112 * The LEB might have been put meanwhile, and the task which put it is
1113 * probably waiting on @ubi->move_mutex. No need to continue the work,
1114 * cancel it.
1115 */
1116 if (vol->eba_tbl[lnum] != from) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001117 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1118 vol_id, lnum, from, vol->eba_tbl[lnum]);
1119 err = MOVE_CANCEL_RACE;
Kyungmin Park961df832008-11-19 16:25:44 +01001120 goto out_unlock_leb;
1121 }
1122
1123 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001124 * OK, now the LEB is locked and we can safely start moving it. Since
1125 * this function utilizes the @ubi->peb_buf buffer which is shared
1126 * with some other functions - we lock the buffer by taking the
Kyungmin Park961df832008-11-19 16:25:44 +01001127 * @ubi->buf_mutex.
1128 */
1129 mutex_lock(&ubi->buf_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001130 dbg_wl("read %d bytes of data", aldata_size);
1131 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
Kyungmin Park961df832008-11-19 16:25:44 +01001132 if (err && err != UBI_IO_BITFLIPS) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001133 ubi_warn(ubi, "error %d while reading data from PEB %d",
Kyungmin Park961df832008-11-19 16:25:44 +01001134 err, from);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001135 err = MOVE_SOURCE_RD_ERR;
Kyungmin Park961df832008-11-19 16:25:44 +01001136 goto out_unlock_buf;
1137 }
1138
1139 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001140 * Now we have got to calculate how much data we have to copy. In
Kyungmin Park961df832008-11-19 16:25:44 +01001141 * case of a static volume it is fairly easy - the VID header contains
1142 * the data size. In case of a dynamic volume it is more difficult - we
1143 * have to read the contents, cut 0xFF bytes from the end and copy only
1144 * the first part. We must do this to avoid writing 0xFF bytes as it
1145 * may have some side-effects. And not only this. It is important not
1146 * to include those 0xFFs to CRC because later the they may be filled
1147 * by data.
1148 */
1149 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1150 aldata_size = data_size =
Heiko Schocherff94bc42014-06-24 10:10:04 +02001151 ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
Kyungmin Park961df832008-11-19 16:25:44 +01001152
1153 cond_resched();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001154 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
Kyungmin Park961df832008-11-19 16:25:44 +01001155 cond_resched();
1156
1157 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001158 * It may turn out to be that the whole @from physical eraseblock
Kyungmin Park961df832008-11-19 16:25:44 +01001159 * contains only 0xFF bytes. Then we have to only write the VID header
1160 * and do not write any data. This also means we should not set
1161 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1162 */
1163 if (data_size > 0) {
1164 vid_hdr->copy_flag = 1;
1165 vid_hdr->data_size = cpu_to_be32(data_size);
1166 vid_hdr->data_crc = cpu_to_be32(crc);
1167 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001168 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
Kyungmin Park961df832008-11-19 16:25:44 +01001169
1170 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001171 if (err) {
1172 if (err == -EIO)
1173 err = MOVE_TARGET_WR_ERR;
Kyungmin Park961df832008-11-19 16:25:44 +01001174 goto out_unlock_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001175 }
Kyungmin Park961df832008-11-19 16:25:44 +01001176
1177 cond_resched();
1178
1179 /* Read the VID header back and check if it was written correctly */
1180 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1181 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001182 if (err != UBI_IO_BITFLIPS) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001183 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001184 err, to);
1185 if (is_error_sane(err))
1186 err = MOVE_TARGET_RD_ERR;
1187 } else
1188 err = MOVE_TARGET_BITFLIPS;
Kyungmin Park961df832008-11-19 16:25:44 +01001189 goto out_unlock_buf;
1190 }
1191
1192 if (data_size > 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001193 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1194 if (err) {
1195 if (err == -EIO)
1196 err = MOVE_TARGET_WR_ERR;
Kyungmin Park961df832008-11-19 16:25:44 +01001197 goto out_unlock_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001198 }
Kyungmin Park961df832008-11-19 16:25:44 +01001199
1200 cond_resched();
1201
1202 /*
1203 * We've written the data and are going to read it back to make
1204 * sure it was written correctly.
1205 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001206 memset(ubi->peb_buf, 0xFF, aldata_size);
1207 err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
Kyungmin Park961df832008-11-19 16:25:44 +01001208 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001209 if (err != UBI_IO_BITFLIPS) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001210 ubi_warn(ubi, "error %d while reading data back from PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001211 err, to);
1212 if (is_error_sane(err))
1213 err = MOVE_TARGET_RD_ERR;
1214 } else
1215 err = MOVE_TARGET_BITFLIPS;
Kyungmin Park961df832008-11-19 16:25:44 +01001216 goto out_unlock_buf;
1217 }
1218
1219 cond_resched();
1220
Heiko Schocherff94bc42014-06-24 10:10:04 +02001221 if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001222 ubi_warn(ubi, "read data back from PEB %d and it is different",
Kyungmin Park961df832008-11-19 16:25:44 +01001223 to);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001224 err = -EINVAL;
Kyungmin Park961df832008-11-19 16:25:44 +01001225 goto out_unlock_buf;
1226 }
1227 }
1228
1229 ubi_assert(vol->eba_tbl[lnum] == from);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001230 down_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +01001231 vol->eba_tbl[lnum] = to;
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001232 up_read(&ubi->fm_eba_sem);
Kyungmin Park961df832008-11-19 16:25:44 +01001233
1234out_unlock_buf:
1235 mutex_unlock(&ubi->buf_mutex);
1236out_unlock_leb:
1237 leb_write_unlock(ubi, vol_id, lnum);
1238 return err;
1239}
1240
1241/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001242 * print_rsvd_warning - warn about not having enough reserved PEBs.
Kyungmin Park961df832008-11-19 16:25:44 +01001243 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001244 *
1245 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1246 * cannot reserve enough PEBs for bad block handling. This function makes a
1247 * decision whether we have to print a warning or not. The algorithm is as
1248 * follows:
1249 * o if this is a new UBI image, then just print the warning
1250 * o if this is an UBI image which has already been used for some time, print
1251 * a warning only if we can reserve less than 10% of the expected amount of
1252 * the reserved PEB.
1253 *
1254 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1255 * of PEBs becomes smaller, which is normal and we do not want to scare users
1256 * with a warning every time they attach the MTD device. This was an issue
1257 * reported by real users.
1258 */
1259static void print_rsvd_warning(struct ubi_device *ubi,
1260 struct ubi_attach_info *ai)
1261{
1262 /*
1263 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1264 * large number to distinguish between newly flashed and used images.
1265 */
1266 if (ai->max_sqnum > (1 << 18)) {
1267 int min = ubi->beb_rsvd_level / 10;
1268
1269 if (!min)
1270 min = 1;
1271 if (ubi->beb_rsvd_pebs > min)
1272 return;
1273 }
1274
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001275 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001276 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1277 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001278 ubi_warn(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001279 ubi->corr_peb_count);
1280}
1281
1282/**
1283 * self_check_eba - run a self check on the EBA table constructed by fastmap.
1284 * @ubi: UBI device description object
1285 * @ai_fastmap: UBI attach info object created by fastmap
1286 * @ai_scan: UBI attach info object created by scanning
1287 *
1288 * Returns < 0 in case of an internal error, 0 otherwise.
1289 * If a bad EBA table entry was found it will be printed out and
1290 * ubi_assert() triggers.
1291 */
1292int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
1293 struct ubi_attach_info *ai_scan)
1294{
1295 int i, j, num_volumes, ret = 0;
1296 int **scan_eba, **fm_eba;
1297 struct ubi_ainf_volume *av;
1298 struct ubi_volume *vol;
1299 struct ubi_ainf_peb *aeb;
1300 struct rb_node *rb;
1301
1302 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1303
1304 scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
1305 if (!scan_eba)
1306 return -ENOMEM;
1307
1308 fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
1309 if (!fm_eba) {
1310 kfree(scan_eba);
1311 return -ENOMEM;
1312 }
1313
1314 for (i = 0; i < num_volumes; i++) {
1315 vol = ubi->volumes[i];
1316 if (!vol)
1317 continue;
1318
1319 scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
1320 GFP_KERNEL);
1321 if (!scan_eba[i]) {
1322 ret = -ENOMEM;
1323 goto out_free;
1324 }
1325
1326 fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
1327 GFP_KERNEL);
1328 if (!fm_eba[i]) {
1329 ret = -ENOMEM;
1330 goto out_free;
1331 }
1332
1333 for (j = 0; j < vol->reserved_pebs; j++)
1334 scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
1335
1336 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
1337 if (!av)
1338 continue;
1339
1340 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1341 scan_eba[i][aeb->lnum] = aeb->pnum;
1342
1343 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
1344 if (!av)
1345 continue;
1346
1347 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1348 fm_eba[i][aeb->lnum] = aeb->pnum;
1349
1350 for (j = 0; j < vol->reserved_pebs; j++) {
1351 if (scan_eba[i][j] != fm_eba[i][j]) {
1352 if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
1353 fm_eba[i][j] == UBI_LEB_UNMAPPED)
1354 continue;
1355
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001356 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001357 vol->vol_id, i, fm_eba[i][j],
1358 scan_eba[i][j]);
1359 ubi_assert(0);
1360 }
1361 }
1362 }
1363
1364out_free:
1365 for (i = 0; i < num_volumes; i++) {
1366 if (!ubi->volumes[i])
1367 continue;
1368
1369 kfree(scan_eba[i]);
1370 kfree(fm_eba[i]);
1371 }
1372
1373 kfree(scan_eba);
1374 kfree(fm_eba);
1375 return ret;
1376}
1377
1378/**
1379 * ubi_eba_init - initialize the EBA sub-system using attaching information.
1380 * @ubi: UBI device description object
1381 * @ai: attaching information
Kyungmin Park961df832008-11-19 16:25:44 +01001382 *
1383 * This function returns zero in case of success and a negative error code in
1384 * case of failure.
1385 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001386int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
Kyungmin Park961df832008-11-19 16:25:44 +01001387{
1388 int i, j, err, num_volumes;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001389 struct ubi_ainf_volume *av;
Kyungmin Park961df832008-11-19 16:25:44 +01001390 struct ubi_volume *vol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001391 struct ubi_ainf_peb *aeb;
Kyungmin Park961df832008-11-19 16:25:44 +01001392 struct rb_node *rb;
1393
Heiko Schocherff94bc42014-06-24 10:10:04 +02001394 dbg_eba("initialize EBA sub-system");
Kyungmin Park961df832008-11-19 16:25:44 +01001395
1396 spin_lock_init(&ubi->ltree_lock);
1397 mutex_init(&ubi->alc_mutex);
1398 ubi->ltree = RB_ROOT;
1399
Heiko Schocherff94bc42014-06-24 10:10:04 +02001400 ubi->global_sqnum = ai->max_sqnum + 1;
Kyungmin Park961df832008-11-19 16:25:44 +01001401 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1402
1403 for (i = 0; i < num_volumes; i++) {
1404 vol = ubi->volumes[i];
1405 if (!vol)
1406 continue;
1407
1408 cond_resched();
1409
1410 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1411 GFP_KERNEL);
1412 if (!vol->eba_tbl) {
1413 err = -ENOMEM;
1414 goto out_free;
1415 }
1416
1417 for (j = 0; j < vol->reserved_pebs; j++)
1418 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1419
Heiko Schocherff94bc42014-06-24 10:10:04 +02001420 av = ubi_find_av(ai, idx2vol_id(ubi, i));
1421 if (!av)
Kyungmin Park961df832008-11-19 16:25:44 +01001422 continue;
1423
Heiko Schocherff94bc42014-06-24 10:10:04 +02001424 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1425 if (aeb->lnum >= vol->reserved_pebs)
Kyungmin Park961df832008-11-19 16:25:44 +01001426 /*
1427 * This may happen in case of an unclean reboot
1428 * during re-size.
1429 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001430 ubi_move_aeb_to_list(av, aeb, &ai->erase);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001431 else
1432 vol->eba_tbl[aeb->lnum] = aeb->pnum;
Kyungmin Park961df832008-11-19 16:25:44 +01001433 }
1434 }
1435
1436 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001437 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
Kyungmin Park961df832008-11-19 16:25:44 +01001438 ubi->avail_pebs, EBA_RESERVED_PEBS);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001439 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001440 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001441 ubi->corr_peb_count);
Kyungmin Park961df832008-11-19 16:25:44 +01001442 err = -ENOSPC;
1443 goto out_free;
1444 }
1445 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1446 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1447
1448 if (ubi->bad_allowed) {
1449 ubi_calculate_reserved(ubi);
1450
1451 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1452 /* No enough free physical eraseblocks */
1453 ubi->beb_rsvd_pebs = ubi->avail_pebs;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001454 print_rsvd_warning(ubi, ai);
Kyungmin Park961df832008-11-19 16:25:44 +01001455 } else
1456 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1457
1458 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1459 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1460 }
1461
Heiko Schocherff94bc42014-06-24 10:10:04 +02001462 dbg_eba("EBA sub-system is initialized");
Kyungmin Park961df832008-11-19 16:25:44 +01001463 return 0;
1464
1465out_free:
1466 for (i = 0; i < num_volumes; i++) {
1467 if (!ubi->volumes[i])
1468 continue;
1469 kfree(ubi->volumes[i]->eba_tbl);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001470 ubi->volumes[i]->eba_tbl = NULL;
Kyungmin Park961df832008-11-19 16:25:44 +01001471 }
1472 return err;
1473}