]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mtd/ubi/kapi.c
Merge branch 'amd-iommu/2.6.32' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / drivers / mtd / ubi / kapi.c
1 /*
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 /**
29  * 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  */
36 void 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 }
44 EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
45
46 /**
47  * 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  */
54 int 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;
60         ubi = ubi_get_device(ubi_num);
61         if (!ubi)
62                 return -ENODEV;
63         ubi_do_get_device_info(ubi, di);
64         ubi_put_device(ubi);
65         return 0;
66 }
67 EXPORT_SYMBOL_GPL(ubi_get_device_info);
68
69 /**
70  * ubi_do_get_volume_info - get information about UBI volume.
71  * @ubi: UBI device description object
72  * @vol: volume description object
73  * @vi: the information is stored here
74  */
75 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
76                             struct ubi_volume_info *vi)
77 {
78         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 }
91
92 /**
93  * ubi_get_volume_info - get information about UBI volume.
94  * @desc: volume descriptor
95  * @vi: the information is stored here
96  */
97 void 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 }
102 EXPORT_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  */
122 struct 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
129         dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
130
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
212 out_unlock:
213         spin_unlock(&ubi->volumes_lock);
214         module_put(THIS_MODULE);
215 out_free:
216         kfree(desc);
217 out_put_ubi:
218         ubi_put_device(ubi);
219         dbg_err("cannot open device %d, volume %d, error %d",
220                 ubi_num, vol_id, err);
221         return ERR_PTR(err);
222 }
223 EXPORT_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  */
233 struct 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
240         dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
241
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 }
280 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
281
282 /**
283  * ubi_close_volume - close UBI volume.
284  * @desc: volume descriptor
285  */
286 void ubi_close_volume(struct ubi_volume_desc *desc)
287 {
288         struct ubi_volume *vol = desc->vol;
289         struct ubi_device *ubi = vol->ubi;
290
291         dbg_gen("close device %d, volume %d, mode %d",
292                 ubi->ubi_num, vol->vol_id, desc->mode);
293
294         spin_lock(&ubi->volumes_lock);
295         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         }
305         vol->ref_count -= 1;
306         spin_unlock(&ubi->volumes_lock);
307
308         kfree(desc);
309         put_device(&vol->dev);
310         ubi_put_device(ubi);
311         module_put(THIS_MODULE);
312 }
313 EXPORT_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  */
342 int 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
349         dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
350
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
356         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         }
364
365         if (vol->upd_marker)
366                 return -EBADF;
367         if (len == 0)
368                 return 0;
369
370         err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
371         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 }
378 EXPORT_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  */
407 int 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
414         dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
415
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 ||
423             offset + len > vol->usable_leb_size ||
424             offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
425                 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
437         return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
438 }
439 EXPORT_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  */
457 int 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
464         dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
465
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 ||
473             len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
474                 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
486         return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
487 }
488 EXPORT_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  */
502 int 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;
506         int err;
507
508         dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
509
510         if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
511                 return -EROFS;
512
513         if (lnum < 0 || lnum >= vol->reserved_pebs)
514                 return -EINVAL;
515
516         if (vol->upd_marker)
517                 return -EBADF;
518
519         err = ubi_eba_unmap_leb(ubi, vol, lnum);
520         if (err)
521                 return err;
522
523         return ubi_wl_flush(ubi);
524 }
525 EXPORT_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  */
563 int 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;
567
568         dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
569
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
579         return ubi_eba_unmap_leb(ubi, vol, lnum);
580 }
581 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
582
583 /**
584  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
585  * @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
590  * eraseblock. This means, that after a successful invocation of this
591  * 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  */
600 int 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;
604
605         dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
606
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
623         return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
624 }
625 EXPORT_SYMBOL_GPL(ubi_leb_map);
626
627 /**
628  * 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  */
643 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
644 {
645         struct ubi_volume *vol = desc->vol;
646
647         dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
648
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 }
657 EXPORT_SYMBOL_GPL(ubi_is_mapped);
658
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  */
667 int 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 }
681 EXPORT_SYMBOL_GPL(ubi_sync);
682
683 BLOCKING_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  */
701 int 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 }
724 EXPORT_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  */
733 int ubi_unregister_volume_notifier(struct notifier_block *nb)
734 {
735         return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
736 }
737 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);