request.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. * Main bcache entry point - handle a read or a write request and decide what to
  3. * do with it; the make_request functions are called by the block layer.
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #include "bcache.h"
  9. #include "btree.h"
  10. #include "debug.h"
  11. #include "request.h"
  12. #include "writeback.h"
  13. #include <linux/cgroup.h>
  14. #include <linux/module.h>
  15. #include <linux/hash.h>
  16. #include <linux/random.h>
  17. #include "blk-cgroup.h"
  18. #include <trace/events/bcache.h>
  19. #define CUTOFF_CACHE_ADD 95
  20. #define CUTOFF_CACHE_READA 90
  21. struct kmem_cache *bch_search_cache;
  22. static void bch_data_insert_start(struct closure *);
  23. /* Cgroup interface */
  24. #ifdef CONFIG_CGROUP_BCACHE
  25. static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 };
  26. static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup)
  27. {
  28. struct cgroup_subsys_state *css;
  29. return cgroup &&
  30. (css = cgroup_subsys_state(cgroup, bcache_subsys_id))
  31. ? container_of(css, struct bch_cgroup, css)
  32. : &bcache_default_cgroup;
  33. }
  34. struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio)
  35. {
  36. struct cgroup_subsys_state *css = bio->bi_css
  37. ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id)
  38. : task_subsys_state(current, bcache_subsys_id);
  39. return css
  40. ? container_of(css, struct bch_cgroup, css)
  41. : &bcache_default_cgroup;
  42. }
  43. static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft,
  44. struct file *file,
  45. char __user *buf, size_t nbytes, loff_t *ppos)
  46. {
  47. char tmp[1024];
  48. int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes,
  49. cgroup_to_bcache(cgrp)->cache_mode + 1);
  50. if (len < 0)
  51. return len;
  52. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  53. }
  54. static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft,
  55. const char *buf)
  56. {
  57. int v = bch_read_string_list(buf, bch_cache_modes);
  58. if (v < 0)
  59. return v;
  60. cgroup_to_bcache(cgrp)->cache_mode = v - 1;
  61. return 0;
  62. }
  63. static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft)
  64. {
  65. return cgroup_to_bcache(cgrp)->verify;
  66. }
  67. static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val)
  68. {
  69. cgroup_to_bcache(cgrp)->verify = val;
  70. return 0;
  71. }
  72. static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft)
  73. {
  74. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  75. return atomic_read(&bcachecg->stats.cache_hits);
  76. }
  77. static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft)
  78. {
  79. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  80. return atomic_read(&bcachecg->stats.cache_misses);
  81. }
  82. static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp,
  83. struct cftype *cft)
  84. {
  85. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  86. return atomic_read(&bcachecg->stats.cache_bypass_hits);
  87. }
  88. static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp,
  89. struct cftype *cft)
  90. {
  91. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  92. return atomic_read(&bcachecg->stats.cache_bypass_misses);
  93. }
  94. static struct cftype bch_files[] = {
  95. {
  96. .name = "cache_mode",
  97. .read = cache_mode_read,
  98. .write_string = cache_mode_write,
  99. },
  100. {
  101. .name = "verify",
  102. .read_u64 = bch_verify_read,
  103. .write_u64 = bch_verify_write,
  104. },
  105. {
  106. .name = "cache_hits",
  107. .read_u64 = bch_cache_hits_read,
  108. },
  109. {
  110. .name = "cache_misses",
  111. .read_u64 = bch_cache_misses_read,
  112. },
  113. {
  114. .name = "cache_bypass_hits",
  115. .read_u64 = bch_cache_bypass_hits_read,
  116. },
  117. {
  118. .name = "cache_bypass_misses",
  119. .read_u64 = bch_cache_bypass_misses_read,
  120. },
  121. { } /* terminate */
  122. };
  123. static void init_bch_cgroup(struct bch_cgroup *cg)
  124. {
  125. cg->cache_mode = -1;
  126. }
  127. static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup)
  128. {
  129. struct bch_cgroup *cg;
  130. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  131. if (!cg)
  132. return ERR_PTR(-ENOMEM);
  133. init_bch_cgroup(cg);
  134. return &cg->css;
  135. }
  136. static void bcachecg_destroy(struct cgroup *cgroup)
  137. {
  138. struct bch_cgroup *cg = cgroup_to_bcache(cgroup);
  139. kfree(cg);
  140. }
  141. struct cgroup_subsys bcache_subsys = {
  142. .create = bcachecg_create,
  143. .destroy = bcachecg_destroy,
  144. .subsys_id = bcache_subsys_id,
  145. .name = "bcache",
  146. .module = THIS_MODULE,
  147. };
  148. EXPORT_SYMBOL_GPL(bcache_subsys);
  149. #endif
  150. static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
  151. {
  152. #ifdef CONFIG_CGROUP_BCACHE
  153. int r = bch_bio_to_cgroup(bio)->cache_mode;
  154. if (r >= 0)
  155. return r;
  156. #endif
  157. return BDEV_CACHE_MODE(&dc->sb);
  158. }
  159. static bool verify(struct cached_dev *dc, struct bio *bio)
  160. {
  161. #ifdef CONFIG_CGROUP_BCACHE
  162. if (bch_bio_to_cgroup(bio)->verify)
  163. return true;
  164. #endif
  165. return dc->verify;
  166. }
  167. static void bio_csum(struct bio *bio, struct bkey *k)
  168. {
  169. struct bio_vec bv;
  170. struct bvec_iter iter;
  171. uint64_t csum = 0;
  172. bio_for_each_segment(bv, bio, iter) {
  173. void *d = kmap(bv.bv_page) + bv.bv_offset;
  174. csum = bch_crc64_update(csum, d, bv.bv_len);
  175. kunmap(bv.bv_page);
  176. }
  177. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  178. }
  179. /* Insert data into cache */
  180. static void bch_data_insert_keys(struct closure *cl)
  181. {
  182. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  183. atomic_t *journal_ref = NULL;
  184. struct bkey *replace_key = op->replace ? &op->replace_key : NULL;
  185. int ret;
  186. /*
  187. * If we're looping, might already be waiting on
  188. * another journal write - can't wait on more than one journal write at
  189. * a time
  190. *
  191. * XXX: this looks wrong
  192. */
  193. #if 0
  194. while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING)
  195. closure_sync(&s->cl);
  196. #endif
  197. if (!op->replace)
  198. journal_ref = bch_journal(op->c, &op->insert_keys,
  199. op->flush_journal ? cl : NULL);
  200. ret = bch_btree_insert(op->c, &op->insert_keys,
  201. journal_ref, replace_key);
  202. if (ret == -ESRCH) {
  203. op->replace_collision = true;
  204. } else if (ret) {
  205. op->error = -ENOMEM;
  206. op->insert_data_done = true;
  207. }
  208. if (journal_ref)
  209. atomic_dec_bug(journal_ref);
  210. if (!op->insert_data_done)
  211. continue_at(cl, bch_data_insert_start, bcache_wq);
  212. bch_keylist_free(&op->insert_keys);
  213. closure_return(cl);
  214. }
  215. static int bch_keylist_realloc(struct keylist *l, unsigned u64s,
  216. struct cache_set *c)
  217. {
  218. size_t oldsize = bch_keylist_nkeys(l);
  219. size_t newsize = oldsize + u64s;
  220. /*
  221. * The journalling code doesn't handle the case where the keys to insert
  222. * is bigger than an empty write: If we just return -ENOMEM here,
  223. * bio_insert() and bio_invalidate() will insert the keys created so far
  224. * and finish the rest when the keylist is empty.
  225. */
  226. if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
  227. return -ENOMEM;
  228. return __bch_keylist_realloc(l, u64s);
  229. }
  230. static void bch_data_invalidate(struct closure *cl)
  231. {
  232. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  233. struct bio *bio = op->bio;
  234. pr_debug("invalidating %i sectors from %llu",
  235. bio_sectors(bio), (uint64_t) bio->bi_iter.bi_sector);
  236. while (bio_sectors(bio)) {
  237. unsigned sectors = min(bio_sectors(bio),
  238. 1U << (KEY_SIZE_BITS - 1));
  239. if (bch_keylist_realloc(&op->insert_keys, 2, op->c))
  240. goto out;
  241. bio->bi_iter.bi_sector += sectors;
  242. bio->bi_iter.bi_size -= sectors << 9;
  243. bch_keylist_add(&op->insert_keys,
  244. &KEY(op->inode, bio->bi_iter.bi_sector, sectors));
  245. }
  246. op->insert_data_done = true;
  247. bio_put(bio);
  248. out:
  249. continue_at(cl, bch_data_insert_keys, bcache_wq);
  250. }
  251. static void bch_data_insert_error(struct closure *cl)
  252. {
  253. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  254. /*
  255. * Our data write just errored, which means we've got a bunch of keys to
  256. * insert that point to data that wasn't succesfully written.
  257. *
  258. * We don't have to insert those keys but we still have to invalidate
  259. * that region of the cache - so, if we just strip off all the pointers
  260. * from the keys we'll accomplish just that.
  261. */
  262. struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys;
  263. while (src != op->insert_keys.top) {
  264. struct bkey *n = bkey_next(src);
  265. SET_KEY_PTRS(src, 0);
  266. memmove(dst, src, bkey_bytes(src));
  267. dst = bkey_next(dst);
  268. src = n;
  269. }
  270. op->insert_keys.top = dst;
  271. bch_data_insert_keys(cl);
  272. }
  273. static void bch_data_insert_endio(struct bio *bio, int error)
  274. {
  275. struct closure *cl = bio->bi_private;
  276. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  277. if (error) {
  278. /* TODO: We could try to recover from this. */
  279. if (op->writeback)
  280. op->error = error;
  281. else if (!op->replace)
  282. set_closure_fn(cl, bch_data_insert_error, bcache_wq);
  283. else
  284. set_closure_fn(cl, NULL, NULL);
  285. }
  286. bch_bbio_endio(op->c, bio, error, "writing data to cache");
  287. }
  288. static void bch_data_insert_start(struct closure *cl)
  289. {
  290. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  291. struct bio *bio = op->bio, *n;
  292. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) {
  293. set_gc_sectors(op->c);
  294. wake_up_gc(op->c);
  295. }
  296. if (op->bypass)
  297. return bch_data_invalidate(cl);
  298. /*
  299. * Journal writes are marked REQ_FLUSH; if the original write was a
  300. * flush, it'll wait on the journal write.
  301. */
  302. bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA);
  303. do {
  304. unsigned i;
  305. struct bkey *k;
  306. struct bio_set *split = op->c->bio_split;
  307. /* 1 for the device pointer and 1 for the chksum */
  308. if (bch_keylist_realloc(&op->insert_keys,
  309. 3 + (op->csum ? 1 : 0),
  310. op->c))
  311. continue_at(cl, bch_data_insert_keys, bcache_wq);
  312. k = op->insert_keys.top;
  313. bkey_init(k);
  314. SET_KEY_INODE(k, op->inode);
  315. SET_KEY_OFFSET(k, bio->bi_iter.bi_sector);
  316. if (!bch_alloc_sectors(op->c, k, bio_sectors(bio),
  317. op->write_point, op->write_prio,
  318. op->writeback))
  319. goto err;
  320. n = bio_next_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  321. n->bi_end_io = bch_data_insert_endio;
  322. n->bi_private = cl;
  323. if (op->writeback) {
  324. SET_KEY_DIRTY(k, true);
  325. for (i = 0; i < KEY_PTRS(k); i++)
  326. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  327. GC_MARK_DIRTY);
  328. }
  329. SET_KEY_CSUM(k, op->csum);
  330. if (KEY_CSUM(k))
  331. bio_csum(n, k);
  332. trace_bcache_cache_insert(k);
  333. bch_keylist_push(&op->insert_keys);
  334. n->bi_rw |= REQ_WRITE;
  335. bch_submit_bbio(n, op->c, k, 0);
  336. } while (n != bio);
  337. op->insert_data_done = true;
  338. continue_at(cl, bch_data_insert_keys, bcache_wq);
  339. err:
  340. /* bch_alloc_sectors() blocks if s->writeback = true */
  341. BUG_ON(op->writeback);
  342. /*
  343. * But if it's not a writeback write we'd rather just bail out if
  344. * there aren't any buckets ready to write to - it might take awhile and
  345. * we might be starving btree writes for gc or something.
  346. */
  347. if (!op->replace) {
  348. /*
  349. * Writethrough write: We can't complete the write until we've
  350. * updated the index. But we don't want to delay the write while
  351. * we wait for buckets to be freed up, so just invalidate the
  352. * rest of the write.
  353. */
  354. op->bypass = true;
  355. return bch_data_invalidate(cl);
  356. } else {
  357. /*
  358. * From a cache miss, we can just insert the keys for the data
  359. * we have written or bail out if we didn't do anything.
  360. */
  361. op->insert_data_done = true;
  362. bio_put(bio);
  363. if (!bch_keylist_empty(&op->insert_keys))
  364. continue_at(cl, bch_data_insert_keys, bcache_wq);
  365. else
  366. closure_return(cl);
  367. }
  368. }
  369. /**
  370. * bch_data_insert - stick some data in the cache
  371. *
  372. * This is the starting point for any data to end up in a cache device; it could
  373. * be from a normal write, or a writeback write, or a write to a flash only
  374. * volume - it's also used by the moving garbage collector to compact data in
  375. * mostly empty buckets.
  376. *
  377. * It first writes the data to the cache, creating a list of keys to be inserted
  378. * (if the data had to be fragmented there will be multiple keys); after the
  379. * data is written it calls bch_journal, and after the keys have been added to
  380. * the next journal write they're inserted into the btree.
  381. *
  382. * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
  383. * and op->inode is used for the key inode.
  384. *
  385. * If s->bypass is true, instead of inserting the data it invalidates the
  386. * region of the cache represented by s->cache_bio and op->inode.
  387. */
  388. void bch_data_insert(struct closure *cl)
  389. {
  390. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  391. trace_bcache_write(op->bio, op->writeback, op->bypass);
  392. bch_keylist_init(&op->insert_keys);
  393. bio_get(op->bio);
  394. bch_data_insert_start(cl);
  395. }
  396. /* Congested? */
  397. unsigned bch_get_congested(struct cache_set *c)
  398. {
  399. int i;
  400. long rand;
  401. if (!c->congested_read_threshold_us &&
  402. !c->congested_write_threshold_us)
  403. return 0;
  404. i = (local_clock_us() - c->congested_last_us) / 1024;
  405. if (i < 0)
  406. return 0;
  407. i += atomic_read(&c->congested);
  408. if (i >= 0)
  409. return 0;
  410. i += CONGESTED_MAX;
  411. if (i > 0)
  412. i = fract_exp_two(i, 6);
  413. rand = get_random_int();
  414. i -= bitmap_weight(&rand, BITS_PER_LONG);
  415. return i > 0 ? i : 1;
  416. }
  417. static void add_sequential(struct task_struct *t)
  418. {
  419. ewma_add(t->sequential_io_avg,
  420. t->sequential_io, 8, 0);
  421. t->sequential_io = 0;
  422. }
  423. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  424. {
  425. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  426. }
  427. static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
  428. {
  429. struct cache_set *c = dc->disk.c;
  430. unsigned mode = cache_mode(dc, bio);
  431. unsigned sectors, congested = bch_get_congested(c);
  432. struct task_struct *task = current;
  433. struct io *i;
  434. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  435. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  436. (bio->bi_rw & REQ_DISCARD))
  437. goto skip;
  438. if (mode == CACHE_MODE_NONE ||
  439. (mode == CACHE_MODE_WRITEAROUND &&
  440. (bio->bi_rw & REQ_WRITE)))
  441. goto skip;
  442. if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
  443. bio_sectors(bio) & (c->sb.block_size - 1)) {
  444. pr_debug("skipping unaligned io");
  445. goto skip;
  446. }
  447. if (bypass_torture_test(dc)) {
  448. if ((get_random_int() & 3) == 3)
  449. goto skip;
  450. else
  451. goto rescale;
  452. }
  453. if (!congested && !dc->sequential_cutoff)
  454. goto rescale;
  455. if (!congested &&
  456. mode == CACHE_MODE_WRITEBACK &&
  457. (bio->bi_rw & REQ_WRITE) &&
  458. (bio->bi_rw & REQ_SYNC))
  459. goto rescale;
  460. spin_lock(&dc->io_lock);
  461. hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash)
  462. if (i->last == bio->bi_iter.bi_sector &&
  463. time_before(jiffies, i->jiffies))
  464. goto found;
  465. i = list_first_entry(&dc->io_lru, struct io, lru);
  466. add_sequential(task);
  467. i->sequential = 0;
  468. found:
  469. if (i->sequential + bio->bi_iter.bi_size > i->sequential)
  470. i->sequential += bio->bi_iter.bi_size;
  471. i->last = bio_end_sector(bio);
  472. i->jiffies = jiffies + msecs_to_jiffies(5000);
  473. task->sequential_io = i->sequential;
  474. hlist_del(&i->hash);
  475. hlist_add_head(&i->hash, iohash(dc, i->last));
  476. list_move_tail(&i->lru, &dc->io_lru);
  477. spin_unlock(&dc->io_lock);
  478. sectors = max(task->sequential_io,
  479. task->sequential_io_avg) >> 9;
  480. if (dc->sequential_cutoff &&
  481. sectors >= dc->sequential_cutoff >> 9) {
  482. trace_bcache_bypass_sequential(bio);
  483. goto skip;
  484. }
  485. if (congested && sectors >= congested) {
  486. trace_bcache_bypass_congested(bio);
  487. goto skip;
  488. }
  489. rescale:
  490. bch_rescale_priorities(c, bio_sectors(bio));
  491. return false;
  492. skip:
  493. bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
  494. return true;
  495. }
  496. /* Cache lookup */
  497. struct search {
  498. /* Stack frame for bio_complete */
  499. struct closure cl;
  500. struct bbio bio;
  501. struct bio *orig_bio;
  502. struct bio *cache_miss;
  503. struct bcache_device *d;
  504. unsigned insert_bio_sectors;
  505. unsigned recoverable:1;
  506. unsigned write:1;
  507. unsigned read_dirty_data:1;
  508. unsigned long start_time;
  509. struct btree_op op;
  510. struct data_insert_op iop;
  511. };
  512. static void bch_cache_read_endio(struct bio *bio, int error)
  513. {
  514. struct bbio *b = container_of(bio, struct bbio, bio);
  515. struct closure *cl = bio->bi_private;
  516. struct search *s = container_of(cl, struct search, cl);
  517. /*
  518. * If the bucket was reused while our bio was in flight, we might have
  519. * read the wrong data. Set s->error but not error so it doesn't get
  520. * counted against the cache device, but we'll still reread the data
  521. * from the backing device.
  522. */
  523. if (error)
  524. s->iop.error = error;
  525. else if (!KEY_DIRTY(&b->key) &&
  526. ptr_stale(s->iop.c, &b->key, 0)) {
  527. atomic_long_inc(&s->iop.c->cache_read_races);
  528. s->iop.error = -EINTR;
  529. }
  530. bch_bbio_endio(s->iop.c, bio, error, "reading from cache");
  531. }
  532. /*
  533. * Read from a single key, handling the initial cache miss if the key starts in
  534. * the middle of the bio
  535. */
  536. static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
  537. {
  538. struct search *s = container_of(op, struct search, op);
  539. struct bio *n, *bio = &s->bio.bio;
  540. struct bkey *bio_key;
  541. unsigned ptr;
  542. if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
  543. return MAP_CONTINUE;
  544. if (KEY_INODE(k) != s->iop.inode ||
  545. KEY_START(k) > bio->bi_iter.bi_sector) {
  546. unsigned bio_sectors = bio_sectors(bio);
  547. unsigned sectors = KEY_INODE(k) == s->iop.inode
  548. ? min_t(uint64_t, INT_MAX,
  549. KEY_START(k) - bio->bi_iter.bi_sector)
  550. : INT_MAX;
  551. int ret = s->d->cache_miss(b, s, bio, sectors);
  552. if (ret != MAP_CONTINUE)
  553. return ret;
  554. /* if this was a complete miss we shouldn't get here */
  555. BUG_ON(bio_sectors <= sectors);
  556. }
  557. if (!KEY_SIZE(k))
  558. return MAP_CONTINUE;
  559. /* XXX: figure out best pointer - for multiple cache devices */
  560. ptr = 0;
  561. PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
  562. if (KEY_DIRTY(k))
  563. s->read_dirty_data = true;
  564. n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
  565. KEY_OFFSET(k) - bio->bi_iter.bi_sector),
  566. GFP_NOIO, s->d->bio_split);
  567. bio_key = &container_of(n, struct bbio, bio)->key;
  568. bch_bkey_copy_single_ptr(bio_key, k, ptr);
  569. bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
  570. bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
  571. n->bi_end_io = bch_cache_read_endio;
  572. n->bi_private = &s->cl;
  573. /*
  574. * The bucket we're reading from might be reused while our bio
  575. * is in flight, and we could then end up reading the wrong
  576. * data.
  577. *
  578. * We guard against this by checking (in cache_read_endio()) if
  579. * the pointer is stale again; if so, we treat it as an error
  580. * and reread from the backing device (but we don't pass that
  581. * error up anywhere).
  582. */
  583. __bch_submit_bbio(n, b->c);
  584. return n == bio ? MAP_DONE : MAP_CONTINUE;
  585. }
  586. static void cache_lookup(struct closure *cl)
  587. {
  588. struct search *s = container_of(cl, struct search, iop.cl);
  589. struct bio *bio = &s->bio.bio;
  590. int ret;
  591. bch_btree_op_init(&s->op, -1);
  592. ret = bch_btree_map_keys(&s->op, s->iop.c,
  593. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  594. cache_lookup_fn, MAP_END_KEY);
  595. if (ret == -EAGAIN)
  596. continue_at(cl, cache_lookup, bcache_wq);
  597. closure_return(cl);
  598. }
  599. /* Common code for the make_request functions */
  600. static void request_endio(struct bio *bio, int error)
  601. {
  602. struct closure *cl = bio->bi_private;
  603. if (error) {
  604. struct search *s = container_of(cl, struct search, cl);
  605. s->iop.error = error;
  606. /* Only cache read errors are recoverable */
  607. s->recoverable = false;
  608. }
  609. bio_put(bio);
  610. closure_put(cl);
  611. }
  612. static void bio_complete(struct search *s)
  613. {
  614. if (s->orig_bio) {
  615. int cpu, rw = bio_data_dir(s->orig_bio);
  616. unsigned long duration = jiffies - s->start_time;
  617. cpu = part_stat_lock();
  618. part_round_stats(cpu, &s->d->disk->part0);
  619. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  620. part_stat_unlock();
  621. trace_bcache_request_end(s->d, s->orig_bio);
  622. bio_endio(s->orig_bio, s->iop.error);
  623. s->orig_bio = NULL;
  624. }
  625. }
  626. static void do_bio_hook(struct search *s, struct bio *orig_bio)
  627. {
  628. struct bio *bio = &s->bio.bio;
  629. bio_init(bio);
  630. __bio_clone_fast(bio, orig_bio);
  631. bio->bi_end_io = request_endio;
  632. bio->bi_private = &s->cl;
  633. atomic_set(&bio->bi_cnt, 3);
  634. }
  635. static void search_free(struct closure *cl)
  636. {
  637. struct search *s = container_of(cl, struct search, cl);
  638. bio_complete(s);
  639. if (s->iop.bio)
  640. bio_put(s->iop.bio);
  641. closure_debug_destroy(cl);
  642. mempool_free(s, s->d->c->search);
  643. }
  644. static inline struct search *search_alloc(struct bio *bio,
  645. struct bcache_device *d)
  646. {
  647. struct search *s;
  648. s = mempool_alloc(d->c->search, GFP_NOIO);
  649. closure_init(&s->cl, NULL);
  650. do_bio_hook(s, bio);
  651. s->orig_bio = bio;
  652. s->cache_miss = NULL;
  653. s->d = d;
  654. s->recoverable = 1;
  655. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  656. s->read_dirty_data = 0;
  657. s->start_time = jiffies;
  658. s->iop.c = d->c;
  659. s->iop.bio = NULL;
  660. s->iop.inode = d->id;
  661. s->iop.write_point = hash_long((unsigned long) current, 16);
  662. s->iop.write_prio = 0;
  663. s->iop.error = 0;
  664. s->iop.flags = 0;
  665. s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0;
  666. return s;
  667. }
  668. /* Cached devices */
  669. static void cached_dev_bio_complete(struct closure *cl)
  670. {
  671. struct search *s = container_of(cl, struct search, cl);
  672. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  673. search_free(cl);
  674. cached_dev_put(dc);
  675. }
  676. /* Process reads */
  677. static void cached_dev_cache_miss_done(struct closure *cl)
  678. {
  679. struct search *s = container_of(cl, struct search, cl);
  680. if (s->iop.replace_collision)
  681. bch_mark_cache_miss_collision(s->iop.c, s->d);
  682. if (s->iop.bio) {
  683. int i;
  684. struct bio_vec *bv;
  685. bio_for_each_segment_all(bv, s->iop.bio, i)
  686. __free_page(bv->bv_page);
  687. }
  688. cached_dev_bio_complete(cl);
  689. }
  690. static void cached_dev_read_error(struct closure *cl)
  691. {
  692. struct search *s = container_of(cl, struct search, cl);
  693. struct bio *bio = &s->bio.bio;
  694. if (s->recoverable) {
  695. /* Retry from the backing device: */
  696. trace_bcache_read_retry(s->orig_bio);
  697. s->iop.error = 0;
  698. do_bio_hook(s, s->orig_bio);
  699. /* XXX: invalidate cache */
  700. closure_bio_submit(bio, cl, s->d);
  701. }
  702. continue_at(cl, cached_dev_cache_miss_done, NULL);
  703. }
  704. static void cached_dev_read_done(struct closure *cl)
  705. {
  706. struct search *s = container_of(cl, struct search, cl);
  707. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  708. /*
  709. * We had a cache miss; cache_bio now contains data ready to be inserted
  710. * into the cache.
  711. *
  712. * First, we copy the data we just read from cache_bio's bounce buffers
  713. * to the buffers the original bio pointed to:
  714. */
  715. if (s->iop.bio) {
  716. bio_reset(s->iop.bio);
  717. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  718. s->iop.bio->bi_bdev = s->cache_miss->bi_bdev;
  719. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  720. bch_bio_map(s->iop.bio, NULL);
  721. bio_copy_data(s->cache_miss, s->iop.bio);
  722. bio_put(s->cache_miss);
  723. s->cache_miss = NULL;
  724. }
  725. if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data)
  726. bch_data_verify(dc, s->orig_bio);
  727. bio_complete(s);
  728. if (s->iop.bio &&
  729. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  730. BUG_ON(!s->iop.replace);
  731. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  732. }
  733. continue_at(cl, cached_dev_cache_miss_done, NULL);
  734. }
  735. static void cached_dev_read_done_bh(struct closure *cl)
  736. {
  737. struct search *s = container_of(cl, struct search, cl);
  738. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  739. bch_mark_cache_accounting(s->iop.c, s->d,
  740. !s->cache_miss, s->iop.bypass);
  741. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  742. if (s->iop.error)
  743. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  744. else if (s->iop.bio || verify(dc, &s->bio.bio))
  745. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  746. else
  747. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  748. }
  749. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  750. struct bio *bio, unsigned sectors)
  751. {
  752. int ret = MAP_CONTINUE;
  753. unsigned reada = 0;
  754. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  755. struct bio *miss, *cache_bio;
  756. if (s->cache_miss || s->iop.bypass) {
  757. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  758. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  759. goto out_submit;
  760. }
  761. if (!(bio->bi_rw & REQ_RAHEAD) &&
  762. !(bio->bi_rw & REQ_META) &&
  763. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  764. reada = min_t(sector_t, dc->readahead >> 9,
  765. bdev_sectors(bio->bi_bdev) - bio_end_sector(bio));
  766. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  767. s->iop.replace_key = KEY(s->iop.inode,
  768. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  769. s->insert_bio_sectors);
  770. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  771. if (ret)
  772. return ret;
  773. s->iop.replace = true;
  774. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  775. /* btree_search_recurse()'s btree iterator is no good anymore */
  776. ret = miss == bio ? MAP_DONE : -EINTR;
  777. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  778. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  779. dc->disk.bio_split);
  780. if (!cache_bio)
  781. goto out_submit;
  782. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  783. cache_bio->bi_bdev = miss->bi_bdev;
  784. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  785. cache_bio->bi_end_io = request_endio;
  786. cache_bio->bi_private = &s->cl;
  787. bch_bio_map(cache_bio, NULL);
  788. if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  789. goto out_put;
  790. if (reada)
  791. bch_mark_cache_readahead(s->iop.c, s->d);
  792. s->cache_miss = miss;
  793. s->iop.bio = cache_bio;
  794. bio_get(cache_bio);
  795. closure_bio_submit(cache_bio, &s->cl, s->d);
  796. return ret;
  797. out_put:
  798. bio_put(cache_bio);
  799. out_submit:
  800. miss->bi_end_io = request_endio;
  801. miss->bi_private = &s->cl;
  802. closure_bio_submit(miss, &s->cl, s->d);
  803. return ret;
  804. }
  805. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  806. {
  807. struct closure *cl = &s->cl;
  808. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  809. continue_at(cl, cached_dev_read_done_bh, NULL);
  810. }
  811. /* Process writes */
  812. static void cached_dev_write_complete(struct closure *cl)
  813. {
  814. struct search *s = container_of(cl, struct search, cl);
  815. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  816. up_read_non_owner(&dc->writeback_lock);
  817. cached_dev_bio_complete(cl);
  818. }
  819. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  820. {
  821. struct closure *cl = &s->cl;
  822. struct bio *bio = &s->bio.bio;
  823. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  824. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  825. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  826. down_read_non_owner(&dc->writeback_lock);
  827. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  828. /*
  829. * We overlap with some dirty data undergoing background
  830. * writeback, force this write to writeback
  831. */
  832. s->iop.bypass = false;
  833. s->iop.writeback = true;
  834. }
  835. /*
  836. * Discards aren't _required_ to do anything, so skipping if
  837. * check_overlapping returned true is ok
  838. *
  839. * But check_overlapping drops dirty keys for which io hasn't started,
  840. * so we still want to call it.
  841. */
  842. if (bio->bi_rw & REQ_DISCARD)
  843. s->iop.bypass = true;
  844. if (should_writeback(dc, s->orig_bio,
  845. cache_mode(dc, bio),
  846. s->iop.bypass)) {
  847. s->iop.bypass = false;
  848. s->iop.writeback = true;
  849. }
  850. if (s->iop.bypass) {
  851. s->iop.bio = s->orig_bio;
  852. bio_get(s->iop.bio);
  853. if (!(bio->bi_rw & REQ_DISCARD) ||
  854. blk_queue_discard(bdev_get_queue(dc->bdev)))
  855. closure_bio_submit(bio, cl, s->d);
  856. } else if (s->iop.writeback) {
  857. bch_writeback_add(dc);
  858. s->iop.bio = bio;
  859. if (bio->bi_rw & REQ_FLUSH) {
  860. /* Also need to send a flush to the backing device */
  861. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  862. dc->disk.bio_split);
  863. flush->bi_rw = WRITE_FLUSH;
  864. flush->bi_bdev = bio->bi_bdev;
  865. flush->bi_end_io = request_endio;
  866. flush->bi_private = cl;
  867. closure_bio_submit(flush, cl, s->d);
  868. }
  869. } else {
  870. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  871. closure_bio_submit(bio, cl, s->d);
  872. }
  873. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  874. continue_at(cl, cached_dev_write_complete, NULL);
  875. }
  876. static void cached_dev_nodata(struct closure *cl)
  877. {
  878. struct search *s = container_of(cl, struct search, cl);
  879. struct bio *bio = &s->bio.bio;
  880. if (s->iop.flush_journal)
  881. bch_journal_meta(s->iop.c, cl);
  882. /* If it's a flush, we send the flush to the backing device too */
  883. closure_bio_submit(bio, cl, s->d);
  884. continue_at(cl, cached_dev_bio_complete, NULL);
  885. }
  886. /* Cached devices - read & write stuff */
  887. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  888. {
  889. struct search *s;
  890. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  891. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  892. int cpu, rw = bio_data_dir(bio);
  893. cpu = part_stat_lock();
  894. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  895. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  896. part_stat_unlock();
  897. bio->bi_bdev = dc->bdev;
  898. bio->bi_iter.bi_sector += dc->sb.data_offset;
  899. if (cached_dev_get(dc)) {
  900. s = search_alloc(bio, d);
  901. trace_bcache_request_start(s->d, bio);
  902. if (!bio->bi_iter.bi_size) {
  903. /*
  904. * can't call bch_journal_meta from under
  905. * generic_make_request
  906. */
  907. continue_at_nobarrier(&s->cl,
  908. cached_dev_nodata,
  909. bcache_wq);
  910. } else {
  911. s->iop.bypass = check_should_bypass(dc, bio);
  912. if (rw)
  913. cached_dev_write(dc, s);
  914. else
  915. cached_dev_read(dc, s);
  916. }
  917. } else {
  918. if ((bio->bi_rw & REQ_DISCARD) &&
  919. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  920. bio_endio(bio, 0);
  921. else
  922. bch_generic_make_request(bio, &d->bio_split_hook);
  923. }
  924. }
  925. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  926. unsigned int cmd, unsigned long arg)
  927. {
  928. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  929. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  930. }
  931. static int cached_dev_congested(void *data, int bits)
  932. {
  933. struct bcache_device *d = data;
  934. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  935. struct request_queue *q = bdev_get_queue(dc->bdev);
  936. int ret = 0;
  937. if (bdi_congested(&q->backing_dev_info, bits))
  938. return 1;
  939. if (cached_dev_get(dc)) {
  940. unsigned i;
  941. struct cache *ca;
  942. for_each_cache(ca, d->c, i) {
  943. q = bdev_get_queue(ca->bdev);
  944. ret |= bdi_congested(&q->backing_dev_info, bits);
  945. }
  946. cached_dev_put(dc);
  947. }
  948. return ret;
  949. }
  950. void bch_cached_dev_request_init(struct cached_dev *dc)
  951. {
  952. struct gendisk *g = dc->disk.disk;
  953. g->queue->make_request_fn = cached_dev_make_request;
  954. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  955. dc->disk.cache_miss = cached_dev_cache_miss;
  956. dc->disk.ioctl = cached_dev_ioctl;
  957. }
  958. /* Flash backed devices */
  959. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  960. struct bio *bio, unsigned sectors)
  961. {
  962. struct bio_vec bv;
  963. struct bvec_iter iter;
  964. /* Zero fill bio */
  965. bio_for_each_segment(bv, bio, iter) {
  966. unsigned j = min(bv.bv_len >> 9, sectors);
  967. void *p = kmap(bv.bv_page);
  968. memset(p + bv.bv_offset, 0, j << 9);
  969. kunmap(bv.bv_page);
  970. sectors -= j;
  971. }
  972. bio_advance(bio, min(sectors << 9, bio->bi_iter.bi_size));
  973. if (!bio->bi_iter.bi_size)
  974. return MAP_DONE;
  975. return MAP_CONTINUE;
  976. }
  977. static void flash_dev_nodata(struct closure *cl)
  978. {
  979. struct search *s = container_of(cl, struct search, cl);
  980. if (s->iop.flush_journal)
  981. bch_journal_meta(s->iop.c, cl);
  982. continue_at(cl, search_free, NULL);
  983. }
  984. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  985. {
  986. struct search *s;
  987. struct closure *cl;
  988. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  989. int cpu, rw = bio_data_dir(bio);
  990. cpu = part_stat_lock();
  991. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  992. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  993. part_stat_unlock();
  994. s = search_alloc(bio, d);
  995. cl = &s->cl;
  996. bio = &s->bio.bio;
  997. trace_bcache_request_start(s->d, bio);
  998. if (!bio->bi_iter.bi_size) {
  999. /*
  1000. * can't call bch_journal_meta from under
  1001. * generic_make_request
  1002. */
  1003. continue_at_nobarrier(&s->cl,
  1004. flash_dev_nodata,
  1005. bcache_wq);
  1006. } else if (rw) {
  1007. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  1008. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  1009. &KEY(d->id, bio_end_sector(bio), 0));
  1010. s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0;
  1011. s->iop.writeback = true;
  1012. s->iop.bio = bio;
  1013. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  1014. } else {
  1015. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  1016. }
  1017. continue_at(cl, search_free, NULL);
  1018. }
  1019. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1020. unsigned int cmd, unsigned long arg)
  1021. {
  1022. return -ENOTTY;
  1023. }
  1024. static int flash_dev_congested(void *data, int bits)
  1025. {
  1026. struct bcache_device *d = data;
  1027. struct request_queue *q;
  1028. struct cache *ca;
  1029. unsigned i;
  1030. int ret = 0;
  1031. for_each_cache(ca, d->c, i) {
  1032. q = bdev_get_queue(ca->bdev);
  1033. ret |= bdi_congested(&q->backing_dev_info, bits);
  1034. }
  1035. return ret;
  1036. }
  1037. void bch_flash_dev_request_init(struct bcache_device *d)
  1038. {
  1039. struct gendisk *g = d->disk;
  1040. g->queue->make_request_fn = flash_dev_make_request;
  1041. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1042. d->cache_miss = flash_dev_cache_miss;
  1043. d->ioctl = flash_dev_ioctl;
  1044. }
  1045. void bch_request_exit(void)
  1046. {
  1047. #ifdef CONFIG_CGROUP_BCACHE
  1048. cgroup_unload_subsys(&bcache_subsys);
  1049. #endif
  1050. if (bch_search_cache)
  1051. kmem_cache_destroy(bch_search_cache);
  1052. }
  1053. int __init bch_request_init(void)
  1054. {
  1055. bch_search_cache = KMEM_CACHE(search, 0);
  1056. if (!bch_search_cache)
  1057. return -ENOMEM;
  1058. #ifdef CONFIG_CGROUP_BCACHE
  1059. cgroup_load_subsys(&bcache_subsys);
  1060. init_bch_cgroup(&bcache_default_cgroup);
  1061. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1062. #endif
  1063. return 0;
  1064. }