blob: fd2bbd64f13835d67e25ad5275d550b3a1ca8058 [file] [log] [blame]
Kyungmin Park2d262c42008-11-19 16:26:54 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Park2d262c42008-11-19 16:26:54 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9/* This file mostly implements UBI kernel API functions */
10
Heiko Schocherff94bc42014-06-24 10:10:04 +020011#ifndef __UBOOT__
Kyungmin Park2d262c42008-11-19 16:26:54 +010012#include <linux/module.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020013#include <linux/slab.h>
14#include <linux/namei.h>
15#include <linux/fs.h>
Kyungmin Park2d262c42008-11-19 16:26:54 +010016#include <asm/div64.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020017#else
Kyungmin Park2d262c42008-11-19 16:26:54 +010018#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020019#endif
20#include <linux/err.h>
21
Kyungmin Park2d262c42008-11-19 16:26:54 +010022#include "ubi.h"
23
24/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020025 * ubi_do_get_device_info - get information about UBI device.
26 * @ubi: UBI device description object
27 * @di: the information is stored here
28 *
29 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
30 * device is locked and cannot disappear.
31 */
32void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
33{
34 di->ubi_num = ubi->ubi_num;
35 di->leb_size = ubi->leb_size;
36 di->leb_start = ubi->leb_start;
37 di->min_io_size = ubi->min_io_size;
38 di->max_write_size = ubi->max_write_size;
39 di->ro_mode = ubi->ro_mode;
40#ifndef __UBOOT__
41 di->cdev = ubi->cdev.dev;
42#endif
43}
44EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
45
46/**
Kyungmin Park2d262c42008-11-19 16:26:54 +010047 * ubi_get_device_info - get information about UBI device.
48 * @ubi_num: UBI device number
49 * @di: the information is stored here
50 *
51 * 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.
53 */
54int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
55{
56 struct ubi_device *ubi;
57
58 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
59 return -EINVAL;
Kyungmin Park2d262c42008-11-19 16:26:54 +010060 ubi = ubi_get_device(ubi_num);
61 if (!ubi)
62 return -ENODEV;
Heiko Schocherff94bc42014-06-24 10:10:04 +020063 ubi_do_get_device_info(ubi, di);
Kyungmin Park2d262c42008-11-19 16:26:54 +010064 ubi_put_device(ubi);
65 return 0;
66}
67EXPORT_SYMBOL_GPL(ubi_get_device_info);
68
69/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020070 * ubi_do_get_volume_info - get information about UBI volume.
71 * @ubi: UBI device description object
72 * @vol: volume description object
Kyungmin Park2d262c42008-11-19 16:26:54 +010073 * @vi: the information is stored here
74 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020075void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
76 struct ubi_volume_info *vi)
Kyungmin Park2d262c42008-11-19 16:26:54 +010077{
Kyungmin Park2d262c42008-11-19 16:26:54 +010078 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;
89 vi->cdev = vol->cdev.dev;
90}
Heiko Schocherff94bc42014-06-24 10:10:04 +020091
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}
Kyungmin Park2d262c42008-11-19 16:26:54 +0100102EXPORT_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;
126 struct ubi_device *ubi;
127 struct ubi_volume *vol;
128
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100130
131 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
132 return ERR_PTR(-EINVAL);
133
134 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
135 mode != UBI_EXCLUSIVE)
136 return ERR_PTR(-EINVAL);
137
138 /*
139 * First of all, we have to get the UBI device to prevent its removal.
140 */
141 ubi = ubi_get_device(ubi_num);
142 if (!ubi)
143 return ERR_PTR(-ENODEV);
144
145 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
146 err = -EINVAL;
147 goto out_put_ubi;
148 }
149
150 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
151 if (!desc) {
152 err = -ENOMEM;
153 goto out_put_ubi;
154 }
155
156 err = -ENODEV;
157 if (!try_module_get(THIS_MODULE))
158 goto out_free;
159
160 spin_lock(&ubi->volumes_lock);
161 vol = ubi->volumes[vol_id];
162 if (!vol)
163 goto out_unlock;
164
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 }
185 get_device(&vol->dev);
186 vol->ref_count += 1;
187 spin_unlock(&ubi->volumes_lock);
188
189 desc->vol = vol;
190 desc->mode = mode;
191
192 mutex_lock(&ubi->ckvol_mutex);
193 if (!vol->checked) {
194 /* This is the first open - check the volume */
195 err = ubi_check_volume(ubi, vol_id);
196 if (err < 0) {
197 mutex_unlock(&ubi->ckvol_mutex);
198 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 }
208 mutex_unlock(&ubi->ckvol_mutex);
209
210 return desc;
211
212out_unlock:
213 spin_unlock(&ubi->volumes_lock);
214 module_put(THIS_MODULE);
215out_free:
216 kfree(desc);
217out_put_ubi:
218 ubi_put_device(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200219 ubi_err("cannot open device %d, volume %d, error %d",
220 ubi_num, vol_id, err);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100221 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;
237 struct ubi_device *ubi;
238 struct ubi_volume_desc *ret;
239
Heiko Schocherff94bc42014-06-24 10:10:04 +0200240 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100241
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
249 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
250 return ERR_PTR(-EINVAL);
251
252 ubi = ubi_get_device(ubi_num);
253 if (!ubi)
254 return ERR_PTR(-ENODEV);
255
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
268 if (vol_id >= 0)
269 ret = ubi_open_volume(ubi_num, vol_id, mode);
270 else
271 ret = ERR_PTR(-ENODEV);
272
273 /*
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;
279}
280EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
281
Heiko Schocherff94bc42014-06-24 10:10:04 +0200282#ifndef __UBOOT__
283/**
284 * ubi_open_volume_path - open UBI volume by its character device node path.
285 * @pathname: volume character device node path
286 * @mode: open mode
287 *
288 * This function is similar to 'ubi_open_volume()', but opens a volume the path
289 * to its character device node.
290 */
291struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
292{
293 int error, ubi_num, vol_id, mod;
294 struct inode *inode;
295 struct path path;
296
297 dbg_gen("open volume %s, mode %d", pathname, mode);
298
299 if (!pathname || !*pathname)
300 return ERR_PTR(-EINVAL);
301
302 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
303 if (error)
304 return ERR_PTR(error);
305
306 inode = path.dentry->d_inode;
307 mod = inode->i_mode;
308 ubi_num = ubi_major2num(imajor(inode));
309 vol_id = iminor(inode) - 1;
310 path_put(&path);
311
312 if (!S_ISCHR(mod))
313 return ERR_PTR(-EINVAL);
314 if (vol_id >= 0 && ubi_num >= 0)
315 return ubi_open_volume(ubi_num, vol_id, mode);
316 return ERR_PTR(-ENODEV);
317}
318EXPORT_SYMBOL_GPL(ubi_open_volume_path);
319#endif
320
Kyungmin Park2d262c42008-11-19 16:26:54 +0100321/**
322 * 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;
328 struct ubi_device *ubi = vol->ubi;
329
Heiko Schocherff94bc42014-06-24 10:10:04 +0200330 dbg_gen("close device %d, volume %d, mode %d",
331 ubi->ubi_num, vol->vol_id, desc->mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100332
333 spin_lock(&ubi->volumes_lock);
334 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 }
344 vol->ref_count -= 1;
345 spin_unlock(&ubi->volumes_lock);
346
347 kfree(desc);
348 put_device(&vol->dev);
349 ubi_put_device(ubi);
350 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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200388 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100389
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
395 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 }
403
404 if (vol->upd_marker)
405 return -EBADF;
406 if (len == 0)
407 return 0;
408
409 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000410 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
Kyungmin Park2d262c42008-11-19 16:26:54 +0100411 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
Kyungmin Park2d262c42008-11-19 16:26:54 +0100426 *
427 * This function writes @len bytes of data from @buf to offset @offset of
Heiko Schocherff94bc42014-06-24 10:10:04 +0200428 * logical eraseblock @lnum.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100429 *
430 * This function takes care of physical eraseblock write failures. If write to
431 * the physical eraseblock write operation fails, the logical eraseblock is
432 * re-mapped to another physical eraseblock, the data is recovered, and the
433 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
434 *
435 * If all the data were successfully written, zero is returned. If an error
436 * occurred and UBI has not been able to recover from it, this function returns
437 * a negative error code. Note, in case of an error, it is possible that
438 * something was still written to the flash media, but that may be some
439 * garbage.
440 *
441 * If the volume is damaged because of an interrupted update this function just
442 * returns immediately with %-EBADF code.
443 */
444int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200445 int offset, int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100446{
447 struct ubi_volume *vol = desc->vol;
448 struct ubi_device *ubi = vol->ubi;
449 int vol_id = vol->vol_id;
450
Heiko Schocherff94bc42014-06-24 10:10:04 +0200451 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100452
453 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
454 return -EINVAL;
455
456 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
457 return -EROFS;
458
459 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
460 offset + len > vol->usable_leb_size ||
461 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
462 return -EINVAL;
463
Kyungmin Park2d262c42008-11-19 16:26:54 +0100464 if (vol->upd_marker)
465 return -EBADF;
466
467 if (len == 0)
468 return 0;
469
Heiko Schocherff94bc42014-06-24 10:10:04 +0200470 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100471}
472EXPORT_SYMBOL_GPL(ubi_leb_write);
473
474/*
475 * ubi_leb_change - change logical eraseblock atomically.
476 * @desc: volume descriptor
477 * @lnum: logical eraseblock number to change
478 * @buf: data to write
479 * @len: how many bytes to write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100480 *
481 * This function changes the contents of a logical eraseblock atomically. @buf
482 * has to contain new logical eraseblock data, and @len - the length of the
Heiko Schocherff94bc42014-06-24 10:10:04 +0200483 * data, which has to be aligned. The length may be shorter than the logical
Kyungmin Park2d262c42008-11-19 16:26:54 +0100484 * eraseblock size, ant the logical eraseblock may be appended to more times
485 * later on. This function guarantees that in case of an unclean reboot the old
486 * contents is preserved. Returns zero in case of success and a negative error
487 * code in case of failure.
488 */
489int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200490 int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100491{
492 struct ubi_volume *vol = desc->vol;
493 struct ubi_device *ubi = vol->ubi;
494 int vol_id = vol->vol_id;
495
Heiko Schocherff94bc42014-06-24 10:10:04 +0200496 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100497
498 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
499 return -EINVAL;
500
501 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
502 return -EROFS;
503
504 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
505 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
506 return -EINVAL;
507
Kyungmin Park2d262c42008-11-19 16:26:54 +0100508 if (vol->upd_marker)
509 return -EBADF;
510
511 if (len == 0)
512 return 0;
513
Heiko Schocherff94bc42014-06-24 10:10:04 +0200514 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100515}
516EXPORT_SYMBOL_GPL(ubi_leb_change);
517
518/**
519 * ubi_leb_erase - erase logical eraseblock.
520 * @desc: volume descriptor
521 * @lnum: logical eraseblock number
522 *
523 * This function un-maps logical eraseblock @lnum and synchronously erases the
524 * correspondent physical eraseblock. Returns zero in case of success and a
525 * negative error code in case of failure.
526 *
527 * If the volume is damaged because of an interrupted update this function just
528 * returns immediately with %-EBADF code.
529 */
530int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
531{
532 struct ubi_volume *vol = desc->vol;
533 struct ubi_device *ubi = vol->ubi;
534 int err;
535
Heiko Schocherff94bc42014-06-24 10:10:04 +0200536 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100537
538 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
539 return -EROFS;
540
541 if (lnum < 0 || lnum >= vol->reserved_pebs)
542 return -EINVAL;
543
544 if (vol->upd_marker)
545 return -EBADF;
546
547 err = ubi_eba_unmap_leb(ubi, vol, lnum);
548 if (err)
549 return err;
550
Heiko Schocherff94bc42014-06-24 10:10:04 +0200551 return ubi_wl_flush(ubi, vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100552}
553EXPORT_SYMBOL_GPL(ubi_leb_erase);
554
555/**
556 * ubi_leb_unmap - un-map logical eraseblock.
557 * @desc: volume descriptor
558 * @lnum: logical eraseblock number
559 *
560 * This function un-maps logical eraseblock @lnum and schedules the
561 * corresponding physical eraseblock for erasure, so that it will eventually be
Heiko Schocherff94bc42014-06-24 10:10:04 +0200562 * physically erased in background. This operation is much faster than the
Kyungmin Park2d262c42008-11-19 16:26:54 +0100563 * erase operation.
564 *
565 * Unlike erase, the un-map operation does not guarantee that the logical
566 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
567 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
568 * happens after this, the logical eraseblocks will not necessarily be
569 * un-mapped again when this MTD device is attached. They may actually be
570 * mapped to the same physical eraseblocks again. So, this function has to be
571 * used with care.
572 *
573 * In other words, when un-mapping a logical eraseblock, UBI does not store
574 * any information about this on the flash media, it just marks the logical
575 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
576 * eraseblock is physically erased, it will be mapped again to the same logical
577 * eraseblock when the MTD device is attached again.
578 *
579 * The main and obvious use-case of this function is when the contents of a
580 * logical eraseblock has to be re-written. Then it is much more efficient to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200581 * first un-map it, then write new data, rather than first erase it, then write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100582 * new data. Note, once new data has been written to the logical eraseblock,
583 * UBI guarantees that the old contents has gone forever. In other words, if an
584 * unclean reboot happens after the logical eraseblock has been un-mapped and
585 * then written to, it will contain the last written data.
586 *
587 * This function returns zero in case of success and a negative error code in
588 * case of failure. If the volume is damaged because of an interrupted update
589 * this function just returns immediately with %-EBADF code.
590 */
591int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
592{
593 struct ubi_volume *vol = desc->vol;
594 struct ubi_device *ubi = vol->ubi;
595
Heiko Schocherff94bc42014-06-24 10:10:04 +0200596 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100597
598 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
599 return -EROFS;
600
601 if (lnum < 0 || lnum >= vol->reserved_pebs)
602 return -EINVAL;
603
604 if (vol->upd_marker)
605 return -EBADF;
606
607 return ubi_eba_unmap_leb(ubi, vol, lnum);
608}
609EXPORT_SYMBOL_GPL(ubi_leb_unmap);
610
611/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200612 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100613 * @desc: volume descriptor
614 * @lnum: logical eraseblock number
Kyungmin Park2d262c42008-11-19 16:26:54 +0100615 *
616 * This function maps an un-mapped logical eraseblock @lnum to a physical
Heiko Schocherff94bc42014-06-24 10:10:04 +0200617 * eraseblock. This means, that after a successful invocation of this
Kyungmin Park2d262c42008-11-19 16:26:54 +0100618 * function the logical eraseblock @lnum will be empty (contain only %0xFF
619 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
620 * happens.
621 *
622 * This function returns zero in case of success, %-EBADF if the volume is
623 * damaged because of an interrupted update, %-EBADMSG if the logical
624 * eraseblock is already mapped, and other negative error codes in case of
625 * other failures.
626 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200627int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100628{
629 struct ubi_volume *vol = desc->vol;
630 struct ubi_device *ubi = vol->ubi;
631
Heiko Schocherff94bc42014-06-24 10:10:04 +0200632 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100633
634 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
635 return -EROFS;
636
637 if (lnum < 0 || lnum >= vol->reserved_pebs)
638 return -EINVAL;
639
Kyungmin Park2d262c42008-11-19 16:26:54 +0100640 if (vol->upd_marker)
641 return -EBADF;
642
643 if (vol->eba_tbl[lnum] >= 0)
644 return -EBADMSG;
645
Heiko Schocherff94bc42014-06-24 10:10:04 +0200646 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100647}
648EXPORT_SYMBOL_GPL(ubi_leb_map);
649
650/**
651 * ubi_is_mapped - check if logical eraseblock is mapped.
652 * @desc: volume descriptor
653 * @lnum: logical eraseblock number
654 *
655 * This function checks if logical eraseblock @lnum is mapped to a physical
656 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
657 * mean it will still be un-mapped after the UBI device is re-attached. The
658 * logical eraseblock may become mapped to the physical eraseblock it was last
659 * mapped to.
660 *
661 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
662 * error code in case of failure. If the volume is damaged because of an
663 * interrupted update this function just returns immediately with %-EBADF error
664 * code.
665 */
666int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
667{
668 struct ubi_volume *vol = desc->vol;
669
Heiko Schocherff94bc42014-06-24 10:10:04 +0200670 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100671
672 if (lnum < 0 || lnum >= vol->reserved_pebs)
673 return -EINVAL;
674
675 if (vol->upd_marker)
676 return -EBADF;
677
678 return vol->eba_tbl[lnum] >= 0;
679}
680EXPORT_SYMBOL_GPL(ubi_is_mapped);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200681
682/**
683 * ubi_sync - synchronize UBI device buffers.
684 * @ubi_num: UBI device to synchronize
685 *
686 * The underlying MTD device may cache data in hardware or in software. This
687 * function ensures the caches are flushed. Returns zero in case of success and
688 * a negative error code in case of failure.
689 */
690int ubi_sync(int ubi_num)
691{
692 struct ubi_device *ubi;
693
694 ubi = ubi_get_device(ubi_num);
695 if (!ubi)
696 return -ENODEV;
697
698 mtd_sync(ubi->mtd);
699 ubi_put_device(ubi);
700 return 0;
701}
702EXPORT_SYMBOL_GPL(ubi_sync);
703
704/**
705 * ubi_flush - flush UBI work queue.
706 * @ubi_num: UBI device to flush work queue
707 * @vol_id: volume id to flush for
708 * @lnum: logical eraseblock number to flush for
709 *
710 * This function executes all pending works for a particular volume id / logical
711 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
712 * a wildcard for all of the corresponding volume numbers or logical
713 * eraseblock numbers. It returns zero in case of success and a negative error
714 * code in case of failure.
715 */
716int ubi_flush(int ubi_num, int vol_id, int lnum)
717{
718 struct ubi_device *ubi;
719 int err = 0;
720
721 ubi = ubi_get_device(ubi_num);
722 if (!ubi)
723 return -ENODEV;
724
725 err = ubi_wl_flush(ubi, vol_id, lnum);
726 ubi_put_device(ubi);
727 return err;
728}
729EXPORT_SYMBOL_GPL(ubi_flush);
730
731#ifndef __UBOOT__
732BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
733
734/**
735 * ubi_register_volume_notifier - register a volume notifier.
736 * @nb: the notifier description object
737 * @ignore_existing: if non-zero, do not send "added" notification for all
738 * already existing volumes
739 *
740 * This function registers a volume notifier, which means that
741 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
742 * removed, re-sized, re-named, or updated. The first argument of the function
743 * is the notification type. The second argument is pointer to a
744 * &struct ubi_notification object which describes the notification event.
745 * Using UBI API from the volume notifier is prohibited.
746 *
747 * This function returns zero in case of success and a negative error code
748 * in case of failure.
749 */
750int ubi_register_volume_notifier(struct notifier_block *nb,
751 int ignore_existing)
752{
753 int err;
754
755 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
756 if (err != 0)
757 return err;
758 if (ignore_existing)
759 return 0;
760
761 /*
762 * We are going to walk all UBI devices and all volumes, and
763 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
764 * event. We have to lock the @ubi_devices_mutex to make sure UBI
765 * devices do not disappear.
766 */
767 mutex_lock(&ubi_devices_mutex);
768 ubi_enumerate_volumes(nb);
769 mutex_unlock(&ubi_devices_mutex);
770
771 return err;
772}
773EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
774
775/**
776 * ubi_unregister_volume_notifier - unregister the volume notifier.
777 * @nb: the notifier description object
778 *
779 * This function unregisters volume notifier @nm and returns zero in case of
780 * success and a negative error code in case of failure.
781 */
782int ubi_unregister_volume_notifier(struct notifier_block *nb)
783{
784 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
785}
786EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
787#endif