attach.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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(sizeof(*av), GFP_KERNEL);
  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(void)
  1277. {
  1278. struct ubi_attach_info *ai;
  1279. ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
  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("ubi_aeb_slab_cache",
  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();
  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();
  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();
  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_ai;
  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();
  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_ai:
  1439. destroy_ai(ai);
  1440. return err;
  1441. }
  1442. /**
  1443. * self_check_ai - check the attaching information.
  1444. * @ubi: UBI device description object
  1445. * @ai: attaching information
  1446. *
  1447. * This function returns zero if the attaching information is all right, and a
  1448. * negative error code if not or if an error occurred.
  1449. */
  1450. static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1451. {
  1452. struct ubi_vid_io_buf *vidb = ai->vidb;
  1453. struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb);
  1454. int pnum, err, vols_found = 0;
  1455. struct rb_node *rb1, *rb2;
  1456. struct ubi_ainf_volume *av;
  1457. struct ubi_ainf_peb *aeb, *last_aeb;
  1458. uint8_t *buf;
  1459. if (!ubi_dbg_chk_gen(ubi))
  1460. return 0;
  1461. /*
  1462. * At first, check that attaching information is OK.
  1463. */
  1464. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1465. int leb_count = 0;
  1466. cond_resched();
  1467. vols_found += 1;
  1468. if (ai->is_empty) {
  1469. ubi_err(ubi, "bad is_empty flag");
  1470. goto bad_av;
  1471. }
  1472. if (av->vol_id < 0 || av->highest_lnum < 0 ||
  1473. av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
  1474. av->data_pad < 0 || av->last_data_size < 0) {
  1475. ubi_err(ubi, "negative values");
  1476. goto bad_av;
  1477. }
  1478. if (av->vol_id >= UBI_MAX_VOLUMES &&
  1479. av->vol_id < UBI_INTERNAL_VOL_START) {
  1480. ubi_err(ubi, "bad vol_id");
  1481. goto bad_av;
  1482. }
  1483. if (av->vol_id > ai->highest_vol_id) {
  1484. ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there",
  1485. ai->highest_vol_id, av->vol_id);
  1486. goto out;
  1487. }
  1488. if (av->vol_type != UBI_DYNAMIC_VOLUME &&
  1489. av->vol_type != UBI_STATIC_VOLUME) {
  1490. ubi_err(ubi, "bad vol_type");
  1491. goto bad_av;
  1492. }
  1493. if (av->data_pad > ubi->leb_size / 2) {
  1494. ubi_err(ubi, "bad data_pad");
  1495. goto bad_av;
  1496. }
  1497. last_aeb = NULL;
  1498. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1499. cond_resched();
  1500. last_aeb = aeb;
  1501. leb_count += 1;
  1502. if (aeb->pnum < 0 || aeb->ec < 0) {
  1503. ubi_err(ubi, "negative values");
  1504. goto bad_aeb;
  1505. }
  1506. if (aeb->ec < ai->min_ec) {
  1507. ubi_err(ubi, "bad ai->min_ec (%d), %d found",
  1508. ai->min_ec, aeb->ec);
  1509. goto bad_aeb;
  1510. }
  1511. if (aeb->ec > ai->max_ec) {
  1512. ubi_err(ubi, "bad ai->max_ec (%d), %d found",
  1513. ai->max_ec, aeb->ec);
  1514. goto bad_aeb;
  1515. }
  1516. if (aeb->pnum >= ubi->peb_count) {
  1517. ubi_err(ubi, "too high PEB number %d, total PEBs %d",
  1518. aeb->pnum, ubi->peb_count);
  1519. goto bad_aeb;
  1520. }
  1521. if (av->vol_type == UBI_STATIC_VOLUME) {
  1522. if (aeb->lnum >= av->used_ebs) {
  1523. ubi_err(ubi, "bad lnum or used_ebs");
  1524. goto bad_aeb;
  1525. }
  1526. } else {
  1527. if (av->used_ebs != 0) {
  1528. ubi_err(ubi, "non-zero used_ebs");
  1529. goto bad_aeb;
  1530. }
  1531. }
  1532. if (aeb->lnum > av->highest_lnum) {
  1533. ubi_err(ubi, "incorrect highest_lnum or lnum");
  1534. goto bad_aeb;
  1535. }
  1536. }
  1537. if (av->leb_count != leb_count) {
  1538. ubi_err(ubi, "bad leb_count, %d objects in the tree",
  1539. leb_count);
  1540. goto bad_av;
  1541. }
  1542. if (!last_aeb)
  1543. continue;
  1544. aeb = last_aeb;
  1545. if (aeb->lnum != av->highest_lnum) {
  1546. ubi_err(ubi, "bad highest_lnum");
  1547. goto bad_aeb;
  1548. }
  1549. }
  1550. if (vols_found != ai->vols_found) {
  1551. ubi_err(ubi, "bad ai->vols_found %d, should be %d",
  1552. ai->vols_found, vols_found);
  1553. goto out;
  1554. }
  1555. /* Check that attaching information is correct */
  1556. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1557. last_aeb = NULL;
  1558. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1559. int vol_type;
  1560. cond_resched();
  1561. last_aeb = aeb;
  1562. err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1);
  1563. if (err && err != UBI_IO_BITFLIPS) {
  1564. ubi_err(ubi, "VID header is not OK (%d)",
  1565. err);
  1566. if (err > 0)
  1567. err = -EIO;
  1568. return err;
  1569. }
  1570. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1571. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1572. if (av->vol_type != vol_type) {
  1573. ubi_err(ubi, "bad vol_type");
  1574. goto bad_vid_hdr;
  1575. }
  1576. if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1577. ubi_err(ubi, "bad sqnum %llu", aeb->sqnum);
  1578. goto bad_vid_hdr;
  1579. }
  1580. if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
  1581. ubi_err(ubi, "bad vol_id %d", av->vol_id);
  1582. goto bad_vid_hdr;
  1583. }
  1584. if (av->compat != vidh->compat) {
  1585. ubi_err(ubi, "bad compat %d", vidh->compat);
  1586. goto bad_vid_hdr;
  1587. }
  1588. if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
  1589. ubi_err(ubi, "bad lnum %d", aeb->lnum);
  1590. goto bad_vid_hdr;
  1591. }
  1592. if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1593. ubi_err(ubi, "bad used_ebs %d", av->used_ebs);
  1594. goto bad_vid_hdr;
  1595. }
  1596. if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
  1597. ubi_err(ubi, "bad data_pad %d", av->data_pad);
  1598. goto bad_vid_hdr;
  1599. }
  1600. }
  1601. if (!last_aeb)
  1602. continue;
  1603. if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1604. ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum);
  1605. goto bad_vid_hdr;
  1606. }
  1607. if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
  1608. ubi_err(ubi, "bad last_data_size %d",
  1609. av->last_data_size);
  1610. goto bad_vid_hdr;
  1611. }
  1612. }
  1613. /*
  1614. * Make sure that all the physical eraseblocks are in one of the lists
  1615. * or trees.
  1616. */
  1617. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1618. if (!buf)
  1619. return -ENOMEM;
  1620. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1621. err = ubi_io_is_bad(ubi, pnum);
  1622. if (err < 0) {
  1623. kfree(buf);
  1624. return err;
  1625. } else if (err)
  1626. buf[pnum] = 1;
  1627. }
  1628. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
  1629. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
  1630. buf[aeb->pnum] = 1;
  1631. list_for_each_entry(aeb, &ai->free, u.list)
  1632. buf[aeb->pnum] = 1;
  1633. list_for_each_entry(aeb, &ai->corr, u.list)
  1634. buf[aeb->pnum] = 1;
  1635. list_for_each_entry(aeb, &ai->erase, u.list)
  1636. buf[aeb->pnum] = 1;
  1637. list_for_each_entry(aeb, &ai->alien, u.list)
  1638. buf[aeb->pnum] = 1;
  1639. err = 0;
  1640. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1641. if (!buf[pnum]) {
  1642. ubi_err(ubi, "PEB %d is not referred", pnum);
  1643. err = 1;
  1644. }
  1645. kfree(buf);
  1646. if (err)
  1647. goto out;
  1648. return 0;
  1649. bad_aeb:
  1650. ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum);
  1651. ubi_dump_aeb(aeb, 0);
  1652. ubi_dump_av(av);
  1653. goto out;
  1654. bad_av:
  1655. ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
  1656. ubi_dump_av(av);
  1657. goto out;
  1658. bad_vid_hdr:
  1659. ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
  1660. ubi_dump_av(av);
  1661. ubi_dump_vid_hdr(vidh);
  1662. out:
  1663. dump_stack();
  1664. return -EINVAL;
  1665. }