request.c 34 KB

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