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