Linux 3.12.39
[linux/fpc-iii.git] / drivers / mtd / ubi / fastmap.c
blob85cd77c9cd12bd77a7aa20a32fce2b03c1965523
1 /*
2 * Copyright (c) 2012 Linutronix GmbH
3 * Author: Richard Weinberger <richard@nod.at>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2.
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.
16 #include <linux/crc32.h>
17 #include "ubi.h"
19 /**
20 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
21 * @ubi: UBI device description object
23 size_t ubi_calc_fm_size(struct ubi_device *ubi)
25 size_t size;
27 size = sizeof(struct ubi_fm_hdr) + \
28 sizeof(struct ubi_fm_scan_pool) + \
29 sizeof(struct ubi_fm_scan_pool) + \
30 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + \
31 (sizeof(struct ubi_fm_eba) + \
32 (ubi->peb_count * sizeof(__be32))) + \
33 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
34 return roundup(size, ubi->leb_size);
38 /**
39 * new_fm_vhdr - allocate a new volume header for fastmap usage.
40 * @ubi: UBI device description object
41 * @vol_id: the VID of the new header
43 * Returns a new struct ubi_vid_hdr on success.
44 * NULL indicates out of memory.
46 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
48 struct ubi_vid_hdr *new;
50 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
51 if (!new)
52 goto out;
54 new->vol_type = UBI_VID_DYNAMIC;
55 new->vol_id = cpu_to_be32(vol_id);
57 /* UBI implementations without fastmap support have to delete the
58 * fastmap.
60 new->compat = UBI_COMPAT_DELETE;
62 out:
63 return new;
66 /**
67 * add_aeb - create and add a attach erase block to a given list.
68 * @ai: UBI attach info object
69 * @list: the target list
70 * @pnum: PEB number of the new attach erase block
71 * @ec: erease counter of the new LEB
72 * @scrub: scrub this PEB after attaching
74 * Returns 0 on success, < 0 indicates an internal error.
76 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
77 int pnum, int ec, int scrub)
79 struct ubi_ainf_peb *aeb;
81 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
82 if (!aeb)
83 return -ENOMEM;
85 aeb->pnum = pnum;
86 aeb->ec = ec;
87 aeb->lnum = -1;
88 aeb->scrub = scrub;
89 aeb->copy_flag = aeb->sqnum = 0;
91 ai->ec_sum += aeb->ec;
92 ai->ec_count++;
94 if (ai->max_ec < aeb->ec)
95 ai->max_ec = aeb->ec;
97 if (ai->min_ec > aeb->ec)
98 ai->min_ec = aeb->ec;
100 list_add_tail(&aeb->u.list, list);
102 return 0;
106 * add_vol - create and add a new volume to ubi_attach_info.
107 * @ai: ubi_attach_info object
108 * @vol_id: VID of the new volume
109 * @used_ebs: number of used EBS
110 * @data_pad: data padding value of the new volume
111 * @vol_type: volume type
112 * @last_eb_bytes: number of bytes in the last LEB
114 * Returns the new struct ubi_ainf_volume on success.
115 * NULL indicates an error.
117 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
118 int used_ebs, int data_pad, u8 vol_type,
119 int last_eb_bytes)
121 struct ubi_ainf_volume *av;
122 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
124 while (*p) {
125 parent = *p;
126 av = rb_entry(parent, struct ubi_ainf_volume, rb);
128 if (vol_id > av->vol_id)
129 p = &(*p)->rb_left;
130 else if (vol_id > av->vol_id)
131 p = &(*p)->rb_right;
134 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
135 if (!av)
136 goto out;
138 av->highest_lnum = av->leb_count = 0;
139 av->vol_id = vol_id;
140 av->used_ebs = used_ebs;
141 av->data_pad = data_pad;
142 av->last_data_size = last_eb_bytes;
143 av->compat = 0;
144 av->vol_type = vol_type;
145 av->root = RB_ROOT;
147 dbg_bld("found volume (ID %i)", vol_id);
149 rb_link_node(&av->rb, parent, p);
150 rb_insert_color(&av->rb, &ai->volumes);
152 out:
153 return av;
157 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
158 * from it's original list.
159 * @ai: ubi_attach_info object
160 * @aeb: the to be assigned SEB
161 * @av: target scan volume
163 static void assign_aeb_to_av(struct ubi_attach_info *ai,
164 struct ubi_ainf_peb *aeb,
165 struct ubi_ainf_volume *av)
167 struct ubi_ainf_peb *tmp_aeb;
168 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
170 p = &av->root.rb_node;
171 while (*p) {
172 parent = *p;
174 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
175 if (aeb->lnum != tmp_aeb->lnum) {
176 if (aeb->lnum < tmp_aeb->lnum)
177 p = &(*p)->rb_left;
178 else
179 p = &(*p)->rb_right;
181 continue;
182 } else
183 break;
186 list_del(&aeb->u.list);
187 av->leb_count++;
189 rb_link_node(&aeb->u.rb, parent, p);
190 rb_insert_color(&aeb->u.rb, &av->root);
194 * update_vol - inserts or updates a LEB which was found a pool.
195 * @ubi: the UBI device object
196 * @ai: attach info object
197 * @av: the volume this LEB belongs to
198 * @new_vh: the volume header derived from new_aeb
199 * @new_aeb: the AEB to be examined
201 * Returns 0 on success, < 0 indicates an internal error.
203 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
204 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
205 struct ubi_ainf_peb *new_aeb)
207 struct rb_node **p = &av->root.rb_node, *parent = NULL;
208 struct ubi_ainf_peb *aeb, *victim;
209 int cmp_res;
211 while (*p) {
212 parent = *p;
213 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
215 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
216 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
217 p = &(*p)->rb_left;
218 else
219 p = &(*p)->rb_right;
221 continue;
224 /* This case can happen if the fastmap gets written
225 * because of a volume change (creation, deletion, ..).
226 * Then a PEB can be within the persistent EBA and the pool.
228 if (aeb->pnum == new_aeb->pnum) {
229 ubi_assert(aeb->lnum == new_aeb->lnum);
230 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
232 return 0;
235 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
236 if (cmp_res < 0)
237 return cmp_res;
239 /* new_aeb is newer */
240 if (cmp_res & 1) {
241 victim = kmem_cache_alloc(ai->aeb_slab_cache,
242 GFP_KERNEL);
243 if (!victim)
244 return -ENOMEM;
246 victim->ec = aeb->ec;
247 victim->pnum = aeb->pnum;
248 list_add_tail(&victim->u.list, &ai->erase);
250 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
251 av->last_data_size = \
252 be32_to_cpu(new_vh->data_size);
254 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
255 av->vol_id, aeb->lnum, new_aeb->pnum);
257 aeb->ec = new_aeb->ec;
258 aeb->pnum = new_aeb->pnum;
259 aeb->copy_flag = new_vh->copy_flag;
260 aeb->scrub = new_aeb->scrub;
261 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
263 /* new_aeb is older */
264 } else {
265 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
266 av->vol_id, aeb->lnum, new_aeb->pnum);
267 list_add_tail(&new_aeb->u.list, &ai->erase);
270 return 0;
272 /* This LEB is new, let's add it to the volume */
274 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
275 av->highest_lnum = be32_to_cpu(new_vh->lnum);
276 av->last_data_size = be32_to_cpu(new_vh->data_size);
279 if (av->vol_type == UBI_STATIC_VOLUME)
280 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
282 av->leb_count++;
284 rb_link_node(&new_aeb->u.rb, parent, p);
285 rb_insert_color(&new_aeb->u.rb, &av->root);
287 return 0;
291 * process_pool_aeb - we found a non-empty PEB in a pool.
292 * @ubi: UBI device object
293 * @ai: attach info object
294 * @new_vh: the volume header derived from new_aeb
295 * @new_aeb: the AEB to be examined
297 * Returns 0 on success, < 0 indicates an internal error.
299 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
300 struct ubi_vid_hdr *new_vh,
301 struct ubi_ainf_peb *new_aeb)
303 struct ubi_ainf_volume *av, *tmp_av = NULL;
304 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
305 int found = 0;
307 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
308 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
309 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
311 return 0;
314 /* Find the volume this SEB belongs to */
315 while (*p) {
316 parent = *p;
317 tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
319 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
320 p = &(*p)->rb_left;
321 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
322 p = &(*p)->rb_right;
323 else {
324 found = 1;
325 break;
329 if (found)
330 av = tmp_av;
331 else {
332 ubi_err("orphaned volume in fastmap pool!");
333 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
334 return UBI_BAD_FASTMAP;
337 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
339 return update_vol(ubi, ai, av, new_vh, new_aeb);
343 * unmap_peb - unmap a PEB.
344 * If fastmap detects a free PEB in the pool it has to check whether
345 * this PEB has been unmapped after writing the fastmap.
347 * @ai: UBI attach info object
348 * @pnum: The PEB to be unmapped
350 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
352 struct ubi_ainf_volume *av;
353 struct rb_node *node, *node2;
354 struct ubi_ainf_peb *aeb;
356 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
357 av = rb_entry(node, struct ubi_ainf_volume, rb);
359 for (node2 = rb_first(&av->root); node2;
360 node2 = rb_next(node2)) {
361 aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
362 if (aeb->pnum == pnum) {
363 rb_erase(&aeb->u.rb, &av->root);
364 kmem_cache_free(ai->aeb_slab_cache, aeb);
365 return;
372 * scan_pool - scans a pool for changed (no longer empty PEBs).
373 * @ubi: UBI device object
374 * @ai: attach info object
375 * @pebs: an array of all PEB numbers in the to be scanned pool
376 * @pool_size: size of the pool (number of entries in @pebs)
377 * @max_sqnum: pointer to the maximal sequence number
378 * @eba_orphans: list of PEBs which need to be scanned
379 * @free: list of PEBs which are most likely free (and go into @ai->free)
381 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
382 * < 0 indicates an internal error.
384 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
385 int *pebs, int pool_size, unsigned long long *max_sqnum,
386 struct list_head *eba_orphans, struct list_head *free)
388 struct ubi_vid_hdr *vh;
389 struct ubi_ec_hdr *ech;
390 struct ubi_ainf_peb *new_aeb, *tmp_aeb;
391 int i, pnum, err, found_orphan, ret = 0;
393 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
394 if (!ech)
395 return -ENOMEM;
397 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
398 if (!vh) {
399 kfree(ech);
400 return -ENOMEM;
403 dbg_bld("scanning fastmap pool: size = %i", pool_size);
406 * Now scan all PEBs in the pool to find changes which have been made
407 * after the creation of the fastmap
409 for (i = 0; i < pool_size; i++) {
410 int scrub = 0;
412 pnum = be32_to_cpu(pebs[i]);
414 if (ubi_io_is_bad(ubi, pnum)) {
415 ubi_err("bad PEB in fastmap pool!");
416 ret = UBI_BAD_FASTMAP;
417 goto out;
420 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
421 if (err && err != UBI_IO_BITFLIPS) {
422 ubi_err("unable to read EC header! PEB:%i err:%i",
423 pnum, err);
424 ret = err > 0 ? UBI_BAD_FASTMAP : err;
425 goto out;
426 } else if (ret == UBI_IO_BITFLIPS)
427 scrub = 1;
429 if (be32_to_cpu(ech->image_seq) != ubi->image_seq) {
430 ubi_err("bad image seq: 0x%x, expected: 0x%x",
431 be32_to_cpu(ech->image_seq), ubi->image_seq);
432 err = UBI_BAD_FASTMAP;
433 goto out;
436 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
437 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
438 unsigned long long ec = be64_to_cpu(ech->ec);
439 unmap_peb(ai, pnum);
440 dbg_bld("Adding PEB to free: %i", pnum);
441 if (err == UBI_IO_FF_BITFLIPS)
442 add_aeb(ai, free, pnum, ec, 1);
443 else
444 add_aeb(ai, free, pnum, ec, 0);
445 continue;
446 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
447 dbg_bld("Found non empty PEB:%i in pool", pnum);
449 if (err == UBI_IO_BITFLIPS)
450 scrub = 1;
452 found_orphan = 0;
453 list_for_each_entry(tmp_aeb, eba_orphans, u.list) {
454 if (tmp_aeb->pnum == pnum) {
455 found_orphan = 1;
456 break;
459 if (found_orphan) {
460 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
461 list_del(&tmp_aeb->u.list);
464 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
465 GFP_KERNEL);
466 if (!new_aeb) {
467 ret = -ENOMEM;
468 goto out;
471 new_aeb->ec = be64_to_cpu(ech->ec);
472 new_aeb->pnum = pnum;
473 new_aeb->lnum = be32_to_cpu(vh->lnum);
474 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
475 new_aeb->copy_flag = vh->copy_flag;
476 new_aeb->scrub = scrub;
478 if (*max_sqnum < new_aeb->sqnum)
479 *max_sqnum = new_aeb->sqnum;
481 err = process_pool_aeb(ubi, ai, vh, new_aeb);
482 if (err) {
483 ret = err > 0 ? UBI_BAD_FASTMAP : err;
484 goto out;
486 } else {
487 /* We are paranoid and fall back to scanning mode */
488 ubi_err("fastmap pool PEBs contains damaged PEBs!");
489 ret = err > 0 ? UBI_BAD_FASTMAP : err;
490 goto out;
495 out:
496 ubi_free_vid_hdr(ubi, vh);
497 kfree(ech);
498 return ret;
502 * count_fastmap_pebs - Counts the PEBs found by fastmap.
503 * @ai: The UBI attach info object
505 static int count_fastmap_pebs(struct ubi_attach_info *ai)
507 struct ubi_ainf_peb *aeb;
508 struct ubi_ainf_volume *av;
509 struct rb_node *rb1, *rb2;
510 int n = 0;
512 list_for_each_entry(aeb, &ai->erase, u.list)
513 n++;
515 list_for_each_entry(aeb, &ai->free, u.list)
516 n++;
518 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
519 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
520 n++;
522 return n;
526 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
527 * @ubi: UBI device object
528 * @ai: UBI attach info object
529 * @fm: the fastmap to be attached
531 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
532 * < 0 indicates an internal error.
534 static int ubi_attach_fastmap(struct ubi_device *ubi,
535 struct ubi_attach_info *ai,
536 struct ubi_fastmap_layout *fm)
538 struct list_head used, eba_orphans, free;
539 struct ubi_ainf_volume *av;
540 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
541 struct ubi_ec_hdr *ech;
542 struct ubi_fm_sb *fmsb;
543 struct ubi_fm_hdr *fmhdr;
544 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
545 struct ubi_fm_ec *fmec;
546 struct ubi_fm_volhdr *fmvhdr;
547 struct ubi_fm_eba *fm_eba;
548 int ret, i, j, pool_size, wl_pool_size;
549 size_t fm_pos = 0, fm_size = ubi->fm_size;
550 unsigned long long max_sqnum = 0;
551 void *fm_raw = ubi->fm_buf;
553 INIT_LIST_HEAD(&used);
554 INIT_LIST_HEAD(&free);
555 INIT_LIST_HEAD(&eba_orphans);
556 INIT_LIST_HEAD(&ai->corr);
557 INIT_LIST_HEAD(&ai->free);
558 INIT_LIST_HEAD(&ai->erase);
559 INIT_LIST_HEAD(&ai->alien);
560 ai->volumes = RB_ROOT;
561 ai->min_ec = UBI_MAX_ERASECOUNTER;
563 ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab",
564 sizeof(struct ubi_ainf_peb),
565 0, 0, NULL);
566 if (!ai->aeb_slab_cache) {
567 ret = -ENOMEM;
568 goto fail;
571 fmsb = (struct ubi_fm_sb *)(fm_raw);
572 ai->max_sqnum = fmsb->sqnum;
573 fm_pos += sizeof(struct ubi_fm_sb);
574 if (fm_pos >= fm_size)
575 goto fail_bad;
577 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
578 fm_pos += sizeof(*fmhdr);
579 if (fm_pos >= fm_size)
580 goto fail_bad;
582 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
583 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
584 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
585 goto fail_bad;
588 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
589 fm_pos += sizeof(*fmpl1);
590 if (fm_pos >= fm_size)
591 goto fail_bad;
592 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
593 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
594 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
595 goto fail_bad;
598 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
599 fm_pos += sizeof(*fmpl2);
600 if (fm_pos >= fm_size)
601 goto fail_bad;
602 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
603 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
604 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
605 goto fail_bad;
608 pool_size = be16_to_cpu(fmpl1->size);
609 wl_pool_size = be16_to_cpu(fmpl2->size);
610 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
611 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
613 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
614 ubi_err("bad pool size: %i", pool_size);
615 goto fail_bad;
618 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
619 ubi_err("bad WL pool size: %i", wl_pool_size);
620 goto fail_bad;
624 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
625 fm->max_pool_size < 0) {
626 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
627 goto fail_bad;
630 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
631 fm->max_wl_pool_size < 0) {
632 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
633 goto fail_bad;
636 /* read EC values from free list */
637 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
638 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
639 fm_pos += sizeof(*fmec);
640 if (fm_pos >= fm_size)
641 goto fail_bad;
643 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
644 be32_to_cpu(fmec->ec), 0);
647 /* read EC values from used list */
648 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
649 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
650 fm_pos += sizeof(*fmec);
651 if (fm_pos >= fm_size)
652 goto fail_bad;
654 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
655 be32_to_cpu(fmec->ec), 0);
658 /* read EC values from scrub list */
659 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
660 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
661 fm_pos += sizeof(*fmec);
662 if (fm_pos >= fm_size)
663 goto fail_bad;
665 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
666 be32_to_cpu(fmec->ec), 1);
669 /* read EC values from erase list */
670 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
671 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
672 fm_pos += sizeof(*fmec);
673 if (fm_pos >= fm_size)
674 goto fail_bad;
676 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
677 be32_to_cpu(fmec->ec), 1);
680 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
681 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
683 /* Iterate over all volumes and read their EBA table */
684 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
685 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
686 fm_pos += sizeof(*fmvhdr);
687 if (fm_pos >= fm_size)
688 goto fail_bad;
690 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
691 ubi_err("bad fastmap vol header magic: 0x%x, " \
692 "expected: 0x%x",
693 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
694 goto fail_bad;
697 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
698 be32_to_cpu(fmvhdr->used_ebs),
699 be32_to_cpu(fmvhdr->data_pad),
700 fmvhdr->vol_type,
701 be32_to_cpu(fmvhdr->last_eb_bytes));
703 if (!av)
704 goto fail_bad;
706 ai->vols_found++;
707 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
708 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
710 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
711 fm_pos += sizeof(*fm_eba);
712 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
713 if (fm_pos >= fm_size)
714 goto fail_bad;
716 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
717 ubi_err("bad fastmap EBA header magic: 0x%x, " \
718 "expected: 0x%x",
719 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
720 goto fail_bad;
723 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
724 int pnum = be32_to_cpu(fm_eba->pnum[j]);
726 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
727 continue;
729 aeb = NULL;
730 list_for_each_entry(tmp_aeb, &used, u.list) {
731 if (tmp_aeb->pnum == pnum) {
732 aeb = tmp_aeb;
733 break;
737 /* This can happen if a PEB is already in an EBA known
738 * by this fastmap but the PEB itself is not in the used
739 * list.
740 * In this case the PEB can be within the fastmap pool
741 * or while writing the fastmap it was in the protection
742 * queue.
744 if (!aeb) {
745 aeb = kmem_cache_alloc(ai->aeb_slab_cache,
746 GFP_KERNEL);
747 if (!aeb) {
748 ret = -ENOMEM;
750 goto fail;
753 aeb->lnum = j;
754 aeb->pnum = be32_to_cpu(fm_eba->pnum[j]);
755 aeb->ec = -1;
756 aeb->scrub = aeb->copy_flag = aeb->sqnum = 0;
757 list_add_tail(&aeb->u.list, &eba_orphans);
758 continue;
761 aeb->lnum = j;
763 if (av->highest_lnum <= aeb->lnum)
764 av->highest_lnum = aeb->lnum;
766 assign_aeb_to_av(ai, aeb, av);
768 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
769 aeb->pnum, aeb->lnum, av->vol_id);
772 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
773 if (!ech) {
774 ret = -ENOMEM;
775 goto fail;
778 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans,
779 u.list) {
780 int err;
782 if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) {
783 ubi_err("bad PEB in fastmap EBA orphan list");
784 ret = UBI_BAD_FASTMAP;
785 kfree(ech);
786 goto fail;
789 err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0);
790 if (err && err != UBI_IO_BITFLIPS) {
791 ubi_err("unable to read EC header! PEB:%i " \
792 "err:%i", tmp_aeb->pnum, err);
793 ret = err > 0 ? UBI_BAD_FASTMAP : err;
794 kfree(ech);
796 goto fail;
797 } else if (err == UBI_IO_BITFLIPS)
798 tmp_aeb->scrub = 1;
800 tmp_aeb->ec = be64_to_cpu(ech->ec);
801 assign_aeb_to_av(ai, tmp_aeb, av);
804 kfree(ech);
807 ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum,
808 &eba_orphans, &free);
809 if (ret)
810 goto fail;
812 ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum,
813 &eba_orphans, &free);
814 if (ret)
815 goto fail;
817 if (max_sqnum > ai->max_sqnum)
818 ai->max_sqnum = max_sqnum;
820 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
821 list_move_tail(&tmp_aeb->u.list, &ai->free);
824 * If fastmap is leaking PEBs (must not happen), raise a
825 * fat warning and fall back to scanning mode.
826 * We do this here because in ubi_wl_init() it's too late
827 * and we cannot fall back to scanning.
829 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
830 ai->bad_peb_count - fm->used_blocks))
831 goto fail_bad;
833 return 0;
835 fail_bad:
836 ret = UBI_BAD_FASTMAP;
837 fail:
838 return ret;
842 * ubi_scan_fastmap - scan the fastmap.
843 * @ubi: UBI device object
844 * @ai: UBI attach info to be filled
845 * @fm_anchor: The fastmap starts at this PEB
847 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
848 * UBI_BAD_FASTMAP if one was found but is not usable.
849 * < 0 indicates an internal error.
851 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
852 int fm_anchor)
854 struct ubi_fm_sb *fmsb, *fmsb2;
855 struct ubi_vid_hdr *vh;
856 struct ubi_ec_hdr *ech;
857 struct ubi_fastmap_layout *fm;
858 int i, used_blocks, pnum, ret = 0;
859 size_t fm_size;
860 __be32 crc, tmp_crc;
861 unsigned long long sqnum = 0;
863 mutex_lock(&ubi->fm_mutex);
864 memset(ubi->fm_buf, 0, ubi->fm_size);
866 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
867 if (!fmsb) {
868 ret = -ENOMEM;
869 goto out;
872 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
873 if (!fm) {
874 ret = -ENOMEM;
875 kfree(fmsb);
876 goto out;
879 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
880 if (ret && ret != UBI_IO_BITFLIPS)
881 goto free_fm_sb;
882 else if (ret == UBI_IO_BITFLIPS)
883 fm->to_be_tortured[0] = 1;
885 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
886 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
887 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
888 ret = UBI_BAD_FASTMAP;
889 goto free_fm_sb;
892 if (fmsb->version != UBI_FM_FMT_VERSION) {
893 ubi_err("bad fastmap version: %i, expected: %i",
894 fmsb->version, UBI_FM_FMT_VERSION);
895 ret = UBI_BAD_FASTMAP;
896 goto free_fm_sb;
899 used_blocks = be32_to_cpu(fmsb->used_blocks);
900 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
901 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
902 ret = UBI_BAD_FASTMAP;
903 goto free_fm_sb;
906 fm_size = ubi->leb_size * used_blocks;
907 if (fm_size != ubi->fm_size) {
908 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
909 ubi->fm_size);
910 ret = UBI_BAD_FASTMAP;
911 goto free_fm_sb;
914 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
915 if (!ech) {
916 ret = -ENOMEM;
917 goto free_fm_sb;
920 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
921 if (!vh) {
922 ret = -ENOMEM;
923 goto free_hdr;
926 for (i = 0; i < used_blocks; i++) {
927 pnum = be32_to_cpu(fmsb->block_loc[i]);
929 if (ubi_io_is_bad(ubi, pnum)) {
930 ret = UBI_BAD_FASTMAP;
931 goto free_hdr;
934 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
935 if (ret && ret != UBI_IO_BITFLIPS) {
936 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
937 i, pnum);
938 if (ret > 0)
939 ret = UBI_BAD_FASTMAP;
940 goto free_hdr;
941 } else if (ret == UBI_IO_BITFLIPS)
942 fm->to_be_tortured[i] = 1;
944 if (!ubi->image_seq)
945 ubi->image_seq = be32_to_cpu(ech->image_seq);
947 if (be32_to_cpu(ech->image_seq) != ubi->image_seq) {
948 ret = UBI_BAD_FASTMAP;
949 goto free_hdr;
952 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
953 if (ret && ret != UBI_IO_BITFLIPS) {
954 ubi_err("unable to read fastmap block# %i (PEB: %i)",
955 i, pnum);
956 goto free_hdr;
959 if (i == 0) {
960 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
961 ubi_err("bad fastmap anchor vol_id: 0x%x," \
962 " expected: 0x%x",
963 be32_to_cpu(vh->vol_id),
964 UBI_FM_SB_VOLUME_ID);
965 ret = UBI_BAD_FASTMAP;
966 goto free_hdr;
968 } else {
969 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
970 ubi_err("bad fastmap data vol_id: 0x%x," \
971 " expected: 0x%x",
972 be32_to_cpu(vh->vol_id),
973 UBI_FM_DATA_VOLUME_ID);
974 ret = UBI_BAD_FASTMAP;
975 goto free_hdr;
979 if (sqnum < be64_to_cpu(vh->sqnum))
980 sqnum = be64_to_cpu(vh->sqnum);
982 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
983 ubi->leb_start, ubi->leb_size);
984 if (ret && ret != UBI_IO_BITFLIPS) {
985 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
986 "err: %i)", i, pnum, ret);
987 goto free_hdr;
991 kfree(fmsb);
992 fmsb = NULL;
994 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
995 tmp_crc = be32_to_cpu(fmsb2->data_crc);
996 fmsb2->data_crc = 0;
997 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
998 if (crc != tmp_crc) {
999 ubi_err("fastmap data CRC is invalid");
1000 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
1001 ret = UBI_BAD_FASTMAP;
1002 goto free_hdr;
1005 fmsb2->sqnum = sqnum;
1007 fm->used_blocks = used_blocks;
1009 ret = ubi_attach_fastmap(ubi, ai, fm);
1010 if (ret) {
1011 if (ret > 0)
1012 ret = UBI_BAD_FASTMAP;
1013 goto free_hdr;
1016 for (i = 0; i < used_blocks; i++) {
1017 struct ubi_wl_entry *e;
1019 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1020 if (!e) {
1021 while (i--)
1022 kfree(fm->e[i]);
1024 ret = -ENOMEM;
1025 goto free_hdr;
1028 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1029 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1030 fm->e[i] = e;
1033 ubi->fm = fm;
1034 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1035 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1036 ubi_msg("attached by fastmap");
1037 ubi_msg("fastmap pool size: %d", ubi->fm_pool.max_size);
1038 ubi_msg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
1039 ubi->fm_disabled = 0;
1041 ubi_free_vid_hdr(ubi, vh);
1042 kfree(ech);
1043 out:
1044 mutex_unlock(&ubi->fm_mutex);
1045 if (ret == UBI_BAD_FASTMAP)
1046 ubi_err("Attach by fastmap failed, doing a full scan!");
1047 return ret;
1049 free_hdr:
1050 ubi_free_vid_hdr(ubi, vh);
1051 kfree(ech);
1052 free_fm_sb:
1053 kfree(fmsb);
1054 kfree(fm);
1055 goto out;
1059 * ubi_write_fastmap - writes a fastmap.
1060 * @ubi: UBI device object
1061 * @new_fm: the to be written fastmap
1063 * Returns 0 on success, < 0 indicates an internal error.
1065 static int ubi_write_fastmap(struct ubi_device *ubi,
1066 struct ubi_fastmap_layout *new_fm)
1068 size_t fm_pos = 0;
1069 void *fm_raw;
1070 struct ubi_fm_sb *fmsb;
1071 struct ubi_fm_hdr *fmh;
1072 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1073 struct ubi_fm_ec *fec;
1074 struct ubi_fm_volhdr *fvh;
1075 struct ubi_fm_eba *feba;
1076 struct rb_node *node;
1077 struct ubi_wl_entry *wl_e;
1078 struct ubi_volume *vol;
1079 struct ubi_vid_hdr *avhdr, *dvhdr;
1080 struct ubi_work *ubi_wrk;
1081 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1082 int scrub_peb_count, erase_peb_count;
1084 fm_raw = ubi->fm_buf;
1085 memset(ubi->fm_buf, 0, ubi->fm_size);
1087 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1088 if (!avhdr) {
1089 ret = -ENOMEM;
1090 goto out;
1093 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1094 if (!dvhdr) {
1095 ret = -ENOMEM;
1096 goto out_kfree;
1099 spin_lock(&ubi->volumes_lock);
1100 spin_lock(&ubi->wl_lock);
1102 fmsb = (struct ubi_fm_sb *)fm_raw;
1103 fm_pos += sizeof(*fmsb);
1104 ubi_assert(fm_pos <= ubi->fm_size);
1106 fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1107 fm_pos += sizeof(*fmh);
1108 ubi_assert(fm_pos <= ubi->fm_size);
1110 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1111 fmsb->version = UBI_FM_FMT_VERSION;
1112 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1113 /* the max sqnum will be filled in while *reading* the fastmap */
1114 fmsb->sqnum = 0;
1116 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1117 free_peb_count = 0;
1118 used_peb_count = 0;
1119 scrub_peb_count = 0;
1120 erase_peb_count = 0;
1121 vol_count = 0;
1123 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1124 fm_pos += sizeof(*fmpl1);
1125 fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1126 fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1127 fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1129 for (i = 0; i < ubi->fm_pool.size; i++)
1130 fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1132 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1133 fm_pos += sizeof(*fmpl2);
1134 fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1135 fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1136 fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1138 for (i = 0; i < ubi->fm_wl_pool.size; i++)
1139 fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1141 for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1142 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1143 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1145 fec->pnum = cpu_to_be32(wl_e->pnum);
1146 fec->ec = cpu_to_be32(wl_e->ec);
1148 free_peb_count++;
1149 fm_pos += sizeof(*fec);
1150 ubi_assert(fm_pos <= ubi->fm_size);
1152 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1154 for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1155 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1156 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1158 fec->pnum = cpu_to_be32(wl_e->pnum);
1159 fec->ec = cpu_to_be32(wl_e->ec);
1161 used_peb_count++;
1162 fm_pos += sizeof(*fec);
1163 ubi_assert(fm_pos <= ubi->fm_size);
1165 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1167 for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1168 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1169 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1171 fec->pnum = cpu_to_be32(wl_e->pnum);
1172 fec->ec = cpu_to_be32(wl_e->ec);
1174 scrub_peb_count++;
1175 fm_pos += sizeof(*fec);
1176 ubi_assert(fm_pos <= ubi->fm_size);
1178 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1181 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1182 if (ubi_is_erase_work(ubi_wrk)) {
1183 wl_e = ubi_wrk->e;
1184 ubi_assert(wl_e);
1186 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1188 fec->pnum = cpu_to_be32(wl_e->pnum);
1189 fec->ec = cpu_to_be32(wl_e->ec);
1191 erase_peb_count++;
1192 fm_pos += sizeof(*fec);
1193 ubi_assert(fm_pos <= ubi->fm_size);
1196 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1198 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1199 vol = ubi->volumes[i];
1201 if (!vol)
1202 continue;
1204 vol_count++;
1206 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1207 fm_pos += sizeof(*fvh);
1208 ubi_assert(fm_pos <= ubi->fm_size);
1210 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1211 fvh->vol_id = cpu_to_be32(vol->vol_id);
1212 fvh->vol_type = vol->vol_type;
1213 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1214 fvh->data_pad = cpu_to_be32(vol->data_pad);
1215 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1217 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1218 vol->vol_type == UBI_STATIC_VOLUME);
1220 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1221 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1222 ubi_assert(fm_pos <= ubi->fm_size);
1224 for (j = 0; j < vol->reserved_pebs; j++)
1225 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1227 feba->reserved_pebs = cpu_to_be32(j);
1228 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1230 fmh->vol_count = cpu_to_be32(vol_count);
1231 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1233 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1234 avhdr->lnum = 0;
1236 spin_unlock(&ubi->wl_lock);
1237 spin_unlock(&ubi->volumes_lock);
1239 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1240 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1241 if (ret) {
1242 ubi_err("unable to write vid_hdr to fastmap SB!");
1243 goto out_kfree;
1246 for (i = 0; i < new_fm->used_blocks; i++) {
1247 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1248 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1251 fmsb->data_crc = 0;
1252 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1253 ubi->fm_size));
1255 for (i = 1; i < new_fm->used_blocks; i++) {
1256 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1257 dvhdr->lnum = cpu_to_be32(i);
1258 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1259 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1260 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1261 if (ret) {
1262 ubi_err("unable to write vid_hdr to PEB %i!",
1263 new_fm->e[i]->pnum);
1264 goto out_kfree;
1268 for (i = 0; i < new_fm->used_blocks; i++) {
1269 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1270 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1271 if (ret) {
1272 ubi_err("unable to write fastmap to PEB %i!",
1273 new_fm->e[i]->pnum);
1274 goto out_kfree;
1278 ubi_assert(new_fm);
1279 ubi->fm = new_fm;
1281 dbg_bld("fastmap written!");
1283 out_kfree:
1284 ubi_free_vid_hdr(ubi, avhdr);
1285 ubi_free_vid_hdr(ubi, dvhdr);
1286 out:
1287 return ret;
1291 * erase_block - Manually erase a PEB.
1292 * @ubi: UBI device object
1293 * @pnum: PEB to be erased
1295 * Returns the new EC value on success, < 0 indicates an internal error.
1297 static int erase_block(struct ubi_device *ubi, int pnum)
1299 int ret;
1300 struct ubi_ec_hdr *ec_hdr;
1301 long long ec;
1303 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1304 if (!ec_hdr)
1305 return -ENOMEM;
1307 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1308 if (ret < 0)
1309 goto out;
1310 else if (ret && ret != UBI_IO_BITFLIPS) {
1311 ret = -EINVAL;
1312 goto out;
1315 ret = ubi_io_sync_erase(ubi, pnum, 0);
1316 if (ret < 0)
1317 goto out;
1319 ec = be64_to_cpu(ec_hdr->ec);
1320 ec += ret;
1321 if (ec > UBI_MAX_ERASECOUNTER) {
1322 ret = -EINVAL;
1323 goto out;
1326 ec_hdr->ec = cpu_to_be64(ec);
1327 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1328 if (ret < 0)
1329 goto out;
1331 ret = ec;
1332 out:
1333 kfree(ec_hdr);
1334 return ret;
1338 * invalidate_fastmap - destroys a fastmap.
1339 * @ubi: UBI device object
1340 * @fm: the fastmap to be destroyed
1342 * Returns 0 on success, < 0 indicates an internal error.
1344 static int invalidate_fastmap(struct ubi_device *ubi,
1345 struct ubi_fastmap_layout *fm)
1347 int ret;
1348 struct ubi_vid_hdr *vh;
1350 ret = erase_block(ubi, fm->e[0]->pnum);
1351 if (ret < 0)
1352 return ret;
1354 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1355 if (!vh)
1356 return -ENOMEM;
1358 /* deleting the current fastmap SB is not enough, an old SB may exist,
1359 * so create a (corrupted) SB such that fastmap will find it and fall
1360 * back to scanning mode in any case */
1361 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1362 ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1364 return ret;
1368 * ubi_update_fastmap - will be called by UBI if a volume changes or
1369 * a fastmap pool becomes full.
1370 * @ubi: UBI device object
1372 * Returns 0 on success, < 0 indicates an internal error.
1374 int ubi_update_fastmap(struct ubi_device *ubi)
1376 int ret, i;
1377 struct ubi_fastmap_layout *new_fm, *old_fm;
1378 struct ubi_wl_entry *tmp_e;
1380 mutex_lock(&ubi->fm_mutex);
1382 ubi_refill_pools(ubi);
1384 if (ubi->ro_mode || ubi->fm_disabled) {
1385 mutex_unlock(&ubi->fm_mutex);
1386 return 0;
1389 ret = ubi_ensure_anchor_pebs(ubi);
1390 if (ret) {
1391 mutex_unlock(&ubi->fm_mutex);
1392 return ret;
1395 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1396 if (!new_fm) {
1397 mutex_unlock(&ubi->fm_mutex);
1398 return -ENOMEM;
1401 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1403 for (i = 0; i < new_fm->used_blocks; i++) {
1404 new_fm->e[i] = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1405 if (!new_fm->e[i]) {
1406 while (i--)
1407 kfree(new_fm->e[i]);
1409 kfree(new_fm);
1410 mutex_unlock(&ubi->fm_mutex);
1411 return -ENOMEM;
1415 old_fm = ubi->fm;
1416 ubi->fm = NULL;
1418 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1419 ubi_err("fastmap too large");
1420 ret = -ENOSPC;
1421 goto err;
1424 for (i = 1; i < new_fm->used_blocks; i++) {
1425 spin_lock(&ubi->wl_lock);
1426 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1427 spin_unlock(&ubi->wl_lock);
1429 if (!tmp_e && !old_fm) {
1430 int j;
1431 ubi_err("could not get any free erase block");
1433 for (j = 1; j < i; j++)
1434 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1436 ret = -ENOSPC;
1437 goto err;
1438 } else if (!tmp_e && old_fm) {
1439 ret = erase_block(ubi, old_fm->e[i]->pnum);
1440 if (ret < 0) {
1441 int j;
1443 for (j = 1; j < i; j++)
1444 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1445 j, 0);
1447 ubi_err("could not erase old fastmap PEB");
1448 goto err;
1451 new_fm->e[i]->pnum = old_fm->e[i]->pnum;
1452 new_fm->e[i]->ec = old_fm->e[i]->ec;
1453 } else {
1454 new_fm->e[i]->pnum = tmp_e->pnum;
1455 new_fm->e[i]->ec = tmp_e->ec;
1457 if (old_fm)
1458 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1459 old_fm->to_be_tortured[i]);
1463 spin_lock(&ubi->wl_lock);
1464 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1465 spin_unlock(&ubi->wl_lock);
1467 if (old_fm) {
1468 /* no fresh anchor PEB was found, reuse the old one */
1469 if (!tmp_e) {
1470 ret = erase_block(ubi, old_fm->e[0]->pnum);
1471 if (ret < 0) {
1472 int i;
1473 ubi_err("could not erase old anchor PEB");
1475 for (i = 1; i < new_fm->used_blocks; i++)
1476 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1477 i, 0);
1478 goto err;
1481 new_fm->e[0]->pnum = old_fm->e[0]->pnum;
1482 new_fm->e[0]->ec = ret;
1483 } else {
1484 /* we've got a new anchor PEB, return the old one */
1485 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1486 old_fm->to_be_tortured[0]);
1488 new_fm->e[0]->pnum = tmp_e->pnum;
1489 new_fm->e[0]->ec = tmp_e->ec;
1491 } else {
1492 if (!tmp_e) {
1493 int i;
1494 ubi_err("could not find any anchor PEB");
1496 for (i = 1; i < new_fm->used_blocks; i++)
1497 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1499 ret = -ENOSPC;
1500 goto err;
1503 new_fm->e[0]->pnum = tmp_e->pnum;
1504 new_fm->e[0]->ec = tmp_e->ec;
1507 down_write(&ubi->work_sem);
1508 down_write(&ubi->fm_sem);
1509 ret = ubi_write_fastmap(ubi, new_fm);
1510 up_write(&ubi->fm_sem);
1511 up_write(&ubi->work_sem);
1513 if (ret)
1514 goto err;
1516 out_unlock:
1517 mutex_unlock(&ubi->fm_mutex);
1518 kfree(old_fm);
1519 return ret;
1521 err:
1522 kfree(new_fm);
1524 ubi_warn("Unable to write new fastmap, err=%i", ret);
1526 ret = 0;
1527 if (old_fm) {
1528 ret = invalidate_fastmap(ubi, old_fm);
1529 if (ret < 0)
1530 ubi_err("Unable to invalidiate current fastmap!");
1531 else if (ret)
1532 ret = 0;
1534 goto out_unlock;