]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_fusemount.c
Enable caching of negative dentries
[bcachefs-tools-debian] / cmd_fusemount.c
index 085c08a42ebe80fddca2504a128094c32030f3ab..f7e8b0d38f7eb53f5172b133a42329d796428b2a 100644 (file)
@@ -44,7 +44,7 @@ static struct stat inode_to_stat(struct bch_fs *c,
                                 struct bch_inode_unpacked *bi)
 {
        return (struct stat) {
-               .st_ino         = bi->bi_inum,
+               .st_ino         = unmap_root_ino(bi->bi_inum),
                .st_size        = bi->bi_size,
                .st_mode        = bi->bi_mode,
                .st_uid         = bi->bi_uid,
@@ -63,7 +63,7 @@ static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
                                              struct bch_inode_unpacked *bi)
 {
        return (struct fuse_entry_param) {
-               .ino            = bi->bi_inum,
+               .ino            = unmap_root_ino(bi->bi_inum),
                .generation     = bi->bi_generation,
                .attr           = inode_to_stat(c, bi),
                .attr_timeout   = DBL_MAX,
@@ -98,6 +98,9 @@ static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir,
        u64 inum;
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
+                dir, name);
+
        dir = map_root_ino(dir);
 
        ret = bch2_inode_find_by_inum(c, dir, &bi);
@@ -110,20 +113,26 @@ 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);
        if (ret)
                goto err;
 
-       bi.bi_inum = unmap_root_ino(bi.bi_inum);
+       fuse_log(FUSE_LOG_DEBUG, "fuse_lookup ret(inum=%llu)\n",
+                bi.bi_inum);
 
        struct fuse_entry_param e = inode_to_entry(c, &bi);
        fuse_reply_entry(req, &e);
        return;
 err:
+       fuse_log(FUSE_LOG_DEBUG, "fuse_lookup error %i\n", ret);
        fuse_reply_err(req, -ret);
 }
 
@@ -135,15 +144,19 @@ static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t inum,
        struct stat attr;
        int ret;
 
+       fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n",
+                inum);
+
        inum = map_root_ino(inum);
 
        ret = bch2_inode_find_by_inum(c, inum, &bi);
        if (ret) {
+               fuse_log(FUSE_LOG_DEBUG, "fuse_getattr error %i\n", ret);
                fuse_reply_err(req, -ret);
                return;
        }
 
-       bi.bi_inum = unmap_root_ino(bi.bi_inum);
+       fuse_log(FUSE_LOG_DEBUG, "fuse_getattr success\n");
 
        attr = inode_to_stat(c, &bi);
        fuse_reply_attr(req, &attr, DBL_MAX);
@@ -486,7 +499,7 @@ static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
        free(buf);
 }
 
-static int write_set_inode(struct bch_fs *c, fuse_ino_t inum, off_t new_size)
+static int inode_update_times(struct bch_fs *c, fuse_ino_t inum)
 {
        struct btree_trans trans;
        struct btree_iter *iter;
@@ -504,7 +517,6 @@ retry:
        if (ret)
                goto err;
 
-       inode_u.bi_size = max_t(u64, inode_u.bi_size, new_size);
        inode_u.bi_mtime = now;
        inode_u.bi_ctime = now;
 
@@ -526,7 +538,7 @@ err:
 static int write_aligned(struct bch_fs *c, fuse_ino_t inum,
                         struct bch_io_opts io_opts, void *buf,
                         size_t aligned_size, off_t aligned_offset,
-                        size_t *written_out)
+                        off_t new_i_size, size_t *written_out)
 {
        struct bch_write_op     op = { 0 };
        struct bio_vec          bv;
@@ -544,6 +556,7 @@ static int write_aligned(struct bch_fs *c, fuse_ino_t inum,
        op.nr_replicas  = io_opts.data_replicas;
        op.target       = io_opts.foreground_target;
        op.pos          = POS(inum, aligned_offset >> 9);
+       op.new_i_size   = new_i_size;
 
        userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
        bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
@@ -618,7 +631,8 @@ static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
 
        /* Actually write. */
        ret = write_aligned(c, inum, io_opts, aligned_buf,
-                           align.size, align.start, &aligned_written);
+                           align.size, align.start,
+                           offset + size, &aligned_written);
 
        /* Figure out how many unaligned bytes were written. */
        size_t written = align_fix_up_bytes(&align, aligned_written);
@@ -631,11 +645,11 @@ static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
                ret = 0;
 
        /*
-        * Update inode data.
+        * Update inode times.
         * TODO: Integrate with bch2_extent_update()
         */
        if (!ret)
-               ret = write_set_inode(c, inum, offset + written);
+               ret = inode_update_times(c, inum);
 
        if (!ret) {
                BUG_ON(written == 0);
@@ -674,7 +688,8 @@ static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
 
        size_t aligned_written;
        ret = write_aligned(c, new_inode.bi_inum, io_opts, aligned_buf,
-                           align.size, align.start, &aligned_written);
+                           align.size, align.start, link_len + 1,
+                           &aligned_written);
        free(aligned_buf);
 
        if (ret)
@@ -683,7 +698,7 @@ static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
        size_t written = align_fix_up_bytes(&align, aligned_written);
        BUG_ON(written != link_len + 1); // TODO: handle short
 
-       ret = write_set_inode(c, new_inode.bi_inum, written);
+       ret = inode_update_times(c, new_inode.bi_inum);
        if (ret)
                goto err;
 
@@ -762,43 +777,44 @@ static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
 }
 #endif
 
