]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.h
disk groups
[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
36         bool            encrypted;
37         char            *passphrase;
38 };
39
40 static inline struct format_opts format_opts_default()
41 {
42         return (struct format_opts) {
43                 .on_error_action        = BCH_ON_ERROR_RO,
44                 .encoded_extent_max     = 128,
45                 .meta_csum_type         = BCH_CSUM_OPT_CRC32C,
46                 .data_csum_type         = BCH_CSUM_OPT_CRC32C,
47                 .meta_replicas          = 1,
48                 .data_replicas          = 1,
49                 .meta_replicas_required = 1,
50                 .data_replicas_required = 1,
51         };
52 }
53
54 struct dev_opts {
55         int             fd;
56         char            *path;
57         u64             size; /* 512 byte sectors */
58         unsigned        bucket_size;
59         const char      *group;
60         unsigned        data_allowed;
61         bool            discard;
62
63         u64             nbuckets;
64
65         u64             sb_offset;
66         u64             sb_end;
67 };
68
69 static inline struct dev_opts dev_opts_default()
70 {
71         return (struct dev_opts) {
72                 .data_allowed           = ~0U << 2,
73         };
74 }
75
76 void bch2_pick_bucket_size(struct format_opts, struct dev_opts *);
77 struct bch_sb *bch2_format(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 void bch2_sb_print(struct bch_sb *, bool, unsigned, enum units);
83
84 /* ioctl interface: */
85
86 int bcachectl_open(void);
87
88 struct bchfs_handle {
89         uuid_le uuid;
90         int     ioctl_fd;
91         int     sysfs_fd;
92 };
93
94 void bcache_fs_close(struct bchfs_handle);
95 struct bchfs_handle bcache_fs_open(const char *);
96 struct bchfs_handle bchu_fs_open_by_dev(const char *, unsigned *);
97
98 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
99 {
100         struct bch_ioctl_disk i = { .dev = (__u64) dev, };
101
102         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
103 }
104
105 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
106                                     unsigned flags)
107 {
108         struct bch_ioctl_disk i = {
109                 .flags  = flags|BCH_BY_INDEX,
110                 .dev    = dev_idx,
111         };
112
113         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
114 }
115
116 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
117 {
118         struct bch_ioctl_disk i = { .dev = (__u64) dev, };
119
120         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
121 }
122
123 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
124                                      unsigned flags)
125 {
126         struct bch_ioctl_disk i = {
127                 .flags  = flags|BCH_BY_INDEX,
128                 .dev    = dev_idx,
129         };
130
131         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
132 }
133
134 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
135                                        unsigned new_state, unsigned flags)
136 {
137         struct bch_ioctl_disk_set_state i = {
138                 .flags          = flags|BCH_BY_INDEX,
139                 .new_state      = new_state,
140                 .dev            = dev,
141         };
142
143         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
144 }
145
146 static inline struct bch_ioctl_usage *bchu_usage(struct bchfs_handle fs)
147 {
148         struct bch_ioctl_usage *u = NULL;
149         unsigned nr_devices = 4;
150
151         while (1) {
152                 u = xrealloc(u, sizeof(*u) + sizeof(u->devs[0]) * nr_devices);
153                 u->nr_devices = nr_devices;
154
155                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_USAGE, u))
156                         return u;
157
158                 if (errno != ENOSPC)
159                         die("BCH_IOCTL_USAGE error: %m");
160                 nr_devices *= 2;
161         }
162 }
163
164 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
165 {
166         size_t size = 4096;
167         struct bch_sb *sb = NULL;
168
169         while (1) {
170                 sb = xrealloc(sb, size);
171                 struct bch_ioctl_read_super i = {
172                         .size   = size,
173                         .sb     = (u64) sb,
174                 };
175
176                 if (idx != -1) {
177                         i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
178                         i.dev = idx;
179                 }
180
181                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
182                         return sb;
183                 if (errno != ERANGE)
184                         die("BCH_IOCTL_READ_SUPER error: %m");
185                 size *= 2;
186         }
187 }
188
189 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
190 {
191         struct bch_ioctl_disk_get_idx i = { .dev = dev };
192
193         return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
194 }
195
196 static inline void bchu_disk_resize(struct bchfs_handle fs,
197                                     unsigned idx,
198                                     u64 nbuckets)
199 {
200         struct bch_ioctl_disk_resize i = {
201                 .flags  = BCH_BY_INDEX,
202                 .dev    = idx,
203                 .nbuckets = nbuckets,
204         };
205
206         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
207 }
208
209 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
210
211 #endif /* _LIBBCACHE_H */