bcache.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. #ifndef _BCACHE_H
  2. #define _BCACHE_H
  3. /*
  4. * SOME HIGH LEVEL CODE DOCUMENTATION:
  5. *
  6. * Bcache mostly works with cache sets, cache devices, and backing devices.
  7. *
  8. * Support for multiple cache devices hasn't quite been finished off yet, but
  9. * it's about 95% plumbed through. A cache set and its cache devices is sort of
  10. * like a md raid array and its component devices. Most of the code doesn't care
  11. * about individual cache devices, the main abstraction is the cache set.
  12. *
  13. * Multiple cache devices is intended to give us the ability to mirror dirty
  14. * cached data and metadata, without mirroring clean cached data.
  15. *
  16. * Backing devices are different, in that they have a lifetime independent of a
  17. * cache set. When you register a newly formatted backing device it'll come up
  18. * in passthrough mode, and then you can attach and detach a backing device from
  19. * a cache set at runtime - while it's mounted and in use. Detaching implicitly
  20. * invalidates any cached data for that backing device.
  21. *
  22. * A cache set can have multiple (many) backing devices attached to it.
  23. *
  24. * There's also flash only volumes - this is the reason for the distinction
  25. * between struct cached_dev and struct bcache_device. A flash only volume
  26. * works much like a bcache device that has a backing device, except the
  27. * "cached" data is always dirty. The end result is that we get thin
  28. * provisioning with very little additional code.
  29. *
  30. * Flash only volumes work but they're not production ready because the moving
  31. * garbage collector needs more work. More on that later.
  32. *
  33. * BUCKETS/ALLOCATION:
  34. *
  35. * Bcache is primarily designed for caching, which means that in normal
  36. * operation all of our available space will be allocated. Thus, we need an
  37. * efficient way of deleting things from the cache so we can write new things to
  38. * it.
  39. *
  40. * To do this, we first divide the cache device up into buckets. A bucket is the
  41. * unit of allocation; they're typically around 1 mb - anywhere from 128k to 2M+
  42. * works efficiently.
  43. *
  44. * Each bucket has a 16 bit priority, and an 8 bit generation associated with
  45. * it. The gens and priorities for all the buckets are stored contiguously and
  46. * packed on disk (in a linked list of buckets - aside from the superblock, all
  47. * of bcache's metadata is stored in buckets).
  48. *
  49. * The priority is used to implement an LRU. We reset a bucket's priority when
  50. * we allocate it or on cache it, and every so often we decrement the priority
  51. * of each bucket. It could be used to implement something more sophisticated,
  52. * if anyone ever gets around to it.
  53. *
  54. * The generation is used for invalidating buckets. Each pointer also has an 8
  55. * bit generation embedded in it; for a pointer to be considered valid, its gen
  56. * must match the gen of the bucket it points into. Thus, to reuse a bucket all
  57. * we have to do is increment its gen (and write its new gen to disk; we batch
  58. * this up).
  59. *
  60. * Bcache is entirely COW - we never write twice to a bucket, even buckets that
  61. * contain metadata (including btree nodes).
  62. *
  63. * THE BTREE:
  64. *
  65. * Bcache is in large part design around the btree.
  66. *
  67. * At a high level, the btree is just an index of key -> ptr tuples.
  68. *
  69. * Keys represent extents, and thus have a size field. Keys also have a variable
  70. * number of pointers attached to them (potentially zero, which is handy for
  71. * invalidating the cache).
  72. *
  73. * The key itself is an inode:offset pair. The inode number corresponds to a
  74. * backing device or a flash only volume. The offset is the ending offset of the
  75. * extent within the inode - not the starting offset; this makes lookups
  76. * slightly more convenient.
  77. *
  78. * Pointers contain the cache device id, the offset on that device, and an 8 bit
  79. * generation number. More on the gen later.
  80. *
  81. * Index lookups are not fully abstracted - cache lookups in particular are
  82. * still somewhat mixed in with the btree code, but things are headed in that
  83. * direction.
  84. *
  85. * Updates are fairly well abstracted, though. There are two different ways of
  86. * updating the btree; insert and replace.
  87. *
  88. * BTREE_INSERT will just take a list of keys and insert them into the btree -
  89. * overwriting (possibly only partially) any extents they overlap with. This is
  90. * used to update the index after a write.
  91. *
  92. * BTREE_REPLACE is really cmpxchg(); it inserts a key into the btree iff it is
  93. * overwriting a key that matches another given key. This is used for inserting
  94. * data into the cache after a cache miss, and for background writeback, and for
  95. * the moving garbage collector.
  96. *
  97. * There is no "delete" operation; deleting things from the index is
  98. * accomplished by either by invalidating pointers (by incrementing a bucket's
  99. * gen) or by inserting a key with 0 pointers - which will overwrite anything
  100. * previously present at that location in the index.
  101. *
  102. * This means that there are always stale/invalid keys in the btree. They're
  103. * filtered out by the code that iterates through a btree node, and removed when
  104. * a btree node is rewritten.
  105. *
  106. * BTREE NODES:
  107. *
  108. * Our unit of allocation is a bucket, and we we can't arbitrarily allocate and
  109. * free smaller than a bucket - so, that's how big our btree nodes are.
  110. *
  111. * (If buckets are really big we'll only use part of the bucket for a btree node
  112. * - no less than 1/4th - but a bucket still contains no more than a single
  113. * btree node. I'd actually like to change this, but for now we rely on the
  114. * bucket's gen for deleting btree nodes when we rewrite/split a node.)
  115. *
  116. * Anyways, btree nodes are big - big enough to be inefficient with a textbook
  117. * btree implementation.
  118. *
  119. * The way this is solved is that btree nodes are internally log structured; we
  120. * can append new keys to an existing btree node without rewriting it. This
  121. * means each set of keys we write is sorted, but the node is not.
  122. *
  123. * We maintain this log structure in memory - keeping 1Mb of keys sorted would
  124. * be expensive, and we have to distinguish between the keys we have written and
  125. * the keys we haven't. So to do a lookup in a btree node, we have to search
  126. * each sorted set. But we do merge written sets together lazily, so the cost of
  127. * these extra searches is quite low (normally most of the keys in a btree node
  128. * will be in one big set, and then there'll be one or two sets that are much
  129. * smaller).
  130. *
  131. * This log structure makes bcache's btree more of a hybrid between a
  132. * conventional btree and a compacting data structure, with some of the
  133. * advantages of both.
  134. *
  135. * GARBAGE COLLECTION:
  136. *
  137. * We can't just invalidate any bucket - it might contain dirty data or
  138. * metadata. If it once contained dirty data, other writes might overwrite it
  139. * later, leaving no valid pointers into that bucket in the index.
  140. *
  141. * Thus, the primary purpose of garbage collection is to find buckets to reuse.
  142. * It also counts how much valid data it each bucket currently contains, so that
  143. * allocation can reuse buckets sooner when they've been mostly overwritten.
  144. *
  145. * It also does some things that are really internal to the btree
  146. * implementation. If a btree node contains pointers that are stale by more than
  147. * some threshold, it rewrites the btree node to avoid the bucket's generation
  148. * wrapping around. It also merges adjacent btree nodes if they're empty enough.
  149. *
  150. * THE JOURNAL:
  151. *
  152. * Bcache's journal is not necessary for consistency; we always strictly
  153. * order metadata writes so that the btree and everything else is consistent on
  154. * disk in the event of an unclean shutdown, and in fact bcache had writeback
  155. * caching (with recovery from unclean shutdown) before journalling was
  156. * implemented.
  157. *
  158. * Rather, the journal is purely a performance optimization; we can't complete a
  159. * write until we've updated the index on disk, otherwise the cache would be
  160. * inconsistent in the event of an unclean shutdown. This means that without the
  161. * journal, on random write workloads we constantly have to update all the leaf
  162. * nodes in the btree, and those writes will be mostly empty (appending at most
  163. * a few keys each) - highly inefficient in terms of amount of metadata writes,
  164. * and it puts more strain on the various btree resorting/compacting code.
  165. *
  166. * The journal is just a log of keys we've inserted; on startup we just reinsert
  167. * all the keys in the open journal entries. That means that when we're updating
  168. * a node in the btree, we can wait until a 4k block of keys fills up before
  169. * writing them out.
  170. *
  171. * For simplicity, we only journal updates to leaf nodes; updates to parent
  172. * nodes are rare enough (since our leaf nodes are huge) that it wasn't worth
  173. * the complexity to deal with journalling them (in particular, journal replay)
  174. * - updates to non leaf nodes just happen synchronously (see btree_split()).
  175. */
  176. #define pr_fmt(fmt) "bcache: %s() " fmt "\n", __func__
  177. #include <linux/bcache.h>
  178. #include <linux/bio.h>
  179. #include <linux/kobject.h>
  180. #include <linux/list.h>
  181. #include <linux/mutex.h>
  182. #include <linux/rbtree.h>
  183. #include <linux/rwsem.h>
  184. #include <linux/types.h>
  185. #include <linux/workqueue.h>
  186. #include "util.h"
  187. #include "closure.h"
  188. struct bucket {
  189. atomic_t pin;
  190. uint16_t prio;
  191. uint8_t gen;
  192. uint8_t disk_gen;
  193. uint8_t last_gc; /* Most out of date gen in the btree */
  194. uint8_t gc_gen;
  195. uint16_t gc_mark;
  196. };
  197. /*
  198. * I'd use bitfields for these, but I don't trust the compiler not to screw me
  199. * as multiple threads touch struct bucket without locking
  200. */
  201. BITMASK(GC_MARK, struct bucket, gc_mark, 0, 2);
  202. #define GC_MARK_RECLAIMABLE 0
  203. #define GC_MARK_DIRTY 1
  204. #define GC_MARK_METADATA 2
  205. BITMASK(GC_SECTORS_USED, struct bucket, gc_mark, 2, 14);
  206. #include "journal.h"
  207. #include "stats.h"
  208. struct search;
  209. struct btree;
  210. struct keybuf;
  211. struct keybuf_key {
  212. struct rb_node node;
  213. BKEY_PADDED(key);
  214. void *private;
  215. };
  216. struct keybuf {
  217. struct bkey last_scanned;
  218. spinlock_t lock;
  219. /*
  220. * Beginning and end of range in rb tree - so that we can skip taking
  221. * lock and checking the rb tree when we need to check for overlapping
  222. * keys.
  223. */
  224. struct bkey start;
  225. struct bkey end;
  226. struct rb_root keys;
  227. #define KEYBUF_NR 500
  228. DECLARE_ARRAY_ALLOCATOR(struct keybuf_key, freelist, KEYBUF_NR);
  229. };
  230. struct bio_split_pool {
  231. struct bio_set *bio_split;
  232. mempool_t *bio_split_hook;
  233. };
  234. struct bio_split_hook {
  235. struct closure cl;
  236. struct bio_split_pool *p;
  237. struct bio *bio;
  238. bio_end_io_t *bi_end_io;
  239. void *bi_private;
  240. };
  241. struct bcache_device {
  242. struct closure cl;
  243. struct kobject kobj;
  244. struct cache_set *c;
  245. unsigned id;
  246. #define BCACHEDEVNAME_SIZE 12
  247. char name[BCACHEDEVNAME_SIZE];
  248. struct gendisk *disk;
  249. unsigned long flags;
  250. #define BCACHE_DEV_CLOSING 0
  251. #define BCACHE_DEV_DETACHING 1
  252. #define BCACHE_DEV_UNLINK_DONE 2
  253. unsigned nr_stripes;
  254. unsigned stripe_size;
  255. atomic_t *stripe_sectors_dirty;
  256. unsigned long *full_dirty_stripes;
  257. unsigned long sectors_dirty_last;
  258. long sectors_dirty_derivative;
  259. mempool_t *unaligned_bvec;
  260. struct bio_set *bio_split;
  261. unsigned data_csum:1;
  262. int (*cache_miss)(struct btree *, struct search *,
  263. struct bio *, unsigned);
  264. int (*ioctl) (struct bcache_device *, fmode_t, unsigned, unsigned long);
  265. struct bio_split_pool bio_split_hook;
  266. };
  267. struct io {
  268. /* Used to track sequential IO so it can be skipped */
  269. struct hlist_node hash;
  270. struct list_head lru;
  271. unsigned long jiffies;
  272. unsigned sequential;
  273. sector_t last;
  274. };
  275. struct cached_dev {
  276. struct list_head list;
  277. struct bcache_device disk;
  278. struct block_device *bdev;
  279. struct cache_sb sb;
  280. struct bio sb_bio;
  281. struct bio_vec sb_bv[1];
  282. struct closure_with_waitlist sb_write;
  283. /* Refcount on the cache set. Always nonzero when we're caching. */
  284. atomic_t count;
  285. struct work_struct detach;
  286. /*
  287. * Device might not be running if it's dirty and the cache set hasn't
  288. * showed up yet.
  289. */
  290. atomic_t running;
  291. /*
  292. * Writes take a shared lock from start to finish; scanning for dirty
  293. * data to refill the rb tree requires an exclusive lock.
  294. */
  295. struct rw_semaphore writeback_lock;
  296. /*
  297. * Nonzero, and writeback has a refcount (d->count), iff there is dirty
  298. * data in the cache. Protected by writeback_lock; must have an
  299. * shared lock to set and exclusive lock to clear.
  300. */
  301. atomic_t has_dirty;
  302. struct bch_ratelimit writeback_rate;
  303. struct delayed_work writeback_rate_update;
  304. /*
  305. * Internal to the writeback code, so read_dirty() can keep track of
  306. * where it's at.
  307. */
  308. sector_t last_read;
  309. /* Limit number of writeback bios in flight */
  310. struct semaphore in_flight;
  311. struct task_struct *writeback_thread;
  312. struct keybuf writeback_keys;
  313. /* For tracking sequential IO */
  314. #define RECENT_IO_BITS 7
  315. #define RECENT_IO (1 << RECENT_IO_BITS)
  316. struct io io[RECENT_IO];
  317. struct hlist_head io_hash[RECENT_IO + 1];
  318. struct list_head io_lru;
  319. spinlock_t io_lock;
  320. struct cache_accounting accounting;
  321. /* The rest of this all shows up in sysfs */
  322. unsigned sequential_cutoff;
  323. unsigned readahead;
  324. unsigned verify:1;
  325. unsigned partial_stripes_expensive:1;
  326. unsigned writeback_metadata:1;
  327. unsigned writeback_running:1;
  328. unsigned char writeback_percent;
  329. unsigned writeback_delay;
  330. int writeback_rate_change;
  331. int64_t writeback_rate_derivative;
  332. uint64_t writeback_rate_target;
  333. unsigned writeback_rate_update_seconds;
  334. unsigned writeback_rate_d_term;
  335. unsigned writeback_rate_p_term_inverse;
  336. unsigned writeback_rate_d_smooth;
  337. };
  338. enum alloc_watermarks {
  339. WATERMARK_PRIO,
  340. WATERMARK_METADATA,
  341. WATERMARK_MOVINGGC,
  342. WATERMARK_NONE,
  343. WATERMARK_MAX
  344. };
  345. struct cache {
  346. struct cache_set *set;
  347. struct cache_sb sb;
  348. struct bio sb_bio;
  349. struct bio_vec sb_bv[1];
  350. struct kobject kobj;
  351. struct block_device *bdev;
  352. unsigned watermark[WATERMARK_MAX];
  353. struct task_struct *alloc_thread;
  354. struct closure prio;
  355. struct prio_set *disk_buckets;
  356. /*
  357. * When allocating new buckets, prio_write() gets first dibs - since we
  358. * may not be allocate at all without writing priorities and gens.
  359. * prio_buckets[] contains the last buckets we wrote priorities to (so
  360. * gc can mark them as metadata), prio_next[] contains the buckets
  361. * allocated for the next prio write.
  362. */
  363. uint64_t *prio_buckets;
  364. uint64_t *prio_last_buckets;
  365. /*
  366. * free: Buckets that are ready to be used
  367. *
  368. * free_inc: Incoming buckets - these are buckets that currently have
  369. * cached data in them, and we can't reuse them until after we write
  370. * their new gen to disk. After prio_write() finishes writing the new
  371. * gens/prios, they'll be moved to the free list (and possibly discarded
  372. * in the process)
  373. *
  374. * unused: GC found nothing pointing into these buckets (possibly
  375. * because all the data they contained was overwritten), so we only
  376. * need to discard them before they can be moved to the free list.
  377. */
  378. DECLARE_FIFO(long, free);
  379. DECLARE_FIFO(long, free_inc);
  380. DECLARE_FIFO(long, unused);
  381. size_t fifo_last_bucket;
  382. /* Allocation stuff: */
  383. struct bucket *buckets;
  384. DECLARE_HEAP(struct bucket *, heap);
  385. /*
  386. * max(gen - disk_gen) for all buckets. When it gets too big we have to
  387. * call prio_write() to keep gens from wrapping.
  388. */
  389. uint8_t need_save_prio;
  390. unsigned gc_move_threshold;
  391. /*
  392. * If nonzero, we know we aren't going to find any buckets to invalidate
  393. * until a gc finishes - otherwise we could pointlessly burn a ton of
  394. * cpu
  395. */
  396. unsigned invalidate_needs_gc:1;
  397. bool discard; /* Get rid of? */
  398. struct journal_device journal;
  399. /* The rest of this all shows up in sysfs */
  400. #define IO_ERROR_SHIFT 20
  401. atomic_t io_errors;
  402. atomic_t io_count;
  403. atomic_long_t meta_sectors_written;
  404. atomic_long_t btree_sectors_written;
  405. atomic_long_t sectors_written;
  406. struct bio_split_pool bio_split_hook;
  407. };
  408. struct gc_stat {
  409. size_t nodes;
  410. size_t key_bytes;
  411. size_t nkeys;
  412. uint64_t data; /* sectors */
  413. unsigned in_use; /* percent */
  414. };
  415. /*
  416. * Flag bits, for how the cache set is shutting down, and what phase it's at:
  417. *
  418. * CACHE_SET_UNREGISTERING means we're not just shutting down, we're detaching
  419. * all the backing devices first (their cached data gets invalidated, and they
  420. * won't automatically reattach).
  421. *
  422. * CACHE_SET_STOPPING always gets set first when we're closing down a cache set;
  423. * we'll continue to run normally for awhile with CACHE_SET_STOPPING set (i.e.
  424. * flushing dirty data).
  425. */
  426. #define CACHE_SET_UNREGISTERING 0
  427. #define CACHE_SET_STOPPING 1
  428. struct cache_set {
  429. struct closure cl;
  430. struct list_head list;
  431. struct kobject kobj;
  432. struct kobject internal;
  433. struct dentry *debug;
  434. struct cache_accounting accounting;
  435. unsigned long flags;
  436. struct cache_sb sb;
  437. struct cache *cache[MAX_CACHES_PER_SET];
  438. struct cache *cache_by_alloc[MAX_CACHES_PER_SET];
  439. int caches_loaded;
  440. struct bcache_device **devices;
  441. struct list_head cached_devs;
  442. uint64_t cached_dev_sectors;
  443. struct closure caching;
  444. struct closure_with_waitlist sb_write;
  445. mempool_t *search;
  446. mempool_t *bio_meta;
  447. struct bio_set *bio_split;
  448. /* For the btree cache */
  449. struct shrinker shrink;
  450. /* For the btree cache and anything allocation related */
  451. struct mutex bucket_lock;
  452. /* log2(bucket_size), in sectors */
  453. unsigned short bucket_bits;
  454. /* log2(block_size), in sectors */
  455. unsigned short block_bits;
  456. /*
  457. * Default number of pages for a new btree node - may be less than a
  458. * full bucket
  459. */
  460. unsigned btree_pages;
  461. /*
  462. * Lists of struct btrees; lru is the list for structs that have memory
  463. * allocated for actual btree node, freed is for structs that do not.
  464. *
  465. * We never free a struct btree, except on shutdown - we just put it on
  466. * the btree_cache_freed list and reuse it later. This simplifies the
  467. * code, and it doesn't cost us much memory as the memory usage is
  468. * dominated by buffers that hold the actual btree node data and those
  469. * can be freed - and the number of struct btrees allocated is
  470. * effectively bounded.
  471. *
  472. * btree_cache_freeable effectively is a small cache - we use it because
  473. * high order page allocations can be rather expensive, and it's quite
  474. * common to delete and allocate btree nodes in quick succession. It
  475. * should never grow past ~2-3 nodes in practice.
  476. */
  477. struct list_head btree_cache;
  478. struct list_head btree_cache_freeable;
  479. struct list_head btree_cache_freed;
  480. /* Number of elements in btree_cache + btree_cache_freeable lists */
  481. unsigned bucket_cache_used;
  482. /*
  483. * If we need to allocate memory for a new btree node and that
  484. * allocation fails, we can cannibalize another node in the btree cache
  485. * to satisfy the allocation. However, only one thread can be doing this
  486. * at a time, for obvious reasons - try_harder and try_wait are
  487. * basically a lock for this that we can wait on asynchronously. The
  488. * btree_root() macro releases the lock when it returns.
  489. */
  490. struct task_struct *try_harder;
  491. wait_queue_head_t try_wait;
  492. uint64_t try_harder_start;
  493. /*
  494. * When we free a btree node, we increment the gen of the bucket the
  495. * node is in - but we can't rewrite the prios and gens until we
  496. * finished whatever it is we were doing, otherwise after a crash the
  497. * btree node would be freed but for say a split, we might not have the
  498. * pointers to the new nodes inserted into the btree yet.
  499. *
  500. * This is a refcount that blocks prio_write() until the new keys are
  501. * written.
  502. */
  503. atomic_t prio_blocked;
  504. wait_queue_head_t bucket_wait;
  505. /*
  506. * For any bio we don't skip we subtract the number of sectors from
  507. * rescale; when it hits 0 we rescale all the bucket priorities.
  508. */
  509. atomic_t rescale;
  510. /*
  511. * When we invalidate buckets, we use both the priority and the amount
  512. * of good data to determine which buckets to reuse first - to weight
  513. * those together consistently we keep track of the smallest nonzero
  514. * priority of any bucket.
  515. */
  516. uint16_t min_prio;
  517. /*
  518. * max(gen - gc_gen) for all buckets. When it gets too big we have to gc
  519. * to keep gens from wrapping around.
  520. */
  521. uint8_t need_gc;
  522. struct gc_stat gc_stats;
  523. size_t nbuckets;
  524. struct task_struct *gc_thread;
  525. /* Where in the btree gc currently is */
  526. struct bkey gc_done;
  527. /*
  528. * The allocation code needs gc_mark in struct bucket to be correct, but
  529. * it's not while a gc is in progress. Protected by bucket_lock.
  530. */
  531. int gc_mark_valid;
  532. /* Counts how many sectors bio_insert has added to the cache */
  533. atomic_t sectors_to_gc;
  534. wait_queue_head_t moving_gc_wait;
  535. struct keybuf moving_gc_keys;
  536. /* Number of moving GC bios in flight */
  537. struct semaphore moving_in_flight;
  538. struct btree *root;
  539. #ifdef CONFIG_BCACHE_DEBUG
  540. struct btree *verify_data;
  541. struct mutex verify_lock;
  542. #endif
  543. unsigned nr_uuids;
  544. struct uuid_entry *uuids;
  545. BKEY_PADDED(uuid_bucket);
  546. struct closure_with_waitlist uuid_write;
  547. /*
  548. * A btree node on disk could have too many bsets for an iterator to fit
  549. * on the stack - have to dynamically allocate them
  550. */
  551. mempool_t *fill_iter;
  552. /*
  553. * btree_sort() is a merge sort and requires temporary space - single
  554. * element mempool
  555. */
  556. struct mutex sort_lock;
  557. struct bset *sort;
  558. unsigned sort_crit_factor;
  559. /* List of buckets we're currently writing data to */
  560. struct list_head data_buckets;
  561. spinlock_t data_bucket_lock;
  562. struct journal journal;
  563. #define CONGESTED_MAX 1024
  564. unsigned congested_last_us;
  565. atomic_t congested;
  566. /* The rest of this all shows up in sysfs */
  567. unsigned congested_read_threshold_us;
  568. unsigned congested_write_threshold_us;
  569. struct time_stats sort_time;
  570. struct time_stats btree_gc_time;
  571. struct time_stats btree_split_time;
  572. struct time_stats btree_read_time;
  573. struct time_stats try_harder_time;
  574. atomic_long_t cache_read_races;
  575. atomic_long_t writeback_keys_done;
  576. atomic_long_t writeback_keys_failed;
  577. enum {
  578. ON_ERROR_UNREGISTER,
  579. ON_ERROR_PANIC,
  580. } on_error;
  581. unsigned error_limit;
  582. unsigned error_decay;
  583. unsigned short journal_delay_ms;
  584. unsigned verify:1;
  585. unsigned key_merging_disabled:1;
  586. unsigned expensive_debug_checks:1;
  587. unsigned gc_always_rewrite:1;
  588. unsigned shrinker_disabled:1;
  589. unsigned copy_gc_enabled:1;
  590. #define BUCKET_HASH_BITS 12
  591. struct hlist_head bucket_hash[1 << BUCKET_HASH_BITS];
  592. };
  593. struct bbio {
  594. unsigned submit_time_us;
  595. union {
  596. struct bkey key;
  597. uint64_t _pad[3];
  598. /*
  599. * We only need pad = 3 here because we only ever carry around a
  600. * single pointer - i.e. the pointer we're doing io to/from.
  601. */
  602. };
  603. struct bio bio;
  604. };
  605. static inline unsigned local_clock_us(void)
  606. {
  607. return local_clock() >> 10;
  608. }
  609. #define BTREE_PRIO USHRT_MAX
  610. #define INITIAL_PRIO 32768
  611. #define btree_bytes(c) ((c)->btree_pages * PAGE_SIZE)
  612. #define btree_blocks(b) \
  613. ((unsigned) (KEY_SIZE(&b->key) >> (b)->c->block_bits))
  614. #define btree_default_blocks(c) \
  615. ((unsigned) ((PAGE_SECTORS * (c)->btree_pages) >> (c)->block_bits))
  616. #define bucket_pages(c) ((c)->sb.bucket_size / PAGE_SECTORS)
  617. #define bucket_bytes(c) ((c)->sb.bucket_size << 9)
  618. #define block_bytes(c) ((c)->sb.block_size << 9)
  619. #define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t))
  620. #define set_bytes(i) __set_bytes(i, i->keys)
  621. #define __set_blocks(i, k, c) DIV_ROUND_UP(__set_bytes(i, k), block_bytes(c))
  622. #define set_blocks(i, c) __set_blocks(i, (i)->keys, c)
  623. #define node(i, j) ((struct bkey *) ((i)->d + (j)))
  624. #define end(i) node(i, (i)->keys)
  625. #define index(i, b) \
  626. ((size_t) (((void *) i - (void *) (b)->sets[0].data) / \
  627. block_bytes(b->c)))
  628. #define btree_data_space(b) (PAGE_SIZE << (b)->page_order)
  629. #define prios_per_bucket(c) \
  630. ((bucket_bytes(c) - sizeof(struct prio_set)) / \
  631. sizeof(struct bucket_disk))
  632. #define prio_buckets(c) \
  633. DIV_ROUND_UP((size_t) (c)->sb.nbuckets, prios_per_bucket(c))
  634. static inline size_t sector_to_bucket(struct cache_set *c, sector_t s)
  635. {
  636. return s >> c->bucket_bits;
  637. }
  638. static inline sector_t bucket_to_sector(struct cache_set *c, size_t b)
  639. {
  640. return ((sector_t) b) << c->bucket_bits;
  641. }
  642. static inline sector_t bucket_remainder(struct cache_set *c, sector_t s)
  643. {
  644. return s & (c->sb.bucket_size - 1);
  645. }
  646. static inline struct cache *PTR_CACHE(struct cache_set *c,
  647. const struct bkey *k,
  648. unsigned ptr)
  649. {
  650. return c->cache[PTR_DEV(k, ptr)];
  651. }
  652. static inline size_t PTR_BUCKET_NR(struct cache_set *c,
  653. const struct bkey *k,
  654. unsigned ptr)
  655. {
  656. return sector_to_bucket(c, PTR_OFFSET(k, ptr));
  657. }
  658. static inline struct bucket *PTR_BUCKET(struct cache_set *c,
  659. const struct bkey *k,
  660. unsigned ptr)
  661. {
  662. return PTR_CACHE(c, k, ptr)->buckets + PTR_BUCKET_NR(c, k, ptr);
  663. }
  664. /* Btree key macros */
  665. static inline void bkey_init(struct bkey *k)
  666. {
  667. *k = ZERO_KEY;
  668. }
  669. /*
  670. * This is used for various on disk data structures - cache_sb, prio_set, bset,
  671. * jset: The checksum is _always_ the first 8 bytes of these structs
  672. */
  673. #define csum_set(i) \
  674. bch_crc64(((void *) (i)) + sizeof(uint64_t), \
  675. ((void *) end(i)) - (((void *) (i)) + sizeof(uint64_t)))
  676. /* Error handling macros */
  677. #define btree_bug(b, ...) \
  678. do { \
  679. if (bch_cache_set_error((b)->c, __VA_ARGS__)) \
  680. dump_stack(); \
  681. } while (0)
  682. #define cache_bug(c, ...) \
  683. do { \
  684. if (bch_cache_set_error(c, __VA_ARGS__)) \
  685. dump_stack(); \
  686. } while (0)
  687. #define btree_bug_on(cond, b, ...) \
  688. do { \
  689. if (cond) \
  690. btree_bug(b, __VA_ARGS__); \
  691. } while (0)
  692. #define cache_bug_on(cond, c, ...) \
  693. do { \
  694. if (cond) \
  695. cache_bug(c, __VA_ARGS__); \
  696. } while (0)
  697. #define cache_set_err_on(cond, c, ...) \
  698. do { \
  699. if (cond) \
  700. bch_cache_set_error(c, __VA_ARGS__); \
  701. } while (0)
  702. /* Looping macros */
  703. #define for_each_cache(ca, cs, iter) \
  704. for (iter = 0; ca = cs->cache[iter], iter < (cs)->sb.nr_in_set; iter++)
  705. #define for_each_bucket(b, ca) \
  706. for (b = (ca)->buckets + (ca)->sb.first_bucket; \
  707. b < (ca)->buckets + (ca)->sb.nbuckets; b++)
  708. static inline void cached_dev_put(struct cached_dev *dc)
  709. {
  710. if (atomic_dec_and_test(&dc->count))
  711. schedule_work(&dc->detach);
  712. }
  713. static inline bool cached_dev_get(struct cached_dev *dc)
  714. {
  715. if (!atomic_inc_not_zero(&dc->count))
  716. return false;
  717. /* Paired with the mb in cached_dev_attach */
  718. smp_mb__after_atomic_inc();
  719. return true;
  720. }
  721. /*
  722. * bucket_gc_gen() returns the difference between the bucket's current gen and
  723. * the oldest gen of any pointer into that bucket in the btree (last_gc).
  724. *
  725. * bucket_disk_gen() returns the difference between the current gen and the gen
  726. * on disk; they're both used to make sure gens don't wrap around.
  727. */
  728. static inline uint8_t bucket_gc_gen(struct bucket *b)
  729. {
  730. return b->gen - b->last_gc;
  731. }
  732. static inline uint8_t bucket_disk_gen(struct bucket *b)
  733. {
  734. return b->gen - b->disk_gen;
  735. }
  736. #define BUCKET_GC_GEN_MAX 96U
  737. #define BUCKET_DISK_GEN_MAX 64U
  738. #define kobj_attribute_write(n, fn) \
  739. static struct kobj_attribute ksysfs_##n = __ATTR(n, S_IWUSR, NULL, fn)
  740. #define kobj_attribute_rw(n, show, store) \
  741. static struct kobj_attribute ksysfs_##n = \
  742. __ATTR(n, S_IWUSR|S_IRUSR, show, store)
  743. static inline void wake_up_allocators(struct cache_set *c)
  744. {
  745. struct cache *ca;
  746. unsigned i;
  747. for_each_cache(ca, c, i)
  748. wake_up_process(ca->alloc_thread);
  749. }
  750. /* Forward declarations */
  751. void bch_count_io_errors(struct cache *, int, const char *);
  752. void bch_bbio_count_io_errors(struct cache_set *, struct bio *,
  753. int, const char *);
  754. void bch_bbio_endio(struct cache_set *, struct bio *, int, const char *);
  755. void bch_bbio_free(struct bio *, struct cache_set *);
  756. struct bio *bch_bbio_alloc(struct cache_set *);
  757. struct bio *bch_bio_split(struct bio *, int, gfp_t, struct bio_set *);
  758. void bch_generic_make_request(struct bio *, struct bio_split_pool *);
  759. void __bch_submit_bbio(struct bio *, struct cache_set *);
  760. void bch_submit_bbio(struct bio *, struct cache_set *, struct bkey *, unsigned);
  761. uint8_t bch_inc_gen(struct cache *, struct bucket *);
  762. void bch_rescale_priorities(struct cache_set *, int);
  763. bool bch_bucket_add_unused(struct cache *, struct bucket *);
  764. long bch_bucket_alloc(struct cache *, unsigned, bool);
  765. void bch_bucket_free(struct cache_set *, struct bkey *);
  766. int __bch_bucket_alloc_set(struct cache_set *, unsigned,
  767. struct bkey *, int, bool);
  768. int bch_bucket_alloc_set(struct cache_set *, unsigned,
  769. struct bkey *, int, bool);
  770. bool bch_alloc_sectors(struct cache_set *, struct bkey *, unsigned,
  771. unsigned, unsigned, bool);
  772. __printf(2, 3)
  773. bool bch_cache_set_error(struct cache_set *, const char *, ...);
  774. void bch_prio_write(struct cache *);
  775. void bch_write_bdev_super(struct cached_dev *, struct closure *);
  776. extern struct workqueue_struct *bcache_wq;
  777. extern const char * const bch_cache_modes[];
  778. extern struct mutex bch_register_lock;
  779. extern struct list_head bch_cache_sets;
  780. extern struct kobj_type bch_cached_dev_ktype;
  781. extern struct kobj_type bch_flash_dev_ktype;
  782. extern struct kobj_type bch_cache_set_ktype;
  783. extern struct kobj_type bch_cache_set_internal_ktype;
  784. extern struct kobj_type bch_cache_ktype;
  785. void bch_cached_dev_release(struct kobject *);
  786. void bch_flash_dev_release(struct kobject *);
  787. void bch_cache_set_release(struct kobject *);
  788. void bch_cache_release(struct kobject *);
  789. int bch_uuid_write(struct cache_set *);
  790. void bcache_write_super(struct cache_set *);
  791. int bch_flash_dev_create(struct cache_set *c, uint64_t size);
  792. int bch_cached_dev_attach(struct cached_dev *, struct cache_set *);
  793. void bch_cached_dev_detach(struct cached_dev *);
  794. void bch_cached_dev_run(struct cached_dev *);
  795. void bcache_device_stop(struct bcache_device *);
  796. void bch_cache_set_unregister(struct cache_set *);
  797. void bch_cache_set_stop(struct cache_set *);
  798. struct cache_set *bch_cache_set_alloc(struct cache_sb *);
  799. void bch_btree_cache_free(struct cache_set *);
  800. int bch_btree_cache_alloc(struct cache_set *);
  801. void bch_moving_init_cache_set(struct cache_set *);
  802. int bch_open_buckets_alloc(struct cache_set *);
  803. void bch_open_buckets_free(struct cache_set *);
  804. int bch_cache_allocator_start(struct cache *ca);
  805. int bch_cache_allocator_init(struct cache *ca);
  806. void bch_debug_exit(void);
  807. int bch_debug_init(struct kobject *);
  808. void bch_request_exit(void);
  809. int bch_request_init(void);
  810. void bch_btree_exit(void);
  811. int bch_btree_init(void);
  812. #endif /* _BCACHE_H */