alloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Primary bucket allocation code
  4. *
  5. * Copyright 2012 Google, Inc.
  6. *
  7. * Allocation in bcache is done in terms of buckets:
  8. *
  9. * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
  10. * btree pointers - they must match for the pointer to be considered valid.
  11. *
  12. * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
  13. * bucket simply by incrementing its gen.
  14. *
  15. * The gens (along with the priorities; it's really the gens are important but
  16. * the code is named as if it's the priorities) are written in an arbitrary list
  17. * of buckets on disk, with a pointer to them in the journal header.
  18. *
  19. * When we invalidate a bucket, we have to write its new gen to disk and wait
  20. * for that write to complete before we use it - otherwise after a crash we
  21. * could have pointers that appeared to be good but pointed to data that had
  22. * been overwritten.
  23. *
  24. * Since the gens and priorities are all stored contiguously on disk, we can
  25. * batch this up: We fill up the free_inc list with freshly invalidated buckets,
  26. * call prio_write(), and when prio_write() finishes we pull buckets off the
  27. * free_inc list.
  28. *
  29. * free_inc isn't the only freelist - if it was, we'd often to sleep while
  30. * priorities and gens were being written before we could allocate. c->free is a
  31. * smaller freelist, and buckets on that list are always ready to be used.
  32. *
  33. * There is another freelist, because sometimes we have buckets that we know
  34. * have nothing pointing into them - these we can reuse without waiting for
  35. * priorities to be rewritten. These come from freed btree nodes and buckets
  36. * that garbage collection discovered no longer had valid keys pointing into
  37. * them (because they were overwritten). That's the unused list - buckets on the
  38. * unused list move to the free list.
  39. *
  40. * It's also important to ensure that gens don't wrap around - with respect to
  41. * either the oldest gen in the btree or the gen on disk. This is quite
  42. * difficult to do in practice, but we explicitly guard against it anyways - if
  43. * a bucket is in danger of wrapping around we simply skip invalidating it that
  44. * time around, and we garbage collect or rewrite the priorities sooner than we
  45. * would have otherwise.
  46. *
  47. * bch_bucket_alloc() allocates a single bucket from a specific cache.
  48. *
  49. * bch_bucket_alloc_set() allocates one bucket from different caches
  50. * out of a cache set.
  51. *
  52. * free_some_buckets() drives all the processes described above. It's called
  53. * from bch_bucket_alloc() and a few other places that need to make sure free
  54. * buckets are ready.
  55. *
  56. * invalidate_buckets_(lru|fifo)() find buckets that are available to be
  57. * invalidated, and then invalidate them and stick them on the free_inc list -
  58. * in either lru or fifo order.
  59. */
  60. #include "bcache.h"
  61. #include "btree.h"
  62. #include <linux/blkdev.h>
  63. #include <linux/kthread.h>
  64. #include <linux/random.h>
  65. #include <trace/events/bcache.h>
  66. #define MAX_OPEN_BUCKETS 128
  67. /* Bucket heap / gen */
  68. uint8_t bch_inc_gen(struct cache *ca, struct bucket *b)
  69. {
  70. uint8_t ret = ++b->gen;
  71. ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b));
  72. WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX);
  73. return ret;
  74. }
  75. void bch_rescale_priorities(struct cache_set *c, int sectors)
  76. {
  77. struct cache *ca;
  78. struct bucket *b;
  79. unsigned long next = c->nbuckets * c->cache->sb.bucket_size / 1024;
  80. int r;
  81. atomic_sub(sectors, &c->rescale);
  82. do {
  83. r = atomic_read(&c->rescale);
  84. if (r >= 0)
  85. return;
  86. } while (atomic_cmpxchg(&c->rescale, r, r + next) != r);
  87. mutex_lock(&c->bucket_lock);
  88. c->min_prio = USHRT_MAX;
  89. ca = c->cache;
  90. for_each_bucket(b, ca)
  91. if (b->prio &&
  92. b->prio != BTREE_PRIO &&
  93. !atomic_read(&b->pin)) {
  94. b->prio--;
  95. c->min_prio = min(c->min_prio, b->prio);
  96. }
  97. mutex_unlock(&c->bucket_lock);
  98. }
  99. /*
  100. * Background allocation thread: scans for buckets to be invalidated,
  101. * invalidates them, rewrites prios/gens (marking them as invalidated on disk),
  102. * then puts them on the various freelists.
  103. */
  104. static inline bool can_inc_bucket_gen(struct bucket *b)
  105. {
  106. return bucket_gc_gen(b) < BUCKET_GC_GEN_MAX;
  107. }
  108. bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b)
  109. {
  110. return (ca->set->gc_mark_valid || b->reclaimable_in_gc) &&
  111. ((!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) &&
  112. !atomic_read(&b->pin) && can_inc_bucket_gen(b));
  113. }
  114. void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
  115. {
  116. lockdep_assert_held(&ca->set->bucket_lock);
  117. BUG_ON(GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE);
  118. if (GC_SECTORS_USED(b))
  119. trace_bcache_invalidate(ca, b - ca->buckets);
  120. bch_inc_gen(ca, b);
  121. b->prio = INITIAL_PRIO;
  122. atomic_inc(&b->pin);
  123. b->reclaimable_in_gc = 0;
  124. }
  125. static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
  126. {
  127. __bch_invalidate_one_bucket(ca, b);
  128. fifo_push(&ca->free_inc, b - ca->buckets);
  129. }
  130. /*
  131. * Determines what order we're going to reuse buckets, smallest bucket_prio()
  132. * first: we also take into account the number of sectors of live data in that
  133. * bucket, and in order for that multiply to make sense we have to scale bucket
  134. *
  135. * Thus, we scale the bucket priorities so that the bucket with the smallest
  136. * prio is worth 1/8th of what INITIAL_PRIO is worth.
  137. */
  138. #define bucket_prio(b) \
  139. ({ \
  140. unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
  141. \
  142. (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \
  143. })
  144. #define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r))
  145. #define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r))
  146. static void invalidate_buckets_lru(struct cache *ca)
  147. {
  148. struct bucket *b;
  149. ssize_t i;
  150. ca->heap.used = 0;
  151. for_each_bucket(b, ca) {
  152. if (!bch_can_invalidate_bucket(ca, b))
  153. continue;
  154. if (!heap_full(&ca->heap))
  155. heap_add(&ca->heap, b, bucket_max_cmp);
  156. else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
  157. ca->heap.data[0] = b;
  158. heap_sift(&ca->heap, 0, bucket_max_cmp);
  159. }
  160. }
  161. for (i = ca->heap.used / 2 - 1; i >= 0; --i)
  162. heap_sift(&ca->heap, i, bucket_min_cmp);
  163. while (!fifo_full(&ca->free_inc)) {
  164. if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
  165. /*
  166. * We don't want to be calling invalidate_buckets()
  167. * multiple times when it can't do anything
  168. */
  169. ca->invalidate_needs_gc = 1;
  170. wake_up_gc(ca->set);
  171. return;
  172. }
  173. bch_invalidate_one_bucket(ca, b);
  174. }
  175. }
  176. static void invalidate_buckets_fifo(struct cache *ca)
  177. {
  178. struct bucket *b;
  179. size_t checked = 0;
  180. while (!fifo_full(&ca->free_inc)) {
  181. if (ca->fifo_last_bucket < ca->sb.first_bucket ||
  182. ca->fifo_last_bucket >= ca->sb.nbuckets)
  183. ca->fifo_last_bucket = ca->sb.first_bucket;
  184. b = ca->buckets + ca->fifo_last_bucket++;
  185. if (bch_can_invalidate_bucket(ca, b))
  186. bch_invalidate_one_bucket(ca, b);
  187. if (++checked >= ca->sb.nbuckets) {
  188. ca->invalidate_needs_gc = 1;
  189. wake_up_gc(ca->set);
  190. return;
  191. }
  192. }
  193. }
  194. static void invalidate_buckets_random(struct cache *ca)
  195. {
  196. struct bucket *b;
  197. size_t checked = 0;
  198. while (!fifo_full(&ca->free_inc)) {
  199. size_t n;
  200. get_random_bytes(&n, sizeof(n));
  201. n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
  202. n += ca->sb.first_bucket;
  203. b = ca->buckets + n;
  204. if (bch_can_invalidate_bucket(ca, b))
  205. bch_invalidate_one_bucket(ca, b);
  206. if (++checked >= ca->sb.nbuckets / 2) {
  207. ca->invalidate_needs_gc = 1;
  208. wake_up_gc(ca->set);
  209. return;
  210. }
  211. }
  212. }
  213. static void invalidate_buckets(struct cache *ca)
  214. {
  215. BUG_ON(ca->invalidate_needs_gc);
  216. switch (CACHE_REPLACEMENT(&ca->sb)) {
  217. case CACHE_REPLACEMENT_LRU:
  218. invalidate_buckets_lru(ca);
  219. break;
  220. case CACHE_REPLACEMENT_FIFO:
  221. invalidate_buckets_fifo(ca);
  222. break;
  223. case CACHE_REPLACEMENT_RANDOM:
  224. invalidate_buckets_random(ca);
  225. break;
  226. }
  227. }
  228. #define allocator_wait(ca, cond) \
  229. do { \
  230. while (1) { \
  231. set_current_state(TASK_INTERRUPTIBLE); \
  232. if (cond) \
  233. break; \
  234. \
  235. mutex_unlock(&(ca)->set->bucket_lock); \
  236. if (kthread_should_stop() || \
  237. test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags)) { \
  238. set_current_state(TASK_RUNNING); \
  239. goto out; \
  240. } \
  241. \
  242. schedule(); \
  243. mutex_lock(&(ca)->set->bucket_lock); \
  244. } \
  245. __set_current_state(TASK_RUNNING); \
  246. } while (0)
  247. static int bch_allocator_push(struct cache *ca, long bucket)
  248. {
  249. unsigned int i;
  250. /* Prios/gens are actually the most important reserve */
  251. if (fifo_push(&ca->free[RESERVE_PRIO], bucket))
  252. return true;
  253. for (i = 0; i < RESERVE_NR; i++)
  254. if (fifo_push(&ca->free[i], bucket))
  255. return true;
  256. return false;
  257. }
  258. static int bch_allocator_thread(void *arg)
  259. {
  260. struct cache *ca = arg;
  261. mutex_lock(&ca->set->bucket_lock);
  262. while (1) {
  263. /*
  264. * First, we pull buckets off of the unused and free_inc lists,
  265. * then we add the bucket to the free list:
  266. */
  267. while (1) {
  268. long bucket;
  269. if (!fifo_pop(&ca->free_inc, bucket))
  270. break;
  271. allocator_wait(ca, bch_allocator_push(ca, bucket));
  272. wake_up(&ca->set->btree_cache_wait);
  273. wake_up(&ca->set->bucket_wait);
  274. }
  275. /*
  276. * We've run out of free buckets, we need to find some buckets
  277. * we can invalidate. First, invalidate them in memory and add
  278. * them to the free_inc list:
  279. */
  280. retry_invalidate:
  281. allocator_wait(ca, !ca->invalidate_needs_gc);
  282. invalidate_buckets(ca);
  283. /*
  284. * Now, we write their new gens to disk so we can start writing
  285. * new stuff to them:
  286. */
  287. allocator_wait(ca, !atomic_read(&ca->set->prio_blocked));
  288. if (CACHE_SYNC(&ca->sb)) {
  289. /*
  290. * This could deadlock if an allocation with a btree
  291. * node locked ever blocked - having the btree node
  292. * locked would block garbage collection, but here we're
  293. * waiting on garbage collection before we invalidate
  294. * and free anything.
  295. *
  296. * But this should be safe since the btree code always
  297. * uses btree_check_reserve() before allocating now, and
  298. * if it fails it blocks without btree nodes locked.
  299. */
  300. if (!fifo_full(&ca->free_inc))
  301. goto retry_invalidate;
  302. if (bch_prio_write(ca, false) < 0) {
  303. ca->invalidate_needs_gc = 1;
  304. wake_up_gc(ca->set);
  305. }
  306. }
  307. }
  308. out:
  309. wait_for_kthread_stop();
  310. return 0;
  311. }
  312. /* Allocation */
  313. long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait)
  314. {
  315. DEFINE_WAIT(w);
  316. struct bucket *b;
  317. long r;
  318. /* No allocation if CACHE_SET_IO_DISABLE bit is set */
  319. if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags)))
  320. return -1;
  321. /* fastpath */
  322. if (fifo_pop(&ca->free[RESERVE_NONE], r) ||
  323. fifo_pop(&ca->free[reserve], r))
  324. goto out;
  325. if (!wait) {
  326. trace_bcache_alloc_fail(ca, reserve);
  327. return -1;
  328. }
  329. do {
  330. prepare_to_wait(&ca->set->bucket_wait, &w,
  331. TASK_UNINTERRUPTIBLE);
  332. mutex_unlock(&ca->set->bucket_lock);
  333. atomic_inc(&ca->set->bucket_wait_cnt);
  334. schedule();
  335. atomic_dec(&ca->set->bucket_wait_cnt);
  336. mutex_lock(&ca->set->bucket_lock);
  337. } while (!fifo_pop(&ca->free[RESERVE_NONE], r) &&
  338. !fifo_pop(&ca->free[reserve], r));
  339. finish_wait(&ca->set->bucket_wait, &w);
  340. out:
  341. if (ca->alloc_thread)
  342. wake_up_process(ca->alloc_thread);
  343. trace_bcache_alloc(ca, reserve);
  344. if (expensive_debug_checks(ca->set)) {
  345. size_t iter;
  346. long i;
  347. unsigned int j;
  348. for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
  349. BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
  350. for (j = 0; j < RESERVE_NR; j++)
  351. fifo_for_each(i, &ca->free[j], iter)
  352. BUG_ON(i == r);
  353. fifo_for_each(i, &ca->free_inc, iter)
  354. BUG_ON(i == r);
  355. }
  356. b = ca->buckets + r;
  357. BUG_ON(atomic_read(&b->pin) != 1);
  358. SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
  359. if (reserve <= RESERVE_PRIO) {
  360. SET_GC_MARK(b, GC_MARK_METADATA);
  361. SET_GC_MOVE(b, 0);
  362. b->prio = BTREE_PRIO;
  363. } else {
  364. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  365. SET_GC_MOVE(b, 0);
  366. b->prio = INITIAL_PRIO;
  367. }
  368. if (ca->set->avail_nbuckets > 0) {
  369. ca->set->avail_nbuckets--;
  370. bch_update_bucket_in_use(ca->set, &ca->set->gc_stats);
  371. }
  372. return r;
  373. }
  374. void __bch_bucket_free(struct cache *ca, struct bucket *b)
  375. {
  376. SET_GC_MARK(b, 0);
  377. SET_GC_SECTORS_USED(b, 0);
  378. if (ca->set->avail_nbuckets < ca->set->nbuckets) {
  379. ca->set->avail_nbuckets++;
  380. bch_update_bucket_in_use(ca->set, &ca->set->gc_stats);
  381. }
  382. }
  383. void bch_bucket_free(struct cache_set *c, struct bkey *k)
  384. {
  385. unsigned int i;
  386. for (i = 0; i < KEY_PTRS(k); i++)
  387. __bch_bucket_free(c->cache, PTR_BUCKET(c, k, i));
  388. }
  389. int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
  390. struct bkey *k, bool wait)
  391. {
  392. struct cache *ca;
  393. long b;
  394. /* No allocation if CACHE_SET_IO_DISABLE bit is set */
  395. if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
  396. return -1;
  397. lockdep_assert_held(&c->bucket_lock);
  398. bkey_init(k);
  399. ca = c->cache;
  400. b = bch_bucket_alloc(ca, reserve, wait);
  401. if (b < 0)
  402. return -1;
  403. k->ptr[0] = MAKE_PTR(ca->buckets[b].gen,
  404. bucket_to_sector(c, b),
  405. ca->sb.nr_this_dev);
  406. SET_KEY_PTRS(k, 1);
  407. return 0;
  408. }
  409. int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
  410. struct bkey *k, bool wait)
  411. {
  412. int ret;
  413. mutex_lock(&c->bucket_lock);
  414. ret = __bch_bucket_alloc_set(c, reserve, k, wait);
  415. mutex_unlock(&c->bucket_lock);
  416. return ret;
  417. }
  418. /* Sector allocator */
  419. struct open_bucket {
  420. struct list_head list;
  421. unsigned int last_write_point;
  422. unsigned int sectors_free;
  423. BKEY_PADDED(key);
  424. };
  425. /*
  426. * We keep multiple buckets open for writes, and try to segregate different
  427. * write streams for better cache utilization: first we try to segregate flash
  428. * only volume write streams from cached devices, secondly we look for a bucket
  429. * where the last write to it was sequential with the current write, and
  430. * failing that we look for a bucket that was last used by the same task.
  431. *
  432. * The ideas is if you've got multiple tasks pulling data into the cache at the
  433. * same time, you'll get better cache utilization if you try to segregate their
  434. * data and preserve locality.
  435. *
  436. * For example, dirty sectors of flash only volume is not reclaimable, if their
  437. * dirty sectors mixed with dirty sectors of cached device, such buckets will
  438. * be marked as dirty and won't be reclaimed, though the dirty data of cached
  439. * device have been written back to backend device.
  440. *
  441. * And say you've starting Firefox at the same time you're copying a
  442. * bunch of files. Firefox will likely end up being fairly hot and stay in the
  443. * cache awhile, but the data you copied might not be; if you wrote all that
  444. * data to the same buckets it'd get invalidated at the same time.
  445. *
  446. * Both of those tasks will be doing fairly random IO so we can't rely on
  447. * detecting sequential IO to segregate their data, but going off of the task
  448. * should be a sane heuristic.
  449. */
  450. static struct open_bucket *pick_data_bucket(struct cache_set *c,
  451. const struct bkey *search,
  452. unsigned int write_point,
  453. struct bkey *alloc)
  454. {
  455. struct open_bucket *ret, *ret_task = NULL;
  456. list_for_each_entry_reverse(ret, &c->data_buckets, list)
  457. if (UUID_FLASH_ONLY(&c->uuids[KEY_INODE(&ret->key)]) !=
  458. UUID_FLASH_ONLY(&c->uuids[KEY_INODE(search)]))
  459. continue;
  460. else if (!bkey_cmp(&ret->key, search))
  461. goto found;
  462. else if (ret->last_write_point == write_point)
  463. ret_task = ret;
  464. ret = ret_task ?: list_first_entry(&c->data_buckets,
  465. struct open_bucket, list);
  466. found:
  467. if (!ret->sectors_free && KEY_PTRS(alloc)) {
  468. ret->sectors_free = c->cache->sb.bucket_size;
  469. bkey_copy(&ret->key, alloc);
  470. bkey_init(alloc);
  471. }
  472. if (!ret->sectors_free)
  473. ret = NULL;
  474. return ret;
  475. }
  476. /*
  477. * Allocates some space in the cache to write to, and k to point to the newly
  478. * allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the
  479. * end of the newly allocated space).
  480. *
  481. * May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many
  482. * sectors were actually allocated.
  483. *
  484. * If s->writeback is true, will not fail.
  485. */
  486. bool bch_alloc_sectors(struct cache_set *c,
  487. struct bkey *k,
  488. unsigned int sectors,
  489. unsigned int write_point,
  490. unsigned int write_prio,
  491. bool wait)
  492. {
  493. struct open_bucket *b;
  494. BKEY_PADDED(key) alloc;
  495. unsigned int i;
  496. /*
  497. * We might have to allocate a new bucket, which we can't do with a
  498. * spinlock held. So if we have to allocate, we drop the lock, allocate
  499. * and then retry. KEY_PTRS() indicates whether alloc points to
  500. * allocated bucket(s).
  501. */
  502. bkey_init(&alloc.key);
  503. spin_lock(&c->data_bucket_lock);
  504. while (!(b = pick_data_bucket(c, k, write_point, &alloc.key))) {
  505. unsigned int watermark = write_prio
  506. ? RESERVE_MOVINGGC
  507. : RESERVE_NONE;
  508. spin_unlock(&c->data_bucket_lock);
  509. if (bch_bucket_alloc_set(c, watermark, &alloc.key, wait))
  510. return false;
  511. spin_lock(&c->data_bucket_lock);
  512. }
  513. /*
  514. * If we had to allocate, we might race and not need to allocate the
  515. * second time we call pick_data_bucket(). If we allocated a bucket but
  516. * didn't use it, drop the refcount bch_bucket_alloc_set() took:
  517. */
  518. if (KEY_PTRS(&alloc.key))
  519. bkey_put(c, &alloc.key);
  520. for (i = 0; i < KEY_PTRS(&b->key); i++)
  521. EBUG_ON(ptr_stale(c, &b->key, i));
  522. /* Set up the pointer to the space we're allocating: */
  523. for (i = 0; i < KEY_PTRS(&b->key); i++)
  524. k->ptr[i] = b->key.ptr[i];
  525. sectors = min(sectors, b->sectors_free);
  526. SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors);
  527. SET_KEY_SIZE(k, sectors);
  528. SET_KEY_PTRS(k, KEY_PTRS(&b->key));
  529. /*
  530. * Move b to the end of the lru, and keep track of what this bucket was
  531. * last used for:
  532. */
  533. list_move_tail(&b->list, &c->data_buckets);
  534. bkey_copy_key(&b->key, k);
  535. b->last_write_point = write_point;
  536. b->sectors_free -= sectors;
  537. for (i = 0; i < KEY_PTRS(&b->key); i++) {
  538. SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors);
  539. atomic_long_add(sectors,
  540. &c->cache->sectors_written);
  541. }
  542. if (b->sectors_free < c->cache->sb.block_size)
  543. b->sectors_free = 0;
  544. /*
  545. * k takes refcounts on the buckets it points to until it's inserted
  546. * into the btree, but if we're done with this bucket we just transfer
  547. * get_data_bucket()'s refcount.
  548. */
  549. if (b->sectors_free)
  550. for (i = 0; i < KEY_PTRS(&b->key); i++)
  551. atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin);
  552. spin_unlock(&c->data_bucket_lock);
  553. return true;
  554. }
  555. /* Init */
  556. void bch_open_buckets_free(struct cache_set *c)
  557. {
  558. struct open_bucket *b;
  559. while (!list_empty(&c->data_buckets)) {
  560. b = list_first_entry(&c->data_buckets,
  561. struct open_bucket, list);
  562. list_del(&b->list);
  563. kfree(b);
  564. }
  565. }
  566. int bch_open_buckets_alloc(struct cache_set *c)
  567. {
  568. int i;
  569. spin_lock_init(&c->data_bucket_lock);
  570. for (i = 0; i < MAX_OPEN_BUCKETS; i++) {
  571. struct open_bucket *b = kzalloc_obj(*b);
  572. if (!b)
  573. return -ENOMEM;
  574. list_add(&b->list, &c->data_buckets);
  575. }
  576. return 0;
  577. }
  578. int bch_cache_allocator_start(struct cache *ca)
  579. {
  580. struct task_struct *k = kthread_run(bch_allocator_thread,
  581. ca, "bcache_allocator");
  582. if (IS_ERR(k))
  583. return PTR_ERR(k);
  584. ca->alloc_thread = k;
  585. return 0;
  586. }