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 bdev_handle *handle;
56 struct block_device *bdev;
59 u64 bucket_size; /* bytes */
61 unsigned data_allowed;
71 static inline struct dev_opts dev_opts_default()
73 return (struct dev_opts) {
74 .data_allowed = ~0U << 2,
79 u64 bch2_pick_bucket_size(struct bch_opts, struct dev_opts *);
80 void bch2_check_bucket_size(struct bch_opts, struct dev_opts *);
82 struct bch_sb *bch2_format(struct bch_opt_strs,
84 struct format_opts, struct dev_opts *, size_t);
86 void bch2_super_write(int, struct bch_sb *);
87 struct bch_sb *__bch2_super_read(int, u64);
89 /* ioctl interface: */
91 int bcachectl_open(void);
99 void bcache_fs_close(struct bchfs_handle);
100 struct bchfs_handle bcache_fs_open(const char *);
101 struct bchfs_handle bchu_fs_open_by_dev(const char *, int *);
102 int bchu_dev_path_to_idx(struct bchfs_handle, const char *);
104 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
106 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
108 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
111 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
114 struct bch_ioctl_disk i = {
115 .flags = flags|BCH_BY_INDEX,
119 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
122 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
124 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
126 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
129 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
132 struct bch_ioctl_disk i = {
133 .flags = flags|BCH_BY_INDEX,
137 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
140 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
141 unsigned new_state, unsigned flags)
143 struct bch_ioctl_disk_set_state i = {
144 .flags = flags|BCH_BY_INDEX,
145 .new_state = new_state,
149 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
152 static inline struct bch_ioctl_fs_usage *bchu_fs_usage(struct bchfs_handle fs)
154 struct bch_ioctl_fs_usage *u = NULL;
155 size_t replica_entries_bytes = 4096;
158 u = xrealloc(u, sizeof(*u) + replica_entries_bytes);
159 u->replica_entries_bytes = replica_entries_bytes;
161 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_FS_USAGE, u))
165 die("BCH_IOCTL_USAGE error: %m");
167 replica_entries_bytes *= 2;
171 static inline struct bch_ioctl_dev_usage_v2 *bchu_dev_usage(struct bchfs_handle fs,
174 struct bch_ioctl_dev_usage_v2 *u = xcalloc(sizeof(*u) + sizeof(u->d[0]) * BCH_DATA_NR, 1);
177 u->flags = BCH_BY_INDEX;
178 u->nr_data_types = BCH_DATA_NR;
180 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE_V2, u))
183 struct bch_ioctl_dev_usage u_v1 = { .dev = idx, .flags = BCH_BY_INDEX};
184 xioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE, &u_v1);
186 u->state = u_v1.state;
187 u->nr_data_types = ARRAY_SIZE(u_v1.d);
188 u->bucket_size = u_v1.bucket_size;
189 u->nr_buckets = u_v1.nr_buckets;
191 for (unsigned i = 0; i < ARRAY_SIZE(u_v1.d); i++)
197 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
200 struct bch_sb *sb = NULL;
203 sb = xrealloc(sb, size);
204 struct bch_ioctl_read_super i = {
206 .sb = (unsigned long) sb,
210 i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
214 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
217 die("BCH_IOCTL_READ_SUPER error: %m");
222 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
224 struct bch_ioctl_disk_get_idx i = { .dev = dev };
226 return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
229 static inline void bchu_disk_resize(struct bchfs_handle fs,
233 struct bch_ioctl_disk_resize i = {
234 .flags = BCH_BY_INDEX,
236 .nbuckets = nbuckets,
239 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
242 static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
246 struct bch_ioctl_disk_resize i = {
247 .flags = BCH_BY_INDEX,
249 .nbuckets = nbuckets,
252 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
255 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
264 typedef DARRAY(struct dev_name) dev_names;
266 dev_names bchu_fs_get_devices(struct bchfs_handle);
268 #endif /* _LIBBCACHE_H */