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