ubi.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. #ifndef __LINUX_UBI_H__
  21. #define __LINUX_UBI_H__
  22. #include <linux/ioctl.h>
  23. #include <linux/types.h>
  24. #include <mtd/ubi-user.h>
  25. /*
  26. * enum ubi_open_mode - UBI volume open mode constants.
  27. *
  28. * UBI_READONLY: read-only mode
  29. * UBI_READWRITE: read-write mode
  30. * UBI_EXCLUSIVE: exclusive mode
  31. */
  32. enum {
  33. UBI_READONLY = 1,
  34. UBI_READWRITE,
  35. UBI_EXCLUSIVE
  36. };
  37. /**
  38. * struct ubi_volume_info - UBI volume description data structure.
  39. * @vol_id: volume ID
  40. * @ubi_num: UBI device number this volume belongs to
  41. * @size: how many physical eraseblocks are reserved for this volume
  42. * @used_bytes: how many bytes of data this volume contains
  43. * @used_ebs: how many physical eraseblocks of this volume actually contain any
  44. * data
  45. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  46. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  47. * @upd_marker: non-zero if the volume has update marker set
  48. * @alignment: volume alignment
  49. * @usable_leb_size: how many bytes are available in logical eraseblocks of
  50. * this volume
  51. * @name_len: volume name length
  52. * @name: volume name
  53. * @cdev: UBI volume character device major and minor numbers
  54. *
  55. * The @corrupted flag is only relevant to static volumes and is always zero
  56. * for dynamic ones. This is because UBI does not care about dynamic volume
  57. * data protection and only cares about protecting static volume data.
  58. *
  59. * The @upd_marker flag is set if the volume update operation was interrupted.
  60. * Before touching the volume data during the update operation, UBI first sets
  61. * the update marker flag for this volume. If the volume update operation was
  62. * further interrupted, the update marker indicates this. If the update marker
  63. * is set, the contents of the volume is certainly damaged and a new volume
  64. * update operation has to be started.
  65. *
  66. * To put it differently, @corrupted and @upd_marker fields have different
  67. * semantics:
  68. * o the @corrupted flag means that this static volume is corrupted for some
  69. * reasons, but not because an interrupted volume update
  70. * o the @upd_marker field means that the volume is damaged because of an
  71. * interrupted update operation.
  72. *
  73. * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
  74. *
  75. * The @used_bytes and @used_ebs fields are only really needed for static
  76. * volumes and contain the number of bytes stored in this static volume and how
  77. * many eraseblock this data occupies. In case of dynamic volumes, the
  78. * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
  79. * field is equivalent to @size.
  80. *
  81. * In general, logical eraseblock size is a property of the UBI device, not
  82. * of the UBI volume. Indeed, the logical eraseblock size depends on the
  83. * physical eraseblock size and on how much bytes UBI headers consume. But
  84. * because of the volume alignment (@alignment), the usable size of logical
  85. * eraseblocks if a volume may be less. The following equation is true:
  86. * @usable_leb_size = LEB size - (LEB size mod @alignment),
  87. * where LEB size is the logical eraseblock size defined by the UBI device.
  88. *
  89. * The alignment is multiple to the minimal flash input/output unit size or %1
  90. * if all the available space is used.
  91. *
  92. * To put this differently, alignment may be considered is a way to change
  93. * volume logical eraseblock sizes.
  94. */
  95. struct ubi_volume_info {
  96. int ubi_num;
  97. int vol_id;
  98. int size;
  99. long long used_bytes;
  100. int used_ebs;
  101. int vol_type;
  102. int corrupted;
  103. int upd_marker;
  104. int alignment;
  105. int usable_leb_size;
  106. int name_len;
  107. const char *name;
  108. dev_t cdev;
  109. };
  110. /**
  111. * struct ubi_device_info - UBI device description data structure.
  112. * @ubi_num: ubi device number
  113. * @leb_size: logical eraseblock size on this UBI device
  114. * @leb_start: starting offset of logical eraseblocks within physical
  115. * eraseblocks
  116. * @min_io_size: minimal I/O unit size
  117. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  118. * time (MTD write buffer size)
  119. * @ro_mode: if this device is in read-only mode
  120. * @cdev: UBI character device major and minor numbers
  121. *
  122. * Note, @leb_size is the logical eraseblock size offered by the UBI device.
  123. * Volumes of this UBI device may have smaller logical eraseblock size if their
  124. * alignment is not equivalent to %1.
  125. *
  126. * The @max_write_size field describes flash write maximum write unit. For
  127. * example, NOR flash allows for changing individual bytes, so @min_io_size is
  128. * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
  129. * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
  130. * writing large chunks of data, they write 64-bytes at a time. Obviously, this
  131. * improves write throughput.
  132. *
  133. * Also, the MTD device may have N interleaved (striped) flash chips
  134. * underneath, in which case @min_io_size can be physical min. I/O size of
  135. * single flash chip, while @max_write_size can be N * @min_io_size.
  136. *
  137. * The @max_write_size field is always greater or equivalent to @min_io_size.
  138. * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
  139. * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
  140. * page size.
  141. */
  142. struct ubi_device_info {
  143. int ubi_num;
  144. int leb_size;
  145. int leb_start;
  146. int min_io_size;
  147. int max_write_size;
  148. int ro_mode;
  149. dev_t cdev;
  150. };
  151. /*
  152. * Volume notification types.
  153. * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
  154. * volume was created)
  155. * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
  156. * or a volume was removed)
  157. * @UBI_VOLUME_RESIZED: a volume has been re-sized
  158. * @UBI_VOLUME_RENAMED: a volume has been re-named
  159. * @UBI_VOLUME_UPDATED: data has been written to a volume
  160. *
  161. * These constants define which type of event has happened when a volume
  162. * notification function is invoked.
  163. */
  164. enum {
  165. UBI_VOLUME_ADDED,
  166. UBI_VOLUME_REMOVED,
  167. UBI_VOLUME_RESIZED,
  168. UBI_VOLUME_RENAMED,
  169. UBI_VOLUME_UPDATED,
  170. };
  171. /*
  172. * struct ubi_notification - UBI notification description structure.
  173. * @di: UBI device description object
  174. * @vi: UBI volume description object
  175. *
  176. * UBI notifiers are called with a pointer to an object of this type. The
  177. * object describes the notification. Namely, it provides a description of the
  178. * UBI device and UBI volume the notification informs about.
  179. */
  180. struct ubi_notification {
  181. struct ubi_device_info di;
  182. struct ubi_volume_info vi;
  183. };
  184. /* UBI descriptor given to users when they open UBI volumes */
  185. struct ubi_volume_desc;
  186. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
  187. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  188. struct ubi_volume_info *vi);
  189. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
  190. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  191. int mode);
  192. struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
  193. int ubi_register_volume_notifier(struct notifier_block *nb,
  194. int ignore_existing);
  195. int ubi_unregister_volume_notifier(struct notifier_block *nb);
  196. void ubi_close_volume(struct ubi_volume_desc *desc);
  197. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  198. int len, int check);
  199. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  200. int offset, int len, int dtype);
  201. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  202. int len, int dtype);
  203. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
  204. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  205. int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
  206. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
  207. int ubi_sync(int ubi_num);
  208. /*
  209. * This function is the same as the 'ubi_leb_read()' function, but it does not
  210. * provide the checking capability.
  211. */
  212. static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  213. int offset, int len)
  214. {
  215. return ubi_leb_read(desc, lnum, buf, offset, len, 0);
  216. }
  217. /*
  218. * This function is the same as the 'ubi_leb_write()' functions, but it does
  219. * not have the data type argument.
  220. */
  221. static inline int ubi_write(struct ubi_volume_desc *desc, int lnum,
  222. const void *buf, int offset, int len)
  223. {
  224. return ubi_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
  225. }
  226. /*
  227. * This function is the same as the 'ubi_leb_change()' functions, but it does
  228. * not have the data type argument.
  229. */
  230. static inline int ubi_change(struct ubi_volume_desc *desc, int lnum,
  231. const void *buf, int len)
  232. {
  233. return ubi_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
  234. }
  235. #endif /* !__LINUX_UBI_H__ */