blob: 9c283768319f913ee1d9b84f41f2527d3c865cdf [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/* This file mostly implements UBI kernel API functions */
22
23#include <linux/module.h>
24#include <linux/err.h>
25#include <asm/div64.h>
26#include "ubi.h"
27
28/**
29 * ubi_get_device_info - get information about UBI device.
30 * @ubi_num: UBI device number
31 * @di: the information is stored here
32 *
33 * This function returns %0 in case of success and a %-ENODEV if there is no
34 * such UBI device.
35 */
36int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
37{
38 const struct ubi_device *ubi;
39
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040040 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
Artem Bityutskiy503990e2007-07-11 16:03:29 +030041 !ubi_devices[ubi_num])
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040042 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040043
44 ubi = ubi_devices[ubi_num];
45 di->ubi_num = ubi->ubi_num;
46 di->leb_size = ubi->leb_size;
47 di->min_io_size = ubi->min_io_size;
48 di->ro_mode = ubi->ro_mode;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020049 di->cdev = ubi->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040050 return 0;
51}
52EXPORT_SYMBOL_GPL(ubi_get_device_info);
53
54/**
55 * ubi_get_volume_info - get information about UBI volume.
56 * @desc: volume descriptor
57 * @vi: the information is stored here
58 */
59void ubi_get_volume_info(struct ubi_volume_desc *desc,
60 struct ubi_volume_info *vi)
61{
62 const struct ubi_volume *vol = desc->vol;
63 const struct ubi_device *ubi = vol->ubi;
64
65 vi->vol_id = vol->vol_id;
66 vi->ubi_num = ubi->ubi_num;
67 vi->size = vol->reserved_pebs;
68 vi->used_bytes = vol->used_bytes;
69 vi->vol_type = vol->vol_type;
70 vi->corrupted = vol->corrupted;
71 vi->upd_marker = vol->upd_marker;
72 vi->alignment = vol->alignment;
73 vi->usable_leb_size = vol->usable_leb_size;
74 vi->name_len = vol->name_len;
75 vi->name = vol->name;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +020076 vi->cdev = vol->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040077}
78EXPORT_SYMBOL_GPL(ubi_get_volume_info);
79
80/**
81 * ubi_open_volume - open UBI volume.
82 * @ubi_num: UBI device number
83 * @vol_id: volume ID
84 * @mode: open mode
85 *
86 * The @mode parameter specifies if the volume should be opened in read-only
87 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
88 * nobody else will be able to open this volume. UBI allows to have many volume
89 * readers and one writer at a time.
90 *
91 * If a static volume is being opened for the first time since boot, it will be
92 * checked by this function, which means it will be fully read and the CRC
93 * checksum of each logical eraseblock will be checked.
94 *
95 * This function returns volume descriptor in case of success and a negative
96 * error code in case of failure.
97 */
98struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
99{
100 int err;
101 struct ubi_volume_desc *desc;
Jesper Juhl0169b492007-08-04 01:25:26 +0200102 struct ubi_device *ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400103 struct ubi_volume *vol;
104
105 dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
106
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200107 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
108 return ERR_PTR(-EINVAL);
Jesper Juhl0169b492007-08-04 01:25:26 +0200109
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400110 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
111 mode != UBI_EXCLUSIVE)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200112 return ERR_PTR(-EINVAL);
113
114 ubi = ubi_devices[ubi_num];
115 if (!ubi)
116 return ERR_PTR(-ENODEV);
117
118 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
119 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400120
121 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200122 if (!desc)
123 return ERR_PTR(-ENOMEM);
124
125 err = -ENODEV;
126 if (!try_module_get(THIS_MODULE))
127 goto out_free;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400128
129 spin_lock(&ubi->volumes_lock);
130 vol = ubi->volumes[vol_id];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200131 if (!vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400132 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400133
134 err = -EBUSY;
135 switch (mode) {
136 case UBI_READONLY:
137 if (vol->exclusive)
138 goto out_unlock;
139 vol->readers += 1;
140 break;
141
142 case UBI_READWRITE:
143 if (vol->exclusive || vol->writers > 0)
144 goto out_unlock;
145 vol->writers += 1;
146 break;
147
148 case UBI_EXCLUSIVE:
149 if (vol->exclusive || vol->writers || vol->readers)
150 goto out_unlock;
151 vol->exclusive = 1;
152 break;
153 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200154 get_device(&vol->dev);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400155 spin_unlock(&ubi->volumes_lock);
156
157 desc->vol = vol;
158 desc->mode = mode;
159
160 /*
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200161 * To prevent simultaneous checks of the same volume we use
162 * @volumes_mutex, although it is not the purpose it was introduced
163 * for.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400164 */
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200165 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400166 if (!vol->checked) {
167 /* This is the first open - check the volume */
168 err = ubi_check_volume(ubi, vol_id);
169 if (err < 0) {
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200170 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171 ubi_close_volume(desc);
172 return ERR_PTR(err);
173 }
174 if (err == 1) {
175 ubi_warn("volume %d on UBI device %d is corrupted",
176 vol_id, ubi->ubi_num);
177 vol->corrupted = 1;
178 }
179 vol->checked = 1;
180 }
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200181 mutex_unlock(&ubi->volumes_mutex);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200182
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400183 return desc;
184
185out_unlock:
186 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187 module_put(THIS_MODULE);
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200188out_free:
189 kfree(desc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400190 return ERR_PTR(err);
191}
192EXPORT_SYMBOL_GPL(ubi_open_volume);
193
194/**
195 * ubi_open_volume_nm - open UBI volume by name.
196 * @ubi_num: UBI device number
197 * @name: volume name
198 * @mode: open mode
199 *
200 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
201 */
202struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
203 int mode)
204{
205 int i, vol_id = -1, len;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400206 struct ubi_device *ubi;
207
208 dbg_msg("open volume %s, mode %d", name, mode);
209
210 if (!name)
211 return ERR_PTR(-EINVAL);
212
213 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
214 if (len > UBI_VOL_NAME_MAX)
215 return ERR_PTR(-EINVAL);
216
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200217 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
218 return ERR_PTR(-EINVAL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400219
220 ubi = ubi_devices[ubi_num];
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200221 if (!ubi)
222 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400223
224 spin_lock(&ubi->volumes_lock);
225 /* Walk all volumes of this UBI device */
226 for (i = 0; i < ubi->vtbl_slots; i++) {
227 struct ubi_volume *vol = ubi->volumes[i];
228
229 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
230 vol_id = i;
231 break;
232 }
233 }
234 spin_unlock(&ubi->volumes_lock);
235
236 if (vol_id < 0)
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200237 return ERR_PTR(-ENODEV);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400238
Artem Bityutskiy35ad5fb2007-12-17 14:22:55 +0200239 return ubi_open_volume(ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400240}
241EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
242
243/**
244 * ubi_close_volume - close UBI volume.
245 * @desc: volume descriptor
246 */
247void ubi_close_volume(struct ubi_volume_desc *desc)
248{
249 struct ubi_volume *vol = desc->vol;
250
251 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
252
253 spin_lock(&vol->ubi->volumes_lock);
254 switch (desc->mode) {
255 case UBI_READONLY:
256 vol->readers -= 1;
257 break;
258 case UBI_READWRITE:
259 vol->writers -= 1;
260 break;
261 case UBI_EXCLUSIVE:
262 vol->exclusive = 0;
263 }
264 spin_unlock(&vol->ubi->volumes_lock);
265
266 kfree(desc);
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200267 put_device(&vol->dev);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400268 module_put(THIS_MODULE);
269}
270EXPORT_SYMBOL_GPL(ubi_close_volume);
271
272/**
273 * ubi_leb_read - read data.
274 * @desc: volume descriptor
275 * @lnum: logical eraseblock number to read from
276 * @buf: buffer where to store the read data
277 * @offset: offset within the logical eraseblock to read from
278 * @len: how many bytes to read
279 * @check: whether UBI has to check the read data's CRC or not.
280 *
281 * This function reads data from offset @offset of logical eraseblock @lnum and
282 * stores the data at @buf. When reading from static volumes, @check specifies
283 * whether the data has to be checked or not. If yes, the whole logical
284 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
285 * checksum is per-eraseblock). So checking may substantially slow down the
286 * read speed. The @check argument is ignored for dynamic volumes.
287 *
288 * In case of success, this function returns zero. In case of failure, this
289 * function returns a negative error code.
290 *
291 * %-EBADMSG error code is returned:
292 * o for both static and dynamic volumes if MTD driver has detected a data
293 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
294 * o for static volumes in case of data CRC mismatch.
295 *
296 * If the volume is damaged because of an interrupted update this function just
297 * returns immediately with %-EBADF error code.
298 */
299int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
300 int len, int check)
301{
302 struct ubi_volume *vol = desc->vol;
303 struct ubi_device *ubi = vol->ubi;
304 int err, vol_id = vol->vol_id;
305
306 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
307
308 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
309 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
310 offset + len > vol->usable_leb_size)
311 return -EINVAL;
312
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300313 if (vol->vol_type == UBI_STATIC_VOLUME) {
314 if (vol->used_ebs == 0)
315 /* Empty static UBI volume */
316 return 0;
317 if (lnum == vol->used_ebs - 1 &&
318 offset + len > vol->last_eb_bytes)
319 return -EINVAL;
320 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400321
322 if (vol->upd_marker)
323 return -EBADF;
324 if (len == 0)
325 return 0;
326
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200327 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400328 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
329 ubi_warn("mark volume %d as corrupted", vol_id);
330 vol->corrupted = 1;
331 }
332
333 return err;
334}
335EXPORT_SYMBOL_GPL(ubi_leb_read);
336
337/**
338 * ubi_leb_write - write data.
339 * @desc: volume descriptor
340 * @lnum: logical eraseblock number to write to
341 * @buf: data to write
342 * @offset: offset within the logical eraseblock where to write
343 * @len: how many bytes to write
344 * @dtype: expected data type
345 *
346 * This function writes @len bytes of data from @buf to offset @offset of
347 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
348 * the data.
349 *
350 * This function takes care of physical eraseblock write failures. If write to
351 * the physical eraseblock write operation fails, the logical eraseblock is
352 * re-mapped to another physical eraseblock, the data is recovered, and the
353 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
354 *
355 * If all the data were successfully written, zero is returned. If an error
356 * occurred and UBI has not been able to recover from it, this function returns
357 * a negative error code. Note, in case of an error, it is possible that
358 * something was still written to the flash media, but that may be some
359 * garbage.
360 *
361 * If the volume is damaged because of an interrupted update this function just
362 * returns immediately with %-EBADF code.
363 */
364int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
365 int offset, int len, int dtype)
366{
367 struct ubi_volume *vol = desc->vol;
368 struct ubi_device *ubi = vol->ubi;
369 int vol_id = vol->vol_id;
370
371 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
372
373 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
374 return -EINVAL;
375
376 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
377 return -EROFS;
378
379 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
380 offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
381 len % ubi->min_io_size)
382 return -EINVAL;
383
384 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
385 dtype != UBI_UNKNOWN)
386 return -EINVAL;
387
388 if (vol->upd_marker)
389 return -EBADF;
390
391 if (len == 0)
392 return 0;
393
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200394 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395}
396EXPORT_SYMBOL_GPL(ubi_leb_write);
397
398/*
399 * ubi_leb_change - change logical eraseblock atomically.
400 * @desc: volume descriptor
401 * @lnum: logical eraseblock number to change
402 * @buf: data to write
403 * @len: how many bytes to write
404 * @dtype: expected data type
405 *
406 * This function changes the contents of a logical eraseblock atomically. @buf
407 * has to contain new logical eraseblock data, and @len - the length of the
408 * data, which has to be aligned. The length may be shorter then the logical
409 * eraseblock size, ant the logical eraseblock may be appended to more times
410 * later on. This function guarantees that in case of an unclean reboot the old
411 * contents is preserved. Returns zero in case of success and a negative error
412 * code in case of failure.
413 */
414int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
415 int len, int dtype)
416{
417 struct ubi_volume *vol = desc->vol;
418 struct ubi_device *ubi = vol->ubi;
419 int vol_id = vol->vol_id;
420
421 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
422
423 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
424 return -EINVAL;
425
426 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
427 return -EROFS;
428
429 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
430 len > vol->usable_leb_size || len % ubi->min_io_size)
431 return -EINVAL;
432
433 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
434 dtype != UBI_UNKNOWN)
435 return -EINVAL;
436
437 if (vol->upd_marker)
438 return -EBADF;
439
440 if (len == 0)
441 return 0;
442
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200443 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400444}
445EXPORT_SYMBOL_GPL(ubi_leb_change);
446
447/**
448 * ubi_leb_erase - erase logical eraseblock.
449 * @desc: volume descriptor
450 * @lnum: logical eraseblock number
451 *
452 * This function un-maps logical eraseblock @lnum and synchronously erases the
453 * correspondent physical eraseblock. Returns zero in case of success and a
454 * negative error code in case of failure.
455 *
456 * If the volume is damaged because of an interrupted update this function just
457 * returns immediately with %-EBADF code.
458 */
459int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
460{
461 struct ubi_volume *vol = desc->vol;
462 struct ubi_device *ubi = vol->ubi;
463 int err, vol_id = vol->vol_id;
464
465 dbg_msg("erase LEB %d:%d", vol_id, lnum);
466
467 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
468 return -EROFS;
469
470 if (lnum < 0 || lnum >= vol->reserved_pebs)
471 return -EINVAL;
472
473 if (vol->upd_marker)
474 return -EBADF;
475
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200476 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400477 if (err)
478 return err;
479
480 return ubi_wl_flush(ubi);
481}
482EXPORT_SYMBOL_GPL(ubi_leb_erase);
483
484/**
485 * ubi_leb_unmap - un-map logical eraseblock.
486 * @desc: volume descriptor
487 * @lnum: logical eraseblock number
488 *
489 * This function un-maps logical eraseblock @lnum and schedules the
490 * corresponding physical eraseblock for erasure, so that it will eventually be
491 * physically erased in background. This operation is much faster then the
492 * erase operation.
493 *
494 * Unlike erase, the un-map operation does not guarantee that the logical
495 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
496 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
497 * happens after this, the logical eraseblocks will not necessarily be
498 * un-mapped again when this MTD device is attached. They may actually be
499 * mapped to the same physical eraseblocks again. So, this function has to be
500 * used with care.
501 *
502 * In other words, when un-mapping a logical eraseblock, UBI does not store
503 * any information about this on the flash media, it just marks the logical
504 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
505 * eraseblock is physically erased, it will be mapped again to the same logical
506 * eraseblock when the MTD device is attached again.
507 *
508 * The main and obvious use-case of this function is when the contents of a
509 * logical eraseblock has to be re-written. Then it is much more efficient to
510 * first un-map it, then write new data, rather then first erase it, then write
511 * new data. Note, once new data has been written to the logical eraseblock,
512 * UBI guarantees that the old contents has gone forever. In other words, if an
513 * unclean reboot happens after the logical eraseblock has been un-mapped and
514 * then written to, it will contain the last written data.
515 *
516 * This function returns zero in case of success and a negative error code in
517 * case of failure. If the volume is damaged because of an interrupted update
518 * this function just returns immediately with %-EBADF code.
519 */
520int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
521{
522 struct ubi_volume *vol = desc->vol;
523 struct ubi_device *ubi = vol->ubi;
524 int vol_id = vol->vol_id;
525
526 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
527
528 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
529 return -EROFS;
530
531 if (lnum < 0 || lnum >= vol->reserved_pebs)
532 return -EINVAL;
533
534 if (vol->upd_marker)
535 return -EBADF;
536
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200537 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400538}
539EXPORT_SYMBOL_GPL(ubi_leb_unmap);
540
541/**
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200542 * ubi_leb_map - map logical erasblock to a physical eraseblock.
543 * @desc: volume descriptor
544 * @lnum: logical eraseblock number
545 * @dtype: expected data type
546 *
547 * This function maps an un-mapped logical eraseblock @lnum to a physical
548 * eraseblock. This means, that after a successfull invocation of this
549 * function the logical eraseblock @lnum will be empty (contain only %0xFF
550 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
551 * happens.
552 *
553 * This function returns zero in case of success, %-EBADF if the volume is
554 * damaged because of an interrupted update, %-EBADMSG if the logical
555 * eraseblock is already mapped, and other negative error codes in case of
556 * other failures.
557 */
558int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
559{
560 struct ubi_volume *vol = desc->vol;
561 struct ubi_device *ubi = vol->ubi;
562 int vol_id = vol->vol_id;
563
564 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
565
566 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
567 return -EROFS;
568
569 if (lnum < 0 || lnum >= vol->reserved_pebs)
570 return -EINVAL;
571
572 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
573 dtype != UBI_UNKNOWN)
574 return -EINVAL;
575
576 if (vol->upd_marker)
577 return -EBADF;
578
579 if (vol->eba_tbl[lnum] >= 0)
580 return -EBADMSG;
581
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200582 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200583}
584EXPORT_SYMBOL_GPL(ubi_leb_map);
585
586/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400587 * ubi_is_mapped - check if logical eraseblock is mapped.
588 * @desc: volume descriptor
589 * @lnum: logical eraseblock number
590 *
591 * This function checks if logical eraseblock @lnum is mapped to a physical
592 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
593 * mean it will still be un-mapped after the UBI device is re-attached. The
594 * logical eraseblock may become mapped to the physical eraseblock it was last
595 * mapped to.
596 *
597 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
598 * error code in case of failure. If the volume is damaged because of an
599 * interrupted update this function just returns immediately with %-EBADF error
600 * code.
601 */
602int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
603{
604 struct ubi_volume *vol = desc->vol;
605
606 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
607
608 if (lnum < 0 || lnum >= vol->reserved_pebs)
609 return -EINVAL;
610
611 if (vol->upd_marker)
612 return -EBADF;
613
614 return vol->eba_tbl[lnum] >= 0;
615}
616EXPORT_SYMBOL_GPL(ubi_is_mapped);