scan.c 34 KB

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