request.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 <linux/cgroup.h>
  13. #include <linux/module.h>
  14. #include <linux/hash.h>
  15. #include <linux/random.h>
  16. #include "blk-cgroup.h"
  17. #include <trace/events/bcache.h>
  18. #define CUTOFF_CACHE_ADD 95
  19. #define CUTOFF_CACHE_READA 90
  20. #define CUTOFF_WRITEBACK 50
  21. #define CUTOFF_WRITEBACK_SYNC 75
  22. struct kmem_cache *bch_search_cache;
  23. static void check_should_skip(struct cached_dev *, struct search *);
  24. /* Cgroup interface */
  25. #ifdef CONFIG_CGROUP_BCACHE
  26. static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 };
  27. static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup)
  28. {
  29. struct cgroup_subsys_state *css;
  30. return cgroup &&
  31. (css = cgroup_subsys_state(cgroup, bcache_subsys_id))
  32. ? container_of(css, struct bch_cgroup, css)
  33. : &bcache_default_cgroup;
  34. }
  35. struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio)
  36. {
  37. struct cgroup_subsys_state *css = bio->bi_css
  38. ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id)
  39. : task_subsys_state(current, bcache_subsys_id);
  40. return css
  41. ? container_of(css, struct bch_cgroup, css)
  42. : &bcache_default_cgroup;
  43. }
  44. static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft,
  45. struct file *file,
  46. char __user *buf, size_t nbytes, loff_t *ppos)
  47. {
  48. char tmp[1024];
  49. int len = snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes,
  50. cgroup_to_bcache(cgrp)->cache_mode + 1);
  51. if (len < 0)
  52. return len;
  53. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  54. }
  55. static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft,
  56. const char *buf)
  57. {
  58. int v = read_string_list(buf, bch_cache_modes);
  59. if (v < 0)
  60. return v;
  61. cgroup_to_bcache(cgrp)->cache_mode = v - 1;
  62. return 0;
  63. }
  64. static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft)
  65. {
  66. return cgroup_to_bcache(cgrp)->verify;
  67. }
  68. static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val)
  69. {
  70. cgroup_to_bcache(cgrp)->verify = val;
  71. return 0;
  72. }
  73. static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft)
  74. {
  75. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  76. return atomic_read(&bcachecg->stats.cache_hits);
  77. }
  78. static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft)
  79. {
  80. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  81. return atomic_read(&bcachecg->stats.cache_misses);
  82. }
  83. static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp,
  84. struct cftype *cft)
  85. {
  86. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  87. return atomic_read(&bcachecg->stats.cache_bypass_hits);
  88. }
  89. static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp,
  90. struct cftype *cft)
  91. {
  92. struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
  93. return atomic_read(&bcachecg->stats.cache_bypass_misses);
  94. }
  95. static struct cftype bch_files[] = {
  96. {
  97. .name = "cache_mode",
  98. .read = cache_mode_read,
  99. .write_string = cache_mode_write,
  100. },
  101. {
  102. .name = "verify",
  103. .read_u64 = bch_verify_read,
  104. .write_u64 = bch_verify_write,
  105. },
  106. {
  107. .name = "cache_hits",
  108. .read_u64 = bch_cache_hits_read,
  109. },
  110. {
  111. .name = "cache_misses",
  112. .read_u64 = bch_cache_misses_read,
  113. },
  114. {
  115. .name = "cache_bypass_hits",
  116. .read_u64 = bch_cache_bypass_hits_read,
  117. },
  118. {
  119. .name = "cache_bypass_misses",
  120. .read_u64 = bch_cache_bypass_misses_read,
  121. },
  122. { } /* terminate */
  123. };
  124. static void init_bch_cgroup(struct bch_cgroup *cg)
  125. {
  126. cg->cache_mode = -1;
  127. }
  128. static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup)
  129. {
  130. struct bch_cgroup *cg;
  131. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  132. if (!cg)
  133. return ERR_PTR(-ENOMEM);
  134. init_bch_cgroup(cg);
  135. return &cg->css;
  136. }
  137. static void bcachecg_destroy(struct cgroup *cgroup)
  138. {
  139. struct bch_cgroup *cg = cgroup_to_bcache(cgroup);
  140. free_css_id(&bcache_subsys, &cg->css);
  141. kfree(cg);
  142. }
  143. struct cgroup_subsys bcache_subsys = {
  144. .create = bcachecg_create,
  145. .destroy = bcachecg_destroy,
  146. .subsys_id = bcache_subsys_id,
  147. .name = "bcache",
  148. .module = THIS_MODULE,
  149. };
  150. EXPORT_SYMBOL_GPL(bcache_subsys);
  151. #endif
  152. static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
  153. {
  154. #ifdef CONFIG_CGROUP_BCACHE
  155. int r = bch_bio_to_cgroup(bio)->cache_mode;
  156. if (r >= 0)
  157. return r;
  158. #endif
  159. return BDEV_CACHE_MODE(&dc->sb);
  160. }
  161. static bool verify(struct cached_dev *dc, struct bio *bio)
  162. {
  163. #ifdef CONFIG_CGROUP_BCACHE
  164. if (bch_bio_to_cgroup(bio)->verify)
  165. return true;
  166. #endif
  167. return dc->verify;
  168. }
  169. static void bio_csum(struct bio *bio, struct bkey *k)
  170. {
  171. struct bio_vec *bv;
  172. uint64_t csum = 0;
  173. int i;
  174. bio_for_each_segment(bv, bio, i) {
  175. void *d = kmap(bv->bv_page) + bv->bv_offset;
  176. csum = crc64_update(csum, d, bv->bv_len);
  177. kunmap(bv->bv_page);
  178. }
  179. k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
  180. }
  181. /* Insert data into cache */
  182. static void bio_invalidate(struct closure *cl)
  183. {
  184. struct btree_op *op = container_of(cl, struct btree_op, cl);
  185. struct bio *bio = op->cache_bio;
  186. pr_debug("invalidating %i sectors from %llu",
  187. bio_sectors(bio), (uint64_t) bio->bi_sector);
  188. while (bio_sectors(bio)) {
  189. unsigned len = min(bio_sectors(bio), 1U << 14);
  190. if (bch_keylist_realloc(&op->keys, 0, op->c))
  191. goto out;
  192. bio->bi_sector += len;
  193. bio->bi_size -= len << 9;
  194. bch_keylist_add(&op->keys,
  195. &KEY(op->inode, bio->bi_sector, len));
  196. }
  197. op->insert_data_done = true;
  198. bio_put(bio);
  199. out:
  200. continue_at(cl, bch_journal, bcache_wq);
  201. }
  202. struct open_bucket {
  203. struct list_head list;
  204. struct task_struct *last;
  205. unsigned sectors_free;
  206. BKEY_PADDED(key);
  207. };
  208. void bch_open_buckets_free(struct cache_set *c)
  209. {
  210. struct open_bucket *b;
  211. while (!list_empty(&c->data_buckets)) {
  212. b = list_first_entry(&c->data_buckets,
  213. struct open_bucket, list);
  214. list_del(&b->list);
  215. kfree(b);
  216. }
  217. }
  218. int bch_open_buckets_alloc(struct cache_set *c)
  219. {
  220. int i;
  221. spin_lock_init(&c->data_bucket_lock);
  222. for (i = 0; i < 6; i++) {
  223. struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
  224. if (!b)
  225. return -ENOMEM;
  226. list_add(&b->list, &c->data_buckets);
  227. }
  228. return 0;
  229. }
  230. /*
  231. * We keep multiple buckets open for writes, and try to segregate different
  232. * write streams for better cache utilization: first we look for a bucket where
  233. * the last write to it was sequential with the current write, and failing that
  234. * we look for a bucket that was last used by the same task.
  235. *
  236. * The ideas is if you've got multiple tasks pulling data into the cache at the
  237. * same time, you'll get better cache utilization if you try to segregate their
  238. * data and preserve locality.
  239. *
  240. * For example, say you've starting Firefox at the same time you're copying a
  241. * bunch of files. Firefox will likely end up being fairly hot and stay in the
  242. * cache awhile, but the data you copied might not be; if you wrote all that
  243. * data to the same buckets it'd get invalidated at the same time.
  244. *
  245. * Both of those tasks will be doing fairly random IO so we can't rely on
  246. * detecting sequential IO to segregate their data, but going off of the task
  247. * should be a sane heuristic.
  248. */
  249. static struct open_bucket *pick_data_bucket(struct cache_set *c,
  250. const struct bkey *search,
  251. struct task_struct *task,
  252. struct bkey *alloc)
  253. {
  254. struct open_bucket *ret, *ret_task = NULL;
  255. list_for_each_entry_reverse(ret, &c->data_buckets, list)
  256. if (!bkey_cmp(&ret->key, search))
  257. goto found;
  258. else if (ret->last == task)
  259. ret_task = ret;
  260. ret = ret_task ?: list_first_entry(&c->data_buckets,
  261. struct open_bucket, list);
  262. found:
  263. if (!ret->sectors_free && KEY_PTRS(alloc)) {
  264. ret->sectors_free = c->sb.bucket_size;
  265. bkey_copy(&ret->key, alloc);
  266. bkey_init(alloc);
  267. }
  268. if (!ret->sectors_free)
  269. ret = NULL;
  270. return ret;
  271. }
  272. /*
  273. * Allocates some space in the cache to write to, and k to point to the newly
  274. * allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the
  275. * end of the newly allocated space).
  276. *
  277. * May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many
  278. * sectors were actually allocated.
  279. *
  280. * If s->writeback is true, will not fail.
  281. */
  282. static bool bch_alloc_sectors(struct bkey *k, unsigned sectors,
  283. struct search *s)
  284. {
  285. struct cache_set *c = s->op.c;
  286. struct open_bucket *b;
  287. BKEY_PADDED(key) alloc;
  288. struct closure cl, *w = NULL;
  289. unsigned i;
  290. if (s->writeback) {
  291. closure_init_stack(&cl);
  292. w = &cl;
  293. }
  294. /*
  295. * We might have to allocate a new bucket, which we can't do with a
  296. * spinlock held. So if we have to allocate, we drop the lock, allocate
  297. * and then retry. KEY_PTRS() indicates whether alloc points to
  298. * allocated bucket(s).
  299. */
  300. bkey_init(&alloc.key);
  301. spin_lock(&c->data_bucket_lock);
  302. while (!(b = pick_data_bucket(c, k, s->task, &alloc.key))) {
  303. unsigned watermark = s->op.write_prio
  304. ? WATERMARK_MOVINGGC
  305. : WATERMARK_NONE;
  306. spin_unlock(&c->data_bucket_lock);
  307. if (bch_bucket_alloc_set(c, watermark, &alloc.key, 1, w))
  308. return false;
  309. spin_lock(&c->data_bucket_lock);
  310. }
  311. /*
  312. * If we had to allocate, we might race and not need to allocate the
  313. * second time we call find_data_bucket(). If we allocated a bucket but
  314. * didn't use it, drop the refcount bch_bucket_alloc_set() took:
  315. */
  316. if (KEY_PTRS(&alloc.key))
  317. __bkey_put(c, &alloc.key);
  318. for (i = 0; i < KEY_PTRS(&b->key); i++)
  319. EBUG_ON(ptr_stale(c, &b->key, i));
  320. /* Set up the pointer to the space we're allocating: */
  321. for (i = 0; i < KEY_PTRS(&b->key); i++)
  322. k->ptr[i] = b->key.ptr[i];
  323. sectors = min(sectors, b->sectors_free);
  324. SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors);
  325. SET_KEY_SIZE(k, sectors);
  326. SET_KEY_PTRS(k, KEY_PTRS(&b->key));
  327. /*
  328. * Move b to the end of the lru, and keep track of what this bucket was
  329. * last used for:
  330. */
  331. list_move_tail(&b->list, &c->data_buckets);
  332. bkey_copy_key(&b->key, k);
  333. b->last = s->task;
  334. b->sectors_free -= sectors;
  335. for (i = 0; i < KEY_PTRS(&b->key); i++) {
  336. SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors);
  337. atomic_long_add(sectors,
  338. &PTR_CACHE(c, &b->key, i)->sectors_written);
  339. }
  340. if (b->sectors_free < c->sb.block_size)
  341. b->sectors_free = 0;
  342. /*
  343. * k takes refcounts on the buckets it points to until it's inserted
  344. * into the btree, but if we're done with this bucket we just transfer
  345. * get_data_bucket()'s refcount.
  346. */
  347. if (b->sectors_free)
  348. for (i = 0; i < KEY_PTRS(&b->key); i++)
  349. atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin);
  350. spin_unlock(&c->data_bucket_lock);
  351. return true;
  352. }
  353. static void bch_insert_data_error(struct closure *cl)
  354. {
  355. struct btree_op *op = container_of(cl, struct btree_op, cl);
  356. /*
  357. * Our data write just errored, which means we've got a bunch of keys to
  358. * insert that point to data that wasn't succesfully written.
  359. *
  360. * We don't have to insert those keys but we still have to invalidate
  361. * that region of the cache - so, if we just strip off all the pointers
  362. * from the keys we'll accomplish just that.
  363. */
  364. struct bkey *src = op->keys.bottom, *dst = op->keys.bottom;
  365. while (src != op->keys.top) {
  366. struct bkey *n = bkey_next(src);
  367. SET_KEY_PTRS(src, 0);
  368. bkey_copy(dst, src);
  369. dst = bkey_next(dst);
  370. src = n;
  371. }
  372. op->keys.top = dst;
  373. bch_journal(cl);
  374. }
  375. static void bch_insert_data_endio(struct bio *bio, int error)
  376. {
  377. struct closure *cl = bio->bi_private;
  378. struct btree_op *op = container_of(cl, struct btree_op, cl);
  379. struct search *s = container_of(op, struct search, op);
  380. if (error) {
  381. /* TODO: We could try to recover from this. */
  382. if (s->writeback)
  383. s->error = error;
  384. else if (s->write)
  385. set_closure_fn(cl, bch_insert_data_error, bcache_wq);
  386. else
  387. set_closure_fn(cl, NULL, NULL);
  388. }
  389. bch_bbio_endio(op->c, bio, error, "writing data to cache");
  390. }
  391. static void bch_insert_data_loop(struct closure *cl)
  392. {
  393. struct btree_op *op = container_of(cl, struct btree_op, cl);
  394. struct search *s = container_of(op, struct search, op);
  395. struct bio *bio = op->cache_bio, *n;
  396. if (op->skip)
  397. return bio_invalidate(cl);
  398. if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) {
  399. set_gc_sectors(op->c);
  400. bch_queue_gc(op->c);
  401. }
  402. do {
  403. unsigned i;
  404. struct bkey *k;
  405. struct bio_set *split = s->d
  406. ? s->d->bio_split : op->c->bio_split;
  407. /* 1 for the device pointer and 1 for the chksum */
  408. if (bch_keylist_realloc(&op->keys,
  409. 1 + (op->csum ? 1 : 0),
  410. op->c))
  411. continue_at(cl, bch_journal, bcache_wq);
  412. k = op->keys.top;
  413. bkey_init(k);
  414. SET_KEY_INODE(k, op->inode);
  415. SET_KEY_OFFSET(k, bio->bi_sector);
  416. if (!bch_alloc_sectors(k, bio_sectors(bio), s))
  417. goto err;
  418. n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split);
  419. if (!n) {
  420. __bkey_put(op->c, k);
  421. continue_at(cl, bch_insert_data_loop, bcache_wq);
  422. }
  423. n->bi_end_io = bch_insert_data_endio;
  424. n->bi_private = cl;
  425. if (s->writeback) {
  426. SET_KEY_DIRTY(k, true);
  427. for (i = 0; i < KEY_PTRS(k); i++)
  428. SET_GC_MARK(PTR_BUCKET(op->c, k, i),
  429. GC_MARK_DIRTY);
  430. }
  431. SET_KEY_CSUM(k, op->csum);
  432. if (KEY_CSUM(k))
  433. bio_csum(n, k);
  434. pr_debug("%s", pkey(k));
  435. bch_keylist_push(&op->keys);
  436. trace_bcache_cache_insert(n, n->bi_sector, n->bi_bdev);
  437. n->bi_rw |= REQ_WRITE;
  438. bch_submit_bbio(n, op->c, k, 0);
  439. } while (n != bio);
  440. op->insert_data_done = true;
  441. continue_at(cl, bch_journal, bcache_wq);
  442. err:
  443. /* bch_alloc_sectors() blocks if s->writeback = true */
  444. BUG_ON(s->writeback);
  445. /*
  446. * But if it's not a writeback write we'd rather just bail out if
  447. * there aren't any buckets ready to write to - it might take awhile and
  448. * we might be starving btree writes for gc or something.
  449. */
  450. if (s->write) {
  451. /*
  452. * Writethrough write: We can't complete the write until we've
  453. * updated the index. But we don't want to delay the write while
  454. * we wait for buckets to be freed up, so just invalidate the
  455. * rest of the write.
  456. */
  457. op->skip = true;
  458. return bio_invalidate(cl);
  459. } else {
  460. /*
  461. * From a cache miss, we can just insert the keys for the data
  462. * we have written or bail out if we didn't do anything.
  463. */
  464. op->insert_data_done = true;
  465. bio_put(bio);
  466. if (!bch_keylist_empty(&op->keys))
  467. continue_at(cl, bch_journal, bcache_wq);
  468. else
  469. closure_return(cl);
  470. }
  471. }
  472. /**
  473. * bch_insert_data - stick some data in the cache
  474. *
  475. * This is the starting point for any data to end up in a cache device; it could
  476. * be from a normal write, or a writeback write, or a write to a flash only
  477. * volume - it's also used by the moving garbage collector to compact data in
  478. * mostly empty buckets.
  479. *
  480. * It first writes the data to the cache, creating a list of keys to be inserted
  481. * (if the data had to be fragmented there will be multiple keys); after the
  482. * data is written it calls bch_journal, and after the keys have been added to
  483. * the next journal write they're inserted into the btree.
  484. *
  485. * It inserts the data in op->cache_bio; bi_sector is used for the key offset,
  486. * and op->inode is used for the key inode.
  487. *
  488. * If op->skip is true, instead of inserting the data it invalidates the region
  489. * of the cache represented by op->cache_bio and op->inode.
  490. */
  491. void bch_insert_data(struct closure *cl)
  492. {
  493. struct btree_op *op = container_of(cl, struct btree_op, cl);
  494. bch_keylist_init(&op->keys);
  495. bio_get(op->cache_bio);
  496. bch_insert_data_loop(cl);
  497. }
  498. void bch_btree_insert_async(struct closure *cl)
  499. {
  500. struct btree_op *op = container_of(cl, struct btree_op, cl);
  501. struct search *s = container_of(op, struct search, op);
  502. if (bch_btree_insert(op, op->c)) {
  503. s->error = -ENOMEM;
  504. op->insert_data_done = true;
  505. }
  506. if (op->insert_data_done) {
  507. bch_keylist_free(&op->keys);
  508. closure_return(cl);
  509. } else
  510. continue_at(cl, bch_insert_data_loop, bcache_wq);
  511. }
  512. /* Common code for the make_request functions */
  513. static void request_endio(struct bio *bio, int error)
  514. {
  515. struct closure *cl = bio->bi_private;
  516. if (error) {
  517. struct search *s = container_of(cl, struct search, cl);
  518. s->error = error;
  519. /* Only cache read errors are recoverable */
  520. s->recoverable = false;
  521. }
  522. bio_put(bio);
  523. closure_put(cl);
  524. }
  525. void bch_cache_read_endio(struct bio *bio, int error)
  526. {
  527. struct bbio *b = container_of(bio, struct bbio, bio);
  528. struct closure *cl = bio->bi_private;
  529. struct search *s = container_of(cl, struct search, cl);
  530. /*
  531. * If the bucket was reused while our bio was in flight, we might have
  532. * read the wrong data. Set s->error but not error so it doesn't get
  533. * counted against the cache device, but we'll still reread the data
  534. * from the backing device.
  535. */
  536. if (error)
  537. s->error = error;
  538. else if (ptr_stale(s->op.c, &b->key, 0)) {
  539. atomic_long_inc(&s->op.c->cache_read_races);
  540. s->error = -EINTR;
  541. }
  542. bch_bbio_endio(s->op.c, bio, error, "reading from cache");
  543. }
  544. static void bio_complete(struct search *s)
  545. {
  546. if (s->orig_bio) {
  547. int cpu, rw = bio_data_dir(s->orig_bio);
  548. unsigned long duration = jiffies - s->start_time;
  549. cpu = part_stat_lock();
  550. part_round_stats(cpu, &s->d->disk->part0);
  551. part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
  552. part_stat_unlock();
  553. trace_bcache_request_end(s, s->orig_bio);
  554. bio_endio(s->orig_bio, s->error);
  555. s->orig_bio = NULL;
  556. }
  557. }
  558. static void do_bio_hook(struct search *s)
  559. {
  560. struct bio *bio = &s->bio.bio;
  561. memcpy(bio, s->orig_bio, sizeof(struct bio));
  562. bio->bi_end_io = request_endio;
  563. bio->bi_private = &s->cl;
  564. atomic_set(&bio->bi_cnt, 3);
  565. }
  566. static void search_free(struct closure *cl)
  567. {
  568. struct search *s = container_of(cl, struct search, cl);
  569. bio_complete(s);
  570. if (s->op.cache_bio)
  571. bio_put(s->op.cache_bio);
  572. if (s->unaligned_bvec)
  573. mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
  574. closure_debug_destroy(cl);
  575. mempool_free(s, s->d->c->search);
  576. }
  577. static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
  578. {
  579. struct bio_vec *bv;
  580. struct search *s = mempool_alloc(d->c->search, GFP_NOIO);
  581. memset(s, 0, offsetof(struct search, op.keys));
  582. __closure_init(&s->cl, NULL);
  583. s->op.inode = d->id;
  584. s->op.c = d->c;
  585. s->d = d;
  586. s->op.lock = -1;
  587. s->task = current;
  588. s->orig_bio = bio;
  589. s->write = (bio->bi_rw & REQ_WRITE) != 0;
  590. s->op.flush_journal = (bio->bi_rw & REQ_FLUSH) != 0;
  591. s->op.skip = (bio->bi_rw & REQ_DISCARD) != 0;
  592. s->recoverable = 1;
  593. s->start_time = jiffies;
  594. do_bio_hook(s);
  595. if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
  596. bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
  597. memcpy(bv, bio_iovec(bio),
  598. sizeof(struct bio_vec) * bio_segments(bio));
  599. s->bio.bio.bi_io_vec = bv;
  600. s->unaligned_bvec = 1;
  601. }
  602. return s;
  603. }
  604. static void btree_read_async(struct closure *cl)
  605. {
  606. struct btree_op *op = container_of(cl, struct btree_op, cl);
  607. int ret = btree_root(search_recurse, op->c, op);
  608. if (ret == -EAGAIN)
  609. continue_at(cl, btree_read_async, bcache_wq);
  610. closure_return(cl);
  611. }
  612. /* Cached devices */
  613. static void cached_dev_bio_complete(struct closure *cl)
  614. {
  615. struct search *s = container_of(cl, struct search, cl);
  616. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  617. search_free(cl);
  618. cached_dev_put(dc);
  619. }
  620. /* Process reads */
  621. static void cached_dev_read_complete(struct closure *cl)
  622. {
  623. struct search *s = container_of(cl, struct search, cl);
  624. if (s->op.insert_collision)
  625. bch_mark_cache_miss_collision(s);
  626. if (s->op.cache_bio) {
  627. int i;
  628. struct bio_vec *bv;
  629. __bio_for_each_segment(bv, s->op.cache_bio, i, 0)
  630. __free_page(bv->bv_page);
  631. }
  632. cached_dev_bio_complete(cl);
  633. }
  634. static void request_read_error(struct closure *cl)
  635. {
  636. struct search *s = container_of(cl, struct search, cl);
  637. struct bio_vec *bv;
  638. int i;
  639. if (s->recoverable) {
  640. /* The cache read failed, but we can retry from the backing
  641. * device.
  642. */
  643. pr_debug("recovering at sector %llu",
  644. (uint64_t) s->orig_bio->bi_sector);
  645. s->error = 0;
  646. bv = s->bio.bio.bi_io_vec;
  647. do_bio_hook(s);
  648. s->bio.bio.bi_io_vec = bv;
  649. if (!s->unaligned_bvec)
  650. bio_for_each_segment(bv, s->orig_bio, i)
  651. bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
  652. else
  653. memcpy(s->bio.bio.bi_io_vec,
  654. bio_iovec(s->orig_bio),
  655. sizeof(struct bio_vec) *
  656. bio_segments(s->orig_bio));
  657. /* XXX: invalidate cache */
  658. trace_bcache_read_retry(&s->bio.bio);
  659. closure_bio_submit(&s->bio.bio, &s->cl, s->d);
  660. }
  661. continue_at(cl, cached_dev_read_complete, NULL);
  662. }
  663. static void request_read_done(struct closure *cl)
  664. {
  665. struct search *s = container_of(cl, struct search, cl);
  666. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  667. /*
  668. * s->cache_bio != NULL implies that we had a cache miss; cache_bio now
  669. * contains data ready to be inserted into the cache.
  670. *
  671. * First, we copy the data we just read from cache_bio's bounce buffers
  672. * to the buffers the original bio pointed to:
  673. */
  674. if (s->op.cache_bio) {
  675. struct bio_vec *src, *dst;
  676. unsigned src_offset, dst_offset, bytes;
  677. void *dst_ptr;
  678. bio_reset(s->op.cache_bio);
  679. s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
  680. s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
  681. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  682. bio_map(s->op.cache_bio, NULL);
  683. src = bio_iovec(s->op.cache_bio);
  684. dst = bio_iovec(s->cache_miss);
  685. src_offset = src->bv_offset;
  686. dst_offset = dst->bv_offset;
  687. dst_ptr = kmap(dst->bv_page);
  688. while (1) {
  689. if (dst_offset == dst->bv_offset + dst->bv_len) {
  690. kunmap(dst->bv_page);
  691. dst++;
  692. if (dst == bio_iovec_idx(s->cache_miss,
  693. s->cache_miss->bi_vcnt))
  694. break;
  695. dst_offset = dst->bv_offset;
  696. dst_ptr = kmap(dst->bv_page);
  697. }
  698. if (src_offset == src->bv_offset + src->bv_len) {
  699. src++;
  700. if (src == bio_iovec_idx(s->op.cache_bio,
  701. s->op.cache_bio->bi_vcnt))
  702. BUG();
  703. src_offset = src->bv_offset;
  704. }
  705. bytes = min(dst->bv_offset + dst->bv_len - dst_offset,
  706. src->bv_offset + src->bv_len - src_offset);
  707. memcpy(dst_ptr + dst_offset,
  708. page_address(src->bv_page) + src_offset,
  709. bytes);
  710. src_offset += bytes;
  711. dst_offset += bytes;
  712. }
  713. bio_put(s->cache_miss);
  714. s->cache_miss = NULL;
  715. }
  716. if (verify(dc, &s->bio.bio) && s->recoverable)
  717. bch_data_verify(s);
  718. bio_complete(s);
  719. if (s->op.cache_bio &&
  720. !test_bit(CACHE_SET_STOPPING, &s->op.c->flags)) {
  721. s->op.type = BTREE_REPLACE;
  722. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  723. }
  724. continue_at(cl, cached_dev_read_complete, NULL);
  725. }
  726. static void request_read_done_bh(struct closure *cl)
  727. {
  728. struct search *s = container_of(cl, struct search, cl);
  729. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  730. bch_mark_cache_accounting(s, !s->cache_miss, s->op.skip);
  731. if (s->error)
  732. continue_at_nobarrier(cl, request_read_error, bcache_wq);
  733. else if (s->op.cache_bio || verify(dc, &s->bio.bio))
  734. continue_at_nobarrier(cl, request_read_done, bcache_wq);
  735. else
  736. continue_at_nobarrier(cl, cached_dev_read_complete, NULL);
  737. }
  738. static int cached_dev_cache_miss(struct btree *b, struct search *s,
  739. struct bio *bio, unsigned sectors)
  740. {
  741. int ret = 0;
  742. unsigned reada;
  743. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  744. struct bio *miss;
  745. miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
  746. if (!miss)
  747. return -EAGAIN;
  748. if (miss == bio)
  749. s->op.lookup_done = true;
  750. miss->bi_end_io = request_endio;
  751. miss->bi_private = &s->cl;
  752. if (s->cache_miss || s->op.skip)
  753. goto out_submit;
  754. if (miss != bio ||
  755. (bio->bi_rw & REQ_RAHEAD) ||
  756. (bio->bi_rw & REQ_META) ||
  757. s->op.c->gc_stats.in_use >= CUTOFF_CACHE_READA)
  758. reada = 0;
  759. else {
  760. reada = min(dc->readahead >> 9,
  761. sectors - bio_sectors(miss));
  762. if (bio_end(miss) + reada > bdev_sectors(miss->bi_bdev))
  763. reada = bdev_sectors(miss->bi_bdev) - bio_end(miss);
  764. }
  765. s->cache_bio_sectors = bio_sectors(miss) + reada;
  766. s->op.cache_bio = bio_alloc_bioset(GFP_NOWAIT,
  767. DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS),
  768. dc->disk.bio_split);
  769. if (!s->op.cache_bio)
  770. goto out_submit;
  771. s->op.cache_bio->bi_sector = miss->bi_sector;
  772. s->op.cache_bio->bi_bdev = miss->bi_bdev;
  773. s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
  774. s->op.cache_bio->bi_end_io = request_endio;
  775. s->op.cache_bio->bi_private = &s->cl;
  776. /* btree_search_recurse()'s btree iterator is no good anymore */
  777. ret = -EINTR;
  778. if (!bch_btree_insert_check_key(b, &s->op, s->op.cache_bio))
  779. goto out_put;
  780. bio_map(s->op.cache_bio, NULL);
  781. if (bio_alloc_pages(s->op.cache_bio, __GFP_NOWARN|GFP_NOIO))
  782. goto out_put;
  783. s->cache_miss = miss;
  784. bio_get(s->op.cache_bio);
  785. trace_bcache_cache_miss(s->orig_bio);
  786. closure_bio_submit(s->op.cache_bio, &s->cl, s->d);
  787. return ret;
  788. out_put:
  789. bio_put(s->op.cache_bio);
  790. s->op.cache_bio = NULL;
  791. out_submit:
  792. closure_bio_submit(miss, &s->cl, s->d);
  793. return ret;
  794. }
  795. static void request_read(struct cached_dev *dc, struct search *s)
  796. {
  797. struct closure *cl = &s->cl;
  798. check_should_skip(dc, s);
  799. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  800. continue_at(cl, request_read_done_bh, NULL);
  801. }
  802. /* Process writes */
  803. static void cached_dev_write_complete(struct closure *cl)
  804. {
  805. struct search *s = container_of(cl, struct search, cl);
  806. struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
  807. up_read_non_owner(&dc->writeback_lock);
  808. cached_dev_bio_complete(cl);
  809. }
  810. static bool should_writeback(struct cached_dev *dc, struct bio *bio)
  811. {
  812. unsigned threshold = (bio->bi_rw & REQ_SYNC)
  813. ? CUTOFF_WRITEBACK_SYNC
  814. : CUTOFF_WRITEBACK;
  815. return !atomic_read(&dc->disk.detaching) &&
  816. cache_mode(dc, bio) == CACHE_MODE_WRITEBACK &&
  817. dc->disk.c->gc_stats.in_use < threshold;
  818. }
  819. static void request_write(struct cached_dev *dc, struct search *s)
  820. {
  821. struct closure *cl = &s->cl;
  822. struct bio *bio = &s->bio.bio;
  823. struct bkey start, end;
  824. start = KEY(dc->disk.id, bio->bi_sector, 0);
  825. end = KEY(dc->disk.id, bio_end(bio), 0);
  826. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
  827. check_should_skip(dc, s);
  828. down_read_non_owner(&dc->writeback_lock);
  829. if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
  830. s->op.skip = false;
  831. s->writeback = true;
  832. }
  833. if (bio->bi_rw & REQ_DISCARD)
  834. goto skip;
  835. if (s->op.skip)
  836. goto skip;
  837. if (should_writeback(dc, s->orig_bio))
  838. s->writeback = true;
  839. if (!s->writeback) {
  840. s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
  841. dc->disk.bio_split);
  842. trace_bcache_writethrough(s->orig_bio);
  843. closure_bio_submit(bio, cl, s->d);
  844. } else {
  845. s->op.cache_bio = bio;
  846. trace_bcache_writeback(s->orig_bio);
  847. bch_writeback_add(dc, bio_sectors(bio));
  848. }
  849. out:
  850. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  851. continue_at(cl, cached_dev_write_complete, NULL);
  852. skip:
  853. s->op.skip = true;
  854. s->op.cache_bio = s->orig_bio;
  855. bio_get(s->op.cache_bio);
  856. trace_bcache_write_skip(s->orig_bio);
  857. if ((bio->bi_rw & REQ_DISCARD) &&
  858. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  859. goto out;
  860. closure_bio_submit(bio, cl, s->d);
  861. goto out;
  862. }
  863. static void request_nodata(struct cached_dev *dc, struct search *s)
  864. {
  865. struct closure *cl = &s->cl;
  866. struct bio *bio = &s->bio.bio;
  867. if (bio->bi_rw & REQ_DISCARD) {
  868. request_write(dc, s);
  869. return;
  870. }
  871. if (s->op.flush_journal)
  872. bch_journal_meta(s->op.c, cl);
  873. closure_bio_submit(bio, cl, s->d);
  874. continue_at(cl, cached_dev_bio_complete, NULL);
  875. }
  876. /* Cached devices - read & write stuff */
  877. int bch_get_congested(struct cache_set *c)
  878. {
  879. int i;
  880. if (!c->congested_read_threshold_us &&
  881. !c->congested_write_threshold_us)
  882. return 0;
  883. i = (local_clock_us() - c->congested_last_us) / 1024;
  884. if (i < 0)
  885. return 0;
  886. i += atomic_read(&c->congested);
  887. if (i >= 0)
  888. return 0;
  889. i += CONGESTED_MAX;
  890. return i <= 0 ? 1 : fract_exp_two(i, 6);
  891. }
  892. static void add_sequential(struct task_struct *t)
  893. {
  894. ewma_add(t->sequential_io_avg,
  895. t->sequential_io, 8, 0);
  896. t->sequential_io = 0;
  897. }
  898. static void check_should_skip(struct cached_dev *dc, struct search *s)
  899. {
  900. struct hlist_head *iohash(uint64_t k)
  901. { return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; }
  902. struct cache_set *c = s->op.c;
  903. struct bio *bio = &s->bio.bio;
  904. long rand;
  905. int cutoff = bch_get_congested(c);
  906. unsigned mode = cache_mode(dc, bio);
  907. if (atomic_read(&dc->disk.detaching) ||
  908. c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
  909. (bio->bi_rw & REQ_DISCARD))
  910. goto skip;
  911. if (mode == CACHE_MODE_NONE ||
  912. (mode == CACHE_MODE_WRITEAROUND &&
  913. (bio->bi_rw & REQ_WRITE)))
  914. goto skip;
  915. if (bio->bi_sector & (c->sb.block_size - 1) ||
  916. bio_sectors(bio) & (c->sb.block_size - 1)) {
  917. pr_debug("skipping unaligned io");
  918. goto skip;
  919. }
  920. if (!cutoff) {
  921. cutoff = dc->sequential_cutoff >> 9;
  922. if (!cutoff)
  923. goto rescale;
  924. if (mode == CACHE_MODE_WRITEBACK &&
  925. (bio->bi_rw & REQ_WRITE) &&
  926. (bio->bi_rw & REQ_SYNC))
  927. goto rescale;
  928. }
  929. if (dc->sequential_merge) {
  930. struct io *i;
  931. spin_lock(&dc->io_lock);
  932. hlist_for_each_entry(i, iohash(bio->bi_sector), hash)
  933. if (i->last == bio->bi_sector &&
  934. time_before(jiffies, i->jiffies))
  935. goto found;
  936. i = list_first_entry(&dc->io_lru, struct io, lru);
  937. add_sequential(s->task);
  938. i->sequential = 0;
  939. found:
  940. if (i->sequential + bio->bi_size > i->sequential)
  941. i->sequential += bio->bi_size;
  942. i->last = bio_end(bio);
  943. i->jiffies = jiffies + msecs_to_jiffies(5000);
  944. s->task->sequential_io = i->sequential;
  945. hlist_del(&i->hash);
  946. hlist_add_head(&i->hash, iohash(i->last));
  947. list_move_tail(&i->lru, &dc->io_lru);
  948. spin_unlock(&dc->io_lock);
  949. } else {
  950. s->task->sequential_io = bio->bi_size;
  951. add_sequential(s->task);
  952. }
  953. rand = get_random_int();
  954. cutoff -= bitmap_weight(&rand, BITS_PER_LONG);
  955. if (cutoff <= (int) (max(s->task->sequential_io,
  956. s->task->sequential_io_avg) >> 9))
  957. goto skip;
  958. rescale:
  959. bch_rescale_priorities(c, bio_sectors(bio));
  960. return;
  961. skip:
  962. bch_mark_sectors_bypassed(s, bio_sectors(bio));
  963. s->op.skip = true;
  964. }
  965. static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
  966. {
  967. struct search *s;
  968. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  969. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  970. int cpu, rw = bio_data_dir(bio);
  971. cpu = part_stat_lock();
  972. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  973. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  974. part_stat_unlock();
  975. bio->bi_bdev = dc->bdev;
  976. bio->bi_sector += BDEV_DATA_START;
  977. if (cached_dev_get(dc)) {
  978. s = search_alloc(bio, d);
  979. trace_bcache_request_start(s, bio);
  980. if (!bio_has_data(bio))
  981. request_nodata(dc, s);
  982. else if (rw)
  983. request_write(dc, s);
  984. else
  985. request_read(dc, s);
  986. } else {
  987. if ((bio->bi_rw & REQ_DISCARD) &&
  988. !blk_queue_discard(bdev_get_queue(dc->bdev)))
  989. bio_endio(bio, 0);
  990. else
  991. bch_generic_make_request(bio, &d->bio_split_hook);
  992. }
  993. }
  994. static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
  995. unsigned int cmd, unsigned long arg)
  996. {
  997. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  998. return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  999. }
  1000. static int cached_dev_congested(void *data, int bits)
  1001. {
  1002. struct bcache_device *d = data;
  1003. struct cached_dev *dc = container_of(d, struct cached_dev, disk);
  1004. struct request_queue *q = bdev_get_queue(dc->bdev);
  1005. int ret = 0;
  1006. if (bdi_congested(&q->backing_dev_info, bits))
  1007. return 1;
  1008. if (cached_dev_get(dc)) {
  1009. unsigned i;
  1010. struct cache *ca;
  1011. for_each_cache(ca, d->c, i) {
  1012. q = bdev_get_queue(ca->bdev);
  1013. ret |= bdi_congested(&q->backing_dev_info, bits);
  1014. }
  1015. cached_dev_put(dc);
  1016. }
  1017. return ret;
  1018. }
  1019. void bch_cached_dev_request_init(struct cached_dev *dc)
  1020. {
  1021. struct gendisk *g = dc->disk.disk;
  1022. g->queue->make_request_fn = cached_dev_make_request;
  1023. g->queue->backing_dev_info.congested_fn = cached_dev_congested;
  1024. dc->disk.cache_miss = cached_dev_cache_miss;
  1025. dc->disk.ioctl = cached_dev_ioctl;
  1026. }
  1027. /* Flash backed devices */
  1028. static int flash_dev_cache_miss(struct btree *b, struct search *s,
  1029. struct bio *bio, unsigned sectors)
  1030. {
  1031. /* Zero fill bio */
  1032. while (bio->bi_idx != bio->bi_vcnt) {
  1033. struct bio_vec *bv = bio_iovec(bio);
  1034. unsigned j = min(bv->bv_len >> 9, sectors);
  1035. void *p = kmap(bv->bv_page);
  1036. memset(p + bv->bv_offset, 0, j << 9);
  1037. kunmap(bv->bv_page);
  1038. bv->bv_len -= j << 9;
  1039. bv->bv_offset += j << 9;
  1040. if (bv->bv_len)
  1041. return 0;
  1042. bio->bi_sector += j;
  1043. bio->bi_size -= j << 9;
  1044. bio->bi_idx++;
  1045. sectors -= j;
  1046. }
  1047. s->op.lookup_done = true;
  1048. return 0;
  1049. }
  1050. static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
  1051. {
  1052. struct search *s;
  1053. struct closure *cl;
  1054. struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
  1055. int cpu, rw = bio_data_dir(bio);
  1056. cpu = part_stat_lock();
  1057. part_stat_inc(cpu, &d->disk->part0, ios[rw]);
  1058. part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
  1059. part_stat_unlock();
  1060. s = search_alloc(bio, d);
  1061. cl = &s->cl;
  1062. bio = &s->bio.bio;
  1063. trace_bcache_request_start(s, bio);
  1064. if (bio_has_data(bio) && !rw) {
  1065. closure_call(&s->op.cl, btree_read_async, NULL, cl);
  1066. } else if (bio_has_data(bio) || s->op.skip) {
  1067. bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
  1068. &KEY(d->id, bio->bi_sector, 0),
  1069. &KEY(d->id, bio_end(bio), 0));
  1070. s->writeback = true;
  1071. s->op.cache_bio = bio;
  1072. closure_call(&s->op.cl, bch_insert_data, NULL, cl);
  1073. } else {
  1074. /* No data - probably a cache flush */
  1075. if (s->op.flush_journal)
  1076. bch_journal_meta(s->op.c, cl);
  1077. }
  1078. continue_at(cl, search_free, NULL);
  1079. }
  1080. static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
  1081. unsigned int cmd, unsigned long arg)
  1082. {
  1083. return -ENOTTY;
  1084. }
  1085. static int flash_dev_congested(void *data, int bits)
  1086. {
  1087. struct bcache_device *d = data;
  1088. struct request_queue *q;
  1089. struct cache *ca;
  1090. unsigned i;
  1091. int ret = 0;
  1092. for_each_cache(ca, d->c, i) {
  1093. q = bdev_get_queue(ca->bdev);
  1094. ret |= bdi_congested(&q->backing_dev_info, bits);
  1095. }
  1096. return ret;
  1097. }
  1098. void bch_flash_dev_request_init(struct bcache_device *d)
  1099. {
  1100. struct gendisk *g = d->disk;
  1101. g->queue->make_request_fn = flash_dev_make_request;
  1102. g->queue->backing_dev_info.congested_fn = flash_dev_congested;
  1103. d->cache_miss = flash_dev_cache_miss;
  1104. d->ioctl = flash_dev_ioctl;
  1105. }
  1106. void bch_request_exit(void)
  1107. {
  1108. #ifdef CONFIG_CGROUP_BCACHE
  1109. cgroup_unload_subsys(&bcache_subsys);
  1110. #endif
  1111. if (bch_search_cache)
  1112. kmem_cache_destroy(bch_search_cache);
  1113. }
  1114. int __init bch_request_init(void)
  1115. {
  1116. bch_search_cache = KMEM_CACHE(search, 0);
  1117. if (!bch_search_cache)
  1118. return -ENOMEM;
  1119. #ifdef CONFIG_CGROUP_BCACHE
  1120. cgroup_load_subsys(&bcache_subsys);
  1121. init_bch_cgroup(&bcache_default_cgroup);
  1122. cgroup_add_cftypes(&bcache_subsys, bch_files);
  1123. #endif
  1124. return 0;
  1125. }