fastmap-wl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * produce_free_peb - produce a free physical eraseblock.
  146. * @ubi: UBI device description object
  147. *
  148. * This function tries to make a free PEB by means of synchronous execution of
  149. * pending works. This may be needed if, for example the background thread is
  150. * disabled. Returns zero in case of success and a negative error code in case
  151. * of failure.
  152. */
  153. static int produce_free_peb(struct ubi_device *ubi)
  154. {
  155. int err;
  156. while (!ubi->free.rb_node && ubi->works_count) {
  157. dbg_wl("do one work synchronously");
  158. err = do_work(ubi);
  159. if (err)
  160. return err;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * ubi_wl_get_peb - get a physical eraseblock.
  166. * @ubi: UBI device description object
  167. *
  168. * This function returns a physical eraseblock in case of success and a
  169. * negative error code in case of failure.
  170. * Returns with ubi->fm_eba_sem held in read mode!
  171. */
  172. int ubi_wl_get_peb(struct ubi_device *ubi)
  173. {
  174. int ret, retried = 0;
  175. struct ubi_fm_pool *pool = &ubi->fm_pool;
  176. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  177. again:
  178. down_read(&ubi->fm_eba_sem);
  179. spin_lock(&ubi->wl_lock);
  180. /* We check here also for the WL pool because at this point we can
  181. * refill the WL pool synchronous. */
  182. if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
  183. spin_unlock(&ubi->wl_lock);
  184. up_read(&ubi->fm_eba_sem);
  185. ret = ubi_update_fastmap(ubi);
  186. if (ret) {
  187. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  188. down_read(&ubi->fm_eba_sem);
  189. return -ENOSPC;
  190. }
  191. down_read(&ubi->fm_eba_sem);
  192. spin_lock(&ubi->wl_lock);
  193. }
  194. if (pool->used == pool->size) {
  195. spin_unlock(&ubi->wl_lock);
  196. if (retried) {
  197. ubi_err(ubi, "Unable to get a free PEB from user WL pool");
  198. ret = -ENOSPC;
  199. goto out;
  200. }
  201. retried = 1;
  202. up_read(&ubi->fm_eba_sem);
  203. ret = produce_free_peb(ubi);
  204. if (ret < 0) {
  205. down_read(&ubi->fm_eba_sem);
  206. goto out;
  207. }
  208. goto again;
  209. }
  210. ubi_assert(pool->used < pool->size);
  211. ret = pool->pebs[pool->used++];
  212. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  213. spin_unlock(&ubi->wl_lock);
  214. out:
  215. return ret;
  216. }
  217. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  218. *
  219. * @ubi: UBI device description object
  220. */
  221. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  222. {
  223. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  224. int pnum;
  225. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  226. if (pool->used == pool->size) {
  227. /* We cannot update the fastmap here because this
  228. * function is called in atomic context.
  229. * Let's fail here and refill/update it as soon as possible. */
  230. if (!ubi->fm_work_scheduled) {
  231. ubi->fm_work_scheduled = 1;
  232. schedule_work(&ubi->fm_work);
  233. }
  234. return NULL;
  235. }
  236. pnum = pool->pebs[pool->used++];
  237. return ubi->lookuptbl[pnum];
  238. }
  239. /**
  240. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  241. * @ubi: UBI device description object
  242. */
  243. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  244. {
  245. struct ubi_work *wrk;
  246. spin_lock(&ubi->wl_lock);
  247. if (ubi->wl_scheduled) {
  248. spin_unlock(&ubi->wl_lock);
  249. return 0;
  250. }
  251. ubi->wl_scheduled = 1;
  252. spin_unlock(&ubi->wl_lock);
  253. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  254. if (!wrk) {
  255. spin_lock(&ubi->wl_lock);
  256. ubi->wl_scheduled = 0;
  257. spin_unlock(&ubi->wl_lock);
  258. return -ENOMEM;
  259. }
  260. wrk->anchor = 1;
  261. wrk->func = &wear_leveling_worker;
  262. __schedule_ubi_work(ubi, wrk);
  263. return 0;
  264. }
  265. /**
  266. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  267. * sub-system.
  268. * see: ubi_wl_put_peb()
  269. *
  270. * @ubi: UBI device description object
  271. * @fm_e: physical eraseblock to return
  272. * @lnum: the last used logical eraseblock number for the PEB
  273. * @torture: if this physical eraseblock has to be tortured
  274. */
  275. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  276. int lnum, int torture)
  277. {
  278. struct ubi_wl_entry *e;
  279. int vol_id, pnum = fm_e->pnum;
  280. dbg_wl("PEB %d", pnum);
  281. ubi_assert(pnum >= 0);
  282. ubi_assert(pnum < ubi->peb_count);
  283. spin_lock(&ubi->wl_lock);
  284. e = ubi->lookuptbl[pnum];
  285. /* This can happen if we recovered from a fastmap the very
  286. * first time and writing now a new one. In this case the wl system
  287. * has never seen any PEB used by the original fastmap.
  288. */
  289. if (!e) {
  290. e = fm_e;
  291. ubi_assert(e->ec >= 0);
  292. ubi->lookuptbl[pnum] = e;
  293. }
  294. spin_unlock(&ubi->wl_lock);
  295. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  296. return schedule_erase(ubi, e, vol_id, lnum, torture, true);
  297. }
  298. /**
  299. * ubi_is_erase_work - checks whether a work is erase work.
  300. * @wrk: The work object to be checked
  301. */
  302. int ubi_is_erase_work(struct ubi_work *wrk)
  303. {
  304. return wrk->func == erase_worker;
  305. }
  306. static void ubi_fastmap_close(struct ubi_device *ubi)
  307. {
  308. int i;
  309. return_unused_pool_pebs(ubi, &ubi->fm_pool);
  310. return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
  311. if (ubi->fm) {
  312. for (i = 0; i < ubi->fm->used_blocks; i++)
  313. kfree(ubi->fm->e[i]);
  314. }
  315. kfree(ubi->fm);
  316. }
  317. /**
  318. * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
  319. * See find_mean_wl_entry()
  320. *
  321. * @ubi: UBI device description object
  322. * @e: physical eraseblock to return
  323. * @root: RB tree to test against.
  324. */
  325. static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
  326. struct ubi_wl_entry *e,
  327. struct rb_root *root) {
  328. if (e && !ubi->fm_disabled && !ubi->fm &&
  329. e->pnum < UBI_FM_MAX_START)
  330. e = rb_entry(rb_next(root->rb_node),
  331. struct ubi_wl_entry, u.rb);
  332. return e;
  333. }