blob: 96f5fef5f3fa6830376487ee6bcf2c11b4805129 [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
107 err = -ENODEV;
Jesper Juhl0169b492007-08-04 01:25:26 +0200108 if (ubi_num < 0)
109 return ERR_PTR(err);
110
111 ubi = ubi_devices[ubi_num];
112
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400113 if (!try_module_get(THIS_MODULE))
114 return ERR_PTR(err);
115
Jesper Juhl0169b492007-08-04 01:25:26 +0200116 if (ubi_num >= UBI_MAX_DEVICES || !ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400117 goto out_put;
118
119 err = -EINVAL;
120 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
121 goto out_put;
122 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
123 mode != UBI_EXCLUSIVE)
124 goto out_put;
125
126 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
127 if (!desc) {
128 err = -ENOMEM;
129 goto out_put;
130 }
131
132 spin_lock(&ubi->volumes_lock);
133 vol = ubi->volumes[vol_id];
134 if (!vol) {
135 err = -ENODEV;
136 goto out_unlock;
137 }
138
139 err = -EBUSY;
140 switch (mode) {
141 case UBI_READONLY:
142 if (vol->exclusive)
143 goto out_unlock;
144 vol->readers += 1;
145 break;
146
147 case UBI_READWRITE:
148 if (vol->exclusive || vol->writers > 0)
149 goto out_unlock;
150 vol->writers += 1;
151 break;
152
153 case UBI_EXCLUSIVE:
154 if (vol->exclusive || vol->writers || vol->readers)
155 goto out_unlock;
156 vol->exclusive = 1;
157 break;
158 }
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200159 get_device(&vol->dev);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400160 spin_unlock(&ubi->volumes_lock);
161
162 desc->vol = vol;
163 desc->mode = mode;
164
165 /*
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200166 * To prevent simultaneous checks of the same volume we use
167 * @volumes_mutex, although it is not the purpose it was introduced
168 * for.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169 */
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200170 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171 if (!vol->checked) {
172 /* This is the first open - check the volume */
173 err = ubi_check_volume(ubi, vol_id);
174 if (err < 0) {
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200175 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400176 ubi_close_volume(desc);
177 return ERR_PTR(err);
178 }
179 if (err == 1) {
180 ubi_warn("volume %d on UBI device %d is corrupted",
181 vol_id, ubi->ubi_num);
182 vol->corrupted = 1;
183 }
184 vol->checked = 1;
185 }
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200186 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187 return desc;
188
189out_unlock:
190 spin_unlock(&ubi->volumes_lock);
191 kfree(desc);
192out_put:
193 module_put(THIS_MODULE);
194 return ERR_PTR(err);
195}
196EXPORT_SYMBOL_GPL(ubi_open_volume);
197
198/**
199 * ubi_open_volume_nm - open UBI volume by name.
200 * @ubi_num: UBI device number
201 * @name: volume name
202 * @mode: open mode
203 *
204 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
205 */
206struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
207 int mode)
208{
209 int i, vol_id = -1, len;
210 struct ubi_volume_desc *ret;
211 struct ubi_device *ubi;
212
213 dbg_msg("open volume %s, mode %d", name, mode);
214
215 if (!name)
216 return ERR_PTR(-EINVAL);
217
218 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
219 if (len > UBI_VOL_NAME_MAX)
220 return ERR_PTR(-EINVAL);
221
222 ret = ERR_PTR(-ENODEV);
223 if (!try_module_get(THIS_MODULE))
224 return ret;
225
226 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num])
227 goto out_put;
228
229 ubi = ubi_devices[ubi_num];
230
231 spin_lock(&ubi->volumes_lock);
232 /* Walk all volumes of this UBI device */
233 for (i = 0; i < ubi->vtbl_slots; i++) {
234 struct ubi_volume *vol = ubi->volumes[i];
235
236 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
237 vol_id = i;
238 break;
239 }
240 }
241 spin_unlock(&ubi->volumes_lock);
242
243 if (vol_id < 0)
244 goto out_put;
245
246 ret = ubi_open_volume(ubi_num, vol_id, mode);
247
248out_put:
249 module_put(THIS_MODULE);
250 return ret;
251}
252EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
253
254/**
255 * ubi_close_volume - close UBI volume.
256 * @desc: volume descriptor
257 */
258void ubi_close_volume(struct ubi_volume_desc *desc)
259{
260 struct ubi_volume *vol = desc->vol;
261
262 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
263
264 spin_lock(&vol->ubi->volumes_lock);
265 switch (desc->mode) {
266 case UBI_READONLY:
267 vol->readers -= 1;
268 break;
269 case UBI_READWRITE:
270 vol->writers -= 1;
271 break;
272 case UBI_EXCLUSIVE:
273 vol->exclusive = 0;
274 }
275 spin_unlock(&vol->ubi->volumes_lock);
276
277 kfree(desc);
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200278 put_device(&vol->dev);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400279 module_put(THIS_MODULE);
280}
281EXPORT_SYMBOL_GPL(ubi_close_volume);
282
283/**
284 * ubi_leb_read - read data.
285 * @desc: volume descriptor
286 * @lnum: logical eraseblock number to read from
287 * @buf: buffer where to store the read data
288 * @offset: offset within the logical eraseblock to read from
289 * @len: how many bytes to read
290 * @check: whether UBI has to check the read data's CRC or not.
291 *
292 * This function reads data from offset @offset of logical eraseblock @lnum and
293 * stores the data at @buf. When reading from static volumes, @check specifies
294 * whether the data has to be checked or not. If yes, the whole logical
295 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
296 * checksum is per-eraseblock). So checking may substantially slow down the
297 * read speed. The @check argument is ignored for dynamic volumes.
298 *
299 * In case of success, this function returns zero. In case of failure, this
300 * function returns a negative error code.
301 *
302 * %-EBADMSG error code is returned:
303 * o for both static and dynamic volumes if MTD driver has detected a data
304 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
305 * o for static volumes in case of data CRC mismatch.
306 *
307 * If the volume is damaged because of an interrupted update this function just
308 * returns immediately with %-EBADF error code.
309 */
310int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
311 int len, int check)
312{
313 struct ubi_volume *vol = desc->vol;
314 struct ubi_device *ubi = vol->ubi;
315 int err, vol_id = vol->vol_id;
316
317 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
318
319 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
320 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
321 offset + len > vol->usable_leb_size)
322 return -EINVAL;
323
Artem Bityutskiy4ab60a02007-05-05 14:59:23 +0300324 if (vol->vol_type == UBI_STATIC_VOLUME) {
325 if (vol->used_ebs == 0)
326 /* Empty static UBI volume */
327 return 0;
328 if (lnum == vol->used_ebs - 1 &&
329 offset + len > vol->last_eb_bytes)
330 return -EINVAL;
331 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332
333 if (vol->upd_marker)
334 return -EBADF;
335 if (len == 0)
336 return 0;
337
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200338 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400339 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
340 ubi_warn("mark volume %d as corrupted", vol_id);
341 vol->corrupted = 1;
342 }
343
344 return err;
345}
346EXPORT_SYMBOL_GPL(ubi_leb_read);
347
348/**
349 * ubi_leb_write - write data.
350 * @desc: volume descriptor
351 * @lnum: logical eraseblock number to write to
352 * @buf: data to write
353 * @offset: offset within the logical eraseblock where to write
354 * @len: how many bytes to write
355 * @dtype: expected data type
356 *
357 * This function writes @len bytes of data from @buf to offset @offset of
358 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
359 * the data.
360 *
361 * This function takes care of physical eraseblock write failures. If write to
362 * the physical eraseblock write operation fails, the logical eraseblock is
363 * re-mapped to another physical eraseblock, the data is recovered, and the
364 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
365 *
366 * If all the data were successfully written, zero is returned. If an error
367 * occurred and UBI has not been able to recover from it, this function returns
368 * a negative error code. Note, in case of an error, it is possible that
369 * something was still written to the flash media, but that may be some
370 * garbage.
371 *
372 * If the volume is damaged because of an interrupted update this function just
373 * returns immediately with %-EBADF code.
374 */
375int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
376 int offset, int len, int dtype)
377{
378 struct ubi_volume *vol = desc->vol;
379 struct ubi_device *ubi = vol->ubi;
380 int vol_id = vol->vol_id;
381
382 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
383
384 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
385 return -EINVAL;
386
387 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
388 return -EROFS;
389
390 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
391 offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
392 len % ubi->min_io_size)
393 return -EINVAL;
394
395 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
396 dtype != UBI_UNKNOWN)
397 return -EINVAL;
398
399 if (vol->upd_marker)
400 return -EBADF;
401
402 if (len == 0)
403 return 0;
404
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200405 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400406}
407EXPORT_SYMBOL_GPL(ubi_leb_write);
408
409/*
410 * ubi_leb_change - change logical eraseblock atomically.
411 * @desc: volume descriptor
412 * @lnum: logical eraseblock number to change
413 * @buf: data to write
414 * @len: how many bytes to write
415 * @dtype: expected data type
416 *
417 * This function changes the contents of a logical eraseblock atomically. @buf
418 * has to contain new logical eraseblock data, and @len - the length of the
419 * data, which has to be aligned. The length may be shorter then the logical
420 * eraseblock size, ant the logical eraseblock may be appended to more times
421 * later on. This function guarantees that in case of an unclean reboot the old
422 * contents is preserved. Returns zero in case of success and a negative error
423 * code in case of failure.
424 */
425int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
426 int len, int dtype)
427{
428 struct ubi_volume *vol = desc->vol;
429 struct ubi_device *ubi = vol->ubi;
430 int vol_id = vol->vol_id;
431
432 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
433
434 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
435 return -EINVAL;
436
437 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
438 return -EROFS;
439
440 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
441 len > vol->usable_leb_size || len % ubi->min_io_size)
442 return -EINVAL;
443
444 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
445 dtype != UBI_UNKNOWN)
446 return -EINVAL;
447
448 if (vol->upd_marker)
449 return -EBADF;
450
451 if (len == 0)
452 return 0;
453
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200454 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400455}
456EXPORT_SYMBOL_GPL(ubi_leb_change);
457
458/**
459 * ubi_leb_erase - erase logical eraseblock.
460 * @desc: volume descriptor
461 * @lnum: logical eraseblock number
462 *
463 * This function un-maps logical eraseblock @lnum and synchronously erases the
464 * correspondent physical eraseblock. Returns zero in case of success and a
465 * negative error code in case of failure.
466 *
467 * If the volume is damaged because of an interrupted update this function just
468 * returns immediately with %-EBADF code.
469 */
470int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
471{
472 struct ubi_volume *vol = desc->vol;
473 struct ubi_device *ubi = vol->ubi;
474 int err, vol_id = vol->vol_id;
475
476 dbg_msg("erase LEB %d:%d", vol_id, lnum);
477
478 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
479 return -EROFS;
480
481 if (lnum < 0 || lnum >= vol->reserved_pebs)
482 return -EINVAL;
483
484 if (vol->upd_marker)
485 return -EBADF;
486
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200487 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 if (err)
489 return err;
490
491 return ubi_wl_flush(ubi);
492}
493EXPORT_SYMBOL_GPL(ubi_leb_erase);
494
495/**
496 * ubi_leb_unmap - un-map logical eraseblock.
497 * @desc: volume descriptor
498 * @lnum: logical eraseblock number
499 *
500 * This function un-maps logical eraseblock @lnum and schedules the
501 * corresponding physical eraseblock for erasure, so that it will eventually be
502 * physically erased in background. This operation is much faster then the
503 * erase operation.
504 *
505 * Unlike erase, the un-map operation does not guarantee that the logical
506 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
507 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
508 * happens after this, the logical eraseblocks will not necessarily be
509 * un-mapped again when this MTD device is attached. They may actually be
510 * mapped to the same physical eraseblocks again. So, this function has to be
511 * used with care.
512 *
513 * In other words, when un-mapping a logical eraseblock, UBI does not store
514 * any information about this on the flash media, it just marks the logical
515 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
516 * eraseblock is physically erased, it will be mapped again to the same logical
517 * eraseblock when the MTD device is attached again.
518 *
519 * The main and obvious use-case of this function is when the contents of a
520 * logical eraseblock has to be re-written. Then it is much more efficient to
521 * first un-map it, then write new data, rather then first erase it, then write
522 * new data. Note, once new data has been written to the logical eraseblock,
523 * UBI guarantees that the old contents has gone forever. In other words, if an
524 * unclean reboot happens after the logical eraseblock has been un-mapped and
525 * then written to, it will contain the last written data.
526 *
527 * This function returns zero in case of success and a negative error code in
528 * case of failure. If the volume is damaged because of an interrupted update
529 * this function just returns immediately with %-EBADF code.
530 */
531int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
532{
533 struct ubi_volume *vol = desc->vol;
534 struct ubi_device *ubi = vol->ubi;
535 int vol_id = vol->vol_id;
536
537 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
538
539 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
540 return -EROFS;
541
542 if (lnum < 0 || lnum >= vol->reserved_pebs)
543 return -EINVAL;
544
545 if (vol->upd_marker)
546 return -EBADF;
547
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200548 return ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400549}
550EXPORT_SYMBOL_GPL(ubi_leb_unmap);
551
552/**
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200553 * ubi_leb_map - map logical erasblock to a physical eraseblock.
554 * @desc: volume descriptor
555 * @lnum: logical eraseblock number
556 * @dtype: expected data type
557 *
558 * This function maps an un-mapped logical eraseblock @lnum to a physical
559 * eraseblock. This means, that after a successfull invocation of this
560 * function the logical eraseblock @lnum will be empty (contain only %0xFF
561 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
562 * happens.
563 *
564 * This function returns zero in case of success, %-EBADF if the volume is
565 * damaged because of an interrupted update, %-EBADMSG if the logical
566 * eraseblock is already mapped, and other negative error codes in case of
567 * other failures.
568 */
569int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
570{
571 struct ubi_volume *vol = desc->vol;
572 struct ubi_device *ubi = vol->ubi;
573 int vol_id = vol->vol_id;
574
575 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
576
577 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
578 return -EROFS;
579
580 if (lnum < 0 || lnum >= vol->reserved_pebs)
581 return -EINVAL;
582
583 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
584 dtype != UBI_UNKNOWN)
585 return -EINVAL;
586
587 if (vol->upd_marker)
588 return -EBADF;
589
590 if (vol->eba_tbl[lnum] >= 0)
591 return -EBADMSG;
592
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200593 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
Artem Bityutskiy393852e2007-12-06 18:47:30 +0200594}
595EXPORT_SYMBOL_GPL(ubi_leb_map);
596
597/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400598 * ubi_is_mapped - check if logical eraseblock is mapped.
599 * @desc: volume descriptor
600 * @lnum: logical eraseblock number
601 *
602 * This function checks if logical eraseblock @lnum is mapped to a physical
603 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
604 * mean it will still be un-mapped after the UBI device is re-attached. The
605 * logical eraseblock may become mapped to the physical eraseblock it was last
606 * mapped to.
607 *
608 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
609 * error code in case of failure. If the volume is damaged because of an
610 * interrupted update this function just returns immediately with %-EBADF error
611 * code.
612 */
613int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
614{
615 struct ubi_volume *vol = desc->vol;
616
617 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
618
619 if (lnum < 0 || lnum >= vol->reserved_pebs)
620 return -EINVAL;
621
622 if (vol->upd_marker)
623 return -EBADF;
624
625 return vol->eba_tbl[lnum] >= 0;
626}
627EXPORT_SYMBOL_GPL(ubi_is_mapped);