xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision 960b35d06b6d6b377fd47a8a44e41a96f35ce485)
1801c135cSArtem B. Bityutskiy /*
2801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
3801c135cSArtem B. Bityutskiy  *
4801c135cSArtem B. Bityutskiy  * This program is free software; you can redistribute it and/or modify
5801c135cSArtem B. Bityutskiy  * it under the terms of the GNU General Public License as published by
6801c135cSArtem B. Bityutskiy  * the Free Software Foundation; either version 2 of the License, or
7801c135cSArtem B. Bityutskiy  * (at your option) any later version.
8801c135cSArtem B. Bityutskiy  *
9801c135cSArtem B. Bityutskiy  * This program is distributed in the hope that it will be useful,
10801c135cSArtem B. Bityutskiy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11801c135cSArtem B. Bityutskiy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12801c135cSArtem B. Bityutskiy  * the GNU General Public License for more details.
13801c135cSArtem B. Bityutskiy  *
14801c135cSArtem B. Bityutskiy  * You should have received a copy of the GNU General Public License
15801c135cSArtem B. Bityutskiy  * along with this program; if not, write to the Free Software
16801c135cSArtem B. Bityutskiy  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17801c135cSArtem B. Bityutskiy  *
18801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
19801c135cSArtem B. Bityutskiy  */
20801c135cSArtem B. Bityutskiy 
21801c135cSArtem B. Bityutskiy /* This file mostly implements UBI kernel API functions */
22801c135cSArtem B. Bityutskiy 
23801c135cSArtem B. Bityutskiy #include <linux/module.h>
24801c135cSArtem B. Bityutskiy #include <linux/err.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
26b5710284SCorentin Chary #include <linux/namei.h>
27b5710284SCorentin Chary #include <linux/fs.h>
28801c135cSArtem B. Bityutskiy #include <asm/div64.h>
29801c135cSArtem B. Bityutskiy #include "ubi.h"
30801c135cSArtem B. Bityutskiy 
31801c135cSArtem B. Bityutskiy /**
320e0ee1ccSDmitry Pervushin  * ubi_do_get_device_info - get information about UBI device.
330e0ee1ccSDmitry Pervushin  * @ubi: UBI device description object
340e0ee1ccSDmitry Pervushin  * @di: the information is stored here
350e0ee1ccSDmitry Pervushin  *
360e0ee1ccSDmitry Pervushin  * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
370e0ee1ccSDmitry Pervushin  * device is locked and cannot disappear.
380e0ee1ccSDmitry Pervushin  */
390e0ee1ccSDmitry Pervushin void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
400e0ee1ccSDmitry Pervushin {
410e0ee1ccSDmitry Pervushin 	di->ubi_num = ubi->ubi_num;
420e0ee1ccSDmitry Pervushin 	di->leb_size = ubi->leb_size;
43f43ec882SArtem Bityutskiy 	di->leb_start = ubi->leb_start;
440e0ee1ccSDmitry Pervushin 	di->min_io_size = ubi->min_io_size;
4530b542efSArtem Bityutskiy 	di->max_write_size = ubi->max_write_size;
460e0ee1ccSDmitry Pervushin 	di->ro_mode = ubi->ro_mode;
470e0ee1ccSDmitry Pervushin 	di->cdev = ubi->cdev.dev;
480e0ee1ccSDmitry Pervushin }
490e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
500e0ee1ccSDmitry Pervushin 
510e0ee1ccSDmitry Pervushin /**
52801c135cSArtem B. Bityutskiy  * ubi_get_device_info - get information about UBI device.
53801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
54801c135cSArtem B. Bityutskiy  * @di: the information is stored here
55801c135cSArtem B. Bityutskiy  *
56e73f4459SArtem Bityutskiy  * This function returns %0 in case of success, %-EINVAL if the UBI device
57e73f4459SArtem Bityutskiy  * number is invalid, and %-ENODEV if there is no such UBI device.
58801c135cSArtem B. Bityutskiy  */
59801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
60801c135cSArtem B. Bityutskiy {
61e73f4459SArtem Bityutskiy 	struct ubi_device *ubi;
62801c135cSArtem B. Bityutskiy 
63e73f4459SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
64e73f4459SArtem Bityutskiy 		return -EINVAL;
65e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
66e73f4459SArtem Bityutskiy 	if (!ubi)
67801c135cSArtem B. Bityutskiy 		return -ENODEV;
680e0ee1ccSDmitry Pervushin 	ubi_do_get_device_info(ubi, di);
69e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
70801c135cSArtem B. Bityutskiy 	return 0;
71801c135cSArtem B. Bityutskiy }
72801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info);
73801c135cSArtem B. Bityutskiy 
74801c135cSArtem B. Bityutskiy /**
750e0ee1ccSDmitry Pervushin  * ubi_do_get_volume_info - get information about UBI volume.
760e0ee1ccSDmitry Pervushin  * @ubi: UBI device description object
770e0ee1ccSDmitry Pervushin  * @vol: volume description object
78801c135cSArtem B. Bityutskiy  * @vi: the information is stored here
79801c135cSArtem B. Bityutskiy  */
800e0ee1ccSDmitry Pervushin void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
81801c135cSArtem B. Bityutskiy 			    struct ubi_volume_info *vi)
82801c135cSArtem B. Bityutskiy {
83801c135cSArtem B. Bityutskiy 	vi->vol_id = vol->vol_id;
84801c135cSArtem B. Bityutskiy 	vi->ubi_num = ubi->ubi_num;
85801c135cSArtem B. Bityutskiy 	vi->size = vol->reserved_pebs;
86801c135cSArtem B. Bityutskiy 	vi->used_bytes = vol->used_bytes;
87801c135cSArtem B. Bityutskiy 	vi->vol_type = vol->vol_type;
88801c135cSArtem B. Bityutskiy 	vi->corrupted = vol->corrupted;
89801c135cSArtem B. Bityutskiy 	vi->upd_marker = vol->upd_marker;
90801c135cSArtem B. Bityutskiy 	vi->alignment = vol->alignment;
91801c135cSArtem B. Bityutskiy 	vi->usable_leb_size = vol->usable_leb_size;
92801c135cSArtem B. Bityutskiy 	vi->name_len = vol->name_len;
93801c135cSArtem B. Bityutskiy 	vi->name = vol->name;
9449dfc299SArtem Bityutskiy 	vi->cdev = vol->cdev.dev;
95801c135cSArtem B. Bityutskiy }
960e0ee1ccSDmitry Pervushin 
970e0ee1ccSDmitry Pervushin /**
980e0ee1ccSDmitry Pervushin  * ubi_get_volume_info - get information about UBI volume.
990e0ee1ccSDmitry Pervushin  * @desc: volume descriptor
1000e0ee1ccSDmitry Pervushin  * @vi: the information is stored here
1010e0ee1ccSDmitry Pervushin  */
1020e0ee1ccSDmitry Pervushin void ubi_get_volume_info(struct ubi_volume_desc *desc,
1030e0ee1ccSDmitry Pervushin 			 struct ubi_volume_info *vi)
1040e0ee1ccSDmitry Pervushin {
1050e0ee1ccSDmitry Pervushin 	ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
1060e0ee1ccSDmitry Pervushin }
107801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info);
108801c135cSArtem B. Bityutskiy 
109801c135cSArtem B. Bityutskiy /**
110801c135cSArtem B. Bityutskiy  * ubi_open_volume - open UBI volume.
111801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
112801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
113801c135cSArtem B. Bityutskiy  * @mode: open mode
114801c135cSArtem B. Bityutskiy  *
115801c135cSArtem B. Bityutskiy  * The @mode parameter specifies if the volume should be opened in read-only
116801c135cSArtem B. Bityutskiy  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
117801c135cSArtem B. Bityutskiy  * nobody else will be able to open this volume. UBI allows to have many volume
118801c135cSArtem B. Bityutskiy  * readers and one writer at a time.
119801c135cSArtem B. Bityutskiy  *
120801c135cSArtem B. Bityutskiy  * If a static volume is being opened for the first time since boot, it will be
121801c135cSArtem B. Bityutskiy  * checked by this function, which means it will be fully read and the CRC
122801c135cSArtem B. Bityutskiy  * checksum of each logical eraseblock will be checked.
123801c135cSArtem B. Bityutskiy  *
124801c135cSArtem B. Bityutskiy  * This function returns volume descriptor in case of success and a negative
125801c135cSArtem B. Bityutskiy  * error code in case of failure.
126801c135cSArtem B. Bityutskiy  */
127801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
128801c135cSArtem B. Bityutskiy {
129801c135cSArtem B. Bityutskiy 	int err;
130801c135cSArtem B. Bityutskiy 	struct ubi_volume_desc *desc;
1310169b49dSJesper Juhl 	struct ubi_device *ubi;
132801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol;
133801c135cSArtem B. Bityutskiy 
134e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
135801c135cSArtem B. Bityutskiy 
13635ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
13735ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
1380169b49dSJesper Juhl 
139801c135cSArtem B. Bityutskiy 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
140fafdd2bfSRichard Weinberger 	    mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
14135ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
14235ad5fb7SArtem Bityutskiy 
143e73f4459SArtem Bityutskiy 	/*
144e73f4459SArtem Bityutskiy 	 * First of all, we have to get the UBI device to prevent its removal.
145e73f4459SArtem Bityutskiy 	 */
146e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
14735ad5fb7SArtem Bityutskiy 	if (!ubi)
14835ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
14935ad5fb7SArtem Bityutskiy 
150e73f4459SArtem Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
151e73f4459SArtem Bityutskiy 		err = -EINVAL;
152e73f4459SArtem Bityutskiy 		goto out_put_ubi;
153e73f4459SArtem Bityutskiy 	}
154801c135cSArtem B. Bityutskiy 
155801c135cSArtem B. Bityutskiy 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
156e73f4459SArtem Bityutskiy 	if (!desc) {
157e73f4459SArtem Bityutskiy 		err = -ENOMEM;
158e73f4459SArtem Bityutskiy 		goto out_put_ubi;
159e73f4459SArtem Bityutskiy 	}
16035ad5fb7SArtem Bityutskiy 
16135ad5fb7SArtem Bityutskiy 	err = -ENODEV;
16235ad5fb7SArtem Bityutskiy 	if (!try_module_get(THIS_MODULE))
16335ad5fb7SArtem Bityutskiy 		goto out_free;
164801c135cSArtem B. Bityutskiy 
165801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
166801c135cSArtem B. Bityutskiy 	vol = ubi->volumes[vol_id];
16735ad5fb7SArtem Bityutskiy 	if (!vol)
168801c135cSArtem B. Bityutskiy 		goto out_unlock;
169801c135cSArtem B. Bityutskiy 
170801c135cSArtem B. Bityutskiy 	err = -EBUSY;
171801c135cSArtem B. Bityutskiy 	switch (mode) {
172801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
173801c135cSArtem B. Bityutskiy 		if (vol->exclusive)
174801c135cSArtem B. Bityutskiy 			goto out_unlock;
175801c135cSArtem B. Bityutskiy 		vol->readers += 1;
176801c135cSArtem B. Bityutskiy 		break;
177801c135cSArtem B. Bityutskiy 
178801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
179801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers > 0)
180801c135cSArtem B. Bityutskiy 			goto out_unlock;
181801c135cSArtem B. Bityutskiy 		vol->writers += 1;
182801c135cSArtem B. Bityutskiy 		break;
183801c135cSArtem B. Bityutskiy 
184801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
185fafdd2bfSRichard Weinberger 		if (vol->exclusive || vol->writers || vol->readers ||
186fafdd2bfSRichard Weinberger 		    vol->metaonly)
187801c135cSArtem B. Bityutskiy 			goto out_unlock;
188801c135cSArtem B. Bityutskiy 		vol->exclusive = 1;
189801c135cSArtem B. Bityutskiy 		break;
190fafdd2bfSRichard Weinberger 
191fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
192fafdd2bfSRichard Weinberger 		if (vol->metaonly || vol->exclusive)
193fafdd2bfSRichard Weinberger 			goto out_unlock;
194fafdd2bfSRichard Weinberger 		vol->metaonly = 1;
195fafdd2bfSRichard Weinberger 		break;
196801c135cSArtem B. Bityutskiy 	}
197450f872aSArtem Bityutskiy 	get_device(&vol->dev);
198d05c77a8SArtem Bityutskiy 	vol->ref_count += 1;
199801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
200801c135cSArtem B. Bityutskiy 
201801c135cSArtem B. Bityutskiy 	desc->vol = vol;
202801c135cSArtem B. Bityutskiy 	desc->mode = mode;
203801c135cSArtem B. Bityutskiy 
204783b273aSArtem Bityutskiy 	mutex_lock(&ubi->ckvol_mutex);
205801c135cSArtem B. Bityutskiy 	if (!vol->checked) {
206801c135cSArtem B. Bityutskiy 		/* This is the first open - check the volume */
207801c135cSArtem B. Bityutskiy 		err = ubi_check_volume(ubi, vol_id);
208801c135cSArtem B. Bityutskiy 		if (err < 0) {
209783b273aSArtem Bityutskiy 			mutex_unlock(&ubi->ckvol_mutex);
210801c135cSArtem B. Bityutskiy 			ubi_close_volume(desc);
211801c135cSArtem B. Bityutskiy 			return ERR_PTR(err);
212801c135cSArtem B. Bityutskiy 		}
213801c135cSArtem B. Bityutskiy 		if (err == 1) {
21432608703STanya Brokhman 			ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
215801c135cSArtem B. Bityutskiy 				 vol_id, ubi->ubi_num);
216801c135cSArtem B. Bityutskiy 			vol->corrupted = 1;
217801c135cSArtem B. Bityutskiy 		}
218801c135cSArtem B. Bityutskiy 		vol->checked = 1;
219801c135cSArtem B. Bityutskiy 	}
220783b273aSArtem Bityutskiy 	mutex_unlock(&ubi->ckvol_mutex);
22135ad5fb7SArtem Bityutskiy 
222801c135cSArtem B. Bityutskiy 	return desc;
223801c135cSArtem B. Bityutskiy 
224801c135cSArtem B. Bityutskiy out_unlock:
225801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
226801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
22735ad5fb7SArtem Bityutskiy out_free:
22835ad5fb7SArtem Bityutskiy 	kfree(desc);
229e73f4459SArtem Bityutskiy out_put_ubi:
230e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
23132608703STanya Brokhman 	ubi_err(ubi, "cannot open device %d, volume %d, error %d",
232e1cf7e6dSArtem Bityutskiy 		ubi_num, vol_id, err);
233801c135cSArtem B. Bityutskiy 	return ERR_PTR(err);
234801c135cSArtem B. Bityutskiy }
235801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume);
236801c135cSArtem B. Bityutskiy 
237801c135cSArtem B. Bityutskiy /**
238801c135cSArtem B. Bityutskiy  * ubi_open_volume_nm - open UBI volume by name.
239801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
240801c135cSArtem B. Bityutskiy  * @name: volume name
241801c135cSArtem B. Bityutskiy  * @mode: open mode
242801c135cSArtem B. Bityutskiy  *
243801c135cSArtem B. Bityutskiy  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
244801c135cSArtem B. Bityutskiy  */
245801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
246801c135cSArtem B. Bityutskiy 					   int mode)
247801c135cSArtem B. Bityutskiy {
248801c135cSArtem B. Bityutskiy 	int i, vol_id = -1, len;
249801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi;
250e73f4459SArtem Bityutskiy 	struct ubi_volume_desc *ret;
251801c135cSArtem B. Bityutskiy 
252e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
253801c135cSArtem B. Bityutskiy 
254801c135cSArtem B. Bityutskiy 	if (!name)
255801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
256801c135cSArtem B. Bityutskiy 
257801c135cSArtem B. Bityutskiy 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
258801c135cSArtem B. Bityutskiy 	if (len > UBI_VOL_NAME_MAX)
259801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
260801c135cSArtem B. Bityutskiy 
26135ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
26235ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
263801c135cSArtem B. Bityutskiy 
264e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
26535ad5fb7SArtem Bityutskiy 	if (!ubi)
26635ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
267801c135cSArtem B. Bityutskiy 
268801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
269801c135cSArtem B. Bityutskiy 	/* Walk all volumes of this UBI device */
270801c135cSArtem B. Bityutskiy 	for (i = 0; i < ubi->vtbl_slots; i++) {
271801c135cSArtem B. Bityutskiy 		struct ubi_volume *vol = ubi->volumes[i];
272801c135cSArtem B. Bityutskiy 
273801c135cSArtem B. Bityutskiy 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
274801c135cSArtem B. Bityutskiy 			vol_id = i;
275801c135cSArtem B. Bityutskiy 			break;
276801c135cSArtem B. Bityutskiy 		}
277801c135cSArtem B. Bityutskiy 	}
278801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
279801c135cSArtem B. Bityutskiy 
280e73f4459SArtem Bityutskiy 	if (vol_id >= 0)
281e73f4459SArtem Bityutskiy 		ret = ubi_open_volume(ubi_num, vol_id, mode);
282e73f4459SArtem Bityutskiy 	else
283e73f4459SArtem Bityutskiy 		ret = ERR_PTR(-ENODEV);
284801c135cSArtem B. Bityutskiy 
285e73f4459SArtem Bityutskiy 	/*
286e73f4459SArtem Bityutskiy 	 * We should put the UBI device even in case of success, because
287e73f4459SArtem Bityutskiy 	 * 'ubi_open_volume()' took a reference as well.
288e73f4459SArtem Bityutskiy 	 */
289e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
290e73f4459SArtem Bityutskiy 	return ret;
291801c135cSArtem B. Bityutskiy }
292801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
293801c135cSArtem B. Bityutskiy 
294801c135cSArtem B. Bityutskiy /**
295b5710284SCorentin Chary  * ubi_open_volume_path - open UBI volume by its character device node path.
296b5710284SCorentin Chary  * @pathname: volume character device node path
297b5710284SCorentin Chary  * @mode: open mode
298b5710284SCorentin Chary  *
299b5710284SCorentin Chary  * This function is similar to 'ubi_open_volume()', but opens a volume the path
300b5710284SCorentin Chary  * to its character device node.
301b5710284SCorentin Chary  */
302b5710284SCorentin Chary struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
303b5710284SCorentin Chary {
304b531b55aSArtem Bityutskiy 	int error, ubi_num, vol_id, mod;
305b5710284SCorentin Chary 	struct inode *inode;
306b5710284SCorentin Chary 	struct path path;
307b5710284SCorentin Chary 
308b5710284SCorentin Chary 	dbg_gen("open volume %s, mode %d", pathname, mode);
309b5710284SCorentin Chary 
310b5710284SCorentin Chary 	if (!pathname || !*pathname)
311b5710284SCorentin Chary 		return ERR_PTR(-EINVAL);
312b5710284SCorentin Chary 
313b5710284SCorentin Chary 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
314b5710284SCorentin Chary 	if (error)
315b5710284SCorentin Chary 		return ERR_PTR(error);
316b5710284SCorentin Chary 
317bb668734SDavid Howells 	inode = d_backing_inode(path.dentry);
318b531b55aSArtem Bityutskiy 	mod = inode->i_mode;
319b5710284SCorentin Chary 	ubi_num = ubi_major2num(imajor(inode));
320b5710284SCorentin Chary 	vol_id = iminor(inode) - 1;
321b5710284SCorentin Chary 	path_put(&path);
322b531b55aSArtem Bityutskiy 
323b531b55aSArtem Bityutskiy 	if (!S_ISCHR(mod))
324b531b55aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
325b531b55aSArtem Bityutskiy 	if (vol_id >= 0 && ubi_num >= 0)
326b531b55aSArtem Bityutskiy 		return ubi_open_volume(ubi_num, vol_id, mode);
327b531b55aSArtem Bityutskiy 	return ERR_PTR(-ENODEV);
328b5710284SCorentin Chary }
329b5710284SCorentin Chary EXPORT_SYMBOL_GPL(ubi_open_volume_path);
330b5710284SCorentin Chary 
331b5710284SCorentin Chary /**
332801c135cSArtem B. Bityutskiy  * ubi_close_volume - close UBI volume.
333801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
334801c135cSArtem B. Bityutskiy  */
335801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc)
336801c135cSArtem B. Bityutskiy {
337801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
338e73f4459SArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
339801c135cSArtem B. Bityutskiy 
340e1cf7e6dSArtem Bityutskiy 	dbg_gen("close device %d, volume %d, mode %d",
341e1cf7e6dSArtem Bityutskiy 		ubi->ubi_num, vol->vol_id, desc->mode);
342801c135cSArtem B. Bityutskiy 
343e73f4459SArtem Bityutskiy 	spin_lock(&ubi->volumes_lock);
344801c135cSArtem B. Bityutskiy 	switch (desc->mode) {
345801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
346801c135cSArtem B. Bityutskiy 		vol->readers -= 1;
347801c135cSArtem B. Bityutskiy 		break;
348801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
349801c135cSArtem B. Bityutskiy 		vol->writers -= 1;
350801c135cSArtem B. Bityutskiy 		break;
351801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
352801c135cSArtem B. Bityutskiy 		vol->exclusive = 0;
353fafdd2bfSRichard Weinberger 		break;
354fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
355fafdd2bfSRichard Weinberger 		vol->metaonly = 0;
356fafdd2bfSRichard Weinberger 		break;
357801c135cSArtem B. Bityutskiy 	}
358d05c77a8SArtem Bityutskiy 	vol->ref_count -= 1;
359e73f4459SArtem Bityutskiy 	spin_unlock(&ubi->volumes_lock);
360801c135cSArtem B. Bityutskiy 
361d05c77a8SArtem Bityutskiy 	kfree(desc);
362e73f4459SArtem Bityutskiy 	put_device(&vol->dev);
363e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
364801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
365801c135cSArtem B. Bityutskiy }
366801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume);
367801c135cSArtem B. Bityutskiy 
368801c135cSArtem B. Bityutskiy /**
3699ff08979SRichard Weinberger  * leb_read_sanity_check - does sanity checks on read requests.
3709ff08979SRichard Weinberger  * @desc: volume descriptor
3719ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
3729ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
3739ff08979SRichard Weinberger  * @len: how many bytes to read
3749ff08979SRichard Weinberger  *
3759ff08979SRichard Weinberger  * This function is used by ubi_leb_read() and ubi_leb_read_sg()
3769ff08979SRichard Weinberger  * to perform sanity checks.
3779ff08979SRichard Weinberger  */
3789ff08979SRichard Weinberger static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
3799ff08979SRichard Weinberger 				 int offset, int len)
3809ff08979SRichard Weinberger {
3819ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
3829ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
3839ff08979SRichard Weinberger 	int vol_id = vol->vol_id;
3849ff08979SRichard Weinberger 
3859ff08979SRichard Weinberger 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
3869ff08979SRichard Weinberger 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
3879ff08979SRichard Weinberger 	    offset + len > vol->usable_leb_size)
3889ff08979SRichard Weinberger 		return -EINVAL;
3899ff08979SRichard Weinberger 
3909ff08979SRichard Weinberger 	if (vol->vol_type == UBI_STATIC_VOLUME) {
3919ff08979SRichard Weinberger 		if (vol->used_ebs == 0)
3929ff08979SRichard Weinberger 			/* Empty static UBI volume */
3939ff08979SRichard Weinberger 			return 0;
3949ff08979SRichard Weinberger 		if (lnum == vol->used_ebs - 1 &&
3959ff08979SRichard Weinberger 		    offset + len > vol->last_eb_bytes)
3969ff08979SRichard Weinberger 			return -EINVAL;
3979ff08979SRichard Weinberger 	}
3989ff08979SRichard Weinberger 
3999ff08979SRichard Weinberger 	if (vol->upd_marker)
4009ff08979SRichard Weinberger 		return -EBADF;
4019ff08979SRichard Weinberger 
4029ff08979SRichard Weinberger 	return 0;
4039ff08979SRichard Weinberger }
4049ff08979SRichard Weinberger 
4059ff08979SRichard Weinberger /**
406801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
407801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
408801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
409801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
410801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
411801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
412801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
413801c135cSArtem B. Bityutskiy  *
414801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
415801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
416801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
417801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
418801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
419801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
420801c135cSArtem B. Bityutskiy  *
421801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
422801c135cSArtem B. Bityutskiy  * function returns a negative error code.
423801c135cSArtem B. Bityutskiy  *
424801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
425801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
426801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
427801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
428801c135cSArtem B. Bityutskiy  *
429801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
430801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
431801c135cSArtem B. Bityutskiy  */
432801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
433801c135cSArtem B. Bityutskiy 		 int len, int check)
434801c135cSArtem B. Bityutskiy {
435801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
436801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
437801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
438801c135cSArtem B. Bityutskiy 
439c8566350SArtem Bityutskiy 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
440801c135cSArtem B. Bityutskiy 
4419ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4429ff08979SRichard Weinberger 	if (err < 0)
4439ff08979SRichard Weinberger 		return err;
444801c135cSArtem B. Bityutskiy 
445801c135cSArtem B. Bityutskiy 	if (len == 0)
446801c135cSArtem B. Bityutskiy 		return 0;
447801c135cSArtem B. Bityutskiy 
44889b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
449d57f4054SBrian Norris 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
45032608703STanya Brokhman 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
451801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
452801c135cSArtem B. Bityutskiy 	}
453801c135cSArtem B. Bityutskiy 
454801c135cSArtem B. Bityutskiy 	return err;
455801c135cSArtem B. Bityutskiy }
456801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
457801c135cSArtem B. Bityutskiy 
4589ff08979SRichard Weinberger 
4599ff08979SRichard Weinberger /**
4609ff08979SRichard Weinberger  * ubi_leb_read_sg - read data into a scatter gather list.
4619ff08979SRichard Weinberger  * @desc: volume descriptor
4629ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
4639ff08979SRichard Weinberger  * @buf: buffer where to store the read data
4649ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
4659ff08979SRichard Weinberger  * @len: how many bytes to read
4669ff08979SRichard Weinberger  * @check: whether UBI has to check the read data's CRC or not.
4679ff08979SRichard Weinberger  *
4689ff08979SRichard Weinberger  * This function works exactly like ubi_leb_read_sg(). But instead of
4699ff08979SRichard Weinberger  * storing the read data into a buffer it writes to an UBI scatter gather
4709ff08979SRichard Weinberger  * list.
4719ff08979SRichard Weinberger  */
4729ff08979SRichard Weinberger int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
4739ff08979SRichard Weinberger 		    int offset, int len, int check)
4749ff08979SRichard Weinberger {
4759ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
4769ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
4779ff08979SRichard Weinberger 	int err, vol_id = vol->vol_id;
4789ff08979SRichard Weinberger 
4799ff08979SRichard Weinberger 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
4809ff08979SRichard Weinberger 
4819ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4829ff08979SRichard Weinberger 	if (err < 0)
4839ff08979SRichard Weinberger 		return err;
4849ff08979SRichard Weinberger 
4859ff08979SRichard Weinberger 	if (len == 0)
4869ff08979SRichard Weinberger 		return 0;
4879ff08979SRichard Weinberger 
4889ff08979SRichard Weinberger 	err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
4899ff08979SRichard Weinberger 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
4909ff08979SRichard Weinberger 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
4919ff08979SRichard Weinberger 		vol->corrupted = 1;
4929ff08979SRichard Weinberger 	}
4939ff08979SRichard Weinberger 
4949ff08979SRichard Weinberger 	return err;
4959ff08979SRichard Weinberger }
4969ff08979SRichard Weinberger EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
4979ff08979SRichard Weinberger 
498801c135cSArtem B. Bityutskiy /**
499801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
500801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
501801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
502801c135cSArtem B. Bityutskiy  * @buf: data to write
503801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
504801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
505801c135cSArtem B. Bityutskiy  *
506801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
507b36a261eSRichard Weinberger  * logical eraseblock @lnum.
508801c135cSArtem B. Bityutskiy  *
509801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
510801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
511801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
512801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
513801c135cSArtem B. Bityutskiy  *
514801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
515801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
516801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
517801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
518801c135cSArtem B. Bityutskiy  * garbage.
519801c135cSArtem B. Bityutskiy  *
520801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
521801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
522801c135cSArtem B. Bityutskiy  */
523801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
524b36a261eSRichard Weinberger 		  int offset, int len)
525801c135cSArtem B. Bityutskiy {
526801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
527801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
528801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
529801c135cSArtem B. Bityutskiy 
530c8566350SArtem Bityutskiy 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
531801c135cSArtem B. Bityutskiy 
532801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
533801c135cSArtem B. Bityutskiy 		return -EINVAL;
534801c135cSArtem B. Bityutskiy 
535801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
536801c135cSArtem B. Bityutskiy 		return -EROFS;
537801c135cSArtem B. Bityutskiy 
538801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
539cadb40ccSKyungmin Park 	    offset + len > vol->usable_leb_size ||
540cadb40ccSKyungmin Park 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
541801c135cSArtem B. Bityutskiy 		return -EINVAL;
542801c135cSArtem B. Bityutskiy 
543801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
544801c135cSArtem B. Bityutskiy 		return -EBADF;
545801c135cSArtem B. Bityutskiy 
546801c135cSArtem B. Bityutskiy 	if (len == 0)
547801c135cSArtem B. Bityutskiy 		return 0;
548801c135cSArtem B. Bityutskiy 
549b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
550801c135cSArtem B. Bityutskiy }
551801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
552801c135cSArtem B. Bityutskiy 
553801c135cSArtem B. Bityutskiy /*
554801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
555801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
556801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
557801c135cSArtem B. Bityutskiy  * @buf: data to write
558801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
559801c135cSArtem B. Bityutskiy  *
560801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
561801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
5623f502622SShinya Kuribayashi  * data, which has to be aligned. The length may be shorter than the logical
563801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
564801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
565801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
566801c135cSArtem B. Bityutskiy  * code in case of failure.
567801c135cSArtem B. Bityutskiy  */
568801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
569b36a261eSRichard Weinberger 		   int len)
570801c135cSArtem B. Bityutskiy {
571801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
572801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
573801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
574801c135cSArtem B. Bityutskiy 
575c8566350SArtem Bityutskiy 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
576801c135cSArtem B. Bityutskiy 
577801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
578801c135cSArtem B. Bityutskiy 		return -EINVAL;
579801c135cSArtem B. Bityutskiy 
580801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
581801c135cSArtem B. Bityutskiy 		return -EROFS;
582801c135cSArtem B. Bityutskiy 
583801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
584cadb40ccSKyungmin Park 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
585801c135cSArtem B. Bityutskiy 		return -EINVAL;
586801c135cSArtem B. Bityutskiy 
587801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
588801c135cSArtem B. Bityutskiy 		return -EBADF;
589801c135cSArtem B. Bityutskiy 
590801c135cSArtem B. Bityutskiy 	if (len == 0)
591801c135cSArtem B. Bityutskiy 		return 0;
592801c135cSArtem B. Bityutskiy 
593b36a261eSRichard Weinberger 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
594801c135cSArtem B. Bityutskiy }
595801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
596801c135cSArtem B. Bityutskiy 
597801c135cSArtem B. Bityutskiy /**
598801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
599801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
600801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
601801c135cSArtem B. Bityutskiy  *
602801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
603801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
604801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
605801c135cSArtem B. Bityutskiy  *
606801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
607801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
608801c135cSArtem B. Bityutskiy  */
609801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
610801c135cSArtem B. Bityutskiy {
611801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
612801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
613ae616e1bSArtem Bityutskiy 	int err;
614801c135cSArtem B. Bityutskiy 
615c8566350SArtem Bityutskiy 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
616801c135cSArtem B. Bityutskiy 
617801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
618801c135cSArtem B. Bityutskiy 		return -EROFS;
619801c135cSArtem B. Bityutskiy 
620801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
621801c135cSArtem B. Bityutskiy 		return -EINVAL;
622801c135cSArtem B. Bityutskiy 
623801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
624801c135cSArtem B. Bityutskiy 		return -EBADF;
625801c135cSArtem B. Bityutskiy 
62689b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
627801c135cSArtem B. Bityutskiy 	if (err)
628801c135cSArtem B. Bityutskiy 		return err;
629801c135cSArtem B. Bityutskiy 
63062f38455SJoel Reardon 	return ubi_wl_flush(ubi, vol->vol_id, lnum);
631801c135cSArtem B. Bityutskiy }
632801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
633801c135cSArtem B. Bityutskiy 
634801c135cSArtem B. Bityutskiy /**
635801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
636801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
637801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
638801c135cSArtem B. Bityutskiy  *
639801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
640801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
6413f502622SShinya Kuribayashi  * physically erased in background. This operation is much faster than the
642801c135cSArtem B. Bityutskiy  * erase operation.
643801c135cSArtem B. Bityutskiy  *
644801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
645801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
646801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
647801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
648801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
649801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
650801c135cSArtem B. Bityutskiy  * used with care.
651801c135cSArtem B. Bityutskiy  *
652801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
653801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
654801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
655801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
656801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
657801c135cSArtem B. Bityutskiy  *
658801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
659801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
6603f502622SShinya Kuribayashi  * first un-map it, then write new data, rather than first erase it, then write
661801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
662801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
663801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
664801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
665801c135cSArtem B. Bityutskiy  *
666801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
667801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
668801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
669801c135cSArtem B. Bityutskiy  */
670801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
671801c135cSArtem B. Bityutskiy {
672801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
673801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
674801c135cSArtem B. Bityutskiy 
675c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
676801c135cSArtem B. Bityutskiy 
677801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
678801c135cSArtem B. Bityutskiy 		return -EROFS;
679801c135cSArtem B. Bityutskiy 
680801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
681801c135cSArtem B. Bityutskiy 		return -EINVAL;
682801c135cSArtem B. Bityutskiy 
683801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
684801c135cSArtem B. Bityutskiy 		return -EBADF;
685801c135cSArtem B. Bityutskiy 
68689b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
687801c135cSArtem B. Bityutskiy }
688801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
689801c135cSArtem B. Bityutskiy 
690801c135cSArtem B. Bityutskiy /**
6910e0ee1ccSDmitry Pervushin  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
692393852ecSArtem Bityutskiy  * @desc: volume descriptor
693393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
694393852ecSArtem Bityutskiy  *
695393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
69673ac36eaSColy Li  * eraseblock. This means, that after a successful invocation of this
697393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
698393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
699393852ecSArtem Bityutskiy  * happens.
700393852ecSArtem Bityutskiy  *
701393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
702393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
703393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
704393852ecSArtem Bityutskiy  * other failures.
705393852ecSArtem Bityutskiy  */
706b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
707393852ecSArtem Bityutskiy {
708393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
709393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
710393852ecSArtem Bityutskiy 
711*960b35d0Sz00189512 	dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
712393852ecSArtem Bityutskiy 
713393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
714393852ecSArtem Bityutskiy 		return -EROFS;
715393852ecSArtem Bityutskiy 
716393852ecSArtem Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
717393852ecSArtem Bityutskiy 		return -EINVAL;
718393852ecSArtem Bityutskiy 
719393852ecSArtem Bityutskiy 	if (vol->upd_marker)
720393852ecSArtem Bityutskiy 		return -EBADF;
721393852ecSArtem Bityutskiy 
722393852ecSArtem Bityutskiy 	if (vol->eba_tbl[lnum] >= 0)
723393852ecSArtem Bityutskiy 		return -EBADMSG;
724393852ecSArtem Bityutskiy 
725b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
726393852ecSArtem Bityutskiy }
727393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
728393852ecSArtem Bityutskiy 
729393852ecSArtem Bityutskiy /**
730801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
731801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
732801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
733801c135cSArtem B. Bityutskiy  *
734801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
735801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
736801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
737801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
738801c135cSArtem B. Bityutskiy  * mapped to.
739801c135cSArtem B. Bityutskiy  *
740801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
741801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
742801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
743801c135cSArtem B. Bityutskiy  * code.
744801c135cSArtem B. Bityutskiy  */
745801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
746801c135cSArtem B. Bityutskiy {
747801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
748801c135cSArtem B. Bityutskiy 
749c8566350SArtem Bityutskiy 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
750801c135cSArtem B. Bityutskiy 
751801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
752801c135cSArtem B. Bityutskiy 		return -EINVAL;
753801c135cSArtem B. Bityutskiy 
754801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
755801c135cSArtem B. Bityutskiy 		return -EBADF;
756801c135cSArtem B. Bityutskiy 
757801c135cSArtem B. Bityutskiy 	return vol->eba_tbl[lnum] >= 0;
758801c135cSArtem B. Bityutskiy }
759801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
760a5bf6190SArtem Bityutskiy 
761a5bf6190SArtem Bityutskiy /**
762a5bf6190SArtem Bityutskiy  * ubi_sync - synchronize UBI device buffers.
763a5bf6190SArtem Bityutskiy  * @ubi_num: UBI device to synchronize
764a5bf6190SArtem Bityutskiy  *
765a5bf6190SArtem Bityutskiy  * The underlying MTD device may cache data in hardware or in software. This
766a5bf6190SArtem Bityutskiy  * function ensures the caches are flushed. Returns zero in case of success and
767a5bf6190SArtem Bityutskiy  * a negative error code in case of failure.
768a5bf6190SArtem Bityutskiy  */
769a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num)
770a5bf6190SArtem Bityutskiy {
771a5bf6190SArtem Bityutskiy 	struct ubi_device *ubi;
772a5bf6190SArtem Bityutskiy 
773a5bf6190SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
774a5bf6190SArtem Bityutskiy 	if (!ubi)
775a5bf6190SArtem Bityutskiy 		return -ENODEV;
776a5bf6190SArtem Bityutskiy 
77785f2f2a8SArtem Bityutskiy 	mtd_sync(ubi->mtd);
778a5bf6190SArtem Bityutskiy 	ubi_put_device(ubi);
779a5bf6190SArtem Bityutskiy 	return 0;
780a5bf6190SArtem Bityutskiy }
781a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync);
7820e0ee1ccSDmitry Pervushin 
78362f38455SJoel Reardon /**
78462f38455SJoel Reardon  * ubi_flush - flush UBI work queue.
78562f38455SJoel Reardon  * @ubi_num: UBI device to flush work queue
78662f38455SJoel Reardon  * @vol_id: volume id to flush for
78762f38455SJoel Reardon  * @lnum: logical eraseblock number to flush for
78862f38455SJoel Reardon  *
78962f38455SJoel Reardon  * This function executes all pending works for a particular volume id / logical
79062f38455SJoel Reardon  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
79162f38455SJoel Reardon  * a wildcard for all of the corresponding volume numbers or logical
79262f38455SJoel Reardon  * eraseblock numbers. It returns zero in case of success and a negative error
79362f38455SJoel Reardon  * code in case of failure.
79462f38455SJoel Reardon  */
79562f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum)
79662f38455SJoel Reardon {
79762f38455SJoel Reardon 	struct ubi_device *ubi;
79862f38455SJoel Reardon 	int err = 0;
79962f38455SJoel Reardon 
80062f38455SJoel Reardon 	ubi = ubi_get_device(ubi_num);
80162f38455SJoel Reardon 	if (!ubi)
80262f38455SJoel Reardon 		return -ENODEV;
80362f38455SJoel Reardon 
80462f38455SJoel Reardon 	err = ubi_wl_flush(ubi, vol_id, lnum);
80562f38455SJoel Reardon 	ubi_put_device(ubi);
80662f38455SJoel Reardon 	return err;
80762f38455SJoel Reardon }
80862f38455SJoel Reardon EXPORT_SYMBOL_GPL(ubi_flush);
80962f38455SJoel Reardon 
8100e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
8110e0ee1ccSDmitry Pervushin 
8120e0ee1ccSDmitry Pervushin /**
8130e0ee1ccSDmitry Pervushin  * ubi_register_volume_notifier - register a volume notifier.
8140e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8150e0ee1ccSDmitry Pervushin  * @ignore_existing: if non-zero, do not send "added" notification for all
8160e0ee1ccSDmitry Pervushin  *                   already existing volumes
8170e0ee1ccSDmitry Pervushin  *
8180e0ee1ccSDmitry Pervushin  * This function registers a volume notifier, which means that
8190e0ee1ccSDmitry Pervushin  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
8200e0ee1ccSDmitry Pervushin  * removed, re-sized, re-named, or updated. The first argument of the function
8210e0ee1ccSDmitry Pervushin  * is the notification type. The second argument is pointer to a
8220e0ee1ccSDmitry Pervushin  * &struct ubi_notification object which describes the notification event.
8230e0ee1ccSDmitry Pervushin  * Using UBI API from the volume notifier is prohibited.
8240e0ee1ccSDmitry Pervushin  *
8250e0ee1ccSDmitry Pervushin  * This function returns zero in case of success and a negative error code
8260e0ee1ccSDmitry Pervushin  * in case of failure.
8270e0ee1ccSDmitry Pervushin  */
8280e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
8290e0ee1ccSDmitry Pervushin 				 int ignore_existing)
8300e0ee1ccSDmitry Pervushin {
8310e0ee1ccSDmitry Pervushin 	int err;
8320e0ee1ccSDmitry Pervushin 
8330e0ee1ccSDmitry Pervushin 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
8340e0ee1ccSDmitry Pervushin 	if (err != 0)
8350e0ee1ccSDmitry Pervushin 		return err;
8360e0ee1ccSDmitry Pervushin 	if (ignore_existing)
8370e0ee1ccSDmitry Pervushin 		return 0;
8380e0ee1ccSDmitry Pervushin 
8390e0ee1ccSDmitry Pervushin 	/*
8400e0ee1ccSDmitry Pervushin 	 * We are going to walk all UBI devices and all volumes, and
8410e0ee1ccSDmitry Pervushin 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
8420e0ee1ccSDmitry Pervushin 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
8430e0ee1ccSDmitry Pervushin 	 * devices do not disappear.
8440e0ee1ccSDmitry Pervushin 	 */
8450e0ee1ccSDmitry Pervushin 	mutex_lock(&ubi_devices_mutex);
8460e0ee1ccSDmitry Pervushin 	ubi_enumerate_volumes(nb);
8470e0ee1ccSDmitry Pervushin 	mutex_unlock(&ubi_devices_mutex);
8480e0ee1ccSDmitry Pervushin 
8490e0ee1ccSDmitry Pervushin 	return err;
8500e0ee1ccSDmitry Pervushin }
8510e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
8520e0ee1ccSDmitry Pervushin 
8530e0ee1ccSDmitry Pervushin /**
8540e0ee1ccSDmitry Pervushin  * ubi_unregister_volume_notifier - unregister the volume notifier.
8550e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8560e0ee1ccSDmitry Pervushin  *
8570e0ee1ccSDmitry Pervushin  * This function unregisters volume notifier @nm and returns zero in case of
8580e0ee1ccSDmitry Pervushin  * success and a negative error code in case of failure.
8590e0ee1ccSDmitry Pervushin  */
8600e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb)
8610e0ee1ccSDmitry Pervushin {
8620e0ee1ccSDmitry Pervushin 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
8630e0ee1ccSDmitry Pervushin }
8640e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
865