request.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. int cpu, rw = bio_data_dir(s->orig_bio);
  479. unsigned long duration = jiffies - s->start_time;
  480. cpu = part_stat_lock();
  481. part_round_stats(cpu, &s->d->disk->part0);
  482. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  483. part_stat_unlock();
  484. trace_bcache_request_end(s->d, s->orig_bio);
  485. bio_endio(s->orig_bio, s->iop.error);
  486. s->orig_bio = NULL;
  487. }
  488. }
  489. static void do_bio_hook(struct search *s, struct bio *orig_bio)
  490. {
  491. struct bio *bio = &s->bio.bio;
  492. bio_init(bio);
  493. __bio_clone_fast(bio, orig_bio);
  494. bio->bi_end_io = request_endio;
  495. bio->bi_private = &s->cl;
  496. atomic_set(&bio->bi_cnt, 3);
  497. }
  498. static void search_free(struct closure *cl)
  499. {
  500. struct search *s = container_of(cl, struct search, cl);
  501. bio_complete(s);
  502. if (s->iop.bio)
  503. bio_put(s->iop.bio);
  504. closure_debug_destroy(cl);
  505. mempool_free(s, s->d->c->search);
  506. }
  507. static inline struct search *search_alloc(struct bio *bio,
  508. struct bcache_device *d)
  509. {
  510. struct search *s;
  511. s = mempool_alloc(d->c->search, GFP_NOIO);
  512. closure_init(&s->cl, NULL);
  513. do_bio_hook(s, bio);
  514. s->orig_bio = bio;
  515. s->cache_miss = NULL;
  516. s->d = d;
  517. s->recoverable = 1;
  518. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  519. s->read_dirty_data = 0;
  520. s->start_time = jiffies;
  521. s->iop.c = d->c;
  522. s->iop.bio = NULL;
  523. s->iop.inode = d->id;
  524. s->iop.write_point = hash_long((unsigned long) current, 16);
  525. s->iop.write_prio = 0;
  526. s->iop.error = 0;
  527. s->iop.flags = 0;
  528. s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0;
  529. s->iop.wq = bcache_wq;
  530. return s;
  531. }
  532. /* Cached devices */
  533. static void cached_dev_bio_complete(struct closure *cl)
  534. {
  535. struct search *s = container_of(cl, struct search, cl);
  536. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  537. search_free(cl);
  538. cached_dev_put(dc);
  539. }
  540. /* Process reads */
  541. static void cached_dev_cache_miss_done(struct closure *cl)
  542. {
  543. struct search *s = container_of(cl, struct search, cl);
  544. if (s->iop.replace_collision)
  545. bch_mark_cache_miss_collision(s->iop.c, s->d);
  546. if (s->iop.bio) {
  547. int i;
  548. struct bio_vec *bv;
  549. bio_for_each_segment_all(bv, s->iop.bio, i)
  550. __free_page(bv->bv_page);
  551. }
  552. cached_dev_bio_complete(cl);
  553. }
  554. static void cached_dev_read_error(struct closure *cl)
  555. {
  556. struct search *s = container_of(cl, struct search, cl);
  557. struct bio *bio = &s->bio.bio;
  558. if (s->recoverable) {
  559. /* Retry from the backing device: */
  560. trace_bcache_read_retry(s->orig_bio);
  561. s->iop.error = 0;
  562. do_bio_hook(s, s->orig_bio);
  563. /* XXX: invalidate cache */
  564. closure_bio_submit(bio, cl, s->d);
  565. }
  566. continue_at(cl, cached_dev_cache_miss_done, NULL);
  567. }
  568. static void cached_dev_read_done(struct closure *cl)
  569. {
  570. struct search *s = container_of(cl, struct search, cl);
  571. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  572. /*
  573. * We had a cache miss; cache_bio now contains data ready to be inserted
  574. * into the cache.
  575. *
  576. * First, we copy the data we just read from cache_bio's bounce buffers
  577. * to the buffers the original bio pointed to:
  578. */
  579. if (s->iop.bio) {
  580. bio_reset(s->iop.bio);
  581. s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector;
  582. s->iop.bio->bi_bdev = s->cache_miss->bi_bdev;
  583. s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  584. bch_bio_map(s->iop.bio, NULL);
  585. bio_copy_data(s->cache_miss, s->iop.bio);
  586. bio_put(s->cache_miss);
  587. s->cache_miss = NULL;
  588. }
  589. if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data)
  590. bch_data_verify(dc, s->orig_bio);
  591. bio_complete(s);
  592. if (s->iop.bio &&
  593. !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
  594. BUG_ON(!s->iop.replace);
  595. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  596. }
  597. continue_at(cl, cached_dev_cache_miss_done, NULL);
  598. }
  599. static void cached_dev_read_done_bh(struct closure *cl)
  600. {
  601. struct search *s = container_of(cl, struct search, cl);
  602. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  603. bch_mark_cache_accounting(s->iop.c, s->d,
  604. !s->cache_miss, s->iop.bypass);
  605. trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
  606. if (s->iop.error)
  607. continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
  608. else if (s->iop.bio || verify(dc, &s->bio.bio))
  609. continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
  610. else
  611. continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
  612. }
  613. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  614. struct bio *bio, unsigned sectors)
  615. {
  616. int ret = MAP_CONTINUE;
  617. unsigned reada = 0;
  618. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  619. struct bio *miss, *cache_bio;
  620. if (s->cache_miss || s->iop.bypass) {
  621. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  622. ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
  623. goto out_submit;
  624. }
  625. if (!(bio->bi_rw & REQ_RAHEAD) &&
  626. !(bio->bi_rw & REQ_META) &&
  627. s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
  628. reada = min_t(sector_t, dc->readahead >> 9,
  629. bdev_sectors(bio->bi_bdev) - bio_end_sector(bio));
  630. s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
  631. s->iop.replace_key = KEY(s->iop.inode,
  632. bio->bi_iter.bi_sector + s->insert_bio_sectors,
  633. s->insert_bio_sectors);
  634. ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
  635. if (ret)
  636. return ret;
  637. s->iop.replace = true;
  638. miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  639. /* btree_search_recurse()'s btree iterator is no good anymore */
  640. ret = miss == bio ? MAP_DONE : -EINTR;
  641. cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  642. DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
  643. dc->disk.bio_split);
  644. if (!cache_bio)
  645. goto out_submit;
  646. cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector;
  647. cache_bio->bi_bdev = miss->bi_bdev;
  648. cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9;
  649. cache_bio->bi_end_io = request_endio;
  650. cache_bio->bi_private = &s->cl;
  651. bch_bio_map(cache_bio, NULL);
  652. if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
  653. goto out_put;
  654. if (reada)
  655. bch_mark_cache_readahead(s->iop.c, s->d);
  656. s->cache_miss = miss;
  657. s->iop.bio = cache_bio;
  658. bio_get(cache_bio);
  659. closure_bio_submit(cache_bio, &s->cl, s->d);
  660. return ret;
  661. out_put:
  662. bio_put(cache_bio);
  663. out_submit:
  664. miss->bi_end_io = request_endio;
  665. miss->bi_private = &s->cl;
  666. closure_bio_submit(miss, &s->cl, s->d);
  667. return ret;
  668. }
  669. static void cached_dev_read(struct cached_dev *dc, struct search *s)
  670. {
  671. struct closure *cl = &s->cl;
  672. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  673. continue_at(cl, cached_dev_read_done_bh, NULL);
  674. }
  675. /* Process writes */
  676. static void cached_dev_write_complete(struct closure *cl)
  677. {
  678. struct search *s = container_of(cl, struct search, cl);
  679. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  680. up_read_non_owner(&dc->writeback_lock);
  681. cached_dev_bio_complete(cl);
  682. }
  683. static void cached_dev_write(struct cached_dev *dc, struct search *s)
  684. {
  685. struct closure *cl = &s->cl;
  686. struct bio *bio = &s->bio.bio;
  687. struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0);
  688. struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
  689. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
  690. down_read_non_owner(&dc->writeback_lock);
  691. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  692. /*
  693. * We overlap with some dirty data undergoing background
  694. * writeback, force this write to writeback
  695. */
  696. s->iop.bypass = false;
  697. s->iop.writeback = true;
  698. }
  699. /*
  700. * Discards aren't _required_ to do anything, so skipping if
  701. * check_overlapping returned true is ok
  702. *
  703. * But check_overlapping drops dirty keys for which io hasn't started,
  704. * so we still want to call it.
  705. */
  706. if (bio->bi_rw & REQ_DISCARD)
  707. s->iop.bypass = true;
  708. if (should_writeback(dc, s->orig_bio,
  709. cache_mode(dc, bio),
  710. s->iop.bypass)) {
  711. s->iop.bypass = false;
  712. s->iop.writeback = true;
  713. }
  714. if (s->iop.bypass) {
  715. s->iop.bio = s->orig_bio;
  716. bio_get(s->iop.bio);
  717. if (!(bio->bi_rw & REQ_DISCARD) ||
  718. blk_queue_discard(bdev_get_queue(dc->bdev)))
  719. closure_bio_submit(bio, cl, s->d);
  720. } else if (s->iop.writeback) {
  721. bch_writeback_add(dc);
  722. s->iop.bio = bio;
  723. if (bio->bi_rw & REQ_FLUSH) {
  724. /* Also need to send a flush to the backing device */
  725. struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
  726. dc->disk.bio_split);
  727. flush->bi_rw = WRITE_FLUSH;
  728. flush->bi_bdev = bio->bi_bdev;
  729. flush->bi_end_io = request_endio;
  730. flush->bi_private = cl;
  731. closure_bio_submit(flush, cl, s->d);
  732. }
  733. } else {
  734. s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split);
  735. closure_bio_submit(bio, cl, s->d);
  736. }
  737. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  738. continue_at(cl, cached_dev_write_complete, NULL);
  739. }
  740. static void cached_dev_nodata(struct closure *cl)
  741. {
  742. struct search *s = container_of(cl, struct search, cl);
  743. struct bio *bio = &s->bio.bio;
  744. if (s->iop.flush_journal)
  745. bch_journal_meta(s->iop.c, cl);
  746. /* If it's a flush, we send the flush to the backing device too */
  747. closure_bio_submit(bio, cl, s->d);
  748. continue_at(cl, cached_dev_bio_complete, NULL);
  749. }
  750. /* Cached devices - read & write stuff */
  751. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  752. {
  753. struct search *s;
  754. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  755. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  756. int cpu, rw = bio_data_dir(bio);
  757. cpu = part_stat_lock();
  758. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  759. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  760. part_stat_unlock();
  761. bio->bi_bdev = dc->bdev;
  762. bio->bi_iter.bi_sector += dc->sb.data_offset;
  763. if (cached_dev_get(dc)) {
  764. s = search_alloc(bio, d);
  765. trace_bcache_request_start(s->d, bio);
  766. if (!bio->bi_iter.bi_size) {
  767. /*
  768. * can't call bch_journal_meta from under
  769. * generic_make_request
  770. */
  771. continue_at_nobarrier(&s->cl,
  772. cached_dev_nodata,
  773. bcache_wq);
  774. } else {
  775. s->iop.bypass = check_should_bypass(dc, bio);
  776. if (rw)
  777. cached_dev_write(dc, s);
  778. else
  779. cached_dev_read(dc, s);
  780. }
  781. } else {
  782. if ((bio->bi_rw & REQ_DISCARD) &&
  783. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  784. bio_endio(bio, 0);
  785. else
  786. bch_generic_make_request(bio, &d->bio_split_hook);
  787. }
  788. }
  789. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  790. unsigned int cmd, unsigned long arg)
  791. {
  792. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  793. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  794. }
  795. static int cached_dev_congested(void *data, int bits)
  796. {
  797. struct bcache_device *d = data;
  798. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  799. struct request_queue *q = bdev_get_queue(dc->bdev);
  800. int ret = 0;
  801. if (bdi_congested(&q->backing_dev_info, bits))
  802. return 1;
  803. if (cached_dev_get(dc)) {
  804. unsigned i;
  805. struct cache *ca;
  806. for_each_cache(ca, d->c, i) {
  807. q = bdev_get_queue(ca->bdev);
  808. ret |= bdi_congested(&q->backing_dev_info, bits);
  809. }
  810. cached_dev_put(dc);
  811. }
  812. return ret;
  813. }
  814. void bch_cached_dev_request_init(struct cached_dev *dc)
  815. {
  816. struct gendisk *g = dc->disk.disk;
  817. g->queue->make_request_fn = cached_dev_make_request;
  818. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  819. dc->disk.cache_miss = cached_dev_cache_miss;
  820. dc->disk.ioctl = cached_dev_ioctl;
  821. }
  822. /* Flash backed devices */
  823. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  824. struct bio *bio, unsigned sectors)
  825. {
  826. unsigned bytes = min(sectors, bio_sectors(bio)) << 9;
  827. swap(bio->bi_iter.bi_size, bytes);
  828. zero_fill_bio(bio);
  829. swap(bio->bi_iter.bi_size, bytes);
  830. bio_advance(bio, bytes);
  831. if (!bio->bi_iter.bi_size)
  832. return MAP_DONE;
  833. return MAP_CONTINUE;
  834. }
  835. static void flash_dev_nodata(struct closure *cl)
  836. {
  837. struct search *s = container_of(cl, struct search, cl);
  838. if (s->iop.flush_journal)
  839. bch_journal_meta(s->iop.c, cl);
  840. continue_at(cl, search_free, NULL);
  841. }
  842. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  843. {
  844. struct search *s;
  845. struct closure *cl;
  846. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  847. int cpu, rw = bio_data_dir(bio);
  848. cpu = part_stat_lock();
  849. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  850. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  851. part_stat_unlock();
  852. s = search_alloc(bio, d);
  853. cl = &s->cl;
  854. bio = &s->bio.bio;
  855. trace_bcache_request_start(s->d, bio);
  856. if (!bio->bi_iter.bi_size) {
  857. /*
  858. * can't call bch_journal_meta from under
  859. * generic_make_request
  860. */
  861. continue_at_nobarrier(&s->cl,
  862. flash_dev_nodata,
  863. bcache_wq);
  864. } else if (rw) {
  865. bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
  866. &KEY(d->id, bio->bi_iter.bi_sector, 0),
  867. &KEY(d->id, bio_end_sector(bio), 0));
  868. s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0;
  869. s->iop.writeback = true;
  870. s->iop.bio = bio;
  871. closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
  872. } else {
  873. closure_call(&s->iop.cl, cache_lookup, NULL, cl);
  874. }
  875. continue_at(cl, search_free, NULL);
  876. }
  877. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  878. unsigned int cmd, unsigned long arg)
  879. {
  880. return -ENOTTY;
  881. }
  882. static int flash_dev_congested(void *data, int bits)
  883. {
  884. struct bcache_device *d = data;
  885. struct request_queue *q;
  886. struct cache *ca;
  887. unsigned i;
  888. int ret = 0;
  889. for_each_cache(ca, d->c, i) {
  890. q = bdev_get_queue(ca->bdev);
  891. ret |= bdi_congested(&q->backing_dev_info, bits);
  892. }
  893. return ret;
  894. }
  895. void bch_flash_dev_request_init(struct bcache_device *d)
  896. {
  897. struct gendisk *g = d->disk;
  898. g->queue->make_request_fn = flash_dev_make_request;
  899. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  900. d->cache_miss = flash_dev_cache_miss;
  901. d->ioctl = flash_dev_ioctl;
  902. }
  903. void bch_request_exit(void)
  904. {
  905. if (bch_search_cache)
  906. kmem_cache_destroy(bch_search_cache);
  907. }
  908. int __init bch_request_init(void)
  909. {
  910. bch_search_cache = KMEM_CACHE(search, 0);
  911. if (!bch_search_cache)
  912. return -ENOMEM;
  913. return 0;
  914. }