blob: 589c423fac2dd53a15bbfe94062c7734ef1a344a [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/* This file mostly implements UBI kernel API functions */
22
23#include <linux/module.h>
24#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Corentin Charyb5710282009-09-28 21:10:11 +020026#include <linux/namei.h>
27#include <linux/fs.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040028#include <asm/div64.h>
29#include "ubi.h"
30
31/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040032 * ubi_do_get_device_info - get information about UBI device.
33 * @ubi: UBI device description object
34 * @di: the information is stored here
35 *
36 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
37 * device is locked and cannot disappear.
38 */
39void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
40{
41 di->ubi_num = ubi->ubi_num;
42 di->leb_size = ubi->leb_size;
Artem Bityutskiyf43ec882011-02-14 13:36:54 +020043 di->leb_start = ubi->leb_start;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040044 di->min_io_size = ubi->min_io_size;
Artem Bityutskiy30b542e2011-01-30 18:37:33 +020045 di->max_write_size = ubi->max_write_size;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040046 di->ro_mode = ubi->ro_mode;
47 di->cdev = ubi->cdev.dev;
48}
49EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
50
51/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040052 * ubi_get_device_info - get information about UBI device.
53 * @ubi_num: UBI device number
54 * @di: the information is stored here
55 *
Artem Bityutskiye73f4452007-12-17 17:37:26 +020056 * This function returns %0 in case of success, %-EINVAL if the UBI device
57 * number is invalid, and %-ENODEV if there is no such UBI device.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040058 */
59int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
60{
Artem Bityutskiye73f4452007-12-17 17:37:26 +020061 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040062
Artem Bityutskiye73f4452007-12-17 17:37:26 +020063 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
64 return -EINVAL;
Artem Bityutskiye73f4452007-12-17 17:37:26 +020065 ubi = ubi_get_device(ubi_num);
66 if (!ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040067 return -ENODEV;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040068 ubi_do_get_device_info(ubi, di);
Artem Bityutskiye73f4452007-12-17 17:37:26 +020069 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040070 return 0;
71}
72EXPORT_SYMBOL_GPL(ubi_get_device_info);
73
74/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040075 * ubi_do_get_volume_info - get information about UBI volume.
76 * @ubi: UBI device description object
77 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040078 * @vi: the information is stored here
79 */
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040080void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
81 struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040082{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040083 vi->vol_id = vol->vol_id;
84 vi->ubi_num = ubi->ubi_num;
85 vi->size = vol->reserved_pebs;
86 vi->used_bytes = vol->used_bytes;
87 vi->vol_type = vol->vol_type;
88 vi->corrupted = vol->corrupted;
89 vi->upd_marker = vol->upd_marker;
90 vi->alignment = vol->alignment;
91 vi->usable_leb_size = vol->usable_leb_size;
92 vi->name_len = vol->name_len;
93 vi->name = vol->name;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020094 vi->cdev = vol->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040095}
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040096
97/**
98 * ubi_get_volume_info - get information about UBI volume.
99 * @desc: volume descriptor
100 * @vi: the information is stored here
101 */
102void ubi_get_volume_info(struct ubi_volume_desc *desc,
103 struct ubi_volume_info *vi)
104{
105 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
106}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400107EXPORT_SYMBOL_GPL(ubi_get_volume_info);
108
109/**
110 * ubi_open_volume - open UBI volume.
111 * @ubi_num: UBI device number
112 * @vol_id: volume ID
113 * @mode: open mode
114 *
115 * The @mode parameter specifies if the volume should be opened in read-only
116 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
117 * nobody else will be able to open this volume. UBI allows to have many volume
118 * readers and one writer at a time.
119 *
120 * If a static volume is being opened for the first time since boot, it will be
121 * checked by this function, which means it will be fully read and the CRC
122 * checksum of each logical eraseblock will be checked.
123 *
124 * This function returns volume descriptor in case of success and a negative
125 * error code in case of failure.
126 */
127struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
128{
129 int err;
130 struct ubi_volume_desc *desc;
Jesper Juhl0169b492007-08-04 01:25:26 +0200131 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400132 struct ubi_volume *vol;
133
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300134 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400135
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200136 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
137 return ERR_PTR(-EINVAL);
Jesper Juhl0169b492007-08-04 01:25:26 +0200138
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400139 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +0100140 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200141 return ERR_PTR(-EINVAL);
142
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200143 /*
144 * First of all, we have to get the UBI device to prevent its removal.
145 */
146 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200147 if (!ubi)
148 return ERR_PTR(-ENODEV);
149
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200150 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
151 err = -EINVAL;
152 goto out_put_ubi;
153 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400154
155 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200156 if (!desc) {
157 err = -ENOMEM;
158 goto out_put_ubi;
159 }
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200160
161 err = -ENODEV;
162 if (!try_module_get(THIS_MODULE))
163 goto out_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400164
165 spin_lock(&ubi->volumes_lock);
166 vol = ubi->volumes[vol_id];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200167 if (!vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400168 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169
170 err = -EBUSY;
171 switch (mode) {
172 case UBI_READONLY:
173 if (vol->exclusive)
174 goto out_unlock;
175 vol->readers += 1;
176 break;
177
178 case UBI_READWRITE:
179 if (vol->exclusive || vol->writers > 0)
180 goto out_unlock;
181 vol->writers += 1;
182 break;
183
184 case UBI_EXCLUSIVE:
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +0100185 if (vol->exclusive || vol->writers || vol->readers ||
186 vol->metaonly)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187 goto out_unlock;
188 vol->exclusive = 1;
189 break;
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +0100190
191 case UBI_METAONLY:
192 if (vol->metaonly || vol->exclusive)
193 goto out_unlock;
194 vol->metaonly = 1;
195 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400196 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200197 get_device(&vol->dev);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200198 vol->ref_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400199 spin_unlock(&ubi->volumes_lock);
200
201 desc->vol = vol;
202 desc->mode = mode;
203
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200204 mutex_lock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400205 if (!vol->checked) {
206 /* This is the first open - check the volume */
207 err = ubi_check_volume(ubi, vol_id);
208 if (err < 0) {
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200209 mutex_unlock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400210 ubi_close_volume(desc);
211 return ERR_PTR(err);
212 }
213 if (err == 1) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300214 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400215 vol_id, ubi->ubi_num);
216 vol->corrupted = 1;
217 }
218 vol->checked = 1;
219 }
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200220 mutex_unlock(&ubi->ckvol_mutex);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200221
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400222 return desc;
223
224out_unlock:
225 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400226 module_put(THIS_MODULE);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200227out_free:
228 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200229out_put_ubi:
230 ubi_put_device(ubi);
Tanya Brokhman326087032014-10-20 19:57:00 +0300231 ubi_err(ubi, "cannot open device %d, volume %d, error %d",
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300232 ubi_num, vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400233 return ERR_PTR(err);
234}
235EXPORT_SYMBOL_GPL(ubi_open_volume);
236
237/**
238 * ubi_open_volume_nm - open UBI volume by name.
239 * @ubi_num: UBI device number
240 * @name: volume name
241 * @mode: open mode
242 *
243 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
244 */
245struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
246 int mode)
247{
248 int i, vol_id = -1, len;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400249 struct ubi_device *ubi;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200250 struct ubi_volume_desc *ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400251
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300252 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253
254 if (!name)
255 return ERR_PTR(-EINVAL);
256
257 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
258 if (len > UBI_VOL_NAME_MAX)
259 return ERR_PTR(-EINVAL);
260
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200261 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
262 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400263
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200264 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200265 if (!ubi)
266 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400267
268 spin_lock(&ubi->volumes_lock);
269 /* Walk all volumes of this UBI device */
270 for (i = 0; i < ubi->vtbl_slots; i++) {
271 struct ubi_volume *vol = ubi->volumes[i];
272
273 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
274 vol_id = i;
275 break;
276 }
277 }
278 spin_unlock(&ubi->volumes_lock);
279
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200280 if (vol_id >= 0)
281 ret = ubi_open_volume(ubi_num, vol_id, mode);
282 else
283 ret = ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400284
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200285 /*
286 * We should put the UBI device even in case of success, because
287 * 'ubi_open_volume()' took a reference as well.
288 */
289 ubi_put_device(ubi);
290 return ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291}
292EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
293
294/**
Corentin Charyb5710282009-09-28 21:10:11 +0200295 * ubi_open_volume_path - open UBI volume by its character device node path.
296 * @pathname: volume character device node path
297 * @mode: open mode
298 *
299 * This function is similar to 'ubi_open_volume()', but opens a volume the path
300 * to its character device node.
301 */
302struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
303{
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200304 int error, ubi_num, vol_id, mod;
Corentin Charyb5710282009-09-28 21:10:11 +0200305 struct inode *inode;
306 struct path path;
307
308 dbg_gen("open volume %s, mode %d", pathname, mode);
309
310 if (!pathname || !*pathname)
311 return ERR_PTR(-EINVAL);
312
313 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
314 if (error)
315 return ERR_PTR(error);
316
317 inode = path.dentry->d_inode;
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200318 mod = inode->i_mode;
Corentin Charyb5710282009-09-28 21:10:11 +0200319 ubi_num = ubi_major2num(imajor(inode));
320 vol_id = iminor(inode) - 1;
Corentin Charyb5710282009-09-28 21:10:11 +0200321 path_put(&path);
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200322
323 if (!S_ISCHR(mod))
324 return ERR_PTR(-EINVAL);
325 if (vol_id >= 0 && ubi_num >= 0)
326 return ubi_open_volume(ubi_num, vol_id, mode);
327 return ERR_PTR(-ENODEV);
Corentin Charyb5710282009-09-28 21:10:11 +0200328}
329EXPORT_SYMBOL_GPL(ubi_open_volume_path);
330
331/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332 * ubi_close_volume - close UBI volume.
333 * @desc: volume descriptor
334 */
335void ubi_close_volume(struct ubi_volume_desc *desc)
336{
337 struct ubi_volume *vol = desc->vol;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200338 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400339
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300340 dbg_gen("close device %d, volume %d, mode %d",
341 ubi->ubi_num, vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400342
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200343 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400344 switch (desc->mode) {
345 case UBI_READONLY:
346 vol->readers -= 1;
347 break;
348 case UBI_READWRITE:
349 vol->writers -= 1;
350 break;
351 case UBI_EXCLUSIVE:
352 vol->exclusive = 0;
Richard Weinbergerfafdd2b2014-11-24 22:30:09 +0100353 break;
354 case UBI_METAONLY:
355 vol->metaonly = 0;
356 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400357 }
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200358 vol->ref_count -= 1;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200359 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400360
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200361 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200362 put_device(&vol->dev);
363 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400364 module_put(THIS_MODULE);
365}
366EXPORT_SYMBOL_GPL(ubi_close_volume);
367
368/**
369 * ubi_leb_read - read data.
370 * @desc: volume descriptor
371 * @lnum: logical eraseblock number to read from
372 * @buf: buffer where to store the read data
373 * @offset: offset within the logical eraseblock to read from
374 * @len: how many bytes to read
375 * @check: whether UBI has to check the read data's CRC or not.
376 *
377 * This function reads data from offset @offset of logical eraseblock @lnum and
378 * stores the data at @buf. When reading from static volumes, @check specifies
379 * whether the data has to be checked or not. If yes, the whole logical
380 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
381 * checksum is per-eraseblock). So checking may substantially slow down the
382 * read speed. The @check argument is ignored for dynamic volumes.
383 *
384 * In case of success, this function returns zero. In case of failure, this
385 * function returns a negative error code.
386 *
387 * %-EBADMSG error code is returned:
388 * o for both static and dynamic volumes if MTD driver has detected a data
389 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
390 * o for static volumes in case of data CRC mismatch.
391 *
392 * If the volume is damaged because of an interrupted update this function just
393 * returns immediately with %-EBADF error code.
394 */
395int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
396 int len, int check)
397{
398 struct ubi_volume *vol = desc->vol;
399 struct ubi_device *ubi = vol->ubi;
400 int err, vol_id = vol->vol_id;
401
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300402 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400403
404 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
405 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
406 offset + len > vol->usable_leb_size)
407 return -EINVAL;
408
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300409 if (vol->vol_type == UBI_STATIC_VOLUME) {
410 if (vol->used_ebs == 0)
411 /* Empty static UBI volume */
412 return 0;
413 if (lnum == vol->used_ebs - 1 &&
414 offset + len > vol->last_eb_bytes)
415 return -EINVAL;
416 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400417
418 if (vol->upd_marker)
419 return -EBADF;
420 if (len == 0)
421 return 0;
422
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200423 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Brian Norrisd57f40542011-09-20 18:34:25 -0700424 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300425 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400426 vol->corrupted = 1;
427 }
428
429 return err;
430}
431EXPORT_SYMBOL_GPL(ubi_leb_read);
432
433/**
434 * ubi_leb_write - write data.
435 * @desc: volume descriptor
436 * @lnum: logical eraseblock number to write to
437 * @buf: data to write
438 * @offset: offset within the logical eraseblock where to write
439 * @len: how many bytes to write
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400440 *
441 * This function writes @len bytes of data from @buf to offset @offset of
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200442 * logical eraseblock @lnum.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443 *
444 * This function takes care of physical eraseblock write failures. If write to
445 * the physical eraseblock write operation fails, the logical eraseblock is
446 * re-mapped to another physical eraseblock, the data is recovered, and the
447 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
448 *
449 * If all the data were successfully written, zero is returned. If an error
450 * occurred and UBI has not been able to recover from it, this function returns
451 * a negative error code. Note, in case of an error, it is possible that
452 * something was still written to the flash media, but that may be some
453 * garbage.
454 *
455 * If the volume is damaged because of an interrupted update this function just
456 * returns immediately with %-EBADF code.
457 */
458int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200459 int offset, int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400460{
461 struct ubi_volume *vol = desc->vol;
462 struct ubi_device *ubi = vol->ubi;
463 int vol_id = vol->vol_id;
464
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300465 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400466
467 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
468 return -EINVAL;
469
470 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
471 return -EROFS;
472
473 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900474 offset + len > vol->usable_leb_size ||
475 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400476 return -EINVAL;
477
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400478 if (vol->upd_marker)
479 return -EBADF;
480
481 if (len == 0)
482 return 0;
483
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200484 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400485}
486EXPORT_SYMBOL_GPL(ubi_leb_write);
487
488/*
489 * ubi_leb_change - change logical eraseblock atomically.
490 * @desc: volume descriptor
491 * @lnum: logical eraseblock number to change
492 * @buf: data to write
493 * @len: how many bytes to write
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400494 *
495 * This function changes the contents of a logical eraseblock atomically. @buf
496 * has to contain new logical eraseblock data, and @len - the length of the
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900497 * data, which has to be aligned. The length may be shorter than the logical
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400498 * eraseblock size, ant the logical eraseblock may be appended to more times
499 * later on. This function guarantees that in case of an unclean reboot the old
500 * contents is preserved. Returns zero in case of success and a negative error
501 * code in case of failure.
502 */
503int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200504 int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400505{
506 struct ubi_volume *vol = desc->vol;
507 struct ubi_device *ubi = vol->ubi;
508 int vol_id = vol->vol_id;
509
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300510 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400511
512 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
513 return -EINVAL;
514
515 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
516 return -EROFS;
517
518 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900519 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400520 return -EINVAL;
521
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400522 if (vol->upd_marker)
523 return -EBADF;
524
525 if (len == 0)
526 return 0;
527
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200528 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400529}
530EXPORT_SYMBOL_GPL(ubi_leb_change);
531
532/**
533 * ubi_leb_erase - erase logical eraseblock.
534 * @desc: volume descriptor
535 * @lnum: logical eraseblock number
536 *
537 * This function un-maps logical eraseblock @lnum and synchronously erases the
538 * correspondent physical eraseblock. Returns zero in case of success and a
539 * negative error code in case of failure.
540 *
541 * If the volume is damaged because of an interrupted update this function just
542 * returns immediately with %-EBADF code.
543 */
544int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
545{
546 struct ubi_volume *vol = desc->vol;
547 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200548 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400549
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300550 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400551
552 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
553 return -EROFS;
554
555 if (lnum < 0 || lnum >= vol->reserved_pebs)
556 return -EINVAL;
557
558 if (vol->upd_marker)
559 return -EBADF;
560
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200561 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400562 if (err)
563 return err;
564
Joel Reardon62f384552012-05-20 21:27:11 +0200565 return ubi_wl_flush(ubi, vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400566}
567EXPORT_SYMBOL_GPL(ubi_leb_erase);
568
569/**
570 * ubi_leb_unmap - un-map logical eraseblock.
571 * @desc: volume descriptor
572 * @lnum: logical eraseblock number
573 *
574 * This function un-maps logical eraseblock @lnum and schedules the
575 * corresponding physical eraseblock for erasure, so that it will eventually be
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900576 * physically erased in background. This operation is much faster than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400577 * erase operation.
578 *
579 * Unlike erase, the un-map operation does not guarantee that the logical
580 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
581 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
582 * happens after this, the logical eraseblocks will not necessarily be
583 * un-mapped again when this MTD device is attached. They may actually be
584 * mapped to the same physical eraseblocks again. So, this function has to be
585 * used with care.
586 *
587 * In other words, when un-mapping a logical eraseblock, UBI does not store
588 * any information about this on the flash media, it just marks the logical
589 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
590 * eraseblock is physically erased, it will be mapped again to the same logical
591 * eraseblock when the MTD device is attached again.
592 *
593 * The main and obvious use-case of this function is when the contents of a
594 * logical eraseblock has to be re-written. Then it is much more efficient to
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900595 * first un-map it, then write new data, rather than first erase it, then write
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400596 * new data. Note, once new data has been written to the logical eraseblock,
597 * UBI guarantees that the old contents has gone forever. In other words, if an
598 * unclean reboot happens after the logical eraseblock has been un-mapped and
599 * then written to, it will contain the last written data.
600 *
601 * This function returns zero in case of success and a negative error code in
602 * case of failure. If the volume is damaged because of an interrupted update
603 * this function just returns immediately with %-EBADF code.
604 */
605int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
606{
607 struct ubi_volume *vol = desc->vol;
608 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400609
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300610 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400611
612 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
613 return -EROFS;
614
615 if (lnum < 0 || lnum >= vol->reserved_pebs)
616 return -EINVAL;
617
618 if (vol->upd_marker)
619 return -EBADF;
620
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200621 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400622}
623EXPORT_SYMBOL_GPL(ubi_leb_unmap);
624
625/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400626 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200627 * @desc: volume descriptor
628 * @lnum: logical eraseblock number
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200629 *
630 * This function maps an un-mapped logical eraseblock @lnum to a physical
Coly Li73ac36e2009-01-07 18:09:16 -0800631 * eraseblock. This means, that after a successful invocation of this
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200632 * function the logical eraseblock @lnum will be empty (contain only %0xFF
633 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
634 * happens.
635 *
636 * This function returns zero in case of success, %-EBADF if the volume is
637 * damaged because of an interrupted update, %-EBADMSG if the logical
638 * eraseblock is already mapped, and other negative error codes in case of
639 * other failures.
640 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200641int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200642{
643 struct ubi_volume *vol = desc->vol;
644 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200645
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300646 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200647
648 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
649 return -EROFS;
650
651 if (lnum < 0 || lnum >= vol->reserved_pebs)
652 return -EINVAL;
653
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200654 if (vol->upd_marker)
655 return -EBADF;
656
657 if (vol->eba_tbl[lnum] >= 0)
658 return -EBADMSG;
659
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200660 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200661}
662EXPORT_SYMBOL_GPL(ubi_leb_map);
663
664/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400665 * ubi_is_mapped - check if logical eraseblock is mapped.
666 * @desc: volume descriptor
667 * @lnum: logical eraseblock number
668 *
669 * This function checks if logical eraseblock @lnum is mapped to a physical
670 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
671 * mean it will still be un-mapped after the UBI device is re-attached. The
672 * logical eraseblock may become mapped to the physical eraseblock it was last
673 * mapped to.
674 *
675 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
676 * error code in case of failure. If the volume is damaged because of an
677 * interrupted update this function just returns immediately with %-EBADF error
678 * code.
679 */
680int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
681{
682 struct ubi_volume *vol = desc->vol;
683
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300684 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400685
686 if (lnum < 0 || lnum >= vol->reserved_pebs)
687 return -EINVAL;
688
689 if (vol->upd_marker)
690 return -EBADF;
691
692 return vol->eba_tbl[lnum] >= 0;
693}
694EXPORT_SYMBOL_GPL(ubi_is_mapped);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300695
696/**
697 * ubi_sync - synchronize UBI device buffers.
698 * @ubi_num: UBI device to synchronize
699 *
700 * The underlying MTD device may cache data in hardware or in software. This
701 * function ensures the caches are flushed. Returns zero in case of success and
702 * a negative error code in case of failure.
703 */
704int ubi_sync(int ubi_num)
705{
706 struct ubi_device *ubi;
707
708 ubi = ubi_get_device(ubi_num);
709 if (!ubi)
710 return -ENODEV;
711
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200712 mtd_sync(ubi->mtd);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300713 ubi_put_device(ubi);
714 return 0;
715}
716EXPORT_SYMBOL_GPL(ubi_sync);
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400717
Joel Reardon62f384552012-05-20 21:27:11 +0200718/**
719 * ubi_flush - flush UBI work queue.
720 * @ubi_num: UBI device to flush work queue
721 * @vol_id: volume id to flush for
722 * @lnum: logical eraseblock number to flush for
723 *
724 * This function executes all pending works for a particular volume id / logical
725 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
726 * a wildcard for all of the corresponding volume numbers or logical
727 * eraseblock numbers. It returns zero in case of success and a negative error
728 * code in case of failure.
729 */
730int ubi_flush(int ubi_num, int vol_id, int lnum)
731{
732 struct ubi_device *ubi;
733 int err = 0;
734
735 ubi = ubi_get_device(ubi_num);
736 if (!ubi)
737 return -ENODEV;
738
739 err = ubi_wl_flush(ubi, vol_id, lnum);
740 ubi_put_device(ubi);
741 return err;
742}
743EXPORT_SYMBOL_GPL(ubi_flush);
744
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400745BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
746
747/**
748 * ubi_register_volume_notifier - register a volume notifier.
749 * @nb: the notifier description object
750 * @ignore_existing: if non-zero, do not send "added" notification for all
751 * already existing volumes
752 *
753 * This function registers a volume notifier, which means that
754 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
755 * removed, re-sized, re-named, or updated. The first argument of the function
756 * is the notification type. The second argument is pointer to a
757 * &struct ubi_notification object which describes the notification event.
758 * Using UBI API from the volume notifier is prohibited.
759 *
760 * This function returns zero in case of success and a negative error code
761 * in case of failure.
762 */
763int ubi_register_volume_notifier(struct notifier_block *nb,
764 int ignore_existing)
765{
766 int err;
767
768 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
769 if (err != 0)
770 return err;
771 if (ignore_existing)
772 return 0;
773
774 /*
775 * We are going to walk all UBI devices and all volumes, and
776 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
777 * event. We have to lock the @ubi_devices_mutex to make sure UBI
778 * devices do not disappear.
779 */
780 mutex_lock(&ubi_devices_mutex);
781 ubi_enumerate_volumes(nb);
782 mutex_unlock(&ubi_devices_mutex);
783
784 return err;
785}
786EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
787
788/**
789 * ubi_unregister_volume_notifier - unregister the volume notifier.
790 * @nb: the notifier description object
791 *
792 * This function unregisters volume notifier @nm and returns zero in case of
793 * success and a negative error code in case of failure.
794 */
795int ubi_unregister_volume_notifier(struct notifier_block *nb)
796{
797 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
798}
799EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);