xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision fafdd2bf2638157670f28462b641150d16dbaeca)
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 &&
140*fafdd2bfSRichard 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:
185*fafdd2bfSRichard Weinberger 		if (vol->exclusive || vol->writers || vol->readers ||
186*fafdd2bfSRichard Weinberger 		    vol->metaonly)
187801c135cSArtem B. Bityutskiy 			goto out_unlock;
188801c135cSArtem B. Bityutskiy 		vol->exclusive = 1;
189801c135cSArtem B. Bityutskiy 		break;
190*fafdd2bfSRichard Weinberger 
191*fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
192*fafdd2bfSRichard Weinberger 		if (vol->metaonly || vol->exclusive)
193*fafdd2bfSRichard Weinberger 			goto out_unlock;
194*fafdd2bfSRichard Weinberger 		vol->metaonly = 1;
195*fafdd2bfSRichard 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 
317b5710284SCorentin Chary 	inode = path.dentry->d_inode;
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;
353*fafdd2bfSRichard Weinberger 		break;
354*fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
355*fafdd2bfSRichard Weinberger 		vol->metaonly = 0;
356*fafdd2bfSRichard 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 /**
369801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
370801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
371801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
372801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
373801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
374801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
375801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
376801c135cSArtem B. Bityutskiy  *
377801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
378801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
379801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
380801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
381801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
382801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
383801c135cSArtem B. Bityutskiy  *
384801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
385801c135cSArtem B. Bityutskiy  * function returns a negative error code.
386801c135cSArtem B. Bityutskiy  *
387801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
388801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
389801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
390801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
391801c135cSArtem B. Bityutskiy  *
392801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
393801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
394801c135cSArtem B. Bityutskiy  */
395801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
396801c135cSArtem B. Bityutskiy 		 int len, int check)
397801c135cSArtem B. Bityutskiy {
398801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
399801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
400801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
401801c135cSArtem B. Bityutskiy 
402c8566350SArtem Bityutskiy 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
403801c135cSArtem B. Bityutskiy 
404801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
405801c135cSArtem B. Bityutskiy 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
406801c135cSArtem B. Bityutskiy 	    offset + len > vol->usable_leb_size)
407801c135cSArtem B. Bityutskiy 		return -EINVAL;
408801c135cSArtem B. Bityutskiy 
4094ab60a0dSArtem Bityutskiy 	if (vol->vol_type == UBI_STATIC_VOLUME) {
4104ab60a0dSArtem Bityutskiy 		if (vol->used_ebs == 0)
4114ab60a0dSArtem Bityutskiy 			/* Empty static UBI volume */
4124ab60a0dSArtem Bityutskiy 			return 0;
4134ab60a0dSArtem Bityutskiy 		if (lnum == vol->used_ebs - 1 &&
414801c135cSArtem B. Bityutskiy 		    offset + len > vol->last_eb_bytes)
415801c135cSArtem B. Bityutskiy 			return -EINVAL;
4164ab60a0dSArtem Bityutskiy 	}
417801c135cSArtem B. Bityutskiy 
418801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
419801c135cSArtem B. Bityutskiy 		return -EBADF;
420801c135cSArtem B. Bityutskiy 	if (len == 0)
421801c135cSArtem B. Bityutskiy 		return 0;
422801c135cSArtem B. Bityutskiy 
42389b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
424d57f4054SBrian Norris 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
42532608703STanya Brokhman 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
426801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
427801c135cSArtem B. Bityutskiy 	}
428801c135cSArtem B. Bityutskiy 
429801c135cSArtem B. Bityutskiy 	return err;
430801c135cSArtem B. Bityutskiy }
431801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
432801c135cSArtem B. Bityutskiy 
433801c135cSArtem B. Bityutskiy /**
434801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
435801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
436801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
437801c135cSArtem B. Bityutskiy  * @buf: data to write
438801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
439801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
440801c135cSArtem B. Bityutskiy  *
441801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
442b36a261eSRichard Weinberger  * logical eraseblock @lnum.
443801c135cSArtem B. Bityutskiy  *
444801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
445801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
446801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
447801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
448801c135cSArtem B. Bityutskiy  *
449801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
450801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
451801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
452801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
453801c135cSArtem B. Bityutskiy  * garbage.
454801c135cSArtem B. Bityutskiy  *
455801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
456801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
457801c135cSArtem B. Bityutskiy  */
458801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
459b36a261eSRichard Weinberger 		  int offset, int len)
460801c135cSArtem B. Bityutskiy {
461801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
462801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
463801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
464801c135cSArtem B. Bityutskiy 
465c8566350SArtem Bityutskiy 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
466801c135cSArtem B. Bityutskiy 
467801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
468801c135cSArtem B. Bityutskiy 		return -EINVAL;
469801c135cSArtem B. Bityutskiy 
470801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
471801c135cSArtem B. Bityutskiy 		return -EROFS;
472801c135cSArtem B. Bityutskiy 
473801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
474cadb40ccSKyungmin Park 	    offset + len > vol->usable_leb_size ||
475cadb40ccSKyungmin Park 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
476801c135cSArtem B. Bityutskiy 		return -EINVAL;
477801c135cSArtem B. Bityutskiy 
478801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
479801c135cSArtem B. Bityutskiy 		return -EBADF;
480801c135cSArtem B. Bityutskiy 
481801c135cSArtem B. Bityutskiy 	if (len == 0)
482801c135cSArtem B. Bityutskiy 		return 0;
483801c135cSArtem B. Bityutskiy 
484b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
485801c135cSArtem B. Bityutskiy }
486801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
487801c135cSArtem B. Bityutskiy 
488801c135cSArtem B. Bityutskiy /*
489801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
490801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
491801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
492801c135cSArtem B. Bityutskiy  * @buf: data to write
493801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
494801c135cSArtem B. Bityutskiy  *
495801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
496801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
4973f502622SShinya Kuribayashi  * data, which has to be aligned. The length may be shorter than the logical
498801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
499801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
500801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
501801c135cSArtem B. Bityutskiy  * code in case of failure.
502801c135cSArtem B. Bityutskiy  */
503801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
504b36a261eSRichard Weinberger 		   int len)
505801c135cSArtem B. Bityutskiy {
506801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
507801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
508801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
509801c135cSArtem B. Bityutskiy 
510c8566350SArtem Bityutskiy 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
511801c135cSArtem B. Bityutskiy 
512801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
513801c135cSArtem B. Bityutskiy 		return -EINVAL;
514801c135cSArtem B. Bityutskiy 
515801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
516801c135cSArtem B. Bityutskiy 		return -EROFS;
517801c135cSArtem B. Bityutskiy 
518801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
519cadb40ccSKyungmin Park 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
520801c135cSArtem B. Bityutskiy 		return -EINVAL;
521801c135cSArtem B. Bityutskiy 
522801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
523801c135cSArtem B. Bityutskiy 		return -EBADF;
524801c135cSArtem B. Bityutskiy 
525801c135cSArtem B. Bityutskiy 	if (len == 0)
526801c135cSArtem B. Bityutskiy 		return 0;
527801c135cSArtem B. Bityutskiy 
528b36a261eSRichard Weinberger 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
529801c135cSArtem B. Bityutskiy }
530801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
531801c135cSArtem B. Bityutskiy 
532801c135cSArtem B. Bityutskiy /**
533801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
534801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
535801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
536801c135cSArtem B. Bityutskiy  *
537801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
538801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
539801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
540801c135cSArtem B. Bityutskiy  *
541801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
542801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
543801c135cSArtem B. Bityutskiy  */
544801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
545801c135cSArtem B. Bityutskiy {
546801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
547801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
548ae616e1bSArtem Bityutskiy 	int err;
549801c135cSArtem B. Bityutskiy 
550c8566350SArtem Bityutskiy 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
551801c135cSArtem B. Bityutskiy 
552801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
553801c135cSArtem B. Bityutskiy 		return -EROFS;
554801c135cSArtem B. Bityutskiy 
555801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
556801c135cSArtem B. Bityutskiy 		return -EINVAL;
557801c135cSArtem B. Bityutskiy 
558801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
559801c135cSArtem B. Bityutskiy 		return -EBADF;
560801c135cSArtem B. Bityutskiy 
56189b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
562801c135cSArtem B. Bityutskiy 	if (err)
563801c135cSArtem B. Bityutskiy 		return err;
564801c135cSArtem B. Bityutskiy 
56562f38455SJoel Reardon 	return ubi_wl_flush(ubi, vol->vol_id, lnum);
566801c135cSArtem B. Bityutskiy }
567801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
568801c135cSArtem B. Bityutskiy 
569801c135cSArtem B. Bityutskiy /**
570801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
571801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
572801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
573801c135cSArtem B. Bityutskiy  *
574801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
575801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
5763f502622SShinya Kuribayashi  * physically erased in background. This operation is much faster than the
577801c135cSArtem B. Bityutskiy  * erase operation.
578801c135cSArtem B. Bityutskiy  *
579801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
580801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
581801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
582801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
583801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
584801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
585801c135cSArtem B. Bityutskiy  * used with care.
586801c135cSArtem B. Bityutskiy  *
587801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
588801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
589801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
590801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
591801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
592801c135cSArtem B. Bityutskiy  *
593801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
594801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
5953f502622SShinya Kuribayashi  * first un-map it, then write new data, rather than first erase it, then write
596801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
597801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
598801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
599801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
600801c135cSArtem B. Bityutskiy  *
601801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
602801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
603801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
604801c135cSArtem B. Bityutskiy  */
605801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
606801c135cSArtem B. Bityutskiy {
607801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
608801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
609801c135cSArtem B. Bityutskiy 
610c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
611801c135cSArtem B. Bityutskiy 
612801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
613801c135cSArtem B. Bityutskiy 		return -EROFS;
614801c135cSArtem B. Bityutskiy 
615801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
616801c135cSArtem B. Bityutskiy 		return -EINVAL;
617801c135cSArtem B. Bityutskiy 
618801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
619801c135cSArtem B. Bityutskiy 		return -EBADF;
620801c135cSArtem B. Bityutskiy 
62189b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
622801c135cSArtem B. Bityutskiy }
623801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
624801c135cSArtem B. Bityutskiy 
625801c135cSArtem B. Bityutskiy /**
6260e0ee1ccSDmitry Pervushin  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
627393852ecSArtem Bityutskiy  * @desc: volume descriptor
628393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
629393852ecSArtem Bityutskiy  *
630393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
63173ac36eaSColy Li  * eraseblock. This means, that after a successful invocation of this
632393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
633393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
634393852ecSArtem Bityutskiy  * happens.
635393852ecSArtem Bityutskiy  *
636393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
637393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
638393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
639393852ecSArtem Bityutskiy  * other failures.
640393852ecSArtem Bityutskiy  */
641b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
642393852ecSArtem Bityutskiy {
643393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
644393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
645393852ecSArtem Bityutskiy 
646c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
647393852ecSArtem Bityutskiy 
648393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
649393852ecSArtem Bityutskiy 		return -EROFS;
650393852ecSArtem Bityutskiy 
651393852ecSArtem Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
652393852ecSArtem Bityutskiy 		return -EINVAL;
653393852ecSArtem Bityutskiy 
654393852ecSArtem Bityutskiy 	if (vol->upd_marker)
655393852ecSArtem Bityutskiy 		return -EBADF;
656393852ecSArtem Bityutskiy 
657393852ecSArtem Bityutskiy 	if (vol->eba_tbl[lnum] >= 0)
658393852ecSArtem Bityutskiy 		return -EBADMSG;
659393852ecSArtem Bityutskiy 
660b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
661393852ecSArtem Bityutskiy }
662393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
663393852ecSArtem Bityutskiy 
664393852ecSArtem Bityutskiy /**
665801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
666801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
667801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
668801c135cSArtem B. Bityutskiy  *
669801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
670801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
671801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
672801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
673801c135cSArtem B. Bityutskiy  * mapped to.
674801c135cSArtem B. Bityutskiy  *
675801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
676801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
677801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
678801c135cSArtem B. Bityutskiy  * code.
679801c135cSArtem B. Bityutskiy  */
680801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
681801c135cSArtem B. Bityutskiy {
682801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
683801c135cSArtem B. Bityutskiy 
684c8566350SArtem Bityutskiy 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
685801c135cSArtem B. Bityutskiy 
686801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
687801c135cSArtem B. Bityutskiy 		return -EINVAL;
688801c135cSArtem B. Bityutskiy 
689801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
690801c135cSArtem B. Bityutskiy 		return -EBADF;
691801c135cSArtem B. Bityutskiy 
692801c135cSArtem B. Bityutskiy 	return vol->eba_tbl[lnum] >= 0;
693801c135cSArtem B. Bityutskiy }
694801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
695a5bf6190SArtem Bityutskiy 
696a5bf6190SArtem Bityutskiy /**
697a5bf6190SArtem Bityutskiy  * ubi_sync - synchronize UBI device buffers.
698a5bf6190SArtem Bityutskiy  * @ubi_num: UBI device to synchronize
699a5bf6190SArtem Bityutskiy  *
700a5bf6190SArtem Bityutskiy  * The underlying MTD device may cache data in hardware or in software. This
701a5bf6190SArtem Bityutskiy  * function ensures the caches are flushed. Returns zero in case of success and
702a5bf6190SArtem Bityutskiy  * a negative error code in case of failure.
703a5bf6190SArtem Bityutskiy  */
704a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num)
705a5bf6190SArtem Bityutskiy {
706a5bf6190SArtem Bityutskiy 	struct ubi_device *ubi;
707a5bf6190SArtem Bityutskiy 
708a5bf6190SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
709a5bf6190SArtem Bityutskiy 	if (!ubi)
710a5bf6190SArtem Bityutskiy 		return -ENODEV;
711a5bf6190SArtem Bityutskiy 
71285f2f2a8SArtem Bityutskiy 	mtd_sync(ubi->mtd);
713a5bf6190SArtem Bityutskiy 	ubi_put_device(ubi);
714a5bf6190SArtem Bityutskiy 	return 0;
715a5bf6190SArtem Bityutskiy }
716a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync);
7170e0ee1ccSDmitry Pervushin 
71862f38455SJoel Reardon /**
71962f38455SJoel Reardon  * ubi_flush - flush UBI work queue.
72062f38455SJoel Reardon  * @ubi_num: UBI device to flush work queue
72162f38455SJoel Reardon  * @vol_id: volume id to flush for
72262f38455SJoel Reardon  * @lnum: logical eraseblock number to flush for
72362f38455SJoel Reardon  *
72462f38455SJoel Reardon  * This function executes all pending works for a particular volume id / logical
72562f38455SJoel Reardon  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
72662f38455SJoel Reardon  * a wildcard for all of the corresponding volume numbers or logical
72762f38455SJoel Reardon  * eraseblock numbers. It returns zero in case of success and a negative error
72862f38455SJoel Reardon  * code in case of failure.
72962f38455SJoel Reardon  */
73062f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum)
73162f38455SJoel Reardon {
73262f38455SJoel Reardon 	struct ubi_device *ubi;
73362f38455SJoel Reardon 	int err = 0;
73462f38455SJoel Reardon 
73562f38455SJoel Reardon 	ubi = ubi_get_device(ubi_num);
73662f38455SJoel Reardon 	if (!ubi)
73762f38455SJoel Reardon 		return -ENODEV;
73862f38455SJoel Reardon 
73962f38455SJoel Reardon 	err = ubi_wl_flush(ubi, vol_id, lnum);
74062f38455SJoel Reardon 	ubi_put_device(ubi);
74162f38455SJoel Reardon 	return err;
74262f38455SJoel Reardon }
74362f38455SJoel Reardon EXPORT_SYMBOL_GPL(ubi_flush);
74462f38455SJoel Reardon 
7450e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
7460e0ee1ccSDmitry Pervushin 
7470e0ee1ccSDmitry Pervushin /**
7480e0ee1ccSDmitry Pervushin  * ubi_register_volume_notifier - register a volume notifier.
7490e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
7500e0ee1ccSDmitry Pervushin  * @ignore_existing: if non-zero, do not send "added" notification for all
7510e0ee1ccSDmitry Pervushin  *                   already existing volumes
7520e0ee1ccSDmitry Pervushin  *
7530e0ee1ccSDmitry Pervushin  * This function registers a volume notifier, which means that
7540e0ee1ccSDmitry Pervushin  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
7550e0ee1ccSDmitry Pervushin  * removed, re-sized, re-named, or updated. The first argument of the function
7560e0ee1ccSDmitry Pervushin  * is the notification type. The second argument is pointer to a
7570e0ee1ccSDmitry Pervushin  * &struct ubi_notification object which describes the notification event.
7580e0ee1ccSDmitry Pervushin  * Using UBI API from the volume notifier is prohibited.
7590e0ee1ccSDmitry Pervushin  *
7600e0ee1ccSDmitry Pervushin  * This function returns zero in case of success and a negative error code
7610e0ee1ccSDmitry Pervushin  * in case of failure.
7620e0ee1ccSDmitry Pervushin  */
7630e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
7640e0ee1ccSDmitry Pervushin 				 int ignore_existing)
7650e0ee1ccSDmitry Pervushin {
7660e0ee1ccSDmitry Pervushin 	int err;
7670e0ee1ccSDmitry Pervushin 
7680e0ee1ccSDmitry Pervushin 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
7690e0ee1ccSDmitry Pervushin 	if (err != 0)
7700e0ee1ccSDmitry Pervushin 		return err;
7710e0ee1ccSDmitry Pervushin 	if (ignore_existing)
7720e0ee1ccSDmitry Pervushin 		return 0;
7730e0ee1ccSDmitry Pervushin 
7740e0ee1ccSDmitry Pervushin 	/*
7750e0ee1ccSDmitry Pervushin 	 * We are going to walk all UBI devices and all volumes, and
7760e0ee1ccSDmitry Pervushin 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
7770e0ee1ccSDmitry Pervushin 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
7780e0ee1ccSDmitry Pervushin 	 * devices do not disappear.
7790e0ee1ccSDmitry Pervushin 	 */
7800e0ee1ccSDmitry Pervushin 	mutex_lock(&ubi_devices_mutex);
7810e0ee1ccSDmitry Pervushin 	ubi_enumerate_volumes(nb);
7820e0ee1ccSDmitry Pervushin 	mutex_unlock(&ubi_devices_mutex);
7830e0ee1ccSDmitry Pervushin 
7840e0ee1ccSDmitry Pervushin 	return err;
7850e0ee1ccSDmitry Pervushin }
7860e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
7870e0ee1ccSDmitry Pervushin 
7880e0ee1ccSDmitry Pervushin /**
7890e0ee1ccSDmitry Pervushin  * ubi_unregister_volume_notifier - unregister the volume notifier.
7900e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
7910e0ee1ccSDmitry Pervushin  *
7920e0ee1ccSDmitry Pervushin  * This function unregisters volume notifier @nm and returns zero in case of
7930e0ee1ccSDmitry Pervushin  * success and a negative error code in case of failure.
7940e0ee1ccSDmitry Pervushin  */
7950e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb)
7960e0ee1ccSDmitry Pervushin {
7970e0ee1ccSDmitry Pervushin 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
7980e0ee1ccSDmitry Pervushin }
7990e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
800