]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.h
Upload to unstable
[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 "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 block_device *bdev;
56         char            *path;
57         u64             size;           /* bytes*/
58         u64             bucket_size;    /* bytes */
59         const char      *label;
60         unsigned        data_allowed;
61         unsigned        durability;
62         bool            discard;
63
64         u64             nbuckets;
65
66         u64             sb_offset;
67         u64             sb_end;
68 };
69
70 static inline struct dev_opts dev_opts_default()
71 {
72         return (struct dev_opts) {
73                 .data_allowed           = ~0U << 2,
74                 .durability             = 1,
75         };
76 }
77
78 u64 bch2_pick_bucket_size(struct bch_opts, struct dev_opts *);
79 void bch2_check_bucket_size(struct bch_opts, struct dev_opts *);
80
81 struct bch_sb *bch2_format(struct bch_opt_strs,
82                            struct bch_opts,
83                            struct format_opts, struct dev_opts *, size_t);
84
85 void bch2_super_write(int, struct bch_sb *);
86 struct bch_sb *__bch2_super_read(int, u64);
87
88 /* ioctl interface: */
89
90 int bcachectl_open(void);
91
92 struct bchfs_handle {
93         __uuid_t        uuid;
94         int             ioctl_fd;
95         int             sysfs_fd;
96 };
97
98 void bcache_fs_close(struct bchfs_handle);
99 struct bchfs_handle bcache_fs_open(const char *);
100 struct bchfs_handle bchu_fs_open_by_dev(const char *, int *);
101 int bchu_dev_path_to_idx(struct bchfs_handle, const char *);
102
103 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
104 {
105         struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
106
107         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
108 }
109
110 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
111                                     unsigned flags)
112 {
113         struct bch_ioctl_disk i = {
114                 .flags  = flags|BCH_BY_INDEX,
115                 .dev    = dev_idx,
116         };
117
118         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
119 }
120
121 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
122 {
123         struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
124
125         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
126 }
127
128 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
129                                      unsigned flags)
130 {
131         struct bch_ioctl_disk i = {
132                 .flags  = flags|BCH_BY_INDEX,
133                 .dev    = dev_idx,
134         };
135
136         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
137 }
138
139 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
140                                        unsigned new_state, unsigned flags)
141 {
142         struct bch_ioctl_disk_set_state i = {
143                 .flags          = flags|BCH_BY_INDEX,
144                 .new_state      = new_state,
145                 .dev            = dev,
146         };
147
148         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
149 }
150
151 static inline struct bch_ioctl_fs_usage *bchu_fs_usage(struct bchfs_handle fs)
152 {
153         struct bch_ioctl_fs_usage *u = NULL;
154         size_t replica_entries_bytes = 4096;
155
156         while (1) {
157                 u = xrealloc(u, sizeof(*u) + replica_entries_bytes);
158                 u->replica_entries_bytes = replica_entries_bytes;
159
160                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_FS_USAGE, u))
161                         return u;
162
163                 if (errno != ERANGE)
164                         die("BCH_IOCTL_USAGE error: %m");
165
166                 replica_entries_bytes *= 2;
167         }
168 }
169
170 static inline struct bch_ioctl_dev_usage bchu_dev_usage(struct bchfs_handle fs,
171                                                         unsigned idx)
172 {
173         struct bch_ioctl_dev_usage i = { .dev = idx, .flags = BCH_BY_INDEX};
174
175         if (xioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE, &i))
176                 die("BCH_IOCTL_DEV_USAGE error: %m");
177         return i;
178 }
179
180 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
181 {
182         size_t size = 4096;
183         struct bch_sb *sb = NULL;
184
185         while (1) {
186                 sb = xrealloc(sb, size);
187                 struct bch_ioctl_read_super i = {
188                         .size   = size,
189                         .sb     = (unsigned long) sb,
190                 };
191
192                 if (idx != -1) {
193                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
194                         i.dev = idx;
195                 }
196
197                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
198                         return sb;
199                 if (errno != ERANGE)
200                         die("BCH_IOCTL_READ_SUPER error: %m");
201                 size *= 2;
202         }
203 }
204
205 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
206 {
207         struct bch_ioctl_disk_get_idx i = { .dev = dev };
208
209         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
210 }
211
212 static inline void bchu_disk_resize(struct bchfs_handle fs,
213                                     unsigned idx,
214                                     u64 nbuckets)
215 {
216         struct bch_ioctl_disk_resize i = {
217                 .flags  = BCH_BY_INDEX,
218                 .dev    = idx,
219                 .nbuckets = nbuckets,
220         };
221
222         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
223 }
224
225 static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
226                                             unsigned idx,
227                                             u64 nbuckets)
228 {
229         struct bch_ioctl_disk_resize i = {
230                 .flags  = BCH_BY_INDEX,
231                 .dev    = idx,
232                 .nbuckets = nbuckets,
233         };
234
235         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
236 }
237
238 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
239
240 struct dev_name {
241         unsigned        idx;
242         char            *dev;
243         char            *label;
244         uuid_t          uuid;
245 };
246 typedef DARRAY(struct dev_name) dev_names;
247
248 dev_names bchu_fs_get_devices(struct bchfs_handle);
249
250 #endif /* _LIBBCACHE_H */