scan.c 35 KB

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