request.c 32 KB

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