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