scan.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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 && !mtd_is_eccerr(err))
  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_buf, 0x00, ubi->leb_size);
  695. err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
  696. ubi->leb_size);
  697. if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
  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_buf, 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_buf, 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 deliberately
  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 || bitflips)
  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");
  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_si;
  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_si:
  1091. ubi_scan_destroy_si(si);
  1092. return ERR_PTR(err);
  1093. }
  1094. /**
  1095. * destroy_sv - free the scanning volume information
  1096. * @sv: scanning volume information
  1097. * @si: scanning information
  1098. *
  1099. * This function destroys the volume RB-tree (@sv->root) and the scanning
  1100. * volume information.
  1101. */
  1102. static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
  1103. {
  1104. struct ubi_scan_leb *seb;
  1105. struct rb_node *this = sv->root.rb_node;
  1106. while (this) {
  1107. if (this->rb_left)
  1108. this = this->rb_left;
  1109. else if (this->rb_right)
  1110. this = this->rb_right;
  1111. else {
  1112. seb = rb_entry(this, struct ubi_scan_leb, u.rb);
  1113. this = rb_parent(this);
  1114. if (this) {
  1115. if (this->rb_left == &seb->u.rb)
  1116. this->rb_left = NULL;
  1117. else
  1118. this->rb_right = NULL;
  1119. }
  1120. kmem_cache_free(si->scan_leb_slab, seb);
  1121. }
  1122. }
  1123. kfree(sv);
  1124. }
  1125. /**
  1126. * ubi_scan_destroy_si - destroy scanning information.
  1127. * @si: scanning information
  1128. */
  1129. void ubi_scan_destroy_si(struct ubi_scan_info *si)
  1130. {
  1131. struct ubi_scan_leb *seb, *seb_tmp;
  1132. struct ubi_scan_volume *sv;
  1133. struct rb_node *rb;
  1134. list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
  1135. list_del(&seb->u.list);
  1136. kmem_cache_free(si->scan_leb_slab, seb);
  1137. }
  1138. list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
  1139. list_del(&seb->u.list);
  1140. kmem_cache_free(si->scan_leb_slab, seb);
  1141. }
  1142. list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
  1143. list_del(&seb->u.list);
  1144. kmem_cache_free(si->scan_leb_slab, seb);
  1145. }
  1146. list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
  1147. list_del(&seb->u.list);
  1148. kmem_cache_free(si->scan_leb_slab, seb);
  1149. }
  1150. /* Destroy the volume RB-tree */
  1151. rb = si->volumes.rb_node;
  1152. while (rb) {
  1153. if (rb->rb_left)
  1154. rb = rb->rb_left;
  1155. else if (rb->rb_right)
  1156. rb = rb->rb_right;
  1157. else {
  1158. sv = rb_entry(rb, struct ubi_scan_volume, rb);
  1159. rb = rb_parent(rb);
  1160. if (rb) {
  1161. if (rb->rb_left == &sv->rb)
  1162. rb->rb_left = NULL;
  1163. else
  1164. rb->rb_right = NULL;
  1165. }
  1166. destroy_sv(si, sv);
  1167. }
  1168. }
  1169. if (si->scan_leb_slab)
  1170. kmem_cache_destroy(si->scan_leb_slab);
  1171. kfree(si);
  1172. }
  1173. #ifdef CONFIG_MTD_UBI_DEBUG
  1174. /**
  1175. * paranoid_check_si - check the scanning information.
  1176. * @ubi: UBI device description object
  1177. * @si: scanning information
  1178. *
  1179. * This function returns zero if the scanning information is all right, and a
  1180. * negative error code if not or if an error occurred.
  1181. */
  1182. static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
  1183. {
  1184. int pnum, err, vols_found = 0;
  1185. struct rb_node *rb1, *rb2;
  1186. struct ubi_scan_volume *sv;
  1187. struct ubi_scan_leb *seb, *last_seb;
  1188. uint8_t *buf;
  1189. if (!ubi->dbg->chk_gen)
  1190. return 0;
  1191. /*
  1192. * At first, check that scanning information is OK.
  1193. */
  1194. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1195. int leb_count = 0;
  1196. cond_resched();
  1197. vols_found += 1;
  1198. if (si->is_empty) {
  1199. ubi_err("bad is_empty flag");
  1200. goto bad_sv;
  1201. }
  1202. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  1203. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  1204. sv->data_pad < 0 || sv->last_data_size < 0) {
  1205. ubi_err("negative values");
  1206. goto bad_sv;
  1207. }
  1208. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  1209. sv->vol_id < UBI_INTERNAL_VOL_START) {
  1210. ubi_err("bad vol_id");
  1211. goto bad_sv;
  1212. }
  1213. if (sv->vol_id > si->highest_vol_id) {
  1214. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  1215. si->highest_vol_id, sv->vol_id);
  1216. goto out;
  1217. }
  1218. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  1219. sv->vol_type != UBI_STATIC_VOLUME) {
  1220. ubi_err("bad vol_type");
  1221. goto bad_sv;
  1222. }
  1223. if (sv->data_pad > ubi->leb_size / 2) {
  1224. ubi_err("bad data_pad");
  1225. goto bad_sv;
  1226. }
  1227. last_seb = NULL;
  1228. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1229. cond_resched();
  1230. last_seb = seb;
  1231. leb_count += 1;
  1232. if (seb->pnum < 0 || seb->ec < 0) {
  1233. ubi_err("negative values");
  1234. goto bad_seb;
  1235. }
  1236. if (seb->ec < si->min_ec) {
  1237. ubi_err("bad si->min_ec (%d), %d found",
  1238. si->min_ec, seb->ec);
  1239. goto bad_seb;
  1240. }
  1241. if (seb->ec > si->max_ec) {
  1242. ubi_err("bad si->max_ec (%d), %d found",
  1243. si->max_ec, seb->ec);
  1244. goto bad_seb;
  1245. }
  1246. if (seb->pnum >= ubi->peb_count) {
  1247. ubi_err("too high PEB number %d, total PEBs %d",
  1248. seb->pnum, ubi->peb_count);
  1249. goto bad_seb;
  1250. }
  1251. if (sv->vol_type == UBI_STATIC_VOLUME) {
  1252. if (seb->lnum >= sv->used_ebs) {
  1253. ubi_err("bad lnum or used_ebs");
  1254. goto bad_seb;
  1255. }
  1256. } else {
  1257. if (sv->used_ebs != 0) {
  1258. ubi_err("non-zero used_ebs");
  1259. goto bad_seb;
  1260. }
  1261. }
  1262. if (seb->lnum > sv->highest_lnum) {
  1263. ubi_err("incorrect highest_lnum or lnum");
  1264. goto bad_seb;
  1265. }
  1266. }
  1267. if (sv->leb_count != leb_count) {
  1268. ubi_err("bad leb_count, %d objects in the tree",
  1269. leb_count);
  1270. goto bad_sv;
  1271. }
  1272. if (!last_seb)
  1273. continue;
  1274. seb = last_seb;
  1275. if (seb->lnum != sv->highest_lnum) {
  1276. ubi_err("bad highest_lnum");
  1277. goto bad_seb;
  1278. }
  1279. }
  1280. if (vols_found != si->vols_found) {
  1281. ubi_err("bad si->vols_found %d, should be %d",
  1282. si->vols_found, vols_found);
  1283. goto out;
  1284. }
  1285. /* Check that scanning information is correct */
  1286. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1287. last_seb = NULL;
  1288. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1289. int vol_type;
  1290. cond_resched();
  1291. last_seb = seb;
  1292. err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
  1293. if (err && err != UBI_IO_BITFLIPS) {
  1294. ubi_err("VID header is not OK (%d)", err);
  1295. if (err > 0)
  1296. err = -EIO;
  1297. return err;
  1298. }
  1299. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1300. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1301. if (sv->vol_type != vol_type) {
  1302. ubi_err("bad vol_type");
  1303. goto bad_vid_hdr;
  1304. }
  1305. if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1306. ubi_err("bad sqnum %llu", seb->sqnum);
  1307. goto bad_vid_hdr;
  1308. }
  1309. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1310. ubi_err("bad vol_id %d", sv->vol_id);
  1311. goto bad_vid_hdr;
  1312. }
  1313. if (sv->compat != vidh->compat) {
  1314. ubi_err("bad compat %d", vidh->compat);
  1315. goto bad_vid_hdr;
  1316. }
  1317. if (seb->lnum != be32_to_cpu(vidh->lnum)) {
  1318. ubi_err("bad lnum %d", seb->lnum);
  1319. goto bad_vid_hdr;
  1320. }
  1321. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1322. ubi_err("bad used_ebs %d", sv->used_ebs);
  1323. goto bad_vid_hdr;
  1324. }
  1325. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1326. ubi_err("bad data_pad %d", sv->data_pad);
  1327. goto bad_vid_hdr;
  1328. }
  1329. }
  1330. if (!last_seb)
  1331. continue;
  1332. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1333. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1334. goto bad_vid_hdr;
  1335. }
  1336. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1337. ubi_err("bad last_data_size %d", sv->last_data_size);
  1338. goto bad_vid_hdr;
  1339. }
  1340. }
  1341. /*
  1342. * Make sure that all the physical eraseblocks are in one of the lists
  1343. * or trees.
  1344. */
  1345. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1346. if (!buf)
  1347. return -ENOMEM;
  1348. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1349. err = ubi_io_is_bad(ubi, pnum);
  1350. if (err < 0) {
  1351. kfree(buf);
  1352. return err;
  1353. } else if (err)
  1354. buf[pnum] = 1;
  1355. }
  1356. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1357. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
  1358. buf[seb->pnum] = 1;
  1359. list_for_each_entry(seb, &si->free, u.list)
  1360. buf[seb->pnum] = 1;
  1361. list_for_each_entry(seb, &si->corr, u.list)
  1362. buf[seb->pnum] = 1;
  1363. list_for_each_entry(seb, &si->erase, u.list)
  1364. buf[seb->pnum] = 1;
  1365. list_for_each_entry(seb, &si->alien, u.list)
  1366. buf[seb->pnum] = 1;
  1367. err = 0;
  1368. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1369. if (!buf[pnum]) {
  1370. ubi_err("PEB %d is not referred", pnum);
  1371. err = 1;
  1372. }
  1373. kfree(buf);
  1374. if (err)
  1375. goto out;
  1376. return 0;
  1377. bad_seb:
  1378. ubi_err("bad scanning information about LEB %d", seb->lnum);
  1379. ubi_dbg_dump_seb(seb, 0);
  1380. ubi_dbg_dump_sv(sv);
  1381. goto out;
  1382. bad_sv:
  1383. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1384. ubi_dbg_dump_sv(sv);
  1385. goto out;
  1386. bad_vid_hdr:
  1387. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1388. ubi_dbg_dump_sv(sv);
  1389. ubi_dbg_dump_vid_hdr(vidh);
  1390. out:
  1391. ubi_dbg_dump_stack();
  1392. return -EINVAL;
  1393. }
  1394. #endif /* CONFIG_MTD_UBI_DEBUG */