scan.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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 <linux/math64.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 && err != -EBADMSG)
  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))
  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 than 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. si->mean_ec = div_u64(si->ec_sum, si->ec_count);
  788. if (si->is_empty)
  789. ubi_msg("empty MTD device detected");
  790. /*
  791. * In case of unknown erase counter we use the mean erase counter
  792. * value.
  793. */
  794. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  795. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  796. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  797. seb->ec = si->mean_ec;
  798. }
  799. list_for_each_entry(seb, &si->free, u.list) {
  800. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  801. seb->ec = si->mean_ec;
  802. }
  803. list_for_each_entry(seb, &si->corr, u.list)
  804. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  805. seb->ec = si->mean_ec;
  806. list_for_each_entry(seb, &si->erase, u.list)
  807. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  808. seb->ec = si->mean_ec;
  809. err = paranoid_check_si(ubi, si);
  810. if (err) {
  811. if (err > 0)
  812. err = -EINVAL;
  813. goto out_vidh;
  814. }
  815. ubi_free_vid_hdr(ubi, vidh);
  816. kfree(ech);
  817. return si;
  818. out_vidh:
  819. ubi_free_vid_hdr(ubi, vidh);
  820. out_ech:
  821. kfree(ech);
  822. out_si:
  823. ubi_scan_destroy_si(si);
  824. return ERR_PTR(err);
  825. }
  826. /**
  827. * destroy_sv - free the scanning volume information
  828. * @sv: scanning volume information
  829. *
  830. * This function destroys the volume RB-tree (@sv->root) and the scanning
  831. * volume information.
  832. */
  833. static void destroy_sv(struct ubi_scan_volume *sv)
  834. {
  835. struct ubi_scan_leb *seb;
  836. struct rb_node *this = sv->root.rb_node;
  837. while (this) {
  838. if (this->rb_left)
  839. this = this->rb_left;
  840. else if (this->rb_right)
  841. this = this->rb_right;
  842. else {
  843. seb = rb_entry(this, struct ubi_scan_leb, u.rb);
  844. this = rb_parent(this);
  845. if (this) {
  846. if (this->rb_left == &seb->u.rb)
  847. this->rb_left = NULL;
  848. else
  849. this->rb_right = NULL;
  850. }
  851. kfree(seb);
  852. }
  853. }
  854. kfree(sv);
  855. }
  856. /**
  857. * ubi_scan_destroy_si - destroy scanning information.
  858. * @si: scanning information
  859. */
  860. void ubi_scan_destroy_si(struct ubi_scan_info *si)
  861. {
  862. struct ubi_scan_leb *seb, *seb_tmp;
  863. struct ubi_scan_volume *sv;
  864. struct rb_node *rb;
  865. list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
  866. list_del(&seb->u.list);
  867. kfree(seb);
  868. }
  869. list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
  870. list_del(&seb->u.list);
  871. kfree(seb);
  872. }
  873. list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
  874. list_del(&seb->u.list);
  875. kfree(seb);
  876. }
  877. list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
  878. list_del(&seb->u.list);
  879. kfree(seb);
  880. }
  881. /* Destroy the volume RB-tree */
  882. rb = si->volumes.rb_node;
  883. while (rb) {
  884. if (rb->rb_left)
  885. rb = rb->rb_left;
  886. else if (rb->rb_right)
  887. rb = rb->rb_right;
  888. else {
  889. sv = rb_entry(rb, struct ubi_scan_volume, rb);
  890. rb = rb_parent(rb);
  891. if (rb) {
  892. if (rb->rb_left == &sv->rb)
  893. rb->rb_left = NULL;
  894. else
  895. rb->rb_right = NULL;
  896. }
  897. destroy_sv(sv);
  898. }
  899. }
  900. kfree(si);
  901. }
  902. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  903. /**
  904. * paranoid_check_si - check the scanning information.
  905. * @ubi: UBI device description object
  906. * @si: scanning information
  907. *
  908. * This function returns zero if the scanning information is all right, %1 if
  909. * not and a negative error code if an error occurred.
  910. */
  911. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
  912. {
  913. int pnum, err, vols_found = 0;
  914. struct rb_node *rb1, *rb2;
  915. struct ubi_scan_volume *sv;
  916. struct ubi_scan_leb *seb, *last_seb;
  917. uint8_t *buf;
  918. /*
  919. * At first, check that scanning information is OK.
  920. */
  921. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  922. int leb_count = 0;
  923. cond_resched();
  924. vols_found += 1;
  925. if (si->is_empty) {
  926. ubi_err("bad is_empty flag");
  927. goto bad_sv;
  928. }
  929. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  930. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  931. sv->data_pad < 0 || sv->last_data_size < 0) {
  932. ubi_err("negative values");
  933. goto bad_sv;
  934. }
  935. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  936. sv->vol_id < UBI_INTERNAL_VOL_START) {
  937. ubi_err("bad vol_id");
  938. goto bad_sv;
  939. }
  940. if (sv->vol_id > si->highest_vol_id) {
  941. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  942. si->highest_vol_id, sv->vol_id);
  943. goto out;
  944. }
  945. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  946. sv->vol_type != UBI_STATIC_VOLUME) {
  947. ubi_err("bad vol_type");
  948. goto bad_sv;
  949. }
  950. if (sv->data_pad > ubi->leb_size / 2) {
  951. ubi_err("bad data_pad");
  952. goto bad_sv;
  953. }
  954. last_seb = NULL;
  955. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  956. cond_resched();
  957. last_seb = seb;
  958. leb_count += 1;
  959. if (seb->pnum < 0 || seb->ec < 0) {
  960. ubi_err("negative values");
  961. goto bad_seb;
  962. }
  963. if (seb->ec < si->min_ec) {
  964. ubi_err("bad si->min_ec (%d), %d found",
  965. si->min_ec, seb->ec);
  966. goto bad_seb;
  967. }
  968. if (seb->ec > si->max_ec) {
  969. ubi_err("bad si->max_ec (%d), %d found",
  970. si->max_ec, seb->ec);
  971. goto bad_seb;
  972. }
  973. if (seb->pnum >= ubi->peb_count) {
  974. ubi_err("too high PEB number %d, total PEBs %d",
  975. seb->pnum, ubi->peb_count);
  976. goto bad_seb;
  977. }
  978. if (sv->vol_type == UBI_STATIC_VOLUME) {
  979. if (seb->lnum >= sv->used_ebs) {
  980. ubi_err("bad lnum or used_ebs");
  981. goto bad_seb;
  982. }
  983. } else {
  984. if (sv->used_ebs != 0) {
  985. ubi_err("non-zero used_ebs");
  986. goto bad_seb;
  987. }
  988. }
  989. if (seb->lnum > sv->highest_lnum) {
  990. ubi_err("incorrect highest_lnum or lnum");
  991. goto bad_seb;
  992. }
  993. }
  994. if (sv->leb_count != leb_count) {
  995. ubi_err("bad leb_count, %d objects in the tree",
  996. leb_count);
  997. goto bad_sv;
  998. }
  999. if (!last_seb)
  1000. continue;
  1001. seb = last_seb;
  1002. if (seb->lnum != sv->highest_lnum) {
  1003. ubi_err("bad highest_lnum");
  1004. goto bad_seb;
  1005. }
  1006. }
  1007. if (vols_found != si->vols_found) {
  1008. ubi_err("bad si->vols_found %d, should be %d",
  1009. si->vols_found, vols_found);
  1010. goto out;
  1011. }
  1012. /* Check that scanning information is correct */
  1013. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1014. last_seb = NULL;
  1015. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1016. int vol_type;
  1017. cond_resched();
  1018. last_seb = seb;
  1019. err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
  1020. if (err && err != UBI_IO_BITFLIPS) {
  1021. ubi_err("VID header is not OK (%d)", err);
  1022. if (err > 0)
  1023. err = -EIO;
  1024. return err;
  1025. }
  1026. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1027. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1028. if (sv->vol_type != vol_type) {
  1029. ubi_err("bad vol_type");
  1030. goto bad_vid_hdr;
  1031. }
  1032. if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1033. ubi_err("bad sqnum %llu", seb->sqnum);
  1034. goto bad_vid_hdr;
  1035. }
  1036. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1037. ubi_err("bad vol_id %d", sv->vol_id);
  1038. goto bad_vid_hdr;
  1039. }
  1040. if (sv->compat != vidh->compat) {
  1041. ubi_err("bad compat %d", vidh->compat);
  1042. goto bad_vid_hdr;
  1043. }
  1044. if (seb->lnum != be32_to_cpu(vidh->lnum)) {
  1045. ubi_err("bad lnum %d", seb->lnum);
  1046. goto bad_vid_hdr;
  1047. }
  1048. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1049. ubi_err("bad used_ebs %d", sv->used_ebs);
  1050. goto bad_vid_hdr;
  1051. }
  1052. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1053. ubi_err("bad data_pad %d", sv->data_pad);
  1054. goto bad_vid_hdr;
  1055. }
  1056. }
  1057. if (!last_seb)
  1058. continue;
  1059. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1060. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1061. goto bad_vid_hdr;
  1062. }
  1063. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1064. ubi_err("bad last_data_size %d", sv->last_data_size);
  1065. goto bad_vid_hdr;
  1066. }
  1067. }
  1068. /*
  1069. * Make sure that all the physical eraseblocks are in one of the lists
  1070. * or trees.
  1071. */
  1072. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1073. if (!buf)
  1074. return -ENOMEM;
  1075. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1076. err = ubi_io_is_bad(ubi, pnum);
  1077. if (err < 0) {
  1078. kfree(buf);
  1079. return err;
  1080. } else if (err)
  1081. buf[pnum] = 1;
  1082. }
  1083. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1084. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1085. buf[seb->pnum] = 1;
  1086. list_for_each_entry(seb, &si->free, u.list)
  1087. buf[seb->pnum] = 1;
  1088. list_for_each_entry(seb, &si->corr, u.list)
  1089. buf[seb->pnum] = 1;
  1090. list_for_each_entry(seb, &si->erase, u.list)
  1091. buf[seb->pnum] = 1;
  1092. list_for_each_entry(seb, &si->alien, u.list)
  1093. buf[seb->pnum] = 1;
  1094. err = 0;
  1095. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1096. if (!buf[pnum]) {
  1097. ubi_err("PEB %d is not referred", pnum);
  1098. err = 1;
  1099. }
  1100. kfree(buf);
  1101. if (err)
  1102. goto out;
  1103. return 0;
  1104. bad_seb:
  1105. ubi_err("bad scanning information about LEB %d", seb->lnum);
  1106. ubi_dbg_dump_seb(seb, 0);
  1107. ubi_dbg_dump_sv(sv);
  1108. goto out;
  1109. bad_sv:
  1110. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1111. ubi_dbg_dump_sv(sv);
  1112. goto out;
  1113. bad_vid_hdr:
  1114. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1115. ubi_dbg_dump_sv(sv);
  1116. ubi_dbg_dump_vid_hdr(vidh);
  1117. out:
  1118. ubi_dbg_dump_stack();
  1119. return 1;
  1120. }
  1121. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */