scan.c 34 KB

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