scan.c 34 KB

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