scan.c 43 KB

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