request.c 36 KB

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