request.c 36 KB

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