blob: 81bb6a33b555836a4f4bbe9d1f4cc0eb1caa66d3 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * The UBI Eraseblock Association (EBA) unit.
23 *
24 * This unit is responsible for I/O to/from logical eraseblock.
25 *
26 * Although in this implementation the EBA table is fully kept and managed in
27 * RAM, which assumes poor scalability, it might be (partially) maintained on
28 * flash in future implementations.
29 *
30 * The EBA unit implements per-logical eraseblock locking. Before accessing a
31 * logical eraseblock it is locked for reading or writing. The per-logical
32 * eraseblock locking is implemented by means of the lock tree. The lock tree
33 * is an RB-tree which refers all the currently locked logical eraseblocks. The
34 * lock tree elements are &struct ltree_entry objects. They are indexed by
35 * (@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/**
50 * struct ltree_entry - an entry in the lock tree.
51 * @rb: links RB-tree nodes
52 * @vol_id: volume ID of the locked logical eraseblock
53 * @lnum: locked logical eraseblock number
54 * @users: how many tasks are using this logical eraseblock or wait for it
55 * @mutex: read/write mutex to implement read/write access serialization to
56 * the (@vol_id, @lnum) logical eraseblock
57 *
58 * When a logical eraseblock is being locked - corresponding &struct ltree_entry
59 * object is inserted to the lock tree (@ubi->ltree).
60 */
61struct ltree_entry {
62 struct rb_node rb;
63 int vol_id;
64 int lnum;
65 int users;
66 struct rw_semaphore mutex;
67};
68
69/* Slab cache for lock-tree entries */
70static struct kmem_cache *ltree_slab;
71
72/**
73 * next_sqnum - get next sequence number.
74 * @ubi: UBI device description object
75 *
76 * This function returns next sequence number to use, which is just the current
77 * global sequence counter value. It also increases the global sequence
78 * counter.
79 */
80static unsigned long long next_sqnum(struct ubi_device *ubi)
81{
82 unsigned long long sqnum;
83
84 spin_lock(&ubi->ltree_lock);
85 sqnum = ubi->global_sqnum++;
86 spin_unlock(&ubi->ltree_lock);
87
88 return sqnum;
89}
90
91/**
92 * ubi_get_compat - get compatibility flags of a volume.
93 * @ubi: UBI device description object
94 * @vol_id: volume ID
95 *
96 * This function returns compatibility flags for an internal volume. User
97 * volumes have no compatibility flags, so %0 is returned.
98 */
99static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
100{
101 if (vol_id == UBI_LAYOUT_VOL_ID)
102 return UBI_LAYOUT_VOLUME_COMPAT;
103 return 0;
104}
105
106/**
107 * ltree_lookup - look up the lock tree.
108 * @ubi: UBI device description object
109 * @vol_id: volume ID
110 * @lnum: logical eraseblock number
111 *
112 * This function returns a pointer to the corresponding &struct ltree_entry
113 * object if the logical eraseblock is locked and %NULL if it is not.
114 * @ubi->ltree_lock has to be locked.
115 */
116static struct ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
117 int lnum)
118{
119 struct rb_node *p;
120
121 p = ubi->ltree.rb_node;
122 while (p) {
123 struct ltree_entry *le;
124
125 le = rb_entry(p, struct ltree_entry, rb);
126
127 if (vol_id < le->vol_id)
128 p = p->rb_left;
129 else if (vol_id > le->vol_id)
130 p = p->rb_right;
131 else {
132 if (lnum < le->lnum)
133 p = p->rb_left;
134 else if (lnum > le->lnum)
135 p = p->rb_right;
136 else
137 return le;
138 }
139 }
140
141 return NULL;
142}
143
144/**
145 * ltree_add_entry - add new entry to the lock tree.
146 * @ubi: UBI device description object
147 * @vol_id: volume ID
148 * @lnum: logical eraseblock number
149 *
150 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
151 * lock tree. If such entry is already there, its usage counter is increased.
152 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
153 * failed.
154 */
155static struct ltree_entry *ltree_add_entry(struct ubi_device *ubi, int vol_id,
156 int lnum)
157{
158 struct ltree_entry *le, *le1, *le_free;
159
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300160 le = kmem_cache_alloc(ltree_slab, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400161 if (!le)
162 return ERR_PTR(-ENOMEM);
163
164 le->vol_id = vol_id;
165 le->lnum = lnum;
166
167 spin_lock(&ubi->ltree_lock);
168 le1 = ltree_lookup(ubi, vol_id, lnum);
169
170 if (le1) {
171 /*
172 * This logical eraseblock is already locked. The newly
173 * allocated lock entry is not needed.
174 */
175 le_free = le;
176 le = le1;
177 } else {
178 struct rb_node **p, *parent = NULL;
179
180 /*
181 * No lock entry, add the newly allocated one to the
182 * @ubi->ltree RB-tree.
183 */
184 le_free = NULL;
185
186 p = &ubi->ltree.rb_node;
187 while (*p) {
188 parent = *p;
189 le1 = rb_entry(parent, struct ltree_entry, rb);
190
191 if (vol_id < le1->vol_id)
192 p = &(*p)->rb_left;
193 else if (vol_id > le1->vol_id)
194 p = &(*p)->rb_right;
195 else {
196 ubi_assert(lnum != le1->lnum);
197 if (lnum < le1->lnum)
198 p = &(*p)->rb_left;
199 else
200 p = &(*p)->rb_right;
201 }
202 }
203
204 rb_link_node(&le->rb, parent, p);
205 rb_insert_color(&le->rb, &ubi->ltree);
206 }
207 le->users += 1;
208 spin_unlock(&ubi->ltree_lock);
209
210 if (le_free)
211 kmem_cache_free(ltree_slab, le_free);
212
213 return le;
214}
215
216/**
217 * leb_read_lock - lock logical eraseblock for reading.
218 * @ubi: UBI device description object
219 * @vol_id: volume ID
220 * @lnum: logical eraseblock number
221 *
222 * This function locks a logical eraseblock for reading. Returns zero in case
223 * of success and a negative error code in case of failure.
224 */
225static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
226{
227 struct ltree_entry *le;
228
229 le = ltree_add_entry(ubi, vol_id, lnum);
230 if (IS_ERR(le))
231 return PTR_ERR(le);
232 down_read(&le->mutex);
233 return 0;
234}
235
236/**
237 * leb_read_unlock - unlock logical eraseblock.
238 * @ubi: UBI device description object
239 * @vol_id: volume ID
240 * @lnum: logical eraseblock number
241 */
242static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
243{
244 int free = 0;
245 struct ltree_entry *le;
246
247 spin_lock(&ubi->ltree_lock);
248 le = ltree_lookup(ubi, vol_id, lnum);
249 le->users -= 1;
250 ubi_assert(le->users >= 0);
251 if (le->users == 0) {
252 rb_erase(&le->rb, &ubi->ltree);
253 free = 1;
254 }
255 spin_unlock(&ubi->ltree_lock);
256
257 up_read(&le->mutex);
258 if (free)
259 kmem_cache_free(ltree_slab, le);
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. Returns zero in case
269 * of success and a negative error code in case of failure.
270 */
271static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
272{
273 struct ltree_entry *le;
274
275 le = ltree_add_entry(ubi, vol_id, lnum);
276 if (IS_ERR(le))
277 return PTR_ERR(le);
278 down_write(&le->mutex);
279 return 0;
280}
281
282/**
283 * leb_write_unlock - unlock logical eraseblock.
284 * @ubi: UBI device description object
285 * @vol_id: volume ID
286 * @lnum: logical eraseblock number
287 */
288static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
289{
290 int free;
291 struct ltree_entry *le;
292
293 spin_lock(&ubi->ltree_lock);
294 le = ltree_lookup(ubi, vol_id, lnum);
295 le->users -= 1;
296 ubi_assert(le->users >= 0);
297 if (le->users == 0) {
298 rb_erase(&le->rb, &ubi->ltree);
299 free = 1;
300 } else
301 free = 0;
302 spin_unlock(&ubi->ltree_lock);
303
304 up_write(&le->mutex);
305 if (free)
306 kmem_cache_free(ltree_slab, le);
307}
308
309/**
310 * ubi_eba_unmap_leb - un-map logical eraseblock.
311 * @ubi: UBI device description object
312 * @vol_id: volume ID
313 * @lnum: logical eraseblock number
314 *
315 * This function un-maps logical eraseblock @lnum and schedules corresponding
316 * physical eraseblock for erasure. Returns zero in case of success and a
317 * negative error code in case of failure.
318 */
319int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum)
320{
321 int idx = vol_id2idx(ubi, vol_id), err, pnum;
322 struct ubi_volume *vol = ubi->volumes[idx];
323
324 if (ubi->ro_mode)
325 return -EROFS;
326
327 err = leb_write_lock(ubi, vol_id, lnum);
328 if (err)
329 return err;
330
331 pnum = vol->eba_tbl[lnum];
332 if (pnum < 0)
333 /* This logical eraseblock is already unmapped */
334 goto out_unlock;
335
336 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
337
338 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
339 err = ubi_wl_put_peb(ubi, pnum, 0);
340
341out_unlock:
342 leb_write_unlock(ubi, vol_id, lnum);
343 return err;
344}
345
346/**
347 * ubi_eba_read_leb - read data.
348 * @ubi: UBI device description object
349 * @vol_id: volume ID
350 * @lnum: logical eraseblock number
351 * @buf: buffer to store the read data
352 * @offset: offset from where to read
353 * @len: how many bytes to read
354 * @check: data CRC check flag
355 *
356 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
357 * bytes. The @check flag only makes sense for static volumes and forces
358 * eraseblock data CRC checking.
359 *
360 * In case of success this function returns zero. In case of a static volume,
361 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
362 * returned for any volume type if an ECC error was detected by the MTD device
363 * driver. Other negative error cored may be returned in case of other errors.
364 */
365int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,
366 int offset, int len, int check)
367{
368 int err, pnum, scrub = 0, idx = vol_id2idx(ubi, vol_id);
369 struct ubi_vid_hdr *vid_hdr;
370 struct ubi_volume *vol = ubi->volumes[idx];
Jeff Garzika6343af2007-07-17 05:39:58 -0400371 uint32_t uninitialized_var(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400372
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) {
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300400 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401 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 */
417 if (err == UBI_IO_BAD_VID_HDR) {
418 ubi_warn("bad VID header at PEB %d, LEB"
419 "%d:%d", pnum, vol_id, lnum);
420 err = -EBADMSG;
421 } else
422 ubi_ro_mode(ubi);
423 }
424 goto out_free;
425 } else if (err == UBI_IO_BITFLIPS)
426 scrub = 1;
427
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300428 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
429 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400430
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300431 crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400432 ubi_free_vid_hdr(ubi, vid_hdr);
433 }
434
435 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
436 if (err) {
437 if (err == UBI_IO_BITFLIPS) {
438 scrub = 1;
439 err = 0;
440 } else if (err == -EBADMSG) {
441 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
442 goto out_unlock;
443 scrub = 1;
444 if (!check) {
445 ubi_msg("force data checking");
446 check = 1;
447 goto retry;
448 }
449 } else
450 goto out_unlock;
451 }
452
453 if (check) {
Jeff Garzik2ab934b2007-07-17 01:49:56 -0400454 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400455 if (crc1 != crc) {
456 ubi_warn("CRC error: calculated %#08x, must be %#08x",
457 crc1, crc);
458 err = -EBADMSG;
459 goto out_unlock;
460 }
461 }
462
463 if (scrub)
464 err = ubi_wl_scrub_peb(ubi, pnum);
465
466 leb_read_unlock(ubi, vol_id, lnum);
467 return err;
468
469out_free:
470 ubi_free_vid_hdr(ubi, vid_hdr);
471out_unlock:
472 leb_read_unlock(ubi, vol_id, lnum);
473 return err;
474}
475
476/**
477 * recover_peb - recover from write failure.
478 * @ubi: UBI device description object
479 * @pnum: the physical eraseblock to recover
480 * @vol_id: volume ID
481 * @lnum: logical eraseblock number
482 * @buf: data which was not written because of the write failure
483 * @offset: offset of the failed write
484 * @len: how many bytes should have been written
485 *
486 * This function is called in case of a write failure and moves all good data
487 * from the potentially bad physical eraseblock to a good physical eraseblock.
488 * This function also writes the data which was not written due to the failure.
489 * Returns new physical eraseblock number in case of success, and a negative
490 * error code in case of failure.
491 */
492static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
493 const void *buf, int offset, int len)
494{
495 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
496 struct ubi_volume *vol = ubi->volumes[idx];
497 struct ubi_vid_hdr *vid_hdr;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400498
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300499 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400500 if (!vid_hdr) {
501 return -ENOMEM;
502 }
503
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300504 mutex_lock(&ubi->buf_mutex);
505
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400506retry:
507 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
508 if (new_pnum < 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300509 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400510 ubi_free_vid_hdr(ubi, vid_hdr);
511 return new_pnum;
512 }
513
514 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
515
516 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
517 if (err && err != UBI_IO_BITFLIPS) {
518 if (err > 0)
519 err = -EIO;
520 goto out_put;
521 }
522
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300523 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400524 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
525 if (err)
526 goto write_error;
527
528 data_size = offset + len;
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300529 memset(ubi->peb_buf1 + offset, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400530
531 /* Read everything before the area where the write failure happened */
532 if (offset > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300533 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
534 if (err && err != UBI_IO_BITFLIPS)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400535 goto out_put;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400536 }
537
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300538 memcpy(ubi->peb_buf1 + offset, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400539
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300540 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
541 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400542 goto write_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400543
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300544 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400545 ubi_free_vid_hdr(ubi, vid_hdr);
546
547 vol->eba_tbl[lnum] = new_pnum;
548 ubi_wl_put_peb(ubi, pnum, 1);
549
550 ubi_msg("data was successfully recovered");
551 return 0;
552
553out_put:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300554 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400555 ubi_wl_put_peb(ubi, new_pnum, 1);
556 ubi_free_vid_hdr(ubi, vid_hdr);
557 return err;
558
559write_error:
560 /*
561 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
562 * get another one.
563 */
564 ubi_warn("failed to write to PEB %d", new_pnum);
565 ubi_wl_put_peb(ubi, new_pnum, 1);
566 if (++tries > UBI_IO_RETRIES) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300567 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568 ubi_free_vid_hdr(ubi, vid_hdr);
569 return err;
570 }
571 ubi_msg("try again");
572 goto retry;
573}
574
575/**
576 * ubi_eba_write_leb - write data to dynamic volume.
577 * @ubi: UBI device description object
578 * @vol_id: volume ID
579 * @lnum: logical eraseblock number
580 * @buf: the data to write
581 * @offset: offset within the logical eraseblock where to write
582 * @len: how many bytes to write
583 * @dtype: data type
584 *
585 * This function writes data to logical eraseblock @lnum of a dynamic volume
586 * @vol_id. Returns zero in case of success and a negative error code in case
587 * of failure. In case of error, it is possible that something was still
588 * written to the flash media, but may be some garbage.
589 */
590int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum,
591 const void *buf, int offset, int len, int dtype)
592{
593 int idx = vol_id2idx(ubi, vol_id), err, pnum, tries = 0;
594 struct ubi_volume *vol = ubi->volumes[idx];
595 struct ubi_vid_hdr *vid_hdr;
596
597 if (ubi->ro_mode)
598 return -EROFS;
599
600 err = leb_write_lock(ubi, vol_id, lnum);
601 if (err)
602 return err;
603
604 pnum = vol->eba_tbl[lnum];
605 if (pnum >= 0) {
606 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
607 len, offset, vol_id, lnum, pnum);
608
609 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
610 if (err) {
611 ubi_warn("failed to write data to PEB %d", pnum);
612 if (err == -EIO && ubi->bad_allowed)
613 err = recover_peb(ubi, pnum, vol_id, lnum, buf, offset, len);
614 if (err)
615 ubi_ro_mode(ubi);
616 }
617 leb_write_unlock(ubi, vol_id, lnum);
618 return err;
619 }
620
621 /*
622 * The logical eraseblock is not mapped. We have to get a free physical
623 * eraseblock and write the volume identifier header there first.
624 */
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300625 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400626 if (!vid_hdr) {
627 leb_write_unlock(ubi, vol_id, lnum);
628 return -ENOMEM;
629 }
630
631 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300632 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
633 vid_hdr->vol_id = cpu_to_be32(vol_id);
634 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400635 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300636 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400637
638retry:
639 pnum = ubi_wl_get_peb(ubi, dtype);
640 if (pnum < 0) {
641 ubi_free_vid_hdr(ubi, vid_hdr);
642 leb_write_unlock(ubi, vol_id, lnum);
643 return pnum;
644 }
645
646 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
647 len, offset, vol_id, lnum, pnum);
648
649 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
650 if (err) {
651 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
652 vol_id, lnum, pnum);
653 goto write_error;
654 }
655
656 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
657 if (err) {
658 ubi_warn("failed to write %d bytes at offset %d of LEB %d:%d, "
659 "PEB %d", len, offset, vol_id, lnum, pnum);
660 goto write_error;
661 }
662
663 vol->eba_tbl[lnum] = pnum;
664
665 leb_write_unlock(ubi, vol_id, lnum);
666 ubi_free_vid_hdr(ubi, vid_hdr);
667 return 0;
668
669write_error:
670 if (err != -EIO || !ubi->bad_allowed) {
671 ubi_ro_mode(ubi);
672 leb_write_unlock(ubi, vol_id, lnum);
673 ubi_free_vid_hdr(ubi, vid_hdr);
674 return err;
675 }
676
677 /*
678 * Fortunately, this is the first write operation to this physical
679 * eraseblock, so just put it and request a new one. We assume that if
680 * this physical eraseblock went bad, the erase code will handle that.
681 */
682 err = ubi_wl_put_peb(ubi, pnum, 1);
683 if (err || ++tries > UBI_IO_RETRIES) {
684 ubi_ro_mode(ubi);
685 leb_write_unlock(ubi, vol_id, lnum);
686 ubi_free_vid_hdr(ubi, vid_hdr);
687 return err;
688 }
689
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300690 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400691 ubi_msg("try another PEB");
692 goto retry;
693}
694
695/**
696 * ubi_eba_write_leb_st - write data to static volume.
697 * @ubi: UBI device description object
698 * @vol_id: volume ID
699 * @lnum: logical eraseblock number
700 * @buf: data to write
701 * @len: how many bytes to write
702 * @dtype: data type
703 * @used_ebs: how many logical eraseblocks will this volume contain
704 *
705 * This function writes data to logical eraseblock @lnum of static volume
706 * @vol_id. The @used_ebs argument should contain total number of logical
707 * eraseblock in this static volume.
708 *
709 * When writing to the last logical eraseblock, the @len argument doesn't have
710 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
711 * to the real data size, although the @buf buffer has to contain the
712 * alignment. In all other cases, @len has to be aligned.
713 *
714 * It is prohibited to write more then once to logical eraseblocks of static
715 * volumes. This function returns zero in case of success and a negative error
716 * code in case of failure.
717 */
718int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum,
719 const void *buf, int len, int dtype, int used_ebs)
720{
721 int err, pnum, tries = 0, data_size = len;
722 int idx = vol_id2idx(ubi, vol_id);
723 struct ubi_volume *vol = ubi->volumes[idx];
724 struct ubi_vid_hdr *vid_hdr;
725 uint32_t crc;
726
727 if (ubi->ro_mode)
728 return -EROFS;
729
730 if (lnum == used_ebs - 1)
731 /* If this is the last LEB @len may be unaligned */
732 len = ALIGN(data_size, ubi->min_io_size);
733 else
734 ubi_assert(len % ubi->min_io_size == 0);
735
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300736 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400737 if (!vid_hdr)
738 return -ENOMEM;
739
740 err = leb_write_lock(ubi, vol_id, lnum);
741 if (err) {
742 ubi_free_vid_hdr(ubi, vid_hdr);
743 return err;
744 }
745
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300746 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
747 vid_hdr->vol_id = cpu_to_be32(vol_id);
748 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400749 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300750 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400751
752 crc = crc32(UBI_CRC32_INIT, buf, data_size);
753 vid_hdr->vol_type = UBI_VID_STATIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300754 vid_hdr->data_size = cpu_to_be32(data_size);
755 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
756 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400757
758retry:
759 pnum = ubi_wl_get_peb(ubi, dtype);
760 if (pnum < 0) {
761 ubi_free_vid_hdr(ubi, vid_hdr);
762 leb_write_unlock(ubi, vol_id, lnum);
763 return pnum;
764 }
765
766 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
767 len, vol_id, lnum, pnum, used_ebs);
768
769 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
770 if (err) {
771 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
772 vol_id, lnum, pnum);
773 goto write_error;
774 }
775
776 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
777 if (err) {
778 ubi_warn("failed to write %d bytes of data to PEB %d",
779 len, pnum);
780 goto write_error;
781 }
782
783 ubi_assert(vol->eba_tbl[lnum] < 0);
784 vol->eba_tbl[lnum] = pnum;
785
786 leb_write_unlock(ubi, vol_id, lnum);
787 ubi_free_vid_hdr(ubi, vid_hdr);
788 return 0;
789
790write_error:
791 if (err != -EIO || !ubi->bad_allowed) {
792 /*
793 * This flash device does not admit of bad eraseblocks or
794 * something nasty and unexpected happened. Switch to read-only
795 * mode just in case.
796 */
797 ubi_ro_mode(ubi);
798 leb_write_unlock(ubi, vol_id, lnum);
799 ubi_free_vid_hdr(ubi, vid_hdr);
800 return err;
801 }
802
803 err = ubi_wl_put_peb(ubi, pnum, 1);
804 if (err || ++tries > UBI_IO_RETRIES) {
805 ubi_ro_mode(ubi);
806 leb_write_unlock(ubi, vol_id, lnum);
807 ubi_free_vid_hdr(ubi, vid_hdr);
808 return err;
809 }
810
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300811 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400812 ubi_msg("try another PEB");
813 goto retry;
814}
815
816/*
817 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
818 * @ubi: UBI device description object
819 * @vol_id: volume ID
820 * @lnum: logical eraseblock number
821 * @buf: data to write
822 * @len: how many bytes to write
823 * @dtype: data type
824 *
825 * This function changes the contents of a logical eraseblock atomically. @buf
826 * has to contain new logical eraseblock data, and @len - the length of the
827 * data, which has to be aligned. This function guarantees that in case of an
828 * unclean reboot the old contents is preserved. Returns zero in case of
829 * success and a negative error code in case of failure.
830 */
831int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
832 const void *buf, int len, int dtype)
833{
834 int err, pnum, tries = 0, idx = vol_id2idx(ubi, vol_id);
835 struct ubi_volume *vol = ubi->volumes[idx];
836 struct ubi_vid_hdr *vid_hdr;
837 uint32_t crc;
838
839 if (ubi->ro_mode)
840 return -EROFS;
841
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300842 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400843 if (!vid_hdr)
844 return -ENOMEM;
845
846 err = leb_write_lock(ubi, vol_id, lnum);
847 if (err) {
848 ubi_free_vid_hdr(ubi, vid_hdr);
849 return err;
850 }
851
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300852 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
853 vid_hdr->vol_id = cpu_to_be32(vol_id);
854 vid_hdr->lnum = cpu_to_be32(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400855 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300856 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400857
858 crc = crc32(UBI_CRC32_INIT, buf, len);
Artem Bityutskiy84a92582007-07-04 16:16:51 +0300859 vid_hdr->vol_type = UBI_VID_DYNAMIC;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300860 vid_hdr->data_size = cpu_to_be32(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400861 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300862 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863
864retry:
865 pnum = ubi_wl_get_peb(ubi, dtype);
866 if (pnum < 0) {
867 ubi_free_vid_hdr(ubi, vid_hdr);
868 leb_write_unlock(ubi, vol_id, lnum);
869 return pnum;
870 }
871
872 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
873 vol_id, lnum, vol->eba_tbl[lnum], pnum);
874
875 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
876 if (err) {
877 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
878 vol_id, lnum, pnum);
879 goto write_error;
880 }
881
882 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
883 if (err) {
884 ubi_warn("failed to write %d bytes of data to PEB %d",
885 len, pnum);
886 goto write_error;
887 }
888
Artem Bityutskiya443db42007-05-21 20:26:05 +0300889 if (vol->eba_tbl[lnum] >= 0) {
890 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
891 if (err) {
892 ubi_free_vid_hdr(ubi, vid_hdr);
893 leb_write_unlock(ubi, vol_id, lnum);
894 return err;
895 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400896 }
897
898 vol->eba_tbl[lnum] = pnum;
899 leb_write_unlock(ubi, vol_id, lnum);
900 ubi_free_vid_hdr(ubi, vid_hdr);
901 return 0;
902
903write_error:
904 if (err != -EIO || !ubi->bad_allowed) {
905 /*
906 * This flash device does not admit of bad eraseblocks or
907 * something nasty and unexpected happened. Switch to read-only
908 * mode just in case.
909 */
910 ubi_ro_mode(ubi);
911 leb_write_unlock(ubi, vol_id, lnum);
912 ubi_free_vid_hdr(ubi, vid_hdr);
913 return err;
914 }
915
916 err = ubi_wl_put_peb(ubi, pnum, 1);
917 if (err || ++tries > UBI_IO_RETRIES) {
918 ubi_ro_mode(ubi);
919 leb_write_unlock(ubi, vol_id, lnum);
920 ubi_free_vid_hdr(ubi, vid_hdr);
921 return err;
922 }
923
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300924 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400925 ubi_msg("try another PEB");
926 goto retry;
927}
928
929/**
930 * ltree_entry_ctor - lock tree entries slab cache constructor.
931 * @obj: the lock-tree entry to construct
932 * @cache: the lock tree entry slab cache
933 * @flags: constructor flags
934 */
935static void ltree_entry_ctor(void *obj, struct kmem_cache *cache,
936 unsigned long flags)
937{
938 struct ltree_entry *le = obj;
939
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400940 le->users = 0;
941 init_rwsem(&le->mutex);
942}
943
944/**
945 * ubi_eba_copy_leb - copy logical eraseblock.
946 * @ubi: UBI device description object
947 * @from: physical eraseblock number from where to copy
948 * @to: physical eraseblock number where to copy
949 * @vid_hdr: VID header of the @from physical eraseblock
950 *
951 * This function copies logical eraseblock from physical eraseblock @from to
952 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
953 * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
954 * was canceled because bit-flips were detected at the target PEB, and a
955 * negative error code in case of failure.
956 */
957int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
958 struct ubi_vid_hdr *vid_hdr)
959{
960 int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
961 struct ubi_volume *vol;
962 uint32_t crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400963
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300964 vol_id = be32_to_cpu(vid_hdr->vol_id);
965 lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400966
967 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
968
969 if (vid_hdr->vol_type == UBI_VID_STATIC) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300970 data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400971 aldata_size = ALIGN(data_size, ubi->min_io_size);
972 } else
973 data_size = aldata_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300974 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400975
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400976 /*
977 * We do not want anybody to write to this logical eraseblock while we
978 * are moving it, so we lock it.
979 */
980 err = leb_write_lock(ubi, vol_id, lnum);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300981 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400982 return err;
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300983
984 mutex_lock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400985
986 /*
987 * But the logical eraseblock might have been put by this time.
988 * Cancel if it is true.
989 */
990 idx = vol_id2idx(ubi, vol_id);
991
992 /*
993 * We may race with volume deletion/re-size, so we have to hold
994 * @ubi->volumes_lock.
995 */
996 spin_lock(&ubi->volumes_lock);
997 vol = ubi->volumes[idx];
998 if (!vol) {
999 dbg_eba("volume %d was removed meanwhile", vol_id);
1000 spin_unlock(&ubi->volumes_lock);
1001 goto out_unlock;
1002 }
1003
1004 pnum = vol->eba_tbl[lnum];
1005 if (pnum != from) {
1006 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1007 "PEB %d, cancel", vol_id, lnum, from, pnum);
1008 spin_unlock(&ubi->volumes_lock);
1009 goto out_unlock;
1010 }
1011 spin_unlock(&ubi->volumes_lock);
1012
1013 /* OK, now the LEB is locked and we can safely start moving it */
1014
1015 dbg_eba("read %d bytes of data", aldata_size);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001016 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001017 if (err && err != UBI_IO_BITFLIPS) {
1018 ubi_warn("error %d while reading data from PEB %d",
1019 err, from);
1020 goto out_unlock;
1021 }
1022
1023 /*
1024 * Now we have got to calculate how much data we have to to copy. In
1025 * case of a static volume it is fairly easy - the VID header contains
1026 * the data size. In case of a dynamic volume it is more difficult - we
1027 * have to read the contents, cut 0xFF bytes from the end and copy only
1028 * the first part. We must do this to avoid writing 0xFF bytes as it
1029 * may have some side-effects. And not only this. It is important not
1030 * to include those 0xFFs to CRC because later the they may be filled
1031 * by data.
1032 */
1033 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1034 aldata_size = data_size =
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001035 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001036
1037 cond_resched();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001038 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001039 cond_resched();
1040
1041 /*
1042 * It may turn out to me that the whole @from physical eraseblock
1043 * contains only 0xFF bytes. Then we have to only write the VID header
1044 * and do not write any data. This also means we should not set
1045 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1046 */
1047 if (data_size > 0) {
1048 vid_hdr->copy_flag = 1;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001049 vid_hdr->data_size = cpu_to_be32(data_size);
1050 vid_hdr->data_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001051 }
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001052 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001053
1054 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1055 if (err)
1056 goto out_unlock;
1057
1058 cond_resched();
1059
1060 /* Read the VID header back and check if it was written correctly */
1061 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1062 if (err) {
1063 if (err != UBI_IO_BITFLIPS)
1064 ubi_warn("cannot read VID header back from PEB %d", to);
1065 goto out_unlock;
1066 }
1067
1068 if (data_size > 0) {
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001069 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001070 if (err)
1071 goto out_unlock;
1072
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001073 cond_resched();
1074
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001075 /*
1076 * We've written the data and are going to read it back to make
1077 * sure it was written correctly.
1078 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001079
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001080 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001081 if (err) {
1082 if (err != UBI_IO_BITFLIPS)
1083 ubi_warn("cannot read data back from PEB %d",
1084 to);
1085 goto out_unlock;
1086 }
1087
1088 cond_resched();
1089
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001090 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001091 ubi_warn("read data back from PEB %d - it is different",
1092 to);
1093 goto out_unlock;
1094 }
1095 }
1096
1097 ubi_assert(vol->eba_tbl[lnum] == from);
1098 vol->eba_tbl[lnum] = to;
1099
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001100out_unlock:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001101 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001102 leb_write_unlock(ubi, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001103 return err;
1104}
1105
1106/**
1107 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1108 * @ubi: UBI device description object
1109 * @si: scanning information
1110 *
1111 * This function returns zero in case of success and a negative error code in
1112 * case of failure.
1113 */
1114int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1115{
1116 int i, j, err, num_volumes;
1117 struct ubi_scan_volume *sv;
1118 struct ubi_volume *vol;
1119 struct ubi_scan_leb *seb;
1120 struct rb_node *rb;
1121
1122 dbg_eba("initialize EBA unit");
1123
1124 spin_lock_init(&ubi->ltree_lock);
1125 ubi->ltree = RB_ROOT;
1126
1127 if (ubi_devices_cnt == 0) {
1128 ltree_slab = kmem_cache_create("ubi_ltree_slab",
1129 sizeof(struct ltree_entry), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001130 0, &ltree_entry_ctor);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001131 if (!ltree_slab)
1132 return -ENOMEM;
1133 }
1134
1135 ubi->global_sqnum = si->max_sqnum + 1;
1136 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1137
1138 for (i = 0; i < num_volumes; i++) {
1139 vol = ubi->volumes[i];
1140 if (!vol)
1141 continue;
1142
1143 cond_resched();
1144
1145 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1146 GFP_KERNEL);
1147 if (!vol->eba_tbl) {
1148 err = -ENOMEM;
1149 goto out_free;
1150 }
1151
1152 for (j = 0; j < vol->reserved_pebs; j++)
1153 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1154
1155 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1156 if (!sv)
1157 continue;
1158
1159 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1160 if (seb->lnum >= vol->reserved_pebs)
1161 /*
1162 * This may happen in case of an unclean reboot
1163 * during re-size.
1164 */
1165 ubi_scan_move_to_list(sv, seb, &si->erase);
1166 vol->eba_tbl[seb->lnum] = seb->pnum;
1167 }
1168 }
1169
1170 if (ubi->bad_allowed) {
1171 ubi_calculate_reserved(ubi);
1172
1173 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1174 /* No enough free physical eraseblocks */
1175 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1176 ubi_warn("cannot reserve enough PEBs for bad PEB "
1177 "handling, reserved %d, need %d",
1178 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1179 } else
1180 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1181
1182 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1183 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1184 }
1185
1186 dbg_eba("EBA unit is initialized");
1187 return 0;
1188
1189out_free:
1190 for (i = 0; i < num_volumes; i++) {
1191 if (!ubi->volumes[i])
1192 continue;
1193 kfree(ubi->volumes[i]->eba_tbl);
1194 }
1195 if (ubi_devices_cnt == 0)
1196 kmem_cache_destroy(ltree_slab);
1197 return err;
1198}
1199
1200/**
1201 * ubi_eba_close - close EBA unit.
1202 * @ubi: UBI device description object
1203 */
1204void ubi_eba_close(const struct ubi_device *ubi)
1205{
1206 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1207
1208 dbg_eba("close EBA unit");
1209
1210 for (i = 0; i < num_volumes; i++) {
1211 if (!ubi->volumes[i])
1212 continue;
1213 kfree(ubi->volumes[i]->eba_tbl);
1214 }
1215 if (ubi_devices_cnt == 1)
1216 kmem_cache_destroy(ltree_slab);
1217}