Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (c) International Business Machines Corp., 2006
0004  *
0005  * Author: Artem Bityutskiy (Битюцкий Артём)
0006  */
0007 
0008 #ifndef __LINUX_UBI_H__
0009 #define __LINUX_UBI_H__
0010 
0011 #include <linux/ioctl.h>
0012 #include <linux/types.h>
0013 #include <linux/scatterlist.h>
0014 #include <mtd/ubi-user.h>
0015 
0016 /* All voumes/LEBs */
0017 #define UBI_ALL -1
0018 
0019 /*
0020  * Maximum number of scatter gather list entries,
0021  * we use only 64 to have a lower memory foot print.
0022  */
0023 #define UBI_MAX_SG_COUNT 64
0024 
0025 /*
0026  * enum ubi_open_mode - UBI volume open mode constants.
0027  *
0028  * UBI_READONLY: read-only mode
0029  * UBI_READWRITE: read-write mode
0030  * UBI_EXCLUSIVE: exclusive mode
0031  * UBI_METAONLY: modify only the volume meta-data,
0032  *  i.e. the data stored in the volume table, but not in any of volume LEBs.
0033  */
0034 enum {
0035     UBI_READONLY = 1,
0036     UBI_READWRITE,
0037     UBI_EXCLUSIVE,
0038     UBI_METAONLY
0039 };
0040 
0041 /**
0042  * struct ubi_volume_info - UBI volume description data structure.
0043  * @vol_id: volume ID
0044  * @ubi_num: UBI device number this volume belongs to
0045  * @size: how many physical eraseblocks are reserved for this volume
0046  * @used_bytes: how many bytes of data this volume contains
0047  * @used_ebs: how many physical eraseblocks of this volume actually contain any
0048  *            data
0049  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
0050  * @corrupted: non-zero if the volume is corrupted (static volumes only)
0051  * @upd_marker: non-zero if the volume has update marker set
0052  * @alignment: volume alignment
0053  * @usable_leb_size: how many bytes are available in logical eraseblocks of
0054  *                   this volume
0055  * @name_len: volume name length
0056  * @name: volume name
0057  * @cdev: UBI volume character device major and minor numbers
0058  *
0059  * The @corrupted flag is only relevant to static volumes and is always zero
0060  * for dynamic ones. This is because UBI does not care about dynamic volume
0061  * data protection and only cares about protecting static volume data.
0062  *
0063  * The @upd_marker flag is set if the volume update operation was interrupted.
0064  * Before touching the volume data during the update operation, UBI first sets
0065  * the update marker flag for this volume. If the volume update operation was
0066  * further interrupted, the update marker indicates this. If the update marker
0067  * is set, the contents of the volume is certainly damaged and a new volume
0068  * update operation has to be started.
0069  *
0070  * To put it differently, @corrupted and @upd_marker fields have different
0071  * semantics:
0072  *     o the @corrupted flag means that this static volume is corrupted for some
0073  *       reasons, but not because an interrupted volume update
0074  *     o the @upd_marker field means that the volume is damaged because of an
0075  *       interrupted update operation.
0076  *
0077  * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
0078  *
0079  * The @used_bytes and @used_ebs fields are only really needed for static
0080  * volumes and contain the number of bytes stored in this static volume and how
0081  * many eraseblock this data occupies. In case of dynamic volumes, the
0082  * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
0083  * field is equivalent to @size.
0084  *
0085  * In general, logical eraseblock size is a property of the UBI device, not
0086  * of the UBI volume. Indeed, the logical eraseblock size depends on the
0087  * physical eraseblock size and on how much bytes UBI headers consume. But
0088  * because of the volume alignment (@alignment), the usable size of logical
0089  * eraseblocks if a volume may be less. The following equation is true:
0090  *  @usable_leb_size = LEB size - (LEB size mod @alignment),
0091  * where LEB size is the logical eraseblock size defined by the UBI device.
0092  *
0093  * The alignment is multiple to the minimal flash input/output unit size or %1
0094  * if all the available space is used.
0095  *
0096  * To put this differently, alignment may be considered is a way to change
0097  * volume logical eraseblock sizes.
0098  */
0099 struct ubi_volume_info {
0100     int ubi_num;
0101     int vol_id;
0102     int size;
0103     long long used_bytes;
0104     int used_ebs;
0105     int vol_type;
0106     int corrupted;
0107     int upd_marker;
0108     int alignment;
0109     int usable_leb_size;
0110     int name_len;
0111     const char *name;
0112     dev_t cdev;
0113 };
0114 
0115 /**
0116  * struct ubi_sgl - UBI scatter gather list data structure.
0117  * @list_pos: current position in @sg[]
0118  * @page_pos: current position in @sg[@list_pos]
0119  * @sg: the scatter gather list itself
0120  *
0121  * ubi_sgl is a wrapper around a scatter list which keeps track of the
0122  * current position in the list and the current list item such that
0123  * it can be used across multiple ubi_leb_read_sg() calls.
0124  */
0125 struct ubi_sgl {
0126     int list_pos;
0127     int page_pos;
0128     struct scatterlist sg[UBI_MAX_SG_COUNT];
0129 };
0130 
0131 /**
0132  * ubi_sgl_init - initialize an UBI scatter gather list data structure.
0133  * @usgl: the UBI scatter gather struct itself
0134  *
0135  * Please note that you still have to use sg_init_table() or any adequate
0136  * function to initialize the unterlaying struct scatterlist.
0137  */
0138 static inline void ubi_sgl_init(struct ubi_sgl *usgl)
0139 {
0140     usgl->list_pos = 0;
0141     usgl->page_pos = 0;
0142 }
0143 
0144 /**
0145  * struct ubi_device_info - UBI device description data structure.
0146  * @ubi_num: ubi device number
0147  * @leb_size: logical eraseblock size on this UBI device
0148  * @leb_start: starting offset of logical eraseblocks within physical
0149  *             eraseblocks
0150  * @min_io_size: minimal I/O unit size
0151  * @max_write_size: maximum amount of bytes the underlying flash can write at a
0152  *                  time (MTD write buffer size)
0153  * @ro_mode: if this device is in read-only mode
0154  * @cdev: UBI character device major and minor numbers
0155  *
0156  * Note, @leb_size is the logical eraseblock size offered by the UBI device.
0157  * Volumes of this UBI device may have smaller logical eraseblock size if their
0158  * alignment is not equivalent to %1.
0159  *
0160  * The @max_write_size field describes flash write maximum write unit. For
0161  * example, NOR flash allows for changing individual bytes, so @min_io_size is
0162  * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
0163  * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
0164  * writing large chunks of data, they write 64-bytes at a time. Obviously, this
0165  * improves write throughput.
0166  *
0167  * Also, the MTD device may have N interleaved (striped) flash chips
0168  * underneath, in which case @min_io_size can be physical min. I/O size of
0169  * single flash chip, while @max_write_size can be N * @min_io_size.
0170  *
0171  * The @max_write_size field is always greater or equivalent to @min_io_size.
0172  * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
0173  * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
0174  * page size.
0175  */
0176 struct ubi_device_info {
0177     int ubi_num;
0178     int leb_size;
0179     int leb_start;
0180     int min_io_size;
0181     int max_write_size;
0182     int ro_mode;
0183     dev_t cdev;
0184 };
0185 
0186 /*
0187  * Volume notification types.
0188  * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
0189  *                    volume was created)
0190  * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
0191  *          or a volume was removed)
0192  * @UBI_VOLUME_RESIZED: a volume has been re-sized
0193  * @UBI_VOLUME_RENAMED: a volume has been re-named
0194  * @UBI_VOLUME_UPDATED: data has been written to a volume
0195  *
0196  * These constants define which type of event has happened when a volume
0197  * notification function is invoked.
0198  */
0199 enum {
0200     UBI_VOLUME_ADDED,
0201     UBI_VOLUME_REMOVED,
0202     UBI_VOLUME_RESIZED,
0203     UBI_VOLUME_RENAMED,
0204     UBI_VOLUME_UPDATED,
0205 };
0206 
0207 /*
0208  * struct ubi_notification - UBI notification description structure.
0209  * @di: UBI device description object
0210  * @vi: UBI volume description object
0211  *
0212  * UBI notifiers are called with a pointer to an object of this type. The
0213  * object describes the notification. Namely, it provides a description of the
0214  * UBI device and UBI volume the notification informs about.
0215  */
0216 struct ubi_notification {
0217     struct ubi_device_info di;
0218     struct ubi_volume_info vi;
0219 };
0220 
0221 /* UBI descriptor given to users when they open UBI volumes */
0222 struct ubi_volume_desc;
0223 
0224 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
0225 void ubi_get_volume_info(struct ubi_volume_desc *desc,
0226              struct ubi_volume_info *vi);
0227 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
0228 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
0229                        int mode);
0230 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
0231 
0232 int ubi_register_volume_notifier(struct notifier_block *nb,
0233                  int ignore_existing);
0234 int ubi_unregister_volume_notifier(struct notifier_block *nb);
0235 
0236 void ubi_close_volume(struct ubi_volume_desc *desc);
0237 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
0238          int len, int check);
0239 int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
0240            int offset, int len, int check);
0241 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
0242           int offset, int len);
0243 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
0244            int len);
0245 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
0246 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
0247 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
0248 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
0249 int ubi_sync(int ubi_num);
0250 int ubi_flush(int ubi_num, int vol_id, int lnum);
0251 
0252 /*
0253  * This function is the same as the 'ubi_leb_read()' function, but it does not
0254  * provide the checking capability.
0255  */
0256 static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
0257                int offset, int len)
0258 {
0259     return ubi_leb_read(desc, lnum, buf, offset, len, 0);
0260 }
0261 
0262 /*
0263  * This function is the same as the 'ubi_leb_read_sg()' function, but it does
0264  * not provide the checking capability.
0265  */
0266 static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,
0267                   struct ubi_sgl *sgl, int offset, int len)
0268 {
0269     return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);
0270 }
0271 #endif /* !__LINUX_UBI_H__ */