fastmap.c 39 KB

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