scan.c 43 KB

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