1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) International Business Machines Corp., 2006
5 * Author: Artem Bityutskiy (Битюцкий Артём)
8 /* This file mostly implements UBI kernel API functions */
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/namei.h>
15 #include <asm/div64.h>
19 * ubi_do_get_device_info - get information about UBI device.
20 * @ubi: UBI device description object
21 * @di: the information is stored here
23 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
24 * device is locked and cannot disappear.
26 void ubi_do_get_device_info(struct ubi_device
*ubi
, struct ubi_device_info
*di
)
28 di
->ubi_num
= ubi
->ubi_num
;
29 di
->leb_size
= ubi
->leb_size
;
30 di
->leb_start
= ubi
->leb_start
;
31 di
->min_io_size
= ubi
->min_io_size
;
32 di
->max_write_size
= ubi
->max_write_size
;
33 di
->ro_mode
= ubi
->ro_mode
;
34 di
->cdev
= ubi
->cdev
.dev
;
36 EXPORT_SYMBOL_GPL(ubi_do_get_device_info
);
39 * ubi_get_device_info - get information about UBI device.
40 * @ubi_num: UBI device number
41 * @di: the information is stored here
43 * This function returns %0 in case of success, %-EINVAL if the UBI device
44 * number is invalid, and %-ENODEV if there is no such UBI device.
46 int ubi_get_device_info(int ubi_num
, struct ubi_device_info
*di
)
48 struct ubi_device
*ubi
;
50 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
52 ubi
= ubi_get_device(ubi_num
);
55 ubi_do_get_device_info(ubi
, di
);
59 EXPORT_SYMBOL_GPL(ubi_get_device_info
);
62 * ubi_do_get_volume_info - get information about UBI volume.
63 * @ubi: UBI device description object
64 * @vol: volume description object
65 * @vi: the information is stored here
67 void ubi_do_get_volume_info(struct ubi_device
*ubi
, struct ubi_volume
*vol
,
68 struct ubi_volume_info
*vi
)
70 vi
->vol_id
= vol
->vol_id
;
71 vi
->ubi_num
= ubi
->ubi_num
;
72 vi
->size
= vol
->reserved_pebs
;
73 vi
->used_bytes
= vol
->used_bytes
;
74 vi
->vol_type
= vol
->vol_type
;
75 vi
->corrupted
= vol
->corrupted
;
76 vi
->upd_marker
= vol
->upd_marker
;
77 vi
->alignment
= vol
->alignment
;
78 vi
->usable_leb_size
= vol
->usable_leb_size
;
79 vi
->name_len
= vol
->name_len
;
81 vi
->cdev
= vol
->cdev
.dev
;
85 * ubi_get_volume_info - get information about UBI volume.
86 * @desc: volume descriptor
87 * @vi: the information is stored here
89 void ubi_get_volume_info(struct ubi_volume_desc
*desc
,
90 struct ubi_volume_info
*vi
)
92 ubi_do_get_volume_info(desc
->vol
->ubi
, desc
->vol
, vi
);
94 EXPORT_SYMBOL_GPL(ubi_get_volume_info
);
97 * ubi_open_volume - open UBI volume.
98 * @ubi_num: UBI device number
102 * The @mode parameter specifies if the volume should be opened in read-only
103 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
104 * nobody else will be able to open this volume. UBI allows to have many volume
105 * readers and one writer at a time.
107 * If a static volume is being opened for the first time since boot, it will be
108 * checked by this function, which means it will be fully read and the CRC
109 * checksum of each logical eraseblock will be checked.
111 * This function returns volume descriptor in case of success and a negative
112 * error code in case of failure.
114 struct ubi_volume_desc
*ubi_open_volume(int ubi_num
, int vol_id
, int mode
)
117 struct ubi_volume_desc
*desc
;
118 struct ubi_device
*ubi
;
119 struct ubi_volume
*vol
;
121 dbg_gen("open device %d, volume %d, mode %d", ubi_num
, vol_id
, mode
);
123 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
124 return ERR_PTR(-EINVAL
);
126 if (mode
!= UBI_READONLY
&& mode
!= UBI_READWRITE
&&
127 mode
!= UBI_EXCLUSIVE
&& mode
!= UBI_METAONLY
)
128 return ERR_PTR(-EINVAL
);
131 * First of all, we have to get the UBI device to prevent its removal.
133 ubi
= ubi_get_device(ubi_num
);
135 return ERR_PTR(-ENODEV
);
137 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
) {
142 desc
= kmalloc(sizeof(struct ubi_volume_desc
), GFP_KERNEL
);
149 if (!try_module_get(THIS_MODULE
))
152 spin_lock(&ubi
->volumes_lock
);
153 vol
= ubi
->volumes
[vol_id
];
166 if (vol
->exclusive
|| vol
->writers
> 0)
172 if (vol
->exclusive
|| vol
->writers
|| vol
->readers
||
179 if (vol
->metaonly
|| vol
->exclusive
)
184 get_device(&vol
->dev
);
186 spin_unlock(&ubi
->volumes_lock
);
191 mutex_lock(&ubi
->ckvol_mutex
);
192 if (!vol
->checked
&& !vol
->skip_check
) {
193 /* This is the first open - check the volume */
194 err
= ubi_check_volume(ubi
, vol_id
);
196 mutex_unlock(&ubi
->ckvol_mutex
);
197 ubi_close_volume(desc
);
201 ubi_warn(ubi
, "volume %d on UBI device %d is corrupted",
202 vol_id
, ubi
->ubi_num
);
207 mutex_unlock(&ubi
->ckvol_mutex
);
212 spin_unlock(&ubi
->volumes_lock
);
213 module_put(THIS_MODULE
);
217 ubi_err(ubi
, "cannot open device %d, volume %d, error %d",
218 ubi_num
, vol_id
, err
);
222 EXPORT_SYMBOL_GPL(ubi_open_volume
);
225 * ubi_open_volume_nm - open UBI volume by name.
226 * @ubi_num: UBI device number
230 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
232 struct ubi_volume_desc
*ubi_open_volume_nm(int ubi_num
, const char *name
,
235 int i
, vol_id
= -1, len
;
236 struct ubi_device
*ubi
;
237 struct ubi_volume_desc
*ret
;
239 dbg_gen("open device %d, volume %s, mode %d", ubi_num
, name
, mode
);
242 return ERR_PTR(-EINVAL
);
244 len
= strnlen(name
, UBI_VOL_NAME_MAX
+ 1);
245 if (len
> UBI_VOL_NAME_MAX
)
246 return ERR_PTR(-EINVAL
);
248 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
249 return ERR_PTR(-EINVAL
);
251 ubi
= ubi_get_device(ubi_num
);
253 return ERR_PTR(-ENODEV
);
255 spin_lock(&ubi
->volumes_lock
);
256 /* Walk all volumes of this UBI device */
257 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
258 struct ubi_volume
*vol
= ubi
->volumes
[i
];
260 if (vol
&& len
== vol
->name_len
&& !strcmp(name
, vol
->name
)) {
265 spin_unlock(&ubi
->volumes_lock
);
268 ret
= ubi_open_volume(ubi_num
, vol_id
, mode
);
270 ret
= ERR_PTR(-ENODEV
);
273 * We should put the UBI device even in case of success, because
274 * 'ubi_open_volume()' took a reference as well.
279 EXPORT_SYMBOL_GPL(ubi_open_volume_nm
);
282 * ubi_open_volume_path - open UBI volume by its character device node path.
283 * @pathname: volume character device node path
286 * This function is similar to 'ubi_open_volume()', but opens a volume the path
287 * to its character device node.
289 struct ubi_volume_desc
*ubi_open_volume_path(const char *pathname
, int mode
)
291 int error
, ubi_num
, vol_id
;
295 dbg_gen("open volume %s, mode %d", pathname
, mode
);
297 if (!pathname
|| !*pathname
)
298 return ERR_PTR(-EINVAL
);
300 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
302 return ERR_PTR(error
);
304 error
= vfs_getattr(&path
, &stat
, STATX_TYPE
, AT_STATX_SYNC_AS_STAT
);
307 return ERR_PTR(error
);
309 if (!S_ISCHR(stat
.mode
))
310 return ERR_PTR(-EINVAL
);
312 ubi_num
= ubi_major2num(MAJOR(stat
.rdev
));
313 vol_id
= MINOR(stat
.rdev
) - 1;
315 if (vol_id
>= 0 && ubi_num
>= 0)
316 return ubi_open_volume(ubi_num
, vol_id
, mode
);
317 return ERR_PTR(-ENODEV
);
319 EXPORT_SYMBOL_GPL(ubi_open_volume_path
);
322 * ubi_close_volume - close UBI volume.
323 * @desc: volume descriptor
325 void ubi_close_volume(struct ubi_volume_desc
*desc
)
327 struct ubi_volume
*vol
= desc
->vol
;
328 struct ubi_device
*ubi
= vol
->ubi
;
330 dbg_gen("close device %d, volume %d, mode %d",
331 ubi
->ubi_num
, vol
->vol_id
, desc
->mode
);
333 spin_lock(&ubi
->volumes_lock
);
334 switch (desc
->mode
) {
349 spin_unlock(&ubi
->volumes_lock
);
352 put_device(&vol
->dev
);
354 module_put(THIS_MODULE
);
356 EXPORT_SYMBOL_GPL(ubi_close_volume
);
359 * leb_read_sanity_check - does sanity checks on read requests.
360 * @desc: volume descriptor
361 * @lnum: logical eraseblock number to read from
362 * @offset: offset within the logical eraseblock to read from
363 * @len: how many bytes to read
365 * This function is used by ubi_leb_read() and ubi_leb_read_sg()
366 * to perform sanity checks.
368 static int leb_read_sanity_check(struct ubi_volume_desc
*desc
, int lnum
,
371 struct ubi_volume
*vol
= desc
->vol
;
372 struct ubi_device
*ubi
= vol
->ubi
;
373 int vol_id
= vol
->vol_id
;
375 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
|| lnum
< 0 ||
376 lnum
>= vol
->used_ebs
|| offset
< 0 || len
< 0 ||
377 offset
+ len
> vol
->usable_leb_size
)
380 if (vol
->vol_type
== UBI_STATIC_VOLUME
) {
381 if (vol
->used_ebs
== 0)
382 /* Empty static UBI volume */
384 if (lnum
== vol
->used_ebs
- 1 &&
385 offset
+ len
> vol
->last_eb_bytes
)
396 * ubi_leb_read - read data.
397 * @desc: volume descriptor
398 * @lnum: logical eraseblock number to read from
399 * @buf: buffer where to store the read data
400 * @offset: offset within the logical eraseblock to read from
401 * @len: how many bytes to read
402 * @check: whether UBI has to check the read data's CRC or not.
404 * This function reads data from offset @offset of logical eraseblock @lnum and
405 * stores the data at @buf. When reading from static volumes, @check specifies
406 * whether the data has to be checked or not. If yes, the whole logical
407 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
408 * checksum is per-eraseblock). So checking may substantially slow down the
409 * read speed. The @check argument is ignored for dynamic volumes.
411 * In case of success, this function returns zero. In case of failure, this
412 * function returns a negative error code.
414 * %-EBADMSG error code is returned:
415 * o for both static and dynamic volumes if MTD driver has detected a data
416 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
417 * o for static volumes in case of data CRC mismatch.
419 * If the volume is damaged because of an interrupted update this function just
420 * returns immediately with %-EBADF error code.
422 int ubi_leb_read(struct ubi_volume_desc
*desc
, int lnum
, char *buf
, int offset
,
425 struct ubi_volume
*vol
= desc
->vol
;
426 struct ubi_device
*ubi
= vol
->ubi
;
427 int err
, vol_id
= vol
->vol_id
;
429 dbg_gen("read %d bytes from LEB %d:%d:%d", len
, vol_id
, lnum
, offset
);
431 err
= leb_read_sanity_check(desc
, lnum
, offset
, len
);
438 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, buf
, offset
, len
, check
);
439 if (err
&& mtd_is_eccerr(err
) && vol
->vol_type
== UBI_STATIC_VOLUME
) {
440 ubi_warn(ubi
, "mark volume %d as corrupted", vol_id
);
446 EXPORT_SYMBOL_GPL(ubi_leb_read
);
450 * ubi_leb_read_sg - read data into a scatter gather list.
451 * @desc: volume descriptor
452 * @lnum: logical eraseblock number to read from
453 * @sgl: UBI scatter gather list to store the read data
454 * @offset: offset within the logical eraseblock to read from
455 * @len: how many bytes to read
456 * @check: whether UBI has to check the read data's CRC or not.
458 * This function works exactly like ubi_leb_read_sg(). But instead of
459 * storing the read data into a buffer it writes to an UBI scatter gather
462 int ubi_leb_read_sg(struct ubi_volume_desc
*desc
, int lnum
, struct ubi_sgl
*sgl
,
463 int offset
, int len
, int check
)
465 struct ubi_volume
*vol
= desc
->vol
;
466 struct ubi_device
*ubi
= vol
->ubi
;
467 int err
, vol_id
= vol
->vol_id
;
469 dbg_gen("read %d bytes from LEB %d:%d:%d", len
, vol_id
, lnum
, offset
);
471 err
= leb_read_sanity_check(desc
, lnum
, offset
, len
);
478 err
= ubi_eba_read_leb_sg(ubi
, vol
, sgl
, lnum
, offset
, len
, check
);
479 if (err
&& mtd_is_eccerr(err
) && vol
->vol_type
== UBI_STATIC_VOLUME
) {
480 ubi_warn(ubi
, "mark volume %d as corrupted", vol_id
);
486 EXPORT_SYMBOL_GPL(ubi_leb_read_sg
);
489 * ubi_leb_write - write data.
490 * @desc: volume descriptor
491 * @lnum: logical eraseblock number to write to
492 * @buf: data to write
493 * @offset: offset within the logical eraseblock where to write
494 * @len: how many bytes to write
496 * This function writes @len bytes of data from @buf to offset @offset of
497 * logical eraseblock @lnum.
499 * This function takes care of physical eraseblock write failures. If write to
500 * the physical eraseblock write operation fails, the logical eraseblock is
501 * re-mapped to another physical eraseblock, the data is recovered, and the
502 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
504 * If all the data were successfully written, zero is returned. If an error
505 * occurred and UBI has not been able to recover from it, this function returns
506 * a negative error code. Note, in case of an error, it is possible that
507 * something was still written to the flash media, but that may be some
510 * If the volume is damaged because of an interrupted update this function just
511 * returns immediately with %-EBADF code.
513 int ubi_leb_write(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
516 struct ubi_volume
*vol
= desc
->vol
;
517 struct ubi_device
*ubi
= vol
->ubi
;
518 int vol_id
= vol
->vol_id
;
520 dbg_gen("write %d bytes to LEB %d:%d:%d", len
, vol_id
, lnum
, offset
);
522 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
)
525 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
528 if (!ubi_leb_valid(vol
, lnum
) || offset
< 0 || len
< 0 ||
529 offset
+ len
> vol
->usable_leb_size
||
530 offset
& (ubi
->min_io_size
- 1) || len
& (ubi
->min_io_size
- 1))
539 return ubi_eba_write_leb(ubi
, vol
, lnum
, buf
, offset
, len
);
541 EXPORT_SYMBOL_GPL(ubi_leb_write
);
544 * ubi_leb_change - change logical eraseblock atomically.
545 * @desc: volume descriptor
546 * @lnum: logical eraseblock number to change
547 * @buf: data to write
548 * @len: how many bytes to write
550 * This function changes the contents of a logical eraseblock atomically. @buf
551 * has to contain new logical eraseblock data, and @len - the length of the
552 * data, which has to be aligned. The length may be shorter than the logical
553 * eraseblock size, ant the logical eraseblock may be appended to more times
554 * later on. This function guarantees that in case of an unclean reboot the old
555 * contents is preserved. Returns zero in case of success and a negative error
556 * code in case of failure.
558 int ubi_leb_change(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
561 struct ubi_volume
*vol
= desc
->vol
;
562 struct ubi_device
*ubi
= vol
->ubi
;
563 int vol_id
= vol
->vol_id
;
565 dbg_gen("atomically write %d bytes to LEB %d:%d", len
, vol_id
, lnum
);
567 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
)
570 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
573 if (!ubi_leb_valid(vol
, lnum
) || len
< 0 ||
574 len
> vol
->usable_leb_size
|| len
& (ubi
->min_io_size
- 1))
583 return ubi_eba_atomic_leb_change(ubi
, vol
, lnum
, buf
, len
);
585 EXPORT_SYMBOL_GPL(ubi_leb_change
);
588 * ubi_leb_erase - erase logical eraseblock.
589 * @desc: volume descriptor
590 * @lnum: logical eraseblock number
592 * This function un-maps logical eraseblock @lnum and synchronously erases the
593 * correspondent physical eraseblock. Returns zero in case of success and a
594 * negative error code in case of failure.
596 * If the volume is damaged because of an interrupted update this function just
597 * returns immediately with %-EBADF code.
599 int ubi_leb_erase(struct ubi_volume_desc
*desc
, int lnum
)
601 struct ubi_volume
*vol
= desc
->vol
;
602 struct ubi_device
*ubi
= vol
->ubi
;
605 dbg_gen("erase LEB %d:%d", vol
->vol_id
, lnum
);
607 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
610 if (!ubi_leb_valid(vol
, lnum
))
616 err
= ubi_eba_unmap_leb(ubi
, vol
, lnum
);
620 return ubi_wl_flush(ubi
, vol
->vol_id
, lnum
);
622 EXPORT_SYMBOL_GPL(ubi_leb_erase
);
625 * ubi_leb_unmap - un-map logical eraseblock.
626 * @desc: volume descriptor
627 * @lnum: logical eraseblock number
629 * This function un-maps logical eraseblock @lnum and schedules the
630 * corresponding physical eraseblock for erasure, so that it will eventually be
631 * physically erased in background. This operation is much faster than the
634 * Unlike erase, the un-map operation does not guarantee that the logical
635 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
636 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
637 * happens after this, the logical eraseblocks will not necessarily be
638 * un-mapped again when this MTD device is attached. They may actually be
639 * mapped to the same physical eraseblocks again. So, this function has to be
642 * In other words, when un-mapping a logical eraseblock, UBI does not store
643 * any information about this on the flash media, it just marks the logical
644 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
645 * eraseblock is physically erased, it will be mapped again to the same logical
646 * eraseblock when the MTD device is attached again.
648 * The main and obvious use-case of this function is when the contents of a
649 * logical eraseblock has to be re-written. Then it is much more efficient to
650 * first un-map it, then write new data, rather than first erase it, then write
651 * new data. Note, once new data has been written to the logical eraseblock,
652 * UBI guarantees that the old contents has gone forever. In other words, if an
653 * unclean reboot happens after the logical eraseblock has been un-mapped and
654 * then written to, it will contain the last written data.
656 * This function returns zero in case of success and a negative error code in
657 * case of failure. If the volume is damaged because of an interrupted update
658 * this function just returns immediately with %-EBADF code.
660 int ubi_leb_unmap(struct ubi_volume_desc
*desc
, int lnum
)
662 struct ubi_volume
*vol
= desc
->vol
;
663 struct ubi_device
*ubi
= vol
->ubi
;
665 dbg_gen("unmap LEB %d:%d", vol
->vol_id
, lnum
);
667 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
670 if (!ubi_leb_valid(vol
, lnum
))
676 return ubi_eba_unmap_leb(ubi
, vol
, lnum
);
678 EXPORT_SYMBOL_GPL(ubi_leb_unmap
);
681 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
682 * @desc: volume descriptor
683 * @lnum: logical eraseblock number
685 * This function maps an un-mapped logical eraseblock @lnum to a physical
686 * eraseblock. This means, that after a successful invocation of this
687 * function the logical eraseblock @lnum will be empty (contain only %0xFF
688 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
691 * This function returns zero in case of success, %-EBADF if the volume is
692 * damaged because of an interrupted update, %-EBADMSG if the logical
693 * eraseblock is already mapped, and other negative error codes in case of
696 int ubi_leb_map(struct ubi_volume_desc
*desc
, int lnum
)
698 struct ubi_volume
*vol
= desc
->vol
;
699 struct ubi_device
*ubi
= vol
->ubi
;
701 dbg_gen("map LEB %d:%d", vol
->vol_id
, lnum
);
703 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
706 if (!ubi_leb_valid(vol
, lnum
))
712 if (ubi_eba_is_mapped(vol
, lnum
))
715 return ubi_eba_write_leb(ubi
, vol
, lnum
, NULL
, 0, 0);
717 EXPORT_SYMBOL_GPL(ubi_leb_map
);
720 * ubi_is_mapped - check if logical eraseblock is mapped.
721 * @desc: volume descriptor
722 * @lnum: logical eraseblock number
724 * This function checks if logical eraseblock @lnum is mapped to a physical
725 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
726 * mean it will still be un-mapped after the UBI device is re-attached. The
727 * logical eraseblock may become mapped to the physical eraseblock it was last
730 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
731 * error code in case of failure. If the volume is damaged because of an
732 * interrupted update this function just returns immediately with %-EBADF error
735 int ubi_is_mapped(struct ubi_volume_desc
*desc
, int lnum
)
737 struct ubi_volume
*vol
= desc
->vol
;
739 dbg_gen("test LEB %d:%d", vol
->vol_id
, lnum
);
741 if (!ubi_leb_valid(vol
, lnum
))
747 return ubi_eba_is_mapped(vol
, lnum
);
749 EXPORT_SYMBOL_GPL(ubi_is_mapped
);
752 * ubi_sync - synchronize UBI device buffers.
753 * @ubi_num: UBI device to synchronize
755 * The underlying MTD device may cache data in hardware or in software. This
756 * function ensures the caches are flushed. Returns zero in case of success and
757 * a negative error code in case of failure.
759 int ubi_sync(int ubi_num
)
761 struct ubi_device
*ubi
;
763 ubi
= ubi_get_device(ubi_num
);
771 EXPORT_SYMBOL_GPL(ubi_sync
);
774 * ubi_flush - flush UBI work queue.
775 * @ubi_num: UBI device to flush work queue
776 * @vol_id: volume id to flush for
777 * @lnum: logical eraseblock number to flush for
779 * This function executes all pending works for a particular volume id / logical
780 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
781 * a wildcard for all of the corresponding volume numbers or logical
782 * eraseblock numbers. It returns zero in case of success and a negative error
783 * code in case of failure.
785 int ubi_flush(int ubi_num
, int vol_id
, int lnum
)
787 struct ubi_device
*ubi
;
790 ubi
= ubi_get_device(ubi_num
);
794 err
= ubi_wl_flush(ubi
, vol_id
, lnum
);
798 EXPORT_SYMBOL_GPL(ubi_flush
);
800 BLOCKING_NOTIFIER_HEAD(ubi_notifiers
);
803 * ubi_register_volume_notifier - register a volume notifier.
804 * @nb: the notifier description object
805 * @ignore_existing: if non-zero, do not send "added" notification for all
806 * already existing volumes
808 * This function registers a volume notifier, which means that
809 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
810 * removed, re-sized, re-named, or updated. The first argument of the function
811 * is the notification type. The second argument is pointer to a
812 * &struct ubi_notification object which describes the notification event.
813 * Using UBI API from the volume notifier is prohibited.
815 * This function returns zero in case of success and a negative error code
816 * in case of failure.
818 int ubi_register_volume_notifier(struct notifier_block
*nb
,
823 err
= blocking_notifier_chain_register(&ubi_notifiers
, nb
);
830 * We are going to walk all UBI devices and all volumes, and
831 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
832 * event. We have to lock the @ubi_devices_mutex to make sure UBI
833 * devices do not disappear.
835 mutex_lock(&ubi_devices_mutex
);
836 ubi_enumerate_volumes(nb
);
837 mutex_unlock(&ubi_devices_mutex
);
841 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier
);
844 * ubi_unregister_volume_notifier - unregister the volume notifier.
845 * @nb: the notifier description object
847 * This function unregisters volume notifier @nm and returns zero in case of
848 * success and a negative error code in case of failure.
850 int ubi_unregister_volume_notifier(struct notifier_block
*nb
)
852 return blocking_notifier_chain_unregister(&ubi_notifiers
, nb
);
854 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier
);