xref: /openbmc/qemu/hw/9pfs/9p-local.c (revision 3dbcf27334b6c41e74a476b55d76f60df1c4007b)
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  */
13f00d4f59SWei Liu 
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"
19f00d4f59SWei Liu #include "fsdev/qemu-fsdev.h"   /* local_ops */
20f00d4f59SWei Liu #include <arpa/inet.h>
21f00d4f59SWei Liu #include <pwd.h>
22f00d4f59SWei Liu #include <grp.h>
23f00d4f59SWei Liu #include <sys/socket.h>
24f00d4f59SWei Liu #include <sys/un.h>
25f00d4f59SWei Liu #include "qemu/xattr.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
2763325b18SGreg Kurz #include "qemu/error-report.h"
28f00d4f59SWei Liu #include <libgen.h>
29f00d4f59SWei Liu #include <linux/fs.h>
30f00d4f59SWei Liu #ifdef CONFIG_LINUX_MAGIC_H
31f00d4f59SWei Liu #include <linux/magic.h>
32f00d4f59SWei Liu #endif
33f00d4f59SWei Liu #include <sys/ioctl.h>
34f00d4f59SWei Liu 
35f00d4f59SWei Liu #ifndef XFS_SUPER_MAGIC
36f00d4f59SWei Liu #define XFS_SUPER_MAGIC  0x58465342
37f00d4f59SWei Liu #endif
38f00d4f59SWei Liu #ifndef EXT2_SUPER_MAGIC
39f00d4f59SWei Liu #define EXT2_SUPER_MAGIC 0xEF53
40f00d4f59SWei Liu #endif
41f00d4f59SWei Liu #ifndef REISERFS_SUPER_MAGIC
42f00d4f59SWei Liu #define REISERFS_SUPER_MAGIC 0x52654973
43f00d4f59SWei Liu #endif
44f00d4f59SWei Liu #ifndef BTRFS_SUPER_MAGIC
45f00d4f59SWei Liu #define BTRFS_SUPER_MAGIC 0x9123683E
46f00d4f59SWei Liu #endif
47f00d4f59SWei Liu 
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;
56*3dbcf273SGreg Kurz     int fd = data->mountfd;
57996a0d76SGreg Kurz 
58*3dbcf273SGreg Kurz     while (*path && fd != -1) {
59*3dbcf273SGreg Kurz         const char *c;
60*3dbcf273SGreg Kurz         int next_fd;
61*3dbcf273SGreg Kurz         char *head;
62*3dbcf273SGreg Kurz 
63*3dbcf273SGreg Kurz         /* Only relative paths without consecutive slashes */
64*3dbcf273SGreg Kurz         assert(*path != '/');
65*3dbcf273SGreg Kurz 
66*3dbcf273SGreg Kurz         head = g_strdup(path);
67*3dbcf273SGreg Kurz         c = strchrnul(path, '/');
68*3dbcf273SGreg Kurz         if (*c) {
69*3dbcf273SGreg Kurz             /* Intermediate path element */
70*3dbcf273SGreg Kurz             head[c - path] = 0;
71*3dbcf273SGreg Kurz             path = c + 1;
72*3dbcf273SGreg Kurz             next_fd = openat_dir(fd, head);
73*3dbcf273SGreg Kurz         } else {
74*3dbcf273SGreg Kurz             /* Rightmost path element */
75*3dbcf273SGreg Kurz             next_fd = openat_file(fd, head, flags, mode);
76*3dbcf273SGreg Kurz             path = c;
77*3dbcf273SGreg Kurz         }
78*3dbcf273SGreg Kurz         g_free(head);
79*3dbcf273SGreg Kurz         if (fd != data->mountfd) {
80*3dbcf273SGreg Kurz             close_preserve_errno(fd);
81*3dbcf273SGreg Kurz         }
82*3dbcf273SGreg Kurz         fd = next_fd;
83996a0d76SGreg Kurz     }
84996a0d76SGreg Kurz 
85*3dbcf273SGreg Kurz     assert(fd != data->mountfd);
86*3dbcf273SGreg 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 
109f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata"
110f00d4f59SWei Liu 
111f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
112f00d4f59SWei Liu {
113f00d4f59SWei Liu     int fd, o_mode = 0;
114f00d4f59SWei Liu     FILE *fp;
115f9aef99bSGreg Kurz     int flags;
116f00d4f59SWei Liu     /*
117f00d4f59SWei Liu      * only supports two modes
118f00d4f59SWei Liu      */
119f00d4f59SWei Liu     if (mode[0] == 'r') {
120f9aef99bSGreg Kurz         flags = O_RDONLY;
121f00d4f59SWei Liu     } else if (mode[0] == 'w') {
122f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
123f00d4f59SWei Liu         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
124f00d4f59SWei Liu     } else {
125f00d4f59SWei Liu         return NULL;
126f00d4f59SWei Liu     }
127f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
128f00d4f59SWei Liu     if (fd == -1) {
129f00d4f59SWei Liu         return NULL;
130f00d4f59SWei Liu     }
131f00d4f59SWei Liu     fp = fdopen(fd, mode);
132f00d4f59SWei Liu     if (!fp) {
133f00d4f59SWei Liu         close(fd);
134f00d4f59SWei Liu     }
135f00d4f59SWei Liu     return fp;
136f00d4f59SWei Liu }
137f00d4f59SWei Liu 
138f00d4f59SWei Liu #define ATTR_MAX 100
139f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
140f00d4f59SWei Liu                                    struct stat *stbuf)
141f00d4f59SWei Liu {
142f00d4f59SWei Liu     FILE *fp;
143f00d4f59SWei Liu     char buf[ATTR_MAX];
144f9aef99bSGreg Kurz     int map_dirfd;
145f00d4f59SWei Liu 
14699f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
147f9aef99bSGreg Kurz     if (map_dirfd == -1) {
148f9aef99bSGreg Kurz         return;
149f9aef99bSGreg Kurz     }
150f9aef99bSGreg Kurz 
151f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
152f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
153f00d4f59SWei Liu     if (!fp) {
154f00d4f59SWei Liu         return;
155f00d4f59SWei Liu     }
156f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
157f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
158f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
159f00d4f59SWei Liu             stbuf->st_uid = atoi(buf+11);
160f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
161f00d4f59SWei Liu             stbuf->st_gid = atoi(buf+11);
162f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
163f00d4f59SWei Liu             stbuf->st_mode = atoi(buf+12);
164f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
165f00d4f59SWei Liu             stbuf->st_rdev = atoi(buf+12);
166f00d4f59SWei Liu         }
167f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
168f00d4f59SWei Liu     }
169f00d4f59SWei Liu     fclose(fp);
170f00d4f59SWei Liu }
171f00d4f59SWei Liu 
172f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
173f00d4f59SWei Liu {
174f9aef99bSGreg Kurz     int err = -1;
175f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
176f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
177f9aef99bSGreg Kurz     int dirfd;
178f00d4f59SWei Liu 
179f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
180f9aef99bSGreg Kurz     if (dirfd == -1) {
181f9aef99bSGreg Kurz         goto out;
182f9aef99bSGreg Kurz     }
183f9aef99bSGreg Kurz 
184f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
185f00d4f59SWei Liu     if (err) {
186f00d4f59SWei Liu         goto err_out;
187f00d4f59SWei Liu     }
188f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
189f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
190f00d4f59SWei Liu         uid_t tmp_uid;
191f00d4f59SWei Liu         gid_t tmp_gid;
192f00d4f59SWei Liu         mode_t tmp_mode;
193f00d4f59SWei Liu         dev_t tmp_dev;
194f9aef99bSGreg Kurz 
195f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
196f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
197f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
198f00d4f59SWei Liu         }
199f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
200f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
201f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
202f00d4f59SWei Liu         }
203f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
204f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
205f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
206f00d4f59SWei Liu         }
207f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
208f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
209f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
210f00d4f59SWei Liu         }
211f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
212f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
213f00d4f59SWei Liu     }
214f00d4f59SWei Liu 
215f00d4f59SWei Liu err_out:
216f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
217f9aef99bSGreg Kurz out:
218f9aef99bSGreg Kurz     g_free(name);
219f9aef99bSGreg Kurz     g_free(dirpath);
220f00d4f59SWei Liu     return err;
221f00d4f59SWei Liu }
222f00d4f59SWei Liu 
223e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
224e3187a45SGreg Kurz                                         FsCred *credp)
225f00d4f59SWei Liu {
226f00d4f59SWei Liu     FILE *fp;
227e3187a45SGreg Kurz     int ret;
228f00d4f59SWei Liu     char buf[ATTR_MAX];
229f00d4f59SWei Liu     int uid = -1, gid = -1, mode = -1, rdev = -1;
230e3187a45SGreg Kurz     int map_dirfd;
231f00d4f59SWei Liu 
232e3187a45SGreg Kurz     ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
233e3187a45SGreg Kurz     if (ret < 0 && errno != EEXIST) {
234e3187a45SGreg Kurz         return -1;
235e3187a45SGreg Kurz     }
236e3187a45SGreg Kurz 
237e3187a45SGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
238e3187a45SGreg Kurz     if (map_dirfd == -1) {
239e3187a45SGreg Kurz         return -1;
240e3187a45SGreg Kurz     }
241e3187a45SGreg Kurz 
242e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
243f00d4f59SWei Liu     if (!fp) {
244e3187a45SGreg Kurz         if (errno == ENOENT) {
245e3187a45SGreg Kurz             goto update_map_file;
246e3187a45SGreg Kurz         } else {
247e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
248e3187a45SGreg Kurz             return -1;
249e3187a45SGreg Kurz         }
250f00d4f59SWei Liu     }
251f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
252f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
253f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
254f00d4f59SWei Liu             uid = atoi(buf + 11);
255f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
256f00d4f59SWei Liu             gid = atoi(buf + 11);
257f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
258f00d4f59SWei Liu             mode = atoi(buf + 12);
259f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
260f00d4f59SWei Liu             rdev = atoi(buf + 12);
261f00d4f59SWei Liu         }
262f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
263f00d4f59SWei Liu     }
264f00d4f59SWei Liu     fclose(fp);
265f00d4f59SWei Liu 
266f00d4f59SWei Liu update_map_file:
267e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "w");
268e3187a45SGreg Kurz     close_preserve_errno(map_dirfd);
269f00d4f59SWei Liu     if (!fp) {
270e3187a45SGreg Kurz         return -1;
271f00d4f59SWei Liu     }
272f00d4f59SWei Liu 
273f00d4f59SWei Liu     if (credp->fc_uid != -1) {
274f00d4f59SWei Liu         uid = credp->fc_uid;
275f00d4f59SWei Liu     }
276f00d4f59SWei Liu     if (credp->fc_gid != -1) {
277f00d4f59SWei Liu         gid = credp->fc_gid;
278f00d4f59SWei Liu     }
279f00d4f59SWei Liu     if (credp->fc_mode != -1) {
280f00d4f59SWei Liu         mode = credp->fc_mode;
281f00d4f59SWei Liu     }
282f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
283f00d4f59SWei Liu         rdev = credp->fc_rdev;
284f00d4f59SWei Liu     }
285f00d4f59SWei Liu 
286f00d4f59SWei Liu     if (uid != -1) {
287f00d4f59SWei Liu         fprintf(fp, "virtfs.uid=%d\n", uid);
288f00d4f59SWei Liu     }
289f00d4f59SWei Liu     if (gid != -1) {
290f00d4f59SWei Liu         fprintf(fp, "virtfs.gid=%d\n", gid);
291f00d4f59SWei Liu     }
292f00d4f59SWei Liu     if (mode != -1) {
293f00d4f59SWei Liu         fprintf(fp, "virtfs.mode=%d\n", mode);
294f00d4f59SWei Liu     }
295f00d4f59SWei Liu     if (rdev != -1) {
296f00d4f59SWei Liu         fprintf(fp, "virtfs.rdev=%d\n", rdev);
297f00d4f59SWei Liu     }
298f00d4f59SWei Liu     fclose(fp);
299f00d4f59SWei Liu 
300e3187a45SGreg Kurz     return 0;
301e3187a45SGreg Kurz }
302e3187a45SGreg Kurz 
303e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
304e3187a45SGreg Kurz {
305e3187a45SGreg Kurz     int fd, ret;
306e3187a45SGreg Kurz 
307e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
308e3187a45SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet. As an
309e3187a45SGreg Kurz      * alternative, let's open the file and use fchmod() instead. This
310e3187a45SGreg Kurz      * may fail depending on the permissions of the file, but it is the
311e3187a45SGreg Kurz      * best we can do to avoid TOCTTOU. We first try to open read-only
312e3187a45SGreg Kurz      * in case name points to a directory. If that fails, we try write-only
313e3187a45SGreg Kurz      * in case name doesn't point to a directory.
314e3187a45SGreg Kurz      */
315e3187a45SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY, 0);
316e3187a45SGreg Kurz     if (fd == -1) {
317e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
318e3187a45SGreg Kurz         if (errno == EACCES) {
319e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
320e3187a45SGreg Kurz         }
321e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
322e3187a45SGreg Kurz             errno = EACCES;
323e3187a45SGreg Kurz         }
324e3187a45SGreg Kurz     }
325e3187a45SGreg Kurz     if (fd == -1) {
326e3187a45SGreg Kurz         return -1;
327e3187a45SGreg Kurz     }
328e3187a45SGreg Kurz     ret = fchmod(fd, mode);
329e3187a45SGreg Kurz     close_preserve_errno(fd);
330f00d4f59SWei Liu     return ret;
331f00d4f59SWei Liu }
332f00d4f59SWei Liu 
333e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
334f00d4f59SWei Liu {
335f00d4f59SWei Liu     int err;
336f00d4f59SWei Liu 
337f00d4f59SWei Liu     if (credp->fc_uid != -1) {
338f00d4f59SWei Liu         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
339e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
340e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
341f00d4f59SWei Liu         if (err) {
342f00d4f59SWei Liu             return err;
343f00d4f59SWei Liu         }
344f00d4f59SWei Liu     }
345f00d4f59SWei Liu     if (credp->fc_gid != -1) {
346f00d4f59SWei Liu         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
347e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
348e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
349f00d4f59SWei Liu         if (err) {
350f00d4f59SWei Liu             return err;
351f00d4f59SWei Liu         }
352f00d4f59SWei Liu     }
353f00d4f59SWei Liu     if (credp->fc_mode != -1) {
354f00d4f59SWei Liu         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
355e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
356e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
357f00d4f59SWei Liu         if (err) {
358f00d4f59SWei Liu             return err;
359f00d4f59SWei Liu         }
360f00d4f59SWei Liu     }
361f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
362f00d4f59SWei Liu         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
363e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
364e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
365f00d4f59SWei Liu         if (err) {
366f00d4f59SWei Liu             return err;
367f00d4f59SWei Liu         }
368f00d4f59SWei Liu     }
369f00d4f59SWei Liu     return 0;
370f00d4f59SWei Liu }
371f00d4f59SWei Liu 
372d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
373d815e721SGreg Kurz                                       const char *name, FsCred *credp)
374f00d4f59SWei Liu {
375d815e721SGreg Kurz     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
376b314f6a0SGreg Kurz                  AT_SYMLINK_NOFOLLOW) < 0) {
377f00d4f59SWei Liu         /*
378f00d4f59SWei Liu          * If we fail to change ownership and if we are
379f00d4f59SWei Liu          * using security model none. Ignore the error
380f00d4f59SWei Liu          */
381f00d4f59SWei Liu         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
382f00d4f59SWei Liu             return -1;
383f00d4f59SWei Liu         }
384d815e721SGreg Kurz     }
385d815e721SGreg Kurz 
386d815e721SGreg Kurz     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
387d815e721SGreg Kurz }
388f00d4f59SWei Liu 
389f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
390f00d4f59SWei Liu                               char *buf, size_t bufsz)
391f00d4f59SWei Liu {
392f00d4f59SWei Liu     ssize_t tsize = -1;
393f00d4f59SWei Liu 
394f00d4f59SWei Liu     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
395f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
396f00d4f59SWei Liu         int fd;
397bec1e954SGreg Kurz 
398bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
399f00d4f59SWei Liu         if (fd == -1) {
400f00d4f59SWei Liu             return -1;
401f00d4f59SWei Liu         }
402f00d4f59SWei Liu         do {
403f00d4f59SWei Liu             tsize = read(fd, (void *)buf, bufsz);
404f00d4f59SWei Liu         } while (tsize == -1 && errno == EINTR);
405bec1e954SGreg Kurz         close_preserve_errno(fd);
406f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
407f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
408bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
409bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
410bec1e954SGreg Kurz         int dirfd;
411bec1e954SGreg Kurz 
412bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
413bec1e954SGreg Kurz         if (dirfd == -1) {
414bec1e954SGreg Kurz             goto out;
415bec1e954SGreg Kurz         }
416bec1e954SGreg Kurz 
417bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
418bec1e954SGreg Kurz         close_preserve_errno(dirfd);
419bec1e954SGreg Kurz     out:
420bec1e954SGreg Kurz         g_free(name);
421bec1e954SGreg Kurz         g_free(dirpath);
422f00d4f59SWei Liu     }
423f00d4f59SWei Liu     return tsize;
424f00d4f59SWei Liu }
425f00d4f59SWei Liu 
426f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
427f00d4f59SWei Liu {
428f00d4f59SWei Liu     return close(fs->fd);
429f00d4f59SWei Liu }
430f00d4f59SWei Liu 
431f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
432f00d4f59SWei Liu {
433f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
434f00d4f59SWei Liu }
435f00d4f59SWei Liu 
436f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path,
437f00d4f59SWei Liu                       int flags, V9fsFidOpenState *fs)
438f00d4f59SWei Liu {
43921328e1eSGreg Kurz     int fd;
440f00d4f59SWei Liu 
441996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
44221328e1eSGreg Kurz     if (fd == -1) {
44321328e1eSGreg Kurz         return -1;
44421328e1eSGreg Kurz     }
44521328e1eSGreg Kurz     fs->fd = fd;
446f00d4f59SWei Liu     return fs->fd;
447f00d4f59SWei Liu }
448f00d4f59SWei Liu 
449f00d4f59SWei Liu static int local_opendir(FsContext *ctx,
450f00d4f59SWei Liu                          V9fsPath *fs_path, V9fsFidOpenState *fs)
451f00d4f59SWei Liu {
452996a0d76SGreg Kurz     int dirfd;
45321328e1eSGreg Kurz     DIR *stream;
454f00d4f59SWei Liu 
455996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
456996a0d76SGreg Kurz     if (dirfd == -1) {
457f00d4f59SWei Liu         return -1;
458f00d4f59SWei Liu     }
459996a0d76SGreg Kurz 
460996a0d76SGreg Kurz     stream = fdopendir(dirfd);
46121328e1eSGreg Kurz     if (!stream) {
462faab207fSGreg Kurz         close(dirfd);
463f00d4f59SWei Liu         return -1;
464f00d4f59SWei Liu     }
46521328e1eSGreg Kurz     fs->dir.stream = stream;
466f00d4f59SWei Liu     return 0;
467f00d4f59SWei Liu }
468f00d4f59SWei Liu 
469f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
470f00d4f59SWei Liu {
471f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
472f00d4f59SWei Liu }
473f00d4f59SWei Liu 
474f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
475f00d4f59SWei Liu {
476f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
477f00d4f59SWei Liu }
478f00d4f59SWei Liu 
4797a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
4807a95434eSGreg Kurz {
4817a95434eSGreg Kurz     return !strcmp(name, VIRTFS_META_DIR);
4827a95434eSGreg Kurz }
4837a95434eSGreg Kurz 
484635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
485f00d4f59SWei Liu {
486635324e8SGreg Kurz     struct dirent *entry;
487f00d4f59SWei Liu 
488f00d4f59SWei Liu again:
489635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
490635324e8SGreg Kurz     if (!entry) {
491635324e8SGreg Kurz         return NULL;
492635324e8SGreg Kurz     }
493635324e8SGreg Kurz 
494f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED) {
495f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
496f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4977a95434eSGreg Kurz         if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
4987a95434eSGreg Kurz             /* skip the meta data directory */
499f00d4f59SWei Liu             goto again;
500f00d4f59SWei Liu         }
501f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
502f00d4f59SWei Liu     }
503635324e8SGreg Kurz 
504635324e8SGreg Kurz     return entry;
505f00d4f59SWei Liu }
506f00d4f59SWei Liu 
507f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
508f00d4f59SWei Liu {
509f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
510f00d4f59SWei Liu }
511f00d4f59SWei Liu 
512f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
513f00d4f59SWei Liu                             const struct iovec *iov,
514f00d4f59SWei Liu                             int iovcnt, off_t offset)
515f00d4f59SWei Liu {
516f00d4f59SWei Liu #ifdef CONFIG_PREADV
517f00d4f59SWei Liu     return preadv(fs->fd, iov, iovcnt, offset);
518f00d4f59SWei Liu #else
519f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
520f00d4f59SWei Liu     if (err == -1) {
521f00d4f59SWei Liu         return err;
522f00d4f59SWei Liu     } else {
523f00d4f59SWei Liu         return readv(fs->fd, iov, iovcnt);
524f00d4f59SWei Liu     }
525f00d4f59SWei Liu #endif
526f00d4f59SWei Liu }
527f00d4f59SWei Liu 
528f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
529f00d4f59SWei Liu                              const struct iovec *iov,
530f00d4f59SWei Liu                              int iovcnt, off_t offset)
531f00d4f59SWei Liu {
5326fe76accSGreg Kurz     ssize_t ret;
533f00d4f59SWei Liu #ifdef CONFIG_PREADV
534f00d4f59SWei Liu     ret = pwritev(fs->fd, iov, iovcnt, offset);
535f00d4f59SWei Liu #else
536f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
537f00d4f59SWei Liu     if (err == -1) {
538f00d4f59SWei Liu         return err;
539f00d4f59SWei Liu     } else {
540f00d4f59SWei Liu         ret = writev(fs->fd, iov, iovcnt);
541f00d4f59SWei Liu     }
542f00d4f59SWei Liu #endif
543f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE
544f00d4f59SWei Liu     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
545f00d4f59SWei Liu         /*
546f00d4f59SWei Liu          * Initiate a writeback. This is not a data integrity sync.
547f00d4f59SWei Liu          * We want to ensure that we don't leave dirty pages in the cache
548f00d4f59SWei Liu          * after write when writeout=immediate is sepcified.
549f00d4f59SWei Liu          */
550f00d4f59SWei Liu         sync_file_range(fs->fd, offset, ret,
551f00d4f59SWei Liu                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
552f00d4f59SWei Liu     }
553f00d4f59SWei Liu #endif
554f00d4f59SWei Liu     return ret;
555f00d4f59SWei Liu }
556f00d4f59SWei Liu 
557f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
558f00d4f59SWei Liu {
559e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
560e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
561f00d4f59SWei Liu     int ret = -1;
562e3187a45SGreg Kurz     int dirfd;
563e3187a45SGreg Kurz 
564e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
565e3187a45SGreg Kurz     if (dirfd == -1) {
566e3187a45SGreg Kurz         goto out;
567e3187a45SGreg Kurz     }
568f00d4f59SWei Liu 
569f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
570e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
571f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
572e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
573e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
574e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
575e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
576f00d4f59SWei Liu     }
577e3187a45SGreg Kurz     close_preserve_errno(dirfd);
578e3187a45SGreg Kurz 
579e3187a45SGreg Kurz out:
580e3187a45SGreg Kurz     g_free(dirpath);
581e3187a45SGreg Kurz     g_free(name);
582f00d4f59SWei Liu     return ret;
583f00d4f59SWei Liu }
584f00d4f59SWei Liu 
585f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
586f00d4f59SWei Liu                        const char *name, FsCred *credp)
587f00d4f59SWei Liu {
588f00d4f59SWei Liu     int err = -1;
589d815e721SGreg Kurz     int dirfd;
590f00d4f59SWei Liu 
5917a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
5927a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
5937a95434eSGreg Kurz         errno = EINVAL;
5947a95434eSGreg Kurz         return -1;
5957a95434eSGreg Kurz     }
5967a95434eSGreg Kurz 
597d815e721SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
598d815e721SGreg Kurz     if (dirfd == -1) {
599d815e721SGreg Kurz         return -1;
600d815e721SGreg Kurz     }
601f00d4f59SWei Liu 
602d815e721SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
603d815e721SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
604d815e721SGreg Kurz         err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
605d815e721SGreg Kurz         if (err == -1) {
606d815e721SGreg Kurz             goto out;
607d815e721SGreg Kurz         }
608d815e721SGreg Kurz 
609f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
610d815e721SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
611d815e721SGreg Kurz         } else {
612d815e721SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
613f00d4f59SWei Liu         }
614f00d4f59SWei Liu         if (err == -1) {
615f00d4f59SWei Liu             goto err_end;
616f00d4f59SWei Liu         }
617d815e721SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
618d815e721SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
619d815e721SGreg Kurz         err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
620f00d4f59SWei Liu         if (err == -1) {
621f00d4f59SWei Liu             goto out;
622f00d4f59SWei Liu         }
623d815e721SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
624f00d4f59SWei Liu         if (err == -1) {
625f00d4f59SWei Liu             goto err_end;
626f00d4f59SWei Liu         }
627f00d4f59SWei Liu     }
628f00d4f59SWei Liu     goto out;
629f00d4f59SWei Liu 
630f00d4f59SWei Liu err_end:
631d815e721SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
632f00d4f59SWei Liu out:
633d815e721SGreg Kurz     close_preserve_errno(dirfd);
634f00d4f59SWei Liu     return err;
635f00d4f59SWei Liu }
636f00d4f59SWei Liu 
637f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
638f00d4f59SWei Liu                        const char *name, FsCred *credp)
639f00d4f59SWei Liu {
640f00d4f59SWei Liu     int err = -1;
6413f3a1699SGreg Kurz     int dirfd;
642f00d4f59SWei Liu 
6437a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6447a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6457a95434eSGreg Kurz         errno = EINVAL;
6467a95434eSGreg Kurz         return -1;
6477a95434eSGreg Kurz     }
6487a95434eSGreg Kurz 
6493f3a1699SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
6503f3a1699SGreg Kurz     if (dirfd == -1) {
6513f3a1699SGreg Kurz         return -1;
6523f3a1699SGreg Kurz     }
653f00d4f59SWei Liu 
6543f3a1699SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
6553f3a1699SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6563f3a1699SGreg Kurz         err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS);
6573f3a1699SGreg Kurz         if (err == -1) {
6583f3a1699SGreg Kurz             goto out;
6593f3a1699SGreg Kurz         }
6603f3a1699SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFDIR;
6613f3a1699SGreg Kurz 
662f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6633f3a1699SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
6643f3a1699SGreg Kurz         } else {
6653f3a1699SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
666f00d4f59SWei Liu         }
667f00d4f59SWei Liu         if (err == -1) {
668f00d4f59SWei Liu             goto err_end;
669f00d4f59SWei Liu         }
6703f3a1699SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
6713f3a1699SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
6723f3a1699SGreg Kurz         err = mkdirat(dirfd, name, credp->fc_mode);
673f00d4f59SWei Liu         if (err == -1) {
674f00d4f59SWei Liu             goto out;
675f00d4f59SWei Liu         }
6763f3a1699SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
677f00d4f59SWei Liu         if (err == -1) {
678f00d4f59SWei Liu             goto err_end;
679f00d4f59SWei Liu         }
680f00d4f59SWei Liu     }
681f00d4f59SWei Liu     goto out;
682f00d4f59SWei Liu 
683f00d4f59SWei Liu err_end:
6843f3a1699SGreg Kurz     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
685f00d4f59SWei Liu out:
6863f3a1699SGreg Kurz     close_preserve_errno(dirfd);
687f00d4f59SWei Liu     return err;
688f00d4f59SWei Liu }
689f00d4f59SWei Liu 
690f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type,
691f00d4f59SWei Liu                        V9fsFidOpenState *fs, struct stat *stbuf)
692f00d4f59SWei Liu {
693f00d4f59SWei Liu     int err, fd;
694f00d4f59SWei Liu 
695f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
696f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
697f00d4f59SWei Liu     } else {
698f00d4f59SWei Liu         fd = fs->fd;
699f00d4f59SWei Liu     }
700f00d4f59SWei Liu 
701f00d4f59SWei Liu     err = fstat(fd, stbuf);
702f00d4f59SWei Liu     if (err) {
703f00d4f59SWei Liu         return err;
704f00d4f59SWei Liu     }
705f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
706f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
707f00d4f59SWei Liu         uid_t tmp_uid;
708f00d4f59SWei Liu         gid_t tmp_gid;
709f00d4f59SWei Liu         mode_t tmp_mode;
710f00d4f59SWei Liu         dev_t tmp_dev;
711f00d4f59SWei Liu 
712f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
713f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
714f00d4f59SWei Liu         }
715f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
716f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
717f00d4f59SWei Liu         }
718f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
719f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
720f00d4f59SWei Liu         }
721f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
722f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
723f00d4f59SWei Liu         }
724f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
725f00d4f59SWei Liu         errno = EOPNOTSUPP;
726f00d4f59SWei Liu         return -1;
727f00d4f59SWei Liu     }
728f00d4f59SWei Liu     return err;
729f00d4f59SWei Liu }
730f00d4f59SWei Liu 
731f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
732f00d4f59SWei Liu                        int flags, FsCred *credp, V9fsFidOpenState *fs)
733f00d4f59SWei Liu {
734f00d4f59SWei Liu     int fd = -1;
735f00d4f59SWei Liu     int err = -1;
736a565fea5SGreg Kurz     int dirfd;
737f00d4f59SWei Liu 
7387a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7397a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7407a95434eSGreg Kurz         errno = EINVAL;
7417a95434eSGreg Kurz         return -1;
7427a95434eSGreg Kurz     }
7437a95434eSGreg Kurz 
744f00d4f59SWei Liu     /*
745f00d4f59SWei Liu      * Mark all the open to not follow symlinks
746f00d4f59SWei Liu      */
747f00d4f59SWei Liu     flags |= O_NOFOLLOW;
748f00d4f59SWei Liu 
749a565fea5SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
750a565fea5SGreg Kurz     if (dirfd == -1) {
751a565fea5SGreg Kurz         return -1;
752a565fea5SGreg Kurz     }
753f00d4f59SWei Liu 
754f00d4f59SWei Liu     /* Determine the security model */
755a565fea5SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
756a565fea5SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
757a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS);
758a565fea5SGreg Kurz         if (fd == -1) {
759a565fea5SGreg Kurz             goto out;
760a565fea5SGreg Kurz         }
761a565fea5SGreg Kurz         credp->fc_mode = credp->fc_mode|S_IFREG;
762f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
763f00d4f59SWei Liu             /* Set cleint credentials in xattr */
764a565fea5SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
765a565fea5SGreg Kurz         } else {
766a565fea5SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
767f00d4f59SWei Liu         }
768f00d4f59SWei Liu         if (err == -1) {
769f00d4f59SWei Liu             goto err_end;
770f00d4f59SWei Liu         }
771f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
772f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
773a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, credp->fc_mode);
774f00d4f59SWei Liu         if (fd == -1) {
775f00d4f59SWei Liu             goto out;
776f00d4f59SWei Liu         }
777a565fea5SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
778f00d4f59SWei Liu         if (err == -1) {
779f00d4f59SWei Liu             goto err_end;
780f00d4f59SWei Liu         }
781f00d4f59SWei Liu     }
782f00d4f59SWei Liu     err = fd;
783f00d4f59SWei Liu     fs->fd = fd;
784f00d4f59SWei Liu     goto out;
785f00d4f59SWei Liu 
786f00d4f59SWei Liu err_end:
787a565fea5SGreg Kurz     unlinkat_preserve_errno(dirfd, name,
788a565fea5SGreg Kurz                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
789a565fea5SGreg Kurz     close_preserve_errno(fd);
790f00d4f59SWei Liu out:
791a565fea5SGreg Kurz     close_preserve_errno(dirfd);
792f00d4f59SWei Liu     return err;
793f00d4f59SWei Liu }
794f00d4f59SWei Liu 
795f00d4f59SWei Liu 
796f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath,
797f00d4f59SWei Liu                          V9fsPath *dir_path, const char *name, FsCred *credp)
798f00d4f59SWei Liu {
799f00d4f59SWei Liu     int err = -1;
80038771613SGreg Kurz     int dirfd;
801f00d4f59SWei Liu 
8027a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8037a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
8047a95434eSGreg Kurz         errno = EINVAL;
8057a95434eSGreg Kurz         return -1;
8067a95434eSGreg Kurz     }
8077a95434eSGreg Kurz 
80838771613SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
80938771613SGreg Kurz     if (dirfd == -1) {
81038771613SGreg Kurz         return -1;
81138771613SGreg Kurz     }
812f00d4f59SWei Liu 
813f00d4f59SWei Liu     /* Determine the security model */
81438771613SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
81538771613SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
81638771613SGreg Kurz         int fd;
81738771613SGreg Kurz         ssize_t oldpath_size, write_size;
81838771613SGreg Kurz 
81938771613SGreg Kurz         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
82038771613SGreg Kurz                          SM_LOCAL_MODE_BITS);
82138771613SGreg Kurz         if (fd == -1) {
82238771613SGreg Kurz             goto out;
82338771613SGreg Kurz         }
82438771613SGreg Kurz         /* Write the oldpath (target) to the file. */
82538771613SGreg Kurz         oldpath_size = strlen(oldpath);
82638771613SGreg Kurz         do {
82738771613SGreg Kurz             write_size = write(fd, (void *)oldpath, oldpath_size);
82838771613SGreg Kurz         } while (write_size == -1 && errno == EINTR);
82938771613SGreg Kurz         close_preserve_errno(fd);
83038771613SGreg Kurz 
83138771613SGreg Kurz         if (write_size != oldpath_size) {
83238771613SGreg Kurz             goto err_end;
83338771613SGreg Kurz         }
83438771613SGreg Kurz         /* Set cleint credentials in symlink's xattr */
83538771613SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFLNK;
83638771613SGreg Kurz 
837f00d4f59SWei Liu         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
83838771613SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
83938771613SGreg Kurz         } else {
84038771613SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
841f00d4f59SWei Liu         }
842f00d4f59SWei Liu         if (err == -1) {
843f00d4f59SWei Liu             goto err_end;
844f00d4f59SWei Liu         }
84538771613SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
84638771613SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
84738771613SGreg Kurz         err = symlinkat(oldpath, dirfd, name);
848f00d4f59SWei Liu         if (err) {
849f00d4f59SWei Liu             goto out;
850f00d4f59SWei Liu         }
85138771613SGreg Kurz         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
85238771613SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
853f00d4f59SWei Liu         if (err == -1) {
854f00d4f59SWei Liu             /*
855f00d4f59SWei Liu              * If we fail to change ownership and if we are
856f00d4f59SWei Liu              * using security model none. Ignore the error
857f00d4f59SWei Liu              */
858f00d4f59SWei Liu             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
859f00d4f59SWei Liu                 goto err_end;
86038771613SGreg Kurz             } else {
861f00d4f59SWei Liu                 err = 0;
862f00d4f59SWei Liu             }
863f00d4f59SWei Liu         }
86438771613SGreg Kurz     }
865f00d4f59SWei Liu     goto out;
866f00d4f59SWei Liu 
867f00d4f59SWei Liu err_end:
86838771613SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
869f00d4f59SWei Liu out:
87038771613SGreg Kurz     close_preserve_errno(dirfd);
871f00d4f59SWei Liu     return err;
872f00d4f59SWei Liu }
873f00d4f59SWei Liu 
874f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath,
875f00d4f59SWei Liu                       V9fsPath *dirpath, const char *name)
876f00d4f59SWei Liu {
877ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
878ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
879ad0b46e6SGreg Kurz     int ret = -1;
880ad0b46e6SGreg Kurz     int odirfd, ndirfd;
881f00d4f59SWei Liu 
8827a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8837a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
8847a95434eSGreg Kurz         errno = EINVAL;
8857a95434eSGreg Kurz         return -1;
8867a95434eSGreg Kurz     }
8877a95434eSGreg Kurz 
888ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
889ad0b46e6SGreg Kurz     if (odirfd == -1) {
8906dd4b1f1SGreg Kurz         goto out;
8916dd4b1f1SGreg Kurz     }
892f00d4f59SWei Liu 
893ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
894ad0b46e6SGreg Kurz     if (ndirfd == -1) {
895ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
896ad0b46e6SGreg Kurz         goto out;
897ad0b46e6SGreg Kurz     }
898ad0b46e6SGreg Kurz 
899ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
900ad0b46e6SGreg Kurz     if (ret < 0) {
901ad0b46e6SGreg Kurz         goto out_close;
902ad0b46e6SGreg Kurz     }
903f00d4f59SWei Liu 
904f00d4f59SWei Liu     /* now link the virtfs_metadata files */
9056dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
906ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
9076dd4b1f1SGreg Kurz 
908ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
909ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
910ad0b46e6SGreg Kurz             goto err_undo_link;
911f00d4f59SWei Liu         }
912ad0b46e6SGreg Kurz 
913ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
914ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
915ad0b46e6SGreg Kurz             goto err;
916ad0b46e6SGreg Kurz         }
917ad0b46e6SGreg Kurz 
918ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
919ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
920ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
921ad0b46e6SGreg Kurz             goto err;
922ad0b46e6SGreg Kurz         }
923ad0b46e6SGreg Kurz 
924ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
925ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
926ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
927f00d4f59SWei Liu         if (ret < 0 && errno != ENOENT) {
928ad0b46e6SGreg Kurz             goto err_undo_link;
929f00d4f59SWei Liu         }
9306dd4b1f1SGreg Kurz 
931ad0b46e6SGreg Kurz         ret = 0;
932f00d4f59SWei Liu     }
933ad0b46e6SGreg Kurz     goto out_close;
934ad0b46e6SGreg Kurz 
935ad0b46e6SGreg Kurz err:
936ad0b46e6SGreg Kurz     ret = -1;
937ad0b46e6SGreg Kurz err_undo_link:
938ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
939ad0b46e6SGreg Kurz out_close:
940ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
941ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
9426dd4b1f1SGreg Kurz out:
943ad0b46e6SGreg Kurz     g_free(oname);
944ad0b46e6SGreg Kurz     g_free(odirpath);
945f00d4f59SWei Liu     return ret;
946f00d4f59SWei Liu }
947f00d4f59SWei Liu 
948f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
949f00d4f59SWei Liu {
950ac125d99SGreg Kurz     int fd, ret;
951f00d4f59SWei Liu 
952ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
953ac125d99SGreg Kurz     if (fd == -1) {
954ac125d99SGreg Kurz         return -1;
955ac125d99SGreg Kurz     }
956ac125d99SGreg Kurz     ret = ftruncate(fd, size);
957ac125d99SGreg Kurz     close_preserve_errno(fd);
958f00d4f59SWei Liu     return ret;
959f00d4f59SWei Liu }
960f00d4f59SWei Liu 
961f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
962f00d4f59SWei Liu {
963d369f207SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
964d369f207SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
965f00d4f59SWei Liu     int ret = -1;
966d369f207SGreg Kurz     int dirfd;
967d369f207SGreg Kurz 
968d369f207SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
969d369f207SGreg Kurz     if (dirfd == -1) {
970d369f207SGreg Kurz         goto out;
971d369f207SGreg Kurz     }
972f00d4f59SWei Liu 
973f00d4f59SWei Liu     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
974f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
975f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_NONE)) {
976d369f207SGreg Kurz         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
977d369f207SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
978f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
979d369f207SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
980f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
981d369f207SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
982f00d4f59SWei Liu     }
983d369f207SGreg Kurz 
984d369f207SGreg Kurz     close_preserve_errno(dirfd);
985d369f207SGreg Kurz out:
986d369f207SGreg Kurz     g_free(name);
987d369f207SGreg Kurz     g_free(dirpath);
988f00d4f59SWei Liu     return ret;
989f00d4f59SWei Liu }
990f00d4f59SWei Liu 
991f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path,
992f00d4f59SWei Liu                            const struct timespec *buf)
993f00d4f59SWei Liu {
994a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
995a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
996a33eda0dSGreg Kurz     int dirfd, ret = -1;
997f00d4f59SWei Liu 
998a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
999a33eda0dSGreg Kurz     if (dirfd == -1) {
1000a33eda0dSGreg Kurz         goto out;
1001a33eda0dSGreg Kurz     }
1002a33eda0dSGreg Kurz 
1003a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1004a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1005a33eda0dSGreg Kurz out:
1006a33eda0dSGreg Kurz     g_free(dirpath);
1007a33eda0dSGreg Kurz     g_free(name);
1008f00d4f59SWei Liu     return ret;
1009f00d4f59SWei Liu }
1010f00d4f59SWei Liu 
1011df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1012df4938a6SGreg Kurz                                  int flags)
1013f00d4f59SWei Liu {
1014df4938a6SGreg Kurz     int ret = -1;
1015f00d4f59SWei Liu 
1016f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1017df4938a6SGreg Kurz         int map_dirfd;
1018df4938a6SGreg Kurz 
10196a87e792SGreg Kurz         /* We need to remove the metadata as well:
10206a87e792SGreg Kurz          * - the metadata directory if we're removing a directory
10216a87e792SGreg Kurz          * - the metadata file in the parent's metadata directory
10226a87e792SGreg Kurz          *
10236a87e792SGreg Kurz          * If any of these are missing (ie, ENOENT) then we're probably
10246a87e792SGreg Kurz          * trying to remove something that wasn't created in mapped-file
10256a87e792SGreg Kurz          * mode. We just ignore the error.
10266a87e792SGreg Kurz          */
1027df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1028df4938a6SGreg Kurz             int fd;
1029df4938a6SGreg Kurz 
1030b003fc0dSGreg Kurz             fd = openat_dir(dirfd, name);
1031df4938a6SGreg Kurz             if (fd == -1) {
1032f00d4f59SWei Liu                 goto err_out;
1033f00d4f59SWei Liu             }
1034df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1035df4938a6SGreg Kurz             close_preserve_errno(fd);
1036df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1037f00d4f59SWei Liu                 goto err_out;
1038f00d4f59SWei Liu             }
1039f00d4f59SWei Liu         }
1040df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
10416a87e792SGreg Kurz         if (map_dirfd != -1) {
1042df4938a6SGreg Kurz             ret = unlinkat(map_dirfd, name, 0);
1043df4938a6SGreg Kurz             close_preserve_errno(map_dirfd);
1044df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
10456a87e792SGreg Kurz                 goto err_out;
10466a87e792SGreg Kurz             }
10476a87e792SGreg Kurz         } else if (errno != ENOENT) {
1048f00d4f59SWei Liu             goto err_out;
1049f00d4f59SWei Liu         }
1050f00d4f59SWei Liu     }
1051f00d4f59SWei Liu 
1052df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1053f00d4f59SWei Liu err_out:
1054df4938a6SGreg Kurz     return ret;
1055df4938a6SGreg Kurz }
1056df4938a6SGreg Kurz 
1057f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path)
1058f00d4f59SWei Liu {
1059f00d4f59SWei Liu     struct stat stbuf;
1060a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1061a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1062a0e640a8SGreg Kurz     int flags = 0;
1063a0e640a8SGreg Kurz     int dirfd;
1064a0e640a8SGreg Kurz     int err = -1;
1065f00d4f59SWei Liu 
1066a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1067b7361d46SGreg Kurz     if (dirfd == -1) {
1068a0e640a8SGreg Kurz         goto out;
1069a0e640a8SGreg Kurz     }
1070a0e640a8SGreg Kurz 
1071a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1072f00d4f59SWei Liu         goto err_out;
1073f00d4f59SWei Liu     }
1074a0e640a8SGreg Kurz 
1075f00d4f59SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
1076a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1077f00d4f59SWei Liu     }
1078f00d4f59SWei Liu 
1079a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
1080f00d4f59SWei Liu err_out:
1081a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1082a0e640a8SGreg Kurz out:
1083a0e640a8SGreg Kurz     g_free(name);
1084a0e640a8SGreg Kurz     g_free(dirpath);
1085f00d4f59SWei Liu     return err;
1086f00d4f59SWei Liu }
1087f00d4f59SWei Liu 
1088f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type,
1089f00d4f59SWei Liu                        V9fsFidOpenState *fs, int datasync)
1090f00d4f59SWei Liu {
1091f00d4f59SWei Liu     int fd;
1092f00d4f59SWei Liu 
1093f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
1094f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
1095f00d4f59SWei Liu     } else {
1096f00d4f59SWei Liu         fd = fs->fd;
1097f00d4f59SWei Liu     }
1098f00d4f59SWei Liu 
1099f00d4f59SWei Liu     if (datasync) {
1100f00d4f59SWei Liu         return qemu_fdatasync(fd);
1101f00d4f59SWei Liu     } else {
1102f00d4f59SWei Liu         return fsync(fd);
1103f00d4f59SWei Liu     }
1104f00d4f59SWei Liu }
1105f00d4f59SWei Liu 
1106f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1107f00d4f59SWei Liu {
110831e51d1cSGreg Kurz     int fd, ret;
1109f00d4f59SWei Liu 
111031e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
111123da0145SGreg Kurz     if (fd == -1) {
111223da0145SGreg Kurz         return -1;
111323da0145SGreg Kurz     }
111431e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
111531e51d1cSGreg Kurz     close_preserve_errno(fd);
1116f00d4f59SWei Liu     return ret;
1117f00d4f59SWei Liu }
1118f00d4f59SWei Liu 
1119f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1120f00d4f59SWei Liu                                const char *name, void *value, size_t size)
1121f00d4f59SWei Liu {
1122f00d4f59SWei Liu     char *path = fs_path->data;
1123f00d4f59SWei Liu 
1124f00d4f59SWei Liu     return v9fs_get_xattr(ctx, path, name, value, size);
1125f00d4f59SWei Liu }
1126f00d4f59SWei Liu 
1127f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1128f00d4f59SWei Liu                                 void *value, size_t size)
1129f00d4f59SWei Liu {
1130f00d4f59SWei Liu     char *path = fs_path->data;
1131f00d4f59SWei Liu 
1132f00d4f59SWei Liu     return v9fs_list_xattr(ctx, path, value, size);
1133f00d4f59SWei Liu }
1134f00d4f59SWei Liu 
1135f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1136f00d4f59SWei Liu                            void *value, size_t size, int flags)
1137f00d4f59SWei Liu {
1138f00d4f59SWei Liu     char *path = fs_path->data;
1139f00d4f59SWei Liu 
1140f00d4f59SWei Liu     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1141f00d4f59SWei Liu }
1142f00d4f59SWei Liu 
1143f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1144f00d4f59SWei Liu                               const char *name)
1145f00d4f59SWei Liu {
1146f00d4f59SWei Liu     char *path = fs_path->data;
1147f00d4f59SWei Liu 
1148f00d4f59SWei Liu     return v9fs_remove_xattr(ctx, path, name);
1149f00d4f59SWei Liu }
1150f00d4f59SWei Liu 
1151f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1152f00d4f59SWei Liu                               const char *name, V9fsPath *target)
1153f00d4f59SWei Liu {
11547a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
11557a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
11567a95434eSGreg Kurz         errno = EINVAL;
11577a95434eSGreg Kurz         return -1;
11587a95434eSGreg Kurz     }
11597a95434eSGreg Kurz 
1160f00d4f59SWei Liu     if (dir_path) {
1161f57f5878SGreg Kurz         if (!strcmp(name, ".")) {
1162f57f5878SGreg Kurz             /* "." relative to "foo/bar" is "foo/bar" */
1163f57f5878SGreg Kurz             v9fs_path_copy(target, dir_path);
1164f57f5878SGreg Kurz         } else if (!strcmp(name, "..")) {
1165f57f5878SGreg Kurz             if (!strcmp(dir_path->data, ".")) {
1166f57f5878SGreg Kurz                 /* ".." relative to the root is "." */
1167f57f5878SGreg Kurz                 v9fs_path_sprintf(target, ".");
11689c6b899fSGreg Kurz             } else {
1169f57f5878SGreg Kurz                 char *tmp = g_path_get_dirname(dir_path->data);
1170f57f5878SGreg Kurz                 /* Symbolic links are resolved by the client. We can assume
1171f57f5878SGreg Kurz                  * that ".." relative to "foo/bar" is equivalent to "foo"
11729c6b899fSGreg Kurz                  */
1173f57f5878SGreg Kurz                 v9fs_path_sprintf(target, "%s", tmp);
1174f57f5878SGreg Kurz                 g_free(tmp);
1175f57f5878SGreg Kurz             }
1176f57f5878SGreg Kurz         } else {
1177f57f5878SGreg Kurz             assert(!strchr(name, '/'));
1178f57f5878SGreg Kurz             v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1179f57f5878SGreg Kurz         }
1180f57f5878SGreg Kurz     } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1181f57f5878SGreg Kurz                !strcmp(name, "..")) {
1182f57f5878SGreg Kurz             /* This is the root fid */
1183f57f5878SGreg Kurz         v9fs_path_sprintf(target, ".");
1184f57f5878SGreg Kurz     } else {
1185f57f5878SGreg Kurz         assert(!strchr(name, '/'));
1186f57f5878SGreg Kurz         v9fs_path_sprintf(target, "./%s", name);
1187f00d4f59SWei Liu     }
1188f00d4f59SWei Liu     return 0;
1189f00d4f59SWei Liu }
1190f00d4f59SWei Liu 
1191f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1192f00d4f59SWei Liu                           const char *old_name, V9fsPath *newdir,
1193f00d4f59SWei Liu                           const char *new_name)
1194f00d4f59SWei Liu {
1195f00d4f59SWei Liu     int ret;
119699f2cf4bSGreg Kurz     int odirfd, ndirfd;
1197f00d4f59SWei Liu 
11987a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
11997a95434eSGreg Kurz         (local_is_mapped_file_metadata(ctx, old_name) ||
12007a95434eSGreg Kurz          local_is_mapped_file_metadata(ctx, new_name))) {
12017a95434eSGreg Kurz         errno = EINVAL;
12027a95434eSGreg Kurz         return -1;
12037a95434eSGreg Kurz     }
12047a95434eSGreg Kurz 
120599f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
120699f2cf4bSGreg Kurz     if (odirfd == -1) {
120799f2cf4bSGreg Kurz         return -1;
120899f2cf4bSGreg Kurz     }
1209f00d4f59SWei Liu 
121099f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
121199f2cf4bSGreg Kurz     if (ndirfd == -1) {
121299f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
121399f2cf4bSGreg Kurz         return -1;
121499f2cf4bSGreg Kurz     }
1215f00d4f59SWei Liu 
121699f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
121799f2cf4bSGreg Kurz     if (ret < 0) {
121899f2cf4bSGreg Kurz         goto out;
121999f2cf4bSGreg Kurz     }
122099f2cf4bSGreg Kurz 
122199f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
122299f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
122399f2cf4bSGreg Kurz 
122499f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
122599f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
122699f2cf4bSGreg Kurz             goto err_undo_rename;
122799f2cf4bSGreg Kurz         }
122899f2cf4bSGreg Kurz 
12296dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
123099f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
123199f2cf4bSGreg Kurz             goto err;
123299f2cf4bSGreg Kurz         }
123399f2cf4bSGreg Kurz 
12346dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
123599f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
123699f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
123799f2cf4bSGreg Kurz             goto err;
123899f2cf4bSGreg Kurz         }
123999f2cf4bSGreg Kurz 
124099f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
124199f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
124299f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
124399f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
124499f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
124599f2cf4bSGreg Kurz             goto err_undo_rename;
124699f2cf4bSGreg Kurz         }
124799f2cf4bSGreg Kurz 
124899f2cf4bSGreg Kurz         ret = 0;
124999f2cf4bSGreg Kurz     }
125099f2cf4bSGreg Kurz     goto out;
125199f2cf4bSGreg Kurz 
125299f2cf4bSGreg Kurz err:
125399f2cf4bSGreg Kurz     ret = -1;
125499f2cf4bSGreg Kurz err_undo_rename:
125599f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
125699f2cf4bSGreg Kurz out:
125799f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
125899f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
1259f00d4f59SWei Liu     return ret;
1260f00d4f59SWei Liu }
1261f00d4f59SWei Liu 
1262d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1263d2767edeSGreg Kurz {
1264d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1265d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1266d2767edeSGreg Kurz }
1267d2767edeSGreg Kurz 
1268d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1269d2767edeSGreg Kurz                         const char *newpath)
1270d2767edeSGreg Kurz {
1271d2767edeSGreg Kurz     int err;
1272d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1273d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1274d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1275d2767edeSGreg Kurz 
1276d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1277d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1278d2767edeSGreg Kurz 
1279d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1280d2767edeSGreg Kurz 
1281d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1282d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1283d2767edeSGreg Kurz     g_free(nname);
1284d2767edeSGreg Kurz     g_free(oname);
1285d2767edeSGreg Kurz 
1286d2767edeSGreg Kurz     return err;
1287d2767edeSGreg Kurz }
1288d2767edeSGreg Kurz 
1289f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1290f00d4f59SWei Liu                           const char *name, int flags)
1291f00d4f59SWei Liu {
1292f00d4f59SWei Liu     int ret;
1293df4938a6SGreg Kurz     int dirfd;
1294f00d4f59SWei Liu 
12957a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12967a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
12977a95434eSGreg Kurz         errno = EINVAL;
12987a95434eSGreg Kurz         return -1;
12997a95434eSGreg Kurz     }
13007a95434eSGreg Kurz 
1301df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1302df4938a6SGreg Kurz     if (dirfd == -1) {
1303df4938a6SGreg Kurz         return -1;
1304df4938a6SGreg Kurz     }
1305f00d4f59SWei Liu 
1306df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1307df4938a6SGreg Kurz     close_preserve_errno(dirfd);
1308f00d4f59SWei Liu     return ret;
1309f00d4f59SWei Liu }
1310f00d4f59SWei Liu 
1311f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1312f00d4f59SWei Liu                                 mode_t st_mode, uint64_t *st_gen)
1313f00d4f59SWei Liu {
1314f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION
1315f00d4f59SWei Liu     int err;
1316f00d4f59SWei Liu     V9fsFidOpenState fid_open;
1317f00d4f59SWei Liu 
1318f00d4f59SWei Liu     /*
1319f00d4f59SWei Liu      * Do not try to open special files like device nodes, fifos etc
1320f00d4f59SWei Liu      * We can get fd for regular files and directories only
1321f00d4f59SWei Liu      */
1322f00d4f59SWei Liu     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1323f00d4f59SWei Liu         errno = ENOTTY;
1324f00d4f59SWei Liu         return -1;
1325f00d4f59SWei Liu     }
1326f00d4f59SWei Liu     err = local_open(ctx, path, O_RDONLY, &fid_open);
1327f00d4f59SWei Liu     if (err < 0) {
1328f00d4f59SWei Liu         return err;
1329f00d4f59SWei Liu     }
1330f00d4f59SWei Liu     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1331f00d4f59SWei Liu     local_close(ctx, &fid_open);
1332f00d4f59SWei Liu     return err;
1333f00d4f59SWei Liu #else
1334f00d4f59SWei Liu     errno = ENOTTY;
1335f00d4f59SWei Liu     return -1;
1336f00d4f59SWei Liu #endif
1337f00d4f59SWei Liu }
1338f00d4f59SWei Liu 
1339f00d4f59SWei Liu static int local_init(FsContext *ctx)
1340f00d4f59SWei Liu {
1341f00d4f59SWei Liu     struct statfs stbuf;
13420e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13430e35a378SGreg Kurz 
13440e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13450e35a378SGreg Kurz     if (data->mountfd == -1) {
13460e35a378SGreg Kurz         goto err;
13470e35a378SGreg Kurz     }
1348f00d4f59SWei Liu 
134900c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
135000c90bd1SGreg Kurz     /*
135100c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
135200c90bd1SGreg Kurz      */
13530e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13540e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13550e35a378SGreg Kurz         goto err;
135600c90bd1SGreg Kurz     }
135700c90bd1SGreg Kurz     switch (stbuf.f_type) {
135800c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
135900c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
136000c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
136100c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
136200c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
136300c90bd1SGreg Kurz         break;
136400c90bd1SGreg Kurz     }
136500c90bd1SGreg Kurz #endif
1366f00d4f59SWei Liu 
1367f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1368f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1369f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1370f00d4f59SWei Liu         ctx->xops = mapped_xattr_ops;
1371f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_NONE) {
1372f00d4f59SWei Liu         ctx->xops = none_xattr_ops;
1373f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1374f00d4f59SWei Liu         /*
1375f00d4f59SWei Liu          * xattr operation for mapped-file and passthrough
1376f00d4f59SWei Liu          * remain same.
1377f00d4f59SWei Liu          */
1378f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1379f00d4f59SWei Liu     }
1380f00d4f59SWei Liu     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
138100c90bd1SGreg Kurz 
13820e35a378SGreg Kurz     ctx->private = data;
138300c90bd1SGreg Kurz     return 0;
13840e35a378SGreg Kurz 
13850e35a378SGreg Kurz err:
13860e35a378SGreg Kurz     g_free(data);
13870e35a378SGreg Kurz     return -1;
1388f00d4f59SWei Liu }
13890e35a378SGreg Kurz 
13900e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13910e35a378SGreg Kurz {
13920e35a378SGreg Kurz     LocalData *data = ctx->private;
13930e35a378SGreg Kurz 
13940e35a378SGreg Kurz     close(data->mountfd);
13950e35a378SGreg Kurz     g_free(data);
1396f00d4f59SWei Liu }
1397f00d4f59SWei Liu 
1398f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1399f00d4f59SWei Liu {
1400f00d4f59SWei Liu     const char *sec_model = qemu_opt_get(opts, "security_model");
1401f00d4f59SWei Liu     const char *path = qemu_opt_get(opts, "path");
1402b8bbdb88SPradeep Jagadeesh     Error *err = NULL;
1403f00d4f59SWei Liu 
1404f00d4f59SWei Liu     if (!sec_model) {
140563325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
140663325b18SGreg Kurz         error_printf("valid options are:"
140763325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1408f00d4f59SWei Liu         return -1;
1409f00d4f59SWei Liu     }
1410f00d4f59SWei Liu 
1411f00d4f59SWei Liu     if (!strcmp(sec_model, "passthrough")) {
1412f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1413f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped") ||
1414f00d4f59SWei Liu                !strcmp(sec_model, "mapped-xattr")) {
1415f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED;
1416f00d4f59SWei Liu     } else if (!strcmp(sec_model, "none")) {
1417f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_NONE;
1418f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped-file")) {
1419f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1420f00d4f59SWei Liu     } else {
142163325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
142263325b18SGreg Kurz         error_printf("valid options are:"
142363325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1424f00d4f59SWei Liu         return -1;
1425f00d4f59SWei Liu     }
1426f00d4f59SWei Liu 
1427f00d4f59SWei Liu     if (!path) {
142863325b18SGreg Kurz         error_report("fsdev: No path specified");
1429f00d4f59SWei Liu         return -1;
1430f00d4f59SWei Liu     }
1431b8bbdb88SPradeep Jagadeesh 
1432b8bbdb88SPradeep Jagadeesh     fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1433b8bbdb88SPradeep Jagadeesh     if (err) {
1434b8bbdb88SPradeep Jagadeesh         error_reportf_err(err, "Throttle configuration is not valid: ");
1435b8bbdb88SPradeep Jagadeesh         return -1;
1436b8bbdb88SPradeep Jagadeesh     }
1437b8bbdb88SPradeep Jagadeesh 
1438f00d4f59SWei Liu     fse->path = g_strdup(path);
1439f00d4f59SWei Liu 
1440f00d4f59SWei Liu     return 0;
1441f00d4f59SWei Liu }
1442f00d4f59SWei Liu 
1443f00d4f59SWei Liu FileOperations local_ops = {
1444f00d4f59SWei Liu     .parse_opts = local_parse_opts,
1445f00d4f59SWei Liu     .init  = local_init,
14460e35a378SGreg Kurz     .cleanup = local_cleanup,
1447f00d4f59SWei Liu     .lstat = local_lstat,
1448f00d4f59SWei Liu     .readlink = local_readlink,
1449f00d4f59SWei Liu     .close = local_close,
1450f00d4f59SWei Liu     .closedir = local_closedir,
1451f00d4f59SWei Liu     .open = local_open,
1452f00d4f59SWei Liu     .opendir = local_opendir,
1453f00d4f59SWei Liu     .rewinddir = local_rewinddir,
1454f00d4f59SWei Liu     .telldir = local_telldir,
1455635324e8SGreg Kurz     .readdir = local_readdir,
1456f00d4f59SWei Liu     .seekdir = local_seekdir,
1457f00d4f59SWei Liu     .preadv = local_preadv,
1458f00d4f59SWei Liu     .pwritev = local_pwritev,
1459f00d4f59SWei Liu     .chmod = local_chmod,
1460f00d4f59SWei Liu     .mknod = local_mknod,
1461f00d4f59SWei Liu     .mkdir = local_mkdir,
1462f00d4f59SWei Liu     .fstat = local_fstat,
1463f00d4f59SWei Liu     .open2 = local_open2,
1464f00d4f59SWei Liu     .symlink = local_symlink,
1465f00d4f59SWei Liu     .link = local_link,
1466f00d4f59SWei Liu     .truncate = local_truncate,
1467f00d4f59SWei Liu     .rename = local_rename,
1468f00d4f59SWei Liu     .chown = local_chown,
1469f00d4f59SWei Liu     .utimensat = local_utimensat,
1470f00d4f59SWei Liu     .remove = local_remove,
1471f00d4f59SWei Liu     .fsync = local_fsync,
1472f00d4f59SWei Liu     .statfs = local_statfs,
1473f00d4f59SWei Liu     .lgetxattr = local_lgetxattr,
1474f00d4f59SWei Liu     .llistxattr = local_llistxattr,
1475f00d4f59SWei Liu     .lsetxattr = local_lsetxattr,
1476f00d4f59SWei Liu     .lremovexattr = local_lremovexattr,
1477f00d4f59SWei Liu     .name_to_path = local_name_to_path,
1478f00d4f59SWei Liu     .renameat  = local_renameat,
1479f00d4f59SWei Liu     .unlinkat = local_unlinkat,
1480f00d4f59SWei Liu };
1481