fastmap.c 41 KB

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