attach.c 47 KB

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