request.c 33 KB

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