attach.c 47 KB

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