]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.h
Update bcachefs sources to e99d29e402 bcachefs: zstd support, compression refactoring
[bcachefs-tools-debian] / libbcachefs.h
1 #ifndef _LIBBCACHE_H
2 #define _LIBBCACHE_H
3
4 #include <linux/uuid.h>
5 #include <stdbool.h>
6
7 #include "libbcachefs/bcachefs_format.h"
8 #include "libbcachefs/bcachefs_ioctl.h"
9 #include "tools-util.h"
10 #include "libbcachefs/vstructs.h"
11
12 struct format_opts {
13         char            *label;
14         uuid_le         uuid;
15
16         unsigned        on_error_action;
17
18         unsigned        block_size;
19         unsigned        btree_node_size;
20         unsigned        encoded_extent_max;
21
22         unsigned        meta_replicas;
23         unsigned        data_replicas;
24
25         unsigned        meta_replicas_required;
26         unsigned        data_replicas_required;
27
28         unsigned        meta_csum_type;
29         unsigned        data_csum_type;
30         unsigned        compression_type;
31
32         bool            encrypted;
33         char            *passphrase;
34 };
35
36 static inline struct format_opts format_opts_default()
37 {
38         return (struct format_opts) {
39                 .on_error_action        = BCH_ON_ERROR_RO,
40                 .encoded_extent_max     = 128,
41                 .meta_csum_type         = BCH_CSUM_OPT_CRC32C,
42                 .data_csum_type         = BCH_CSUM_OPT_CRC32C,
43                 .meta_replicas          = 1,
44                 .data_replicas          = 1,
45                 .meta_replicas_required = 1,
46                 .data_replicas_required = 1,
47         };
48 }
49
50 struct dev_opts {
51         int             fd;
52         char            *path;
53         u64             size; /* 512 byte sectors */
54         unsigned        bucket_size;
55         unsigned        tier;
56         unsigned        data_allowed;
57         bool            discard;
58
59         u64             nbuckets;
60
61         u64             sb_offset;
62         u64             sb_end;
63 };
64
65 static inline struct dev_opts dev_opts_default()
66 {
67         return (struct dev_opts) {
68                 .data_allowed           = ~0U << 2,
69         };
70 }
71
72 void bch2_pick_bucket_size(struct format_opts, struct dev_opts *);
73 struct bch_sb *bch2_format(struct format_opts, struct dev_opts *, size_t);
74
75 void bch2_super_write(int, struct bch_sb *);
76 struct bch_sb *__bch2_super_read(int, u64);
77
78 void bch2_sb_print(struct bch_sb *, bool, unsigned, enum units);
79
80 /* ioctl interface: */
81
82 int bcachectl_open(void);
83
84 struct bchfs_handle {
85         uuid_le uuid;
86         int     ioctl_fd;
87         int     sysfs_fd;
88 };
89
90 void bcache_fs_close(struct bchfs_handle);
91 struct bchfs_handle bcache_fs_open(const char *);
92 struct bchfs_handle bchu_fs_open_by_dev(const char *, unsigned *);
93
94 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
95 {
96         struct bch_ioctl_disk i = { .dev = (__u64) dev, };
97
98         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
99 }
100
101 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
102                                     unsigned flags)
103 {
104         struct bch_ioctl_disk i = {
105                 .flags  = flags|BCH_BY_INDEX,
106                 .dev    = dev_idx,
107         };
108
109         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
110 }
111
112 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
113 {
114         struct bch_ioctl_disk i = { .dev = (__u64) dev, };
115
116         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
117 }
118
119 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
120                                      unsigned flags)
121 {
122         struct bch_ioctl_disk i = {
123                 .flags  = flags|BCH_BY_INDEX,
124                 .dev    = dev_idx,
125         };
126
127         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
128 }
129
130 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
131                                        unsigned new_state, unsigned flags)
132 {
133         struct bch_ioctl_disk_set_state i = {
134                 .flags          = flags|BCH_BY_INDEX,
135                 .new_state      = new_state,
136                 .dev            = dev,
137         };
138
139         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
140 }
141
142 static inline struct bch_ioctl_usage *bchu_usage(struct bchfs_handle fs)
143 {
144         struct bch_ioctl_usage *u = NULL;
145         unsigned nr_devices = 4;
146
147         while (1) {
148                 u = xrealloc(u, sizeof(*u) + sizeof(u->devs[0]) * nr_devices);
149                 u->nr_devices = nr_devices;
150
151                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_USAGE, u))
152                         return u;
153
154                 if (errno != ENOSPC)
155                         die("BCH_IOCTL_USAGE error: %m");
156                 nr_devices *= 2;
157         }
158 }
159
160 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
161 {
162         size_t size = 4096;
163         struct bch_sb *sb = NULL;
164
165         while (1) {
166                 sb = xrealloc(sb, size);
167                 struct bch_ioctl_read_super i = {
168                         .size   = size,
169                         .sb     = (u64) sb,
170                 };
171
172                 if (idx != -1) {
173                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
174                         i.dev = idx;
175                 }
176
177                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
178                         return sb;
179                 if (errno != ERANGE)
180                         die("BCH_IOCTL_READ_SUPER error: %m");
181                 size *= 2;
182         }
183 }
184
185 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
186 {
187         struct bch_ioctl_disk_get_idx i = { .dev = dev };
188
189         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
190 }
191
192 static inline void bchu_disk_resize(struct bchfs_handle fs,
193                                     unsigned idx,
194                                     u64 nbuckets)
195 {
196         struct bch_ioctl_disk_resize i = {
197                 .flags  = BCH_BY_INDEX,
198                 .dev    = idx,
199                 .nbuckets = nbuckets,
200         };
201
202         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
203 }
204
205 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
206
207 #endif /* _LIBBCACHE_H */