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