scan.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. int image_seq;
  660. /* Make sure UBI version is OK */
  661. if (ech->version != UBI_VERSION) {
  662. ubi_err("this UBI version is %d, image version is %d",
  663. UBI_VERSION, (int)ech->version);
  664. return -EINVAL;
  665. }
  666. ec = be64_to_cpu(ech->ec);
  667. if (ec > UBI_MAX_ERASECOUNTER) {
  668. /*
  669. * Erase counter overflow. The EC headers have 64 bits
  670. * reserved, but we anyway make use of only 31 bit
  671. * values, as this seems to be enough for any existing
  672. * flash. Upgrade UBI and use 64-bit erase counters
  673. * internally.
  674. */
  675. ubi_err("erase counter overflow, max is %d",
  676. UBI_MAX_ERASECOUNTER);
  677. ubi_dbg_dump_ec_hdr(ech);
  678. return -EINVAL;
  679. }
  680. /*
  681. * Make sure that all PEBs have the same image sequence number.
  682. * This allows us to detect situations when users flash UBI
  683. * images incorrectly, so that the flash has the new UBI image
  684. * and leftovers from the old one. This feature was added
  685. * relatively recently, and the sequence number was always
  686. * zero, because old UBI implementations always set it to zero.
  687. * For this reasons, we do not panic if some PEBs have zero
  688. * sequence number, while other PEBs have non-zero sequence
  689. * number.
  690. */
  691. image_seq = be32_to_cpu(ech->image_seq);
  692. if (!si->image_seq_set) {
  693. ubi->image_seq = image_seq;
  694. si->image_seq_set = 1;
  695. } else if (ubi->image_seq && ubi->image_seq != image_seq) {
  696. ubi_err("bad image sequence number %d in PEB %d, "
  697. "expected %d", image_seq, pnum, ubi->image_seq);
  698. ubi_dbg_dump_ec_hdr(ech);
  699. return -EINVAL;
  700. }
  701. }
  702. /* OK, we've done with the EC header, let's look at the VID header */
  703. err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
  704. if (err < 0)
  705. return err;
  706. else if (err == UBI_IO_BITFLIPS)
  707. bitflips = 1;
  708. else if (err == UBI_IO_BAD_VID_HDR ||
  709. (err == UBI_IO_PEB_FREE && ec_corr)) {
  710. /* VID header is corrupted */
  711. err = add_to_list(si, pnum, ec, &si->corr);
  712. if (err)
  713. return err;
  714. goto adjust_mean_ec;
  715. } else if (err == UBI_IO_PEB_FREE) {
  716. /* No VID header - the physical eraseblock is free */
  717. err = add_to_list(si, pnum, ec, &si->free);
  718. if (err)
  719. return err;
  720. goto adjust_mean_ec;
  721. }
  722. vol_id = be32_to_cpu(vidh->vol_id);
  723. if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
  724. int lnum = be32_to_cpu(vidh->lnum);
  725. /* Unsupported internal volume */
  726. switch (vidh->compat) {
  727. case UBI_COMPAT_DELETE:
  728. ubi_msg("\"delete\" compatible internal volume %d:%d"
  729. " found, remove it", vol_id, lnum);
  730. err = add_to_list(si, pnum, ec, &si->corr);
  731. if (err)
  732. return err;
  733. break;
  734. case UBI_COMPAT_RO:
  735. ubi_msg("read-only compatible internal volume %d:%d"
  736. " found, switch to read-only mode",
  737. vol_id, lnum);
  738. ubi->ro_mode = 1;
  739. break;
  740. case UBI_COMPAT_PRESERVE:
  741. ubi_msg("\"preserve\" compatible internal volume %d:%d"
  742. " found", vol_id, lnum);
  743. err = add_to_list(si, pnum, ec, &si->alien);
  744. if (err)
  745. return err;
  746. si->alien_peb_count += 1;
  747. return 0;
  748. case UBI_COMPAT_REJECT:
  749. ubi_err("incompatible internal volume %d:%d found",
  750. vol_id, lnum);
  751. return -EINVAL;
  752. }
  753. }
  754. if (ec_corr)
  755. ubi_warn("valid VID header but corrupted EC header at PEB %d",
  756. pnum);
  757. err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
  758. if (err)
  759. return err;
  760. adjust_mean_ec:
  761. if (!ec_corr) {
  762. si->ec_sum += ec;
  763. si->ec_count += 1;
  764. if (ec > si->max_ec)
  765. si->max_ec = ec;
  766. if (ec < si->min_ec)
  767. si->min_ec = ec;
  768. }
  769. return 0;
  770. }
  771. /**
  772. * ubi_scan - scan an MTD device.
  773. * @ubi: UBI device description object
  774. *
  775. * This function does full scanning of an MTD device and returns complete
  776. * information about it. In case of failure, an error code is returned.
  777. */
  778. struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
  779. {
  780. int err, pnum;
  781. struct rb_node *rb1, *rb2;
  782. struct ubi_scan_volume *sv;
  783. struct ubi_scan_leb *seb;
  784. struct ubi_scan_info *si;
  785. si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
  786. if (!si)
  787. return ERR_PTR(-ENOMEM);
  788. INIT_LIST_HEAD(&si->corr);
  789. INIT_LIST_HEAD(&si->free);
  790. INIT_LIST_HEAD(&si->erase);
  791. INIT_LIST_HEAD(&si->alien);
  792. si->volumes = RB_ROOT;
  793. si->is_empty = 1;
  794. err = -ENOMEM;
  795. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  796. if (!ech)
  797. goto out_si;
  798. vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  799. if (!vidh)
  800. goto out_ech;
  801. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  802. cond_resched();
  803. dbg_gen("process PEB %d", pnum);
  804. err = process_eb(ubi, si, pnum);
  805. if (err < 0)
  806. goto out_vidh;
  807. }
  808. dbg_msg("scanning is finished");
  809. /* Calculate mean erase counter */
  810. if (si->ec_count)
  811. si->mean_ec = div_u64(si->ec_sum, si->ec_count);
  812. if (si->is_empty)
  813. ubi_msg("empty MTD device detected");
  814. /*
  815. * In case of unknown erase counter we use the mean erase counter
  816. * value.
  817. */
  818. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  819. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  820. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  821. seb->ec = si->mean_ec;
  822. }
  823. list_for_each_entry(seb, &si->free, u.list) {
  824. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  825. seb->ec = si->mean_ec;
  826. }
  827. list_for_each_entry(seb, &si->corr, u.list)
  828. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  829. seb->ec = si->mean_ec;
  830. list_for_each_entry(seb, &si->erase, u.list)
  831. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  832. seb->ec = si->mean_ec;
  833. err = paranoid_check_si(ubi, si);
  834. if (err) {
  835. if (err > 0)
  836. err = -EINVAL;
  837. goto out_vidh;
  838. }
  839. ubi_free_vid_hdr(ubi, vidh);
  840. kfree(ech);
  841. return si;
  842. out_vidh:
  843. ubi_free_vid_hdr(ubi, vidh);
  844. out_ech:
  845. kfree(ech);
  846. out_si:
  847. ubi_scan_destroy_si(si);
  848. return ERR_PTR(err);
  849. }
  850. /**
  851. * destroy_sv - free the scanning volume information
  852. * @sv: scanning volume information
  853. *
  854. * This function destroys the volume RB-tree (@sv->root) and the scanning
  855. * volume information.
  856. */
  857. static void destroy_sv(struct ubi_scan_volume *sv)
  858. {
  859. struct ubi_scan_leb *seb;
  860. struct rb_node *this = sv->root.rb_node;
  861. while (this) {
  862. if (this->rb_left)
  863. this = this->rb_left;
  864. else if (this->rb_right)
  865. this = this->rb_right;
  866. else {
  867. seb = rb_entry(this, struct ubi_scan_leb, u.rb);
  868. this = rb_parent(this);
  869. if (this) {
  870. if (this->rb_left == &seb->u.rb)
  871. this->rb_left = NULL;
  872. else
  873. this->rb_right = NULL;
  874. }
  875. kfree(seb);
  876. }
  877. }
  878. kfree(sv);
  879. }
  880. /**
  881. * ubi_scan_destroy_si - destroy scanning information.
  882. * @si: scanning information
  883. */
  884. void ubi_scan_destroy_si(struct ubi_scan_info *si)
  885. {
  886. struct ubi_scan_leb *seb, *seb_tmp;
  887. struct ubi_scan_volume *sv;
  888. struct rb_node *rb;
  889. list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
  890. list_del(&seb->u.list);
  891. kfree(seb);
  892. }
  893. list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
  894. list_del(&seb->u.list);
  895. kfree(seb);
  896. }
  897. list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
  898. list_del(&seb->u.list);
  899. kfree(seb);
  900. }
  901. list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
  902. list_del(&seb->u.list);
  903. kfree(seb);
  904. }
  905. /* Destroy the volume RB-tree */
  906. rb = si->volumes.rb_node;
  907. while (rb) {
  908. if (rb->rb_left)
  909. rb = rb->rb_left;
  910. else if (rb->rb_right)
  911. rb = rb->rb_right;
  912. else {
  913. sv = rb_entry(rb, struct ubi_scan_volume, rb);
  914. rb = rb_parent(rb);
  915. if (rb) {
  916. if (rb->rb_left == &sv->rb)
  917. rb->rb_left = NULL;
  918. else
  919. rb->rb_right = NULL;
  920. }
  921. destroy_sv(sv);
  922. }
  923. }
  924. kfree(si);
  925. }
  926. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  927. /**
  928. * paranoid_check_si - check the scanning information.
  929. * @ubi: UBI device description object
  930. * @si: scanning information
  931. *
  932. * This function returns zero if the scanning information is all right, %1 if
  933. * not and a negative error code if an error occurred.
  934. */
  935. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
  936. {
  937. int pnum, err, vols_found = 0;
  938. struct rb_node *rb1, *rb2;
  939. struct ubi_scan_volume *sv;
  940. struct ubi_scan_leb *seb, *last_seb;
  941. uint8_t *buf;
  942. /*
  943. * At first, check that scanning information is OK.
  944. */
  945. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  946. int leb_count = 0;
  947. cond_resched();
  948. vols_found += 1;
  949. if (si->is_empty) {
  950. ubi_err("bad is_empty flag");
  951. goto bad_sv;
  952. }
  953. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  954. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  955. sv->data_pad < 0 || sv->last_data_size < 0) {
  956. ubi_err("negative values");
  957. goto bad_sv;
  958. }
  959. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  960. sv->vol_id < UBI_INTERNAL_VOL_START) {
  961. ubi_err("bad vol_id");
  962. goto bad_sv;
  963. }
  964. if (sv->vol_id > si->highest_vol_id) {
  965. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  966. si->highest_vol_id, sv->vol_id);
  967. goto out;
  968. }
  969. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  970. sv->vol_type != UBI_STATIC_VOLUME) {
  971. ubi_err("bad vol_type");
  972. goto bad_sv;
  973. }
  974. if (sv->data_pad > ubi->leb_size / 2) {
  975. ubi_err("bad data_pad");
  976. goto bad_sv;
  977. }
  978. last_seb = NULL;
  979. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  980. cond_resched();
  981. last_seb = seb;
  982. leb_count += 1;
  983. if (seb->pnum < 0 || seb->ec < 0) {
  984. ubi_err("negative values");
  985. goto bad_seb;
  986. }
  987. if (seb->ec < si->min_ec) {
  988. ubi_err("bad si->min_ec (%d), %d found",
  989. si->min_ec, seb->ec);
  990. goto bad_seb;
  991. }
  992. if (seb->ec > si->max_ec) {
  993. ubi_err("bad si->max_ec (%d), %d found",
  994. si->max_ec, seb->ec);
  995. goto bad_seb;
  996. }
  997. if (seb->pnum >= ubi->peb_count) {
  998. ubi_err("too high PEB number %d, total PEBs %d",
  999. seb->pnum, ubi->peb_count);
  1000. goto bad_seb;
  1001. }
  1002. if (sv->vol_type == UBI_STATIC_VOLUME) {
  1003. if (seb->lnum >= sv->used_ebs) {
  1004. ubi_err("bad lnum or used_ebs");
  1005. goto bad_seb;
  1006. }
  1007. } else {
  1008. if (sv->used_ebs != 0) {
  1009. ubi_err("non-zero used_ebs");
  1010. goto bad_seb;
  1011. }
  1012. }
  1013. if (seb->lnum > sv->highest_lnum) {
  1014. ubi_err("incorrect highest_lnum or lnum");
  1015. goto bad_seb;
  1016. }
  1017. }
  1018. if (sv->leb_count != leb_count) {
  1019. ubi_err("bad leb_count, %d objects in the tree",
  1020. leb_count);
  1021. goto bad_sv;
  1022. }
  1023. if (!last_seb)
  1024. continue;
  1025. seb = last_seb;
  1026. if (seb->lnum != sv->highest_lnum) {
  1027. ubi_err("bad highest_lnum");
  1028. goto bad_seb;
  1029. }
  1030. }
  1031. if (vols_found != si->vols_found) {
  1032. ubi_err("bad si->vols_found %d, should be %d",
  1033. si->vols_found, vols_found);
  1034. goto out;
  1035. }
  1036. /* Check that scanning information is correct */
  1037. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1038. last_seb = NULL;
  1039. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1040. int vol_type;
  1041. cond_resched();
  1042. last_seb = seb;
  1043. err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
  1044. if (err && err != UBI_IO_BITFLIPS) {
  1045. ubi_err("VID header is not OK (%d)", err);
  1046. if (err > 0)
  1047. err = -EIO;
  1048. return err;
  1049. }
  1050. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1051. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1052. if (sv->vol_type != vol_type) {
  1053. ubi_err("bad vol_type");
  1054. goto bad_vid_hdr;
  1055. }
  1056. if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1057. ubi_err("bad sqnum %llu", seb->sqnum);
  1058. goto bad_vid_hdr;
  1059. }
  1060. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1061. ubi_err("bad vol_id %d", sv->vol_id);
  1062. goto bad_vid_hdr;
  1063. }
  1064. if (sv->compat != vidh->compat) {
  1065. ubi_err("bad compat %d", vidh->compat);
  1066. goto bad_vid_hdr;
  1067. }
  1068. if (seb->lnum != be32_to_cpu(vidh->lnum)) {
  1069. ubi_err("bad lnum %d", seb->lnum);
  1070. goto bad_vid_hdr;
  1071. }
  1072. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1073. ubi_err("bad used_ebs %d", sv->used_ebs);
  1074. goto bad_vid_hdr;
  1075. }
  1076. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1077. ubi_err("bad data_pad %d", sv->data_pad);
  1078. goto bad_vid_hdr;
  1079. }
  1080. }
  1081. if (!last_seb)
  1082. continue;
  1083. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1084. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1085. goto bad_vid_hdr;
  1086. }
  1087. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1088. ubi_err("bad last_data_size %d", sv->last_data_size);
  1089. goto bad_vid_hdr;
  1090. }
  1091. }
  1092. /*
  1093. * Make sure that all the physical eraseblocks are in one of the lists
  1094. * or trees.
  1095. */
  1096. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1097. if (!buf)
  1098. return -ENOMEM;
  1099. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1100. err = ubi_io_is_bad(ubi, pnum);
  1101. if (err < 0) {
  1102. kfree(buf);
  1103. return err;
  1104. } else if (err)
  1105. buf[pnum] = 1;
  1106. }
  1107. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1108. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1109. buf[seb->pnum] = 1;
  1110. list_for_each_entry(seb, &si->free, u.list)
  1111. buf[seb->pnum] = 1;
  1112. list_for_each_entry(seb, &si->corr, u.list)
  1113. buf[seb->pnum] = 1;
  1114. list_for_each_entry(seb, &si->erase, u.list)
  1115. buf[seb->pnum] = 1;
  1116. list_for_each_entry(seb, &si->alien, u.list)
  1117. buf[seb->pnum] = 1;
  1118. err = 0;
  1119. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1120. if (!buf[pnum]) {
  1121. ubi_err("PEB %d is not referred", pnum);
  1122. err = 1;
  1123. }
  1124. kfree(buf);
  1125. if (err)
  1126. goto out;
  1127. return 0;
  1128. bad_seb:
  1129. ubi_err("bad scanning information about LEB %d", seb->lnum);
  1130. ubi_dbg_dump_seb(seb, 0);
  1131. ubi_dbg_dump_sv(sv);
  1132. goto out;
  1133. bad_sv:
  1134. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1135. ubi_dbg_dump_sv(sv);
  1136. goto out;
  1137. bad_vid_hdr:
  1138. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1139. ubi_dbg_dump_sv(sv);
  1140. ubi_dbg_dump_vid_hdr(vidh);
  1141. out:
  1142. ubi_dbg_dump_stack();
  1143. return 1;
  1144. }
  1145. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */