fastmap.c 40 KB

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