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