fastmap.c 41 KB

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