request.c 28 KB

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