fastmap-wl.c 8.4 KB

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