scan.c 35 KB

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