blob: 701df4f848f66045b4747fc214ea41b2497828c6 [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;
43 di->min_io_size = ubi->min_io_size;
Artem Bityutskiy30b542e2011-01-30 18:37:33 +020044 di->max_write_size = ubi->max_write_size;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040045 di->ro_mode = ubi->ro_mode;
46 di->cdev = ubi->cdev.dev;
47}
48EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
49
50/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040051 * ubi_get_device_info - get information about UBI device.
52 * @ubi_num: UBI device number
53 * @di: the information is stored here
54 *
Artem Bityutskiye73f4452007-12-17 17:37:26 +020055 * This function returns %0 in case of success, %-EINVAL if the UBI device
56 * number is invalid, and %-ENODEV if there is no such UBI device.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040057 */
58int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
59{
Artem Bityutskiye73f4452007-12-17 17:37:26 +020060 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040061
Artem Bityutskiye73f4452007-12-17 17:37:26 +020062 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
63 return -EINVAL;
Artem Bityutskiye73f4452007-12-17 17:37:26 +020064 ubi = ubi_get_device(ubi_num);
65 if (!ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040066 return -ENODEV;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040067 ubi_do_get_device_info(ubi, di);
Artem Bityutskiye73f4452007-12-17 17:37:26 +020068 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040069 return 0;
70}
71EXPORT_SYMBOL_GPL(ubi_get_device_info);
72
73/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040074 * ubi_do_get_volume_info - get information about UBI volume.
75 * @ubi: UBI device description object
76 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040077 * @vi: the information is stored here
78 */
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040079void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
80 struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040081{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040082 vi->vol_id = vol->vol_id;
83 vi->ubi_num = ubi->ubi_num;
84 vi->size = vol->reserved_pebs;
85 vi->used_bytes = vol->used_bytes;
86 vi->vol_type = vol->vol_type;
87 vi->corrupted = vol->corrupted;
88 vi->upd_marker = vol->upd_marker;
89 vi->alignment = vol->alignment;
90 vi->usable_leb_size = vol->usable_leb_size;
91 vi->name_len = vol->name_len;
92 vi->name = vol->name;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020093 vi->cdev = vol->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040094}
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040095
96/**
97 * ubi_get_volume_info - get information about UBI volume.
98 * @desc: volume descriptor
99 * @vi: the information is stored here
100 */
101void ubi_get_volume_info(struct ubi_volume_desc *desc,
102 struct ubi_volume_info *vi)
103{
104 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
105}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400106EXPORT_SYMBOL_GPL(ubi_get_volume_info);
107
108/**
109 * ubi_open_volume - open UBI volume.
110 * @ubi_num: UBI device number
111 * @vol_id: volume ID
112 * @mode: open mode
113 *
114 * The @mode parameter specifies if the volume should be opened in read-only
115 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
116 * nobody else will be able to open this volume. UBI allows to have many volume
117 * readers and one writer at a time.
118 *
119 * If a static volume is being opened for the first time since boot, it will be
120 * checked by this function, which means it will be fully read and the CRC
121 * checksum of each logical eraseblock will be checked.
122 *
123 * This function returns volume descriptor in case of success and a negative
124 * error code in case of failure.
125 */
126struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
127{
128 int err;
129 struct ubi_volume_desc *desc;
Jesper Juhl0169b492007-08-04 01:25:26 +0200130 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400131 struct ubi_volume *vol;
132
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300133 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200135 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
136 return ERR_PTR(-EINVAL);
Jesper Juhl0169b492007-08-04 01:25:26 +0200137
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400138 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
139 mode != UBI_EXCLUSIVE)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200140 return ERR_PTR(-EINVAL);
141
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200142 /*
143 * First of all, we have to get the UBI device to prevent its removal.
144 */
145 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200146 if (!ubi)
147 return ERR_PTR(-ENODEV);
148
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200149 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
150 err = -EINVAL;
151 goto out_put_ubi;
152 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400153
154 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200155 if (!desc) {
156 err = -ENOMEM;
157 goto out_put_ubi;
158 }
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200159
160 err = -ENODEV;
161 if (!try_module_get(THIS_MODULE))
162 goto out_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400163
164 spin_lock(&ubi->volumes_lock);
165 vol = ubi->volumes[vol_id];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200166 if (!vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400167 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400168
169 err = -EBUSY;
170 switch (mode) {
171 case UBI_READONLY:
172 if (vol->exclusive)
173 goto out_unlock;
174 vol->readers += 1;
175 break;
176
177 case UBI_READWRITE:
178 if (vol->exclusive || vol->writers > 0)
179 goto out_unlock;
180 vol->writers += 1;
181 break;
182
183 case UBI_EXCLUSIVE:
184 if (vol->exclusive || vol->writers || vol->readers)
185 goto out_unlock;
186 vol->exclusive = 1;
187 break;
188 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200189 get_device(&vol->dev);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200190 vol->ref_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400191 spin_unlock(&ubi->volumes_lock);
192
193 desc->vol = vol;
194 desc->mode = mode;
195
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200196 mutex_lock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400197 if (!vol->checked) {
198 /* This is the first open - check the volume */
199 err = ubi_check_volume(ubi, vol_id);
200 if (err < 0) {
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200201 mutex_unlock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400202 ubi_close_volume(desc);
203 return ERR_PTR(err);
204 }
205 if (err == 1) {
206 ubi_warn("volume %d on UBI device %d is corrupted",
207 vol_id, ubi->ubi_num);
208 vol->corrupted = 1;
209 }
210 vol->checked = 1;
211 }
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200212 mutex_unlock(&ubi->ckvol_mutex);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200213
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400214 return desc;
215
216out_unlock:
217 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400218 module_put(THIS_MODULE);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200219out_free:
220 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200221out_put_ubi:
222 ubi_put_device(ubi);
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300223 dbg_err("cannot open device %d, volume %d, error %d",
224 ubi_num, vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400225 return ERR_PTR(err);
226}
227EXPORT_SYMBOL_GPL(ubi_open_volume);
228
229/**
230 * ubi_open_volume_nm - open UBI volume by name.
231 * @ubi_num: UBI device number
232 * @name: volume name
233 * @mode: open mode
234 *
235 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
236 */
237struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
238 int mode)
239{
240 int i, vol_id = -1, len;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400241 struct ubi_device *ubi;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200242 struct ubi_volume_desc *ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400243
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300244 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400245
246 if (!name)
247 return ERR_PTR(-EINVAL);
248
249 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
250 if (len > UBI_VOL_NAME_MAX)
251 return ERR_PTR(-EINVAL);
252
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200253 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
254 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200256 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200257 if (!ubi)
258 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400259
260 spin_lock(&ubi->volumes_lock);
261 /* Walk all volumes of this UBI device */
262 for (i = 0; i < ubi->vtbl_slots; i++) {
263 struct ubi_volume *vol = ubi->volumes[i];
264
265 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
266 vol_id = i;
267 break;
268 }
269 }
270 spin_unlock(&ubi->volumes_lock);
271
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200272 if (vol_id >= 0)
273 ret = ubi_open_volume(ubi_num, vol_id, mode);
274 else
275 ret = ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400276
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200277 /*
278 * We should put the UBI device even in case of success, because
279 * 'ubi_open_volume()' took a reference as well.
280 */
281 ubi_put_device(ubi);
282 return ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400283}
284EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
285
286/**
Corentin Charyb5710282009-09-28 21:10:11 +0200287 * ubi_open_volume_path - open UBI volume by its character device node path.
288 * @pathname: volume character device node path
289 * @mode: open mode
290 *
291 * This function is similar to 'ubi_open_volume()', but opens a volume the path
292 * to its character device node.
293 */
294struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
295{
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200296 int error, ubi_num, vol_id, mod;
Corentin Charyb5710282009-09-28 21:10:11 +0200297 struct inode *inode;
298 struct path path;
299
300 dbg_gen("open volume %s, mode %d", pathname, mode);
301
302 if (!pathname || !*pathname)
303 return ERR_PTR(-EINVAL);
304
305 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
306 if (error)
307 return ERR_PTR(error);
308
309 inode = path.dentry->d_inode;
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200310 mod = inode->i_mode;
Corentin Charyb5710282009-09-28 21:10:11 +0200311 ubi_num = ubi_major2num(imajor(inode));
312 vol_id = iminor(inode) - 1;
Corentin Charyb5710282009-09-28 21:10:11 +0200313 path_put(&path);
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200314
315 if (!S_ISCHR(mod))
316 return ERR_PTR(-EINVAL);
317 if (vol_id >= 0 && ubi_num >= 0)
318 return ubi_open_volume(ubi_num, vol_id, mode);
319 return ERR_PTR(-ENODEV);
Corentin Charyb5710282009-09-28 21:10:11 +0200320}
321EXPORT_SYMBOL_GPL(ubi_open_volume_path);
322
323/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400324 * ubi_close_volume - close UBI volume.
325 * @desc: volume descriptor
326 */
327void ubi_close_volume(struct ubi_volume_desc *desc)
328{
329 struct ubi_volume *vol = desc->vol;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200330 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400331
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300332 dbg_gen("close device %d, volume %d, mode %d",
333 ubi->ubi_num, vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400334
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200335 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400336 switch (desc->mode) {
337 case UBI_READONLY:
338 vol->readers -= 1;
339 break;
340 case UBI_READWRITE:
341 vol->writers -= 1;
342 break;
343 case UBI_EXCLUSIVE:
344 vol->exclusive = 0;
345 }
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200346 vol->ref_count -= 1;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200347 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400348
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200349 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200350 put_device(&vol->dev);
351 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400352 module_put(THIS_MODULE);
353}
354EXPORT_SYMBOL_GPL(ubi_close_volume);
355
356/**
357 * ubi_leb_read - read data.
358 * @desc: volume descriptor
359 * @lnum: logical eraseblock number to read from
360 * @buf: buffer where to store the read data
361 * @offset: offset within the logical eraseblock to read from
362 * @len: how many bytes to read
363 * @check: whether UBI has to check the read data's CRC or not.
364 *
365 * This function reads data from offset @offset of logical eraseblock @lnum and
366 * stores the data at @buf. When reading from static volumes, @check specifies
367 * whether the data has to be checked or not. If yes, the whole logical
368 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
369 * checksum is per-eraseblock). So checking may substantially slow down the
370 * read speed. The @check argument is ignored for dynamic volumes.
371 *
372 * In case of success, this function returns zero. In case of failure, this
373 * function returns a negative error code.
374 *
375 * %-EBADMSG error code is returned:
376 * o for both static and dynamic volumes if MTD driver has detected a data
377 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
378 * o for static volumes in case of data CRC mismatch.
379 *
380 * If the volume is damaged because of an interrupted update this function just
381 * returns immediately with %-EBADF error code.
382 */
383int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
384 int len, int check)
385{
386 struct ubi_volume *vol = desc->vol;
387 struct ubi_device *ubi = vol->ubi;
388 int err, vol_id = vol->vol_id;
389
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300390 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400391
392 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
393 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
394 offset + len > vol->usable_leb_size)
395 return -EINVAL;
396
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300397 if (vol->vol_type == UBI_STATIC_VOLUME) {
398 if (vol->used_ebs == 0)
399 /* Empty static UBI volume */
400 return 0;
401 if (lnum == vol->used_ebs - 1 &&
402 offset + len > vol->last_eb_bytes)
403 return -EINVAL;
404 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400405
406 if (vol->upd_marker)
407 return -EBADF;
408 if (len == 0)
409 return 0;
410
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200411 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400412 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
413 ubi_warn("mark volume %d as corrupted", vol_id);
414 vol->corrupted = 1;
415 }
416
417 return err;
418}
419EXPORT_SYMBOL_GPL(ubi_leb_read);
420
421/**
422 * ubi_leb_write - write data.
423 * @desc: volume descriptor
424 * @lnum: logical eraseblock number to write to
425 * @buf: data to write
426 * @offset: offset within the logical eraseblock where to write
427 * @len: how many bytes to write
428 * @dtype: expected data type
429 *
430 * This function writes @len bytes of data from @buf to offset @offset of
431 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
432 * the data.
433 *
434 * This function takes care of physical eraseblock write failures. If write to
435 * the physical eraseblock write operation fails, the logical eraseblock is
436 * re-mapped to another physical eraseblock, the data is recovered, and the
437 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
438 *
439 * If all the data were successfully written, zero is returned. If an error
440 * occurred and UBI has not been able to recover from it, this function returns
441 * a negative error code. Note, in case of an error, it is possible that
442 * something was still written to the flash media, but that may be some
443 * garbage.
444 *
445 * If the volume is damaged because of an interrupted update this function just
446 * returns immediately with %-EBADF code.
447 */
448int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
449 int offset, int len, int dtype)
450{
451 struct ubi_volume *vol = desc->vol;
452 struct ubi_device *ubi = vol->ubi;
453 int vol_id = vol->vol_id;
454
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300455 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400456
457 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
458 return -EINVAL;
459
460 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
461 return -EROFS;
462
463 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900464 offset + len > vol->usable_leb_size ||
465 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400466 return -EINVAL;
467
468 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
469 dtype != UBI_UNKNOWN)
470 return -EINVAL;
471
472 if (vol->upd_marker)
473 return -EBADF;
474
475 if (len == 0)
476 return 0;
477
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200478 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400479}
480EXPORT_SYMBOL_GPL(ubi_leb_write);
481
482/*
483 * ubi_leb_change - change logical eraseblock atomically.
484 * @desc: volume descriptor
485 * @lnum: logical eraseblock number to change
486 * @buf: data to write
487 * @len: how many bytes to write
488 * @dtype: expected data type
489 *
490 * This function changes the contents of a logical eraseblock atomically. @buf
491 * has to contain new logical eraseblock data, and @len - the length of the
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900492 * data, which has to be aligned. The length may be shorter than the logical
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400493 * eraseblock size, ant the logical eraseblock may be appended to more times
494 * later on. This function guarantees that in case of an unclean reboot the old
495 * contents is preserved. Returns zero in case of success and a negative error
496 * code in case of failure.
497 */
498int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
499 int len, int dtype)
500{
501 struct ubi_volume *vol = desc->vol;
502 struct ubi_device *ubi = vol->ubi;
503 int vol_id = vol->vol_id;
504
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300505 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400506
507 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
508 return -EINVAL;
509
510 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
511 return -EROFS;
512
513 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900514 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 return -EINVAL;
516
517 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
518 dtype != UBI_UNKNOWN)
519 return -EINVAL;
520
521 if (vol->upd_marker)
522 return -EBADF;
523
524 if (len == 0)
525 return 0;
526
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200527 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400528}
529EXPORT_SYMBOL_GPL(ubi_leb_change);
530
531/**
532 * ubi_leb_erase - erase logical eraseblock.
533 * @desc: volume descriptor
534 * @lnum: logical eraseblock number
535 *
536 * This function un-maps logical eraseblock @lnum and synchronously erases the
537 * correspondent physical eraseblock. Returns zero in case of success and a
538 * negative error code in case of failure.
539 *
540 * If the volume is damaged because of an interrupted update this function just
541 * returns immediately with %-EBADF code.
542 */
543int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
544{
545 struct ubi_volume *vol = desc->vol;
546 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200547 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400548
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300549 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400550
551 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
552 return -EROFS;
553
554 if (lnum < 0 || lnum >= vol->reserved_pebs)
555 return -EINVAL;
556
557 if (vol->upd_marker)
558 return -EBADF;
559
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200560 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400561 if (err)
562 return err;
563
564 return ubi_wl_flush(ubi);
565}
566EXPORT_SYMBOL_GPL(ubi_leb_erase);
567
568/**
569 * ubi_leb_unmap - un-map logical eraseblock.
570 * @desc: volume descriptor
571 * @lnum: logical eraseblock number
572 *
573 * This function un-maps logical eraseblock @lnum and schedules the
574 * corresponding physical eraseblock for erasure, so that it will eventually be
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900575 * physically erased in background. This operation is much faster than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400576 * erase operation.
577 *
578 * Unlike erase, the un-map operation does not guarantee that the logical
579 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
580 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
581 * happens after this, the logical eraseblocks will not necessarily be
582 * un-mapped again when this MTD device is attached. They may actually be
583 * mapped to the same physical eraseblocks again. So, this function has to be
584 * used with care.
585 *
586 * In other words, when un-mapping a logical eraseblock, UBI does not store
587 * any information about this on the flash media, it just marks the logical
588 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
589 * eraseblock is physically erased, it will be mapped again to the same logical
590 * eraseblock when the MTD device is attached again.
591 *
592 * The main and obvious use-case of this function is when the contents of a
593 * logical eraseblock has to be re-written. Then it is much more efficient to
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900594 * first un-map it, then write new data, rather than first erase it, then write
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400595 * new data. Note, once new data has been written to the logical eraseblock,
596 * UBI guarantees that the old contents has gone forever. In other words, if an
597 * unclean reboot happens after the logical eraseblock has been un-mapped and
598 * then written to, it will contain the last written data.
599 *
600 * This function returns zero in case of success and a negative error code in
601 * case of failure. If the volume is damaged because of an interrupted update
602 * this function just returns immediately with %-EBADF code.
603 */
604int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
605{
606 struct ubi_volume *vol = desc->vol;
607 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400608
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300609 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610
611 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
612 return -EROFS;
613
614 if (lnum < 0 || lnum >= vol->reserved_pebs)
615 return -EINVAL;
616
617 if (vol->upd_marker)
618 return -EBADF;
619
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200620 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400621}
622EXPORT_SYMBOL_GPL(ubi_leb_unmap);
623
624/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400625 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200626 * @desc: volume descriptor
627 * @lnum: logical eraseblock number
628 * @dtype: expected data type
629 *
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 */
641int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
642{
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
654 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
655 dtype != UBI_UNKNOWN)
656 return -EINVAL;
657
658 if (vol->upd_marker)
659 return -EBADF;
660
661 if (vol->eba_tbl[lnum] >= 0)
662 return -EBADMSG;
663
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200664 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200665}
666EXPORT_SYMBOL_GPL(ubi_leb_map);
667
668/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 * ubi_is_mapped - check if logical eraseblock is mapped.
670 * @desc: volume descriptor
671 * @lnum: logical eraseblock number
672 *
673 * This function checks if logical eraseblock @lnum is mapped to a physical
674 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
675 * mean it will still be un-mapped after the UBI device is re-attached. The
676 * logical eraseblock may become mapped to the physical eraseblock it was last
677 * mapped to.
678 *
679 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
680 * error code in case of failure. If the volume is damaged because of an
681 * interrupted update this function just returns immediately with %-EBADF error
682 * code.
683 */
684int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
685{
686 struct ubi_volume *vol = desc->vol;
687
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300688 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400689
690 if (lnum < 0 || lnum >= vol->reserved_pebs)
691 return -EINVAL;
692
693 if (vol->upd_marker)
694 return -EBADF;
695
696 return vol->eba_tbl[lnum] >= 0;
697}
698EXPORT_SYMBOL_GPL(ubi_is_mapped);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300699
700/**
701 * ubi_sync - synchronize UBI device buffers.
702 * @ubi_num: UBI device to synchronize
703 *
704 * The underlying MTD device may cache data in hardware or in software. This
705 * function ensures the caches are flushed. Returns zero in case of success and
706 * a negative error code in case of failure.
707 */
708int ubi_sync(int ubi_num)
709{
710 struct ubi_device *ubi;
711
712 ubi = ubi_get_device(ubi_num);
713 if (!ubi)
714 return -ENODEV;
715
716 if (ubi->mtd->sync)
717 ubi->mtd->sync(ubi->mtd);
718
719 ubi_put_device(ubi);
720 return 0;
721}
722EXPORT_SYMBOL_GPL(ubi_sync);
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400723
724BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
725
726/**
727 * ubi_register_volume_notifier - register a volume notifier.
728 * @nb: the notifier description object
729 * @ignore_existing: if non-zero, do not send "added" notification for all
730 * already existing volumes
731 *
732 * This function registers a volume notifier, which means that
733 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
734 * removed, re-sized, re-named, or updated. The first argument of the function
735 * is the notification type. The second argument is pointer to a
736 * &struct ubi_notification object which describes the notification event.
737 * Using UBI API from the volume notifier is prohibited.
738 *
739 * This function returns zero in case of success and a negative error code
740 * in case of failure.
741 */
742int ubi_register_volume_notifier(struct notifier_block *nb,
743 int ignore_existing)
744{
745 int err;
746
747 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
748 if (err != 0)
749 return err;
750 if (ignore_existing)
751 return 0;
752
753 /*
754 * We are going to walk all UBI devices and all volumes, and
755 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
756 * event. We have to lock the @ubi_devices_mutex to make sure UBI
757 * devices do not disappear.
758 */
759 mutex_lock(&ubi_devices_mutex);
760 ubi_enumerate_volumes(nb);
761 mutex_unlock(&ubi_devices_mutex);
762
763 return err;
764}
765EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
766
767/**
768 * ubi_unregister_volume_notifier - unregister the volume notifier.
769 * @nb: the notifier description object
770 *
771 * This function unregisters volume notifier @nm and returns zero in case of
772 * success and a negative error code in case of failure.
773 */
774int ubi_unregister_volume_notifier(struct notifier_block *nb)
775{
776 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
777}
778EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);