]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/ubi/kapi.c
Merge branch 'master' of git://www.denx.de/git/u-boot-imx
[karo-tx-uboot.git] / drivers / mtd / ubi / kapi.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  *
6  * Author: Artem Bityutskiy (Битюцкий Артём)
7  */
8
9 /* This file mostly implements UBI kernel API functions */
10
11 #define __UBOOT__
12 #ifndef __UBOOT__
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/namei.h>
16 #include <linux/fs.h>
17 #include <asm/div64.h>
18 #else
19 #include <ubi_uboot.h>
20 #endif
21 #include <linux/err.h>
22
23 #include "ubi.h"
24
25 /**
26  * ubi_do_get_device_info - get information about UBI device.
27  * @ubi: UBI device description object
28  * @di: the information is stored here
29  *
30  * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
31  * device is locked and cannot disappear.
32  */
33 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
34 {
35         di->ubi_num = ubi->ubi_num;
36         di->leb_size = ubi->leb_size;
37         di->leb_start = ubi->leb_start;
38         di->min_io_size = ubi->min_io_size;
39         di->max_write_size = ubi->max_write_size;
40         di->ro_mode = ubi->ro_mode;
41 #ifndef __UBOOT__
42         di->cdev = ubi->cdev.dev;
43 #endif
44 }
45 EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
46
47 /**
48  * ubi_get_device_info - get information about UBI device.
49  * @ubi_num: UBI device number
50  * @di: the information is stored here
51  *
52  * This function returns %0 in case of success, %-EINVAL if the UBI device
53  * number is invalid, and %-ENODEV if there is no such UBI device.
54  */
55 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
56 {
57         struct ubi_device *ubi;
58
59         if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
60                 return -EINVAL;
61         ubi = ubi_get_device(ubi_num);
62         if (!ubi)
63                 return -ENODEV;
64         ubi_do_get_device_info(ubi, di);
65         ubi_put_device(ubi);
66         return 0;
67 }
68 EXPORT_SYMBOL_GPL(ubi_get_device_info);
69
70 /**
71  * ubi_do_get_volume_info - get information about UBI volume.
72  * @ubi: UBI device description object
73  * @vol: volume description object
74  * @vi: the information is stored here
75  */
76 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
77                             struct ubi_volume_info *vi)
78 {
79         vi->vol_id = vol->vol_id;
80         vi->ubi_num = ubi->ubi_num;
81         vi->size = vol->reserved_pebs;
82         vi->used_bytes = vol->used_bytes;
83         vi->vol_type = vol->vol_type;
84         vi->corrupted = vol->corrupted;
85         vi->upd_marker = vol->upd_marker;
86         vi->alignment = vol->alignment;
87         vi->usable_leb_size = vol->usable_leb_size;
88         vi->name_len = vol->name_len;
89         vi->name = vol->name;
90         vi->cdev = vol->cdev.dev;
91 }
92
93 /**
94  * ubi_get_volume_info - get information about UBI volume.
95  * @desc: volume descriptor
96  * @vi: the information is stored here
97  */
98 void ubi_get_volume_info(struct ubi_volume_desc *desc,
99                          struct ubi_volume_info *vi)
100 {
101         ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
102 }
103 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
104
105 /**
106  * ubi_open_volume - open UBI volume.
107  * @ubi_num: UBI device number
108  * @vol_id: volume ID
109  * @mode: open mode
110  *
111  * The @mode parameter specifies if the volume should be opened in read-only
112  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
113  * nobody else will be able to open this volume. UBI allows to have many volume
114  * readers and one writer at a time.
115  *
116  * If a static volume is being opened for the first time since boot, it will be
117  * checked by this function, which means it will be fully read and the CRC
118  * checksum of each logical eraseblock will be checked.
119  *
120  * This function returns volume descriptor in case of success and a negative
121  * error code in case of failure.
122  */
123 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
124 {
125         int err;
126         struct ubi_volume_desc *desc;
127         struct ubi_device *ubi;
128         struct ubi_volume *vol;
129
130         dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
131
132         if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
133                 return ERR_PTR(-EINVAL);
134
135         if (mode != UBI_READONLY && mode != UBI_READWRITE &&
136             mode != UBI_EXCLUSIVE)
137                 return ERR_PTR(-EINVAL);
138
139         /*
140          * First of all, we have to get the UBI device to prevent its removal.
141          */
142         ubi = ubi_get_device(ubi_num);
143         if (!ubi)
144                 return ERR_PTR(-ENODEV);
145
146         if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
147                 err = -EINVAL;
148                 goto out_put_ubi;
149         }
150
151         desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
152         if (!desc) {
153                 err = -ENOMEM;
154                 goto out_put_ubi;
155         }
156
157         err = -ENODEV;
158         if (!try_module_get(THIS_MODULE))
159                 goto out_free;
160
161         spin_lock(&ubi->volumes_lock);
162         vol = ubi->volumes[vol_id];
163         if (!vol)
164                 goto out_unlock;
165
166         err = -EBUSY;
167         switch (mode) {
168         case UBI_READONLY:
169                 if (vol->exclusive)
170                         goto out_unlock;
171                 vol->readers += 1;
172                 break;
173
174         case UBI_READWRITE:
175                 if (vol->exclusive || vol->writers > 0)
176                         goto out_unlock;
177                 vol->writers += 1;
178                 break;
179
180         case UBI_EXCLUSIVE:
181                 if (vol->exclusive || vol->writers || vol->readers)
182                         goto out_unlock;
183                 vol->exclusive = 1;
184                 break;
185         }
186         get_device(&vol->dev);
187         vol->ref_count += 1;
188         spin_unlock(&ubi->volumes_lock);
189
190         desc->vol = vol;
191         desc->mode = mode;
192
193         mutex_lock(&ubi->ckvol_mutex);
194         if (!vol->checked) {
195                 /* This is the first open - check the volume */
196                 err = ubi_check_volume(ubi, vol_id);
197                 if (err < 0) {
198                         mutex_unlock(&ubi->ckvol_mutex);
199                         ubi_close_volume(desc);
200                         return ERR_PTR(err);
201                 }
202                 if (err == 1) {
203                         ubi_warn("volume %d on UBI device %d is corrupted",
204                                  vol_id, ubi->ubi_num);
205                         vol->corrupted = 1;
206                 }
207                 vol->checked = 1;
208         }
209         mutex_unlock(&ubi->ckvol_mutex);
210
211         return desc;
212
213 out_unlock:
214         spin_unlock(&ubi->volumes_lock);
215         module_put(THIS_MODULE);
216 out_free:
217         kfree(desc);
218 out_put_ubi:
219         ubi_put_device(ubi);
220         ubi_err("cannot open device %d, volume %d, error %d",
221                 ubi_num, vol_id, err);
222         return ERR_PTR(err);
223 }
224 EXPORT_SYMBOL_GPL(ubi_open_volume);
225
226 /**
227  * ubi_open_volume_nm - open UBI volume by name.
228  * @ubi_num: UBI device number
229  * @name: volume name
230  * @mode: open mode
231  *
232  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
233  */
234 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
235                                            int mode)
236 {
237         int i, vol_id = -1, len;
238         struct ubi_device *ubi;
239         struct ubi_volume_desc *ret;
240
241         dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
242
243         if (!name)
244                 return ERR_PTR(-EINVAL);
245
246         len = strnlen(name, UBI_VOL_NAME_MAX + 1);
247         if (len > UBI_VOL_NAME_MAX)
248                 return ERR_PTR(-EINVAL);
249
250         if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
251                 return ERR_PTR(-EINVAL);
252
253         ubi = ubi_get_device(ubi_num);
254         if (!ubi)
255                 return ERR_PTR(-ENODEV);
256
257         spin_lock(&ubi->volumes_lock);
258         /* Walk all volumes of this UBI device */
259         for (i = 0; i < ubi->vtbl_slots; i++) {
260                 struct ubi_volume *vol = ubi->volumes[i];
261
262                 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
263                         vol_id = i;
264                         break;
265                 }
266         }
267         spin_unlock(&ubi->volumes_lock);
268
269         if (vol_id >= 0)
270                 ret = ubi_open_volume(ubi_num, vol_id, mode);
271         else
272                 ret = ERR_PTR(-ENODEV);
273
274         /*
275          * We should put the UBI device even in case of success, because
276          * 'ubi_open_volume()' took a reference as well.
277          */
278         ubi_put_device(ubi);
279         return ret;
280 }
281 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
282
283 #ifndef __UBOOT__
284 /**
285  * 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  */
292 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
293 {
294         int error, ubi_num, vol_id, mod;
295         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;
308         mod = inode->i_mode;
309         ubi_num = ubi_major2num(imajor(inode));
310         vol_id = iminor(inode) - 1;
311         path_put(&path);
312
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);
318 }
319 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
320 #endif
321
322 /**
323  * ubi_close_volume - close UBI volume.
324  * @desc: volume descriptor
325  */
326 void ubi_close_volume(struct ubi_volume_desc *desc)
327 {
328         struct ubi_volume *vol = desc->vol;
329         struct ubi_device *ubi = vol->ubi;
330
331         dbg_gen("close device %d, volume %d, mode %d",
332                 ubi->ubi_num, vol->vol_id, desc->mode);
333
334         spin_lock(&ubi->volumes_lock);
335         switch (desc->mode) {
336         case UBI_READONLY:
337                 vol->readers -= 1;
338                 break;
339         case UBI_READWRITE:
340                 vol->writers -= 1;
341                 break;
342         case UBI_EXCLUSIVE:
343                 vol->exclusive = 0;
344         }
345         vol->ref_count -= 1;
346         spin_unlock(&ubi->volumes_lock);
347
348         kfree(desc);
349         put_device(&vol->dev);
350         ubi_put_device(ubi);
351         module_put(THIS_MODULE);
352 }
353 EXPORT_SYMBOL_GPL(ubi_close_volume);
354
355 /**
356  * ubi_leb_read - read data.
357  * @desc: volume descriptor
358  * @lnum: logical eraseblock number to read from
359  * @buf: buffer where to store the read data
360  * @offset: offset within the logical eraseblock to read from
361  * @len: how many bytes to read
362  * @check: whether UBI has to check the read data's CRC or not.
363  *
364  * This function reads data from offset @offset of logical eraseblock @lnum and
365  * stores the data at @buf. When reading from static volumes, @check specifies
366  * whether the data has to be checked or not. If yes, the whole logical
367  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
368  * checksum is per-eraseblock). So checking may substantially slow down the
369  * read speed. The @check argument is ignored for dynamic volumes.
370  *
371  * In case of success, this function returns zero. In case of failure, this
372  * function returns a negative error code.
373  *
374  * %-EBADMSG error code is returned:
375  * o for both static and dynamic volumes if MTD driver has detected a data
376  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
377  * o for static volumes in case of data CRC mismatch.
378  *
379  * If the volume is damaged because of an interrupted update this function just
380  * returns immediately with %-EBADF error code.
381  */
382 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
383                  int len, int check)
384 {
385         struct ubi_volume *vol = desc->vol;
386         struct ubi_device *ubi = vol->ubi;
387         int err, vol_id = vol->vol_id;
388
389         dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
390
391         if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
392             lnum >= vol->used_ebs || offset < 0 || len < 0 ||
393             offset + len > vol->usable_leb_size)
394                 return -EINVAL;
395
396         if (vol->vol_type == UBI_STATIC_VOLUME) {
397                 if (vol->used_ebs == 0)
398                         /* Empty static UBI volume */
399                         return 0;
400                 if (lnum == vol->used_ebs - 1 &&
401                     offset + len > vol->last_eb_bytes)
402                         return -EINVAL;
403         }
404
405         if (vol->upd_marker)
406                 return -EBADF;
407         if (len == 0)
408                 return 0;
409
410         err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
411         if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
412                 ubi_warn("mark volume %d as corrupted", vol_id);
413                 vol->corrupted = 1;
414         }
415
416         return err;
417 }
418 EXPORT_SYMBOL_GPL(ubi_leb_read);
419
420 /**
421  * ubi_leb_write - write data.
422  * @desc: volume descriptor
423  * @lnum: logical eraseblock number to write to
424  * @buf: data to write
425  * @offset: offset within the logical eraseblock where to write
426  * @len: how many bytes to write
427  *
428  * This function writes @len bytes of data from @buf to offset @offset of
429  * logical eraseblock @lnum.
430  *
431  * This function takes care of physical eraseblock write failures. If write to
432  * the physical eraseblock write operation fails, the logical eraseblock is
433  * re-mapped to another physical eraseblock, the data is recovered, and the
434  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
435  *
436  * If all the data were successfully written, zero is returned. If an error
437  * occurred and UBI has not been able to recover from it, this function returns
438  * a negative error code. Note, in case of an error, it is possible that
439  * something was still written to the flash media, but that may be some
440  * garbage.
441  *
442  * If the volume is damaged because of an interrupted update this function just
443  * returns immediately with %-EBADF code.
444  */
445 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
446                   int offset, int len)
447 {
448         struct ubi_volume *vol = desc->vol;
449         struct ubi_device *ubi = vol->ubi;
450         int vol_id = vol->vol_id;
451
452         dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
453
454         if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
455                 return -EINVAL;
456
457         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
458                 return -EROFS;
459
460         if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
461             offset + len > vol->usable_leb_size ||
462             offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
463                 return -EINVAL;
464
465         if (vol->upd_marker)
466                 return -EBADF;
467
468         if (len == 0)
469                 return 0;
470
471         return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
472 }
473 EXPORT_SYMBOL_GPL(ubi_leb_write);
474
475 /*
476  * ubi_leb_change - change logical eraseblock atomically.
477  * @desc: volume descriptor
478  * @lnum: logical eraseblock number to change
479  * @buf: data to write
480  * @len: how many bytes to write
481  *
482  * This function changes the contents of a logical eraseblock atomically. @buf
483  * has to contain new logical eraseblock data, and @len - the length of the
484  * data, which has to be aligned. The length may be shorter than the logical
485  * eraseblock size, ant the logical eraseblock may be appended to more times
486  * later on. This function guarantees that in case of an unclean reboot the old
487  * contents is preserved. Returns zero in case of success and a negative error
488  * code in case of failure.
489  */
490 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
491                    int len)
492 {
493         struct ubi_volume *vol = desc->vol;
494         struct ubi_device *ubi = vol->ubi;
495         int vol_id = vol->vol_id;
496
497         dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
498
499         if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
500                 return -EINVAL;
501
502         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
503                 return -EROFS;
504
505         if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
506             len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
507                 return -EINVAL;
508
509         if (vol->upd_marker)
510                 return -EBADF;
511
512         if (len == 0)
513                 return 0;
514
515         return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
516 }
517 EXPORT_SYMBOL_GPL(ubi_leb_change);
518
519 /**
520  * ubi_leb_erase - erase logical eraseblock.
521  * @desc: volume descriptor
522  * @lnum: logical eraseblock number
523  *
524  * This function un-maps logical eraseblock @lnum and synchronously erases the
525  * correspondent physical eraseblock. Returns zero in case of success and a
526  * negative error code in case of failure.
527  *
528  * If the volume is damaged because of an interrupted update this function just
529  * returns immediately with %-EBADF code.
530  */
531 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
532 {
533         struct ubi_volume *vol = desc->vol;
534         struct ubi_device *ubi = vol->ubi;
535         int err;
536
537         dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
538
539         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
540                 return -EROFS;
541
542         if (lnum < 0 || lnum >= vol->reserved_pebs)
543                 return -EINVAL;
544
545         if (vol->upd_marker)
546                 return -EBADF;
547
548         err = ubi_eba_unmap_leb(ubi, vol, lnum);
549         if (err)
550                 return err;
551
552         return ubi_wl_flush(ubi, vol->vol_id, lnum);
553 }
554 EXPORT_SYMBOL_GPL(ubi_leb_erase);
555
556 /**
557  * ubi_leb_unmap - un-map logical eraseblock.
558  * @desc: volume descriptor
559  * @lnum: logical eraseblock number
560  *
561  * This function un-maps logical eraseblock @lnum and schedules the
562  * corresponding physical eraseblock for erasure, so that it will eventually be
563  * physically erased in background. This operation is much faster than the
564  * erase operation.
565  *
566  * Unlike erase, the un-map operation does not guarantee that the logical
567  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
568  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
569  * happens after this, the logical eraseblocks will not necessarily be
570  * un-mapped again when this MTD device is attached. They may actually be
571  * mapped to the same physical eraseblocks again. So, this function has to be
572  * used with care.
573  *
574  * In other words, when un-mapping a logical eraseblock, UBI does not store
575  * any information about this on the flash media, it just marks the logical
576  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
577  * eraseblock is physically erased, it will be mapped again to the same logical
578  * eraseblock when the MTD device is attached again.
579  *
580  * The main and obvious use-case of this function is when the contents of a
581  * logical eraseblock has to be re-written. Then it is much more efficient to
582  * first un-map it, then write new data, rather than first erase it, then write
583  * new data. Note, once new data has been written to the logical eraseblock,
584  * UBI guarantees that the old contents has gone forever. In other words, if an
585  * unclean reboot happens after the logical eraseblock has been un-mapped and
586  * then written to, it will contain the last written data.
587  *
588  * This function returns zero in case of success and a negative error code in
589  * case of failure. If the volume is damaged because of an interrupted update
590  * this function just returns immediately with %-EBADF code.
591  */
592 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
593 {
594         struct ubi_volume *vol = desc->vol;
595         struct ubi_device *ubi = vol->ubi;
596
597         dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
598
599         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
600                 return -EROFS;
601
602         if (lnum < 0 || lnum >= vol->reserved_pebs)
603                 return -EINVAL;
604
605         if (vol->upd_marker)
606                 return -EBADF;
607
608         return ubi_eba_unmap_leb(ubi, vol, lnum);
609 }
610 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
611
612 /**
613  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
614  * @desc: volume descriptor
615  * @lnum: logical eraseblock number
616  *
617  * This function maps an un-mapped logical eraseblock @lnum to a physical
618  * eraseblock. This means, that after a successful invocation of this
619  * function the logical eraseblock @lnum will be empty (contain only %0xFF
620  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
621  * happens.
622  *
623  * This function returns zero in case of success, %-EBADF if the volume is
624  * damaged because of an interrupted update, %-EBADMSG if the logical
625  * eraseblock is already mapped, and other negative error codes in case of
626  * other failures.
627  */
628 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
629 {
630         struct ubi_volume *vol = desc->vol;
631         struct ubi_device *ubi = vol->ubi;
632
633         dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
634
635         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
636                 return -EROFS;
637
638         if (lnum < 0 || lnum >= vol->reserved_pebs)
639                 return -EINVAL;
640
641         if (vol->upd_marker)
642                 return -EBADF;
643
644         if (vol->eba_tbl[lnum] >= 0)
645                 return -EBADMSG;
646
647         return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
648 }
649 EXPORT_SYMBOL_GPL(ubi_leb_map);
650
651 /**
652  * ubi_is_mapped - check if logical eraseblock is mapped.
653  * @desc: volume descriptor
654  * @lnum: logical eraseblock number
655  *
656  * This function checks if logical eraseblock @lnum is mapped to a physical
657  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
658  * mean it will still be un-mapped after the UBI device is re-attached. The
659  * logical eraseblock may become mapped to the physical eraseblock it was last
660  * mapped to.
661  *
662  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
663  * error code in case of failure. If the volume is damaged because of an
664  * interrupted update this function just returns immediately with %-EBADF error
665  * code.
666  */
667 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
668 {
669         struct ubi_volume *vol = desc->vol;
670
671         dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
672
673         if (lnum < 0 || lnum >= vol->reserved_pebs)
674                 return -EINVAL;
675
676         if (vol->upd_marker)
677                 return -EBADF;
678
679         return vol->eba_tbl[lnum] >= 0;
680 }
681 EXPORT_SYMBOL_GPL(ubi_is_mapped);
682
683 /**
684  * ubi_sync - synchronize UBI device buffers.
685  * @ubi_num: UBI device to synchronize
686  *
687  * The underlying MTD device may cache data in hardware or in software. This
688  * function ensures the caches are flushed. Returns zero in case of success and
689  * a negative error code in case of failure.
690  */
691 int ubi_sync(int ubi_num)
692 {
693         struct ubi_device *ubi;
694
695         ubi = ubi_get_device(ubi_num);
696         if (!ubi)
697                 return -ENODEV;
698
699         mtd_sync(ubi->mtd);
700         ubi_put_device(ubi);
701         return 0;
702 }
703 EXPORT_SYMBOL_GPL(ubi_sync);
704
705 /**
706  * ubi_flush - flush UBI work queue.
707  * @ubi_num: UBI device to flush work queue
708  * @vol_id: volume id to flush for
709  * @lnum: logical eraseblock number to flush for
710  *
711  * This function executes all pending works for a particular volume id / logical
712  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
713  * a wildcard for all of the corresponding volume numbers or logical
714  * eraseblock numbers. It returns zero in case of success and a negative error
715  * code in case of failure.
716  */
717 int ubi_flush(int ubi_num, int vol_id, int lnum)
718 {
719         struct ubi_device *ubi;
720         int err = 0;
721
722         ubi = ubi_get_device(ubi_num);
723         if (!ubi)
724                 return -ENODEV;
725
726         err = ubi_wl_flush(ubi, vol_id, lnum);
727         ubi_put_device(ubi);
728         return err;
729 }
730 EXPORT_SYMBOL_GPL(ubi_flush);
731
732 #ifndef __UBOOT__
733 BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
734
735 /**
736  * ubi_register_volume_notifier - register a volume notifier.
737  * @nb: the notifier description object
738  * @ignore_existing: if non-zero, do not send "added" notification for all
739  *                   already existing volumes
740  *
741  * This function registers a volume notifier, which means that
742  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
743  * removed, re-sized, re-named, or updated. The first argument of the function
744  * is the notification type. The second argument is pointer to a
745  * &struct ubi_notification object which describes the notification event.
746  * Using UBI API from the volume notifier is prohibited.
747  *
748  * This function returns zero in case of success and a negative error code
749  * in case of failure.
750  */
751 int ubi_register_volume_notifier(struct notifier_block *nb,
752                                  int ignore_existing)
753 {
754         int err;
755
756         err = blocking_notifier_chain_register(&ubi_notifiers, nb);
757         if (err != 0)
758                 return err;
759         if (ignore_existing)
760                 return 0;
761
762         /*
763          * We are going to walk all UBI devices and all volumes, and
764          * notify the user about existing volumes by the %UBI_VOLUME_ADDED
765          * event. We have to lock the @ubi_devices_mutex to make sure UBI
766          * devices do not disappear.
767          */
768         mutex_lock(&ubi_devices_mutex);
769         ubi_enumerate_volumes(nb);
770         mutex_unlock(&ubi_devices_mutex);
771
772         return err;
773 }
774 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
775
776 /**
777  * ubi_unregister_volume_notifier - unregister the volume notifier.
778  * @nb: the notifier description object
779  *
780  * This function unregisters volume notifier @nm and returns zero in case of
781  * success and a negative error code in case of failure.
782  */
783 int ubi_unregister_volume_notifier(struct notifier_block *nb)
784 {
785         return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
786 }
787 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
788 #endif