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