]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_fusemount.c
Update bcachefs sources to 7c0fe6f104 bcachefs: Fix bch2_fsck_ask_yn()
[bcachefs-tools-debian] / cmd_fusemount.c
index 79ca363a287445a8dd137243fd7e218eaec7cd68..4470f83844a128b9f83f79d091c788b0d976c43c 100644 (file)
@@ -1,3 +1,5 @@
+#ifdef BCACHEFS_FUSE
+
 #include <errno.h>
 #include <float.h>
 #include <getopt.h>
@@ -15,6 +17,7 @@
 #include "libbcachefs/btree_iter.h"
 #include "libbcachefs/buckets.h"
 #include "libbcachefs/dirent.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/error.h"
 #include "libbcachefs/fs-common.h"
 #include "libbcachefs/inode.h"
@@ -113,8 +116,12 @@ static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir,
 
        inum = bch2_dirent_lookup(c, dir, &hash_info, &qstr);
        if (!inum) {
-               ret = -ENOENT;
-               goto err;
+               struct fuse_entry_param e = {
+                       .attr_timeout   = DBL_MAX,
+                       .entry_timeout  = DBL_MAX,
+               };
+               fuse_reply_entry(req, &e);
+               return;
        }
 
        ret = bch2_inode_find_by_inum(c, inum, &bi);
@@ -165,10 +172,13 @@ static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t inum,
        struct bch_fs *c = fuse_req_userdata(req);
        struct bch_inode_unpacked inode_u;
        struct btree_trans trans;
-       struct btree_iter *iter;
+       struct btree_iter iter;
        u64 now;
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n",
+                inum, to_set);
+
        inum = map_root_ino(inum);
 
        bch2_trans_init(&trans, c, 0, 0);
@@ -176,8 +186,7 @@ retry:
        bch2_trans_begin(&trans);
        now = bch2_current_time(c);
 
-       iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
-       ret = PTR_ERR_OR_ZERO(iter);
+       ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
        if (ret)
                goto err;
 
@@ -199,11 +208,11 @@ retry:
                inode_u.bi_mtime = now;
        /* TODO: CTIME? */
 
-       ret   = bch2_inode_write(&trans, iter, &inode_u) ?:
+       ret   = bch2_inode_write(&trans, &iter, &inode_u) ?:
                bch2_trans_commit(&trans, NULL, NULL,
-                                 BTREE_INSERT_ATOMIC|
                                  BTREE_INSERT_NOFAIL);
 err:
+        bch2_trans_iter_exit(&trans, &iter);
        if (ret == -EINTR)
                goto retry;
 
@@ -228,7 +237,7 @@ static int do_create(struct bch_fs *c, u64 dir,
 
        bch2_inode_init_early(c, new_inode);
 
-       return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
+       return bch2_trans_do(c, NULL, NULL, 0,
                        bch2_create_trans(&trans,
                                dir, &dir_u,
                                new_inode, &qstr,
@@ -243,6 +252,8 @@ static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
        struct bch_inode_unpacked new_inode;
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mknod(%llu, %s, %x, %x)\n",
+                dir, name, mode, rdev);
        ret = do_create(c, dir, name, mode, rdev, &new_inode);
        if (ret)
                goto err;
@@ -257,6 +268,9 @@ err:
 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
                                const char *name, mode_t mode)
 {
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mkdir(%llu, %s, %x)\n",
+                dir, name, mode);
+
        BUG_ON(mode & S_IFMT);
 
        mode |= S_IFDIR;
@@ -271,9 +285,11 @@ static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
        struct qstr qstr = QSTR(name);
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir, name);
+
        dir = map_root_ino(dir);
 
-       ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL,
+       ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
                            bch2_unlink_trans(&trans, dir, &dir_u,
                                              &inode_u, &qstr));
 
