request.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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/module.h>
  14. #include <linux/hash.h>
  15. #include <linux/random.h>
  16. #include <linux/backing-dev.h>
  17. #include <trace/events/bcache.h>
  18. #define CUTOFF_CACHE_ADD 95
  19. #define CUTOFF_CACHE_READA 90
  20. struct kmem_cache *bch_search_cache;
  21. static void bch_data_insert_start(struct closure *);
  22. static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
  23. {
  24. return BDEV_CACHE_MODE(&dc->sb);
  25. }
  26. static bool verify(struct cached_dev *dc, struct bio *bio)
  27. {
  28. return dc->verify;
  29. }
  30. static void bio_csum(struct bio *bio, struct bkey *k)
  31. {
  32. struct bio_vec bv;
  33. struct bvec_iter iter;
  34. uint64_t csum = 0;
  35. bio_for_each_segment(bv, bio, iter) {
  36. void *d = kmap(bv.bv_page) + bv.bv_offset;
  37. csum = bch_crc64_update(csum, d, bv.bv_len);
  38. kunmap(bv.bv_page);
  39. }
  40. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  41. }
  42. /* Insert data into cache */
  43. static void bch_data_insert_keys(struct closure *cl)
  44. {
  45. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  46. atomic_t *journal_ref = NULL;
  47. struct bkey *replace_key = op->replace ? &op->replace_key : NULL;
  48. int ret;
  49. /*
  50. * If we're looping, might already be waiting on
  51. * another journal write - can't wait on more than one journal write at
  52. * a time
  53. *
  54. * XXX: this looks wrong
  55. */
  56. #if 0
  57. while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING)
  58. closure_sync(&s->cl);
  59. #endif
  60. if (!op->replace)
  61. journal_ref = bch_journal(op->c, &op->insert_keys,
  62. op->flush_journal ? cl : NULL);
  63. ret = bch_btree_insert(op->c, &op->insert_keys,
  64. journal_ref, replace_key);
  65. if (ret == -ESRCH) {
  66. op->replace_collision = true;
  67. } else if (ret) {
  68. op->status = BLK_STS_RESOURCE;
  69. op->insert_data_done = true;
  70. }
  71. if (journal_ref)
  72. atomic_dec_bug(journal_ref);
  73. if (!op->insert_data_done) {
  74. continue_at(cl, bch_data_insert_start, op->wq);
  75. return;
  76. }
  77. bch_keylist_free(&op->insert_keys);
  78. closure_return(cl);
  79. }
  80. static int bch_keylist_realloc(struct keylist *l, unsigned u64s,
  81. struct cache_set *c)
  82. {
  83. size_t oldsize = bch_keylist_nkeys(l);
  84. size_t newsize = oldsize + u64s;
  85. /*
  86. * The journalling code doesn't handle the case where the keys to insert
  87. * is bigger than an empty write: If we just return -ENOMEM here,
  88. * bio_insert() and bio_invalidate() will insert the keys created so far
  89. * and finish the rest when the keylist is empty.
  90. */
  91. if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
  92. return -ENOMEM;
  93. return __bch_keylist_realloc(l, u64s);
  94. }
  95. static void bch_data_invalidate(struct closure *cl)
  96. {
  97. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  98. struct bio *bio = op->bio;
  99. pr_debug("invalidating %i sectors from %llu",
  100. bio_sectors(bio), (uint64_t) bio->bi_iter.bi_sector);
  101. while (bio_sectors(bio)) {
  102. unsigned sectors = min(bio_sectors(bio),
  103. 1U << (KEY_SIZE_BITS - 1));
  104. if (bch_keylist_realloc(&op->insert_keys, 2, op->c))
  105. goto out;
  106. bio->bi_iter.bi_sector += sectors;
  107. bio->bi_iter.bi_size -= sectors << 9;
  108. bch_keylist_add(&op->insert_keys,
  109. &KEY(op->inode, bio->bi_iter.bi_sector, sectors));
  110. }
  111. op->insert_data_done = true;
  112. bio_put(bio);
  113. out:
  114. continue_at(cl, bch_data_insert_keys, op->wq);
  115. }
  116. static void bch_data_insert_error(struct closure *cl)
  117. {
  118. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  119. /*
  120. * Our data write just errored, which means we've got a bunch of keys to
  121. * insert that point to data that wasn't succesfully written.
  122. *
  123. * We don't have to insert those keys but we still have to invalidate
  124. * that region of the cache - so, if we just strip off all the pointers
  125. * from the keys we'll accomplish just that.
  126. */
  127. struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys;
  128. while (src != op->insert_keys.top) {
  129. struct bkey *n = bkey_next(src);
  130. SET_KEY_PTRS(src, 0);
  131. memmove(dst, src, bkey_bytes(src));
  132. dst = bkey_next(dst);
  133. src = n;
  134. }
  135. op->insert_keys.top = dst;
  136. bch_data_insert_keys(cl);
  137. }
  138. static void bch_data_insert_endio(struct bio *bio)
  139. {
  140. struct closure *cl = bio->bi_private;
  141. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  142. if (bio->bi_status) {
  143. /* TODO: We could try to recover from this. */
  144. if (op->writeback)
  145. op->status = bio->bi_status;
  146. else if (!op->replace)
  147. set_closure_fn(cl, bch_data_insert_error, op->wq);
  148. else
  149. set_closure_fn(cl, NULL, NULL);
  150. }
  151. bch_bbio_endio(op->c, bio, bio->bi_status, "writing data to cache");
  152. }
  153. static void bch_data_insert_start(struct closure *cl)
  154. {
  155. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  156. struct bio *bio = op->bio, *n;
  157. if (op->bypass)
  158. return bch_data_invalidate(cl);
  159. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0)
  160. wake_up_gc(op->c);
  161. /*
  162. * Journal writes are marked REQ_PREFLUSH; if the original write was a
  163. * flush, it'll wait on the journal write.
  164. */
  165. bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA);
  166. do {
  167. unsigned i;
  168. struct bkey *k;
  169. struct bio_set *split = op->c->bio_split;
  170. /* 1 for the device pointer and 1 for the chksum */
  171. if (bch_keylist_realloc(&op->insert_keys,
  172. 3 + (op->csum ? 1 : 0),
  173. op->c)) {
  174. continue_at(cl, bch_data_insert_keys, op->wq);
  175. return;
  176. }
  177. k = op->insert_keys.top;
  178. bkey_init(k);
  179. SET_KEY_INODE(k, op->inode);
  180. SET_KEY_OFFSET(k, bio->bi_iter.bi_sector);
  181. if (!bch_alloc_sectors(op->c, k, bio_sectors(bio),
  182. op->write_point, op->write_prio,
  183. op->writeback))
  184. goto err;
  185. n = bio_next_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  186. n->bi_end_io = bch_data_insert_endio;
  187. n->bi_private = cl;
  188. if (op->writeback) {
  189. SET_KEY_DIRTY(k, true);
  190. for (i = 0; i < KEY_PTRS(k); i++)
  191. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  192. GC_MARK_DIRTY);
  193. }
  194. SET_KEY_CSUM(k, op->csum);
  195. if (KEY_CSUM(k))
  196. bio_csum(n, k);
  197. trace_bcache_cache_insert(k);
  198. bch_keylist_push(&op->insert_keys);
  199. bio_set_op_attrs(n, REQ_OP_WRITE, 0);
  200. bch_submit_bbio(n, op->c, k, 0);
  201. } while (n != bio);
  202. op->insert_data_done = true;
  203. continue_at(cl, bch_data_insert_keys, op->wq);
  204. return;
  205. err:
  206. /* bch_alloc_sectors() blocks if s->writeback = true */
  207. BUG_ON(op->writeback);
  208. /*
  209. * But if it's not a writeback write we'd rather just bail out if
  210. * there aren't any buckets ready to write to - it might take awhile and
  211. * we might be starving btree writes for gc or something.
  212. */
  213. if (!op->replace) {
  214. /*
  215. * Writethrough write: We can't complete the write until we've
  216. * updated the index. But we don't want to delay the write while
  217. * we wait for buckets to be freed up, so just invalidate the
  218. * rest of the write.
  219. */
  220. op->bypass = true;
  221. return bch_data_invalidate(cl);
  222. } else {
  223. /*
  224. * From a cache miss, we can just insert the keys for the data
  225. * we have written or bail out if we didn't do anything.
  226. */
  227. op->insert_data_done = true;
  228. bio_put(bio);
  229. if (!bch_keylist_empty(&op->insert_keys))
  230. continue_at(cl, bch_data_insert_keys, op->wq);
  231. else
  232. closure_return(cl);
  233. }
  234. }
  235. /**
  236. * bch_data_insert - stick some data in the cache
  237. *
  238. * This is the starting point for any data to end up in a cache device; it could
  239. * be from a normal write, or a writeback write, or a write to a flash only
  240. * volume - it's also used by the moving garbage collector to compact data in
  241. * mostly empty buckets.
  242. *
  243. * It first writes the data to the cache, creating a list of keys to be inserted
  244. * (if the data had to be fragmented there will be multiple keys); after the
  245. * data is written it calls bch_journal, and after the keys have been added to
  246. * the next journal write they're inserted into the btree.
  247. *
  248. * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
  249. * and op->inode is used for the key inode.
  250. *
  251. * If s->bypass is true, instead of inserting the data it invalidates the
  252. * region of the cache represented by s->cache_bio and op->inode.
  253. */
  254. void bch_data_insert(struct closure *cl)
  255. {
  256. struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
  257. trace_bcache_write(op->c, op->inode, op->bio,
  258. op->writeback, op->bypass);
  259. bch_keylist_init(&op->insert_keys);
  260. bio_get(op->bio);
  261. bch_data_insert_start(cl);
  262. }
  263. /* Congested? */
  264. unsigned bch_get_congested(struct cache_set *c)
  265. {
  266. int i;
  267. long rand;
  268. if (!c->congested_read_threshold_us &&
  269. !c->congested_write_threshold_us)
  270. return 0;
  271. i = (local_clock_us() - c->congested_last_us) / 1024;
  272. if (i < 0)
  273. return 0;
  274. i += atomic_read(&c->congested);
  275. if (i >= 0)
  276. return 0;
  277. i += CONGESTED_MAX;
  278. if (i > 0)
  279. i = fract_exp_two(i, 6);
  280. rand = get_random_int();
  281. i -= bitmap_weight(&rand, BITS_PER_LONG);
  282. return i > 0 ? i : 1;
  283. }
  284. static void add_sequential(struct task_struct *t)
  285. {
  286. ewma_add(t->sequential_io_avg,
  287. t->sequential_io, 8, 0);
  288. t->sequential_io = 0;
  289. }
  290. static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
  291. {
  292. return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
  293. }
  294. static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
  295. {
  296. struct cache_set *c = dc->disk.c;
  297. unsigned mode = cache_mode(dc, bio);
  298. unsigned sectors, congested = bch_get_congested(c);
  299. struct task_struct *task = current;
  300. struct io *i;
  301. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  302. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  303. (bio_op(bio) == REQ_OP_DISCARD))
  304. goto skip;
  305. if (mode == CACHE_MODE_NONE ||
  306. (mode == CACHE_MODE_WRITEAROUND &&
  307. op_is_write(bio_op(bio))))
  308. goto skip;
  309. if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
  310. bio_sectors(bio) & (c->sb.block_size - 1)) {
  311. pr_debug("skipping unaligned io");
  312. goto skip;
  313. }
  314. if (bypass_torture_test(dc)) {
  315. if ((get_random_int() & 3) == 3)
  316. goto skip;
  317. else
  318. goto rescale;
  319. }
  320. if (!congested && !dc->sequential_cutoff)
  321. goto rescale;
  322. spin_lock(&dc->io_lock);
  323. hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash)
  324. if (i->last == bio->bi_iter.bi_sector &&
  325. time_before(jiffies, i->jiffies))
  326. goto found;
  327. i = list_first_entry(&dc->io_lru, struct io, lru);
  328. add_sequential(task);
  329. i->sequential = 0;
  330. found:
  331. if (i->sequential + bio->bi_iter.bi_size > i->sequential)
  332. i->sequential += bio->bi_iter.bi_size;
  333. i->last = bio_end_sector(bio);
  334. i->jiffies = jiffies + msecs_to_jiffies(5000);
  335. task->sequential_io = i->sequential;
  336. hlist_del(&i->hash);
  337. hlist_add_head(&i->hash, iohash(dc, i->last));
  338. list_move_tail(&i->lru, &dc->io_lru);
  339. spin_unlock(&dc->io_lock);
  340. sectors = max(task->sequential_io,
  341. task->sequential_io_avg) >> 9;
  342. if (dc->sequential_cutoff &&
  343. sectors >= dc->sequential_cutoff >> 9) {
  344. trace_bcache_bypass_sequential(bio);
  345. goto skip;
  346. }
  347. if (congested && sectors >= congested) {
  348. trace_bcache_bypass_congested(bio);
  349. goto skip;
  350. }
  351. rescale:
  352. bch_rescale_priorities(c, bio_sectors(bio));
  353. return false;
  354. skip:
  355. bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
  356. return true;
  357. }
  358. /* Cache lookup */
  359. struct search {
  360. /* Stack frame for bio_complete */
  361. struct closure cl;
  362. struct bbio bio;
  363. struct bio *orig_bio;
  364. struct bio *cache_miss;
  365. struct bcache_device *d;
  366. unsigned insert_bio_sectors;
  367. unsigned recoverable:1;
  368. unsigned write:1;
  369. unsigned read_dirty_data:1;
  370. unsigned long start_time;
  371. struct btree_op op;
  372. struct data_insert_op iop;
  373. };
  374. static void bch_cache_read_endio(struct bio *bio)
  375. {
  376. struct bbio *b = container_of(bio, struct bbio, bio);
  377. struct closure *cl = bio->bi_private;
  378. struct search *s = container_of(cl, struct search, cl);
  379. /*
  380. * If the bucket was reused while our bio was in flight, we might have
  381. * read the wrong data. Set s->error but not error so it doesn't get
  382. * counted against the cache device, but we'll still reread the data
  383. * from the backing device.
  384. */
  385. if (bio->bi_status)
  386. s->iop.status = bio->bi_status;
  387. else if (!KEY_DIRTY(&b->key) &&
  388. ptr_stale(s->iop.c, &b->key, 0)) {
  389. atomic_long_inc(&s->iop.c->cache_read_races);
  390. s->iop.status = BLK_STS_IOERR;
  391. }
  392. bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache");
  393. }
  394. /*
  395. * Read from a single key, handling the initial cache miss if the key starts in
  396. * the middle of the bio
  397. */
  398. static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
  399. {
  400. struct search *s = container_of(op, struct search, op);
  401. struct bio *n, *bio = &s->bio.bio;
  402. struct bkey *bio_key;
  403. unsigned ptr;
  404. if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
  405. return MAP_CONTINUE;
  406. if (KEY_INODE(k) != s->iop.inode ||
  407. KEY_START(k) > bio->bi_iter.bi_sector) {
  408. unsigned bio_sectors = bio_sectors(bio);
  409. unsigned sectors = KEY_INODE(k) == s->iop.inode
  410. ? min_t(uint64_t, INT_MAX,
  411. KEY_START(k) - bio->bi_iter.bi_sector)
  412. : INT_MAX;
  413. int ret = s->d->cache_miss(b, s, bio, sectors);
  414. if (ret != MAP_CONTINUE)
  415. return ret;
  416. /* if this was a complete miss we shouldn't get here */
  417. BUG_ON(bio_sectors <= sectors);
  418. }
  419. if (!KEY_SIZE(k))
  420. return MAP_CONTINUE;
  421. /* XXX: figure out best pointer - for multiple cache devices */
  422. ptr = 0;
  423. PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
  424. if (KEY_DIRTY(k))
  425. s->read_dirty_data = true;
  426. n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
  427. KEY_OFFSET(k) - bio->bi_iter.bi_sector),
  428. GFP_NOIO, s->d->bio_split);
  429. bio_key = &container_of(n, struct bbio, bio)->key;
  430. bch_bkey_copy_single_ptr(bio_key, k, ptr);
  431. bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
  432. bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
  433. n->bi_end_io = bch_cache_read_endio;
  434. n->bi_private = &s->cl;
  435. /*
  436. * The bucket we're reading from might be reused while our bio
  437. * is in flight, and we could then end up reading the wrong
  438. * data.
  439. *
  440. * We guard against this by checking (in cache_read_endio()) if
  441. * the pointer is stale again; if so, we treat it as an error
  442. * and reread from the backing device (but we don't pass that
  443. * error up anywhere).
  444. */
  445. __bch_submit_bbio(n, b->c);
  446. return n == bio ? MAP_DONE : MAP_CONTINUE;
  447. }
  448. static void cache_lookup(struct closure *cl)
  449. {
  450. struct search *s = container_of(cl, struct search, iop.cl);
  451. struct bio *bio = &s->bio.bio;
  452. int ret;
  453. bch_btree_op_init(&s->op, -1);
  454. ret = bch_btree_map_keys(&s->op, s->iop.c,
  455. &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0),
  456. cache_lookup_fn, MAP_END_KEY);
  457. if (ret == -EAGAIN) {
  458. continue_at(cl, cache_lookup, bcache_wq);
  459. return;
  460. }
  461. closure_return(cl);
  462. }
  463. /* Common code for the make_request functions */
  464. static void request_endio(struct bio *bio)
  465. {
  466. struct closure *cl = bio->bi_private;
  467. if (bio->bi_status) {
  468. struct search *s = container_of(cl, struct search, cl);
  469. s->iop.status = bio->bi_status;
  470. /* Only cache read errors are recoverable */
  471. s->recoverable = false;
  472. }
  473. bio_put(bio);
  474. closure_put(cl);
  475. }
  476. static void bio_complete(struct search *s)
  477. {
  478. if (s->orig_bio) {
  479. struct request_queue *q = s->orig_bio->bi_disk->queue;
  480. generic_end_io_acct(q, bio_data_dir(s->orig_bio),
  481. &s->d->disk->part0, s->start_time);
  482. trace_bcache_request_end(s->d, s->orig_bio);
  483. s->orig_bio->bi_status = s->iop.status;
  484. bio_endio(s->orig_bio);
  485. s->orig_bio = NULL;
  486. }
  487. }
  488. static void do_bio_hook(struct search *s, struct bio *orig_bio)
  489. {
  490. struct bio *bio = &s->bio.bio;
  491. bio_init(bio, NULL, 0);
  492. __bio_clone_fast(bio, orig_bio);
  493. bio->bi_end_io = request_endio;
  494. bio->bi_private = &s->cl;
  495. bio_cnt_set(bio, 3);
  496. }
  497. static void search_free(struct closure *cl)
  498. {
  499. struct search *s = container_of(cl, struct search, cl);
  500. bio_complete(s);
  501. if (s->iop.bio)
  502. bio_put(s->iop.bio);
  503. closure_debug_destroy(cl);
  504. mempool_free(s, s->d->c->search);
  505. }
  506. static inline struct search *search_alloc(struct bio *bio,
  507. struct bcache_device *d)
  508. {
  509. struct search *s;
  510. s = mempool_alloc(d->c->search, GFP_NOIO);
  511. closure_init(&s->cl, NULL);
  512. do_bio_hook(s, bio);
  513. s->orig_bio = bio;
  514. s->cache_miss = NULL;
  515. s->d = d;
  516. s->recoverable = 1;
  517. s->write = op_is_write(bio_op(bio));
  518. s->read_dirty_data = 0;
  519. s->start_time = jiffies;
  520. s->iop.c = d->c;
  521. s->iop.bio = NULL;
  522. s->iop.inode = d->id;
  523. s->iop.write_point = hash_long((unsigned long) current, 16);
  524. s->iop.write_prio = 0;
  525. s->iop.status = 0;
  526. s->iop.flags = 0;
  527. s->iop.flush_journal = op_is_flush(bio->bi_opf);
  528. s->iop.wq = bcache_wq;
  529. return s;
  530. }
  531. /* Cached devices */
  532. static void cached_dev_bio_complete(struct closure *cl)
  533. {
  534. struct search *s = container_of(cl, struct search, cl);
  535. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  536. search_free(cl);
  537. cached_dev_put(dc);
  538. }
  539. /* Process reads */
  540. static void cached_dev_cache_miss_done(struct closure *cl)
  541. {
  542. struct search *s = container_of(cl, struct search, cl);
  543. if (s->iop.replace_collision)
  544. bch_mark_cache_miss_collision(s->iop.c, s->d);
  545. if (s->iop.bio)
  546. bio_free_pages(s->iop.bio);
  547. cached_dev_bio_complete(cl);
  548. }
  549. static void cached_dev_read_error(struct closure *cl)
  550. {
  551. struct search *s = container_of(cl, struct search, cl);
  552. struct bio *bio = &s->bio.bio;
  553. if (s->recoverable) {
  554. /* Retry from the backing device: */
  555. trace_bcache_read_retry(s->orig_bio);
  556. s->iop.status = 0;
  557. do_bio_hook(s, s->orig_bio);
  558. /* XXX: invalidate cache */
  559. closure_bio_submit(bio, cl);
  560. }
  561. continue_at(cl, cached_dev_cache_miss_done, NULL);
  562. }
  563. static void cached_dev_read_done(struct closure *cl)
  564. {
  565. struct search *s = container_of(cl, struct search, cl);
  566. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  567. /*
  568. * We had a cache miss; cache_bio now contains data ready to be inserted
  569. * into the cache.
  570. *
  571. * First, we copy the data we just read from cache_bio's bounce buffers
  572. * to the buffers the original bio pointed to:
  573. */
  574. if (s->iop.bio) {
  575. bio_reset(s->iop.bio);
  576. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  577. bio_copy_dev(s->iop.bio, s->cache_miss);
  578. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  579. bch_bio_map(s->iop.bio, NULL);
  580. bio_copy_data(s->cache_miss, s->iop.bio);
  581. bio_put(s->cache_miss);
  582. s->cache_miss = NULL;
  583. }
  584. if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data)
  585. bch_data_verify(dc, s->orig_bio);
  586. bio_complete(s);
  587. if (s->iop.bio &&
  588. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  589. BUG_ON(!s->iop.replace);
  590. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  591. }
  592. continue_at(cl, cached_dev_cache_miss_done, NULL);
  593. }
  594. static void cached_dev_read_done_bh(struct closure *cl)
  595. {
  596. struct search *s = container_of(cl, struct search, cl);
  597. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  598. bch_mark_cache_accounting(s->iop.c, s->d,
  599. !s->cache_miss, s->iop.bypass);
  600. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  601. if (s->iop.status)
  602. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  603. else if (s->iop.bio || verify(dc, &s->bio.bio))
  604. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  605. else
  606. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  607. }
  608. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  609. struct bio *bio, unsigned sectors)
  610. {
  611. int ret = MAP_CONTINUE;
  612. unsigned reada = 0;
  613. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  614. struct bio *miss, *cache_bio;
  615. if (s->cache_miss || s->iop.bypass) {
  616. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  617. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  618. goto out_submit;
  619. }
  620. if (!(bio->bi_opf & REQ_RAHEAD) &&
  621. !(bio->bi_opf & REQ_META) &&
  622. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  623. reada = min_t(sector_t, dc->readahead >> 9,
  624. get_capacity(bio->bi_disk) - bio_end_sector(bio));
  625. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  626. s->iop.replace_key = KEY(s->iop.inode,
  627. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  628. s->insert_bio_sectors);
  629. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  630. if (ret)
  631. return ret;
  632. s->iop.replace = true;
  633. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  634. /* btree_search_recurse()'s btree iterator is no good anymore */
  635. ret = miss == bio ? MAP_DONE : -EINTR;
  636. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  637. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  638. dc->disk.bio_split);
  639. if (!cache_bio)
  640. goto out_submit;
  641. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  642. bio_copy_dev(cache_bio, miss);
  643. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  644. cache_bio->bi_end_io = request_endio;
  645. cache_bio->bi_private = &s->cl;
  646. bch_bio_map(cache_bio, NULL);
  647. if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  648. goto out_put;
  649. if (reada)
  650. bch_mark_cache_readahead(s->iop.c, s->d);
  651. s->cache_miss = miss;
  652. s->iop.bio = cache_bio;
  653. bio_get(cache_bio);
  654. closure_bio_submit(cache_bio, &s->cl);
  655. return ret;
  656. out_put:
  657. bio_put(cache_bio);
  658. out_submit:
  659. miss->bi_end_io = request_endio;
  660. miss->bi_private = &s->cl;
  661. closure_bio_submit(miss, &s->cl);
  662. return ret;
  663. }
  664. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  665. {
  666. struct closure *cl = &s->cl;
  667. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  668. continue_at(cl, cached_dev_read_done_bh, NULL);
  669. }
  670. /* Process writes */
  671. static void cached_dev_write_complete(struct closure *cl)
  672. {
  673. struct search *s = container_of(cl, struct search, cl);
  674. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  675. up_read_non_owner(&dc->writeback_lock);
  676. cached_dev_bio_complete(cl);
  677. }
  678. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  679. {
  680. struct closure *cl = &s->cl;
  681. struct bio *bio = &s->bio.bio;
  682. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  683. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  684. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  685. down_read_non_owner(&dc->writeback_lock);
  686. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  687. /*
  688. * We overlap with some dirty data undergoing background
  689. * writeback, force this write to writeback
  690. */
  691. s->iop.bypass = false;
  692. s->iop.writeback = true;
  693. }
  694. /*
  695. * Discards aren't _required_ to do anything, so skipping if
  696. * check_overlapping returned true is ok
  697. *
  698. * But check_overlapping drops dirty keys for which io hasn't started,
  699. * so we still want to call it.
  700. */
  701. if (bio_op(bio) == REQ_OP_DISCARD)
  702. s->iop.bypass = true;
  703. if (should_writeback(dc, s->orig_bio,
  704. cache_mode(dc, bio),
  705. s->iop.bypass)) {
  706. s->iop.bypass = false;
  707. s->iop.writeback = true;
  708. }
  709. if (s->iop.bypass) {
  710. s->iop.bio = s->orig_bio;
  711. bio_get(s->iop.bio);
  712. if ((bio_op(bio) != REQ_OP_DISCARD) ||
  713. blk_queue_discard(bdev_get_queue(dc->bdev)))
  714. closure_bio_submit(bio, cl);
  715. } else if (s->iop.writeback) {
  716. bch_writeback_add(dc);
  717. s->iop.bio = bio;
  718. if (bio->bi_opf & REQ_PREFLUSH) {
  719. /* Also need to send a flush to the backing device */
  720. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  721. dc->disk.bio_split);
  722. bio_copy_dev(flush, bio);
  723. flush->bi_end_io = request_endio;
  724. flush->bi_private = cl;
  725. flush->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  726. closure_bio_submit(flush, cl);
  727. }
  728. } else {
  729. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  730. closure_bio_submit(bio, cl);
  731. }
  732. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  733. continue_at(cl, cached_dev_write_complete, NULL);
  734. }
  735. static void cached_dev_nodata(struct closure *cl)
  736. {
  737. struct search *s = container_of(cl, struct search, cl);
  738. struct bio *bio = &s->bio.bio;
  739. if (s->iop.flush_journal)
  740. bch_journal_meta(s->iop.c, cl);
  741. /* If it's a flush, we send the flush to the backing device too */
  742. closure_bio_submit(bio, cl);
  743. continue_at(cl, cached_dev_bio_complete, NULL);
  744. }
  745. /* Cached devices - read & write stuff */
  746. static blk_qc_t cached_dev_make_request(struct request_queue *q,
  747. struct bio *bio)
  748. {
  749. struct search *s;
  750. struct bcache_device *d = bio->bi_disk->private_data;
  751. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  752. int rw = bio_data_dir(bio);
  753. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  754. bio_set_dev(bio, dc->bdev);
  755. bio->bi_iter.bi_sector += dc->sb.data_offset;
  756. if (cached_dev_get(dc)) {
  757. s = search_alloc(bio, d);
  758. trace_bcache_request_start(s->d, bio);
  759. if (!bio->bi_iter.bi_size) {
  760. /*
  761. * can't call bch_journal_meta from under
  762. * generic_make_request
  763. */
  764. continue_at_nobarrier(&s->cl,
  765. cached_dev_nodata,
  766. bcache_wq);
  767. } else {
  768. s->iop.bypass = check_should_bypass(dc, bio);
  769. if (rw)
  770. cached_dev_write(dc, s);
  771. else
  772. cached_dev_read(dc, s);
  773. }
  774. } else {
  775. if ((bio_op(bio) == REQ_OP_DISCARD) &&
  776. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  777. bio_endio(bio);
  778. else
  779. generic_make_request(bio);
  780. }
  781. return BLK_QC_T_NONE;
  782. }
  783. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  784. unsigned int cmd, unsigned long arg)
  785. {
  786. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  787. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  788. }
  789. static int cached_dev_congested(void *data, int bits)
  790. {
  791. struct bcache_device *d = data;
  792. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  793. struct request_queue *q = bdev_get_queue(dc->bdev);
  794. int ret = 0;
  795. if (bdi_congested(q->backing_dev_info, bits))
  796. return 1;
  797. if (cached_dev_get(dc)) {
  798. unsigned i;
  799. struct cache *ca;
  800. for_each_cache(ca, d->c, i) {
  801. q = bdev_get_queue(ca->bdev);
  802. ret |= bdi_congested(q->backing_dev_info, bits);
  803. }
  804. cached_dev_put(dc);
  805. }
  806. return ret;
  807. }
  808. void bch_cached_dev_request_init(struct cached_dev *dc)
  809. {
  810. struct gendisk *g = dc->disk.disk;
  811. g->queue->make_request_fn = cached_dev_make_request;
  812. g->queue->backing_dev_info->congested_fn = cached_dev_congested;
  813. dc->disk.cache_miss = cached_dev_cache_miss;
  814. dc->disk.ioctl = cached_dev_ioctl;
  815. }
  816. /* Flash backed devices */
  817. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  818. struct bio *bio, unsigned sectors)
  819. {
  820. unsigned bytes = min(sectors, bio_sectors(bio)) << 9;
  821. swap(bio->bi_iter.bi_size, bytes);
  822. zero_fill_bio(bio);
  823. swap(bio->bi_iter.bi_size, bytes);
  824. bio_advance(bio, bytes);
  825. if (!bio->bi_iter.bi_size)
  826. return MAP_DONE;
  827. return MAP_CONTINUE;
  828. }
  829. static void flash_dev_nodata(struct closure *cl)
  830. {
  831. struct search *s = container_of(cl, struct search, cl);
  832. if (s->iop.flush_journal)
  833. bch_journal_meta(s->iop.c, cl);
  834. continue_at(cl, search_free, NULL);
  835. }
  836. static blk_qc_t flash_dev_make_request(struct request_queue *q,
  837. struct bio *bio)
  838. {
  839. struct search *s;
  840. struct closure *cl;
  841. struct bcache_device *d = bio->bi_disk->private_data;
  842. int rw = bio_data_dir(bio);
  843. generic_start_io_acct(q, rw, bio_sectors(bio), &d->disk->part0);
  844. s = search_alloc(bio, d);
  845. cl = &s->cl;
  846. bio = &s->bio.bio;
  847. trace_bcache_request_start(s->d, bio);
  848. if (!bio->bi_iter.bi_size) {
  849. /*
  850. * can't call bch_journal_meta from under
  851. * generic_make_request
  852. */
  853. continue_at_nobarrier(&s->cl,
  854. flash_dev_nodata,
  855. bcache_wq);
  856. return BLK_QC_T_NONE;
  857. } else if (rw) {
  858. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  859. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  860. &KEY(d->id, bio_end_sector(bio), 0));
  861. s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0;
  862. s->iop.writeback = true;
  863. s->iop.bio = bio;
  864. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  865. } else {
  866. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  867. }
  868. continue_at(cl, search_free, NULL);
  869. return BLK_QC_T_NONE;
  870. }
  871. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  872. unsigned int cmd, unsigned long arg)
  873. {
  874. return -ENOTTY;
  875. }
  876. static int flash_dev_congested(void *data, int bits)
  877. {
  878. struct bcache_device *d = data;
  879. struct request_queue *q;
  880. struct cache *ca;
  881. unsigned i;
  882. int ret = 0;
  883. for_each_cache(ca, d->c, i) {
  884. q = bdev_get_queue(ca->bdev);
  885. ret |= bdi_congested(q->backing_dev_info, bits);
  886. }
  887. return ret;
  888. }
  889. void bch_flash_dev_request_init(struct bcache_device *d)
  890. {
  891. struct gendisk *g = d->disk;
  892. g->queue->make_request_fn = flash_dev_make_request;
  893. g->queue->backing_dev_info->congested_fn = flash_dev_congested;
  894. d->cache_miss = flash_dev_cache_miss;
  895. d->ioctl = flash_dev_ioctl;
  896. }
  897. void bch_request_exit(void)
  898. {
  899. if (bch_search_cache)
  900. kmem_cache_destroy(bch_search_cache);
  901. }
  902. int __init bch_request_init(void)
  903. {
  904. bch_search_cache = KMEM_CACHE(search, 0);
  905. if (!bch_search_cache)
  906. return -ENOMEM;
  907. return 0;
  908. }