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