]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.h
Use opts.h for format options
[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 };
19 struct {
20 #define x(_name, ...)   char    *_name;
21         BCH_OPTS()
22 #undef x
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_usage *bchu_usage(struct bchfs_handle fs)
144 {
145         struct bch_ioctl_usage *u = NULL;
146         unsigned nr_devices = 4;
147
148         while (1) {
149                 u = xrealloc(u, sizeof(*u) + sizeof(u->devs[0]) * nr_devices);
150                 u->nr_devices = nr_devices;
151
152                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_USAGE, u))
153                         return u;
154
155                 if (errno != ENOSPC)
156                         die("BCH_IOCTL_USAGE error: %m");
157                 nr_devices *= 2;
158         }
159 }
160
161 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
162 {
163         size_t size = 4096;
164         struct bch_sb *sb = NULL;
165
166         while (1) {
167                 sb = xrealloc(sb, size);
168                 struct bch_ioctl_read_super i = {
169                         .size   = size,
170                         .sb     = (unsigned long) sb,
171                 };
172
173                 if (idx != -1) {
174                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
175                         i.dev = idx;
176                 }
177
178                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
179                         return sb;
180                 if (errno != ERANGE)
181                         die("BCH_IOCTL_READ_SUPER error: %m");
182                 size *= 2;
183         }
184 }
185
186 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
187 {
188         struct bch_ioctl_disk_get_idx i = { .dev = dev };
189
190         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
191 }
192
193 static inline void bchu_disk_resize(struct bchfs_handle fs,
194                                     unsigned idx,
195                                     u64 nbuckets)
196 {
197         struct bch_ioctl_disk_resize i = {
198                 .flags  = BCH_BY_INDEX,
199                 .dev    = idx,
200                 .nbuckets = nbuckets,
201         };
202
203         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
204 }
205
206 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
207
208 #endif /* _LIBBCACHE_H */