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,
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 void bch2_pick_bucket_size(struct bch_opts, struct dev_opts *);
79 struct bch_sb *bch2_format(struct bch_opt_strs,
81 struct format_opts, struct dev_opts *, size_t);
83 void bch2_super_write(int, struct bch_sb *);
84 struct bch_sb *__bch2_super_read(int, u64);
86 /* ioctl interface: */
88 int bcachectl_open(void);
96 void bcache_fs_close(struct bchfs_handle);
97 struct bchfs_handle bcache_fs_open(const char *);
98 struct bchfs_handle bchu_fs_open_by_dev(const char *, int *);
99 int bchu_dev_path_to_idx(struct bchfs_handle, const char *);
101 static inline void bchu_disk_add(struct bchfs_handle fs, char *dev)
103 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
105 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
108 static inline void bchu_disk_remove(struct bchfs_handle fs, unsigned dev_idx,
111 struct bch_ioctl_disk i = {
112 .flags = flags|BCH_BY_INDEX,
116 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &i);
119 static inline void bchu_disk_online(struct bchfs_handle fs, char *dev)
121 struct bch_ioctl_disk i = { .dev = (unsigned long) dev, };
123 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ONLINE, &i);
126 static inline void bchu_disk_offline(struct bchfs_handle fs, unsigned dev_idx,
129 struct bch_ioctl_disk i = {
130 .flags = flags|BCH_BY_INDEX,
134 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_OFFLINE, &i);
137 static inline void bchu_disk_set_state(struct bchfs_handle fs, unsigned dev,
138 unsigned new_state, unsigned flags)
140 struct bch_ioctl_disk_set_state i = {
141 .flags = flags|BCH_BY_INDEX,
142 .new_state = new_state,
146 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
149 static inline struct bch_ioctl_fs_usage *bchu_fs_usage(struct bchfs_handle fs)
151 struct bch_ioctl_fs_usage *u = NULL;
152 size_t replica_entries_bytes = 4096;
155 u = xrealloc(u, sizeof(*u) + replica_entries_bytes);
156 u->replica_entries_bytes = replica_entries_bytes;
158 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_FS_USAGE, u))
162 die("BCH_IOCTL_USAGE error: %m");
164 replica_entries_bytes *= 2;
168 static inline struct bch_ioctl_dev_usage bchu_dev_usage(struct bchfs_handle fs,
171 struct bch_ioctl_dev_usage i = { .dev = idx, .flags = BCH_BY_INDEX};
173 if (xioctl(fs.ioctl_fd, BCH_IOCTL_DEV_USAGE, &i))
174 die("BCH_IOCTL_DEV_USAGE error: %m");
178 static inline struct bch_sb *bchu_read_super(struct bchfs_handle fs, unsigned idx)
181 struct bch_sb *sb = NULL;
184 sb = xrealloc(sb, size);
185 struct bch_ioctl_read_super i = {
187 .sb = (unsigned long) sb,
191 i.flags |= BCH_READ_DEV|BCH_BY_INDEX;
195 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_READ_SUPER, &i))
198 die("BCH_IOCTL_READ_SUPER error: %m");
203 static inline unsigned bchu_disk_get_idx(struct bchfs_handle fs, dev_t dev)
205 struct bch_ioctl_disk_get_idx i = { .dev = dev };
207 return xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_GET_IDX, &i);
210 static inline void bchu_disk_resize(struct bchfs_handle fs,
214 struct bch_ioctl_disk_resize i = {
215 .flags = BCH_BY_INDEX,
217 .nbuckets = nbuckets,
220 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i);
223 static inline void bchu_disk_resize_journal(struct bchfs_handle fs,
227 struct bch_ioctl_disk_resize i = {
228 .flags = BCH_BY_INDEX,
230 .nbuckets = nbuckets,
233 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i);
236 int bchu_data(struct bchfs_handle, struct bch_ioctl_data);
244 typedef DARRAY(struct dev_name) dev_names;
246 dev_names bchu_fs_get_devices(struct bchfs_handle);
248 #endif /* _LIBBCACHE_H */