fastmap-wl.c 8.5 KB

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