scan.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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. err = 0;
  692. goto out_unlock;
  693. }
  694. if (err)
  695. goto out_unlock;
  696. if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size))
  697. goto out_unlock;
  698. ubi_err("PEB %d contains corrupted VID header, and the data does not "
  699. "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
  700. "header corruption which requires manual inspection", pnum);
  701. ubi_dbg_dump_vid_hdr(vid_hdr);
  702. dbg_msg("hexdump of PEB %d offset %d, length %d",
  703. pnum, ubi->leb_start, ubi->leb_size);
  704. ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  705. ubi->peb_buf1, ubi->leb_size, 1);
  706. err = 1;
  707. out_unlock:
  708. mutex_unlock(&ubi->buf_mutex);
  709. return err;
  710. }
  711. /**
  712. * process_eb - read, check UBI headers, and add them to scanning information.
  713. * @ubi: UBI device description object
  714. * @si: scanning information
  715. * @pnum: the physical eraseblock number
  716. *
  717. * This function returns a zero if the physical eraseblock was successfully
  718. * handled and a negative error code in case of failure.
  719. */
  720. static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
  721. int pnum)
  722. {
  723. long long uninitialized_var(ec);
  724. int err, bitflips = 0, vol_id, ec_err = 0;
  725. dbg_bld("scan PEB %d", pnum);
  726. /* Skip bad physical eraseblocks */
  727. err = ubi_io_is_bad(ubi, pnum);
  728. if (err < 0)
  729. return err;
  730. else if (err) {
  731. /*
  732. * FIXME: this is actually duty of the I/O sub-system to
  733. * initialize this, but MTD does not provide enough
  734. * information.
  735. */
  736. si->bad_peb_count += 1;
  737. return 0;
  738. }
  739. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  740. if (err < 0)
  741. return err;
  742. switch (err) {
  743. case 0:
  744. break;
  745. case UBI_IO_BITFLIPS:
  746. bitflips = 1;
  747. break;
  748. case UBI_IO_FF:
  749. si->empty_peb_count += 1;
  750. return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
  751. &si->erase);
  752. case UBI_IO_FF_BITFLIPS:
  753. si->empty_peb_count += 1;
  754. return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
  755. &si->erase);
  756. case UBI_IO_BAD_HDR_EBADMSG:
  757. case UBI_IO_BAD_HDR:
  758. /*
  759. * We have to also look at the VID header, possibly it is not
  760. * corrupted. Set %bitflips flag in order to make this PEB be
  761. * moved and EC be re-created.
  762. */
  763. ec_err = err;
  764. ec = UBI_SCAN_UNKNOWN_EC;
  765. bitflips = 1;
  766. break;
  767. default:
  768. ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
  769. return -EINVAL;
  770. }
  771. if (!ec_err) {
  772. int image_seq;
  773. /* Make sure UBI version is OK */
  774. if (ech->version != UBI_VERSION) {
  775. ubi_err("this UBI version is %d, image version is %d",
  776. UBI_VERSION, (int)ech->version);
  777. return -EINVAL;
  778. }
  779. ec = be64_to_cpu(ech->ec);
  780. if (ec > UBI_MAX_ERASECOUNTER) {
  781. /*
  782. * Erase counter overflow. The EC headers have 64 bits
  783. * reserved, but we anyway make use of only 31 bit
  784. * values, as this seems to be enough for any existing
  785. * flash. Upgrade UBI and use 64-bit erase counters
  786. * internally.
  787. */
  788. ubi_err("erase counter overflow, max is %d",
  789. UBI_MAX_ERASECOUNTER);
  790. ubi_dbg_dump_ec_hdr(ech);
  791. return -EINVAL;
  792. }
  793. /*
  794. * Make sure that all PEBs have the same image sequence number.
  795. * This allows us to detect situations when users flash UBI
  796. * images incorrectly, so that the flash has the new UBI image
  797. * and leftovers from the old one. This feature was added
  798. * relatively recently, and the sequence number was always
  799. * zero, because old UBI implementations always set it to zero.
  800. * For this reasons, we do not panic if some PEBs have zero
  801. * sequence number, while other PEBs have non-zero sequence
  802. * number.
  803. */
  804. image_seq = be32_to_cpu(ech->image_seq);
  805. if (!ubi->image_seq && image_seq)
  806. ubi->image_seq = image_seq;
  807. if (ubi->image_seq && image_seq &&
  808. ubi->image_seq != image_seq) {
  809. ubi_err("bad image sequence number %d in PEB %d, "
  810. "expected %d", image_seq, pnum, ubi->image_seq);
  811. ubi_dbg_dump_ec_hdr(ech);
  812. return -EINVAL;
  813. }
  814. }
  815. /* OK, we've done with the EC header, let's look at the VID header */
  816. err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
  817. if (err < 0)
  818. return err;
  819. switch (err) {
  820. case 0:
  821. break;
  822. case UBI_IO_BITFLIPS:
  823. bitflips = 1;
  824. break;
  825. case UBI_IO_BAD_HDR_EBADMSG:
  826. if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
  827. /*
  828. * Both EC and VID headers are corrupted and were read
  829. * with data integrity error, probably this is a bad
  830. * PEB, bit it is not marked as bad yet. This may also
  831. * be a result of power cut during erasure.
  832. */
  833. si->maybe_bad_peb_count += 1;
  834. case UBI_IO_BAD_HDR:
  835. if (ec_err)
  836. /*
  837. * Both headers are corrupted. There is a possibility
  838. * that this a valid UBI PEB which has corresponding
  839. * LEB, but the headers are corrupted. However, it is
  840. * impossible to distinguish it from a PEB which just
  841. * contains garbage because of a power cut during erase
  842. * operation. So we just schedule this PEB for erasure.
  843. *
  844. * Besides, in case of NOR flash, we deliberatly
  845. * corrupt both headers because NOR flash erasure is
  846. * slow and can start from the end.
  847. */
  848. err = 0;
  849. else
  850. /*
  851. * The EC was OK, but the VID header is corrupted. We
  852. * have to check what is in the data area.
  853. */
  854. err = check_corruption(ubi, vidh, pnum);
  855. if (err < 0)
  856. return err;
  857. else if (!err)
  858. /* This corruption is caused by a power cut */
  859. err = add_to_list(si, pnum, ec, 1, &si->erase);
  860. else
  861. /* This is an unexpected corruption */
  862. err = add_corrupted(si, pnum, ec);
  863. if (err)
  864. return err;
  865. goto adjust_mean_ec;
  866. case UBI_IO_FF_BITFLIPS:
  867. err = add_to_list(si, pnum, ec, 1, &si->erase);
  868. if (err)
  869. return err;
  870. goto adjust_mean_ec;
  871. case UBI_IO_FF:
  872. if (ec_err)
  873. err = add_to_list(si, pnum, ec, 1, &si->erase);
  874. else
  875. err = add_to_list(si, pnum, ec, 0, &si->free);
  876. if (err)
  877. return err;
  878. goto adjust_mean_ec;
  879. default:
  880. ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
  881. err);
  882. return -EINVAL;
  883. }
  884. vol_id = be32_to_cpu(vidh->vol_id);
  885. if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
  886. int lnum = be32_to_cpu(vidh->lnum);
  887. /* Unsupported internal volume */
  888. switch (vidh->compat) {
  889. case UBI_COMPAT_DELETE:
  890. ubi_msg("\"delete\" compatible internal volume %d:%d"
  891. " found, will remove it", vol_id, lnum);
  892. err = add_to_list(si, pnum, ec, 1, &si->erase);
  893. if (err)
  894. return err;
  895. return 0;
  896. case UBI_COMPAT_RO:
  897. ubi_msg("read-only compatible internal volume %d:%d"
  898. " found, switch to read-only mode",
  899. vol_id, lnum);
  900. ubi->ro_mode = 1;
  901. break;
  902. case UBI_COMPAT_PRESERVE:
  903. ubi_msg("\"preserve\" compatible internal volume %d:%d"
  904. " found", vol_id, lnum);
  905. err = add_to_list(si, pnum, ec, 0, &si->alien);
  906. if (err)
  907. return err;
  908. return 0;
  909. case UBI_COMPAT_REJECT:
  910. ubi_err("incompatible internal volume %d:%d found",
  911. vol_id, lnum);
  912. return -EINVAL;
  913. }
  914. }
  915. if (ec_err)
  916. ubi_warn("valid VID header but corrupted EC header at PEB %d",
  917. pnum);
  918. err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
  919. if (err)
  920. return err;
  921. adjust_mean_ec:
  922. if (!ec_err) {
  923. si->ec_sum += ec;
  924. si->ec_count += 1;
  925. if (ec > si->max_ec)
  926. si->max_ec = ec;
  927. if (ec < si->min_ec)
  928. si->min_ec = ec;
  929. }
  930. return 0;
  931. }
  932. /**
  933. * check_what_we_have - check what PEB were found by scanning.
  934. * @ubi: UBI device description object
  935. * @si: scanning information
  936. *
  937. * This is a helper function which takes a look what PEBs were found by
  938. * scanning, and decides whether the flash is empty and should be formatted and
  939. * whether there are too many corrupted PEBs and we should not attach this
  940. * MTD device. Returns zero if we should proceed with attaching the MTD device,
  941. * and %-EINVAL if we should not.
  942. */
  943. static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
  944. {
  945. struct ubi_scan_leb *seb;
  946. int max_corr, peb_count;
  947. peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
  948. max_corr = peb_count / 20 ?: 8;
  949. /*
  950. * Few corrupted PEBs is not a problem and may be just a result of
  951. * unclean reboots. However, many of them may indicate some problems
  952. * with the flash HW or driver.
  953. */
  954. if (si->corr_peb_count) {
  955. ubi_err("%d PEBs are corrupted and preserved",
  956. si->corr_peb_count);
  957. printk(KERN_ERR "Corrupted PEBs are:");
  958. list_for_each_entry(seb, &si->corr, u.list)
  959. printk(KERN_CONT " %d", seb->pnum);
  960. printk(KERN_CONT "\n");
  961. /*
  962. * If too many PEBs are corrupted, we refuse attaching,
  963. * otherwise, only print a warning.
  964. */
  965. if (si->corr_peb_count >= max_corr) {
  966. ubi_err("too many corrupted PEBs, refusing this device");
  967. return -EINVAL;
  968. }
  969. }
  970. if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
  971. /*
  972. * All PEBs are empty, or almost all - a couple PEBs look like
  973. * they may be bad PEBs which were not marked as bad yet.
  974. *
  975. * This piece of code basically tries to distinguish between
  976. * the following situations:
  977. *
  978. * 1. Flash is empty, but there are few bad PEBs, which are not
  979. * marked as bad so far, and which were read with error. We
  980. * want to go ahead and format this flash. While formatting,
  981. * the faulty PEBs will probably be marked as bad.
  982. *
  983. * 2. Flash contains non-UBI data and we do not want to format
  984. * it and destroy possibly important information.
  985. */
  986. if (si->maybe_bad_peb_count <= 2) {
  987. si->is_empty = 1;
  988. ubi_msg("empty MTD device detected");
  989. get_random_bytes(&ubi->image_seq,
  990. sizeof(ubi->image_seq));
  991. } else {
  992. ubi_err("MTD device is not UBI-formatted and possibly "
  993. "contains non-UBI data - refusing it");
  994. return -EINVAL;
  995. }
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * ubi_scan - scan an MTD device.
  1001. * @ubi: UBI device description object
  1002. *
  1003. * This function does full scanning of an MTD device and returns complete
  1004. * information about it. In case of failure, an error code is returned.
  1005. */
  1006. struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
  1007. {
  1008. int err, pnum;
  1009. struct rb_node *rb1, *rb2;
  1010. struct ubi_scan_volume *sv;
  1011. struct ubi_scan_leb *seb;
  1012. struct ubi_scan_info *si;
  1013. si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
  1014. if (!si)
  1015. return ERR_PTR(-ENOMEM);
  1016. INIT_LIST_HEAD(&si->corr);
  1017. INIT_LIST_HEAD(&si->free);
  1018. INIT_LIST_HEAD(&si->erase);
  1019. INIT_LIST_HEAD(&si->alien);
  1020. si->volumes = RB_ROOT;
  1021. err = -ENOMEM;
  1022. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  1023. if (!ech)
  1024. goto out_si;
  1025. vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  1026. if (!vidh)
  1027. goto out_ech;
  1028. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1029. cond_resched();
  1030. dbg_gen("process PEB %d", pnum);
  1031. err = process_eb(ubi, si, pnum);
  1032. if (err < 0)
  1033. goto out_vidh;
  1034. }
  1035. dbg_msg("scanning is finished");
  1036. /* Calculate mean erase counter */
  1037. if (si->ec_count)
  1038. si->mean_ec = div_u64(si->ec_sum, si->ec_count);
  1039. err = check_what_we_have(ubi, si);
  1040. if (err)
  1041. goto out_vidh;
  1042. /*
  1043. * In case of unknown erase counter we use the mean erase counter
  1044. * value.
  1045. */
  1046. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1047. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1048. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  1049. seb->ec = si->mean_ec;
  1050. }
  1051. list_for_each_entry(seb, &si->free, u.list) {
  1052. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  1053. seb->ec = si->mean_ec;
  1054. }
  1055. list_for_each_entry(seb, &si->corr, u.list)
  1056. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  1057. seb->ec = si->mean_ec;
  1058. list_for_each_entry(seb, &si->erase, u.list)
  1059. if (seb->ec == UBI_SCAN_UNKNOWN_EC)
  1060. seb->ec = si->mean_ec;
  1061. err = paranoid_check_si(ubi, si);
  1062. if (err)
  1063. goto out_vidh;
  1064. ubi_free_vid_hdr(ubi, vidh);
  1065. kfree(ech);
  1066. return si;
  1067. out_vidh:
  1068. ubi_free_vid_hdr(ubi, vidh);
  1069. out_ech:
  1070. kfree(ech);
  1071. out_si:
  1072. ubi_scan_destroy_si(si);
  1073. return ERR_PTR(err);
  1074. }
  1075. /**
  1076. * destroy_sv - free the scanning volume information
  1077. * @sv: scanning volume information
  1078. *
  1079. * This function destroys the volume RB-tree (@sv->root) and the scanning
  1080. * volume information.
  1081. */
  1082. static void destroy_sv(struct ubi_scan_volume *sv)
  1083. {
  1084. struct ubi_scan_leb *seb;
  1085. struct rb_node *this = sv->root.rb_node;
  1086. while (this) {
  1087. if (this->rb_left)
  1088. this = this->rb_left;
  1089. else if (this->rb_right)
  1090. this = this->rb_right;
  1091. else {
  1092. seb = rb_entry(this, struct ubi_scan_leb, u.rb);
  1093. this = rb_parent(this);
  1094. if (this) {
  1095. if (this->rb_left == &seb->u.rb)
  1096. this->rb_left = NULL;
  1097. else
  1098. this->rb_right = NULL;
  1099. }
  1100. kfree(seb);
  1101. }
  1102. }
  1103. kfree(sv);
  1104. }
  1105. /**
  1106. * ubi_scan_destroy_si - destroy scanning information.
  1107. * @si: scanning information
  1108. */
  1109. void ubi_scan_destroy_si(struct ubi_scan_info *si)
  1110. {
  1111. struct ubi_scan_leb *seb, *seb_tmp;
  1112. struct ubi_scan_volume *sv;
  1113. struct rb_node *rb;
  1114. list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
  1115. list_del(&seb->u.list);
  1116. kfree(seb);
  1117. }
  1118. list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
  1119. list_del(&seb->u.list);
  1120. kfree(seb);
  1121. }
  1122. list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
  1123. list_del(&seb->u.list);
  1124. kfree(seb);
  1125. }
  1126. list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
  1127. list_del(&seb->u.list);
  1128. kfree(seb);
  1129. }
  1130. /* Destroy the volume RB-tree */
  1131. rb = si->volumes.rb_node;
  1132. while (rb) {
  1133. if (rb->rb_left)
  1134. rb = rb->rb_left;
  1135. else if (rb->rb_right)
  1136. rb = rb->rb_right;
  1137. else {
  1138. sv = rb_entry(rb, struct ubi_scan_volume, rb);
  1139. rb = rb_parent(rb);
  1140. if (rb) {
  1141. if (rb->rb_left == &sv->rb)
  1142. rb->rb_left = NULL;
  1143. else
  1144. rb->rb_right = NULL;
  1145. }
  1146. destroy_sv(sv);
  1147. }
  1148. }
  1149. kfree(si);
  1150. }
  1151. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  1152. /**
  1153. * paranoid_check_si - check the scanning information.
  1154. * @ubi: UBI device description object
  1155. * @si: scanning information
  1156. *
  1157. * This function returns zero if the scanning information is all right, and a
  1158. * negative error code if not or if an error occurred.
  1159. */
  1160. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
  1161. {
  1162. int pnum, err, vols_found = 0;
  1163. struct rb_node *rb1, *rb2;
  1164. struct ubi_scan_volume *sv;
  1165. struct ubi_scan_leb *seb, *last_seb;
  1166. uint8_t *buf;
  1167. /*
  1168. * At first, check that scanning information is OK.
  1169. */
  1170. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1171. int leb_count = 0;
  1172. cond_resched();
  1173. vols_found += 1;
  1174. if (si->is_empty) {
  1175. ubi_err("bad is_empty flag");
  1176. goto bad_sv;
  1177. }
  1178. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  1179. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  1180. sv->data_pad < 0 || sv->last_data_size < 0) {
  1181. ubi_err("negative values");
  1182. goto bad_sv;
  1183. }
  1184. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  1185. sv->vol_id < UBI_INTERNAL_VOL_START) {
  1186. ubi_err("bad vol_id");
  1187. goto bad_sv;
  1188. }
  1189. if (sv->vol_id > si->highest_vol_id) {
  1190. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  1191. si->highest_vol_id, sv->vol_id);
  1192. goto out;
  1193. }
  1194. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  1195. sv->vol_type != UBI_STATIC_VOLUME) {
  1196. ubi_err("bad vol_type");
  1197. goto bad_sv;
  1198. }
  1199. if (sv->data_pad > ubi->leb_size / 2) {
  1200. ubi_err("bad data_pad");
  1201. goto bad_sv;
  1202. }
  1203. last_seb = NULL;
  1204. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1205. cond_resched();
  1206. last_seb = seb;
  1207. leb_count += 1;
  1208. if (seb->pnum < 0 || seb->ec < 0) {
  1209. ubi_err("negative values");
  1210. goto bad_seb;
  1211. }
  1212. if (seb->ec < si->min_ec) {
  1213. ubi_err("bad si->min_ec (%d), %d found",
  1214. si->min_ec, seb->ec);
  1215. goto bad_seb;
  1216. }
  1217. if (seb->ec > si->max_ec) {
  1218. ubi_err("bad si->max_ec (%d), %d found",
  1219. si->max_ec, seb->ec);
  1220. goto bad_seb;
  1221. }
  1222. if (seb->pnum >= ubi->peb_count) {
  1223. ubi_err("too high PEB number %d, total PEBs %d",
  1224. seb->pnum, ubi->peb_count);
  1225. goto bad_seb;
  1226. }
  1227. if (sv->vol_type == UBI_STATIC_VOLUME) {
  1228. if (seb->lnum >= sv->used_ebs) {
  1229. ubi_err("bad lnum or used_ebs");
  1230. goto bad_seb;
  1231. }
  1232. } else {
  1233. if (sv->used_ebs != 0) {
  1234. ubi_err("non-zero used_ebs");
  1235. goto bad_seb;
  1236. }
  1237. }
  1238. if (seb->lnum > sv->highest_lnum) {
  1239. ubi_err("incorrect highest_lnum or lnum");
  1240. goto bad_seb;
  1241. }
  1242. }
  1243. if (sv->leb_count != leb_count) {
  1244. ubi_err("bad leb_count, %d objects in the tree",
  1245. leb_count);
  1246. goto bad_sv;
  1247. }
  1248. if (!last_seb)
  1249. continue;
  1250. seb = last_seb;
  1251. if (seb->lnum != sv->highest_lnum) {
  1252. ubi_err("bad highest_lnum");
  1253. goto bad_seb;
  1254. }
  1255. }
  1256. if (vols_found != si->vols_found) {
  1257. ubi_err("bad si->vols_found %d, should be %d",
  1258. si->vols_found, vols_found);
  1259. goto out;
  1260. }
  1261. /* Check that scanning information is correct */
  1262. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1263. last_seb = NULL;
  1264. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1265. int vol_type;
  1266. cond_resched();
  1267. last_seb = seb;
  1268. err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
  1269. if (err && err != UBI_IO_BITFLIPS) {
  1270. ubi_err("VID header is not OK (%d)", err);
  1271. if (err > 0)
  1272. err = -EIO;
  1273. return err;
  1274. }
  1275. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1276. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1277. if (sv->vol_type != vol_type) {
  1278. ubi_err("bad vol_type");
  1279. goto bad_vid_hdr;
  1280. }
  1281. if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1282. ubi_err("bad sqnum %llu", seb->sqnum);
  1283. goto bad_vid_hdr;
  1284. }
  1285. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1286. ubi_err("bad vol_id %d", sv->vol_id);
  1287. goto bad_vid_hdr;
  1288. }
  1289. if (sv->compat != vidh->compat) {
  1290. ubi_err("bad compat %d", vidh->compat);
  1291. goto bad_vid_hdr;
  1292. }
  1293. if (seb->lnum != be32_to_cpu(vidh->lnum)) {
  1294. ubi_err("bad lnum %d", seb->lnum);
  1295. goto bad_vid_hdr;
  1296. }
  1297. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1298. ubi_err("bad used_ebs %d", sv->used_ebs);
  1299. goto bad_vid_hdr;
  1300. }
  1301. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1302. ubi_err("bad data_pad %d", sv->data_pad);
  1303. goto bad_vid_hdr;
  1304. }
  1305. }
  1306. if (!last_seb)
  1307. continue;
  1308. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1309. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1310. goto bad_vid_hdr;
  1311. }
  1312. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1313. ubi_err("bad last_data_size %d", sv->last_data_size);
  1314. goto bad_vid_hdr;
  1315. }
  1316. }
  1317. /*
  1318. * Make sure that all the physical eraseblocks are in one of the lists
  1319. * or trees.
  1320. */
  1321. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1322. if (!buf)
  1323. return -ENOMEM;
  1324. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1325. err = ubi_io_is_bad(ubi, pnum);
  1326. if (err < 0) {
  1327. kfree(buf);
  1328. return err;
  1329. } else if (err)
  1330. buf[pnum] = 1;
  1331. }
  1332. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1333. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1334. buf[seb->pnum] = 1;
  1335. list_for_each_entry(seb, &si->free, u.list)
  1336. buf[seb->pnum] = 1;
  1337. list_for_each_entry(seb, &si->corr, u.list)
  1338. buf[seb->pnum] = 1;
  1339. list_for_each_entry(seb, &si->erase, u.list)
  1340. buf[seb->pnum] = 1;
  1341. list_for_each_entry(seb, &si->alien, u.list)
  1342. buf[seb->pnum] = 1;
  1343. err = 0;
  1344. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1345. if (!buf[pnum]) {
  1346. ubi_err("PEB %d is not referred", pnum);
  1347. err = 1;
  1348. }
  1349. kfree(buf);
  1350. if (err)
  1351. goto out;
  1352. return 0;
  1353. bad_seb:
  1354. ubi_err("bad scanning information about LEB %d", seb->lnum);
  1355. ubi_dbg_dump_seb(seb, 0);
  1356. ubi_dbg_dump_sv(sv);
  1357. goto out;
  1358. bad_sv:
  1359. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1360. ubi_dbg_dump_sv(sv);
  1361. goto out;
  1362. bad_vid_hdr:
  1363. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1364. ubi_dbg_dump_sv(sv);
  1365. ubi_dbg_dump_vid_hdr(vidh);
  1366. out:
  1367. ubi_dbg_dump_stack();
  1368. return -EINVAL;
  1369. }
  1370. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */