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>
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)
27 size = sizeof(struct ubi_fm_sb) + \
28 sizeof(struct ubi_fm_hdr) + \
29 sizeof(struct ubi_fm_scan_pool) + \
30 sizeof(struct ubi_fm_scan_pool) + \
31 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + \
32 (sizeof(struct ubi_fm_eba) + \
33 (ubi->peb_count * sizeof(__be32))) + \
34 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
35 return roundup(size, ubi->leb_size);
40 * new_fm_vhdr - allocate a new volume header for fastmap usage.
41 * @ubi: UBI device description object
42 * @vol_id: the VID of the new header
44 * Returns a new struct ubi_vid_hdr on success.
45 * NULL indicates out of memory.
47 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
49 struct ubi_vid_hdr *new;
51 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
55 new->vol_type = UBI_VID_DYNAMIC;
56 new->vol_id = cpu_to_be32(vol_id);
58 /* UBI implementations without fastmap support have to delete the
61 new->compat = UBI_COMPAT_DELETE;
68 * add_aeb - create and add a attach erase block to a given list.
69 * @ai: UBI attach info object
70 * @list: the target list
71 * @pnum: PEB number of the new attach erase block
72 * @ec: erease counter of the new LEB
73 * @scrub: scrub this PEB after attaching
75 * Returns 0 on success, < 0 indicates an internal error.
77 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
78 int pnum, int ec, int scrub)
80 struct ubi_ainf_peb *aeb;
82 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
90 aeb->copy_flag = aeb->sqnum = 0;
92 ai->ec_sum += aeb->ec;
95 if (ai->max_ec < aeb->ec)
98 if (ai->min_ec > aeb->ec)
101 list_add_tail(&aeb->u.list, list);
107 * add_vol - create and add a new volume to ubi_attach_info.
108 * @ai: ubi_attach_info object
109 * @vol_id: VID of the new volume
110 * @used_ebs: number of used EBS
111 * @data_pad: data padding value of the new volume
112 * @vol_type: volume type
113 * @last_eb_bytes: number of bytes in the last LEB
115 * Returns the new struct ubi_ainf_volume on success.
116 * NULL indicates an error.
118 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
119 int used_ebs, int data_pad, u8 vol_type,
122 struct ubi_ainf_volume *av;
123 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
127 av = rb_entry(parent, struct ubi_ainf_volume, rb);
129 if (vol_id > av->vol_id)
135 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
139 av->highest_lnum = av->leb_count = av->used_ebs = 0;
141 av->data_pad = data_pad;
142 av->last_data_size = last_eb_bytes;
144 av->vol_type = vol_type;
146 if (av->vol_type == UBI_STATIC_VOLUME)
147 av->used_ebs = used_ebs;
149 dbg_bld("found volume (ID %i)", vol_id);
151 rb_link_node(&av->rb, parent, p);
152 rb_insert_color(&av->rb, &ai->volumes);
159 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
160 * from it's original list.
161 * @ai: ubi_attach_info object
162 * @aeb: the to be assigned SEB
163 * @av: target scan volume
165 static void assign_aeb_to_av(struct ubi_attach_info *ai,
166 struct ubi_ainf_peb *aeb,
167 struct ubi_ainf_volume *av)
169 struct ubi_ainf_peb *tmp_aeb;
170 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
172 p = &av->root.rb_node;
176 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
177 if (aeb->lnum != tmp_aeb->lnum) {
178 if (aeb->lnum < tmp_aeb->lnum)
188 list_del(&aeb->u.list);
191 rb_link_node(&aeb->u.rb, parent, p);
192 rb_insert_color(&aeb->u.rb, &av->root);
196 * update_vol - inserts or updates a LEB which was found a pool.
197 * @ubi: the UBI device object
198 * @ai: attach info object
199 * @av: the volume this LEB belongs to
200 * @new_vh: the volume header derived from new_aeb
201 * @new_aeb: the AEB to be examined
203 * Returns 0 on success, < 0 indicates an internal error.
205 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
206 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
207 struct ubi_ainf_peb *new_aeb)
209 struct rb_node **p = &av->root.rb_node, *parent = NULL;
210 struct ubi_ainf_peb *aeb, *victim;
215 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
217 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
218 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
226 /* This case can happen if the fastmap gets written
227 * because of a volume change (creation, deletion, ..).
228 * Then a PEB can be within the persistent EBA and the pool.
230 if (aeb->pnum == new_aeb->pnum) {
231 ubi_assert(aeb->lnum == new_aeb->lnum);
232 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
237 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
241 /* new_aeb is newer */
243 victim = kmem_cache_alloc(ai->aeb_slab_cache,
248 victim->ec = aeb->ec;
249 victim->pnum = aeb->pnum;
250 list_add_tail(&victim->u.list, &ai->erase);
252 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
253 av->last_data_size = \
254 be32_to_cpu(new_vh->data_size);
256 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
257 av->vol_id, aeb->lnum, new_aeb->pnum);
259 aeb->ec = new_aeb->ec;
260 aeb->pnum = new_aeb->pnum;
261 aeb->copy_flag = new_vh->copy_flag;
262 aeb->scrub = new_aeb->scrub;
263 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
265 /* new_aeb is older */
267 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
268 av->vol_id, aeb->lnum, new_aeb->pnum);
269 list_add_tail(&new_aeb->u.list, &ai->erase);
274 /* This LEB is new, let's add it to the volume */
276 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
277 av->highest_lnum = be32_to_cpu(new_vh->lnum);
278 av->last_data_size = be32_to_cpu(new_vh->data_size);
281 if (av->vol_type == UBI_STATIC_VOLUME)
282 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
286 rb_link_node(&new_aeb->u.rb, parent, p);
287 rb_insert_color(&new_aeb->u.rb, &av->root);
293 * process_pool_aeb - we found a non-empty PEB in a pool.
294 * @ubi: UBI device object
295 * @ai: attach info object
296 * @new_vh: the volume header derived from new_aeb
297 * @new_aeb: the AEB to be examined
299 * Returns 0 on success, < 0 indicates an internal error.
301 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
302 struct ubi_vid_hdr *new_vh,
303 struct ubi_ainf_peb *new_aeb)
305 struct ubi_ainf_volume *av, *tmp_av = NULL;
306 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
309 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
310 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
311 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
316 /* Find the volume this SEB belongs to */
319 tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
321 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
323 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
334 ubi_err(ubi, "orphaned volume in fastmap pool!");
335 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
336 return UBI_BAD_FASTMAP;
339 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
341 return update_vol(ubi, ai, av, new_vh, new_aeb);
345 * unmap_peb - unmap a PEB.
346 * If fastmap detects a free PEB in the pool it has to check whether
347 * this PEB has been unmapped after writing the fastmap.
349 * @ai: UBI attach info object
350 * @pnum: The PEB to be unmapped
352 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
354 struct ubi_ainf_volume *av;
355 struct rb_node *node, *node2;
356 struct ubi_ainf_peb *aeb;
358 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
359 av = rb_entry(node, struct ubi_ainf_volume, rb);
361 for (node2 = rb_first(&av->root); node2;
362 node2 = rb_next(node2)) {
363 aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
364 if (aeb->pnum == pnum) {
365 rb_erase(&aeb->u.rb, &av->root);
367 kmem_cache_free(ai->aeb_slab_cache, aeb);
375 * scan_pool - scans a pool for changed (no longer empty PEBs).
376 * @ubi: UBI device object
377 * @ai: attach info object
378 * @pebs: an array of all PEB numbers in the to be scanned pool
379 * @pool_size: size of the pool (number of entries in @pebs)
380 * @max_sqnum: pointer to the maximal sequence number
381 * @free: list of PEBs which are most likely free (and go into @ai->free)
383 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
384 * < 0 indicates an internal error.
386 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
387 int *pebs, int pool_size, unsigned long long *max_sqnum,
388 struct list_head *free)
390 struct ubi_vid_hdr *vh;
391 struct ubi_ec_hdr *ech;
392 struct ubi_ainf_peb *new_aeb;
393 int i, pnum, err, ret = 0;
395 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
399 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
405 dbg_bld("scanning fastmap pool: size = %i", pool_size);
408 * Now scan all PEBs in the pool to find changes which have been made
409 * after the creation of the fastmap
411 for (i = 0; i < pool_size; i++) {
415 pnum = be32_to_cpu(pebs[i]);
417 if (ubi_io_is_bad(ubi, pnum)) {
418 ubi_err(ubi, "bad PEB in fastmap pool!");
419 ret = UBI_BAD_FASTMAP;
423 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
424 if (err && err != UBI_IO_BITFLIPS) {
425 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
427 ret = err > 0 ? UBI_BAD_FASTMAP : err;
429 } else if (err == UBI_IO_BITFLIPS)
433 * Older UBI implementations have image_seq set to zero, so
434 * we shouldn't fail if image_seq == 0.
436 image_seq = be32_to_cpu(ech->image_seq);
438 if (image_seq && (image_seq != ubi->image_seq)) {
439 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
440 be32_to_cpu(ech->image_seq), ubi->image_seq);
441 ret = UBI_BAD_FASTMAP;
445 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
446 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
447 unsigned long long ec = be64_to_cpu(ech->ec);
449 dbg_bld("Adding PEB to free: %i", pnum);
450 if (err == UBI_IO_FF_BITFLIPS)
451 add_aeb(ai, free, pnum, ec, 1);
453 add_aeb(ai, free, pnum, ec, 0);
455 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
456 dbg_bld("Found non empty PEB:%i in pool", pnum);
458 if (err == UBI_IO_BITFLIPS)
461 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
468 new_aeb->ec = be64_to_cpu(ech->ec);
469 new_aeb->pnum = pnum;
470 new_aeb->lnum = be32_to_cpu(vh->lnum);
471 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
472 new_aeb->copy_flag = vh->copy_flag;
473 new_aeb->scrub = scrub;
475 if (*max_sqnum < new_aeb->sqnum)
476 *max_sqnum = new_aeb->sqnum;
478 err = process_pool_aeb(ubi, ai, vh, new_aeb);
480 ret = err > 0 ? UBI_BAD_FASTMAP : err;
484 /* We are paranoid and fall back to scanning mode */
485 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
486 ret = err > 0 ? UBI_BAD_FASTMAP : err;
493 ubi_free_vid_hdr(ubi, vh);
499 * count_fastmap_pebs - Counts the PEBs found by fastmap.
500 * @ai: The UBI attach info object
502 static int count_fastmap_pebs(struct ubi_attach_info *ai)
504 struct ubi_ainf_peb *aeb;
505 struct ubi_ainf_volume *av;
506 struct rb_node *rb1, *rb2;
509 list_for_each_entry(aeb, &ai->erase, u.list)
512 list_for_each_entry(aeb, &ai->free, u.list)
515 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
516 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
523 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
524 * @ubi: UBI device object
525 * @ai: UBI attach info object
526 * @fm: the fastmap to be attached
528 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
529 * < 0 indicates an internal error.
531 static int ubi_attach_fastmap(struct ubi_device *ubi,
532 struct ubi_attach_info *ai,
533 struct ubi_fastmap_layout *fm)
535 struct list_head used, free;
536 struct ubi_ainf_volume *av;
537 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
538 struct ubi_fm_sb *fmsb;
539 struct ubi_fm_hdr *fmhdr;
540 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
541 struct ubi_fm_ec *fmec;
542 struct ubi_fm_volhdr *fmvhdr;
543 struct ubi_fm_eba *fm_eba;
544 int ret, i, j, pool_size, wl_pool_size;
545 size_t fm_pos = 0, fm_size = ubi->fm_size;
546 unsigned long long max_sqnum = 0;
547 void *fm_raw = ubi->fm_buf;
549 INIT_LIST_HEAD(&used);
550 INIT_LIST_HEAD(&free);
551 ai->min_ec = UBI_MAX_ERASECOUNTER;
553 fmsb = (struct ubi_fm_sb *)(fm_raw);
554 ai->max_sqnum = fmsb->sqnum;
555 fm_pos += sizeof(struct ubi_fm_sb);
556 if (fm_pos >= fm_size)
559 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
560 fm_pos += sizeof(*fmhdr);
561 if (fm_pos >= fm_size)
564 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
565 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
566 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
570 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
571 fm_pos += sizeof(*fmpl1);
572 if (fm_pos >= fm_size)
574 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
575 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
576 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
580 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
581 fm_pos += sizeof(*fmpl2);
582 if (fm_pos >= fm_size)
584 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
585 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
586 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
590 pool_size = be16_to_cpu(fmpl1->size);
591 wl_pool_size = be16_to_cpu(fmpl2->size);
592 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
593 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
595 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
596 ubi_err(ubi, "bad pool size: %i", pool_size);
600 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
601 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
606 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
607 fm->max_pool_size < 0) {
608 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
612 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
613 fm->max_wl_pool_size < 0) {
614 ubi_err(ubi, "bad maximal WL pool size: %i",
615 fm->max_wl_pool_size);
619 /* read EC values from free list */
620 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
621 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
622 fm_pos += sizeof(*fmec);
623 if (fm_pos >= fm_size)
626 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
627 be32_to_cpu(fmec->ec), 0);
630 /* read EC values from used list */
631 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
632 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
633 fm_pos += sizeof(*fmec);
634 if (fm_pos >= fm_size)
637 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
638 be32_to_cpu(fmec->ec), 0);
641 /* read EC values from scrub list */
642 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
643 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
644 fm_pos += sizeof(*fmec);
645 if (fm_pos >= fm_size)
648 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
649 be32_to_cpu(fmec->ec), 1);
652 /* read EC values from erase list */
653 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
654 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
655 fm_pos += sizeof(*fmec);
656 if (fm_pos >= fm_size)
659 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
660 be32_to_cpu(fmec->ec), 1);
663 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
664 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
666 /* Iterate over all volumes and read their EBA table */
667 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
668 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
669 fm_pos += sizeof(*fmvhdr);
670 if (fm_pos >= fm_size)
673 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
674 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
675 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
679 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
680 be32_to_cpu(fmvhdr->used_ebs),
681 be32_to_cpu(fmvhdr->data_pad),
683 be32_to_cpu(fmvhdr->last_eb_bytes));
689 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
690 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
692 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
693 fm_pos += sizeof(*fm_eba);
694 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
695 if (fm_pos >= fm_size)
698 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
699 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
700 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
704 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
705 int pnum = be32_to_cpu(fm_eba->pnum[j]);
707 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
711 list_for_each_entry(tmp_aeb, &used, u.list) {
712 if (tmp_aeb->pnum == pnum) {
719 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
725 if (av->highest_lnum <= aeb->lnum)
726 av->highest_lnum = aeb->lnum;
728 assign_aeb_to_av(ai, aeb, av);
730 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
731 aeb->pnum, aeb->lnum, av->vol_id);
735 ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum, &free);
739 ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum, &free);
743 if (max_sqnum > ai->max_sqnum)
744 ai->max_sqnum = max_sqnum;
746 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
747 list_move_tail(&tmp_aeb->u.list, &ai->free);
749 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
750 list_move_tail(&tmp_aeb->u.list, &ai->erase);
752 ubi_assert(list_empty(&free));
755 * If fastmap is leaking PEBs (must not happen), raise a
756 * fat warning and fall back to scanning mode.
757 * We do this here because in ubi_wl_init() it's too late
758 * and we cannot fall back to scanning.
760 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
761 ai->bad_peb_count - fm->used_blocks))
767 ret = UBI_BAD_FASTMAP;
769 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
770 list_del(&tmp_aeb->u.list);
771 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
773 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
774 list_del(&tmp_aeb->u.list);
775 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
782 * ubi_scan_fastmap - scan the fastmap.
783 * @ubi: UBI device object
784 * @ai: UBI attach info to be filled
785 * @fm_anchor: The fastmap starts at this PEB
787 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
788 * UBI_BAD_FASTMAP if one was found but is not usable.
789 * < 0 indicates an internal error.
791 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
794 struct ubi_fm_sb *fmsb, *fmsb2;
795 struct ubi_vid_hdr *vh;
796 struct ubi_ec_hdr *ech;
797 struct ubi_fastmap_layout *fm;
798 int i, used_blocks, pnum, ret = 0;
801 unsigned long long sqnum = 0;
803 down_write(&ubi->fm_protect);
804 memset(ubi->fm_buf, 0, ubi->fm_size);
806 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
812 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
819 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
820 if (ret && ret != UBI_IO_BITFLIPS)
822 else if (ret == UBI_IO_BITFLIPS)
823 fm->to_be_tortured[0] = 1;
825 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
826 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
827 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
828 ret = UBI_BAD_FASTMAP;
832 if (fmsb->version != UBI_FM_FMT_VERSION) {
833 ubi_err(ubi, "bad fastmap version: %i, expected: %i",
834 fmsb->version, UBI_FM_FMT_VERSION);
835 ret = UBI_BAD_FASTMAP;
839 used_blocks = be32_to_cpu(fmsb->used_blocks);
840 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
841 ubi_err(ubi, "number of fastmap blocks is invalid: %i",
843 ret = UBI_BAD_FASTMAP;
847 fm_size = ubi->leb_size * used_blocks;
848 if (fm_size != ubi->fm_size) {
849 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
850 fm_size, ubi->fm_size);
851 ret = UBI_BAD_FASTMAP;
855 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
861 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
867 for (i = 0; i < used_blocks; i++) {
870 pnum = be32_to_cpu(fmsb->block_loc[i]);
872 if (ubi_io_is_bad(ubi, pnum)) {
873 ret = UBI_BAD_FASTMAP;
877 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
878 if (ret && ret != UBI_IO_BITFLIPS) {
879 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
882 ret = UBI_BAD_FASTMAP;
884 } else if (ret == UBI_IO_BITFLIPS)
885 fm->to_be_tortured[i] = 1;
887 image_seq = be32_to_cpu(ech->image_seq);
889 ubi->image_seq = image_seq;
892 * Older UBI implementations have image_seq set to zero, so
893 * we shouldn't fail if image_seq == 0.
895 if (image_seq && (image_seq != ubi->image_seq)) {
896 ubi_err(ubi, "wrong image seq:%d instead of %d",
897 be32_to_cpu(ech->image_seq), ubi->image_seq);
898 ret = UBI_BAD_FASTMAP;
902 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
903 if (ret && ret != UBI_IO_BITFLIPS) {
904 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
910 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
911 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
912 be32_to_cpu(vh->vol_id),
913 UBI_FM_SB_VOLUME_ID);
914 ret = UBI_BAD_FASTMAP;
918 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
919 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
920 be32_to_cpu(vh->vol_id),
921 UBI_FM_DATA_VOLUME_ID);
922 ret = UBI_BAD_FASTMAP;
927 if (sqnum < be64_to_cpu(vh->sqnum))
928 sqnum = be64_to_cpu(vh->sqnum);
930 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
931 ubi->leb_start, ubi->leb_size);
932 if (ret && ret != UBI_IO_BITFLIPS) {
933 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
934 "err: %i)", i, pnum, ret);
942 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
943 tmp_crc = be32_to_cpu(fmsb2->data_crc);
945 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
946 if (crc != tmp_crc) {
947 ubi_err(ubi, "fastmap data CRC is invalid");
948 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
950 ret = UBI_BAD_FASTMAP;
954 fmsb2->sqnum = sqnum;
956 fm->used_blocks = used_blocks;
958 ret = ubi_attach_fastmap(ubi, ai, fm);
961 ret = UBI_BAD_FASTMAP;
965 for (i = 0; i < used_blocks; i++) {
966 struct ubi_wl_entry *e;
968 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
977 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
978 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
983 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
984 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
985 ubi_msg(ubi, "attached by fastmap");
986 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
987 ubi_msg(ubi, "fastmap WL pool size: %d",
988 ubi->fm_wl_pool.max_size);
989 ubi->fm_disabled = 0;
991 ubi_free_vid_hdr(ubi, vh);
994 up_write(&ubi->fm_protect);
995 if (ret == UBI_BAD_FASTMAP)
996 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1000 ubi_free_vid_hdr(ubi, vh);
1009 * ubi_write_fastmap - writes a fastmap.
1010 * @ubi: UBI device object
1011 * @new_fm: the to be written fastmap
1013 * Returns 0 on success, < 0 indicates an internal error.
1015 static int ubi_write_fastmap(struct ubi_device *ubi,
1016 struct ubi_fastmap_layout *new_fm)
1020 struct ubi_fm_sb *fmsb;
1021 struct ubi_fm_hdr *fmh;
1022 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1023 struct ubi_fm_ec *fec;
1024 struct ubi_fm_volhdr *fvh;
1025 struct ubi_fm_eba *feba;
1026 struct rb_node *node;
1027 struct ubi_wl_entry *wl_e;
1028 struct ubi_volume *vol;
1029 struct ubi_vid_hdr *avhdr, *dvhdr;
1030 struct ubi_work *ubi_wrk;
1031 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1032 int scrub_peb_count, erase_peb_count;
1034 fm_raw = ubi->fm_buf;
1035 memset(ubi->fm_buf, 0, ubi->fm_size);
1037 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1043 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1049 spin_lock(&ubi->volumes_lock);
1050 spin_lock(&ubi->wl_lock);
1052 fmsb = (struct ubi_fm_sb *)fm_raw;
1053 fm_pos += sizeof(*fmsb);
1054 ubi_assert(fm_pos <= ubi->fm_size);
1056 fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1057 fm_pos += sizeof(*fmh);
1058 ubi_assert(fm_pos <= ubi->fm_size);
1060 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1061 fmsb->version = UBI_FM_FMT_VERSION;
1062 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1063 /* the max sqnum will be filled in while *reading* the fastmap */
1066 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1069 scrub_peb_count = 0;
1070 erase_peb_count = 0;
1073 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1074 fm_pos += sizeof(*fmpl1);
1075 fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1076 fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1077 fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1079 for (i = 0; i < ubi->fm_pool.size; i++)
1080 fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1082 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1083 fm_pos += sizeof(*fmpl2);
1084 fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1085 fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1086 fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1088 for (i = 0; i < ubi->fm_wl_pool.size; i++)
1089 fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1091 for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1092 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1093 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1095 fec->pnum = cpu_to_be32(wl_e->pnum);
1096 fec->ec = cpu_to_be32(wl_e->ec);
1099 fm_pos += sizeof(*fec);
1100 ubi_assert(fm_pos <= ubi->fm_size);
1102 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1104 for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1105 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1106 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1108 fec->pnum = cpu_to_be32(wl_e->pnum);
1109 fec->ec = cpu_to_be32(wl_e->ec);
1112 fm_pos += sizeof(*fec);
1113 ubi_assert(fm_pos <= ubi->fm_size);
1116 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++) {
1117 list_for_each_entry(wl_e, &ubi->pq[i], u.list) {
1118 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1120 fec->pnum = cpu_to_be32(wl_e->pnum);
1121 fec->ec = cpu_to_be32(wl_e->ec);
1124 fm_pos += sizeof(*fec);
1125 ubi_assert(fm_pos <= ubi->fm_size);
1128 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1130 for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1131 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1132 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1134 fec->pnum = cpu_to_be32(wl_e->pnum);
1135 fec->ec = cpu_to_be32(wl_e->ec);
1138 fm_pos += sizeof(*fec);
1139 ubi_assert(fm_pos <= ubi->fm_size);
1141 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1144 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1145 if (ubi_is_erase_work(ubi_wrk)) {
1149 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1151 fec->pnum = cpu_to_be32(wl_e->pnum);
1152 fec->ec = cpu_to_be32(wl_e->ec);
1155 fm_pos += sizeof(*fec);
1156 ubi_assert(fm_pos <= ubi->fm_size);
1159 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1161 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1162 vol = ubi->volumes[i];
1169 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1170 fm_pos += sizeof(*fvh);
1171 ubi_assert(fm_pos <= ubi->fm_size);
1173 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1174 fvh->vol_id = cpu_to_be32(vol->vol_id);
1175 fvh->vol_type = vol->vol_type;
1176 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1177 fvh->data_pad = cpu_to_be32(vol->data_pad);
1178 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1180 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1181 vol->vol_type == UBI_STATIC_VOLUME);
1183 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1184 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1185 ubi_assert(fm_pos <= ubi->fm_size);
1187 for (j = 0; j < vol->reserved_pebs; j++)
1188 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1190 feba->reserved_pebs = cpu_to_be32(j);
1191 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1193 fmh->vol_count = cpu_to_be32(vol_count);
1194 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1196 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1199 spin_unlock(&ubi->wl_lock);
1200 spin_unlock(&ubi->volumes_lock);
1202 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1203 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1205 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1209 for (i = 0; i < new_fm->used_blocks; i++) {
1210 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1211 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1215 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1218 for (i = 1; i < new_fm->used_blocks; i++) {
1219 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1220 dvhdr->lnum = cpu_to_be32(i);
1221 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1222 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1223 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1225 ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1226 new_fm->e[i]->pnum);
1231 for (i = 0; i < new_fm->used_blocks; i++) {
1232 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1233 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1235 ubi_err(ubi, "unable to write fastmap to PEB %i!",
1236 new_fm->e[i]->pnum);
1244 dbg_bld("fastmap written!");
1247 ubi_free_vid_hdr(ubi, avhdr);
1248 ubi_free_vid_hdr(ubi, dvhdr);
1254 * erase_block - Manually erase a PEB.
1255 * @ubi: UBI device object
1256 * @pnum: PEB to be erased
1258 * Returns the new EC value on success, < 0 indicates an internal error.
1260 static int erase_block(struct ubi_device *ubi, int pnum)
1263 struct ubi_ec_hdr *ec_hdr;
1266 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1270 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1273 else if (ret && ret != UBI_IO_BITFLIPS) {
1278 ret = ubi_io_sync_erase(ubi, pnum, 0);
1282 ec = be64_to_cpu(ec_hdr->ec);
1284 if (ec > UBI_MAX_ERASECOUNTER) {
1289 ec_hdr->ec = cpu_to_be64(ec);
1290 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1301 * invalidate_fastmap - destroys a fastmap.
1302 * @ubi: UBI device object
1303 * @fm: the fastmap to be destroyed
1305 * Returns 0 on success, < 0 indicates an internal error.
1307 static int invalidate_fastmap(struct ubi_device *ubi,
1308 struct ubi_fastmap_layout *fm)
1311 struct ubi_vid_hdr *vh;
1313 ret = erase_block(ubi, fm->e[0]->pnum);
1317 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1321 /* deleting the current fastmap SB is not enough, an old SB may exist,
1322 * so create a (corrupted) SB such that fastmap will find it and fall
1323 * back to scanning mode in any case */
1324 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1325 ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1331 * ubi_update_fastmap - will be called by UBI if a volume changes or
1332 * a fastmap pool becomes full.
1333 * @ubi: UBI device object
1335 * Returns 0 on success, < 0 indicates an internal error.
1337 int ubi_update_fastmap(struct ubi_device *ubi)
1340 struct ubi_fastmap_layout *new_fm, *old_fm;
1341 struct ubi_wl_entry *tmp_e;
1343 down_write(&ubi->fm_protect);
1345 ubi_refill_pools(ubi);
1347 if (ubi->ro_mode || ubi->fm_disabled) {
1348 up_write(&ubi->fm_protect);
1352 ret = ubi_ensure_anchor_pebs(ubi);
1354 up_write(&ubi->fm_protect);
1358 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1360 up_write(&ubi->fm_protect);
1364 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1368 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1369 ubi_err(ubi, "fastmap too large");
1374 for (i = 1; i < new_fm->used_blocks; i++) {
1375 spin_lock(&ubi->wl_lock);
1376 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1377 spin_unlock(&ubi->wl_lock);
1379 if (!tmp_e && !old_fm) {
1381 ubi_err(ubi, "could not get any free erase block");
1383 for (j = 1; j < i; j++)
1384 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1388 } else if (!tmp_e && old_fm) {
1389 ret = erase_block(ubi, old_fm->e[i]->pnum);
1393 for (j = 1; j < i; j++)
1394 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1397 ubi_err(ubi, "could not erase old fastmap PEB");
1400 new_fm->e[i] = old_fm->e[i];
1402 new_fm->e[i] = tmp_e;
1405 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1406 old_fm->to_be_tortured[i]);
1410 spin_lock(&ubi->wl_lock);
1411 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1412 spin_unlock(&ubi->wl_lock);
1415 /* no fresh anchor PEB was found, reuse the old one */
1417 ret = erase_block(ubi, old_fm->e[0]->pnum);
1420 ubi_err(ubi, "could not erase old anchor PEB");
1422 for (i = 1; i < new_fm->used_blocks; i++)
1423 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1427 new_fm->e[0] = old_fm->e[0];
1428 new_fm->e[0]->ec = ret;
1430 /* we've got a new anchor PEB, return the old one */
1431 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1432 old_fm->to_be_tortured[0]);
1433 new_fm->e[0] = tmp_e;
1438 ubi_err(ubi, "could not find any anchor PEB");
1440 for (i = 1; i < new_fm->used_blocks; i++)
1441 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1446 new_fm->e[0] = tmp_e;
1449 down_write(&ubi->work_sem);
1450 down_write(&ubi->fm_eba_sem);
1451 ret = ubi_write_fastmap(ubi, new_fm);
1452 up_write(&ubi->fm_eba_sem);
1453 up_write(&ubi->work_sem);
1459 up_write(&ubi->fm_protect);
1466 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1470 ret = invalidate_fastmap(ubi, old_fm);
1472 ubi_err(ubi, "Unable to invalidiate current fastmap!");