blob: 1361574e2b00157bb2ab98fcc1be6254b1e002f9 [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>
Corentin Charyb5710282009-09-28 21:10:11 +020025#include <linux/namei.h>
26#include <linux/fs.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040027#include <asm/div64.h>
28#include "ubi.h"
29
30/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040031 * ubi_do_get_device_info - get information about UBI device.
32 * @ubi: UBI device description object
33 * @di: the information is stored here
34 *
35 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
36 * device is locked and cannot disappear.
37 */
38void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
39{
40 di->ubi_num = ubi->ubi_num;
41 di->leb_size = ubi->leb_size;
42 di->min_io_size = ubi->min_io_size;
43 di->ro_mode = ubi->ro_mode;
44 di->cdev = ubi->cdev.dev;
45}
46EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
47
48/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040049 * ubi_get_device_info - get information about UBI device.
50 * @ubi_num: UBI device number
51 * @di: the information is stored here
52 *
Artem Bityutskiye73f4452007-12-17 17:37:26 +020053 * This function returns %0 in case of success, %-EINVAL if the UBI device
54 * number is invalid, and %-ENODEV if there is no such UBI device.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040055 */
56int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
57{
Artem Bityutskiye73f4452007-12-17 17:37:26 +020058 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040059
Artem Bityutskiye73f4452007-12-17 17:37:26 +020060 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
61 return -EINVAL;
Artem Bityutskiye73f4452007-12-17 17:37:26 +020062 ubi = ubi_get_device(ubi_num);
63 if (!ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040064 return -ENODEV;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040065 ubi_do_get_device_info(ubi, di);
Artem Bityutskiye73f4452007-12-17 17:37:26 +020066 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040067 return 0;
68}
69EXPORT_SYMBOL_GPL(ubi_get_device_info);
70
71/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040072 * ubi_do_get_volume_info - get information about UBI volume.
73 * @ubi: UBI device description object
74 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040075 * @vi: the information is stored here
76 */
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040077void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
78 struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040079{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040080 vi->vol_id = vol->vol_id;
81 vi->ubi_num = ubi->ubi_num;
82 vi->size = vol->reserved_pebs;
83 vi->used_bytes = vol->used_bytes;
84 vi->vol_type = vol->vol_type;
85 vi->corrupted = vol->corrupted;
86 vi->upd_marker = vol->upd_marker;
87 vi->alignment = vol->alignment;
88 vi->usable_leb_size = vol->usable_leb_size;
89 vi->name_len = vol->name_len;
90 vi->name = vol->name;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020091 vi->cdev = vol->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040092}
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040093
94/**
95 * ubi_get_volume_info - get information about UBI volume.
96 * @desc: volume descriptor
97 * @vi: the information is stored here
98 */
99void ubi_get_volume_info(struct ubi_volume_desc *desc,
100 struct ubi_volume_info *vi)
101{
102 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
103}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400104EXPORT_SYMBOL_GPL(ubi_get_volume_info);
105
106/**
107 * ubi_open_volume - open UBI volume.
108 * @ubi_num: UBI device number
109 * @vol_id: volume ID
110 * @mode: open mode
111 *
112 * The @mode parameter specifies if the volume should be opened in read-only
113 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
114 * nobody else will be able to open this volume. UBI allows to have many volume
115 * readers and one writer at a time.
116 *
117 * If a static volume is being opened for the first time since boot, it will be
118 * checked by this function, which means it will be fully read and the CRC
119 * checksum of each logical eraseblock will be checked.
120 *
121 * This function returns volume descriptor in case of success and a negative
122 * error code in case of failure.
123 */
124struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
125{
126 int err;
127 struct ubi_volume_desc *desc;
Jesper Juhl0169b492007-08-04 01:25:26 +0200128 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400129 struct ubi_volume *vol;
130
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300131 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400132
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200133 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
134 return ERR_PTR(-EINVAL);
Jesper Juhl0169b492007-08-04 01:25:26 +0200135
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400136 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
137 mode != UBI_EXCLUSIVE)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200138 return ERR_PTR(-EINVAL);
139
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200140 /*
141 * First of all, we have to get the UBI device to prevent its removal.
142 */
143 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200144 if (!ubi)
145 return ERR_PTR(-ENODEV);
146
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200147 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
148 err = -EINVAL;
149 goto out_put_ubi;
150 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400151
152 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200153 if (!desc) {
154 err = -ENOMEM;
155 goto out_put_ubi;
156 }
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200157
158 err = -ENODEV;
159 if (!try_module_get(THIS_MODULE))
160 goto out_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400161
162 spin_lock(&ubi->volumes_lock);
163 vol = ubi->volumes[vol_id];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200164 if (!vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400165 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400166
167 err = -EBUSY;
168 switch (mode) {
169 case UBI_READONLY:
170 if (vol->exclusive)
171 goto out_unlock;
172 vol->readers += 1;
173 break;
174
175 case UBI_READWRITE:
176 if (vol->exclusive || vol->writers > 0)
177 goto out_unlock;
178 vol->writers += 1;
179 break;
180
181 case UBI_EXCLUSIVE:
182 if (vol->exclusive || vol->writers || vol->readers)
183 goto out_unlock;
184 vol->exclusive = 1;
185 break;
186 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200187 get_device(&vol->dev);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200188 vol->ref_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400189 spin_unlock(&ubi->volumes_lock);
190
191 desc->vol = vol;
192 desc->mode = mode;
193
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200194 mutex_lock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400195 if (!vol->checked) {
196 /* This is the first open - check the volume */
197 err = ubi_check_volume(ubi, vol_id);
198 if (err < 0) {
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200199 mutex_unlock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400200 ubi_close_volume(desc);
201 return ERR_PTR(err);
202 }
203 if (err == 1) {
204 ubi_warn("volume %d on UBI device %d is corrupted",
205 vol_id, ubi->ubi_num);
206 vol->corrupted = 1;
207 }
208 vol->checked = 1;
209 }
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200210 mutex_unlock(&ubi->ckvol_mutex);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200211
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400212 return desc;
213
214out_unlock:
215 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 module_put(THIS_MODULE);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200217out_free:
218 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200219out_put_ubi:
220 ubi_put_device(ubi);
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300221 dbg_err("cannot open device %d, volume %d, error %d",
222 ubi_num, vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400223 return ERR_PTR(err);
224}
225EXPORT_SYMBOL_GPL(ubi_open_volume);
226
227/**
228 * ubi_open_volume_nm - open UBI volume by name.
229 * @ubi_num: UBI device number
230 * @name: volume name
231 * @mode: open mode
232 *
233 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
234 */
235struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
236 int mode)
237{
238 int i, vol_id = -1, len;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239 struct ubi_device *ubi;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200240 struct ubi_volume_desc *ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400241
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300242 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400243
244 if (!name)
245 return ERR_PTR(-EINVAL);
246
247 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
248 if (len > UBI_VOL_NAME_MAX)
249 return ERR_PTR(-EINVAL);
250
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200251 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
252 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200254 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200255 if (!ubi)
256 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400257
258 spin_lock(&ubi->volumes_lock);
259 /* Walk all volumes of this UBI device */
260 for (i = 0; i < ubi->vtbl_slots; i++) {
261 struct ubi_volume *vol = ubi->volumes[i];
262
263 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
264 vol_id = i;
265 break;
266 }
267 }
268 spin_unlock(&ubi->volumes_lock);
269
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200270 if (vol_id >= 0)
271 ret = ubi_open_volume(ubi_num, vol_id, mode);
272 else
273 ret = ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400274
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200275 /*
276 * We should put the UBI device even in case of success, because
277 * 'ubi_open_volume()' took a reference as well.
278 */
279 ubi_put_device(ubi);
280 return ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400281}
282EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
283
284/**
Corentin Charyb5710282009-09-28 21:10:11 +0200285 * ubi_open_volume_path - open UBI volume by its character device node path.
286 * @pathname: volume character device node path
287 * @mode: open mode
288 *
289 * This function is similar to 'ubi_open_volume()', but opens a volume the path
290 * to its character device node.
291 */
292struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
293{
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200294 int error, ubi_num, vol_id, mod;
Corentin Charyb5710282009-09-28 21:10:11 +0200295 struct inode *inode;
296 struct path path;
297
298 dbg_gen("open volume %s, mode %d", pathname, mode);
299
300 if (!pathname || !*pathname)
301 return ERR_PTR(-EINVAL);
302
303 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
304 if (error)
305 return ERR_PTR(error);
306
307 inode = path.dentry->d_inode;
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200308 mod = inode->i_mode;
Corentin Charyb5710282009-09-28 21:10:11 +0200309 ubi_num = ubi_major2num(imajor(inode));
310 vol_id = iminor(inode) - 1;
Corentin Charyb5710282009-09-28 21:10:11 +0200311 path_put(&path);
Artem Bityutskiyb531b552010-01-05 17:25:59 +0200312
313 if (!S_ISCHR(mod))
314 return ERR_PTR(-EINVAL);
315 if (vol_id >= 0 && ubi_num >= 0)
316 return ubi_open_volume(ubi_num, vol_id, mode);
317 return ERR_PTR(-ENODEV);
Corentin Charyb5710282009-09-28 21:10:11 +0200318}
319EXPORT_SYMBOL_GPL(ubi_open_volume_path);
320
321/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400322 * ubi_close_volume - close UBI volume.
323 * @desc: volume descriptor
324 */
325void ubi_close_volume(struct ubi_volume_desc *desc)
326{
327 struct ubi_volume *vol = desc->vol;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200328 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300330 dbg_gen("close device %d, volume %d, mode %d",
331 ubi->ubi_num, vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200333 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400334 switch (desc->mode) {
335 case UBI_READONLY:
336 vol->readers -= 1;
337 break;
338 case UBI_READWRITE:
339 vol->writers -= 1;
340 break;
341 case UBI_EXCLUSIVE:
342 vol->exclusive = 0;
343 }
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200344 vol->ref_count -= 1;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200345 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400346
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200347 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200348 put_device(&vol->dev);
349 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400350 module_put(THIS_MODULE);
351}
352EXPORT_SYMBOL_GPL(ubi_close_volume);
353
354/**
355 * ubi_leb_read - read data.
356 * @desc: volume descriptor
357 * @lnum: logical eraseblock number to read from
358 * @buf: buffer where to store the read data
359 * @offset: offset within the logical eraseblock to read from
360 * @len: how many bytes to read
361 * @check: whether UBI has to check the read data's CRC or not.
362 *
363 * This function reads data from offset @offset of logical eraseblock @lnum and
364 * stores the data at @buf. When reading from static volumes, @check specifies
365 * whether the data has to be checked or not. If yes, the whole logical
366 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
367 * checksum is per-eraseblock). So checking may substantially slow down the
368 * read speed. The @check argument is ignored for dynamic volumes.
369 *
370 * In case of success, this function returns zero. In case of failure, this
371 * function returns a negative error code.
372 *
373 * %-EBADMSG error code is returned:
374 * o for both static and dynamic volumes if MTD driver has detected a data
375 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
376 * o for static volumes in case of data CRC mismatch.
377 *
378 * If the volume is damaged because of an interrupted update this function just
379 * returns immediately with %-EBADF error code.
380 */
381int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
382 int len, int check)
383{
384 struct ubi_volume *vol = desc->vol;
385 struct ubi_device *ubi = vol->ubi;
386 int err, vol_id = vol->vol_id;
387
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300388 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400389
390 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
391 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
392 offset + len > vol->usable_leb_size)
393 return -EINVAL;
394
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300395 if (vol->vol_type == UBI_STATIC_VOLUME) {
396 if (vol->used_ebs == 0)
397 /* Empty static UBI volume */
398 return 0;
399 if (lnum == vol->used_ebs - 1 &&
400 offset + len > vol->last_eb_bytes)
401 return -EINVAL;
402 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400403
404 if (vol->upd_marker)
405 return -EBADF;
406 if (len == 0)
407 return 0;
408
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200409 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400410 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
411 ubi_warn("mark volume %d as corrupted", vol_id);
412 vol->corrupted = 1;
413 }
414
415 return err;
416}
417EXPORT_SYMBOL_GPL(ubi_leb_read);
418
419/**
420 * ubi_leb_write - write data.
421 * @desc: volume descriptor
422 * @lnum: logical eraseblock number to write to
423 * @buf: data to write
424 * @offset: offset within the logical eraseblock where to write
425 * @len: how many bytes to write
426 * @dtype: expected data type
427 *
428 * This function writes @len bytes of data from @buf to offset @offset of
429 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
430 * the data.
431 *
432 * This function takes care of physical eraseblock write failures. If write to
433 * the physical eraseblock write operation fails, the logical eraseblock is
434 * re-mapped to another physical eraseblock, the data is recovered, and the
435 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
436 *
437 * If all the data were successfully written, zero is returned. If an error
438 * occurred and UBI has not been able to recover from it, this function returns
439 * a negative error code. Note, in case of an error, it is possible that
440 * something was still written to the flash media, but that may be some
441 * garbage.
442 *
443 * If the volume is damaged because of an interrupted update this function just
444 * returns immediately with %-EBADF code.
445 */
446int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
447 int offset, int len, int dtype)
448{
449 struct ubi_volume *vol = desc->vol;
450 struct ubi_device *ubi = vol->ubi;
451 int vol_id = vol->vol_id;
452
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300453 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400454
455 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
456 return -EINVAL;
457
458 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
459 return -EROFS;
460
461 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900462 offset + len > vol->usable_leb_size ||
463 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400464 return -EINVAL;
465
466 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
467 dtype != UBI_UNKNOWN)
468 return -EINVAL;
469
470 if (vol->upd_marker)
471 return -EBADF;
472
473 if (len == 0)
474 return 0;
475
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200476 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400477}
478EXPORT_SYMBOL_GPL(ubi_leb_write);
479
480/*
481 * ubi_leb_change - change logical eraseblock atomically.
482 * @desc: volume descriptor
483 * @lnum: logical eraseblock number to change
484 * @buf: data to write
485 * @len: how many bytes to write
486 * @dtype: expected data type
487 *
488 * This function changes the contents of a logical eraseblock atomically. @buf
489 * has to contain new logical eraseblock data, and @len - the length of the
490 * data, which has to be aligned. The length may be shorter then the logical
491 * eraseblock size, ant the logical eraseblock may be appended to more times
492 * later on. This function guarantees that in case of an unclean reboot the old
493 * contents is preserved. Returns zero in case of success and a negative error
494 * code in case of failure.
495 */
496int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
497 int len, int dtype)
498{
499 struct ubi_volume *vol = desc->vol;
500 struct ubi_device *ubi = vol->ubi;
501 int vol_id = vol->vol_id;
502
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300503 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400504
505 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
506 return -EINVAL;
507
508 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
509 return -EROFS;
510
511 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900512 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400513 return -EINVAL;
514
515 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
516 dtype != UBI_UNKNOWN)
517 return -EINVAL;
518
519 if (vol->upd_marker)
520 return -EBADF;
521
522 if (len == 0)
523 return 0;
524
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200525 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400526}
527EXPORT_SYMBOL_GPL(ubi_leb_change);
528
529/**
530 * ubi_leb_erase - erase logical eraseblock.
531 * @desc: volume descriptor
532 * @lnum: logical eraseblock number
533 *
534 * This function un-maps logical eraseblock @lnum and synchronously erases the
535 * correspondent physical eraseblock. Returns zero in case of success and a
536 * negative error code in case of failure.
537 *
538 * If the volume is damaged because of an interrupted update this function just
539 * returns immediately with %-EBADF code.
540 */
541int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
542{
543 struct ubi_volume *vol = desc->vol;
544 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200545 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400546
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300547 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400548
549 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
550 return -EROFS;
551
552 if (lnum < 0 || lnum >= vol->reserved_pebs)
553 return -EINVAL;
554
555 if (vol->upd_marker)
556 return -EBADF;
557
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200558 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400559 if (err)
560 return err;
561
562 return ubi_wl_flush(ubi);
563}
564EXPORT_SYMBOL_GPL(ubi_leb_erase);
565
566/**
567 * ubi_leb_unmap - un-map logical eraseblock.
568 * @desc: volume descriptor
569 * @lnum: logical eraseblock number
570 *
571 * This function un-maps logical eraseblock @lnum and schedules the
572 * corresponding physical eraseblock for erasure, so that it will eventually be
573 * physically erased in background. This operation is much faster then the
574 * erase operation.
575 *
576 * Unlike erase, the un-map operation does not guarantee that the logical
577 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
578 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
579 * happens after this, the logical eraseblocks will not necessarily be
580 * un-mapped again when this MTD device is attached. They may actually be
581 * mapped to the same physical eraseblocks again. So, this function has to be
582 * used with care.
583 *
584 * In other words, when un-mapping a logical eraseblock, UBI does not store
585 * any information about this on the flash media, it just marks the logical
586 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
587 * eraseblock is physically erased, it will be mapped again to the same logical
588 * eraseblock when the MTD device is attached again.
589 *
590 * The main and obvious use-case of this function is when the contents of a
591 * logical eraseblock has to be re-written. Then it is much more efficient to
592 * first un-map it, then write new data, rather then first erase it, then write
593 * new data. Note, once new data has been written to the logical eraseblock,
594 * UBI guarantees that the old contents has gone forever. In other words, if an
595 * unclean reboot happens after the logical eraseblock has been un-mapped and
596 * then written to, it will contain the last written data.
597 *
598 * This function returns zero in case of success and a negative error code in
599 * case of failure. If the volume is damaged because of an interrupted update
600 * this function just returns immediately with %-EBADF code.
601 */
602int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
603{
604 struct ubi_volume *vol = desc->vol;
605 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400606
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300607 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400608
609 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
610 return -EROFS;
611
612 if (lnum < 0 || lnum >= vol->reserved_pebs)
613 return -EINVAL;
614
615 if (vol->upd_marker)
616 return -EBADF;
617
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200618 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400619}
620EXPORT_SYMBOL_GPL(ubi_leb_unmap);
621
622/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400623 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200624 * @desc: volume descriptor
625 * @lnum: logical eraseblock number
626 * @dtype: expected data type
627 *
628 * This function maps an un-mapped logical eraseblock @lnum to a physical
Coly Li73ac36e2009-01-07 18:09:16 -0800629 * eraseblock. This means, that after a successful invocation of this
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200630 * function the logical eraseblock @lnum will be empty (contain only %0xFF
631 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
632 * happens.
633 *
634 * This function returns zero in case of success, %-EBADF if the volume is
635 * damaged because of an interrupted update, %-EBADMSG if the logical
636 * eraseblock is already mapped, and other negative error codes in case of
637 * other failures.
638 */
639int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
640{
641 struct ubi_volume *vol = desc->vol;
642 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200643
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300644 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200645
646 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
647 return -EROFS;
648
649 if (lnum < 0 || lnum >= vol->reserved_pebs)
650 return -EINVAL;
651
652 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
653 dtype != UBI_UNKNOWN)
654 return -EINVAL;
655
656 if (vol->upd_marker)
657 return -EBADF;
658
659 if (vol->eba_tbl[lnum] >= 0)
660 return -EBADMSG;
661
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200662 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200663}
664EXPORT_SYMBOL_GPL(ubi_leb_map);
665
666/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400667 * ubi_is_mapped - check if logical eraseblock is mapped.
668 * @desc: volume descriptor
669 * @lnum: logical eraseblock number
670 *
671 * This function checks if logical eraseblock @lnum is mapped to a physical
672 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
673 * mean it will still be un-mapped after the UBI device is re-attached. The
674 * logical eraseblock may become mapped to the physical eraseblock it was last
675 * mapped to.
676 *
677 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
678 * error code in case of failure. If the volume is damaged because of an
679 * interrupted update this function just returns immediately with %-EBADF error
680 * code.
681 */
682int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
683{
684 struct ubi_volume *vol = desc->vol;
685
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300686 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400687
688 if (lnum < 0 || lnum >= vol->reserved_pebs)
689 return -EINVAL;
690
691 if (vol->upd_marker)
692 return -EBADF;
693
694 return vol->eba_tbl[lnum] >= 0;
695}
696EXPORT_SYMBOL_GPL(ubi_is_mapped);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300697
698/**
699 * ubi_sync - synchronize UBI device buffers.
700 * @ubi_num: UBI device to synchronize
701 *
702 * The underlying MTD device may cache data in hardware or in software. This
703 * function ensures the caches are flushed. Returns zero in case of success and
704 * a negative error code in case of failure.
705 */
706int ubi_sync(int ubi_num)
707{
708 struct ubi_device *ubi;
709
710 ubi = ubi_get_device(ubi_num);
711 if (!ubi)
712 return -ENODEV;
713
714 if (ubi->mtd->sync)
715 ubi->mtd->sync(ubi->mtd);
716
717 ubi_put_device(ubi);
718 return 0;
719}
720EXPORT_SYMBOL_GPL(ubi_sync);
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400721
722BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
723
724/**
725 * ubi_register_volume_notifier - register a volume notifier.
726 * @nb: the notifier description object
727 * @ignore_existing: if non-zero, do not send "added" notification for all
728 * already existing volumes
729 *
730 * This function registers a volume notifier, which means that
731 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
732 * removed, re-sized, re-named, or updated. The first argument of the function
733 * is the notification type. The second argument is pointer to a
734 * &struct ubi_notification object which describes the notification event.
735 * Using UBI API from the volume notifier is prohibited.
736 *
737 * This function returns zero in case of success and a negative error code
738 * in case of failure.
739 */
740int ubi_register_volume_notifier(struct notifier_block *nb,
741 int ignore_existing)
742{
743 int err;
744
745 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
746 if (err != 0)
747 return err;
748 if (ignore_existing)
749 return 0;
750
751 /*
752 * We are going to walk all UBI devices and all volumes, and
753 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
754 * event. We have to lock the @ubi_devices_mutex to make sure UBI
755 * devices do not disappear.
756 */
757 mutex_lock(&ubi_devices_mutex);
758 ubi_enumerate_volumes(nb);
759 mutex_unlock(&ubi_devices_mutex);
760
761 return err;
762}
763EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
764
765/**
766 * ubi_unregister_volume_notifier - unregister the volume notifier.
767 * @nb: the notifier description object
768 *
769 * This function unregisters volume notifier @nm and returns zero in case of
770 * success and a negative error code in case of failure.
771 */
772int ubi_unregister_volume_notifier(struct notifier_block *nb)
773{
774 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
775}
776EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);