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_context {
364 struct dir_context ctx;
370 static int fuse_filldir(struct dir_context *_ctx,
371 const char *name, int namelen,
372 loff_t pos, u64 dir, unsigned type)
374 struct fuse_dir_context *ctx =
375 container_of(_ctx, struct fuse_dir_context, ctx);
377 struct stat statbuf = {
378 .st_ino = map_root_ino(dir),
379 .st_mode = type << 12,
382 size_t len = fuse_add_direntry(ctx->req,
389 if (len > ctx->bufsize)
397 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
398 size_t size, off_t off,
399 struct fuse_file_info *fi)
401 struct bch_fs *c = fuse_req_userdata(req);
403 struct fuse_dir_context ctx = {
404 .ctx.actor = fuse_filldir,
408 .bufsize = sizeof(buf),
412 dir = map_root_ino(dir);
414 ret = bch2_readdir(c, dir, &ctx.ctx);
416 fuse_reply_buf(req, buf, ctx.buf - buf);
418 fuse_reply_err(req, -ret);
423 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
424 struct fuse_file_info *fi)
426 struct bch_fs *c = fuse_req_userdata(req);
429 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
430 struct fuse_file_info *fi)
432 struct bch_fs *c = fuse_req_userdata(req);
436 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
438 struct bch_fs *c = fuse_req_userdata(req);
439 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
440 unsigned shift = c->block_bits;
441 struct statvfs statbuf = {
442 .f_bsize = block_bytes(c),
443 .f_frsize = block_bytes(c),
444 .f_blocks = usage.capacity >> shift,
445 .f_bfree = (usage.capacity - usage.used) >> shift,
446 //.f_bavail = statbuf.f_bfree,
447 .f_files = usage.nr_inodes,
449 .f_namemax = BCH_NAME_MAX,
452 fuse_reply_statfs(req, &statbuf);
456 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
457 const char *name, const char *value,
458 size_t size, int flags)
460 struct bch_fs *c = fuse_req_userdata(req);
463 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
464 const char *name, size_t size)
466 struct bch_fs *c = fuse_req_userdata(req);
468 fuse_reply_xattr(req, );
471 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
473 struct bch_fs *c = fuse_req_userdata(req);
476 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
479 struct bch_fs *c = fuse_req_userdata(req);
483 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
484 const char *name, mode_t mode,
485 struct fuse_file_info *fi)
487 struct bch_fs *c = fuse_req_userdata(req);
488 struct bch_inode_unpacked new_inode;
491 ret = do_create(c, dir, name, mode, 0, &new_inode);
495 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
496 fuse_reply_create(req, &e, fi);
499 fuse_reply_err(req, -ret);
504 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
505 struct fuse_bufvec *bufv, off_t off,
506 struct fuse_file_info *fi)
508 struct bch_fs *c = fuse_req_userdata(req);
511 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
512 off_t offset, off_t length,
513 struct fuse_file_info *fi)
515 struct bch_fs *c = fuse_req_userdata(req);
519 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
520 .destroy = bcachefs_fuse_destroy,
521 .lookup = bcachefs_fuse_lookup,
522 .getattr = bcachefs_fuse_getattr,
523 .setattr = bcachefs_fuse_setattr,
524 .readlink = bcachefs_fuse_readlink,
525 .mknod = bcachefs_fuse_mknod,
526 .mkdir = bcachefs_fuse_mkdir,
527 .unlink = bcachefs_fuse_unlink,
528 .rmdir = bcachefs_fuse_rmdir,
529 //.symlink = bcachefs_fuse_symlink,
530 .rename = bcachefs_fuse_rename,
531 .link = bcachefs_fuse_link,
532 //.open = bcachefs_fuse_open,
533 //.read = bcachefs_fuse_read,
534 //.write = bcachefs_fuse_write,
535 //.flush = bcachefs_fuse_flush,
536 //.release = bcachefs_fuse_release,
537 //.fsync = bcachefs_fuse_fsync,
538 //.opendir = bcachefs_fuse_opendir,
539 .readdir = bcachefs_fuse_readdir,
540 //.releasedir = bcachefs_fuse_releasedir,
541 //.fsyncdir = bcachefs_fuse_fsyncdir,
542 .statfs = bcachefs_fuse_statfs,
543 //.setxattr = bcachefs_fuse_setxattr,
544 //.getxattr = bcachefs_fuse_getxattr,
545 //.listxattr = bcachefs_fuse_listxattr,
546 //.removexattr = bcachefs_fuse_removexattr,
547 .create = bcachefs_fuse_create,
551 .getlk = bcachefs_fuse_getlk,
552 .setlk = bcachefs_fuse_setlk,
554 //.write_buf = bcachefs_fuse_write_buf,
555 //.fallocate = bcachefs_fuse_fallocate,
560 * Setup and command parsing.
569 static void bf_context_free(struct bf_context *ctx)
573 free(ctx->devices_str);
574 for (i = 0; i < ctx->nr_devices; ++i)
575 free(ctx->devices[i]);
579 static struct fuse_opt bf_opts[] = {
584 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
587 static int bf_opt_proc(void *data, const char *arg, int key,
588 struct fuse_args *outargs)
590 struct bf_context *ctx = data;
593 case FUSE_OPT_KEY_NONOPT:
594 /* Just extract the first non-option string. */
595 if (!ctx->devices_str) {
596 ctx->devices_str = strdup(arg);
606 * dev1:dev2 -> [ dev1, dev2 ]
609 static void tokenize_devices(struct bf_context *ctx)
611 char *devices_str = strdup(ctx->devices_str);
612 char *devices_tmp = devices_str;
613 char **devices = NULL;
617 while ((dev = strsep(&devices_tmp, ":"))) {
618 if (strlen(dev) > 0) {
619 devices = realloc(devices, (nr + 1) * sizeof *devices);
620 devices[nr] = strdup(dev);
626 devices = malloc(sizeof *devices);
627 devices[0] = strdup(ctx->devices_str);
631 ctx->devices = devices;
632 ctx->nr_devices = nr;
637 static void usage(char *argv[])
639 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
644 int cmd_fusemount(int argc, char *argv[])
646 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
647 struct bch_opts bch_opts = bch2_opts_empty();
648 struct bf_context ctx = { 0 };
649 struct bch_fs *c = NULL;
652 /* Parse arguments. */
653 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
654 die("fuse_opt_parse err: %m");
656 struct fuse_cmdline_opts fuse_opts;
657 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
658 die("fuse_parse_cmdline err: %m");
660 if (fuse_opts.show_help) {
663 fuse_lowlevel_help();
667 if (fuse_opts.show_version) {
668 /* TODO: Show bcachefs version. */
669 printf("FUSE library version %s\n", fuse_pkgversion());
670 fuse_lowlevel_version();
674 if (!fuse_opts.mountpoint) {
676 printf("Please supply a mountpoint.\n");
680 if (!ctx.devices_str) {
682 printf("Please specify a device or device1:device2:...\n");
686 tokenize_devices(&ctx);
689 printf("Opening bcachefs filesystem on:\n");
690 for (i = 0; i < ctx.nr_devices; ++i)
691 printf("\t%s\n", ctx.devices[i]);
693 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
695 die("error opening %s: %s", ctx.devices_str,
696 strerror(-PTR_ERR(c)));
699 struct fuse_session *se =
700 fuse_session_new(&args, &bcachefs_fuse_ops,
701 sizeof(bcachefs_fuse_ops), c);
703 die("fuse_lowlevel_new err: %m");
705 if (fuse_set_signal_handlers(se) < 0)
706 die("fuse_set_signal_handlers err: %m");
708 if (fuse_session_mount(se, fuse_opts.mountpoint))
709 die("fuse_mount err: %m");
711 fuse_daemonize(fuse_opts.foreground);
713 ret = fuse_session_loop(se);
716 fuse_session_unmount(se);
717 fuse_remove_signal_handlers(se);
718 fuse_session_destroy(se);
721 free(fuse_opts.mountpoint);
722 fuse_opt_free_args(&args);
723 bf_context_free(&ctx);