fastmap-wl.c 9.0 KB

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