attach.c 52 KB

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