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