xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision 7554769641da272ea8821194c2efda08a11014b0)
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 {
30461edc3f3SRichard Weinberger 	int error, ubi_num, vol_id;
305ad022c87SRichard Weinberger 	struct path path;
30661edc3f3SRichard Weinberger 	struct kstat stat;
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 
313ad022c87SRichard Weinberger 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
314b5710284SCorentin Chary 	if (error)
315b5710284SCorentin Chary 		return ERR_PTR(error);
316b5710284SCorentin Chary 
31761edc3f3SRichard Weinberger 	error = vfs_getattr(&path, &stat);
318ad022c87SRichard Weinberger 	path_put(&path);
31961edc3f3SRichard Weinberger 	if (error)
32061edc3f3SRichard Weinberger 		return ERR_PTR(error);
321ad022c87SRichard Weinberger 
32261edc3f3SRichard Weinberger 	if (!S_ISCHR(stat.mode))
323b531b55aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
32461edc3f3SRichard Weinberger 
32561edc3f3SRichard Weinberger 	ubi_num = ubi_major2num(MAJOR(stat.rdev));
32661edc3f3SRichard Weinberger 	vol_id = MINOR(stat.rdev) - 1;
32761edc3f3SRichard Weinberger 
328b531b55aSArtem Bityutskiy 	if (vol_id >= 0 && ubi_num >= 0)
329b531b55aSArtem Bityutskiy 		return ubi_open_volume(ubi_num, vol_id, mode);
330b531b55aSArtem Bityutskiy 	return ERR_PTR(-ENODEV);
331b5710284SCorentin Chary }
332b5710284SCorentin Chary EXPORT_SYMBOL_GPL(ubi_open_volume_path);
333b5710284SCorentin Chary 
334b5710284SCorentin Chary /**
335801c135cSArtem B. Bityutskiy  * ubi_close_volume - close UBI volume.
336801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
337801c135cSArtem B. Bityutskiy  */
338801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc)
339801c135cSArtem B. Bityutskiy {
340801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
341e73f4459SArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
342801c135cSArtem B. Bityutskiy 
343e1cf7e6dSArtem Bityutskiy 	dbg_gen("close device %d, volume %d, mode %d",
344e1cf7e6dSArtem Bityutskiy 		ubi->ubi_num, vol->vol_id, desc->mode);
345801c135cSArtem B. Bityutskiy 
346e73f4459SArtem Bityutskiy 	spin_lock(&ubi->volumes_lock);
347801c135cSArtem B. Bityutskiy 	switch (desc->mode) {
348801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
349801c135cSArtem B. Bityutskiy 		vol->readers -= 1;
350801c135cSArtem B. Bityutskiy 		break;
351801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
352801c135cSArtem B. Bityutskiy 		vol->writers -= 1;
353801c135cSArtem B. Bityutskiy 		break;
354801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
355801c135cSArtem B. Bityutskiy 		vol->exclusive = 0;
356fafdd2bfSRichard Weinberger 		break;
357fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
358fafdd2bfSRichard Weinberger 		vol->metaonly = 0;
359fafdd2bfSRichard Weinberger 		break;
360801c135cSArtem B. Bityutskiy 	}
361d05c77a8SArtem Bityutskiy 	vol->ref_count -= 1;
362e73f4459SArtem Bityutskiy 	spin_unlock(&ubi->volumes_lock);
363801c135cSArtem B. Bityutskiy 
364d05c77a8SArtem Bityutskiy 	kfree(desc);
365e73f4459SArtem Bityutskiy 	put_device(&vol->dev);
366e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
367801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
368801c135cSArtem B. Bityutskiy }
369801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume);
370801c135cSArtem B. Bityutskiy 
371801c135cSArtem B. Bityutskiy /**
3729ff08979SRichard Weinberger  * leb_read_sanity_check - does sanity checks on read requests.
3739ff08979SRichard Weinberger  * @desc: volume descriptor
3749ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
3759ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
3769ff08979SRichard Weinberger  * @len: how many bytes to read
3779ff08979SRichard Weinberger  *
3789ff08979SRichard Weinberger  * This function is used by ubi_leb_read() and ubi_leb_read_sg()
3799ff08979SRichard Weinberger  * to perform sanity checks.
3809ff08979SRichard Weinberger  */
3819ff08979SRichard Weinberger static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
3829ff08979SRichard Weinberger 				 int offset, int len)
3839ff08979SRichard Weinberger {
3849ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
3859ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
3869ff08979SRichard Weinberger 	int vol_id = vol->vol_id;
3879ff08979SRichard Weinberger 
3889ff08979SRichard Weinberger 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
3899ff08979SRichard Weinberger 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
3909ff08979SRichard Weinberger 	    offset + len > vol->usable_leb_size)
3919ff08979SRichard Weinberger 		return -EINVAL;
3929ff08979SRichard Weinberger 
3939ff08979SRichard Weinberger 	if (vol->vol_type == UBI_STATIC_VOLUME) {
3949ff08979SRichard Weinberger 		if (vol->used_ebs == 0)
3959ff08979SRichard Weinberger 			/* Empty static UBI volume */
3969ff08979SRichard Weinberger 			return 0;
3979ff08979SRichard Weinberger 		if (lnum == vol->used_ebs - 1 &&
3989ff08979SRichard Weinberger 		    offset + len > vol->last_eb_bytes)
3999ff08979SRichard Weinberger 			return -EINVAL;
4009ff08979SRichard Weinberger 	}
4019ff08979SRichard Weinberger 
4029ff08979SRichard Weinberger 	if (vol->upd_marker)
4039ff08979SRichard Weinberger 		return -EBADF;
4049ff08979SRichard Weinberger 
4059ff08979SRichard Weinberger 	return 0;
4069ff08979SRichard Weinberger }
4079ff08979SRichard Weinberger 
4089ff08979SRichard Weinberger /**
409801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
410801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
411801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
412801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
413801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
414801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
415801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
416801c135cSArtem B. Bityutskiy  *
417801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
418801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
419801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
420801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
421801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
422801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
423801c135cSArtem B. Bityutskiy  *
424801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
425801c135cSArtem B. Bityutskiy  * function returns a negative error code.
426801c135cSArtem B. Bityutskiy  *
427801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
428801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
429801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
430801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
431801c135cSArtem B. Bityutskiy  *
432801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
433801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
434801c135cSArtem B. Bityutskiy  */
435801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
436801c135cSArtem B. Bityutskiy 		 int len, int check)
437801c135cSArtem B. Bityutskiy {
438801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
439801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
440801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
441801c135cSArtem B. Bityutskiy 
442c8566350SArtem Bityutskiy 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
443801c135cSArtem B. Bityutskiy 
4449ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4459ff08979SRichard Weinberger 	if (err < 0)
4469ff08979SRichard Weinberger 		return err;
447801c135cSArtem B. Bityutskiy 
448801c135cSArtem B. Bityutskiy 	if (len == 0)
449801c135cSArtem B. Bityutskiy 		return 0;
450801c135cSArtem B. Bityutskiy 
45189b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
452d57f4054SBrian Norris 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
45332608703STanya Brokhman 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
454801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
455801c135cSArtem B. Bityutskiy 	}
456801c135cSArtem B. Bityutskiy 
457801c135cSArtem B. Bityutskiy 	return err;
458801c135cSArtem B. Bityutskiy }
459801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
460801c135cSArtem B. Bityutskiy 
4619ff08979SRichard Weinberger 
4629ff08979SRichard Weinberger /**
4639ff08979SRichard Weinberger  * ubi_leb_read_sg - read data into a scatter gather list.
4649ff08979SRichard Weinberger  * @desc: volume descriptor
4659ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
4669ff08979SRichard Weinberger  * @buf: buffer where to store the read data
4679ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
4689ff08979SRichard Weinberger  * @len: how many bytes to read
4699ff08979SRichard Weinberger  * @check: whether UBI has to check the read data's CRC or not.
4709ff08979SRichard Weinberger  *
4719ff08979SRichard Weinberger  * This function works exactly like ubi_leb_read_sg(). But instead of
4729ff08979SRichard Weinberger  * storing the read data into a buffer it writes to an UBI scatter gather
4739ff08979SRichard Weinberger  * list.
4749ff08979SRichard Weinberger  */
4759ff08979SRichard Weinberger int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
4769ff08979SRichard Weinberger 		    int offset, int len, int check)
4779ff08979SRichard Weinberger {
4789ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
4799ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
4809ff08979SRichard Weinberger 	int err, vol_id = vol->vol_id;
4819ff08979SRichard Weinberger 
4829ff08979SRichard Weinberger 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
4839ff08979SRichard Weinberger 
4849ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4859ff08979SRichard Weinberger 	if (err < 0)
4869ff08979SRichard Weinberger 		return err;
4879ff08979SRichard Weinberger 
4889ff08979SRichard Weinberger 	if (len == 0)
4899ff08979SRichard Weinberger 		return 0;
4909ff08979SRichard Weinberger 
4919ff08979SRichard Weinberger 	err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
4929ff08979SRichard Weinberger 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
4939ff08979SRichard Weinberger 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
4949ff08979SRichard Weinberger 		vol->corrupted = 1;
4959ff08979SRichard Weinberger 	}
4969ff08979SRichard Weinberger 
4979ff08979SRichard Weinberger 	return err;
4989ff08979SRichard Weinberger }
4999ff08979SRichard Weinberger EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
5009ff08979SRichard Weinberger 
501801c135cSArtem B. Bityutskiy /**
502801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
503801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
504801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
505801c135cSArtem B. Bityutskiy  * @buf: data to write
506801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
507801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
508801c135cSArtem B. Bityutskiy  *
509801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
510b36a261eSRichard Weinberger  * logical eraseblock @lnum.
511801c135cSArtem B. Bityutskiy  *
512801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
513801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
514801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
515801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
516801c135cSArtem B. Bityutskiy  *
517801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
518801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
519801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
520801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
521801c135cSArtem B. Bityutskiy  * garbage.
522801c135cSArtem B. Bityutskiy  *
523801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
524801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
525801c135cSArtem B. Bityutskiy  */
526801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
527b36a261eSRichard Weinberger 		  int offset, int len)
528801c135cSArtem B. Bityutskiy {
529801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
530801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
531801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
532801c135cSArtem B. Bityutskiy 
533c8566350SArtem Bityutskiy 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
534801c135cSArtem B. Bityutskiy 
535801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
536801c135cSArtem B. Bityutskiy 		return -EINVAL;
537801c135cSArtem B. Bityutskiy 
538801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
539801c135cSArtem B. Bityutskiy 		return -EROFS;
540801c135cSArtem B. Bityutskiy 
5419a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
542cadb40ccSKyungmin Park 	    offset + len > vol->usable_leb_size ||
543cadb40ccSKyungmin Park 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
544801c135cSArtem B. Bityutskiy 		return -EINVAL;
545801c135cSArtem B. Bityutskiy 
546801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
547801c135cSArtem B. Bityutskiy 		return -EBADF;
548801c135cSArtem B. Bityutskiy 
549801c135cSArtem B. Bityutskiy 	if (len == 0)
550801c135cSArtem B. Bityutskiy 		return 0;
551801c135cSArtem B. Bityutskiy 
552b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
553801c135cSArtem B. Bityutskiy }
554801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
555801c135cSArtem B. Bityutskiy 
556801c135cSArtem B. Bityutskiy /*
557801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
558801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
559801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
560801c135cSArtem B. Bityutskiy  * @buf: data to write
561801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
562801c135cSArtem B. Bityutskiy  *
563801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
564801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
5653f502622SShinya Kuribayashi  * data, which has to be aligned. The length may be shorter than the logical
566801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
567801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
568801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
569801c135cSArtem B. Bityutskiy  * code in case of failure.
570801c135cSArtem B. Bityutskiy  */
571801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
572b36a261eSRichard Weinberger 		   int len)
573801c135cSArtem B. Bityutskiy {
574801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
575801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
576801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
577801c135cSArtem B. Bityutskiy 
578c8566350SArtem Bityutskiy 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
579801c135cSArtem B. Bityutskiy 
580801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
581801c135cSArtem B. Bityutskiy 		return -EINVAL;
582801c135cSArtem B. Bityutskiy 
583801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
584801c135cSArtem B. Bityutskiy 		return -EROFS;
585801c135cSArtem B. Bityutskiy 
5869a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum) || len < 0 ||
587cadb40ccSKyungmin Park 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
588801c135cSArtem B. Bityutskiy 		return -EINVAL;
589801c135cSArtem B. Bityutskiy 
590801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
591801c135cSArtem B. Bityutskiy 		return -EBADF;
592801c135cSArtem B. Bityutskiy 
593801c135cSArtem B. Bityutskiy 	if (len == 0)
594801c135cSArtem B. Bityutskiy 		return 0;
595801c135cSArtem B. Bityutskiy 
596b36a261eSRichard Weinberger 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
597801c135cSArtem B. Bityutskiy }
598801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
599801c135cSArtem B. Bityutskiy 
600801c135cSArtem B. Bityutskiy /**
601801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
602801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
603801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
604801c135cSArtem B. Bityutskiy  *
605801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
606801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
607801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
608801c135cSArtem B. Bityutskiy  *
609801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
610801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
611801c135cSArtem B. Bityutskiy  */
612801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
613801c135cSArtem B. Bityutskiy {
614801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
615801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
616ae616e1bSArtem Bityutskiy 	int err;
617801c135cSArtem B. Bityutskiy 
618c8566350SArtem Bityutskiy 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
619801c135cSArtem B. Bityutskiy 
620801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
621801c135cSArtem B. Bityutskiy 		return -EROFS;
622801c135cSArtem B. Bityutskiy 
6239a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
624801c135cSArtem B. Bityutskiy 		return -EINVAL;
625801c135cSArtem B. Bityutskiy 
626801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
627801c135cSArtem B. Bityutskiy 		return -EBADF;
628801c135cSArtem B. Bityutskiy 
62989b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
630801c135cSArtem B. Bityutskiy 	if (err)
631801c135cSArtem B. Bityutskiy 		return err;
632801c135cSArtem B. Bityutskiy 
63362f38455SJoel Reardon 	return ubi_wl_flush(ubi, vol->vol_id, lnum);
634801c135cSArtem B. Bityutskiy }
635801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
636801c135cSArtem B. Bityutskiy 
637801c135cSArtem B. Bityutskiy /**
638801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
639801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
640801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
641801c135cSArtem B. Bityutskiy  *
642801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
643801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
6443f502622SShinya Kuribayashi  * physically erased in background. This operation is much faster than the
645801c135cSArtem B. Bityutskiy  * erase operation.
646801c135cSArtem B. Bityutskiy  *
647801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
648801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
649801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
650801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
651801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
652801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
653801c135cSArtem B. Bityutskiy  * used with care.
654801c135cSArtem B. Bityutskiy  *
655801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
656801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
657801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
658801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
659801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
660801c135cSArtem B. Bityutskiy  *
661801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
662801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
6633f502622SShinya Kuribayashi  * first un-map it, then write new data, rather than first erase it, then write
664801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
665801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
666801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
667801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
668801c135cSArtem B. Bityutskiy  *
669801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
670801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
671801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
672801c135cSArtem B. Bityutskiy  */
673801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
674801c135cSArtem B. Bityutskiy {
675801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
676801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
677801c135cSArtem B. Bityutskiy 
678c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
679801c135cSArtem B. Bityutskiy 
680801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
681801c135cSArtem B. Bityutskiy 		return -EROFS;
682801c135cSArtem B. Bityutskiy 
6839a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
684801c135cSArtem B. Bityutskiy 		return -EINVAL;
685801c135cSArtem B. Bityutskiy 
686801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
687801c135cSArtem B. Bityutskiy 		return -EBADF;
688801c135cSArtem B. Bityutskiy 
68989b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
690801c135cSArtem B. Bityutskiy }
691801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
692801c135cSArtem B. Bityutskiy 
693801c135cSArtem B. Bityutskiy /**
6940e0ee1ccSDmitry Pervushin  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
695393852ecSArtem Bityutskiy  * @desc: volume descriptor
696393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
697393852ecSArtem Bityutskiy  *
698393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
69973ac36eaSColy Li  * eraseblock. This means, that after a successful invocation of this
700393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
701393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
702393852ecSArtem Bityutskiy  * happens.
703393852ecSArtem Bityutskiy  *
704393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
705393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
706393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
707393852ecSArtem Bityutskiy  * other failures.
708393852ecSArtem Bityutskiy  */
709b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
710393852ecSArtem Bityutskiy {
711393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
712393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
713393852ecSArtem Bityutskiy 
714960b35d0Sz00189512 	dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
715393852ecSArtem Bityutskiy 
716393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
717393852ecSArtem Bityutskiy 		return -EROFS;
718393852ecSArtem Bityutskiy 
7199a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
720393852ecSArtem Bityutskiy 		return -EINVAL;
721393852ecSArtem Bityutskiy 
722393852ecSArtem Bityutskiy 	if (vol->upd_marker)
723393852ecSArtem Bityutskiy 		return -EBADF;
724393852ecSArtem Bityutskiy 
725*75547696SBoris Brezillon 	if (ubi_eba_is_mapped(vol, lnum))
726393852ecSArtem Bityutskiy 		return -EBADMSG;
727393852ecSArtem Bityutskiy 
728b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
729393852ecSArtem Bityutskiy }
730393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
731393852ecSArtem Bityutskiy 
732393852ecSArtem Bityutskiy /**
733801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
734801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
735801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
736801c135cSArtem B. Bityutskiy  *
737801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
738801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
739801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
740801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
741801c135cSArtem B. Bityutskiy  * mapped to.
742801c135cSArtem B. Bityutskiy  *
743801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
744801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
745801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
746801c135cSArtem B. Bityutskiy  * code.
747801c135cSArtem B. Bityutskiy  */
748801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
749801c135cSArtem B. Bityutskiy {
750801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
751801c135cSArtem B. Bityutskiy 
752c8566350SArtem Bityutskiy 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
753801c135cSArtem B. Bityutskiy 
7549a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
755801c135cSArtem B. Bityutskiy 		return -EINVAL;
756801c135cSArtem B. Bityutskiy 
757801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
758801c135cSArtem B. Bityutskiy 		return -EBADF;
759801c135cSArtem B. Bityutskiy 
760*75547696SBoris Brezillon 	return ubi_eba_is_mapped(vol, lnum);
761801c135cSArtem B. Bityutskiy }
762801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
763a5bf6190SArtem Bityutskiy 
764a5bf6190SArtem Bityutskiy /**
765a5bf6190SArtem Bityutskiy  * ubi_sync - synchronize UBI device buffers.
766a5bf6190SArtem Bityutskiy  * @ubi_num: UBI device to synchronize
767a5bf6190SArtem Bityutskiy  *
768a5bf6190SArtem Bityutskiy  * The underlying MTD device may cache data in hardware or in software. This
769a5bf6190SArtem Bityutskiy  * function ensures the caches are flushed. Returns zero in case of success and
770a5bf6190SArtem Bityutskiy  * a negative error code in case of failure.
771a5bf6190SArtem Bityutskiy  */
772a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num)
773a5bf6190SArtem Bityutskiy {
774a5bf6190SArtem Bityutskiy 	struct ubi_device *ubi;
775a5bf6190SArtem Bityutskiy 
776a5bf6190SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
777a5bf6190SArtem Bityutskiy 	if (!ubi)
778a5bf6190SArtem Bityutskiy 		return -ENODEV;
779a5bf6190SArtem Bityutskiy 
78085f2f2a8SArtem Bityutskiy 	mtd_sync(ubi->mtd);
781a5bf6190SArtem Bityutskiy 	ubi_put_device(ubi);
782a5bf6190SArtem Bityutskiy 	return 0;
783a5bf6190SArtem Bityutskiy }
784a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync);
7850e0ee1ccSDmitry Pervushin 
78662f38455SJoel Reardon /**
78762f38455SJoel Reardon  * ubi_flush - flush UBI work queue.
78862f38455SJoel Reardon  * @ubi_num: UBI device to flush work queue
78962f38455SJoel Reardon  * @vol_id: volume id to flush for
79062f38455SJoel Reardon  * @lnum: logical eraseblock number to flush for
79162f38455SJoel Reardon  *
79262f38455SJoel Reardon  * This function executes all pending works for a particular volume id / logical
79362f38455SJoel Reardon  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
79462f38455SJoel Reardon  * a wildcard for all of the corresponding volume numbers or logical
79562f38455SJoel Reardon  * eraseblock numbers. It returns zero in case of success and a negative error
79662f38455SJoel Reardon  * code in case of failure.
79762f38455SJoel Reardon  */
79862f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum)
79962f38455SJoel Reardon {
80062f38455SJoel Reardon 	struct ubi_device *ubi;
80162f38455SJoel Reardon 	int err = 0;
80262f38455SJoel Reardon 
80362f38455SJoel Reardon 	ubi = ubi_get_device(ubi_num);
80462f38455SJoel Reardon 	if (!ubi)
80562f38455SJoel Reardon 		return -ENODEV;
80662f38455SJoel Reardon 
80762f38455SJoel Reardon 	err = ubi_wl_flush(ubi, vol_id, lnum);
80862f38455SJoel Reardon 	ubi_put_device(ubi);
80962f38455SJoel Reardon 	return err;
81062f38455SJoel Reardon }
81162f38455SJoel Reardon EXPORT_SYMBOL_GPL(ubi_flush);
81262f38455SJoel Reardon 
8130e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
8140e0ee1ccSDmitry Pervushin 
8150e0ee1ccSDmitry Pervushin /**
8160e0ee1ccSDmitry Pervushin  * ubi_register_volume_notifier - register a volume notifier.
8170e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8180e0ee1ccSDmitry Pervushin  * @ignore_existing: if non-zero, do not send "added" notification for all
8190e0ee1ccSDmitry Pervushin  *                   already existing volumes
8200e0ee1ccSDmitry Pervushin  *
8210e0ee1ccSDmitry Pervushin  * This function registers a volume notifier, which means that
8220e0ee1ccSDmitry Pervushin  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
8230e0ee1ccSDmitry Pervushin  * removed, re-sized, re-named, or updated. The first argument of the function
8240e0ee1ccSDmitry Pervushin  * is the notification type. The second argument is pointer to a
8250e0ee1ccSDmitry Pervushin  * &struct ubi_notification object which describes the notification event.
8260e0ee1ccSDmitry Pervushin  * Using UBI API from the volume notifier is prohibited.
8270e0ee1ccSDmitry Pervushin  *
8280e0ee1ccSDmitry Pervushin  * This function returns zero in case of success and a negative error code
8290e0ee1ccSDmitry Pervushin  * in case of failure.
8300e0ee1ccSDmitry Pervushin  */
8310e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
8320e0ee1ccSDmitry Pervushin 				 int ignore_existing)
8330e0ee1ccSDmitry Pervushin {
8340e0ee1ccSDmitry Pervushin 	int err;
8350e0ee1ccSDmitry Pervushin 
8360e0ee1ccSDmitry Pervushin 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
8370e0ee1ccSDmitry Pervushin 	if (err != 0)
8380e0ee1ccSDmitry Pervushin 		return err;
8390e0ee1ccSDmitry Pervushin 	if (ignore_existing)
8400e0ee1ccSDmitry Pervushin 		return 0;
8410e0ee1ccSDmitry Pervushin 
8420e0ee1ccSDmitry Pervushin 	/*
8430e0ee1ccSDmitry Pervushin 	 * We are going to walk all UBI devices and all volumes, and
8440e0ee1ccSDmitry Pervushin 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
8450e0ee1ccSDmitry Pervushin 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
8460e0ee1ccSDmitry Pervushin 	 * devices do not disappear.
8470e0ee1ccSDmitry Pervushin 	 */
8480e0ee1ccSDmitry Pervushin 	mutex_lock(&ubi_devices_mutex);
8490e0ee1ccSDmitry Pervushin 	ubi_enumerate_volumes(nb);
8500e0ee1ccSDmitry Pervushin 	mutex_unlock(&ubi_devices_mutex);
8510e0ee1ccSDmitry Pervushin 
8520e0ee1ccSDmitry Pervushin 	return err;
8530e0ee1ccSDmitry Pervushin }
8540e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
8550e0ee1ccSDmitry Pervushin 
8560e0ee1ccSDmitry Pervushin /**
8570e0ee1ccSDmitry Pervushin  * ubi_unregister_volume_notifier - unregister the volume notifier.
8580e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8590e0ee1ccSDmitry Pervushin  *
8600e0ee1ccSDmitry Pervushin  * This function unregisters volume notifier @nm and returns zero in case of
8610e0ee1ccSDmitry Pervushin  * success and a negative error code in case of failure.
8620e0ee1ccSDmitry Pervushin  */
8630e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb)
8640e0ee1ccSDmitry Pervushin {
8650e0ee1ccSDmitry Pervushin 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
8660e0ee1ccSDmitry Pervushin }
8670e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
868