275 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * Copyright (c) International Business Machines Corp., 20064 *5 * Author: Artem Bityutskiy (Битюцкий Артём)6 */7 8#ifndef __LINUX_UBI_H__9#define __LINUX_UBI_H__10 11#include <linux/ioctl.h>12#include <linux/types.h>13#include <linux/scatterlist.h>14#include <mtd/ubi-user.h>15 16/* All voumes/LEBs */17#define UBI_ALL -118 19/*20 * Maximum number of scatter gather list entries,21 * we use only 64 to have a lower memory foot print.22 */23#define UBI_MAX_SG_COUNT 6424 25/*26 * enum ubi_open_mode - UBI volume open mode constants.27 *28 * UBI_READONLY: read-only mode29 * UBI_READWRITE: read-write mode30 * UBI_EXCLUSIVE: exclusive mode31 * UBI_METAONLY: modify only the volume meta-data,32 * i.e. the data stored in the volume table, but not in any of volume LEBs.33 */34enum {35 UBI_READONLY = 1,36 UBI_READWRITE,37 UBI_EXCLUSIVE,38 UBI_METAONLY39};40 41/**42 * struct ubi_volume_info - UBI volume description data structure.43 * @vol_id: volume ID44 * @ubi_num: UBI device number this volume belongs to45 * @size: how many physical eraseblocks are reserved for this volume46 * @used_bytes: how many bytes of data this volume contains47 * @used_ebs: how many physical eraseblocks of this volume actually contain any48 * data49 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)50 * @corrupted: non-zero if the volume is corrupted (static volumes only)51 * @upd_marker: non-zero if the volume has update marker set52 * @alignment: volume alignment53 * @usable_leb_size: how many bytes are available in logical eraseblocks of54 * this volume55 * @name_len: volume name length56 * @name: volume name57 * @cdev: UBI volume character device major and minor numbers58 *59 * The @corrupted flag is only relevant to static volumes and is always zero60 * for dynamic ones. This is because UBI does not care about dynamic volume61 * data protection and only cares about protecting static volume data.62 *63 * The @upd_marker flag is set if the volume update operation was interrupted.64 * Before touching the volume data during the update operation, UBI first sets65 * the update marker flag for this volume. If the volume update operation was66 * further interrupted, the update marker indicates this. If the update marker67 * is set, the contents of the volume is certainly damaged and a new volume68 * update operation has to be started.69 *70 * To put it differently, @corrupted and @upd_marker fields have different71 * semantics:72 * o the @corrupted flag means that this static volume is corrupted for some73 * reasons, but not because an interrupted volume update74 * o the @upd_marker field means that the volume is damaged because of an75 * interrupted update operation.76 *77 * I.e., the @corrupted flag is never set if the @upd_marker flag is set.78 *79 * The @used_bytes and @used_ebs fields are only really needed for static80 * volumes and contain the number of bytes stored in this static volume and how81 * many eraseblock this data occupies. In case of dynamic volumes, the82 * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs83 * field is equivalent to @size.84 *85 * In general, logical eraseblock size is a property of the UBI device, not86 * of the UBI volume. Indeed, the logical eraseblock size depends on the87 * physical eraseblock size and on how much bytes UBI headers consume. But88 * because of the volume alignment (@alignment), the usable size of logical89 * eraseblocks if a volume may be less. The following equation is true:90 * @usable_leb_size = LEB size - (LEB size mod @alignment),91 * where LEB size is the logical eraseblock size defined by the UBI device.92 *93 * The alignment is multiple to the minimal flash input/output unit size or %194 * if all the available space is used.95 *96 * To put this differently, alignment may be considered is a way to change97 * volume logical eraseblock sizes.98 */99struct ubi_volume_info {100 int ubi_num;101 int vol_id;102 int size;103 long long used_bytes;104 int used_ebs;105 int vol_type;106 int corrupted;107 int upd_marker;108 int alignment;109 int usable_leb_size;110 int name_len;111 const char *name;112 dev_t cdev;113 struct device *dev;114};115 116/**117 * struct ubi_sgl - UBI scatter gather list data structure.118 * @list_pos: current position in @sg[]119 * @page_pos: current position in @sg[@list_pos]120 * @sg: the scatter gather list itself121 *122 * ubi_sgl is a wrapper around a scatter list which keeps track of the123 * current position in the list and the current list item such that124 * it can be used across multiple ubi_leb_read_sg() calls.125 */126struct ubi_sgl {127 int list_pos;128 int page_pos;129 struct scatterlist sg[UBI_MAX_SG_COUNT];130};131 132/**133 * ubi_sgl_init - initialize an UBI scatter gather list data structure.134 * @usgl: the UBI scatter gather struct itself135 *136 * Please note that you still have to use sg_init_table() or any adequate137 * function to initialize the unterlaying struct scatterlist.138 */139static inline void ubi_sgl_init(struct ubi_sgl *usgl)140{141 usgl->list_pos = 0;142 usgl->page_pos = 0;143}144 145/**146 * struct ubi_device_info - UBI device description data structure.147 * @ubi_num: ubi device number148 * @leb_size: logical eraseblock size on this UBI device149 * @leb_start: starting offset of logical eraseblocks within physical150 * eraseblocks151 * @min_io_size: minimal I/O unit size152 * @max_write_size: maximum amount of bytes the underlying flash can write at a153 * time (MTD write buffer size)154 * @ro_mode: if this device is in read-only mode155 * @cdev: UBI character device major and minor numbers156 *157 * Note, @leb_size is the logical eraseblock size offered by the UBI device.158 * Volumes of this UBI device may have smaller logical eraseblock size if their159 * alignment is not equivalent to %1.160 *161 * The @max_write_size field describes flash write maximum write unit. For162 * example, NOR flash allows for changing individual bytes, so @min_io_size is163 * %1. However, it does not mean than NOR flash has to write data byte-by-byte.164 * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when165 * writing large chunks of data, they write 64-bytes at a time. Obviously, this166 * improves write throughput.167 *168 * Also, the MTD device may have N interleaved (striped) flash chips169 * underneath, in which case @min_io_size can be physical min. I/O size of170 * single flash chip, while @max_write_size can be N * @min_io_size.171 *172 * The @max_write_size field is always greater or equivalent to @min_io_size.173 * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In174 * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND175 * page size.176 */177struct ubi_device_info {178 int ubi_num;179 int leb_size;180 int leb_start;181 int min_io_size;182 int max_write_size;183 int ro_mode;184 dev_t cdev;185};186 187/*188 * Volume notification types.189 * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a190 * volume was created)191 * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached192 * or a volume was removed)193 * @UBI_VOLUME_RESIZED: a volume has been re-sized194 * @UBI_VOLUME_RENAMED: a volume has been re-named195 * @UBI_VOLUME_SHUTDOWN: a volume is going to removed, shutdown users196 * @UBI_VOLUME_UPDATED: data has been written to a volume197 *198 * These constants define which type of event has happened when a volume199 * notification function is invoked.200 */201enum {202 UBI_VOLUME_ADDED,203 UBI_VOLUME_REMOVED,204 UBI_VOLUME_RESIZED,205 UBI_VOLUME_RENAMED,206 UBI_VOLUME_SHUTDOWN,207 UBI_VOLUME_UPDATED,208};209 210/*211 * struct ubi_notification - UBI notification description structure.212 * @di: UBI device description object213 * @vi: UBI volume description object214 *215 * UBI notifiers are called with a pointer to an object of this type. The216 * object describes the notification. Namely, it provides a description of the217 * UBI device and UBI volume the notification informs about.218 */219struct ubi_notification {220 struct ubi_device_info di;221 struct ubi_volume_info vi;222};223 224/* UBI descriptor given to users when they open UBI volumes */225struct ubi_volume_desc;226 227int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);228void ubi_get_volume_info(struct ubi_volume_desc *desc,229 struct ubi_volume_info *vi);230struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);231struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,232 int mode);233struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);234 235int ubi_register_volume_notifier(struct notifier_block *nb,236 int ignore_existing);237int ubi_unregister_volume_notifier(struct notifier_block *nb);238 239void ubi_close_volume(struct ubi_volume_desc *desc);240int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,241 int len, int check);242int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,243 int offset, int len, int check);244int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,245 int offset, int len);246int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,247 int len);248int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);249int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);250int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);251int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);252int ubi_sync(int ubi_num);253int ubi_flush(int ubi_num, int vol_id, int lnum);254 255/*256 * This function is the same as the 'ubi_leb_read()' function, but it does not257 * provide the checking capability.258 */259static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,260 int offset, int len)261{262 return ubi_leb_read(desc, lnum, buf, offset, len, 0);263}264 265/*266 * This function is the same as the 'ubi_leb_read_sg()' function, but it does267 * not provide the checking capability.268 */269static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,270 struct ubi_sgl *sgl, int offset, int len)271{272 return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);273}274#endif /* !__LINUX_UBI_H__ */275