~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/mtd/ubi/scan.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 * eraseblocks are put to the @free list and the physical eraseblock to be
40
40
 * erased are put to the @erase list.
41
41
 *
 
42
 * About corruptions
 
43
 * ~~~~~~~~~~~~~~~~~
 
44
 *
 
45
 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
 
46
 * whether the headers are corrupted or not. Sometimes UBI also protects the
 
47
 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
 
48
 * when it moves the contents of a PEB for wear-leveling purposes.
 
49
 *
42
50
 * UBI tries to distinguish between 2 types of corruptions.
43
 
 * 1. Corruptions caused by power cuts. These are harmless and expected
44
 
 *    corruptions and UBI tries to handle them gracefully, without printing too
45
 
 *    many warnings and error messages. The idea is that we do not lose
46
 
 *    important data in these case - we may lose only the data which was being
47
 
 *    written to the media just before the power cut happened, and the upper
48
 
 *    layers (e.g., UBIFS) are supposed to handle these situations. UBI puts
49
 
 *    these PEBs to the head of the @erase list and they are scheduled for
50
 
 *    erasure.
 
51
 *
 
52
 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
 
53
 * tries to handle them gracefully, without printing too many warnings and
 
54
 * error messages. The idea is that we do not lose important data in these case
 
55
 * - we may lose only the data which was being written to the media just before
 
56
 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
 
57
 * handle such data losses (e.g., by using the FS journal).
 
58
 *
 
59
 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
 
60
 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
 
61
 * PEBs in the @erase list are scheduled for erasure later.
51
62
 *
52
63
 * 2. Unexpected corruptions which are not caused by power cuts. During
53
 
 *    scanning, such PEBs are put to the @corr list and UBI preserves them.
54
 
 *    Obviously, this lessens the amount of available PEBs, and if at some
55
 
 *    point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
56
 
 *    informs about such PEBs every time the MTD device is attached.
 
64
 * scanning, such PEBs are put to the @corr list and UBI preserves them.
 
65
 * Obviously, this lessens the amount of available PEBs, and if at some  point
 
66
 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
 
67
 * about such PEBs every time the MTD device is attached.
57
68
 *
58
69
 * However, it is difficult to reliably distinguish between these types of
59
 
 * corruptions and UBI's strategy is as follows. UBI assumes (2.) if the VID
60
 
 * header is corrupted and the data area does not contain all 0xFFs, and there
61
 
 * were not bit-flips or integrity errors while reading the data area. Otherwise
62
 
 * UBI assumes (1.). The assumptions are:
63
 
 *   o if the data area contains only 0xFFs, there is no data, and it is safe
64
 
 *     to just erase this PEB.
65
 
 *   o if the data area has bit-flips and data integrity errors (ECC errors on
 
70
 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
 
71
 * if the VID header is corrupted and the data area does not contain all 0xFFs,
 
72
 * and there were no bit-flips or integrity errors while reading the data area.
 
73
 * Otherwise UBI assumes corruption type 1. So the decision criteria are as
 
74
 * follows.
 
75
 *   o If the data area contains only 0xFFs, there is no data, and it is safe
 
76
 *     to just erase this PEB - this is corruption type 1.
 
77
 *   o If the data area has bit-flips or data integrity errors (ECC errors on
66
78
 *     NAND), it is probably a PEB which was being erased when power cut
67
 
 *     happened.
 
79
 *     happened, so this is corruption type 1. However, this is just a guess,
 
80
 *     which might be wrong.
 
81
 *   o Otherwise this it corruption type 2.
68
82
 */
69
83
 
70
84
#include <linux/err.h>
74
88
#include <linux/random.h>
75
89
#include "ubi.h"
76
90
 
77
 
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
 
91
#ifdef CONFIG_MTD_UBI_DEBUG
78
92
static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
79
93
#else
80
94
#define paranoid_check_si(ubi, si) 0
115
129
        } else
116
130
                BUG();
117
131
 
118
 
        seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
 
132
        seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
119
133
        if (!seb)
120
134
                return -ENOMEM;
121
135
 
144
158
 
145
159
        dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
146
160
 
147
 
        seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
 
161
        seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
148
162
        if (!seb)
149
163
                return -ENOMEM;
150
164
 
553
567
        if (err)
554
568
                return err;
555
569
 
556
 
        seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
 
570
        seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
557
571
        if (!seb)
558
572
                return -ENOMEM;
559
573
 
954
968
                         * contains garbage because of a power cut during erase
955
969
                         * operation. So we just schedule this PEB for erasure.
