request.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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, op->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, op->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, op->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, op->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, op->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, op->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. s->iop.wq = bcache_wq;
  667. return s;
  668. }
  669. /* Cached devices */
  670. static void cached_dev_bio_complete(struct closure *cl)
  671. {
  672. struct search *s = container_of(cl, struct search, cl);
  673. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  674. search_free(cl);
  675. cached_dev_put(dc);
  676. }
  677. /* Process reads */
  678. static void cached_dev_cache_miss_done(struct closure *cl)
  679. {
  680. struct search *s = container_of(cl, struct search, cl);
  681. if (s->iop.replace_collision)
  682. bch_mark_cache_miss_collision(s->iop.c, s->d);
  683. if (s->iop.bio) {
  684. int i;
  685. struct bio_vec *bv;
  686. bio_for_each_segment_all(bv, s->iop.bio, i)
  687. __free_page(bv->bv_page);
  688. }
  689. cached_dev_bio_complete(cl);
  690. }
  691. static void cached_dev_read_error(struct closure *cl)
  692. {
  693. struct search *s = container_of(cl, struct search, cl);
  694. struct bio *bio = &s->bio.bio;
  695. if (s->recoverable) {
  696. /* Retry from the backing device: */
  697. trace_bcache_read_retry(s->orig_bio);
  698. s->iop.error = 0;
  699. do_bio_hook(s, s->orig_bio);
  700. /* XXX: invalidate cache */
  701. closure_bio_submit(bio, cl, s->d);
  702. }
  703. continue_at(cl, cached_dev_cache_miss_done, NULL);
  704. }
  705. static void cached_dev_read_done(struct closure *cl)
  706. {
  707. struct search *s = container_of(cl, struct search, cl);
  708. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  709. /*
  710. * We had a cache miss; cache_bio now contains data ready to be inserted
  711. * into the cache.
  712. *
  713. * First, we copy the data we just read from cache_bio's bounce buffers
  714. * to the buffers the original bio pointed to:
  715. */
  716. if (s->iop.bio) {
  717. bio_reset(s->iop.bio);
  718. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  719. s->iop.bio->bi_bdev = s->cache_miss->bi_bdev;
  720. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  721. bch_bio_map(s->iop.bio, NULL);
  722. bio_copy_data(s->cache_miss, s->iop.bio);
  723. bio_put(s->cache_miss);
  724. s->cache_miss = NULL;
  725. }
  726. if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data)
  727. bch_data_verify(dc, s->orig_bio);
  728. bio_complete(s);
  729. if (s->iop.bio &&
  730. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  731. BUG_ON(!s->iop.replace);
  732. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  733. }
  734. continue_at(cl, cached_dev_cache_miss_done, NULL);
  735. }
  736. static void cached_dev_read_done_bh(struct closure *cl)
  737. {
  738. struct search *s = container_of(cl, struct search, cl);
  739. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  740. bch_mark_cache_accounting(s->iop.c, s->d,
  741. !s->cache_miss, s->iop.bypass);
  742. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  743. if (s->iop.error)
  744. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  745. else if (s->iop.bio || verify(dc, &s->bio.bio))
  746. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  747. else
  748. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  749. }
  750. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  751. struct bio *bio, unsigned sectors)
  752. {
  753. int ret = MAP_CONTINUE;
  754. unsigned reada = 0;
  755. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  756. struct bio *miss, *cache_bio;
  757. if (s->cache_miss || s->iop.bypass) {
  758. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  759. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  760. goto out_submit;
  761. }
  762. if (!(bio->bi_rw & REQ_RAHEAD) &&
  763. !(bio->bi_rw & REQ_META) &&
  764. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  765. reada = min_t(sector_t, dc->readahead >> 9,
  766. bdev_sectors(bio->bi_bdev) - bio_end_sector(bio));
  767. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  768. s->iop.replace_key = KEY(s->iop.inode,
  769. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  770. s->insert_bio_sectors);
  771. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  772. if (ret)
  773. return ret;
  774. s->iop.replace = true;
  775. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  776. /* btree_search_recurse()'s btree iterator is no good anymore */
  777. ret = miss == bio ? MAP_DONE : -EINTR;
  778. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  779. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  780. dc->disk.bio_split);
  781. if (!cache_bio)
  782. goto out_submit;
  783. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  784. cache_bio->bi_bdev = miss->bi_bdev;
  785. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  786. cache_bio->bi_end_io = request_endio;
  787. cache_bio->bi_private = &s->cl;
  788. bch_bio_map(cache_bio, NULL);
  789. if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  790. goto out_put;
  791. if (reada)
  792. bch_mark_cache_readahead(s->iop.c, s->d);
  793. s->cache_miss = miss;
  794. s->iop.bio = cache_bio;
  795. bio_get(cache_bio);
  796. closure_bio_submit(cache_bio, &s->cl, s->d);
  797. return ret;
  798. out_put:
  799. bio_put(cache_bio);
  800. out_submit:
  801. miss->bi_end_io = request_endio;
  802. miss->bi_private = &s->cl;
  803. closure_bio_submit(miss, &s->cl, s->d);
  804. return ret;
  805. }
  806. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  807. {
  808. struct closure *cl = &s->cl;
  809. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  810. continue_at(cl, cached_dev_read_done_bh, NULL);
  811. }
  812. /* Process writes */
  813. static void cached_dev_write_complete(struct closure *cl)
  814. {
  815. struct search *s = container_of(cl, struct search, cl);
  816. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  817. up_read_non_owner(&dc->writeback_lock);
  818. cached_dev_bio_complete(cl);
  819. }
  820. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  821. {
  822. struct closure *cl = &s->cl;
  823. struct bio *bio = &s->bio.bio;
  824. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  825. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  826. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  827. down_read_non_owner(&dc->writeback_lock);
  828. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  829. /*
  830. * We overlap with some dirty data undergoing background
  831. * writeback, force this write to writeback
  832. */
  833. s->iop.bypass = false;
  834. s->iop.writeback = true;
  835. }
  836. /*
  837. * Discards aren't _required_ to do anything, so skipping if
  838. * check_overlapping returned true is ok
  839. *
  840. * But check_overlapping drops dirty keys for which io hasn't started,
  841. * so we still want to call it.
  842. */
  843. if (bio->bi_rw & REQ_DISCARD)
  844. s->iop.bypass = true;
  845. if (should_writeback(dc, s->orig_bio,
  846. cache_mode(dc, bio),
  847. s->iop.bypass)) {
  848. s->iop.bypass = false;
  849. s->iop.writeback = true;
  850. }
  851. if (s->iop.bypass) {
  852. s->iop.bio = s->orig_bio;
  853. bio_get(s->iop.bio);
  854. if (!(bio->bi_rw & REQ_DISCARD) ||
  855. blk_queue_discard(bdev_get_queue(dc->bdev)))
  856. closure_bio_submit(bio, cl, s->d);
  857. } else if (s->iop.writeback) {
  858. bch_writeback_add(dc);
  859. s->iop.bio = bio;
  860. if (bio->bi_rw & REQ_FLUSH) {
  861. /* Also need to send a flush to the backing device */
  862. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  863. dc->disk.bio_split);
  864. flush->bi_rw = WRITE_FLUSH;
  865. flush->bi_bdev = bio->bi_bdev;
  866. flush->bi_end_io = request_endio;
  867. flush->bi_private = cl;
  868. closure_bio_submit(flush, cl, s->d);
  869. }
  870. } else {
  871. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  872. closure_bio_submit(bio, cl, s->d);
  873. }
  874. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  875. continue_at(cl, cached_dev_write_complete, NULL);
  876. }
  877. static void cached_dev_nodata(struct closure *cl)
  878. {
  879. struct search *s = container_of(cl, struct search, cl);
  880. struct bio *bio = &s->bio.bio;
  881. if (s->iop.flush_journal)
  882. bch_journal_meta(s->iop.c, cl);
  883. /* If it's a flush, we send the flush to the backing device too */
  884. closure_bio_submit(bio, cl, s->d);
  885. continue_at(cl, cached_dev_bio_complete, NULL);
  886. }
  887. /* Cached devices - read & write stuff */
  888. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  889. {
  890. struct search *s;
  891. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  892. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  893. int cpu, rw = bio_data_dir(bio);
  894. cpu = part_stat_lock();
  895. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  896. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  897. part_stat_unlock();
  898. bio->bi_bdev = dc->bdev;
  899. bio->bi_iter.bi_sector += dc->sb.data_offset;
  900. if (cached_dev_get(dc)) {
  901. s = search_alloc(bio, d);
  902. trace_bcache_request_start(s->d, bio);
  903. if (!bio->bi_iter.bi_size) {
  904. /*
  905. * can't call bch_journal_meta from under
  906. * generic_make_request
  907. */
  908. continue_at_nobarrier(&s->cl,
  909. cached_dev_nodata,
  910. bcache_wq);
  911. } else {
  912. s->iop.bypass = check_should_bypass(dc, bio);
  913. if (rw)
  914. cached_dev_write(dc, s);
  915. else
  916. cached_dev_read(dc, s);
  917. }
  918. } else {
  919. if ((bio->bi_rw & REQ_DISCARD) &&
  920. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  921. bio_endio(bio, 0);
  922. else
  923. bch_generic_make_request(bio, &d->bio_split_hook);
  924. }
  925. }
  926. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  927. unsigned int cmd, unsigned long arg)
  928. {
  929. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  930. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  931. }
  932. static int cached_dev_congested(void *data, int bits)
  933. {
  934. struct bcache_device *d = data;
  935. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  936. struct request_queue *q = bdev_get_queue(dc->bdev);
  937. int ret = 0;
  938. if (bdi_congested(&q->backing_dev_info, bits))
  939. return 1;
  940. if (cached_dev_get(dc)) {
  941. unsigned i;
  942. struct cache *ca;
  943. for_each_cache(ca, d->c, i) {
  944. q = bdev_get_queue(ca->bdev);
  945. ret |= bdi_congested(&q->backing_dev_info, bits);
  946. }
  947. cached_dev_put(dc);
  948. }
  949. return ret;
  950. }
  951. void bch_cached_dev_request_init(struct cached_dev *dc)
  952. {
  953. struct gendisk *g = dc->disk.disk;
  954. g->queue->make_request_fn = cached_dev_make_request;
  955. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  956. dc->disk.cache_miss = cached_dev_cache_miss;
  957. dc->disk.ioctl = cached_dev_ioctl;
  958. }
  959. /* Flash backed devices */
  960. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  961. struct bio *bio, unsigned sectors)
  962. {
  963. unsigned bytes = min(sectors, bio_sectors(bio)) << 9;
  964. swap(bio->bi_iter.bi_size, bytes);
  965. zero_fill_bio(bio);
  966. swap(bio->bi_iter.bi_size, bytes);
  967. bio_advance(bio, bytes);
  968. if (!bio->bi_iter.bi_size)
  969. return MAP_DONE;
  970. return MAP_CONTINUE;
  971. }
  972. static void flash_dev_nodata(struct closure *cl)
  973. {
  974. struct search *s = container_of(cl, struct search, cl);
  975. if (s->iop.flush_journal)
  976. bch_journal_meta(s->iop.c, cl);
  977. continue_at(cl, search_free, NULL);
  978. }
  979. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  980. {
  981. struct search *s;
  982. struct closure *cl;
  983. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  984. int cpu, rw = bio_data_dir(bio);
  985. cpu = part_stat_lock();
  986. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  987. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  988. part_stat_unlock();
  989. s = search_alloc(bio, d);
  990. cl = &s->cl;
  991. bio = &s->bio.bio;
  992. trace_bcache_request_start(s->d, bio);
  993. if (!bio->bi_iter.bi_size) {
  994. /*
  995. * can't call bch_journal_meta from under
  996. * generic_make_request
  997. */
  998. continue_at_nobarrier(&s->cl,
  999. flash_dev_nodata,
  1000. bcache_wq);
  1001. } else if (rw) {
  1002. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  1003. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  1004. &KEY(d->id, bio_end_sector(bio), 0));
  1005. s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0;
  1006. s->iop.writeback = true;
  1007. s->iop.bio = bio;
  1008. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  1009. } else {
  1010. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  1011. }
  1012. continue_at(cl, search_free, NULL);
  1013. }
  1014. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1015. unsigned int cmd, unsigned long arg)
  1016. {
  1017. return -ENOTTY;
  1018. }
  1019. static int flash_dev_congested(void *data, int bits)
  1020. {
  1021. struct bcache_device *d = data;
  1022. struct request_queue *q;
  1023. struct cache *ca;
  1024. unsigned i;
  1025. int ret = 0;
  1026. for_each_cache(ca, d->c, i) {
  1027. q = bdev_get_queue(ca->bdev);
  1028. ret |= bdi_congested(&q->backing_dev_info, bits);
  1029. }
  1030. return ret;
  1031. }
  1032. void bch_flash_dev_request_init(struct bcache_device *d)
  1033. {
  1034. struct gendisk *g = d->disk;
  1035. g->queue->make_request_fn = flash_dev_make_request;
  1036. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1037. d->cache_miss = flash_dev_cache_miss;
  1038. d->ioctl = flash_dev_ioctl;
  1039. }
  1040. void bch_request_exit(void)
  1041. {
  1042. #ifdef CONFIG_CGROUP_BCACHE
  1043. cgroup_unload_subsys(&bcache_subsys);
  1044. #endif
  1045. if (bch_search_cache)
  1046. kmem_cache_destroy(bch_search_cache);
  1047. }
  1048. int __init bch_request_init(void)
  1049. {
  1050. bch_search_cache = KMEM_CACHE(search, 0);
  1051. if (!bch_search_cache)
  1052. return -ENOMEM;
  1053. #ifdef CONFIG_CGROUP_BCACHE
  1054. cgroup_load_subsys(&bcache_subsys);
  1055. init_bch_cgroup(&bcache_default_cgroup);
  1056. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1057. #endif
  1058. return 0;
  1059. }