xref: /qemu/hw/9pfs/9p-local.c (revision a9e0c9c0f14e19d23443ac24c8080b4708d2eab8)
19f107513SAnthony Liguori /*
2f00d4f59SWei Liu  * 9p Posix callback
39f107513SAnthony Liguori  *
49f107513SAnthony Liguori  * Copyright IBM, Corp. 2010
59f107513SAnthony Liguori  *
69f107513SAnthony Liguori  * Authors:
79f107513SAnthony Liguori  *  Anthony Liguori   <aliguori@us.ibm.com>
89f107513SAnthony Liguori  *
99f107513SAnthony Liguori  * This work is licensed under the terms of the GNU GPL, version 2.  See
109f107513SAnthony Liguori  * the COPYING file in the top-level directory.
119f107513SAnthony Liguori  */
12873c3213SStefan Weil 
136f569084SChristian Schoenebeck /*
146f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
156f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
166f569084SChristian Schoenebeck  */
176f569084SChristian Schoenebeck 
18fbc04127SPeter Maydell #include "qemu/osdep.h"
19ebe74f8bSWei Liu #include "9p.h"
20996a0d76SGreg Kurz #include "9p-local.h"
21267ae092SWei Liu #include "9p-xattr.h"
220e35a378SGreg Kurz #include "9p-util.h"
2369b15212SStefan Weil #include "fsdev/qemu-fsdev.h"   /* local_ops */
24c494dd6fSAnthony Liguori #include <arpa/inet.h>
25131dcb25SAnthony Liguori #include <pwd.h>
26131dcb25SAnthony Liguori #include <grp.h>
27c494dd6fSAnthony Liguori #include <sys/socket.h>
28c494dd6fSAnthony Liguori #include <sys/un.h>
291de7afc9SPaolo Bonzini #include "qemu/xattr.h"
30e688df6bSMarkus Armbruster #include "qapi/error.h"
31f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
3263325b18SGreg Kurz #include "qemu/error-report.h"
33922a01a0SMarkus Armbruster #include "qemu/option.h"
342c30dd74SAneesh Kumar K.V #include <libgen.h>
35e0bd743bSKeno Fischer #ifdef CONFIG_LINUX
36e06a765eSHarsh Prateek Bora #include <linux/fs.h>
37e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H
38e06a765eSHarsh Prateek Bora #include <linux/magic.h>
39e06a765eSHarsh Prateek Bora #endif
40e0bd743bSKeno Fischer #endif
41e06a765eSHarsh Prateek Bora #include <sys/ioctl.h>
42e06a765eSHarsh Prateek Bora 
43e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC
44e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC  0x58465342
45e06a765eSHarsh Prateek Bora #endif
46e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC
47e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53
48e06a765eSHarsh Prateek Bora #endif
49e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC
50e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973
51e06a765eSHarsh Prateek Bora #endif
52e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC
53e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E
54e06a765eSHarsh Prateek Bora #endif
55131dcb25SAnthony Liguori 
560e35a378SGreg Kurz typedef struct {
570e35a378SGreg Kurz     int mountfd;
580e35a378SGreg Kurz } LocalData;
590e35a378SGreg Kurz 
local_open_nofollow(FsContext * fs_ctx,const char * path,int flags,mode_t mode)60996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
61996a0d76SGreg Kurz                         mode_t mode)
62996a0d76SGreg Kurz {
63996a0d76SGreg Kurz     LocalData *data = fs_ctx->private;
643dbcf273SGreg Kurz     int fd = data->mountfd;
65996a0d76SGreg Kurz 
663dbcf273SGreg Kurz     while (*path && fd != -1) {
673dbcf273SGreg Kurz         const char *c;
683dbcf273SGreg Kurz         int next_fd;
693dbcf273SGreg Kurz         char *head;
703dbcf273SGreg Kurz 
713dbcf273SGreg Kurz         /* Only relative paths without consecutive slashes */
723dbcf273SGreg Kurz         assert(*path != '/');
733dbcf273SGreg Kurz 
743dbcf273SGreg Kurz         head = g_strdup(path);
755c99fa37SKeno Fischer         c = qemu_strchrnul(path, '/');
763dbcf273SGreg Kurz         if (*c) {
773dbcf273SGreg Kurz             /* Intermediate path element */
783dbcf273SGreg Kurz             head[c - path] = 0;
793dbcf273SGreg Kurz             path = c + 1;
803dbcf273SGreg Kurz             next_fd = openat_dir(fd, head);
813dbcf273SGreg Kurz         } else {
823dbcf273SGreg Kurz             /* Rightmost path element */
833dbcf273SGreg Kurz             next_fd = openat_file(fd, head, flags, mode);
843dbcf273SGreg Kurz             path = c;
853dbcf273SGreg Kurz         }
863dbcf273SGreg Kurz         g_free(head);
873dbcf273SGreg Kurz         if (fd != data->mountfd) {
883dbcf273SGreg Kurz             close_preserve_errno(fd);
893dbcf273SGreg Kurz         }
903dbcf273SGreg Kurz         fd = next_fd;
91996a0d76SGreg Kurz     }
92996a0d76SGreg Kurz 
933dbcf273SGreg Kurz     assert(fd != data->mountfd);
943dbcf273SGreg Kurz     return fd;
95996a0d76SGreg Kurz }
96996a0d76SGreg Kurz 
local_opendir_nofollow(FsContext * fs_ctx,const char * path)97996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
98996a0d76SGreg Kurz {
99996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
100996a0d76SGreg Kurz }
101996a0d76SGreg Kurz 
renameat_preserve_errno(int odirfd,const char * opath,int ndirfd,const char * npath)10299f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
10399f2cf4bSGreg Kurz                                     const char *npath)
10499f2cf4bSGreg Kurz {
10599f2cf4bSGreg Kurz     int serrno = errno;
1066ca60cd7SBin Meng     qemu_renameat(odirfd, opath, ndirfd, npath);
10799f2cf4bSGreg Kurz     errno = serrno;
10899f2cf4bSGreg Kurz }
10999f2cf4bSGreg Kurz 
unlinkat_preserve_errno(int dirfd,const char * path,int flags)110ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
111ad0b46e6SGreg Kurz {
112ad0b46e6SGreg Kurz     int serrno = errno;
1136ca60cd7SBin Meng     qemu_unlinkat(dirfd, path, flags);
114ad0b46e6SGreg Kurz     errno = serrno;
115ad0b46e6SGreg Kurz }
116ad0b46e6SGreg Kurz 
1172c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
11881ffbf5aSGreg Kurz #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
1192c30dd74SAneesh Kumar K.V 
local_fopenat(int dirfd,const char * name,const char * mode)120f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
1210ceb092eSAneesh Kumar K.V {
1220ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
1230ceb092eSAneesh Kumar K.V     FILE *fp;
124f9aef99bSGreg Kurz     int flags;
1250ceb092eSAneesh Kumar K.V     /*
1260ceb092eSAneesh Kumar K.V      * only supports two modes
1270ceb092eSAneesh Kumar K.V      */
1280ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
129f9aef99bSGreg Kurz         flags = O_RDONLY;
1300ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
131f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
1320ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1330ceb092eSAneesh Kumar K.V     } else {
1340ceb092eSAneesh Kumar K.V         return NULL;
1350ceb092eSAneesh Kumar K.V     }
136f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
1370ceb092eSAneesh Kumar K.V     if (fd == -1) {
1380ceb092eSAneesh Kumar K.V         return NULL;
1390ceb092eSAneesh Kumar K.V     }
1400ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1410ceb092eSAneesh Kumar K.V     if (!fp) {
1420ceb092eSAneesh Kumar K.V         close(fd);
1430ceb092eSAneesh Kumar K.V     }
1440ceb092eSAneesh Kumar K.V     return fp;
1450ceb092eSAneesh Kumar K.V }
1460ceb092eSAneesh Kumar K.V 
1472c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
local_mapped_file_attr(int dirfd,const char * name,struct stat * stbuf)148f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
1492c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1502c30dd74SAneesh Kumar K.V {
1512c30dd74SAneesh Kumar K.V     FILE *fp;
1522c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
153f9aef99bSGreg Kurz     int map_dirfd;
1542c30dd74SAneesh Kumar K.V 
15581ffbf5aSGreg Kurz     if (strcmp(name, ".")) {
15699f2cf4bSGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
157f9aef99bSGreg Kurz         if (map_dirfd == -1) {
158f9aef99bSGreg Kurz             return;
159f9aef99bSGreg Kurz         }
160f9aef99bSGreg Kurz 
161f9aef99bSGreg Kurz         fp = local_fopenat(map_dirfd, name, "r");
162f9aef99bSGreg Kurz         close_preserve_errno(map_dirfd);
16381ffbf5aSGreg Kurz     } else {
16481ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
16581ffbf5aSGreg Kurz     }
1662c30dd74SAneesh Kumar K.V     if (!fp) {
1672c30dd74SAneesh Kumar K.V         return;
1682c30dd74SAneesh Kumar K.V     }
1692c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1702c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1712c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1722c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf + 11);
1732c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1742c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf + 11);
1752c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1762c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf + 12);
1772c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1782c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf + 12);
1792c30dd74SAneesh Kumar K.V         }
1802c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1812c30dd74SAneesh Kumar K.V     }
1822c30dd74SAneesh Kumar K.V     fclose(fp);
1832c30dd74SAneesh Kumar K.V }
1842c30dd74SAneesh Kumar K.V 
local_lstat(FsContext * fs_ctx,V9fsPath * fs_path,struct stat * stbuf)1852289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
186131dcb25SAnthony Liguori {
187f9aef99bSGreg Kurz     int err = -1;
188f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
189f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
190f9aef99bSGreg Kurz     int dirfd;
1912289be19SAneesh Kumar K.V 
192f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
193f9aef99bSGreg Kurz     if (dirfd == -1) {
194f9aef99bSGreg Kurz         goto out;
195f9aef99bSGreg Kurz     }
196f9aef99bSGreg Kurz 
1976ca60cd7SBin Meng     err = qemu_fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
1981237ad76SVenkateswararao Jujjuri (JV)     if (err) {
1994fa4ce71SChen Gang         goto err_out;
2001237ad76SVenkateswararao Jujjuri (JV)     }
201b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
2021237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
2031237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
2041237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
2051237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
2061237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
207f9aef99bSGreg Kurz 
208f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
209f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
210f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
2111237ad76SVenkateswararao Jujjuri (JV)         }
212f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
213f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
214f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
2151237ad76SVenkateswararao Jujjuri (JV)         }
216f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
217f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
218f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
2191237ad76SVenkateswararao Jujjuri (JV)         }
220f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
221f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
222f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
2231237ad76SVenkateswararao Jujjuri (JV)         }
2242c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
225f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
2261237ad76SVenkateswararao Jujjuri (JV)     }
2274fa4ce71SChen Gang 
2284fa4ce71SChen Gang err_out:
229f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
230f9aef99bSGreg Kurz out:
231f9aef99bSGreg Kurz     g_free(name);
232f9aef99bSGreg Kurz     g_free(dirpath);
2331237ad76SVenkateswararao Jujjuri (JV)     return err;
234131dcb25SAnthony Liguori }
235131dcb25SAnthony Liguori 
local_set_mapped_file_attrat(int dirfd,const char * name,FsCred * credp)236e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
237e3187a45SGreg Kurz                                         FsCred *credp)
2382c30dd74SAneesh Kumar K.V {
2392c30dd74SAneesh Kumar K.V     FILE *fp;
240e3187a45SGreg Kurz     int ret;
2412c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2422c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
24381ffbf5aSGreg Kurz     int map_dirfd = -1, map_fd;
24481ffbf5aSGreg Kurz     bool is_root = !strcmp(name, ".");
2452c30dd74SAneesh Kumar K.V 
24681ffbf5aSGreg Kurz     if (is_root) {
24781ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
24881ffbf5aSGreg Kurz         if (!fp) {
24981ffbf5aSGreg Kurz             if (errno == ENOENT) {
25081ffbf5aSGreg Kurz                 goto update_map_file;
25181ffbf5aSGreg Kurz             } else {
25281ffbf5aSGreg Kurz                 return -1;
25381ffbf5aSGreg Kurz             }
25481ffbf5aSGreg Kurz         }
25581ffbf5aSGreg Kurz     } else {
2566ca60cd7SBin Meng         ret = qemu_mkdirat(dirfd, VIRTFS_META_DIR, 0700);
257e3187a45SGreg Kurz         if (ret < 0 && errno != EEXIST) {
258e3187a45SGreg Kurz             return -1;
259e3187a45SGreg Kurz         }
260e3187a45SGreg Kurz 
261e3187a45SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
262e3187a45SGreg Kurz         if (map_dirfd == -1) {
263e3187a45SGreg Kurz             return -1;
264e3187a45SGreg Kurz         }
265e3187a45SGreg Kurz 
266e3187a45SGreg Kurz         fp = local_fopenat(map_dirfd, name, "r");
2672c30dd74SAneesh Kumar K.V         if (!fp) {
268e3187a45SGreg Kurz             if (errno == ENOENT) {
269e3187a45SGreg Kurz                 goto update_map_file;
270e3187a45SGreg Kurz             } else {
271e3187a45SGreg Kurz                 close_preserve_errno(map_dirfd);
272e3187a45SGreg Kurz                 return -1;
273e3187a45SGreg Kurz             }
2742c30dd74SAneesh Kumar K.V         }
27581ffbf5aSGreg Kurz     }
2762c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2772c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2782c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2792c30dd74SAneesh Kumar K.V             uid = atoi(buf + 11);
2802c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2812c30dd74SAneesh Kumar K.V             gid = atoi(buf + 11);
2822c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2832c30dd74SAneesh Kumar K.V             mode = atoi(buf + 12);
2842c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2852c30dd74SAneesh Kumar K.V             rdev = atoi(buf + 12);
2862c30dd74SAneesh Kumar K.V         }
2872c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2882c30dd74SAneesh Kumar K.V     }
2892c30dd74SAneesh Kumar K.V     fclose(fp);
2902c30dd74SAneesh Kumar K.V 
2912c30dd74SAneesh Kumar K.V update_map_file:
29281ffbf5aSGreg Kurz     if (is_root) {
29381ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
29481ffbf5aSGreg Kurz     } else {
295e3187a45SGreg Kurz         fp = local_fopenat(map_dirfd, name, "w");
29681ffbf5aSGreg Kurz         /* We can't go this far with map_dirfd not being a valid file descriptor
29781ffbf5aSGreg Kurz          * but some versions of gcc aren't smart enough to see it.
29881ffbf5aSGreg Kurz          */
29981ffbf5aSGreg Kurz         if (map_dirfd != -1) {
300e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
30181ffbf5aSGreg Kurz         }
30281ffbf5aSGreg Kurz     }
3032c30dd74SAneesh Kumar K.V     if (!fp) {
304e3187a45SGreg Kurz         return -1;
3052c30dd74SAneesh Kumar K.V     }
3062c30dd74SAneesh Kumar K.V 
30781ffbf5aSGreg Kurz     map_fd = fileno(fp);
30881ffbf5aSGreg Kurz     assert(map_fd != -1);
30981ffbf5aSGreg Kurz     ret = fchmod(map_fd, 0600);
31081ffbf5aSGreg Kurz     assert(ret == 0);
31181ffbf5aSGreg Kurz 
3122c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
3132c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
3142c30dd74SAneesh Kumar K.V     }
3152c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
3162c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
3172c30dd74SAneesh Kumar K.V     }
318230f1b31SKeno Fischer     if (credp->fc_mode != (mode_t)-1) {
3192c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
3202c30dd74SAneesh Kumar K.V     }
3212c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
3222c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
3232c30dd74SAneesh Kumar K.V     }
3242c30dd74SAneesh Kumar K.V 
3252c30dd74SAneesh Kumar K.V     if (uid != -1) {
3262c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
3272c30dd74SAneesh Kumar K.V     }
3282c30dd74SAneesh Kumar K.V     if (gid != -1) {
3292c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
3302c30dd74SAneesh Kumar K.V     }
3312c30dd74SAneesh Kumar K.V     if (mode != -1) {
3322c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
3332c30dd74SAneesh Kumar K.V     }
3342c30dd74SAneesh Kumar K.V     if (rdev != -1) {
3352c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
3362c30dd74SAneesh Kumar K.V     }
3372c30dd74SAneesh Kumar K.V     fclose(fp);
3382c30dd74SAneesh Kumar K.V 
339e3187a45SGreg Kurz     return 0;
340e3187a45SGreg Kurz }
341e3187a45SGreg Kurz 
fchmodat_nofollow(int dirfd,const char * name,mode_t mode)342e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
343e3187a45SGreg Kurz {
3444751fd53SGreg Kurz     struct stat stbuf;
345e3187a45SGreg Kurz     int fd, ret;
346e3187a45SGreg Kurz 
347e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
3484751fd53SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet.
349e3187a45SGreg Kurz      */
3504751fd53SGreg Kurz 
3514751fd53SGreg Kurz      /* First, we clear non-racing symlinks out of the way. */
3526ca60cd7SBin Meng     if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) {
3534751fd53SGreg Kurz         return -1;
3544751fd53SGreg Kurz     }
3554751fd53SGreg Kurz     if (S_ISLNK(stbuf.st_mode)) {
3564751fd53SGreg Kurz         errno = ELOOP;
3574751fd53SGreg Kurz         return -1;
3584751fd53SGreg Kurz     }
3594751fd53SGreg Kurz 
360aa5e85a1SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0);
3614751fd53SGreg Kurz #if O_PATH_9P_UTIL == 0
362aa5e85a1SGreg Kurz     /* Fallback for systems that don't support O_PATH: we depend on the file
363aa5e85a1SGreg Kurz      * being readable or writable.
364aa5e85a1SGreg Kurz      */
365e3187a45SGreg Kurz     if (fd == -1) {
366e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
367e3187a45SGreg Kurz         if (errno == EACCES) {
368e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
369e3187a45SGreg Kurz         }
370e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
371e3187a45SGreg Kurz             errno = EACCES;
372e3187a45SGreg Kurz         }
373e3187a45SGreg Kurz     }
374e3187a45SGreg Kurz     if (fd == -1) {
375e3187a45SGreg Kurz         return -1;
376e3187a45SGreg Kurz     }
377e3187a45SGreg Kurz     ret = fchmod(fd, mode);
3784751fd53SGreg Kurz #else
379aa5e85a1SGreg Kurz     /* Access modes are ignored when O_PATH is supported. If name is a symbolic
380aa5e85a1SGreg Kurz      * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor
381aa5e85a1SGreg Kurz      * referring to the symbolic link.
382aa5e85a1SGreg Kurz      */
3834751fd53SGreg Kurz     if (fd == -1) {
3844751fd53SGreg Kurz         return -1;
3854751fd53SGreg Kurz     }
3864751fd53SGreg Kurz 
3874751fd53SGreg Kurz     /* Now we handle racing symlinks. */
3884751fd53SGreg Kurz     ret = fstat(fd, &stbuf);
3894751fd53SGreg Kurz     if (!ret) {
3904751fd53SGreg Kurz         if (S_ISLNK(stbuf.st_mode)) {
3914751fd53SGreg Kurz             errno = ELOOP;
3924751fd53SGreg Kurz             ret = -1;
3934751fd53SGreg Kurz         } else {
3944751fd53SGreg Kurz             char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd);
3954751fd53SGreg Kurz             ret = chmod(proc_path, mode);
3964751fd53SGreg Kurz             g_free(proc_path);
3974751fd53SGreg Kurz         }
3984751fd53SGreg Kurz     }
3994751fd53SGreg Kurz #endif
400e3187a45SGreg Kurz     close_preserve_errno(fd);
4012c30dd74SAneesh Kumar K.V     return ret;
4022c30dd74SAneesh Kumar K.V }
4032c30dd74SAneesh Kumar K.V 
local_set_xattrat(int dirfd,const char * path,FsCred * credp)404e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
405131dcb25SAnthony Liguori {
406758e8e38SVenkateswararao Jujjuri (JV)     int err;
4072289be19SAneesh Kumar K.V 
408758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
409f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
410e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
411e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
412758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
413758e8e38SVenkateswararao Jujjuri (JV)             return err;
414131dcb25SAnthony Liguori         }
415131dcb25SAnthony Liguori     }
416758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
417f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
418e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
419e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
420758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
421758e8e38SVenkateswararao Jujjuri (JV)             return err;
422131dcb25SAnthony Liguori         }
423131dcb25SAnthony Liguori     }
424230f1b31SKeno Fischer     if (credp->fc_mode != (mode_t)-1) {
425f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
426e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
427e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
428758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
429758e8e38SVenkateswararao Jujjuri (JV)             return err;
430131dcb25SAnthony Liguori         }
431131dcb25SAnthony Liguori     }
432758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
433f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
434e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
435e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
436758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
437758e8e38SVenkateswararao Jujjuri (JV)             return err;
438131dcb25SAnthony Liguori         }
439758e8e38SVenkateswararao Jujjuri (JV)     }
440131dcb25SAnthony Liguori     return 0;
441131dcb25SAnthony Liguori }
442131dcb25SAnthony Liguori 
local_set_cred_passthrough(FsContext * fs_ctx,int dirfd,const char * name,FsCred * credp)443d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
444d815e721SGreg Kurz                                       const char *name, FsCred *credp)
4454750a96fSVenkateswararao Jujjuri (JV) {
446d815e721SGreg Kurz     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
447b314f6a0SGreg Kurz                  AT_SYMLINK_NOFOLLOW) < 0) {
44812848bfcSAneesh Kumar K.V         /*
44912848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
45012848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
45112848bfcSAneesh Kumar K.V          */
452b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
4534fa4ce71SChen Gang             return -1;
4544750a96fSVenkateswararao Jujjuri (JV)         }
455d815e721SGreg Kurz     }
456d815e721SGreg Kurz 
457d815e721SGreg Kurz     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
458d815e721SGreg Kurz }
4594750a96fSVenkateswararao Jujjuri (JV) 
local_readlink(FsContext * fs_ctx,V9fsPath * fs_path,char * buf,size_t bufsz)4602289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
461131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
462131dcb25SAnthony Liguori {
463879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
4642289be19SAneesh Kumar K.V 
4652c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
4662c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
467879c2813SVenkateswararao Jujjuri (JV)         int fd;
468bec1e954SGreg Kurz 
469bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
470879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
471879c2813SVenkateswararao Jujjuri (JV)             return -1;
472879c2813SVenkateswararao Jujjuri (JV)         }
47337b0b24eSNikita Ivanov         tsize = RETRY_ON_EINTR(read(fd, (void *)buf, bufsz));
474bec1e954SGreg Kurz         close_preserve_errno(fd);
475b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
476b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
477bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
478bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
479bec1e954SGreg Kurz         int dirfd;
480bec1e954SGreg Kurz 
481bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
482bec1e954SGreg Kurz         if (dirfd == -1) {
483bec1e954SGreg Kurz             goto out;
484bec1e954SGreg Kurz         }
485bec1e954SGreg Kurz 
486bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
487bec1e954SGreg Kurz         close_preserve_errno(dirfd);
488bec1e954SGreg Kurz     out:
489bec1e954SGreg Kurz         g_free(name);
490bec1e954SGreg Kurz         g_free(dirpath);
491879c2813SVenkateswararao Jujjuri (JV)     }
492879c2813SVenkateswararao Jujjuri (JV)     return tsize;
493131dcb25SAnthony Liguori }
494131dcb25SAnthony Liguori 
local_close(FsContext * ctx,V9fsFidOpenState * fs)495cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
496131dcb25SAnthony Liguori {
497cc720ddbSAneesh Kumar K.V     return close(fs->fd);
498131dcb25SAnthony Liguori }
499131dcb25SAnthony Liguori 
local_closedir(FsContext * ctx,V9fsFidOpenState * fs)500cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
501131dcb25SAnthony Liguori {
502f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
503131dcb25SAnthony Liguori }
5049f107513SAnthony Liguori 
local_open(FsContext * ctx,V9fsPath * fs_path,int flags,V9fsFidOpenState * fs)505cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
506cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
507a6568fe2SAnthony Liguori {
50821328e1eSGreg Kurz     int fd;
5092289be19SAneesh Kumar K.V 
510996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
51121328e1eSGreg Kurz     if (fd == -1) {
51221328e1eSGreg Kurz         return -1;
51321328e1eSGreg Kurz     }
51421328e1eSGreg Kurz     fs->fd = fd;
515cc720ddbSAneesh Kumar K.V     return fs->fd;
516a6568fe2SAnthony Liguori }
517a6568fe2SAnthony Liguori 
local_opendir(FsContext * ctx,V9fsPath * fs_path,V9fsFidOpenState * fs)518cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
519cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
520a6568fe2SAnthony Liguori {
521996a0d76SGreg Kurz     int dirfd;
52221328e1eSGreg Kurz     DIR *stream;
5232289be19SAneesh Kumar K.V 
524996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
525996a0d76SGreg Kurz     if (dirfd == -1) {
526cc720ddbSAneesh Kumar K.V         return -1;
527cc720ddbSAneesh Kumar K.V     }
528996a0d76SGreg Kurz 
529996a0d76SGreg Kurz     stream = fdopendir(dirfd);
53021328e1eSGreg Kurz     if (!stream) {
531faab207fSGreg Kurz         close(dirfd);
532cc720ddbSAneesh Kumar K.V         return -1;
533cc720ddbSAneesh Kumar K.V     }
53421328e1eSGreg Kurz     fs->dir.stream = stream;
535cc720ddbSAneesh Kumar K.V     return 0;
536a6568fe2SAnthony Liguori }
537a6568fe2SAnthony Liguori 
local_rewinddir(FsContext * ctx,V9fsFidOpenState * fs)538cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
539a9231555SAnthony Liguori {
540f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
541a9231555SAnthony Liguori }
542a9231555SAnthony Liguori 
local_telldir(FsContext * ctx,V9fsFidOpenState * fs)543cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
544a9231555SAnthony Liguori {
545f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
546a9231555SAnthony Liguori }
547a9231555SAnthony Liguori 
local_is_mapped_file_metadata(FsContext * fs_ctx,const char * name)5487a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
5497a95434eSGreg Kurz {
55081ffbf5aSGreg Kurz     return
55181ffbf5aSGreg Kurz         !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
5527a95434eSGreg Kurz }
5537a95434eSGreg Kurz 
local_readdir(FsContext * ctx,V9fsFidOpenState * fs)554635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
555a9231555SAnthony Liguori {
556635324e8SGreg Kurz     struct dirent *entry;
5572c30dd74SAneesh Kumar K.V 
5582c30dd74SAneesh Kumar K.V again:
559635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
560635324e8SGreg Kurz     if (!entry) {
561635324e8SGreg Kurz         return NULL;
562635324e8SGreg Kurz     }
5636b3b279bSKeno Fischer #ifdef CONFIG_DARWIN
5646b3b279bSKeno Fischer     int off;
5656b3b279bSKeno Fischer     off = telldir(fs->dir.stream);
5666b3b279bSKeno Fischer     /* If telldir fails, fail the entire readdir call */
5676b3b279bSKeno Fischer     if (off < 0) {
5686b3b279bSKeno Fischer         return NULL;
5696b3b279bSKeno Fischer     }
5706b3b279bSKeno Fischer     entry->d_seekoff = off;
5716b3b279bSKeno Fischer #endif
572635324e8SGreg Kurz 
573840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
574840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
575840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5767a95434eSGreg Kurz         if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
57781ffbf5aSGreg Kurz             /* skip the meta data */
5782c30dd74SAneesh Kumar K.V             goto again;
5792c30dd74SAneesh Kumar K.V         }
580840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
5812c30dd74SAneesh Kumar K.V     }
582635324e8SGreg Kurz 
583635324e8SGreg Kurz     return entry;
584a9231555SAnthony Liguori }
585a9231555SAnthony Liguori 
local_seekdir(FsContext * ctx,V9fsFidOpenState * fs,off_t off)586cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
587a9231555SAnthony Liguori {
588f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
589a9231555SAnthony Liguori }
590a9231555SAnthony Liguori 
local_preadv(FsContext * ctx,V9fsFidOpenState * fs,const struct iovec * iov,int iovcnt,off_t offset)591cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
592cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
59356d15a53SSanchit Garg                             int iovcnt, off_t offset)
594a9231555SAnthony Liguori {
59556d15a53SSanchit Garg #ifdef CONFIG_PREADV
596cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
59756d15a53SSanchit Garg #else
598cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
59956d15a53SSanchit Garg     if (err == -1) {
60056d15a53SSanchit Garg         return err;
60156d15a53SSanchit Garg     } else {
602cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
603a9231555SAnthony Liguori     }
60456d15a53SSanchit Garg #endif
605a9231555SAnthony Liguori }
606a9231555SAnthony Liguori 
local_pwritev(FsContext * ctx,V9fsFidOpenState * fs,const struct iovec * iov,int iovcnt,off_t offset)607cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
608cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
60956d15a53SSanchit Garg                              int iovcnt, off_t offset)
6108449360cSAnthony Liguori {
6116fe76accSGreg Kurz     ssize_t ret;
61256d15a53SSanchit Garg #ifdef CONFIG_PREADV
613cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
61456d15a53SSanchit Garg #else
615cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
61656d15a53SSanchit Garg     if (err == -1) {
61756d15a53SSanchit Garg         return err;
61856d15a53SSanchit Garg     } else {
619cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
6208449360cSAnthony Liguori     }
62156d15a53SSanchit Garg #endif
622d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
623d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
624d3ab98e6SAneesh Kumar K.V         /*
625d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
626d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
62728cbbdd2SMichael Tokarev          * after write when writeout=immediate is specified.
628d3ab98e6SAneesh Kumar K.V          */
629cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
630d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
631d3ab98e6SAneesh Kumar K.V     }
632d3ab98e6SAneesh Kumar K.V #endif
633d3ab98e6SAneesh Kumar K.V     return ret;
63456d15a53SSanchit Garg }
6358449360cSAnthony Liguori 
local_chmod(FsContext * fs_ctx,V9fsPath * fs_path,FsCred * credp)6362289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
637c494dd6fSAnthony Liguori {
638e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
639e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
6404fa4ce71SChen Gang     int ret = -1;
641e3187a45SGreg Kurz     int dirfd;
642e3187a45SGreg Kurz 
643e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
644e3187a45SGreg Kurz     if (dirfd == -1) {
645e3187a45SGreg Kurz         goto out;
646e3187a45SGreg Kurz     }
6472289be19SAneesh Kumar K.V 
648b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
649e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
6502c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
651e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
652e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
653e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
654e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
655e95ead32SVenkateswararao Jujjuri (JV)     }
656e3187a45SGreg Kurz     close_preserve_errno(dirfd);
657e3187a45SGreg Kurz 
658e3187a45SGreg Kurz out:
659e3187a45SGreg Kurz     g_free(dirpath);
660e3187a45SGreg Kurz     g_free(name);
6614fa4ce71SChen Gang     return ret;
662c494dd6fSAnthony Liguori }
663c494dd6fSAnthony Liguori 
local_mknod(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,FsCred * credp)6642289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
6652289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
666c494dd6fSAnthony Liguori {
6671c293312SVenkateswararao Jujjuri (JV)     int err = -1;
668d815e721SGreg Kurz     int dirfd;
6691c293312SVenkateswararao Jujjuri (JV) 
6707a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6717a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6727a95434eSGreg Kurz         errno = EINVAL;
6737a95434eSGreg Kurz         return -1;
6747a95434eSGreg Kurz     }
6757a95434eSGreg Kurz 
676d815e721SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
677d815e721SGreg Kurz     if (dirfd == -1) {
678d815e721SGreg Kurz         return -1;
679d815e721SGreg Kurz     }
6802289be19SAneesh Kumar K.V 
681d815e721SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
682d815e721SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
683029ed1bdSKeno Fischer         err = qemu_mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
684d815e721SGreg Kurz         if (err == -1) {
685d815e721SGreg Kurz             goto out;
686d815e721SGreg Kurz         }
687d815e721SGreg Kurz 
688b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
689d815e721SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
690d815e721SGreg Kurz         } else {
691d815e721SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
6921c293312SVenkateswararao Jujjuri (JV)         }
6931c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6941c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6951c293312SVenkateswararao Jujjuri (JV)         }
696d815e721SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
697d815e721SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
698029ed1bdSKeno Fischer         err = qemu_mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
6992c30dd74SAneesh Kumar K.V         if (err == -1) {
7002c30dd74SAneesh Kumar K.V             goto out;
7012c30dd74SAneesh Kumar K.V         }
702d815e721SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
7032c30dd74SAneesh Kumar K.V         if (err == -1) {
7041c293312SVenkateswararao Jujjuri (JV)             goto err_end;
7051c293312SVenkateswararao Jujjuri (JV)         }
7061c293312SVenkateswararao Jujjuri (JV)     }
7072289be19SAneesh Kumar K.V     goto out;
7081c293312SVenkateswararao Jujjuri (JV) 
7091c293312SVenkateswararao Jujjuri (JV) err_end:
710d815e721SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
7112289be19SAneesh Kumar K.V out:
712d815e721SGreg Kurz     close_preserve_errno(dirfd);
7131c293312SVenkateswararao Jujjuri (JV)     return err;
714c494dd6fSAnthony Liguori }
715c494dd6fSAnthony Liguori 
local_mkdir(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,FsCred * credp)7162289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
7172289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
718c494dd6fSAnthony Liguori {
71900ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
7203f3a1699SGreg Kurz     int dirfd;
72100ec5c37SVenkateswararao Jujjuri (JV) 
7227a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7237a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7247a95434eSGreg Kurz         errno = EINVAL;
7257a95434eSGreg Kurz         return -1;
7267a95434eSGreg Kurz     }
7277a95434eSGreg Kurz 
7283f3a1699SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
7293f3a1699SGreg Kurz     if (dirfd == -1) {
7303f3a1699SGreg Kurz         return -1;
7313f3a1699SGreg Kurz     }
7322289be19SAneesh Kumar K.V 
7333f3a1699SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
7343f3a1699SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7356ca60cd7SBin Meng         err = qemu_mkdirat(dirfd, name, fs_ctx->dmode);
7363f3a1699SGreg Kurz         if (err == -1) {
7373f3a1699SGreg Kurz             goto out;
7383f3a1699SGreg Kurz         }
7393f3a1699SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFDIR;
7403f3a1699SGreg Kurz 
741b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7423f3a1699SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
7433f3a1699SGreg Kurz         } else {
7443f3a1699SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
74500ec5c37SVenkateswararao Jujjuri (JV)         }
74600ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
74700ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
74800ec5c37SVenkateswararao Jujjuri (JV)         }
7493f3a1699SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
7503f3a1699SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
7516ca60cd7SBin Meng         err = qemu_mkdirat(dirfd, name, credp->fc_mode);
7522c30dd74SAneesh Kumar K.V         if (err == -1) {
7532c30dd74SAneesh Kumar K.V             goto out;
7542c30dd74SAneesh Kumar K.V         }
7553f3a1699SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
7562c30dd74SAneesh Kumar K.V         if (err == -1) {
75700ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
75800ec5c37SVenkateswararao Jujjuri (JV)         }
75900ec5c37SVenkateswararao Jujjuri (JV)     }
7602289be19SAneesh Kumar K.V     goto out;
76100ec5c37SVenkateswararao Jujjuri (JV) 
76200ec5c37SVenkateswararao Jujjuri (JV) err_end:
7633f3a1699SGreg Kurz     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
7642289be19SAneesh Kumar K.V out:
7653f3a1699SGreg Kurz     close_preserve_errno(dirfd);
76600ec5c37SVenkateswararao Jujjuri (JV)     return err;
767c494dd6fSAnthony Liguori }
768c494dd6fSAnthony Liguori 
local_fid_fd(int fid_type,V9fsFidOpenState * fs)7694f82ce8cSGreg Kurz static int local_fid_fd(int fid_type, V9fsFidOpenState *fs)
7704f82ce8cSGreg Kurz {
7714f82ce8cSGreg Kurz     if (fid_type == P9_FID_DIR) {
7724f82ce8cSGreg Kurz         return dirfd(fs->dir.stream);
7734f82ce8cSGreg Kurz     } else {
7744f82ce8cSGreg Kurz         return fs->fd;
7754f82ce8cSGreg Kurz     }
7764f82ce8cSGreg Kurz }
7774f82ce8cSGreg Kurz 
local_fstat(FsContext * fs_ctx,int fid_type,V9fsFidOpenState * fs,struct stat * stbuf)7788b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
779cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
780c494dd6fSAnthony Liguori {
7814f82ce8cSGreg Kurz     int err, fd = local_fid_fd(fid_type, fs);
7828b888272SAneesh Kumar K.V 
7838b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
7841237ad76SVenkateswararao Jujjuri (JV)     if (err) {
7851237ad76SVenkateswararao Jujjuri (JV)         return err;
7861237ad76SVenkateswararao Jujjuri (JV)     }
787b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7881237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
7891237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
7901237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
7911237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
7921237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
7931237ad76SVenkateswararao Jujjuri (JV) 
794b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.uid",
795b5989326SKeno Fischer                            &tmp_uid, sizeof(uid_t)) > 0) {
796f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
7971237ad76SVenkateswararao Jujjuri (JV)         }
798b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.gid",
799b5989326SKeno Fischer                            &tmp_gid, sizeof(gid_t)) > 0) {
800f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
8011237ad76SVenkateswararao Jujjuri (JV)         }
802b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.mode",
803b5989326SKeno Fischer                            &tmp_mode, sizeof(mode_t)) > 0) {
804f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
8051237ad76SVenkateswararao Jujjuri (JV)         }
806b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.rdev",
807b5989326SKeno Fischer                            &tmp_dev, sizeof(dev_t)) > 0) {
808f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
8091237ad76SVenkateswararao Jujjuri (JV)         }
8102c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
8112c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
8122c30dd74SAneesh Kumar K.V         return -1;
8131237ad76SVenkateswararao Jujjuri (JV)     }
8141237ad76SVenkateswararao Jujjuri (JV)     return err;
815c494dd6fSAnthony Liguori }
816c494dd6fSAnthony Liguori 
local_open2(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,int flags,FsCred * credp,V9fsFidOpenState * fs)8172289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
818cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
819c494dd6fSAnthony Liguori {
8204750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
8214750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
822a565fea5SGreg Kurz     int dirfd;
8234750a96fSVenkateswararao Jujjuri (JV) 
8247a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8257a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8267a95434eSGreg Kurz         errno = EINVAL;
8277a95434eSGreg Kurz         return -1;
8287a95434eSGreg Kurz     }
8297a95434eSGreg Kurz 
8300ceb092eSAneesh Kumar K.V     /*
8310ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
8320ceb092eSAneesh Kumar K.V      */
8330ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
8340ceb092eSAneesh Kumar K.V 
835a565fea5SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
836a565fea5SGreg Kurz     if (dirfd == -1) {
837a565fea5SGreg Kurz         return -1;
838a565fea5SGreg Kurz     }
8392289be19SAneesh Kumar K.V 
8404750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
841a565fea5SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
842a565fea5SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
843b96feb2cSTobias Schramm         fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
844a565fea5SGreg Kurz         if (fd == -1) {
845a565fea5SGreg Kurz             goto out;
846a565fea5SGreg Kurz         }
847a565fea5SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFREG;
848b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
84928cbbdd2SMichael Tokarev             /* Set client credentials in xattr */
850a565fea5SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
851a565fea5SGreg Kurz         } else {
852a565fea5SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
8534750a96fSVenkateswararao Jujjuri (JV)         }
8542c30dd74SAneesh Kumar K.V         if (err == -1) {
8552c30dd74SAneesh Kumar K.V             goto err_end;
8562c30dd74SAneesh Kumar K.V         }
857b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
858b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
859a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, credp->fc_mode);
8604750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
8612289be19SAneesh Kumar K.V             goto out;
8624750a96fSVenkateswararao Jujjuri (JV)         }
863a565fea5SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
8644750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
8654750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
8664750a96fSVenkateswararao Jujjuri (JV)         }
8674750a96fSVenkateswararao Jujjuri (JV)     }
8682289be19SAneesh Kumar K.V     err = fd;
869cc720ddbSAneesh Kumar K.V     fs->fd = fd;
8702289be19SAneesh Kumar K.V     goto out;
8714750a96fSVenkateswararao Jujjuri (JV) 
8724750a96fSVenkateswararao Jujjuri (JV) err_end:
873a565fea5SGreg Kurz     unlinkat_preserve_errno(dirfd, name,
874a565fea5SGreg Kurz                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
875a565fea5SGreg Kurz     close_preserve_errno(fd);
8762289be19SAneesh Kumar K.V out:
877a565fea5SGreg Kurz     close_preserve_errno(dirfd);
8784750a96fSVenkateswararao Jujjuri (JV)     return err;
879c494dd6fSAnthony Liguori }
880c494dd6fSAnthony Liguori 
881758e8e38SVenkateswararao Jujjuri (JV) 
local_symlink(FsContext * fs_ctx,const char * oldpath,V9fsPath * dir_path,const char * name,FsCred * credp)882879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
8832289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
884c494dd6fSAnthony Liguori {
885879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
88638771613SGreg Kurz     int dirfd;
887879c2813SVenkateswararao Jujjuri (JV) 
8887a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8897a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8907a95434eSGreg Kurz         errno = EINVAL;
8917a95434eSGreg Kurz         return -1;
8927a95434eSGreg Kurz     }
8937a95434eSGreg Kurz 
89438771613SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
89538771613SGreg Kurz     if (dirfd == -1) {
89638771613SGreg Kurz         return -1;
89738771613SGreg Kurz     }
8982289be19SAneesh Kumar K.V 
899879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
90038771613SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
90138771613SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
90238771613SGreg Kurz         int fd;
90338771613SGreg Kurz         ssize_t oldpath_size, write_size;
90438771613SGreg Kurz 
90538771613SGreg Kurz         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
906b96feb2cSTobias Schramm                          fs_ctx->fmode);
90738771613SGreg Kurz         if (fd == -1) {
90838771613SGreg Kurz             goto out;
90938771613SGreg Kurz         }
91038771613SGreg Kurz         /* Write the oldpath (target) to the file. */
91138771613SGreg Kurz         oldpath_size = strlen(oldpath);
91237b0b24eSNikita Ivanov         write_size = RETRY_ON_EINTR(write(fd, (void *)oldpath, oldpath_size));
91338771613SGreg Kurz         close_preserve_errno(fd);
91438771613SGreg Kurz 
91538771613SGreg Kurz         if (write_size != oldpath_size) {
91638771613SGreg Kurz             goto err_end;
91738771613SGreg Kurz         }
91828cbbdd2SMichael Tokarev         /* Set client credentials in symlink's xattr */
91938771613SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFLNK;
92038771613SGreg Kurz 
921b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
92238771613SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
92338771613SGreg Kurz         } else {
92438771613SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
925879c2813SVenkateswararao Jujjuri (JV)         }
926879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
927879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
928879c2813SVenkateswararao Jujjuri (JV)         }
92938771613SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
93038771613SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
93138771613SGreg Kurz         err = symlinkat(oldpath, dirfd, name);
932879c2813SVenkateswararao Jujjuri (JV)         if (err) {
9332289be19SAneesh Kumar K.V             goto out;
934879c2813SVenkateswararao Jujjuri (JV)         }
93538771613SGreg Kurz         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
93638771613SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
937879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
93812848bfcSAneesh Kumar K.V             /*
93912848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
94012848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
94112848bfcSAneesh Kumar K.V              */
942b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
943879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
94438771613SGreg Kurz             } else {
94512848bfcSAneesh Kumar K.V                 err = 0;
946879c2813SVenkateswararao Jujjuri (JV)             }
947879c2813SVenkateswararao Jujjuri (JV)         }
94838771613SGreg Kurz     }
9492289be19SAneesh Kumar K.V     goto out;
950879c2813SVenkateswararao Jujjuri (JV) 
951879c2813SVenkateswararao Jujjuri (JV) err_end:
95238771613SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
9532289be19SAneesh Kumar K.V out:
95438771613SGreg Kurz     close_preserve_errno(dirfd);
955879c2813SVenkateswararao Jujjuri (JV)     return err;
956c494dd6fSAnthony Liguori }
957c494dd6fSAnthony Liguori 
local_link(FsContext * ctx,V9fsPath * oldpath,V9fsPath * dirpath,const char * name)9582289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
9592289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
960c494dd6fSAnthony Liguori {
961ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
962ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
963ad0b46e6SGreg Kurz     int ret = -1;
964ad0b46e6SGreg Kurz     int odirfd, ndirfd;
965c494dd6fSAnthony Liguori 
9667a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
9677a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
9687a95434eSGreg Kurz         errno = EINVAL;
969841b8d09SJiajun Chen         goto out;
9707a95434eSGreg Kurz     }
9717a95434eSGreg Kurz 
972ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
973ad0b46e6SGreg Kurz     if (odirfd == -1) {
9746dd4b1f1SGreg Kurz         goto out;
9756dd4b1f1SGreg Kurz     }
9762289be19SAneesh Kumar K.V 
977ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
978ad0b46e6SGreg Kurz     if (ndirfd == -1) {
979ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
980ad0b46e6SGreg Kurz         goto out;
981ad0b46e6SGreg Kurz     }
982ad0b46e6SGreg Kurz 
983ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
984ad0b46e6SGreg Kurz     if (ret < 0) {
985ad0b46e6SGreg Kurz         goto out_close;
986ad0b46e6SGreg Kurz     }
9872c30dd74SAneesh Kumar K.V 
9882c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
9896dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
990ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
9916dd4b1f1SGreg Kurz 
9926ca60cd7SBin Meng         ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
993ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
994ad0b46e6SGreg Kurz             goto err_undo_link;
9952c30dd74SAneesh Kumar K.V         }
996ad0b46e6SGreg Kurz 
997ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
998ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
999ad0b46e6SGreg Kurz             goto err;
1000ad0b46e6SGreg Kurz         }
1001ad0b46e6SGreg Kurz 
1002ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1003ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
1004ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
1005ad0b46e6SGreg Kurz             goto err;
1006ad0b46e6SGreg Kurz         }
1007ad0b46e6SGreg Kurz 
1008ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1009ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
1010ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
10112c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
1012ad0b46e6SGreg Kurz             goto err_undo_link;
10132c30dd74SAneesh Kumar K.V         }
10146dd4b1f1SGreg Kurz 
1015ad0b46e6SGreg Kurz         ret = 0;
10162c30dd74SAneesh Kumar K.V     }
1017ad0b46e6SGreg Kurz     goto out_close;
1018ad0b46e6SGreg Kurz 
1019ad0b46e6SGreg Kurz err:
1020ad0b46e6SGreg Kurz     ret = -1;
1021ad0b46e6SGreg Kurz err_undo_link:
1022ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
1023ad0b46e6SGreg Kurz out_close:
1024ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
1025ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
10266dd4b1f1SGreg Kurz out:
1027ad0b46e6SGreg Kurz     g_free(oname);
1028ad0b46e6SGreg Kurz     g_free(odirpath);
10292289be19SAneesh Kumar K.V     return ret;
1030c494dd6fSAnthony Liguori }
1031c494dd6fSAnthony Liguori 
local_truncate(FsContext * ctx,V9fsPath * fs_path,off_t size)10322289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
10338cf89e00SAnthony Liguori {
1034ac125d99SGreg Kurz     int fd, ret;
10352289be19SAneesh Kumar K.V 
1036ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1037ac125d99SGreg Kurz     if (fd == -1) {
1038ac125d99SGreg Kurz         return -1;
1039ac125d99SGreg Kurz     }
1040ac125d99SGreg Kurz     ret = ftruncate(fd, size);
1041ac125d99SGreg Kurz     close_preserve_errno(fd);
10424fa4ce71SChen Gang     return ret;
10438cf89e00SAnthony Liguori }
10448cf89e00SAnthony Liguori 
local_ftruncate(FsContext * ctx,int fid_type,V9fsFidOpenState * fs,off_t size)10450c798dd5SGreg Kurz static int local_ftruncate(FsContext *ctx, int fid_type, V9fsFidOpenState *fs,
10460c798dd5SGreg Kurz                            off_t size)
10470c798dd5SGreg Kurz {
10480c798dd5SGreg Kurz     int fd = local_fid_fd(fid_type, fs);
10490c798dd5SGreg Kurz 
10500c798dd5SGreg Kurz     return ftruncate(fd, size);
10510c798dd5SGreg Kurz }
10520c798dd5SGreg Kurz 
local_chown(FsContext * fs_ctx,V9fsPath * fs_path,FsCred * credp)10532289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
10548cf89e00SAnthony Liguori {
1055d369f207SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1056d369f207SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
10574fa4ce71SChen Gang     int ret = -1;
1058d369f207SGreg Kurz     int dirfd;
1059d369f207SGreg Kurz 
1060d369f207SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1061d369f207SGreg Kurz     if (dirfd == -1) {
1062d369f207SGreg Kurz         goto out;
1063d369f207SGreg Kurz     }
10642289be19SAneesh Kumar K.V 
1065c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
106617b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
106717b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1068d369f207SGreg Kurz         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1069d369f207SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
1070b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1071d369f207SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
10722c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1073d369f207SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1074f7613beeSVenkateswararao Jujjuri (JV)     }
1075d369f207SGreg Kurz 
1076d369f207SGreg Kurz     close_preserve_errno(dirfd);
1077d369f207SGreg Kurz out:
1078d369f207SGreg Kurz     g_free(name);
1079d369f207SGreg Kurz     g_free(dirpath);
10804fa4ce71SChen Gang     return ret;
10818cf89e00SAnthony Liguori }
10828cf89e00SAnthony Liguori 
local_utimensat(FsContext * s,V9fsPath * fs_path,const struct timespec * buf)10832289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
108474bc02b2SM. Mohan Kumar                            const struct timespec *buf)
10858cf89e00SAnthony Liguori {
1086a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1087a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1088a33eda0dSGreg Kurz     int dirfd, ret = -1;
10892289be19SAneesh Kumar K.V 
1090a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1091a33eda0dSGreg Kurz     if (dirfd == -1) {
1092a33eda0dSGreg Kurz         goto out;
1093a33eda0dSGreg Kurz     }
1094a33eda0dSGreg Kurz 
10956ca60cd7SBin Meng     ret = qemu_utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1096a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1097a33eda0dSGreg Kurz out:
1098a33eda0dSGreg Kurz     g_free(dirpath);
1099a33eda0dSGreg Kurz     g_free(name);
11004fa4ce71SChen Gang     return ret;
11018cf89e00SAnthony Liguori }
11028cf89e00SAnthony Liguori 
local_futimens(FsContext * s,int fid_type,V9fsFidOpenState * fs,const struct timespec * times)1103*371a269fSGreg Kurz static int local_futimens(FsContext *s, int fid_type, V9fsFidOpenState *fs,
1104*371a269fSGreg Kurz                           const struct timespec *times)
1105*371a269fSGreg Kurz {
1106*371a269fSGreg Kurz     int fd = local_fid_fd(fid_type, fs);
1107*371a269fSGreg Kurz 
1108*371a269fSGreg Kurz     return qemu_futimens(fd, times);
1109*371a269fSGreg Kurz }
1110*371a269fSGreg Kurz 
local_unlinkat_common(FsContext * ctx,int dirfd,const char * name,int flags)1111df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1112df4938a6SGreg Kurz                                  int flags)
11135bae1900SAnthony Liguori {
1114846cf408SDaniel Henrique Barboza     int ret;
11152c30dd74SAneesh Kumar K.V 
11162c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1117df4938a6SGreg Kurz         int map_dirfd;
1118df4938a6SGreg Kurz 
11196a87e792SGreg Kurz         /* We need to remove the metadata as well:
11206a87e792SGreg Kurz          * - the metadata directory if we're removing a directory
11216a87e792SGreg Kurz          * - the metadata file in the parent's metadata directory
11226a87e792SGreg Kurz          *
11236a87e792SGreg Kurz          * If any of these are missing (ie, ENOENT) then we're probably
11246a87e792SGreg Kurz          * trying to remove something that wasn't created in mapped-file
11256a87e792SGreg Kurz          * mode. We just ignore the error.
11266a87e792SGreg Kurz          */
1127df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1128df4938a6SGreg Kurz             int fd;
1129df4938a6SGreg Kurz 
1130b003fc0dSGreg Kurz             fd = openat_dir(dirfd, name);
1131df4938a6SGreg Kurz             if (fd == -1) {
1132846cf408SDaniel Henrique Barboza                 return -1;
11332c30dd74SAneesh Kumar K.V             }
11346ca60cd7SBin Meng             ret = qemu_unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1135df4938a6SGreg Kurz             close_preserve_errno(fd);
1136df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1137846cf408SDaniel Henrique Barboza                 return -1;
11382c30dd74SAneesh Kumar K.V             }
11392c30dd74SAneesh Kumar K.V         }
1140df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
11416a87e792SGreg Kurz         if (map_dirfd != -1) {
11426ca60cd7SBin Meng             ret = qemu_unlinkat(map_dirfd, name, 0);
1143df4938a6SGreg Kurz             close_preserve_errno(map_dirfd);
1144df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1145846cf408SDaniel Henrique Barboza                 return -1;
11466a87e792SGreg Kurz             }
11476a87e792SGreg Kurz         } else if (errno != ENOENT) {
1148846cf408SDaniel Henrique Barboza             return -1;
11492c30dd74SAneesh Kumar K.V         }
11502c30dd74SAneesh Kumar K.V     }
11514fa4ce71SChen Gang 
11526ca60cd7SBin Meng     return qemu_unlinkat(dirfd, name, flags);
1153df4938a6SGreg Kurz }
1154df4938a6SGreg Kurz 
local_remove(FsContext * ctx,const char * path)11558cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
11568cf89e00SAnthony Liguori {
11578cf89e00SAnthony Liguori     struct stat stbuf;
1158a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1159a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1160a0e640a8SGreg Kurz     int flags = 0;
1161a0e640a8SGreg Kurz     int dirfd;
1162a0e640a8SGreg Kurz     int err = -1;
11638cf89e00SAnthony Liguori 
1164a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1165b7361d46SGreg Kurz     if (dirfd == -1) {
1166a0e640a8SGreg Kurz         goto out;
1167a0e640a8SGreg Kurz     }
1168a0e640a8SGreg Kurz 
11696ca60cd7SBin Meng     if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
11708cf89e00SAnthony Liguori         goto err_out;
11718cf89e00SAnthony Liguori     }
1172a0e640a8SGreg Kurz 
11738cf89e00SAnthony Liguori     if (S_ISDIR(stbuf.st_mode)) {
1174a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1175faa44e3dSVenkateswararao Jujjuri (JV)     }
11764fa4ce71SChen Gang 
1177a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
11782c30dd74SAneesh Kumar K.V err_out:
1179a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1180a0e640a8SGreg Kurz out:
1181a0e640a8SGreg Kurz     g_free(name);
1182a0e640a8SGreg Kurz     g_free(dirpath);
11832c30dd74SAneesh Kumar K.V     return err;
11845bae1900SAnthony Liguori }
11855bae1900SAnthony Liguori 
local_fsync(FsContext * ctx,int fid_type,V9fsFidOpenState * fs,int datasync)11868b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
11878b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
11888cf89e00SAnthony Liguori {
11894f82ce8cSGreg Kurz     int fd = local_fid_fd(fid_type, fs);
11908b888272SAneesh Kumar K.V 
11918b888272SAneesh Kumar K.V     if (datasync) {
11928b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
11938b888272SAneesh Kumar K.V     } else {
11948b888272SAneesh Kumar K.V         return fsync(fd);
11958cf89e00SAnthony Liguori     }
119649594973SVenkateswararao Jujjuri (JV) }
11978cf89e00SAnthony Liguori 
local_statfs(FsContext * s,V9fsPath * fs_path,struct statfs * stbuf)11982289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1199be940c87SM. Mohan Kumar {
120031e51d1cSGreg Kurz     int fd, ret;
12012289be19SAneesh Kumar K.V 
120231e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
120323da0145SGreg Kurz     if (fd == -1) {
120423da0145SGreg Kurz         return -1;
120523da0145SGreg Kurz     }
120631e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
120731e51d1cSGreg Kurz     close_preserve_errno(fd);
12084fa4ce71SChen Gang     return ret;
1209be940c87SM. Mohan Kumar }
1210be940c87SM. Mohan Kumar 
local_lgetxattr(FsContext * ctx,V9fsPath * fs_path,const char * name,void * value,size_t size)12112289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1212fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1213fa32ef88SAneesh Kumar K.V {
12142289be19SAneesh Kumar K.V     char *path = fs_path->data;
12152289be19SAneesh Kumar K.V 
1216fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1217fa32ef88SAneesh Kumar K.V }
1218fa32ef88SAneesh Kumar K.V 
local_llistxattr(FsContext * ctx,V9fsPath * fs_path,void * value,size_t size)12192289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1220fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1221fa32ef88SAneesh Kumar K.V {
12222289be19SAneesh Kumar K.V     char *path = fs_path->data;
12232289be19SAneesh Kumar K.V 
1224fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
122561b6c499SAneesh Kumar K.V }
122661b6c499SAneesh Kumar K.V 
local_lsetxattr(FsContext * ctx,V9fsPath * fs_path,const char * name,void * value,size_t size,int flags)12272289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
122810b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
122910b468bdSAneesh Kumar K.V {
12302289be19SAneesh Kumar K.V     char *path = fs_path->data;
12312289be19SAneesh Kumar K.V 
1232fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
123310b468bdSAneesh Kumar K.V }
123410b468bdSAneesh Kumar K.V 
local_lremovexattr(FsContext * ctx,V9fsPath * fs_path,const char * name)12352289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
12362289be19SAneesh Kumar K.V                               const char *name)
12379ed3ef26SAneesh Kumar K.V {
12382289be19SAneesh Kumar K.V     char *path = fs_path->data;
12392289be19SAneesh Kumar K.V 
1240fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
12419ed3ef26SAneesh Kumar K.V }
12429ed3ef26SAneesh Kumar K.V 
local_name_to_path(FsContext * ctx,V9fsPath * dir_path,const char * name,V9fsPath * target)12432289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
12442289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
12452289be19SAneesh Kumar K.V {
12467a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12477a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
12487a95434eSGreg Kurz         errno = EINVAL;
12497a95434eSGreg Kurz         return -1;
12507a95434eSGreg Kurz     }
12517a95434eSGreg Kurz 
12522289be19SAneesh Kumar K.V     if (dir_path) {
1253f57f5878SGreg Kurz         if (!strcmp(name, ".")) {
1254f57f5878SGreg Kurz             /* "." relative to "foo/bar" is "foo/bar" */
1255f57f5878SGreg Kurz             v9fs_path_copy(target, dir_path);
1256f57f5878SGreg Kurz         } else if (!strcmp(name, "..")) {
1257f57f5878SGreg Kurz             if (!strcmp(dir_path->data, ".")) {
1258f57f5878SGreg Kurz                 /* ".." relative to the root is "." */
1259f57f5878SGreg Kurz                 v9fs_path_sprintf(target, ".");
12609c6b899fSGreg Kurz             } else {
1261f57f5878SGreg Kurz                 char *tmp = g_path_get_dirname(dir_path->data);
1262f57f5878SGreg Kurz                 /* Symbolic links are resolved by the client. We can assume
1263f57f5878SGreg Kurz                  * that ".." relative to "foo/bar" is equivalent to "foo"
12649c6b899fSGreg Kurz                  */
1265f57f5878SGreg Kurz                 v9fs_path_sprintf(target, "%s", tmp);
1266f57f5878SGreg Kurz                 g_free(tmp);
1267f57f5878SGreg Kurz             }
1268f57f5878SGreg Kurz         } else {
1269f57f5878SGreg Kurz             assert(!strchr(name, '/'));
1270f57f5878SGreg Kurz             v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1271f57f5878SGreg Kurz         }
1272f57f5878SGreg Kurz     } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1273f57f5878SGreg Kurz                !strcmp(name, "..")) {
1274f57f5878SGreg Kurz             /* This is the root fid */
1275f57f5878SGreg Kurz         v9fs_path_sprintf(target, ".");
1276f57f5878SGreg Kurz     } else {
1277f57f5878SGreg Kurz         assert(!strchr(name, '/'));
1278f57f5878SGreg Kurz         v9fs_path_sprintf(target, "./%s", name);
12792289be19SAneesh Kumar K.V     }
12802289be19SAneesh Kumar K.V     return 0;
12812289be19SAneesh Kumar K.V }
12822289be19SAneesh Kumar K.V 
local_renameat(FsContext * ctx,V9fsPath * olddir,const char * old_name,V9fsPath * newdir,const char * new_name)12832289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
12842289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
12852289be19SAneesh Kumar K.V                           const char *new_name)
12862289be19SAneesh Kumar K.V {
12872289be19SAneesh Kumar K.V     int ret;
128899f2cf4bSGreg Kurz     int odirfd, ndirfd;
12892289be19SAneesh Kumar K.V 
12907a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12917a95434eSGreg Kurz         (local_is_mapped_file_metadata(ctx, old_name) ||
12927a95434eSGreg Kurz          local_is_mapped_file_metadata(ctx, new_name))) {
12937a95434eSGreg Kurz         errno = EINVAL;
12947a95434eSGreg Kurz         return -1;
12957a95434eSGreg Kurz     }
12967a95434eSGreg Kurz 
129799f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
129899f2cf4bSGreg Kurz     if (odirfd == -1) {
129999f2cf4bSGreg Kurz         return -1;
130099f2cf4bSGreg Kurz     }
13012289be19SAneesh Kumar K.V 
130299f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
130399f2cf4bSGreg Kurz     if (ndirfd == -1) {
130499f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
130599f2cf4bSGreg Kurz         return -1;
130699f2cf4bSGreg Kurz     }
13072289be19SAneesh Kumar K.V 
13086ca60cd7SBin Meng     ret = qemu_renameat(odirfd, old_name, ndirfd, new_name);
130999f2cf4bSGreg Kurz     if (ret < 0) {
131099f2cf4bSGreg Kurz         goto out;
131199f2cf4bSGreg Kurz     }
131299f2cf4bSGreg Kurz 
131399f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
131499f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
131599f2cf4bSGreg Kurz 
13166ca60cd7SBin Meng         ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
131799f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
131899f2cf4bSGreg Kurz             goto err_undo_rename;
131999f2cf4bSGreg Kurz         }
132099f2cf4bSGreg Kurz 
13216dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
132299f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
132399f2cf4bSGreg Kurz             goto err;
132499f2cf4bSGreg Kurz         }
132599f2cf4bSGreg Kurz 
13266dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
132799f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
132899f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
132999f2cf4bSGreg Kurz             goto err;
133099f2cf4bSGreg Kurz         }
133199f2cf4bSGreg Kurz 
133299f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
13336ca60cd7SBin Meng         ret = qemu_renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
133499f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
133599f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
133699f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
133799f2cf4bSGreg Kurz             goto err_undo_rename;
133899f2cf4bSGreg Kurz         }
133999f2cf4bSGreg Kurz 
134099f2cf4bSGreg Kurz         ret = 0;
134199f2cf4bSGreg Kurz     }
134299f2cf4bSGreg Kurz     goto out;
134399f2cf4bSGreg Kurz 
134499f2cf4bSGreg Kurz err:
134599f2cf4bSGreg Kurz     ret = -1;
134699f2cf4bSGreg Kurz err_undo_rename:
134799f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
134899f2cf4bSGreg Kurz out:
134999f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
135099f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
13512289be19SAneesh Kumar K.V     return ret;
13522289be19SAneesh Kumar K.V }
13532289be19SAneesh Kumar K.V 
v9fs_path_init_dirname(V9fsPath * path,const char * str)1354d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1355d2767edeSGreg Kurz {
1356d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1357d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1358d2767edeSGreg Kurz }
1359d2767edeSGreg Kurz 
local_rename(FsContext * ctx,const char * oldpath,const char * newpath)1360d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1361d2767edeSGreg Kurz                         const char *newpath)
1362d2767edeSGreg Kurz {
1363d2767edeSGreg Kurz     int err;
1364d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1365d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1366d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1367d2767edeSGreg Kurz 
1368d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1369d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1370d2767edeSGreg Kurz 
1371d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1372d2767edeSGreg Kurz 
1373d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1374d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1375d2767edeSGreg Kurz     g_free(nname);
1376d2767edeSGreg Kurz     g_free(oname);
1377d2767edeSGreg Kurz 
1378d2767edeSGreg Kurz     return err;
1379d2767edeSGreg Kurz }
1380d2767edeSGreg Kurz 
local_unlinkat(FsContext * ctx,V9fsPath * dir,const char * name,int flags)13812289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
13822289be19SAneesh Kumar K.V                           const char *name, int flags)
13832289be19SAneesh Kumar K.V {
13842289be19SAneesh Kumar K.V     int ret;
1385df4938a6SGreg Kurz     int dirfd;
13862c30dd74SAneesh Kumar K.V 
13877a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
13887a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
13897a95434eSGreg Kurz         errno = EINVAL;
13907a95434eSGreg Kurz         return -1;
13917a95434eSGreg Kurz     }
13927a95434eSGreg Kurz 
1393df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1394df4938a6SGreg Kurz     if (dirfd == -1) {
1395df4938a6SGreg Kurz         return -1;
1396df4938a6SGreg Kurz     }
13972289be19SAneesh Kumar K.V 
1398df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1399df4938a6SGreg Kurz     close_preserve_errno(dirfd);
14002289be19SAneesh Kumar K.V     return ret;
14012289be19SAneesh Kumar K.V }
14029ed3ef26SAneesh Kumar K.V 
14035b7b2f9aSKeno Fischer #ifdef FS_IOC_GETVERSION
local_ioc_getversion(FsContext * ctx,V9fsPath * path,mode_t st_mode,uint64_t * st_gen)1404e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1405e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1406e06a765eSHarsh Prateek Bora {
14070e5fc994SKirill A. Shutemov     int err;
1408cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1409cc720ddbSAneesh Kumar K.V 
1410e06a765eSHarsh Prateek Bora     /*
1411e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1412e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1413e06a765eSHarsh Prateek Bora      */
1414e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
14151a9978a5SKirill A. Shutemov         errno = ENOTTY;
14161a9978a5SKirill A. Shutemov         return -1;
1417e06a765eSHarsh Prateek Bora     }
1418cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1419cc720ddbSAneesh Kumar K.V     if (err < 0) {
1420cc720ddbSAneesh Kumar K.V         return err;
1421e06a765eSHarsh Prateek Bora     }
1422cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1423cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1424e06a765eSHarsh Prateek Bora     return err;
14255b7b2f9aSKeno Fischer }
14260e5fc994SKirill A. Shutemov #endif
1427e06a765eSHarsh Prateek Bora 
local_ioc_getversion_init(FsContext * ctx,LocalData * data,Error ** errp)14285b7b2f9aSKeno Fischer static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp)
14290174fe73SAneesh Kumar K.V {
143000c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
14315b7b2f9aSKeno Fischer     struct statfs stbuf;
14325b7b2f9aSKeno Fischer 
143300c90bd1SGreg Kurz     /*
143428cbbdd2SMichael Tokarev      * use ioc_getversion only if the ioctl is defined
143500c90bd1SGreg Kurz      */
14360e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
14372306271cSKeno Fischer         error_setg_errno(errp, errno,
14382306271cSKeno Fischer                          "failed to stat file system at '%s'", ctx->fs_root);
14395b7b2f9aSKeno Fischer         return -1;
144000c90bd1SGreg Kurz     }
144100c90bd1SGreg Kurz     switch (stbuf.f_type) {
144200c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
144300c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
144400c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
144500c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
144600c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
144700c90bd1SGreg Kurz         break;
144800c90bd1SGreg Kurz     }
144900c90bd1SGreg Kurz #endif
14505b7b2f9aSKeno Fischer     return 0;
14515b7b2f9aSKeno Fischer }
14525b7b2f9aSKeno Fischer 
local_init(FsContext * ctx,Error ** errp)14535b7b2f9aSKeno Fischer static int local_init(FsContext *ctx, Error **errp)
14545b7b2f9aSKeno Fischer {
14555b7b2f9aSKeno Fischer     LocalData *data = g_malloc(sizeof(*data));
14565b7b2f9aSKeno Fischer 
14575b7b2f9aSKeno Fischer     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
14585b7b2f9aSKeno Fischer     if (data->mountfd == -1) {
14595b7b2f9aSKeno Fischer         error_setg_errno(errp, errno, "failed to open '%s'", ctx->fs_root);
14605b7b2f9aSKeno Fischer         goto err;
14615b7b2f9aSKeno Fischer     }
14625b7b2f9aSKeno Fischer 
14635b7b2f9aSKeno Fischer     if (local_ioc_getversion_init(ctx, data, errp) < 0) {
14645b7b2f9aSKeno Fischer         close(data->mountfd);
14655b7b2f9aSKeno Fischer         goto err;
14665b7b2f9aSKeno Fischer     }
1467e06a765eSHarsh Prateek Bora 
14682c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
14692c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
14702c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
14712c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
14722c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
14732c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
14742c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
14752c30dd74SAneesh Kumar K.V         /*
14762c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
14772c30dd74SAneesh Kumar K.V          * remain same.
14782c30dd74SAneesh Kumar K.V          */
14792c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
14802c30dd74SAneesh Kumar K.V     }
1481c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
148200c90bd1SGreg Kurz 
14830e35a378SGreg Kurz     ctx->private = data;
148400c90bd1SGreg Kurz     return 0;
14850e35a378SGreg Kurz 
14860e35a378SGreg Kurz err:
14870e35a378SGreg Kurz     g_free(data);
14880e35a378SGreg Kurz     return -1;
1489e06a765eSHarsh Prateek Bora }
14900e35a378SGreg Kurz 
local_cleanup(FsContext * ctx)14910e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
14920e35a378SGreg Kurz {
14930e35a378SGreg Kurz     LocalData *data = ctx->private;
14940e35a378SGreg Kurz 
1495c0da0cb7SGreg Kurz     if (!data) {
1496c0da0cb7SGreg Kurz         return;
1497c0da0cb7SGreg Kurz     }
1498c0da0cb7SGreg Kurz 
14990e35a378SGreg Kurz     close(data->mountfd);
15000e35a378SGreg Kurz     g_free(data);
15010174fe73SAneesh Kumar K.V }
15020174fe73SAneesh Kumar K.V 
error_append_security_model_hint(Error * const * errp)15034c5ec47eSVladimir Sementsov-Ogievskiy static void error_append_security_model_hint(Error *const *errp)
150491cda4e8SGreg Kurz {
150591cda4e8SGreg Kurz     error_append_hint(errp, "Valid options are: security_model="
150691cda4e8SGreg Kurz                       "[passthrough|mapped-xattr|mapped-file|none]\n");
150791cda4e8SGreg Kurz }
150891cda4e8SGreg Kurz 
local_parse_opts(QemuOpts * opts,FsDriverEntry * fse,Error ** errp)150991cda4e8SGreg Kurz static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
151099519f0aSAneesh Kumar K.V {
151192c45122SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
151299519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
151399519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
15141a6ed33cSAntonios Motakis     const char *multidevs = qemu_opt_get(opts, "multidevs");
151599519f0aSAneesh Kumar K.V 
151699519f0aSAneesh Kumar K.V     if (!sec_model) {
151791cda4e8SGreg Kurz         error_setg(errp, "security_model property not set");
151891cda4e8SGreg Kurz         error_append_security_model_hint(errp);
151999519f0aSAneesh Kumar K.V         return -1;
152099519f0aSAneesh Kumar K.V     }
152199519f0aSAneesh Kumar K.V 
152299519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
152399519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
15242c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
15252c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
152699519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
152799519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
152899519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
15292c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
15302c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
153199519f0aSAneesh Kumar K.V     } else {
153291cda4e8SGreg Kurz         error_setg(errp, "invalid security_model property '%s'", sec_model);
153391cda4e8SGreg Kurz         error_append_security_model_hint(errp);
153499519f0aSAneesh Kumar K.V         return -1;
153599519f0aSAneesh Kumar K.V     }
153699519f0aSAneesh Kumar K.V 
15371a6ed33cSAntonios Motakis     if (multidevs) {
15381a6ed33cSAntonios Motakis         if (!strcmp(multidevs, "remap")) {
15391a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
15401a6ed33cSAntonios Motakis             fse->export_flags |= V9FS_REMAP_INODES;
15411a6ed33cSAntonios Motakis         } else if (!strcmp(multidevs, "forbid")) {
15421a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_REMAP_INODES;
15431a6ed33cSAntonios Motakis             fse->export_flags |= V9FS_FORBID_MULTIDEVS;
15441a6ed33cSAntonios Motakis         } else if (!strcmp(multidevs, "warn")) {
15451a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
15461a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_REMAP_INODES;
15471a6ed33cSAntonios Motakis         } else {
154892c45122SVladimir Sementsov-Ogievskiy             error_setg(errp, "invalid multidevs property '%s'",
15491a6ed33cSAntonios Motakis                        multidevs);
155092c45122SVladimir Sementsov-Ogievskiy             error_append_hint(errp, "Valid options are: multidevs="
15511a6ed33cSAntonios Motakis                               "[remap|forbid|warn]\n");
15521a6ed33cSAntonios Motakis             return -1;
15531a6ed33cSAntonios Motakis         }
1554a2f17bd4SChristian Schoenebeck     } else {
1555a2f17bd4SChristian Schoenebeck         fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
1556a2f17bd4SChristian Schoenebeck         fse->export_flags |= V9FS_REMAP_INODES;
15571a6ed33cSAntonios Motakis     }
15581a6ed33cSAntonios Motakis 
155999519f0aSAneesh Kumar K.V     if (!path) {
156091cda4e8SGreg Kurz         error_setg(errp, "path property not set");
156199519f0aSAneesh Kumar K.V         return -1;
156299519f0aSAneesh Kumar K.V     }
1563b8bbdb88SPradeep Jagadeesh 
156492c45122SVladimir Sementsov-Ogievskiy     if (fsdev_throttle_parse_opts(opts, &fse->fst, errp)) {
156592c45122SVladimir Sementsov-Ogievskiy         error_prepend(errp, "invalid throttle configuration: ");
1566b8bbdb88SPradeep Jagadeesh         return -1;
1567b8bbdb88SPradeep Jagadeesh     }
1568b8bbdb88SPradeep Jagadeesh 
1569b96feb2cSTobias Schramm     if (fse->export_flags & V9FS_SM_MAPPED ||
1570b96feb2cSTobias Schramm         fse->export_flags & V9FS_SM_MAPPED_FILE) {
1571b96feb2cSTobias Schramm         fse->fmode =
1572b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1573b96feb2cSTobias Schramm         fse->dmode =
1574b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1575b96feb2cSTobias Schramm     } else {
1576b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "fmode")) {
157791cda4e8SGreg Kurz             error_setg(errp, "fmode is only valid for mapped security modes");
1578b96feb2cSTobias Schramm             return -1;
1579b96feb2cSTobias Schramm         }
1580b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "dmode")) {
158191cda4e8SGreg Kurz             error_setg(errp, "dmode is only valid for mapped security modes");
1582b96feb2cSTobias Schramm             return -1;
1583b96feb2cSTobias Schramm         }
1584b96feb2cSTobias Schramm     }
1585b96feb2cSTobias Schramm 
158699519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
158799519f0aSAneesh Kumar K.V 
158899519f0aSAneesh Kumar K.V     return 0;
158999519f0aSAneesh Kumar K.V }
159099519f0aSAneesh Kumar K.V 
local_has_valid_file_handle(int fid_type,V9fsFidOpenState * fs)1591f2bb367dSGreg Kurz static bool local_has_valid_file_handle(int fid_type, V9fsFidOpenState *fs)
1592f2bb367dSGreg Kurz {
1593f2bb367dSGreg Kurz     return
1594f2bb367dSGreg Kurz         (fid_type == P9_FID_FILE && fs->fd != -1) ||
1595f2bb367dSGreg Kurz         (fid_type == P9_FID_DIR && fs->dir.stream != NULL);
1596f2bb367dSGreg Kurz }
1597f2bb367dSGreg Kurz 
15989f107513SAnthony Liguori FileOperations local_ops = {
159999519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
16000174fe73SAneesh Kumar K.V     .init  = local_init,
16010e35a378SGreg Kurz     .cleanup = local_cleanup,
1602131dcb25SAnthony Liguori     .lstat = local_lstat,
1603131dcb25SAnthony Liguori     .readlink = local_readlink,
1604131dcb25SAnthony Liguori     .close = local_close,
1605131dcb25SAnthony Liguori     .closedir = local_closedir,
1606a6568fe2SAnthony Liguori     .open = local_open,
1607a6568fe2SAnthony Liguori     .opendir = local_opendir,
1608a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1609a9231555SAnthony Liguori     .telldir = local_telldir,
1610635324e8SGreg Kurz     .readdir = local_readdir,
1611a9231555SAnthony Liguori     .seekdir = local_seekdir,
161256d15a53SSanchit Garg     .preadv = local_preadv,
161356d15a53SSanchit Garg     .pwritev = local_pwritev,
1614c494dd6fSAnthony Liguori     .chmod = local_chmod,
1615c494dd6fSAnthony Liguori     .mknod = local_mknod,
1616c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1617c494dd6fSAnthony Liguori     .fstat = local_fstat,
1618c494dd6fSAnthony Liguori     .open2 = local_open2,
1619c494dd6fSAnthony Liguori     .symlink = local_symlink,
1620c494dd6fSAnthony Liguori     .link = local_link,
16218cf89e00SAnthony Liguori     .truncate = local_truncate,
16228cf89e00SAnthony Liguori     .rename = local_rename,
16238cf89e00SAnthony Liguori     .chown = local_chown,
162474bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
16255bae1900SAnthony Liguori     .remove = local_remove,
16268cf89e00SAnthony Liguori     .fsync = local_fsync,
1627be940c87SM. Mohan Kumar     .statfs = local_statfs,
1628fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1629fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
163010b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
16319ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
16322289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
16332289be19SAneesh Kumar K.V     .renameat  = local_renameat,
16342289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
1635f2bb367dSGreg Kurz     .has_valid_file_handle = local_has_valid_file_handle,
16360c798dd5SGreg Kurz     .ftruncate = local_ftruncate,
1637*371a269fSGreg Kurz     .futimens = local_futimens,
16389f107513SAnthony Liguori };
1639