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