fastmap.c 40 KB

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