attach.c 47 KB

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