fastmap.c 38 KB

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