fastmap.c 40 KB

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