ubi.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <asm/ioctl.h>
  23. #include <linux/types.h>
  24. #include <mtd/ubi-user.h>
  25. /*
  26. * UBI data type hint constants.
  27. *
  28. * UBI_LONGTERM: long-term data
  29. * UBI_SHORTTERM: short-term data
  30. * UBI_UNKNOWN: data persistence is unknown
  31. *
  32. * These constants are used when data is written to UBI volumes in order to
  33. * help the UBI wear-leveling unit to find more appropriate physical
  34. * eraseblocks.
  35. */
  36. enum {
  37. UBI_LONGTERM = 1,
  38. UBI_SHORTTERM,
  39. UBI_UNKNOWN
  40. };
  41. /*
  42. * enum ubi_open_mode - UBI volume open mode constants.
  43. *
  44. * UBI_READONLY: read-only mode
  45. * UBI_READWRITE: read-write mode
  46. * UBI_EXCLUSIVE: exclusive mode
  47. */
  48. enum {
  49. UBI_READONLY = 1,
  50. UBI_READWRITE,
  51. UBI_EXCLUSIVE
  52. };
  53. /**
  54. * struct ubi_volume_info - UBI volume description data structure.
  55. * @vol_id: volume ID
  56. * @ubi_num: UBI device number this volume belongs to
  57. * @size: how many physical eraseblocks are reserved for this volume
  58. * @used_bytes: how many bytes of data this volume contains
  59. * @used_ebs: how many physical eraseblocks of this volume actually contain any
  60. * data
  61. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  62. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  63. * @upd_marker: non-zero if the volume has update marker set
  64. * @alignment: volume alignment
  65. * @usable_leb_size: how many bytes are available in logical eraseblocks of
  66. * this volume
  67. * @name_len: volume name length
  68. * @name: volume name
  69. * @cdev: UBI volume character device major and minor numbers
  70. *
  71. * The @corrupted flag is only relevant to static volumes and is always zero
  72. * for dynamic ones. This is because UBI does not care about dynamic volume
  73. * data protection and only cares about protecting static volume data.
  74. *
  75. * The @upd_marker flag is set if the volume update operation was interrupted.
  76. * Before touching the volume data during the update operation, UBI first sets
  77. * the update marker flag for this volume. If the volume update operation was
  78. * further interrupted, the update marker indicates this. If the update marker
  79. * is set, the contents of the volume is certainly damaged and a new volume
  80. * update operation has to be started.
  81. *
  82. * To put it differently, @corrupted and @upd_marker fields have different
  83. * semantics:
  84. * o the @corrupted flag means that this static volume is corrupted for some
  85. * reasons, but not because an interrupted volume update
  86. * o the @upd_marker field means that the volume is damaged because of an
  87. * interrupted update operation.
  88. *
  89. * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
  90. *
  91. * The @used_bytes and @used_ebs fields are only really needed for static
  92. * volumes and contain the number of bytes stored in this static volume and how
  93. * many eraseblock this data occupies. In case of dynamic volumes, the
  94. * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
  95. * field is equivalent to @size.
  96. *
  97. * In general, logical eraseblock size is a property of the UBI device, not
  98. * of the UBI volume. Indeed, the logical eraseblock size depends on the
  99. * physical eraseblock size and on how much bytes UBI headers consume. But
  100. * because of the volume alignment (@alignment), the usable size of logical
  101. * eraseblocks if a volume may be less. The following equation is true:
  102. * @usable_leb_size = LEB size - (LEB size mod @alignment),
  103. * where LEB size is the logical eraseblock size defined by the UBI device.
  104. *
  105. * The alignment is multiple to the minimal flash input/output unit size or %1
  106. * if all the available space is used.
  107. *
  108. * To put this differently, alignment may be considered is a way to change
  109. * volume logical eraseblock sizes.
  110. */
  111. struct ubi_volume_info {
  112. int ubi_num;
  113. int vol_id;
  114. int size;
  115. long long used_bytes;
  116. int used_ebs;
  117. int vol_type;
  118. int corrupted;
  119. int upd_marker;
  120. int alignment;
  121. int usable_leb_size;
  122. int name_len;
  123. const char *name;
  124. dev_t cdev;
  125. };
  126. /**
  127. * struct ubi_device_info - UBI device description data structure.
  128. * @ubi_num: ubi device number
  129. * @leb_size: logical eraseblock size on this UBI device
  130. * @min_io_size: minimal I/O unit size
  131. * @ro_mode: if this device is in read-only mode
  132. * @cdev: UBI character device major and minor numbers
  133. *
  134. * Note, @leb_size is the logical eraseblock size offered by the UBI device.
  135. * Volumes of this UBI device may have smaller logical eraseblock size if their
  136. * alignment is not equivalent to %1.
  137. */
  138. struct ubi_device_info {
  139. int ubi_num;
  140. int leb_size;
  141. int min_io_size;
  142. int ro_mode;
  143. dev_t cdev;
  144. };
  145. /* UBI descriptor given to users when they open UBI volumes */
  146. struct ubi_volume_desc;
  147. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
  148. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  149. struct ubi_volume_info *vi);
  150. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
  151. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  152. int mode);
  153. void ubi_close_volume(struct ubi_volume_desc *desc);
  154. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  155. int len, int check);
  156. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  157. int offset, int len, int dtype);
  158. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  159. int len, int dtype);
  160. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
  161. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  162. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
  163. /*
  164. * This function is the same as the 'ubi_leb_read()' function, but it does not
  165. * provide the checking capability.
  166. */
  167. static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  168. int offset, int len)
  169. {
  170. return ubi_leb_read(desc, lnum, buf, offset, len, 0);
  171. }
  172. /*
  173. * This function is the same as the 'ubi_leb_write()' functions, but it does
  174. * not have the data type argument.
  175. */
  176. static inline int ubi_write(struct ubi_volume_desc *desc, int lnum,
  177. const void *buf, int offset, int len)
  178. {
  179. return ubi_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
  180. }
  181. /*
  182. * This function is the same as the 'ubi_leb_change()' functions, but it does
  183. * not have the data type argument.
  184. */
  185. static inline int ubi_change(struct ubi_volume_desc *desc, int lnum,
  186. const void *buf, int len)
  187. {
  188. return ubi_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
  189. }
  190. #endif /* !__LINUX_UBI_H__ */