scan.c 42 KB

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