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