fastmap.c 38 KB

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