attach.c 44 KB

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