request.c 36 KB

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