fastmap.c 38 KB

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