fastmap.c 37 KB

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