xref: /openbmc/linux/drivers/mtd/ubi/kapi.c (revision 35ad5fb76cc0a08e14068408b064103439feee36)
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>
25801c135cSArtem B. Bityutskiy #include <asm/div64.h>
26801c135cSArtem B. Bityutskiy #include "ubi.h"
27801c135cSArtem B. Bityutskiy 
28801c135cSArtem B. Bityutskiy /**
29801c135cSArtem B. Bityutskiy  * ubi_get_device_info - get information about UBI device.
30801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
31801c135cSArtem B. Bityutskiy  * @di: the information is stored here
32801c135cSArtem B. Bityutskiy  *
33801c135cSArtem B. Bityutskiy  * This function returns %0 in case of success and a %-ENODEV if there is no
34801c135cSArtem B. Bityutskiy  * such UBI device.
35801c135cSArtem B. Bityutskiy  */
36801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
37801c135cSArtem B. Bityutskiy {
38801c135cSArtem B. Bityutskiy 	const struct ubi_device *ubi;
39801c135cSArtem B. Bityutskiy 
40801c135cSArtem B. Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
41503990ebSArtem Bityutskiy 	    !ubi_devices[ubi_num])
42801c135cSArtem B. Bityutskiy 		return -ENODEV;
43801c135cSArtem B. Bityutskiy 
44801c135cSArtem B. Bityutskiy 	ubi = ubi_devices[ubi_num];
45801c135cSArtem B. Bityutskiy 	di->ubi_num = ubi->ubi_num;
46801c135cSArtem B. Bityutskiy 	di->leb_size = ubi->leb_size;
47801c135cSArtem B. Bityutskiy 	di->min_io_size = ubi->min_io_size;
48801c135cSArtem B. Bityutskiy 	di->ro_mode = ubi->ro_mode;
4949dfc299SArtem Bityutskiy 	di->cdev = ubi->cdev.dev;
50801c135cSArtem B. Bityutskiy 	return 0;
51801c135cSArtem B. Bityutskiy }
52801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info);
53801c135cSArtem B. Bityutskiy 
54801c135cSArtem B. Bityutskiy /**
55801c135cSArtem B. Bityutskiy  * ubi_get_volume_info - get information about UBI volume.
56801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
57801c135cSArtem B. Bityutskiy  * @vi: the information is stored here
58801c135cSArtem B. Bityutskiy  */
59801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc,
60801c135cSArtem B. Bityutskiy 			 struct ubi_volume_info *vi)
61801c135cSArtem B. Bityutskiy {
62801c135cSArtem B. Bityutskiy 	const struct ubi_volume *vol = desc->vol;
63801c135cSArtem B. Bityutskiy 	const struct ubi_device *ubi = vol->ubi;
64801c135cSArtem B. Bityutskiy 
65801c135cSArtem B. Bityutskiy 	vi->vol_id = vol->vol_id;
66801c135cSArtem B. Bityutskiy 	vi->ubi_num = ubi->ubi_num;
67801c135cSArtem B. Bityutskiy 	vi->size = vol->reserved_pebs;
68801c135cSArtem B. Bityutskiy 	vi->used_bytes = vol->used_bytes;
69801c135cSArtem B. Bityutskiy 	vi->vol_type = vol->vol_type;
70801c135cSArtem B. Bityutskiy 	vi->corrupted = vol->corrupted;
71801c135cSArtem B. Bityutskiy 	vi->upd_marker = vol->upd_marker;
72801c135cSArtem B. Bityutskiy 	vi->alignment = vol->alignment;
73801c135cSArtem B. Bityutskiy 	vi->usable_leb_size = vol->usable_leb_size;
74801c135cSArtem B. Bityutskiy 	vi->name_len = vol->name_len;
75801c135cSArtem B. Bityutskiy 	vi->name = vol->name;
7649dfc299SArtem Bityutskiy 	vi->cdev = vol->cdev.dev;
77801c135cSArtem B. Bityutskiy }
78801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info);
79801c135cSArtem B. Bityutskiy 
80801c135cSArtem B. Bityutskiy /**
81801c135cSArtem B. Bityutskiy  * ubi_open_volume - open UBI volume.
82801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
83801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
84801c135cSArtem B. Bityutskiy  * @mode: open mode
85801c135cSArtem B. Bityutskiy  *
86801c135cSArtem B. Bityutskiy  * The @mode parameter specifies if the volume should be opened in read-only
87801c135cSArtem B. Bityutskiy  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
88801c135cSArtem B. Bityutskiy  * nobody else will be able to open this volume. UBI allows to have many volume
89801c135cSArtem B. Bityutskiy  * readers and one writer at a time.
90801c135cSArtem B. Bityutskiy  *
91801c135cSArtem B. Bityutskiy  * If a static volume is being opened for the first time since boot, it will be
92801c135cSArtem B. Bityutskiy  * checked by this function, which means it will be fully read and the CRC
93801c135cSArtem B. Bityutskiy  * checksum of each logical eraseblock will be checked.
94801c135cSArtem B. Bityutskiy  *
95801c135cSArtem B. Bityutskiy  * This function returns volume descriptor in case of success and a negative
96801c135cSArtem B. Bityutskiy  * error code in case of failure.
97801c135cSArtem B. Bityutskiy  */
98801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
99801c135cSArtem B. Bityutskiy {
100801c135cSArtem B. Bityutskiy 	int err;
101801c135cSArtem B. Bityutskiy 	struct ubi_volume_desc *desc;
1020169b49dSJesper Juhl 	struct ubi_device *ubi;
103801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol;
104801c135cSArtem B. Bityutskiy 
105801c135cSArtem B. Bityutskiy 	dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
106801c135cSArtem B. Bityutskiy 
107*35ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
108*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
1090169b49dSJesper Juhl 
110801c135cSArtem B. Bityutskiy 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
111801c135cSArtem B. Bityutskiy 	    mode != UBI_EXCLUSIVE)
112*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
113*35ad5fb7SArtem Bityutskiy 
114*35ad5fb7SArtem Bityutskiy 	ubi = ubi_devices[ubi_num];
115*35ad5fb7SArtem Bityutskiy 	if (!ubi)
116*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
117*35ad5fb7SArtem Bityutskiy 
118*35ad5fb7SArtem Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
119*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
120801c135cSArtem B. Bityutskiy 
121801c135cSArtem B. Bityutskiy 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
122*35ad5fb7SArtem Bityutskiy 	if (!desc)
123*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENOMEM);
124*35ad5fb7SArtem Bityutskiy 
125*35ad5fb7SArtem Bityutskiy 	err = -ENODEV;
126*35ad5fb7SArtem Bityutskiy 	if (!try_module_get(THIS_MODULE))
127*35ad5fb7SArtem Bityutskiy 		goto out_free;
128801c135cSArtem B. Bityutskiy 
129801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
130801c135cSArtem B. Bityutskiy 	vol = ubi->volumes[vol_id];
131*35ad5fb7SArtem Bityutskiy 	if (!vol)
132801c135cSArtem B. Bityutskiy 		goto out_unlock;
133801c135cSArtem B. Bityutskiy 
134801c135cSArtem B. Bityutskiy 	err = -EBUSY;
135801c135cSArtem B. Bityutskiy 	switch (mode) {
136801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
137801c135cSArtem B. Bityutskiy 		if (vol->exclusive)
138801c135cSArtem B. Bityutskiy 			goto out_unlock;
139801c135cSArtem B. Bityutskiy 		vol->readers += 1;
140801c135cSArtem B. Bityutskiy 		break;
141801c135cSArtem B. Bityutskiy 
142801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
143801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers > 0)
144801c135cSArtem B. Bityutskiy 			goto out_unlock;
145801c135cSArtem B. Bityutskiy 		vol->writers += 1;
146801c135cSArtem B. Bityutskiy 		break;
147801c135cSArtem B. Bityutskiy 
148801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
149801c135cSArtem B. Bityutskiy 		if (vol->exclusive || vol->writers || vol->readers)
150801c135cSArtem B. Bityutskiy 			goto out_unlock;
151801c135cSArtem B. Bityutskiy 		vol->exclusive = 1;
152801c135cSArtem B. Bityutskiy 		break;
153801c135cSArtem B. Bityutskiy 	}
154450f872aSArtem Bityutskiy 	get_device(&vol->dev);
155801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
156801c135cSArtem B. Bityutskiy 
157801c135cSArtem B. Bityutskiy 	desc->vol = vol;
158801c135cSArtem B. Bityutskiy 	desc->mode = mode;
159801c135cSArtem B. Bityutskiy 
160801c135cSArtem B. Bityutskiy 	/*
161cae0a771SArtem Bityutskiy 	 * To prevent simultaneous checks of the same volume we use
162cae0a771SArtem Bityutskiy 	 * @volumes_mutex, although it is not the purpose it was introduced
163cae0a771SArtem Bityutskiy 	 * for.
164801c135cSArtem B. Bityutskiy 	 */
165cae0a771SArtem Bityutskiy 	mutex_lock(&ubi->volumes_mutex);
166801c135cSArtem B. Bityutskiy 	if (!vol->checked) {
167801c135cSArtem B. Bityutskiy 		/* This is the first open - check the volume */
168801c135cSArtem B. Bityutskiy 		err = ubi_check_volume(ubi, vol_id);
169801c135cSArtem B. Bityutskiy 		if (err < 0) {
170cae0a771SArtem Bityutskiy 			mutex_unlock(&ubi->volumes_mutex);
171801c135cSArtem B. Bityutskiy 			ubi_close_volume(desc);
172801c135cSArtem B. Bityutskiy 			return ERR_PTR(err);
173801c135cSArtem B. Bityutskiy 		}
174801c135cSArtem B. Bityutskiy 		if (err == 1) {
175801c135cSArtem B. Bityutskiy 			ubi_warn("volume %d on UBI device %d is corrupted",
176801c135cSArtem B. Bityutskiy 				 vol_id, ubi->ubi_num);
177801c135cSArtem B. Bityutskiy 			vol->corrupted = 1;
178801c135cSArtem B. Bityutskiy 		}
179801c135cSArtem B. Bityutskiy 		vol->checked = 1;
180801c135cSArtem B. Bityutskiy 	}
181cae0a771SArtem Bityutskiy 	mutex_unlock(&ubi->volumes_mutex);
182*35ad5fb7SArtem Bityutskiy 
183801c135cSArtem B. Bityutskiy 	return desc;
184801c135cSArtem B. Bityutskiy 
185801c135cSArtem B. Bityutskiy out_unlock:
186801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
187801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
188*35ad5fb7SArtem Bityutskiy out_free:
189*35ad5fb7SArtem Bityutskiy 	kfree(desc);
190801c135cSArtem B. Bityutskiy 	return ERR_PTR(err);
191801c135cSArtem B. Bityutskiy }
192801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume);
193801c135cSArtem B. Bityutskiy 
194801c135cSArtem B. Bityutskiy /**
195801c135cSArtem B. Bityutskiy  * ubi_open_volume_nm - open UBI volume by name.
196801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
197801c135cSArtem B. Bityutskiy  * @name: volume name
198801c135cSArtem B. Bityutskiy  * @mode: open mode
199801c135cSArtem B. Bityutskiy  *
200801c135cSArtem B. Bityutskiy  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
201801c135cSArtem B. Bityutskiy  */
202801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
203801c135cSArtem B. Bityutskiy 					   int mode)
204801c135cSArtem B. Bityutskiy {
205801c135cSArtem B. Bityutskiy 	int i, vol_id = -1, len;
206801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi;
207801c135cSArtem B. Bityutskiy 
208801c135cSArtem B. Bityutskiy 	dbg_msg("open volume %s, mode %d", name, mode);
209801c135cSArtem B. Bityutskiy 
210801c135cSArtem B. Bityutskiy 	if (!name)
211801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
212801c135cSArtem B. Bityutskiy 
213801c135cSArtem B. Bityutskiy 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
214801c135cSArtem B. Bityutskiy 	if (len > UBI_VOL_NAME_MAX)
215801c135cSArtem B. Bityutskiy 		return ERR_PTR(-EINVAL);
216801c135cSArtem B. Bityutskiy 
217*35ad5fb7SArtem Bityutskiy 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
218*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-EINVAL);
219801c135cSArtem B. Bityutskiy 
220801c135cSArtem B. Bityutskiy 	ubi = ubi_devices[ubi_num];
221*35ad5fb7SArtem Bityutskiy 	if (!ubi)
222*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
223801c135cSArtem B. Bityutskiy 
224801c135cSArtem B. Bityutskiy 	spin_lock(&ubi->volumes_lock);
225801c135cSArtem B. Bityutskiy 	/* Walk all volumes of this UBI device */
226801c135cSArtem B. Bityutskiy 	for (i = 0; i < ubi->vtbl_slots; i++) {
227801c135cSArtem B. Bityutskiy 		struct ubi_volume *vol = ubi->volumes[i];
228801c135cSArtem B. Bityutskiy 
229801c135cSArtem B. Bityutskiy 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
230801c135cSArtem B. Bityutskiy 			vol_id = i;
231801c135cSArtem B. Bityutskiy 			break;
232801c135cSArtem B. Bityutskiy 		}
233801c135cSArtem B. Bityutskiy 	}
234801c135cSArtem B. Bityutskiy 	spin_unlock(&ubi->volumes_lock);
235801c135cSArtem B. Bityutskiy 
236801c135cSArtem B. Bityutskiy 	if (vol_id < 0)
237*35ad5fb7SArtem Bityutskiy 		return ERR_PTR(-ENODEV);
238801c135cSArtem B. Bityutskiy 
239*35ad5fb7SArtem Bityutskiy 	return ubi_open_volume(ubi_num, vol_id, mode);
240801c135cSArtem B. Bityutskiy }
241801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
242801c135cSArtem B. Bityutskiy 
243801c135cSArtem B. Bityutskiy /**
244801c135cSArtem B. Bityutskiy  * ubi_close_volume - close UBI volume.
245801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
246801c135cSArtem B. Bityutskiy  */
247801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc)
248801c135cSArtem B. Bityutskiy {
249801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
250801c135cSArtem B. Bityutskiy 
251801c135cSArtem B. Bityutskiy 	dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
252801c135cSArtem B. Bityutskiy 
253801c135cSArtem B. Bityutskiy 	spin_lock(&vol->ubi->volumes_lock);
254801c135cSArtem B. Bityutskiy 	switch (desc->mode) {
255801c135cSArtem B. Bityutskiy 	case UBI_READONLY:
256801c135cSArtem B. Bityutskiy 		vol->readers -= 1;
257801c135cSArtem B. Bityutskiy 		break;
258801c135cSArtem B. Bityutskiy 	case UBI_READWRITE:
259801c135cSArtem B. Bityutskiy 		vol->writers -= 1;
260801c135cSArtem B. Bityutskiy 		break;
261801c135cSArtem B. Bityutskiy 	case UBI_EXCLUSIVE:
262801c135cSArtem B. Bityutskiy 		vol->exclusive = 0;
263801c135cSArtem B. Bityutskiy 	}
264801c135cSArtem B. Bityutskiy 	spin_unlock(&vol->ubi->volumes_lock);
265801c135cSArtem B. Bityutskiy 
266801c135cSArtem B. Bityutskiy 	kfree(desc);
267450f872aSArtem Bityutskiy 	put_device(&vol->dev);
268801c135cSArtem B. Bityutskiy 	module_put(THIS_MODULE);
269801c135cSArtem B. Bityutskiy }
270801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume);
271801c135cSArtem B. Bityutskiy 
272801c135cSArtem B. Bityutskiy /**
273801c135cSArtem B. Bityutskiy  * ubi_leb_read - read data.
274801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
275801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to read from
276801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
277801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock to read from
278801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
279801c135cSArtem B. Bityutskiy  * @check: whether UBI has to check the read data's CRC or not.
280801c135cSArtem B. Bityutskiy  *
281801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of logical eraseblock @lnum and
282801c135cSArtem B. Bityutskiy  * stores the data at @buf. When reading from static volumes, @check specifies
283801c135cSArtem B. Bityutskiy  * whether the data has to be checked or not. If yes, the whole logical
284801c135cSArtem B. Bityutskiy  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
285801c135cSArtem B. Bityutskiy  * checksum is per-eraseblock). So checking may substantially slow down the
286801c135cSArtem B. Bityutskiy  * read speed. The @check argument is ignored for dynamic volumes.
287801c135cSArtem B. Bityutskiy  *
288801c135cSArtem B. Bityutskiy  * In case of success, this function returns zero. In case of failure, this
289801c135cSArtem B. Bityutskiy  * function returns a negative error code.
290801c135cSArtem B. Bityutskiy  *
291801c135cSArtem B. Bityutskiy  * %-EBADMSG error code is returned:
292801c135cSArtem B. Bityutskiy  * o for both static and dynamic volumes if MTD driver has detected a data
293801c135cSArtem B. Bityutskiy  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
294801c135cSArtem B. Bityutskiy  * o for static volumes in case of data CRC mismatch.
295801c135cSArtem B. Bityutskiy  *
296801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
297801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF error code.
298801c135cSArtem B. Bityutskiy  */
299801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
300801c135cSArtem B. Bityutskiy 		 int len, int check)
301801c135cSArtem B. Bityutskiy {
302801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
303801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
304801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
305801c135cSArtem B. Bityutskiy 
306801c135cSArtem B. Bityutskiy 	dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
307801c135cSArtem B. Bityutskiy 
308801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
309801c135cSArtem B. Bityutskiy 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
310801c135cSArtem B. Bityutskiy 	    offset + len > vol->usable_leb_size)
311801c135cSArtem B. Bityutskiy 		return -EINVAL;
312801c135cSArtem B. Bityutskiy 
3134ab60a0dSArtem Bityutskiy 	if (vol->vol_type == UBI_STATIC_VOLUME) {
3144ab60a0dSArtem Bityutskiy 		if (vol->used_ebs == 0)
3154ab60a0dSArtem Bityutskiy 			/* Empty static UBI volume */
3164ab60a0dSArtem Bityutskiy 			return 0;
3174ab60a0dSArtem Bityutskiy 		if (lnum == vol->used_ebs - 1 &&
318801c135cSArtem B. Bityutskiy 		    offset + len > vol->last_eb_bytes)
319801c135cSArtem B. Bityutskiy 			return -EINVAL;
3204ab60a0dSArtem Bityutskiy 	}
321801c135cSArtem B. Bityutskiy 
322801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
323801c135cSArtem B. Bityutskiy 		return -EBADF;
324801c135cSArtem B. Bityutskiy 	if (len == 0)
325801c135cSArtem B. Bityutskiy 		return 0;
326801c135cSArtem B. Bityutskiy 
32789b96b69SArtem Bityutskiy 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
328801c135cSArtem B. Bityutskiy 	if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
329801c135cSArtem B. Bityutskiy 		ubi_warn("mark volume %d as corrupted", vol_id);
330801c135cSArtem B. Bityutskiy 		vol->corrupted = 1;
331801c135cSArtem B. Bityutskiy 	}
332801c135cSArtem B. Bityutskiy 
333801c135cSArtem B. Bityutskiy 	return err;
334801c135cSArtem B. Bityutskiy }
335801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read);
336801c135cSArtem B. Bityutskiy 
337801c135cSArtem B. Bityutskiy /**
338801c135cSArtem B. Bityutskiy  * ubi_leb_write - write data.
339801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
340801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to write to
341801c135cSArtem B. Bityutskiy  * @buf: data to write
342801c135cSArtem B. Bityutskiy  * @offset: offset within the logical eraseblock where to write
343801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
344801c135cSArtem B. Bityutskiy  * @dtype: expected data type
345801c135cSArtem B. Bityutskiy  *
346801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from @buf to offset @offset of
347801c135cSArtem B. Bityutskiy  * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
348801c135cSArtem B. Bityutskiy  * the data.
349801c135cSArtem B. Bityutskiy  *
350801c135cSArtem B. Bityutskiy  * This function takes care of physical eraseblock write failures. If write to
351801c135cSArtem B. Bityutskiy  * the physical eraseblock write operation fails, the logical eraseblock is
352801c135cSArtem B. Bityutskiy  * re-mapped to another physical eraseblock, the data is recovered, and the
353801c135cSArtem B. Bityutskiy  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
354801c135cSArtem B. Bityutskiy  *
355801c135cSArtem B. Bityutskiy  * If all the data were successfully written, zero is returned. If an error
356801c135cSArtem B. Bityutskiy  * occurred and UBI has not been able to recover from it, this function returns
357801c135cSArtem B. Bityutskiy  * a negative error code. Note, in case of an error, it is possible that
358801c135cSArtem B. Bityutskiy  * something was still written to the flash media, but that may be some
359801c135cSArtem B. Bityutskiy  * garbage.
360801c135cSArtem B. Bityutskiy  *
361801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
362801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
363801c135cSArtem B. Bityutskiy  */
364801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
365801c135cSArtem B. Bityutskiy 		  int offset, int len, int dtype)
366801c135cSArtem B. Bityutskiy {
367801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
368801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
369801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
370801c135cSArtem B. Bityutskiy 
371801c135cSArtem B. Bityutskiy 	dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
372801c135cSArtem B. Bityutskiy 
373801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
374801c135cSArtem B. Bityutskiy 		return -EINVAL;
375801c135cSArtem B. Bityutskiy 
376801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
377801c135cSArtem B. Bityutskiy 		return -EROFS;
378801c135cSArtem B. Bityutskiy 
379801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
380801c135cSArtem B. Bityutskiy 	    offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
381801c135cSArtem B. Bityutskiy 	    len % ubi->min_io_size)
382801c135cSArtem B. Bityutskiy 		return -EINVAL;
383801c135cSArtem B. Bityutskiy 
384801c135cSArtem B. Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
385801c135cSArtem B. Bityutskiy 	    dtype != UBI_UNKNOWN)
386801c135cSArtem B. Bityutskiy 		return -EINVAL;
387801c135cSArtem B. Bityutskiy 
388801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
389801c135cSArtem B. Bityutskiy 		return -EBADF;
390801c135cSArtem B. Bityutskiy 
391801c135cSArtem B. Bityutskiy 	if (len == 0)
392801c135cSArtem B. Bityutskiy 		return 0;
393801c135cSArtem B. Bityutskiy 
39489b96b69SArtem Bityutskiy 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
395801c135cSArtem B. Bityutskiy }
396801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write);
397801c135cSArtem B. Bityutskiy 
398801c135cSArtem B. Bityutskiy /*
399801c135cSArtem B. Bityutskiy  * ubi_leb_change - change logical eraseblock atomically.
400801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
401801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number to change
402801c135cSArtem B. Bityutskiy  * @buf: data to write
403801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
404801c135cSArtem B. Bityutskiy  * @dtype: expected data type
405801c135cSArtem B. Bityutskiy  *
406801c135cSArtem B. Bityutskiy  * This function changes the contents of a logical eraseblock atomically. @buf
407801c135cSArtem B. Bityutskiy  * has to contain new logical eraseblock data, and @len - the length of the
408801c135cSArtem B. Bityutskiy  * data, which has to be aligned. The length may be shorter then the logical
409801c135cSArtem B. Bityutskiy  * eraseblock size, ant the logical eraseblock may be appended to more times
410801c135cSArtem B. Bityutskiy  * later on. This function guarantees that in case of an unclean reboot the old
411801c135cSArtem B. Bityutskiy  * contents is preserved. Returns zero in case of success and a negative error
412801c135cSArtem B. Bityutskiy  * code in case of failure.
413801c135cSArtem B. Bityutskiy  */
414801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
415801c135cSArtem B. Bityutskiy 		   int len, int dtype)
416801c135cSArtem B. Bityutskiy {
417801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
418801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
419801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
420801c135cSArtem B. Bityutskiy 
421801c135cSArtem B. Bityutskiy 	dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
422801c135cSArtem B. Bityutskiy 
423801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
424801c135cSArtem B. Bityutskiy 		return -EINVAL;
425801c135cSArtem B. Bityutskiy 
426801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
427801c135cSArtem B. Bityutskiy 		return -EROFS;
428801c135cSArtem B. Bityutskiy 
429801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
430801c135cSArtem B. Bityutskiy 	    len > vol->usable_leb_size || len % ubi->min_io_size)
431801c135cSArtem B. Bityutskiy 		return -EINVAL;
432801c135cSArtem B. Bityutskiy 
433801c135cSArtem B. Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
434801c135cSArtem B. Bityutskiy 	    dtype != UBI_UNKNOWN)
435801c135cSArtem B. Bityutskiy 		return -EINVAL;
436801c135cSArtem B. Bityutskiy 
437801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
438801c135cSArtem B. Bityutskiy 		return -EBADF;
439801c135cSArtem B. Bityutskiy 
440801c135cSArtem B. Bityutskiy 	if (len == 0)
441801c135cSArtem B. Bityutskiy 		return 0;
442801c135cSArtem B. Bityutskiy 
44389b96b69SArtem Bityutskiy 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
444801c135cSArtem B. Bityutskiy }
445801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change);
446801c135cSArtem B. Bityutskiy 
447801c135cSArtem B. Bityutskiy /**
448801c135cSArtem B. Bityutskiy  * ubi_leb_erase - erase logical eraseblock.
449801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
450801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
451801c135cSArtem B. Bityutskiy  *
452801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and synchronously erases the
453801c135cSArtem B. Bityutskiy  * correspondent physical eraseblock. Returns zero in case of success and a
454801c135cSArtem B. Bityutskiy  * negative error code in case of failure.
455801c135cSArtem B. Bityutskiy  *
456801c135cSArtem B. Bityutskiy  * If the volume is damaged because of an interrupted update this function just
457801c135cSArtem B. Bityutskiy  * returns immediately with %-EBADF code.
458801c135cSArtem B. Bityutskiy  */
459801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
460801c135cSArtem B. Bityutskiy {
461801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
462801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
463801c135cSArtem B. Bityutskiy 	int err, vol_id = vol->vol_id;
464801c135cSArtem B. Bityutskiy 
465801c135cSArtem B. Bityutskiy 	dbg_msg("erase LEB %d:%d", vol_id, lnum);
466801c135cSArtem B. Bityutskiy 
467801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
468801c135cSArtem B. Bityutskiy 		return -EROFS;
469801c135cSArtem B. Bityutskiy 
470801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
471801c135cSArtem B. Bityutskiy 		return -EINVAL;
472801c135cSArtem B. Bityutskiy 
473801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
474801c135cSArtem B. Bityutskiy 		return -EBADF;
475801c135cSArtem B. Bityutskiy 
47689b96b69SArtem Bityutskiy 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
477801c135cSArtem B. Bityutskiy 	if (err)
478801c135cSArtem B. Bityutskiy 		return err;
479801c135cSArtem B. Bityutskiy 
480801c135cSArtem B. Bityutskiy 	return ubi_wl_flush(ubi);
481801c135cSArtem B. Bityutskiy }
482801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase);
483801c135cSArtem B. Bityutskiy 
484801c135cSArtem B. Bityutskiy /**
485801c135cSArtem B. Bityutskiy  * ubi_leb_unmap - un-map logical eraseblock.
486801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
487801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
488801c135cSArtem B. Bityutskiy  *
489801c135cSArtem B. Bityutskiy  * This function un-maps logical eraseblock @lnum and schedules the
490801c135cSArtem B. Bityutskiy  * corresponding physical eraseblock for erasure, so that it will eventually be
491801c135cSArtem B. Bityutskiy  * physically erased in background. This operation is much faster then the
492801c135cSArtem B. Bityutskiy  * erase operation.
493801c135cSArtem B. Bityutskiy  *
494801c135cSArtem B. Bityutskiy  * Unlike erase, the un-map operation does not guarantee that the logical
495801c135cSArtem B. Bityutskiy  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
496801c135cSArtem B. Bityutskiy  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
497801c135cSArtem B. Bityutskiy  * happens after this, the logical eraseblocks will not necessarily be
498801c135cSArtem B. Bityutskiy  * un-mapped again when this MTD device is attached. They may actually be
499801c135cSArtem B. Bityutskiy  * mapped to the same physical eraseblocks again. So, this function has to be
500801c135cSArtem B. Bityutskiy  * used with care.
501801c135cSArtem B. Bityutskiy  *
502801c135cSArtem B. Bityutskiy  * In other words, when un-mapping a logical eraseblock, UBI does not store
503801c135cSArtem B. Bityutskiy  * any information about this on the flash media, it just marks the logical
504801c135cSArtem B. Bityutskiy  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
505801c135cSArtem B. Bityutskiy  * eraseblock is physically erased, it will be mapped again to the same logical
506801c135cSArtem B. Bityutskiy  * eraseblock when the MTD device is attached again.
507801c135cSArtem B. Bityutskiy  *
508801c135cSArtem B. Bityutskiy  * The main and obvious use-case of this function is when the contents of a
509801c135cSArtem B. Bityutskiy  * logical eraseblock has to be re-written. Then it is much more efficient to
510801c135cSArtem B. Bityutskiy  * first un-map it, then write new data, rather then first erase it, then write
511801c135cSArtem B. Bityutskiy  * new data. Note, once new data has been written to the logical eraseblock,
512801c135cSArtem B. Bityutskiy  * UBI guarantees that the old contents has gone forever. In other words, if an
513801c135cSArtem B. Bityutskiy  * unclean reboot happens after the logical eraseblock has been un-mapped and
514801c135cSArtem B. Bityutskiy  * then written to, it will contain the last written data.
515801c135cSArtem B. Bityutskiy  *
516801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
517801c135cSArtem B. Bityutskiy  * case of failure. If the volume is damaged because of an interrupted update
518801c135cSArtem B. Bityutskiy  * this function just returns immediately with %-EBADF code.
519801c135cSArtem B. Bityutskiy  */
520801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
521801c135cSArtem B. Bityutskiy {
522801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
523801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi = vol->ubi;
524801c135cSArtem B. Bityutskiy 	int vol_id = vol->vol_id;
525801c135cSArtem B. Bityutskiy 
526801c135cSArtem B. Bityutskiy 	dbg_msg("unmap LEB %d:%d", vol_id, lnum);
527801c135cSArtem B. Bityutskiy 
528801c135cSArtem B. Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
529801c135cSArtem B. Bityutskiy 		return -EROFS;
530801c135cSArtem B. Bityutskiy 
531801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
532801c135cSArtem B. Bityutskiy 		return -EINVAL;
533801c135cSArtem B. Bityutskiy 
534801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
535801c135cSArtem B. Bityutskiy 		return -EBADF;
536801c135cSArtem B. Bityutskiy 
53789b96b69SArtem Bityutskiy 	return ubi_eba_unmap_leb(ubi, vol, lnum);
538801c135cSArtem B. Bityutskiy }
539801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap);
540801c135cSArtem B. Bityutskiy 
541801c135cSArtem B. Bityutskiy /**
542393852ecSArtem Bityutskiy  * ubi_leb_map - map logical erasblock to a physical eraseblock.
543393852ecSArtem Bityutskiy  * @desc: volume descriptor
544393852ecSArtem Bityutskiy  * @lnum: logical eraseblock number
545393852ecSArtem Bityutskiy  * @dtype: expected data type
546393852ecSArtem Bityutskiy  *
547393852ecSArtem Bityutskiy  * This function maps an un-mapped logical eraseblock @lnum to a physical
548393852ecSArtem Bityutskiy  * eraseblock. This means, that after a successfull invocation of this
549393852ecSArtem Bityutskiy  * function the logical eraseblock @lnum will be empty (contain only %0xFF
550393852ecSArtem Bityutskiy  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
551393852ecSArtem Bityutskiy  * happens.
552393852ecSArtem Bityutskiy  *
553393852ecSArtem Bityutskiy  * This function returns zero in case of success, %-EBADF if the volume is
554393852ecSArtem Bityutskiy  * damaged because of an interrupted update, %-EBADMSG if the logical
555393852ecSArtem Bityutskiy  * eraseblock is already mapped, and other negative error codes in case of
556393852ecSArtem Bityutskiy  * other failures.
557393852ecSArtem Bityutskiy  */
558393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
559393852ecSArtem Bityutskiy {
560393852ecSArtem Bityutskiy 	struct ubi_volume *vol = desc->vol;
561393852ecSArtem Bityutskiy 	struct ubi_device *ubi = vol->ubi;
562393852ecSArtem Bityutskiy 	int vol_id = vol->vol_id;
563393852ecSArtem Bityutskiy 
564393852ecSArtem Bityutskiy 	dbg_msg("unmap LEB %d:%d", vol_id, lnum);
565393852ecSArtem Bityutskiy 
566393852ecSArtem Bityutskiy 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
567393852ecSArtem Bityutskiy 		return -EROFS;
568393852ecSArtem Bityutskiy 
569393852ecSArtem Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
570393852ecSArtem Bityutskiy 		return -EINVAL;
571393852ecSArtem Bityutskiy 
572393852ecSArtem Bityutskiy 	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
573393852ecSArtem Bityutskiy 	    dtype != UBI_UNKNOWN)
574393852ecSArtem Bityutskiy 		return -EINVAL;
575393852ecSArtem Bityutskiy 
576393852ecSArtem Bityutskiy 	if (vol->upd_marker)
577393852ecSArtem Bityutskiy 		return -EBADF;
578393852ecSArtem Bityutskiy 
579393852ecSArtem Bityutskiy 	if (vol->eba_tbl[lnum] >= 0)
580393852ecSArtem Bityutskiy 		return -EBADMSG;
581393852ecSArtem Bityutskiy 
58289b96b69SArtem Bityutskiy 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
583393852ecSArtem Bityutskiy }
584393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map);
585393852ecSArtem Bityutskiy 
586393852ecSArtem Bityutskiy /**
587801c135cSArtem B. Bityutskiy  * ubi_is_mapped - check if logical eraseblock is mapped.
588801c135cSArtem B. Bityutskiy  * @desc: volume descriptor
589801c135cSArtem B. Bityutskiy  * @lnum: logical eraseblock number
590801c135cSArtem B. Bityutskiy  *
591801c135cSArtem B. Bityutskiy  * This function checks if logical eraseblock @lnum is mapped to a physical
592801c135cSArtem B. Bityutskiy  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
593801c135cSArtem B. Bityutskiy  * mean it will still be un-mapped after the UBI device is re-attached. The
594801c135cSArtem B. Bityutskiy  * logical eraseblock may become mapped to the physical eraseblock it was last
595801c135cSArtem B. Bityutskiy  * mapped to.
596801c135cSArtem B. Bityutskiy  *
597801c135cSArtem B. Bityutskiy  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
598801c135cSArtem B. Bityutskiy  * error code in case of failure. If the volume is damaged because of an
599801c135cSArtem B. Bityutskiy  * interrupted update this function just returns immediately with %-EBADF error
600801c135cSArtem B. Bityutskiy  * code.
601801c135cSArtem B. Bityutskiy  */
602801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
603801c135cSArtem B. Bityutskiy {
604801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol = desc->vol;
605801c135cSArtem B. Bityutskiy 
606801c135cSArtem B. Bityutskiy 	dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
607801c135cSArtem B. Bityutskiy 
608801c135cSArtem B. Bityutskiy 	if (lnum < 0 || lnum >= vol->reserved_pebs)
609801c135cSArtem B. Bityutskiy 		return -EINVAL;
610801c135cSArtem B. Bityutskiy 
611801c135cSArtem B. Bityutskiy 	if (vol->upd_marker)
612801c135cSArtem B. Bityutskiy 		return -EBADF;
613801c135cSArtem B. Bityutskiy 
614801c135cSArtem B. Bityutskiy 	return vol->eba_tbl[lnum] >= 0;
615801c135cSArtem B. Bityutskiy }
616801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped);
617