xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision b6baa9962648aee4a22c8b6a31ce69585b03d173)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy  *
5801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
6801c135cSArtem B. Bityutskiy  */
7801c135cSArtem B. Bityutskiy 
8801c135cSArtem B. Bityutskiy /* This file mostly implements UBI kernel API functions */
9801c135cSArtem B. Bityutskiy 
10801c135cSArtem B. Bityutskiy #include <linux/module.h>
11801c135cSArtem B. Bityutskiy #include <linux/err.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13b5710284SCorentin Chary #include <linux/namei.h>
14b5710284SCorentin Chary #include <linux/fs.h>
15801c135cSArtem B. Bityutskiy #include <asm/div64.h>
16801c135cSArtem B. Bityutskiy #include "ubi.h"
17801c135cSArtem B. Bityutskiy 
18801c135cSArtem B. Bityutskiy /**
190e0ee1ccSDmitry Pervushin  * ubi_do_get_device_info - get information about UBI device.
200e0ee1ccSDmitry Pervushin  * @ubi: UBI device description object
210e0ee1ccSDmitry Pervushin  * @di: the information is stored here
220e0ee1ccSDmitry Pervushin  *
230e0ee1ccSDmitry Pervushin  * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
240e0ee1ccSDmitry Pervushin  * device is locked and cannot disappear.
250e0ee1ccSDmitry Pervushin  */
260e0ee1ccSDmitry Pervushin void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
270e0ee1ccSDmitry Pervushin {
280e0ee1ccSDmitry Pervushin 	di->ubi_num = ubi->ubi_num;
290e0ee1ccSDmitry Pervushin 	di->leb_size = ubi->leb_size;
30f43ec882SArtem Bityutskiy 	di->leb_start = ubi->leb_start;
310e0ee1ccSDmitry Pervushin 	di->min_io_size = ubi->min_io_size;
3230b542efSArtem Bityutskiy 	di->max_write_size = ubi->max_write_size;
330e0ee1ccSDmitry Pervushin 	di->ro_mode = ubi->ro_mode;
340e0ee1ccSDmitry Pervushin 	di->cdev = ubi->cdev.dev;
350e0ee1ccSDmitry Pervushin }
360e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
370e0ee1ccSDmitry Pervushin 
380e0ee1ccSDmitry Pervushin /**
39801c135cSArtem B. Bityutskiy  * ubi_get_device_info - get information about UBI device.
40801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
41801c135cSArtem B. Bityutskiy  * @di: the information is stored here
42801c135cSArtem B. Bityutskiy  *
43e73f4459SArtem Bityutskiy  * This function returns %0 in case of success, %-EINVAL if the UBI device
44e73f4459SArtem Bityutskiy  * number is invalid, and %-ENODEV if there is no such UBI device.
45801c135cSArtem B. Bityutskiy  */
46801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
47801c135cSArtem B. Bityutskiy {
48e73f4459SArtem Bityutskiy 	struct ubi_device *ubi;
49801c135cSArtem B. Bityutskiy 
50e73f4459SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
51e73f4459SArtem Bityutskiy 		return -EINVAL;
52e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
53e73f4459SArtem Bityutskiy 	if (!ubi)
54801c135cSArtem B. Bityutskiy 		return -ENODEV;
550e0ee1ccSDmitry Pervushin 	ubi_do_get_device_info(ubi, di);
56e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
57801c135cSArtem B. Bityutskiy 	return 0;
58801c135cSArtem B. Bityutskiy }
59801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info);
60801c135cSArtem B. Bityutskiy 
61801c135cSArtem B. Bityutskiy /**
620e0ee1ccSDmitry Pervushin  * ubi_do_get_volume_info - get information about UBI volume.
630e0ee1ccSDmitry Pervushin  * @ubi: UBI device description object
640e0ee1ccSDmitry Pervushin  * @vol: volume description object
65801c135cSArtem B. Bityutskiy  * @vi: the information is stored here
66801c135cSArtem B. Bityutskiy  */
670e0ee1ccSDmitry Pervushin void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
68801c135cSArtem B. Bityutskiy 			    struct ubi_volume_info *vi)
69801c135cSArtem B. Bityutskiy {
70801c135cSArtem B. Bityutskiy 	vi->vol_id = vol->vol_id;
71801c135cSArtem B. Bityutskiy 	vi->ubi_num = ubi->ubi_num;
72801c135cSArtem B. Bityutskiy 	vi->size = vol->reserved_pebs;
73801c135cSArtem B. Bityutskiy 	vi->used_bytes = vol->used_bytes;
74801c135cSArtem B. Bityutskiy 	vi->vol_type = vol->vol_type;
75801c135cSArtem B. Bityutskiy 	vi->corrupted = vol->corrupted;
76801c135cSArtem B. Bityutskiy 	vi->upd_marker = vol->upd_marker;
77801c135cSArtem B. Bityutskiy 	vi->alignment = vol->alignment;
78801c135cSArtem B. Bityutskiy 	vi->usable_leb_size = vol->usable_leb_size;
79801c135cSArtem B. Bityutskiy 	vi->name_len = vol->name_len;
80801c135cSArtem B. Bityutskiy 	vi->name = vol->name;
8149dfc299SArtem Bityutskiy 	vi->cdev = vol->cdev.dev;
82801c135cSArtem B. Bityutskiy }
830e0ee1ccSDmitry Pervushin 
840e0ee1ccSDmitry Pervushin /**
850e0ee1ccSDmitry Pervushin  * ubi_get_volume_info - get information about UBI volume.
860e0ee1ccSDmitry Pervushin  * @desc: volume descriptor
870e0ee1ccSDmitry Pervushin  * @vi: the information is stored here
880e0ee1ccSDmitry Pervushin  */
890e0ee1ccSDmitry Pervushin void ubi_get_volume_info(struct ubi_volume_desc *desc,
900e0ee1ccSDmitry Pervushin 			 struct ubi_volume_info *vi)
910e0ee1ccSDmitry Pervushin {
920e0ee1ccSDmitry Pervushin 	ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
930e0ee1ccSDmitry Pervushin }
94801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info);
95801c135cSArtem B. Bityutskiy 
96801c135cSArtem B. Bityutskiy /**
97801c135cSArtem B. Bityutskiy  * ubi_open_volume - open UBI volume.
98801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
99801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
100801c135cSArtem B. Bityutskiy  * @mode: open mode
101801c135cSArtem B. Bityutskiy  *
102801c135cSArtem B. Bityutskiy  * The @mode parameter specifies if the volume should be opened in read-only
103801c135cSArtem B. Bityutskiy  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
104801c135cSArtem B. Bityutskiy  * nobody else will be able to open this volume. UBI allows to have many volume
105801c135cSArtem B. Bityutskiy  * readers and one writer at a time.
106801c135cSArtem B. Bityutskiy  *
107801c135cSArtem B. Bityutskiy  * If a static volume is being opened for the first time since boot, it will be
108801c135cSArtem B. Bityutskiy  * checked by this function, which means it will be fully read and the CRC
109801c135cSArtem B. Bityutskiy  * checksum of each logical eraseblock will be checked.
110801c135cSArtem B. Bityutskiy  *
111801c135cSArtem B. Bityutskiy  * This function returns volume descriptor in case of success and a negative
112801c135cSArtem B. Bityutskiy  * error code in case of failure.
113801c135cSArtem B. Bityutskiy  */
114801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
115801c135cSArtem B. Bityutskiy {
116801c135cSArtem B. Bityutskiy 	int err;
117801c135cSArtem B. Bityutskiy 	struct ubi_volume_desc *desc;
1180169b49dSJesper Juhl 	struct ubi_device *ubi;
119801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol;
120801c135cSArtem B. Bityutskiy 
121e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
122801c135cSArtem B. Bityutskiy 
12335ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
12435ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
1250169b49dSJesper Juhl 
126801c135cSArtem B. Bityutskiy 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
127fafdd2bfSRichard Weinberger 	    mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
12835ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
12935ad5fb7SArtem Bityutskiy 
130e73f4459SArtem Bityutskiy 	/*
131e73f4459SArtem Bityutskiy 	 * First of all, we have to get the UBI device to prevent its removal.
132e73f4459SArtem Bityutskiy 	 */
133e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
13435ad5fb7SArtem Bityutskiy 	if (!ubi)
13535ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
13635ad5fb7SArtem Bityutskiy 
137e73f4459SArtem Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
138e73f4459SArtem Bityutskiy 		err = -EINVAL;
139e73f4459SArtem Bityutskiy 		goto out_put_ubi;
140e73f4459SArtem Bityutskiy 	}
141801c135cSArtem B. Bityutskiy 
142801c135cSArtem B. Bityutskiy 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
143e73f4459SArtem Bityutskiy 	if (!desc) {
144e73f4459SArtem Bityutskiy 		err = -ENOMEM;
145e73f4459SArtem Bityutskiy 		goto out_put_ubi;
146e73f4459SArtem Bityutskiy 	}
14735ad5fb7SArtem Bityutskiy 
14835ad5fb7SArtem Bityutskiy 	err = -ENODEV;
14935ad5fb7SArtem Bityutskiy 	if (!try_module_get(THIS_MODULE))
15035ad5fb7SArtem Bityutskiy 		goto out_free;
151801c135cSArtem B. Bityutskiy 
152801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
153801c135cSArtem B. Bityutskiy 	vol = ubi->volumes[vol_id];
15435ad5fb7SArtem Bityutskiy 	if (!vol)
155801c135cSArtem B. Bityutskiy 		goto out_unlock;
156801c135cSArtem B. Bityutskiy 
157801c135cSArtem B. Bityutskiy 	err = -EBUSY;
158801c135cSArtem B. Bityutskiy 	switch (mode) {
159801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
160801c135cSArtem B. Bityutskiy 		if (vol->exclusive)
161801c135cSArtem B. Bityutskiy 			goto out_unlock;
162801c135cSArtem B. Bityutskiy 		vol->readers += 1;
163801c135cSArtem B. Bityutskiy 		break;
164801c135cSArtem B. Bityutskiy 
165801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
166801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers > 0)
167801c135cSArtem B. Bityutskiy 			goto out_unlock;
168801c135cSArtem B. Bityutskiy 		vol->writers += 1;
169801c135cSArtem B. Bityutskiy 		break;
170801c135cSArtem B. Bityutskiy 
171801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
172fafdd2bfSRichard Weinberger 		if (vol->exclusive || vol->writers || vol->readers ||
173fafdd2bfSRichard Weinberger 		    vol->metaonly)
174801c135cSArtem B. Bityutskiy 			goto out_unlock;
175801c135cSArtem B. Bityutskiy 		vol->exclusive = 1;
176801c135cSArtem B. Bityutskiy 		break;
177fafdd2bfSRichard Weinberger 
178fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
179fafdd2bfSRichard Weinberger 		if (vol->metaonly || vol->exclusive)
180fafdd2bfSRichard Weinberger 			goto out_unlock;
181fafdd2bfSRichard Weinberger 		vol->metaonly = 1;
182fafdd2bfSRichard Weinberger 		break;
183801c135cSArtem B. Bityutskiy 	}
184450f872aSArtem Bityutskiy 	get_device(&vol->dev);
185d05c77a8SArtem Bityutskiy 	vol->ref_count += 1;
186801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
187801c135cSArtem B. Bityutskiy 
188801c135cSArtem B. Bityutskiy 	desc->vol = vol;
189801c135cSArtem B. Bityutskiy 	desc->mode = mode;
190801c135cSArtem B. Bityutskiy 
191783b273aSArtem Bityutskiy 	mutex_lock(&ubi->ckvol_mutex);
19262652517SQuentin Schulz 	if (!vol->checked && !vol->skip_check) {
193801c135cSArtem B. Bityutskiy 		/* This is the first open - check the volume */
194801c135cSArtem B. Bityutskiy 		err = ubi_check_volume(ubi, vol_id);
195801c135cSArtem B. Bityutskiy 		if (err < 0) {
196783b273aSArtem Bityutskiy 			mutex_unlock(&ubi->ckvol_mutex);
197801c135cSArtem B. Bityutskiy 			ubi_close_volume(desc);
198801c135cSArtem B. Bityutskiy 			return ERR_PTR(err);
199801c135cSArtem B. Bityutskiy 		}
200801c135cSArtem B. Bityutskiy 		if (err == 1) {
20132608703STanya Brokhman 			ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
202801c135cSArtem B. Bityutskiy 				 vol_id, ubi->ubi_num);
203801c135cSArtem B. Bityutskiy 			vol->corrupted = 1;
204801c135cSArtem B. Bityutskiy 		}
205801c135cSArtem B. Bityutskiy 		vol->checked = 1;
206801c135cSArtem B. Bityutskiy 	}
207783b273aSArtem Bityutskiy 	mutex_unlock(&ubi->ckvol_mutex);
20835ad5fb7SArtem Bityutskiy 
209801c135cSArtem B. Bityutskiy 	return desc;
210801c135cSArtem B. Bityutskiy 
211801c135cSArtem B. Bityutskiy out_unlock:
212801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
213801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
21435ad5fb7SArtem Bityutskiy out_free:
21535ad5fb7SArtem Bityutskiy 	kfree(desc);
216e73f4459SArtem Bityutskiy out_put_ubi:
21732608703STanya Brokhman 	ubi_err(ubi, "cannot open device %d, volume %d, error %d",
218e1cf7e6dSArtem Bityutskiy 		ubi_num, vol_id, err);
219e5420877SPan Bian 	ubi_put_device(ubi);
220801c135cSArtem B. Bityutskiy 	return ERR_PTR(err);
221801c135cSArtem B. Bityutskiy }
222801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume);
223801c135cSArtem B. Bityutskiy 
224801c135cSArtem B. Bityutskiy /**
225801c135cSArtem B. Bityutskiy  * ubi_open_volume_nm - open UBI volume by name.
226801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
227801c135cSArtem B. Bityutskiy  * @name: volume name
228801c135cSArtem B. Bityutskiy  * @mode: open mode
229801c135cSArtem B. Bityutskiy  *
230801c135cSArtem B. Bityutskiy  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
231801c135cSArtem B. Bityutskiy  */
232801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
233801c135cSArtem B. Bityutskiy 					   int mode)
234801c135cSArtem B. Bityutskiy {
235801c135cSArtem B. Bityutskiy 	int i, vol_id = -1, len;
236801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi;
237e73f4459SArtem Bityutskiy 	struct ubi_volume_desc *ret;
238801c135cSArtem B. Bityutskiy 
239e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
240801c135cSArtem B. Bityutskiy 
241801c135cSArtem B. Bityutskiy 	if (!name)
242801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
243801c135cSArtem B. Bityutskiy 
244801c135cSArtem B. Bityutskiy 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
245801c135cSArtem B. Bityutskiy 	if (len > UBI_VOL_NAME_MAX)
246801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
247801c135cSArtem B. Bityutskiy 
24835ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
24935ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
250801c135cSArtem B. Bityutskiy 
251e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
25235ad5fb7SArtem Bityutskiy 	if (!ubi)
25335ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
254801c135cSArtem B. Bityutskiy 
255801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
256801c135cSArtem B. Bityutskiy 	/* Walk all volumes of this UBI device */
257801c135cSArtem B. Bityutskiy 	for (i = 0; i < ubi->vtbl_slots; i++) {
258801c135cSArtem B. Bityutskiy 		struct ubi_volume *vol = ubi->volumes[i];
259801c135cSArtem B. Bityutskiy 
260801c135cSArtem B. Bityutskiy 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
261801c135cSArtem B. Bityutskiy 			vol_id = i;
262801c135cSArtem B. Bityutskiy 			break;
263801c135cSArtem B. Bityutskiy 		}
264801c135cSArtem B. Bityutskiy 	}
265801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
266801c135cSArtem B. Bityutskiy 
267e73f4459SArtem Bityutskiy 	if (vol_id >= 0)
268e73f4459SArtem Bityutskiy 		ret = ubi_open_volume(ubi_num, vol_id, mode);
269e73f4459SArtem Bityutskiy 	else
270e73f4459SArtem Bityutskiy 		ret = ERR_PTR(-ENODEV);
271801c135cSArtem B. Bityutskiy 
272e73f4459SArtem Bityutskiy 	/*
273e73f4459SArtem Bityutskiy 	 * We should put the UBI device even in case of success, because
274e73f4459SArtem Bityutskiy 	 * 'ubi_open_volume()' took a reference as well.
275e73f4459SArtem Bityutskiy 	 */
276e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
277e73f4459SArtem Bityutskiy 	return ret;
278801c135cSArtem B. Bityutskiy }
279801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
280801c135cSArtem B. Bityutskiy 
281801c135cSArtem B. Bityutskiy /**
282b5710284SCorentin Chary  * ubi_open_volume_path - open UBI volume by its character device node path.
283b5710284SCorentin Chary  * @pathname: volume character device node path
284b5710284SCorentin Chary  * @mode: open mode
285b5710284SCorentin Chary  *
286b5710284SCorentin Chary  * This function is similar to 'ubi_open_volume()', but opens a volume the path
287b5710284SCorentin Chary  * to its character device node.
288b5710284SCorentin Chary  */
289b5710284SCorentin Chary struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
290b5710284SCorentin Chary {
29161edc3f3SRichard Weinberger 	int error, ubi_num, vol_id;
292ad022c87SRichard Weinberger 	struct path path;
29361edc3f3SRichard Weinberger 	struct kstat stat;
294b5710284SCorentin Chary 
295b5710284SCorentin Chary 	dbg_gen("open volume %s, mode %d", pathname, mode);
296b5710284SCorentin Chary 
297b5710284SCorentin Chary 	if (!pathname || !*pathname)
298b5710284SCorentin Chary 		return ERR_PTR(-EINVAL);
299b5710284SCorentin Chary 
300ad022c87SRichard Weinberger 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
301b5710284SCorentin Chary 	if (error)
302b5710284SCorentin Chary 		return ERR_PTR(error);
303b5710284SCorentin Chary 
304a528d35eSDavid Howells 	error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
305ad022c87SRichard Weinberger 	path_put(&path);
30661edc3f3SRichard Weinberger 	if (error)
30761edc3f3SRichard Weinberger 		return ERR_PTR(error);
308ad022c87SRichard Weinberger 
30961edc3f3SRichard Weinberger 	if (!S_ISCHR(stat.mode))
310b531b55aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
31161edc3f3SRichard Weinberger 
31261edc3f3SRichard Weinberger 	ubi_num = ubi_major2num(MAJOR(stat.rdev));
31361edc3f3SRichard Weinberger 	vol_id = MINOR(stat.rdev) - 1;
31461edc3f3SRichard Weinberger 
315b531b55aSArtem Bityutskiy 	if (vol_id >= 0 && ubi_num >= 0)
316b531b55aSArtem Bityutskiy 		return ubi_open_volume(ubi_num, vol_id, mode);
317b531b55aSArtem Bityutskiy 	return ERR_PTR(-ENODEV);
318b5710284SCorentin Chary }
319b5710284SCorentin Chary EXPORT_SYMBOL_GPL(ubi_open_volume_path);
320b5710284SCorentin Chary 
321b5710284SCorentin Chary /**
322801c135cSArtem B. Bityutskiy  * ubi_close_volume - close UBI volume.
323801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
324801c135cSArtem B. Bityutskiy  */
325801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc)
326801c135cSArtem B. Bityutskiy {
327801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
328e73f4459SArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
329801c135cSArtem B. Bityutskiy 
330e1cf7e6dSArtem Bityutskiy 	dbg_gen("close device %d, volume %d, mode %d",
331e1cf7e6dSArtem Bityutskiy 		ubi->ubi_num, vol->vol_id, desc->mode);
332801c135cSArtem B. Bityutskiy 
333e73f4459SArtem Bityutskiy 	spin_lock(&ubi->volumes_lock);
334801c135cSArtem B. Bityutskiy 	switch (desc->mode) {
335801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
336801c135cSArtem B. Bityutskiy 		vol->readers -= 1;
337801c135cSArtem B. Bityutskiy 		break;
338801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
339801c135cSArtem B. Bityutskiy 		vol->writers -= 1;
340801c135cSArtem B. Bityutskiy 		break;
341801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
342801c135cSArtem B. Bityutskiy 		vol->exclusive = 0;
343fafdd2bfSRichard Weinberger 		break;
344fafdd2bfSRichard Weinberger 	case UBI_METAONLY:
345fafdd2bfSRichard Weinberger 		vol->metaonly = 0;
346fafdd2bfSRichard Weinberger 		break;
347801c135cSArtem B. Bityutskiy 	}
348d05c77a8SArtem Bityutskiy 	vol->ref_count -= 1;
349e73f4459SArtem Bityutskiy 	spin_unlock(&ubi->volumes_lock);
350801c135cSArtem B. Bityutskiy 
351d05c77a8SArtem Bityutskiy 	kfree(desc);
352e73f4459SArtem Bityutskiy 	put_device(&vol->dev);
353e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
354801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
355801c135cSArtem B. Bityutskiy }
356801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume);
357801c135cSArtem B. Bityutskiy 
358801c135cSArtem B. Bityutskiy /**
3599ff08979SRichard Weinberger  * leb_read_sanity_check - does sanity checks on read requests.
3609ff08979SRichard Weinberger  * @desc: volume descriptor
3619ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
3629ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
3639ff08979SRichard Weinberger  * @len: how many bytes to read
3649ff08979SRichard Weinberger  *
3659ff08979SRichard Weinberger  * This function is used by ubi_leb_read() and ubi_leb_read_sg()
3669ff08979SRichard Weinberger  * to perform sanity checks.
3679ff08979SRichard Weinberger  */
3689ff08979SRichard Weinberger static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
3699ff08979SRichard Weinberger 				 int offset, int len)
3709ff08979SRichard Weinberger {
3719ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
3729ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
3739ff08979SRichard Weinberger 	int vol_id = vol->vol_id;
3749ff08979SRichard Weinberger 
3759ff08979SRichard Weinberger 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
3769ff08979SRichard Weinberger 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
3779ff08979SRichard Weinberger 	    offset + len > vol->usable_leb_size)
3789ff08979SRichard Weinberger 		return -EINVAL;
3799ff08979SRichard Weinberger 
3809ff08979SRichard Weinberger 	if (vol->vol_type == UBI_STATIC_VOLUME) {
3819ff08979SRichard Weinberger 		if (vol->used_ebs == 0)
3829ff08979SRichard Weinberger 			/* Empty static UBI volume */
3839ff08979SRichard Weinberger 			return 0;
3849ff08979SRichard Weinberger 		if (lnum == vol->used_ebs - 1 &&
3859ff08979SRichard Weinberger 		    offset + len > vol->last_eb_bytes)
3869ff08979SRichard Weinberger 			return -EINVAL;
3879ff08979SRichard Weinberger 	}
3889ff08979SRichard Weinberger 
3899ff08979SRichard Weinberger 	if (vol->upd_marker)
3909ff08979SRichard Weinberger 		return -EBADF;
3919ff08979SRichard Weinberger 
3929ff08979SRichard Weinberger 	return 0;
3939ff08979SRichard Weinberger }
3949ff08979SRichard Weinberger 
3959ff08979SRichard Weinberger /**
396801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
397801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
398801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
399801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
400801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
401801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
402801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
403801c135cSArtem B. Bityutskiy  *
404801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
405801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
406801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
407801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
408801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
409801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
410801c135cSArtem B. Bityutskiy  *
411801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
412801c135cSArtem B. Bityutskiy  * function returns a negative error code.
413801c135cSArtem B. Bityutskiy  *
414801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
415801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
416801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
417801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
418801c135cSArtem B. Bityutskiy  *
419801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
420801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
421801c135cSArtem B. Bityutskiy  */
422801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
423801c135cSArtem B. Bityutskiy 		 int len, int check)
424801c135cSArtem B. Bityutskiy {
425801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
426801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
427801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
428801c135cSArtem B. Bityutskiy 
429c8566350SArtem Bityutskiy 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
430801c135cSArtem B. Bityutskiy 
4319ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4329ff08979SRichard Weinberger 	if (err < 0)
4339ff08979SRichard Weinberger 		return err;
434801c135cSArtem B. Bityutskiy 
435801c135cSArtem B. Bityutskiy 	if (len == 0)
436801c135cSArtem B. Bityutskiy 		return 0;
437801c135cSArtem B. Bityutskiy 
43889b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
439d57f4054SBrian Norris 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
44032608703STanya Brokhman 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
441801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
442801c135cSArtem B. Bityutskiy 	}
443801c135cSArtem B. Bityutskiy 
444801c135cSArtem B. Bityutskiy 	return err;
445801c135cSArtem B. Bityutskiy }
446801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
447801c135cSArtem B. Bityutskiy 
4489ff08979SRichard Weinberger 
4499ff08979SRichard Weinberger /**
4509ff08979SRichard Weinberger  * ubi_leb_read_sg - read data into a scatter gather list.
4519ff08979SRichard Weinberger  * @desc: volume descriptor
4529ff08979SRichard Weinberger  * @lnum: logical eraseblock number to read from
453*b6baa996SLee Jones  * @sgl: UBI scatter gather list to store the read data
4549ff08979SRichard Weinberger  * @offset: offset within the logical eraseblock to read from
4559ff08979SRichard Weinberger  * @len: how many bytes to read
4569ff08979SRichard Weinberger  * @check: whether UBI has to check the read data's CRC or not.
4579ff08979SRichard Weinberger  *
4589ff08979SRichard Weinberger  * This function works exactly like ubi_leb_read_sg(). But instead of
4599ff08979SRichard Weinberger  * storing the read data into a buffer it writes to an UBI scatter gather
4609ff08979SRichard Weinberger  * list.
4619ff08979SRichard Weinberger  */
4629ff08979SRichard Weinberger int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
4639ff08979SRichard Weinberger 		    int offset, int len, int check)
4649ff08979SRichard Weinberger {
4659ff08979SRichard Weinberger 	struct ubi_volume *vol = desc->vol;
4669ff08979SRichard Weinberger 	struct ubi_device *ubi = vol->ubi;
4679ff08979SRichard Weinberger 	int err, vol_id = vol->vol_id;
4689ff08979SRichard Weinberger 
4699ff08979SRichard Weinberger 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
4709ff08979SRichard Weinberger 
4719ff08979SRichard Weinberger 	err = leb_read_sanity_check(desc, lnum, offset, len);
4729ff08979SRichard Weinberger 	if (err < 0)
4739ff08979SRichard Weinberger 		return err;
4749ff08979SRichard Weinberger 
4759ff08979SRichard Weinberger 	if (len == 0)
4769ff08979SRichard Weinberger 		return 0;
4779ff08979SRichard Weinberger 
4789ff08979SRichard Weinberger 	err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
4799ff08979SRichard Weinberger 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
4809ff08979SRichard Weinberger 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
4819ff08979SRichard Weinberger 		vol->corrupted = 1;
4829ff08979SRichard Weinberger 	}
4839ff08979SRichard Weinberger 
4849ff08979SRichard Weinberger 	return err;
4859ff08979SRichard Weinberger }
4869ff08979SRichard Weinberger EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
4879ff08979SRichard Weinberger 
488801c135cSArtem B. Bityutskiy /**
489801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
490801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
491801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
492801c135cSArtem B. Bityutskiy  * @buf: data to write
493801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
494801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
495801c135cSArtem B. Bityutskiy  *
496801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
497b36a261eSRichard Weinberger  * logical eraseblock @lnum.
498801c135cSArtem B. Bityutskiy  *
499801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
500801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
501801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
502801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
503801c135cSArtem B. Bityutskiy  *
504801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
505801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
506801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
507801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
508801c135cSArtem B. Bityutskiy  * garbage.
509801c135cSArtem B. Bityutskiy  *
510801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
511801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
512801c135cSArtem B. Bityutskiy  */
513801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
514b36a261eSRichard Weinberger 		  int offset, int len)
515801c135cSArtem B. Bityutskiy {
516801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
517801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
518801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
519801c135cSArtem B. Bityutskiy 
520c8566350SArtem Bityutskiy 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
521801c135cSArtem B. Bityutskiy 
522801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
523801c135cSArtem B. Bityutskiy 		return -EINVAL;
524801c135cSArtem B. Bityutskiy 
525801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
526801c135cSArtem B. Bityutskiy 		return -EROFS;
527801c135cSArtem B. Bityutskiy 
5289a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
529cadb40ccSKyungmin Park 	    offset + len > vol->usable_leb_size ||
530cadb40ccSKyungmin Park 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
531801c135cSArtem B. Bityutskiy 		return -EINVAL;
532801c135cSArtem B. Bityutskiy 
533801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
534801c135cSArtem B. Bityutskiy 		return -EBADF;
535801c135cSArtem B. Bityutskiy 
536801c135cSArtem B. Bityutskiy 	if (len == 0)
537801c135cSArtem B. Bityutskiy 		return 0;
538801c135cSArtem B. Bityutskiy 
539b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
540801c135cSArtem B. Bityutskiy }
541801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
542801c135cSArtem B. Bityutskiy 
543801c135cSArtem B. Bityutskiy /*
544801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
545801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
546801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
547801c135cSArtem B. Bityutskiy  * @buf: data to write
548801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
549801c135cSArtem B. Bityutskiy  *
550801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
551801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
5523f502622SShinya Kuribayashi  * data, which has to be aligned. The length may be shorter than the logical
553801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
554801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
555801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
556801c135cSArtem B. Bityutskiy  * code in case of failure.
557801c135cSArtem B. Bityutskiy  */
558801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
559b36a261eSRichard Weinberger 		   int len)
560801c135cSArtem B. Bityutskiy {
561801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
562801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
563801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
564801c135cSArtem B. Bityutskiy 
565c8566350SArtem Bityutskiy 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
566801c135cSArtem B. Bityutskiy 
567801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
568801c135cSArtem B. Bityutskiy 		return -EINVAL;
569801c135cSArtem B. Bityutskiy 
570801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
571801c135cSArtem B. Bityutskiy 		return -EROFS;
572801c135cSArtem B. Bityutskiy 
5739a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum) || len < 0 ||
574cadb40ccSKyungmin Park 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
575801c135cSArtem B. Bityutskiy 		return -EINVAL;
576801c135cSArtem B. Bityutskiy 
577801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
578801c135cSArtem B. Bityutskiy 		return -EBADF;
579801c135cSArtem B. Bityutskiy 
580801c135cSArtem B. Bityutskiy 	if (len == 0)
581801c135cSArtem B. Bityutskiy 		return 0;
582801c135cSArtem B. Bityutskiy 
583b36a261eSRichard Weinberger 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
584801c135cSArtem B. Bityutskiy }
585801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
586801c135cSArtem B. Bityutskiy 
587801c135cSArtem B. Bityutskiy /**
588801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
589801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
590801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
591801c135cSArtem B. Bityutskiy  *
592801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
593801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
594801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
595801c135cSArtem B. Bityutskiy  *
596801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
597801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
598801c135cSArtem B. Bityutskiy  */
599801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
600801c135cSArtem B. Bityutskiy {
601801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
602801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
603ae616e1bSArtem Bityutskiy 	int err;
604801c135cSArtem B. Bityutskiy 
605c8566350SArtem Bityutskiy 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
606801c135cSArtem B. Bityutskiy 
607801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
608801c135cSArtem B. Bityutskiy 		return -EROFS;
609801c135cSArtem B. Bityutskiy 
6109a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
611801c135cSArtem B. Bityutskiy 		return -EINVAL;
612801c135cSArtem B. Bityutskiy 
613801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
614801c135cSArtem B. Bityutskiy 		return -EBADF;
615801c135cSArtem B. Bityutskiy 
61689b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
617801c135cSArtem B. Bityutskiy 	if (err)
618801c135cSArtem B. Bityutskiy 		return err;
619801c135cSArtem B. Bityutskiy 
62062f38455SJoel Reardon 	return ubi_wl_flush(ubi, vol->vol_id, lnum);
621801c135cSArtem B. Bityutskiy }
622801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
623801c135cSArtem B. Bityutskiy 
624801c135cSArtem B. Bityutskiy /**
625801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
626801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
627801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
628801c135cSArtem B. Bityutskiy  *
629801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
630801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
6313f502622SShinya Kuribayashi  * physically erased in background. This operation is much faster than the
632801c135cSArtem B. Bityutskiy  * erase operation.
633801c135cSArtem B. Bityutskiy  *
634801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
635801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
636801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
637801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
638801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
639801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
640801c135cSArtem B. Bityutskiy  * used with care.
641801c135cSArtem B. Bityutskiy  *
642801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
643801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
644801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
645801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
646801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
647801c135cSArtem B. Bityutskiy  *
648801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
649801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
6503f502622SShinya Kuribayashi  * first un-map it, then write new data, rather than first erase it, then write
651801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
652801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
653801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
654801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
655801c135cSArtem B. Bityutskiy  *
656801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
657801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
658801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
659801c135cSArtem B. Bityutskiy  */
660801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
661801c135cSArtem B. Bityutskiy {
662801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
663801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
664801c135cSArtem B. Bityutskiy 
665c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
666801c135cSArtem B. Bityutskiy 
667801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
668801c135cSArtem B. Bityutskiy 		return -EROFS;
669801c135cSArtem B. Bityutskiy 
6709a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
671801c135cSArtem B. Bityutskiy 		return -EINVAL;
672801c135cSArtem B. Bityutskiy 
673801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
674801c135cSArtem B. Bityutskiy 		return -EBADF;
675801c135cSArtem B. Bityutskiy 
67689b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
677801c135cSArtem B. Bityutskiy }
678801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
679801c135cSArtem B. Bityutskiy 
680801c135cSArtem B. Bityutskiy /**
6810e0ee1ccSDmitry Pervushin  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
682393852ecSArtem Bityutskiy  * @desc: volume descriptor
683393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
684393852ecSArtem Bityutskiy  *
685393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
68673ac36eaSColy Li  * eraseblock. This means, that after a successful invocation of this
687393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
688393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
689393852ecSArtem Bityutskiy  * happens.
690393852ecSArtem Bityutskiy  *
691393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
692393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
693393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
694393852ecSArtem Bityutskiy  * other failures.
695393852ecSArtem Bityutskiy  */
696b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
697393852ecSArtem Bityutskiy {
698393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
699393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
700393852ecSArtem Bityutskiy 
701960b35d0Sz00189512 	dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
702393852ecSArtem Bityutskiy 
703393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
704393852ecSArtem Bityutskiy 		return -EROFS;
705393852ecSArtem Bityutskiy 
7069a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
707393852ecSArtem Bityutskiy 		return -EINVAL;
708393852ecSArtem Bityutskiy 
709393852ecSArtem Bityutskiy 	if (vol->upd_marker)
710393852ecSArtem Bityutskiy 		return -EBADF;
711393852ecSArtem Bityutskiy 
71275547696SBoris Brezillon 	if (ubi_eba_is_mapped(vol, lnum))
713393852ecSArtem Bityutskiy 		return -EBADMSG;
714393852ecSArtem Bityutskiy 
715b36a261eSRichard Weinberger 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
716393852ecSArtem Bityutskiy }
717393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
718393852ecSArtem Bityutskiy 
719393852ecSArtem Bityutskiy /**
720801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
721801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
722801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
723801c135cSArtem B. Bityutskiy  *
724801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
725801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
726801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
727801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
728801c135cSArtem B. Bityutskiy  * mapped to.
729801c135cSArtem B. Bityutskiy  *
730801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
731801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
732801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
733801c135cSArtem B. Bityutskiy  * code.
734801c135cSArtem B. Bityutskiy  */
735801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
736801c135cSArtem B. Bityutskiy {
737801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
738801c135cSArtem B. Bityutskiy 
739c8566350SArtem Bityutskiy 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
740801c135cSArtem B. Bityutskiy 
7419a5f09acSBoris Brezillon 	if (!ubi_leb_valid(vol, lnum))
742801c135cSArtem B. Bityutskiy 		return -EINVAL;
743801c135cSArtem B. Bityutskiy 
744801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
745801c135cSArtem B. Bityutskiy 		return -EBADF;
746801c135cSArtem B. Bityutskiy 
74775547696SBoris Brezillon 	return ubi_eba_is_mapped(vol, lnum);
748801c135cSArtem B. Bityutskiy }
749801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
750a5bf6190SArtem Bityutskiy 
751a5bf6190SArtem Bityutskiy /**
752a5bf6190SArtem Bityutskiy  * ubi_sync - synchronize UBI device buffers.
753a5bf6190SArtem Bityutskiy  * @ubi_num: UBI device to synchronize
754a5bf6190SArtem Bityutskiy  *
755a5bf6190SArtem Bityutskiy  * The underlying MTD device may cache data in hardware or in software. This
756a5bf6190SArtem Bityutskiy  * function ensures the caches are flushed. Returns zero in case of success and
757a5bf6190SArtem Bityutskiy  * a negative error code in case of failure.
758a5bf6190SArtem Bityutskiy  */
759a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num)
760a5bf6190SArtem Bityutskiy {
761a5bf6190SArtem Bityutskiy 	struct ubi_device *ubi;
762a5bf6190SArtem Bityutskiy 
763a5bf6190SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
764a5bf6190SArtem Bityutskiy 	if (!ubi)
765a5bf6190SArtem Bityutskiy 		return -ENODEV;
766a5bf6190SArtem Bityutskiy 
76785f2f2a8SArtem Bityutskiy 	mtd_sync(ubi->mtd);
768a5bf6190SArtem Bityutskiy 	ubi_put_device(ubi);
769a5bf6190SArtem Bityutskiy 	return 0;
770a5bf6190SArtem Bityutskiy }
771a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync);
7720e0ee1ccSDmitry Pervushin 
77362f38455SJoel Reardon /**
77462f38455SJoel Reardon  * ubi_flush - flush UBI work queue.
77562f38455SJoel Reardon  * @ubi_num: UBI device to flush work queue
77662f38455SJoel Reardon  * @vol_id: volume id to flush for
77762f38455SJoel Reardon  * @lnum: logical eraseblock number to flush for
77862f38455SJoel Reardon  *
77962f38455SJoel Reardon  * This function executes all pending works for a particular volume id / logical
78062f38455SJoel Reardon  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
78162f38455SJoel Reardon  * a wildcard for all of the corresponding volume numbers or logical
78262f38455SJoel Reardon  * eraseblock numbers. It returns zero in case of success and a negative error
78362f38455SJoel Reardon  * code in case of failure.
78462f38455SJoel Reardon  */
78562f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum)
78662f38455SJoel Reardon {
78762f38455SJoel Reardon 	struct ubi_device *ubi;
78862f38455SJoel Reardon 	int err = 0;
78962f38455SJoel Reardon 
79062f38455SJoel Reardon 	ubi = ubi_get_device(ubi_num);
79162f38455SJoel Reardon 	if (!ubi)
79262f38455SJoel Reardon 		return -ENODEV;
79362f38455SJoel Reardon 
79462f38455SJoel Reardon 	err = ubi_wl_flush(ubi, vol_id, lnum);
79562f38455SJoel Reardon 	ubi_put_device(ubi);
79662f38455SJoel Reardon 	return err;
79762f38455SJoel Reardon }
79862f38455SJoel Reardon EXPORT_SYMBOL_GPL(ubi_flush);
79962f38455SJoel Reardon 
8000e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
8010e0ee1ccSDmitry Pervushin 
8020e0ee1ccSDmitry Pervushin /**
8030e0ee1ccSDmitry Pervushin  * ubi_register_volume_notifier - register a volume notifier.
8040e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8050e0ee1ccSDmitry Pervushin  * @ignore_existing: if non-zero, do not send "added" notification for all
8060e0ee1ccSDmitry Pervushin  *                   already existing volumes
8070e0ee1ccSDmitry Pervushin  *
8080e0ee1ccSDmitry Pervushin  * This function registers a volume notifier, which means that
8090e0ee1ccSDmitry Pervushin  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
8100e0ee1ccSDmitry Pervushin  * removed, re-sized, re-named, or updated. The first argument of the function
8110e0ee1ccSDmitry Pervushin  * is the notification type. The second argument is pointer to a
8120e0ee1ccSDmitry Pervushin  * &struct ubi_notification object which describes the notification event.
8130e0ee1ccSDmitry Pervushin  * Using UBI API from the volume notifier is prohibited.
8140e0ee1ccSDmitry Pervushin  *
8150e0ee1ccSDmitry Pervushin  * This function returns zero in case of success and a negative error code
8160e0ee1ccSDmitry Pervushin  * in case of failure.
8170e0ee1ccSDmitry Pervushin  */
8180e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
8190e0ee1ccSDmitry Pervushin 				 int ignore_existing)
8200e0ee1ccSDmitry Pervushin {
8210e0ee1ccSDmitry Pervushin 	int err;
8220e0ee1ccSDmitry Pervushin 
8230e0ee1ccSDmitry Pervushin 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
8240e0ee1ccSDmitry Pervushin 	if (err != 0)
8250e0ee1ccSDmitry Pervushin 		return err;
8260e0ee1ccSDmitry Pervushin 	if (ignore_existing)
8270e0ee1ccSDmitry Pervushin 		return 0;
8280e0ee1ccSDmitry Pervushin 
8290e0ee1ccSDmitry Pervushin 	/*
8300e0ee1ccSDmitry Pervushin 	 * We are going to walk all UBI devices and all volumes, and
8310e0ee1ccSDmitry Pervushin 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
8320e0ee1ccSDmitry Pervushin 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
8330e0ee1ccSDmitry Pervushin 	 * devices do not disappear.
8340e0ee1ccSDmitry Pervushin 	 */
8350e0ee1ccSDmitry Pervushin 	mutex_lock(&ubi_devices_mutex);
8360e0ee1ccSDmitry Pervushin 	ubi_enumerate_volumes(nb);
8370e0ee1ccSDmitry Pervushin 	mutex_unlock(&ubi_devices_mutex);
8380e0ee1ccSDmitry Pervushin 
8390e0ee1ccSDmitry Pervushin 	return err;
8400e0ee1ccSDmitry Pervushin }
8410e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
8420e0ee1ccSDmitry Pervushin 
8430e0ee1ccSDmitry Pervushin /**
8440e0ee1ccSDmitry Pervushin  * ubi_unregister_volume_notifier - unregister the volume notifier.
8450e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
8460e0ee1ccSDmitry Pervushin  *
8470e0ee1ccSDmitry Pervushin  * This function unregisters volume notifier @nm and returns zero in case of
8480e0ee1ccSDmitry Pervushin  * success and a negative error code in case of failure.
8490e0ee1ccSDmitry Pervushin  */
8500e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb)
8510e0ee1ccSDmitry Pervushin {
8520e0ee1ccSDmitry Pervushin 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
8530e0ee1ccSDmitry Pervushin }
8540e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
855