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