xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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>
25*5a0e3ad6STejun 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;
430e0ee1ccSDmitry Pervushin 	di->min_io_size = ubi->min_io_size;
440e0ee1ccSDmitry Pervushin 	di->ro_mode = ubi->ro_mode;
450e0ee1ccSDmitry Pervushin 	di->cdev = ubi->cdev.dev;
460e0ee1ccSDmitry Pervushin }
470e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
480e0ee1ccSDmitry Pervushin 
490e0ee1ccSDmitry Pervushin /**
50801c135cSArtem B. Bityutskiy  * ubi_get_device_info - get information about UBI device.
51801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
52801c135cSArtem B. Bityutskiy  * @di: the information is stored here
53801c135cSArtem B. Bityutskiy  *
54e73f4459SArtem Bityutskiy  * This function returns %0 in case of success, %-EINVAL if the UBI device
55e73f4459SArtem Bityutskiy  * number is invalid, and %-ENODEV if there is no such UBI device.
56801c135cSArtem B. Bityutskiy  */
57801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
58801c135cSArtem B. Bityutskiy {
59e73f4459SArtem Bityutskiy 	struct ubi_device *ubi;
60801c135cSArtem B. Bityutskiy 
61e73f4459SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
62e73f4459SArtem Bityutskiy 		return -EINVAL;
63e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
64e73f4459SArtem Bityutskiy 	if (!ubi)
65801c135cSArtem B. Bityutskiy 		return -ENODEV;
660e0ee1ccSDmitry Pervushin 	ubi_do_get_device_info(ubi, di);
67e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
68801c135cSArtem B. Bityutskiy 	return 0;
69801c135cSArtem B. Bityutskiy }
70801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info);
71801c135cSArtem B. Bityutskiy 
72801c135cSArtem B. Bityutskiy /**
730e0ee1ccSDmitry Pervushin  * ubi_do_get_volume_info - get information about UBI volume.
740e0ee1ccSDmitry Pervushin  * @ubi: UBI device description object
750e0ee1ccSDmitry Pervushin  * @vol: volume description object
76801c135cSArtem B. Bityutskiy  * @vi: the information is stored here
77801c135cSArtem B. Bityutskiy  */
780e0ee1ccSDmitry Pervushin void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
79801c135cSArtem B. Bityutskiy 			    struct ubi_volume_info *vi)
80801c135cSArtem B. Bityutskiy {
81801c135cSArtem B. Bityutskiy 	vi->vol_id = vol->vol_id;
82801c135cSArtem B. Bityutskiy 	vi->ubi_num = ubi->ubi_num;
83801c135cSArtem B. Bityutskiy 	vi->size = vol->reserved_pebs;
84801c135cSArtem B. Bityutskiy 	vi->used_bytes = vol->used_bytes;
85801c135cSArtem B. Bityutskiy 	vi->vol_type = vol->vol_type;
86801c135cSArtem B. Bityutskiy 	vi->corrupted = vol->corrupted;
87801c135cSArtem B. Bityutskiy 	vi->upd_marker = vol->upd_marker;
88801c135cSArtem B. Bityutskiy 	vi->alignment = vol->alignment;
89801c135cSArtem B. Bityutskiy 	vi->usable_leb_size = vol->usable_leb_size;
90801c135cSArtem B. Bityutskiy 	vi->name_len = vol->name_len;
91801c135cSArtem B. Bityutskiy 	vi->name = vol->name;
9249dfc299SArtem Bityutskiy 	vi->cdev = vol->cdev.dev;
93801c135cSArtem B. Bityutskiy }
940e0ee1ccSDmitry Pervushin 
950e0ee1ccSDmitry Pervushin /**
960e0ee1ccSDmitry Pervushin  * ubi_get_volume_info - get information about UBI volume.
970e0ee1ccSDmitry Pervushin  * @desc: volume descriptor
980e0ee1ccSDmitry Pervushin  * @vi: the information is stored here
990e0ee1ccSDmitry Pervushin  */
1000e0ee1ccSDmitry Pervushin void ubi_get_volume_info(struct ubi_volume_desc *desc,
1010e0ee1ccSDmitry Pervushin 			 struct ubi_volume_info *vi)
1020e0ee1ccSDmitry Pervushin {
1030e0ee1ccSDmitry Pervushin 	ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
1040e0ee1ccSDmitry Pervushin }
105801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info);
106801c135cSArtem B. Bityutskiy 
107801c135cSArtem B. Bityutskiy /**
108801c135cSArtem B. Bityutskiy  * ubi_open_volume - open UBI volume.
109801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
110801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
111801c135cSArtem B. Bityutskiy  * @mode: open mode
112801c135cSArtem B. Bityutskiy  *
113801c135cSArtem B. Bityutskiy  * The @mode parameter specifies if the volume should be opened in read-only
114801c135cSArtem B. Bityutskiy  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
115801c135cSArtem B. Bityutskiy  * nobody else will be able to open this volume. UBI allows to have many volume
116801c135cSArtem B. Bityutskiy  * readers and one writer at a time.
117801c135cSArtem B. Bityutskiy  *
118801c135cSArtem B. Bityutskiy  * If a static volume is being opened for the first time since boot, it will be
119801c135cSArtem B. Bityutskiy  * checked by this function, which means it will be fully read and the CRC
120801c135cSArtem B. Bityutskiy  * checksum of each logical eraseblock will be checked.
121801c135cSArtem B. Bityutskiy  *
122801c135cSArtem B. Bityutskiy  * This function returns volume descriptor in case of success and a negative
123801c135cSArtem B. Bityutskiy  * error code in case of failure.
124801c135cSArtem B. Bityutskiy  */
125801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
126801c135cSArtem B. Bityutskiy {
127801c135cSArtem B. Bityutskiy 	int err;
128801c135cSArtem B. Bityutskiy 	struct ubi_volume_desc *desc;
1290169b49dSJesper Juhl 	struct ubi_device *ubi;
130801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol;
131801c135cSArtem B. Bityutskiy 
132e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
133801c135cSArtem B. Bityutskiy 
13435ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
13535ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
1360169b49dSJesper Juhl 
137801c135cSArtem B. Bityutskiy 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
138801c135cSArtem B. Bityutskiy 	    mode != UBI_EXCLUSIVE)
13935ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
14035ad5fb7SArtem Bityutskiy 
141e73f4459SArtem Bityutskiy 	/*
142e73f4459SArtem Bityutskiy 	 * First of all, we have to get the UBI device to prevent its removal.
143e73f4459SArtem Bityutskiy 	 */
144e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
14535ad5fb7SArtem Bityutskiy 	if (!ubi)
14635ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
14735ad5fb7SArtem Bityutskiy 
148e73f4459SArtem Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
149e73f4459SArtem Bityutskiy 		err = -EINVAL;
150e73f4459SArtem Bityutskiy 		goto out_put_ubi;
151e73f4459SArtem Bityutskiy 	}
152801c135cSArtem B. Bityutskiy 
153801c135cSArtem B. Bityutskiy 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
154e73f4459SArtem Bityutskiy 	if (!desc) {
155e73f4459SArtem Bityutskiy 		err = -ENOMEM;
156e73f4459SArtem Bityutskiy 		goto out_put_ubi;
157e73f4459SArtem Bityutskiy 	}
15835ad5fb7SArtem Bityutskiy 
15935ad5fb7SArtem Bityutskiy 	err = -ENODEV;
16035ad5fb7SArtem Bityutskiy 	if (!try_module_get(THIS_MODULE))
16135ad5fb7SArtem Bityutskiy 		goto out_free;
162801c135cSArtem B. Bityutskiy 
163801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
164801c135cSArtem B. Bityutskiy 	vol = ubi->volumes[vol_id];
16535ad5fb7SArtem Bityutskiy 	if (!vol)
166801c135cSArtem B. Bityutskiy 		goto out_unlock;
167801c135cSArtem B. Bityutskiy 
168801c135cSArtem B. Bityutskiy 	err = -EBUSY;
169801c135cSArtem B. Bityutskiy 	switch (mode) {
170801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
171801c135cSArtem B. Bityutskiy 		if (vol->exclusive)
172801c135cSArtem B. Bityutskiy 			goto out_unlock;
173801c135cSArtem B. Bityutskiy 		vol->readers += 1;
174801c135cSArtem B. Bityutskiy 		break;
175801c135cSArtem B. Bityutskiy 
176801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
177801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers > 0)
178801c135cSArtem B. Bityutskiy 			goto out_unlock;
179801c135cSArtem B. Bityutskiy 		vol->writers += 1;
180801c135cSArtem B. Bityutskiy 		break;
181801c135cSArtem B. Bityutskiy 
182801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
183801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers || vol->readers)
184801c135cSArtem B. Bityutskiy 			goto out_unlock;
185801c135cSArtem B. Bityutskiy 		vol->exclusive = 1;
186801c135cSArtem B. Bityutskiy 		break;
187801c135cSArtem B. Bityutskiy 	}
188450f872aSArtem Bityutskiy 	get_device(&vol->dev);
189d05c77a8SArtem Bityutskiy 	vol->ref_count += 1;
190801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
191801c135cSArtem B. Bityutskiy 
192801c135cSArtem B. Bityutskiy 	desc->vol = vol;
193801c135cSArtem B. Bityutskiy 	desc->mode = mode;
194801c135cSArtem B. Bityutskiy 
195783b273aSArtem Bityutskiy 	mutex_lock(&ubi->ckvol_mutex);
196801c135cSArtem B. Bityutskiy 	if (!vol->checked) {
197801c135cSArtem B. Bityutskiy 		/* This is the first open - check the volume */
198801c135cSArtem B. Bityutskiy 		err = ubi_check_volume(ubi, vol_id);
199801c135cSArtem B. Bityutskiy 		if (err < 0) {
200783b273aSArtem Bityutskiy 			mutex_unlock(&ubi->ckvol_mutex);
201801c135cSArtem B. Bityutskiy 			ubi_close_volume(desc);
202801c135cSArtem B. Bityutskiy 			return ERR_PTR(err);
203801c135cSArtem B. Bityutskiy 		}
204801c135cSArtem B. Bityutskiy 		if (err == 1) {
205801c135cSArtem B. Bityutskiy 			ubi_warn("volume %d on UBI device %d is corrupted",
206801c135cSArtem B. Bityutskiy 				 vol_id, ubi->ubi_num);
207801c135cSArtem B. Bityutskiy 			vol->corrupted = 1;
208801c135cSArtem B. Bityutskiy 		}
209801c135cSArtem B. Bityutskiy 		vol->checked = 1;
210801c135cSArtem B. Bityutskiy 	}
211783b273aSArtem Bityutskiy 	mutex_unlock(&ubi->ckvol_mutex);
21235ad5fb7SArtem Bityutskiy 
213801c135cSArtem B. Bityutskiy 	return desc;
214801c135cSArtem B. Bityutskiy 
215801c135cSArtem B. Bityutskiy out_unlock:
216801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
217801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
21835ad5fb7SArtem Bityutskiy out_free:
21935ad5fb7SArtem Bityutskiy 	kfree(desc);
220e73f4459SArtem Bityutskiy out_put_ubi:
221e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
222e1cf7e6dSArtem Bityutskiy 	dbg_err("cannot open device %d, volume %d, error %d",
223e1cf7e6dSArtem Bityutskiy 		ubi_num, vol_id, err);
224801c135cSArtem B. Bityutskiy 	return ERR_PTR(err);
225801c135cSArtem B. Bityutskiy }
226801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume);
227801c135cSArtem B. Bityutskiy 
228801c135cSArtem B. Bityutskiy /**
229801c135cSArtem B. Bityutskiy  * ubi_open_volume_nm - open UBI volume by name.
230801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
231801c135cSArtem B. Bityutskiy  * @name: volume name
232801c135cSArtem B. Bityutskiy  * @mode: open mode
233801c135cSArtem B. Bityutskiy  *
234801c135cSArtem B. Bityutskiy  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
235801c135cSArtem B. Bityutskiy  */
236801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
237801c135cSArtem B. Bityutskiy 					   int mode)
238801c135cSArtem B. Bityutskiy {
239801c135cSArtem B. Bityutskiy 	int i, vol_id = -1, len;
240801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi;
241e73f4459SArtem Bityutskiy 	struct ubi_volume_desc *ret;
242801c135cSArtem B. Bityutskiy 
243e1cf7e6dSArtem Bityutskiy 	dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
244801c135cSArtem B. Bityutskiy 
245801c135cSArtem B. Bityutskiy 	if (!name)
246801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
247801c135cSArtem B. Bityutskiy 
248801c135cSArtem B. Bityutskiy 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
249801c135cSArtem B. Bityutskiy 	if (len > UBI_VOL_NAME_MAX)
250801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
251801c135cSArtem B. Bityutskiy 
25235ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
25335ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
254801c135cSArtem B. Bityutskiy 
255e73f4459SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
25635ad5fb7SArtem Bityutskiy 	if (!ubi)
25735ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
258801c135cSArtem B. Bityutskiy 
259801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
260801c135cSArtem B. Bityutskiy 	/* Walk all volumes of this UBI device */
261801c135cSArtem B. Bityutskiy 	for (i = 0; i < ubi->vtbl_slots; i++) {
262801c135cSArtem B. Bityutskiy 		struct ubi_volume *vol = ubi->volumes[i];
263801c135cSArtem B. Bityutskiy 
264801c135cSArtem B. Bityutskiy 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
265801c135cSArtem B. Bityutskiy 			vol_id = i;
266801c135cSArtem B. Bityutskiy 			break;
267801c135cSArtem B. Bityutskiy 		}
268801c135cSArtem B. Bityutskiy 	}
269801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
270801c135cSArtem B. Bityutskiy 
271e73f4459SArtem Bityutskiy 	if (vol_id >= 0)
272e73f4459SArtem Bityutskiy 		ret = ubi_open_volume(ubi_num, vol_id, mode);
273e73f4459SArtem Bityutskiy 	else
274e73f4459SArtem Bityutskiy 		ret = ERR_PTR(-ENODEV);
275801c135cSArtem B. Bityutskiy 
276e73f4459SArtem Bityutskiy 	/*
277e73f4459SArtem Bityutskiy 	 * We should put the UBI device even in case of success, because
278e73f4459SArtem Bityutskiy 	 * 'ubi_open_volume()' took a reference as well.
279e73f4459SArtem Bityutskiy 	 */
280e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
281e73f4459SArtem Bityutskiy 	return ret;
282801c135cSArtem B. Bityutskiy }
283801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
284801c135cSArtem B. Bityutskiy 
285801c135cSArtem B. Bityutskiy /**
286b5710284SCorentin Chary  * ubi_open_volume_path - open UBI volume by its character device node path.
287b5710284SCorentin Chary  * @pathname: volume character device node path
288b5710284SCorentin Chary  * @mode: open mode
289b5710284SCorentin Chary  *
290b5710284SCorentin Chary  * This function is similar to 'ubi_open_volume()', but opens a volume the path
291b5710284SCorentin Chary  * to its character device node.
292b5710284SCorentin Chary  */
293b5710284SCorentin Chary struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
294b5710284SCorentin Chary {
295b531b55aSArtem Bityutskiy 	int error, ubi_num, vol_id, mod;
296b5710284SCorentin Chary 	struct inode *inode;
297b5710284SCorentin Chary 	struct path path;
298b5710284SCorentin Chary 
299b5710284SCorentin Chary 	dbg_gen("open volume %s, mode %d", pathname, mode);
300b5710284SCorentin Chary 
301b5710284SCorentin Chary 	if (!pathname || !*pathname)
302b5710284SCorentin Chary 		return ERR_PTR(-EINVAL);
303b5710284SCorentin Chary 
304b5710284SCorentin Chary 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
305b5710284SCorentin Chary 	if (error)
306b5710284SCorentin Chary 		return ERR_PTR(error);
307b5710284SCorentin Chary 
308b5710284SCorentin Chary 	inode = path.dentry->d_inode;
309b531b55aSArtem Bityutskiy 	mod = inode->i_mode;
310b5710284SCorentin Chary 	ubi_num = ubi_major2num(imajor(inode));
311b5710284SCorentin Chary 	vol_id = iminor(inode) - 1;
312b5710284SCorentin Chary 	path_put(&path);
313b531b55aSArtem Bityutskiy 
314b531b55aSArtem Bityutskiy 	if (!S_ISCHR(mod))
315b531b55aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
316b531b55aSArtem Bityutskiy 	if (vol_id >= 0 && ubi_num >= 0)
317b531b55aSArtem Bityutskiy 		return ubi_open_volume(ubi_num, vol_id, mode);
318b531b55aSArtem Bityutskiy 	return ERR_PTR(-ENODEV);
319b5710284SCorentin Chary }
320b5710284SCorentin Chary EXPORT_SYMBOL_GPL(ubi_open_volume_path);
321b5710284SCorentin Chary 
322b5710284SCorentin Chary /**
323801c135cSArtem B. Bityutskiy  * ubi_close_volume - close UBI volume.
324801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
325801c135cSArtem B. Bityutskiy  */
326801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc)
327801c135cSArtem B. Bityutskiy {
328801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
329e73f4459SArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
330801c135cSArtem B. Bityutskiy 
331e1cf7e6dSArtem Bityutskiy 	dbg_gen("close device %d, volume %d, mode %d",
332e1cf7e6dSArtem Bityutskiy 		ubi->ubi_num, vol->vol_id, desc->mode);
333801c135cSArtem B. Bityutskiy 
334e73f4459SArtem Bityutskiy 	spin_lock(&ubi->volumes_lock);
335801c135cSArtem B. Bityutskiy 	switch (desc->mode) {
336801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
337801c135cSArtem B. Bityutskiy 		vol->readers -= 1;
338801c135cSArtem B. Bityutskiy 		break;
339801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
340801c135cSArtem B. Bityutskiy 		vol->writers -= 1;
341801c135cSArtem B. Bityutskiy 		break;
342801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
343801c135cSArtem B. Bityutskiy 		vol->exclusive = 0;
344801c135cSArtem B. Bityutskiy 	}
345d05c77a8SArtem Bityutskiy 	vol->ref_count -= 1;
346e73f4459SArtem Bityutskiy 	spin_unlock(&ubi->volumes_lock);
347801c135cSArtem B. Bityutskiy 
348d05c77a8SArtem Bityutskiy 	kfree(desc);
349e73f4459SArtem Bityutskiy 	put_device(&vol->dev);
350e73f4459SArtem Bityutskiy 	ubi_put_device(ubi);
351801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
352801c135cSArtem B. Bityutskiy }
353801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume);
354801c135cSArtem B. Bityutskiy 
355801c135cSArtem B. Bityutskiy /**
356801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
357801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
358801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
359801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
360801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
361801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
362801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
363801c135cSArtem B. Bityutskiy  *
364801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
365801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
366801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
367801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
368801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
369801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
370801c135cSArtem B. Bityutskiy  *
371801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
372801c135cSArtem B. Bityutskiy  * function returns a negative error code.
373801c135cSArtem B. Bityutskiy  *
374801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
375801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
376801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
377801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
378801c135cSArtem B. Bityutskiy  *
379801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
380801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
381801c135cSArtem B. Bityutskiy  */
382801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
383801c135cSArtem B. Bityutskiy 		 int len, int check)
384801c135cSArtem B. Bityutskiy {
385801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
386801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
387801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
388801c135cSArtem B. Bityutskiy 
389c8566350SArtem Bityutskiy 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
390801c135cSArtem B. Bityutskiy 
391801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
392801c135cSArtem B. Bityutskiy 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
393801c135cSArtem B. Bityutskiy 	    offset + len > vol->usable_leb_size)
394801c135cSArtem B. Bityutskiy 		return -EINVAL;
395801c135cSArtem B. Bityutskiy 
3964ab60a0dSArtem Bityutskiy 	if (vol->vol_type == UBI_STATIC_VOLUME) {
3974ab60a0dSArtem Bityutskiy 		if (vol->used_ebs == 0)
3984ab60a0dSArtem Bityutskiy 			/* Empty static UBI volume */
3994ab60a0dSArtem Bityutskiy 			return 0;
4004ab60a0dSArtem Bityutskiy 		if (lnum == vol->used_ebs - 1 &&
401801c135cSArtem B. Bityutskiy 		    offset + len > vol->last_eb_bytes)
402801c135cSArtem B. Bityutskiy 			return -EINVAL;
4034ab60a0dSArtem Bityutskiy 	}
404801c135cSArtem B. Bityutskiy 
405801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
406801c135cSArtem B. Bityutskiy 		return -EBADF;
407801c135cSArtem B. Bityutskiy 	if (len == 0)
408801c135cSArtem B. Bityutskiy 		return 0;
409801c135cSArtem B. Bityutskiy 
41089b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
411801c135cSArtem B. Bityutskiy 	if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
412801c135cSArtem B. Bityutskiy 		ubi_warn("mark volume %d as corrupted", vol_id);
413801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
414801c135cSArtem B. Bityutskiy 	}
415801c135cSArtem B. Bityutskiy 
416801c135cSArtem B. Bityutskiy 	return err;
417801c135cSArtem B. Bityutskiy }
418801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
419801c135cSArtem B. Bityutskiy 
420801c135cSArtem B. Bityutskiy /**
421801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
422801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
423801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
424801c135cSArtem B. Bityutskiy  * @buf: data to write
425801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
426801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
427801c135cSArtem B. Bityutskiy  * @dtype: expected data type
428801c135cSArtem B. Bityutskiy  *
429801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
430801c135cSArtem B. Bityutskiy  * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
431801c135cSArtem B. Bityutskiy  * the data.
432801c135cSArtem B. Bityutskiy  *
433801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
434801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
435801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
436801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
437801c135cSArtem B. Bityutskiy  *
438801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
439801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
440801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
441801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
442801c135cSArtem B. Bityutskiy  * garbage.
443801c135cSArtem B. Bityutskiy  *
444801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
445801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
446801c135cSArtem B. Bityutskiy  */
447801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
448801c135cSArtem B. Bityutskiy 		  int offset, int len, int dtype)
449801c135cSArtem B. Bityutskiy {
450801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
451801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
452801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
453801c135cSArtem B. Bityutskiy 
454c8566350SArtem Bityutskiy 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
455801c135cSArtem B. Bityutskiy 
456801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
457801c135cSArtem B. Bityutskiy 		return -EINVAL;
458801c135cSArtem B. Bityutskiy 
459801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
460801c135cSArtem B. Bityutskiy 		return -EROFS;
461801c135cSArtem B. Bityutskiy 
462801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
463cadb40ccSKyungmin Park 	    offset + len > vol->usable_leb_size ||
464cadb40ccSKyungmin Park 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
465801c135cSArtem B. Bityutskiy 		return -EINVAL;
466801c135cSArtem B. Bityutskiy 
467801c135cSArtem B. Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
468801c135cSArtem B. Bityutskiy 	    dtype != UBI_UNKNOWN)
469801c135cSArtem B. Bityutskiy 		return -EINVAL;
470801c135cSArtem B. Bityutskiy 
471801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
472801c135cSArtem B. Bityutskiy 		return -EBADF;
473801c135cSArtem B. Bityutskiy 
474801c135cSArtem B. Bityutskiy 	if (len == 0)
475801c135cSArtem B. Bityutskiy 		return 0;
476801c135cSArtem B. Bityutskiy 
47789b96b69SArtem Bityutskiy 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
478801c135cSArtem B. Bityutskiy }
479801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
480801c135cSArtem B. Bityutskiy 
481801c135cSArtem B. Bityutskiy /*
482801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
483801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
484801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
485801c135cSArtem B. Bityutskiy  * @buf: data to write
486801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
487801c135cSArtem B. Bityutskiy  * @dtype: expected data type
488801c135cSArtem B. Bityutskiy  *
489801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
490801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
491801c135cSArtem B. Bityutskiy  * data, which has to be aligned. The length may be shorter then the logical
492801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
493801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
494801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
495801c135cSArtem B. Bityutskiy  * code in case of failure.
496801c135cSArtem B. Bityutskiy  */
497801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
498801c135cSArtem B. Bityutskiy 		   int len, int dtype)
499801c135cSArtem B. Bityutskiy {
500801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
501801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
502801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
503801c135cSArtem B. Bityutskiy 
504c8566350SArtem Bityutskiy 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
505801c135cSArtem B. Bityutskiy 
506801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
507801c135cSArtem B. Bityutskiy 		return -EINVAL;
508801c135cSArtem B. Bityutskiy 
509801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
510801c135cSArtem B. Bityutskiy 		return -EROFS;
511801c135cSArtem B. Bityutskiy 
512801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
513cadb40ccSKyungmin Park 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
514801c135cSArtem B. Bityutskiy 		return -EINVAL;
515801c135cSArtem B. Bityutskiy 
516801c135cSArtem B. Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
517801c135cSArtem B. Bityutskiy 	    dtype != UBI_UNKNOWN)
518801c135cSArtem B. Bityutskiy 		return -EINVAL;
519801c135cSArtem B. Bityutskiy 
520801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
521801c135cSArtem B. Bityutskiy 		return -EBADF;
522801c135cSArtem B. Bityutskiy 
523801c135cSArtem B. Bityutskiy 	if (len == 0)
524801c135cSArtem B. Bityutskiy 		return 0;
525801c135cSArtem B. Bityutskiy 
52689b96b69SArtem Bityutskiy 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
527801c135cSArtem B. Bityutskiy }
528801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
529801c135cSArtem B. Bityutskiy 
530801c135cSArtem B. Bityutskiy /**
531801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
532801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
533801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
534801c135cSArtem B. Bityutskiy  *
535801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
536801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
537801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
538801c135cSArtem B. Bityutskiy  *
539801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
540801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
541801c135cSArtem B. Bityutskiy  */
542801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
543801c135cSArtem B. Bityutskiy {
544801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
545801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
546ae616e1bSArtem Bityutskiy 	int err;
547801c135cSArtem B. Bityutskiy 
548c8566350SArtem Bityutskiy 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
549801c135cSArtem B. Bityutskiy 
550801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
551801c135cSArtem B. Bityutskiy 		return -EROFS;
552801c135cSArtem B. Bityutskiy 
553801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
554801c135cSArtem B. Bityutskiy 		return -EINVAL;
555801c135cSArtem B. Bityutskiy 
556801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
557801c135cSArtem B. Bityutskiy 		return -EBADF;
558801c135cSArtem B. Bityutskiy 
55989b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
560801c135cSArtem B. Bityutskiy 	if (err)
561801c135cSArtem B. Bityutskiy 		return err;
562801c135cSArtem B. Bityutskiy 
563801c135cSArtem B. Bityutskiy 	return ubi_wl_flush(ubi);
564801c135cSArtem B. Bityutskiy }
565801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
566801c135cSArtem B. Bityutskiy 
567801c135cSArtem B. Bityutskiy /**
568801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
569801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
570801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
571801c135cSArtem B. Bityutskiy  *
572801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
573801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
574801c135cSArtem B. Bityutskiy  * physically erased in background. This operation is much faster then the
575801c135cSArtem B. Bityutskiy  * erase operation.
576801c135cSArtem B. Bityutskiy  *
577801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
578801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
579801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
580801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
581801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
582801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
583801c135cSArtem B. Bityutskiy  * used with care.
584801c135cSArtem B. Bityutskiy  *
585801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
586801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
587801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
588801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
589801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
590801c135cSArtem B. Bityutskiy  *
591801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
592801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
593801c135cSArtem B. Bityutskiy  * first un-map it, then write new data, rather then first erase it, then write
594801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
595801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
596801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
597801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
598801c135cSArtem B. Bityutskiy  *
599801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
600801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
601801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
602801c135cSArtem B. Bityutskiy  */
603801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
604801c135cSArtem B. Bityutskiy {
605801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
606801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
607801c135cSArtem B. Bityutskiy 
608c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
609801c135cSArtem B. Bityutskiy 
610801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
611801c135cSArtem B. Bityutskiy 		return -EROFS;
612801c135cSArtem B. Bityutskiy 
613801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
614801c135cSArtem B. Bityutskiy 		return -EINVAL;
615801c135cSArtem B. Bityutskiy 
616801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
617801c135cSArtem B. Bityutskiy 		return -EBADF;
618801c135cSArtem B. Bityutskiy 
61989b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
620801c135cSArtem B. Bityutskiy }
621801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
622801c135cSArtem B. Bityutskiy 
623801c135cSArtem B. Bityutskiy /**
6240e0ee1ccSDmitry Pervushin  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
625393852ecSArtem Bityutskiy  * @desc: volume descriptor
626393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
627393852ecSArtem Bityutskiy  * @dtype: expected data type
628393852ecSArtem Bityutskiy  *
629393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
63073ac36eaSColy Li  * eraseblock. This means, that after a successful invocation of this
631393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
632393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
633393852ecSArtem Bityutskiy  * happens.
634393852ecSArtem Bityutskiy  *
635393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
636393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
637393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
638393852ecSArtem Bityutskiy  * other failures.
639393852ecSArtem Bityutskiy  */
640393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
641393852ecSArtem Bityutskiy {
642393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
643393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
644393852ecSArtem Bityutskiy 
645c8566350SArtem Bityutskiy 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
646393852ecSArtem Bityutskiy 
647393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
648393852ecSArtem Bityutskiy 		return -EROFS;
649393852ecSArtem Bityutskiy 
650393852ecSArtem Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
651393852ecSArtem Bityutskiy 		return -EINVAL;
652393852ecSArtem Bityutskiy 
653393852ecSArtem Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
654393852ecSArtem Bityutskiy 	    dtype != UBI_UNKNOWN)
655393852ecSArtem Bityutskiy 		return -EINVAL;
656393852ecSArtem Bityutskiy 
657393852ecSArtem Bityutskiy 	if (vol->upd_marker)
658393852ecSArtem Bityutskiy 		return -EBADF;
659393852ecSArtem Bityutskiy 
660393852ecSArtem Bityutskiy 	if (vol->eba_tbl[lnum] >= 0)
661393852ecSArtem Bityutskiy 		return -EBADMSG;
662393852ecSArtem Bityutskiy 
66389b96b69SArtem Bityutskiy 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
664393852ecSArtem Bityutskiy }
665393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
666393852ecSArtem Bityutskiy 
667393852ecSArtem Bityutskiy /**
668801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
669801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
670801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
671801c135cSArtem B. Bityutskiy  *
672801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
673801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
674801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
675801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
676801c135cSArtem B. Bityutskiy  * mapped to.
677801c135cSArtem B. Bityutskiy  *
678801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
679801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
680801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
681801c135cSArtem B. Bityutskiy  * code.
682801c135cSArtem B. Bityutskiy  */
683801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
684801c135cSArtem B. Bityutskiy {
685801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
686801c135cSArtem B. Bityutskiy 
687c8566350SArtem Bityutskiy 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
688801c135cSArtem B. Bityutskiy 
689801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
690801c135cSArtem B. Bityutskiy 		return -EINVAL;
691801c135cSArtem B. Bityutskiy 
692801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
693801c135cSArtem B. Bityutskiy 		return -EBADF;
694801c135cSArtem B. Bityutskiy 
695801c135cSArtem B. Bityutskiy 	return vol->eba_tbl[lnum] >= 0;
696801c135cSArtem B. Bityutskiy }
697801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
698a5bf6190SArtem Bityutskiy 
699a5bf6190SArtem Bityutskiy /**
700a5bf6190SArtem Bityutskiy  * ubi_sync - synchronize UBI device buffers.
701a5bf6190SArtem Bityutskiy  * @ubi_num: UBI device to synchronize
702a5bf6190SArtem Bityutskiy  *
703a5bf6190SArtem Bityutskiy  * The underlying MTD device may cache data in hardware or in software. This
704a5bf6190SArtem Bityutskiy  * function ensures the caches are flushed. Returns zero in case of success and
705a5bf6190SArtem Bityutskiy  * a negative error code in case of failure.
706a5bf6190SArtem Bityutskiy  */
707a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num)
708a5bf6190SArtem Bityutskiy {
709a5bf6190SArtem Bityutskiy 	struct ubi_device *ubi;
710a5bf6190SArtem Bityutskiy 
711a5bf6190SArtem Bityutskiy 	ubi = ubi_get_device(ubi_num);
712a5bf6190SArtem Bityutskiy 	if (!ubi)
713a5bf6190SArtem Bityutskiy 		return -ENODEV;
714a5bf6190SArtem Bityutskiy 
715a5bf6190SArtem Bityutskiy 	if (ubi->mtd->sync)
716a5bf6190SArtem Bityutskiy 		ubi->mtd->sync(ubi->mtd);
717a5bf6190SArtem Bityutskiy 
718a5bf6190SArtem Bityutskiy 	ubi_put_device(ubi);
719a5bf6190SArtem Bityutskiy 	return 0;
720a5bf6190SArtem Bityutskiy }
721a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync);
7220e0ee1ccSDmitry Pervushin 
7230e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
7240e0ee1ccSDmitry Pervushin 
7250e0ee1ccSDmitry Pervushin /**
7260e0ee1ccSDmitry Pervushin  * ubi_register_volume_notifier - register a volume notifier.
7270e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
7280e0ee1ccSDmitry Pervushin  * @ignore_existing: if non-zero, do not send "added" notification for all
7290e0ee1ccSDmitry Pervushin  *                   already existing volumes
7300e0ee1ccSDmitry Pervushin  *
7310e0ee1ccSDmitry Pervushin  * This function registers a volume notifier, which means that
7320e0ee1ccSDmitry Pervushin  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
7330e0ee1ccSDmitry Pervushin  * removed, re-sized, re-named, or updated. The first argument of the function
7340e0ee1ccSDmitry Pervushin  * is the notification type. The second argument is pointer to a
7350e0ee1ccSDmitry Pervushin  * &struct ubi_notification object which describes the notification event.
7360e0ee1ccSDmitry Pervushin  * Using UBI API from the volume notifier is prohibited.
7370e0ee1ccSDmitry Pervushin  *
7380e0ee1ccSDmitry Pervushin  * This function returns zero in case of success and a negative error code
7390e0ee1ccSDmitry Pervushin  * in case of failure.
7400e0ee1ccSDmitry Pervushin  */
7410e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
7420e0ee1ccSDmitry Pervushin 				 int ignore_existing)
7430e0ee1ccSDmitry Pervushin {
7440e0ee1ccSDmitry Pervushin 	int err;
7450e0ee1ccSDmitry Pervushin 
7460e0ee1ccSDmitry Pervushin 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
7470e0ee1ccSDmitry Pervushin 	if (err != 0)
7480e0ee1ccSDmitry Pervushin 		return err;
7490e0ee1ccSDmitry Pervushin 	if (ignore_existing)
7500e0ee1ccSDmitry Pervushin 		return 0;
7510e0ee1ccSDmitry Pervushin 
7520e0ee1ccSDmitry Pervushin 	/*
7530e0ee1ccSDmitry Pervushin 	 * We are going to walk all UBI devices and all volumes, and
7540e0ee1ccSDmitry Pervushin 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
7550e0ee1ccSDmitry Pervushin 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
7560e0ee1ccSDmitry Pervushin 	 * devices do not disappear.
7570e0ee1ccSDmitry Pervushin 	 */
7580e0ee1ccSDmitry Pervushin 	mutex_lock(&ubi_devices_mutex);
7590e0ee1ccSDmitry Pervushin 	ubi_enumerate_volumes(nb);
7600e0ee1ccSDmitry Pervushin 	mutex_unlock(&ubi_devices_mutex);
7610e0ee1ccSDmitry Pervushin 
7620e0ee1ccSDmitry Pervushin 	return err;
7630e0ee1ccSDmitry Pervushin }
7640e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
7650e0ee1ccSDmitry Pervushin 
7660e0ee1ccSDmitry Pervushin /**
7670e0ee1ccSDmitry Pervushin  * ubi_unregister_volume_notifier - unregister the volume notifier.
7680e0ee1ccSDmitry Pervushin  * @nb: the notifier description object
7690e0ee1ccSDmitry Pervushin  *
7700e0ee1ccSDmitry Pervushin  * This function unregisters volume notifier @nm and returns zero in case of
7710e0ee1ccSDmitry Pervushin  * success and a negative error code in case of failure.
7720e0ee1ccSDmitry Pervushin  */
7730e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb)
7740e0ee1ccSDmitry Pervushin {
7750e0ee1ccSDmitry Pervushin 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
7760e0ee1ccSDmitry Pervushin }
7770e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
778