fastmap.c 39 KB

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