956
970
                         *
957
 
                         * Besides, in case of NOR flash, we deliberatly
 
971
                         * Besides, in case of NOR flash, we deliberately
958
972
                         * corrupt both headers because NOR flash erasure is
959
973
                         * slow and can start from the end.
960
974
                         */
1089
1103
                 * otherwise, only print a warning.
1090
1104
                 */
1091
1105
                if (si->corr_peb_count >= max_corr) {
1092
 
                        ubi_err("too many corrupted PEBs, refusing this device");
 
1106
                        ubi_err("too many corrupted PEBs, refusing");
1093
1107
                        return -EINVAL;
1094
1108
                }
1095
1109
        }
1152
1166
        si->volumes = RB_ROOT;
1153
1167
 
1154
1168
        err = -ENOMEM;
 
1169
        si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
 
1170
                                              sizeof(struct ubi_scan_leb),
 
1171
                                              0, 0, NULL);
 
1172
        if (!si->scan_leb_slab)
 
1173
                goto out_si;
 
1174
 
1155
1175
        ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1156
1176
        if (!ech)
1157
 
                goto out_si;
 
1177
                goto out_slab;
1158
1178
 
1159
1179
        vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1160
1180
        if (!vidh)
1215
1235
        ubi_free_vid_hdr(ubi, vidh);
1216
1236
out_ech:
1217
1237
        kfree(ech);
 
1238
out_slab:
 
1239
        kmem_cache_destroy(si->scan_leb_slab);
1218
1240
out_si:
1219
1241
        ubi_scan_destroy_si(si);
1220
1242
        return ERR_PTR(err);
1223
1245
/**
1224
1246
 * destroy_sv - free the scanning volume information
1225
1247
 * @sv: scanning volume information
 
1248
 * @si: scanning information
1226
1249
 *
1227
1250
 * This function destroys the volume RB-tree (@sv->root) and the scanning
1228
1251
 * volume information.
1229
1252
 */
1230
 
static void destroy_sv(struct ubi_scan_volume *sv)
 
1253
static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
1231
1254
{
1232
1255
        struct ubi_scan_leb *seb;
1233
1256
        struct rb_node *this = sv->root.rb_node;
1247
1270
                                        this->rb_right = NULL;
1248
1271
                        }
1249
1272
 
1250
 
                        kfree(seb);
 
1273
                        kmem_cache_free(si->scan_leb_slab, seb);
1251
1274
                }
1252
1275
        }
1253
1276
        kfree(sv);
1265
1288
 
1266
1289
        list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1267
1290
                list_del(&seb->u.list);
1268
 
                kfree(seb);
 
1291
                kmem_cache_free(si->scan_leb_slab, seb);
1269
1292
        }
1270
1293
        list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1271
1294
                list_del(&seb->u.list);
1272
 
                kfree(seb);
 
1295
                kmem_cache_free(si->scan_leb_slab, seb);
1273
1296
        }
1274
1297
        list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1275
1298
                list_del(&seb->u.list);
1276
 
                kfree(seb);
 
1299
                kmem_cache_free(si->scan_leb_slab, seb);
1277
1300
        }
1278
1301
        list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1279
1302
                list_del(&seb->u.list);
1280
 
                kfree(seb);
 
1303
                kmem_cache_free(si->scan_leb_slab, seb);
1281
1304
        }
1282
1305
 
1283
1306
        /* Destroy the volume RB-tree */
1298
1321
                                        rb->rb_right = NULL;
1299
1322
                        }
1300
1323
 
1301
 
                        destroy_sv(sv);
 
1324
                        destroy_sv(si, sv);
1302
1325
                }
1303
1326
        }
1304
1327
 
 
1328
        kmem_cache_destroy(si->scan_leb_slab);
1305
1329
        kfree(si);
1306
1330
}
1307
1331
 
1308
 
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
 
1332
#ifdef CONFIG_MTD_UBI_DEBUG
1309
1333
 
1310
1334
/**
1311
1335
 * paranoid_check_si - check the scanning information.
1323
1347
        struct ubi_scan_leb *seb, *last_seb;
1324
1348
        uint8_t *buf;
1325
1349
 
 
1350
        if (!(ubi_chk_flags & UBI_CHK_GEN))
 
1351
                return 0;
 
1352
 
1326
1353
        /*
1327
1354
         * At first, check that scanning information is OK.
1328
1355
         */
1575
1602
        return -EINVAL;
1576
1603
}
1577
1604
 
1578
 
#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
 
1605
#endif /* CONFIG_MTD_UBI_DEBUG */