aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi/scan.c')
-rw-r--r--drivers/mtd/ubi/scan.c95
1 files changed, 61 insertions, 34 deletions
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 79ca304fc4db..11eb8ef12485 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -39,32 +39,46 @@
39 * eraseblocks are put to the @free list and the physical eraseblock to be 39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list. 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 * UBI tries to distinguish between 2 types of corruptions. 50 * UBI tries to distinguish between 2 types of corruptions.
43 * 1. Corruptions caused by power cuts. These are harmless and expected 51 *
44 * corruptions and UBI tries to handle them gracefully, without printing too 52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
45 * many warnings and error messages. The idea is that we do not lose 53 * tries to handle them gracefully, without printing too many warnings and
46 * important data in these case - we may lose only the data which was being 54 * error messages. The idea is that we do not lose important data in these case
47 * written to the media just before the power cut happened, and the upper 55 * - we may lose only the data which was being written to the media just before
48 * layers (e.g., UBIFS) are supposed to handle these situations. UBI puts 56 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
49 * these PEBs to the head of the @erase list and they are scheduled for 57 * handle such data losses (e.g., by using the FS journal).
50 * erasure. 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 * 2. Unexpected corruptions which are not caused by power cuts. During 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. 64 * 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 65 * Obviously, this lessens the amount of available PEBs, and if at some point
55 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly 66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
56 * informs about such PEBs every time the MTD device is attached. 67 * about such PEBs every time the MTD device is attached.
57 * 68 *
58 * However, it is difficult to reliably distinguish between these types of 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 70 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
60 * header is corrupted and the data area does not contain all 0xFFs, and there 71 * if the VID header is corrupted and the data area does not contain all 0xFFs,
61 * were not bit-flips or integrity errors while reading the data area. Otherwise 72 * and there were no bit-flips or integrity errors while reading the data area.
62 * UBI assumes (1.). The assumptions are: 73 * Otherwise UBI assumes corruption type 1. So the decision criteria are as
63 * o if the data area contains only 0xFFs, there is no data, and it is safe 74 * follows.
64 * to just erase this PEB. 75 * o If the data area contains only 0xFFs, there is no data, and it is safe
65 * o if the data area has bit-flips and data integrity errors (ECC errors on 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 * NAND), it is probably a PEB which was being erased when power cut 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#include <linux/err.h> 84#include <linux/err.h>
@@ -74,7 +88,7 @@
74#include <linux/random.h> 88#include <linux/random.h>
75#include "ubi.h" 89#include "ubi.h"
76 90
77#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 91#ifdef CONFIG_MTD_UBI_DEBUG
78static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si); 92static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
79#else 93#else
80#define paranoid_check_si(ubi, si) 0 94#define paranoid_check_si(ubi, si) 0
@@ -115,7 +129,7 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
115 } else 129 } else
116 BUG(); 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 if (!seb) 133 if (!seb)
120 return -ENOMEM; 134 return -ENOMEM;
121 135
@@ -144,7 +158,7 @@ static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
144 158
145 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); 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 if (!seb) 162 if (!seb)
149 return -ENOMEM; 163 return -ENOMEM;
150 164
@@ -553,7 +567,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
553 if (err) 567 if (err)
554 return err; 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 if (!seb) 571 if (!seb)
558 return -ENOMEM; 572 return -ENOMEM;
559 573
@@ -1152,9 +1166,15 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1152 si->volumes = RB_ROOT; 1166 si->volumes = RB_ROOT;
1153 1167
1154 err = -ENOMEM; 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 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1175 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1156 if (!ech) 1176 if (!ech)
1157 goto out_si; 1177 goto out_slab;
1158 1178
1159 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1179 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1160 if (!vidh) 1180 if (!vidh)
@@ -1215,6 +1235,8 @@ out_vidh:
1215 ubi_free_vid_hdr(ubi, vidh); 1235 ubi_free_vid_hdr(ubi, vidh);
1216out_ech: 1236out_ech:
1217 kfree(ech); 1237 kfree(ech);
1238out_slab:
1239 kmem_cache_destroy(si->scan_leb_slab);
1218out_si: 1240out_si:
1219 ubi_scan_destroy_si(si); 1241 ubi_scan_destroy_si(si);
1220 return ERR_PTR(err); 1242 return ERR_PTR(err);
@@ -1223,11 +1245,12 @@ out_si:
1223/** 1245/**
1224 * destroy_sv - free the scanning volume information 1246 * destroy_sv - free the scanning volume information
1225 * @sv: scanning volume information 1247 * @sv: scanning volume information
1248 * @si: scanning information
1226 * 1249 *
1227 * This function destroys the volume RB-tree (@sv->root) and the scanning 1250 * This function destroys the volume RB-tree (@sv->root) and the scanning
1228 * volume information. 1251 * volume information.
1229 */ 1252 */
1230static void destroy_sv(struct ubi_scan_volume *sv) 1253static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
1231{ 1254{
1232 struct ubi_scan_leb *seb; 1255 struct ubi_scan_leb *seb;
1233 struct rb_node *this = sv->root.rb_node; 1256 struct rb_node *this = sv->root.rb_node;
@@ -1247,7 +1270,7 @@ static void destroy_sv(struct ubi_scan_volume *sv)
1247 this->rb_right = NULL; 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 kfree(sv); 1276 kfree(sv);
@@ -1265,19 +1288,19 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1265 1288
1266 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { 1289 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1267 list_del(&seb->u.list); 1290 list_del(&seb->u.list);
1268 kfree(seb); 1291 kmem_cache_free(si->scan_leb_slab, seb);
1269 } 1292 }
1270 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { 1293 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1271 list_del(&seb->u.list); 1294 list_del(&seb->u.list);
1272 kfree(seb); 1295 kmem_cache_free(si->scan_leb_slab, seb);
1273 } 1296 }
1274 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { 1297 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1275 list_del(&seb->u.list); 1298 list_del(&seb->u.list);
1276 kfree(seb); 1299 kmem_cache_free(si->scan_leb_slab, seb);
1277 } 1300 }
1278 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { 1301 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1279 list_del(&seb->u.list); 1302 list_del(&seb->u.list);
1280 kfree(seb); 1303 kmem_cache_free(si->scan_leb_slab, seb);
1281 } 1304 }
1282 1305
1283 /* Destroy the volume RB-tree */ 1306 /* Destroy the volume RB-tree */
@@ -1298,14 +1321,15 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1298 rb->rb_right = NULL; 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 kfree(si); 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 * paranoid_check_si - check the scanning information. 1335 * paranoid_check_si - check the scanning information.
@@ -1323,6 +1347,9 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1323 struct ubi_scan_leb *seb, *last_seb; 1347 struct ubi_scan_leb *seb, *last_seb;
1324 uint8_t *buf; 1348 uint8_t *buf;
1325 1349
1350 if (!(ubi_chk_flags & UBI_CHK_GEN))
1351 return 0;
1352
1326 /* 1353 /*
1327 * At first, check that scanning information is OK. 1354 * At first, check that scanning information is OK.
1328 */ 1355 */
@@ -1575,4 +1602,4 @@ out:
1575 return -EINVAL; 1602 return -EINVAL;
1576} 1603}
1577 1604
1578#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1605#endif /* CONFIG_MTD_UBI_DEBUG */