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
553 * @dbg: debugging information for this UBI device
580 /* Note, mean_ec is not updated run-time - should be fixed */
583 /* EBA sub-system's stuff */
605 /* Wear-leveling sub-system's stuff */
627 /* I/O sub-system's stuff */
659 * struct ubi_ainf_peb - attach information about a physical eraseblock.
662 * @vol_id: ID of the volume this LEB belongs to
667 * @u: unions RB-tree or @list links
668 * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
673 * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
690 * struct ubi_ainf_volume - attaching information about a volume.
691 * @vol_id: volume ID
692 * @highest_lnum: highest logical eraseblock number in this volume
693 * @leb_count: number of logical eraseblocks in this volume
694 * @vol_type: volume type
695 * @used_ebs: number of used logical eraseblocks in this volume (only for
698 * volume (always equivalent to the usable logical eraseblock
700 * @data_pad: how many bytes at the end of logical eraseblocks of this volume
701 * are not used (due to volume alignment)
702 * @compat: compatibility flags of this volume
703 * @rb: link in the volume RB-tree
704 * @root: root of the RB-tree containing all the eraseblock belonging to this
705 * volume (&struct ubi_ainf_peb objects)
707 * One object of this type is allocated for each volume when attaching an MTD
724 * struct ubi_attach_info - MTD device attaching information.
725 * @volumes: root of the volume RB-tree
729 * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
730 * those belonging to "preserve"-compatible internal volumes)
741 * @highest_vol_id: highest volume ID
756 * be used by other UBI sub-systems to build final UBI data structures, further
757 * error-recovery and so on.
787 * struct ubi_work - UBI work description data structure.
791 * @vol_id: the volume ID on which this erasure is being performed
797 * WL sub-system is shutting down.
803 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
825 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
831 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
833 int ubi_attach(struct ubi_device *ubi, int force_scan);
837 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
839 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
841 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
844 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
847 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
848 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
849 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
852 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
854 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
856 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
858 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
862 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
864 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
865 void ubi_update_reserved(struct ubi_device *ubi);
866 void ubi_calculate_reserved(struct ubi_device *ubi);
871 return lnum >= 0 && lnum < vol->reserved_pebs; in ubi_leb_valid()
884 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
886 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
888 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
891 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
893 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
895 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
897 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
899 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
900 unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
901 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
905 int ubi_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture);
906 int ubi_wl_get_peb(struct ubi_device *ubi);
907 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
909 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
910 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
911 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
912 void ubi_wl_close(struct ubi_device *ubi);
914 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
915 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
918 void ubi_refill_pools_and_lock(struct ubi_device *ubi);
919 int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
920 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
923 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
925 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
927 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
928 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
929 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
930 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
932 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
934 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
936 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
945 void ubi_put_device(struct ubi_device *ubi);
948 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
950 int ubi_notify_all(struct ubi_device *ubi, int ntype,
953 void ubi_free_all_volumes(struct ubi_device *ubi);
954 void ubi_free_internal_volumes(struct ubi_device *ubi);
957 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
958 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
962 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
967 size_t ubi_calc_fm_size(struct ubi_device *ubi);
968 int ubi_update_fastmap(struct ubi_device *ubi);
969 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
974 static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; } in ubi_update_fastmap() argument
990 return -ENOSYS; in ubiblock_create()
994 return -ENOSYS; in ubiblock_remove()
999 * ubi_for_each_free_peb - walk the UBI free RB tree.
1000 * @ubi: UBI device description object
1002 * @pos: a pointer to RB-tree entry type to use as a loop counter
1004 #define ubi_for_each_free_peb(ubi, e, tmp_rb) \ argument
1005 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
1008 * ubi_for_each_used_peb - walk the UBI used RB tree.
1009 * @ubi: UBI device description object
1011 * @pos: a pointer to RB-tree entry type to use as a loop counter
1013 #define ubi_for_each_used_peb(ubi, e, tmp_rb) \ argument
1014 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
1017 * ubi_for_each_scub_peb - walk the UBI scub RB tree.
1018 * @ubi: UBI device description object
1020 * @pos: a pointer to RB-tree entry type to use as a loop counter
1022 #define ubi_for_each_scrub_peb(ubi, e, tmp_rb) \ argument
1023 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
1026 * ubi_for_each_protected_peb - walk the UBI protection queue.
1027 * @ubi: UBI device description object
1031 #define ubi_for_each_protected_peb(ubi, i, e) \ argument
1033 list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
1036 * ubi_rb_for_each_entry - walk an RB-tree.
1038 * @pos: a pointer to RB-tree entry type to use as a loop counter
1039 * @root: RB-tree's root
1040 * @member: the name of the 'struct rb_node' within the RB-tree entry
1050 * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
1052 * @av: volume attaching information
1060 rb_erase(&aeb->u.rb, &av->root); in ubi_move_aeb_to_list()
1061 list_add_tail(&aeb->u.list, list); in ubi_move_aeb_to_list()
1065 * ubi_init_vid_buf - Initialize a VID buffer
1066 * @ubi: the UBI device
1070 static inline void ubi_init_vid_buf(const struct ubi_device *ubi, in ubi_init_vid_buf() argument
1075 memset(buf, 0, ubi->vid_hdr_alsize); in ubi_init_vid_buf()
1077 vidb->buffer = buf; in ubi_init_vid_buf()
1078 vidb->hdr = buf + ubi->vid_hdr_shift; in ubi_init_vid_buf()
1082 * ubi_init_vid_buf - Allocate a VID buffer
1083 * @ubi: the UBI device
1087 ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags) in ubi_alloc_vid_buf() argument
1096 buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags); in ubi_alloc_vid_buf()
1102 ubi_init_vid_buf(ubi, vidb, buf); in ubi_alloc_vid_buf()
1108 * ubi_free_vid_buf - Free a VID buffer
1116 kfree(vidb->buffer); in ubi_free_vid_buf()
1121 * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
1126 return vidb->hdr; in ubi_get_vid_hdr()
1130 * ubi_ro_mode - switch to read-only mode.
1131 * @ubi: UBI device description object
1133 static inline void ubi_ro_mode(struct ubi_device *ubi) in ubi_ro_mode() argument
1135 if (!ubi->ro_mode) { in ubi_ro_mode()
1136 ubi->ro_mode = 1; in ubi_ro_mode()
1137 ubi_warn(ubi, "switch to read-only mode"); in ubi_ro_mode()
1147 static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf, in ubi_io_read_data() argument
1151 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len); in ubi_io_read_data()
1159 static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf, in ubi_io_write_data() argument
1164 if (ubi_dbg_power_cut(ubi, MASK_POWER_CUT_DATA)) { in ubi_io_write_data()
1165 ubi_warn(ubi, "XXXXX emulating a power cut when writing data XXXXX"); in ubi_io_write_data()
1166 ubi_ro_mode(ubi); in ubi_io_write_data()
1167 return -EROFS; in ubi_io_write_data()
1169 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len); in ubi_io_write_data()
1173 * vol_id2idx - get table index by volume ID.
1174 * @ubi: UBI device description object
1175 * @vol_id: volume ID
1177 static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id) in vol_id2idx() argument
1180 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots; in vol_id2idx()
1186 * idx2vol_id - get volume ID by table index.
1187 * @ubi: UBI device description object
1190 static inline int idx2vol_id(const struct ubi_device *ubi, int idx) in idx2vol_id() argument
1192 if (idx >= ubi->vtbl_slots) in idx2vol_id()
1193 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START; in idx2vol_id()
1199 * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
1200 * @vol_id: volume ID
1214 * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
1215 * @ubi: UBI device description object
1221 static inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi, in ubi_find_fm_block() argument
1226 if (ubi->fm) { in ubi_find_fm_block()
1227 for (i = 0; i < ubi->fm->used_blocks; i++) { in ubi_find_fm_block()
1228 if (ubi->fm->e[i]->pnum == pnum) in ubi_find_fm_block()
1229 return ubi->fm->e[i]; in ubi_find_fm_block()