attach.c 46 KB

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