attach.c 47 KB

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