Linux Audio

Check our new training course

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