scan.c 34 KB

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