scan.c 34 KB

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