Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / mtd / ubi / kapi.c
blobf1ea8677467fbfc6f3585d54544a50ed2c17c99d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2006
5 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
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>
14 #include <linux/fs.h>
15 #include <asm/div64.h>
16 #include "ubi.h"
18 /**
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);
38 /**
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)
51 return -EINVAL;
52 ubi = ubi_get_device(ubi_num);
53 if (!ubi)
54 return -ENODEV;
55 ubi_do_get_device_info(ubi, di);
56 ubi_put_device(ubi);
57 return 0;
59 EXPORT_SYMBOL_GPL(ubi_get_device_info);
61 /**
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;
80 vi->name = vol->name;
81 vi->cdev = vol->cdev.dev;
82 vi->dev = &vol->dev;
85 /**
86 * ubi_get_volume_info - get information about UBI volume.
87 * @desc: volume descriptor
88 * @vi: the information is stored here
90 void ubi_get_volume_info(struct ubi_volume_desc *desc,
91 struct ubi_volume_info *vi)
93 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
95 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
97 /**
98 * ubi_open_volume - open UBI volume.
99 * @ubi_num: UBI device number
100 * @vol_id: volume ID
101 * @mode: open mode
103 * The @mode parameter specifies if the volume should be opened in read-only
104 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
105 * nobody else will be able to open this volume. UBI allows to have many volume
106 * readers and one writer at a time.
108 * If a static volume is being opened for the first time since boot, it will be
109 * checked by this function, which means it will be fully read and the CRC
110 * checksum of each logical eraseblock will be checked.
112 * This function returns volume descriptor in case of success and a negative
113 * error code in case of failure.
115 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
117 int err;
118 struct ubi_volume_desc *desc;
119 struct ubi_device *ubi;
120 struct ubi_volume *vol;
122 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
124 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
125 return ERR_PTR(-EINVAL);
127 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
128 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
129 return ERR_PTR(-EINVAL);
132 * First of all, we have to get the UBI device to prevent its removal.
134 ubi = ubi_get_device(ubi_num);
135 if (!ubi)
136 return ERR_PTR(-ENODEV);
138 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
139 err = -EINVAL;
140 goto out_put_ubi;
143 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
144 if (!desc) {
145 err = -ENOMEM;
146 goto out_put_ubi;
149 err = -ENODEV;
150 if (!try_module_get(THIS_MODULE))
151 goto out_free;
153 spin_lock(&ubi->volumes_lock);
154 vol = ubi->volumes[vol_id];
155 if (!vol || vol->is_dead)
156 goto out_unlock;
158 err = -EBUSY;
159 switch (mode) {
160 case UBI_READONLY:
161 if (vol->exclusive)
162 goto out_unlock;
163 vol->readers += 1;
164 break;
166 case UBI_READWRITE:
167 if (vol->exclusive || vol->writers > 0)
168 goto out_unlock;
169 vol->writers += 1;
170 break;
172 case UBI_EXCLUSIVE:
173 if (vol->exclusive || vol->writers || vol->readers ||
174 vol->metaonly)
175 goto out_unlock;
176 vol->exclusive = 1;
177 break;
179 case UBI_METAONLY:
180 if (vol->metaonly || vol->exclusive)
181 goto out_unlock;
182 vol->metaonly = 1;
183 break;
185 get_device(&vol->dev);
186 vol->ref_count += 1;
187 spin_unlock(&ubi->volumes_lock);
189 desc->vol = vol;
190 desc->mode = mode;
192 mutex_lock(&ubi->ckvol_mutex);
193 if (!vol->checked && !vol->skip_check) {
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);
201 if (err == 1) {
202 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
203 vol_id, ubi->ubi_num);
204 vol->corrupted = 1;
206 vol->checked = 1;
208 mutex_unlock(&ubi->ckvol_mutex);
210 return desc;
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_err(ubi, "cannot open device %d, volume %d, error %d",
219 ubi_num, vol_id, err);
220 ubi_put_device(ubi);
221 return ERR_PTR(err);
223 EXPORT_SYMBOL_GPL(ubi_open_volume);
226 * ubi_open_volume_nm - open UBI volume by name.
227 * @ubi_num: UBI device number
228 * @name: volume name
229 * @mode: open mode
231 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
233 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
234 int mode)
236 int i, vol_id = -1, len;
237 struct ubi_device *ubi;
238 struct ubi_volume_desc *ret;
240 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
242 if (!name)
243 return ERR_PTR(-EINVAL);
245 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
246 if (len > UBI_VOL_NAME_MAX)
247 return ERR_PTR(-EINVAL);
249 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
250 return ERR_PTR(-EINVAL);
252 ubi = ubi_get_device(ubi_num);
253 if (!ubi)
254 return ERR_PTR(-ENODEV);
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];
261 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
262 vol_id = i;
263 break;
266 spin_unlock(&ubi->volumes_lock);
268 if (vol_id >= 0)
269 ret = ubi_open_volume(ubi_num, vol_id, mode);
270 else
271 ret = ERR_PTR(-ENODEV);
274 * We should put the UBI device even in case of success, because
275 * 'ubi_open_volume()' took a reference as well.
277 ubi_put_device(ubi);
278 return ret;
280 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
283 * ubi_get_num_by_path - get UBI device and volume number from device path
284 * @pathname: volume character device node path
285 * @ubi_num: pointer to UBI device number to be set
286 * @vol_id: pointer to UBI volume ID to be set
288 * Returns 0 on success and sets ubi_num and vol_id, returns error otherwise.
290 int ubi_get_num_by_path(const char *pathname, int *ubi_num, int *vol_id)
292 int error;
293 struct path path;
294 struct kstat stat;
296 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
297 if (error)
298 return error;
300 error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
301 path_put(&path);
302 if (error)
303 return error;
305 if (!S_ISCHR(stat.mode))
306 return -EINVAL;
308 *ubi_num = ubi_major2num(MAJOR(stat.rdev));
309 *vol_id = MINOR(stat.rdev) - 1;
311 if (*vol_id < 0 || *ubi_num < 0)
312 return -ENODEV;
314 return 0;
318 * ubi_open_volume_path - open UBI volume by its character device node path.
319 * @pathname: volume character device node path
320 * @mode: open mode
322 * This function is similar to 'ubi_open_volume()', but opens a volume the path
323 * to its character device node.
325 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
327 int error, ubi_num, vol_id;
329 dbg_gen("open volume %s, mode %d", pathname, mode);
331 if (!pathname || !*pathname)
332 return ERR_PTR(-EINVAL);
334 error = ubi_get_num_by_path(pathname, &ubi_num, &vol_id);
335 if (error)
336 return ERR_PTR(error);
338 return ubi_open_volume(ubi_num, vol_id, mode);
340 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
343 * ubi_close_volume - close UBI volume.
344 * @desc: volume descriptor
346 void ubi_close_volume(struct ubi_volume_desc *desc)
348 struct ubi_volume *vol = desc->vol;
349 struct ubi_device *ubi = vol->ubi;
351 dbg_gen("close device %d, volume %d, mode %d",
352 ubi->ubi_num, vol->vol_id, desc->mode);
354 spin_lock(&ubi->volumes_lock);
355 switch (desc->mode) {
356 case UBI_READONLY:
357 vol->readers -= 1;
358 break;
359 case UBI_READWRITE:
360 vol->writers -= 1;
361 break;
362 case UBI_EXCLUSIVE:
363 vol->exclusive = 0;
364 break;
365 case UBI_METAONLY:
366 vol->metaonly = 0;
367 break;
369 vol->ref_count -= 1;
370 spin_unlock(&ubi->volumes_lock);
372 kfree(desc);
373 put_device(&vol->dev);
374 ubi_put_device(ubi);
375 module_put(THIS_MODULE);
377 EXPORT_SYMBOL_GPL(ubi_close_volume);
380 * leb_read_sanity_check - does sanity checks on read requests.
381 * @desc: volume descriptor
382 * @lnum: logical eraseblock number to read from
383 * @offset: offset within the logical eraseblock to read from
384 * @len: how many bytes to read
386 * This function is used by ubi_leb_read() and ubi_leb_read_sg()
387 * to perform sanity checks.
389 static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
390 int offset, int len)
392 struct ubi_volume *vol = desc->vol;
393 struct ubi_device *ubi = vol->ubi;
394 int vol_id = vol->vol_id;
396 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
397 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
398 offset + len > vol->usable_leb_size)
399 return -EINVAL;
401 if (vol->vol_type == UBI_STATIC_VOLUME) {
402 if (vol->used_ebs == 0)
403 /* Empty static UBI volume */
404 return 0;
405 if (lnum == vol->used_ebs - 1 &&
406 offset + len > vol->last_eb_bytes)
407 return -EINVAL;
410 if (vol->upd_marker)
411 return -EBADF;
413 return 0;
417 * ubi_leb_read - read data.
418 * @desc: volume descriptor
419 * @lnum: logical eraseblock number to read from
420 * @buf: buffer where to store the read data
421 * @offset: offset within the logical eraseblock to read from
422 * @len: how many bytes to read
423 * @check: whether UBI has to check the read data's CRC or not.
425 * This function reads data from offset @offset of logical eraseblock @lnum and
426 * stores the data at @buf. When reading from static volumes, @check specifies
427 * whether the data has to be checked or not. If yes, the whole logical
428 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
429 * checksum is per-eraseblock). So checking may substantially slow down the
430 * read speed. The @check argument is ignored for dynamic volumes.
432 * In case of success, this function returns zero. In case of failure, this
433 * function returns a negative error code.
435 * %-EBADMSG error code is returned:
436 * o for both static and dynamic volumes if MTD driver has detected a data
437 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
438 * o for static volumes in case of data CRC mismatch.
440 * If the volume is damaged because of an interrupted update this function just
441 * returns immediately with %-EBADF error code.
443 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
444 int len, int check)
446 struct ubi_volume *vol = desc->vol;
447 struct ubi_device *ubi = vol->ubi;
448 int err, vol_id = vol->vol_id;
450 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
452 err = leb_read_sanity_check(desc, lnum, offset, len);
453 if (err < 0)
454 return err;
456 if (len == 0)
457 return 0;
459 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
460 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
461 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
462 vol->corrupted = 1;
465 return err;
467 EXPORT_SYMBOL_GPL(ubi_leb_read);
471 * ubi_leb_read_sg - read data into a scatter gather list.
472 * @desc: volume descriptor
473 * @lnum: logical eraseblock number to read from
474 * @sgl: UBI scatter gather list to store the read data
475 * @offset: offset within the logical eraseblock to read from
476 * @len: how many bytes to read
477 * @check: whether UBI has to check the read data's CRC or not.
479 * This function works exactly like ubi_leb_read_sg(). But instead of
480 * storing the read data into a buffer it writes to an UBI scatter gather
481 * list.
483 int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
484 int offset, int len, int check)
486 struct ubi_volume *vol = desc->vol;
487 struct ubi_device *ubi = vol->ubi;
488 int err, vol_id = vol->vol_id;
490 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
492 err = leb_read_sanity_check(desc, lnum, offset, len);
493 if (err < 0)
494 return err;
496 if (len == 0)
497 return 0;
499 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
500 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
501 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
502 vol->corrupted = 1;
505 return err;
507 EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
510 * ubi_leb_write - write data.
511 * @desc: volume descriptor
512 * @lnum: logical eraseblock number to write to
513 * @buf: data to write
514 * @offset: offset within the logical eraseblock where to write
515 * @len: how many bytes to write
517 * This function writes @len bytes of data from @buf to offset @offset of
518 * logical eraseblock @lnum.
520 * This function takes care of physical eraseblock write failures. If write to
521 * the physical eraseblock write operation fails, the logical eraseblock is
522 * re-mapped to another physical eraseblock, the data is recovered, and the
523 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
525 * If all the data were successfully written, zero is returned. If an error
526 * occurred and UBI has not been able to recover from it, this function returns
527 * a negative error code. Note, in case of an error, it is possible that
528 * something was still written to the flash media, but that may be some
529 * garbage.
531 * If the volume is damaged because of an interrupted update this function just
532 * returns immediately with %-EBADF code.
534 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
535 int offset, int len)
537 struct ubi_volume *vol = desc->vol;
538 struct ubi_device *ubi = vol->ubi;
539 int vol_id = vol->vol_id;
541 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
543 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
544 return -EINVAL;
546 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
547 return -EROFS;
549 if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
550 offset + len > vol->usable_leb_size ||
551 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
552 return -EINVAL;
554 if (vol->upd_marker)
555 return -EBADF;
557 if (len == 0)
558 return 0;
560 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
562 EXPORT_SYMBOL_GPL(ubi_leb_write);
565 * ubi_leb_change - change logical eraseblock atomically.
566 * @desc: volume descriptor
567 * @lnum: logical eraseblock number to change
568 * @buf: data to write
569 * @len: how many bytes to write
571 * This function changes the contents of a logical eraseblock atomically. @buf
572 * has to contain new logical eraseblock data, and @len - the length of the
573 * data, which has to be aligned. The length may be shorter than the logical
574 * eraseblock size, ant the logical eraseblock may be appended to more times
575 * later on. This function guarantees that in case of an unclean reboot the old
576 * contents is preserved. Returns zero in case of success and a negative error
577 * code in case of failure.
579 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
580 int len)
582 struct ubi_volume *vol = desc->vol;
583 struct ubi_device *ubi = vol->ubi;
584 int vol_id = vol->vol_id;
586 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
588 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
589 return -EINVAL;
591 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
592 return -EROFS;
594 if (!ubi_leb_valid(vol, lnum) || len < 0 ||
595 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
596 return -EINVAL;
598 if (vol->upd_marker)
599 return -EBADF;
601 if (len == 0)
602 return 0;
604 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
606 EXPORT_SYMBOL_GPL(ubi_leb_change);
609 * ubi_leb_erase - erase logical eraseblock.
610 * @desc: volume descriptor
611 * @lnum: logical eraseblock number
613 * This function un-maps logical eraseblock @lnum and synchronously erases the
614 * correspondent physical eraseblock. Returns zero in case of success and a
615 * negative error code in case of failure.
617 * If the volume is damaged because of an interrupted update this function just
618 * returns immediately with %-EBADF code.
620 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
622 struct ubi_volume *vol = desc->vol;
623 struct ubi_device *ubi = vol->ubi;
624 int err;
626 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
628 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
629 return -EROFS;
631 if (!ubi_leb_valid(vol, lnum))
632 return -EINVAL;
634 if (vol->upd_marker)
635 return -EBADF;
637 err = ubi_eba_unmap_leb(ubi, vol, lnum);
638 if (err)
639 return err;
641 return ubi_wl_flush(ubi, vol->vol_id, lnum);
643 EXPORT_SYMBOL_GPL(ubi_leb_erase);
646 * ubi_leb_unmap - un-map logical eraseblock.
647 * @desc: volume descriptor
648 * @lnum: logical eraseblock number
650 * This function un-maps logical eraseblock @lnum and schedules the
651 * corresponding physical eraseblock for erasure, so that it will eventually be
652 * physically erased in background. This operation is much faster than the
653 * erase operation.
655 * Unlike erase, the un-map operation does not guarantee that the logical
656 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
657 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
658 * happens after this, the logical eraseblocks will not necessarily be
659 * un-mapped again when this MTD device is attached. They may actually be
660 * mapped to the same physical eraseblocks again. So, this function has to be
661 * used with care.
663 * In other words, when un-mapping a logical eraseblock, UBI does not store
664 * any information about this on the flash media, it just marks the logical
665 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
666 * eraseblock is physically erased, it will be mapped again to the same logical
667 * eraseblock when the MTD device is attached again.
669 * The main and obvious use-case of this function is when the contents of a
670 * logical eraseblock has to be re-written. Then it is much more efficient to
671 * first un-map it, then write new data, rather than first erase it, then write
672 * new data. Note, once new data has been written to the logical eraseblock,
673 * UBI guarantees that the old contents has gone forever. In other words, if an
674 * unclean reboot happens after the logical eraseblock has been un-mapped and
675 * then written to, it will contain the last written data.
677 * This function returns zero in case of success and a negative error code in
678 * case of failure. If the volume is damaged because of an interrupted update
679 * this function just returns immediately with %-EBADF code.
681 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
683 struct ubi_volume *vol = desc->vol;
684 struct ubi_device *ubi = vol->ubi;
686 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
688 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
689 return -EROFS;
691 if (!ubi_leb_valid(vol, lnum))
692 return -EINVAL;
694 if (vol->upd_marker)
695 return -EBADF;
697 return ubi_eba_unmap_leb(ubi, vol, lnum);
699 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
702 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
703 * @desc: volume descriptor
704 * @lnum: logical eraseblock number
706 * This function maps an un-mapped logical eraseblock @lnum to a physical
707 * eraseblock. This means, that after a successful invocation of this
708 * function the logical eraseblock @lnum will be empty (contain only %0xFF
709 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
710 * happens.
712 * This function returns zero in case of success, %-EBADF if the volume is
713 * damaged because of an interrupted update, %-EBADMSG if the logical
714 * eraseblock is already mapped, and other negative error codes in case of
715 * other failures.
717 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
719 struct ubi_volume *vol = desc->vol;
720 struct ubi_device *ubi = vol->ubi;
722 dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
724 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
725 return -EROFS;
727 if (!ubi_leb_valid(vol, lnum))
728 return -EINVAL;
730 if (vol->upd_marker)
731 return -EBADF;
733 if (ubi_eba_is_mapped(vol, lnum))
734 return -EBADMSG;
736 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
738 EXPORT_SYMBOL_GPL(ubi_leb_map);
741 * ubi_is_mapped - check if logical eraseblock is mapped.
742 * @desc: volume descriptor
743 * @lnum: logical eraseblock number
745 * This function checks if logical eraseblock @lnum is mapped to a physical
746 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
747 * mean it will still be un-mapped after the UBI device is re-attached. The
748 * logical eraseblock may become mapped to the physical eraseblock it was last
749 * mapped to.
751 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
752 * error code in case of failure. If the volume is damaged because of an
753 * interrupted update this function just returns immediately with %-EBADF error
754 * code.
756 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
758 struct ubi_volume *vol = desc->vol;
760 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
762 if (!ubi_leb_valid(vol, lnum))
763 return -EINVAL;
765 if (vol->upd_marker)
766 return -EBADF;
768 return ubi_eba_is_mapped(vol, lnum);
770 EXPORT_SYMBOL_GPL(ubi_is_mapped);
773 * ubi_sync - synchronize UBI device buffers.
774 * @ubi_num: UBI device to synchronize
776 * The underlying MTD device may cache data in hardware or in software. This
777 * function ensures the caches are flushed. Returns zero in case of success and
778 * a negative error code in case of failure.
780 int ubi_sync(int ubi_num)
782 struct ubi_device *ubi;
784 ubi = ubi_get_device(ubi_num);
785 if (!ubi)
786 return -ENODEV;
788 mtd_sync(ubi->mtd);
789 ubi_put_device(ubi);
790 return 0;
792 EXPORT_SYMBOL_GPL(ubi_sync);
795 * ubi_flush - flush UBI work queue.
796 * @ubi_num: UBI device to flush work queue
797 * @vol_id: volume id to flush for
798 * @lnum: logical eraseblock number to flush for
800 * This function executes all pending works for a particular volume id / logical
801 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
802 * a wildcard for all of the corresponding volume numbers or logical
803 * eraseblock numbers. It returns zero in case of success and a negative error
804 * code in case of failure.
806 int ubi_flush(int ubi_num, int vol_id, int lnum)
808 struct ubi_device *ubi;
809 int err = 0;
811 ubi = ubi_get_device(ubi_num);
812 if (!ubi)
813 return -ENODEV;
815 err = ubi_wl_flush(ubi, vol_id, lnum);
816 ubi_put_device(ubi);
817 return err;
819 EXPORT_SYMBOL_GPL(ubi_flush);
821 BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
824 * ubi_register_volume_notifier - register a volume notifier.
825 * @nb: the notifier description object
826 * @ignore_existing: if non-zero, do not send "added" notification for all
827 * already existing volumes
829 * This function registers a volume notifier, which means that
830 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
831 * removed, re-sized, re-named, or updated. The first argument of the function
832 * is the notification type. The second argument is pointer to a
833 * &struct ubi_notification object which describes the notification event.
834 * Using UBI API from the volume notifier is prohibited.
836 * This function returns zero in case of success and a negative error code
837 * in case of failure.
839 int ubi_register_volume_notifier(struct notifier_block *nb,
840 int ignore_existing)
842 int err;
844 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
845 if (err != 0)
846 return err;
847 if (ignore_existing)
848 return 0;
851 * We are going to walk all UBI devices and all volumes, and
852 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
853 * event. We have to lock the @ubi_devices_mutex to make sure UBI
854 * devices do not disappear.
856 mutex_lock(&ubi_devices_mutex);
857 ubi_enumerate_volumes(nb);
858 mutex_unlock(&ubi_devices_mutex);
860 return err;
862 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
865 * ubi_unregister_volume_notifier - unregister the volume notifier.
866 * @nb: the notifier description object
868 * This function unregisters volume notifier @nm and returns zero in case of
869 * success and a negative error code in case of failure.
871 int ubi_unregister_volume_notifier(struct notifier_block *nb)
873 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
875 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);