scan.c 34 KB

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