4 #include <linux/uuid.h>
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"
15 #define SUPERBLOCK_SIZE_DEFAULT 2048 /* 1 MB */
19 char *by_id[bch2_opts_nr];
21 #define x(_name, ...) char *_name;
28 void bch2_opt_strs_free(struct bch_opt_strs *);
29 struct bch_opt_strs bch2_cmdline_opts_get(int *, char *[], unsigned);
30 struct bch_opts bch2_parse_opts(struct bch_opt_strs);
31 void bch2_opts_usage(unsigned);
37 unsigned superblock_size;
42 static inline struct format_opts format_opts_default()
44 unsigned version = !access( "/sys/module/bcachefs/parameters/version", R_OK)
45 ? read_file_u64(AT_FDCWD, "/sys/module/bcachefs/parameters/version")
46 : bcachefs_metadata_version_current;
48 return (struct format_opts) {
50 .superblock_size = SUPERBLOCK_SIZE_DEFAULT,
55 struct block_device *bdev;
58 u64 bucket_size; /* bytes */
60 unsigned data_allowed;
70 static inline struct dev_opts dev_opts_default()
72 return (struct dev_opts) {
73 .data_allowed = ~0U << 2,
78 u64 bch2_pick_bucket_size(struct bch_opts, struct dev_opts *);
79 void bch2_check_bucket_size(struct bch_opts, struct dev_opts *);
81 struct bch_sb *bch2_format(struct bch_opt_strs,
83 struct format_opts, struct dev_opts *, size_t);
85 void bch2_super_write(int, struct bch_sb *);
86 struct bch_sb *__bch2_super_read(int, u64);
88 /* ioctl interface: */
90 int bcachectl_open(void);
98 void bcache_fs_close(struct bchfs_handle);
99 struct bchfs_handle bcache_fs_open(const char *);
100 struct bchfs_handle bchu_fs_open_by_dev(const char *, int *);
101 int bchu_dev_path_to_idx(struct bchfs_handle, const char *);
103 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
105 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
107 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
110 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
113 struct bch_ioctl_disk i = {
114 .flags = flags|BCH_BY_INDEX,
118 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
121 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
123 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
125 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
128 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
131 struct bch_ioctl_disk i = {
132 .flags = flags|BCH_BY_INDEX,
136 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
139 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
140 unsigned new_state, unsigned flags)
142 struct bch_ioctl_disk_set_state i = {
143 .flags = flags|BCH_BY_INDEX,
144 .new_state = new_state,
148 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
151 static inline struct bch_ioctl_fs_usage *bchu_fs_usage(struct bchfs_handle fs)
153 struct bch_ioctl_fs_usage *u = NULL;
154 size_t replica_entries_bytes = 4096;
157 u = xrealloc(u, sizeof(*u) + replica_entries_bytes);
158 u->replica_entries_bytes = replica_entries_bytes;
160 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_FS_USAGE, u))
164 die("BCH_IOCTL_USAGE error: %m");
166 replica_entries_bytes *= 2;
170 static inline struct bch_ioctl_dev_usage_v2 *bchu_dev_usage(struct bchfs_handle fs,
173 struct bch_ioctl_dev_usage_v2 *u = xcalloc(sizeof(*u) + sizeof(u->d[0]) * BCH_DATA_NR, 1);
176 u->flags = BCH_BY_INDEX;
177 u->nr_data_types = BCH_DATA_NR;
179 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE_V2, u))
182 struct bch_ioctl_dev_usage u_v1 = { .dev = idx, .flags = BCH_BY_INDEX};
183 xioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE, &u_v1);
185 u->state = u_v1.state;
186 u->nr_data_types = ARRAY_SIZE(u_v1.d);
187 u->bucket_size = u_v1.bucket_size;
188 u->nr_buckets = u_v1.nr_buckets;
190 for (unsigned i = 0; i < ARRAY_SIZE(u_v1.d); i++)
196 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
199 struct bch_sb *sb = NULL;
202 sb = xrealloc(sb, size);
203 struct bch_ioctl_read_super i = {
205 .sb = (unsigned long) sb,
209 i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
213 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
216 die("BCH_IOCTL_READ_SUPER error: %m");
221 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
223 struct bch_ioctl_disk_get_idx i = { .dev = dev };
225 return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
228 static inline void bchu_disk_resize(struct bchfs_handle fs,
232 struct bch_ioctl_disk_resize i = {
233 .flags = BCH_BY_INDEX,
235 .nbuckets = nbuckets,
238 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
241 static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
245 struct bch_ioctl_disk_resize i = {
246 .flags = BCH_BY_INDEX,
248 .nbuckets = nbuckets,
251 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
254 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
263 typedef DARRAY(struct dev_name) dev_names;
265 dev_names bchu_fs_get_devices(struct bchfs_handle);
267 #endif /* _LIBBCACHE_H */