scan.c 33 KB

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