scan.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. * UBI scanning sub-system.
  22. *
  23. * This sub-system is responsible for scanning the flash media, checking UBI
  24. * headers and providing complete information about the UBI flash image.
  25. *
  26. * The scanning information is represented by a &struct ubi_scan_info' object.
  27. * Information about found volumes is represented by &struct ubi_scan_volume
  28. * objects which are kept in volume RB-tree with root at the @volumes field.
  29. * The RB-tree is indexed by the volume ID.
  30. *
  31. * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
  32. * These objects are kept in per-volume RB-trees with the root at the
  33. * corresponding &struct ubi_scan_volume object. To put it differently, we keep
  34. * an RB-tree of per-volume objects and each of these objects is the root of
  35. * RB-tree of per-eraseblock objects.
  36. *
  37. * Corrupted physical eraseblocks are put to the @corr list, free physical
  38. * eraseblocks are put to the @free list and the physical eraseblock to be
  39. * erased are put to the @erase list.
  40. */
  41. #include <linux/err.h>
  42. #include <linux/slab.h>
  43. #include <linux/crc32.h>
  44. #include <linux/math64.h>
  45. #include <linux/random.h>
  46. #include "ubi.h"
  47. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  48. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
  49. #else
  50. #define paranoid_check_si(ubi, si) 0
  51. #endif
  52. /* Temporary variables used during scanning */
  53. static struct ubi_ec_hdr *ech;
  54. static struct ubi_vid_hdr *vidh;
  55. /**
  56. * add_to_list - add physical eraseblock to a list.
  57. * @si: scanning information
  58. * @pnum: physical eraseblock number to add
  59. * @ec: erase counter of the physical eraseblock
  60. * @list: the list to add to
  61. *
  62. * This function adds physical eraseblock @pnum to free, erase, corrupted or
  63. * alien lists. Returns zero in case of success and a negative error code in
  64. * case of failure.
  65. */
  66. static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
  67. struct list_head *list)
  68. {
  69. struct ubi_scan_leb *seb;
  70. if (list == &si->free) {
  71. dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
  72. si->free_peb_count += 1;
  73. } else if (list == &si->erase) {
  74. dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
  75. si->erase_peb_count += 1;
  76. } else if (list == &si->corr) {
  77. dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
  78. si->corr_peb_count += 1;
  79. } else if (list == &si->alien) {
  80. dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
  81. si->alien_peb_count += 1;
  82. } else
  83. BUG();
  84. seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
  85. if (!seb)
  86. return -ENOMEM;
  87. seb->pnum = pnum;
  88. seb->ec = ec;
  89. list_add_tail(&seb->u.list, list);
  90. return 0;
  91. }
  92. /**
  93. * validate_vid_hdr - check volume identifier header.
  94. * @vid_hdr: the volume identifier header to check
  95. * @sv: information about the volume this logical eraseblock belongs to
  96. * @pnum: physical eraseblock number the VID header came from
  97. *
  98. * This function checks that data stored in @vid_hdr is consistent. Returns
  99. * non-zero if an inconsistency was found and zero if not.
  100. *
  101. * Note, UBI does sanity check of everything it reads from the flash media.
  102. * Most of the checks are done in the I/O sub-system. Here we check that the
  103. * information in the VID header is consistent to the information in other VID
  104. * headers of the same volume.
  105. */
  106. static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
  107. const struct ubi_scan_volume *sv, int pnum)
  108. {
  109. int vol_type = vid_hdr->vol_type;
  110. int vol_id = be32_to_cpu(vid_hdr->vol_id);
  111. int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  112. int data_pad = be32_to_cpu(vid_hdr->data_pad);
  113. if (sv->leb_count != 0) {
  114. int sv_vol_type;
  115. /*
  116. * This is not the first logical eraseblock belonging to this
  117. * volume. Ensure that the data in its VID header is consistent
  118. * to the data in previous logical eraseblock headers.
  119. */
  120. if (vol_id != sv->vol_id) {
  121. dbg_err("inconsistent vol_id");
  122. goto bad;
  123. }
  124. if (sv->vol_type == UBI_STATIC_VOLUME)
  125. sv_vol_type = UBI_VID_STATIC;
  126. else
  127. sv_vol_type = UBI_VID_DYNAMIC;
  128. if (vol_type != sv_vol_type) {
  129. dbg_err("inconsistent vol_type");
  130. goto bad;
  131. }
  132. if (used_ebs != sv->used_ebs) {
  133. dbg_err("inconsistent used_ebs");
  134. goto bad;
  135. }
  136. if (data_pad != sv->data_pad) {
  137. dbg_err("inconsistent data_pad");
  138. goto bad;
  139. }
  140. }
  141. return 0;
  142. bad:
  143. ubi_err("inconsistent VID header at PEB %d", pnum);
  144. ubi_dbg_dump_vid_hdr(vid_hdr);
  145. ubi_dbg_dump_sv(sv);
  146. return -EINVAL;
  147. }
  148. /**
  149. * add_volume - add volume to the scanning information.
  150. * @si: scanning information
  151. * @vol_id: ID of the volume to add
  152. * @pnum: physical eraseblock number
  153. * @vid_hdr: volume identifier header
  154. *
  155. * If the volume corresponding to the @vid_hdr logical eraseblock is already
  156. * present in the scanning information, this function does nothing. Otherwise
  157. * it adds corresponding volume to the scanning information. Returns a pointer
  158. * to the scanning volume object in case of success and a negative error code
  159. * in case of failure.
  160. */
  161. static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
  162. int pnum,
  163. const struct ubi_vid_hdr *vid_hdr)
  164. {
  165. struct ubi_scan_volume *sv;
  166. struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
  167. ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
  168. /* Walk the volume RB-tree to look if this volume is already present */
  169. while (*p) {
  170. parent = *p;
  171. sv = rb_entry(parent, struct ubi_scan_volume, rb);
  172. if (vol_id == sv->vol_id)
  173. return sv;
  174. if (vol_id > sv->vol_id)
  175. p = &(*p)->rb_left;
  176. else
  177. p = &(*p)->rb_right;
  178. }
  179. /* The volume is absent - add it */
  180. sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
  181. if (!sv)
  182. return ERR_PTR(-ENOMEM);
  183. sv->highest_lnum = sv->leb_count = 0;
  184. sv->vol_id = vol_id;
  185. sv->root = RB_ROOT;
  186. sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  187. sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
  188. sv->compat = vid_hdr->compat;
  189. sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
  190. : UBI_STATIC_VOLUME;
  191. if (vol_id > si->highest_vol_id)
  192. si->highest_vol_id = vol_id;
  193. rb_link_node(&sv->rb, parent, p);
  194. rb_insert_color(&sv->rb, &si->volumes);
  195. si->vols_found += 1;
  196. dbg_bld("added volume %d", vol_id);
  197. return sv;
  198. }
  199. /**
  200. * compare_lebs - find out which logical eraseblock is newer.
  201. * @ubi: UBI device description object
  202. * @seb: first logical eraseblock to compare
  203. * @pnum: physical eraseblock number of the second logical eraseblock to
  204. * compare
  205. * @vid_hdr: volume identifier header of the second logical eraseblock
  206. *
  207. * This function compares 2 copies of a LEB and informs which one is newer. In
  208. * case of success this function returns a positive value, in case of failure, a
  209. * negative error code is returned. The success return codes use the following
  210. * bits:
  211. * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
  212. * second PEB (described by @pnum and @vid_hdr);
  213. * o bit 0 is set: the second PEB is newer;
  214. * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
  215. * o bit 1 is set: bit-flips were detected in the newer LEB;
  216. * o bit 2 is cleared: the older LEB is not corrupted;
  217. * o bit 2 is set: the older LEB is corrupted.
  218. */
  219. static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
  220. int pnum, const struct ubi_vid_hdr *vid_hdr)
  221. {
  222. void *buf;
  223. int len, err, second_is_newer, bitflips = 0, corrupted = 0;
  224. uint32_t data_crc, crc;
  225. struct ubi_vid_hdr *vh = NULL;
  226. unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
  227. if (sqnum2 == seb->sqnum) {
  228. /*
  229. * This must be a really ancient UBI image which has been
  230. * created before sequence numbers support has been added. At
  231. * that times we used 32-bit LEB versions stored in logical
  232. * eraseblocks. That was before UBI got into mainline. We do not
  233. * support these images anymore. Well, those images will work
  234. * still work, but only if no unclean reboots happened.
  235. */
  236. ubi_err("unsupported on-flash UBI format\n");
  237. return -EINVAL;
  238. }
  239. /* Obviously the LEB with lower sequence counter is older */
  240. second_is_newer = !!(sqnum2 > seb->sqnum);
  241. /*
  242. * Now we know which copy is newer. If the copy flag of the PEB with
  243. * newer version is not set, then we just return, otherwise we have to
  244. * check data CRC. For the second PEB we already have the VID header,
  245. * for the first one - we'll need to re-read it from flash.
  246. *
  247. * Note: this may be optimized so that we wouldn't read twice.
  248. */
  249. if (second_is_newer) {
  250. if (!vid_hdr->copy_flag) {
  251. /* It is not a copy, so it is newer */
  252. dbg_bld("second PEB %d is newer, copy_flag is unset",
  253. pnum);
  254. return 1;
  255. }
  256. } else {
  257. pnum = seb->pnum;
  258. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  259. if (!vh)
  260. return -ENOMEM;
  261. err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  262. if (err) {
  263. if (err == UBI_IO_BITFLIPS)
  264. bitflips = 1;
  265. else {
  266. dbg_err("VID of PEB %d header is bad, but it "
  267. "was OK earlier", pnum);
  268. if (err > 0)
  269. err = -EIO;
  270. goto out_free_vidh;
  271. }
  272. }
  273. if (!vh->copy_flag) {
  274. /* It is not a copy, so it is newer */
  275. dbg_bld("first PEB %d is newer, copy_flag is unset",
  276. pnum);
  277. err = bitflips << 1;
  278. goto out_free_vidh;
  279. }
  280. vid_hdr = vh;
  281. }
  282. /* Read the data of the copy and check the CRC */
  283. len = be32_to_cpu(vid_hdr->data_size);
  284. buf = vmalloc(len);
  285. if (!buf) {
  286. err = -ENOMEM;
  287. goto out_free_vidh;
  288. }
  289. err = ubi_io_read_data(ubi, buf, pnum, 0, len);
  290. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  291. goto out_free_buf;
  292. data_crc = be32_to_cpu(vid_hdr->data_crc);
  293. crc = crc32(UBI_CRC32_INIT, buf, len);
  294. if (crc != data_crc) {
  295. dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
  296. pnum, crc, data_crc);
  297. corrupted = 1;
  298. bitflips = 0;
  299. second_is_newer = !second_is_newer;
  300. } else {
  301. dbg_bld("PEB %d CRC is OK", pnum);
  302. bitflips = !!err;
  303. }
  304. vfree(buf);
  305. ubi_free_vid_hdr(ubi, vh);
  306. if (second_is_newer)
  307. dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
  308. else
  309. dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
  310. return second_is_newer | (bitflips << 1) | (corrupted << 2);
  311. out_free_buf:
  312. vfree(buf);
  313. out_free_vidh:
  314. ubi_free_vid_hdr(ubi, vh);
  315. return err;
  316. }
  317. /**
  318. * ubi_scan_add_used - add physical eraseblock to the scanning information.
  319. * @ubi: UBI device description object
  320. * @si: scanning information
  321. * @pnum: the physical eraseblock number
  322. * @ec: erase counter
  323. * @vid_hdr: the volume identifier header
  324. * @bitflips: if bit-flips were detected when this physical eraseblock was read
  325. *
  326. * This function adds information about a used physical eraseblock to the
  327. * 'used' tree of the corresponding volume. The function is rather complex
  328. * because it has to handle cases when this is not the first physical
  329. * eraseblock belonging to the same logical eraseblock, and the newer one has
  330. * to be picked, while the older one has to be dropped. This function returns
  331. * zero in case of success and a negative error code in case of failure.
  332. */
  333. int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
  334. int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
  335. int bitflips)
  336. {
  337. int err, vol_id, lnum;
  338. unsigned long long sqnum;
  339. struct ubi_scan_volume *sv;
  340. struct ubi_scan_leb *seb;
  341. struct rb_node **p, *parent = NULL;
  342. vol_id = be32_to_cpu(vid_hdr->vol_id);
  343. lnum = be32_to_cpu(vid_hdr->lnum);
  344. sqnum = be64_to_cpu(vid_hdr->sqnum);
  345. dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
  346. pnum, vol_id, lnum, ec, sqnum, bitflips);
  347. sv = add_volume(si, vol_id, pnum, vid_hdr);
  348. if (IS_ERR(sv))
  349. return PTR_ERR(sv);
  350. if (si->max_sqnum < sqnum)
  351. si->max_sqnum = sqnum;
  352. /*
  353. * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
  354. * if this is the first instance of this logical eraseblock or not.
  355. */
  356. p = &sv->root.rb_node;
  357. while (*p) {
  358. int cmp_res;
  359. parent = *p;
  360. seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
  361. if (lnum != seb->lnum) {
  362. if (lnum < seb->lnum)
  363. p = &(*p)->rb_left;
  364. else
  365. p = &(*p)->rb_right;
  366. continue;
  367. }
  368. /*
  369. * There is already a physical eraseblock describing the same
  370. * logical eraseblock present.
  371. */
  372. dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
  373. "EC %d", seb->pnum, seb->sqnum, seb->ec);
  374. /*
  375. * Make sure that the logical eraseblocks have different
  376. * sequence numbers. Otherwise the image is bad.
  377. *
  378. * However, if the sequence number is zero, we assume it must
  379. * be an ancient UBI image from the era when UBI did not have
  380. * sequence numbers. We still can attach these images, unless
  381. * there is a need to distinguish between old and new
  382. * eraseblocks, in which case we'll refuse the image in
  383. * 'compare_lebs()'. In other words, we attach old clean
  384. * images, but refuse attaching old images with duplicated
  385. * logical eraseblocks because there was an unclean reboot.
  386. */
  387. if (seb->sqnum == sqnum && sqnum != 0) {
  388. ubi_err("two LEBs with same sequence number %llu",
  389. sqnum);
  390. ubi_dbg_dump_seb(seb, 0);
  391. ubi_dbg_dump_vid_hdr(vid_hdr);
  392. return -EINVAL;
  393. }
  394. /*
  395. * Now we have to drop the older one and preserve the newer
  396. * one.
  397. */
  398. cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
  399. if (cmp_res < 0)
  400. return cmp_res;
  401. if (cmp_res & 1) {
  402. /*
  403. * This logical eraseblock is newer than the one
  404. * found earlier.
  405. */
  406. err = validate_vid_hdr(vid_hdr, sv, pnum);
  407. if (err)
  408. return err;
  409. if (cmp_res & 4)
  410. err = add_to_list(si, seb->pnum, seb->ec,
  411. &si->corr);
  412. else
  413. err = add_to_list(si, seb->pnum, seb->ec,
  414. &si->erase);
  415. if (err)
  416. return err;
  417. seb->ec = ec;
  418. seb->pnum = pnum;
  419. seb->scrub = ((cmp_res & 2) || bitflips);
  420. seb->sqnum = sqnum;
  421. if (sv->highest_lnum == lnum)
  422. sv->last_data_size =
  423. be32_to_cpu(vid_hdr->data_size);
  424. return 0;
  425. } else {
  426. /*
  427. * This logical eraseblock is older than the one found
  428. * previously.
  429. */
  430. if (cmp_res & 4)
  431. return add_to_list(si, pnum, ec, &si->corr);
  432. else
  433. return add_to_list(si, pnum, ec, &si->erase);
  434. }
  435. }
  436. /*
  437. * We've met this logical eraseblock for the first time, add it to the
  438. * scanning information.
  439. */
  440. err = validate_vid_hdr(vid_hdr, sv, pnum);
  441. if (err)
  442. return err;
  443. seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
  444. if (!seb)
  445. return -ENOMEM;
  446. seb->ec = ec;
  447. seb->pnum = pnum;
  448. seb->lnum = lnum;
  449. seb->sqnum = sqnum;
  450. seb->scrub = bitflips;
  451. if (sv->highest_lnum <= lnum) {
  452. sv->highest_lnum = lnum;
  453. sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
  454. }
  455. sv->leb_count += 1;
  456. rb_link_node(&seb->u.rb, parent, p);
  457. rb_insert_color(&seb->u.rb, &sv->root);
  458. si->used_peb_count += 1;
  459. return 0;
  460. }
  461. /**
  462. * ubi_scan_find_sv - find volume in the scanning information.
  463. * @si: scanning information
  464. * @vol_id: the requested volume ID
  465. *
  466. * This function returns a pointer to the volume description or %NULL if there
  467. * are no data about this volume in the scanning information.
  468. */
  469. struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
  470. int vol_id)
  471. {
  472. struct ubi_scan_volume *sv;
  473. struct rb_node *p = si->volumes.rb_node;
  474. while (p) {
  475. sv = rb_entry(p, struct ubi_scan_volume, rb);
  476. if (vol_id == sv->vol_id)
  477. return sv;
  478. if (vol_id > sv->vol_id)
  479. p = p->rb_left;
  480. else
  481. p = p->rb_right;
  482. }
  483. return NULL;
  484. }
  485. /**
  486. * ubi_scan_find_seb - find LEB in the volume scanning information.
  487. * @sv: a pointer to the volume scanning information
  488. * @lnum: the requested logical eraseblock
  489. *
  490. * This function returns a pointer to the scanning logical eraseblock or %NULL
  491. * if there are no data about it in the scanning volume information.
  492. */
  493. struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
  494. int lnum)
  495. {
  496. struct ubi_scan_leb *seb;
  497. struct rb_node *p = sv->root.rb_node;
  498. while (p) {
  499. seb = rb_entry(p, struct ubi_scan_leb, u.rb);
  500. if (lnum == seb->lnum)
  501. return seb;
  502. if (lnum > seb->lnum)
  503. p = p->rb_left;
  504. else
  505. p = p->rb_right;
  506. }
  507. return NULL;
  508. }
  509. /**
  510. * ubi_scan_rm_volume - delete scanning information about a volume.
  511. * @si: scanning information
  512. * @sv: the volume scanning information to delete
  513. */
  514. void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
  515. {
  516. struct rb_node *rb;
  517. struct ubi_scan_leb *seb;
  518. dbg_bld("remove scanning information about volume %d", sv->vol_id);
  519. while ((rb = rb_first(&sv->root))) {
  520. seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
  521. rb_erase(&seb->u.rb, &sv->root);
  522. list_add_tail(&seb->u.list, &si->erase);
  523. }
  524. rb_erase(&sv->rb, &si->volumes);
  525. kfree(sv);
  526. si->vols_found -= 1;
  527. }
  528. /**
  529. * ubi_scan_erase_peb - erase a physical eraseblock.
  530. * @ubi: UBI device description object
  531. * @si: scanning information
  532. * @pnum: physical eraseblock number to erase;
  533. * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
  534. *
  535. * This function erases physical eraseblock 'pnum', and writes the erase
  536. * counter header to it. This function should only be used on UBI device
  537. * initialization stages, when the EBA sub-system had not been yet initialized.
  538. * This function returns zero in case of success and a negative error code in
  539. * case of failure.
  540. */
  541. int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
  542. int pnum, int ec)
  543. {
  544. int err;
  545. struct ubi_ec_hdr *ec_hdr;
  546. if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
  547. /*
  548. * Erase counter overflow. Upgrade UBI and use 64-bit
  549. * erase counters internally.
  550. */
  551. ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
  552. return -EINVAL;
  553. }
  554. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  555. if (!ec_hdr)
  556. return -ENOMEM;
  557. ec_hdr->ec = cpu_to_be64(ec);
  558. err = ubi_io_sync_erase(ubi, pnum, 0);
  559. if (err < 0)
  560. goto out_free;
  561. err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
  562. out_free:
  563. kfree(ec_hdr);
  564. return err;
  565. }
  566. /**
  567. * ubi_scan_get_free_peb - get a free physical eraseblock.
  568. * @ubi: UBI device description object
  569. * @si: scanning information
  570. *
  571. * This function returns a free physical eraseblock. It is supposed to be
  572. * called on the UBI initialization stages when the wear-leveling sub-system is
  573. * not initialized yet. This function picks a physical eraseblocks from one of
  574. * the lists, writes the EC header if it is needed, and removes it from the
  575. * list.
  576. *
  577. * This function returns scanning physical eraseblock information in case of
  578. * success and an error code in case of failure.
  579. */
  580. struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
  581. struct ubi_scan_info *si)
  582. {
  583. int err = 0, i;
  584. struct ubi_scan_leb *seb;
  585. if (!list_empty(&si->free)) {
  586. seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
  587. list_del(&seb->u.list);
  588. dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
  589. return seb;
  590. }
  591. for (i = 0; i < 2; i++) {
  592. struct list_head *head;
  593. struct ubi_scan_leb *tmp_seb;
  594. if (i == 0)
  595. head = &si->erase;
  596. else
  597. head = &si->corr;
  598. /*
  599. * We try to erase the first physical eraseblock from the @head
  600. * list and pick it if we succeed, or try to erase the
  601. * next one if not. And so forth. We don't want to take care
  602. * about bad eraseblocks here - they'll be handled later.
  603. */
  604. list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
  605. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  606. seb->ec = si->mean_ec;
  607. err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
  608. if (err)
  609. continue;
  610. seb->ec += 1;
  611. list_del(&seb->u.list);
  612. dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
  613. return seb;
  614. }
  615. }
  616. ubi_err("no eraseblocks found");
  617. return ERR_PTR(-ENOSPC);
  618. }
  619. /**
  620. * process_eb - read, check UBI headers, and add them to scanning information.
  621. * @ubi: UBI device description object
  622. * @si: scanning information
  623. * @pnum: the physical eraseblock number
  624. *
  625. * This function returns a zero if the physical eraseblock was successfully
  626. * handled and a negative error code in case of failure.
  627. */
  628. static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
  629. int pnum)
  630. {
  631. long long uninitialized_var(ec);
  632. int err, bitflips = 0, vol_id, ec_err = 0;
  633. dbg_bld("scan PEB %d", pnum);
  634. /* Skip bad physical eraseblocks */
  635. err = ubi_io_is_bad(ubi, pnum);
  636. if (err < 0)
  637. return err;
  638. else if (err) {
  639. /*
  640. * FIXME: this is actually duty of the I/O sub-system to
  641. * initialize this, but MTD does not provide enough
  642. * information.
  643. */
  644. si->bad_peb_count += 1;
  645. return 0;
  646. }
  647. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  648. if (err < 0)
  649. return err;
  650. switch (err) {
  651. case 0:
  652. break;
  653. case UBI_IO_BITFLIPS:
  654. bitflips = 1;
  655. break;
  656. case UBI_IO_FF:
  657. case UBI_IO_FF_BITFLIPS:
  658. return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
  659. case UBI_IO_BAD_HDR_EBADMSG:
  660. si->read_err_count += 1;
  661. case UBI_IO_BAD_HDR:
  662. /*
  663. * We have to also look at the VID header, possibly it is not
  664. * corrupted. Set %bitflips flag in order to make this PEB be
  665. * moved and EC be re-created.
  666. */
  667. ec_err = err;
  668. ec = UBI_SCAN_UNKNOWN_EC;
  669. bitflips = 1;
  670. break;
  671. default:
  672. ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
  673. return -EINVAL;
  674. }
  675. if (!ec_err) {
  676. int image_seq;
  677. /* Make sure UBI version is OK */
  678. if (ech->version != UBI_VERSION) {
  679. ubi_err("this UBI version is %d, image version is %d",
  680. UBI_VERSION, (int)ech->version);
  681. return -EINVAL;
  682. }
  683. ec = be64_to_cpu(ech->ec);
  684. if (ec > UBI_MAX_ERASECOUNTER) {
  685. /*
  686. * Erase counter overflow. The EC headers have 64 bits
  687. * reserved, but we anyway make use of only 31 bit
  688. * values, as this seems to be enough for any existing
  689. * flash. Upgrade UBI and use 64-bit erase counters
  690. * internally.
  691. */
  692. ubi_err("erase counter overflow, max is %d",
  693. UBI_MAX_ERASECOUNTER);
  694. ubi_dbg_dump_ec_hdr(ech);
  695. return -EINVAL;
  696. }
  697. /*
  698. * Make sure that all PEBs have the same image sequence number.
  699. * This allows us to detect situations when users flash UBI
  700. * images incorrectly, so that the flash has the new UBI image
  701. * and leftovers from the old one. This feature was added
  702. * relatively recently, and the sequence number was always
  703. * zero, because old UBI implementations always set it to zero.
  704. * For this reasons, we do not panic if some PEBs have zero
  705. * sequence number, while other PEBs have non-zero sequence
  706. * number.
  707. */
  708. image_seq = be32_to_cpu(ech->image_seq);
  709. if (!ubi->image_seq && image_seq)
  710. ubi->image_seq = image_seq;
  711. if (ubi->image_seq && image_seq &&
  712. ubi->image_seq != image_seq) {
  713. ubi_err("bad image sequence number %d in PEB %d, "
  714. "expected %d", image_seq, pnum, ubi->image_seq);
  715. ubi_dbg_dump_ec_hdr(ech);
  716. return -EINVAL;
  717. }
  718. }
  719. /* OK, we've done with the EC header, let's look at the VID header */
  720. err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
  721. if (err < 0)
  722. return err;
  723. switch (err) {
  724. case 0:
  725. break;
  726. case UBI_IO_BITFLIPS:
  727. bitflips = 1;
  728. break;
  729. case UBI_IO_BAD_HDR_EBADMSG:
  730. si->read_err_count += 1;
  731. case UBI_IO_BAD_HDR:
  732. case UBI_IO_FF_BITFLIPS:
  733. err = add_to_list(si, pnum, ec, &si->corr);
  734. if (err)
  735. return err;
  736. goto adjust_mean_ec;
  737. case UBI_IO_FF:
  738. if (ec_err)
  739. err = add_to_list(si, pnum, ec, &si->corr);
  740. else
  741. err = add_to_list(si, pnum, ec, &si->free);
  742. if (err)
  743. return err;
  744. goto adjust_mean_ec;
  745. default:
  746. ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
  747. err);
  748. return -EINVAL;
  749. }
  750. vol_id = be32_to_cpu(vidh->vol_id);
  751. if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
  752. int lnum = be32_to_cpu(vidh->lnum);
  753. /* Unsupported internal volume */
  754. switch (vidh->compat) {
  755. case UBI_COMPAT_DELETE:
  756. ubi_msg("\"delete\" compatible internal volume %d:%d"
  757. " found, will remove it", vol_id, lnum);
  758. err = add_to_list(si, pnum, ec, &si->erase);
  759. if (err)
  760. return err;
  761. return 0;
  762. case UBI_COMPAT_RO:
  763. ubi_msg("read-only compatible internal volume %d:%d"
  764. " found, switch to read-only mode",
  765. vol_id, lnum);
  766. ubi->ro_mode = 1;
  767. break;
  768. case UBI_COMPAT_PRESERVE:
  769. ubi_msg("\"preserve\" compatible internal volume %d:%d"
  770. " found", vol_id, lnum);
  771. err = add_to_list(si, pnum, ec, &si->alien);
  772. if (err)
  773. return err;
  774. return 0;
  775. case UBI_COMPAT_REJECT:
  776. ubi_err("incompatible internal volume %d:%d found",
  777. vol_id, lnum);
  778. return -EINVAL;
  779. }
  780. }
  781. if (ec_err)
  782. ubi_warn("valid VID header but corrupted EC header at PEB %d",
  783. pnum);
  784. err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
  785. if (err)
  786. return err;
  787. adjust_mean_ec:
  788. if (!ec_err) {
  789. si->ec_sum += ec;
  790. si->ec_count += 1;
  791. if (ec > si->max_ec)
  792. si->max_ec = ec;
  793. if (ec < si->min_ec)
  794. si->min_ec = ec;
  795. }
  796. return 0;
  797. }
  798. /**
  799. * check_what_we_have - check what PEB were found by scanning.
  800. * @ubi: UBI device description object
  801. * @si: scanning information
  802. *
  803. * This is a helper function which takes a look what PEBs were found by
  804. * scanning, and decides whether the flash is empty and should be formatted and
  805. * whether there are too many corrupted PEBs and we should not attach this
  806. * MTD device. Returns zero if we should proceed with attaching the MTD device,
  807. * and %-EINVAL if we should not.
  808. */
  809. static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
  810. {
  811. struct ubi_scan_leb *seb;
  812. int max_corr;
  813. max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
  814. max_corr = max_corr / 20 ?: 8;
  815. /*
  816. * Few corrupted PEBs are not a problem and may be just a result of
  817. * unclean reboots. However, many of them may indicate some problems
  818. * with the flash HW or driver.
  819. */
  820. if (si->corr_peb_count >= 8) {
  821. ubi_warn("%d PEBs are corrupted", si->corr_peb_count);
  822. printk(KERN_WARNING "corrupted PEBs are:");
  823. list_for_each_entry(seb, &si->corr, u.list)
  824. printk(KERN_CONT " %d", seb->pnum);
  825. printk(KERN_CONT "\n");
  826. /*
  827. * If too many PEBs are corrupted, we refuse attaching,
  828. * otherwise, only print a warning.
  829. */
  830. if (si->corr_peb_count >= max_corr) {
  831. ubi_err("too many corrupted PEBs, refusing this device");
  832. return -EINVAL;
  833. }
  834. }
  835. if (si->free_peb_count + si->used_peb_count +
  836. si->alien_peb_count == 0) {
  837. /* No UBI-formatted eraseblocks were found */
  838. if (si->corr_peb_count == si->read_err_count &&
  839. si->corr_peb_count < 8) {
  840. /* No or just few corrupted PEBs, and all of them had a
  841. * read error. We assume that those are bad PEBs, which
  842. * were just not marked as bad so far.
  843. *
  844. * This piece of code basically tries to distinguish
  845. * between the following 2 situations:
  846. *
  847. * 1. Flash is empty, but there are few bad PEBs, which
  848. * are not marked as bad so far, and which were read
  849. * with error. We want to go ahead and format this
  850. * flash. While formating, the faulty PEBs will
  851. * probably be marked as bad.
  852. *
  853. * 2. Flash probably contains non-UBI data and we do
  854. * not want to format it and destroy possibly needed
  855. * data (e.g., consider the case when the bootloader
  856. * MTD partition was accidentally fed to UBI).
  857. */
  858. si->is_empty = 1;
  859. ubi_msg("empty MTD device detected");
  860. get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq));
  861. } else {
  862. ubi_err("MTD device possibly contains non-UBI data, "
  863. "refusing it");
  864. return -EINVAL;
  865. }
  866. }
  867. if (si->corr_peb_count > 0)
  868. ubi_msg("corrupted PEBs will be formatted");
  869. return 0;
  870. }
  871. /**
  872. * ubi_scan - scan an MTD device.
  873. * @ubi: UBI device description object
  874. *
  875. * This function does full scanning of an MTD device and returns complete
  876. * information about it. In case of failure, an error code is returned.
  877. */
  878. struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
  879. {
  880. int err, pnum;
  881. struct rb_node *rb1, *rb2;
  882. struct ubi_scan_volume *sv;
  883. struct ubi_scan_leb *seb;
  884. struct ubi_scan_info *si;
  885. si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
  886. if (!si)
  887. return ERR_PTR(-ENOMEM);
  888. INIT_LIST_HEAD(&si->corr);
  889. INIT_LIST_HEAD(&si->free);
  890. INIT_LIST_HEAD(&si->erase);
  891. INIT_LIST_HEAD(&si->alien);
  892. si->volumes = RB_ROOT;
  893. err = -ENOMEM;
  894. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  895. if (!ech)
  896. goto out_si;
  897. vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  898. if (!vidh)
  899. goto out_ech;
  900. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  901. cond_resched();
  902. dbg_gen("process PEB %d", pnum);
  903. err = process_eb(ubi, si, pnum);
  904. if (err < 0)
  905. goto out_vidh;
  906. }
  907. dbg_msg("scanning is finished");
  908. /* Calculate mean erase counter */
  909. if (si->ec_count)
  910. si->mean_ec = div_u64(si->ec_sum, si->ec_count);
  911. err = check_what_we_have(ubi, si);
  912. if (err)
  913. goto out_vidh;
  914. /*
  915. * In case of unknown erase counter we use the mean erase counter
  916. * value.
  917. */
  918. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  919. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  920. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  921. seb->ec = si->mean_ec;
  922. }
  923. list_for_each_entry(seb, &si->free, u.list) {
  924. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  925. seb->ec = si->mean_ec;
  926. }
  927. list_for_each_entry(seb, &si->corr, u.list)
  928. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  929. seb->ec = si->mean_ec;
  930. list_for_each_entry(seb, &si->erase, u.list)
  931. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  932. seb->ec = si->mean_ec;
  933. err = paranoid_check_si(ubi, si);
  934. if (err)
  935. goto out_vidh;
  936. ubi_free_vid_hdr(ubi, vidh);
  937. kfree(ech);
  938. return si;
  939. out_vidh:
  940. ubi_free_vid_hdr(ubi, vidh);
  941. out_ech:
  942. kfree(ech);
  943. out_si:
  944. ubi_scan_destroy_si(si);
  945. return ERR_PTR(err);
  946. }
  947. /**
  948. * destroy_sv - free the scanning volume information
  949. * @sv: scanning volume information
  950. *
  951. * This function destroys the volume RB-tree (@sv->root) and the scanning
  952. * volume information.
  953. */
  954. static void destroy_sv(struct ubi_scan_volume *sv)
  955. {
  956. struct ubi_scan_leb *seb;
  957. struct rb_node *this = sv->root.rb_node;
  958. while (this) {
  959. if (this->rb_left)
  960. this = this->rb_left;
  961. else if (this->rb_right)
  962. this = this->rb_right;
  963. else {
  964. seb = rb_entry(this, struct ubi_scan_leb, u.rb);
  965. this = rb_parent(this);
  966. if (this) {
  967. if (this->rb_left == &seb->u.rb)
  968. this->rb_left = NULL;
  969. else
  970. this->rb_right = NULL;
  971. }
  972. kfree(seb);
  973. }
  974. }
  975. kfree(sv);
  976. }
  977. /**
  978. * ubi_scan_destroy_si - destroy scanning information.
  979. * @si: scanning information
  980. */
  981. void ubi_scan_destroy_si(struct ubi_scan_info *si)
  982. {
  983. struct ubi_scan_leb *seb, *seb_tmp;
  984. struct ubi_scan_volume *sv;
  985. struct rb_node *rb;
  986. list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
  987. list_del(&seb->u.list);
  988. kfree(seb);
  989. }
  990. list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
  991. list_del(&seb->u.list);
  992. kfree(seb);
  993. }
  994. list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
  995. list_del(&seb->u.list);
  996. kfree(seb);
  997. }
  998. list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
  999. list_del(&seb->u.list);
  1000. kfree(seb);
  1001. }
  1002. /* Destroy the volume RB-tree */
  1003. rb = si->volumes.rb_node;
  1004. while (rb) {
  1005. if (rb->rb_left)
  1006. rb = rb->rb_left;
  1007. else if (rb->rb_right)
  1008. rb = rb->rb_right;
  1009. else {
  1010. sv = rb_entry(rb, struct ubi_scan_volume, rb);
  1011. rb = rb_parent(rb);
  1012. if (rb) {
  1013. if (rb->rb_left == &sv->rb)
  1014. rb->rb_left = NULL;
  1015. else
  1016. rb->rb_right = NULL;
  1017. }
  1018. destroy_sv(sv);
  1019. }
  1020. }
  1021. kfree(si);
  1022. }
  1023. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  1024. /**
  1025. * paranoid_check_si - check the scanning information.
  1026. * @ubi: UBI device description object
  1027. * @si: scanning information
  1028. *
  1029. * This function returns zero if the scanning information is all right, and a
  1030. * negative error code if not or if an error occurred.
  1031. */
  1032. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
  1033. {
  1034. int pnum, err, vols_found = 0;
  1035. struct rb_node *rb1, *rb2;
  1036. struct ubi_scan_volume *sv;
  1037. struct ubi_scan_leb *seb, *last_seb;
  1038. uint8_t *buf;
  1039. /*
  1040. * At first, check that scanning information is OK.
  1041. */
  1042. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1043. int leb_count = 0;
  1044. cond_resched();
  1045. vols_found += 1;
  1046. if (si->is_empty) {
  1047. ubi_err("bad is_empty flag");
  1048. goto bad_sv;
  1049. }
  1050. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  1051. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  1052. sv->data_pad < 0 || sv->last_data_size < 0) {
  1053. ubi_err("negative values");
  1054. goto bad_sv;
  1055. }
  1056. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  1057. sv->vol_id < UBI_INTERNAL_VOL_START) {
  1058. ubi_err("bad vol_id");
  1059. goto bad_sv;
  1060. }
  1061. if (sv->vol_id > si->highest_vol_id) {
  1062. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  1063. si->highest_vol_id, sv->vol_id);
  1064. goto out;
  1065. }
  1066. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  1067. sv->vol_type != UBI_STATIC_VOLUME) {
  1068. ubi_err("bad vol_type");
  1069. goto bad_sv;
  1070. }
  1071. if (sv->data_pad > ubi->leb_size / 2) {
  1072. ubi_err("bad data_pad");
  1073. goto bad_sv;
  1074. }
  1075. last_seb = NULL;
  1076. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1077. cond_resched();
  1078. last_seb = seb;
  1079. leb_count += 1;
  1080. if (seb->pnum < 0 || seb->ec < 0) {
  1081. ubi_err("negative values");
  1082. goto bad_seb;
  1083. }
  1084. if (seb->ec < si->min_ec) {
  1085. ubi_err("bad si->min_ec (%d), %d found",
  1086. si->min_ec, seb->ec);
  1087. goto bad_seb;
  1088. }
  1089. if (seb->ec > si->max_ec) {
  1090. ubi_err("bad si->max_ec (%d), %d found",
  1091. si->max_ec, seb->ec);
  1092. goto bad_seb;
  1093. }
  1094. if (seb->pnum >= ubi->peb_count) {
  1095. ubi_err("too high PEB number %d, total PEBs %d",
  1096. seb->pnum, ubi->peb_count);
  1097. goto bad_seb;
  1098. }
  1099. if (sv->vol_type == UBI_STATIC_VOLUME) {
  1100. if (seb->lnum >= sv->used_ebs) {
  1101. ubi_err("bad lnum or used_ebs");
  1102. goto bad_seb;
  1103. }
  1104. } else {
  1105. if (sv->used_ebs != 0) {
  1106. ubi_err("non-zero used_ebs");
  1107. goto bad_seb;
  1108. }
  1109. }
  1110. if (seb->lnum > sv->highest_lnum) {
  1111. ubi_err("incorrect highest_lnum or lnum");
  1112. goto bad_seb;
  1113. }
  1114. }
  1115. if (sv->leb_count != leb_count) {
  1116. ubi_err("bad leb_count, %d objects in the tree",
  1117. leb_count);
  1118. goto bad_sv;
  1119. }
  1120. if (!last_seb)
  1121. continue;
  1122. seb = last_seb;
  1123. if (seb->lnum != sv->highest_lnum) {
  1124. ubi_err("bad highest_lnum");
  1125. goto bad_seb;
  1126. }
  1127. }
  1128. if (vols_found != si->vols_found) {
  1129. ubi_err("bad si->vols_found %d, should be %d",
  1130. si->vols_found, vols_found);
  1131. goto out;
  1132. }
  1133. /* Check that scanning information is correct */
  1134. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1135. last_seb = NULL;
  1136. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1137. int vol_type;
  1138. cond_resched();
  1139. last_seb = seb;
  1140. err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
  1141. if (err && err != UBI_IO_BITFLIPS) {
  1142. ubi_err("VID header is not OK (%d)", err);
  1143. if (err > 0)
  1144. err = -EIO;
  1145. return err;
  1146. }
  1147. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1148. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1149. if (sv->vol_type != vol_type) {
  1150. ubi_err("bad vol_type");
  1151. goto bad_vid_hdr;
  1152. }
  1153. if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1154. ubi_err("bad sqnum %llu", seb->sqnum);
  1155. goto bad_vid_hdr;
  1156. }
  1157. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1158. ubi_err("bad vol_id %d", sv->vol_id);
  1159. goto bad_vid_hdr;
  1160. }
  1161. if (sv->compat != vidh->compat) {
  1162. ubi_err("bad compat %d", vidh->compat);
  1163. goto bad_vid_hdr;
  1164. }
  1165. if (seb->lnum != be32_to_cpu(vidh->lnum)) {
  1166. ubi_err("bad lnum %d", seb->lnum);
  1167. goto bad_vid_hdr;
  1168. }
  1169. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1170. ubi_err("bad used_ebs %d", sv->used_ebs);
  1171. goto bad_vid_hdr;
  1172. }
  1173. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1174. ubi_err("bad data_pad %d", sv->data_pad);
  1175. goto bad_vid_hdr;
  1176. }
  1177. }
  1178. if (!last_seb)
  1179. continue;
  1180. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1181. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1182. goto bad_vid_hdr;
  1183. }
  1184. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1185. ubi_err("bad last_data_size %d", sv->last_data_size);
  1186. goto bad_vid_hdr;
  1187. }
  1188. }
  1189. /*
  1190. * Make sure that all the physical eraseblocks are in one of the lists
  1191. * or trees.
  1192. */
  1193. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1194. if (!buf)
  1195. return -ENOMEM;
  1196. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1197. err = ubi_io_is_bad(ubi, pnum);
  1198. if (err < 0) {
  1199. kfree(buf);
  1200. return err;
  1201. } else if (err)
  1202. buf[pnum] = 1;
  1203. }
  1204. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1205. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1206. buf[seb->pnum] = 1;
  1207. list_for_each_entry(seb, &si->free, u.list)
  1208. buf[seb->pnum] = 1;
  1209. list_for_each_entry(seb, &si->corr, u.list)
  1210. buf[seb->pnum] = 1;
  1211. list_for_each_entry(seb, &si->erase, u.list)
  1212. buf[seb->pnum] = 1;
  1213. list_for_each_entry(seb, &si->alien, u.list)
  1214. buf[seb->pnum] = 1;
  1215. err = 0;
  1216. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1217. if (!buf[pnum]) {
  1218. ubi_err("PEB %d is not referred", pnum);
  1219. err = 1;
  1220. }
  1221. kfree(buf);
  1222. if (err)
  1223. goto out;
  1224. return 0;
  1225. bad_seb:
  1226. ubi_err("bad scanning information about LEB %d", seb->lnum);
  1227. ubi_dbg_dump_seb(seb, 0);
  1228. ubi_dbg_dump_sv(sv);
  1229. goto out;
  1230. bad_sv:
  1231. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1232. ubi_dbg_dump_sv(sv);
  1233. goto out;
  1234. bad_vid_hdr:
  1235. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1236. ubi_dbg_dump_sv(sv);
  1237. ubi_dbg_dump_vid_hdr(vidh);
  1238. out:
  1239. ubi_dbg_dump_stack();
  1240. return -EINVAL;
  1241. }
  1242. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */