fastmap.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. /*
  2. * Copyright (c) 2012 Linutronix GmbH
  3. * Copyright (c) 2014 sigma star gmbh
  4. * Author: Richard Weinberger <richard@nod.at>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/crc32.h>
  17. #include "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. static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
  380. __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
  381. struct list_head *free)
  382. {
  383. struct ubi_vid_hdr *vh;
  384. struct ubi_ec_hdr *ech;
  385. struct ubi_ainf_peb *new_aeb;
  386. int i, pnum, err, ret = 0;
  387. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  388. if (!ech)
  389. return -ENOMEM;
  390. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  391. if (!vh) {
  392. kfree(ech);
  393. return -ENOMEM;
  394. }
  395. dbg_bld("scanning fastmap pool: size = %i", pool_size);
  396. /*
  397. * Now scan all PEBs in the pool to find changes which have been made
  398. * after the creation of the fastmap
  399. */
  400. for (i = 0; i < pool_size; i++) {
  401. int scrub = 0;
  402. int image_seq;
  403. pnum = be32_to_cpu(pebs[i]);
  404. if (ubi_io_is_bad(ubi, pnum)) {
  405. ubi_err(ubi, "bad PEB in fastmap pool!");
  406. ret = UBI_BAD_FASTMAP;
  407. goto out;
  408. }
  409. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  410. if (err && err != UBI_IO_BITFLIPS) {
  411. ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
  412. pnum, err);
  413. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  414. goto out;
  415. } else if (err == UBI_IO_BITFLIPS)
  416. scrub = 1;
  417. /*
  418. * Older UBI implementations have image_seq set to zero, so
  419. * we shouldn't fail if image_seq == 0.
  420. */
  421. image_seq = be32_to_cpu(ech->image_seq);
  422. if (image_seq && (image_seq != ubi->image_seq)) {
  423. ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
  424. be32_to_cpu(ech->image_seq), ubi->image_seq);
  425. ret = UBI_BAD_FASTMAP;
  426. goto out;
  427. }
  428. err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  429. if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
  430. unsigned long long ec = be64_to_cpu(ech->ec);
  431. unmap_peb(ai, pnum);
  432. dbg_bld("Adding PEB to free: %i", pnum);
  433. if (err == UBI_IO_FF_BITFLIPS)
  434. add_aeb(ai, free, pnum, ec, 1);
  435. else
  436. add_aeb(ai, free, pnum, ec, 0);
  437. continue;
  438. } else if (err == 0 || err == UBI_IO_BITFLIPS) {
  439. dbg_bld("Found non empty PEB:%i in pool", pnum);
  440. if (err == UBI_IO_BITFLIPS)
  441. scrub = 1;
  442. new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
  443. GFP_KERNEL);
  444. if (!new_aeb) {
  445. ret = -ENOMEM;
  446. goto out;
  447. }
  448. new_aeb->ec = be64_to_cpu(ech->ec);
  449. new_aeb->pnum = pnum;
  450. new_aeb->lnum = be32_to_cpu(vh->lnum);
  451. new_aeb->sqnum = be64_to_cpu(vh->sqnum);
  452. new_aeb->copy_flag = vh->copy_flag;
  453. new_aeb->scrub = scrub;
  454. if (*max_sqnum < new_aeb->sqnum)
  455. *max_sqnum = new_aeb->sqnum;
  456. err = process_pool_aeb(ubi, ai, vh, new_aeb);
  457. if (err) {
  458. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  459. goto out;
  460. }
  461. } else {
  462. /* We are paranoid and fall back to scanning mode */
  463. ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
  464. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  465. goto out;
  466. }
  467. }
  468. out:
  469. ubi_free_vid_hdr(ubi, vh);
  470. kfree(ech);
  471. return ret;
  472. }
  473. /**
  474. * count_fastmap_pebs - Counts the PEBs found by fastmap.
  475. * @ai: The UBI attach info object
  476. */
  477. static int count_fastmap_pebs(struct ubi_attach_info *ai)
  478. {
  479. struct ubi_ainf_peb *aeb;
  480. struct ubi_ainf_volume *av;
  481. struct rb_node *rb1, *rb2;
  482. int n = 0;
  483. list_for_each_entry(aeb, &ai->erase, u.list)
  484. n++;
  485. list_for_each_entry(aeb, &ai->free, u.list)
  486. n++;
  487. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
  488. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
  489. n++;
  490. return n;
  491. }
  492. /**
  493. * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
  494. * @ubi: UBI device object
  495. * @ai: UBI attach info object
  496. * @fm: the fastmap to be attached
  497. *
  498. * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
  499. * < 0 indicates an internal error.
  500. */
  501. static int ubi_attach_fastmap(struct ubi_device *ubi,
  502. struct ubi_attach_info *ai,
  503. struct ubi_fastmap_layout *fm)
  504. {
  505. struct list_head used, free;
  506. struct ubi_ainf_volume *av;
  507. struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
  508. struct ubi_fm_sb *fmsb;
  509. struct ubi_fm_hdr *fmhdr;
  510. struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
  511. struct ubi_fm_ec *fmec;
  512. struct ubi_fm_volhdr *fmvhdr;
  513. struct ubi_fm_eba *fm_eba;
  514. int ret, i, j, pool_size, wl_pool_size;
  515. size_t fm_pos = 0, fm_size = ubi->fm_size;
  516. unsigned long long max_sqnum = 0;
  517. void *fm_raw = ubi->fm_buf;
  518. INIT_LIST_HEAD(&used);
  519. INIT_LIST_HEAD(&free);
  520. ai->min_ec = UBI_MAX_ERASECOUNTER;
  521. fmsb = (struct ubi_fm_sb *)(fm_raw);
  522. ai->max_sqnum = fmsb->sqnum;
  523. fm_pos += sizeof(struct ubi_fm_sb);
  524. if (fm_pos >= fm_size)
  525. goto fail_bad;
  526. fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  527. fm_pos += sizeof(*fmhdr);
  528. if (fm_pos >= fm_size)
  529. goto fail_bad;
  530. if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
  531. ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
  532. be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
  533. goto fail_bad;
  534. }
  535. fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  536. fm_pos += sizeof(*fmpl);
  537. if (fm_pos >= fm_size)
  538. goto fail_bad;
  539. if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
  540. ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
  541. be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
  542. goto fail_bad;
  543. }
  544. fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  545. fm_pos += sizeof(*fmpl_wl);
  546. if (fm_pos >= fm_size)
  547. goto fail_bad;
  548. if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
  549. ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
  550. be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
  551. goto fail_bad;
  552. }
  553. pool_size = be16_to_cpu(fmpl->size);
  554. wl_pool_size = be16_to_cpu(fmpl_wl->size);
  555. fm->max_pool_size = be16_to_cpu(fmpl->max_size);
  556. fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
  557. if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
  558. ubi_err(ubi, "bad pool size: %i", pool_size);
  559. goto fail_bad;
  560. }
  561. if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
  562. ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
  563. goto fail_bad;
  564. }
  565. if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
  566. fm->max_pool_size < 0) {
  567. ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
  568. goto fail_bad;
  569. }
  570. if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
  571. fm->max_wl_pool_size < 0) {
  572. ubi_err(ubi, "bad maximal WL pool size: %i",
  573. fm->max_wl_pool_size);
  574. goto fail_bad;
  575. }
  576. /* read EC values from free list */
  577. for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
  578. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  579. fm_pos += sizeof(*fmec);
  580. if (fm_pos >= fm_size)
  581. goto fail_bad;
  582. add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
  583. be32_to_cpu(fmec->ec), 0);
  584. }
  585. /* read EC values from used list */
  586. for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
  587. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  588. fm_pos += sizeof(*fmec);
  589. if (fm_pos >= fm_size)
  590. goto fail_bad;
  591. add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
  592. be32_to_cpu(fmec->ec), 0);
  593. }
  594. /* read EC values from scrub list */
  595. for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
  596. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  597. fm_pos += sizeof(*fmec);
  598. if (fm_pos >= fm_size)
  599. goto fail_bad;
  600. add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
  601. be32_to_cpu(fmec->ec), 1);
  602. }
  603. /* read EC values from erase list */
  604. for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
  605. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  606. fm_pos += sizeof(*fmec);
  607. if (fm_pos >= fm_size)
  608. goto fail_bad;
  609. add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
  610. be32_to_cpu(fmec->ec), 1);
  611. }
  612. ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
  613. ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
  614. /* Iterate over all volumes and read their EBA table */
  615. for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
  616. fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  617. fm_pos += sizeof(*fmvhdr);
  618. if (fm_pos >= fm_size)
  619. goto fail_bad;
  620. if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
  621. ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
  622. be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
  623. goto fail_bad;
  624. }
  625. av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
  626. be32_to_cpu(fmvhdr->used_ebs),
  627. be32_to_cpu(fmvhdr->data_pad),
  628. fmvhdr->vol_type,
  629. be32_to_cpu(fmvhdr->last_eb_bytes));
  630. if (!av)
  631. goto fail_bad;
  632. if (PTR_ERR(av) == -EINVAL) {
  633. ubi_err(ubi, "volume (ID %i) already exists",
  634. fmvhdr->vol_id);
  635. goto fail_bad;
  636. }
  637. ai->vols_found++;
  638. if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
  639. ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
  640. fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  641. fm_pos += sizeof(*fm_eba);
  642. fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
  643. if (fm_pos >= fm_size)
  644. goto fail_bad;
  645. if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
  646. ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
  647. be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
  648. goto fail_bad;
  649. }
  650. for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
  651. int pnum = be32_to_cpu(fm_eba->pnum[j]);
  652. if (pnum < 0)
  653. continue;
  654. aeb = NULL;
  655. list_for_each_entry(tmp_aeb, &used, u.list) {
  656. if (tmp_aeb->pnum == pnum) {
  657. aeb = tmp_aeb;
  658. break;
  659. }
  660. }
  661. if (!aeb) {
  662. ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
  663. goto fail_bad;
  664. }
  665. aeb->lnum = j;
  666. if (av->highest_lnum <= aeb->lnum)
  667. av->highest_lnum = aeb->lnum;
  668. assign_aeb_to_av(ai, aeb, av);
  669. dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
  670. aeb->pnum, aeb->lnum, av->vol_id);
  671. }
  672. }
  673. ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
  674. if (ret)
  675. goto fail;
  676. ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
  677. if (ret)
  678. goto fail;
  679. if (max_sqnum > ai->max_sqnum)
  680. ai->max_sqnum = max_sqnum;
  681. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
  682. list_move_tail(&tmp_aeb->u.list, &ai->free);
  683. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
  684. list_move_tail(&tmp_aeb->u.list, &ai->erase);
  685. ubi_assert(list_empty(&free));
  686. /*
  687. * If fastmap is leaking PEBs (must not happen), raise a
  688. * fat warning and fall back to scanning mode.
  689. * We do this here because in ubi_wl_init() it's too late
  690. * and we cannot fall back to scanning.
  691. */
  692. if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
  693. ai->bad_peb_count - fm->used_blocks))
  694. goto fail_bad;
  695. return 0;
  696. fail_bad:
  697. ret = UBI_BAD_FASTMAP;
  698. fail:
  699. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
  700. list_del(&tmp_aeb->u.list);
  701. kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
  702. }
  703. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
  704. list_del(&tmp_aeb->u.list);
  705. kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
  706. }
  707. return ret;
  708. }
  709. /**
  710. * ubi_scan_fastmap - scan the fastmap.
  711. * @ubi: UBI device object
  712. * @ai: UBI attach info to be filled
  713. * @fm_anchor: The fastmap starts at this PEB
  714. *
  715. * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
  716. * UBI_BAD_FASTMAP if one was found but is not usable.
  717. * < 0 indicates an internal error.
  718. */
  719. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  720. int fm_anchor)
  721. {
  722. struct ubi_fm_sb *fmsb, *fmsb2;
  723. struct ubi_vid_hdr *vh;
  724. struct ubi_ec_hdr *ech;
  725. struct ubi_fastmap_layout *fm;
  726. int i, used_blocks, pnum, ret = 0;
  727. size_t fm_size;
  728. __be32 crc, tmp_crc;
  729. unsigned long long sqnum = 0;
  730. down_write(&ubi->fm_protect);
  731. memset(ubi->fm_buf, 0, ubi->fm_size);
  732. fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
  733. if (!fmsb) {
  734. ret = -ENOMEM;
  735. goto out;
  736. }
  737. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  738. if (!fm) {
  739. ret = -ENOMEM;
  740. kfree(fmsb);
  741. goto out;
  742. }
  743. ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
  744. if (ret && ret != UBI_IO_BITFLIPS)
  745. goto free_fm_sb;
  746. else if (ret == UBI_IO_BITFLIPS)
  747. fm->to_be_tortured[0] = 1;
  748. if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
  749. ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
  750. be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
  751. ret = UBI_BAD_FASTMAP;
  752. goto free_fm_sb;
  753. }
  754. if (fmsb->version != UBI_FM_FMT_VERSION) {
  755. ubi_err(ubi, "bad fastmap version: %i, expected: %i",
  756. fmsb->version, UBI_FM_FMT_VERSION);
  757. ret = UBI_BAD_FASTMAP;
  758. goto free_fm_sb;
  759. }
  760. used_blocks = be32_to_cpu(fmsb->used_blocks);
  761. if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
  762. ubi_err(ubi, "number of fastmap blocks is invalid: %i",
  763. used_blocks);
  764. ret = UBI_BAD_FASTMAP;
  765. goto free_fm_sb;
  766. }
  767. fm_size = ubi->leb_size * used_blocks;
  768. if (fm_size != ubi->fm_size) {
  769. ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
  770. fm_size, ubi->fm_size);
  771. ret = UBI_BAD_FASTMAP;
  772. goto free_fm_sb;
  773. }
  774. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  775. if (!ech) {
  776. ret = -ENOMEM;
  777. goto free_fm_sb;
  778. }
  779. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  780. if (!vh) {
  781. ret = -ENOMEM;
  782. goto free_hdr;
  783. }
  784. for (i = 0; i < used_blocks; i++) {
  785. int image_seq;
  786. pnum = be32_to_cpu(fmsb->block_loc[i]);
  787. if (ubi_io_is_bad(ubi, pnum)) {
  788. ret = UBI_BAD_FASTMAP;
  789. goto free_hdr;
  790. }
  791. ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  792. if (ret && ret != UBI_IO_BITFLIPS) {
  793. ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
  794. i, pnum);
  795. if (ret > 0)
  796. ret = UBI_BAD_FASTMAP;
  797. goto free_hdr;
  798. } else if (ret == UBI_IO_BITFLIPS)
  799. fm->to_be_tortured[i] = 1;
  800. image_seq = be32_to_cpu(ech->image_seq);
  801. if (!ubi->image_seq)
  802. ubi->image_seq = image_seq;
  803. /*
  804. * Older UBI implementations have image_seq set to zero, so
  805. * we shouldn't fail if image_seq == 0.
  806. */
  807. if (image_seq && (image_seq != ubi->image_seq)) {
  808. ubi_err(ubi, "wrong image seq:%d instead of %d",
  809. be32_to_cpu(ech->image_seq), ubi->image_seq);
  810. ret = UBI_BAD_FASTMAP;
  811. goto free_hdr;
  812. }
  813. ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  814. if (ret && ret != UBI_IO_BITFLIPS) {
  815. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
  816. i, pnum);
  817. goto free_hdr;
  818. }
  819. if (i == 0) {
  820. if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
  821. ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
  822. be32_to_cpu(vh->vol_id),
  823. UBI_FM_SB_VOLUME_ID);
  824. ret = UBI_BAD_FASTMAP;
  825. goto free_hdr;
  826. }
  827. } else {
  828. if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
  829. ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
  830. be32_to_cpu(vh->vol_id),
  831. UBI_FM_DATA_VOLUME_ID);
  832. ret = UBI_BAD_FASTMAP;
  833. goto free_hdr;
  834. }
  835. }
  836. if (sqnum < be64_to_cpu(vh->sqnum))
  837. sqnum = be64_to_cpu(vh->sqnum);
  838. ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
  839. ubi->leb_start, ubi->leb_size);
  840. if (ret && ret != UBI_IO_BITFLIPS) {
  841. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
  842. "err: %i)", i, pnum, ret);
  843. goto free_hdr;
  844. }
  845. }
  846. kfree(fmsb);
  847. fmsb = NULL;
  848. fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
  849. tmp_crc = be32_to_cpu(fmsb2->data_crc);
  850. fmsb2->data_crc = 0;
  851. crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
  852. if (crc != tmp_crc) {
  853. ubi_err(ubi, "fastmap data CRC is invalid");
  854. ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
  855. tmp_crc, crc);
  856. ret = UBI_BAD_FASTMAP;
  857. goto free_hdr;
  858. }
  859. fmsb2->sqnum = sqnum;
  860. fm->used_blocks = used_blocks;
  861. ret = ubi_attach_fastmap(ubi, ai, fm);
  862. if (ret) {
  863. if (ret > 0)
  864. ret = UBI_BAD_FASTMAP;
  865. goto free_hdr;
  866. }
  867. for (i = 0; i < used_blocks; i++) {
  868. struct ubi_wl_entry *e;
  869. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  870. if (!e) {
  871. while (i--)
  872. kfree(fm->e[i]);
  873. ret = -ENOMEM;
  874. goto free_hdr;
  875. }
  876. e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
  877. e->ec = be32_to_cpu(fmsb2->block_ec[i]);
  878. fm->e[i] = e;
  879. }
  880. ubi->fm = fm;
  881. ubi->fm_pool.max_size = ubi->fm->max_pool_size;
  882. ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
  883. ubi_msg(ubi, "attached by fastmap");
  884. ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
  885. ubi_msg(ubi, "fastmap WL pool size: %d",
  886. ubi->fm_wl_pool.max_size);
  887. ubi->fm_disabled = 0;
  888. ubi->fast_attach = 1;
  889. ubi_free_vid_hdr(ubi, vh);
  890. kfree(ech);
  891. out:
  892. up_write(&ubi->fm_protect);
  893. if (ret == UBI_BAD_FASTMAP)
  894. ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
  895. return ret;
  896. free_hdr:
  897. ubi_free_vid_hdr(ubi, vh);
  898. kfree(ech);
  899. free_fm_sb:
  900. kfree(fmsb);
  901. kfree(fm);
  902. goto out;
  903. }
  904. /**
  905. * ubi_write_fastmap - writes a fastmap.
  906. * @ubi: UBI device object
  907. * @new_fm: the to be written fastmap
  908. *
  909. * Returns 0 on success, < 0 indicates an internal error.
  910. */
  911. static int ubi_write_fastmap(struct ubi_device *ubi,
  912. struct ubi_fastmap_layout *new_fm)
  913. {
  914. size_t fm_pos = 0;
  915. void *fm_raw;
  916. struct ubi_fm_sb *fmsb;
  917. struct ubi_fm_hdr *fmh;
  918. struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
  919. struct ubi_fm_ec *fec;
  920. struct ubi_fm_volhdr *fvh;
  921. struct ubi_fm_eba *feba;
  922. struct ubi_wl_entry *wl_e;
  923. struct ubi_volume *vol;
  924. struct ubi_vid_hdr *avhdr, *dvhdr;
  925. struct ubi_work *ubi_wrk;
  926. struct rb_node *tmp_rb;
  927. int ret, i, j, free_peb_count, used_peb_count, vol_count;
  928. int scrub_peb_count, erase_peb_count;
  929. int *seen_pebs = NULL;
  930. fm_raw = ubi->fm_buf;
  931. memset(ubi->fm_buf, 0, ubi->fm_size);
  932. avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
  933. if (!avhdr) {
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
  938. if (!dvhdr) {
  939. ret = -ENOMEM;
  940. goto out_kfree;
  941. }
  942. seen_pebs = init_seen(ubi);
  943. if (IS_ERR(seen_pebs)) {
  944. ret = PTR_ERR(seen_pebs);
  945. goto out_kfree;
  946. }
  947. spin_lock(&ubi->volumes_lock);
  948. spin_lock(&ubi->wl_lock);
  949. fmsb = (struct ubi_fm_sb *)fm_raw;
  950. fm_pos += sizeof(*fmsb);
  951. ubi_assert(fm_pos <= ubi->fm_size);
  952. fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  953. fm_pos += sizeof(*fmh);
  954. ubi_assert(fm_pos <= ubi->fm_size);
  955. fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
  956. fmsb->version = UBI_FM_FMT_VERSION;
  957. fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
  958. /* the max sqnum will be filled in while *reading* the fastmap */
  959. fmsb->sqnum = 0;
  960. fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
  961. free_peb_count = 0;
  962. used_peb_count = 0;
  963. scrub_peb_count = 0;
  964. erase_peb_count = 0;
  965. vol_count = 0;
  966. fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  967. fm_pos += sizeof(*fmpl);
  968. fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  969. fmpl->size = cpu_to_be16(ubi->fm_pool.size);
  970. fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
  971. for (i = 0; i < ubi->fm_pool.size; i++) {
  972. fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
  973. set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
  974. }
  975. fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  976. fm_pos += sizeof(*fmpl_wl);
  977. fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  978. fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
  979. fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
  980. for (i = 0; i < ubi->fm_wl_pool.size; i++) {
  981. fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
  982. set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
  983. }
  984. ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
  985. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  986. fec->pnum = cpu_to_be32(wl_e->pnum);
  987. set_seen(ubi, wl_e->pnum, seen_pebs);
  988. fec->ec = cpu_to_be32(wl_e->ec);
  989. free_peb_count++;
  990. fm_pos += sizeof(*fec);
  991. ubi_assert(fm_pos <= ubi->fm_size);
  992. }
  993. fmh->free_peb_count = cpu_to_be32(free_peb_count);
  994. ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
  995. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  996. fec->pnum = cpu_to_be32(wl_e->pnum);
  997. set_seen(ubi, wl_e->pnum, seen_pebs);
  998. fec->ec = cpu_to_be32(wl_e->ec);
  999. used_peb_count++;
  1000. fm_pos += sizeof(*fec);
  1001. ubi_assert(fm_pos <= ubi->fm_size);
  1002. }
  1003. ubi_for_each_protected_peb(ubi, i, wl_e) {
  1004. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1005. fec->pnum = cpu_to_be32(wl_e->pnum);
  1006. set_seen(ubi, wl_e->pnum, seen_pebs);
  1007. fec->ec = cpu_to_be32(wl_e->ec);
  1008. used_peb_count++;
  1009. fm_pos += sizeof(*fec);
  1010. ubi_assert(fm_pos <= ubi->fm_size);
  1011. }
  1012. fmh->used_peb_count = cpu_to_be32(used_peb_count);
  1013. ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
  1014. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1015. fec->pnum = cpu_to_be32(wl_e->pnum);
  1016. set_seen(ubi, wl_e->pnum, seen_pebs);
  1017. fec->ec = cpu_to_be32(wl_e->ec);
  1018. scrub_peb_count++;
  1019. fm_pos += sizeof(*fec);
  1020. ubi_assert(fm_pos <= ubi->fm_size);
  1021. }
  1022. fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
  1023. list_for_each_entry(ubi_wrk, &ubi->works, list) {
  1024. if (ubi_is_erase_work(ubi_wrk)) {
  1025. wl_e = ubi_wrk->e;
  1026. ubi_assert(wl_e);
  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. erase_peb_count++;
  1032. fm_pos += sizeof(*fec);
  1033. ubi_assert(fm_pos <= ubi->fm_size);
  1034. }
  1035. }
  1036. fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
  1037. for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
  1038. vol = ubi->volumes[i];
  1039. if (!vol)
  1040. continue;
  1041. vol_count++;
  1042. fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  1043. fm_pos += sizeof(*fvh);
  1044. ubi_assert(fm_pos <= ubi->fm_size);
  1045. fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
  1046. fvh->vol_id = cpu_to_be32(vol->vol_id);
  1047. fvh->vol_type = vol->vol_type;
  1048. fvh->used_ebs = cpu_to_be32(vol->used_ebs);
  1049. fvh->data_pad = cpu_to_be32(vol->data_pad);
  1050. fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
  1051. ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
  1052. vol->vol_type == UBI_STATIC_VOLUME);
  1053. feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  1054. fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
  1055. ubi_assert(fm_pos <= ubi->fm_size);
  1056. for (j = 0; j < vol->reserved_pebs; j++)
  1057. feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
  1058. feba->reserved_pebs = cpu_to_be32(j);
  1059. feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
  1060. }
  1061. fmh->vol_count = cpu_to_be32(vol_count);
  1062. fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
  1063. avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1064. avhdr->lnum = 0;
  1065. spin_unlock(&ubi->wl_lock);
  1066. spin_unlock(&ubi->volumes_lock);
  1067. dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
  1068. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
  1069. if (ret) {
  1070. ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
  1071. goto out_kfree;
  1072. }
  1073. for (i = 0; i < new_fm->used_blocks; i++) {
  1074. fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
  1075. set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
  1076. fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
  1077. }
  1078. fmsb->data_crc = 0;
  1079. fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
  1080. ubi->fm_size));
  1081. for (i = 1; i < new_fm->used_blocks; i++) {
  1082. dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1083. dvhdr->lnum = cpu_to_be32(i);
  1084. dbg_bld("writing fastmap data to PEB %i sqnum %llu",
  1085. new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
  1086. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
  1087. if (ret) {
  1088. ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
  1089. new_fm->e[i]->pnum);
  1090. goto out_kfree;
  1091. }
  1092. }
  1093. for (i = 0; i < new_fm->used_blocks; i++) {
  1094. ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
  1095. new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
  1096. if (ret) {
  1097. ubi_err(ubi, "unable to write fastmap to PEB %i!",
  1098. new_fm->e[i]->pnum);
  1099. goto out_kfree;
  1100. }
  1101. }
  1102. ubi_assert(new_fm);
  1103. ubi->fm = new_fm;
  1104. ret = self_check_seen(ubi, seen_pebs);
  1105. dbg_bld("fastmap written!");
  1106. out_kfree:
  1107. ubi_free_vid_hdr(ubi, avhdr);
  1108. ubi_free_vid_hdr(ubi, dvhdr);
  1109. free_seen(seen_pebs);
  1110. out:
  1111. return ret;
  1112. }
  1113. /**
  1114. * erase_block - Manually erase a PEB.
  1115. * @ubi: UBI device object
  1116. * @pnum: PEB to be erased
  1117. *
  1118. * Returns the new EC value on success, < 0 indicates an internal error.
  1119. */
  1120. static int erase_block(struct ubi_device *ubi, int pnum)
  1121. {
  1122. int ret;
  1123. struct ubi_ec_hdr *ec_hdr;
  1124. long long ec;
  1125. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  1126. if (!ec_hdr)
  1127. return -ENOMEM;
  1128. ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1129. if (ret < 0)
  1130. goto out;
  1131. else if (ret && ret != UBI_IO_BITFLIPS) {
  1132. ret = -EINVAL;
  1133. goto out;
  1134. }
  1135. ret = ubi_io_sync_erase(ubi, pnum, 0);
  1136. if (ret < 0)
  1137. goto out;
  1138. ec = be64_to_cpu(ec_hdr->ec);
  1139. ec += ret;
  1140. if (ec > UBI_MAX_ERASECOUNTER) {
  1141. ret = -EINVAL;
  1142. goto out;
  1143. }
  1144. ec_hdr->ec = cpu_to_be64(ec);
  1145. ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
  1146. if (ret < 0)
  1147. goto out;
  1148. ret = ec;
  1149. out:
  1150. kfree(ec_hdr);
  1151. return ret;
  1152. }
  1153. /**
  1154. * invalidate_fastmap - destroys a fastmap.
  1155. * @ubi: UBI device object
  1156. *
  1157. * This function ensures that upon next UBI attach a full scan
  1158. * is issued. We need this if UBI is about to write a new fastmap
  1159. * but is unable to do so. In this case we have two options:
  1160. * a) Make sure that the current fastmap will not be usued upon
  1161. * attach time and contine or b) fall back to RO mode to have the
  1162. * current fastmap in a valid state.
  1163. * Returns 0 on success, < 0 indicates an internal error.
  1164. */
  1165. static int invalidate_fastmap(struct ubi_device *ubi)
  1166. {
  1167. int ret;
  1168. struct ubi_fastmap_layout *fm;
  1169. struct ubi_wl_entry *e;
  1170. struct ubi_vid_hdr *vh = NULL;
  1171. if (!ubi->fm)
  1172. return 0;
  1173. ubi->fm = NULL;
  1174. ret = -ENOMEM;
  1175. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  1176. if (!fm)
  1177. goto out;
  1178. vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
  1179. if (!vh)
  1180. goto out_free_fm;
  1181. ret = -ENOSPC;
  1182. e = ubi_wl_get_fm_peb(ubi, 1);
  1183. if (!e)
  1184. goto out_free_fm;
  1185. /*
  1186. * Create fake fastmap such that UBI will fall back
  1187. * to scanning mode.
  1188. */
  1189. vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1190. ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
  1191. if (ret < 0) {
  1192. ubi_wl_put_fm_peb(ubi, e, 0, 0);
  1193. goto out_free_fm;
  1194. }
  1195. fm->used_blocks = 1;
  1196. fm->e[0] = e;
  1197. ubi->fm = fm;
  1198. out:
  1199. ubi_free_vid_hdr(ubi, vh);
  1200. return ret;
  1201. out_free_fm:
  1202. kfree(fm);
  1203. goto out;
  1204. }
  1205. /**
  1206. * return_fm_pebs - returns all PEBs used by a fastmap back to the
  1207. * WL sub-system.
  1208. * @ubi: UBI device object
  1209. * @fm: fastmap layout object
  1210. */
  1211. static void return_fm_pebs(struct ubi_device *ubi,
  1212. struct ubi_fastmap_layout *fm)
  1213. {
  1214. int i;
  1215. if (!fm)
  1216. return;
  1217. for (i = 0; i < fm->used_blocks; i++) {
  1218. if (fm->e[i]) {
  1219. ubi_wl_put_fm_peb(ubi, fm->e[i], i,
  1220. fm->to_be_tortured[i]);
  1221. fm->e[i] = NULL;
  1222. }
  1223. }
  1224. }
  1225. /**
  1226. * ubi_update_fastmap - will be called by UBI if a volume changes or
  1227. * a fastmap pool becomes full.
  1228. * @ubi: UBI device object
  1229. *
  1230. * Returns 0 on success, < 0 indicates an internal error.
  1231. */
  1232. int ubi_update_fastmap(struct ubi_device *ubi)
  1233. {
  1234. int ret, i, j;
  1235. struct ubi_fastmap_layout *new_fm, *old_fm;
  1236. struct ubi_wl_entry *tmp_e;
  1237. down_write(&ubi->fm_protect);
  1238. ubi_refill_pools(ubi);
  1239. if (ubi->ro_mode || ubi->fm_disabled) {
  1240. up_write(&ubi->fm_protect);
  1241. return 0;
  1242. }
  1243. ret = ubi_ensure_anchor_pebs(ubi);
  1244. if (ret) {
  1245. up_write(&ubi->fm_protect);
  1246. return ret;
  1247. }
  1248. new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
  1249. if (!new_fm) {
  1250. up_write(&ubi->fm_protect);
  1251. return -ENOMEM;
  1252. }
  1253. new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
  1254. old_fm = ubi->fm;
  1255. ubi->fm = NULL;
  1256. if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
  1257. ubi_err(ubi, "fastmap too large");
  1258. ret = -ENOSPC;
  1259. goto err;
  1260. }
  1261. for (i = 1; i < new_fm->used_blocks; i++) {
  1262. spin_lock(&ubi->wl_lock);
  1263. tmp_e = ubi_wl_get_fm_peb(ubi, 0);
  1264. spin_unlock(&ubi->wl_lock);
  1265. if (!tmp_e) {
  1266. if (old_fm && old_fm->e[i]) {
  1267. ret = erase_block(ubi, old_fm->e[i]->pnum);
  1268. if (ret < 0) {
  1269. ubi_err(ubi, "could not erase old fastmap PEB");
  1270. for (j = 1; j < i; j++) {
  1271. ubi_wl_put_fm_peb(ubi, new_fm->e[j],
  1272. j, 0);
  1273. new_fm->e[j] = NULL;
  1274. }
  1275. goto err;
  1276. }
  1277. new_fm->e[i] = old_fm->e[i];
  1278. old_fm->e[i] = NULL;
  1279. } else {
  1280. ubi_err(ubi, "could not get any free erase block");
  1281. for (j = 1; j < i; j++) {
  1282. ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
  1283. new_fm->e[j] = NULL;
  1284. }
  1285. ret = -ENOSPC;
  1286. goto err;
  1287. }
  1288. } else {
  1289. new_fm->e[i] = tmp_e;
  1290. if (old_fm && old_fm->e[i]) {
  1291. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1292. old_fm->to_be_tortured[i]);
  1293. old_fm->e[i] = NULL;
  1294. }
  1295. }
  1296. }
  1297. /* Old fastmap is larger than the new one */
  1298. if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
  1299. for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
  1300. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1301. old_fm->to_be_tortured[i]);
  1302. old_fm->e[i] = NULL;
  1303. }
  1304. }
  1305. spin_lock(&ubi->wl_lock);
  1306. tmp_e = ubi_wl_get_fm_peb(ubi, 1);
  1307. spin_unlock(&ubi->wl_lock);
  1308. if (old_fm) {
  1309. /* no fresh anchor PEB was found, reuse the old one */
  1310. if (!tmp_e) {
  1311. ret = erase_block(ubi, old_fm->e[0]->pnum);
  1312. if (ret < 0) {
  1313. ubi_err(ubi, "could not erase old anchor PEB");
  1314. for (i = 1; i < new_fm->used_blocks; i++) {
  1315. ubi_wl_put_fm_peb(ubi, new_fm->e[i],
  1316. i, 0);
  1317. new_fm->e[i] = NULL;
  1318. }
  1319. goto err;
  1320. }
  1321. new_fm->e[0] = old_fm->e[0];
  1322. new_fm->e[0]->ec = ret;
  1323. old_fm->e[0] = NULL;
  1324. } else {
  1325. /* we've got a new anchor PEB, return the old one */
  1326. ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
  1327. old_fm->to_be_tortured[0]);
  1328. new_fm->e[0] = tmp_e;
  1329. old_fm->e[0] = NULL;
  1330. }
  1331. } else {
  1332. if (!tmp_e) {
  1333. ubi_err(ubi, "could not find any anchor PEB");
  1334. for (i = 1; i < new_fm->used_blocks; i++) {
  1335. ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
  1336. new_fm->e[i] = NULL;
  1337. }
  1338. ret = -ENOSPC;
  1339. goto err;
  1340. }
  1341. new_fm->e[0] = tmp_e;
  1342. }
  1343. down_write(&ubi->work_sem);
  1344. down_write(&ubi->fm_eba_sem);
  1345. ret = ubi_write_fastmap(ubi, new_fm);
  1346. up_write(&ubi->fm_eba_sem);
  1347. up_write(&ubi->work_sem);
  1348. if (ret)
  1349. goto err;
  1350. out_unlock:
  1351. up_write(&ubi->fm_protect);
  1352. kfree(old_fm);
  1353. return ret;
  1354. err:
  1355. ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
  1356. ret = invalidate_fastmap(ubi);
  1357. if (ret < 0) {
  1358. ubi_err(ubi, "Unable to invalidiate current fastmap!");
  1359. ubi_ro_mode(ubi);
  1360. } else {
  1361. return_fm_pebs(ubi, old_fm);
  1362. return_fm_pebs(ubi, new_fm);
  1363. ret = 0;
  1364. }
  1365. kfree(new_fm);
  1366. goto out_unlock;
  1367. }