request.c 34 KB

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