-struct fuse_dir_entry {
-       u64             ino;
-       unsigned        type;
-       char            name[0];
-};
-
 struct fuse_dir_context {
        struct dir_context      ctx;
        fuse_req_t              req;
        char                    *buf;
        size_t                  bufsize;
-
-       struct fuse_dir_entry   *prev;
 };
 
-static int fuse_send_dir_entry(struct fuse_dir_context *ctx, loff_t pos)
-{
-       struct fuse_dir_entry *de = ctx->prev;
-       ctx->prev = NULL;
-
-       struct stat statbuf = {
-               .st_ino         = unmap_root_ino(de->ino),
-               .st_mode        = de->type << 12,
-       };
+struct fuse_dirent {
+       uint64_t        ino;
+       uint64_t        off;
+       uint32_t        namelen;
+       uint32_t        type;
+       char name[];
+};
 
-       size_t len = fuse_add_direntry(ctx->req, ctx->buf, ctx->bufsize,
-                                      de->name, &statbuf, pos);
+#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
+#define FUSE_DIRENT_ALIGN(x) \
+       (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
 
-       free(de);
+static size_t fuse_add_direntry2(char *buf, size_t bufsize,
+                                const char *name, int namelen,
+                                const struct stat *stbuf, off_t off)
+{
+       size_t entlen           = FUSE_NAME_OFFSET + namelen;
+       size_t entlen_padded    = FUSE_DIRENT_ALIGN(entlen);
+       struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
 
-       if (len > ctx->bufsize)
-               return -EINVAL;
+       if ((buf == NULL) || (entlen_padded > bufsize))
+               return entlen_padded;
 
-       ctx->buf        += len;
-       ctx->bufsize    -= len;
+       dirent->ino = stbuf->st_ino;
+       dirent->off = off;
+       dirent->namelen = namelen;
+       dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
+       memcpy(dirent->name, name, namelen);
+       memset(dirent->name + namelen, 0, entlen_padded - entlen);
 
-       return 0;
+       return entlen_padded;
 }
 
 static int fuse_filldir(struct dir_context *_ctx,
@@ -808,47 +824,41 @@ static int fuse_filldir(struct dir_context *_ctx,
        struct fuse_dir_context *ctx =
                container_of(_ctx, struct fuse_dir_context, ctx);
 
-       fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(ctx={.ctx={.pos=%llu}}, "
-                "name=%s, namelen=%d, pos=%lld, dir=%llu, type=%u)\n",
-                ctx->ctx.pos, name, namelen, pos, ino, type);
+       struct stat statbuf = {
+               .st_ino         = unmap_root_ino(ino),
+               .st_mode        = type << 12,
+       };
 
-       /*
-        * We have to emit directory entries after reading the next entry,
-        * because the previous entry contains a pointer to next.
-        */
-       if (ctx->prev) {
-               int ret = fuse_send_dir_entry(ctx, pos);
-               if (ret)
-                       return ret;
-       }
+       fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
+                name, statbuf.st_ino, pos);
 
-       struct fuse_dir_entry *cur = malloc(sizeof *cur + namelen + 1);
-       cur->ino = ino;
-       cur->type = type;
-       memcpy(cur->name, name, namelen);
-       cur->name[namelen] = 0;
+       size_t len = fuse_add_direntry2(ctx->buf,
+                                       ctx->bufsize,
+                                       name,
+                                       namelen,
+                                       &statbuf,
+                                       pos + 1);
 
-       ctx->prev = cur;
+       if (len > ctx->bufsize)
+               return -1;
 
+       ctx->buf        += len;
+       ctx->bufsize    -= len;
        return 0;
 }
 
 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
 {
-       int ret = 0;
-
        if (ctx->ctx.pos == 0) {
-               ret = fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
-                                  unmap_root_ino(dir), DT_DIR);
-               if (ret < 0)
+               if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
+                                dir, DT_DIR) < 0)
                        return false;
                ctx->ctx.pos = 1;
        }
 
        if (ctx->ctx.pos == 1) {
-               ret = fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
-                                  /*TODO: parent*/ 1, DT_DIR);
-               if (ret < 0)
+               if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
+                                /*TODO: parent*/ 1, DT_DIR) < 0)
                        return false;
                ctx->ctx.pos = 2;
        }
@@ -890,20 +900,7 @@ static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
                goto reply;
 
        ret = bch2_readdir(c, dir, &ctx.ctx);
-
 reply:
-       /*
-        * If we have something to send, the error above doesn't matter.
-        *
-        * Alternatively, if this send fails, but we previously sent something,
-        * then this is a success.
-        */
-       if (ctx.prev) {
-               ret = fuse_send_dir_entry(&ctx, ctx.ctx.pos);
-               if (ret && ctx.buf != buf)
-                       ret = 0;
-       }
-
        if (!ret) {
                fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
                                        ctx.buf - buf);