scan.c 34 KB

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