diff mbox

[09/13] 9p: introduce fchown file op

Message ID 146702053133.5764.14891011245595156322.stgit@bahia.lan (mailing list archive)
State New, archived
Headers show

Commit Message

Greg Kurz June 27, 2016, 9:42 a.m. UTC
This allows fchown() in the guest to stay functional even if the file
was unlinked.

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 fsdev/file-op-9p.h  |    1 +
 hw/9pfs/9p-handle.c |   10 ++++++++++
 hw/9pfs/9p-local.c  |   22 ++++++++++++++++++++++
 hw/9pfs/9p-proxy.c  |   10 ++++++++++
 hw/9pfs/9p-synth.c  |    8 ++++++++
 hw/9pfs/9p.c        |   18 +++++++++++++++---
 hw/9pfs/cofs.c      |   22 ++++++++++++++++++++++
 hw/9pfs/coth.h      |    1 +
 8 files changed, 89 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h
index 811ca234cf86..1c15d6efdaea 100644
--- a/fsdev/file-op-9p.h
+++ b/fsdev/file-op-9p.h
@@ -143,6 +143,7 @@  struct FileOperations
     int (*ftruncate)(FsContext *, int, V9fsFidOpenState *, off_t);
     int (*futimens)(FsContext *, int, V9fsFidOpenState *,
                     const struct timespec *);
+    int (*fchown)(FsContext *, int, V9fsFidOpenState *, FsCred *);
     void *opaque;
 };
 
diff --git a/hw/9pfs/9p-handle.c b/hw/9pfs/9p-handle.c
index 382b4d57927a..7e8e776cdab0 100644
--- a/hw/9pfs/9p-handle.c
+++ b/hw/9pfs/9p-handle.c
@@ -384,6 +384,15 @@  static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
     return ret;
 }
 
+static int handle_fchown(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs,
+                         FsCred *credp)
+{
+    int fd;
+
+    fd = v9fs_get_fd_fid(fid_type, fs);
+    return fchown(fd, credp->fc_uid, credp->fc_gid);
+}
+
 static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
                             const struct timespec *buf)
 {
@@ -716,4 +725,5 @@  FileOperations handle_ops = {
     .unlinkat     = handle_unlinkat,
     .ftruncate    = handle_ftruncate,
     .futimens     = handle_futimens,
+    .fchown       = handle_fchown,
 };
diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 873bd4b9d997..bc8d3bff1308 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -954,6 +954,27 @@  static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
     return ret;
 }
 
+static int local_fchown(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs,
+                        FsCred *credp)
+{
+    int ret = -1;
+    int fd;
+
+    fd = v9fs_get_fd_fid(fid_type, fs);
+
+    if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
+        (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+        (fs_ctx->export_flags & V9FS_SM_NONE)) {
+        ret = fchown(fd, credp->fc_uid, credp->fc_gid);
+    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+        ret = local_set_xattr(fd, NULL, credp);
+    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+        errno = EOPNOTSUPP;
+        return -1;
+    }
+    return ret;
+}
+
 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
                            const struct timespec *buf)
 {
@@ -1314,4 +1335,5 @@  FileOperations local_ops = {
     .unlinkat = local_unlinkat,
     .ftruncate = local_ftruncate,
     .futimens = local_futimens,
+    .fchown = local_fchown,
 };
diff --git a/hw/9pfs/9p-proxy.c b/hw/9pfs/9p-proxy.c
index cbc94b645852..8f2e27c6f8e4 100644
--- a/hw/9pfs/9p-proxy.c
+++ b/hw/9pfs/9p-proxy.c
@@ -909,6 +909,15 @@  static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
     return retval;
 }
 
+static int proxy_fchown(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs,
+                        FsCred *credp)
+{
+    int fd;
+
+    fd = v9fs_get_fd_fid(fid_type, fs);
+    return fchown(fd, credp->fc_uid, credp->fc_gid);
+}
+
 static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
                            const struct timespec *buf)
 {
@@ -1231,4 +1240,5 @@  FileOperations proxy_ops = {
     .unlinkat     = proxy_unlinkat,
     .ftruncate    = proxy_ftruncate,
     .futimens     = proxy_futimens,
+    .fchown       = proxy_fchown,
 };
diff --git a/hw/9pfs/9p-synth.c b/hw/9pfs/9p-synth.c
index c4770792a79f..b0f9b0cd67f6 100644
--- a/hw/9pfs/9p-synth.c
+++ b/hw/9pfs/9p-synth.c
@@ -405,6 +405,13 @@  static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
     return -1;
 }
 
