scan.c 34 KB

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