xref: /openbmc/qemu/hw/9pfs/9p-local.c (revision 6ca60cd7a388a776d72739e5a404e65c19460511)
1f00d4f59SWei Liu /*
2f00d4f59SWei Liu  * 9p Posix callback
3f00d4f59SWei Liu  *
4f00d4f59SWei Liu  * Copyright IBM, Corp. 2010
5f00d4f59SWei Liu  *
6f00d4f59SWei Liu  * Authors:
7f00d4f59SWei Liu  *  Anthony Liguori   <aliguori@us.ibm.com>
8f00d4f59SWei Liu  *
9f00d4f59SWei Liu  * This work is licensed under the terms of the GNU GPL, version 2.  See
10f00d4f59SWei Liu  * the COPYING file in the top-level directory.
11f00d4f59SWei Liu  */
12f00d4f59SWei Liu 
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"
23f00d4f59SWei Liu #include "fsdev/qemu-fsdev.h"   /* local_ops */
24f00d4f59SWei Liu #include <arpa/inet.h>
25f00d4f59SWei Liu #include <pwd.h>
26f00d4f59SWei Liu #include <grp.h>
27f00d4f59SWei Liu #include <sys/socket.h>
28f00d4f59SWei Liu #include <sys/un.h>
29f00d4f59SWei Liu #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"
34f00d4f59SWei Liu #include <libgen.h>
35e0bd743bSKeno Fischer #ifdef CONFIG_LINUX
36f00d4f59SWei Liu #include <linux/fs.h>
37f00d4f59SWei Liu #ifdef CONFIG_LINUX_MAGIC_H
38f00d4f59SWei Liu #include <linux/magic.h>
39f00d4f59SWei Liu #endif
40e0bd743bSKeno Fischer #endif
41f00d4f59SWei Liu #include <sys/ioctl.h>
42f00d4f59SWei Liu 
43f00d4f59SWei Liu #ifndef XFS_SUPER_MAGIC
44f00d4f59SWei Liu #define XFS_SUPER_MAGIC  0x58465342
45f00d4f59SWei Liu #endif
46f00d4f59SWei Liu #ifndef EXT2_SUPER_MAGIC
47f00d4f59SWei Liu #define EXT2_SUPER_MAGIC 0xEF53
48f00d4f59SWei Liu #endif
49f00d4f59SWei Liu #ifndef REISERFS_SUPER_MAGIC
50f00d4f59SWei Liu #define REISERFS_SUPER_MAGIC 0x52654973
51f00d4f59SWei Liu #endif
52f00d4f59SWei Liu #ifndef BTRFS_SUPER_MAGIC
53f00d4f59SWei Liu #define BTRFS_SUPER_MAGIC 0x9123683E
54f00d4f59SWei Liu #endif
55f00d4f59SWei Liu 
560e35a378SGreg Kurz typedef struct {
570e35a378SGreg Kurz     int mountfd;
580e35a378SGreg Kurz } LocalData;
590e35a378SGreg Kurz 
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 
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 
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;
106*6ca60cd7SBin Meng     qemu_renameat(odirfd, opath, ndirfd, npath);
10799f2cf4bSGreg Kurz     errno = serrno;
10899f2cf4bSGreg Kurz }
10999f2cf4bSGreg Kurz 
110ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
111ad0b46e6SGreg Kurz {
112ad0b46e6SGreg Kurz     int serrno = errno;
113*6ca60cd7SBin Meng     qemu_unlinkat(dirfd, path, flags);
114ad0b46e6SGreg Kurz     errno = serrno;
115ad0b46e6SGreg Kurz }
116ad0b46e6SGreg Kurz 
117f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata"
11881ffbf5aSGreg Kurz #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
119f00d4f59SWei Liu 
120f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
121f00d4f59SWei Liu {
122f00d4f59SWei Liu     int fd, o_mode = 0;
123f00d4f59SWei Liu     FILE *fp;
124f9aef99bSGreg Kurz     int flags;
125f00d4f59SWei Liu     /*
126f00d4f59SWei Liu      * only supports two modes
127f00d4f59SWei Liu      */
128f00d4f59SWei Liu     if (mode[0] == 'r') {
129f9aef99bSGreg Kurz         flags = O_RDONLY;
130f00d4f59SWei Liu     } else if (mode[0] == 'w') {
131f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
132f00d4f59SWei Liu         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
133f00d4f59SWei Liu     } else {
134f00d4f59SWei Liu         return NULL;
135f00d4f59SWei Liu     }
136f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
137f00d4f59SWei Liu     if (fd == -1) {
138f00d4f59SWei Liu         return NULL;
139f00d4f59SWei Liu     }
140f00d4f59SWei Liu     fp = fdopen(fd, mode);
141f00d4f59SWei Liu     if (!fp) {
142f00d4f59SWei Liu         close(fd);
143f00d4f59SWei Liu     }
144f00d4f59SWei Liu     return fp;
145f00d4f59SWei Liu }
146f00d4f59SWei Liu 
147f00d4f59SWei Liu #define ATTR_MAX 100
148f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
149f00d4f59SWei Liu                                    struct stat *stbuf)
150f00d4f59SWei Liu {
151f00d4f59SWei Liu     FILE *fp;
152f00d4f59SWei Liu     char buf[ATTR_MAX];
153f9aef99bSGreg Kurz     int map_dirfd;
154f00d4f59SWei Liu 
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     }
166f00d4f59SWei Liu     if (!fp) {
167f00d4f59SWei Liu         return;
168f00d4f59SWei Liu     }
169f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
170f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
171f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
172f00d4f59SWei Liu             stbuf->st_uid = atoi(buf + 11);
173f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
174f00d4f59SWei Liu             stbuf->st_gid = atoi(buf + 11);
175f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
176f00d4f59SWei Liu             stbuf->st_mode = atoi(buf + 12);
177f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
178f00d4f59SWei Liu             stbuf->st_rdev = atoi(buf + 12);
179f00d4f59SWei Liu         }
180f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
181f00d4f59SWei Liu     }
182f00d4f59SWei Liu     fclose(fp);
183f00d4f59SWei Liu }
184f00d4f59SWei Liu 
185f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
186f00d4f59SWei Liu {
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;
191f00d4f59SWei Liu 
192f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
193f9aef99bSGreg Kurz     if (dirfd == -1) {
194f9aef99bSGreg Kurz         goto out;
195f9aef99bSGreg Kurz     }
196f9aef99bSGreg Kurz 
197*6ca60cd7SBin Meng     err = qemu_fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
198f00d4f59SWei Liu     if (err) {
199f00d4f59SWei Liu         goto err_out;
200f00d4f59SWei Liu     }
201f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
202f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
203f00d4f59SWei Liu         uid_t tmp_uid;
204f00d4f59SWei Liu         gid_t tmp_gid;
205f00d4f59SWei Liu         mode_t tmp_mode;
206f00d4f59SWei Liu         dev_t tmp_dev;
207f9aef99bSGreg Kurz 
208f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
209f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
210f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
211f00d4f59SWei Liu         }
212f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
213f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
214f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
215f00d4f59SWei Liu         }
216f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
217f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
218f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
219f00d4f59SWei Liu         }
220f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
221f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
222f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
223f00d4f59SWei Liu         }
224f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
225f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
226f00d4f59SWei Liu     }
227f00d4f59SWei Liu 
228f00d4f59SWei Liu err_out:
229f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
230f9aef99bSGreg Kurz out:
231f9aef99bSGreg Kurz     g_free(name);
232f9aef99bSGreg Kurz     g_free(dirpath);
233f00d4f59SWei Liu     return err;
234f00d4f59SWei Liu }
235f00d4f59SWei Liu 
236e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
237e3187a45SGreg Kurz                                         FsCred *credp)
238f00d4f59SWei Liu {
239f00d4f59SWei Liu     FILE *fp;
240e3187a45SGreg Kurz     int ret;
241f00d4f59SWei Liu     char buf[ATTR_MAX];
242f00d4f59SWei Liu     int uid = -1, gid = -1, mode = -1, rdev = -1;
24381ffbf5aSGreg Kurz     int map_dirfd = -1, map_fd;
24481ffbf5aSGreg Kurz     bool is_root = !strcmp(name, ".");
245f00d4f59SWei Liu 
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 {
256*6ca60cd7SBin 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");
267f00d4f59SWei Liu         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             }
274f00d4f59SWei Liu         }
27581ffbf5aSGreg Kurz     }
276f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
277f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
278f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
279f00d4f59SWei Liu             uid = atoi(buf + 11);
280f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
281f00d4f59SWei Liu             gid = atoi(buf + 11);
282f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
283f00d4f59SWei Liu             mode = atoi(buf + 12);
284f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
285f00d4f59SWei Liu             rdev = atoi(buf + 12);
286f00d4f59SWei Liu         }
287f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
288f00d4f59SWei Liu     }
289f00d4f59SWei Liu     fclose(fp);
290f00d4f59SWei Liu 
291f00d4f59SWei Liu 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     }
303f00d4f59SWei Liu     if (!fp) {
304e3187a45SGreg Kurz         return -1;
305f00d4f59SWei Liu     }
306f00d4f59SWei Liu 
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 
312f00d4f59SWei Liu     if (credp->fc_uid != -1) {
313f00d4f59SWei Liu         uid = credp->fc_uid;
314f00d4f59SWei Liu     }
315f00d4f59SWei Liu     if (credp->fc_gid != -1) {
316f00d4f59SWei Liu         gid = credp->fc_gid;
317f00d4f59SWei Liu     }
318230f1b31SKeno Fischer     if (credp->fc_mode != (mode_t)-1) {
319f00d4f59SWei Liu         mode = credp->fc_mode;
320f00d4f59SWei Liu     }
321f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
322f00d4f59SWei Liu         rdev = credp->fc_rdev;
323f00d4f59SWei Liu     }
324f00d4f59SWei Liu 
325f00d4f59SWei Liu     if (uid != -1) {
326f00d4f59SWei Liu         fprintf(fp, "virtfs.uid=%d\n", uid);
327f00d4f59SWei Liu     }
328f00d4f59SWei Liu     if (gid != -1) {
329f00d4f59SWei Liu         fprintf(fp, "virtfs.gid=%d\n", gid);
330f00d4f59SWei Liu     }
331f00d4f59SWei Liu     if (mode != -1) {
332f00d4f59SWei Liu         fprintf(fp, "virtfs.mode=%d\n", mode);
333f00d4f59SWei Liu     }
334f00d4f59SWei Liu     if (rdev != -1) {
335f00d4f59SWei Liu         fprintf(fp, "virtfs.rdev=%d\n", rdev);
336f00d4f59SWei Liu     }
337f00d4f59SWei Liu     fclose(fp);
338f00d4f59SWei Liu 
339e3187a45SGreg Kurz     return 0;
340e3187a45SGreg Kurz }
341e3187a45SGreg Kurz 
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. */
352*6ca60cd7SBin 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);
401f00d4f59SWei Liu     return ret;
402f00d4f59SWei Liu }
403f00d4f59SWei Liu 
404e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
405f00d4f59SWei Liu {
406f00d4f59SWei Liu     int err;
407f00d4f59SWei Liu 
408f00d4f59SWei Liu     if (credp->fc_uid != -1) {
409f00d4f59SWei Liu         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);
412f00d4f59SWei Liu         if (err) {
413f00d4f59SWei Liu             return err;
414f00d4f59SWei Liu         }
415f00d4f59SWei Liu     }
416f00d4f59SWei Liu     if (credp->fc_gid != -1) {
417f00d4f59SWei Liu         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);
420f00d4f59SWei Liu         if (err) {
421f00d4f59SWei Liu             return err;
422f00d4f59SWei Liu         }
423f00d4f59SWei Liu     }
424230f1b31SKeno Fischer     if (credp->fc_mode != (mode_t)-1) {
425f00d4f59SWei Liu         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);
428f00d4f59SWei Liu         if (err) {
429f00d4f59SWei Liu             return err;
430f00d4f59SWei Liu         }
431f00d4f59SWei Liu     }
432f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
433f00d4f59SWei Liu         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);
436f00d4f59SWei Liu         if (err) {
437f00d4f59SWei Liu             return err;
438f00d4f59SWei Liu         }
439f00d4f59SWei Liu     }
440f00d4f59SWei Liu     return 0;
441f00d4f59SWei Liu }
442f00d4f59SWei Liu 
443d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
444d815e721SGreg Kurz                                       const char *name, FsCred *credp)
445f00d4f59SWei Liu {
446d815e721SGreg Kurz     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
447b314f6a0SGreg Kurz                  AT_SYMLINK_NOFOLLOW) < 0) {
448f00d4f59SWei Liu         /*
449f00d4f59SWei Liu          * If we fail to change ownership and if we are
450f00d4f59SWei Liu          * using security model none. Ignore the error
451f00d4f59SWei Liu          */
452f00d4f59SWei Liu         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
453f00d4f59SWei Liu             return -1;
454f00d4f59SWei Liu         }
455d815e721SGreg Kurz     }
456d815e721SGreg Kurz 
457d815e721SGreg Kurz     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
458d815e721SGreg Kurz }
459f00d4f59SWei Liu 
460f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
461f00d4f59SWei Liu                               char *buf, size_t bufsz)
462f00d4f59SWei Liu {
463f00d4f59SWei Liu     ssize_t tsize = -1;
464f00d4f59SWei Liu 
465f00d4f59SWei Liu     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
466f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
467f00d4f59SWei Liu         int fd;
468bec1e954SGreg Kurz 
469bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
470f00d4f59SWei Liu         if (fd == -1) {
471f00d4f59SWei Liu             return -1;
472f00d4f59SWei Liu         }
473f00d4f59SWei Liu         do {
474f00d4f59SWei Liu             tsize = read(fd, (void *)buf, bufsz);
475f00d4f59SWei Liu         } while (tsize == -1 && errno == EINTR);
476bec1e954SGreg Kurz         close_preserve_errno(fd);
477f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
478f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
479bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
480bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
481bec1e954SGreg Kurz         int dirfd;
482bec1e954SGreg Kurz 
483bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
484bec1e954SGreg Kurz         if (dirfd == -1) {
485bec1e954SGreg Kurz             goto out;
486bec1e954SGreg Kurz         }
487bec1e954SGreg Kurz 
488bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
489bec1e954SGreg Kurz         close_preserve_errno(dirfd);
490bec1e954SGreg Kurz     out:
491bec1e954SGreg Kurz         g_free(name);
492bec1e954SGreg Kurz         g_free(dirpath);
493f00d4f59SWei Liu     }
494f00d4f59SWei Liu     return tsize;
495f00d4f59SWei Liu }
496f00d4f59SWei Liu 
497f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
498f00d4f59SWei Liu {
499f00d4f59SWei Liu     return close(fs->fd);
500f00d4f59SWei Liu }
501f00d4f59SWei Liu 
502f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
503f00d4f59SWei Liu {
504f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
505f00d4f59SWei Liu }
506f00d4f59SWei Liu 
507f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path,
508f00d4f59SWei Liu                       int flags, V9fsFidOpenState *fs)
509f00d4f59SWei Liu {
51021328e1eSGreg Kurz     int fd;
511f00d4f59SWei Liu 
512996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
51321328e1eSGreg Kurz     if (fd == -1) {
51421328e1eSGreg Kurz         return -1;
51521328e1eSGreg Kurz     }
51621328e1eSGreg Kurz     fs->fd = fd;
517f00d4f59SWei Liu     return fs->fd;
518f00d4f59SWei Liu }
519f00d4f59SWei Liu 
520f00d4f59SWei Liu static int local_opendir(FsContext *ctx,
521f00d4f59SWei Liu                          V9fsPath *fs_path, V9fsFidOpenState *fs)
522f00d4f59SWei Liu {
523996a0d76SGreg Kurz     int dirfd;
52421328e1eSGreg Kurz     DIR *stream;
525f00d4f59SWei Liu 
526996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
527996a0d76SGreg Kurz     if (dirfd == -1) {
528f00d4f59SWei Liu         return -1;
529f00d4f59SWei Liu     }
530996a0d76SGreg Kurz 
531996a0d76SGreg Kurz     stream = fdopendir(dirfd);
53221328e1eSGreg Kurz     if (!stream) {
533faab207fSGreg Kurz         close(dirfd);
534f00d4f59SWei Liu         return -1;
535f00d4f59SWei Liu     }
53621328e1eSGreg Kurz     fs->dir.stream = stream;
537f00d4f59SWei Liu     return 0;
538f00d4f59SWei Liu }
539f00d4f59SWei Liu 
540f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
541f00d4f59SWei Liu {
542f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
543f00d4f59SWei Liu }
544f00d4f59SWei Liu 
545f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
546f00d4f59SWei Liu {
547f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
548f00d4f59SWei Liu }
549f00d4f59SWei Liu 
5507a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
5517a95434eSGreg Kurz {
55281ffbf5aSGreg Kurz     return
55381ffbf5aSGreg Kurz         !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
5547a95434eSGreg Kurz }
5557a95434eSGreg Kurz 
556635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
557f00d4f59SWei Liu {
558635324e8SGreg Kurz     struct dirent *entry;
559f00d4f59SWei Liu 
560f00d4f59SWei Liu again:
561635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
562635324e8SGreg Kurz     if (!entry) {
563635324e8SGreg Kurz         return NULL;
564635324e8SGreg Kurz     }
5656b3b279bSKeno Fischer #ifdef CONFIG_DARWIN
5666b3b279bSKeno Fischer     int off;
5676b3b279bSKeno Fischer     off = telldir(fs->dir.stream);
5686b3b279bSKeno Fischer     /* If telldir fails, fail the entire readdir call */
5696b3b279bSKeno Fischer     if (off < 0) {
5706b3b279bSKeno Fischer         return NULL;
5716b3b279bSKeno Fischer     }
5726b3b279bSKeno Fischer     entry->d_seekoff = off;
5736b3b279bSKeno Fischer #endif
574635324e8SGreg Kurz 
575f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED) {
576f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
577f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5787a95434eSGreg Kurz         if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
57981ffbf5aSGreg Kurz             /* skip the meta data */
580f00d4f59SWei Liu             goto again;
581f00d4f59SWei Liu         }
582f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
583f00d4f59SWei Liu     }
584635324e8SGreg Kurz 
585635324e8SGreg Kurz     return entry;
586f00d4f59SWei Liu }
587f00d4f59SWei Liu 
588f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
589f00d4f59SWei Liu {
590f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
591f00d4f59SWei Liu }
592f00d4f59SWei Liu 
593f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
594f00d4f59SWei Liu                             const struct iovec *iov,
595f00d4f59SWei Liu                             int iovcnt, off_t offset)
596f00d4f59SWei Liu {
597f00d4f59SWei Liu #ifdef CONFIG_PREADV
598f00d4f59SWei Liu     return preadv(fs->fd, iov, iovcnt, offset);
599f00d4f59SWei Liu #else
600f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
601f00d4f59SWei Liu     if (err == -1) {
602f00d4f59SWei Liu         return err;
603f00d4f59SWei Liu     } else {
604f00d4f59SWei Liu         return readv(fs->fd, iov, iovcnt);
605f00d4f59SWei Liu     }
606f00d4f59SWei Liu #endif
607f00d4f59SWei Liu }
608f00d4f59SWei Liu 
609f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
610f00d4f59SWei Liu                              const struct iovec *iov,
611f00d4f59SWei Liu                              int iovcnt, off_t offset)
612f00d4f59SWei Liu {
6136fe76accSGreg Kurz     ssize_t ret;
614f00d4f59SWei Liu #ifdef CONFIG_PREADV
615f00d4f59SWei Liu     ret = pwritev(fs->fd, iov, iovcnt, offset);
616f00d4f59SWei Liu #else
617f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
618f00d4f59SWei Liu     if (err == -1) {
619f00d4f59SWei Liu         return err;
620f00d4f59SWei Liu     } else {
621f00d4f59SWei Liu         ret = writev(fs->fd, iov, iovcnt);
622f00d4f59SWei Liu     }
623f00d4f59SWei Liu #endif
624f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE
625f00d4f59SWei Liu     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
626f00d4f59SWei Liu         /*
627f00d4f59SWei Liu          * Initiate a writeback. This is not a data integrity sync.
628f00d4f59SWei Liu          * We want to ensure that we don't leave dirty pages in the cache
629f00d4f59SWei Liu          * after write when writeout=immediate is sepcified.
630f00d4f59SWei Liu          */
631f00d4f59SWei Liu         sync_file_range(fs->fd, offset, ret,
632f00d4f59SWei Liu                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
633f00d4f59SWei Liu     }
634f00d4f59SWei Liu #endif
635f00d4f59SWei Liu     return ret;
636f00d4f59SWei Liu }
637f00d4f59SWei Liu 
638f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
639f00d4f59SWei Liu {
640e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
641e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
642f00d4f59SWei Liu     int ret = -1;
643e3187a45SGreg Kurz     int dirfd;
644e3187a45SGreg Kurz 
645e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
646e3187a45SGreg Kurz     if (dirfd == -1) {
647e3187a45SGreg Kurz         goto out;
648e3187a45SGreg Kurz     }
649f00d4f59SWei Liu 
650f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
651e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
652f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
653e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
654e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
655e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
656e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
657f00d4f59SWei Liu     }
658e3187a45SGreg Kurz     close_preserve_errno(dirfd);
659e3187a45SGreg Kurz 
660e3187a45SGreg Kurz out:
661e3187a45SGreg Kurz     g_free(dirpath);
662e3187a45SGreg Kurz     g_free(name);
663f00d4f59SWei Liu     return ret;
664f00d4f59SWei Liu }
665f00d4f59SWei Liu 
666f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
667f00d4f59SWei Liu                        const char *name, FsCred *credp)
668f00d4f59SWei Liu {
669f00d4f59SWei Liu     int err = -1;
670d815e721SGreg Kurz     int dirfd;
671f00d4f59SWei Liu 
6727a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6737a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6747a95434eSGreg Kurz         errno = EINVAL;
6757a95434eSGreg Kurz         return -1;
6767a95434eSGreg Kurz     }
6777a95434eSGreg Kurz 
678d815e721SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
679d815e721SGreg Kurz     if (dirfd == -1) {
680d815e721SGreg Kurz         return -1;
681d815e721SGreg Kurz     }
682f00d4f59SWei Liu 
683d815e721SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
684d815e721SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
685029ed1bdSKeno Fischer         err = qemu_mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
686d815e721SGreg Kurz         if (err == -1) {
687d815e721SGreg Kurz             goto out;
688d815e721SGreg Kurz         }
689d815e721SGreg Kurz 
690f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
691d815e721SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
692d815e721SGreg Kurz         } else {
693d815e721SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
694f00d4f59SWei Liu         }
695f00d4f59SWei Liu         if (err == -1) {
696f00d4f59SWei Liu             goto err_end;
697f00d4f59SWei Liu         }
698d815e721SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
699d815e721SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
700029ed1bdSKeno Fischer         err = qemu_mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
701f00d4f59SWei Liu         if (err == -1) {
702f00d4f59SWei Liu             goto out;
703f00d4f59SWei Liu         }
704d815e721SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
705f00d4f59SWei Liu         if (err == -1) {
706f00d4f59SWei Liu             goto err_end;
707f00d4f59SWei Liu         }
708f00d4f59SWei Liu     }
709f00d4f59SWei Liu     goto out;
710f00d4f59SWei Liu 
711f00d4f59SWei Liu err_end:
712d815e721SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
713f00d4f59SWei Liu out:
714d815e721SGreg Kurz     close_preserve_errno(dirfd);
715f00d4f59SWei Liu     return err;
716f00d4f59SWei Liu }
717f00d4f59SWei Liu 
718f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
719f00d4f59SWei Liu                        const char *name, FsCred *credp)
720f00d4f59SWei Liu {
721f00d4f59SWei Liu     int err = -1;
7223f3a1699SGreg Kurz     int dirfd;
723f00d4f59SWei Liu 
7247a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7257a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7267a95434eSGreg Kurz         errno = EINVAL;
7277a95434eSGreg Kurz         return -1;
7287a95434eSGreg Kurz     }
7297a95434eSGreg Kurz 
7303f3a1699SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
7313f3a1699SGreg Kurz     if (dirfd == -1) {
7323f3a1699SGreg Kurz         return -1;
7333f3a1699SGreg Kurz     }
734f00d4f59SWei Liu 
7353f3a1699SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
7363f3a1699SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
737*6ca60cd7SBin Meng         err = qemu_mkdirat(dirfd, name, fs_ctx->dmode);
7383f3a1699SGreg Kurz         if (err == -1) {
7393f3a1699SGreg Kurz             goto out;
7403f3a1699SGreg Kurz         }
7413f3a1699SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFDIR;
7423f3a1699SGreg Kurz 
743f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7443f3a1699SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
7453f3a1699SGreg Kurz         } else {
7463f3a1699SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
747f00d4f59SWei Liu         }
748f00d4f59SWei Liu         if (err == -1) {
749f00d4f59SWei Liu             goto err_end;
750f00d4f59SWei Liu         }
7513f3a1699SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
7523f3a1699SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
753*6ca60cd7SBin Meng         err = qemu_mkdirat(dirfd, name, credp->fc_mode);
754f00d4f59SWei Liu         if (err == -1) {
755f00d4f59SWei Liu             goto out;
756f00d4f59SWei Liu         }
7573f3a1699SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
758f00d4f59SWei Liu         if (err == -1) {
759f00d4f59SWei Liu             goto err_end;
760f00d4f59SWei Liu         }
761f00d4f59SWei Liu     }
762f00d4f59SWei Liu     goto out;
763f00d4f59SWei Liu 
764f00d4f59SWei Liu err_end:
7653f3a1699SGreg Kurz     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
766f00d4f59SWei Liu out:
7673f3a1699SGreg Kurz     close_preserve_errno(dirfd);
768f00d4f59SWei Liu     return err;
769f00d4f59SWei Liu }
770f00d4f59SWei Liu 
771f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type,
772f00d4f59SWei Liu                        V9fsFidOpenState *fs, struct stat *stbuf)
773f00d4f59SWei Liu {
774f00d4f59SWei Liu     int err, fd;
775f00d4f59SWei Liu 
776f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
777f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
778f00d4f59SWei Liu     } else {
779f00d4f59SWei Liu         fd = fs->fd;
780f00d4f59SWei Liu     }
781f00d4f59SWei Liu 
782f00d4f59SWei Liu     err = fstat(fd, stbuf);
783f00d4f59SWei Liu     if (err) {
784f00d4f59SWei Liu         return err;
785f00d4f59SWei Liu     }
786f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
787f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
788f00d4f59SWei Liu         uid_t tmp_uid;
789f00d4f59SWei Liu         gid_t tmp_gid;
790f00d4f59SWei Liu         mode_t tmp_mode;
791f00d4f59SWei Liu         dev_t tmp_dev;
792f00d4f59SWei Liu 
793b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.uid",
794b5989326SKeno Fischer                            &tmp_uid, sizeof(uid_t)) > 0) {
795f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
796f00d4f59SWei Liu         }
797b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.gid",
798b5989326SKeno Fischer                            &tmp_gid, sizeof(gid_t)) > 0) {
799f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
800f00d4f59SWei Liu         }
801b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.mode",
802b5989326SKeno Fischer                            &tmp_mode, sizeof(mode_t)) > 0) {
803f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
804f00d4f59SWei Liu         }
805b5989326SKeno Fischer         if (qemu_fgetxattr(fd, "user.virtfs.rdev",
806b5989326SKeno Fischer                            &tmp_dev, sizeof(dev_t)) > 0) {
807f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
808f00d4f59SWei Liu         }
809f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
810f00d4f59SWei Liu         errno = EOPNOTSUPP;
811f00d4f59SWei Liu         return -1;
812f00d4f59SWei Liu     }
813f00d4f59SWei Liu     return err;
814f00d4f59SWei Liu }
815f00d4f59SWei Liu 
816f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
817f00d4f59SWei Liu                        int flags, FsCred *credp, V9fsFidOpenState *fs)
818f00d4f59SWei Liu {
819f00d4f59SWei Liu     int fd = -1;
820f00d4f59SWei Liu     int err = -1;
821a565fea5SGreg Kurz     int dirfd;
822f00d4f59SWei Liu 
8237a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8247a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8257a95434eSGreg Kurz         errno = EINVAL;
8267a95434eSGreg Kurz         return -1;
8277a95434eSGreg Kurz     }
8287a95434eSGreg Kurz 
829f00d4f59SWei Liu     /*
830f00d4f59SWei Liu      * Mark all the open to not follow symlinks
831f00d4f59SWei Liu      */
832f00d4f59SWei Liu     flags |= O_NOFOLLOW;
833f00d4f59SWei Liu 
834a565fea5SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
835a565fea5SGreg Kurz     if (dirfd == -1) {
836a565fea5SGreg Kurz         return -1;
837a565fea5SGreg Kurz     }
838f00d4f59SWei Liu 
839f00d4f59SWei Liu     /* Determine the security model */
840a565fea5SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
841a565fea5SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
842b96feb2cSTobias Schramm         fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
843a565fea5SGreg Kurz         if (fd == -1) {
844a565fea5SGreg Kurz             goto out;
845a565fea5SGreg Kurz         }
846a565fea5SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFREG;
847f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
848f00d4f59SWei Liu             /* Set cleint credentials in xattr */
849a565fea5SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
850a565fea5SGreg Kurz         } else {
851a565fea5SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
852f00d4f59SWei Liu         }
853f00d4f59SWei Liu         if (err == -1) {
854f00d4f59SWei Liu             goto err_end;
855f00d4f59SWei Liu         }
856f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
857f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
858a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, credp->fc_mode);
859f00d4f59SWei Liu         if (fd == -1) {
860f00d4f59SWei Liu             goto out;
861f00d4f59SWei Liu         }
862a565fea5SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
863f00d4f59SWei Liu         if (err == -1) {
864f00d4f59SWei Liu             goto err_end;
865f00d4f59SWei Liu         }
866f00d4f59SWei Liu     }
867f00d4f59SWei Liu     err = fd;
868f00d4f59SWei Liu     fs->fd = fd;
869f00d4f59SWei Liu     goto out;
870f00d4f59SWei Liu 
871f00d4f59SWei Liu err_end:
872a565fea5SGreg Kurz     unlinkat_preserve_errno(dirfd, name,
873a565fea5SGreg Kurz                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
874a565fea5SGreg Kurz     close_preserve_errno(fd);
875f00d4f59SWei Liu out:
876a565fea5SGreg Kurz     close_preserve_errno(dirfd);
877f00d4f59SWei Liu     return err;
878f00d4f59SWei Liu }
879f00d4f59SWei Liu 
880f00d4f59SWei Liu 
881f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath,
882f00d4f59SWei Liu                          V9fsPath *dir_path, const char *name, FsCred *credp)
883f00d4f59SWei Liu {
884f00d4f59SWei Liu     int err = -1;
88538771613SGreg Kurz     int dirfd;
886f00d4f59SWei Liu 
8877a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8887a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8897a95434eSGreg Kurz         errno = EINVAL;
8907a95434eSGreg Kurz         return -1;
8917a95434eSGreg Kurz     }
8927a95434eSGreg Kurz 
89338771613SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
89438771613SGreg Kurz     if (dirfd == -1) {
89538771613SGreg Kurz         return -1;
89638771613SGreg Kurz     }
897f00d4f59SWei Liu 
898f00d4f59SWei Liu     /* Determine the security model */
89938771613SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
90038771613SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
90138771613SGreg Kurz         int fd;
90238771613SGreg Kurz         ssize_t oldpath_size, write_size;
90338771613SGreg Kurz 
90438771613SGreg Kurz         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
905b96feb2cSTobias Schramm                          fs_ctx->fmode);
90638771613SGreg Kurz         if (fd == -1) {
90738771613SGreg Kurz             goto out;
90838771613SGreg Kurz         }
90938771613SGreg Kurz         /* Write the oldpath (target) to the file. */
91038771613SGreg Kurz         oldpath_size = strlen(oldpath);
91138771613SGreg Kurz         do {
91238771613SGreg Kurz             write_size = write(fd, (void *)oldpath, oldpath_size);
91338771613SGreg Kurz         } while (write_size == -1 && errno == EINTR);
91438771613SGreg Kurz         close_preserve_errno(fd);
91538771613SGreg Kurz 
91638771613SGreg Kurz         if (write_size != oldpath_size) {
91738771613SGreg Kurz             goto err_end;
91838771613SGreg Kurz         }
91938771613SGreg Kurz         /* Set cleint credentials in symlink's xattr */
92038771613SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFLNK;
92138771613SGreg Kurz 
922f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
92338771613SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
92438771613SGreg Kurz         } else {
92538771613SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
926f00d4f59SWei Liu         }
927f00d4f59SWei Liu         if (err == -1) {
928f00d4f59SWei Liu             goto err_end;
929f00d4f59SWei Liu         }
93038771613SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
93138771613SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
93238771613SGreg Kurz         err = symlinkat(oldpath, dirfd, name);
933f00d4f59SWei Liu         if (err) {
934f00d4f59SWei Liu             goto out;
935f00d4f59SWei Liu         }
93638771613SGreg Kurz         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
93738771613SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
938f00d4f59SWei Liu         if (err == -1) {
939f00d4f59SWei Liu             /*
940f00d4f59SWei Liu              * If we fail to change ownership and if we are
941f00d4f59SWei Liu              * using security model none. Ignore the error
942f00d4f59SWei Liu              */
943f00d4f59SWei Liu             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
944f00d4f59SWei Liu                 goto err_end;
94538771613SGreg Kurz             } else {
946f00d4f59SWei Liu                 err = 0;
947f00d4f59SWei Liu             }
948f00d4f59SWei Liu         }
94938771613SGreg Kurz     }
950f00d4f59SWei Liu     goto out;
951f00d4f59SWei Liu 
952f00d4f59SWei Liu err_end:
95338771613SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
954f00d4f59SWei Liu out:
95538771613SGreg Kurz     close_preserve_errno(dirfd);
956f00d4f59SWei Liu     return err;
957f00d4f59SWei Liu }
958f00d4f59SWei Liu 
959f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath,
960f00d4f59SWei Liu                       V9fsPath *dirpath, const char *name)
961f00d4f59SWei Liu {
962ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
963ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
964ad0b46e6SGreg Kurz     int ret = -1;
965ad0b46e6SGreg Kurz     int odirfd, ndirfd;
966f00d4f59SWei Liu 
9677a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
9687a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
9697a95434eSGreg Kurz         errno = EINVAL;
970841b8d09SJiajun Chen         goto out;
9717a95434eSGreg Kurz     }
9727a95434eSGreg Kurz 
973ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
974ad0b46e6SGreg Kurz     if (odirfd == -1) {
9756dd4b1f1SGreg Kurz         goto out;
9766dd4b1f1SGreg Kurz     }
977f00d4f59SWei Liu 
978ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
979ad0b46e6SGreg Kurz     if (ndirfd == -1) {
980ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
981ad0b46e6SGreg Kurz         goto out;
982ad0b46e6SGreg Kurz     }
983ad0b46e6SGreg Kurz 
984ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
985ad0b46e6SGreg Kurz     if (ret < 0) {
986ad0b46e6SGreg Kurz         goto out_close;
987ad0b46e6SGreg Kurz     }
988f00d4f59SWei Liu 
989f00d4f59SWei Liu     /* now link the virtfs_metadata files */
9906dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
991ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
9926dd4b1f1SGreg Kurz 
993*6ca60cd7SBin Meng         ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
994ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
995ad0b46e6SGreg Kurz             goto err_undo_link;
996f00d4f59SWei Liu         }
997ad0b46e6SGreg Kurz 
998ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
999ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
1000ad0b46e6SGreg Kurz             goto err;
1001ad0b46e6SGreg Kurz         }
1002ad0b46e6SGreg Kurz 
1003ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1004ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
1005ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
1006ad0b46e6SGreg Kurz             goto err;
1007ad0b46e6SGreg Kurz         }
1008ad0b46e6SGreg Kurz 
1009ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1010ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
1011ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
1012f00d4f59SWei Liu         if (ret < 0 && errno != ENOENT) {
1013ad0b46e6SGreg Kurz             goto err_undo_link;
1014f00d4f59SWei Liu         }
10156dd4b1f1SGreg Kurz 
1016ad0b46e6SGreg Kurz         ret = 0;
1017f00d4f59SWei Liu     }
1018ad0b46e6SGreg Kurz     goto out_close;
1019ad0b46e6SGreg Kurz 
1020ad0b46e6SGreg Kurz err:
1021ad0b46e6SGreg Kurz     ret = -1;
1022ad0b46e6SGreg Kurz err_undo_link:
1023ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
1024ad0b46e6SGreg Kurz out_close:
1025ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
1026ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
10276dd4b1f1SGreg Kurz out:
1028ad0b46e6SGreg Kurz     g_free(oname);
1029ad0b46e6SGreg Kurz     g_free(odirpath);
1030f00d4f59SWei Liu     return ret;
1031f00d4f59SWei Liu }
1032f00d4f59SWei Liu 
1033f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1034f00d4f59SWei Liu {
1035ac125d99SGreg Kurz     int fd, ret;
1036f00d4f59SWei Liu 
1037ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1038ac125d99SGreg Kurz     if (fd == -1) {
1039ac125d99SGreg Kurz         return -1;
1040ac125d99SGreg Kurz     }
1041ac125d99SGreg Kurz     ret = ftruncate(fd, size);
1042ac125d99SGreg Kurz     close_preserve_errno(fd);
1043f00d4f59SWei Liu     return ret;
1044f00d4f59SWei Liu }
1045f00d4f59SWei Liu 
1046f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1047f00d4f59SWei Liu {
1048d369f207SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1049d369f207SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1050f00d4f59SWei Liu     int ret = -1;
1051d369f207SGreg Kurz     int dirfd;
1052d369f207SGreg Kurz 
1053d369f207SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1054d369f207SGreg Kurz     if (dirfd == -1) {
1055d369f207SGreg Kurz         goto out;
1056d369f207SGreg Kurz     }
1057f00d4f59SWei Liu 
1058f00d4f59SWei Liu     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1059f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1060f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1061d369f207SGreg Kurz         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1062d369f207SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
1063f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1064d369f207SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
1065f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1066d369f207SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1067f00d4f59SWei Liu     }
1068d369f207SGreg Kurz 
1069d369f207SGreg Kurz     close_preserve_errno(dirfd);
1070d369f207SGreg Kurz out:
1071d369f207SGreg Kurz     g_free(name);
1072d369f207SGreg Kurz     g_free(dirpath);
1073f00d4f59SWei Liu     return ret;
1074f00d4f59SWei Liu }
1075f00d4f59SWei Liu 
1076f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1077f00d4f59SWei Liu                            const struct timespec *buf)
1078f00d4f59SWei Liu {
1079a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1080a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1081a33eda0dSGreg Kurz     int dirfd, ret = -1;
1082f00d4f59SWei Liu 
1083a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1084a33eda0dSGreg Kurz     if (dirfd == -1) {
1085a33eda0dSGreg Kurz         goto out;
1086a33eda0dSGreg Kurz     }
1087a33eda0dSGreg Kurz 
1088*6ca60cd7SBin Meng     ret = qemu_utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1089a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1090a33eda0dSGreg Kurz out:
1091a33eda0dSGreg Kurz     g_free(dirpath);
1092a33eda0dSGreg Kurz     g_free(name);
1093f00d4f59SWei Liu     return ret;
1094f00d4f59SWei Liu }
1095f00d4f59SWei Liu 
1096df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1097df4938a6SGreg Kurz                                  int flags)
1098f00d4f59SWei Liu {
1099846cf408SDaniel Henrique Barboza     int ret;
1100f00d4f59SWei Liu 
1101f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1102df4938a6SGreg Kurz         int map_dirfd;
1103df4938a6SGreg Kurz 
11046a87e792SGreg Kurz         /* We need to remove the metadata as well:
11056a87e792SGreg Kurz          * - the metadata directory if we're removing a directory
11066a87e792SGreg Kurz          * - the metadata file in the parent's metadata directory
11076a87e792SGreg Kurz          *
11086a87e792SGreg Kurz          * If any of these are missing (ie, ENOENT) then we're probably
11096a87e792SGreg Kurz          * trying to remove something that wasn't created in mapped-file
11106a87e792SGreg Kurz          * mode. We just ignore the error.
11116a87e792SGreg Kurz          */
1112df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1113df4938a6SGreg Kurz             int fd;
1114df4938a6SGreg Kurz 
1115b003fc0dSGreg Kurz             fd = openat_dir(dirfd, name);
1116df4938a6SGreg Kurz             if (fd == -1) {
1117846cf408SDaniel Henrique Barboza                 return -1;
1118f00d4f59SWei Liu             }
1119*6ca60cd7SBin Meng             ret = qemu_unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1120df4938a6SGreg Kurz             close_preserve_errno(fd);
1121df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1122846cf408SDaniel Henrique Barboza                 return -1;
1123f00d4f59SWei Liu             }
1124f00d4f59SWei Liu         }
1125df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
11266a87e792SGreg Kurz         if (map_dirfd != -1) {
1127*6ca60cd7SBin Meng             ret = qemu_unlinkat(map_dirfd, name, 0);
1128df4938a6SGreg Kurz             close_preserve_errno(map_dirfd);
1129df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1130846cf408SDaniel Henrique Barboza                 return -1;
11316a87e792SGreg Kurz             }
11326a87e792SGreg Kurz         } else if (errno != ENOENT) {
1133846cf408SDaniel Henrique Barboza             return -1;
1134f00d4f59SWei Liu         }
1135f00d4f59SWei Liu     }
1136f00d4f59SWei Liu 
1137*6ca60cd7SBin Meng     return qemu_unlinkat(dirfd, name, flags);
1138df4938a6SGreg Kurz }
1139df4938a6SGreg Kurz 
1140f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path)
1141f00d4f59SWei Liu {
1142f00d4f59SWei Liu     struct stat stbuf;
1143a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1144a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1145a0e640a8SGreg Kurz     int flags = 0;
1146a0e640a8SGreg Kurz     int dirfd;
1147a0e640a8SGreg Kurz     int err = -1;
1148f00d4f59SWei Liu 
1149a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1150b7361d46SGreg Kurz     if (dirfd == -1) {
1151a0e640a8SGreg Kurz         goto out;
1152a0e640a8SGreg Kurz     }
1153a0e640a8SGreg Kurz 
1154*6ca60cd7SBin Meng     if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1155f00d4f59SWei Liu         goto err_out;
1156f00d4f59SWei Liu     }
1157a0e640a8SGreg Kurz 
1158f00d4f59SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
1159a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1160f00d4f59SWei Liu     }
1161f00d4f59SWei Liu 
1162a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
1163f00d4f59SWei Liu err_out:
1164a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1165a0e640a8SGreg Kurz out:
1166a0e640a8SGreg Kurz     g_free(name);
1167a0e640a8SGreg Kurz     g_free(dirpath);
1168f00d4f59SWei Liu     return err;
1169f00d4f59SWei Liu }
1170f00d4f59SWei Liu 
1171f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type,
1172f00d4f59SWei Liu                        V9fsFidOpenState *fs, int datasync)
1173f00d4f59SWei Liu {
1174f00d4f59SWei Liu     int fd;
1175f00d4f59SWei Liu 
1176f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
1177f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
1178f00d4f59SWei Liu     } else {
1179f00d4f59SWei Liu         fd = fs->fd;
1180f00d4f59SWei Liu     }
1181f00d4f59SWei Liu 
1182f00d4f59SWei Liu     if (datasync) {
1183f00d4f59SWei Liu         return qemu_fdatasync(fd);
1184f00d4f59SWei Liu     } else {
1185f00d4f59SWei Liu         return fsync(fd);
1186f00d4f59SWei Liu     }
1187f00d4f59SWei Liu }
1188f00d4f59SWei Liu 
1189f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1190f00d4f59SWei Liu {
119131e51d1cSGreg Kurz     int fd, ret;
1192f00d4f59SWei Liu 
119331e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
119423da0145SGreg Kurz     if (fd == -1) {
119523da0145SGreg Kurz         return -1;
119623da0145SGreg Kurz     }
119731e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
119831e51d1cSGreg Kurz     close_preserve_errno(fd);
1199f00d4f59SWei Liu     return ret;
1200f00d4f59SWei Liu }
1201f00d4f59SWei Liu 
1202f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1203f00d4f59SWei Liu                                const char *name, void *value, size_t size)
1204f00d4f59SWei Liu {
1205f00d4f59SWei Liu     char *path = fs_path->data;
1206f00d4f59SWei Liu 
1207f00d4f59SWei Liu     return v9fs_get_xattr(ctx, path, name, value, size);
1208f00d4f59SWei Liu }
1209f00d4f59SWei Liu 
1210f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1211f00d4f59SWei Liu                                 void *value, size_t size)
1212f00d4f59SWei Liu {
1213f00d4f59SWei Liu     char *path = fs_path->data;
1214f00d4f59SWei Liu 
1215f00d4f59SWei Liu     return v9fs_list_xattr(ctx, path, value, size);
1216f00d4f59SWei Liu }
1217f00d4f59SWei Liu 
1218f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1219f00d4f59SWei Liu                            void *value, size_t size, int flags)
1220f00d4f59SWei Liu {
1221f00d4f59SWei Liu     char *path = fs_path->data;
1222f00d4f59SWei Liu 
1223f00d4f59SWei Liu     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1224f00d4f59SWei Liu }
1225f00d4f59SWei Liu 
1226f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1227f00d4f59SWei Liu                               const char *name)
1228f00d4f59SWei Liu {
1229f00d4f59SWei Liu     char *path = fs_path->data;
1230f00d4f59SWei Liu 
1231f00d4f59SWei Liu     return v9fs_remove_xattr(ctx, path, name);
1232f00d4f59SWei Liu }
1233f00d4f59SWei Liu 
1234f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1235f00d4f59SWei Liu                               const char *name, V9fsPath *target)
1236f00d4f59SWei Liu {
12377a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12387a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
12397a95434eSGreg Kurz         errno = EINVAL;
12407a95434eSGreg Kurz         return -1;
12417a95434eSGreg Kurz     }
12427a95434eSGreg Kurz 
1243f00d4f59SWei Liu     if (dir_path) {
1244f57f5878SGreg Kurz         if (!strcmp(name, ".")) {
1245f57f5878SGreg Kurz             /* "." relative to "foo/bar" is "foo/bar" */
1246f57f5878SGreg Kurz             v9fs_path_copy(target, dir_path);
1247f57f5878SGreg Kurz         } else if (!strcmp(name, "..")) {
1248f57f5878SGreg Kurz             if (!strcmp(dir_path->data, ".")) {
1249f57f5878SGreg Kurz                 /* ".." relative to the root is "." */
1250f57f5878SGreg Kurz                 v9fs_path_sprintf(target, ".");
12519c6b899fSGreg Kurz             } else {
1252f57f5878SGreg Kurz                 char *tmp = g_path_get_dirname(dir_path->data);
1253f57f5878SGreg Kurz                 /* Symbolic links are resolved by the client. We can assume
1254f57f5878SGreg Kurz                  * that ".." relative to "foo/bar" is equivalent to "foo"
12559c6b899fSGreg Kurz                  */
1256f57f5878SGreg Kurz                 v9fs_path_sprintf(target, "%s", tmp);
1257f57f5878SGreg Kurz                 g_free(tmp);
1258f57f5878SGreg Kurz             }
1259f57f5878SGreg Kurz         } else {
1260f57f5878SGreg Kurz             assert(!strchr(name, '/'));
1261f57f5878SGreg Kurz             v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1262f57f5878SGreg Kurz         }
1263f57f5878SGreg Kurz     } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1264f57f5878SGreg Kurz                !strcmp(name, "..")) {
1265f57f5878SGreg Kurz             /* This is the root fid */
1266f57f5878SGreg Kurz         v9fs_path_sprintf(target, ".");
1267f57f5878SGreg Kurz     } else {
1268f57f5878SGreg Kurz         assert(!strchr(name, '/'));
1269f57f5878SGreg Kurz         v9fs_path_sprintf(target, "./%s", name);
1270f00d4f59SWei Liu     }
1271f00d4f59SWei Liu     return 0;
1272f00d4f59SWei Liu }
1273f00d4f59SWei Liu 
1274f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1275f00d4f59SWei Liu                           const char *old_name, V9fsPath *newdir,
1276f00d4f59SWei Liu                           const char *new_name)
1277f00d4f59SWei Liu {
1278f00d4f59SWei Liu     int ret;
127999f2cf4bSGreg Kurz     int odirfd, ndirfd;
1280f00d4f59SWei Liu 
12817a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12827a95434eSGreg Kurz         (local_is_mapped_file_metadata(ctx, old_name) ||
12837a95434eSGreg Kurz          local_is_mapped_file_metadata(ctx, new_name))) {
12847a95434eSGreg Kurz         errno = EINVAL;
12857a95434eSGreg Kurz         return -1;
12867a95434eSGreg Kurz     }
12877a95434eSGreg Kurz 
128899f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
128999f2cf4bSGreg Kurz     if (odirfd == -1) {
129099f2cf4bSGreg Kurz         return -1;
129199f2cf4bSGreg Kurz     }
1292f00d4f59SWei Liu 
129399f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
129499f2cf4bSGreg Kurz     if (ndirfd == -1) {
129599f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
129699f2cf4bSGreg Kurz         return -1;
129799f2cf4bSGreg Kurz     }
1298f00d4f59SWei Liu 
1299*6ca60cd7SBin Meng     ret = qemu_renameat(odirfd, old_name, ndirfd, new_name);
130099f2cf4bSGreg Kurz     if (ret < 0) {
130199f2cf4bSGreg Kurz         goto out;
130299f2cf4bSGreg Kurz     }
130399f2cf4bSGreg Kurz 
130499f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
130599f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
130699f2cf4bSGreg Kurz 
1307*6ca60cd7SBin Meng         ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
130899f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
130999f2cf4bSGreg Kurz             goto err_undo_rename;
131099f2cf4bSGreg Kurz         }
131199f2cf4bSGreg Kurz 
13126dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
131399f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
131499f2cf4bSGreg Kurz             goto err;
131599f2cf4bSGreg Kurz         }
131699f2cf4bSGreg Kurz 
13176dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
131899f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
131999f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
132099f2cf4bSGreg Kurz             goto err;
132199f2cf4bSGreg Kurz         }
132299f2cf4bSGreg Kurz 
132399f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
1324*6ca60cd7SBin Meng         ret = qemu_renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
132599f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
132699f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
132799f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
132899f2cf4bSGreg Kurz             goto err_undo_rename;
132999f2cf4bSGreg Kurz         }
133099f2cf4bSGreg Kurz 
133199f2cf4bSGreg Kurz         ret = 0;
133299f2cf4bSGreg Kurz     }
133399f2cf4bSGreg Kurz     goto out;
133499f2cf4bSGreg Kurz 
133599f2cf4bSGreg Kurz err:
133699f2cf4bSGreg Kurz     ret = -1;
133799f2cf4bSGreg Kurz err_undo_rename:
133899f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
133999f2cf4bSGreg Kurz out:
134099f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
134199f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
1342f00d4f59SWei Liu     return ret;
1343f00d4f59SWei Liu }
1344f00d4f59SWei Liu 
1345d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1346d2767edeSGreg Kurz {
1347d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1348d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1349d2767edeSGreg Kurz }
1350d2767edeSGreg Kurz 
1351d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1352d2767edeSGreg Kurz                         const char *newpath)
1353d2767edeSGreg Kurz {
1354d2767edeSGreg Kurz     int err;
1355d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1356d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1357d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1358d2767edeSGreg Kurz 
1359d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1360d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1361d2767edeSGreg Kurz 
1362d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1363d2767edeSGreg Kurz 
1364d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1365d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1366d2767edeSGreg Kurz     g_free(nname);
1367d2767edeSGreg Kurz     g_free(oname);
1368d2767edeSGreg Kurz 
1369d2767edeSGreg Kurz     return err;
1370d2767edeSGreg Kurz }
1371d2767edeSGreg Kurz 
1372f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1373f00d4f59SWei Liu                           const char *name, int flags)
1374f00d4f59SWei Liu {
1375f00d4f59SWei Liu     int ret;
1376df4938a6SGreg Kurz     int dirfd;
1377f00d4f59SWei Liu 
13787a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
13797a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
13807a95434eSGreg Kurz         errno = EINVAL;
13817a95434eSGreg Kurz         return -1;
13827a95434eSGreg Kurz     }
13837a95434eSGreg Kurz 
1384df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1385df4938a6SGreg Kurz     if (dirfd == -1) {
1386df4938a6SGreg Kurz         return -1;
1387df4938a6SGreg Kurz     }
1388f00d4f59SWei Liu 
1389df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1390df4938a6SGreg Kurz     close_preserve_errno(dirfd);
1391f00d4f59SWei Liu     return ret;
1392f00d4f59SWei Liu }
1393f00d4f59SWei Liu 
13945b7b2f9aSKeno Fischer #ifdef FS_IOC_GETVERSION
1395f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1396f00d4f59SWei Liu                                 mode_t st_mode, uint64_t *st_gen)
1397f00d4f59SWei Liu {
1398f00d4f59SWei Liu     int err;
1399f00d4f59SWei Liu     V9fsFidOpenState fid_open;
1400f00d4f59SWei Liu 
1401f00d4f59SWei Liu     /*
1402f00d4f59SWei Liu      * Do not try to open special files like device nodes, fifos etc
1403f00d4f59SWei Liu      * We can get fd for regular files and directories only
1404f00d4f59SWei Liu      */
1405f00d4f59SWei Liu     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1406f00d4f59SWei Liu         errno = ENOTTY;
1407f00d4f59SWei Liu         return -1;
1408f00d4f59SWei Liu     }
1409f00d4f59SWei Liu     err = local_open(ctx, path, O_RDONLY, &fid_open);
1410f00d4f59SWei Liu     if (err < 0) {
1411f00d4f59SWei Liu         return err;
1412f00d4f59SWei Liu     }
1413f00d4f59SWei Liu     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1414f00d4f59SWei Liu     local_close(ctx, &fid_open);
1415f00d4f59SWei Liu     return err;
14165b7b2f9aSKeno Fischer }
1417f00d4f59SWei Liu #endif
1418f00d4f59SWei Liu 
14195b7b2f9aSKeno Fischer static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp)
1420f00d4f59SWei Liu {
142100c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
14225b7b2f9aSKeno Fischer     struct statfs stbuf;
14235b7b2f9aSKeno Fischer 
142400c90bd1SGreg Kurz     /*
142500c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
142600c90bd1SGreg Kurz      */
14270e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
14282306271cSKeno Fischer         error_setg_errno(errp, errno,
14292306271cSKeno Fischer                          "failed to stat file system at '%s'", ctx->fs_root);
14305b7b2f9aSKeno Fischer         return -1;
143100c90bd1SGreg Kurz     }
143200c90bd1SGreg Kurz     switch (stbuf.f_type) {
143300c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
143400c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
143500c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
143600c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
143700c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
143800c90bd1SGreg Kurz         break;
143900c90bd1SGreg Kurz     }
144000c90bd1SGreg Kurz #endif
14415b7b2f9aSKeno Fischer     return 0;
14425b7b2f9aSKeno Fischer }
14435b7b2f9aSKeno Fischer 
14445b7b2f9aSKeno Fischer static int local_init(FsContext *ctx, Error **errp)
14455b7b2f9aSKeno Fischer {
14465b7b2f9aSKeno Fischer     LocalData *data = g_malloc(sizeof(*data));
14475b7b2f9aSKeno Fischer 
14485b7b2f9aSKeno Fischer     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
14495b7b2f9aSKeno Fischer     if (data->mountfd == -1) {
14505b7b2f9aSKeno Fischer         error_setg_errno(errp, errno, "failed to open '%s'", ctx->fs_root);
14515b7b2f9aSKeno Fischer         goto err;
14525b7b2f9aSKeno Fischer     }
14535b7b2f9aSKeno Fischer 
14545b7b2f9aSKeno Fischer     if (local_ioc_getversion_init(ctx, data, errp) < 0) {
14555b7b2f9aSKeno Fischer         close(data->mountfd);
14565b7b2f9aSKeno Fischer         goto err;
14575b7b2f9aSKeno Fischer     }
1458f00d4f59SWei Liu 
1459f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1460f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1461f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1462f00d4f59SWei Liu         ctx->xops = mapped_xattr_ops;
1463f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_NONE) {
1464f00d4f59SWei Liu         ctx->xops = none_xattr_ops;
1465f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1466f00d4f59SWei Liu         /*
1467f00d4f59SWei Liu          * xattr operation for mapped-file and passthrough
1468f00d4f59SWei Liu          * remain same.
1469f00d4f59SWei Liu          */
1470f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1471f00d4f59SWei Liu     }
1472f00d4f59SWei Liu     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
147300c90bd1SGreg Kurz 
14740e35a378SGreg Kurz     ctx->private = data;
147500c90bd1SGreg Kurz     return 0;
14760e35a378SGreg Kurz 
14770e35a378SGreg Kurz err:
14780e35a378SGreg Kurz     g_free(data);
14790e35a378SGreg Kurz     return -1;
1480f00d4f59SWei Liu }
14810e35a378SGreg Kurz 
14820e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
14830e35a378SGreg Kurz {
14840e35a378SGreg Kurz     LocalData *data = ctx->private;
14850e35a378SGreg Kurz 
1486c0da0cb7SGreg Kurz     if (!data) {
1487c0da0cb7SGreg Kurz         return;
1488c0da0cb7SGreg Kurz     }
1489c0da0cb7SGreg Kurz 
14900e35a378SGreg Kurz     close(data->mountfd);
14910e35a378SGreg Kurz     g_free(data);
1492f00d4f59SWei Liu }
1493f00d4f59SWei Liu 
14944c5ec47eSVladimir Sementsov-Ogievskiy static void error_append_security_model_hint(Error *const *errp)
149591cda4e8SGreg Kurz {
149691cda4e8SGreg Kurz     error_append_hint(errp, "Valid options are: security_model="
149791cda4e8SGreg Kurz                       "[passthrough|mapped-xattr|mapped-file|none]\n");
149891cda4e8SGreg Kurz }
149991cda4e8SGreg Kurz 
150091cda4e8SGreg Kurz static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
1501f00d4f59SWei Liu {
150292c45122SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
1503f00d4f59SWei Liu     const char *sec_model = qemu_opt_get(opts, "security_model");
1504f00d4f59SWei Liu     const char *path = qemu_opt_get(opts, "path");
15051a6ed33cSAntonios Motakis     const char *multidevs = qemu_opt_get(opts, "multidevs");
1506f00d4f59SWei Liu 
1507f00d4f59SWei Liu     if (!sec_model) {
150891cda4e8SGreg Kurz         error_setg(errp, "security_model property not set");
150991cda4e8SGreg Kurz         error_append_security_model_hint(errp);
1510f00d4f59SWei Liu         return -1;
1511f00d4f59SWei Liu     }
1512f00d4f59SWei Liu 
1513f00d4f59SWei Liu     if (!strcmp(sec_model, "passthrough")) {
1514f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1515f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped") ||
1516f00d4f59SWei Liu                !strcmp(sec_model, "mapped-xattr")) {
1517f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED;
1518f00d4f59SWei Liu     } else if (!strcmp(sec_model, "none")) {
1519f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_NONE;
1520f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped-file")) {
1521f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1522f00d4f59SWei Liu     } else {
152391cda4e8SGreg Kurz         error_setg(errp, "invalid security_model property '%s'", sec_model);
152491cda4e8SGreg Kurz         error_append_security_model_hint(errp);
1525f00d4f59SWei Liu         return -1;
1526f00d4f59SWei Liu     }
1527f00d4f59SWei Liu 
15281a6ed33cSAntonios Motakis     if (multidevs) {
15291a6ed33cSAntonios Motakis         if (!strcmp(multidevs, "remap")) {
15301a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
15311a6ed33cSAntonios Motakis             fse->export_flags |= V9FS_REMAP_INODES;
15321a6ed33cSAntonios Motakis         } else if (!strcmp(multidevs, "forbid")) {
15331a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_REMAP_INODES;
15341a6ed33cSAntonios Motakis             fse->export_flags |= V9FS_FORBID_MULTIDEVS;
15351a6ed33cSAntonios Motakis         } else if (!strcmp(multidevs, "warn")) {
15361a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
15371a6ed33cSAntonios Motakis             fse->export_flags &= ~V9FS_REMAP_INODES;
15381a6ed33cSAntonios Motakis         } else {
153992c45122SVladimir Sementsov-Ogievskiy             error_setg(errp, "invalid multidevs property '%s'",
15401a6ed33cSAntonios Motakis                        multidevs);
154192c45122SVladimir Sementsov-Ogievskiy             error_append_hint(errp, "Valid options are: multidevs="
15421a6ed33cSAntonios Motakis                               "[remap|forbid|warn]\n");
15431a6ed33cSAntonios Motakis             return -1;
15441a6ed33cSAntonios Motakis         }
15451a6ed33cSAntonios Motakis     }
15461a6ed33cSAntonios Motakis 
1547f00d4f59SWei Liu     if (!path) {
154891cda4e8SGreg Kurz         error_setg(errp, "path property not set");
1549f00d4f59SWei Liu         return -1;
1550f00d4f59SWei Liu     }
1551b8bbdb88SPradeep Jagadeesh 
155292c45122SVladimir Sementsov-Ogievskiy     if (fsdev_throttle_parse_opts(opts, &fse->fst, errp)) {
155392c45122SVladimir Sementsov-Ogievskiy         error_prepend(errp, "invalid throttle configuration: ");
1554b8bbdb88SPradeep Jagadeesh         return -1;
1555b8bbdb88SPradeep Jagadeesh     }
1556b8bbdb88SPradeep Jagadeesh 
1557b96feb2cSTobias Schramm     if (fse->export_flags & V9FS_SM_MAPPED ||
1558b96feb2cSTobias Schramm         fse->export_flags & V9FS_SM_MAPPED_FILE) {
1559b96feb2cSTobias Schramm         fse->fmode =
1560b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1561b96feb2cSTobias Schramm         fse->dmode =
1562b96feb2cSTobias Schramm             qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1563b96feb2cSTobias Schramm     } else {
1564b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "fmode")) {
156591cda4e8SGreg Kurz             error_setg(errp, "fmode is only valid for mapped security modes");
1566b96feb2cSTobias Schramm             return -1;
1567b96feb2cSTobias Schramm         }
1568b96feb2cSTobias Schramm         if (qemu_opt_find(opts, "dmode")) {
156991cda4e8SGreg Kurz             error_setg(errp, "dmode is only valid for mapped security modes");
1570b96feb2cSTobias Schramm             return -1;
1571b96feb2cSTobias Schramm         }
1572b96feb2cSTobias Schramm     }
1573b96feb2cSTobias Schramm 
1574f00d4f59SWei Liu     fse->path = g_strdup(path);
1575f00d4f59SWei Liu 
1576f00d4f59SWei Liu     return 0;
1577f00d4f59SWei Liu }
1578f00d4f59SWei Liu 
1579f00d4f59SWei Liu FileOperations local_ops = {
1580f00d4f59SWei Liu     .parse_opts = local_parse_opts,
1581f00d4f59SWei Liu     .init  = local_init,
15820e35a378SGreg Kurz     .cleanup = local_cleanup,
1583f00d4f59SWei Liu     .lstat = local_lstat,
1584f00d4f59SWei Liu     .readlink = local_readlink,
1585f00d4f59SWei Liu     .close = local_close,
1586f00d4f59SWei Liu     .closedir = local_closedir,
1587f00d4f59SWei Liu     .open = local_open,
1588f00d4f59SWei Liu     .opendir = local_opendir,
1589f00d4f59SWei Liu     .rewinddir = local_rewinddir,
1590f00d4f59SWei Liu     .telldir = local_telldir,
1591635324e8SGreg Kurz     .readdir = local_readdir,
1592f00d4f59SWei Liu     .seekdir = local_seekdir,
1593f00d4f59SWei Liu     .preadv = local_preadv,
1594f00d4f59SWei Liu     .pwritev = local_pwritev,
1595f00d4f59SWei Liu     .chmod = local_chmod,
1596f00d4f59SWei Liu     .mknod = local_mknod,
1597f00d4f59SWei Liu     .mkdir = local_mkdir,
1598f00d4f59SWei Liu     .fstat = local_fstat,
1599f00d4f59SWei Liu     .open2 = local_open2,
1600f00d4f59SWei Liu     .symlink = local_symlink,
1601f00d4f59SWei Liu     .link = local_link,
1602f00d4f59SWei Liu     .truncate = local_truncate,
1603f00d4f59SWei Liu     .rename = local_rename,
1604f00d4f59SWei Liu     .chown = local_chown,
1605f00d4f59SWei Liu     .utimensat = local_utimensat,
1606f00d4f59SWei Liu     .remove = local_remove,
1607f00d4f59SWei Liu     .fsync = local_fsync,
1608f00d4f59SWei Liu     .statfs = local_statfs,
1609f00d4f59SWei Liu     .lgetxattr = local_lgetxattr,
1610f00d4f59SWei Liu     .llistxattr = local_llistxattr,
1611f00d4f59SWei Liu     .lsetxattr = local_lsetxattr,
1612f00d4f59SWei Liu     .lremovexattr = local_lremovexattr,
1613f00d4f59SWei Liu     .name_to_path = local_name_to_path,
1614f00d4f59SWei Liu     .renameat  = local_renameat,
1615f00d4f59SWei Liu     .unlinkat = local_unlinkat,
1616f00d4f59SWei Liu };
1617