+static int synth_fchown(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs,
+                        FsCred *credp)
+{
+    errno = EPERM;
+    return -1;
+}
+
 static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
                                 const struct timespec *buf)
 {
@@ -582,4 +589,5 @@  FileOperations synth_ops = {
     .unlinkat     = synth_unlinkat,
     .ftruncate    = synth_ftruncate,
     .futimens     = synth_futimens,
+    .fchown       = synth_fchown,
 };
diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
index 97dba6190809..351bbdde4748 100644
--- a/hw/9pfs/9p.c
+++ b/hw/9pfs/9p.c
@@ -1208,6 +1208,19 @@  static int v9fs_do_utimens(V9fsPDU *pdu, V9fsFidState *fidp,
     return err;
 }
 
+static int v9fs_do_chown(V9fsPDU *pdu, V9fsFidState *fidp, uid_t uid, gid_t gid)
+{
+    int err;
+
+    if (fid_has_file(fidp)) {
+        err = v9fs_co_fchown(pdu, fidp, uid, gid);
+    } else {
+        err = v9fs_co_chown(pdu, &fidp->path, uid, gid);
+    }
+
+    return err;
+}
+
 /* Attribute flags */
 #define P9_ATTR_MODE       (1 << 0)
 #define P9_ATTR_UID        (1 << 1)
@@ -1286,8 +1299,7 @@  static void v9fs_setattr(void *opaque)
         if (!(v9iattr.valid & P9_ATTR_GID)) {
             v9iattr.gid = -1;
         }
-        err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
-                            v9iattr.gid);
+        err = v9fs_do_chown(pdu, fidp, v9iattr.uid, v9iattr.gid);
         if (err < 0) {
             goto out;
         }
@@ -2769,7 +2781,7 @@  static void v9fs_wstat(void *opaque)
         }
     }
     if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
-        err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
+        err = v9fs_do_chown(pdu, fidp, v9stat.n_uid, v9stat.n_gid);
         if (err < 0) {
             goto out;
         }
diff --git a/hw/9pfs/cofs.c b/hw/9pfs/cofs.c
index 1ed9c298f98d..4e8fbbe2b24e 100644
--- a/hw/9pfs/cofs.c
+++ b/hw/9pfs/cofs.c
@@ -175,6 +175,28 @@  int v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid, gid_t gid)
     return err;
 }
 
+int v9fs_co_fchown(V9fsPDU *pdu, V9fsFidState *fidp, uid_t uid, gid_t gid)
+{
+    int err;
+    FsCred cred;
+    V9fsState *s = pdu->s;
+
+    if (v9fs_request_cancelled(pdu)) {
+        return -EINTR;
+    }
+    cred_init(&cred);
+    cred.fc_uid = uid;
+    cred.fc_gid = gid;
+    v9fs_co_run_in_worker(
+        {
+            err = s->ops->fchown(&s->ctx, fidp->fid_type, &fidp->fs, &cred);
+            if (err < 0) {
+                err = -errno;
+            }
+        });
+    return err;
+}
+
 int v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
 {
     int err;
diff --git a/hw/9pfs/coth.h b/hw/9pfs/coth.h
index c4e90059f00c..7050298f7170 100644
--- a/hw/9pfs/coth.h
+++ b/hw/9pfs/coth.h
@@ -96,5 +96,6 @@  extern int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t,
                           V9fsStatDotl *v9stat);
 extern int v9fs_co_ftruncate(V9fsPDU *, V9fsFidState *, off_t);
 extern int v9fs_co_futimens(V9fsPDU *, V9fsFidState *, struct timespec [2]);
+extern int v9fs_co_fchown(V9fsPDU *, V9fsFidState *, uid_t, gid_t);
 
 #endif