scan.c 41 KB

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