attach.c 49 KB

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