scan.c 43 KB

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