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