scan.c 35 KB

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