fastmap.c 37 KB

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