]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/libbcachefs.h
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / c_src / 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 "libbcachefs/opts.h"
10 #include "libbcachefs/vstructs.h"
11 #include "tools-util.h"
12
13 /* option parsing */
14
15 #define SUPERBLOCK_SIZE_DEFAULT         2048    /* 1 MB */
16
17 struct bch_opt_strs {
18 union {
19         char                    *by_id[bch2_opts_nr];
20 struct {
21 #define x(_name, ...)   char    *_name;
22         BCH_OPTS()
23 #undef x
24 };
25 };
26 };
27
28 void bch2_opt_strs_free(struct bch_opt_strs *);
29 struct bch_opt_strs bch2_cmdline_opts_get(int *, char *[], unsigned);
30 struct bch_opts bch2_parse_opts(struct bch_opt_strs);
31 void bch2_opts_usage(unsigned);
32
33 struct format_opts {
34         char            *label;
35         __uuid_t        uuid;
36         unsigned        version;
37         unsigned        superblock_size;
38         bool            encrypted;
39         char            *passphrase;
40 };
41
42 static inline struct format_opts format_opts_default()
43 {
44         unsigned version = !access(   "/sys/module/bcachefs/parameters/version", R_OK)
45             ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
46             : bcachefs_metadata_version_current;
47
48         return (struct format_opts) {
49                 .version                = version,
50                 .superblock_size        = SUPERBLOCK_SIZE_DEFAULT,
51         };
52 }
53
54 struct dev_opts {
55         struct bdev_handle *handle;
56         struct block_device *bdev;
57         char            *path;
58         u64             size;           /* bytes*/
59         u64             bucket_size;    /* bytes */
60         const char      *label;
61         unsigned        data_allowed;
62         unsigned        durability;
63         bool            discard;
64
65         u64             nbuckets;
66
67         u64             sb_offset;
68         u64             sb_end;
69 };
70
71 static inline struct dev_opts dev_opts_default()
72 {
73         return (struct dev_opts) {
74                 .data_allowed           = ~0U << 2,
75                 .durability             = 1,
76         };
77 }
78
79 u64 bch2_pick_bucket_size(struct bch_opts, struct dev_opts *);
80 void bch2_check_bucket_size(struct bch_opts, struct dev_opts *);
81
82 struct bch_sb *bch2_format(struct bch_opt_strs,
83                            struct bch_opts,
84                            struct format_opts, struct dev_opts *, size_t);
85
86 void bch2_super_write(int, struct bch_sb *);
87 struct bch_sb *__bch2_super_read(int, u64);
88
89 /* ioctl interface: */
90
91 int bcachectl_open(void);
92
93 struct bchfs_handle {
94         __uuid_t        uuid;
95         int             ioctl_fd;
96         int             sysfs_fd;
97 };
98
99 void bcache_fs_close(struct bchfs_handle);
100 struct bchfs_handle bcache_fs_open(const char *);
101 struct bchfs_handle bchu_fs_open_by_dev(const char *, int *);
102 int bchu_dev_path_to_idx(struct bchfs_handle, const char *);
103
104 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
105 {
106         struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
107
108         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
109 }
110
111 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
112                                     unsigned flags)
113 {
114         struct bch_ioctl_disk i = {
115                 .flags  = flags|BCH_BY_INDEX,
116                 .dev    = dev_idx,
117         };
118
119         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
120 }
121
122 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
123 {
124         struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
125
126         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
127 }
128
129 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
130                                      unsigned flags)
131 {
132         struct bch_ioctl_disk i = {
133                 .flags  = flags|BCH_BY_INDEX,
134                 .dev    = dev_idx,
135         };
136
137         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
138 }
139
140 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
141                                        unsigned new_state, unsigned flags)
142 {
143         struct bch_ioctl_disk_set_state i = {
144                 .flags          = flags|BCH_BY_INDEX,
145                 .new_state      = new_state,
146                 .dev            = dev,
147         };
148
149         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
150 }
151
152 static inline struct bch_ioctl_fs_usage *bchu_fs_usage(struct bchfs_handle fs)
153 {
154         struct bch_ioctl_fs_usage *u = NULL;
155         size_t replica_entries_bytes = 4096;
156
157         while (1) {
158                 u = xrealloc(u, sizeof(*u) + replica_entries_bytes);
159                 u->replica_entries_bytes = replica_entries_bytes;
160
161                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_FS_USAGE, u))
162                         return u;
163
164                 if (errno != ERANGE)
165                         die("BCH_IOCTL_USAGE error: %m");
166
167                 replica_entries_bytes *= 2;
168         }
169 }
170
171 static inline struct bch_ioctl_dev_usage_v2 *bchu_dev_usage(struct bchfs_handle fs,
172                                                             unsigned idx)
173 {
174         struct bch_ioctl_dev_usage_v2 *u = xcalloc(sizeof(*u) + sizeof(u->d[0]) * BCH_DATA_NR, 1);
175
176         u->dev                  = idx;
177         u->flags                = BCH_BY_INDEX;
178         u->nr_data_types        = BCH_DATA_NR;
179
180         if (!ioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE_V2, u))
181                 return u;
182
183         struct bch_ioctl_dev_usage u_v1 = { .dev = idx, .flags = BCH_BY_INDEX};
184         xioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE, &u_v1);
185
186         u->state        = u_v1.state;
187         u->nr_data_types = ARRAY_SIZE(u_v1.d);
188         u->bucket_size  = u_v1.bucket_size;
189         u->nr_buckets   = u_v1.nr_buckets;
190
191         for (unsigned i = 0; i < ARRAY_SIZE(u_v1.d); i++)
192                 u->d[i] = u_v1.d[i];
193
194         return u;
195 }
196
197 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
198 {
199         size_t size = 4096;
200         struct bch_sb *sb = NULL;
201
202         while (1) {
203                 sb = xrealloc(sb, size);
204                 struct bch_ioctl_read_super i = {
205                         .size   = size,
206                         .sb     = (unsigned long) sb,
207                 };
208
209                 if (idx != -1) {
210                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
211                         i.dev = idx;
212                 }
213
214                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
215                         return sb;
216                 if (errno != ERANGE)
217                         die("BCH_IOCTL_READ_SUPER error: %m");
218                 size *= 2;
219         }
220 }
221
222 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
223 {
224         struct bch_ioctl_disk_get_idx i = { .dev = dev };
225
226         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
227 }
228
229 static inline void bchu_disk_resize(struct bchfs_handle fs,
230                                     unsigned idx,
231                                     u64 nbuckets)
232 {
233         struct bch_ioctl_disk_resize i = {
234                 .flags  = BCH_BY_INDEX,
235                 .dev    = idx,
236                 .nbuckets = nbuckets,
237         };
238
239         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
240 }
241
242 static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
243                                             unsigned idx,
244                                             u64 nbuckets)
245 {
246         struct bch_ioctl_disk_resize i = {
247                 .flags  = BCH_BY_INDEX,
248                 .dev    = idx,
249                 .nbuckets = nbuckets,
250         };
251
252         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
253 }
254
255 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
256
257 struct dev_name {
258         unsigned        idx;
259         char            *dev;
260         char            *label;
261         uuid_t          uuid;
262         unsigned        durability;
263 };
264 typedef DARRAY(struct dev_name) dev_names;
265
266 dev_names bchu_fs_get_devices(struct bchfs_handle);
267
268 #endif /* _LIBBCACHE_H */