xref: /qemu/hw/9pfs/9p-local.c (revision b96feb2cb9b2714bffa342b1d4f39d8db71329ba)
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  *
129f107513SAnthony Liguori  */
13873c3213SStefan Weil 
14fbc04127SPeter Maydell #include "qemu/osdep.h"
15ebe74f8bSWei Liu #include "9p.h"
16996a0d76SGreg Kurz #include "9p-local.h"
17267ae092SWei Liu #include "9p-xattr.h"
180e35a378SGreg Kurz #include "9p-util.h"
1969b15212SStefan Weil #include "fsdev/qemu-fsdev.h"   /* local_ops */
20c494dd6fSAnthony Liguori #include <arpa/inet.h>
21131dcb25SAnthony Liguori #include <pwd.h>
22131dcb25SAnthony Liguori #include <grp.h>
23c494dd6fSAnthony Liguori #include <sys/socket.h>
24c494dd6fSAnthony Liguori #include <sys/un.h>
251de7afc9SPaolo Bonzini #include "qemu/xattr.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
2763325b18SGreg Kurz #include "qemu/error-report.h"
282c30dd74SAneesh Kumar K.V #include <libgen.h>
29e06a765eSHarsh Prateek Bora #include <linux/fs.h>
30e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H
31e06a765eSHarsh Prateek Bora #include <linux/magic.h>
32e06a765eSHarsh Prateek Bora #endif
33e06a765eSHarsh Prateek Bora #include <sys/ioctl.h>
34e06a765eSHarsh Prateek Bora 
35e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC
36e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC  0x58465342
37e06a765eSHarsh Prateek Bora #endif
38e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC
39e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53
40e06a765eSHarsh Prateek Bora #endif
41e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC
42e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973
43e06a765eSHarsh Prateek Bora #endif
44e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC
45e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E
46e06a765eSHarsh Prateek Bora #endif
47131dcb25SAnthony Liguori 
480e35a378SGreg Kurz typedef struct {
490e35a378SGreg Kurz     int mountfd;
500e35a378SGreg Kurz } LocalData;
510e35a378SGreg Kurz 
52996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53996a0d76SGreg Kurz                         mode_t mode)
54996a0d76SGreg Kurz {
55996a0d76SGreg Kurz     LocalData *data = fs_ctx->private;
563dbcf273SGreg Kurz     int fd = data->mountfd;
57996a0d76SGreg Kurz 
583dbcf273SGreg Kurz     while (*path && fd != -1) {
593dbcf273SGreg Kurz         const char *c;
603dbcf273SGreg Kurz         int next_fd;
613dbcf273SGreg Kurz         char *head;
623dbcf273SGreg Kurz 
633dbcf273SGreg Kurz         /* Only relative paths without consecutive slashes */
643dbcf273SGreg Kurz         assert(*path != '/');
653dbcf273SGreg Kurz 
663dbcf273SGreg Kurz         head = g_strdup(path);
673dbcf273SGreg Kurz         c = strchrnul(path, '/');
683dbcf273SGreg Kurz         if (*c) {
693dbcf273SGreg Kurz             /* Intermediate path element */
703dbcf273SGreg Kurz             head[c - path] = 0;
713dbcf273SGreg Kurz             path = c + 1;
723dbcf273SGreg Kurz             next_fd = openat_dir(fd, head);
733dbcf273SGreg Kurz         } else {
743dbcf273SGreg Kurz             /* Rightmost path element */
753dbcf273SGreg Kurz             next_fd = openat_file(fd, head, flags, mode);
763dbcf273SGreg Kurz             path = c;
773dbcf273SGreg Kurz         }
783dbcf273SGreg Kurz         g_free(head);
793dbcf273SGreg Kurz         if (fd != data->mountfd) {
803dbcf273SGreg Kurz             close_preserve_errno(fd);
813dbcf273SGreg Kurz         }
823dbcf273SGreg Kurz         fd = next_fd;
83996a0d76SGreg Kurz     }
84996a0d76SGreg Kurz 
853dbcf273SGreg Kurz     assert(fd != data->mountfd);
863dbcf273SGreg Kurz     return fd;
87996a0d76SGreg Kurz }
88996a0d76SGreg Kurz 
89996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
90996a0d76SGreg Kurz {
91996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
92996a0d76SGreg Kurz }
93996a0d76SGreg Kurz 
9499f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
9599f2cf4bSGreg Kurz                                     const char *npath)
9699f2cf4bSGreg Kurz {
9799f2cf4bSGreg Kurz     int serrno = errno;
9899f2cf4bSGreg Kurz     renameat(odirfd, opath, ndirfd, npath);
9999f2cf4bSGreg Kurz     errno = serrno;
10099f2cf4bSGreg Kurz }
10199f2cf4bSGreg Kurz 
102ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
103ad0b46e6SGreg Kurz {
104ad0b46e6SGreg Kurz     int serrno = errno;
105ad0b46e6SGreg Kurz     unlinkat(dirfd, path, flags);
106ad0b46e6SGreg Kurz     errno = serrno;
107ad0b46e6SGreg Kurz }
108ad0b46e6SGreg Kurz 
1092c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
11081ffbf5aSGreg Kurz #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
1112c30dd74SAneesh Kumar K.V 
112f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
1130ceb092eSAneesh Kumar K.V {
1140ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
1150ceb092eSAneesh Kumar K.V     FILE *fp;
116f9aef99bSGreg Kurz     int flags;
1170ceb092eSAneesh Kumar K.V     /*
1180ceb092eSAneesh Kumar K.V      * only supports two modes
1190ceb092eSAneesh Kumar K.V      */
1200ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
121f9aef99bSGreg Kurz         flags = O_RDONLY;
1220ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
123f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
1240ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1250ceb092eSAneesh Kumar K.V     } else {
1260ceb092eSAneesh Kumar K.V         return NULL;
1270ceb092eSAneesh Kumar K.V     }
128f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
1290ceb092eSAneesh Kumar K.V     if (fd == -1) {
1300ceb092eSAneesh Kumar K.V         return NULL;
1310ceb092eSAneesh Kumar K.V     }
1320ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1330ceb092eSAneesh Kumar K.V     if (!fp) {
1340ceb092eSAneesh Kumar K.V         close(fd);
1350ceb092eSAneesh Kumar K.V     }
1360ceb092eSAneesh Kumar K.V     return fp;
1370ceb092eSAneesh Kumar K.V }
1380ceb092eSAneesh Kumar K.V 
1392c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
140f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
1412c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1422c30dd74SAneesh Kumar K.V {
1432c30dd74SAneesh Kumar K.V     FILE *fp;
1442c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
145f9aef99bSGreg Kurz     int map_dirfd;
1462c30dd74SAneesh Kumar K.V 
14781ffbf5aSGreg Kurz     if (strcmp(name, ".")) {
14899f2cf4bSGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
149f9aef99bSGreg Kurz         if (map_dirfd == -1) {
150f9aef99bSGreg Kurz             return;
151f9aef99bSGreg Kurz         }
152f9aef99bSGreg Kurz 
153f9aef99bSGreg Kurz         fp = local_fopenat(map_dirfd, name, "r");
154f9aef99bSGreg Kurz         close_preserve_errno(map_dirfd);
15581ffbf5aSGreg Kurz     } else {
15681ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
15781ffbf5aSGreg Kurz     }
1582c30dd74SAneesh Kumar K.V     if (!fp) {
1592c30dd74SAneesh Kumar K.V         return;
1602c30dd74SAneesh Kumar K.V     }
1612c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1622c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1632c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1642c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1652c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1662c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1672c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1682c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1692c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1702c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1712c30dd74SAneesh Kumar K.V         }
1722c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1732c30dd74SAneesh Kumar K.V     }
1742c30dd74SAneesh Kumar K.V     fclose(fp);
1752c30dd74SAneesh Kumar K.V }
1762c30dd74SAneesh Kumar K.V 
1772289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
178131dcb25SAnthony Liguori {
179f9aef99bSGreg Kurz     int err = -1;
180f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
181f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
182f9aef99bSGreg Kurz     int dirfd;
1832289be19SAneesh Kumar K.V 
184f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
185f9aef99bSGreg Kurz     if (dirfd == -1) {
186f9aef99bSGreg Kurz         goto out;
187f9aef99bSGreg Kurz     }
188f9aef99bSGreg Kurz 
189f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
1901237ad76SVenkateswararao Jujjuri (JV)     if (err) {
1914fa4ce71SChen Gang         goto err_out;
1921237ad76SVenkateswararao Jujjuri (JV)     }
193b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1941237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
1951237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
1961237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
1971237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
1981237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
199f9aef99bSGreg Kurz 
200f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
201f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
202f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
2031237ad76SVenkateswararao Jujjuri (JV)         }
204f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
205f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
206f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
2071237ad76SVenkateswararao Jujjuri (JV)         }
208f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
209f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
210f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
2111237ad76SVenkateswararao Jujjuri (JV)         }
212f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
213f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
214f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
2151237ad76SVenkateswararao Jujjuri (JV)         }
2162c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
217f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
2181237ad76SVenkateswararao Jujjuri (JV)     }
2194fa4ce71SChen Gang 
2204fa4ce71SChen Gang err_out:
221f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
222f9aef99bSGreg Kurz out:
223f9aef99bSGreg Kurz     g_free(name);
224f9aef99bSGreg Kurz     g_free(dirpath);
2251237ad76SVenkateswararao Jujjuri (JV)     return err;
226131dcb25SAnthony Liguori }
227131dcb25SAnthony Liguori 
228e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
229e3187a45SGreg Kurz                                         FsCred *credp)
2302c30dd74SAneesh Kumar K.V {
2312c30dd74SAneesh Kumar K.V     FILE *fp;
232e3187a45SGreg Kurz     int ret;
2332c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2342c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
23581ffbf5aSGreg Kurz     int map_dirfd = -1, map_fd;
23681ffbf5aSGreg Kurz     bool is_root = !strcmp(name, ".");
2372c30dd74SAneesh Kumar K.V 
23881ffbf5aSGreg Kurz     if (is_root) {
23981ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
24081ffbf5aSGreg Kurz         if (!fp) {
24181ffbf5aSGreg Kurz             if (errno == ENOENT) {
24281ffbf5aSGreg Kurz                 goto update_map_file;
24381ffbf5aSGreg Kurz             } else {
24481ffbf5aSGreg Kurz                 return -1;
24581ffbf5aSGreg Kurz             }
24681ffbf5aSGreg Kurz         }
24781ffbf5aSGreg Kurz     } else {
248e3187a45SGreg Kurz         ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
249e3187a45SGreg Kurz         if (ret < 0 && errno != EEXIST) {
250e3187a45SGreg Kurz             return -1;
251e3187a45SGreg Kurz         }
252e3187a45SGreg Kurz 
253e3187a45SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
254e3187a45SGreg Kurz         if (map_dirfd == -1) {
255e3187a45SGreg Kurz             return -1;
256e3187a45SGreg Kurz         }
257e3187a45SGreg Kurz 
258e3187a45SGreg Kurz         fp = local_fopenat(map_dirfd, name, "r");
2592c30dd74SAneesh Kumar K.V         if (!fp) {
260e3187a45SGreg Kurz             if (errno == ENOENT) {
261e3187a45SGreg Kurz                 goto update_map_file;
262e3187a45SGreg Kurz             } else {
263e3187a45SGreg Kurz                 close_preserve_errno(map_dirfd);
264e3187a45SGreg Kurz                 return -1;
265e3187a45SGreg Kurz             }
2662c30dd74SAneesh Kumar K.V         }
26781ffbf5aSGreg Kurz     }
2682c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2692c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2702c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2712c30dd74SAneesh Kumar K.V             uid = atoi(buf + 11);
2722c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2732c30dd74SAneesh Kumar K.V             gid = atoi(buf + 11);
2742c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2752c30dd74SAneesh Kumar K.V             mode = atoi(buf + 12);
2762c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2772c30dd74SAneesh Kumar K.V             rdev = atoi(buf + 12);
2782c30dd74SAneesh Kumar K.V         }
2792c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2802c30dd74SAneesh Kumar K.V     }
2812c30dd74SAneesh Kumar K.V     fclose(fp);
2822c30dd74SAneesh Kumar K.V 
2832c30dd74SAneesh Kumar K.V update_map_file:
28481ffbf5aSGreg Kurz     if (is_root) {
28581ffbf5aSGreg Kurz         fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
28681ffbf5aSGreg Kurz     } else {
287e3187a45SGreg Kurz         fp = local_fopenat(map_dirfd, name, "w");
28881ffbf5aSGreg Kurz         /* We can't go this far with map_dirfd not being a valid file descriptor
28981ffbf5aSGreg Kurz          * but some versions of gcc aren't smart enough to see it.
29081ffbf5aSGreg Kurz          */
29181ffbf5aSGreg Kurz         if (map_dirfd != -1) {
292e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
29381ffbf5aSGreg Kurz         }
29481ffbf5aSGreg Kurz     }
2952c30dd74SAneesh Kumar K.V     if (!fp) {
296e3187a45SGreg Kurz         return -1;
2972c30dd74SAneesh Kumar K.V     }
2982c30dd74SAneesh Kumar K.V 
29981ffbf5aSGreg Kurz     map_fd = fileno(fp);
30081ffbf5aSGreg Kurz     assert(map_fd != -1);
30181ffbf5aSGreg Kurz     ret = fchmod(map_fd, 0600);
30281ffbf5aSGreg Kurz     assert(ret == 0);
30381ffbf5aSGreg Kurz 
3042c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
3052c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
3062c30dd74SAneesh Kumar K.V     }
3072c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
3082c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
3092c30dd74SAneesh Kumar K.V     }
3102c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
3112c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
3122c30dd74SAneesh Kumar K.V     }
3132c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
3142c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
3152c30dd74SAneesh Kumar K.V     }
3162c30dd74SAneesh Kumar K.V 
3172c30dd74SAneesh Kumar K.V     if (uid != -1) {
3182c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
3192c30dd74SAneesh Kumar K.V     }
3202c30dd74SAneesh Kumar K.V     if (gid != -1) {
3212c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
3222c30dd74SAneesh Kumar K.V     }
3232c30dd74SAneesh Kumar K.V     if (mode != -1) {
3242c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
3252c30dd74SAneesh Kumar K.V     }
3262c30dd74SAneesh Kumar K.V     if (rdev != -1) {
3272c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
3282c30dd74SAneesh Kumar K.V     }
3292c30dd74SAneesh Kumar K.V     fclose(fp);
3302c30dd74SAneesh Kumar K.V 
331e3187a45SGreg Kurz     return 0;
332e3187a45SGreg Kurz }
333e3187a45SGreg Kurz 
334e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
335e3187a45SGreg Kurz {
336e3187a45SGreg Kurz     int fd, ret;
337e3187a45SGreg Kurz 
338e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
339e3187a45SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet. As an
340e3187a45SGreg Kurz      * alternative, let's open the file and use fchmod() instead. This
341e3187a45SGreg Kurz      * may fail depending on the permissions of the file, but it is the
342e3187a45SGreg Kurz      * best we can do to avoid TOCTTOU. We first try to open read-only
343e3187a45SGreg Kurz      * in case name points to a directory. If that fails, we try write-only
344e3187a45SGreg Kurz      * in case name doesn't point to a directory.
345e3187a45SGreg Kurz      */
346e3187a45SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY, 0);
347e3187a45SGreg Kurz     if (fd == -1) {
348e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
349e3187a45SGreg Kurz         if (errno == EACCES) {
350e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
351e3187a45SGreg Kurz         }
352e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
353e3187a45SGreg Kurz             errno = EACCES;
354e3187a45SGreg Kurz         }
355e3187a45SGreg Kurz     }
356e3187a45SGreg Kurz     if (fd == -1) {
357e3187a45SGreg Kurz         return -1;
358e3187a45SGreg Kurz     }
359e3187a45SGreg Kurz     ret = fchmod(fd, mode);
360e3187a45SGreg Kurz     close_preserve_errno(fd);
3612c30dd74SAneesh Kumar K.V     return ret;
3622c30dd74SAneesh Kumar K.V }
3632c30dd74SAneesh Kumar K.V 
364e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
365131dcb25SAnthony Liguori {
366758e8e38SVenkateswararao Jujjuri (JV)     int err;
3672289be19SAneesh Kumar K.V 
368758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
369f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
370e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
371e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
372758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
373758e8e38SVenkateswararao Jujjuri (JV)             return err;
374131dcb25SAnthony Liguori         }
375131dcb25SAnthony Liguori     }
376758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
377f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
378e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
379e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
380758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
381758e8e38SVenkateswararao Jujjuri (JV)             return err;
382131dcb25SAnthony Liguori         }
383131dcb25SAnthony Liguori     }
384758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
385f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
386e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
387e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
388758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
389758e8e38SVenkateswararao Jujjuri (JV)             return err;
390131dcb25SAnthony Liguori         }
391131dcb25SAnthony Liguori     }
392758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
393f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
394e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
395e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
396758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
397758e8e38SVenkateswararao Jujjuri (JV)             return err;
398131dcb25SAnthony Liguori         }
399758e8e38SVenkateswararao Jujjuri (JV)     }
400131dcb25SAnthony Liguori     return 0;
401131dcb25SAnthony Liguori }
402131dcb25SAnthony Liguori 
403d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
404d815e721SGreg Kurz                                       const char *name, FsCred *credp)
4054750a96fSVenkateswararao Jujjuri (JV) {
406d815e721SGreg Kurz     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
407b314f6a0SGreg Kurz                  AT_SYMLINK_NOFOLLOW) < 0) {
40812848bfcSAneesh Kumar K.V         /*
40912848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
41012848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
41112848bfcSAneesh Kumar K.V          */
412b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
4134fa4ce71SChen Gang             return -1;
4144750a96fSVenkateswararao Jujjuri (JV)         }
415d815e721SGreg Kurz     }
416d815e721SGreg Kurz 
417d815e721SGreg Kurz     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
418d815e721SGreg Kurz }
4194750a96fSVenkateswararao Jujjuri (JV) 
4202289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
421131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
422131dcb25SAnthony Liguori {
423879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
4242289be19SAneesh Kumar K.V 
4252c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
4262c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
427879c2813SVenkateswararao Jujjuri (JV)         int fd;
428bec1e954SGreg Kurz 
429bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
430879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
431879c2813SVenkateswararao Jujjuri (JV)             return -1;
432879c2813SVenkateswararao Jujjuri (JV)         }
433879c2813SVenkateswararao Jujjuri (JV)         do {
434879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
435879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
436bec1e954SGreg Kurz         close_preserve_errno(fd);
437b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
438b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
439bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
440bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
441bec1e954SGreg Kurz         int dirfd;
442bec1e954SGreg Kurz 
443bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
444bec1e954SGreg Kurz         if (dirfd == -1) {
445bec1e954SGreg Kurz             goto out;
446bec1e954SGreg Kurz         }
447bec1e954SGreg Kurz 
448bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
449bec1e954SGreg Kurz         close_preserve_errno(dirfd);
450bec1e954SGreg Kurz     out:
451bec1e954SGreg Kurz         g_free(name);
452bec1e954SGreg Kurz         g_free(dirpath);
453879c2813SVenkateswararao Jujjuri (JV)     }
454879c2813SVenkateswararao Jujjuri (JV)     return tsize;
455131dcb25SAnthony Liguori }
456131dcb25SAnthony Liguori 
457cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
458131dcb25SAnthony Liguori {
459cc720ddbSAneesh Kumar K.V     return close(fs->fd);
460131dcb25SAnthony Liguori }
461131dcb25SAnthony Liguori 
462cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
463131dcb25SAnthony Liguori {
464f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
465131dcb25SAnthony Liguori }
4669f107513SAnthony Liguori 
467cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
468cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
469a6568fe2SAnthony Liguori {
47021328e1eSGreg Kurz     int fd;
4712289be19SAneesh Kumar K.V 
472996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
47321328e1eSGreg Kurz     if (fd == -1) {
47421328e1eSGreg Kurz         return -1;
47521328e1eSGreg Kurz     }
47621328e1eSGreg Kurz     fs->fd = fd;
477cc720ddbSAneesh Kumar K.V     return fs->fd;
478a6568fe2SAnthony Liguori }
479a6568fe2SAnthony Liguori 
480cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
481cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
482a6568fe2SAnthony Liguori {
483996a0d76SGreg Kurz     int dirfd;
48421328e1eSGreg Kurz     DIR *stream;
4852289be19SAneesh Kumar K.V 
486996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
487996a0d76SGreg Kurz     if (dirfd == -1) {
488cc720ddbSAneesh Kumar K.V         return -1;
489cc720ddbSAneesh Kumar K.V     }
490996a0d76SGreg Kurz 
491996a0d76SGreg Kurz     stream = fdopendir(dirfd);
49221328e1eSGreg Kurz     if (!stream) {
493faab207fSGreg Kurz         close(dirfd);
494cc720ddbSAneesh Kumar K.V         return -1;
495cc720ddbSAneesh Kumar K.V     }
49621328e1eSGreg Kurz     fs->dir.stream = stream;
497cc720ddbSAneesh Kumar K.V     return 0;
498a6568fe2SAnthony Liguori }
499a6568fe2SAnthony Liguori 
500cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
501a9231555SAnthony Liguori {
502f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
503a9231555SAnthony Liguori }
504a9231555SAnthony Liguori 
505cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
506a9231555SAnthony Liguori {
507f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
508a9231555SAnthony Liguori }
509a9231555SAnthony Liguori 
5107a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
5117a95434eSGreg Kurz {
51281ffbf5aSGreg Kurz     return
51381ffbf5aSGreg Kurz         !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
5147a95434eSGreg Kurz }
5157a95434eSGreg Kurz 
516635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
517a9231555SAnthony Liguori {
518635324e8SGreg Kurz     struct dirent *entry;
5192c30dd74SAneesh Kumar K.V 
5202c30dd74SAneesh Kumar K.V again:
521635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
522635324e8SGreg Kurz     if (!entry) {
523635324e8SGreg Kurz         return NULL;
524635324e8SGreg Kurz     }
525635324e8SGreg Kurz 
526840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
527840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
528840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5297a95434eSGreg Kurz         if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
53081ffbf5aSGreg Kurz             /* skip the meta data */
5312c30dd74SAneesh Kumar K.V             goto again;
5322c30dd74SAneesh Kumar K.V         }
533840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
5342c30dd74SAneesh Kumar K.V     }
535635324e8SGreg Kurz 
536635324e8SGreg Kurz     return entry;
537a9231555SAnthony Liguori }
538a9231555SAnthony Liguori 
539cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
540a9231555SAnthony Liguori {
541f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
542a9231555SAnthony Liguori }
543a9231555SAnthony Liguori 
544cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
545cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
54656d15a53SSanchit Garg                             int iovcnt, off_t offset)
547a9231555SAnthony Liguori {
54856d15a53SSanchit Garg #ifdef CONFIG_PREADV
549cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
55056d15a53SSanchit Garg #else
551cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
55256d15a53SSanchit Garg     if (err == -1) {
55356d15a53SSanchit Garg         return err;
55456d15a53SSanchit Garg     } else {
555cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
556a9231555SAnthony Liguori     }
55756d15a53SSanchit Garg #endif
558a9231555SAnthony Liguori }
559a9231555SAnthony Liguori 
560cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
561cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
56256d15a53SSanchit Garg                              int iovcnt, off_t offset)
5638449360cSAnthony Liguori {
5646fe76accSGreg Kurz     ssize_t ret;
56556d15a53SSanchit Garg #ifdef CONFIG_PREADV
566cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
56756d15a53SSanchit Garg #else
568cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
56956d15a53SSanchit Garg     if (err == -1) {
57056d15a53SSanchit Garg         return err;
57156d15a53SSanchit Garg     } else {
572cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
5738449360cSAnthony Liguori     }
57456d15a53SSanchit Garg #endif
575d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
576d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
577d3ab98e6SAneesh Kumar K.V         /*
578d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
579d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
580d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
581d3ab98e6SAneesh Kumar K.V          */
582cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
583d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
584d3ab98e6SAneesh Kumar K.V     }
585d3ab98e6SAneesh Kumar K.V #endif
586d3ab98e6SAneesh Kumar K.V     return ret;
58756d15a53SSanchit Garg }
5888449360cSAnthony Liguori 
5892289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
590c494dd6fSAnthony Liguori {
591e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
592e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
5934fa4ce71SChen Gang     int ret = -1;
594e3187a45SGreg Kurz     int dirfd;
595e3187a45SGreg Kurz 
596e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
597e3187a45SGreg Kurz     if (dirfd == -1) {
598e3187a45SGreg Kurz         goto out;
599e3187a45SGreg Kurz     }
6002289be19SAneesh Kumar K.V 
601b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
602e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
6032c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
604e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
605e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
606e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
607e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
608e95ead32SVenkateswararao Jujjuri (JV)     }
609e3187a45SGreg Kurz     close_preserve_errno(dirfd);
610e3187a45SGreg Kurz 
611e3187a45SGreg Kurz out:
612e3187a45SGreg Kurz     g_free(dirpath);
613e3187a45SGreg Kurz     g_free(name);
6144fa4ce71SChen Gang     return ret;
615c494dd6fSAnthony Liguori }
616c494dd6fSAnthony Liguori 
6172289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
6182289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
619c494dd6fSAnthony Liguori {
6201c293312SVenkateswararao Jujjuri (JV)     int err = -1;
621d815e721SGreg Kurz     int dirfd;
6221c293312SVenkateswararao Jujjuri (JV) 
6237a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6247a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6257a95434eSGreg Kurz         errno = EINVAL;
6267a95434eSGreg Kurz         return -1;
6277a95434eSGreg Kurz     }
6287a95434eSGreg Kurz 
629d815e721SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
630d815e721SGreg Kurz     if (dirfd == -1) {
631d815e721SGreg Kurz         return -1;
632d815e721SGreg Kurz     }
6332289be19SAneesh Kumar K.V 
634d815e721SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
635d815e721SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
636*b96feb2cSTobias Schramm         err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
637d815e721SGreg Kurz         if (err == -1) {
638d815e721SGreg Kurz             goto out;
639d815e721SGreg Kurz         }
640d815e721SGreg Kurz 
641b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
642d815e721SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
643d815e721SGreg Kurz         } else {
644d815e721SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
6451c293312SVenkateswararao Jujjuri (JV)         }
6461c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6471c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6481c293312SVenkateswararao Jujjuri (JV)         }
649d815e721SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
650d815e721SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
651d815e721SGreg Kurz         err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
6522c30dd74SAneesh Kumar K.V         if (err == -1) {
6532c30dd74SAneesh Kumar K.V             goto out;
6542c30dd74SAneesh Kumar K.V         }
655d815e721SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
6562c30dd74SAneesh Kumar K.V         if (err == -1) {
6571c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6581c293312SVenkateswararao Jujjuri (JV)         }
6591c293312SVenkateswararao Jujjuri (JV)     }
6602289be19SAneesh Kumar K.V     goto out;
6611c293312SVenkateswararao Jujjuri (JV) 
6621c293312SVenkateswararao Jujjuri (JV) err_end:
663d815e721SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
6642289be19SAneesh Kumar K.V out:
665d815e721SGreg Kurz     close_preserve_errno(dirfd);
6661c293312SVenkateswararao Jujjuri (JV)     return err;
667c494dd6fSAnthony Liguori }
668c494dd6fSAnthony Liguori 
6692289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
6702289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
671c494dd6fSAnthony Liguori {
67200ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
6733f3a1699SGreg Kurz     int dirfd;
67400ec5c37SVenkateswararao Jujjuri (JV) 
6757a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6767a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6777a95434eSGreg Kurz         errno = EINVAL;
6787a95434eSGreg Kurz         return -1;
6797a95434eSGreg Kurz     }
6807a95434eSGreg Kurz 
6813f3a1699SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
6823f3a1699SGreg Kurz     if (dirfd == -1) {
6833f3a1699SGreg Kurz         return -1;
6843f3a1699SGreg Kurz     }
6852289be19SAneesh Kumar K.V 
6863f3a1699SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
6873f3a1699SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
688*b96feb2cSTobias Schramm         err = mkdirat(dirfd, name, fs_ctx->dmode);
6893f3a1699SGreg Kurz         if (err == -1) {
6903f3a1699SGreg Kurz             goto out;
6913f3a1699SGreg Kurz         }
6923f3a1699SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFDIR;
6933f3a1699SGreg Kurz 
694b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6953f3a1699SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
6963f3a1699SGreg Kurz         } else {
6973f3a1699SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
69800ec5c37SVenkateswararao Jujjuri (JV)         }
69900ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
70000ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
70100ec5c37SVenkateswararao Jujjuri (JV)         }
7023f3a1699SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
7033f3a1699SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
7043f3a1699SGreg Kurz         err = mkdirat(dirfd, name, credp->fc_mode);
7052c30dd74SAneesh Kumar K.V         if (err == -1) {
7062c30dd74SAneesh Kumar K.V             goto out;
7072c30dd74SAneesh Kumar K.V         }
7083f3a1699SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
7092c30dd74SAneesh Kumar K.V         if (err == -1) {
71000ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
71100ec5c37SVenkateswararao Jujjuri (JV)         }
71200ec5c37SVenkateswararao Jujjuri (JV)     }
7132289be19SAneesh Kumar K.V     goto out;
71400ec5c37SVenkateswararao Jujjuri (JV) 
71500ec5c37SVenkateswararao Jujjuri (JV) err_end:
7163f3a1699SGreg Kurz     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
7172289be19SAneesh Kumar K.V out:
7183f3a1699SGreg Kurz     close_preserve_errno(dirfd);
71900ec5c37SVenkateswararao Jujjuri (JV)     return err;
720c494dd6fSAnthony Liguori }
721c494dd6fSAnthony Liguori 
7228b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
723cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
724c494dd6fSAnthony Liguori {
7258b888272SAneesh Kumar K.V     int err, fd;
7268b888272SAneesh Kumar K.V 
7278b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
728f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
7298b888272SAneesh Kumar K.V     } else {
7308b888272SAneesh Kumar K.V         fd = fs->fd;
7318b888272SAneesh Kumar K.V     }
7328b888272SAneesh Kumar K.V 
7338b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
7341237ad76SVenkateswararao Jujjuri (JV)     if (err) {
7351237ad76SVenkateswararao Jujjuri (JV)         return err;
7361237ad76SVenkateswararao Jujjuri (JV)     }
737b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7381237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
7391237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
7401237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
7411237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
7421237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
7431237ad76SVenkateswararao Jujjuri (JV) 
744f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
745f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
7461237ad76SVenkateswararao Jujjuri (JV)         }
747f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
748f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
7491237ad76SVenkateswararao Jujjuri (JV)         }
750f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
751f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
7521237ad76SVenkateswararao Jujjuri (JV)         }
753f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
754f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
7551237ad76SVenkateswararao Jujjuri (JV)         }
7562c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7572c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
7582c30dd74SAneesh Kumar K.V         return -1;
7591237ad76SVenkateswararao Jujjuri (JV)     }
7601237ad76SVenkateswararao Jujjuri (JV)     return err;
761c494dd6fSAnthony Liguori }
762c494dd6fSAnthony Liguori 
7632289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
764cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
765c494dd6fSAnthony Liguori {
7664750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
7674750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
768a565fea5SGreg Kurz     int dirfd;
7694750a96fSVenkateswararao Jujjuri (JV) 
7707a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7717a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7727a95434eSGreg Kurz         errno = EINVAL;
7737a95434eSGreg Kurz         return -1;
7747a95434eSGreg Kurz     }
7757a95434eSGreg Kurz 
7760ceb092eSAneesh Kumar K.V     /*
7770ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
7780ceb092eSAneesh Kumar K.V      */
7790ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
7800ceb092eSAneesh Kumar K.V 
781a565fea5SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
782a565fea5SGreg Kurz     if (dirfd == -1) {
783a565fea5SGreg Kurz         return -1;
784a565fea5SGreg Kurz     }
7852289be19SAneesh Kumar K.V 
7864750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
787a565fea5SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
788a565fea5SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
789*b96feb2cSTobias Schramm         fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
790a565fea5SGreg Kurz         if (fd == -1) {
791a565fea5SGreg Kurz             goto out;
792a565fea5SGreg Kurz         }
793a565fea5SGreg Kurz         credp->fc_mode = credp->fc_mode|S_IFREG;
794b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7954750a96fSVenkateswararao Jujjuri (JV)             /* Set cleint credentials in xattr */
796a565fea5SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
797a565fea5SGreg Kurz         } else {
798a565fea5SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
7994750a96fSVenkateswararao Jujjuri (JV)         }
8002c30dd74SAneesh Kumar K.V         if (err == -1) {
8012c30dd74SAneesh Kumar K.V             goto err_end;
8022c30dd74SAneesh Kumar K.V         }
803b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
804b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
805a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, credp->fc_mode);
8064750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
8072289be19SAneesh Kumar K.V             goto out;
8084750a96fSVenkateswararao Jujjuri (JV)         }
809a565fea5SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
8104750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
8114750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
8124750a96fSVenkateswararao Jujjuri (JV)         }
8134750a96fSVenkateswararao Jujjuri (JV)     }
8142289be19SAneesh Kumar K.V     err = fd;
815cc720ddbSAneesh Kumar K.V     fs->fd = fd;
8162289be19SAneesh Kumar K.V     goto out;
8174750a96fSVenkateswararao Jujjuri (JV) 
8184750a96fSVenkateswararao Jujjuri (JV) err_end:
819a565fea5SGreg Kurz     unlinkat_preserve_errno(dirfd, name,
820a565fea5SGreg Kurz                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
821a565fea5SGreg Kurz     close_preserve_errno(fd);
8222289be19SAneesh Kumar K.V out:
823a565fea5SGreg Kurz     close_preserve_errno(dirfd);
8244750a96fSVenkateswararao Jujjuri (JV)     return err;
825c494dd6fSAnthony Liguori }
826c494dd6fSAnthony Liguori 
827758e8e38SVenkateswararao Jujjuri (JV) 
828879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
8292289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
830c494dd6fSAnthony Liguori {
831879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
83238771613SGreg Kurz     int dirfd;
833879c2813SVenkateswararao Jujjuri (JV) 
8347a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8357a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8367a95434eSGreg Kurz         errno = EINVAL;
8377a95434eSGreg Kurz         return -1;
8387a95434eSGreg Kurz     }
8397a95434eSGreg Kurz 
84038771613SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
84138771613SGreg Kurz     if (dirfd == -1) {
84238771613SGreg Kurz         return -1;
84338771613SGreg Kurz     }
8442289be19SAneesh Kumar K.V 
845879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
84638771613SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
84738771613SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
84838771613SGreg Kurz         int fd;
84938771613SGreg Kurz         ssize_t oldpath_size, write_size;
85038771613SGreg Kurz 
85138771613SGreg Kurz         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
852*b96feb2cSTobias Schramm                          fs_ctx->fmode);
85338771613SGreg Kurz         if (fd == -1) {
85438771613SGreg Kurz             goto out;
85538771613SGreg Kurz         }
85638771613SGreg Kurz         /* Write the oldpath (target) to the file. */
85738771613SGreg Kurz         oldpath_size = strlen(oldpath);
85838771613SGreg Kurz         do {
85938771613SGreg Kurz             write_size = write(fd, (void *)oldpath, oldpath_size);
86038771613SGreg Kurz         } while (write_size == -1 && errno == EINTR);
86138771613SGreg Kurz         close_preserve_errno(fd);
86238771613SGreg Kurz 
86338771613SGreg Kurz         if (write_size != oldpath_size) {
86438771613SGreg Kurz             goto err_end;
86538771613SGreg Kurz         }
86638771613SGreg Kurz         /* Set cleint credentials in symlink's xattr */
86738771613SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFLNK;
86838771613SGreg Kurz 
869b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
87038771613SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
87138771613SGreg Kurz         } else {
87238771613SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
873879c2813SVenkateswararao Jujjuri (JV)         }
874879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
875879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
876879c2813SVenkateswararao Jujjuri (JV)         }
87738771613SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
87838771613SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
87938771613SGreg Kurz         err = symlinkat(oldpath, dirfd, name);
880879c2813SVenkateswararao Jujjuri (JV)         if (err) {
8812289be19SAneesh Kumar K.V             goto out;
882879c2813SVenkateswararao Jujjuri (JV)         }
88338771613SGreg Kurz         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
88438771613SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
885879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
88612848bfcSAneesh Kumar K.V             /*
88712848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
88812848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
88912848bfcSAneesh Kumar K.V              */
890b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
891879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
89238771613SGreg Kurz             } else {
89312848bfcSAneesh Kumar K.V                 err = 0;
894879c2813SVenkateswararao Jujjuri (JV)             }
895879c2813SVenkateswararao Jujjuri (JV)         }
89638771613SGreg Kurz     }
8972289be19SAneesh Kumar K.V     goto out;
898879c2813SVenkateswararao Jujjuri (JV) 
899879c2813SVenkateswararao Jujjuri (JV) err_end:
90038771613SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
9012289be19SAneesh Kumar K.V out:
90238771613SGreg Kurz     close_preserve_errno(dirfd);
903879c2813SVenkateswararao Jujjuri (JV)     return err;
904c494dd6fSAnthony Liguori }
905c494dd6fSAnthony Liguori 
9062289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
9072289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
908c494dd6fSAnthony Liguori {
909ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
910ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
911ad0b46e6SGreg Kurz     int ret = -1;
912ad0b46e6SGreg Kurz     int odirfd, ndirfd;
913c494dd6fSAnthony Liguori 
9147a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
9157a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
9167a95434eSGreg Kurz         errno = EINVAL;
9177a95434eSGreg Kurz         return -1;
9187a95434eSGreg Kurz     }
9197a95434eSGreg Kurz 
920ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
921ad0b46e6SGreg Kurz     if (odirfd == -1) {
9226dd4b1f1SGreg Kurz         goto out;
9236dd4b1f1SGreg Kurz     }
9242289be19SAneesh Kumar K.V 
925ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
926ad0b46e6SGreg Kurz     if (ndirfd == -1) {
927ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
928ad0b46e6SGreg Kurz         goto out;
929ad0b46e6SGreg Kurz     }
930ad0b46e6SGreg Kurz 
931ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
932ad0b46e6SGreg Kurz     if (ret < 0) {
933ad0b46e6SGreg Kurz         goto out_close;
934ad0b46e6SGreg Kurz     }
9352c30dd74SAneesh Kumar K.V 
9362c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
9376dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
9396dd4b1f1SGreg Kurz 
940ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
941ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
942ad0b46e6SGreg Kurz             goto err_undo_link;
9432c30dd74SAneesh Kumar K.V         }
944ad0b46e6SGreg Kurz 
945ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
946ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
947ad0b46e6SGreg Kurz             goto err;
948ad0b46e6SGreg Kurz         }
949ad0b46e6SGreg Kurz 
950ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
951ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
952ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
953ad0b46e6SGreg Kurz             goto err;
954ad0b46e6SGreg Kurz         }
955ad0b46e6SGreg Kurz 
956ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
957ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
958ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
9592c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
960ad0b46e6SGreg Kurz             goto err_undo_link;
9612c30dd74SAneesh Kumar K.V         }
9626dd4b1f1SGreg Kurz 
963ad0b46e6SGreg Kurz         ret = 0;
9642c30dd74SAneesh Kumar K.V     }
965ad0b46e6SGreg Kurz     goto out_close;
966ad0b46e6SGreg Kurz 
967ad0b46e6SGreg Kurz err:
968ad0b46e6SGreg Kurz     ret = -1;
969ad0b46e6SGreg Kurz err_undo_link:
970ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
971ad0b46e6SGreg Kurz out_close:
972ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
973ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
9746dd4b1f1SGreg Kurz out:
975ad0b46e6SGreg Kurz     g_free(oname);
976ad0b46e6SGreg Kurz     g_free(odirpath);
9772289be19SAneesh Kumar K.V     return ret;
978c494dd6fSAnthony Liguori }
979c494dd6fSAnthony Liguori 
9802289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
9818cf89e00SAnthony Liguori {
982ac125d99SGreg Kurz     int fd, ret;
9832289be19SAneesh Kumar K.V 
984ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
985ac125d99SGreg Kurz     if (fd == -1) {
986ac125d99SGreg Kurz         return -1;
987ac125d99SGreg Kurz     }
988ac125d99SGreg Kurz     ret = ftruncate(fd, size);
989ac125d99SGreg Kurz     close_preserve_errno(fd);
9904fa4ce71SChen Gang     return ret;
9918cf89e00SAnthony Liguori }
9928cf89e00SAnthony Liguori 
9932289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
9948cf89e00SAnthony Liguori {
995d369f207SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
996d369f207SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
9974fa4ce71SChen Gang     int ret = -1;
998d369f207SGreg Kurz     int dirfd;
999d369f207SGreg Kurz 
1000d369f207SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1001d369f207SGreg Kurz     if (dirfd == -1) {
1002d369f207SGreg Kurz         goto out;
1003d369f207SGreg Kurz     }
10042289be19SAneesh Kumar K.V 
1005c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
100617b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
100717b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1008d369f207SGreg Kurz         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1009d369f207SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
1010b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1011d369f207SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
10122c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1013d369f207SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1014f7613beeSVenkateswararao Jujjuri (JV)     }
1015d369f207SGreg Kurz 
1016d369f207SGreg Kurz     close_preserve_errno(dirfd);
1017d369f207SGreg Kurz out:
1018d369f207SGreg Kurz     g_free(name);
1019d369f207SGreg Kurz     g_free(dirpath);
10204fa4ce71SChen Gang     return ret;
10218cf89e00SAnthony Liguori }
10228cf89e00SAnthony Liguori 
10232289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
102474bc02b2SM. Mohan Kumar                            const struct timespec *buf)
10258cf89e00SAnthony Liguori {
1026a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1027a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1028a33eda0dSGreg Kurz     int dirfd, ret = -1;
10292289be19SAneesh Kumar K.V 
1030a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1031a33eda0dSGreg Kurz     if (dirfd == -1) {
1032a33eda0dSGreg Kurz         goto out;
1033a33eda0dSGreg Kurz     }
1034a33eda0dSGreg Kurz 
1035a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1036a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1037a33eda0dSGreg Kurz out:
1038a33eda0dSGreg Kurz     g_free(dirpath);
1039a33eda0dSGreg Kurz     g_free(name);
10404fa4ce71SChen Gang     return ret;
10418cf89e00SAnthony Liguori }
10428cf89e00SAnthony Liguori 
1043df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1044df4938a6SGreg Kurz                                  int flags)
10455bae1900SAnthony Liguori {
1046df4938a6SGreg Kurz     int ret = -1;
10472c30dd74SAneesh Kumar K.V 
10482c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1049df4938a6SGreg Kurz         int map_dirfd;
1050df4938a6SGreg Kurz 
10516a87e792SGreg Kurz         /* We need to remove the metadata as well:
10526a87e792SGreg Kurz          * - the metadata directory if we're removing a directory
10536a87e792SGreg Kurz          * - the metadata file in the parent's metadata directory
10546a87e792SGreg Kurz          *
10556a87e792SGreg Kurz          * If any of these are missing (ie, ENOENT) then we're probably
10566a87e792SGreg Kurz          * trying to remove something that wasn't created in mapped-file
10576a87e792SGreg Kurz          * mode. We just ignore the error.
10586a87e792SGreg Kurz          */
1059df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1060df4938a6SGreg Kurz             int fd;
1061df4938a6SGreg Kurz 
1062b003fc0dSGreg Kurz             fd = openat_dir(dirfd, name);
1063df4938a6SGreg Kurz             if (fd == -1) {
10642c30dd74SAneesh Kumar K.V                 goto err_out;
10652c30dd74SAneesh Kumar K.V             }
1066df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1067df4938a6SGreg Kurz             close_preserve_errno(fd);
1068df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
10692c30dd74SAneesh Kumar K.V                 goto err_out;
10702c30dd74SAneesh Kumar K.V             }
10712c30dd74SAneesh Kumar K.V         }
1072df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
10736a87e792SGreg Kurz         if (map_dirfd != -1) {
1074df4938a6SGreg Kurz             ret = unlinkat(map_dirfd, name, 0);
1075df4938a6SGreg Kurz             close_preserve_errno(map_dirfd);
1076df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
10776a87e792SGreg Kurz                 goto err_out;
10786a87e792SGreg Kurz             }
10796a87e792SGreg Kurz         } else if (errno != ENOENT) {
10802c30dd74SAneesh Kumar K.V             goto err_out;
10812c30dd74SAneesh Kumar K.V         }
10822c30dd74SAneesh Kumar K.V     }
10834fa4ce71SChen Gang 
1084df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
10852c30dd74SAneesh Kumar K.V err_out:
1086df4938a6SGreg Kurz     return ret;
1087df4938a6SGreg Kurz }
1088df4938a6SGreg Kurz 
10898cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10908cf89e00SAnthony Liguori {
10918cf89e00SAnthony Liguori     struct stat stbuf;
1092a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1093a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1094a0e640a8SGreg Kurz     int flags = 0;
1095a0e640a8SGreg Kurz     int dirfd;
1096a0e640a8SGreg Kurz     int err = -1;
10978cf89e00SAnthony Liguori 
1098a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1099b7361d46SGreg Kurz     if (dirfd == -1) {
1100a0e640a8SGreg Kurz         goto out;
1101a0e640a8SGreg Kurz     }
1102a0e640a8SGreg Kurz 
1103790db7efSBruce Rogers     if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
11048cf89e00SAnthony Liguori         goto err_out;
11058cf89e00SAnthony Liguori     }
1106a0e640a8SGreg Kurz 
11078cf89e00SAnthony Liguori     if (S_ISDIR(stbuf.st_mode)) {
1108a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
11098cf89e00SAnthony Liguori     }
11105bae1900SAnthony Liguori 
1111a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
11122c30dd74SAneesh Kumar K.V err_out:
1113a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1114a0e640a8SGreg Kurz out:
1115a0e640a8SGreg Kurz     g_free(name);
1116a0e640a8SGreg Kurz     g_free(dirpath);
11172c30dd74SAneesh Kumar K.V     return err;
11185bae1900SAnthony Liguori }
11195bae1900SAnthony Liguori 
11208b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
11218b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
11228cf89e00SAnthony Liguori {
11238b888272SAneesh Kumar K.V     int fd;
11248b888272SAneesh Kumar K.V 
11258b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1126f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
112749594973SVenkateswararao Jujjuri (JV)     } else {
11288b888272SAneesh Kumar K.V         fd = fs->fd;
11298b888272SAneesh Kumar K.V     }
11308b888272SAneesh Kumar K.V 
11318b888272SAneesh Kumar K.V     if (datasync) {
11328b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
11338b888272SAneesh Kumar K.V     } else {
11348b888272SAneesh Kumar K.V         return fsync(fd);
11358cf89e00SAnthony Liguori     }
113649594973SVenkateswararao Jujjuri (JV) }
11378cf89e00SAnthony Liguori 
11382289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1139be940c87SM. Mohan Kumar {
114031e51d1cSGreg Kurz     int fd, ret;
11412289be19SAneesh Kumar K.V 
114231e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
114323da0145SGreg Kurz     if (fd == -1) {
114423da0145SGreg Kurz         return -1;
114523da0145SGreg Kurz     }
114631e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
114731e51d1cSGreg Kurz     close_preserve_errno(fd);
11484fa4ce71SChen Gang     return ret;
1149be940c87SM. Mohan Kumar }
1150be940c87SM. Mohan Kumar 
11512289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1152fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1153fa32ef88SAneesh Kumar K.V {
11542289be19SAneesh Kumar K.V     char *path = fs_path->data;
11552289be19SAneesh Kumar K.V 
1156fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1157fa32ef88SAneesh Kumar K.V }
1158fa32ef88SAneesh Kumar K.V 
11592289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1160fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1161fa32ef88SAneesh Kumar K.V {
11622289be19SAneesh Kumar K.V     char *path = fs_path->data;
11632289be19SAneesh Kumar K.V 
1164fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
116561b6c499SAneesh Kumar K.V }
116661b6c499SAneesh Kumar K.V 
11672289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
116810b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
116910b468bdSAneesh Kumar K.V {
11702289be19SAneesh Kumar K.V     char *path = fs_path->data;
11712289be19SAneesh Kumar K.V 
1172fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
117310b468bdSAneesh Kumar K.V }
117410b468bdSAneesh Kumar K.V 
11752289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11762289be19SAneesh Kumar K.V                               const char *name)
11779ed3ef26SAneesh Kumar K.V {
11782289be19SAneesh Kumar K.V     char *path = fs_path->data;
11792289be19SAneesh Kumar K.V 
1180fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11819ed3ef26SAneesh Kumar K.V }
11829ed3ef26SAneesh Kumar K.V 
11832289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11842289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11852289be19SAneesh Kumar K.V {
11867a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
11877a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
11887a95434eSGreg Kurz         errno = EINVAL;
11897a95434eSGreg Kurz         return -1;
11907a95434eSGreg Kurz     }
11917a95434eSGreg Kurz 
11922289be19SAneesh Kumar K.V     if (dir_path) {
1193f57f5878SGreg Kurz         if (!strcmp(name, ".")) {
1194f57f5878SGreg Kurz             /* "." relative to "foo/bar" is "foo/bar" */
1195f57f5878SGreg Kurz             v9fs_path_copy(target, dir_path);
1196f57f5878SGreg Kurz         } else if (!strcmp(name, "..")) {
1197f57f5878SGreg Kurz             if (!strcmp(dir_path->data, ".")) {
1198f57f5878SGreg Kurz                 /* ".." relative to the root is "." */
1199f57f5878SGreg Kurz                 v9fs_path_sprintf(target, ".");
12009c6b899fSGreg Kurz             } else {
1201f57f5878SGreg Kurz                 char *tmp = g_path_get_dirname(dir_path->data);
1202f57f5878SGreg Kurz                 /* Symbolic links are resolved by the client. We can assume
1203f57f5878SGreg Kurz                  * that ".." relative to "foo/bar" is equivalent to "foo"
12049c6b899fSGreg Kurz                  */
1205f57f5878SGreg Kurz                 v9fs_path_sprintf(target, "%s", tmp);
1206f57f5878SGreg Kurz                 g_free(tmp);
1207f57f5878SGreg Kurz             }
1208f57f5878SGreg Kurz         } else {
1209f57f5878SGreg Kurz             assert(!strchr(name, '/'));
1210f57f5878SGreg Kurz             v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1211f57f5878SGreg Kurz         }
1212f57f5878SGreg Kurz     } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1213f57f5878SGreg Kurz                !strcmp(name, "..")) {
1214f57f5878SGreg Kurz             /* This is the root fid */
1215f57f5878SGreg Kurz         v9fs_path_sprintf(target, ".");
1216f57f5878SGreg Kurz     } else {
1217f57f5878SGreg Kurz         assert(!strchr(name, '/'));
1218f57f5878SGreg Kurz         v9fs_path_sprintf(target, "./%s", name);
12192289be19SAneesh Kumar K.V     }
12202289be19SAneesh Kumar K.V     return 0;
12212289be19SAneesh Kumar K.V }
12222289be19SAneesh Kumar K.V 
12232289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
12242289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
12252289be19SAneesh Kumar K.V                           const char *new_name)
12262289be19SAneesh Kumar K.V {
12272289be19SAneesh Kumar K.V     int ret;
122899f2cf4bSGreg Kurz     int odirfd, ndirfd;
12292289be19SAneesh Kumar K.V 
12307a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12317a95434eSGreg Kurz         (local_is_mapped_file_metadata(ctx, old_name) ||
12327a95434eSGreg Kurz          local_is_mapped_file_metadata(ctx, new_name))) {
12337a95434eSGreg Kurz         errno = EINVAL;
12347a95434eSGreg Kurz         return -1;
12357a95434eSGreg Kurz     }
12367a95434eSGreg Kurz 
123799f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
123899f2cf4bSGreg Kurz     if (odirfd == -1) {
123999f2cf4bSGreg Kurz         return -1;
124099f2cf4bSGreg Kurz     }
12412289be19SAneesh Kumar K.V 
124299f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
124399f2cf4bSGreg Kurz     if (ndirfd == -1) {
124499f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
124599f2cf4bSGreg Kurz         return -1;
124699f2cf4bSGreg Kurz     }
12472289be19SAneesh Kumar K.V 
124899f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
124999f2cf4bSGreg Kurz     if (ret < 0) {
125099f2cf4bSGreg Kurz         goto out;
125199f2cf4bSGreg Kurz     }
125299f2cf4bSGreg Kurz 
125399f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
125499f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
125599f2cf4bSGreg Kurz 
125699f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
125799f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
125899f2cf4bSGreg Kurz             goto err_undo_rename;
125999f2cf4bSGreg Kurz         }
126099f2cf4bSGreg Kurz 
12616dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
126299f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
126399f2cf4bSGreg Kurz             goto err;
126499f2cf4bSGreg Kurz         }
126599f2cf4bSGreg Kurz 
12666dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
126799f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
126899f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
126999f2cf4bSGreg Kurz             goto err;
127099f2cf4bSGreg Kurz         }
127199f2cf4bSGreg Kurz 
127299f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
127399f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
127499f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
127599f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
127699f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
127799f2cf4bSGreg Kurz             goto err_undo_rename;
127899f2cf4bSGreg Kurz         }
127999f2cf4bSGreg Kurz 
128099f2cf4bSGreg Kurz         ret = 0;
128199f2cf4bSGreg Kurz     }
128299f2cf4bSGreg Kurz     goto out;
128399f2cf4bSGreg Kurz 
128499f2cf4bSGreg Kurz err:
128599f2cf4bSGreg Kurz     ret = -1;
128699f2cf4bSGreg Kurz err_undo_rename:
128799f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
128899f2cf4bSGreg Kurz out:
128999f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
129099f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
12912289be19SAneesh Kumar K.V     return ret;
12922289be19SAneesh Kumar K.V }
12932289be19SAneesh Kumar K.V 
1294d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1295d2767edeSGreg Kurz {
1296d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1297d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1298d2767edeSGreg Kurz }
1299d2767edeSGreg Kurz 
1300d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1301d2767edeSGreg Kurz                         const char *newpath)
1302d2767edeSGreg Kurz {
1303d2767edeSGreg Kurz     int err;
1304d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1305d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1306d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1307d2767edeSGreg Kurz 
1308d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1309d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1310d2767edeSGreg Kurz 
1311d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1312d2767edeSGreg Kurz 
1313d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1314d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1315d2767edeSGreg Kurz     g_free(nname);
1316d2767edeSGreg Kurz     g_free(oname);
1317d2767edeSGreg Kurz 
1318d2767edeSGreg Kurz     return err;
1319d2767edeSGreg Kurz }
1320d2767edeSGreg Kurz 
13212289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
13222289be19SAneesh Kumar K.V                           const char *name, int flags)
13232289be19SAneesh Kumar K.V {
13242289be19SAneesh Kumar K.V     int ret;
1325df4938a6SGreg Kurz     int dirfd;
13262c30dd74SAneesh Kumar K.V 
13277a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
13287a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
13297a95434eSGreg Kurz         errno = EINVAL;
13307a95434eSGreg Kurz         return -1;
13317a95434eSGreg Kurz     }
13327a95434eSGreg Kurz 
1333df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1334df4938a6SGreg Kurz     if (dirfd == -1) {
1335df4938a6SGreg Kurz         return -1;
1336df4938a6SGreg Kurz     }
13372289be19SAneesh Kumar K.V 
1338df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1339df4938a6SGreg Kurz     close_preserve_errno(dirfd);
13402289be19SAneesh Kumar K.V     return ret;
13412289be19SAneesh Kumar K.V }
13429ed3ef26SAneesh Kumar K.V 
1343e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1344e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1345e06a765eSHarsh Prateek Bora {
1346ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
13470e5fc994SKirill A. Shutemov     int err;
1348cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1349cc720ddbSAneesh Kumar K.V 
1350e06a765eSHarsh Prateek Bora     /*
1351e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1352e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1353e06a765eSHarsh Prateek Bora      */
1354e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
13551a9978a5SKirill A. Shutemov         errno = ENOTTY;
13561a9978a5SKirill A. Shutemov         return -1;
1357e06a765eSHarsh Prateek Bora     }
1358cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1359cc720ddbSAneesh Kumar K.V     if (err < 0) {
1360cc720ddbSAneesh Kumar K.V         return err;
1361e06a765eSHarsh Prateek Bora     }
1362cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1363cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1364e06a765eSHarsh Prateek Bora     return err;
13650e5fc994SKirill A. Shutemov #else
13660e5fc994SKirill A. Shutemov     errno = ENOTTY;
13670e5fc994SKirill A. Shutemov     return -1;
13680e5fc994SKirill A. Shutemov #endif
1369e06a765eSHarsh Prateek Bora }
1370e06a765eSHarsh Prateek Bora 
13710174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
13720174fe73SAneesh Kumar K.V {
1373e06a765eSHarsh Prateek Bora     struct statfs stbuf;
13740e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13750e35a378SGreg Kurz 
13760e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13770e35a378SGreg Kurz     if (data->mountfd == -1) {
13780e35a378SGreg Kurz         goto err;
13790e35a378SGreg Kurz     }
1380e06a765eSHarsh Prateek Bora 
138100c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
138200c90bd1SGreg Kurz     /*
138300c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
138400c90bd1SGreg Kurz      */
13850e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13860e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13870e35a378SGreg Kurz         goto err;
138800c90bd1SGreg Kurz     }
138900c90bd1SGreg Kurz     switch (stbuf.f_type) {
139000c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
139100c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
139200c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
139300c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
139400c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
139500c90bd1SGreg Kurz         break;
139600c90bd1SGreg Kurz     }
139700c90bd1SGreg Kurz #endif
13980174fe73SAneesh Kumar K.V 
13992c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
14002c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
14012c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
14022c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
14032c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
14042c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
14052c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
14062c30dd74SAneesh Kumar K.V         /*
14072c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
14082c30dd74SAneesh Kumar K.V          * remain same.
14092c30dd74SAneesh Kumar K.V          */
14102c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
14112c30dd74SAneesh Kumar K.V     }
1412c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
141300c90bd1SGreg Kurz 
14140e35a378SGreg Kurz     ctx->private = data;
141500c90bd1SGreg Kurz     return 0;
14160e35a378SGreg Kurz 
14170e35a378SGreg Kurz err:
14180e35a378SGreg Kurz     g_free(data);
14190e35a378SGreg Kurz     return -1;
1420e06a765eSHarsh Prateek Bora }
14210e35a378SGreg Kurz 
14220e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
14230e35a378SGreg Kurz {
14240e35a378SGreg Kurz     LocalData *data = ctx->private;
14250e35a378SGreg Kurz 
14260e35a378SGreg Kurz     close(data->mountfd);
14270e35a378SGreg Kurz     g_free(data);
14280174fe73SAneesh Kumar K.V }
14290174fe73SAneesh Kumar K.V 
143099519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
143199519f0aSAneesh Kumar K.V {
143299519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
143399519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
1434b8bbdb88SPradeep Jagadeesh     Error *err = NULL;
143599519f0aSAneesh Kumar K.V 
143699519f0aSAneesh Kumar K.V     if (!sec_model) {
143763325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
143863325b18SGreg Kurz         error_printf("valid options are:"
143963325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
144099519f0aSAneesh Kumar K.V         return -1;
144199519f0aSAneesh Kumar K.V     }
144299519f0aSAneesh Kumar K.V 
144399519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
144499519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
14452c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
14462c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
144799519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
144899519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
144999519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
14502c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
14512c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
145299519f0aSAneesh Kumar K.V     } else {
145363325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
145463325b18SGreg Kurz         error_printf("valid options are:"
145563325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
145699519f0aSAneesh Kumar K.V         return -1;
145799519f0aSAneesh Kumar K.V     }
145899519f0aSAneesh Kumar K.V 
145999519f0aSAneesh Kumar K.V     if (!path) {
146063325b18SGreg Kurz         error_report("fsdev: No path specified");
146199519f0aSAneesh Kumar K.V         return -1;
146299519f0aSAneesh Kumar K.V     }
1463b8bbdb88SPradeep Jagadeesh 
1464b8bbdb88SPradeep Jagadeesh     fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1465b8bbdb88SPradeep Jagadeesh     if (err) {
1466b8bbdb88SPradeep Jagadeesh         error_reportf_err(err, "Throttle configuration is not valid: ");
1467b8bbdb88SPradeep Jagadeesh         return -1;
1468b8bbdb88SPradeep Jagadeesh     }
1469b8bbdb88SPradeep Jagadeesh 
1470*b96feb2cSTobias Schramm     if (fse->export_flags & V9FS_SM_MAPPED ||
1471*b96feb2cSTobias Schramm         fse->export_flags & V9FS_SM_MAPPED_FILE) {
1472*b96feb2cSTobias Schramm         fse->fmode =
1473*b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1474*b96feb2cSTobias Schramm         fse->dmode =
1475*b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1476*b96feb2cSTobias Schramm     } else {
1477*b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "fmode")) {
1478*b96feb2cSTobias Schramm             error_report("fmode is only valid for mapped 9p modes");
1479*b96feb2cSTobias Schramm             return -1;
1480*b96feb2cSTobias Schramm         }
1481*b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "dmode")) {
1482*b96feb2cSTobias Schramm             error_report("dmode is only valid for mapped 9p modes");
1483*b96feb2cSTobias Schramm             return -1;
1484*b96feb2cSTobias Schramm         }
1485*b96feb2cSTobias Schramm     }
1486*b96feb2cSTobias Schramm 
148799519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
148899519f0aSAneesh Kumar K.V 
148999519f0aSAneesh Kumar K.V     return 0;
149099519f0aSAneesh Kumar K.V }
149199519f0aSAneesh Kumar K.V 
14929f107513SAnthony Liguori FileOperations local_ops = {
149399519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
14940174fe73SAneesh Kumar K.V     .init  = local_init,
14950e35a378SGreg Kurz     .cleanup = local_cleanup,
1496131dcb25SAnthony Liguori     .lstat = local_lstat,
1497131dcb25SAnthony Liguori     .readlink = local_readlink,
1498131dcb25SAnthony Liguori     .close = local_close,
1499131dcb25SAnthony Liguori     .closedir = local_closedir,
1500a6568fe2SAnthony Liguori     .open = local_open,
1501a6568fe2SAnthony Liguori     .opendir = local_opendir,
1502a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1503a9231555SAnthony Liguori     .telldir = local_telldir,
1504635324e8SGreg Kurz     .readdir = local_readdir,
1505a9231555SAnthony Liguori     .seekdir = local_seekdir,
150656d15a53SSanchit Garg     .preadv = local_preadv,
150756d15a53SSanchit Garg     .pwritev = local_pwritev,
1508c494dd6fSAnthony Liguori     .chmod = local_chmod,
1509c494dd6fSAnthony Liguori     .mknod = local_mknod,
1510c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1511c494dd6fSAnthony Liguori     .fstat = local_fstat,
1512c494dd6fSAnthony Liguori     .open2 = local_open2,
1513c494dd6fSAnthony Liguori     .symlink = local_symlink,
1514c494dd6fSAnthony Liguori     .link = local_link,
15158cf89e00SAnthony Liguori     .truncate = local_truncate,
15168cf89e00SAnthony Liguori     .rename = local_rename,
15178cf89e00SAnthony Liguori     .chown = local_chown,
151874bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
15195bae1900SAnthony Liguori     .remove = local_remove,
15208cf89e00SAnthony Liguori     .fsync = local_fsync,
1521be940c87SM. Mohan Kumar     .statfs = local_statfs,
1522fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1523fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
152410b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
15259ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
15262289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
15272289be19SAneesh Kumar K.V     .renameat  = local_renameat,
15282289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
15299f107513SAnthony Liguori };
1530