blob: 8fa2753316e8ca771bd69bb8bf9c150e0454cd7b [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>
26#include <mtd/ubi-user.h>
27
Artem Bityutskiy05a3cb72012-05-20 21:14:22 +030028/* All voumes/LEBs */
29#define UBI_ALL -1
30
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040031/*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040032 * enum ubi_open_mode - UBI volume open mode constants.
33 *
34 * UBI_READONLY: read-only mode
35 * UBI_READWRITE: read-write mode
36 * UBI_EXCLUSIVE: exclusive mode
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +010037 * UBI_METAONLY: modify only the volume meta-data,
38 * i.e. the data stored in the volume table, but not in any of volume LEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040039 */
40enum {
41 UBI_READONLY = 1,
42 UBI_READWRITE,
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +010043 UBI_EXCLUSIVE,
44 UBI_METAONLY
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040045};
46
47/**
48 * struct ubi_volume_info - UBI volume description data structure.
49 * @vol_id: volume ID
50 * @ubi_num: UBI device number this volume belongs to
51 * @size: how many physical eraseblocks are reserved for this volume
52 * @used_bytes: how many bytes of data this volume contains
53 * @used_ebs: how many physical eraseblocks of this volume actually contain any
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030054 * data
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040055 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
56 * @corrupted: non-zero if the volume is corrupted (static volumes only)
57 * @upd_marker: non-zero if the volume has update marker set
58 * @alignment: volume alignment
59 * @usable_leb_size: how many bytes are available in logical eraseblocks of
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030060 * this volume
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040061 * @name_len: volume name length
62 * @name: volume name
63 * @cdev: UBI volume character device major and minor numbers
64 *
65 * The @corrupted flag is only relevant to static volumes and is always zero
66 * for dynamic ones. This is because UBI does not care about dynamic volume
67 * data protection and only cares about protecting static volume data.
68 *
69 * The @upd_marker flag is set if the volume update operation was interrupted.
70 * Before touching the volume data during the update operation, UBI first sets
71 * the update marker flag for this volume. If the volume update operation was
72 * further interrupted, the update marker indicates this. If the update marker
73 * is set, the contents of the volume is certainly damaged and a new volume
74 * update operation has to be started.
75 *
76 * To put it differently, @corrupted and @upd_marker fields have different
77 * semantics:
78 * o the @corrupted flag means that this static volume is corrupted for some
79 * reasons, but not because an interrupted volume update
80 * o the @upd_marker field means that the volume is damaged because of an
81 * interrupted update operation.
82 *
83 * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
84 *
85 * The @used_bytes and @used_ebs fields are only really needed for static
86 * volumes and contain the number of bytes stored in this static volume and how
87 * many eraseblock this data occupies. In case of dynamic volumes, the
88 * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
89 * field is equivalent to @size.
90 *
91 * In general, logical eraseblock size is a property of the UBI device, not
92 * of the UBI volume. Indeed, the logical eraseblock size depends on the
93 * physical eraseblock size and on how much bytes UBI headers consume. But
94 * because of the volume alignment (@alignment), the usable size of logical
95 * eraseblocks if a volume may be less. The following equation is true:
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +030096 * @usable_leb_size = LEB size - (LEB size mod @alignment),
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040097 * where LEB size is the logical eraseblock size defined by the UBI device.
98 *
99 * The alignment is multiple to the minimal flash input/output unit size or %1
100 * if all the available space is used.
101 *
102 * To put this differently, alignment may be considered is a way to change
103 * volume logical eraseblock sizes.
104 */
105struct ubi_volume_info {
106 int ubi_num;
107 int vol_id;
108 int size;
109 long long used_bytes;
110 int used_ebs;
111 int vol_type;
112 int corrupted;
113 int upd_marker;
114 int alignment;
115 int usable_leb_size;
116 int name_len;
117 const char *name;
118 dev_t cdev;
119};
120
121/**
122 * struct ubi_device_info - UBI device description data structure.
123 * @ubi_num: ubi device number
124 * @leb_size: logical eraseblock size on this UBI device
Artem Bityutskiyf43ec882011-02-14 13:36:54 +0200125 * @leb_start: starting offset of logical eraseblocks within physical
126 * eraseblocks
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400127 * @min_io_size: minimal I/O unit size
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200128 * @max_write_size: maximum amount of bytes the underlying flash can write at a
129 * time (MTD write buffer size)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400130 * @ro_mode: if this device is in read-only mode
131 * @cdev: UBI character device major and minor numbers
132 *
133 * Note, @leb_size is the logical eraseblock size offered by the UBI device.
134 * Volumes of this UBI device may have smaller logical eraseblock size if their
135 * alignment is not equivalent to %1.
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200136 *
137 * The @max_write_size field describes flash write maximum write unit. For
138 * example, NOR flash allows for changing individual bytes, so @min_io_size is
139 * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
140 * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
141 * writing large chunks of data, they write 64-bytes at a time. Obviously, this
142 * improves write throughput.
143 *
144 * Also, the MTD device may have N interleaved (striped) flash chips
145 * underneath, in which case @min_io_size can be physical min. I/O size of
146 * single flash chip, while @max_write_size can be N * @min_io_size.
147 *
148 * The @max_write_size field is always greater or equivalent to @min_io_size.
149 * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
150 * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
151 * page size.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400152 */
153struct ubi_device_info {
154 int ubi_num;
155 int leb_size;
Artem Bityutskiyf43ec882011-02-14 13:36:54 +0200156 int leb_start;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400157 int min_io_size;
Artem Bityutskiy30b542e2011-01-30 18:37:33 +0200158 int max_write_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400159 int ro_mode;
160 dev_t cdev;
161};
162
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400163/*
David Wagner01dc9cc2011-06-20 17:34:19 +0200164 * Volume notification types.
165 * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
166 * volume was created)
167 * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
168 * or a volume was removed)
169 * @UBI_VOLUME_RESIZED: a volume has been re-sized
170 * @UBI_VOLUME_RENAMED: a volume has been re-named
171 * @UBI_VOLUME_UPDATED: data has been written to a volume
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400172 *
173 * These constants define which type of event has happened when a volume
174 * notification function is invoked.
175 */
176enum {
177 UBI_VOLUME_ADDED,
178 UBI_VOLUME_REMOVED,
179 UBI_VOLUME_RESIZED,
180 UBI_VOLUME_RENAMED,
181 UBI_VOLUME_UPDATED,
182};
183
184/*
185 * struct ubi_notification - UBI notification description structure.
186 * @di: UBI device description object
187 * @vi: UBI volume description object
188 *
189 * UBI notifiers are called with a pointer to an object of this type. The
190 * object describes the notification. Namely, it provides a description of the
191 * UBI device and UBI volume the notification informs about.
192 */
193struct ubi_notification {
194 struct ubi_device_info di;
195 struct ubi_volume_info vi;
196};
197
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400198/* UBI descriptor given to users when they open UBI volumes */
199struct ubi_volume_desc;
200
201int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
202void ubi_get_volume_info(struct ubi_volume_desc *desc,
203 struct ubi_volume_info *vi);
204struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
205struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
206 int mode);
Corentin Charyb5710282009-09-28 21:10:11 +0200207struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
208
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400209int ubi_register_volume_notifier(struct notifier_block *nb,
210 int ignore_existing);
211int ubi_unregister_volume_notifier(struct notifier_block *nb);
212
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400213void ubi_close_volume(struct ubi_volume_desc *desc);
214int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
215 int len, int check);
216int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200217 int offset, int len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400218int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200219 int len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400220int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
221int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200222int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400223int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300224int ubi_sync(int ubi_num);
Joel Reardon62f384552012-05-20 21:27:11 +0200225int ubi_flush(int ubi_num, int vol_id, int lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400226
227/*
228 * This function is the same as the 'ubi_leb_read()' function, but it does not
229 * provide the checking capability.
230 */
231static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
232 int offset, int len)
233{
234 return ubi_leb_read(desc, lnum, buf, offset, len, 0);
235}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400236#endif /* !__LINUX_UBI_H__ */