scan.c 44 KB

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