blob: 1e271cb559cd97f40eb1082071798ed5d6429acc [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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
21#ifndef __LINUX_UBI_H__
22#define __LINUX_UBI_H__
23
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +030024#include <linux/ioctl.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040025#include <linux/types.h>
Richard Weinberger9ff08972015-01-10 22:52:13 +010026#include <linux/scatterlist.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040027#include <mtd/ubi-user.h>
28
Artem Bityutskiy05a3cb72012-05-20 21:14:22 +030029/* All voumes/LEBs */
30#define UBI_ALL -1
31
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040032/*
Richard Weinberger9ff08972015-01-10 22:52:13 +010033 * Maximum number of scatter gather list entries,
34 * we use only 64 to have a lower memory foot print.
35 */
36#define UBI_MAX_SG_COUNT 64
37
38/*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040039 * enum ubi_open_mode - UBI volume open mode constants.
40 *
41 * UBI_READONLY: read-only mode
42 * UBI_READWRITE: read-write mode
43 * UBI_EXCLUSIVE: exclusive mode
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +010044 * UBI_METAONLY: modify only the volume meta-data,
45 * i.e. the data stored in the volume table, but not in any of volume LEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040046 */
47enum {
48 UBI_READONLY = 1,
49 UBI_READWRITE,
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +010050 UBI_EXCLUSIVE,
51 UBI_METAONLY
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040052};
53
54/**
55 * struct ubi_volume_info - UBI volume description data structure.
56 * @vol_id: volume ID
57 * @ubi_num: UBI device number this volume belongs to
58 * @size: how many physical eraseblocks are reserved for this volume
59 * @used_bytes: how many bytes of data this volume contains
60 * @used_ebs: how many physical eraseblocks of this volume actually contain any
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030061 * data
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040062 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
63 * @corrupted: non-zero if the volume is corrupted (static volumes only)
64 * @upd_marker: non-zero if the volume has update marker set
65 * @alignment: volume alignment
66 * @usable_leb_size: how many bytes are available in logical eraseblocks of
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030067 * this volume
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040068 * @name_len: volume name length
69 * @name: volume name
70 * @cdev: UBI volume character device major and minor numbers
71 *
72 * The @corrupted flag is only relevant to static volumes and is always zero
73 * for dynamic ones. This is because UBI does not care about dynamic volume
74 * data protection and only cares about protecting static volume data.
75 *
76 * The @upd_marker flag is set if the volume update operation was interrupted.
77 * Before touching the volume data during the update operation, UBI first sets
78 * the update marker flag for this volume. If the volume update operation was
79 * further interrupted, the update marker indicates this. If the update marker
80 * is set, the contents of the volume is certainly damaged and a new volume
81 * update operation has to be started.
82 *
83 * To put it differently, @corrupted and @upd_marker fields have different
84 * semantics:
85 * o the @corrupted flag means that this static volume is corrupted for some
86 * reasons, but not because an interrupted volume update
87 * o the @upd_marker field means that the volume is damaged because of an
88 * interrupted update operation.
89 *
90 * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
91 *
92 * The @used_bytes and @used_ebs fields are only really needed for static
93 * volumes and contain the number of bytes stored in this static volume and how
94 * many eraseblock this data occupies. In case of dynamic volumes, the
95 * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
96 * field is equivalent to @size.
97 *
98 * In general, logical eraseblock size is a property of the UBI device, not
99 * of the UBI volume. Indeed, the logical eraseblock size depends on the
100 * physical eraseblock size and on how much bytes UBI headers consume. But
101 * because of the volume alignment (@alignment), the usable size of logical
102 * eraseblocks if a volume may be less. The following equation is true:
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +0300103 * @usable_leb_size = LEB size - (LEB size mod @alignment),
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400104 * where LEB size is the logical eraseblock size defined by the UBI device.
105 *
106 * The alignment is multiple to the minimal flash input/output unit size or %1
107 * if all the available space is used.
108 *
109 * To put this differently, alignment may be considered is a way to change
110 * volume logical eraseblock sizes.
111 */
112struct ubi_volume_info {
113 int ubi_num;
114 int vol_id;
115 int size;
116 long long used_bytes;
117 int used_ebs;
118 int vol_type;
119 int corrupted;
120 int upd_marker;
121 int alignment;
122 int usable_leb_size;
123 int name_len;
124 const char *name;
125 dev_t cdev;
126};
127
128/**
Richard Weinberger9ff08972015-01-10 22:52:13 +0100129 * struct ubi_sgl - UBI scatter gather list data structure.
130 * @list_pos: current position in @sg[]
131 * @page_pos: current position in @sg[@list_pos]
132 * @sg: the scatter gather list itself
133 *
134 * ubi_sgl is a wrapper around a scatter list which keeps track of the
135 * current position in the list and the current list item such that
136 * it can be used across multiple ubi_leb_read_sg() calls.
137 */
138struct ubi_sgl {
139 int list_pos;
140 int page_pos;
141 struct scatterlist sg[UBI_MAX_SG_COUNT];
142};
143
144/**
145 * ubi_sgl_init - initialize an UBI scatter gather list data structure.
146 * @usgl: the UBI scatter gather struct itself
147 *
148 * Please note that you still have to use sg_init_table() or any adequate
149 * function to initialize the unterlaying struct scatterlist.
150 */
151static inline void ubi_sgl_init(struct ubi_sgl *usgl)
152{
153 usgl->list_pos = 0;
154 usgl->page_pos = 0;
155}
156
157/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400158 * struct ubi_device_info - UBI device description data structure.
159 * @ubi_num: ubi device number
160 * @leb_size: logical eraseblock size on this UBI device
Artem Bityutskiyf43ec882011-02-14 13:36:54 +0200161 * @leb_start: starting offset of logical eraseblocks within physical
162 * eraseblocks
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400163 * @min_io_size: minimal I/O unit size
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200164 * @max_write_size: maximum amount of bytes the underlying flash can write at a
165 * time (MTD write buffer size)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400166 * @ro_mode: if this device is in read-only mode
167 * @cdev: UBI character device major and minor numbers
168 *
169 * Note, @leb_size is the logical eraseblock size offered by the UBI device.
170 * Volumes of this UBI device may have smaller logical eraseblock size if their
171 * alignment is not equivalent to %1.
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200172 *
173 * The @max_write_size field describes flash write maximum write unit. For
174 * example, NOR flash allows for changing individual bytes, so @min_io_size is
175 * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
176 * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
177 * writing large chunks of data, they write 64-bytes at a time. Obviously, this
178 * improves write throughput.
179 *
180 * Also, the MTD device may have N interleaved (striped) flash chips
181 * underneath, in which case @min_io_size can be physical min. I/O size of
182 * single flash chip, while @max_write_size can be N * @min_io_size.
183 *
184 * The @max_write_size field is always greater or equivalent to @min_io_size.
185 * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
186 * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
187 * page size.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400188 */
189struct ubi_device_info {
190 int ubi_num;
191 int leb_size;
Artem Bityutskiyf43ec882011-02-14 13:36:54 +0200192 int leb_start;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400193 int min_io_size;
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200194 int max_write_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400195 int ro_mode;
196 dev_t cdev;
197};
198
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400199/*
David Wagner01dc9cc2011-06-20 17:34:19 +0200200 * Volume notification types.
201 * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
202 * volume was created)
203 * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
204 * or a volume was removed)
205 * @UBI_VOLUME_RESIZED: a volume has been re-sized
206 * @UBI_VOLUME_RENAMED: a volume has been re-named
207 * @UBI_VOLUME_UPDATED: data has been written to a volume
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400208 *
209 * These constants define which type of event has happened when a volume
210 * notification function is invoked.
211 */
212enum {
213 UBI_VOLUME_ADDED,
214 UBI_VOLUME_REMOVED,
215 UBI_VOLUME_RESIZED,
216 UBI_VOLUME_RENAMED,
217 UBI_VOLUME_UPDATED,
218};
219
220/*
221 * struct ubi_notification - UBI notification description structure.
222 * @di: UBI device description object
223 * @vi: UBI volume description object
224 *
225 * UBI notifiers are called with a pointer to an object of this type. The
226 * object describes the notification. Namely, it provides a description of the
227 * UBI device and UBI volume the notification informs about.
228 */
229struct ubi_notification {
230 struct ubi_device_info di;
231 struct ubi_volume_info vi;
232};
233
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400234/* UBI descriptor given to users when they open UBI volumes */
235struct ubi_volume_desc;
236
237int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
238void ubi_get_volume_info(struct ubi_volume_desc *desc,
239 struct ubi_volume_info *vi);
240struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
241struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
242 int mode);
Corentin Charyb5710282009-09-28 21:10:11 +0200243struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
244
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400245int ubi_register_volume_notifier(struct notifier_block *nb,
246 int ignore_existing);
247int ubi_unregister_volume_notifier(struct notifier_block *nb);
248
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400249void ubi_close_volume(struct ubi_volume_desc *desc);
250int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
251 int len, int check);
Richard Weinberger9ff08972015-01-10 22:52:13 +0100252int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
253 int offset, int len, int check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400254int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200255 int offset, int len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400256int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200257 int len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400258int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
259int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200260int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400261int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300262int ubi_sync(int ubi_num);
Joel Reardon62f384552012-05-20 21:27:11 +0200263int ubi_flush(int ubi_num, int vol_id, int lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400264
265/*
266 * This function is the same as the 'ubi_leb_read()' function, but it does not
267 * provide the checking capability.
268 */
269static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
270 int offset, int len)
271{
272 return ubi_leb_read(desc, lnum, buf, offset, len, 0);
273}
Richard Weinberger9ff08972015-01-10 22:52:13 +0100274
275/*
276 * This function is the same as the 'ubi_leb_read_sg()' function, but it does
277 * not provide the checking capability.
278 */
279static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,
280 struct ubi_sgl *sgl, int offset, int len)
281{
282 return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);
283}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400284#endif /* !__LINUX_UBI_H__ */