scan.c 42 KB

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