5 #include <sys/statvfs.h>
7 #include <fuse_lowlevel.h>
10 #include "libbcachefs.h"
11 #include "tools-util.h"
13 #include "libbcachefs/bcachefs.h"
14 #include "libbcachefs/btree_iter.h"
15 #include "libbcachefs/buckets.h"
16 #include "libbcachefs/dirent.h"
17 #include "libbcachefs/error.h"
18 #include "libbcachefs/fs-common.h"
19 #include "libbcachefs/inode.h"
20 #include "libbcachefs/opts.h"
21 #include "libbcachefs/super.h"
24 #include "libbcachefs/fs.h"
26 #include <linux/dcache.h>
28 /* XXX cut and pasted from fsck.c */
29 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
31 static inline u64 map_root_ino(u64 ino)
33 return ino == 1 ? 4096 : ino;
36 static inline u64 unmap_root_ino(u64 ino)
38 return ino == 4096 ? 1 : ino;
41 static struct stat inode_to_stat(struct bch_fs *c,
42 struct bch_inode_unpacked *bi)
44 return (struct stat) {
45 .st_size = bi->bi_size,
46 .st_mode = bi->bi_mode,
49 .st_nlink = bch2_inode_nlink_get(bi),
50 .st_rdev = bi->bi_dev,
51 .st_blksize = block_bytes(c),
52 .st_blocks = bi->bi_sectors,
53 .st_atim = bch2_time_to_timespec(c, bi->bi_atime),
54 .st_mtim = bch2_time_to_timespec(c, bi->bi_mtime),
55 .st_ctim = bch2_time_to_timespec(c, bi->bi_ctime),
59 static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
60 struct bch_inode_unpacked *bi)
62 return (struct fuse_entry_param) {
64 .generation = bi->bi_generation,
65 .attr = inode_to_stat(c, bi),
66 .attr_timeout = DBL_MAX,
67 .entry_timeout = DBL_MAX,
71 static void bcachefs_fuse_destroy(void *arg)
73 struct bch_fs *c = arg;
78 static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir,
81 struct bch_fs *c = fuse_req_userdata(req);
82 struct bch_inode_unpacked bi;
83 struct qstr qstr = QSTR(name);
87 dir = map_root_ino(dir);
89 pr_info("dir %llu name %s", (u64) dir, name);
91 inum = bch2_dirent_lookup(c, dir, &qstr);
97 ret = bch2_inode_find_by_inum(c, inum, &bi);
101 bi.bi_inum = unmap_root_ino(bi.bi_inum);
103 struct fuse_entry_param e = inode_to_entry(c, &bi);
104 fuse_reply_entry(req, &e);
107 fuse_reply_err(req, -ret);
110 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t inum,
111 struct fuse_file_info *fi)
113 struct bch_fs *c = fuse_req_userdata(req);
114 struct bch_inode_unpacked bi;
118 inum = map_root_ino(inum);
120 pr_info("inum %llu", (u64) inum);
122 ret = bch2_inode_find_by_inum(c, inum, &bi);
124 fuse_reply_err(req, -ret);
128 bi.bi_inum = unmap_root_ino(bi.bi_inum);
130 attr = inode_to_stat(c, &bi);
131 fuse_reply_attr(req, &attr, DBL_MAX);
134 static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t inum,
135 struct stat *attr, int to_set,
136 struct fuse_file_info *fi)
138 struct bch_fs *c = fuse_req_userdata(req);
139 struct bch_inode_unpacked inode_u;
140 struct btree_trans trans;
141 struct btree_iter *iter;
145 inum = map_root_ino(inum);
147 bch2_trans_init(&trans, c, 0, 0);
149 bch2_trans_begin(&trans);
150 now = bch2_current_time(c);
152 iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
153 ret = PTR_ERR_OR_ZERO(iter);
157 if (to_set & FUSE_SET_ATTR_MODE)
158 inode_u.bi_mode = attr->st_mode;
159 if (to_set & FUSE_SET_ATTR_UID)
160 inode_u.bi_uid = attr->st_uid;
161 if (to_set & FUSE_SET_ATTR_GID)
162 inode_u.bi_gid = attr->st_gid;
163 if (to_set & FUSE_SET_ATTR_SIZE)
164 inode_u.bi_size = attr->st_size;
165 if (to_set & FUSE_SET_ATTR_ATIME)
166 inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
167 if (to_set & FUSE_SET_ATTR_MTIME)
168 inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
169 if (to_set & FUSE_SET_ATTR_ATIME_NOW)
170 inode_u.bi_atime = now;
171 if (to_set & FUSE_SET_ATTR_MTIME_NOW)
172 inode_u.bi_mtime = now;
174 ret = bch2_inode_write(&trans, iter, &inode_u) ?:
175 bch2_trans_commit(&trans, NULL, NULL,
177 BTREE_INSERT_NOFAIL);
182 bch2_trans_exit(&trans);
185 *attr = inode_to_stat(c, &inode_u);
186 fuse_reply_attr(req, attr, DBL_MAX);
188 fuse_reply_err(req, -ret);
192 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
194 //struct bch_fs *c = fuse_req_userdata(req);
196 //char *link = malloc();
198 //fuse_reply_readlink(req, link);
201 static int do_create(struct bch_fs *c, u64 dir,
202 const char *name, mode_t mode, dev_t rdev,
203 struct bch_inode_unpacked *new_inode)
205 struct qstr qstr = QSTR(name);
206 struct bch_inode_unpacked dir_u;
208 dir = map_root_ino(dir);
210 bch2_inode_init_early(c, new_inode);
212 return bch2_trans_do(c, NULL, 0,
213 bch2_create_trans(&trans,
216 0, 0, mode, rdev, NULL, NULL));
219 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
220 const char *name, mode_t mode,
223 struct bch_fs *c = fuse_req_userdata(req);
224 struct bch_inode_unpacked new_inode;
227 ret = do_create(c, dir, name, mode, rdev, &new_inode);
231 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
232 fuse_reply_entry(req, &e);
235 fuse_reply_err(req, -ret);
238 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
239 const char *name, mode_t mode)
241 bcachefs_fuse_mknod(req, dir, name, mode, 0);
244 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
247 struct bch_fs *c = fuse_req_userdata(req);
248 struct bch_inode_unpacked dir_u, inode_u;
249 struct qstr qstr = QSTR(name);
252 dir = map_root_ino(dir);
254 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL,
255 bch2_unlink_trans(&trans, dir, &dir_u,
258 fuse_reply_err(req, -ret);
261 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
264 dir = map_root_ino(dir);
266 bcachefs_fuse_unlink(req, dir, name);
270 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
271 fuse_ino_t parent, const char *name)
273 struct bch_fs *c = fuse_req_userdata(req);
277 static void bcachefs_fuse_rename(fuse_req_t req,
278 fuse_ino_t src_dir, const char *srcname,
279 fuse_ino_t dst_dir, const char *dstname,
282 struct bch_fs *c = fuse_req_userdata(req);
283 struct bch_inode_unpacked dst_dir_u, src_dir_u;
284 struct bch_inode_unpacked src_inode_u, dst_inode_u;
285 struct qstr dst_name = QSTR(srcname);
286 struct qstr src_name = QSTR(dstname);
289 src_dir = map_root_ino(src_dir);
290 dst_dir = map_root_ino(dst_dir);
292 /* XXX handle overwrites */
293 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
294 bch2_rename_trans(&trans,
297 &src_inode_u, &dst_inode_u,
298 &src_name, &dst_name,
301 fuse_reply_err(req, -ret);
304 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
305 fuse_ino_t newparent, const char *newname)
307 struct bch_fs *c = fuse_req_userdata(req);
308 struct bch_inode_unpacked inode_u;
309 struct qstr qstr = QSTR(newname);
312 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
313 bch2_link_trans(&trans, newparent,
314 inum, &inode_u, &qstr));
317 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
318 fuse_reply_entry(req, &e);
320 fuse_reply_err(req, -ret);
325 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
326 struct fuse_file_info *fi)
328 struct bch_fs *c = fuse_req_userdata(req);
331 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
332 size_t size, off_t off,
333 struct fuse_file_info *fi)
335 struct bch_fs *c = fuse_req_userdata(req);
338 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
339 struct fuse_file_info *fi)
341 struct bch_fs *c = fuse_req_userdata(req);
344 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
345 struct fuse_file_info *fi)
347 struct bch_fs *c = fuse_req_userdata(req);
350 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
351 struct fuse_file_info *fi)
353 struct bch_fs *c = fuse_req_userdata(req);
356 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
357 struct fuse_file_info *fi)
359 struct bch_fs *c = fuse_req_userdata(req);
363 struct fuse_dir_entry {
369 struct fuse_dir_context {
370 struct dir_context ctx;
375 struct fuse_dir_entry *prev;
378 static int fuse_send_dir_entry(struct fuse_dir_context *ctx, loff_t pos)
380 struct fuse_dir_entry *de = ctx->prev;
383 struct stat statbuf = {
384 .st_ino = unmap_root_ino(de->ino),
385 .st_mode = de->type << 12,
388 size_t len = fuse_add_direntry(ctx->req, ctx->buf, ctx->bufsize,
389 de->name, &statbuf, pos);
393 if (len > ctx->bufsize)
402 static int fuse_filldir(struct dir_context *_ctx,
403 const char *name, int namelen,
404 loff_t pos, u64 ino, unsigned type)
406 struct fuse_dir_context *ctx =
407 container_of(_ctx, struct fuse_dir_context, ctx);
409 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(ctx={.ctx={.pos=%llu}}, "
410 "name=%s, namelen=%d, pos=%lld, dir=%llu, type=%u)\n",
411 ctx->ctx.pos, name, namelen, pos, ino, type);
414 * We have to emit directory entries after reading the next entry,
415 * because the previous entry contains a pointer to next.
418 int ret = fuse_send_dir_entry(ctx, pos);
423 struct fuse_dir_entry *cur = malloc(sizeof *cur + namelen + 1);
426 memcpy(cur->name, name, namelen);
427 cur->name[namelen] = 0;
434 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
438 if (ctx->ctx.pos == 0) {
439 ret = fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
440 unmap_root_ino(dir), DT_DIR);
446 if (ctx->ctx.pos == 1) {
447 ret = fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
448 /*TODO: parent*/ 1, DT_DIR);
457 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
458 size_t size, off_t off,
459 struct fuse_file_info *fi)
461 struct bch_fs *c = fuse_req_userdata(req);
462 struct bch_inode_unpacked bi;
463 char *buf = calloc(size, 1);
464 struct fuse_dir_context ctx = {
465 .ctx.actor = fuse_filldir,
473 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
474 "off=%lld)\n", dir, size, off);
476 dir = map_root_ino(dir);
478 ret = bch2_inode_find_by_inum(c, dir, &bi);
482 if (!S_ISDIR(bi.bi_mode)) {
487 if (!handle_dots(&ctx, dir))
490 ret = bch2_readdir(c, dir, &ctx.ctx);
494 * If we have something to send, the error above doesn't matter.
496 * Alternatively, if this send fails, but we previously sent something,
497 * then this is a success.
500 ret = fuse_send_dir_entry(&ctx, ctx.ctx.pos);
501 if (ret && ctx.buf != buf)
506 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
508 fuse_reply_buf(req, buf, ctx.buf - buf);
510 fuse_reply_err(req, -ret);
517 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
518 struct fuse_file_info *fi)
520 struct bch_fs *c = fuse_req_userdata(req);
523 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
524 struct fuse_file_info *fi)
526 struct bch_fs *c = fuse_req_userdata(req);
530 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
532 struct bch_fs *c = fuse_req_userdata(req);
533 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
534 unsigned shift = c->block_bits;
535 struct statvfs statbuf = {
536 .f_bsize = block_bytes(c),
537 .f_frsize = block_bytes(c),
538 .f_blocks = usage.capacity >> shift,
539 .f_bfree = (usage.capacity - usage.used) >> shift,
540 //.f_bavail = statbuf.f_bfree,
541 .f_files = usage.nr_inodes,
543 .f_namemax = BCH_NAME_MAX,
546 fuse_reply_statfs(req, &statbuf);
550 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
551 const char *name, const char *value,
552 size_t size, int flags)
554 struct bch_fs *c = fuse_req_userdata(req);
557 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
558 const char *name, size_t size)
560 struct bch_fs *c = fuse_req_userdata(req);
562 fuse_reply_xattr(req, );
565 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
567 struct bch_fs *c = fuse_req_userdata(req);
570 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
573 struct bch_fs *c = fuse_req_userdata(req);
577 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
578 const char *name, mode_t mode,
579 struct fuse_file_info *fi)
581 struct bch_fs *c = fuse_req_userdata(req);
582 struct bch_inode_unpacked new_inode;
585 ret = do_create(c, dir, name, mode, 0, &new_inode);
589 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
590 fuse_reply_create(req, &e, fi);
593 fuse_reply_err(req, -ret);
598 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
599 struct fuse_bufvec *bufv, off_t off,
600 struct fuse_file_info *fi)
602 struct bch_fs *c = fuse_req_userdata(req);
605 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
606 off_t offset, off_t length,
607 struct fuse_file_info *fi)
609 struct bch_fs *c = fuse_req_userdata(req);
613 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
614 .destroy = bcachefs_fuse_destroy,
615 .lookup = bcachefs_fuse_lookup,
616 .getattr = bcachefs_fuse_getattr,
617 .setattr = bcachefs_fuse_setattr,
618 .readlink = bcachefs_fuse_readlink,
619 .mknod = bcachefs_fuse_mknod,
620 .mkdir = bcachefs_fuse_mkdir,
621 .unlink = bcachefs_fuse_unlink,
622 .rmdir = bcachefs_fuse_rmdir,
623 //.symlink = bcachefs_fuse_symlink,
624 .rename = bcachefs_fuse_rename,
625 .link = bcachefs_fuse_link,
626 //.open = bcachefs_fuse_open,
627 //.read = bcachefs_fuse_read,
628 //.write = bcachefs_fuse_write,
629 //.flush = bcachefs_fuse_flush,
630 //.release = bcachefs_fuse_release,
631 //.fsync = bcachefs_fuse_fsync,
632 //.opendir = bcachefs_fuse_opendir,
633 .readdir = bcachefs_fuse_readdir,
634 //.releasedir = bcachefs_fuse_releasedir,
635 //.fsyncdir = bcachefs_fuse_fsyncdir,
636 .statfs = bcachefs_fuse_statfs,
637 //.setxattr = bcachefs_fuse_setxattr,
638 //.getxattr = bcachefs_fuse_getxattr,
639 //.listxattr = bcachefs_fuse_listxattr,
640 //.removexattr = bcachefs_fuse_removexattr,
641 .create = bcachefs_fuse_create,
645 .getlk = bcachefs_fuse_getlk,
646 .setlk = bcachefs_fuse_setlk,
648 //.write_buf = bcachefs_fuse_write_buf,
649 //.fallocate = bcachefs_fuse_fallocate,
654 * Setup and command parsing.
663 static void bf_context_free(struct bf_context *ctx)
667 free(ctx->devices_str);
668 for (i = 0; i < ctx->nr_devices; ++i)
669 free(ctx->devices[i]);
673 static struct fuse_opt bf_opts[] = {
678 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
681 static int bf_opt_proc(void *data, const char *arg, int key,
682 struct fuse_args *outargs)
684 struct bf_context *ctx = data;
687 case FUSE_OPT_KEY_NONOPT:
688 /* Just extract the first non-option string. */
689 if (!ctx->devices_str) {
690 ctx->devices_str = strdup(arg);
700 * dev1:dev2 -> [ dev1, dev2 ]
703 static void tokenize_devices(struct bf_context *ctx)
705 char *devices_str = strdup(ctx->devices_str);
706 char *devices_tmp = devices_str;
707 char **devices = NULL;
711 while ((dev = strsep(&devices_tmp, ":"))) {
712 if (strlen(dev) > 0) {
713 devices = realloc(devices, (nr + 1) * sizeof *devices);
714 devices[nr] = strdup(dev);
720 devices = malloc(sizeof *devices);
721 devices[0] = strdup(ctx->devices_str);
725 ctx->devices = devices;
726 ctx->nr_devices = nr;
731 static void usage(char *argv[])
733 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
738 int cmd_fusemount(int argc, char *argv[])
740 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
741 struct bch_opts bch_opts = bch2_opts_empty();
742 struct bf_context ctx = { 0 };
743 struct bch_fs *c = NULL;
746 /* Parse arguments. */
747 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
748 die("fuse_opt_parse err: %m");
750 struct fuse_cmdline_opts fuse_opts;
751 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
752 die("fuse_parse_cmdline err: %m");
754 if (fuse_opts.show_help) {
757 fuse_lowlevel_help();
761 if (fuse_opts.show_version) {
762 /* TODO: Show bcachefs version. */
763 printf("FUSE library version %s\n", fuse_pkgversion());
764 fuse_lowlevel_version();
768 if (!fuse_opts.mountpoint) {
770 printf("Please supply a mountpoint.\n");
774 if (!ctx.devices_str) {
776 printf("Please specify a device or device1:device2:...\n");
780 tokenize_devices(&ctx);
783 printf("Opening bcachefs filesystem on:\n");
784 for (i = 0; i < ctx.nr_devices; ++i)
785 printf("\t%s\n", ctx.devices[i]);
787 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
789 die("error opening %s: %s", ctx.devices_str,
790 strerror(-PTR_ERR(c)));
793 struct fuse_session *se =
794 fuse_session_new(&args, &bcachefs_fuse_ops,
795 sizeof(bcachefs_fuse_ops), c);
797 die("fuse_lowlevel_new err: %m");
799 if (fuse_set_signal_handlers(se) < 0)
800 die("fuse_set_signal_handlers err: %m");
802 if (fuse_session_mount(se, fuse_opts.mountpoint))
803 die("fuse_mount err: %m");
805 fuse_daemonize(fuse_opts.foreground);
807 ret = fuse_session_loop(se);
810 fuse_session_unmount(se);
811 fuse_remove_signal_handlers(se);
812 fuse_session_destroy(se);
815 free(fuse_opts.mountpoint);
816 fuse_opt_free_args(&args);
817 bf_context_free(&ctx);