fastmap.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /*
  2. * Copyright (c) 2012 Linutronix GmbH
  3. * Copyright (c) 2014 sigma star gmbh
  4. * Author: Richard Weinberger <richard@nod.at>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/crc32.h>
  17. #include <linux/bitmap.h>
  18. #include "ubi.h"
  19. /**
  20. * init_seen - allocate memory for used for debugging.
  21. * @ubi: UBI device description object
  22. */
  23. static inline unsigned long *init_seen(struct ubi_device *ubi)
  24. {
  25. unsigned long *ret;
  26. if (!ubi_dbg_chk_fastmap(ubi))
  27. return NULL;
  28. ret = kcalloc(BITS_TO_LONGS(ubi->peb_count), sizeof(unsigned long),
  29. GFP_KERNEL);
  30. if (!ret)
  31. return ERR_PTR(-ENOMEM);
  32. return ret;
  33. }
  34. /**
  35. * free_seen - free the seen logic integer array.
  36. * @seen: integer array of @ubi->peb_count size
  37. */
  38. static inline void free_seen(unsigned long *seen)
  39. {
  40. kfree(seen);
  41. }
  42. /**
  43. * set_seen - mark a PEB as seen.
  44. * @ubi: UBI device description object
  45. * @pnum: The PEB to be makred as seen
  46. * @seen: integer array of @ubi->peb_count size
  47. */
  48. static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen)
  49. {
  50. if (!ubi_dbg_chk_fastmap(ubi) || !seen)
  51. return;
  52. set_bit(pnum, seen);
  53. }
  54. /**
  55. * self_check_seen - check whether all PEB have been seen by fastmap.
  56. * @ubi: UBI device description object
  57. * @seen: integer array of @ubi->peb_count size
  58. */
  59. static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
  60. {
  61. int pnum, ret = 0;
  62. if (!ubi_dbg_chk_fastmap(ubi) || !seen)
  63. return 0;
  64. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  65. if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
  66. ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
  67. ret = -EINVAL;
  68. }
  69. }
  70. return ret;
  71. }
  72. /**
  73. * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
  74. * @ubi: UBI device description object
  75. */
  76. size_t ubi_calc_fm_size(struct ubi_device *ubi)
  77. {
  78. size_t size;
  79. size = sizeof(struct ubi_fm_sb) +
  80. sizeof(struct ubi_fm_hdr) +
  81. sizeof(struct ubi_fm_scan_pool) +
  82. sizeof(struct ubi_fm_scan_pool) +
  83. (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
  84. (sizeof(struct ubi_fm_eba) +
  85. (ubi->peb_count * sizeof(__be32))) +
  86. sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
  87. return roundup(size, ubi->leb_size);
  88. }
  89. /**
  90. * new_fm_vhdr - allocate a new volume header for fastmap usage.
  91. * @ubi: UBI device description object
  92. * @vol_id: the VID of the new header
  93. *
  94. * Returns a new struct ubi_vid_hdr on success.
  95. * NULL indicates out of memory.
  96. */
  97. static struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id)
  98. {
  99. struct ubi_vid_io_buf *new;
  100. struct ubi_vid_hdr *vh;
  101. new = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
  102. if (!new)
  103. goto out;
  104. vh = ubi_get_vid_hdr(new);
  105. vh->vol_type = UBI_VID_DYNAMIC;
  106. vh->vol_id = cpu_to_be32(vol_id);
  107. /* UBI implementations without fastmap support have to delete the
  108. * fastmap.
  109. */
  110. vh->compat = UBI_COMPAT_DELETE;
  111. out:
  112. return new;
  113. }
  114. /**
  115. * add_aeb - create and add a attach erase block to a given list.
  116. * @ai: UBI attach info object
  117. * @list: the target list
  118. * @pnum: PEB number of the new attach erase block
  119. * @ec: erease counter of the new LEB
  120. * @scrub: scrub this PEB after attaching
  121. *
  122. * Returns 0 on success, < 0 indicates an internal error.
  123. */
  124. static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
  125. int pnum, int ec, int scrub)
  126. {
  127. struct ubi_ainf_peb *aeb;
  128. aeb = ubi_alloc_aeb(ai, pnum, ec);
  129. if (!aeb)
  130. return -ENOMEM;
  131. aeb->lnum = -1;
  132. aeb->scrub = scrub;
  133. aeb->copy_flag = aeb->sqnum = 0;
  134. ai->ec_sum += aeb->ec;
  135. ai->ec_count++;
  136. if (ai->max_ec < aeb->ec)
  137. ai->max_ec = aeb->ec;
  138. if (ai->min_ec > aeb->ec)
  139. ai->min_ec = aeb->ec;
  140. list_add_tail(&aeb->u.list, list);
  141. return 0;
  142. }
  143. /**
  144. * add_vol - create and add a new volume to ubi_attach_info.
  145. * @ai: ubi_attach_info object
  146. * @vol_id: VID of the new volume
  147. * @used_ebs: number of used EBS
  148. * @data_pad: data padding value of the new volume
  149. * @vol_type: volume type
  150. * @last_eb_bytes: number of bytes in the last LEB
  151. *
  152. * Returns the new struct ubi_ainf_volume on success.
  153. * NULL indicates an error.
  154. */
  155. static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
  156. int used_ebs, int data_pad, u8 vol_type,
  157. int last_eb_bytes)
  158. {
  159. struct ubi_ainf_volume *av;
  160. av = ubi_add_av(ai, vol_id);
  161. if (IS_ERR(av))
  162. return av;
  163. av->data_pad = data_pad;
  164. av->last_data_size = last_eb_bytes;
  165. av->compat = 0;
  166. av->vol_type = vol_type;
  167. if (av->vol_type == UBI_STATIC_VOLUME)
  168. av->used_ebs = used_ebs;
  169. dbg_bld("found volume (ID %i)", vol_id);
  170. return av;
  171. }
  172. /**
  173. * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
  174. * from it's original list.
  175. * @ai: ubi_attach_info object
  176. * @aeb: the to be assigned SEB
  177. * @av: target scan volume
  178. */
  179. static void assign_aeb_to_av(struct ubi_attach_info *ai,
  180. struct ubi_ainf_peb *aeb,
  181. struct ubi_ainf_volume *av)
  182. {
  183. struct ubi_ainf_peb *tmp_aeb;
  184. struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
  185. p = &av->root.rb_node;
  186. while (*p) {
  187. parent = *p;
  188. tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
  189. if (aeb->lnum != tmp_aeb->lnum) {
  190. if (aeb->lnum < tmp_aeb->lnum)
  191. p = &(*p)->rb_left;
  192. else
  193. p = &(*p)->rb_right;
  194. continue;
  195. } else
  196. break;
  197. }
  198. list_del(&aeb->u.list);
  199. av->leb_count++;
  200. rb_link_node(&aeb->u.rb, parent, p);
  201. rb_insert_color(&aeb->u.rb, &av->root);
  202. }
  203. /**
  204. * update_vol - inserts or updates a LEB which was found a pool.
  205. * @ubi: the UBI device object
  206. * @ai: attach info object
  207. * @av: the volume this LEB belongs to
  208. * @new_vh: the volume header derived from new_aeb
  209. * @new_aeb: the AEB to be examined
  210. *
  211. * Returns 0 on success, < 0 indicates an internal error.
  212. */
  213. static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
  214. struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
  215. struct ubi_ainf_peb *new_aeb)
  216. {
  217. struct rb_node **p = &av->root.rb_node, *parent = NULL;
  218. struct ubi_ainf_peb *aeb, *victim;
  219. int cmp_res;
  220. while (*p) {
  221. parent = *p;
  222. aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
  223. if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
  224. if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
  225. p = &(*p)->rb_left;
  226. else
  227. p = &(*p)->rb_right;
  228. continue;
  229. }
  230. /* This case can happen if the fastmap gets written
  231. * because of a volume change (creation, deletion, ..).
  232. * Then a PEB can be within the persistent EBA and the pool.
  233. */
  234. if (aeb->pnum == new_aeb->pnum) {
  235. ubi_assert(aeb->lnum == new_aeb->lnum);
  236. ubi_free_aeb(ai, new_aeb);
  237. return 0;
  238. }
  239. cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
  240. if (cmp_res < 0)
  241. return cmp_res;
  242. /* new_aeb is newer */
  243. if (cmp_res & 1) {
  244. victim = ubi_alloc_aeb(ai, aeb->pnum, aeb->ec);
  245. if (!victim)
  246. return -ENOMEM;
  247. list_add_tail(&victim->u.list, &ai->erase);
  248. if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
  249. av->last_data_size =
  250. be32_to_cpu(new_vh->data_size);
  251. dbg_bld("vol %i: AEB %i's PEB %i is the newer",
  252. av->vol_id, aeb->lnum, new_aeb->pnum);
  253. aeb->ec = new_aeb->ec;
  254. aeb->pnum = new_aeb->pnum;
  255. aeb->copy_flag = new_vh->copy_flag;
  256. aeb->scrub = new_aeb->scrub;
  257. aeb->sqnum = new_aeb->sqnum;
  258. ubi_free_aeb(ai, new_aeb);
  259. /* new_aeb is older */
  260. } else {
  261. dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
  262. av->vol_id, aeb->lnum, new_aeb->pnum);
  263. list_add_tail(&new_aeb->u.list, &ai->erase);
  264. }
  265. return 0;
  266. }
  267. /* This LEB is new, let's add it to the volume */
  268. if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
  269. av->highest_lnum = be32_to_cpu(new_vh->lnum);
  270. av->last_data_size = be32_to_cpu(new_vh->data_size);
  271. }
  272. if (av->vol_type == UBI_STATIC_VOLUME)
  273. av->used_ebs = be32_to_cpu(new_vh->used_ebs);
  274. av->leb_count++;
  275. rb_link_node(&new_aeb->u.rb, parent, p);
  276. rb_insert_color(&new_aeb->u.rb, &av->root);
  277. return 0;
  278. }
  279. /**
  280. * process_pool_aeb - we found a non-empty PEB in a pool.
  281. * @ubi: UBI device object
  282. * @ai: attach info object
  283. * @new_vh: the volume header derived from new_aeb
  284. * @new_aeb: the AEB to be examined
  285. *
  286. * Returns 0 on success, < 0 indicates an internal error.
  287. */
  288. static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
  289. struct ubi_vid_hdr *new_vh,
  290. struct ubi_ainf_peb *new_aeb)
  291. {
  292. int vol_id = be32_to_cpu(new_vh->vol_id);
  293. struct ubi_ainf_volume *av;
  294. if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) {
  295. ubi_free_aeb(ai, new_aeb);
  296. return 0;
  297. }
  298. /* Find the volume this SEB belongs to */
  299. av = ubi_find_av(ai, vol_id);
  300. if (!av) {
  301. ubi_err(ubi, "orphaned volume in fastmap pool!");
  302. ubi_free_aeb(ai, new_aeb);
  303. return UBI_BAD_FASTMAP;
  304. }
  305. ubi_assert(vol_id == av->vol_id);
  306. return update_vol(ubi, ai, av, new_vh, new_aeb);
  307. }
  308. /**
  309. * unmap_peb - unmap a PEB.
  310. * If fastmap detects a free PEB in the pool it has to check whether
  311. * this PEB has been unmapped after writing the fastmap.
  312. *
  313. * @ai: UBI attach info object
  314. * @pnum: The PEB to be unmapped
  315. */
  316. static void unmap_peb(struct ubi_attach_info *ai, int pnum)
  317. {
  318. struct ubi_ainf_volume *av;
  319. struct rb_node *node, *node2;
  320. struct ubi_ainf_peb *aeb;
  321. ubi_rb_for_each_entry(node, av, &ai->volumes, rb) {
  322. ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) {
  323. if (aeb->pnum == pnum) {
  324. rb_erase(&aeb->u.rb, &av->root);
  325. av->leb_count--;
  326. ubi_free_aeb(ai, aeb);
  327. return;
  328. }
  329. }
  330. }
  331. }
  332. /**
  333. * scan_pool - scans a pool for changed (no longer empty PEBs).
  334. * @ubi: UBI device object
  335. * @ai: attach info object
  336. * @pebs: an array of all PEB numbers in the to be scanned pool
  337. * @pool_size: size of the pool (number of entries in @pebs)
  338. * @max_sqnum: pointer to the maximal sequence number
  339. * @free: list of PEBs which are most likely free (and go into @ai->free)
  340. *
  341. * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
  342. * < 0 indicates an internal error.
  343. */
  344. static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
  345. __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
  346. struct list_head *free)
  347. {
  348. struct ubi_vid_io_buf *vb;
  349. struct ubi_vid_hdr *vh;
  350. struct ubi_ec_hdr *ech;
  351. struct ubi_ainf_peb *new_aeb;
  352. int i, pnum, err, ret = 0;
  353. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  354. if (!ech)
  355. return -ENOMEM;
  356. vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
  357. if (!vb) {
  358. kfree(ech);
  359. return -ENOMEM;
  360. }
  361. vh = ubi_get_vid_hdr(vb);
  362. dbg_bld("scanning fastmap pool: size = %i", pool_size);
  363. /*
  364. * Now scan all PEBs in the pool to find changes which have been made
  365. * after the creation of the fastmap
  366. */
  367. for (i = 0; i < pool_size; i++) {
  368. int scrub = 0;
  369. int image_seq;
  370. pnum = be32_to_cpu(pebs[i]);
  371. if (ubi_io_is_bad(ubi, pnum)) {
  372. ubi_err(ubi, "bad PEB in fastmap pool!");
  373. ret = UBI_BAD_FASTMAP;
  374. goto out;
  375. }
  376. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  377. if (err && err != UBI_IO_BITFLIPS) {
  378. ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
  379. pnum, err);
  380. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  381. goto out;
  382. } else if (err == UBI_IO_BITFLIPS)
  383. scrub = 1;
  384. /*
  385. * Older UBI implementations have image_seq set to zero, so
  386. * we shouldn't fail if image_seq == 0.
  387. */
  388. image_seq = be32_to_cpu(ech->image_seq);
  389. if (image_seq && (image_seq != ubi->image_seq)) {
  390. ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
  391. be32_to_cpu(ech->image_seq), ubi->image_seq);
  392. ret = UBI_BAD_FASTMAP;
  393. goto out;
  394. }
  395. err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
  396. if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
  397. unsigned long long ec = be64_to_cpu(ech->ec);
  398. unmap_peb(ai, pnum);
  399. dbg_bld("Adding PEB to free: %i", pnum);
  400. if (err == UBI_IO_FF_BITFLIPS)
  401. scrub = 1;
  402. add_aeb(ai, free, pnum, ec, scrub);
  403. continue;
  404. } else if (err == 0 || err == UBI_IO_BITFLIPS) {
  405. dbg_bld("Found non empty PEB:%i in pool", pnum);
  406. if (err == UBI_IO_BITFLIPS)
  407. scrub = 1;
  408. new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec));
  409. if (!new_aeb) {
  410. ret = -ENOMEM;
  411. goto out;
  412. }
  413. new_aeb->lnum = be32_to_cpu(vh->lnum);
  414. new_aeb->sqnum = be64_to_cpu(vh->sqnum);
  415. new_aeb->copy_flag = vh->copy_flag;
  416. new_aeb->scrub = scrub;
  417. if (*max_sqnum < new_aeb->sqnum)
  418. *max_sqnum = new_aeb->sqnum;
  419. err = process_pool_aeb(ubi, ai, vh, new_aeb);
  420. if (err) {
  421. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  422. goto out;
  423. }
  424. } else {
  425. /* We are paranoid and fall back to scanning mode */
  426. ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
  427. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  428. goto out;
  429. }
  430. }
  431. out:
  432. ubi_free_vid_buf(vb);
  433. kfree(ech);
  434. return ret;
  435. }
  436. /**
  437. * count_fastmap_pebs - Counts the PEBs found by fastmap.
  438. * @ai: The UBI attach info object
  439. */
  440. static int count_fastmap_pebs(struct ubi_attach_info *ai)
  441. {
  442. struct ubi_ainf_peb *aeb;
  443. struct ubi_ainf_volume *av;
  444. struct rb_node *rb1, *rb2;
  445. int n = 0;
  446. list_for_each_entry(aeb, &ai->erase, u.list)
  447. n++;
  448. list_for_each_entry(aeb, &ai->free, u.list)
  449. n++;
  450. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
  451. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
  452. n++;
  453. return n;
  454. }
  455. /**
  456. * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
  457. * @ubi: UBI device object
  458. * @ai: UBI attach info object
  459. * @fm: the fastmap to be attached
  460. *
  461. * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
  462. * < 0 indicates an internal error.
  463. */
  464. static int ubi_attach_fastmap(struct ubi_device *ubi,
  465. struct ubi_attach_info *ai,
  466. struct ubi_fastmap_layout *fm)
  467. {
  468. struct list_head used, free;
  469. struct ubi_ainf_volume *av;
  470. struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
  471. struct ubi_fm_sb *fmsb;
  472. struct ubi_fm_hdr *fmhdr;
  473. struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
  474. struct ubi_fm_ec *fmec;
  475. struct ubi_fm_volhdr *fmvhdr;
  476. struct ubi_fm_eba *fm_eba;
  477. int ret, i, j, pool_size, wl_pool_size;
  478. size_t fm_pos = 0, fm_size = ubi->fm_size;
  479. unsigned long long max_sqnum = 0;
  480. void *fm_raw = ubi->fm_buf;
  481. INIT_LIST_HEAD(&used);
  482. INIT_LIST_HEAD(&free);
  483. ai->min_ec = UBI_MAX_ERASECOUNTER;
  484. fmsb = (struct ubi_fm_sb *)(fm_raw);
  485. ai->max_sqnum = fmsb->sqnum;
  486. fm_pos += sizeof(struct ubi_fm_sb);
  487. if (fm_pos >= fm_size)
  488. goto fail_bad;
  489. fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  490. fm_pos += sizeof(*fmhdr);
  491. if (fm_pos >= fm_size)
  492. goto fail_bad;
  493. if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
  494. ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
  495. be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
  496. goto fail_bad;
  497. }
  498. fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  499. fm_pos += sizeof(*fmpl);
  500. if (fm_pos >= fm_size)
  501. goto fail_bad;
  502. if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
  503. ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
  504. be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
  505. goto fail_bad;
  506. }
  507. fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  508. fm_pos += sizeof(*fmpl_wl);
  509. if (fm_pos >= fm_size)
  510. goto fail_bad;
  511. if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
  512. ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
  513. be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
  514. goto fail_bad;
  515. }
  516. pool_size = be16_to_cpu(fmpl->size);
  517. wl_pool_size = be16_to_cpu(fmpl_wl->size);
  518. fm->max_pool_size = be16_to_cpu(fmpl->max_size);
  519. fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
  520. if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
  521. ubi_err(ubi, "bad pool size: %i", pool_size);
  522. goto fail_bad;
  523. }
  524. if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
  525. ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
  526. goto fail_bad;
  527. }
  528. if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
  529. fm->max_pool_size < 0) {
  530. ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
  531. goto fail_bad;
  532. }
  533. if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
  534. fm->max_wl_pool_size < 0) {
  535. ubi_err(ubi, "bad maximal WL pool size: %i",
  536. fm->max_wl_pool_size);
  537. goto fail_bad;
  538. }
  539. /* read EC values from free list */
  540. for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
  541. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  542. fm_pos += sizeof(*fmec);
  543. if (fm_pos >= fm_size)
  544. goto fail_bad;
  545. add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
  546. be32_to_cpu(fmec->ec), 0);
  547. }
  548. /* read EC values from used list */
  549. for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
  550. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  551. fm_pos += sizeof(*fmec);
  552. if (fm_pos >= fm_size)
  553. goto fail_bad;
  554. add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
  555. be32_to_cpu(fmec->ec), 0);
  556. }
  557. /* read EC values from scrub list */
  558. for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
  559. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  560. fm_pos += sizeof(*fmec);
  561. if (fm_pos >= fm_size)
  562. goto fail_bad;
  563. add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
  564. be32_to_cpu(fmec->ec), 1);
  565. }
  566. /* read EC values from erase list */
  567. for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
  568. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  569. fm_pos += sizeof(*fmec);
  570. if (fm_pos >= fm_size)
  571. goto fail_bad;
  572. add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
  573. be32_to_cpu(fmec->ec), 1);
  574. }
  575. ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
  576. ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
  577. /* Iterate over all volumes and read their EBA table */
  578. for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
  579. fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  580. fm_pos += sizeof(*fmvhdr);
  581. if (fm_pos >= fm_size)
  582. goto fail_bad;
  583. if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
  584. ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
  585. be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
  586. goto fail_bad;
  587. }
  588. av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
  589. be32_to_cpu(fmvhdr->used_ebs),
  590. be32_to_cpu(fmvhdr->data_pad),
  591. fmvhdr->vol_type,
  592. be32_to_cpu(fmvhdr->last_eb_bytes));
  593. if (IS_ERR(av)) {
  594. if (PTR_ERR(av) == -EEXIST)
  595. ubi_err(ubi, "volume (ID %i) already exists",
  596. fmvhdr->vol_id);
  597. goto fail_bad;
  598. }
  599. ai->vols_found++;
  600. if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
  601. ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
  602. fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  603. fm_pos += sizeof(*fm_eba);
  604. fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
  605. if (fm_pos >= fm_size)
  606. goto fail_bad;
  607. if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
  608. ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
  609. be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
  610. goto fail_bad;
  611. }
  612. for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
  613. int pnum = be32_to_cpu(fm_eba->pnum[j]);
  614. if (pnum < 0)
  615. continue;
  616. aeb = NULL;
  617. list_for_each_entry(tmp_aeb, &used, u.list) {
  618. if (tmp_aeb->pnum == pnum) {
  619. aeb = tmp_aeb;
  620. break;
  621. }
  622. }
  623. if (!aeb) {
  624. ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
  625. goto fail_bad;
  626. }
  627. aeb->lnum = j;
  628. if (av->highest_lnum <= aeb->lnum)
  629. av->highest_lnum = aeb->lnum;
  630. assign_aeb_to_av(ai, aeb, av);
  631. dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
  632. aeb->pnum, aeb->lnum, av->vol_id);
  633. }
  634. }
  635. ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
  636. if (ret)
  637. goto fail;
  638. ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
  639. if (ret)
  640. goto fail;
  641. if (max_sqnum > ai->max_sqnum)
  642. ai->max_sqnum = max_sqnum;
  643. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
  644. list_move_tail(&tmp_aeb->u.list, &ai->free);
  645. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
  646. list_move_tail(&tmp_aeb->u.list, &ai->erase);
  647. ubi_assert(list_empty(&free));
  648. /*
  649. * If fastmap is leaking PEBs (must not happen), raise a
  650. * fat warning and fall back to scanning mode.
  651. * We do this here because in ubi_wl_init() it's too late
  652. * and we cannot fall back to scanning.
  653. */
  654. if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
  655. ai->bad_peb_count - fm->used_blocks))
  656. goto fail_bad;
  657. return 0;
  658. fail_bad:
  659. ret = UBI_BAD_FASTMAP;
  660. fail:
  661. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
  662. list_del(&tmp_aeb->u.list);
  663. ubi_free_aeb(ai, tmp_aeb);
  664. }
  665. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
  666. list_del(&tmp_aeb->u.list);
  667. ubi_free_aeb(ai, tmp_aeb);
  668. }
  669. return ret;
  670. }
  671. /**
  672. * find_fm_anchor - find the most recent Fastmap superblock (anchor)
  673. * @ai: UBI attach info to be filled
  674. */
  675. static int find_fm_anchor(struct ubi_attach_info *ai)
  676. {
  677. int ret = -1;
  678. struct ubi_ainf_peb *aeb;
  679. unsigned long long max_sqnum = 0;
  680. list_for_each_entry(aeb, &ai->fastmap, u.list) {
  681. if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) {
  682. max_sqnum = aeb->sqnum;
  683. ret = aeb->pnum;
  684. }
  685. }
  686. return ret;
  687. }
  688. static struct ubi_ainf_peb *clone_aeb(struct ubi_attach_info *ai,
  689. struct ubi_ainf_peb *old)
  690. {
  691. struct ubi_ainf_peb *new;
  692. new = ubi_alloc_aeb(ai, old->pnum, old->ec);
  693. if (!new)
  694. return NULL;
  695. new->vol_id = old->vol_id;
  696. new->sqnum = old->sqnum;
  697. new->lnum = old->lnum;
  698. new->scrub = old->scrub;
  699. new->copy_flag = old->copy_flag;
  700. return new;
  701. }
  702. /**
  703. * ubi_scan_fastmap - scan the fastmap.
  704. * @ubi: UBI device object
  705. * @ai: UBI attach info to be filled
  706. * @scan_ai: UBI attach info from the first 64 PEBs,
  707. * used to find the most recent Fastmap data structure
  708. *
  709. * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
  710. * UBI_BAD_FASTMAP if one was found but is not usable.
  711. * < 0 indicates an internal error.
  712. */
  713. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  714. struct ubi_attach_info *scan_ai)
  715. {
  716. struct ubi_fm_sb *fmsb, *fmsb2;
  717. struct ubi_vid_io_buf *vb;
  718. struct ubi_vid_hdr *vh;
  719. struct ubi_ec_hdr *ech;
  720. struct ubi_fastmap_layout *fm;
  721. struct ubi_ainf_peb *aeb;
  722. int i, used_blocks, pnum, fm_anchor, ret = 0;
  723. size_t fm_size;
  724. __be32 crc, tmp_crc;
  725. unsigned long long sqnum = 0;
  726. fm_anchor = find_fm_anchor(scan_ai);
  727. if (fm_anchor < 0)
  728. return UBI_NO_FASTMAP;
  729. /* Copy all (possible) fastmap blocks into our new attach structure. */
  730. list_for_each_entry(aeb, &scan_ai->fastmap, u.list) {
  731. struct ubi_ainf_peb *new;
  732. new = clone_aeb(ai, aeb);
  733. if (!new)
  734. return -ENOMEM;
  735. list_add(&new->u.list, &ai->fastmap);
  736. }
  737. down_write(&ubi->fm_protect);
  738. memset(ubi->fm_buf, 0, ubi->fm_size);
  739. fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
  740. if (!fmsb) {
  741. ret = -ENOMEM;
  742. goto out;
  743. }
  744. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  745. if (!fm) {
  746. ret = -ENOMEM;
  747. kfree(fmsb);
  748. goto out;
  749. }
  750. ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb));
  751. if (ret && ret != UBI_IO_BITFLIPS)
  752. goto free_fm_sb;
  753. else if (ret == UBI_IO_BITFLIPS)
  754. fm->to_be_tortured[0] = 1;
  755. if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
  756. ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
  757. be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
  758. ret = UBI_BAD_FASTMAP;
  759. goto free_fm_sb;
  760. }
  761. if (fmsb->version != UBI_FM_FMT_VERSION) {
  762. ubi_err(ubi, "bad fastmap version: %i, expected: %i",
  763. fmsb->version, UBI_FM_FMT_VERSION);
  764. ret = UBI_BAD_FASTMAP;
  765. goto free_fm_sb;
  766. }
  767. used_blocks = be32_to_cpu(fmsb->used_blocks);
  768. if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
  769. ubi_err(ubi, "number of fastmap blocks is invalid: %i",
  770. used_blocks);
  771. ret = UBI_BAD_FASTMAP;
  772. goto free_fm_sb;
  773. }
  774. fm_size = ubi->leb_size * used_blocks;
  775. if (fm_size != ubi->fm_size) {
  776. ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
  777. fm_size, ubi->fm_size);
  778. ret = UBI_BAD_FASTMAP;
  779. goto free_fm_sb;
  780. }
  781. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  782. if (!ech) {
  783. ret = -ENOMEM;
  784. goto free_fm_sb;
  785. }
  786. vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
  787. if (!vb) {
  788. ret = -ENOMEM;
  789. goto free_hdr;
  790. }
  791. vh = ubi_get_vid_hdr(vb);
  792. for (i = 0; i < used_blocks; i++) {
  793. int image_seq;
  794. pnum = be32_to_cpu(fmsb->block_loc[i]);
  795. if (ubi_io_is_bad(ubi, pnum)) {
  796. ret = UBI_BAD_FASTMAP;
  797. goto free_hdr;
  798. }
  799. if (i == 0 && pnum != fm_anchor) {
  800. ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i",
  801. pnum, fm_anchor);
  802. ret = UBI_BAD_FASTMAP;
  803. goto free_hdr;
  804. }
  805. ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  806. if (ret && ret != UBI_IO_BITFLIPS) {
  807. ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
  808. i, pnum);
  809. if (ret > 0)
  810. ret = UBI_BAD_FASTMAP;
  811. goto free_hdr;
  812. } else if (ret == UBI_IO_BITFLIPS)
  813. fm->to_be_tortured[i] = 1;
  814. image_seq = be32_to_cpu(ech->image_seq);
  815. if (!ubi->image_seq)
  816. ubi->image_seq = image_seq;
  817. /*
  818. * Older UBI implementations have image_seq set to zero, so
  819. * we shouldn't fail if image_seq == 0.
  820. */
  821. if (image_seq && (image_seq != ubi->image_seq)) {
  822. ubi_err(ubi, "wrong image seq:%d instead of %d",
  823. be32_to_cpu(ech->image_seq), ubi->image_seq);
  824. ret = UBI_BAD_FASTMAP;
  825. goto free_hdr;
  826. }
  827. ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
  828. if (ret && ret != UBI_IO_BITFLIPS) {
  829. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
  830. i, pnum);
  831. goto free_hdr;
  832. }
  833. if (i == 0) {
  834. if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
  835. ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
  836. be32_to_cpu(vh->vol_id),
  837. UBI_FM_SB_VOLUME_ID);
  838. ret = UBI_BAD_FASTMAP;
  839. goto free_hdr;
  840. }
  841. } else {
  842. if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
  843. ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
  844. be32_to_cpu(vh->vol_id),
  845. UBI_FM_DATA_VOLUME_ID);
  846. ret = UBI_BAD_FASTMAP;
  847. goto free_hdr;
  848. }
  849. }
  850. if (sqnum < be64_to_cpu(vh->sqnum))
  851. sqnum = be64_to_cpu(vh->sqnum);
  852. ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i),
  853. pnum, 0, ubi->leb_size);
  854. if (ret && ret != UBI_IO_BITFLIPS) {
  855. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
  856. "err: %i)", i, pnum, ret);
  857. goto free_hdr;
  858. }
  859. }
  860. kfree(fmsb);
  861. fmsb = NULL;
  862. fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
  863. tmp_crc = be32_to_cpu(fmsb2->data_crc);
  864. fmsb2->data_crc = 0;
  865. crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
  866. if (crc != tmp_crc) {
  867. ubi_err(ubi, "fastmap data CRC is invalid");
  868. ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
  869. tmp_crc, crc);
  870. ret = UBI_BAD_FASTMAP;
  871. goto free_hdr;
  872. }
  873. fmsb2->sqnum = sqnum;
  874. fm->used_blocks = used_blocks;
  875. ret = ubi_attach_fastmap(ubi, ai, fm);
  876. if (ret) {
  877. if (ret > 0)
  878. ret = UBI_BAD_FASTMAP;
  879. goto free_hdr;
  880. }
  881. for (i = 0; i < used_blocks; i++) {
  882. struct ubi_wl_entry *e;
  883. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  884. if (!e) {
  885. while (i--)
  886. kfree(fm->e[i]);
  887. ret = -ENOMEM;
  888. goto free_hdr;
  889. }
  890. e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
  891. e->ec = be32_to_cpu(fmsb2->block_ec[i]);
  892. fm->e[i] = e;
  893. }
  894. ubi->fm = fm;
  895. ubi->fm_pool.max_size = ubi->fm->max_pool_size;
  896. ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
  897. ubi_msg(ubi, "attached by fastmap");
  898. ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
  899. ubi_msg(ubi, "fastmap WL pool size: %d",
  900. ubi->fm_wl_pool.max_size);
  901. ubi->fm_disabled = 0;
  902. ubi->fast_attach = 1;
  903. ubi_free_vid_buf(vb);
  904. kfree(ech);
  905. out:
  906. up_write(&ubi->fm_protect);
  907. if (ret == UBI_BAD_FASTMAP)
  908. ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
  909. return ret;
  910. free_hdr:
  911. ubi_free_vid_buf(vb);
  912. kfree(ech);
  913. free_fm_sb:
  914. kfree(fmsb);
  915. kfree(fm);
  916. goto out;
  917. }
  918. int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count)
  919. {
  920. struct ubi_device *ubi = vol->ubi;
  921. if (!ubi->fast_attach)
  922. return 0;
  923. vol->checkmap = kcalloc(BITS_TO_LONGS(leb_count), sizeof(unsigned long),
  924. GFP_KERNEL);
  925. if (!vol->checkmap)
  926. return -ENOMEM;
  927. return 0;
  928. }
  929. void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol)
  930. {
  931. kfree(vol->checkmap);
  932. }
  933. /**
  934. * ubi_write_fastmap - writes a fastmap.
  935. * @ubi: UBI device object
  936. * @new_fm: the to be written fastmap
  937. *
  938. * Returns 0 on success, < 0 indicates an internal error.
  939. */
  940. static int ubi_write_fastmap(struct ubi_device *ubi,
  941. struct ubi_fastmap_layout *new_fm)
  942. {
  943. size_t fm_pos = 0;
  944. void *fm_raw;
  945. struct ubi_fm_sb *fmsb;
  946. struct ubi_fm_hdr *fmh;
  947. struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
  948. struct ubi_fm_ec *fec;
  949. struct ubi_fm_volhdr *fvh;
  950. struct ubi_fm_eba *feba;
  951. struct ubi_wl_entry *wl_e;
  952. struct ubi_volume *vol;
  953. struct ubi_vid_io_buf *avbuf, *dvbuf;
  954. struct ubi_vid_hdr *avhdr, *dvhdr;
  955. struct ubi_work *ubi_wrk;
  956. struct rb_node *tmp_rb;
  957. int ret, i, j, free_peb_count, used_peb_count, vol_count;
  958. int scrub_peb_count, erase_peb_count;
  959. unsigned long *seen_pebs;
  960. fm_raw = ubi->fm_buf;
  961. memset(ubi->fm_buf, 0, ubi->fm_size);
  962. avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
  963. if (!avbuf) {
  964. ret = -ENOMEM;
  965. goto out;
  966. }
  967. dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
  968. if (!dvbuf) {
  969. ret = -ENOMEM;
  970. goto out_free_avbuf;
  971. }
  972. avhdr = ubi_get_vid_hdr(avbuf);
  973. dvhdr = ubi_get_vid_hdr(dvbuf);
  974. seen_pebs = init_seen(ubi);
  975. if (IS_ERR(seen_pebs)) {
  976. ret = PTR_ERR(seen_pebs);
  977. goto out_free_dvbuf;
  978. }
  979. spin_lock(&ubi->volumes_lock);
  980. spin_lock(&ubi->wl_lock);
  981. fmsb = (struct ubi_fm_sb *)fm_raw;
  982. fm_pos += sizeof(*fmsb);
  983. ubi_assert(fm_pos <= ubi->fm_size);
  984. fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  985. fm_pos += sizeof(*fmh);
  986. ubi_assert(fm_pos <= ubi->fm_size);
  987. fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
  988. fmsb->version = UBI_FM_FMT_VERSION;
  989. fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
  990. /* the max sqnum will be filled in while *reading* the fastmap */
  991. fmsb->sqnum = 0;
  992. fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
  993. free_peb_count = 0;
  994. used_peb_count = 0;
  995. scrub_peb_count = 0;
  996. erase_peb_count = 0;
  997. vol_count = 0;
  998. fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  999. fm_pos += sizeof(*fmpl);
  1000. fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  1001. fmpl->size = cpu_to_be16(ubi->fm_pool.size);
  1002. fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
  1003. for (i = 0; i < ubi->fm_pool.size; i++) {
  1004. fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
  1005. set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
  1006. }
  1007. fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  1008. fm_pos += sizeof(*fmpl_wl);
  1009. fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  1010. fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
  1011. fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
  1012. for (i = 0; i < ubi->fm_wl_pool.size; i++) {
  1013. fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
  1014. set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
  1015. }
  1016. ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
  1017. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1018. fec->pnum = cpu_to_be32(wl_e->pnum);
  1019. set_seen(ubi, wl_e->pnum, seen_pebs);
  1020. fec->ec = cpu_to_be32(wl_e->ec);
  1021. free_peb_count++;
  1022. fm_pos += sizeof(*fec);
  1023. ubi_assert(fm_pos <= ubi->fm_size);
  1024. }
  1025. fmh->free_peb_count = cpu_to_be32(free_peb_count);
  1026. ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
  1027. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1028. fec->pnum = cpu_to_be32(wl_e->pnum);
  1029. set_seen(ubi, wl_e->pnum, seen_pebs);
  1030. fec->ec = cpu_to_be32(wl_e->ec);
  1031. used_peb_count++;
  1032. fm_pos += sizeof(*fec);
  1033. ubi_assert(fm_pos <= ubi->fm_size);
  1034. }
  1035. ubi_for_each_protected_peb(ubi, i, wl_e) {
  1036. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1037. fec->pnum = cpu_to_be32(wl_e->pnum);
  1038. set_seen(ubi, wl_e->pnum, seen_pebs);
  1039. fec->ec = cpu_to_be32(wl_e->ec);
  1040. used_peb_count++;
  1041. fm_pos += sizeof(*fec);
  1042. ubi_assert(fm_pos <= ubi->fm_size);
  1043. }
  1044. fmh->used_peb_count = cpu_to_be32(used_peb_count);
  1045. ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
  1046. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1047. fec->pnum = cpu_to_be32(wl_e->pnum);
  1048. set_seen(ubi, wl_e->pnum, seen_pebs);
  1049. fec->ec = cpu_to_be32(wl_e->ec);
  1050. scrub_peb_count++;
  1051. fm_pos += sizeof(*fec);
  1052. ubi_assert(fm_pos <= ubi->fm_size);
  1053. }
  1054. fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
  1055. list_for_each_entry(ubi_wrk, &ubi->works, list) {
  1056. if (ubi_is_erase_work(ubi_wrk)) {
  1057. wl_e = ubi_wrk->e;
  1058. ubi_assert(wl_e);
  1059. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1060. fec->pnum = cpu_to_be32(wl_e->pnum);
  1061. set_seen(ubi, wl_e->pnum, seen_pebs);
  1062. fec->ec = cpu_to_be32(wl_e->ec);
  1063. erase_peb_count++;
  1064. fm_pos += sizeof(*fec);
  1065. ubi_assert(fm_pos <= ubi->fm_size);
  1066. }
  1067. }
  1068. fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
  1069. for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
  1070. vol = ubi->volumes[i];
  1071. if (!vol)
  1072. continue;
  1073. vol_count++;
  1074. fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  1075. fm_pos += sizeof(*fvh);
  1076. ubi_assert(fm_pos <= ubi->fm_size);
  1077. fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
  1078. fvh->vol_id = cpu_to_be32(vol->vol_id);
  1079. fvh->vol_type = vol->vol_type;
  1080. fvh->used_ebs = cpu_to_be32(vol->used_ebs);
  1081. fvh->data_pad = cpu_to_be32(vol->data_pad);
  1082. fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
  1083. ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
  1084. vol->vol_type == UBI_STATIC_VOLUME);
  1085. feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  1086. fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
  1087. ubi_assert(fm_pos <= ubi->fm_size);
  1088. for (j = 0; j < vol->reserved_pebs; j++) {
  1089. struct ubi_eba_leb_desc ldesc;
  1090. ubi_eba_get_ldesc(vol, j, &ldesc);
  1091. feba->pnum[j] = cpu_to_be32(ldesc.pnum);
  1092. }
  1093. feba->reserved_pebs = cpu_to_be32(j);
  1094. feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
  1095. }
  1096. fmh->vol_count = cpu_to_be32(vol_count);
  1097. fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
  1098. avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1099. avhdr->lnum = 0;
  1100. spin_unlock(&ubi->wl_lock);
  1101. spin_unlock(&ubi->volumes_lock);
  1102. dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
  1103. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
  1104. if (ret) {
  1105. ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
  1106. goto out_free_seen;
  1107. }
  1108. for (i = 0; i < new_fm->used_blocks; i++) {
  1109. fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
  1110. set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
  1111. fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
  1112. }
  1113. fmsb->data_crc = 0;
  1114. fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
  1115. ubi->fm_size));
  1116. for (i = 1; i < new_fm->used_blocks; i++) {
  1117. dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1118. dvhdr->lnum = cpu_to_be32(i);
  1119. dbg_bld("writing fastmap data to PEB %i sqnum %llu",
  1120. new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
  1121. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf);
  1122. if (ret) {
  1123. ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
  1124. new_fm->e[i]->pnum);
  1125. goto out_free_seen;
  1126. }
  1127. }
  1128. for (i = 0; i < new_fm->used_blocks; i++) {
  1129. ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size),
  1130. new_fm->e[i]->pnum, 0, ubi->leb_size);
  1131. if (ret) {
  1132. ubi_err(ubi, "unable to write fastmap to PEB %i!",
  1133. new_fm->e[i]->pnum);
  1134. goto out_free_seen;
  1135. }
  1136. }
  1137. ubi_assert(new_fm);
  1138. ubi->fm = new_fm;
  1139. ret = self_check_seen(ubi, seen_pebs);
  1140. dbg_bld("fastmap written!");
  1141. out_free_seen:
  1142. free_seen(seen_pebs);
  1143. out_free_dvbuf:
  1144. ubi_free_vid_buf(dvbuf);
  1145. out_free_avbuf:
  1146. ubi_free_vid_buf(avbuf);
  1147. out:
  1148. return ret;
  1149. }
  1150. /**
  1151. * erase_block - Manually erase a PEB.
  1152. * @ubi: UBI device object
  1153. * @pnum: PEB to be erased
  1154. *
  1155. * Returns the new EC value on success, < 0 indicates an internal error.
  1156. */
  1157. static int erase_block(struct ubi_device *ubi, int pnum)
  1158. {
  1159. int ret;
  1160. struct ubi_ec_hdr *ec_hdr;
  1161. long long ec;
  1162. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  1163. if (!ec_hdr)
  1164. return -ENOMEM;
  1165. ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1166. if (ret < 0)
  1167. goto out;
  1168. else if (ret && ret != UBI_IO_BITFLIPS) {
  1169. ret = -EINVAL;
  1170. goto out;
  1171. }
  1172. ret = ubi_io_sync_erase(ubi, pnum, 0);
  1173. if (ret < 0)
  1174. goto out;
  1175. ec = be64_to_cpu(ec_hdr->ec);
  1176. ec += ret;
  1177. if (ec > UBI_MAX_ERASECOUNTER) {
  1178. ret = -EINVAL;
  1179. goto out;
  1180. }
  1181. ec_hdr->ec = cpu_to_be64(ec);
  1182. ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
  1183. if (ret < 0)
  1184. goto out;
  1185. ret = ec;
  1186. out:
  1187. kfree(ec_hdr);
  1188. return ret;
  1189. }
  1190. /**
  1191. * invalidate_fastmap - destroys a fastmap.
  1192. * @ubi: UBI device object
  1193. *
  1194. * This function ensures that upon next UBI attach a full scan
  1195. * is issued. We need this if UBI is about to write a new fastmap
  1196. * but is unable to do so. In this case we have two options:
  1197. * a) Make sure that the current fastmap will not be usued upon
  1198. * attach time and contine or b) fall back to RO mode to have the
  1199. * current fastmap in a valid state.
  1200. * Returns 0 on success, < 0 indicates an internal error.
  1201. */
  1202. static int invalidate_fastmap(struct ubi_device *ubi)
  1203. {
  1204. int ret;
  1205. struct ubi_fastmap_layout *fm;
  1206. struct ubi_wl_entry *e;
  1207. struct ubi_vid_io_buf *vb = NULL;
  1208. struct ubi_vid_hdr *vh;
  1209. if (!ubi->fm)
  1210. return 0;
  1211. ubi->fm = NULL;
  1212. ret = -ENOMEM;
  1213. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  1214. if (!fm)
  1215. goto out;
  1216. vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
  1217. if (!vb)
  1218. goto out_free_fm;
  1219. vh = ubi_get_vid_hdr(vb);
  1220. ret = -ENOSPC;
  1221. e = ubi_wl_get_fm_peb(ubi, 1);
  1222. if (!e)
  1223. goto out_free_fm;
  1224. /*
  1225. * Create fake fastmap such that UBI will fall back
  1226. * to scanning mode.
  1227. */
  1228. vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1229. ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb);
  1230. if (ret < 0) {
  1231. ubi_wl_put_fm_peb(ubi, e, 0, 0);
  1232. goto out_free_fm;
  1233. }
  1234. fm->used_blocks = 1;
  1235. fm->e[0] = e;
  1236. ubi->fm = fm;
  1237. out:
  1238. ubi_free_vid_buf(vb);
  1239. return ret;
  1240. out_free_fm:
  1241. kfree(fm);
  1242. goto out;
  1243. }
  1244. /**
  1245. * return_fm_pebs - returns all PEBs used by a fastmap back to the
  1246. * WL sub-system.
  1247. * @ubi: UBI device object
  1248. * @fm: fastmap layout object
  1249. */
  1250. static void return_fm_pebs(struct ubi_device *ubi,
  1251. struct ubi_fastmap_layout *fm)
  1252. {
  1253. int i;
  1254. if (!fm)
  1255. return;
  1256. for (i = 0; i < fm->used_blocks; i++) {
  1257. if (fm->e[i]) {
  1258. ubi_wl_put_fm_peb(ubi, fm->e[i], i,
  1259. fm->to_be_tortured[i]);
  1260. fm->e[i] = NULL;
  1261. }
  1262. }
  1263. }
  1264. /**
  1265. * ubi_update_fastmap - will be called by UBI if a volume changes or
  1266. * a fastmap pool becomes full.
  1267. * @ubi: UBI device object
  1268. *
  1269. * Returns 0 on success, < 0 indicates an internal error.
  1270. */
  1271. int ubi_update_fastmap(struct ubi_device *ubi)
  1272. {
  1273. int ret, i, j;
  1274. struct ubi_fastmap_layout *new_fm, *old_fm;
  1275. struct ubi_wl_entry *tmp_e;
  1276. down_write(&ubi->fm_protect);
  1277. down_write(&ubi->work_sem);
  1278. down_write(&ubi->fm_eba_sem);
  1279. ubi_refill_pools(ubi);
  1280. if (ubi->ro_mode || ubi->fm_disabled) {
  1281. up_write(&ubi->fm_eba_sem);
  1282. up_write(&ubi->work_sem);
  1283. up_write(&ubi->fm_protect);
  1284. return 0;
  1285. }
  1286. ret = ubi_ensure_anchor_pebs(ubi);
  1287. if (ret) {
  1288. up_write(&ubi->fm_eba_sem);
  1289. up_write(&ubi->work_sem);
  1290. up_write(&ubi->fm_protect);
  1291. return ret;
  1292. }
  1293. new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
  1294. if (!new_fm) {
  1295. up_write(&ubi->fm_eba_sem);
  1296. up_write(&ubi->work_sem);
  1297. up_write(&ubi->fm_protect);
  1298. return -ENOMEM;
  1299. }
  1300. new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
  1301. old_fm = ubi->fm;
  1302. ubi->fm = NULL;
  1303. if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
  1304. ubi_err(ubi, "fastmap too large");
  1305. ret = -ENOSPC;
  1306. goto err;
  1307. }
  1308. for (i = 1; i < new_fm->used_blocks; i++) {
  1309. spin_lock(&ubi->wl_lock);
  1310. tmp_e = ubi_wl_get_fm_peb(ubi, 0);
  1311. spin_unlock(&ubi->wl_lock);
  1312. if (!tmp_e) {
  1313. if (old_fm && old_fm->e[i]) {
  1314. ret = erase_block(ubi, old_fm->e[i]->pnum);
  1315. if (ret < 0) {
  1316. ubi_err(ubi, "could not erase old fastmap PEB");
  1317. for (j = 1; j < i; j++) {
  1318. ubi_wl_put_fm_peb(ubi, new_fm->e[j],
  1319. j, 0);
  1320. new_fm->e[j] = NULL;
  1321. }
  1322. goto err;
  1323. }
  1324. new_fm->e[i] = old_fm->e[i];
  1325. old_fm->e[i] = NULL;
  1326. } else {
  1327. ubi_err(ubi, "could not get any free erase block");
  1328. for (j = 1; j < i; j++) {
  1329. ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
  1330. new_fm->e[j] = NULL;
  1331. }
  1332. ret = -ENOSPC;
  1333. goto err;
  1334. }
  1335. } else {
  1336. new_fm->e[i] = tmp_e;
  1337. if (old_fm && old_fm->e[i]) {
  1338. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1339. old_fm->to_be_tortured[i]);
  1340. old_fm->e[i] = NULL;
  1341. }
  1342. }
  1343. }
  1344. /* Old fastmap is larger than the new one */
  1345. if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
  1346. for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
  1347. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1348. old_fm->to_be_tortured[i]);
  1349. old_fm->e[i] = NULL;
  1350. }
  1351. }
  1352. spin_lock(&ubi->wl_lock);
  1353. tmp_e = ubi_wl_get_fm_peb(ubi, 1);
  1354. spin_unlock(&ubi->wl_lock);
  1355. if (old_fm) {
  1356. /* no fresh anchor PEB was found, reuse the old one */
  1357. if (!tmp_e) {
  1358. ret = erase_block(ubi, old_fm->e[0]->pnum);
  1359. if (ret < 0) {
  1360. ubi_err(ubi, "could not erase old anchor PEB");
  1361. for (i = 1; i < new_fm->used_blocks; i++) {
  1362. ubi_wl_put_fm_peb(ubi, new_fm->e[i],
  1363. i, 0);
  1364. new_fm->e[i] = NULL;
  1365. }
  1366. goto err;
  1367. }
  1368. new_fm->e[0] = old_fm->e[0];
  1369. new_fm->e[0]->ec = ret;
  1370. old_fm->e[0] = NULL;
  1371. } else {
  1372. /* we've got a new anchor PEB, return the old one */
  1373. ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
  1374. old_fm->to_be_tortured[0]);
  1375. new_fm->e[0] = tmp_e;
  1376. old_fm->e[0] = NULL;
  1377. }
  1378. } else {
  1379. if (!tmp_e) {
  1380. ubi_err(ubi, "could not find any anchor PEB");
  1381. for (i = 1; i < new_fm->used_blocks; i++) {
  1382. ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
  1383. new_fm->e[i] = NULL;
  1384. }
  1385. ret = -ENOSPC;
  1386. goto err;
  1387. }
  1388. new_fm->e[0] = tmp_e;
  1389. }
  1390. ret = ubi_write_fastmap(ubi, new_fm);
  1391. if (ret)
  1392. goto err;
  1393. out_unlock:
  1394. up_write(&ubi->fm_eba_sem);
  1395. up_write(&ubi->work_sem);
  1396. up_write(&ubi->fm_protect);
  1397. kfree(old_fm);
  1398. return ret;
  1399. err:
  1400. ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
  1401. ret = invalidate_fastmap(ubi);
  1402. if (ret < 0) {
  1403. ubi_err(ubi, "Unable to invalidate current fastmap!");
  1404. ubi_ro_mode(ubi);
  1405. } else {
  1406. return_fm_pebs(ubi, old_fm);
  1407. return_fm_pebs(ubi, new_fm);
  1408. ret = 0;
  1409. }
  1410. kfree(new_fm);
  1411. goto out_unlock;
  1412. }