@@ -283,6 +299,8 @@ static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
                                const char *name)
 {
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_rmdir(%llu, %s)\n", dir, name);
+
        dir = map_root_ino(dir);
 
        bcachefs_fuse_unlink(req, dir, name);
@@ -300,11 +318,15 @@ static void bcachefs_fuse_rename(fuse_req_t req,
        struct qstr src_name = QSTR(dstname);
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG,
+                "bcachefs_fuse_rename(%llu, %s, %llu, %s, %x)\n",
+                src_dir, srcname, dst_dir, dstname, flags);
+
        src_dir = map_root_ino(src_dir);
        dst_dir = map_root_ino(dst_dir);
 
        /* XXX handle overwrites */
-       ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
+       ret = bch2_trans_do(c, NULL, NULL, 0,
                bch2_rename_trans(&trans,
                                  src_dir, &src_dir_u,
                                  dst_dir, &dst_dir_u,
@@ -319,15 +341,18 @@ static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
                               fuse_ino_t newparent, const char *newname)
 {
        struct bch_fs *c = fuse_req_userdata(req);
-       struct bch_inode_unpacked inode_u;
+       struct bch_inode_unpacked dir_u, inode_u;
        struct qstr qstr = QSTR(newname);
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
+                inum, newparent, newname);
+
        newparent = map_root_ino(newparent);
 
-       ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
+       ret = bch2_trans_do(c, NULL, NULL, 0,
                            bch2_link_trans(&trans, newparent,
-                                           inum, &inode_u, &qstr));
+                                           inum, &dir_u, &inode_u, &qstr));
 
        if (!ret) {
                struct fuse_entry_param e = inode_to_entry(c, &inode_u);
@@ -498,7 +523,7 @@ static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
 static int inode_update_times(struct bch_fs *c, fuse_ino_t inum)
 {
        struct btree_trans trans;
-       struct btree_iter *iter;
+       struct btree_iter iter;
        struct bch_inode_unpacked inode_u;
        int ret = 0;
        u64 now;
@@ -508,22 +533,22 @@ retry:
        bch2_trans_begin(&trans);
        now = bch2_current_time(c);
 
-       iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
-       ret = PTR_ERR_OR_ZERO(iter);
+       ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
        if (ret)
                goto err;
 
        inode_u.bi_mtime = now;
        inode_u.bi_ctime = now;
 
-       ret = bch2_inode_write(&trans, iter, &inode_u);
+       ret = bch2_inode_write(&trans, &iter, &inode_u);
        if (ret)
                goto err;
 
        ret = bch2_trans_commit(&trans, NULL, NULL,
-                               BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL);
+                               BTREE_INSERT_NOFAIL);
 
 err:
+        bch2_trans_iter_exit(&trans, &iter);
        if (ret == -EINTR)
                goto retry;
 
@@ -586,6 +611,8 @@ static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
                 inum, size, offset);
 
        struct fuse_align_io align = align_io(c, size, offset);
+       void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
+       BUG_ON(!aligned_buf);
 
        if (get_inode_io_opts(c, inum, &io_opts)) {
                ret = -ENOENT;
@@ -593,7 +620,6 @@ static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
        }
 
        /* Realign the data and read in start and end, if needed */
-       void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
 
        /* Read partial start data. */
        if (align.pad_start) {
@@ -650,11 +676,13 @@ static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
        if (!ret) {
                BUG_ON(written == 0);
                fuse_reply_write(req, written);
+               free(aligned_buf);
                return;
        }
 
 err:
        fuse_reply_err(req, -ret);
+       free(aligned_buf);
 }
 
 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
@@ -665,6 +693,9 @@ static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
        size_t link_len = strlen(link);
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_symlink(%s, %llu, %s)\n",
+                link, dir, name);
+
        dir = map_root_ino(dir);
 
        ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
@@ -679,6 +710,8 @@ static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
        struct fuse_align_io align = align_io(c, link_len + 1, 0);
 
        void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
+       BUG_ON(!aligned_buf);
+
        memset(aligned_buf, 0, align.size);
        memcpy(aligned_buf, link, link_len); /* already terminated */
 
@@ -713,6 +746,8 @@ static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
        struct bch_fs *c = fuse_req_userdata(req);
        char *buf = NULL;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readlink(%llu)\n", inum);
+
        struct bch_inode_unpacked bi;
        int ret = bch2_inode_find_by_inum(c, inum, &bi);
        if (ret)
@@ -840,6 +875,7 @@ static int fuse_filldir(struct dir_context *_ctx,
 
        ctx->buf        += len;
        ctx->bufsize    -= len;
+
        return 0;
 }
 
@@ -984,6 +1020,9 @@ static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
        struct bch_inode_unpacked new_inode;
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_create(%llu, %s, %x)\n",
+                dir, name, mode);
+
        ret = do_create(c, dir, name, mode, 0, &new_inode);
        if (ret)
                goto err;
@@ -1191,7 +1230,7 @@ int cmd_fusemount(int argc, char *argv[])
        c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
        if (IS_ERR(c))
                die("error opening %s: %s", ctx.devices_str,
-                   strerror(-PTR_ERR(c)));
+                   bch2_err_str(PTR_ERR(c)));
 
        /* Fuse */
        struct fuse_session *se =
@@ -1206,6 +1245,9 @@ int cmd_fusemount(int argc, char *argv[])
        if (fuse_session_mount(se, fuse_opts.mountpoint))
                die("fuse_mount err: %m");
 
+       /* This print statement is a trigger for tests. */
+       printf("Fuse mount initialized.\n");
+
        fuse_daemonize(fuse_opts.foreground);
 
        ret = fuse_session_loop(se);
@@ -1222,3 +1264,5 @@ out:
 
        return ret ? 1 : 0;
 }
+
+#endif /* BCACHEFS_FUSE */