fastmap.c 38 KB

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