attach.c 47 KB

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