scan.c 43 KB

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