attach.c 47 KB

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