UBI: fix checkpatch.pl errors and warnings
[safe/jmp/linux-2.6] / drivers / mtd / ubi / vtbl.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  * Copyright (c) Nokia Corporation, 2006, 2007
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Artem Bityutskiy (Битюцкий Артём)
20  */
21
22 /*
23  * This file includes volume table manipulation code. The volume table is an
24  * on-flash table containing volume meta-data like name, number of reserved
25  * physical eraseblocks, type, etc. The volume table is stored in the so-called
26  * "layout volume".
27  *
28  * The layout volume is an internal volume which is organized as follows. It
29  * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
30  * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
31  * other. This redundancy guarantees robustness to unclean reboots. The volume
32  * table is basically an array of volume table records. Each record contains
33  * full information about the volume and protected by a CRC checksum.
34  *
35  * The volume table is changed, it is first changed in RAM. Then LEB 0 is
36  * erased, and the updated volume table is written back to LEB 0. Then same for
37  * LEB 1. This scheme guarantees recoverability from unclean reboots.
38  *
39  * In this UBI implementation the on-flash volume table does not contain any
40  * information about how many data static volumes contain. This information may
41  * be found from the scanning data.
42  *
43  * But it would still be beneficial to store this information in the volume
44  * table. For example, suppose we have a static volume X, and all its physical
45  * eraseblocks became bad for some reasons. Suppose we are attaching the
46  * corresponding MTD device, the scanning has found no logical eraseblocks
47  * corresponding to the volume X. According to the volume table volume X does
48  * exist. So we don't know whether it is just empty or all its physical
49  * eraseblocks went bad. So we cannot alarm the user about this corruption.
50  *
51  * The volume table also stores so-called "update marker", which is used for
52  * volume updates. Before updating the volume, the update marker is set, and
53  * after the update operation is finished, the update marker is cleared. So if
54  * the update operation was interrupted (e.g. by an unclean reboot) - the
55  * update marker is still there and we know that the volume's contents is
56  * damaged.
57  */
58
59 #include <linux/crc32.h>
60 #include <linux/err.h>
61 #include <asm/div64.h>
62 #include "ubi.h"
63
64 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
65 static void paranoid_vtbl_check(const struct ubi_device *ubi);
66 #else
67 #define paranoid_vtbl_check(ubi)
68 #endif
69
70 /* Empty volume table record */
71 static struct ubi_vtbl_record empty_vtbl_record;
72
73 /**
74  * ubi_change_vtbl_record - change volume table record.
75  * @ubi: UBI device description object
76  * @idx: table index to change
77  * @vtbl_rec: new volume table record
78  *
79  * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
80  * volume table record is written. The caller does not have to calculate CRC of
81  * the record as it is done by this function. Returns zero in case of success
82  * and a negative error code in case of failure.
83  */
84 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
85                            struct ubi_vtbl_record *vtbl_rec)
86 {
87         int i, err;
88         uint32_t crc;
89         struct ubi_volume *layout_vol;
90
91         ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
92         layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
93
94         if (!vtbl_rec)
95                 vtbl_rec = &empty_vtbl_record;
96         else {
97                 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
98                 vtbl_rec->crc = cpu_to_be32(crc);
99         }
100
101         memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
102         for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
103                 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
104                 if (err)
105                         return err;
106
107                 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
108                                         ubi->vtbl_size, UBI_LONGTERM);
109                 if (err)
110                         return err;
111         }
112
113         paranoid_vtbl_check(ubi);
114         return 0;
115 }
116
117 /**
118  * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
119  * @ubi: UBI device description object
120  * @renam_list: list of &struct ubi_rename_entry objects
121  *
122  * This function re-names multiple volumes specified in @req in the volume
123  * table. Returns zero in case of success and a negative error code in case of
124  * failure.
125  */
126 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
127                             struct list_head *rename_list)
128 {
129         int i, err;
130         struct ubi_rename_entry *re;
131         struct ubi_volume *layout_vol;
132
133         list_for_each_entry(re, rename_list, list) {
134                 uint32_t crc;
135                 struct ubi_volume *vol = re->desc->vol;
136                 struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
137
138                 if (re->remove) {
139                         memcpy(vtbl_rec, &empty_vtbl_record,
140                                sizeof(struct ubi_vtbl_record));
141                         continue;
142                 }
143
144                 vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
145                 memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
146                 memset(vtbl_rec->name + re->new_name_len, 0,
147                        UBI_VOL_NAME_MAX + 1 - re->new_name_len);
148                 crc = crc32(UBI_CRC32_INIT, vtbl_rec,
149                             UBI_VTBL_RECORD_SIZE_CRC);
150                 vtbl_rec->crc = cpu_to_be32(crc);
151         }
152
153         layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
154         for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
155                 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
156                 if (err)
157                         return err;
158
159                 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
160                                         ubi->vtbl_size, UBI_LONGTERM);
161                 if (err)
162                         return err;
163         }
164
165         return 0;
166 }
167
168 /**
169  * vtbl_check - check if volume table is not corrupted and contains sensible
170  *              data.
171  * @ubi: UBI device description object
172  * @vtbl: volume table
173  *
174  * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
175  * and %-EINVAL if it contains inconsistent data.
176  */
177 static int vtbl_check(const struct ubi_device *ubi,
178                       const struct ubi_vtbl_record *vtbl)
179 {
180         int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
181         int upd_marker, err;
182         uint32_t crc;
183         const char *name;
184
185         for (i = 0; i < ubi->vtbl_slots; i++) {
186                 cond_resched();
187
188                 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
189                 alignment = be32_to_cpu(vtbl[i].alignment);
190                 data_pad = be32_to_cpu(vtbl[i].data_pad);
191                 upd_marker = vtbl[i].upd_marker;
192                 vol_type = vtbl[i].vol_type;
193                 name_len = be16_to_cpu(vtbl[i].name_len);
194                 name = &vtbl[i].name[0];
195
196                 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
197                 if (be32_to_cpu(vtbl[i].crc) != crc) {
198                         ubi_err("bad CRC at record %u: %#08x, not %#08x",
199                                  i, crc, be32_to_cpu(vtbl[i].crc));
200                         ubi_dbg_dump_vtbl_record(&vtbl[i], i);
201                         return 1;
202                 }
203
204                 if (reserved_pebs == 0) {
205                         if (memcmp(&vtbl[i], &empty_vtbl_record,
206                                                 UBI_VTBL_RECORD_SIZE)) {
207                                 err = 2;
208                                 goto bad;
209                         }
210                         continue;
211                 }
212
213                 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
214                     name_len < 0) {
215                         err = 3;
216                         goto bad;
217                 }
218
219                 if (alignment > ubi->leb_size || alignment == 0) {
220                         err = 4;
221                         goto bad;
222                 }
223
224                 n = alignment & (ubi->min_io_size - 1);
225                 if (alignment != 1 && n) {
226                         err = 5;
227                         goto bad;
228                 }
229
230                 n = ubi->leb_size % alignment;
231                 if (data_pad != n) {
232                         dbg_err("bad data_pad, has to be %d", n);
233                         err = 6;
234                         goto bad;
235                 }
236
237                 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
238                         err = 7;
239                         goto bad;
240                 }
241
242                 if (upd_marker != 0 && upd_marker != 1) {
243                         err = 8;
244                         goto bad;
245                 }
246
247                 if (reserved_pebs > ubi->good_peb_count) {
248                         dbg_err("too large reserved_pebs, good PEBs %d",
249                                 ubi->good_peb_count);
250                         err = 9;
251                         goto bad;
252                 }
253
254                 if (name_len > UBI_VOL_NAME_MAX) {
255                         err = 10;
256                         goto bad;
257                 }
258
259                 if (name[0] == '\0') {
260                         err = 11;
261                         goto bad;
262                 }
263
264                 if (name_len != strnlen(name, name_len + 1)) {
265                         err = 12;
266                         goto bad;
267                 }
268         }
269
270         /* Checks that all names are unique */
271         for (i = 0; i < ubi->vtbl_slots - 1; i++) {
272                 for (n = i + 1; n < ubi->vtbl_slots; n++) {
273                         int len1 = be16_to_cpu(vtbl[i].name_len);
274                         int len2 = be16_to_cpu(vtbl[n].name_len);
275
276                         if (len1 > 0 && len1 == len2 &&
277                             !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
278                                 ubi_err("volumes %d and %d have the same name"
279                                         " \"%s\"", i, n, vtbl[i].name);
280                                 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
281                                 ubi_dbg_dump_vtbl_record(&vtbl[n], n);
282                                 return -EINVAL;
283                         }
284                 }
285         }
286
287         return 0;
288
289 bad:
290         ubi_err("volume table check failed: record %d, error %d", i, err);
291         ubi_dbg_dump_vtbl_record(&vtbl[i], i);
292         return -EINVAL;
293 }
294
295 /**
296  * create_vtbl - create a copy of volume table.
297  * @ubi: UBI device description object
298  * @si: scanning information
299  * @copy: number of the volume table copy
300  * @vtbl: contents of the volume table
301  *
302  * This function returns zero in case of success and a negative error code in
303  * case of failure.
304  */
305 static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
306                        int copy, void *vtbl)
307 {
308         int err, tries = 0;
309         static struct ubi_vid_hdr *vid_hdr;
310         struct ubi_scan_volume *sv;
311         struct ubi_scan_leb *new_seb, *old_seb = NULL;
312
313         ubi_msg("create volume table (copy #%d)", copy + 1);
314
315         vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
316         if (!vid_hdr)
317                 return -ENOMEM;
318
319         /*
320          * Check if there is a logical eraseblock which would have to contain
321          * this volume table copy was found during scanning. It has to be wiped
322          * out.
323          */
324         sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
325         if (sv)
326                 old_seb = ubi_scan_find_seb(sv, copy);
327
328 retry:
329         new_seb = ubi_scan_get_free_peb(ubi, si);
330         if (IS_ERR(new_seb)) {
331                 err = PTR_ERR(new_seb);
332                 goto out_free;
333         }
334
335         vid_hdr->vol_type = UBI_VID_DYNAMIC;
336         vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
337         vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
338         vid_hdr->data_size = vid_hdr->used_ebs =
339                              vid_hdr->data_pad = cpu_to_be32(0);
340         vid_hdr->lnum = cpu_to_be32(copy);
341         vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
342         vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0);
343
344         /* The EC header is already there, write the VID header */
345         err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
346         if (err)
347                 goto write_error;
348
349         /* Write the layout volume contents */
350         err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
351         if (err)
352                 goto write_error;
353
354         /*
355          * And add it to the scanning information. Don't delete the old
356          * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'.
357          */
358         err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
359                                 vid_hdr, 0);
360         kfree(new_seb);
361         ubi_free_vid_hdr(ubi, vid_hdr);
362         return err;
363
364 write_error:
365         if (err == -EIO && ++tries <= 5) {
366                 /*
367                  * Probably this physical eraseblock went bad, try to pick
368                  * another one.
369                  */
370                 list_add_tail(&new_seb->u.list, &si->corr);
371                 goto retry;
372         }
373         kfree(new_seb);
374 out_free:
375         ubi_free_vid_hdr(ubi, vid_hdr);
376         return err;
377
378 }
379
380 /**
381  * process_lvol - process the layout volume.
382  * @ubi: UBI device description object
383  * @si: scanning information
384  * @sv: layout volume scanning information
385  *
386  * This function is responsible for reading the layout volume, ensuring it is
387  * not corrupted, and recovering from corruptions if needed. Returns volume
388  * table in case of success and a negative error code in case of failure.
389  */
390 static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
391                                             struct ubi_scan_info *si,
392                                             struct ubi_scan_volume *sv)
393 {
394         int err;
395         struct rb_node *rb;
396         struct ubi_scan_leb *seb;
397         struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
398         int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
399
400         /*
401          * UBI goes through the following steps when it changes the layout
402          * volume:
403          * a. erase LEB 0;
404          * b. write new data to LEB 0;
405          * c. erase LEB 1;
406          * d. write new data to LEB 1.
407          *
408          * Before the change, both LEBs contain the same data.
409          *
410          * Due to unclean reboots, the contents of LEB 0 may be lost, but there
411          * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
412          * Similarly, LEB 1 may be lost, but there should be LEB 0. And
413          * finally, unclean reboots may result in a situation when neither LEB
414          * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
415          * 0 contains more recent information.
416          *
417          * So the plan is to first check LEB 0. Then
418          * a. if LEB 0 is OK, it must be containing the most resent data; then
419          *    we compare it with LEB 1, and if they are different, we copy LEB
420          *    0 to LEB 1;
421          * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
422          *    to LEB 0.
423          */
424
425         dbg_gen("check layout volume");
426
427         /* Read both LEB 0 and LEB 1 into memory */
428         ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
429                 leb[seb->lnum] = vmalloc(ubi->vtbl_size);
430                 if (!leb[seb->lnum]) {
431                         err = -ENOMEM;
432                         goto out_free;
433                 }
434                 memset(leb[seb->lnum], 0, ubi->vtbl_size);
435
436                 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
437                                        ubi->vtbl_size);
438                 if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
439                         /*
440                          * Scrub the PEB later. Note, -EBADMSG indicates an
441                          * uncorrectable ECC error, but we have our own CRC and
442                          * the data will be checked later. If the data is OK,
443                          * the PEB will be scrubbed (because we set
444                          * seb->scrub). If the data is not OK, the contents of
445                          * the PEB will be recovered from the second copy, and
446                          * seb->scrub will be cleared in
447                          * 'ubi_scan_add_used()'.
448                          */
449                         seb->scrub = 1;
450                 else if (err)
451                         goto out_free;
452         }
453
454         err = -EINVAL;
455         if (leb[0]) {
456                 leb_corrupted[0] = vtbl_check(ubi, leb[0]);
457                 if (leb_corrupted[0] < 0)
458                         goto out_free;
459         }
460
461         if (!leb_corrupted[0]) {
462                 /* LEB 0 is OK */
463                 if (leb[1])
464                         leb_corrupted[1] = memcmp(leb[0], leb[1],
465                                                   ubi->vtbl_size);
466                 if (leb_corrupted[1]) {
467                         ubi_warn("volume table copy #2 is corrupted");
468                         err = create_vtbl(ubi, si, 1, leb[0]);
469                         if (err)
470                                 goto out_free;
471                         ubi_msg("volume table was restored");
472                 }
473
474                 /* Both LEB 1 and LEB 2 are OK and consistent */
475                 vfree(leb[1]);
476                 return leb[0];
477         } else {
478                 /* LEB 0 is corrupted or does not exist */
479                 if (leb[1]) {
480                         leb_corrupted[1] = vtbl_check(ubi, leb[1]);
481                         if (leb_corrupted[1] < 0)
482                                 goto out_free;
483                 }
484                 if (leb_corrupted[1]) {
485                         /* Both LEB 0 and LEB 1 are corrupted */
486                         ubi_err("both volume tables are corrupted");
487                         goto out_free;
488                 }
489
490                 ubi_warn("volume table copy #1 is corrupted");
491                 err = create_vtbl(ubi, si, 0, leb[1]);
492                 if (err)
493                         goto out_free;
494                 ubi_msg("volume table was restored");
495
496                 vfree(leb[0]);
497                 return leb[1];
498         }
499
500 out_free:
501         vfree(leb[0]);
502         vfree(leb[1]);
503         return ERR_PTR(err);
504 }
505
506 /**
507  * create_empty_lvol - create empty layout volume.
508  * @ubi: UBI device description object
509  * @si: scanning information
510  *
511  * This function returns volume table contents in case of success and a
512  * negative error code in case of failure.
513  */
514 static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
515                                                  struct ubi_scan_info *si)
516 {
517         int i;
518         struct ubi_vtbl_record *vtbl;
519
520         vtbl = vmalloc(ubi->vtbl_size);
521         if (!vtbl)
522                 return ERR_PTR(-ENOMEM);
523         memset(vtbl, 0, ubi->vtbl_size);
524
525         for (i = 0; i < ubi->vtbl_slots; i++)
526                 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
527
528         for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
529                 int err;
530
531                 err = create_vtbl(ubi, si, i, vtbl);
532                 if (err) {
533                         vfree(vtbl);
534                         return ERR_PTR(err);
535                 }
536         }
537
538         return vtbl;
539 }
540
541 /**
542  * init_volumes - initialize volume information for existing volumes.
543  * @ubi: UBI device description object
544  * @si: scanning information
545  * @vtbl: volume table
546  *
547  * This function allocates volume description objects for existing volumes.
548  * Returns zero in case of success and a negative error code in case of
549  * failure.
550  */
551 static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
552                         const struct ubi_vtbl_record *vtbl)
553 {
554         int i, reserved_pebs = 0;
555         struct ubi_scan_volume *sv;
556         struct ubi_volume *vol;
557
558         for (i = 0; i < ubi->vtbl_slots; i++) {
559                 cond_resched();
560
561                 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
562                         continue; /* Empty record */
563
564                 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
565                 if (!vol)
566                         return -ENOMEM;
567
568                 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
569                 vol->alignment = be32_to_cpu(vtbl[i].alignment);
570                 vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
571                 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
572                                         UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
573                 vol->name_len = be16_to_cpu(vtbl[i].name_len);
574                 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
575                 memcpy(vol->name, vtbl[i].name, vol->name_len);
576                 vol->name[vol->name_len] = '\0';
577                 vol->vol_id = i;
578
579                 if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
580                         /* Auto re-size flag may be set only for one volume */
581                         if (ubi->autoresize_vol_id != -1) {
582                                 ubi_err("more then one auto-resize volume (%d "
583                                         "and %d)", ubi->autoresize_vol_id, i);
584                                 kfree(vol);
585                                 return -EINVAL;
586                         }
587
588                         ubi->autoresize_vol_id = i;
589                 }
590
591                 ubi_assert(!ubi->volumes[i]);
592                 ubi->volumes[i] = vol;
593                 ubi->vol_count += 1;
594                 vol->ubi = ubi;
595                 reserved_pebs += vol->reserved_pebs;
596
597                 /*
598                  * In case of dynamic volume UBI knows nothing about how many
599                  * data is stored there. So assume the whole volume is used.
600                  */
601                 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
602                         vol->used_ebs = vol->reserved_pebs;
603                         vol->last_eb_bytes = vol->usable_leb_size;
604                         vol->used_bytes =
605                                 (long long)vol->used_ebs * vol->usable_leb_size;
606                         continue;
607                 }
608
609                 /* Static volumes only */
610                 sv = ubi_scan_find_sv(si, i);
611                 if (!sv) {
612                         /*
613                          * No eraseblocks belonging to this volume found. We
614                          * don't actually know whether this static volume is
615                          * completely corrupted or just contains no data. And
616                          * we cannot know this as long as data size is not
617                          * stored on flash. So we just assume the volume is
618                          * empty. FIXME: this should be handled.
619                          */
620                         continue;
621                 }
622
623                 if (sv->leb_count != sv->used_ebs) {
624                         /*
625                          * We found a static volume which misses several
626                          * eraseblocks. Treat it as corrupted.
627                          */
628                         ubi_warn("static volume %d misses %d LEBs - corrupted",
629                                  sv->vol_id, sv->used_ebs - sv->leb_count);
630                         vol->corrupted = 1;
631                         continue;
632                 }
633
634                 vol->used_ebs = sv->used_ebs;
635                 vol->used_bytes =
636                         (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
637                 vol->used_bytes += sv->last_data_size;
638                 vol->last_eb_bytes = sv->last_data_size;
639         }
640
641         /* And add the layout volume */
642         vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
643         if (!vol)
644                 return -ENOMEM;
645
646         vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
647         vol->alignment = 1;
648         vol->vol_type = UBI_DYNAMIC_VOLUME;
649         vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
650         memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
651         vol->usable_leb_size = ubi->leb_size;
652         vol->used_ebs = vol->reserved_pebs;
653         vol->last_eb_bytes = vol->reserved_pebs;
654         vol->used_bytes =
655                 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
656         vol->vol_id = UBI_LAYOUT_VOLUME_ID;
657         vol->ref_count = 1;
658
659         ubi_assert(!ubi->volumes[i]);
660         ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
661         reserved_pebs += vol->reserved_pebs;
662         ubi->vol_count += 1;
663         vol->ubi = ubi;
664
665         if (reserved_pebs > ubi->avail_pebs)
666                 ubi_err("not enough PEBs, required %d, available %d",
667                         reserved_pebs, ubi->avail_pebs);
668         ubi->rsvd_pebs += reserved_pebs;
669         ubi->avail_pebs -= reserved_pebs;
670
671         return 0;
672 }
673
674 /**
675  * check_sv - check volume scanning information.
676  * @vol: UBI volume description object
677  * @sv: volume scanning information
678  *
679  * This function returns zero if the volume scanning information is consistent
680  * to the data read from the volume tabla, and %-EINVAL if not.
681  */
682 static int check_sv(const struct ubi_volume *vol,
683                     const struct ubi_scan_volume *sv)
684 {
685         int err;
686
687         if (sv->highest_lnum >= vol->reserved_pebs) {
688                 err = 1;
689                 goto bad;
690         }
691         if (sv->leb_count > vol->reserved_pebs) {
692                 err = 2;
693                 goto bad;
694         }
695         if (sv->vol_type != vol->vol_type) {
696                 err = 3;
697                 goto bad;
698         }
699         if (sv->used_ebs > vol->reserved_pebs) {
700                 err = 4;
701                 goto bad;
702         }
703         if (sv->data_pad != vol->data_pad) {
704                 err = 5;
705                 goto bad;
706         }
707         return 0;
708
709 bad:
710         ubi_err("bad scanning information, error %d", err);
711         ubi_dbg_dump_sv(sv);
712         ubi_dbg_dump_vol_info(vol);
713         return -EINVAL;
714 }
715
716 /**
717  * check_scanning_info - check that scanning information.
718  * @ubi: UBI device description object
719  * @si: scanning information
720  *
721  * Even though we protect on-flash data by CRC checksums, we still don't trust
722  * the media. This function ensures that scanning information is consistent to
723  * the information read from the volume table. Returns zero if the scanning
724  * information is OK and %-EINVAL if it is not.
725  */
726 static int check_scanning_info(const struct ubi_device *ubi,
727                                struct ubi_scan_info *si)
728 {
729         int err, i;
730         struct ubi_scan_volume *sv;
731         struct ubi_volume *vol;
732
733         if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
734                 ubi_err("scanning found %d volumes, maximum is %d + %d",
735                         si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
736                 return -EINVAL;
737         }
738
739         if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
740             si->highest_vol_id < UBI_INTERNAL_VOL_START) {
741                 ubi_err("too large volume ID %d found by scanning",
742                         si->highest_vol_id);
743                 return -EINVAL;
744         }
745
746         for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
747                 cond_resched();
748
749                 sv = ubi_scan_find_sv(si, i);
750                 vol = ubi->volumes[i];
751                 if (!vol) {
752                         if (sv)
753                                 ubi_scan_rm_volume(si, sv);
754                         continue;
755                 }
756
757                 if (vol->reserved_pebs == 0) {
758                         ubi_assert(i < ubi->vtbl_slots);
759
760                         if (!sv)
761                                 continue;
762
763                         /*
764                          * During scanning we found a volume which does not
765                          * exist according to the information in the volume
766                          * table. This must have happened due to an unclean
767                          * reboot while the volume was being removed. Discard
768                          * these eraseblocks.
769                          */
770                         ubi_msg("finish volume %d removal", sv->vol_id);
771                         ubi_scan_rm_volume(si, sv);
772                 } else if (sv) {
773                         err = check_sv(vol, sv);
774                         if (err)
775                                 return err;
776                 }
777         }
778
779         return 0;
780 }
781
782 /**
783  * ubi_read_volume_table - read volume table.
784  * information.
785  * @ubi: UBI device description object
786  * @si: scanning information
787  *
788  * This function reads volume table, checks it, recover from errors if needed,
789  * or creates it if needed. Returns zero in case of success and a negative
790  * error code in case of failure.
791  */
792 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
793 {
794         int i, err;
795         struct ubi_scan_volume *sv;
796
797         empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
798
799         /*
800          * The number of supported volumes is limited by the eraseblock size
801          * and by the UBI_MAX_VOLUMES constant.
802          */
803         ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
804         if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
805                 ubi->vtbl_slots = UBI_MAX_VOLUMES;
806
807         ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
808         ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
809
810         sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
811         if (!sv) {
812                 /*
813                  * No logical eraseblocks belonging to the layout volume were
814                  * found. This could mean that the flash is just empty. In
815                  * this case we create empty layout volume.
816                  *
817                  * But if flash is not empty this must be a corruption or the
818                  * MTD device just contains garbage.
819                  */
820                 if (si->is_empty) {
821                         ubi->vtbl = create_empty_lvol(ubi, si);
822                         if (IS_ERR(ubi->vtbl))
823                                 return PTR_ERR(ubi->vtbl);
824                 } else {
825                         ubi_err("the layout volume was not found");
826                         return -EINVAL;
827                 }
828         } else {
829                 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
830                         /* This must not happen with proper UBI images */
831                         dbg_err("too many LEBs (%d) in layout volume",
832                                 sv->leb_count);
833                         return -EINVAL;
834                 }
835
836                 ubi->vtbl = process_lvol(ubi, si, sv);
837                 if (IS_ERR(ubi->vtbl))
838                         return PTR_ERR(ubi->vtbl);
839         }
840
841         ubi->avail_pebs = ubi->good_peb_count;
842
843         /*
844          * The layout volume is OK, initialize the corresponding in-RAM data
845          * structures.
846          */
847         err = init_volumes(ubi, si, ubi->vtbl);
848         if (err)
849                 goto out_free;
850
851         /*
852          * Get sure that the scanning information is consistent to the
853          * information stored in the volume table.
854          */
855         err = check_scanning_info(ubi, si);
856         if (err)
857                 goto out_free;
858
859         return 0;
860
861 out_free:
862         vfree(ubi->vtbl);
863         for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
864                 kfree(ubi->volumes[i]);
865                 ubi->volumes[i] = NULL;
866         }
867         return err;
868 }
869
870 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
871
872 /**
873  * paranoid_vtbl_check - check volume table.
874  * @ubi: UBI device description object
875  */
876 static void paranoid_vtbl_check(const struct ubi_device *ubi)
877 {
878         if (vtbl_check(ubi, ubi->vtbl)) {
879                 ubi_err("paranoid check failed");
880                 BUG();
881         }
882 }
883
884 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */