request.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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 = bch_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 = bch_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 = bch_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. trace_bcache_cache_insert(k);
  435. bch_keylist_push(&op->keys);
  436. n->bi_rw |= REQ_WRITE;
  437. bch_submit_bbio(n, op->c, k, 0);
  438. } while (n != bio);
  439. op->insert_data_done = true;
  440. continue_at(cl, bch_journal, bcache_wq);
  441. err:
  442. /* bch_alloc_sectors() blocks if s->writeback = true */
  443. BUG_ON(s->writeback);
  444. /*
  445. * But if it's not a writeback write we'd rather just bail out if
  446. * there aren't any buckets ready to write to - it might take awhile and
  447. * we might be starving btree writes for gc or something.
  448. */
  449. if (s->write) {
  450. /*
  451. * Writethrough write: We can't complete the write until we've
  452. * updated the index. But we don't want to delay the write while
  453. * we wait for buckets to be freed up, so just invalidate the
  454. * rest of the write.
  455. */
  456. op->skip = true;
  457. return bio_invalidate(cl);
  458. } else {
  459. /*
  460. * From a cache miss, we can just insert the keys for the data
  461. * we have written or bail out if we didn't do anything.
  462. */
  463. op->insert_data_done = true;
  464. bio_put(bio);
  465. if (!bch_keylist_empty(&op->keys))
  466. continue_at(cl, bch_journal, bcache_wq);
  467. else
  468. closure_return(cl);
  469. }
  470. }
  471. /**
  472. * bch_insert_data - stick some data in the cache
  473. *
  474. * This is the starting point for any data to end up in a cache device; it could
  475. * be from a normal write, or a writeback write, or a write to a flash only
  476. * volume - it's also used by the moving garbage collector to compact data in
  477. * mostly empty buckets.
  478. *
  479. * It first writes the data to the cache, creating a list of keys to be inserted
  480. * (if the data had to be fragmented there will be multiple keys); after the
  481. * data is written it calls bch_journal, and after the keys have been added to
  482. * the next journal write they're inserted into the btree.
  483. *
  484. * It inserts the data in op->cache_bio; bi_sector is used for the key offset,
  485. * and op->inode is used for the key inode.
  486. *
  487. * If op->skip is true, instead of inserting the data it invalidates the region
  488. * of the cache represented by op->cache_bio and op->inode.
  489. */
  490. void bch_insert_data(struct closure *cl)
  491. {
  492. struct btree_op *op = container_of(cl, struct btree_op, cl);
  493. bch_keylist_init(&op->keys);
  494. bio_get(op->cache_bio);
  495. bch_insert_data_loop(cl);
  496. }
  497. void bch_btree_insert_async(struct closure *cl)
  498. {
  499. struct btree_op *op = container_of(cl, struct btree_op, cl);
  500. struct search *s = container_of(op, struct search, op);
  501. if (bch_btree_insert(op, op->c)) {
  502. s->error = -ENOMEM;
  503. op->insert_data_done = true;
  504. }
  505. if (op->insert_data_done) {
  506. bch_keylist_free(&op->keys);
  507. closure_return(cl);
  508. } else
  509. continue_at(cl, bch_insert_data_loop, bcache_wq);
  510. }
  511. /* Common code for the make_request functions */
  512. static void request_endio(struct bio *bio, int error)
  513. {
  514. struct closure *cl = bio->bi_private;
  515. if (error) {
  516. struct search *s = container_of(cl, struct search, cl);
  517. s->error = error;
  518. /* Only cache read errors are recoverable */
  519. s->recoverable = false;
  520. }
  521. bio_put(bio);
  522. closure_put(cl);
  523. }
  524. void bch_cache_read_endio(struct bio *bio, int error)
  525. {
  526. struct bbio *b = container_of(bio, struct bbio, bio);
  527. struct closure *cl = bio->bi_private;
  528. struct search *s = container_of(cl, struct search, cl);
  529. /*
  530. * If the bucket was reused while our bio was in flight, we might have
  531. * read the wrong data. Set s->error but not error so it doesn't get
  532. * counted against the cache device, but we'll still reread the data
  533. * from the backing device.
  534. */
  535. if (error)
  536. s->error = error;
  537. else if (ptr_stale(s->op.c, &b->key, 0)) {
  538. atomic_long_inc(&s->op.c->cache_read_races);
  539. s->error = -EINTR;
  540. }
  541. bch_bbio_endio(s->op.c, bio, error, "reading from cache");
  542. }
  543. static void bio_complete(struct search *s)
  544. {
  545. if (s->orig_bio) {
  546. int cpu, rw = bio_data_dir(s->orig_bio);
  547. unsigned long duration = jiffies - s->start_time;
  548. cpu = part_stat_lock();
  549. part_round_stats(cpu, &s->d->disk->part0);
  550. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  551. part_stat_unlock();
  552. trace_bcache_request_end(s, s->orig_bio);
  553. bio_endio(s->orig_bio, s->error);
  554. s->orig_bio = NULL;
  555. }
  556. }
  557. static void do_bio_hook(struct search *s)
  558. {
  559. struct bio *bio = &s->bio.bio;
  560. memcpy(bio, s->orig_bio, sizeof(struct bio));
  561. bio->bi_end_io = request_endio;
  562. bio->bi_private = &s->cl;
  563. atomic_set(&bio->bi_cnt, 3);
  564. }
  565. static void search_free(struct closure *cl)
  566. {
  567. struct search *s = container_of(cl, struct search, cl);
  568. bio_complete(s);
  569. if (s->op.cache_bio)
  570. bio_put(s->op.cache_bio);
  571. if (s->unaligned_bvec)
  572. mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
  573. closure_debug_destroy(cl);
  574. mempool_free(s, s->d->c->search);
  575. }
  576. static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
  577. {
  578. struct bio_vec *bv;
  579. struct search *s = mempool_alloc(d->c->search, GFP_NOIO);
  580. memset(s, 0, offsetof(struct search, op.keys));
  581. __closure_init(&s->cl, NULL);
  582. s->op.inode = d->id;
  583. s->op.c = d->c;
  584. s->d = d;
  585. s->op.lock = -1;
  586. s->task = current;
  587. s->orig_bio = bio;
  588. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  589. s->op.flush_journal = (bio->bi_rw & REQ_FLUSH) != 0;
  590. s->op.skip = (bio->bi_rw & REQ_DISCARD) != 0;
  591. s->recoverable = 1;
  592. s->start_time = jiffies;
  593. do_bio_hook(s);
  594. if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
  595. bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
  596. memcpy(bv, bio_iovec(bio),
  597. sizeof(struct bio_vec) * bio_segments(bio));
  598. s->bio.bio.bi_io_vec = bv;
  599. s->unaligned_bvec = 1;
  600. }
  601. return s;
  602. }
  603. static void btree_read_async(struct closure *cl)
  604. {
  605. struct btree_op *op = container_of(cl, struct btree_op, cl);
  606. int ret = btree_root(search_recurse, op->c, op);
  607. if (ret == -EAGAIN)
  608. continue_at(cl, btree_read_async, bcache_wq);
  609. closure_return(cl);
  610. }
  611. /* Cached devices */
  612. static void cached_dev_bio_complete(struct closure *cl)
  613. {
  614. struct search *s = container_of(cl, struct search, cl);
  615. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  616. search_free(cl);
  617. cached_dev_put(dc);
  618. }
  619. /* Process reads */
  620. static void cached_dev_read_complete(struct closure *cl)
  621. {
  622. struct search *s = container_of(cl, struct search, cl);
  623. if (s->op.insert_collision)
  624. bch_mark_cache_miss_collision(s);
  625. if (s->op.cache_bio) {
  626. int i;
  627. struct bio_vec *bv;
  628. __bio_for_each_segment(bv, s->op.cache_bio, i, 0)
  629. __free_page(bv->bv_page);
  630. }
  631. cached_dev_bio_complete(cl);
  632. }
  633. static void request_read_error(struct closure *cl)
  634. {
  635. struct search *s = container_of(cl, struct search, cl);
  636. struct bio_vec *bv;
  637. int i;
  638. if (s->recoverable) {
  639. /* Retry from the backing device: */
  640. trace_bcache_read_retry(s->orig_bio);
  641. s->error = 0;
  642. bv = s->bio.bio.bi_io_vec;
  643. do_bio_hook(s);
  644. s->bio.bio.bi_io_vec = bv;
  645. if (!s->unaligned_bvec)
  646. bio_for_each_segment(bv, s->orig_bio, i)
  647. bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
  648. else
  649. memcpy(s->bio.bio.bi_io_vec,
  650. bio_iovec(s->orig_bio),
  651. sizeof(struct bio_vec) *
  652. bio_segments(s->orig_bio));
  653. /* XXX: invalidate cache */
  654. closure_bio_submit(&s->bio.bio, &s->cl, s->d);
  655. }
  656. continue_at(cl, cached_dev_read_complete, NULL);
  657. }
  658. static void request_read_done(struct closure *cl)
  659. {
  660. struct search *s = container_of(cl, struct search, cl);
  661. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  662. /*
  663. * s->cache_bio != NULL implies that we had a cache miss; cache_bio now
  664. * contains data ready to be inserted into the cache.
  665. *
  666. * First, we copy the data we just read from cache_bio's bounce buffers
  667. * to the buffers the original bio pointed to:
  668. */
  669. if (s->op.cache_bio) {
  670. struct bio_vec *src, *dst;
  671. unsigned src_offset, dst_offset, bytes;
  672. void *dst_ptr;
  673. bio_reset(s->op.cache_bio);
  674. s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
  675. s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
  676. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  677. bch_bio_map(s->op.cache_bio, NULL);
  678. src = bio_iovec(s->op.cache_bio);
  679. dst = bio_iovec(s->cache_miss);
  680. src_offset = src->bv_offset;
  681. dst_offset = dst->bv_offset;
  682. dst_ptr = kmap(dst->bv_page);
  683. while (1) {
  684. if (dst_offset == dst->bv_offset + dst->bv_len) {
  685. kunmap(dst->bv_page);
  686. dst++;
  687. if (dst == bio_iovec_idx(s->cache_miss,
  688. s->cache_miss->bi_vcnt))
  689. break;
  690. dst_offset = dst->bv_offset;
  691. dst_ptr = kmap(dst->bv_page);
  692. }
  693. if (src_offset == src->bv_offset + src->bv_len) {
  694. src++;
  695. if (src == bio_iovec_idx(s->op.cache_bio,
  696. s->op.cache_bio->bi_vcnt))
  697. BUG();
  698. src_offset = src->bv_offset;
  699. }
  700. bytes = min(dst->bv_offset + dst->bv_len - dst_offset,
  701. src->bv_offset + src->bv_len - src_offset);
  702. memcpy(dst_ptr + dst_offset,
  703. page_address(src->bv_page) + src_offset,
  704. bytes);
  705. src_offset += bytes;
  706. dst_offset += bytes;
  707. }
  708. bio_put(s->cache_miss);
  709. s->cache_miss = NULL;
  710. }
  711. if (verify(dc, &s->bio.bio) && s->recoverable)
  712. bch_data_verify(s);
  713. bio_complete(s);
  714. if (s->op.cache_bio &&
  715. !test_bit(CACHE_SET_STOPPING, &s->op.c->flags)) {
  716. s->op.type = BTREE_REPLACE;
  717. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  718. }
  719. continue_at(cl, cached_dev_read_complete, NULL);
  720. }
  721. static void request_read_done_bh(struct closure *cl)
  722. {
  723. struct search *s = container_of(cl, struct search, cl);
  724. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  725. bch_mark_cache_accounting(s, !s->cache_miss, s->op.skip);
  726. trace_bcache_read(s->orig_bio, !s->cache_miss, s->op.skip);
  727. if (s->error)
  728. continue_at_nobarrier(cl, request_read_error, bcache_wq);
  729. else if (s->op.cache_bio || verify(dc, &s->bio.bio))
  730. continue_at_nobarrier(cl, request_read_done, bcache_wq);
  731. else
  732. continue_at_nobarrier(cl, cached_dev_read_complete, NULL);
  733. }
  734. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  735. struct bio *bio, unsigned sectors)
  736. {
  737. int ret = 0;
  738. unsigned reada;
  739. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  740. struct bio *miss;
  741. miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  742. if (!miss)
  743. return -EAGAIN;
  744. if (miss == bio)
  745. s->op.lookup_done = true;
  746. miss->bi_end_io = request_endio;
  747. miss->bi_private = &s->cl;
  748. if (s->cache_miss || s->op.skip)
  749. goto out_submit;
  750. if (miss != bio ||
  751. (bio->bi_rw & REQ_RAHEAD) ||
  752. (bio->bi_rw & REQ_META) ||
  753. s->op.c->gc_stats.in_use >= CUTOFF_CACHE_READA)
  754. reada = 0;
  755. else {
  756. reada = min(dc->readahead >> 9,
  757. sectors - bio_sectors(miss));
  758. if (bio_end(miss) + reada > bdev_sectors(miss->bi_bdev))
  759. reada = bdev_sectors(miss->bi_bdev) - bio_end(miss);
  760. }
  761. s->cache_bio_sectors = bio_sectors(miss) + reada;
  762. s->op.cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  763. DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS),
  764. dc->disk.bio_split);
  765. if (!s->op.cache_bio)
  766. goto out_submit;
  767. s->op.cache_bio->bi_sector = miss->bi_sector;
  768. s->op.cache_bio->bi_bdev = miss->bi_bdev;
  769. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  770. s->op.cache_bio->bi_end_io = request_endio;
  771. s->op.cache_bio->bi_private = &s->cl;
  772. /* btree_search_recurse()'s btree iterator is no good anymore */
  773. ret = -EINTR;
  774. if (!bch_btree_insert_check_key(b, &s->op, s->op.cache_bio))
  775. goto out_put;
  776. bch_bio_map(s->op.cache_bio, NULL);
  777. if (bch_bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
  778. goto out_put;
  779. s->cache_miss = miss;
  780. bio_get(s->op.cache_bio);
  781. closure_bio_submit(s->op.cache_bio, &s->cl, s->d);
  782. return ret;
  783. out_put:
  784. bio_put(s->op.cache_bio);
  785. s->op.cache_bio = NULL;
  786. out_submit:
  787. closure_bio_submit(miss, &s->cl, s->d);
  788. return ret;
  789. }
  790. static void request_read(struct cached_dev *dc, struct search *s)
  791. {
  792. struct closure *cl = &s->cl;
  793. check_should_skip(dc, s);
  794. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  795. continue_at(cl, request_read_done_bh, NULL);
  796. }
  797. /* Process writes */
  798. static void cached_dev_write_complete(struct closure *cl)
  799. {
  800. struct search *s = container_of(cl, struct search, cl);
  801. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  802. up_read_non_owner(&dc->writeback_lock);
  803. cached_dev_bio_complete(cl);
  804. }
  805. static bool should_writeback(struct cached_dev *dc, struct bio *bio)
  806. {
  807. unsigned threshold = (bio->bi_rw & REQ_SYNC)
  808. ? CUTOFF_WRITEBACK_SYNC
  809. : CUTOFF_WRITEBACK;
  810. return !atomic_read(&dc->disk.detaching) &&
  811. cache_mode(dc, bio) == CACHE_MODE_WRITEBACK &&
  812. dc->disk.c->gc_stats.in_use < threshold;
  813. }
  814. static void request_write(struct cached_dev *dc, struct search *s)
  815. {
  816. struct closure *cl = &s->cl;
  817. struct bio *bio = &s->bio.bio;
  818. struct bkey start, end;
  819. start = KEY(dc->disk.id, bio->bi_sector, 0);
  820. end = KEY(dc->disk.id, bio_end(bio), 0);
  821. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
  822. check_should_skip(dc, s);
  823. down_read_non_owner(&dc->writeback_lock);
  824. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  825. s->op.skip = false;
  826. s->writeback = true;
  827. }
  828. if (bio->bi_rw & REQ_DISCARD)
  829. goto skip;
  830. if (s->op.skip)
  831. goto skip;
  832. if (should_writeback(dc, s->orig_bio))
  833. s->writeback = true;
  834. trace_bcache_write(s->orig_bio, s->writeback, s->op.skip);
  835. if (!s->writeback) {
  836. s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
  837. dc->disk.bio_split);
  838. closure_bio_submit(bio, cl, s->d);
  839. } else {
  840. s->op.cache_bio = bio;
  841. bch_writeback_add(dc, bio_sectors(bio));
  842. }
  843. out:
  844. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  845. continue_at(cl, cached_dev_write_complete, NULL);
  846. skip:
  847. s->op.skip = true;
  848. s->op.cache_bio = s->orig_bio;
  849. bio_get(s->op.cache_bio);
  850. if ((bio->bi_rw & REQ_DISCARD) &&
  851. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  852. goto out;
  853. closure_bio_submit(bio, cl, s->d);
  854. goto out;
  855. }
  856. static void request_nodata(struct cached_dev *dc, struct search *s)
  857. {
  858. struct closure *cl = &s->cl;
  859. struct bio *bio = &s->bio.bio;
  860. if (bio->bi_rw & REQ_DISCARD) {
  861. request_write(dc, s);
  862. return;
  863. }
  864. if (s->op.flush_journal)
  865. bch_journal_meta(s->op.c, cl);
  866. closure_bio_submit(bio, cl, s->d);
  867. continue_at(cl, cached_dev_bio_complete, NULL);
  868. }
  869. /* Cached devices - read & write stuff */
  870. unsigned bch_get_congested(struct cache_set *c)
  871. {
  872. int i;
  873. long rand;
  874. if (!c->congested_read_threshold_us &&
  875. !c->congested_write_threshold_us)
  876. return 0;
  877. i = (local_clock_us() - c->congested_last_us) / 1024;
  878. if (i < 0)
  879. return 0;
  880. i += atomic_read(&c->congested);
  881. if (i >= 0)
  882. return 0;
  883. i += CONGESTED_MAX;
  884. if (i > 0)
  885. i = fract_exp_two(i, 6);
  886. rand = get_random_int();
  887. i -= bitmap_weight(&rand, BITS_PER_LONG);
  888. return i > 0 ? i : 1;
  889. }
  890. static void add_sequential(struct task_struct *t)
  891. {
  892. ewma_add(t->sequential_io_avg,
  893. t->sequential_io, 8, 0);
  894. t->sequential_io = 0;
  895. }
  896. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  897. {
  898. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  899. }
  900. static void check_should_skip(struct cached_dev *dc, struct search *s)
  901. {
  902. struct cache_set *c = s->op.c;
  903. struct bio *bio = &s->bio.bio;
  904. unsigned mode = cache_mode(dc, bio);
  905. unsigned sectors, congested = bch_get_congested(c);
  906. if (atomic_read(&dc->disk.detaching) ||
  907. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  908. (bio->bi_rw & REQ_DISCARD))
  909. goto skip;
  910. if (mode == CACHE_MODE_NONE ||
  911. (mode == CACHE_MODE_WRITEAROUND &&
  912. (bio->bi_rw & REQ_WRITE)))
  913. goto skip;
  914. if (bio->bi_sector & (c->sb.block_size - 1) ||
  915. bio_sectors(bio) & (c->sb.block_size - 1)) {
  916. pr_debug("skipping unaligned io");
  917. goto skip;
  918. }
  919. if (!congested && !dc->sequential_cutoff)
  920. goto rescale;
  921. if (!congested &&
  922. mode == CACHE_MODE_WRITEBACK &&
  923. (bio->bi_rw & REQ_WRITE) &&
  924. (bio->bi_rw & REQ_SYNC))
  925. goto rescale;
  926. if (dc->sequential_merge) {
  927. struct io *i;
  928. spin_lock(&dc->io_lock);
  929. hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
  930. if (i->last == bio->bi_sector &&
  931. time_before(jiffies, i->jiffies))
  932. goto found;
  933. i = list_first_entry(&dc->io_lru, struct io, lru);
  934. add_sequential(s->task);
  935. i->sequential = 0;
  936. found:
  937. if (i->sequential + bio->bi_size > i->sequential)
  938. i->sequential += bio->bi_size;
  939. i->last = bio_end(bio);
  940. i->jiffies = jiffies + msecs_to_jiffies(5000);
  941. s->task->sequential_io = i->sequential;
  942. hlist_del(&i->hash);
  943. hlist_add_head(&i->hash, iohash(dc, i->last));
  944. list_move_tail(&i->lru, &dc->io_lru);
  945. spin_unlock(&dc->io_lock);
  946. } else {
  947. s->task->sequential_io = bio->bi_size;
  948. add_sequential(s->task);
  949. }
  950. sectors = max(s->task->sequential_io,
  951. s->task->sequential_io_avg) >> 9;
  952. if (dc->sequential_cutoff &&
  953. sectors >= dc->sequential_cutoff >> 9) {
  954. trace_bcache_bypass_sequential(s->orig_bio);
  955. goto skip;
  956. }
  957. if (congested && sectors >= congested) {
  958. trace_bcache_bypass_congested(s->orig_bio);
  959. goto skip;
  960. }
  961. rescale:
  962. bch_rescale_priorities(c, bio_sectors(bio));
  963. return;
  964. skip:
  965. bch_mark_sectors_bypassed(s, bio_sectors(bio));
  966. s->op.skip = true;
  967. }
  968. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  969. {
  970. struct search *s;
  971. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  972. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  973. int cpu, rw = bio_data_dir(bio);
  974. cpu = part_stat_lock();
  975. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  976. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  977. part_stat_unlock();
  978. bio->bi_bdev = dc->bdev;
  979. bio->bi_sector += dc->sb.data_offset;
  980. if (cached_dev_get(dc)) {
  981. s = search_alloc(bio, d);
  982. trace_bcache_request_start(s, bio);
  983. if (!bio_has_data(bio))
  984. request_nodata(dc, s);
  985. else if (rw)
  986. request_write(dc, s);
  987. else
  988. request_read(dc, s);
  989. } else {
  990. if ((bio->bi_rw & REQ_DISCARD) &&
  991. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  992. bio_endio(bio, 0);
  993. else
  994. bch_generic_make_request(bio, &d->bio_split_hook);
  995. }
  996. }
  997. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  998. unsigned int cmd, unsigned long arg)
  999. {
  1000. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1001. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  1002. }
  1003. static int cached_dev_congested(void *data, int bits)
  1004. {
  1005. struct bcache_device *d = data;
  1006. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1007. struct request_queue *q = bdev_get_queue(dc->bdev);
  1008. int ret = 0;
  1009. if (bdi_congested(&q->backing_dev_info, bits))
  1010. return 1;
  1011. if (cached_dev_get(dc)) {
  1012. unsigned i;
  1013. struct cache *ca;
  1014. for_each_cache(ca, d->c, i) {
  1015. q = bdev_get_queue(ca->bdev);
  1016. ret |= bdi_congested(&q->backing_dev_info, bits);
  1017. }
  1018. cached_dev_put(dc);
  1019. }
  1020. return ret;
  1021. }
  1022. void bch_cached_dev_request_init(struct cached_dev *dc)
  1023. {
  1024. struct gendisk *g = dc->disk.disk;
  1025. g->queue->make_request_fn = cached_dev_make_request;
  1026. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  1027. dc->disk.cache_miss = cached_dev_cache_miss;
  1028. dc->disk.ioctl = cached_dev_ioctl;
  1029. }
  1030. /* Flash backed devices */
  1031. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  1032. struct bio *bio, unsigned sectors)
  1033. {
  1034. /* Zero fill bio */
  1035. while (bio->bi_idx != bio->bi_vcnt) {
  1036. struct bio_vec *bv = bio_iovec(bio);
  1037. unsigned j = min(bv->bv_len >> 9, sectors);
  1038. void *p = kmap(bv->bv_page);
  1039. memset(p + bv->bv_offset, 0, j << 9);
  1040. kunmap(bv->bv_page);
  1041. bv->bv_len -= j << 9;
  1042. bv->bv_offset += j << 9;
  1043. if (bv->bv_len)
  1044. return 0;
  1045. bio->bi_sector += j;
  1046. bio->bi_size -= j << 9;
  1047. bio->bi_idx++;
  1048. sectors -= j;
  1049. }
  1050. s->op.lookup_done = true;
  1051. return 0;
  1052. }
  1053. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  1054. {
  1055. struct search *s;
  1056. struct closure *cl;
  1057. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  1058. int cpu, rw = bio_data_dir(bio);
  1059. cpu = part_stat_lock();
  1060. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  1061. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  1062. part_stat_unlock();
  1063. s = search_alloc(bio, d);
  1064. cl = &s->cl;
  1065. bio = &s->bio.bio;
  1066. trace_bcache_request_start(s, bio);
  1067. if (bio_has_data(bio) && !rw) {
  1068. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  1069. } else if (bio_has_data(bio) || s->op.skip) {
  1070. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
  1071. &KEY(d->id, bio->bi_sector, 0),
  1072. &KEY(d->id, bio_end(bio), 0));
  1073. s->writeback = true;
  1074. s->op.cache_bio = bio;
  1075. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  1076. } else {
  1077. /* No data - probably a cache flush */
  1078. if (s->op.flush_journal)
  1079. bch_journal_meta(s->op.c, cl);
  1080. }
  1081. continue_at(cl, search_free, NULL);
  1082. }
  1083. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1084. unsigned int cmd, unsigned long arg)
  1085. {
  1086. return -ENOTTY;
  1087. }
  1088. static int flash_dev_congested(void *data, int bits)
  1089. {
  1090. struct bcache_device *d = data;
  1091. struct request_queue *q;
  1092. struct cache *ca;
  1093. unsigned i;
  1094. int ret = 0;
  1095. for_each_cache(ca, d->c, i) {
  1096. q = bdev_get_queue(ca->bdev);
  1097. ret |= bdi_congested(&q->backing_dev_info, bits);
  1098. }
  1099. return ret;
  1100. }
  1101. void bch_flash_dev_request_init(struct bcache_device *d)
  1102. {
  1103. struct gendisk *g = d->disk;
  1104. g->queue->make_request_fn = flash_dev_make_request;
  1105. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1106. d->cache_miss = flash_dev_cache_miss;
  1107. d->ioctl = flash_dev_ioctl;
  1108. }
  1109. void bch_request_exit(void)
  1110. {
  1111. #ifdef CONFIG_CGROUP_BCACHE
  1112. cgroup_unload_subsys(&bcache_subsys);
  1113. #endif
  1114. if (bch_search_cache)
  1115. kmem_cache_destroy(bch_search_cache);
  1116. }
  1117. int __init bch_request_init(void)
  1118. {
  1119. bch_search_cache = KMEM_CACHE(search, 0);
  1120. if (!bch_search_cache)
  1121. return -ENOMEM;
  1122. #ifdef CONFIG_CGROUP_BCACHE
  1123. cgroup_load_subsys(&bcache_subsys);
  1124. init_bch_cgroup(&bcache_default_cgroup);
  1125. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1126. #endif
  1127. return 0;
  1128. }