request.c 37 KB

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