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