fastmap.c 39 KB

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