Lines Matching +full:ubi +full:- +full:volume +full:-

1 /* SPDX-License-Identifier: GPL-2.0-or-later */
28 #include <linux/mtd/ubi.h>
31 #include "ubi-media.h"
33 /* Maximum number of supported UBI devices */
36 /* UBI name used for character devices, sysfs, etc */
37 #define UBI_NAME_STR "ubi"
41 /* Normal UBI messages */
43 void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
45 /* UBI warning messages */
47 void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
49 /* UBI error messages */
51 void ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
57 * This marker in the EBA table means that the LEB is um-mapped.
60 #define UBI_LEB_UNMAPPED -1
63 * In case of errors, UBI tries to repeat the operation several times before
64 * returning error. The below constant defines how many times UBI re-tries.
70 * number of (global) erase cycles PEBs are protected from the wear-leveling
75 /* The volume ID/LEB number/erase counter is unknown */
76 #define UBI_UNKNOWN -1
79 * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
82 #define UBI_DFS_DIR_NAME "ubi%d"
89 * Error codes returned by the I/O sub-system.
99 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
101 * Note, it is probably better to have bit-flip and ebadmsg as flags which can
116 * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
124 * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
138 * Return codes of the fastmap sub-system
149 * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
160 * struct ubi_wl_entry - wear-leveling entry.
161 * @u.rb: link in the corresponding (free/used) RB-tree
166 * This data structure is used in the WL sub-system. Each physical eraseblock
168 * RB-trees. See WL sub-system for details.
180 * struct ubi_ltree_entry - an entry in the lock tree.
181 * @rb: links RB-tree nodes
182 * @vol_id: volume ID of the locked logical eraseblock
188 * This data structure is used in the EBA sub-system to implement per-LEB
189 * locking. When a logical eraseblock is being locked - corresponding
190 * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
191 * See EBA sub-system for details.
202 * struct ubi_rename_entry - volume re-name description data structure.
203 * @new_name_len: new volume name length
204 * @new_name: new volume name
205 * @remove: if not zero, this volume should be removed, not re-named
206 * @desc: descriptor of the volume
207 * @list: links re-name entries into a list
209 * This data structure is utilized in the multiple volume re-name code. Namely,
210 * UBI first creates a list of &struct ubi_rename_entry objects from the
225 * struct ubi_fastmap_layout - in-memory fastmap data structure.
227 * @to_be_tortured: if non-zero tortured this PEB
230 * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
241 * struct ubi_fm_pool - in-memory fastmap pool
260 * struct ubi_eba_leb_desc - EBA logical eraseblock descriptor
265 * UBI implementation.
275 * struct ubi_volume - UBI volume description data structure.
278 * @ubi: reference to the UBI device description object
279 * @vol_id: volume ID
280 * @ref_count: volume reference count
281 * @readers: number of users holding this volume in read-only mode
282 * @writers: number of users holding this volume in read-write mode
283 * @exclusive: whether somebody holds this volume in exclusive mode
284 * @metaonly: whether somebody is altering only meta data of this volume
286 * @reserved_pebs: how many physical eraseblocks are reserved for this volume
287 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
289 * @used_ebs: how many logical eraseblocks in this volume contain data
291 * @used_bytes: how many bytes of data this volume contains
292 * @alignment: volume alignment
295 * @name_len: volume name length
296 * @name: volume name
301 * @upd_bytes: how many bytes are expected to be received for volume update or
303 * @upd_received: how many bytes were already received for volume update or
308 * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
309 * @skip_check: %1 if CRC check of this static volume should be skipped.
312 * @checked: %1 if this static volume was checked
313 * @corrupted: %1 if the volume is corrupted (static volumes only)
314 * @upd_marker: %1 if the update marker is set for this volume
315 * @updating: %1 if the volume is being updated
317 * @direct_writes: %1 if direct writes are enabled for this volume
319 * @checkmap: bitmap to remember which PEB->LEB mappings got checked,
320 * protected by UBI LEB lock tree.
322 * The @corrupted field indicates that the volume's contents is corrupted.
323 * Since UBI protects only static volumes, this field is not relevant to
324 * dynamic volumes - it is user's responsibility to assure their data
327 * The @upd_marker flag indicates that this volume is either being updated at
333 struct ubi_device *ubi; member
374 * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
375 * @vol: reference to the corresponding volume description object
385 * struct ubi_debug_info - debugging information for an UBI device.
387 * @chk_gen: if UBI general extra checks are enabled
388 * @chk_io: if UBI I/O extra checks are enabled
389 * @chk_fastmap: if UBI fastmap extra checks are enabled
391 * @emulate_bitflips: emulate bit-flips for testing purposes
398 * @dfs_dir_name: name of debugfs directory containing files of this UBI device
399 * @dfs_dir: direntry object of the UBI device debugfs directory
400 * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
401 * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
402 * @dfs_chk_fastmap: debugfs knob to enable UBI fastmap extra checks
404 * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
438 * struct ubi_device - UBI device description structure
439 * @dev: UBI device object to use the Linux device model
441 * @ubi_num: UBI device number
442 * @ubi_name: UBI device name
443 * @vol_count: number of volumes in this UBI device
444 * @volumes: volumes of this UBI device
447 * @vol->readers, @vol->writers, @vol->exclusive,
448 * @vol->metaonly, @vol->ref_count, @vol->mapping and
449 * @vol->eba_tbl.
450 * @ref_count: count of references on the UBI device
459 * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
460 * of UBI initialization
461 * @vtbl_slots: how many slots are available in the volume table
462 * @vtbl_size: size of the volume table in bytes
463 * @vtbl: in-RAM volume table copy
464 * @device_mutex: protects on-flash volume table and serializes volume
465 * creation, deletion, update, re-size, re-name and set
476 * @fm_disabled: non-zero if fastmap is disabled (default)
477 * @fm: in-memory data structure of the currently used fastmap
478 * @fm_pool: in-memory data structure of the fastmap pool
479 * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
480 * sub-system
487 * @fm_work_scheduled: non-zero if fastmap work was scheduled
488 * @fast_attach: non-zero if UBI was attached by fastmap
493 * @used: RB-tree of used physical eraseblocks
494 * @erroneous: RB-tree of erroneous used physical eraseblocks
495 * @free: RB-tree of free physical eraseblocks
497 * @scrub: RB-tree of physical eraseblocks which need scrubbing
499 * protected from the wear-leveling worker)
508 * @wl_scheduled: non-zero if the wear-leveling was scheduled
527 * used by UBI)
532 * @ro_mode: if the UBI device is in read-only mode
538 * @vid_hdr_offset: starting offset of the volume identifier header (might be
542 * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
544 * @nor_flash: non-zero if working on top of NOR flash
551 * @ckvol_mutex: serializes static volume checking when opening
552 * @wl_reboot_notifier: close all wear-leveling work before reboot
554 * @dbg: debugging information for this UBI device
581 /* Note, mean_ec is not updated run-time - should be fixed */
584 /* EBA sub-system's stuff */
606 /* Wear-leveling sub-system's stuff */
628 /* I/O sub-system's stuff */
661 * struct ubi_ainf_peb - attach information about a physical eraseblock.
664 * @vol_id: ID of the volume this LEB belongs to
669 * @u: unions RB-tree or @list links
670 * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
675 * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
692 * struct ubi_ainf_volume - attaching information about a volume.
693 * @vol_id: volume ID
694 * @highest_lnum: highest logical eraseblock number in this volume
695 * @leb_count: number of logical eraseblocks in this volume
696 * @vol_type: volume type
697 * @used_ebs: number of used logical eraseblocks in this volume (only for
700 * volume (always equivalent to the usable logical eraseblock
702 * @data_pad: how many bytes at the end of logical eraseblocks of this volume
703 * are not used (due to volume alignment)
704 * @compat: compatibility flags of this volume
705 * @rb: link in the volume RB-tree
706 * @root: root of the RB-tree containing all the eraseblock belonging to this
707 * volume (&struct ubi_ainf_peb objects)
709 * One object of this type is allocated for each volume when attaching an MTD
726 * struct ubi_attach_info - MTD device attaching information.
727 * @volumes: root of the volume RB-tree
731 * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
732 * those belonging to "preserve"-compatible internal volumes)
743 * @highest_vol_id: highest volume ID
758 * be used by other UBI sub-systems to build final UBI data structures, further
759 * error-recovery and so on.
789 * struct ubi_work - UBI work description data structure.
793 * @vol_id: the volume ID on which this erasure is being performed
799 * WL sub-system is shutting down.
805 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
827 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
833 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
835 int ubi_attach(struct ubi_device *ubi, int force_scan);
838 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
840 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
842 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
845 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
848 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
849 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
850 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
853 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
855 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
857 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
859 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
863 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
865 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
866 void ubi_update_reserved(struct ubi_device *ubi);
867 void ubi_calculate_reserved(struct ubi_device *ubi);
872 return lnum >= 0 && lnum < vol->reserved_pebs; in ubi_leb_valid()
885 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
887 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
889 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
892 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
894 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
896 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
898 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
900 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
901 unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
902 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
906 int ubi_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture);
907 int ubi_wl_get_peb(struct ubi_device *ubi);
908 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
910 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
911 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
912 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
913 void ubi_wl_close(struct ubi_device *ubi);
915 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
916 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
919 void ubi_refill_pools_and_lock(struct ubi_device *ubi);
920 int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
921 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
924 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
926 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
928 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
929 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
930 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
931 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
933 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
935 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
937 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
946 void ubi_put_device(struct ubi_device *ubi);
949 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
951 int ubi_notify_all(struct ubi_device *ubi, int ntype,
954 void ubi_free_all_volumes(struct ubi_device *ubi);
955 void ubi_free_internal_volumes(struct ubi_device *ubi);
958 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
959 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
963 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
968 size_t ubi_calc_fm_size(struct ubi_device *ubi);
969 int ubi_update_fastmap(struct ubi_device *ubi);
970 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
975 static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; } in ubi_update_fastmap() argument
991 return -ENOSYS; in ubiblock_create()
995 return -ENOSYS; in ubiblock_remove()
1000 * ubi_for_each_free_peb - walk the UBI free RB tree.
1001 * @ubi: UBI device description object
1003 * @pos: a pointer to RB-tree entry type to use as a loop counter
1005 #define ubi_for_each_free_peb(ubi, e, tmp_rb) \ argument
1006 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
1009 * ubi_for_each_used_peb - walk the UBI used RB tree.
1010 * @ubi: UBI device description object
1012 * @pos: a pointer to RB-tree entry type to use as a loop counter
1014 #define ubi_for_each_used_peb(ubi, e, tmp_rb) \ argument
1015 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
1018 * ubi_for_each_scub_peb - walk the UBI scub RB tree.
1019 * @ubi: UBI device description object
1021 * @pos: a pointer to RB-tree entry type to use as a loop counter
1023 #define ubi_for_each_scrub_peb(ubi, e, tmp_rb) \ argument
1024 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
1027 * ubi_for_each_protected_peb - walk the UBI protection queue.
1028 * @ubi: UBI device description object
1032 #define ubi_for_each_protected_peb(ubi, i, e) \ argument
1034 list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
1037 * ubi_rb_for_each_entry - walk an RB-tree.
1039 * @pos: a pointer to RB-tree entry type to use as a loop counter
1040 * @root: RB-tree's root
1041 * @member: the name of the 'struct rb_node' within the RB-tree entry
1051 * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
1053 * @av: volume attaching information
1061 rb_erase(&aeb->u.rb, &av->root); in ubi_move_aeb_to_list()
1062 list_add_tail(&aeb->u.list, list); in ubi_move_aeb_to_list()
1066 * ubi_init_vid_buf - Initialize a VID buffer
1067 * @ubi: the UBI device
1071 static inline void ubi_init_vid_buf(const struct ubi_device *ubi, in ubi_init_vid_buf() argument
1076 memset(buf, 0, ubi->vid_hdr_alsize); in ubi_init_vid_buf()
1078 vidb->buffer = buf; in ubi_init_vid_buf()
1079 vidb->hdr = buf + ubi->vid_hdr_shift; in ubi_init_vid_buf()
1083 * ubi_init_vid_buf - Allocate a VID buffer
1084 * @ubi: the UBI device
1088 ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags) in ubi_alloc_vid_buf() argument
1097 buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags); in ubi_alloc_vid_buf()
1103 ubi_init_vid_buf(ubi, vidb, buf); in ubi_alloc_vid_buf()
1109 * ubi_free_vid_buf - Free a VID buffer
1117 kfree(vidb->buffer); in ubi_free_vid_buf()
1122 * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
1127 return vidb->hdr; in ubi_get_vid_hdr()
1131 * ubi_ro_mode - switch to read-only mode.
1132 * @ubi: UBI device description object
1134 static inline void ubi_ro_mode(struct ubi_device *ubi) in ubi_ro_mode() argument
1136 if (!ubi->ro_mode) { in ubi_ro_mode()
1137 ubi->ro_mode = 1; in ubi_ro_mode()
1138 ubi_warn(ubi, "switch to read-only mode"); in ubi_ro_mode()
1148 static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf, in ubi_io_read_data() argument
1152 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len); in ubi_io_read_data()
1160 static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf, in ubi_io_write_data() argument
1165 if (ubi_dbg_power_cut(ubi, MASK_POWER_CUT_DATA)) { in ubi_io_write_data()
1166 ubi_warn(ubi, "XXXXX emulating a power cut when writing data XXXXX"); in ubi_io_write_data()
1167 ubi_ro_mode(ubi); in ubi_io_write_data()
1168 return -EROFS; in ubi_io_write_data()
1170 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len); in ubi_io_write_data()
1174 * vol_id2idx - get table index by volume ID.
1175 * @ubi: UBI device description object
1176 * @vol_id: volume ID
1178 static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id) in vol_id2idx() argument
1181 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots; in vol_id2idx()
1187 * idx2vol_id - get volume ID by table index.
1188 * @ubi: UBI device description object
1191 static inline int idx2vol_id(const struct ubi_device *ubi, int idx) in idx2vol_id() argument
1193 if (idx >= ubi->vtbl_slots) in idx2vol_id()
1194 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START; in idx2vol_id()
1200 * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
1201 * @vol_id: volume ID
1215 * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
1216 * @ubi: UBI device description object
1222 static inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi, in ubi_find_fm_block() argument
1227 if (ubi->fm) { in ubi_find_fm_block()
1228 for (i = 0; i < ubi->fm->used_blocks; i++) { in ubi_find_fm_block()
1229 if (ubi->fm->e[i]->pnum == pnum) in ubi_find_fm_block()
1230 return ubi->fm->e[i]; in ubi_find_fm_block()