Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) International Business Machines Corp., 2006
0004  * Copyright (c) Nokia Corporation, 2006, 2007
0005  *
0006  * Author: Artem Bityutskiy (Битюцкий Артём)
0007  */
0008 
0009 /*
0010  * This file includes volume table manipulation code. The volume table is an
0011  * on-flash table containing volume meta-data like name, number of reserved
0012  * physical eraseblocks, type, etc. The volume table is stored in the so-called
0013  * "layout volume".
0014  *
0015  * The layout volume is an internal volume which is organized as follows. It
0016  * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
0017  * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
0018  * other. This redundancy guarantees robustness to unclean reboots. The volume
0019  * table is basically an array of volume table records. Each record contains
0020  * full information about the volume and protected by a CRC checksum. Note,
0021  * nowadays we use the atomic LEB change operation when updating the volume
0022  * table, so we do not really need 2 LEBs anymore, but we preserve the older
0023  * design for the backward compatibility reasons.
0024  *
0025  * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
0026  * erased, and the updated volume table is written back to LEB 0. Then same for
0027  * LEB 1. This scheme guarantees recoverability from unclean reboots.
0028  *
0029  * In this UBI implementation the on-flash volume table does not contain any
0030  * information about how much data static volumes contain.
0031  *
0032  * But it would still be beneficial to store this information in the volume
0033  * table. For example, suppose we have a static volume X, and all its physical
0034  * eraseblocks became bad for some reasons. Suppose we are attaching the
0035  * corresponding MTD device, for some reason we find no logical eraseblocks
0036  * corresponding to the volume X. According to the volume table volume X does
0037  * exist. So we don't know whether it is just empty or all its physical
0038  * eraseblocks went bad. So we cannot alarm the user properly.
0039  *
0040  * The volume table also stores so-called "update marker", which is used for
0041  * volume updates. Before updating the volume, the update marker is set, and
0042  * after the update operation is finished, the update marker is cleared. So if
0043  * the update operation was interrupted (e.g. by an unclean reboot) - the
0044  * update marker is still there and we know that the volume's contents is
0045  * damaged.
0046  */
0047 
0048 #include <linux/crc32.h>
0049 #include <linux/err.h>
0050 #include <linux/slab.h>
0051 #include <asm/div64.h>
0052 #include "ubi.h"
0053 
0054 static void self_vtbl_check(const struct ubi_device *ubi);
0055 
0056 /* Empty volume table record */
0057 static struct ubi_vtbl_record empty_vtbl_record;
0058 
0059 /**
0060  * ubi_update_layout_vol - helper for updatting layout volumes on flash
0061  * @ubi: UBI device description object
0062  */
0063 static int ubi_update_layout_vol(struct ubi_device *ubi)
0064 {
0065     struct ubi_volume *layout_vol;
0066     int i, err;
0067 
0068     layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
0069     for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
0070         err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
0071                         ubi->vtbl_size);
0072         if (err)
0073             return err;
0074     }
0075 
0076     return 0;
0077 }
0078 
0079 /**
0080  * ubi_change_vtbl_record - change volume table record.
0081  * @ubi: UBI device description object
0082  * @idx: table index to change
0083  * @vtbl_rec: new volume table record
0084  *
0085  * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
0086  * volume table record is written. The caller does not have to calculate CRC of
0087  * the record as it is done by this function. Returns zero in case of success
0088  * and a negative error code in case of failure.
0089  */
0090 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
0091                struct ubi_vtbl_record *vtbl_rec)
0092 {
0093     int err;
0094     uint32_t crc;
0095 
0096     ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
0097 
0098     if (!vtbl_rec)
0099         vtbl_rec = &empty_vtbl_record;
0100     else {
0101         crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
0102         vtbl_rec->crc = cpu_to_be32(crc);
0103     }
0104 
0105     memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
0106     err = ubi_update_layout_vol(ubi);
0107 
0108     self_vtbl_check(ubi);
0109     return err ? err : 0;
0110 }
0111 
0112 /**
0113  * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
0114  * @ubi: UBI device description object
0115  * @rename_list: list of &struct ubi_rename_entry objects
0116  *
0117  * This function re-names multiple volumes specified in @req in the volume
0118  * table. Returns zero in case of success and a negative error code in case of
0119  * failure.
0120  */
0121 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
0122                 struct list_head *rename_list)
0123 {
0124     struct ubi_rename_entry *re;
0125 
0126     list_for_each_entry(re, rename_list, list) {
0127         uint32_t crc;
0128         struct ubi_volume *vol = re->desc->vol;
0129         struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
0130 
0131         if (re->remove) {
0132             memcpy(vtbl_rec, &empty_vtbl_record,
0133                    sizeof(struct ubi_vtbl_record));
0134             continue;
0135         }
0136 
0137         vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
0138         memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
0139         memset(vtbl_rec->name + re->new_name_len, 0,
0140                UBI_VOL_NAME_MAX + 1 - re->new_name_len);
0141         crc = crc32(UBI_CRC32_INIT, vtbl_rec,
0142                 UBI_VTBL_RECORD_SIZE_CRC);
0143         vtbl_rec->crc = cpu_to_be32(crc);
0144     }
0145 
0146     return ubi_update_layout_vol(ubi);
0147 }
0148 
0149 /**
0150  * vtbl_check - check if volume table is not corrupted and sensible.
0151  * @ubi: UBI device description object
0152  * @vtbl: volume table
0153  *
0154  * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
0155  * and %-EINVAL if it contains inconsistent data.
0156  */
0157 static int vtbl_check(const struct ubi_device *ubi,
0158               const struct ubi_vtbl_record *vtbl)
0159 {
0160     int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
0161     int upd_marker, err;
0162     uint32_t crc;
0163     const char *name;
0164 
0165     for (i = 0; i < ubi->vtbl_slots; i++) {
0166         cond_resched();
0167 
0168         reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
0169         alignment = be32_to_cpu(vtbl[i].alignment);
0170         data_pad = be32_to_cpu(vtbl[i].data_pad);
0171         upd_marker = vtbl[i].upd_marker;
0172         vol_type = vtbl[i].vol_type;
0173         name_len = be16_to_cpu(vtbl[i].name_len);
0174         name = &vtbl[i].name[0];
0175 
0176         crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
0177         if (be32_to_cpu(vtbl[i].crc) != crc) {
0178             ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
0179                  i, crc, be32_to_cpu(vtbl[i].crc));
0180             ubi_dump_vtbl_record(&vtbl[i], i);
0181             return 1;
0182         }
0183 
0184         if (reserved_pebs == 0) {
0185             if (memcmp(&vtbl[i], &empty_vtbl_record,
0186                         UBI_VTBL_RECORD_SIZE)) {
0187                 err = 2;
0188                 goto bad;
0189             }
0190             continue;
0191         }
0192 
0193         if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
0194             name_len < 0) {
0195             err = 3;
0196             goto bad;
0197         }
0198 
0199         if (alignment > ubi->leb_size || alignment == 0) {
0200             err = 4;
0201             goto bad;
0202         }
0203 
0204         n = alignment & (ubi->min_io_size - 1);
0205         if (alignment != 1 && n) {
0206             err = 5;
0207             goto bad;
0208         }
0209 
0210         n = ubi->leb_size % alignment;
0211         if (data_pad != n) {
0212             ubi_err(ubi, "bad data_pad, has to be %d", n);
0213             err = 6;
0214             goto bad;
0215         }
0216 
0217         if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
0218             err = 7;
0219             goto bad;
0220         }
0221 
0222         if (upd_marker != 0 && upd_marker != 1) {
0223             err = 8;
0224             goto bad;
0225         }
0226 
0227         if (reserved_pebs > ubi->good_peb_count) {
0228             ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
0229                 reserved_pebs, ubi->good_peb_count);
0230             err = 9;
0231             goto bad;
0232         }
0233 
0234         if (name_len > UBI_VOL_NAME_MAX) {
0235             err = 10;
0236             goto bad;
0237         }
0238 
0239         if (name[0] == '\0') {
0240             err = 11;
0241             goto bad;
0242         }
0243 
0244         if (name_len != strnlen(name, name_len + 1)) {
0245             err = 12;
0246             goto bad;
0247         }
0248     }
0249 
0250     /* Checks that all names are unique */
0251     for (i = 0; i < ubi->vtbl_slots - 1; i++) {
0252         for (n = i + 1; n < ubi->vtbl_slots; n++) {
0253             int len1 = be16_to_cpu(vtbl[i].name_len);
0254             int len2 = be16_to_cpu(vtbl[n].name_len);
0255 
0256             if (len1 > 0 && len1 == len2 &&
0257                 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
0258                 ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
0259                     i, n, vtbl[i].name);
0260                 ubi_dump_vtbl_record(&vtbl[i], i);
0261                 ubi_dump_vtbl_record(&vtbl[n], n);
0262                 return -EINVAL;
0263             }
0264         }
0265     }
0266 
0267     return 0;
0268 
0269 bad:
0270     ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
0271     ubi_dump_vtbl_record(&vtbl[i], i);
0272     return -EINVAL;
0273 }
0274 
0275 /**
0276  * create_vtbl - create a copy of volume table.
0277  * @ubi: UBI device description object
0278  * @ai: attaching information
0279  * @copy: number of the volume table copy
0280  * @vtbl: contents of the volume table
0281  *
0282  * This function returns zero in case of success and a negative error code in
0283  * case of failure.
0284  */
0285 static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
0286                int copy, void *vtbl)
0287 {
0288     int err, tries = 0;
0289     struct ubi_vid_io_buf *vidb;
0290     struct ubi_vid_hdr *vid_hdr;
0291     struct ubi_ainf_peb *new_aeb;
0292 
0293     dbg_gen("create volume table (copy #%d)", copy + 1);
0294 
0295     vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
0296     if (!vidb)
0297         return -ENOMEM;
0298 
0299     vid_hdr = ubi_get_vid_hdr(vidb);
0300 
0301 retry:
0302     new_aeb = ubi_early_get_peb(ubi, ai);
0303     if (IS_ERR(new_aeb)) {
0304         err = PTR_ERR(new_aeb);
0305         goto out_free;
0306     }
0307 
0308     vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
0309     vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
0310     vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
0311     vid_hdr->data_size = vid_hdr->used_ebs =
0312                  vid_hdr->data_pad = cpu_to_be32(0);
0313     vid_hdr->lnum = cpu_to_be32(copy);
0314     vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
0315 
0316     /* The EC header is already there, write the VID header */
0317     err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vidb);
0318     if (err)
0319         goto write_error;
0320 
0321     /* Write the layout volume contents */
0322     err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
0323     if (err)
0324         goto write_error;
0325 
0326     /*
0327      * And add it to the attaching information. Don't delete the old version
0328      * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
0329      */
0330     err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
0331     ubi_free_aeb(ai, new_aeb);
0332     ubi_free_vid_buf(vidb);
0333     return err;
0334 
0335 write_error:
0336     if (err == -EIO && ++tries <= 5) {
0337         /*
0338          * Probably this physical eraseblock went bad, try to pick
0339          * another one.
0340          */
0341         list_add(&new_aeb->u.list, &ai->erase);
0342         goto retry;
0343     }
0344     ubi_free_aeb(ai, new_aeb);
0345 out_free:
0346     ubi_free_vid_buf(vidb);
0347     return err;
0348 
0349 }
0350 
0351 /**
0352  * process_lvol - process the layout volume.
0353  * @ubi: UBI device description object
0354  * @ai: attaching information
0355  * @av: layout volume attaching information
0356  *
0357  * This function is responsible for reading the layout volume, ensuring it is
0358  * not corrupted, and recovering from corruptions if needed. Returns volume
0359  * table in case of success and a negative error code in case of failure.
0360  */
0361 static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
0362                         struct ubi_attach_info *ai,
0363                         struct ubi_ainf_volume *av)
0364 {
0365     int err;
0366     struct rb_node *rb;
0367     struct ubi_ainf_peb *aeb;
0368     struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
0369     int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
0370 
0371     /*
0372      * UBI goes through the following steps when it changes the layout
0373      * volume:
0374      * a. erase LEB 0;
0375      * b. write new data to LEB 0;
0376      * c. erase LEB 1;
0377      * d. write new data to LEB 1.
0378      *
0379      * Before the change, both LEBs contain the same data.
0380      *
0381      * Due to unclean reboots, the contents of LEB 0 may be lost, but there
0382      * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
0383      * Similarly, LEB 1 may be lost, but there should be LEB 0. And
0384      * finally, unclean reboots may result in a situation when neither LEB
0385      * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
0386      * 0 contains more recent information.
0387      *
0388      * So the plan is to first check LEB 0. Then
0389      * a. if LEB 0 is OK, it must be containing the most recent data; then
0390      *    we compare it with LEB 1, and if they are different, we copy LEB
0391      *    0 to LEB 1;
0392      * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
0393      *    to LEB 0.
0394      */
0395 
0396     dbg_gen("check layout volume");
0397 
0398     /* Read both LEB 0 and LEB 1 into memory */
0399     ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
0400         leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
0401         if (!leb[aeb->lnum]) {
0402             err = -ENOMEM;
0403             goto out_free;
0404         }
0405 
0406         err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
0407                        ubi->vtbl_size);
0408         if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
0409             /*
0410              * Scrub the PEB later. Note, -EBADMSG indicates an
0411              * uncorrectable ECC error, but we have our own CRC and
0412              * the data will be checked later. If the data is OK,
0413              * the PEB will be scrubbed (because we set
0414              * aeb->scrub). If the data is not OK, the contents of
0415              * the PEB will be recovered from the second copy, and
0416              * aeb->scrub will be cleared in
0417              * 'ubi_add_to_av()'.
0418              */
0419             aeb->scrub = 1;
0420         else if (err)
0421             goto out_free;
0422     }
0423 
0424     err = -EINVAL;
0425     if (leb[0]) {
0426         leb_corrupted[0] = vtbl_check(ubi, leb[0]);
0427         if (leb_corrupted[0] < 0)
0428             goto out_free;
0429     }
0430 
0431     if (!leb_corrupted[0]) {
0432         /* LEB 0 is OK */
0433         if (leb[1])
0434             leb_corrupted[1] = memcmp(leb[0], leb[1],
0435                           ubi->vtbl_size);
0436         if (leb_corrupted[1]) {
0437             ubi_warn(ubi, "volume table copy #2 is corrupted");
0438             err = create_vtbl(ubi, ai, 1, leb[0]);
0439             if (err)
0440                 goto out_free;
0441             ubi_msg(ubi, "volume table was restored");
0442         }
0443 
0444         /* Both LEB 1 and LEB 2 are OK and consistent */
0445         vfree(leb[1]);
0446         return leb[0];
0447     } else {
0448         /* LEB 0 is corrupted or does not exist */
0449         if (leb[1]) {
0450             leb_corrupted[1] = vtbl_check(ubi, leb[1]);
0451             if (leb_corrupted[1] < 0)
0452                 goto out_free;
0453         }
0454         if (leb_corrupted[1]) {
0455             /* Both LEB 0 and LEB 1 are corrupted */
0456             ubi_err(ubi, "both volume tables are corrupted");
0457             goto out_free;
0458         }
0459 
0460         ubi_warn(ubi, "volume table copy #1 is corrupted");
0461         err = create_vtbl(ubi, ai, 0, leb[1]);
0462         if (err)
0463             goto out_free;
0464         ubi_msg(ubi, "volume table was restored");
0465 
0466         vfree(leb[0]);
0467         return leb[1];
0468     }
0469 
0470 out_free:
0471     vfree(leb[0]);
0472     vfree(leb[1]);
0473     return ERR_PTR(err);
0474 }
0475 
0476 /**
0477  * create_empty_lvol - create empty layout volume.
0478  * @ubi: UBI device description object
0479  * @ai: attaching information
0480  *
0481  * This function returns volume table contents in case of success and a
0482  * negative error code in case of failure.
0483  */
0484 static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
0485                          struct ubi_attach_info *ai)
0486 {
0487     int i;
0488     struct ubi_vtbl_record *vtbl;
0489 
0490     vtbl = vzalloc(ubi->vtbl_size);
0491     if (!vtbl)
0492         return ERR_PTR(-ENOMEM);
0493 
0494     for (i = 0; i < ubi->vtbl_slots; i++)
0495         memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
0496 
0497     for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
0498         int err;
0499 
0500         err = create_vtbl(ubi, ai, i, vtbl);
0501         if (err) {
0502             vfree(vtbl);
0503             return ERR_PTR(err);
0504         }
0505     }
0506 
0507     return vtbl;
0508 }
0509 
0510 /**
0511  * init_volumes - initialize volume information for existing volumes.
0512  * @ubi: UBI device description object
0513  * @ai: scanning information
0514  * @vtbl: volume table
0515  *
0516  * This function allocates volume description objects for existing volumes.
0517  * Returns zero in case of success and a negative error code in case of
0518  * failure.
0519  */
0520 static int init_volumes(struct ubi_device *ubi,
0521             const struct ubi_attach_info *ai,
0522             const struct ubi_vtbl_record *vtbl)
0523 {
0524     int i, err, reserved_pebs = 0;
0525     struct ubi_ainf_volume *av;
0526     struct ubi_volume *vol;
0527 
0528     for (i = 0; i < ubi->vtbl_slots; i++) {
0529         cond_resched();
0530 
0531         if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
0532             continue; /* Empty record */
0533 
0534         vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
0535         if (!vol)
0536             return -ENOMEM;
0537 
0538         vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
0539         vol->alignment = be32_to_cpu(vtbl[i].alignment);
0540         vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
0541         vol->upd_marker = vtbl[i].upd_marker;
0542         vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
0543                     UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
0544         vol->name_len = be16_to_cpu(vtbl[i].name_len);
0545         vol->usable_leb_size = ubi->leb_size - vol->data_pad;
0546         memcpy(vol->name, vtbl[i].name, vol->name_len);
0547         vol->name[vol->name_len] = '\0';
0548         vol->vol_id = i;
0549 
0550         if (vtbl[i].flags & UBI_VTBL_SKIP_CRC_CHECK_FLG)
0551             vol->skip_check = 1;
0552 
0553         if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
0554             /* Auto re-size flag may be set only for one volume */
0555             if (ubi->autoresize_vol_id != -1) {
0556                 ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
0557                     ubi->autoresize_vol_id, i);
0558                 kfree(vol);
0559                 return -EINVAL;
0560             }
0561 
0562             ubi->autoresize_vol_id = i;
0563         }
0564 
0565         ubi_assert(!ubi->volumes[i]);
0566         ubi->volumes[i] = vol;
0567         ubi->vol_count += 1;
0568         vol->ubi = ubi;
0569         reserved_pebs += vol->reserved_pebs;
0570 
0571         /*
0572          * We use ubi->peb_count and not vol->reserved_pebs because
0573          * we want to keep the code simple. Otherwise we'd have to
0574          * resize/check the bitmap upon volume resize too.
0575          * Allocating a few bytes more does not hurt.
0576          */
0577         err = ubi_fastmap_init_checkmap(vol, ubi->peb_count);
0578         if (err)
0579             return err;
0580 
0581         /*
0582          * In case of dynamic volume UBI knows nothing about how many
0583          * data is stored there. So assume the whole volume is used.
0584          */
0585         if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
0586             vol->used_ebs = vol->reserved_pebs;
0587             vol->last_eb_bytes = vol->usable_leb_size;
0588             vol->used_bytes =
0589                 (long long)vol->used_ebs * vol->usable_leb_size;
0590             continue;
0591         }
0592 
0593         /* Static volumes only */
0594         av = ubi_find_av(ai, i);
0595         if (!av || !av->leb_count) {
0596             /*
0597              * No eraseblocks belonging to this volume found. We
0598              * don't actually know whether this static volume is
0599              * completely corrupted or just contains no data. And
0600              * we cannot know this as long as data size is not
0601              * stored on flash. So we just assume the volume is
0602              * empty. FIXME: this should be handled.
0603              */
0604             continue;
0605         }
0606 
0607         if (av->leb_count != av->used_ebs) {
0608             /*
0609              * We found a static volume which misses several
0610              * eraseblocks. Treat it as corrupted.
0611              */
0612             ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
0613                  av->vol_id, av->used_ebs - av->leb_count);
0614             vol->corrupted = 1;
0615             continue;
0616         }
0617 
0618         vol->used_ebs = av->used_ebs;
0619         vol->used_bytes =
0620             (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
0621         vol->used_bytes += av->last_data_size;
0622         vol->last_eb_bytes = av->last_data_size;
0623     }
0624 
0625     /* And add the layout volume */
0626     vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
0627     if (!vol)
0628         return -ENOMEM;
0629 
0630     vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
0631     vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
0632     vol->vol_type = UBI_DYNAMIC_VOLUME;
0633     vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
0634     memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
0635     vol->usable_leb_size = ubi->leb_size;
0636     vol->used_ebs = vol->reserved_pebs;
0637     vol->last_eb_bytes = vol->reserved_pebs;
0638     vol->used_bytes =
0639         (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
0640     vol->vol_id = UBI_LAYOUT_VOLUME_ID;
0641     vol->ref_count = 1;
0642 
0643     ubi_assert(!ubi->volumes[i]);
0644     ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
0645     reserved_pebs += vol->reserved_pebs;
0646     ubi->vol_count += 1;
0647     vol->ubi = ubi;
0648     err = ubi_fastmap_init_checkmap(vol, UBI_LAYOUT_VOLUME_EBS);
0649     if (err)
0650         return err;
0651 
0652     if (reserved_pebs > ubi->avail_pebs) {
0653         ubi_err(ubi, "not enough PEBs, required %d, available %d",
0654             reserved_pebs, ubi->avail_pebs);
0655         if (ubi->corr_peb_count)
0656             ubi_err(ubi, "%d PEBs are corrupted and not used",
0657                 ubi->corr_peb_count);
0658         return -ENOSPC;
0659     }
0660     ubi->rsvd_pebs += reserved_pebs;
0661     ubi->avail_pebs -= reserved_pebs;
0662 
0663     return 0;
0664 }
0665 
0666 /**
0667  * check_av - check volume attaching information.
0668  * @vol: UBI volume description object
0669  * @av: volume attaching information
0670  *
0671  * This function returns zero if the volume attaching information is consistent
0672  * to the data read from the volume tabla, and %-EINVAL if not.
0673  */
0674 static int check_av(const struct ubi_volume *vol,
0675             const struct ubi_ainf_volume *av)
0676 {
0677     int err;
0678 
0679     if (av->highest_lnum >= vol->reserved_pebs) {
0680         err = 1;
0681         goto bad;
0682     }
0683     if (av->leb_count > vol->reserved_pebs) {
0684         err = 2;
0685         goto bad;
0686     }
0687     if (av->vol_type != vol->vol_type) {
0688         err = 3;
0689         goto bad;
0690     }
0691     if (av->used_ebs > vol->reserved_pebs) {
0692         err = 4;
0693         goto bad;
0694     }
0695     if (av->data_pad != vol->data_pad) {
0696         err = 5;
0697         goto bad;
0698     }
0699     return 0;
0700 
0701 bad:
0702     ubi_err(vol->ubi, "bad attaching information, error %d", err);
0703     ubi_dump_av(av);
0704     ubi_dump_vol_info(vol);
0705     return -EINVAL;
0706 }
0707 
0708 /**
0709  * check_attaching_info - check that attaching information.
0710  * @ubi: UBI device description object
0711  * @ai: attaching information
0712  *
0713  * Even though we protect on-flash data by CRC checksums, we still don't trust
0714  * the media. This function ensures that attaching information is consistent to
0715  * the information read from the volume table. Returns zero if the attaching
0716  * information is OK and %-EINVAL if it is not.
0717  */
0718 static int check_attaching_info(const struct ubi_device *ubi,
0719                    struct ubi_attach_info *ai)
0720 {
0721     int err, i;
0722     struct ubi_ainf_volume *av;
0723     struct ubi_volume *vol;
0724 
0725     if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
0726         ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
0727             ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
0728         return -EINVAL;
0729     }
0730 
0731     if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
0732         ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
0733         ubi_err(ubi, "too large volume ID %d found",
0734             ai->highest_vol_id);
0735         return -EINVAL;
0736     }
0737 
0738     for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
0739         cond_resched();
0740 
0741         av = ubi_find_av(ai, i);
0742         vol = ubi->volumes[i];
0743         if (!vol) {
0744             if (av)
0745                 ubi_remove_av(ai, av);
0746             continue;
0747         }
0748 
0749         if (vol->reserved_pebs == 0) {
0750             ubi_assert(i < ubi->vtbl_slots);
0751 
0752             if (!av)
0753                 continue;
0754 
0755             /*
0756              * During attaching we found a volume which does not
0757              * exist according to the information in the volume
0758              * table. This must have happened due to an unclean
0759              * reboot while the volume was being removed. Discard
0760              * these eraseblocks.
0761              */
0762             ubi_msg(ubi, "finish volume %d removal", av->vol_id);
0763             ubi_remove_av(ai, av);
0764         } else if (av) {
0765             err = check_av(vol, av);
0766             if (err)
0767                 return err;
0768         }
0769     }
0770 
0771     return 0;
0772 }
0773 
0774 /**
0775  * ubi_read_volume_table - read the volume table.
0776  * @ubi: UBI device description object
0777  * @ai: attaching information
0778  *
0779  * This function reads volume table, checks it, recover from errors if needed,
0780  * or creates it if needed. Returns zero in case of success and a negative
0781  * error code in case of failure.
0782  */
0783 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
0784 {
0785     int err;
0786     struct ubi_ainf_volume *av;
0787 
0788     empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
0789 
0790     /*
0791      * The number of supported volumes is limited by the eraseblock size
0792      * and by the UBI_MAX_VOLUMES constant.
0793      */
0794     ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
0795     if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
0796         ubi->vtbl_slots = UBI_MAX_VOLUMES;
0797 
0798     ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
0799     ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
0800 
0801     av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
0802     if (!av) {
0803         /*
0804          * No logical eraseblocks belonging to the layout volume were
0805          * found. This could mean that the flash is just empty. In
0806          * this case we create empty layout volume.
0807          *
0808          * But if flash is not empty this must be a corruption or the
0809          * MTD device just contains garbage.
0810          */
0811         if (ai->is_empty) {
0812             ubi->vtbl = create_empty_lvol(ubi, ai);
0813             if (IS_ERR(ubi->vtbl))
0814                 return PTR_ERR(ubi->vtbl);
0815         } else {
0816             ubi_err(ubi, "the layout volume was not found");
0817             return -EINVAL;
0818         }
0819     } else {
0820         if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
0821             /* This must not happen with proper UBI images */
0822             ubi_err(ubi, "too many LEBs (%d) in layout volume",
0823                 av->leb_count);
0824             return -EINVAL;
0825         }
0826 
0827         ubi->vtbl = process_lvol(ubi, ai, av);
0828         if (IS_ERR(ubi->vtbl))
0829             return PTR_ERR(ubi->vtbl);
0830     }
0831 
0832     ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
0833 
0834     /*
0835      * The layout volume is OK, initialize the corresponding in-RAM data
0836      * structures.
0837      */
0838     err = init_volumes(ubi, ai, ubi->vtbl);
0839     if (err)
0840         goto out_free;
0841 
0842     /*
0843      * Make sure that the attaching information is consistent to the
0844      * information stored in the volume table.
0845      */
0846     err = check_attaching_info(ubi, ai);
0847     if (err)
0848         goto out_free;
0849 
0850     return 0;
0851 
0852 out_free:
0853     vfree(ubi->vtbl);
0854     ubi_free_all_volumes(ubi);
0855     return err;
0856 }
0857 
0858 /**
0859  * self_vtbl_check - check volume table.
0860  * @ubi: UBI device description object
0861  */
0862 static void self_vtbl_check(const struct ubi_device *ubi)
0863 {
0864     if (!ubi_dbg_chk_gen(ubi))
0865         return;
0866 
0867     if (vtbl_check(ubi, ubi->vtbl)) {
0868         ubi_err(ubi, "self-check failed");
0869         BUG();
0870     }
0871 }