ubi.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Author: Artem Bityutskiy (Битюцкий Артём)
  7. */
  8. #ifndef __LINUX_UBI_H__
  9. #define __LINUX_UBI_H__
  10. #include <linux/types.h>
  11. #ifndef __UBOOT__
  12. #include <linux/ioctl.h>
  13. #include <linux/scatterlist.h>
  14. #include <mtd/ubi-user.h>
  15. #endif
  16. /* All voumes/LEBs */
  17. #define UBI_ALL -1
  18. /*
  19. * Maximum number of scatter gather list entries,
  20. * we use only 64 to have a lower memory foot print.
  21. */
  22. #define UBI_MAX_SG_COUNT 64
  23. /*
  24. * enum ubi_open_mode - UBI volume open mode constants.
  25. *
  26. * UBI_READONLY: read-only mode
  27. * UBI_READWRITE: read-write mode
  28. * UBI_EXCLUSIVE: exclusive mode
  29. * UBI_METAONLY: modify only the volume meta-data,
  30. * i.e. the data stored in the volume table, but not in any of volume LEBs.
  31. */
  32. enum {
  33. UBI_READONLY = 1,
  34. UBI_READWRITE,
  35. UBI_EXCLUSIVE,
  36. UBI_METAONLY
  37. };
  38. /**
  39. * struct ubi_volume_info - UBI volume description data structure.
  40. * @vol_id: volume ID
  41. * @ubi_num: UBI device number this volume belongs to
  42. * @size: how many physical eraseblocks are reserved for this volume
  43. * @used_bytes: how many bytes of data this volume contains
  44. * @used_ebs: how many physical eraseblocks of this volume actually contain any
  45. * data
  46. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  47. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  48. * @upd_marker: non-zero if the volume has update marker set
  49. * @alignment: volume alignment
  50. * @usable_leb_size: how many bytes are available in logical eraseblocks of
  51. * this volume
  52. * @name_len: volume name length
  53. * @name: volume name
  54. * @cdev: UBI volume character device major and minor numbers
  55. *
  56. * The @corrupted flag is only relevant to static volumes and is always zero
  57. * for dynamic ones. This is because UBI does not care about dynamic volume
  58. * data protection and only cares about protecting static volume data.
  59. *
  60. * The @upd_marker flag is set if the volume update operation was interrupted.
  61. * Before touching the volume data during the update operation, UBI first sets
  62. * the update marker flag for this volume. If the volume update operation was
  63. * further interrupted, the update marker indicates this. If the update marker
  64. * is set, the contents of the volume is certainly damaged and a new volume
  65. * update operation has to be started.
  66. *
  67. * To put it differently, @corrupted and @upd_marker fields have different
  68. * semantics:
  69. * o the @corrupted flag means that this static volume is corrupted for some
  70. * reasons, but not because an interrupted volume update
  71. * o the @upd_marker field means that the volume is damaged because of an
  72. * interrupted update operation.
  73. *
  74. * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
  75. *
  76. * The @used_bytes and @used_ebs fields are only really needed for static
  77. * volumes and contain the number of bytes stored in this static volume and how
  78. * many eraseblock this data occupies. In case of dynamic volumes, the
  79. * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
  80. * field is equivalent to @size.
  81. *
  82. * In general, logical eraseblock size is a property of the UBI device, not
  83. * of the UBI volume. Indeed, the logical eraseblock size depends on the
  84. * physical eraseblock size and on how much bytes UBI headers consume. But
  85. * because of the volume alignment (@alignment), the usable size of logical
  86. * eraseblocks if a volume may be less. The following equation is true:
  87. * @usable_leb_size = LEB size - (LEB size mod @alignment),
  88. * where LEB size is the logical eraseblock size defined by the UBI device.
  89. *
  90. * The alignment is multiple to the minimal flash input/output unit size or %1
  91. * if all the available space is used.
  92. *
  93. * To put this differently, alignment may be considered is a way to change
  94. * volume logical eraseblock sizes.
  95. */
  96. struct ubi_volume_info {
  97. int ubi_num;
  98. int vol_id;
  99. int size;
  100. long long used_bytes;
  101. int used_ebs;
  102. int vol_type;
  103. int corrupted;
  104. int upd_marker;
  105. int alignment;
  106. int usable_leb_size;
  107. int name_len;
  108. const char *name;
  109. dev_t cdev;
  110. };
  111. /**
  112. * struct ubi_sgl - UBI scatter gather list data structure.
  113. * @list_pos: current position in @sg[]
  114. * @page_pos: current position in @sg[@list_pos]
  115. * @sg: the scatter gather list itself
  116. *
  117. * ubi_sgl is a wrapper around a scatter list which keeps track of the
  118. * current position in the list and the current list item such that
  119. * it can be used across multiple ubi_leb_read_sg() calls.
  120. */
  121. struct ubi_sgl {
  122. int list_pos;
  123. int page_pos;
  124. #ifndef __UBOOT__
  125. struct scatterlist sg[UBI_MAX_SG_COUNT];
  126. #endif
  127. };
  128. /**
  129. * ubi_sgl_init - initialize an UBI scatter gather list data structure.
  130. * @usgl: the UBI scatter gather struct itself
  131. *
  132. * Please note that you still have to use sg_init_table() or any adequate
  133. * function to initialize the unterlaying struct scatterlist.
  134. */
  135. static inline void ubi_sgl_init(struct ubi_sgl *usgl)
  136. {
  137. usgl->list_pos = 0;
  138. usgl->page_pos = 0;
  139. }
  140. /**
  141. * struct ubi_device_info - UBI device description data structure.
  142. * @ubi_num: ubi device number
  143. * @leb_size: logical eraseblock size on this UBI device
  144. * @leb_start: starting offset of logical eraseblocks within physical
  145. * eraseblocks
  146. * @min_io_size: minimal I/O unit size
  147. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  148. * time (MTD write buffer size)
  149. * @ro_mode: if this device is in read-only mode
  150. * @cdev: UBI character device major and minor numbers
  151. *
  152. * Note, @leb_size is the logical eraseblock size offered by the UBI device.
  153. * Volumes of this UBI device may have smaller logical eraseblock size if their
  154. * alignment is not equivalent to %1.
  155. *
  156. * The @max_write_size field describes flash write maximum write unit. For
  157. * example, NOR flash allows for changing individual bytes, so @min_io_size is
  158. * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
  159. * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
  160. * writing large chunks of data, they write 64-bytes at a time. Obviously, this
  161. * improves write throughput.
  162. *
  163. * Also, the MTD device may have N interleaved (striped) flash chips
  164. * underneath, in which case @min_io_size can be physical min. I/O size of
  165. * single flash chip, while @max_write_size can be N * @min_io_size.
  166. *
  167. * The @max_write_size field is always greater or equivalent to @min_io_size.
  168. * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
  169. * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
  170. * page size.
  171. */
  172. struct ubi_device_info {
  173. int ubi_num;
  174. int leb_size;
  175. int leb_start;
  176. int min_io_size;
  177. int max_write_size;
  178. int ro_mode;
  179. #ifndef __UBOOT__
  180. dev_t cdev;
  181. #endif
  182. };
  183. /*
  184. * Volume notification types.
  185. * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
  186. * volume was created)
  187. * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
  188. * or a volume was removed)
  189. * @UBI_VOLUME_RESIZED: a volume has been re-sized
  190. * @UBI_VOLUME_RENAMED: a volume has been re-named
  191. * @UBI_VOLUME_UPDATED: data has been written to a volume
  192. *
  193. * These constants define which type of event has happened when a volume
  194. * notification function is invoked.
  195. */
  196. enum {
  197. UBI_VOLUME_ADDED,
  198. UBI_VOLUME_REMOVED,
  199. UBI_VOLUME_RESIZED,
  200. UBI_VOLUME_RENAMED,
  201. UBI_VOLUME_UPDATED,
  202. };
  203. /*
  204. * struct ubi_notification - UBI notification description structure.
  205. * @di: UBI device description object
  206. * @vi: UBI volume description object
  207. *
  208. * UBI notifiers are called with a pointer to an object of this type. The
  209. * object describes the notification. Namely, it provides a description of the
  210. * UBI device and UBI volume the notification informs about.
  211. */
  212. struct ubi_notification {
  213. struct ubi_device_info di;
  214. struct ubi_volume_info vi;
  215. };
  216. /* UBI descriptor given to users when they open UBI volumes */
  217. struct ubi_volume_desc;
  218. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
  219. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  220. struct ubi_volume_info *vi);
  221. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
  222. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  223. int mode);
  224. struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
  225. #ifndef __UBOOT__
  226. typedef int (*notifier_fn_t)(void *nb,
  227. unsigned long action, void *data);
  228. struct notifier_block {
  229. notifier_fn_t notifier_call;
  230. struct notifier_block *next;
  231. void *next;
  232. int priority;
  233. };
  234. int ubi_register_volume_notifier(struct notifier_block *nb,
  235. int ignore_existing);
  236. int ubi_unregister_volume_notifier(struct notifier_block *nb);
  237. #endif
  238. void ubi_close_volume(struct ubi_volume_desc *desc);
  239. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  240. int len, int check);
  241. int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
  242. int offset, int len, int check);
  243. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  244. int offset, int len);
  245. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  246. int len);
  247. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
  248. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  249. int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
  250. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
  251. int ubi_sync(int ubi_num);
  252. int ubi_flush(int ubi_num, int vol_id, int lnum);
  253. /*
  254. * This function is the same as the 'ubi_leb_read()' function, but it does not
  255. * provide the checking capability.
  256. */
  257. static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  258. int offset, int len)
  259. {
  260. return ubi_leb_read(desc, lnum, buf, offset, len, 0);
  261. }
  262. /*
  263. * This function is the same as the 'ubi_leb_read_sg()' function, but it does
  264. * not provide the checking capability.
  265. */
  266. static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,
  267. struct ubi_sgl *sgl, int offset, int len)
  268. {
  269. return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);
  270. }
  271. #endif /* !__LINUX_UBI_H__ */