scan.c 37 KB

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