fastmap.c 40 KB

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