request.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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 check_should_skip(struct cached_dev *, struct search *);
  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. free_css_id(&bcache_subsys, &cg->css);
  140. kfree(cg);
  141. }
  142. struct cgroup_subsys bcache_subsys = {
  143. .create = bcachecg_create,
  144. .destroy = bcachecg_destroy,
  145. .subsys_id = bcache_subsys_id,
  146. .name = "bcache",
  147. .module = THIS_MODULE,
  148. };
  149. EXPORT_SYMBOL_GPL(bcache_subsys);
  150. #endif
  151. static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
  152. {
  153. #ifdef CONFIG_CGROUP_BCACHE
  154. int r = bch_bio_to_cgroup(bio)->cache_mode;
  155. if (r >= 0)
  156. return r;
  157. #endif
  158. return BDEV_CACHE_MODE(&dc->sb);
  159. }
  160. static bool verify(struct cached_dev *dc, struct bio *bio)
  161. {
  162. #ifdef CONFIG_CGROUP_BCACHE
  163. if (bch_bio_to_cgroup(bio)->verify)
  164. return true;
  165. #endif
  166. return dc->verify;
  167. }
  168. static void bio_csum(struct bio *bio, struct bkey *k)
  169. {
  170. struct bio_vec *bv;
  171. uint64_t csum = 0;
  172. int i;
  173. bio_for_each_segment(bv, bio, i) {
  174. void *d = kmap(bv->bv_page) + bv->bv_offset;
  175. csum = bch_crc64_update(csum, d, bv->bv_len);
  176. kunmap(bv->bv_page);
  177. }
  178. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  179. }
  180. /* Insert data into cache */
  181. static void bio_invalidate(struct closure *cl)
  182. {
  183. struct btree_op *op = container_of(cl, struct btree_op, cl);
  184. struct bio *bio = op->cache_bio;
  185. pr_debug("invalidating %i sectors from %llu",
  186. bio_sectors(bio), (uint64_t) bio->bi_sector);
  187. while (bio_sectors(bio)) {
  188. unsigned len = min(bio_sectors(bio), 1U << 14);
  189. if (bch_keylist_realloc(&op->keys, 0, op->c))
  190. goto out;
  191. bio->bi_sector += len;
  192. bio->bi_size -= len << 9;
  193. bch_keylist_add(&op->keys,
  194. &KEY(op->inode, bio->bi_sector, len));
  195. }
  196. op->insert_data_done = true;
  197. bio_put(bio);
  198. out:
  199. continue_at(cl, bch_journal, bcache_wq);
  200. }
  201. struct open_bucket {
  202. struct list_head list;
  203. struct task_struct *last;
  204. unsigned sectors_free;
  205. BKEY_PADDED(key);
  206. };
  207. void bch_open_buckets_free(struct cache_set *c)
  208. {
  209. struct open_bucket *b;
  210. while (!list_empty(&c->data_buckets)) {
  211. b = list_first_entry(&c->data_buckets,
  212. struct open_bucket, list);
  213. list_del(&b->list);
  214. kfree(b);
  215. }
  216. }
  217. int bch_open_buckets_alloc(struct cache_set *c)
  218. {
  219. int i;
  220. spin_lock_init(&c->data_bucket_lock);
  221. for (i = 0; i < 6; i++) {
  222. struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
  223. if (!b)
  224. return -ENOMEM;
  225. list_add(&b->list, &c->data_buckets);
  226. }
  227. return 0;
  228. }
  229. /*
  230. * We keep multiple buckets open for writes, and try to segregate different
  231. * write streams for better cache utilization: first we look for a bucket where
  232. * the last write to it was sequential with the current write, and failing that
  233. * we look for a bucket that was last used by the same task.
  234. *
  235. * The ideas is if you've got multiple tasks pulling data into the cache at the
  236. * same time, you'll get better cache utilization if you try to segregate their
  237. * data and preserve locality.
  238. *
  239. * For example, say you've starting Firefox at the same time you're copying a
  240. * bunch of files. Firefox will likely end up being fairly hot and stay in the
  241. * cache awhile, but the data you copied might not be; if you wrote all that
  242. * data to the same buckets it'd get invalidated at the same time.
  243. *
  244. * Both of those tasks will be doing fairly random IO so we can't rely on
  245. * detecting sequential IO to segregate their data, but going off of the task
  246. * should be a sane heuristic.
  247. */
  248. static struct open_bucket *pick_data_bucket(struct cache_set *c,
  249. const struct bkey *search,
  250. struct task_struct *task,
  251. struct bkey *alloc)
  252. {
  253. struct open_bucket *ret, *ret_task = NULL;
  254. list_for_each_entry_reverse(ret, &c->data_buckets, list)
  255. if (!bkey_cmp(&ret->key, search))
  256. goto found;
  257. else if (ret->last == task)
  258. ret_task = ret;
  259. ret = ret_task ?: list_first_entry(&c->data_buckets,
  260. struct open_bucket, list);
  261. found:
  262. if (!ret->sectors_free && KEY_PTRS(alloc)) {
  263. ret->sectors_free = c->sb.bucket_size;
  264. bkey_copy(&ret->key, alloc);
  265. bkey_init(alloc);
  266. }
  267. if (!ret->sectors_free)
  268. ret = NULL;
  269. return ret;
  270. }
  271. /*
  272. * Allocates some space in the cache to write to, and k to point to the newly
  273. * allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the
  274. * end of the newly allocated space).
  275. *
  276. * May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many
  277. * sectors were actually allocated.
  278. *
  279. * If s->writeback is true, will not fail.
  280. */
  281. static bool bch_alloc_sectors(struct bkey *k, unsigned sectors,
  282. struct search *s)
  283. {
  284. struct cache_set *c = s->op.c;
  285. struct open_bucket *b;
  286. BKEY_PADDED(key) alloc;
  287. struct closure cl, *w = NULL;
  288. unsigned i;
  289. if (s->writeback) {
  290. closure_init_stack(&cl);
  291. w = &cl;
  292. }
  293. /*
  294. * We might have to allocate a new bucket, which we can't do with a
  295. * spinlock held. So if we have to allocate, we drop the lock, allocate
  296. * and then retry. KEY_PTRS() indicates whether alloc points to
  297. * allocated bucket(s).
  298. */
  299. bkey_init(&alloc.key);
  300. spin_lock(&c->data_bucket_lock);
  301. while (!(b = pick_data_bucket(c, k, s->task, &alloc.key))) {
  302. unsigned watermark = s->op.write_prio
  303. ? WATERMARK_MOVINGGC
  304. : WATERMARK_NONE;
  305. spin_unlock(&c->data_bucket_lock);
  306. if (bch_bucket_alloc_set(c, watermark, &alloc.key, 1, w))
  307. return false;
  308. spin_lock(&c->data_bucket_lock);
  309. }
  310. /*
  311. * If we had to allocate, we might race and not need to allocate the
  312. * second time we call find_data_bucket(). If we allocated a bucket but
  313. * didn't use it, drop the refcount bch_bucket_alloc_set() took:
  314. */
  315. if (KEY_PTRS(&alloc.key))
  316. __bkey_put(c, &alloc.key);
  317. for (i = 0; i < KEY_PTRS(&b->key); i++)
  318. EBUG_ON(ptr_stale(c, &b->key, i));
  319. /* Set up the pointer to the space we're allocating: */
  320. for (i = 0; i < KEY_PTRS(&b->key); i++)
  321. k->ptr[i] = b->key.ptr[i];
  322. sectors = min(sectors, b->sectors_free);
  323. SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors);
  324. SET_KEY_SIZE(k, sectors);
  325. SET_KEY_PTRS(k, KEY_PTRS(&b->key));
  326. /*
  327. * Move b to the end of the lru, and keep track of what this bucket was
  328. * last used for:
  329. */
  330. list_move_tail(&b->list, &c->data_buckets);
  331. bkey_copy_key(&b->key, k);
  332. b->last = s->task;
  333. b->sectors_free -= sectors;
  334. for (i = 0; i < KEY_PTRS(&b->key); i++) {
  335. SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors);
  336. atomic_long_add(sectors,
  337. &PTR_CACHE(c, &b->key, i)->sectors_written);
  338. }
  339. if (b->sectors_free < c->sb.block_size)
  340. b->sectors_free = 0;
  341. /*
  342. * k takes refcounts on the buckets it points to until it's inserted
  343. * into the btree, but if we're done with this bucket we just transfer
  344. * get_data_bucket()'s refcount.
  345. */
  346. if (b->sectors_free)
  347. for (i = 0; i < KEY_PTRS(&b->key); i++)
  348. atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin);
  349. spin_unlock(&c->data_bucket_lock);
  350. return true;
  351. }
  352. static void bch_insert_data_error(struct closure *cl)
  353. {
  354. struct btree_op *op = container_of(cl, struct btree_op, cl);
  355. /*
  356. * Our data write just errored, which means we've got a bunch of keys to
  357. * insert that point to data that wasn't succesfully written.
  358. *
  359. * We don't have to insert those keys but we still have to invalidate
  360. * that region of the cache - so, if we just strip off all the pointers
  361. * from the keys we'll accomplish just that.
  362. */
  363. struct bkey *src = op->keys.bottom, *dst = op->keys.bottom;
  364. while (src != op->keys.top) {
  365. struct bkey *n = bkey_next(src);
  366. SET_KEY_PTRS(src, 0);
  367. bkey_copy(dst, src);
  368. dst = bkey_next(dst);
  369. src = n;
  370. }
  371. op->keys.top = dst;
  372. bch_journal(cl);
  373. }
  374. static void bch_insert_data_endio(struct bio *bio, int error)
  375. {
  376. struct closure *cl = bio->bi_private;
  377. struct btree_op *op = container_of(cl, struct btree_op, cl);
  378. struct search *s = container_of(op, struct search, op);
  379. if (error) {
  380. /* TODO: We could try to recover from this. */
  381. if (s->writeback)
  382. s->error = error;
  383. else if (s->write)
  384. set_closure_fn(cl, bch_insert_data_error, bcache_wq);
  385. else
  386. set_closure_fn(cl, NULL, NULL);
  387. }
  388. bch_bbio_endio(op->c, bio, error, "writing data to cache");
  389. }
  390. static void bch_insert_data_loop(struct closure *cl)
  391. {
  392. struct btree_op *op = container_of(cl, struct btree_op, cl);
  393. struct search *s = container_of(op, struct search, op);
  394. struct bio *bio = op->cache_bio, *n;
  395. if (op->skip)
  396. return bio_invalidate(cl);
  397. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) {
  398. set_gc_sectors(op->c);
  399. bch_queue_gc(op->c);
  400. }
  401. do {
  402. unsigned i;
  403. struct bkey *k;
  404. struct bio_set *split = s->d
  405. ? s->d->bio_split : op->c->bio_split;
  406. /* 1 for the device pointer and 1 for the chksum */
  407. if (bch_keylist_realloc(&op->keys,
  408. 1 + (op->csum ? 1 : 0),
  409. op->c))
  410. continue_at(cl, bch_journal, bcache_wq);
  411. k = op->keys.top;
  412. bkey_init(k);
  413. SET_KEY_INODE(k, op->inode);
  414. SET_KEY_OFFSET(k, bio->bi_sector);
  415. if (!bch_alloc_sectors(k, bio_sectors(bio), s))
  416. goto err;
  417. n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  418. n->bi_end_io = bch_insert_data_endio;
  419. n->bi_private = cl;
  420. if (s->writeback) {
  421. SET_KEY_DIRTY(k, true);
  422. for (i = 0; i < KEY_PTRS(k); i++)
  423. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  424. GC_MARK_DIRTY);
  425. }
  426. SET_KEY_CSUM(k, op->csum);
  427. if (KEY_CSUM(k))
  428. bio_csum(n, k);
  429. trace_bcache_cache_insert(k);
  430. bch_keylist_push(&op->keys);
  431. n->bi_rw |= REQ_WRITE;
  432. bch_submit_bbio(n, op->c, k, 0);
  433. } while (n != bio);
  434. op->insert_data_done = true;
  435. continue_at(cl, bch_journal, bcache_wq);
  436. err:
  437. /* bch_alloc_sectors() blocks if s->writeback = true */
  438. BUG_ON(s->writeback);
  439. /*
  440. * But if it's not a writeback write we'd rather just bail out if
  441. * there aren't any buckets ready to write to - it might take awhile and
  442. * we might be starving btree writes for gc or something.
  443. */
  444. if (s->write) {
  445. /*
  446. * Writethrough write: We can't complete the write until we've
  447. * updated the index. But we don't want to delay the write while
  448. * we wait for buckets to be freed up, so just invalidate the
  449. * rest of the write.
  450. */
  451. op->skip = true;
  452. return bio_invalidate(cl);
  453. } else {
  454. /*
  455. * From a cache miss, we can just insert the keys for the data
  456. * we have written or bail out if we didn't do anything.
  457. */
  458. op->insert_data_done = true;
  459. bio_put(bio);
  460. if (!bch_keylist_empty(&op->keys))
  461. continue_at(cl, bch_journal, bcache_wq);
  462. else
  463. closure_return(cl);
  464. }
  465. }
  466. /**
  467. * bch_insert_data - stick some data in the cache
  468. *
  469. * This is the starting point for any data to end up in a cache device; it could
  470. * be from a normal write, or a writeback write, or a write to a flash only
  471. * volume - it's also used by the moving garbage collector to compact data in
  472. * mostly empty buckets.
  473. *
  474. * It first writes the data to the cache, creating a list of keys to be inserted
  475. * (if the data had to be fragmented there will be multiple keys); after the
  476. * data is written it calls bch_journal, and after the keys have been added to
  477. * the next journal write they're inserted into the btree.
  478. *
  479. * It inserts the data in op->cache_bio; bi_sector is used for the key offset,
  480. * and op->inode is used for the key inode.
  481. *
  482. * If op->skip is true, instead of inserting the data it invalidates the region
  483. * of the cache represented by op->cache_bio and op->inode.
  484. */
  485. void bch_insert_data(struct closure *cl)
  486. {
  487. struct btree_op *op = container_of(cl, struct btree_op, cl);
  488. bch_keylist_init(&op->keys);
  489. bio_get(op->cache_bio);
  490. bch_insert_data_loop(cl);
  491. }
  492. void bch_btree_insert_async(struct closure *cl)
  493. {
  494. struct btree_op *op = container_of(cl, struct btree_op, cl);
  495. struct search *s = container_of(op, struct search, op);
  496. if (bch_btree_insert(op, op->c)) {
  497. s->error = -ENOMEM;
  498. op->insert_data_done = true;
  499. }
  500. if (op->insert_data_done) {
  501. bch_keylist_free(&op->keys);
  502. closure_return(cl);
  503. } else
  504. continue_at(cl, bch_insert_data_loop, bcache_wq);
  505. }
  506. /* Common code for the make_request functions */
  507. static void request_endio(struct bio *bio, int error)
  508. {
  509. struct closure *cl = bio->bi_private;
  510. if (error) {
  511. struct search *s = container_of(cl, struct search, cl);
  512. s->error = error;
  513. /* Only cache read errors are recoverable */
  514. s->recoverable = false;
  515. }
  516. bio_put(bio);
  517. closure_put(cl);
  518. }
  519. void bch_cache_read_endio(struct bio *bio, int error)
  520. {
  521. struct bbio *b = container_of(bio, struct bbio, bio);
  522. struct closure *cl = bio->bi_private;
  523. struct search *s = container_of(cl, struct search, cl);
  524. /*
  525. * If the bucket was reused while our bio was in flight, we might have
  526. * read the wrong data. Set s->error but not error so it doesn't get
  527. * counted against the cache device, but we'll still reread the data
  528. * from the backing device.
  529. */
  530. if (error)
  531. s->error = error;
  532. else if (ptr_stale(s->op.c, &b->key, 0)) {
  533. atomic_long_inc(&s->op.c->cache_read_races);
  534. s->error = -EINTR;
  535. }
  536. bch_bbio_endio(s->op.c, bio, error, "reading from cache");
  537. }
  538. static void bio_complete(struct search *s)
  539. {
  540. if (s->orig_bio) {
  541. int cpu, rw = bio_data_dir(s->orig_bio);
  542. unsigned long duration = jiffies - s->start_time;
  543. cpu = part_stat_lock();
  544. part_round_stats(cpu, &s->d->disk->part0);
  545. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  546. part_stat_unlock();
  547. trace_bcache_request_end(s, s->orig_bio);
  548. bio_endio(s->orig_bio, s->error);
  549. s->orig_bio = NULL;
  550. }
  551. }
  552. static void do_bio_hook(struct search *s)
  553. {
  554. struct bio *bio = &s->bio.bio;
  555. memcpy(bio, s->orig_bio, sizeof(struct bio));
  556. bio->bi_end_io = request_endio;
  557. bio->bi_private = &s->cl;
  558. atomic_set(&bio->bi_cnt, 3);
  559. }
  560. static void search_free(struct closure *cl)
  561. {
  562. struct search *s = container_of(cl, struct search, cl);
  563. bio_complete(s);
  564. if (s->op.cache_bio)
  565. bio_put(s->op.cache_bio);
  566. if (s->unaligned_bvec)
  567. mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
  568. closure_debug_destroy(cl);
  569. mempool_free(s, s->d->c->search);
  570. }
  571. static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
  572. {
  573. struct bio_vec *bv;
  574. struct search *s = mempool_alloc(d->c->search, GFP_NOIO);
  575. memset(s, 0, offsetof(struct search, op.keys));
  576. __closure_init(&s->cl, NULL);
  577. s->op.inode = d->id;
  578. s->op.c = d->c;
  579. s->d = d;
  580. s->op.lock = -1;
  581. s->task = current;
  582. s->orig_bio = bio;
  583. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  584. s->op.flush_journal = (bio->bi_rw & REQ_FLUSH) != 0;
  585. s->op.skip = (bio->bi_rw & REQ_DISCARD) != 0;
  586. s->recoverable = 1;
  587. s->start_time = jiffies;
  588. do_bio_hook(s);
  589. if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
  590. bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
  591. memcpy(bv, bio_iovec(bio),
  592. sizeof(struct bio_vec) * bio_segments(bio));
  593. s->bio.bio.bi_io_vec = bv;
  594. s->unaligned_bvec = 1;
  595. }
  596. return s;
  597. }
  598. static void btree_read_async(struct closure *cl)
  599. {
  600. struct btree_op *op = container_of(cl, struct btree_op, cl);
  601. int ret = btree_root(search_recurse, op->c, op);
  602. if (ret == -EAGAIN)
  603. continue_at(cl, btree_read_async, bcache_wq);
  604. closure_return(cl);
  605. }
  606. /* Cached devices */
  607. static void cached_dev_bio_complete(struct closure *cl)
  608. {
  609. struct search *s = container_of(cl, struct search, cl);
  610. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  611. search_free(cl);
  612. cached_dev_put(dc);
  613. }
  614. /* Process reads */
  615. static void cached_dev_read_complete(struct closure *cl)
  616. {
  617. struct search *s = container_of(cl, struct search, cl);
  618. if (s->op.insert_collision)
  619. bch_mark_cache_miss_collision(s);
  620. if (s->op.cache_bio) {
  621. int i;
  622. struct bio_vec *bv;
  623. __bio_for_each_segment(bv, s->op.cache_bio, i, 0)
  624. __free_page(bv->bv_page);
  625. }
  626. cached_dev_bio_complete(cl);
  627. }
  628. static void request_read_error(struct closure *cl)
  629. {
  630. struct search *s = container_of(cl, struct search, cl);
  631. struct bio_vec *bv;
  632. int i;
  633. if (s->recoverable) {
  634. /* Retry from the backing device: */
  635. trace_bcache_read_retry(s->orig_bio);
  636. s->error = 0;
  637. bv = s->bio.bio.bi_io_vec;
  638. do_bio_hook(s);
  639. s->bio.bio.bi_io_vec = bv;
  640. if (!s->unaligned_bvec)
  641. bio_for_each_segment(bv, s->orig_bio, i)
  642. bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
  643. else
  644. memcpy(s->bio.bio.bi_io_vec,
  645. bio_iovec(s->orig_bio),
  646. sizeof(struct bio_vec) *
  647. bio_segments(s->orig_bio));
  648. /* XXX: invalidate cache */
  649. closure_bio_submit(&s->bio.bio, &s->cl, s->d);
  650. }
  651. continue_at(cl, cached_dev_read_complete, NULL);
  652. }
  653. static void request_read_done(struct closure *cl)
  654. {
  655. struct search *s = container_of(cl, struct search, cl);
  656. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  657. /*
  658. * s->cache_bio != NULL implies that we had a cache miss; cache_bio now
  659. * contains data ready to be inserted into the cache.
  660. *
  661. * First, we copy the data we just read from cache_bio's bounce buffers
  662. * to the buffers the original bio pointed to:
  663. */
  664. if (s->op.cache_bio) {
  665. bio_reset(s->op.cache_bio);
  666. s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
  667. s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
  668. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  669. bch_bio_map(s->op.cache_bio, NULL);
  670. bio_copy_data(s->cache_miss, s->op.cache_bio);
  671. bio_put(s->cache_miss);
  672. s->cache_miss = NULL;
  673. }
  674. if (verify(dc, &s->bio.bio) && s->recoverable)
  675. bch_data_verify(s);
  676. bio_complete(s);
  677. if (s->op.cache_bio &&
  678. !test_bit(CACHE_SET_STOPPING, &s->op.c->flags)) {
  679. s->op.type = BTREE_REPLACE;
  680. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  681. }
  682. continue_at(cl, cached_dev_read_complete, NULL);
  683. }
  684. static void request_read_done_bh(struct closure *cl)
  685. {
  686. struct search *s = container_of(cl, struct search, cl);
  687. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  688. bch_mark_cache_accounting(s, !s->cache_miss, s->op.skip);
  689. trace_bcache_read(s->orig_bio, !s->cache_miss, s->op.skip);
  690. if (s->error)
  691. continue_at_nobarrier(cl, request_read_error, bcache_wq);
  692. else if (s->op.cache_bio || verify(dc, &s->bio.bio))
  693. continue_at_nobarrier(cl, request_read_done, bcache_wq);
  694. else
  695. continue_at_nobarrier(cl, cached_dev_read_complete, NULL);
  696. }
  697. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  698. struct bio *bio, unsigned sectors)
  699. {
  700. int ret = 0;
  701. unsigned reada;
  702. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  703. struct bio *miss;
  704. miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  705. if (miss == bio)
  706. s->op.lookup_done = true;
  707. miss->bi_end_io = request_endio;
  708. miss->bi_private = &s->cl;
  709. if (s->cache_miss || s->op.skip)
  710. goto out_submit;
  711. if (miss != bio ||
  712. (bio->bi_rw & REQ_RAHEAD) ||
  713. (bio->bi_rw & REQ_META) ||
  714. s->op.c->gc_stats.in_use >= CUTOFF_CACHE_READA)
  715. reada = 0;
  716. else {
  717. reada = min(dc->readahead >> 9,
  718. sectors - bio_sectors(miss));
  719. if (bio_end_sector(miss) + reada > bdev_sectors(miss->bi_bdev))
  720. reada = bdev_sectors(miss->bi_bdev) -
  721. bio_end_sector(miss);
  722. }
  723. s->cache_bio_sectors = bio_sectors(miss) + reada;
  724. s->op.cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  725. DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS),
  726. dc->disk.bio_split);
  727. if (!s->op.cache_bio)
  728. goto out_submit;
  729. s->op.cache_bio->bi_sector = miss->bi_sector;
  730. s->op.cache_bio->bi_bdev = miss->bi_bdev;
  731. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  732. s->op.cache_bio->bi_end_io = request_endio;
  733. s->op.cache_bio->bi_private = &s->cl;
  734. /* btree_search_recurse()'s btree iterator is no good anymore */
  735. ret = -EINTR;
  736. if (!bch_btree_insert_check_key(b, &s->op, s->op.cache_bio))
  737. goto out_put;
  738. bch_bio_map(s->op.cache_bio, NULL);
  739. if (bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
  740. goto out_put;
  741. s->cache_miss = miss;
  742. bio_get(s->op.cache_bio);
  743. closure_bio_submit(s->op.cache_bio, &s->cl, s->d);
  744. return ret;
  745. out_put:
  746. bio_put(s->op.cache_bio);
  747. s->op.cache_bio = NULL;
  748. out_submit:
  749. closure_bio_submit(miss, &s->cl, s->d);
  750. return ret;
  751. }
  752. static void request_read(struct cached_dev *dc, struct search *s)
  753. {
  754. struct closure *cl = &s->cl;
  755. check_should_skip(dc, s);
  756. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  757. continue_at(cl, request_read_done_bh, NULL);
  758. }
  759. /* Process writes */
  760. static void cached_dev_write_complete(struct closure *cl)
  761. {
  762. struct search *s = container_of(cl, struct search, cl);
  763. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  764. up_read_non_owner(&dc->writeback_lock);
  765. cached_dev_bio_complete(cl);
  766. }
  767. static void request_write(struct cached_dev *dc, struct search *s)
  768. {
  769. struct closure *cl = &s->cl;
  770. struct bio *bio = &s->bio.bio;
  771. struct bkey start, end;
  772. start = KEY(dc->disk.id, bio->bi_sector, 0);
  773. end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  774. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
  775. check_should_skip(dc, s);
  776. down_read_non_owner(&dc->writeback_lock);
  777. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  778. s->op.skip = false;
  779. s->writeback = true;
  780. }
  781. if (bio->bi_rw & REQ_DISCARD)
  782. goto skip;
  783. if (should_writeback(dc, s->orig_bio,
  784. cache_mode(dc, bio),
  785. s->op.skip)) {
  786. s->op.skip = false;
  787. s->writeback = true;
  788. }
  789. if (s->op.skip)
  790. goto skip;
  791. trace_bcache_write(s->orig_bio, s->writeback, s->op.skip);
  792. if (!s->writeback) {
  793. s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
  794. dc->disk.bio_split);
  795. closure_bio_submit(bio, cl, s->d);
  796. } else {
  797. bch_writeback_add(dc);
  798. if (s->op.flush_journal) {
  799. /* Also need to send a flush to the backing device */
  800. s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
  801. dc->disk.bio_split);
  802. bio->bi_size = 0;
  803. bio->bi_vcnt = 0;
  804. closure_bio_submit(bio, cl, s->d);
  805. } else {
  806. s->op.cache_bio = bio;
  807. }
  808. }
  809. out:
  810. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  811. continue_at(cl, cached_dev_write_complete, NULL);
  812. skip:
  813. s->op.skip = true;
  814. s->op.cache_bio = s->orig_bio;
  815. bio_get(s->op.cache_bio);
  816. if ((bio->bi_rw & REQ_DISCARD) &&
  817. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  818. goto out;
  819. closure_bio_submit(bio, cl, s->d);
  820. goto out;
  821. }
  822. static void request_nodata(struct cached_dev *dc, struct search *s)
  823. {
  824. struct closure *cl = &s->cl;
  825. struct bio *bio = &s->bio.bio;
  826. if (bio->bi_rw & REQ_DISCARD) {
  827. request_write(dc, s);
  828. return;
  829. }
  830. if (s->op.flush_journal)
  831. bch_journal_meta(s->op.c, cl);
  832. closure_bio_submit(bio, cl, s->d);
  833. continue_at(cl, cached_dev_bio_complete, NULL);
  834. }
  835. /* Cached devices - read & write stuff */
  836. unsigned bch_get_congested(struct cache_set *c)
  837. {
  838. int i;
  839. long rand;
  840. if (!c->congested_read_threshold_us &&
  841. !c->congested_write_threshold_us)
  842. return 0;
  843. i = (local_clock_us() - c->congested_last_us) / 1024;
  844. if (i < 0)
  845. return 0;
  846. i += atomic_read(&c->congested);
  847. if (i >= 0)
  848. return 0;
  849. i += CONGESTED_MAX;
  850. if (i > 0)
  851. i = fract_exp_two(i, 6);
  852. rand = get_random_int();
  853. i -= bitmap_weight(&rand, BITS_PER_LONG);
  854. return i > 0 ? i : 1;
  855. }
  856. static void add_sequential(struct task_struct *t)
  857. {
  858. ewma_add(t->sequential_io_avg,
  859. t->sequential_io, 8, 0);
  860. t->sequential_io = 0;
  861. }
  862. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  863. {
  864. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  865. }
  866. static void check_should_skip(struct cached_dev *dc, struct search *s)
  867. {
  868. struct cache_set *c = s->op.c;
  869. struct bio *bio = &s->bio.bio;
  870. unsigned mode = cache_mode(dc, bio);
  871. unsigned sectors, congested = bch_get_congested(c);
  872. if (atomic_read(&dc->disk.detaching) ||
  873. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  874. (bio->bi_rw & REQ_DISCARD))
  875. goto skip;
  876. if (mode == CACHE_MODE_NONE ||
  877. (mode == CACHE_MODE_WRITEAROUND &&
  878. (bio->bi_rw & REQ_WRITE)))
  879. goto skip;
  880. if (bio->bi_sector & (c->sb.block_size - 1) ||
  881. bio_sectors(bio) & (c->sb.block_size - 1)) {
  882. pr_debug("skipping unaligned io");
  883. goto skip;
  884. }
  885. if (!congested && !dc->sequential_cutoff)
  886. goto rescale;
  887. if (!congested &&
  888. mode == CACHE_MODE_WRITEBACK &&
  889. (bio->bi_rw & REQ_WRITE) &&
  890. (bio->bi_rw & REQ_SYNC))
  891. goto rescale;
  892. if (dc->sequential_merge) {
  893. struct io *i;
  894. spin_lock(&dc->io_lock);
  895. hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
  896. if (i->last == bio->bi_sector &&
  897. time_before(jiffies, i->jiffies))
  898. goto found;
  899. i = list_first_entry(&dc->io_lru, struct io, lru);
  900. add_sequential(s->task);
  901. i->sequential = 0;
  902. found:
  903. if (i->sequential + bio->bi_size > i->sequential)
  904. i->sequential += bio->bi_size;
  905. i->last = bio_end_sector(bio);
  906. i->jiffies = jiffies + msecs_to_jiffies(5000);
  907. s->task->sequential_io = i->sequential;
  908. hlist_del(&i->hash);
  909. hlist_add_head(&i->hash, iohash(dc, i->last));
  910. list_move_tail(&i->lru, &dc->io_lru);
  911. spin_unlock(&dc->io_lock);
  912. } else {
  913. s->task->sequential_io = bio->bi_size;
  914. add_sequential(s->task);
  915. }
  916. sectors = max(s->task->sequential_io,
  917. s->task->sequential_io_avg) >> 9;
  918. if (dc->sequential_cutoff &&
  919. sectors >= dc->sequential_cutoff >> 9) {
  920. trace_bcache_bypass_sequential(s->orig_bio);
  921. goto skip;
  922. }
  923. if (congested && sectors >= congested) {
  924. trace_bcache_bypass_congested(s->orig_bio);
  925. goto skip;
  926. }
  927. rescale:
  928. bch_rescale_priorities(c, bio_sectors(bio));
  929. return;
  930. skip:
  931. bch_mark_sectors_bypassed(s, bio_sectors(bio));
  932. s->op.skip = true;
  933. }
  934. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  935. {
  936. struct search *s;
  937. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  938. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  939. int cpu, rw = bio_data_dir(bio);
  940. cpu = part_stat_lock();
  941. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  942. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  943. part_stat_unlock();
  944. bio->bi_bdev = dc->bdev;
  945. bio->bi_sector += dc->sb.data_offset;
  946. if (cached_dev_get(dc)) {
  947. s = search_alloc(bio, d);
  948. trace_bcache_request_start(s, bio);
  949. if (!bio_has_data(bio))
  950. request_nodata(dc, s);
  951. else if (rw)
  952. request_write(dc, s);
  953. else
  954. request_read(dc, s);
  955. } else {
  956. if ((bio->bi_rw & REQ_DISCARD) &&
  957. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  958. bio_endio(bio, 0);
  959. else
  960. bch_generic_make_request(bio, &d->bio_split_hook);
  961. }
  962. }
  963. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  964. unsigned int cmd, unsigned long arg)
  965. {
  966. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  967. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  968. }
  969. static int cached_dev_congested(void *data, int bits)
  970. {
  971. struct bcache_device *d = data;
  972. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  973. struct request_queue *q = bdev_get_queue(dc->bdev);
  974. int ret = 0;
  975. if (bdi_congested(&q->backing_dev_info, bits))
  976. return 1;
  977. if (cached_dev_get(dc)) {
  978. unsigned i;
  979. struct cache *ca;
  980. for_each_cache(ca, d->c, i) {
  981. q = bdev_get_queue(ca->bdev);
  982. ret |= bdi_congested(&q->backing_dev_info, bits);
  983. }
  984. cached_dev_put(dc);
  985. }
  986. return ret;
  987. }
  988. void bch_cached_dev_request_init(struct cached_dev *dc)
  989. {
  990. struct gendisk *g = dc->disk.disk;
  991. g->queue->make_request_fn = cached_dev_make_request;
  992. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  993. dc->disk.cache_miss = cached_dev_cache_miss;
  994. dc->disk.ioctl = cached_dev_ioctl;
  995. }
  996. /* Flash backed devices */
  997. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  998. struct bio *bio, unsigned sectors)
  999. {
  1000. struct bio_vec *bv;
  1001. int i;
  1002. /* Zero fill bio */
  1003. bio_for_each_segment(bv, bio, i) {
  1004. unsigned j = min(bv->bv_len >> 9, sectors);
  1005. void *p = kmap(bv->bv_page);
  1006. memset(p + bv->bv_offset, 0, j << 9);
  1007. kunmap(bv->bv_page);
  1008. sectors -= j;
  1009. }
  1010. bio_advance(bio, min(sectors << 9, bio->bi_size));
  1011. if (!bio->bi_size)
  1012. s->op.lookup_done = true;
  1013. return 0;
  1014. }
  1015. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  1016. {
  1017. struct search *s;
  1018. struct closure *cl;
  1019. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  1020. int cpu, rw = bio_data_dir(bio);
  1021. cpu = part_stat_lock();
  1022. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  1023. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  1024. part_stat_unlock();
  1025. s = search_alloc(bio, d);
  1026. cl = &s->cl;
  1027. bio = &s->bio.bio;
  1028. trace_bcache_request_start(s, bio);
  1029. if (bio_has_data(bio) && !rw) {
  1030. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  1031. } else if (bio_has_data(bio) || s->op.skip) {
  1032. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
  1033. &KEY(d->id, bio->bi_sector, 0),
  1034. &KEY(d->id, bio_end_sector(bio), 0));
  1035. s->writeback = true;
  1036. s->op.cache_bio = bio;
  1037. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  1038. } else {
  1039. /* No data - probably a cache flush */
  1040. if (s->op.flush_journal)
  1041. bch_journal_meta(s->op.c, cl);
  1042. }
  1043. continue_at(cl, search_free, NULL);
  1044. }
  1045. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1046. unsigned int cmd, unsigned long arg)
  1047. {
  1048. return -ENOTTY;
  1049. }
  1050. static int flash_dev_congested(void *data, int bits)
  1051. {
  1052. struct bcache_device *d = data;
  1053. struct request_queue *q;
  1054. struct cache *ca;
  1055. unsigned i;
  1056. int ret = 0;
  1057. for_each_cache(ca, d->c, i) {
  1058. q = bdev_get_queue(ca->bdev);
  1059. ret |= bdi_congested(&q->backing_dev_info, bits);
  1060. }
  1061. return ret;
  1062. }
  1063. void bch_flash_dev_request_init(struct bcache_device *d)
  1064. {
  1065. struct gendisk *g = d->disk;
  1066. g->queue->make_request_fn = flash_dev_make_request;
  1067. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1068. d->cache_miss = flash_dev_cache_miss;
  1069. d->ioctl = flash_dev_ioctl;
  1070. }
  1071. void bch_request_exit(void)
  1072. {
  1073. #ifdef CONFIG_CGROUP_BCACHE
  1074. cgroup_unload_subsys(&bcache_subsys);
  1075. #endif
  1076. if (bch_search_cache)
  1077. kmem_cache_destroy(bch_search_cache);
  1078. }
  1079. int __init bch_request_init(void)
  1080. {
  1081. bch_search_cache = KMEM_CACHE(search, 0);
  1082. if (!bch_search_cache)
  1083. return -ENOMEM;
  1084. #ifdef CONFIG_CGROUP_BCACHE
  1085. cgroup_load_subsys(&bcache_subsys);
  1086. init_bch_cgroup(&bcache_default_cgroup);
  1087. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1088. #endif
  1089. return 0;
  1090. }