Lines Matching +full:ubi +full:- +full:volume +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
10 * This file includes volume table manipulation code. The volume table is an
11 * on-flash table containing volume meta-data like name, number of reserved
12 * physical eraseblocks, type, etc. The volume table is stored in the so-called
13 * "layout volume".
15 * The layout volume is an internal volume which is organized as follows. It
16 * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
17 * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
18 * other. This redundancy guarantees robustness to unclean reboots. The volume
19 * table is basically an array of volume table records. Each record contains
20 * full information about the volume and protected by a CRC checksum. Note,
21 * nowadays we use the atomic LEB change operation when updating the volume
25 * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
26 * erased, and the updated volume table is written back to LEB 0. Then same for
29 * In this UBI implementation the on-flash volume table does not contain any
32 * But it would still be beneficial to store this information in the volume
33 * table. For example, suppose we have a static volume X, and all its physical
36 * corresponding to the volume X. According to the volume table volume X does
40 * The volume table also stores so-called "update marker", which is used for
41 * volume updates. Before updating the volume, the update marker is set, and
43 * the update operation was interrupted (e.g. by an unclean reboot) - the
44 * update marker is still there and we know that the volume's contents is
52 #include "ubi.h"
54 static void self_vtbl_check(const struct ubi_device *ubi);
56 /* Empty volume table record */
60 * ubi_update_layout_vol - helper for updatting layout volumes on flash
61 * @ubi: UBI device description object
63 static int ubi_update_layout_vol(struct ubi_device *ubi) in ubi_update_layout_vol() argument
68 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)]; in ubi_update_layout_vol()
70 err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl, in ubi_update_layout_vol()
71 ubi->vtbl_size); in ubi_update_layout_vol()
80 * ubi_change_vtbl_record - change volume table record.
81 * @ubi: UBI device description object
83 * @vtbl_rec: new volume table record
85 * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
86 * volume table record is written. The caller does not have to calculate CRC of
90 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, in ubi_change_vtbl_record() argument
96 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots); in ubi_change_vtbl_record()
102 vtbl_rec->crc = cpu_to_be32(crc); in ubi_change_vtbl_record()
105 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); in ubi_change_vtbl_record()
106 err = ubi_update_layout_vol(ubi); in ubi_change_vtbl_record()
108 self_vtbl_check(ubi); in ubi_change_vtbl_record()
113 * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
114 * @ubi: UBI device description object
117 * This function re-names multiple volumes specified in @req in the volume
121 int ubi_vtbl_rename_volumes(struct ubi_device *ubi, in ubi_vtbl_rename_volumes() argument
128 struct ubi_volume *vol = re->desc->vol; in ubi_vtbl_rename_volumes()
129 struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id]; in ubi_vtbl_rename_volumes()
131 if (re->remove) { in ubi_vtbl_rename_volumes()
137 vtbl_rec->name_len = cpu_to_be16(re->new_name_len); in ubi_vtbl_rename_volumes()
138 memcpy(vtbl_rec->name, re->new_name, re->new_name_len); in ubi_vtbl_rename_volumes()
139 memset(vtbl_rec->name + re->new_name_len, 0, in ubi_vtbl_rename_volumes()
140 UBI_VOL_NAME_MAX + 1 - re->new_name_len); in ubi_vtbl_rename_volumes()
143 vtbl_rec->crc = cpu_to_be32(crc); in ubi_vtbl_rename_volumes()
146 return ubi_update_layout_vol(ubi); in ubi_vtbl_rename_volumes()
150 * vtbl_check - check if volume table is not corrupted and sensible.
151 * @ubi: UBI device description object
152 * @vtbl: volume table
155 * and %-EINVAL if it contains inconsistent data.
157 static int vtbl_check(const struct ubi_device *ubi, in vtbl_check() argument
165 for (i = 0; i < ubi->vtbl_slots; i++) { in vtbl_check()
178 ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x", in vtbl_check()
199 if (alignment > ubi->leb_size || alignment == 0) { in vtbl_check()
204 n = alignment & (ubi->min_io_size - 1); in vtbl_check()
210 n = ubi->leb_size % alignment; in vtbl_check()
212 ubi_err(ubi, "bad data_pad, has to be %d", n); in vtbl_check()
227 if (reserved_pebs > ubi->good_peb_count) { in vtbl_check()
228 ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d", in vtbl_check()
229 reserved_pebs, ubi->good_peb_count); in vtbl_check()
251 for (i = 0; i < ubi->vtbl_slots - 1; i++) { in vtbl_check()
252 for (n = i + 1; n < ubi->vtbl_slots; n++) { in vtbl_check()
258 ubi_err(ubi, "volumes %d and %d have the same name \"%s\"", in vtbl_check()
262 return -EINVAL; in vtbl_check()
270 ubi_err(ubi, "volume table check failed: record %d, error %d", i, err); in vtbl_check()
272 return -EINVAL; in vtbl_check()
276 * create_vtbl - create a copy of volume table.
277 * @ubi: UBI device description object
279 * @copy: number of the volume table copy
280 * @vtbl: contents of the volume table
285 static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai, in create_vtbl() argument
293 dbg_gen("create volume table (copy #%d)", copy + 1); in create_vtbl()
295 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in create_vtbl()
297 return -ENOMEM; in create_vtbl()
302 new_aeb = ubi_early_get_peb(ubi, ai); in create_vtbl()
308 vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE; in create_vtbl()
309 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID); in create_vtbl()
310 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; in create_vtbl()
311 vid_hdr->data_size = vid_hdr->used_ebs = in create_vtbl()
312 vid_hdr->data_pad = cpu_to_be32(0); in create_vtbl()
313 vid_hdr->lnum = cpu_to_be32(copy); in create_vtbl()
314 vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum); in create_vtbl()
317 err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vidb); in create_vtbl()
321 /* Write the layout volume contents */ in create_vtbl()
322 err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size); in create_vtbl()
330 err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0); in create_vtbl()
336 if (err == -EIO && ++tries <= 5) { in create_vtbl()
341 list_add(&new_aeb->u.list, &ai->erase); in create_vtbl()
352 * process_lvol - process the layout volume.
353 * @ubi: UBI device description object
355 * @av: layout volume attaching information
357 * This function is responsible for reading the layout volume, ensuring it is
358 * not corrupted, and recovering from corruptions if needed. Returns volume
361 static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi, in process_lvol() argument
372 * UBI goes through the following steps when it changes the layout in process_lvol()
373 * volume: in process_lvol()
396 dbg_gen("check layout volume"); in process_lvol()
399 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) { in process_lvol()
400 leb[aeb->lnum] = vzalloc(ubi->vtbl_size); in process_lvol()
401 if (!leb[aeb->lnum]) { in process_lvol()
402 err = -ENOMEM; in process_lvol()
406 err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0, in process_lvol()
407 ubi->vtbl_size); in process_lvol()
410 * Scrub the PEB later. Note, -EBADMSG indicates an in process_lvol()
414 * aeb->scrub). If the data is not OK, the contents of in process_lvol()
416 * aeb->scrub will be cleared in in process_lvol()
419 aeb->scrub = 1; in process_lvol()
424 err = -EINVAL; in process_lvol()
426 leb_corrupted[0] = vtbl_check(ubi, leb[0]); in process_lvol()
435 ubi->vtbl_size); in process_lvol()
437 ubi_warn(ubi, "volume table copy #2 is corrupted"); in process_lvol()
438 err = create_vtbl(ubi, ai, 1, leb[0]); in process_lvol()
441 ubi_msg(ubi, "volume table was restored"); in process_lvol()
450 leb_corrupted[1] = vtbl_check(ubi, leb[1]); in process_lvol()
456 ubi_err(ubi, "both volume tables are corrupted"); in process_lvol()
460 ubi_warn(ubi, "volume table copy #1 is corrupted"); in process_lvol()
461 err = create_vtbl(ubi, ai, 0, leb[1]); in process_lvol()
464 ubi_msg(ubi, "volume table was restored"); in process_lvol()
477 * create_empty_lvol - create empty layout volume.
478 * @ubi: UBI device description object
481 * This function returns volume table contents in case of success and a
484 static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi, in create_empty_lvol() argument
490 vtbl = vzalloc(ubi->vtbl_size); in create_empty_lvol()
492 return ERR_PTR(-ENOMEM); in create_empty_lvol()
494 for (i = 0; i < ubi->vtbl_slots; i++) in create_empty_lvol()
500 err = create_vtbl(ubi, ai, i, vtbl); in create_empty_lvol()
511 * init_volumes - initialize volume information for existing volumes.
512 * @ubi: UBI device description object
514 * @vtbl: volume table
516 * This function allocates volume description objects for existing volumes.
520 static int init_volumes(struct ubi_device *ubi, in init_volumes() argument
528 for (i = 0; i < ubi->vtbl_slots; i++) { in init_volumes()
536 return -ENOMEM; in init_volumes()
538 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); in init_volumes()
539 vol->alignment = be32_to_cpu(vtbl[i].alignment); in init_volumes()
540 vol->data_pad = be32_to_cpu(vtbl[i].data_pad); in init_volumes()
541 vol->upd_marker = vtbl[i].upd_marker; in init_volumes()
542 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? in init_volumes()
544 vol->name_len = be16_to_cpu(vtbl[i].name_len); in init_volumes()
545 vol->usable_leb_size = ubi->leb_size - vol->data_pad; in init_volumes()
546 memcpy(vol->name, vtbl[i].name, vol->name_len); in init_volumes()
547 vol->name[vol->name_len] = '\0'; in init_volumes()
548 vol->vol_id = i; in init_volumes()
551 vol->skip_check = 1; in init_volumes()
554 /* Auto re-size flag may be set only for one volume */ in init_volumes()
555 if (ubi->autoresize_vol_id != -1) { in init_volumes()
556 ubi_err(ubi, "more than one auto-resize volume (%d and %d)", in init_volumes()
557 ubi->autoresize_vol_id, i); in init_volumes()
559 return -EINVAL; in init_volumes()
562 ubi->autoresize_vol_id = i; in init_volumes()
565 ubi_assert(!ubi->volumes[i]); in init_volumes()
566 ubi->volumes[i] = vol; in init_volumes()
567 ubi->vol_count += 1; in init_volumes()
568 vol->ubi = ubi; in init_volumes()
569 reserved_pebs += vol->reserved_pebs; in init_volumes()
572 * We use ubi->peb_count and not vol->reserved_pebs because in init_volumes()
574 * resize/check the bitmap upon volume resize too. in init_volumes()
577 err = ubi_fastmap_init_checkmap(vol, ubi->peb_count); in init_volumes()
582 * In case of dynamic volume UBI knows nothing about how many in init_volumes()
583 * data is stored there. So assume the whole volume is used. in init_volumes()
585 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { in init_volumes()
586 vol->used_ebs = vol->reserved_pebs; in init_volumes()
587 vol->last_eb_bytes = vol->usable_leb_size; in init_volumes()
588 vol->used_bytes = in init_volumes()
589 (long long)vol->used_ebs * vol->usable_leb_size; in init_volumes()
595 if (!av || !av->leb_count) { in init_volumes()
597 * No eraseblocks belonging to this volume found. We in init_volumes()
598 * don't actually know whether this static volume is in init_volumes()
601 * stored on flash. So we just assume the volume is in init_volumes()
607 if (av->leb_count != av->used_ebs) { in init_volumes()
609 * We found a static volume which misses several in init_volumes()
612 ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted", in init_volumes()
613 av->vol_id, av->used_ebs - av->leb_count); in init_volumes()
614 vol->corrupted = 1; in init_volumes()
618 vol->used_ebs = av->used_ebs; in init_volumes()
619 vol->used_bytes = in init_volumes()
620 (long long)(vol->used_ebs - 1) * vol->usable_leb_size; in init_volumes()
621 vol->used_bytes += av->last_data_size; in init_volumes()
622 vol->last_eb_bytes = av->last_data_size; in init_volumes()
625 /* And add the layout volume */ in init_volumes()
628 return -ENOMEM; in init_volumes()
630 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS; in init_volumes()
631 vol->alignment = UBI_LAYOUT_VOLUME_ALIGN; in init_volumes()
632 vol->vol_type = UBI_DYNAMIC_VOLUME; in init_volumes()
633 vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1; in init_volumes()
634 memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1); in init_volumes()
635 vol->usable_leb_size = ubi->leb_size; in init_volumes()
636 vol->used_ebs = vol->reserved_pebs; in init_volumes()
637 vol->last_eb_bytes = vol->reserved_pebs; in init_volumes()
638 vol->used_bytes = in init_volumes()
639 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad); in init_volumes()
640 vol->vol_id = UBI_LAYOUT_VOLUME_ID; in init_volumes()
641 vol->ref_count = 1; in init_volumes()
643 ubi_assert(!ubi->volumes[i]); in init_volumes()
644 ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol; in init_volumes()
645 reserved_pebs += vol->reserved_pebs; in init_volumes()
646 ubi->vol_count += 1; in init_volumes()
647 vol->ubi = ubi; in init_volumes()
652 if (reserved_pebs > ubi->avail_pebs) { in init_volumes()
653 ubi_err(ubi, "not enough PEBs, required %d, available %d", in init_volumes()
654 reserved_pebs, ubi->avail_pebs); in init_volumes()
655 if (ubi->corr_peb_count) in init_volumes()
656 ubi_err(ubi, "%d PEBs are corrupted and not used", in init_volumes()
657 ubi->corr_peb_count); in init_volumes()
658 return -ENOSPC; in init_volumes()
660 ubi->rsvd_pebs += reserved_pebs; in init_volumes()
661 ubi->avail_pebs -= reserved_pebs; in init_volumes()
667 * check_av - check volume attaching information.
668 * @vol: UBI volume description object
669 * @av: volume attaching information
671 * This function returns zero if the volume attaching information is consistent
672 * to the data read from the volume tabla, and %-EINVAL if not.
679 if (av->highest_lnum >= vol->reserved_pebs) { in check_av()
683 if (av->leb_count > vol->reserved_pebs) { in check_av()
687 if (av->vol_type != vol->vol_type) { in check_av()
691 if (av->used_ebs > vol->reserved_pebs) { in check_av()
695 if (av->data_pad != vol->data_pad) { in check_av()
702 ubi_err(vol->ubi, "bad attaching information, error %d", err); in check_av()
705 return -EINVAL; in check_av()
709 * check_attaching_info - check that attaching information.
710 * @ubi: UBI device description object
713 * Even though we protect on-flash data by CRC checksums, we still don't trust
715 * the information read from the volume table. Returns zero if the attaching
716 * information is OK and %-EINVAL if it is not.
718 static int check_attaching_info(const struct ubi_device *ubi, in check_attaching_info() argument
725 if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { in check_attaching_info()
726 ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d", in check_attaching_info()
727 ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); in check_attaching_info()
728 return -EINVAL; in check_attaching_info()
731 if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT && in check_attaching_info()
732 ai->highest_vol_id < UBI_INTERNAL_VOL_START) { in check_attaching_info()
733 ubi_err(ubi, "too large volume ID %d found", in check_attaching_info()
734 ai->highest_vol_id); in check_attaching_info()
735 return -EINVAL; in check_attaching_info()
738 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { in check_attaching_info()
742 vol = ubi->volumes[i]; in check_attaching_info()
749 if (vol->reserved_pebs == 0) { in check_attaching_info()
750 ubi_assert(i < ubi->vtbl_slots); in check_attaching_info()
756 * During attaching we found a volume which does not in check_attaching_info()
757 * exist according to the information in the volume in check_attaching_info()
759 * reboot while the volume was being removed. Discard in check_attaching_info()
762 ubi_msg(ubi, "finish volume %d removal", av->vol_id); in check_attaching_info()
775 * ubi_read_volume_table - read the volume table.
776 * @ubi: UBI device description object
779 * This function reads volume table, checks it, recover from errors if needed,
783 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai) in ubi_read_volume_table() argument
795 if (ubi->leb_size < UBI_VTBL_RECORD_SIZE) { in ubi_read_volume_table()
796 ubi_err(ubi, "LEB size too small for a volume record"); in ubi_read_volume_table()
797 return -EINVAL; in ubi_read_volume_table()
800 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE; in ubi_read_volume_table()
801 if (ubi->vtbl_slots > UBI_MAX_VOLUMES) in ubi_read_volume_table()
802 ubi->vtbl_slots = UBI_MAX_VOLUMES; in ubi_read_volume_table()
804 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; in ubi_read_volume_table()
805 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); in ubi_read_volume_table()
810 * No logical eraseblocks belonging to the layout volume were in ubi_read_volume_table()
812 * this case we create empty layout volume. in ubi_read_volume_table()
817 if (ai->is_empty) { in ubi_read_volume_table()
818 ubi->vtbl = create_empty_lvol(ubi, ai); in ubi_read_volume_table()
819 if (IS_ERR(ubi->vtbl)) in ubi_read_volume_table()
820 return PTR_ERR(ubi->vtbl); in ubi_read_volume_table()
822 ubi_err(ubi, "the layout volume was not found"); in ubi_read_volume_table()
823 return -EINVAL; in ubi_read_volume_table()
826 if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) { in ubi_read_volume_table()
827 /* This must not happen with proper UBI images */ in ubi_read_volume_table()
828 ubi_err(ubi, "too many LEBs (%d) in layout volume", in ubi_read_volume_table()
829 av->leb_count); in ubi_read_volume_table()
830 return -EINVAL; in ubi_read_volume_table()
833 ubi->vtbl = process_lvol(ubi, ai, av); in ubi_read_volume_table()
834 if (IS_ERR(ubi->vtbl)) in ubi_read_volume_table()
835 return PTR_ERR(ubi->vtbl); in ubi_read_volume_table()
838 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count; in ubi_read_volume_table()
841 * The layout volume is OK, initialize the corresponding in-RAM data in ubi_read_volume_table()
844 err = init_volumes(ubi, ai, ubi->vtbl); in ubi_read_volume_table()
850 * information stored in the volume table. in ubi_read_volume_table()
852 err = check_attaching_info(ubi, ai); in ubi_read_volume_table()
859 vfree(ubi->vtbl); in ubi_read_volume_table()
860 ubi_free_all_volumes(ubi); in ubi_read_volume_table()
865 * self_vtbl_check - check volume table.
866 * @ubi: UBI device description object
868 static void self_vtbl_check(const struct ubi_device *ubi) in self_vtbl_check() argument
870 if (!ubi_dbg_chk_gen(ubi)) in self_vtbl_check()
873 if (vtbl_check(ubi, ubi->vtbl)) { in self_vtbl_check()
874 ubi_err(ubi, "self-check failed"); in self_vtbl_check()