fastmap.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  276. /* new_aeb is older */
  277. } else {
  278. dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
  279. av->vol_id, aeb->lnum, new_aeb->pnum);
  280. list_add_tail(&new_aeb->u.list, &ai->erase);
  281. }
  282. return 0;
  283. }
  284. /* This LEB is new, let's add it to the volume */
  285. if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
  286. av->highest_lnum = be32_to_cpu(new_vh->lnum);
  287. av->last_data_size = be32_to_cpu(new_vh->data_size);
  288. }
  289. if (av->vol_type == UBI_STATIC_VOLUME)
  290. av->used_ebs = be32_to_cpu(new_vh->used_ebs);
  291. av->leb_count++;
  292. rb_link_node(&new_aeb->u.rb, parent, p);
  293. rb_insert_color(&new_aeb->u.rb, &av->root);
  294. return 0;
  295. }
  296. /**
  297. * process_pool_aeb - we found a non-empty PEB in a pool.
  298. * @ubi: UBI device object
  299. * @ai: attach info object
  300. * @new_vh: the volume header derived from new_aeb
  301. * @new_aeb: the AEB to be examined
  302. *
  303. * Returns 0 on success, < 0 indicates an internal error.
  304. */
  305. static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
  306. struct ubi_vid_hdr *new_vh,
  307. struct ubi_ainf_peb *new_aeb)
  308. {
  309. struct ubi_ainf_volume *av, *tmp_av = NULL;
  310. struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
  311. int found = 0;
  312. if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
  313. be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
  314. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  315. return 0;
  316. }
  317. /* Find the volume this SEB belongs to */
  318. while (*p) {
  319. parent = *p;
  320. tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
  321. if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
  322. p = &(*p)->rb_left;
  323. else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
  324. p = &(*p)->rb_right;
  325. else {
  326. found = 1;
  327. break;
  328. }
  329. }
  330. if (found)
  331. av = tmp_av;
  332. else {
  333. ubi_err(ubi, "orphaned volume in fastmap pool!");
  334. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  335. return UBI_BAD_FASTMAP;
  336. }
  337. ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
  338. return update_vol(ubi, ai, av, new_vh, new_aeb);
  339. }
  340. /**
  341. * unmap_peb - unmap a PEB.
  342. * If fastmap detects a free PEB in the pool it has to check whether
  343. * this PEB has been unmapped after writing the fastmap.
  344. *
  345. * @ai: UBI attach info object
  346. * @pnum: The PEB to be unmapped
  347. */
  348. static void unmap_peb(struct ubi_attach_info *ai, int pnum)
  349. {
  350. struct ubi_ainf_volume *av;
  351. struct rb_node *node, *node2;
  352. struct ubi_ainf_peb *aeb;
  353. for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
  354. av = rb_entry(node, struct ubi_ainf_volume, rb);
  355. for (node2 = rb_first(&av->root); node2;
  356. node2 = rb_next(node2)) {
  357. aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
  358. if (aeb->pnum == pnum) {
  359. rb_erase(&aeb->u.rb, &av->root);
  360. av->leb_count--;
  361. kmem_cache_free(ai->aeb_slab_cache, aeb);
  362. return;
  363. }
  364. }
  365. }
  366. }
  367. /**
  368. * scan_pool - scans a pool for changed (no longer empty PEBs).
  369. * @ubi: UBI device object
  370. * @ai: attach info object
  371. * @pebs: an array of all PEB numbers in the to be scanned pool
  372. * @pool_size: size of the pool (number of entries in @pebs)
  373. * @max_sqnum: pointer to the maximal sequence number
  374. * @free: list of PEBs which are most likely free (and go into @ai->free)
  375. *
  376. * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
  377. * < 0 indicates an internal error.
  378. */
  379. static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
  380. int *pebs, int pool_size, unsigned long long *max_sqnum,
  381. struct list_head *free)
  382. {
  383. struct ubi_vid_hdr *vh;
  384. struct ubi_ec_hdr *ech;
  385. struct ubi_ainf_peb *new_aeb;
  386. int i, pnum, err, ret = 0;
  387. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  388. if (!ech)
  389. return -ENOMEM;
  390. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  391. if (!vh) {
  392. kfree(ech);
  393. return -ENOMEM;
  394. }
  395. dbg_bld("scanning fastmap pool: size = %i", pool_size);
  396. /*
  397. * Now scan all PEBs in the pool to find changes which have been made
  398. * after the creation of the fastmap
  399. */
  400. for (i = 0; i < pool_size; i++) {
  401. int scrub = 0;
  402. int image_seq;
  403. pnum = be32_to_cpu(pebs[i]);
  404. if (ubi_io_is_bad(ubi, pnum)) {
  405. ubi_err(ubi, "bad PEB in fastmap pool!");
  406. ret = UBI_BAD_FASTMAP;
  407. goto out;
  408. }
  409. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  410. if (err && err != UBI_IO_BITFLIPS) {
  411. ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
  412. pnum, err);
  413. ret = err > 0 ? UBI_BAD_FASTMAP : err;
  414. goto out;
  415. } else if (err == UBI_IO_BITFLIPS)
  416. scrub = 1;
  417. /*
  418. * Older UBI implementations have image_seq set to zero, so
  419. * we shouldn't fail if image_seq == 0.
  420. */
  421. image_seq = be32_to_cpu(ech->image_seq);
  422. if (image_seq && (image_seq != ubi->image_seq)) {
  423. ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
  424. be32_to_cpu(ech->image_seq), ubi->image_seq);
  425. ret = UBI_BAD_FASTMAP;
  426. goto out;
  427. }
  428. err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  429. if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
  430. unsigned long long ec = be64_to_cpu(ech->ec);
  431. unmap_peb(ai, pnum);
  432. dbg_bld("Adding PEB to free: %i", pnum);
  433. if (err == UBI_IO_FF_BITFLIPS)
  434. add_aeb(ai, free, pnum, ec, 1);
  435. else
  436. add_aeb(ai, free, pnum, ec, 0);
  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 (!av)
  631. goto fail_bad;
  632. if (PTR_ERR(av) == -EINVAL) {
  633. ubi_err(ubi, "volume (ID %i) already exists",
  634. fmvhdr->vol_id);
  635. goto fail_bad;
  636. }
  637. ai->vols_found++;
  638. if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
  639. ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
  640. fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  641. fm_pos += sizeof(*fm_eba);
  642. fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
  643. if (fm_pos >= fm_size)
  644. goto fail_bad;
  645. if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
  646. ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
  647. be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
  648. goto fail_bad;
  649. }
  650. for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
  651. int pnum = be32_to_cpu(fm_eba->pnum[j]);
  652. if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
  653. continue;
  654. aeb = NULL;
  655. list_for_each_entry(tmp_aeb, &used, u.list) {
  656. if (tmp_aeb->pnum == pnum) {
  657. aeb = tmp_aeb;
  658. break;
  659. }
  660. }
  661. if (!aeb) {
  662. ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
  663. goto fail_bad;
  664. }
  665. aeb->lnum = j;
  666. if (av->highest_lnum <= aeb->lnum)
  667. av->highest_lnum = aeb->lnum;
  668. assign_aeb_to_av(ai, aeb, av);
  669. dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
  670. aeb->pnum, aeb->lnum, av->vol_id);
  671. }
  672. }
  673. ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
  674. if (ret)
  675. goto fail;
  676. ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
  677. if (ret)
  678. goto fail;
  679. if (max_sqnum > ai->max_sqnum)
  680. ai->max_sqnum = max_sqnum;
  681. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
  682. list_move_tail(&tmp_aeb->u.list, &ai->free);
  683. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
  684. list_move_tail(&tmp_aeb->u.list, &ai->erase);
  685. ubi_assert(list_empty(&free));
  686. /*
  687. * If fastmap is leaking PEBs (must not happen), raise a
  688. * fat warning and fall back to scanning mode.
  689. * We do this here because in ubi_wl_init() it's too late
  690. * and we cannot fall back to scanning.
  691. */
  692. if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
  693. ai->bad_peb_count - fm->used_blocks))
  694. goto fail_bad;
  695. return 0;
  696. fail_bad:
  697. ret = UBI_BAD_FASTMAP;
  698. fail:
  699. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
  700. list_del(&tmp_aeb->u.list);
  701. kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
  702. }
  703. list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
  704. list_del(&tmp_aeb->u.list);
  705. kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
  706. }
  707. return ret;
  708. }
  709. /**
  710. * ubi_scan_fastmap - scan the fastmap.
  711. * @ubi: UBI device object
  712. * @ai: UBI attach info to be filled
  713. * @fm_anchor: The fastmap starts at this PEB
  714. *
  715. * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
  716. * UBI_BAD_FASTMAP if one was found but is not usable.
  717. * < 0 indicates an internal error.
  718. */
  719. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  720. int fm_anchor)
  721. {
  722. struct ubi_fm_sb *fmsb, *fmsb2;
  723. struct ubi_vid_hdr *vh;
  724. struct ubi_ec_hdr *ech;
  725. struct ubi_fastmap_layout *fm;
  726. int i, used_blocks, pnum, ret = 0;
  727. size_t fm_size;
  728. __be32 crc, tmp_crc;
  729. unsigned long long sqnum = 0;
  730. down_write(&ubi->fm_protect);
  731. memset(ubi->fm_buf, 0, ubi->fm_size);
  732. fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
  733. if (!fmsb) {
  734. ret = -ENOMEM;
  735. goto out;
  736. }
  737. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  738. if (!fm) {
  739. ret = -ENOMEM;
  740. kfree(fmsb);
  741. goto out;
  742. }
  743. ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
  744. if (ret && ret != UBI_IO_BITFLIPS)
  745. goto free_fm_sb;
  746. else if (ret == UBI_IO_BITFLIPS)
  747. fm->to_be_tortured[0] = 1;
  748. if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
  749. ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
  750. be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
  751. ret = UBI_BAD_FASTMAP;
  752. goto free_fm_sb;
  753. }
  754. if (fmsb->version != UBI_FM_FMT_VERSION) {
  755. ubi_err(ubi, "bad fastmap version: %i, expected: %i",
  756. fmsb->version, UBI_FM_FMT_VERSION);
  757. ret = UBI_BAD_FASTMAP;
  758. goto free_fm_sb;
  759. }
  760. used_blocks = be32_to_cpu(fmsb->used_blocks);
  761. if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
  762. ubi_err(ubi, "number of fastmap blocks is invalid: %i",
  763. used_blocks);
  764. ret = UBI_BAD_FASTMAP;
  765. goto free_fm_sb;
  766. }
  767. fm_size = ubi->leb_size * used_blocks;
  768. if (fm_size != ubi->fm_size) {
  769. ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
  770. fm_size, ubi->fm_size);
  771. ret = UBI_BAD_FASTMAP;
  772. goto free_fm_sb;
  773. }
  774. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  775. if (!ech) {
  776. ret = -ENOMEM;
  777. goto free_fm_sb;
  778. }
  779. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  780. if (!vh) {
  781. ret = -ENOMEM;
  782. goto free_hdr;
  783. }
  784. for (i = 0; i < used_blocks; i++) {
  785. int image_seq;
  786. pnum = be32_to_cpu(fmsb->block_loc[i]);
  787. if (ubi_io_is_bad(ubi, pnum)) {
  788. ret = UBI_BAD_FASTMAP;
  789. goto free_hdr;
  790. }
  791. ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  792. if (ret && ret != UBI_IO_BITFLIPS) {
  793. ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
  794. i, pnum);
  795. if (ret > 0)
  796. ret = UBI_BAD_FASTMAP;
  797. goto free_hdr;
  798. } else if (ret == UBI_IO_BITFLIPS)
  799. fm->to_be_tortured[i] = 1;
  800. image_seq = be32_to_cpu(ech->image_seq);
  801. if (!ubi->image_seq)
  802. ubi->image_seq = image_seq;
  803. /*
  804. * Older UBI implementations have image_seq set to zero, so
  805. * we shouldn't fail if image_seq == 0.
  806. */
  807. if (image_seq && (image_seq != ubi->image_seq)) {
  808. ubi_err(ubi, "wrong image seq:%d instead of %d",
  809. be32_to_cpu(ech->image_seq), ubi->image_seq);
  810. ret = UBI_BAD_FASTMAP;
  811. goto free_hdr;
  812. }
  813. ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  814. if (ret && ret != UBI_IO_BITFLIPS) {
  815. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
  816. i, pnum);
  817. goto free_hdr;
  818. }
  819. if (i == 0) {
  820. if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
  821. ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
  822. be32_to_cpu(vh->vol_id),
  823. UBI_FM_SB_VOLUME_ID);
  824. ret = UBI_BAD_FASTMAP;
  825. goto free_hdr;
  826. }
  827. } else {
  828. if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
  829. ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
  830. be32_to_cpu(vh->vol_id),
  831. UBI_FM_DATA_VOLUME_ID);
  832. ret = UBI_BAD_FASTMAP;
  833. goto free_hdr;
  834. }
  835. }
  836. if (sqnum < be64_to_cpu(vh->sqnum))
  837. sqnum = be64_to_cpu(vh->sqnum);
  838. ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
  839. ubi->leb_start, ubi->leb_size);
  840. if (ret && ret != UBI_IO_BITFLIPS) {
  841. ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
  842. "err: %i)", i, pnum, ret);
  843. goto free_hdr;
  844. }
  845. }
  846. kfree(fmsb);
  847. fmsb = NULL;
  848. fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
  849. tmp_crc = be32_to_cpu(fmsb2->data_crc);
  850. fmsb2->data_crc = 0;
  851. crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
  852. if (crc != tmp_crc) {
  853. ubi_err(ubi, "fastmap data CRC is invalid");
  854. ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
  855. tmp_crc, crc);
  856. ret = UBI_BAD_FASTMAP;
  857. goto free_hdr;
  858. }
  859. fmsb2->sqnum = sqnum;
  860. fm->used_blocks = used_blocks;
  861. ret = ubi_attach_fastmap(ubi, ai, fm);
  862. if (ret) {
  863. if (ret > 0)
  864. ret = UBI_BAD_FASTMAP;
  865. goto free_hdr;
  866. }
  867. for (i = 0; i < used_blocks; i++) {
  868. struct ubi_wl_entry *e;
  869. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  870. if (!e) {
  871. while (i--)
  872. kfree(fm->e[i]);
  873. ret = -ENOMEM;
  874. goto free_hdr;
  875. }
  876. e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
  877. e->ec = be32_to_cpu(fmsb2->block_ec[i]);
  878. fm->e[i] = e;
  879. }
  880. ubi->fm = fm;
  881. ubi->fm_pool.max_size = ubi->fm->max_pool_size;
  882. ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
  883. ubi_msg(ubi, "attached by fastmap");
  884. ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
  885. ubi_msg(ubi, "fastmap WL pool size: %d",
  886. ubi->fm_wl_pool.max_size);
  887. ubi->fm_disabled = 0;
  888. ubi_free_vid_hdr(ubi, vh);
  889. kfree(ech);
  890. out:
  891. up_write(&ubi->fm_protect);
  892. if (ret == UBI_BAD_FASTMAP)
  893. ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
  894. return ret;
  895. free_hdr:
  896. ubi_free_vid_hdr(ubi, vh);
  897. kfree(ech);
  898. free_fm_sb:
  899. kfree(fmsb);
  900. kfree(fm);
  901. goto out;
  902. }
  903. /**
  904. * ubi_write_fastmap - writes a fastmap.
  905. * @ubi: UBI device object
  906. * @new_fm: the to be written fastmap
  907. *
  908. * Returns 0 on success, < 0 indicates an internal error.
  909. */
  910. static int ubi_write_fastmap(struct ubi_device *ubi,
  911. struct ubi_fastmap_layout *new_fm)
  912. {
  913. size_t fm_pos = 0;
  914. void *fm_raw;
  915. struct ubi_fm_sb *fmsb;
  916. struct ubi_fm_hdr *fmh;
  917. struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
  918. struct ubi_fm_ec *fec;
  919. struct ubi_fm_volhdr *fvh;
  920. struct ubi_fm_eba *feba;
  921. struct ubi_wl_entry *wl_e;
  922. struct ubi_volume *vol;
  923. struct ubi_vid_hdr *avhdr, *dvhdr;
  924. struct ubi_work *ubi_wrk;
  925. struct rb_node *tmp_rb;
  926. int ret, i, j, free_peb_count, used_peb_count, vol_count;
  927. int scrub_peb_count, erase_peb_count;
  928. int *seen_pebs = NULL;
  929. fm_raw = ubi->fm_buf;
  930. memset(ubi->fm_buf, 0, ubi->fm_size);
  931. avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
  932. if (!avhdr) {
  933. ret = -ENOMEM;
  934. goto out;
  935. }
  936. dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
  937. if (!dvhdr) {
  938. ret = -ENOMEM;
  939. goto out_kfree;
  940. }
  941. seen_pebs = init_seen(ubi);
  942. if (IS_ERR(seen_pebs)) {
  943. ret = PTR_ERR(seen_pebs);
  944. goto out_kfree;
  945. }
  946. spin_lock(&ubi->volumes_lock);
  947. spin_lock(&ubi->wl_lock);
  948. fmsb = (struct ubi_fm_sb *)fm_raw;
  949. fm_pos += sizeof(*fmsb);
  950. ubi_assert(fm_pos <= ubi->fm_size);
  951. fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  952. fm_pos += sizeof(*fmh);
  953. ubi_assert(fm_pos <= ubi->fm_size);
  954. fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
  955. fmsb->version = UBI_FM_FMT_VERSION;
  956. fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
  957. /* the max sqnum will be filled in while *reading* the fastmap */
  958. fmsb->sqnum = 0;
  959. fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
  960. free_peb_count = 0;
  961. used_peb_count = 0;
  962. scrub_peb_count = 0;
  963. erase_peb_count = 0;
  964. vol_count = 0;
  965. fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  966. fm_pos += sizeof(*fmpl);
  967. fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  968. fmpl->size = cpu_to_be16(ubi->fm_pool.size);
  969. fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
  970. for (i = 0; i < ubi->fm_pool.size; i++) {
  971. fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
  972. set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
  973. }
  974. fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  975. fm_pos += sizeof(*fmpl_wl);
  976. fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
  977. fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
  978. fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
  979. for (i = 0; i < ubi->fm_wl_pool.size; i++) {
  980. fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
  981. set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
  982. }
  983. ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
  984. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  985. fec->pnum = cpu_to_be32(wl_e->pnum);
  986. set_seen(ubi, wl_e->pnum, seen_pebs);
  987. fec->ec = cpu_to_be32(wl_e->ec);
  988. free_peb_count++;
  989. fm_pos += sizeof(*fec);
  990. ubi_assert(fm_pos <= ubi->fm_size);
  991. }
  992. fmh->free_peb_count = cpu_to_be32(free_peb_count);
  993. ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
  994. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  995. fec->pnum = cpu_to_be32(wl_e->pnum);
  996. set_seen(ubi, wl_e->pnum, seen_pebs);
  997. fec->ec = cpu_to_be32(wl_e->ec);
  998. used_peb_count++;
  999. fm_pos += sizeof(*fec);
  1000. ubi_assert(fm_pos <= ubi->fm_size);
  1001. }
  1002. ubi_for_each_protected_peb(ubi, i, wl_e) {
  1003. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1004. fec->pnum = cpu_to_be32(wl_e->pnum);
  1005. set_seen(ubi, wl_e->pnum, seen_pebs);
  1006. fec->ec = cpu_to_be32(wl_e->ec);
  1007. used_peb_count++;
  1008. fm_pos += sizeof(*fec);
  1009. ubi_assert(fm_pos <= ubi->fm_size);
  1010. }
  1011. fmh->used_peb_count = cpu_to_be32(used_peb_count);
  1012. ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
  1013. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1014. fec->pnum = cpu_to_be32(wl_e->pnum);
  1015. set_seen(ubi, wl_e->pnum, seen_pebs);
  1016. fec->ec = cpu_to_be32(wl_e->ec);
  1017. scrub_peb_count++;
  1018. fm_pos += sizeof(*fec);
  1019. ubi_assert(fm_pos <= ubi->fm_size);
  1020. }
  1021. fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
  1022. list_for_each_entry(ubi_wrk, &ubi->works, list) {
  1023. if (ubi_is_erase_work(ubi_wrk)) {
  1024. wl_e = ubi_wrk->e;
  1025. ubi_assert(wl_e);
  1026. fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  1027. fec->pnum = cpu_to_be32(wl_e->pnum);
  1028. set_seen(ubi, wl_e->pnum, seen_pebs);
  1029. fec->ec = cpu_to_be32(wl_e->ec);
  1030. erase_peb_count++;
  1031. fm_pos += sizeof(*fec);
  1032. ubi_assert(fm_pos <= ubi->fm_size);
  1033. }
  1034. }
  1035. fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
  1036. for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
  1037. vol = ubi->volumes[i];
  1038. if (!vol)
  1039. continue;
  1040. vol_count++;
  1041. fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  1042. fm_pos += sizeof(*fvh);
  1043. ubi_assert(fm_pos <= ubi->fm_size);
  1044. fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
  1045. fvh->vol_id = cpu_to_be32(vol->vol_id);
  1046. fvh->vol_type = vol->vol_type;
  1047. fvh->used_ebs = cpu_to_be32(vol->used_ebs);
  1048. fvh->data_pad = cpu_to_be32(vol->data_pad);
  1049. fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
  1050. ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
  1051. vol->vol_type == UBI_STATIC_VOLUME);
  1052. feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  1053. fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
  1054. ubi_assert(fm_pos <= ubi->fm_size);
  1055. for (j = 0; j < vol->reserved_pebs; j++)
  1056. feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
  1057. feba->reserved_pebs = cpu_to_be32(j);
  1058. feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
  1059. }
  1060. fmh->vol_count = cpu_to_be32(vol_count);
  1061. fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
  1062. avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1063. avhdr->lnum = 0;
  1064. spin_unlock(&ubi->wl_lock);
  1065. spin_unlock(&ubi->volumes_lock);
  1066. dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
  1067. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
  1068. if (ret) {
  1069. ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
  1070. goto out_kfree;
  1071. }
  1072. for (i = 0; i < new_fm->used_blocks; i++) {
  1073. fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
  1074. set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
  1075. fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
  1076. }
  1077. fmsb->data_crc = 0;
  1078. fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
  1079. ubi->fm_size));
  1080. for (i = 1; i < new_fm->used_blocks; i++) {
  1081. dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1082. dvhdr->lnum = cpu_to_be32(i);
  1083. dbg_bld("writing fastmap data to PEB %i sqnum %llu",
  1084. new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
  1085. ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
  1086. if (ret) {
  1087. ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
  1088. new_fm->e[i]->pnum);
  1089. goto out_kfree;
  1090. }
  1091. }
  1092. for (i = 0; i < new_fm->used_blocks; i++) {
  1093. ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
  1094. new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
  1095. if (ret) {
  1096. ubi_err(ubi, "unable to write fastmap to PEB %i!",
  1097. new_fm->e[i]->pnum);
  1098. goto out_kfree;
  1099. }
  1100. }
  1101. ubi_assert(new_fm);
  1102. ubi->fm = new_fm;
  1103. ret = self_check_seen(ubi, seen_pebs);
  1104. dbg_bld("fastmap written!");
  1105. out_kfree:
  1106. ubi_free_vid_hdr(ubi, avhdr);
  1107. ubi_free_vid_hdr(ubi, dvhdr);
  1108. free_seen(seen_pebs);
  1109. out:
  1110. return ret;
  1111. }
  1112. /**
  1113. * erase_block - Manually erase a PEB.
  1114. * @ubi: UBI device object
  1115. * @pnum: PEB to be erased
  1116. *
  1117. * Returns the new EC value on success, < 0 indicates an internal error.
  1118. */
  1119. static int erase_block(struct ubi_device *ubi, int pnum)
  1120. {
  1121. int ret;
  1122. struct ubi_ec_hdr *ec_hdr;
  1123. long long ec;
  1124. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  1125. if (!ec_hdr)
  1126. return -ENOMEM;
  1127. ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1128. if (ret < 0)
  1129. goto out;
  1130. else if (ret && ret != UBI_IO_BITFLIPS) {
  1131. ret = -EINVAL;
  1132. goto out;
  1133. }
  1134. ret = ubi_io_sync_erase(ubi, pnum, 0);
  1135. if (ret < 0)
  1136. goto out;
  1137. ec = be64_to_cpu(ec_hdr->ec);
  1138. ec += ret;
  1139. if (ec > UBI_MAX_ERASECOUNTER) {
  1140. ret = -EINVAL;
  1141. goto out;
  1142. }
  1143. ec_hdr->ec = cpu_to_be64(ec);
  1144. ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
  1145. if (ret < 0)
  1146. goto out;
  1147. ret = ec;
  1148. out:
  1149. kfree(ec_hdr);
  1150. return ret;
  1151. }
  1152. /**
  1153. * invalidate_fastmap - destroys a fastmap.
  1154. * @ubi: UBI device object
  1155. *
  1156. * This function ensures that upon next UBI attach a full scan
  1157. * is issued. We need this if UBI is about to write a new fastmap
  1158. * but is unable to do so. In this case we have two options:
  1159. * a) Make sure that the current fastmap will not be usued upon
  1160. * attach time and contine or b) fall back to RO mode to have the
  1161. * current fastmap in a valid state.
  1162. * Returns 0 on success, < 0 indicates an internal error.
  1163. */
  1164. static int invalidate_fastmap(struct ubi_device *ubi)
  1165. {
  1166. int ret;
  1167. struct ubi_fastmap_layout *fm;
  1168. struct ubi_wl_entry *e;
  1169. struct ubi_vid_hdr *vh = NULL;
  1170. if (!ubi->fm)
  1171. return 0;
  1172. ubi->fm = NULL;
  1173. ret = -ENOMEM;
  1174. fm = kzalloc(sizeof(*fm), GFP_KERNEL);
  1175. if (!fm)
  1176. goto out;
  1177. vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
  1178. if (!vh)
  1179. goto out_free_fm;
  1180. ret = -ENOSPC;
  1181. e = ubi_wl_get_fm_peb(ubi, 1);
  1182. if (!e)
  1183. goto out_free_fm;
  1184. /*
  1185. * Create fake fastmap such that UBI will fall back
  1186. * to scanning mode.
  1187. */
  1188. vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1189. ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
  1190. if (ret < 0) {
  1191. ubi_wl_put_fm_peb(ubi, e, 0, 0);
  1192. goto out_free_fm;
  1193. }
  1194. fm->used_blocks = 1;
  1195. fm->e[0] = e;
  1196. ubi->fm = fm;
  1197. out:
  1198. ubi_free_vid_hdr(ubi, vh);
  1199. return ret;
  1200. out_free_fm:
  1201. kfree(fm);
  1202. goto out;
  1203. }
  1204. /**
  1205. * return_fm_pebs - returns all PEBs used by a fastmap back to the
  1206. * WL sub-system.
  1207. * @ubi: UBI device object
  1208. * @fm: fastmap layout object
  1209. */
  1210. static void return_fm_pebs(struct ubi_device *ubi,
  1211. struct ubi_fastmap_layout *fm)
  1212. {
  1213. int i;
  1214. if (!fm)
  1215. return;
  1216. for (i = 0; i < fm->used_blocks; i++) {
  1217. if (fm->e[i]) {
  1218. ubi_wl_put_fm_peb(ubi, fm->e[i], i,
  1219. fm->to_be_tortured[i]);
  1220. fm->e[i] = NULL;
  1221. }
  1222. }
  1223. }
  1224. /**
  1225. * ubi_update_fastmap - will be called by UBI if a volume changes or
  1226. * a fastmap pool becomes full.
  1227. * @ubi: UBI device object
  1228. *
  1229. * Returns 0 on success, < 0 indicates an internal error.
  1230. */
  1231. int ubi_update_fastmap(struct ubi_device *ubi)
  1232. {
  1233. int ret, i, j;
  1234. struct ubi_fastmap_layout *new_fm, *old_fm;
  1235. struct ubi_wl_entry *tmp_e;
  1236. down_write(&ubi->fm_protect);
  1237. ubi_refill_pools(ubi);
  1238. if (ubi->ro_mode || ubi->fm_disabled) {
  1239. up_write(&ubi->fm_protect);
  1240. return 0;
  1241. }
  1242. ret = ubi_ensure_anchor_pebs(ubi);
  1243. if (ret) {
  1244. up_write(&ubi->fm_protect);
  1245. return ret;
  1246. }
  1247. new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
  1248. if (!new_fm) {
  1249. up_write(&ubi->fm_protect);
  1250. return -ENOMEM;
  1251. }
  1252. new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
  1253. old_fm = ubi->fm;
  1254. ubi->fm = NULL;
  1255. if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
  1256. ubi_err(ubi, "fastmap too large");
  1257. ret = -ENOSPC;
  1258. goto err;
  1259. }
  1260. for (i = 1; i < new_fm->used_blocks; i++) {
  1261. spin_lock(&ubi->wl_lock);
  1262. tmp_e = ubi_wl_get_fm_peb(ubi, 0);
  1263. spin_unlock(&ubi->wl_lock);
  1264. if (!tmp_e) {
  1265. if (old_fm && old_fm->e[i]) {
  1266. ret = erase_block(ubi, old_fm->e[i]->pnum);
  1267. if (ret < 0) {
  1268. ubi_err(ubi, "could not erase old fastmap PEB");
  1269. for (j = 1; j < i; j++) {
  1270. ubi_wl_put_fm_peb(ubi, new_fm->e[j],
  1271. j, 0);
  1272. new_fm->e[j] = NULL;
  1273. }
  1274. goto err;
  1275. }
  1276. new_fm->e[i] = old_fm->e[i];
  1277. old_fm->e[i] = NULL;
  1278. } else {
  1279. ubi_err(ubi, "could not get any free erase block");
  1280. for (j = 1; j < i; j++) {
  1281. ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
  1282. new_fm->e[j] = NULL;
  1283. }
  1284. ret = -ENOSPC;
  1285. goto err;
  1286. }
  1287. } else {
  1288. new_fm->e[i] = tmp_e;
  1289. if (old_fm && old_fm->e[i]) {
  1290. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1291. old_fm->to_be_tortured[i]);
  1292. old_fm->e[i] = NULL;
  1293. }
  1294. }
  1295. }
  1296. /* Old fastmap is larger than the new one */
  1297. if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
  1298. for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
  1299. ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
  1300. old_fm->to_be_tortured[i]);
  1301. old_fm->e[i] = NULL;
  1302. }
  1303. }
  1304. spin_lock(&ubi->wl_lock);
  1305. tmp_e = ubi_wl_get_fm_peb(ubi, 1);
  1306. spin_unlock(&ubi->wl_lock);
  1307. if (old_fm) {
  1308. /* no fresh anchor PEB was found, reuse the old one */
  1309. if (!tmp_e) {
  1310. ret = erase_block(ubi, old_fm->e[0]->pnum);
  1311. if (ret < 0) {
  1312. ubi_err(ubi, "could not erase old anchor PEB");
  1313. for (i = 1; i < new_fm->used_blocks; i++) {
  1314. ubi_wl_put_fm_peb(ubi, new_fm->e[i],
  1315. i, 0);
  1316. new_fm->e[i] = NULL;
  1317. }
  1318. goto err;
  1319. }
  1320. new_fm->e[0] = old_fm->e[0];
  1321. new_fm->e[0]->ec = ret;
  1322. old_fm->e[0] = NULL;
  1323. } else {
  1324. /* we've got a new anchor PEB, return the old one */
  1325. ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
  1326. old_fm->to_be_tortured[0]);
  1327. new_fm->e[0] = tmp_e;
  1328. old_fm->e[0] = NULL;
  1329. }
  1330. } else {
  1331. if (!tmp_e) {
  1332. ubi_err(ubi, "could not find any anchor PEB");
  1333. for (i = 1; i < new_fm->used_blocks; i++) {
  1334. ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
  1335. new_fm->e[i] = NULL;
  1336. }
  1337. ret = -ENOSPC;
  1338. goto err;
  1339. }
  1340. new_fm->e[0] = tmp_e;
  1341. }
  1342. down_write(&ubi->work_sem);
  1343. down_write(&ubi->fm_eba_sem);
  1344. ret = ubi_write_fastmap(ubi, new_fm);
  1345. up_write(&ubi->fm_eba_sem);
  1346. up_write(&ubi->work_sem);
  1347. if (ret)
  1348. goto err;
  1349. out_unlock:
  1350. up_write(&ubi->fm_protect);
  1351. kfree(old_fm);
  1352. return ret;
  1353. err:
  1354. ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
  1355. ret = invalidate_fastmap(ubi);
  1356. if (ret < 0) {
  1357. ubi_err(ubi, "Unable to invalidiate current fastmap!");
  1358. ubi_ro_mode(ubi);
  1359. } else {
  1360. return_fm_pebs(ubi, old_fm);
  1361. return_fm_pebs(ubi, new_fm);
  1362. ret = 0;
  1363. }
  1364. kfree(new_fm);
  1365. goto out_unlock;
  1366. }