fastmap-wl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. /**
  17. * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
  18. * @wrk: the work description object
  19. */
  20. static void update_fastmap_work_fn(struct work_struct *wrk)
  21. {
  22. struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
  23. ubi_update_fastmap(ubi);
  24. spin_lock(&ubi->wl_lock);
  25. ubi->fm_work_scheduled = 0;
  26. spin_unlock(&ubi->wl_lock);
  27. }
  28. /**
  29. * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
  30. * @root: the RB-tree where to look for
  31. */
  32. static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
  33. {
  34. struct rb_node *p;
  35. struct ubi_wl_entry *e, *victim = NULL;
  36. int max_ec = UBI_MAX_ERASECOUNTER;
  37. ubi_rb_for_each_entry(p, e, root, u.rb) {
  38. if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
  39. victim = e;
  40. max_ec = e->ec;
  41. }
  42. }
  43. return victim;
  44. }
  45. static inline void return_unused_peb(struct ubi_device *ubi,
  46. struct ubi_wl_entry *e)
  47. {
  48. wl_tree_add(e, &ubi->free);
  49. ubi->free_count++;
  50. }
  51. /**
  52. * return_unused_pool_pebs - returns unused PEB to the free tree.
  53. * @ubi: UBI device description object
  54. * @pool: fastmap pool description object
  55. */
  56. static void return_unused_pool_pebs(struct ubi_device *ubi,
  57. struct ubi_fm_pool *pool)
  58. {
  59. int i;
  60. struct ubi_wl_entry *e;
  61. for (i = pool->used; i < pool->size; i++) {
  62. e = ubi->lookuptbl[pool->pebs[i]];
  63. return_unused_peb(ubi, e);
  64. }
  65. }
  66. /**
  67. * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
  68. * @ubi: UBI device description object
  69. * @anchor: This PEB will be used as anchor PEB by fastmap
  70. *
  71. * The function returns a physical erase block with a given maximal number
  72. * and removes it from the wl subsystem.
  73. * Must be called with wl_lock held!
  74. */
  75. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
  76. {
  77. struct ubi_wl_entry *e = NULL;
  78. if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  79. goto out;
  80. if (anchor)
  81. e = find_anchor_wl_entry(&ubi->free);
  82. else
  83. e = find_mean_wl_entry(ubi, &ubi->free);
  84. if (!e)
  85. goto out;
  86. self_check_in_wl_tree(ubi, e, &ubi->free);
  87. /* remove it from the free list,
  88. * the wl subsystem does no longer know this erase block */
  89. rb_erase(&e->u.rb, &ubi->free);
  90. ubi->free_count--;
  91. out:
  92. return e;
  93. }
  94. /**
  95. * ubi_refill_pools - refills all fastmap PEB pools.
  96. * @ubi: UBI device description object
  97. */
  98. void ubi_refill_pools(struct ubi_device *ubi)
  99. {
  100. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  101. struct ubi_fm_pool *pool = &ubi->fm_pool;
  102. struct ubi_wl_entry *e;
  103. int enough;
  104. spin_lock(&ubi->wl_lock);
  105. return_unused_pool_pebs(ubi, wl_pool);
  106. return_unused_pool_pebs(ubi, pool);
  107. wl_pool->size = 0;
  108. pool->size = 0;
  109. for (;;) {
  110. enough = 0;
  111. if (pool->size < pool->max_size) {
  112. if (!ubi->free.rb_node)
  113. break;
  114. e = wl_get_wle(ubi);
  115. if (!e)
  116. break;
  117. pool->pebs[pool->size] = e->pnum;
  118. pool->size++;
  119. } else
  120. enough++;
  121. if (wl_pool->size < wl_pool->max_size) {
  122. if (!ubi->free.rb_node ||
  123. (ubi->free_count - ubi->beb_rsvd_pebs < 5))
  124. break;
  125. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  126. self_check_in_wl_tree(ubi, e, &ubi->free);
  127. rb_erase(&e->u.rb, &ubi->free);
  128. ubi->free_count--;
  129. wl_pool->pebs[wl_pool->size] = e->pnum;
  130. wl_pool->size++;
  131. } else
  132. enough++;
  133. if (enough == 2)
  134. break;
  135. }
  136. wl_pool->used = 0;
  137. pool->used = 0;
  138. spin_unlock(&ubi->wl_lock);
  139. }
  140. /**
  141. * produce_free_peb - produce a free physical eraseblock.
  142. * @ubi: UBI device description object
  143. *
  144. * This function tries to make a free PEB by means of synchronous execution of
  145. * pending works. This may be needed if, for example the background thread is
  146. * disabled. Returns zero in case of success and a negative error code in case
  147. * of failure.
  148. */
  149. static int produce_free_peb(struct ubi_device *ubi)
  150. {
  151. int err;
  152. while (!ubi->free.rb_node && ubi->works_count) {
  153. dbg_wl("do one work synchronously");
  154. err = do_work(ubi);
  155. if (err)
  156. return err;
  157. }
  158. return 0;
  159. }
  160. /**
  161. * ubi_wl_get_peb - get a physical eraseblock.
  162. * @ubi: UBI device description object
  163. *
  164. * This function returns a physical eraseblock in case of success and a
  165. * negative error code in case of failure.
  166. * Returns with ubi->fm_eba_sem held in read mode!
  167. */
  168. int ubi_wl_get_peb(struct ubi_device *ubi)
  169. {
  170. int ret, retried = 0;
  171. struct ubi_fm_pool *pool = &ubi->fm_pool;
  172. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  173. again:
  174. down_read(&ubi->fm_eba_sem);
  175. spin_lock(&ubi->wl_lock);
  176. /* We check here also for the WL pool because at this point we can
  177. * refill the WL pool synchronous. */
  178. if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
  179. spin_unlock(&ubi->wl_lock);
  180. up_read(&ubi->fm_eba_sem);
  181. ret = ubi_update_fastmap(ubi);
  182. if (ret) {
  183. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  184. down_read(&ubi->fm_eba_sem);
  185. return -ENOSPC;
  186. }
  187. down_read(&ubi->fm_eba_sem);
  188. spin_lock(&ubi->wl_lock);
  189. }
  190. if (pool->used == pool->size) {
  191. spin_unlock(&ubi->wl_lock);
  192. if (retried) {
  193. ubi_err(ubi, "Unable to get a free PEB from user WL pool");
  194. ret = -ENOSPC;
  195. goto out;
  196. }
  197. retried = 1;
  198. up_read(&ubi->fm_eba_sem);
  199. ret = produce_free_peb(ubi);
  200. if (ret < 0) {
  201. down_read(&ubi->fm_eba_sem);
  202. goto out;
  203. }
  204. goto again;
  205. }
  206. ubi_assert(pool->used < pool->size);
  207. ret = pool->pebs[pool->used++];
  208. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  209. spin_unlock(&ubi->wl_lock);
  210. out:
  211. return ret;
  212. }
  213. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  214. *
  215. * @ubi: UBI device description object
  216. */
  217. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  218. {
  219. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  220. int pnum;
  221. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  222. if (pool->used == pool->size) {
  223. /* We cannot update the fastmap here because this
  224. * function is called in atomic context.
  225. * Let's fail here and refill/update it as soon as possible. */
  226. if (!ubi->fm_work_scheduled) {
  227. ubi->fm_work_scheduled = 1;
  228. schedule_work(&ubi->fm_work);
  229. }
  230. return NULL;
  231. }
  232. pnum = pool->pebs[pool->used++];
  233. return ubi->lookuptbl[pnum];
  234. }
  235. /**
  236. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  237. * @ubi: UBI device description object
  238. */
  239. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  240. {
  241. struct ubi_work *wrk;
  242. struct ubi_wl_entry *anchor;
  243. spin_lock(&ubi->wl_lock);
  244. /* Do we already have an anchor? */
  245. if (ubi->fm_anchor) {
  246. spin_unlock(&ubi->wl_lock);
  247. return 0;
  248. }
  249. /* See if we can find an anchor PEB on the list of free PEBs */
  250. anchor = ubi_wl_get_fm_peb(ubi, 1);
  251. if (anchor) {
  252. ubi->fm_anchor = anchor;
  253. spin_unlock(&ubi->wl_lock);
  254. return 0;
  255. }
  256. /* No luck, trigger wear leveling to produce a new anchor PEB */
  257. ubi->fm_do_produce_anchor = 1;
  258. if (ubi->wl_scheduled) {
  259. spin_unlock(&ubi->wl_lock);
  260. return 0;
  261. }
  262. ubi->wl_scheduled = 1;
  263. spin_unlock(&ubi->wl_lock);
  264. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  265. if (!wrk) {
  266. spin_lock(&ubi->wl_lock);
  267. ubi->wl_scheduled = 0;
  268. spin_unlock(&ubi->wl_lock);
  269. return -ENOMEM;
  270. }
  271. wrk->func = &wear_leveling_worker;
  272. __schedule_ubi_work(ubi, wrk);
  273. return 0;
  274. }
  275. /**
  276. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  277. * sub-system.
  278. * see: ubi_wl_put_peb()
  279. *
  280. * @ubi: UBI device description object
  281. * @fm_e: physical eraseblock to return
  282. * @lnum: the last used logical eraseblock number for the PEB
  283. * @torture: if this physical eraseblock has to be tortured
  284. */
  285. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  286. int lnum, int torture)
  287. {
  288. struct ubi_wl_entry *e;
  289. int vol_id, pnum = fm_e->pnum;
  290. dbg_wl("PEB %d", pnum);
  291. ubi_assert(pnum >= 0);
  292. ubi_assert(pnum < ubi->peb_count);
  293. spin_lock(&ubi->wl_lock);
  294. e = ubi->lookuptbl[pnum];
  295. /* This can happen if we recovered from a fastmap the very
  296. * first time and writing now a new one. In this case the wl system
  297. * has never seen any PEB used by the original fastmap.
  298. */
  299. if (!e) {
  300. e = fm_e;
  301. ubi_assert(e->ec >= 0);
  302. ubi->lookuptbl[pnum] = e;
  303. }
  304. spin_unlock(&ubi->wl_lock);
  305. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  306. return schedule_erase(ubi, e, vol_id, lnum, torture, true);
  307. }
  308. /**
  309. * ubi_is_erase_work - checks whether a work is erase work.
  310. * @wrk: The work object to be checked
  311. */
  312. int ubi_is_erase_work(struct ubi_work *wrk)
  313. {
  314. return wrk->func == erase_worker;
  315. }
  316. static void ubi_fastmap_close(struct ubi_device *ubi)
  317. {
  318. int i;
  319. return_unused_pool_pebs(ubi, &ubi->fm_pool);
  320. return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
  321. if (ubi->fm_anchor) {
  322. return_unused_peb(ubi, ubi->fm_anchor);
  323. ubi->fm_anchor = NULL;
  324. }
  325. if (ubi->fm) {
  326. for (i = 0; i < ubi->fm->used_blocks; i++)
  327. kfree(ubi->fm->e[i]);
  328. }
  329. kfree(ubi->fm);
  330. }
  331. /**
  332. * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
  333. * See find_mean_wl_entry()
  334. *
  335. * @ubi: UBI device description object
  336. * @e: physical eraseblock to return
  337. * @root: RB tree to test against.
  338. */
  339. static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
  340. struct ubi_wl_entry *e,
  341. struct rb_root *root) {
  342. if (e && !ubi->fm_disabled && !ubi->fm &&
  343. e->pnum < UBI_FM_MAX_START)
  344. e = rb_entry(rb_next(root->rb_node),
  345. struct ubi_wl_entry, u.rb);
  346. return e;
  347. }