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