]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_fusemount.c
convert main() from C to Rust
[bcachefs-tools-debian] / cmd_fusemount.c
index f07edb5abff877cc9f7d38ed2a5e63abb7d25f38..d81f31884cb27a2704cd898ff688fd51f5f139c5 100644 (file)
@@ -21,7 +21,8 @@
 #include "libbcachefs/error.h"
 #include "libbcachefs/fs-common.h"
 #include "libbcachefs/inode.h"
-#include "libbcachefs/io.h"
+#include "libbcachefs/io_read.h"
+#include "libbcachefs/io_write.h"
 #include "libbcachefs/opts.h"
 #include "libbcachefs/super.h"
 
 /* XXX cut and pasted from fsck.c */
 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
 
+/* used by write_aligned function for waiting on bch2_write closure */
+struct write_aligned_op_t {
+        struct closure cl;
+
+        /* must be last: */
+        struct bch_write_op             op;
+};
+
+
 static inline subvol_inum map_root_ino(u64 ino)
 {
        return (subvol_inum) { 1, ino == 1 ? 4096 : ino };
@@ -167,7 +177,7 @@ static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t ino,
 {
        struct bch_fs *c = fuse_req_userdata(req);
        struct bch_inode_unpacked inode_u;
-       struct btree_trans trans;
+       struct btree_trans *trans;
        struct btree_iter iter;
        u64 now;
        int ret;
@@ -176,12 +186,12 @@ static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t ino,
 
        fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n", inum.inum, to_set);
 
-       bch2_trans_init(&trans, c, 0, 0);
+       trans = bch2_trans_get(c);
 retry:
-       bch2_trans_begin(&trans);
+       bch2_trans_begin(trans);
        now = bch2_current_time(c);
 
-       ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
+       ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
        if (ret)
                goto err;
 
@@ -203,15 +213,15 @@ retry:
                inode_u.bi_mtime = now;
        /* TODO: CTIME? */
 
-       ret   = bch2_inode_write(&trans, &iter, &inode_u) ?:
-               bch2_trans_commit(&trans, NULL, NULL,
-                                 BTREE_INSERT_NOFAIL);
+       ret   = bch2_inode_write(trans, &iter, &inode_u) ?:
+               bch2_trans_commit(trans, NULL, NULL,
+                                 BCH_TRANS_COMMIT_no_enospc);
 err:
-        bch2_trans_iter_exit(&trans, &iter);
+        bch2_trans_iter_exit(trans, &iter);
        if (ret == -EINTR)
                goto retry;
 
-       bch2_trans_exit(&trans);
+       bch2_trans_put(trans);
 
        if (!ret) {
                *attr = inode_to_stat(c, &inode_u);
@@ -233,7 +243,7 @@ static int do_create(struct bch_fs *c, subvol_inum dir,
        bch2_inode_init_early(c, new_inode);
 
        return bch2_trans_do(c, NULL, NULL, 0,
-                       bch2_create_trans(&trans,
+                       bch2_create_trans(trans,
                                dir, &dir_u,
                                new_inode, &qstr,
                                uid, gid, mode, rdev, NULL, NULL,
@@ -285,8 +295,9 @@ static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir_ino,
 
        fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir.inum, name);
 
-       int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
-                           bch2_unlink_trans(&trans, dir, &dir_u,
+       int ret = bch2_trans_do(c, NULL, NULL,
+                               BCH_TRANS_COMMIT_no_enospc,
+                           bch2_unlink_trans(trans, dir, &dir_u,
                                              &inode_u, &qstr, false));
 
        fuse_reply_err(req, -ret);
@@ -320,7 +331,7 @@ static void bcachefs_fuse_rename(fuse_req_t req,
 
        /* XXX handle overwrites */
        ret = bch2_trans_do(c, NULL, NULL, 0,
-               bch2_rename_trans(&trans,
+               bch2_rename_trans(trans,
                                  src_dir, &src_dir_u,
                                  dst_dir, &dst_dir_u,
                                  &src_inode_u, &dst_inode_u,
@@ -341,10 +352,10 @@ static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t ino,
        int ret;
 
        fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
-                inum, newparent.inum, newname);
+                inum.inum, newparent.inum, newname);
 
        ret = bch2_trans_do(c, NULL, NULL, 0,
-                           bch2_link_trans(&trans, newparent, &dir_u,
+                           bch2_link_trans(trans, newparent, &dir_u,
                                            inum, &inode_u, &qstr));
 
        if (!ret) {
@@ -390,6 +401,14 @@ static void bcachefs_fuse_read_endio(struct bio *bio)
        closure_put(bio->bi_private);
 }
 
+
+static void bcachefs_fuse_write_endio(struct bch_write_op *op)
+{
+       struct write_aligned_op_t *w = container_of(op,struct write_aligned_op_t,op);
+       closure_put(&w->cl);
+}
+
+
 struct fuse_align_io {
        off_t           start;
        size_t          pad_start;
@@ -514,37 +533,36 @@ static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t ino,
 
 static int inode_update_times(struct bch_fs *c, subvol_inum inum)
 {
-       struct btree_trans trans;
+       struct btree_trans *trans;
        struct btree_iter iter;
        struct bch_inode_unpacked inode_u;
        int ret = 0;
        u64 now;
 
-       bch2_trans_init(&trans, c, 0, 0);
+       trans = bch2_trans_get(c);
 retry:
-       bch2_trans_begin(&trans);
+       bch2_trans_begin(trans);
        now = bch2_current_time(c);
 
-       ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
+       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_NOFAIL);
-
+       ret = bch2_trans_commit(trans, NULL, NULL,
+                               BCH_TRANS_COMMIT_no_enospc);
 err:
-        bch2_trans_iter_exit(&trans, &iter);
+        bch2_trans_iter_exit(trans, &iter);
        if (ret == -EINTR)
                goto retry;
 
-       bch2_trans_exit(&trans);
+       bch2_trans_put(trans);
        return ret;
 }
 
@@ -553,41 +571,47 @@ static int write_aligned(struct bch_fs *c, subvol_inum inum,
                         size_t aligned_size, off_t aligned_offset,
                         off_t new_i_size, size_t *written_out)
 {
-       struct bch_write_op     op = { 0 };
+
+       struct write_aligned_op_t w = { 0 }
+;
+       struct bch_write_op     *op = &w.op;
        struct bio_vec          bv;
-       struct closure          cl;
 
        BUG_ON(aligned_size & (block_bytes(c) - 1));
        BUG_ON(aligned_offset & (block_bytes(c) - 1));
 
        *written_out = 0;
 
-       closure_init_stack(&cl);
+       closure_init_stack(&w.cl);
 
-       bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
-       op.write_point  = writepoint_hashed(0);
-       op.nr_replicas  = io_opts.data_replicas;
-       op.target       = io_opts.foreground_target;
-       op.subvol       = inum.subvol;
-       op.pos          = POS(inum.inum, aligned_offset >> 9);
-       op.new_i_size   = new_i_size;
+       bch2_write_op_init(op, c, io_opts); /* XXX reads from op?! */
+       op->write_point = writepoint_hashed(0);
+       op->nr_replicas = io_opts.data_replicas;
+       op->target      = io_opts.foreground_target;
+       op->subvol      = inum.subvol;
+       op->pos         = POS(inum.inum, aligned_offset >> 9);
+       op->new_i_size  = new_i_size;
+       op->end_io = bcachefs_fuse_write_endio;
 
-       userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
-       bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
+       userbio_init(&op->wbio.bio, &bv, buf, aligned_size);
+       bio_set_op_attrs(&op->wbio.bio, REQ_OP_WRITE, REQ_SYNC);
 
-       if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
-                                     op.nr_replicas, 0)) {
+       if (bch2_disk_reservation_get(c, &op->res, aligned_size >> 9,
+                                     op->nr_replicas, 0)) {
                /* XXX: use check_range_allocated like dio write path */
                return -ENOSPC;
        }
 
-       closure_call(&op.cl, bch2_write, NULL, &cl);
-       closure_sync(&cl);
+       closure_get(&w.cl);
+
+       closure_call(&op->cl, bch2_write, NULL, NULL);
+
+       closure_sync(&w.cl);
 
-       if (!op.error)
-               *written_out = op.written << 9;
+       if (!op->error)
+               *written_out = op->written << 9;
 
-       return op.error;
+       return op->error;
 }
 
 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t ino,
@@ -1217,6 +1241,17 @@ int cmd_fusemount(int argc, char *argv[])
        }
        tokenize_devices(&ctx);
 
+       struct printbuf fsname = PRINTBUF;
+       prt_printf(&fsname, "fsname=");
+       for (i = 0; i < ctx.nr_devices; ++i) {
+               if (i)
+                       prt_str(&fsname, ":");
+               prt_str(&fsname, ctx.devices[i]);
+       }
+
+       fuse_opt_add_arg(&args, "-o");
+       fuse_opt_add_arg(&args, fsname.buf);
+
        /* Open bch */
        printf("Opening bcachefs filesystem on:\n");
        for (i = 0; i < ctx.nr_devices; ++i)
@@ -1243,6 +1278,11 @@ int cmd_fusemount(int argc, char *argv[])
        /* This print statement is a trigger for tests. */
        printf("Fuse mount initialized.\n");
 
+       if (fuse_opts.foreground == 0){
+               printf("Fuse forcing to foreground mode, due gcc constructors usage.\n");
+               fuse_opts.foreground = 1;
+       }
+
        fuse_daemonize(fuse_opts.foreground);
 
        ret = fuse_session_loop(se);