scan.c 35 KB

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