In struct fusebuf the attributes (used for getattr/setattr and to return
the ino/mode of a newly created file) are stored in a struct vattr. The
problem with this is, that using struct vattr in userspace is suboptimal
as some related helpers are not available, e.g. VATTR_NULL() and IFTOVT().
What happens on getattr is this: fusefs requests the attributes for a
file from the userland process. libfuse calls the getattr fuse
operation, which fills in a struct stat. This struct is than converted
to a partial struct vattr, which is passed back to the kernel where
"fixup" takes place.
Following diff simplifies that process by replacing the struct vattr in
fusebuf with a struct stat. The conversion is moved to the kernel where
it belongs. As a side effect the <sys/vnode.h> include can be removed
from libfuse.
Ok? Objections?
natano
Index: lib/libfuse/fuse_ops.c
===================================================================
RCS file: /cvs/src/lib/libfuse/fuse_ops.c,v
retrieving revision 1.24
diff -u -p -r1.24 fuse_ops.c
--- lib/libfuse/fuse_ops.c 5 Feb 2014 20:13:58 -0000 1.24
+++ lib/libfuse/fuse_ops.c 22 Aug 2016 20:53:46 -0000
@@ -30,53 +30,30 @@
return (0); \
}
-static void
-stat2attr(struct vattr *v, struct stat *st)
-{
- v->va_fileid = st->st_ino;
- v->va_bytes = st->st_blocks;
- v->va_mode = st->st_mode;
- v->va_nlink = st->st_nlink;
- v->va_uid = st->st_uid;
- v->va_gid = st->st_gid;
- v->va_rdev = st->st_rdev;
- v->va_size = st->st_size;
- v->va_blocksize = st->st_blksize;
- v->va_atime.tv_sec = st->st_atime;
- v->va_atime.tv_nsec = st->st_atimensec;
- v->va_mtime.tv_sec = st->st_mtime;
- v->va_mtime.tv_nsec = st->st_mtimensec;
- v->va_ctime.tv_sec = st->st_ctime;
- v->va_ctime.tv_nsec = st->st_ctimensec;
-}
-
static int
-update_vattr(struct fuse *f, struct vattr *attr, const char *realname,
+update_attr(struct fuse *f, struct stat *attr, const char *realname,
struct fuse_vnode *vn)
{
- struct stat st;
int ret;
- bzero(&st, sizeof(st));
- ret = f->op.getattr(realname, &st);
+ memset(attr, 0, sizeof(struct stat));
+ ret = f->op.getattr(realname, attr);
- if (st.st_blksize == 0)
- st.st_blksize = 512;
- if (st.st_blocks == 0)
- st.st_blocks = 4;
+ if (attr->st_blksize == 0)
+ attr->st_blksize = 512;
+ if (attr->st_blocks == 0)
+ attr->st_blocks = 4;
- st.st_ino = vn->ino;
+ attr->st_ino = vn->ino;
if (f->conf.set_mode)
- st.st_mode = (st.st_mode & S_IFMT) | (0777 & ~f->conf.umask);
+ attr->st_mode = (attr->st_mode & S_IFMT) | (0777 &
~f->conf.umask);
if (f->conf.set_uid)
- st.st_uid = f->conf.uid;
+ attr->st_uid = f->conf.uid;
if (f->conf.set_gid)
- st.st_gid = f->conf.gid;
-
- stat2attr(attr, &st);
+ attr->st_gid = f->conf.gid;
return (ret);
}
@@ -107,7 +84,7 @@ ifuse_ops_getattr(struct fuse *f, struct
DPRINTF("Opcode:\tgetattr\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
- bzero(&fbuf->fb_vattr, sizeof(fbuf->fb_vattr));
+ memset(&fbuf->fb_attr, 0, sizeof(struct stat));
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
@@ -121,7 +98,7 @@ ifuse_ops_getattr(struct fuse *f, struct
return (0);
}
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(realname);
return (0);
@@ -474,7 +451,7 @@ ifuse_ops_lookup(struct fuse *f, struct
return (0);
}
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(fbuf->fb_dat);
free(realname);
@@ -614,9 +591,9 @@ ifuse_ops_create(struct fuse *f, struct
fbuf->fb_err = -ENOSYS;
if (!fbuf->fb_err) {
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
- fbuf->fb_ino = fbuf->fb_vattr.va_fileid;
- fbuf->fb_io_mode = fbuf->fb_vattr.va_mode;
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
+ fbuf->fb_ino = fbuf->fb_attr.st_ino;
+ fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
}
free(realname);
@@ -650,8 +627,8 @@ ifuse_ops_mkdir(struct fuse *f, struct f
fbuf->fb_err = f->op.mkdir(realname, mode);
if (!fbuf->fb_err) {
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
- fbuf->fb_io_mode = fbuf->fb_vattr.va_mode;
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
+ fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
fbuf->fb_ino = vn->ino;
}
free(realname);
@@ -857,7 +834,7 @@ ifuse_ops_setattr(struct fuse *f, struct
if (io->fi_flags & FUSE_FATTR_MODE) {
if (f->op.chmod)
fbuf->fb_err = f->op.chmod(realname,
- fbuf->fb_vattr.va_mode);
+ fbuf->fb_attr.st_mode);
else
fbuf->fb_err = -ENOSYS;
}
@@ -865,9 +842,9 @@ ifuse_ops_setattr(struct fuse *f, struct
if (!fbuf->fb_err && (io->fi_flags & FUSE_FATTR_UID ||
io->fi_flags & FUSE_FATTR_GID) ) {
uid = (io->fi_flags & FUSE_FATTR_UID) ?
- fbuf->fb_vattr.va_uid : (gid_t)-1;
+ fbuf->fb_attr.st_uid : (gid_t)-1;
gid = (io->fi_flags & FUSE_FATTR_GID) ?
- fbuf->fb_vattr.va_gid : (uid_t)-1;
+ fbuf->fb_attr.st_gid : (uid_t)-1;
if (f->op.chown)
fbuf->fb_err = f->op.chown(realname, uid, gid);
else
@@ -876,10 +853,8 @@ ifuse_ops_setattr(struct fuse *f, struct
if (!fbuf->fb_err && ( io->fi_flags & FUSE_FATTR_MTIME ||
io->fi_flags & FUSE_FATTR_ATIME)) {
- ts[0].tv_sec = fbuf->fb_vattr.va_atime.tv_sec;
- ts[0].tv_nsec = fbuf->fb_vattr.va_atime.tv_nsec;
- ts[1].tv_sec = fbuf->fb_vattr.va_mtime.tv_sec;
- ts[1].tv_nsec = fbuf->fb_vattr.va_mtime.tv_nsec;
+ ts[0] = fbuf->fb_attr.st_atim;
+ ts[1] = fbuf->fb_attr.st_mtim;
tbuf.actime = ts[0].tv_sec;
tbuf.modtime = ts[1].tv_sec;
@@ -894,15 +869,15 @@ ifuse_ops_setattr(struct fuse *f, struct
if (!fbuf->fb_err && (io->fi_flags & FUSE_FATTR_SIZE)) {
if (f->op.truncate)
fbuf->fb_err = f->op.truncate(realname,
- fbuf->fb_vattr.va_size);
+ fbuf->fb_attr.st_size);
else
fbuf->fb_err = -ENOSYS;
}
- bzero(&fbuf->fb_vattr, sizeof(fbuf->fb_vattr));
+ memset(&fbuf->fb_attr, 0, sizeof(struct stat));
if (!fbuf->fb_err)
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(realname);
free(fbuf->fb_dat);
@@ -1054,9 +1029,9 @@ ifuse_ops_mknod(struct fuse *f, struct f
fbuf->fb_err = f->op.mknod(realname, mode, dev);
if (!fbuf->fb_err) {
- fbuf->fb_err = update_vattr(f, &fbuf->fb_vattr, realname, vn);
- fbuf->fb_io_mode = fbuf->fb_vattr.va_mode;
- fbuf->fb_ino = fbuf->fb_vattr.va_fileid;
+ fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
+ fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
+ fbuf->fb_ino = fbuf->fb_attr.st_ino;
}
free(realname);
Index: lib/libfuse/fuse_private.h
===================================================================
RCS file: /cvs/src/lib/libfuse/fuse_private.h,v
retrieving revision 1.12
diff -u -p -r1.12 fuse_private.h
--- lib/libfuse/fuse_private.h 27 Aug 2016 01:57:27 -0000 1.12
+++ lib/libfuse/fuse_private.h 27 Aug 2016 07:50:30 -0000
@@ -19,10 +19,12 @@
#define _FUSE_SUBR_H_
#include <sys/dirent.h>
+#include <sys/event.h>
#include <sys/mount.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h>
-#include <sys/vnode.h>
+#include <sys/tree.h>
#include <sys/fusebuf.h>
#include <limits.h>
Index: share/man/man9/fb_setup.9
===================================================================
RCS file: /cvs/src/share/man/man9/fb_setup.9,v
retrieving revision 1.5
diff -u -p -r1.5 fb_setup.9
--- share/man/man9/fb_setup.9 16 May 2016 20:12:20 -0000 1.5
+++ share/man/man9/fb_setup.9 23 Aug 2016 17:16:22 -0000
@@ -56,11 +56,11 @@ struct fusebuf {
struct fb_hdr fb_hdr;
union {
struct statvfs FD_stat;
- struct vattr FD_vattr;
+ struct stat FD_attr;
struct fb_io FD_io;
} FD;
- uint8_t *F_databuf;
+ uint8_t *fb_dat;
};
#define fb_next fb_hdr.fh_next
@@ -71,14 +71,13 @@ struct fusebuf {
#define fb_uuid fb_hdr.fh_uuid
#define fb_stat FD.FD_stat
-#define fb_vattr FD.FD_vattr
+#define fb_attr FD.FD_attr
#define fb_io_fd FD.FD_io.fi_fd
#define fb_io_ino FD.FD_io.fi_ino
#define fb_io_off FD.FD_io.fi_off
#define fb_io_len FD.FD_io.fi_len
#define fb_io_mode FD.FD_io.fi_mode
#define fb_io_flags FD.FD_io.fi_flags
-#define fb_dat F_databuf
.Ed
.Sh DESCRIPTION
These functions provide a way to manage the kernel messaging mechanism for
@@ -206,7 +205,7 @@ The union contains the following element
A struct
.Xr statvfs 3
filled in by the FUSE client statfs for the FUSE VFS statfs code.
-.It Fa FD_vattr
+.It Fa FD_attr
Used by the getattr and setattr calls.
.It Fa FD_io
Contains all fields commonly used by FUSE client callbacks to
@@ -215,7 +214,7 @@ It is used by access, readdir, release,
mkdir, and setattr.
.El
.Pp
-Setattr uses a struct fb_io and a struct vattr.
+Setattr uses a struct fb_io and a struct stat.
Settattr uses
.Fa FD_stat
and encapsulates a struct fb_io in
Index: sys/miscfs/fuse/fuse_device.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fuse_device.c,v
retrieving revision 1.20
diff -u -p -r1.20 fuse_device.c
--- sys/miscfs/fuse/fuse_device.c 22 Jan 2016 17:09:43 -0000 1.20
+++ sys/miscfs/fuse/fuse_device.c 22 Aug 2016 19:08:20 -0000
@@ -22,6 +22,7 @@
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/poll.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/vnode.h>
#include <sys/fusebuf.h>
Index: sys/miscfs/fuse/fuse_file.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fuse_file.c,v
retrieving revision 1.8
diff -u -p -r1.8 fuse_file.c
--- sys/miscfs/fuse/fuse_file.c 18 Mar 2014 08:51:53 -0000 1.8
+++ sys/miscfs/fuse/fuse_file.c 22 Aug 2016 19:08:20 -0000
@@ -17,6 +17,7 @@
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/vnode.h>
#include <sys/fusebuf.h>
Index: sys/miscfs/fuse/fuse_lookup.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fuse_lookup.c,v
retrieving revision 1.14
diff -u -p -r1.14 fuse_lookup.c
--- sys/miscfs/fuse/fuse_lookup.c 21 Aug 2016 09:23:33 -0000 1.14
+++ sys/miscfs/fuse/fuse_lookup.c 22 Aug 2016 19:08:20 -0000
@@ -19,6 +19,7 @@
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/namei.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/vnode.h>
#include <sys/lock.h>
@@ -109,7 +110,7 @@ fusefs_lookup(void *v)
goto out;
}
- nid = fbuf->fb_vattr.va_fileid;
+ nid = fbuf->fb_attr.st_ino;
}
if (nameiop == DELETE && (flags & ISLASTCN)) {
@@ -139,7 +140,7 @@ fusefs_lookup(void *v)
if (error)
goto out;
- tdp->v_type = IFTOVT(fbuf->fb_vattr.va_mode);
+ tdp->v_type = IFTOVT(fbuf->fb_attr.st_mode);
*vpp = tdp;
cnp->cn_flags |= SAVENAME;
@@ -177,7 +178,7 @@ fusefs_lookup(void *v)
if (error)
goto out;
- tdp->v_type = IFTOVT(fbuf->fb_vattr.va_mode);
+ tdp->v_type = IFTOVT(fbuf->fb_attr.st_mode);
if (vdp != NULL && vdp->v_type == VDIR)
VTOI(tdp)->parent = dp->ufs_ino.i_number;
Index: sys/miscfs/fuse/fuse_vfsops.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fuse_vfsops.c,v
retrieving revision 1.26
diff -u -p -r1.26 fuse_vfsops.c
--- sys/miscfs/fuse/fuse_vfsops.c 15 Aug 2016 07:39:46 -0000 1.26
+++ sys/miscfs/fuse/fuse_vfsops.c 22 Aug 2016 19:08:20 -0000
@@ -24,6 +24,7 @@
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/specdev.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>
Index: sys/miscfs/fuse/fuse_vnops.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fuse_vnops.c,v
retrieving revision 1.31
diff -u -p -r1.31 fuse_vnops.c
--- sys/miscfs/fuse/fuse_vnops.c 21 Aug 2016 09:23:33 -0000 1.31
+++ sys/miscfs/fuse/fuse_vnops.c 22 Aug 2016 19:08:20 -0000
@@ -27,6 +27,7 @@
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/specdev.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/vnode.h>
#include <sys/lock.h>
@@ -372,6 +373,7 @@ fusefs_getattr(void *v)
struct proc *p = ap->a_p;
struct fusefs_node *ip;
struct fusebuf *fbuf;
+ struct stat *st;
int error = 0;
ip = VTOI(vp);
@@ -388,12 +390,23 @@ fusefs_getattr(void *v)
return (error);
}
- memcpy(vap, &fbuf->fb_vattr, sizeof(*vap));
+ VATTR_NULL(vap);
+ st = &fbuf->fb_attr;
+ vap->va_type = IFTOVT(st->st_mode);
+ vap->va_mode = st->st_mode & ~S_IFMT;
+ vap->va_nlink = st->st_nlink;
+ vap->va_uid = st->st_uid;
+ vap->va_gid = st->st_gid;
vap->va_fsid = fmp->mp->mnt_stat.f_fsid.val[0];
- vap->va_type = IFTOVT(vap->va_mode);
- vap->va_bytes *= S_BLKSIZE;
- vap->va_mode &= ~S_IFMT;
+ vap->va_fileid = st->st_ino;
+ vap->va_size = st->st_size;
+ vap->va_blocksize = st->st_blksize;
+ vap->va_atime = st->st_atim;
+ vap->va_mtime = st->st_mtim;
+ vap->va_ctime = st->st_ctim;
+ vap->va_rdev = st->st_rdev;
+ vap->va_bytes = st->st_blocks * S_BLKSIZE;
fb_delete(fbuf);
return (error);
@@ -437,7 +450,7 @@ fusefs_setattr(void *v)
error = EROFS;
goto out;
}
- fbuf->fb_vattr.va_uid = vap->va_uid;
+ fbuf->fb_attr.st_uid = vap->va_uid;
io->fi_flags |= FUSE_FATTR_UID;
}
@@ -446,7 +459,7 @@ fusefs_setattr(void *v)
error = EROFS;
goto out;
}
- fbuf->fb_vattr.va_gid = vap->va_gid;
+ fbuf->fb_attr.st_gid = vap->va_gid;
io->fi_flags |= FUSE_FATTR_GID;
}
@@ -466,7 +479,7 @@ fusefs_setattr(void *v)
break;
}
- fbuf->fb_vattr.va_size = vap->va_size;
+ fbuf->fb_attr.st_size = vap->va_size;
io->fi_flags |= FUSE_FATTR_SIZE;
}
@@ -475,8 +488,7 @@ fusefs_setattr(void *v)
error = EROFS;
goto out;
}
- fbuf->fb_vattr.va_atime.tv_sec = vap->va_atime.tv_sec;
- fbuf->fb_vattr.va_atime.tv_nsec = vap->va_atime.tv_nsec;
+ fbuf->fb_attr.st_atim = vap->va_atime;
io->fi_flags |= FUSE_FATTR_ATIME;
}
@@ -485,8 +497,7 @@ fusefs_setattr(void *v)
error = EROFS;
goto out;
}
- fbuf->fb_vattr.va_mtime.tv_sec = vap->va_mtime.tv_sec;
- fbuf->fb_vattr.va_mtime.tv_nsec = vap->va_mtime.tv_nsec;
+ fbuf->fb_attr.st_mtim = vap->va_mtime;
io->fi_flags |= FUSE_FATTR_MTIME;
}
/* XXX should set a flag if (vap->va_vaflags & VA_UTIMES_CHANGE) */
@@ -496,7 +507,7 @@ fusefs_setattr(void *v)
error = EROFS;
goto out;
}
- fbuf->fb_vattr.va_mode = vap->va_mode & ALLPERMS;
+ fbuf->fb_attr.st_mode = vap->va_mode & ALLPERMS;
io->fi_flags |= FUSE_FATTR_MODE;
}
Index: sys/miscfs/fuse/fusebuf.c
===================================================================
RCS file: /cvs/src/sys/miscfs/fuse/fusebuf.c,v
retrieving revision 1.11
diff -u -p -r1.11 fusebuf.c
--- sys/miscfs/fuse/fusebuf.c 14 Mar 2015 03:38:51 -0000 1.11
+++ sys/miscfs/fuse/fusebuf.c 22 Aug 2016 19:08:20 -0000
@@ -19,6 +19,7 @@
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/pool.h>
+#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/systm.h>
#include <sys/vnode.h>
Index: sys/sys/fusebuf.h
===================================================================
RCS file: /cvs/src/sys/sys/fusebuf.h,v
retrieving revision 1.10
diff -u -p -r1.10 fusebuf.h
--- sys/sys/fusebuf.h 13 Aug 2016 11:42:46 -0000 1.10
+++ sys/sys/fusebuf.h 22 Aug 2016 19:08:20 -0000
@@ -62,10 +62,10 @@ struct fusebuf {
struct fb_hdr fb_hdr;
union {
struct statvfs FD_stat; /* vfs statfs */
- struct vattr FD_vattr; /* for attr vnops */
+ struct stat FD_attr; /* for attr vnops */
struct fb_io FD_io; /* for file io vnops */
} FD;
- uint8_t *F_databuf; /* data's */
+ uint8_t *fb_dat; /* data's */
};
#define fb_next fb_hdr.fh_next
@@ -76,7 +76,7 @@ struct fusebuf {
#define fb_uuid fb_hdr.fh_uuid
#define fb_stat FD.FD_stat
-#define fb_vattr FD.FD_vattr
+#define fb_attr FD.FD_attr
#define fb_io_fd FD.FD_io.fi_fd
#define fb_io_ino FD.FD_io.fi_ino
#define fb_io_off FD.FD_io.fi_off
@@ -84,7 +84,6 @@ struct fusebuf {
#define fb_io_mode FD.FD_io.fi_mode
#define fb_io_flags FD.FD_io.fi_flags
#define fb_io_rdev FD.FD_io.fi_rdev
-#define fb_dat F_databuf
/*
* Macros for type conversion