]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.h
minor disk group fixes;, add background_compression option
[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 "tools-util.h"
10 #include "libbcachefs/vstructs.h"
11
12 struct format_opts {
13         char            *label;
14         uuid_le         uuid;
15
16         unsigned        on_error_action;
17
18         unsigned        block_size;
19         unsigned        btree_node_size;
20         unsigned        encoded_extent_max;
21
22         unsigned        meta_replicas;
23         unsigned        data_replicas;
24
25         unsigned        meta_replicas_required;
26         unsigned        data_replicas_required;
27
28         const char      *foreground_target;
29         const char      *background_target;
30         const char      *promote_target;
31
32         unsigned        meta_csum_type;
33         unsigned        data_csum_type;
34         unsigned        compression_type;
35         unsigned        background_compression_type;
36
37         bool            encrypted;
38         char            *passphrase;
39 };
40
41 static inline struct format_opts format_opts_default()
42 {
43         return (struct format_opts) {
44                 .on_error_action        = BCH_ON_ERROR_RO,
45                 .encoded_extent_max     = 128,
46                 .meta_csum_type         = BCH_CSUM_OPT_CRC32C,
47                 .data_csum_type         = BCH_CSUM_OPT_CRC32C,
48                 .meta_replicas          = 1,
49                 .data_replicas          = 1,
50                 .meta_replicas_required = 1,
51                 .data_replicas_required = 1,
52         };
53 }
54
55 struct dev_opts {
56         int             fd;
57         char            *path;
58         u64             size; /* 512 byte sectors */
59         unsigned        bucket_size;
60         const char      *group;
61         unsigned        data_allowed;
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         };
75 }
76
77 void bch2_pick_bucket_size(struct format_opts, struct dev_opts *);
78 struct bch_sb *bch2_format(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 = (__u64) 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 = (__u64) 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_usage *bchu_usage(struct bchfs_handle fs)
148 {
149         struct bch_ioctl_usage *u = NULL;
150         unsigned nr_devices = 4;
151
152         while (1) {
153                 u = xrealloc(u, sizeof(*u) + sizeof(u->devs[0]) * nr_devices);
154                 u->nr_devices = nr_devices;
155
156                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_USAGE, u))
157                         return u;
158
159                 if (errno != ENOSPC)
160                         die("BCH_IOCTL_USAGE error: %m");
161                 nr_devices *= 2;
162         }
163 }
164
165 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
166 {
167         size_t size = 4096;
168         struct bch_sb *sb = NULL;
169
170         while (1) {
171                 sb = xrealloc(sb, size);
172                 struct bch_ioctl_read_super i = {
173                         .size   = size,
174                         .sb     = (u64) sb,
175                 };
176
177                 if (idx != -1) {
178                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
179                         i.dev = idx;
180                 }
181
182                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
183                         return sb;
184                 if (errno != ERANGE)
185                         die("BCH_IOCTL_READ_SUPER error: %m");
186                 size *= 2;
187         }
188 }
189
190 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
191 {
192         struct bch_ioctl_disk_get_idx i = { .dev = dev };
193
194         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
195 }
196
197 static inline void bchu_disk_resize(struct bchfs_handle fs,
198                                     unsigned idx,
199                                     u64 nbuckets)
200 {
201         struct bch_ioctl_disk_resize i = {
202                 .flags  = BCH_BY_INDEX,
203                 .dev    = idx,
204                 .nbuckets = nbuckets,
205         };
206
207         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
208 }
209
210 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
211
212 #endif /* _LIBBCACHE_H */