]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/super.h
014d7aed50a2e17a77b08ae463aa9d941ace3398
[bcachefs-tools-debian] / libbcache / super.h
1 #ifndef _BCACHE_SUPER_H
2 #define _BCACHE_SUPER_H
3
4 #include "extents.h"
5
6 static inline size_t sector_to_bucket(const struct cache *ca, sector_t s)
7 {
8         return s >> ca->bucket_bits;
9 }
10
11 static inline sector_t bucket_to_sector(const struct cache *ca, size_t b)
12 {
13         return ((sector_t) b) << ca->bucket_bits;
14 }
15
16 static inline sector_t bucket_remainder(const struct cache *ca, sector_t s)
17 {
18         return s & (ca->mi.bucket_size - 1);
19 }
20
21 static inline struct cache *bch_next_cache_rcu(struct cache_set *c,
22                                                unsigned *iter)
23 {
24         struct cache *ret = NULL;
25
26         while (*iter < c->sb.nr_devices &&
27                !(ret = rcu_dereference(c->cache[*iter])))
28                 (*iter)++;
29
30         return ret;
31 }
32
33 #define for_each_cache_rcu(ca, c, iter)                                 \
34         for ((iter) = 0; ((ca) = bch_next_cache_rcu((c), &(iter))); (iter)++)
35
36 static inline struct cache *bch_get_next_cache(struct cache_set *c,
37                                                unsigned *iter)
38 {
39         struct cache *ret;
40
41         rcu_read_lock();
42         if ((ret = bch_next_cache_rcu(c, iter)))
43                 percpu_ref_get(&ret->ref);
44         rcu_read_unlock();
45
46         return ret;
47 }
48
49 /*
50  * If you break early, you must drop your ref on the current cache
51  */
52 #define for_each_cache(ca, c, iter)                                     \
53         for ((iter) = 0;                                                \
54              (ca = bch_get_next_cache(c, &(iter)));                     \
55              percpu_ref_put(&ca->ref), (iter)++)
56
57 static inline bool bch_cache_may_remove(struct cache *ca)
58 {
59         struct cache_set *c = ca->set;
60         struct cache_group *tier = &c->cache_tiers[ca->mi.tier];
61
62         /*
63          * Right now, we can't remove the last device from a tier,
64          * - For tier 0, because all metadata lives in tier 0 and because
65          *   there is no way to have foreground writes go directly to tier 1.
66          * - For tier 1, because the code doesn't completely support an
67          *   empty tier 1.
68          */
69
70         /*
71          * Turning a device read-only removes it from the cache group,
72          * so there may only be one read-write device in a tier, and yet
73          * the device we are removing is in the same tier, so we have
74          * to check for identity.
75          * Removing the last RW device from a tier requires turning the
76          * whole cache set RO.
77          */
78
79         return tier->nr_devices != 1 ||
80                 rcu_access_pointer(tier->d[0].dev) != ca;
81 }
82
83 void bch_cache_set_release(struct kobject *);
84 void bch_cache_release(struct kobject *);
85
86 void bch_cache_set_unregister(struct cache_set *);
87 void bch_cache_set_stop(struct cache_set *);
88
89 const char *bch_register_one(const char *path);
90 const char *bch_register_cache_set(char * const *, unsigned,
91                                    struct cache_set_opts,
92                                    struct cache_set **);
93
94 bool bch_cache_set_read_only(struct cache_set *);
95 bool bch_cache_set_emergency_read_only(struct cache_set *);
96 void bch_cache_set_read_only_sync(struct cache_set *);
97 const char *bch_cache_set_read_write(struct cache_set *);
98
99 bool bch_cache_read_only(struct cache *);
100 const char *bch_cache_read_write(struct cache *);
101 bool bch_cache_remove(struct cache *, bool force);
102 int bch_cache_set_add_cache(struct cache_set *, const char *);
103
104 extern struct mutex bch_register_lock;
105 extern struct list_head bch_cache_sets;
106 extern struct idr bch_cache_set_minor;
107 extern struct workqueue_struct *bcache_io_wq;
108 extern struct crypto_shash *bch_sha256;
109
110 extern struct kobj_type bch_cache_set_ktype;
111 extern struct kobj_type bch_cache_set_internal_ktype;
112 extern struct kobj_type bch_cache_set_time_stats_ktype;
113 extern struct kobj_type bch_cache_set_opts_dir_ktype;
114 extern struct kobj_type bch_cache_ktype;
115
116 #endif /* _BCACHE_SUPER_H */