blob: 88a72e9c8beb1085b6d4a811b5b2fa965a954653 [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>
25#include <asm/div64.h>
26#include "ubi.h"
27
28/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040029 * ubi_do_get_device_info - get information about UBI device.
30 * @ubi: UBI device description object
31 * @di: the information is stored here
32 *
33 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
34 * device is locked and cannot disappear.
35 */
36void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
37{
38 di->ubi_num = ubi->ubi_num;
39 di->leb_size = ubi->leb_size;
40 di->min_io_size = ubi->min_io_size;
41 di->ro_mode = ubi->ro_mode;
42 di->cdev = ubi->cdev.dev;
43}
44EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
45
46/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040047 * ubi_get_device_info - get information about UBI device.
48 * @ubi_num: UBI device number
49 * @di: the information is stored here
50 *
Artem Bityutskiye73f4452007-12-17 17:37:26 +020051 * This function returns %0 in case of success, %-EINVAL if the UBI device
52 * number is invalid, and %-ENODEV if there is no such UBI device.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040053 */
54int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
55{
Artem Bityutskiye73f4452007-12-17 17:37:26 +020056 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040057
Artem Bityutskiye73f4452007-12-17 17:37:26 +020058 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
59 return -EINVAL;
Artem Bityutskiye73f4452007-12-17 17:37:26 +020060 ubi = ubi_get_device(ubi_num);
61 if (!ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040062 return -ENODEV;
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040063 ubi_do_get_device_info(ubi, di);
Artem Bityutskiye73f4452007-12-17 17:37:26 +020064 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040065 return 0;
66}
67EXPORT_SYMBOL_GPL(ubi_get_device_info);
68
69/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040070 * ubi_do_get_volume_info - get information about UBI volume.
71 * @ubi: UBI device description object
72 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040073 * @vi: the information is stored here
74 */
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040075void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
76 struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040077{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040078 vi->vol_id = vol->vol_id;
79 vi->ubi_num = ubi->ubi_num;
80 vi->size = vol->reserved_pebs;
81 vi->used_bytes = vol->used_bytes;
82 vi->vol_type = vol->vol_type;
83 vi->corrupted = vol->corrupted;
84 vi->upd_marker = vol->upd_marker;
85 vi->alignment = vol->alignment;
86 vi->usable_leb_size = vol->usable_leb_size;
87 vi->name_len = vol->name_len;
88 vi->name = vol->name;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020089 vi->cdev = vol->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040090}
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +040091
92/**
93 * ubi_get_volume_info - get information about UBI volume.
94 * @desc: volume descriptor
95 * @vi: the information is stored here
96 */
97void ubi_get_volume_info(struct ubi_volume_desc *desc,
98 struct ubi_volume_info *vi)
99{
100 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
101}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400102EXPORT_SYMBOL_GPL(ubi_get_volume_info);
103
104/**
105 * ubi_open_volume - open UBI volume.
106 * @ubi_num: UBI device number
107 * @vol_id: volume ID
108 * @mode: open mode
109 *
110 * The @mode parameter specifies if the volume should be opened in read-only
111 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
112 * nobody else will be able to open this volume. UBI allows to have many volume
113 * readers and one writer at a time.
114 *
115 * If a static volume is being opened for the first time since boot, it will be
116 * checked by this function, which means it will be fully read and the CRC
117 * checksum of each logical eraseblock will be checked.
118 *
119 * This function returns volume descriptor in case of success and a negative
120 * error code in case of failure.
121 */
122struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
123{
124 int err;
125 struct ubi_volume_desc *desc;
Jesper Juhl0169b492007-08-04 01:25:26 +0200126 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400127 struct ubi_volume *vol;
128
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300129 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400130
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200131 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
132 return ERR_PTR(-EINVAL);
Jesper Juhl0169b492007-08-04 01:25:26 +0200133
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
135 mode != UBI_EXCLUSIVE)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200136 return ERR_PTR(-EINVAL);
137
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200138 /*
139 * First of all, we have to get the UBI device to prevent its removal.
140 */
141 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200142 if (!ubi)
143 return ERR_PTR(-ENODEV);
144
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200145 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
146 err = -EINVAL;
147 goto out_put_ubi;
148 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400149
150 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200151 if (!desc) {
152 err = -ENOMEM;
153 goto out_put_ubi;
154 }
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200155
156 err = -ENODEV;
157 if (!try_module_get(THIS_MODULE))
158 goto out_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400159
160 spin_lock(&ubi->volumes_lock);
161 vol = ubi->volumes[vol_id];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200162 if (!vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400163 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400164
165 err = -EBUSY;
166 switch (mode) {
167 case UBI_READONLY:
168 if (vol->exclusive)
169 goto out_unlock;
170 vol->readers += 1;
171 break;
172
173 case UBI_READWRITE:
174 if (vol->exclusive || vol->writers > 0)
175 goto out_unlock;
176 vol->writers += 1;
177 break;
178
179 case UBI_EXCLUSIVE:
180 if (vol->exclusive || vol->writers || vol->readers)
181 goto out_unlock;
182 vol->exclusive = 1;
183 break;
184 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200185 get_device(&vol->dev);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200186 vol->ref_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187 spin_unlock(&ubi->volumes_lock);
188
189 desc->vol = vol;
190 desc->mode = mode;
191
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200192 mutex_lock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400193 if (!vol->checked) {
194 /* This is the first open - check the volume */
195 err = ubi_check_volume(ubi, vol_id);
196 if (err < 0) {
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200197 mutex_unlock(&ubi->ckvol_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400198 ubi_close_volume(desc);
199 return ERR_PTR(err);
200 }
201 if (err == 1) {
202 ubi_warn("volume %d on UBI device %d is corrupted",
203 vol_id, ubi->ubi_num);
204 vol->corrupted = 1;
205 }
206 vol->checked = 1;
207 }
Artem Bityutskiy783b2732007-12-25 18:13:33 +0200208 mutex_unlock(&ubi->ckvol_mutex);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200209
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400210 return desc;
211
212out_unlock:
213 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400214 module_put(THIS_MODULE);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200215out_free:
216 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200217out_put_ubi:
218 ubi_put_device(ubi);
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300219 dbg_err("cannot open device %d, volume %d, error %d",
220 ubi_num, vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400221 return ERR_PTR(err);
222}
223EXPORT_SYMBOL_GPL(ubi_open_volume);
224
225/**
226 * ubi_open_volume_nm - open UBI volume by name.
227 * @ubi_num: UBI device number
228 * @name: volume name
229 * @mode: open mode
230 *
231 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
232 */
233struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
234 int mode)
235{
236 int i, vol_id = -1, len;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400237 struct ubi_device *ubi;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200238 struct ubi_volume_desc *ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300240 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400241
242 if (!name)
243 return ERR_PTR(-EINVAL);
244
245 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
246 if (len > UBI_VOL_NAME_MAX)
247 return ERR_PTR(-EINVAL);
248
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200249 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
250 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400251
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200252 ubi = ubi_get_device(ubi_num);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200253 if (!ubi)
254 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255
256 spin_lock(&ubi->volumes_lock);
257 /* Walk all volumes of this UBI device */
258 for (i = 0; i < ubi->vtbl_slots; i++) {
259 struct ubi_volume *vol = ubi->volumes[i];
260
261 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
262 vol_id = i;
263 break;
264 }
265 }
266 spin_unlock(&ubi->volumes_lock);
267
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200268 if (vol_id >= 0)
269 ret = ubi_open_volume(ubi_num, vol_id, mode);
270 else
271 ret = ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200273 /*
274 * We should put the UBI device even in case of success, because
275 * 'ubi_open_volume()' took a reference as well.
276 */
277 ubi_put_device(ubi);
278 return ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400279}
280EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
281
282/**
283 * ubi_close_volume - close UBI volume.
284 * @desc: volume descriptor
285 */
286void ubi_close_volume(struct ubi_volume_desc *desc)
287{
288 struct ubi_volume *vol = desc->vol;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200289 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400290
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300291 dbg_gen("close device %d, volume %d, mode %d",
292 ubi->ubi_num, vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400293
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200294 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400295 switch (desc->mode) {
296 case UBI_READONLY:
297 vol->readers -= 1;
298 break;
299 case UBI_READWRITE:
300 vol->writers -= 1;
301 break;
302 case UBI_EXCLUSIVE:
303 vol->exclusive = 0;
304 }
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200305 vol->ref_count -= 1;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200306 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400307
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200308 kfree(desc);
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200309 put_device(&vol->dev);
310 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400311 module_put(THIS_MODULE);
312}
313EXPORT_SYMBOL_GPL(ubi_close_volume);
314
315/**
316 * ubi_leb_read - read data.
317 * @desc: volume descriptor
318 * @lnum: logical eraseblock number to read from
319 * @buf: buffer where to store the read data
320 * @offset: offset within the logical eraseblock to read from
321 * @len: how many bytes to read
322 * @check: whether UBI has to check the read data's CRC or not.
323 *
324 * This function reads data from offset @offset of logical eraseblock @lnum and
325 * stores the data at @buf. When reading from static volumes, @check specifies
326 * whether the data has to be checked or not. If yes, the whole logical
327 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
328 * checksum is per-eraseblock). So checking may substantially slow down the
329 * read speed. The @check argument is ignored for dynamic volumes.
330 *
331 * In case of success, this function returns zero. In case of failure, this
332 * function returns a negative error code.
333 *
334 * %-EBADMSG error code is returned:
335 * o for both static and dynamic volumes if MTD driver has detected a data
336 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
337 * o for static volumes in case of data CRC mismatch.
338 *
339 * If the volume is damaged because of an interrupted update this function just
340 * returns immediately with %-EBADF error code.
341 */
342int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
343 int len, int check)
344{
345 struct ubi_volume *vol = desc->vol;
346 struct ubi_device *ubi = vol->ubi;
347 int err, vol_id = vol->vol_id;
348
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300349 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400350
351 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
352 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
353 offset + len > vol->usable_leb_size)
354 return -EINVAL;
355
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300356 if (vol->vol_type == UBI_STATIC_VOLUME) {
357 if (vol->used_ebs == 0)
358 /* Empty static UBI volume */
359 return 0;
360 if (lnum == vol->used_ebs - 1 &&
361 offset + len > vol->last_eb_bytes)
362 return -EINVAL;
363 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400364
365 if (vol->upd_marker)
366 return -EBADF;
367 if (len == 0)
368 return 0;
369
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200370 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400371 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
372 ubi_warn("mark volume %d as corrupted", vol_id);
373 vol->corrupted = 1;
374 }
375
376 return err;
377}
378EXPORT_SYMBOL_GPL(ubi_leb_read);
379
380/**
381 * ubi_leb_write - write data.
382 * @desc: volume descriptor
383 * @lnum: logical eraseblock number to write to
384 * @buf: data to write
385 * @offset: offset within the logical eraseblock where to write
386 * @len: how many bytes to write
387 * @dtype: expected data type
388 *
389 * This function writes @len bytes of data from @buf to offset @offset of
390 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
391 * the data.
392 *
393 * This function takes care of physical eraseblock write failures. If write to
394 * the physical eraseblock write operation fails, the logical eraseblock is
395 * re-mapped to another physical eraseblock, the data is recovered, and the
396 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
397 *
398 * If all the data were successfully written, zero is returned. If an error
399 * occurred and UBI has not been able to recover from it, this function returns
400 * a negative error code. Note, in case of an error, it is possible that
401 * something was still written to the flash media, but that may be some
402 * garbage.
403 *
404 * If the volume is damaged because of an interrupted update this function just
405 * returns immediately with %-EBADF code.
406 */
407int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
408 int offset, int len, int dtype)
409{
410 struct ubi_volume *vol = desc->vol;
411 struct ubi_device *ubi = vol->ubi;
412 int vol_id = vol->vol_id;
413
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300414 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415
416 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
417 return -EINVAL;
418
419 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
420 return -EROFS;
421
422 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900423 offset + len > vol->usable_leb_size ||
424 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400425 return -EINVAL;
426
427 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
428 dtype != UBI_UNKNOWN)
429 return -EINVAL;
430
431 if (vol->upd_marker)
432 return -EBADF;
433
434 if (len == 0)
435 return 0;
436
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200437 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400438}
439EXPORT_SYMBOL_GPL(ubi_leb_write);
440
441/*
442 * ubi_leb_change - change logical eraseblock atomically.
443 * @desc: volume descriptor
444 * @lnum: logical eraseblock number to change
445 * @buf: data to write
446 * @len: how many bytes to write
447 * @dtype: expected data type
448 *
449 * This function changes the contents of a logical eraseblock atomically. @buf
450 * has to contain new logical eraseblock data, and @len - the length of the
451 * data, which has to be aligned. The length may be shorter then the logical
452 * eraseblock size, ant the logical eraseblock may be appended to more times
453 * later on. This function guarantees that in case of an unclean reboot the old
454 * contents is preserved. Returns zero in case of success and a negative error
455 * code in case of failure.
456 */
457int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
458 int len, int dtype)
459{
460 struct ubi_volume *vol = desc->vol;
461 struct ubi_device *ubi = vol->ubi;
462 int vol_id = vol->vol_id;
463
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300464 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400465
466 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
467 return -EINVAL;
468
469 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
470 return -EROFS;
471
472 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900473 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400474 return -EINVAL;
475
476 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
477 dtype != UBI_UNKNOWN)
478 return -EINVAL;
479
480 if (vol->upd_marker)
481 return -EBADF;
482
483 if (len == 0)
484 return 0;
485
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200486 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400487}
488EXPORT_SYMBOL_GPL(ubi_leb_change);
489
490/**
491 * ubi_leb_erase - erase logical eraseblock.
492 * @desc: volume descriptor
493 * @lnum: logical eraseblock number
494 *
495 * This function un-maps logical eraseblock @lnum and synchronously erases the
496 * correspondent physical eraseblock. Returns zero in case of success and a
497 * negative error code in case of failure.
498 *
499 * If the volume is damaged because of an interrupted update this function just
500 * returns immediately with %-EBADF code.
501 */
502int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
503{
504 struct ubi_volume *vol = desc->vol;
505 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200506 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400507
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300508 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400509
510 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
511 return -EROFS;
512
513 if (lnum < 0 || lnum >= vol->reserved_pebs)
514 return -EINVAL;
515
516 if (vol->upd_marker)
517 return -EBADF;
518
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200519 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400520 if (err)
521 return err;
522
523 return ubi_wl_flush(ubi);
524}
525EXPORT_SYMBOL_GPL(ubi_leb_erase);
526
527/**
528 * ubi_leb_unmap - un-map logical eraseblock.
529 * @desc: volume descriptor
530 * @lnum: logical eraseblock number
531 *
532 * This function un-maps logical eraseblock @lnum and schedules the
533 * corresponding physical eraseblock for erasure, so that it will eventually be
534 * physically erased in background. This operation is much faster then the
535 * erase operation.
536 *
537 * Unlike erase, the un-map operation does not guarantee that the logical
538 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
539 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
540 * happens after this, the logical eraseblocks will not necessarily be
541 * un-mapped again when this MTD device is attached. They may actually be
542 * mapped to the same physical eraseblocks again. So, this function has to be
543 * used with care.
544 *
545 * In other words, when un-mapping a logical eraseblock, UBI does not store
546 * any information about this on the flash media, it just marks the logical
547 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
548 * eraseblock is physically erased, it will be mapped again to the same logical
549 * eraseblock when the MTD device is attached again.
550 *
551 * The main and obvious use-case of this function is when the contents of a
552 * logical eraseblock has to be re-written. Then it is much more efficient to
553 * first un-map it, then write new data, rather then first erase it, then write
554 * new data. Note, once new data has been written to the logical eraseblock,
555 * UBI guarantees that the old contents has gone forever. In other words, if an
556 * unclean reboot happens after the logical eraseblock has been un-mapped and
557 * then written to, it will contain the last written data.
558 *
559 * This function returns zero in case of success and a negative error code in
560 * case of failure. If the volume is damaged because of an interrupted update
561 * this function just returns immediately with %-EBADF code.
562 */
563int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
564{
565 struct ubi_volume *vol = desc->vol;
566 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400567
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300568 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400569
570 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
571 return -EROFS;
572
573 if (lnum < 0 || lnum >= vol->reserved_pebs)
574 return -EINVAL;
575
576 if (vol->upd_marker)
577 return -EBADF;
578
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200579 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400580}
581EXPORT_SYMBOL_GPL(ubi_leb_unmap);
582
583/**
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400584 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200585 * @desc: volume descriptor
586 * @lnum: logical eraseblock number
587 * @dtype: expected data type
588 *
589 * This function maps an un-mapped logical eraseblock @lnum to a physical
Coly Li73ac36e2009-01-07 18:09:16 -0800590 * eraseblock. This means, that after a successful invocation of this
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200591 * function the logical eraseblock @lnum will be empty (contain only %0xFF
592 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
593 * happens.
594 *
595 * This function returns zero in case of success, %-EBADF if the volume is
596 * damaged because of an interrupted update, %-EBADMSG if the logical
597 * eraseblock is already mapped, and other negative error codes in case of
598 * other failures.
599 */
600int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
601{
602 struct ubi_volume *vol = desc->vol;
603 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200604
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300605 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200606
607 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
608 return -EROFS;
609
610 if (lnum < 0 || lnum >= vol->reserved_pebs)
611 return -EINVAL;
612
613 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
614 dtype != UBI_UNKNOWN)
615 return -EINVAL;
616
617 if (vol->upd_marker)
618 return -EBADF;
619
620 if (vol->eba_tbl[lnum] >= 0)
621 return -EBADMSG;
622
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200623 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200624}
625EXPORT_SYMBOL_GPL(ubi_leb_map);
626
627/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400628 * ubi_is_mapped - check if logical eraseblock is mapped.
629 * @desc: volume descriptor
630 * @lnum: logical eraseblock number
631 *
632 * This function checks if logical eraseblock @lnum is mapped to a physical
633 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
634 * mean it will still be un-mapped after the UBI device is re-attached. The
635 * logical eraseblock may become mapped to the physical eraseblock it was last
636 * mapped to.
637 *
638 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
639 * error code in case of failure. If the volume is damaged because of an
640 * interrupted update this function just returns immediately with %-EBADF error
641 * code.
642 */
643int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
644{
645 struct ubi_volume *vol = desc->vol;
646
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300647 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400648
649 if (lnum < 0 || lnum >= vol->reserved_pebs)
650 return -EINVAL;
651
652 if (vol->upd_marker)
653 return -EBADF;
654
655 return vol->eba_tbl[lnum] >= 0;
656}
657EXPORT_SYMBOL_GPL(ubi_is_mapped);
Artem Bityutskiya5bf6192008-07-10 18:38:33 +0300658
659/**
660 * ubi_sync - synchronize UBI device buffers.
661 * @ubi_num: UBI device to synchronize
662 *
663 * The underlying MTD device may cache data in hardware or in software. This
664 * function ensures the caches are flushed. Returns zero in case of success and
665 * a negative error code in case of failure.
666 */
667int ubi_sync(int ubi_num)
668{
669 struct ubi_device *ubi;
670
671 ubi = ubi_get_device(ubi_num);
672 if (!ubi)
673 return -ENODEV;
674
675 if (ubi->mtd->sync)
676 ubi->mtd->sync(ubi->mtd);
677
678 ubi_put_device(ubi);
679 return 0;
680}
681EXPORT_SYMBOL_GPL(ubi_sync);
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400682
683BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
684
685/**
686 * ubi_register_volume_notifier - register a volume notifier.
687 * @nb: the notifier description object
688 * @ignore_existing: if non-zero, do not send "added" notification for all
689 * already existing volumes
690 *
691 * This function registers a volume notifier, which means that
692 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
693 * removed, re-sized, re-named, or updated. The first argument of the function
694 * is the notification type. The second argument is pointer to a
695 * &struct ubi_notification object which describes the notification event.
696 * Using UBI API from the volume notifier is prohibited.
697 *
698 * This function returns zero in case of success and a negative error code
699 * in case of failure.
700 */
701int ubi_register_volume_notifier(struct notifier_block *nb,
702 int ignore_existing)
703{
704 int err;
705
706 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
707 if (err != 0)
708 return err;
709 if (ignore_existing)
710 return 0;
711
712 /*
713 * We are going to walk all UBI devices and all volumes, and
714 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
715 * event. We have to lock the @ubi_devices_mutex to make sure UBI
716 * devices do not disappear.
717 */
718 mutex_lock(&ubi_devices_mutex);
719 ubi_enumerate_volumes(nb);
720 mutex_unlock(&ubi_devices_mutex);
721
722 return err;
723}
724EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
725
726/**
727 * ubi_unregister_volume_notifier - unregister the volume notifier.
728 * @nb: the notifier description object
729 *
730 * This function unregisters volume notifier @nm and returns zero in case of
731 * success and a negative error code in case of failure.
732 */
733int ubi_unregister_volume_notifier(struct notifier_block *nb)
734{
735 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
736}
737EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);