request.c 28 KB

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