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