attach.c 51 KB

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