xref: /openbmc/qemu/hw/9pfs/9p-local.c (revision e3187a45dd02a7490f9191c16527dc28a4ba45b9)
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;
56996a0d76SGreg Kurz 
57996a0d76SGreg Kurz     /* All paths are relative to the path data->mountfd points to */
58996a0d76SGreg Kurz     while (*path == '/') {
59996a0d76SGreg Kurz         path++;
60996a0d76SGreg Kurz     }
61996a0d76SGreg Kurz 
62996a0d76SGreg Kurz     return relative_openat_nofollow(data->mountfd, path, flags, mode);
63996a0d76SGreg Kurz }
64996a0d76SGreg Kurz 
65996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66996a0d76SGreg Kurz {
67996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68996a0d76SGreg Kurz }
69996a0d76SGreg Kurz 
7099f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
7199f2cf4bSGreg Kurz                                     const char *npath)
7299f2cf4bSGreg Kurz {
7399f2cf4bSGreg Kurz     int serrno = errno;
7499f2cf4bSGreg Kurz     renameat(odirfd, opath, ndirfd, npath);
7599f2cf4bSGreg Kurz     errno = serrno;
7699f2cf4bSGreg Kurz }
7799f2cf4bSGreg Kurz 
78ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
79ad0b46e6SGreg Kurz {
80ad0b46e6SGreg Kurz     int serrno = errno;
81ad0b46e6SGreg Kurz     unlinkat(dirfd, path, flags);
82ad0b46e6SGreg Kurz     errno = serrno;
83ad0b46e6SGreg Kurz }
84ad0b46e6SGreg Kurz 
85f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata"
86f00d4f59SWei Liu 
87f00d4f59SWei Liu static char *local_mapped_attr_path(FsContext *ctx, const char *path)
88f00d4f59SWei Liu {
89f00d4f59SWei Liu     int dirlen;
90f00d4f59SWei Liu     const char *name = strrchr(path, '/');
91f00d4f59SWei Liu     if (name) {
92f00d4f59SWei Liu         dirlen = name - path;
93f00d4f59SWei Liu         ++name;
94f00d4f59SWei Liu     } else {
95f00d4f59SWei Liu         name = path;
96f00d4f59SWei Liu         dirlen = 0;
97f00d4f59SWei Liu     }
98f00d4f59SWei Liu     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
99f00d4f59SWei Liu                            dirlen, path, VIRTFS_META_DIR, name);
100f00d4f59SWei Liu }
101f00d4f59SWei Liu 
102f00d4f59SWei Liu static FILE *local_fopen(const char *path, const char *mode)
103f00d4f59SWei Liu {
104f00d4f59SWei Liu     int fd, o_mode = 0;
105f00d4f59SWei Liu     FILE *fp;
106f00d4f59SWei Liu     int flags = O_NOFOLLOW;
107f00d4f59SWei Liu     /*
108f00d4f59SWei Liu      * only supports two modes
109f00d4f59SWei Liu      */
110f00d4f59SWei Liu     if (mode[0] == 'r') {
111f00d4f59SWei Liu         flags |= O_RDONLY;
112f00d4f59SWei Liu     } else if (mode[0] == 'w') {
113f00d4f59SWei Liu         flags |= O_WRONLY | O_TRUNC | O_CREAT;
114f00d4f59SWei Liu         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
115f00d4f59SWei Liu     } else {
116f00d4f59SWei Liu         return NULL;
117f00d4f59SWei Liu     }
118f00d4f59SWei Liu     fd = open(path, flags, o_mode);
119f00d4f59SWei Liu     if (fd == -1) {
120f00d4f59SWei Liu         return NULL;
121f00d4f59SWei Liu     }
122f00d4f59SWei Liu     fp = fdopen(fd, mode);
123f00d4f59SWei Liu     if (!fp) {
124f00d4f59SWei Liu         close(fd);
125f00d4f59SWei Liu     }
126f00d4f59SWei Liu     return fp;
127f00d4f59SWei Liu }
128f00d4f59SWei Liu 
129f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
130f9aef99bSGreg Kurz {
131f9aef99bSGreg Kurz     int fd, o_mode = 0;
132f9aef99bSGreg Kurz     FILE *fp;
133f9aef99bSGreg Kurz     int flags;
134f9aef99bSGreg Kurz     /*
135f9aef99bSGreg Kurz      * only supports two modes
136f9aef99bSGreg Kurz      */
137f9aef99bSGreg Kurz     if (mode[0] == 'r') {
138f9aef99bSGreg Kurz         flags = O_RDONLY;
139f9aef99bSGreg Kurz     } else if (mode[0] == 'w') {
140f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
141f9aef99bSGreg Kurz         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
142f9aef99bSGreg Kurz     } else {
143f9aef99bSGreg Kurz         return NULL;
144f9aef99bSGreg Kurz     }
145f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
146f9aef99bSGreg Kurz     if (fd == -1) {
147f9aef99bSGreg Kurz         return NULL;
148f9aef99bSGreg Kurz     }
149f9aef99bSGreg Kurz     fp = fdopen(fd, mode);
150f9aef99bSGreg Kurz     if (!fp) {
151f9aef99bSGreg Kurz         close(fd);
152f9aef99bSGreg Kurz     }
153f9aef99bSGreg Kurz     return fp;
154f9aef99bSGreg Kurz }
155f9aef99bSGreg Kurz 
156f00d4f59SWei Liu #define ATTR_MAX 100
157f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
158f00d4f59SWei Liu                                    struct stat *stbuf)
159f00d4f59SWei Liu {
160f00d4f59SWei Liu     FILE *fp;
161f00d4f59SWei Liu     char buf[ATTR_MAX];
162f9aef99bSGreg Kurz     int map_dirfd;
163f00d4f59SWei Liu 
16499f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
165f9aef99bSGreg Kurz     if (map_dirfd == -1) {
166f9aef99bSGreg Kurz         return;
167f9aef99bSGreg Kurz     }
168f9aef99bSGreg Kurz 
169f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
170f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
171f00d4f59SWei Liu     if (!fp) {
172f00d4f59SWei Liu         return;
173f00d4f59SWei Liu     }
174f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
175f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
176f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
177f00d4f59SWei Liu             stbuf->st_uid = atoi(buf+11);
178f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
179f00d4f59SWei Liu             stbuf->st_gid = atoi(buf+11);
180f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
181f00d4f59SWei Liu             stbuf->st_mode = atoi(buf+12);
182f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
183f00d4f59SWei Liu             stbuf->st_rdev = atoi(buf+12);
184f00d4f59SWei Liu         }
185f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
186f00d4f59SWei Liu     }
187f00d4f59SWei Liu     fclose(fp);
188f00d4f59SWei Liu }
189f00d4f59SWei Liu 
190f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
191f00d4f59SWei Liu {
192f9aef99bSGreg Kurz     int err = -1;
193f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
194f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
195f9aef99bSGreg Kurz     int dirfd;
196f00d4f59SWei Liu 
197f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
198f9aef99bSGreg Kurz     if (dirfd == -1) {
199f9aef99bSGreg Kurz         goto out;
200f9aef99bSGreg Kurz     }
201f9aef99bSGreg Kurz 
202f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
203f00d4f59SWei Liu     if (err) {
204f00d4f59SWei Liu         goto err_out;
205f00d4f59SWei Liu     }
206f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
207f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
208f00d4f59SWei Liu         uid_t tmp_uid;
209f00d4f59SWei Liu         gid_t tmp_gid;
210f00d4f59SWei Liu         mode_t tmp_mode;
211f00d4f59SWei Liu         dev_t tmp_dev;
212f9aef99bSGreg Kurz 
213f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
214f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
215f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
216f00d4f59SWei Liu         }
217f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
218f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
219f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
220f00d4f59SWei Liu         }
221f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
222f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
223f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
224f00d4f59SWei Liu         }
225f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
226f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
227f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
228f00d4f59SWei Liu         }
229f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
230f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
231f00d4f59SWei Liu     }
232f00d4f59SWei Liu 
233f00d4f59SWei Liu err_out:
234f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
235f9aef99bSGreg Kurz out:
236f9aef99bSGreg Kurz     g_free(name);
237f9aef99bSGreg Kurz     g_free(dirpath);
238f00d4f59SWei Liu     return err;
239f00d4f59SWei Liu }
240f00d4f59SWei Liu 
241f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
242f00d4f59SWei Liu {
243f00d4f59SWei Liu     int err;
244f00d4f59SWei Liu     char *attr_dir;
245f00d4f59SWei Liu     char *tmp_path = g_strdup(path);
246f00d4f59SWei Liu 
247f00d4f59SWei Liu     attr_dir = g_strdup_printf("%s/%s/%s",
248f00d4f59SWei Liu              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
249f00d4f59SWei Liu 
250f00d4f59SWei Liu     err = mkdir(attr_dir, 0700);
251f00d4f59SWei Liu     if (err < 0 && errno == EEXIST) {
252f00d4f59SWei Liu         err = 0;
253f00d4f59SWei Liu     }
254f00d4f59SWei Liu     g_free(attr_dir);
255f00d4f59SWei Liu     g_free(tmp_path);
256f00d4f59SWei Liu     return err;
257f00d4f59SWei Liu }
258f00d4f59SWei Liu 
259f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx,
260f00d4f59SWei Liu                                       const char *path, FsCred *credp)
261f00d4f59SWei Liu {
262f00d4f59SWei Liu     FILE *fp;
263f00d4f59SWei Liu     int ret = 0;
264f00d4f59SWei Liu     char buf[ATTR_MAX];
265f00d4f59SWei Liu     char *attr_path;
266f00d4f59SWei Liu     int uid = -1, gid = -1, mode = -1, rdev = -1;
267f00d4f59SWei Liu 
268f00d4f59SWei Liu     attr_path = local_mapped_attr_path(ctx, path);
269f00d4f59SWei Liu     fp = local_fopen(attr_path, "r");
270f00d4f59SWei Liu     if (!fp) {
271f00d4f59SWei Liu         goto create_map_file;
272f00d4f59SWei Liu     }
273f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
274f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
275f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
276f00d4f59SWei Liu             uid = atoi(buf+11);
277f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
278f00d4f59SWei Liu             gid = atoi(buf+11);
279f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
280f00d4f59SWei Liu             mode = atoi(buf+12);
281f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
282f00d4f59SWei Liu             rdev = atoi(buf+12);
283f00d4f59SWei Liu         }
284f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
285f00d4f59SWei Liu     }
286f00d4f59SWei Liu     fclose(fp);
287f00d4f59SWei Liu     goto update_map_file;
288f00d4f59SWei Liu 
289f00d4f59SWei Liu create_map_file:
290f00d4f59SWei Liu     ret = local_create_mapped_attr_dir(ctx, path);
291f00d4f59SWei Liu     if (ret < 0) {
292f00d4f59SWei Liu         goto err_out;
293f00d4f59SWei Liu     }
294f00d4f59SWei Liu 
295f00d4f59SWei Liu update_map_file:
296f00d4f59SWei Liu     fp = local_fopen(attr_path, "w");
297f00d4f59SWei Liu     if (!fp) {
298f00d4f59SWei Liu         ret = -1;
299f00d4f59SWei Liu         goto err_out;
300f00d4f59SWei Liu     }
301f00d4f59SWei Liu 
302f00d4f59SWei Liu     if (credp->fc_uid != -1) {
303f00d4f59SWei Liu         uid = credp->fc_uid;
304f00d4f59SWei Liu     }
305f00d4f59SWei Liu     if (credp->fc_gid != -1) {
306f00d4f59SWei Liu         gid = credp->fc_gid;
307f00d4f59SWei Liu     }
308f00d4f59SWei Liu     if (credp->fc_mode != -1) {
309f00d4f59SWei Liu         mode = credp->fc_mode;
310f00d4f59SWei Liu     }
311f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
312f00d4f59SWei Liu         rdev = credp->fc_rdev;
313f00d4f59SWei Liu     }
314f00d4f59SWei Liu 
315f00d4f59SWei Liu 
316f00d4f59SWei Liu     if (uid != -1) {
317f00d4f59SWei Liu         fprintf(fp, "virtfs.uid=%d\n", uid);
318f00d4f59SWei Liu     }
319f00d4f59SWei Liu     if (gid != -1) {
320f00d4f59SWei Liu         fprintf(fp, "virtfs.gid=%d\n", gid);
321f00d4f59SWei Liu     }
322f00d4f59SWei Liu     if (mode != -1) {
323f00d4f59SWei Liu         fprintf(fp, "virtfs.mode=%d\n", mode);
324f00d4f59SWei Liu     }
325f00d4f59SWei Liu     if (rdev != -1) {
326f00d4f59SWei Liu         fprintf(fp, "virtfs.rdev=%d\n", rdev);
327f00d4f59SWei Liu     }
328f00d4f59SWei Liu     fclose(fp);
329f00d4f59SWei Liu 
330f00d4f59SWei Liu err_out:
331f00d4f59SWei Liu     g_free(attr_path);
332f00d4f59SWei Liu     return ret;
333f00d4f59SWei Liu }
334f00d4f59SWei Liu 
335f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp)
336f00d4f59SWei Liu {
337f00d4f59SWei Liu     int err;
338f00d4f59SWei Liu 
339f00d4f59SWei Liu     if (credp->fc_uid != -1) {
340f00d4f59SWei Liu         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
341f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
342f00d4f59SWei Liu         if (err) {
343f00d4f59SWei Liu             return err;
344f00d4f59SWei Liu         }
345f00d4f59SWei Liu     }
346f00d4f59SWei Liu     if (credp->fc_gid != -1) {
347f00d4f59SWei Liu         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
348f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.gid", &tmp_gid, 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);
355f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
356f00d4f59SWei Liu         if (err) {
357f00d4f59SWei Liu             return err;
358f00d4f59SWei Liu         }
359f00d4f59SWei Liu     }
360f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
361f00d4f59SWei Liu         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
362f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
363f00d4f59SWei Liu         if (err) {
364f00d4f59SWei Liu             return err;
365f00d4f59SWei Liu         }
366f00d4f59SWei Liu     }
367f00d4f59SWei Liu     return 0;
368f00d4f59SWei Liu }
369f00d4f59SWei Liu 
370*e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
371*e3187a45SGreg Kurz                                         FsCred *credp)
372*e3187a45SGreg Kurz {
373*e3187a45SGreg Kurz     FILE *fp;
374*e3187a45SGreg Kurz     int ret;
375*e3187a45SGreg Kurz     char buf[ATTR_MAX];
376*e3187a45SGreg Kurz     int uid = -1, gid = -1, mode = -1, rdev = -1;
377*e3187a45SGreg Kurz     int map_dirfd;
378*e3187a45SGreg Kurz 
379*e3187a45SGreg Kurz     ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
380*e3187a45SGreg Kurz     if (ret < 0 && errno != EEXIST) {
381*e3187a45SGreg Kurz         return -1;
382*e3187a45SGreg Kurz     }
383*e3187a45SGreg Kurz 
384*e3187a45SGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
385*e3187a45SGreg Kurz     if (map_dirfd == -1) {
386*e3187a45SGreg Kurz         return -1;
387*e3187a45SGreg Kurz     }
388*e3187a45SGreg Kurz 
389*e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
390*e3187a45SGreg Kurz     if (!fp) {
391*e3187a45SGreg Kurz         if (errno == ENOENT) {
392*e3187a45SGreg Kurz             goto update_map_file;
393*e3187a45SGreg Kurz         } else {
394*e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
395*e3187a45SGreg Kurz             return -1;
396*e3187a45SGreg Kurz         }
397*e3187a45SGreg Kurz     }
398*e3187a45SGreg Kurz     memset(buf, 0, ATTR_MAX);
399*e3187a45SGreg Kurz     while (fgets(buf, ATTR_MAX, fp)) {
400*e3187a45SGreg Kurz         if (!strncmp(buf, "virtfs.uid", 10)) {
401*e3187a45SGreg Kurz             uid = atoi(buf + 11);
402*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.gid", 10)) {
403*e3187a45SGreg Kurz             gid = atoi(buf + 11);
404*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.mode", 11)) {
405*e3187a45SGreg Kurz             mode = atoi(buf + 12);
406*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
407*e3187a45SGreg Kurz             rdev = atoi(buf + 12);
408*e3187a45SGreg Kurz         }
409*e3187a45SGreg Kurz         memset(buf, 0, ATTR_MAX);
410*e3187a45SGreg Kurz     }
411*e3187a45SGreg Kurz     fclose(fp);
412*e3187a45SGreg Kurz 
413*e3187a45SGreg Kurz update_map_file:
414*e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "w");
415*e3187a45SGreg Kurz     close_preserve_errno(map_dirfd);
416*e3187a45SGreg Kurz     if (!fp) {
417*e3187a45SGreg Kurz         return -1;
418*e3187a45SGreg Kurz     }
419*e3187a45SGreg Kurz 
420*e3187a45SGreg Kurz     if (credp->fc_uid != -1) {
421*e3187a45SGreg Kurz         uid = credp->fc_uid;
422*e3187a45SGreg Kurz     }
423*e3187a45SGreg Kurz     if (credp->fc_gid != -1) {
424*e3187a45SGreg Kurz         gid = credp->fc_gid;
425*e3187a45SGreg Kurz     }
426*e3187a45SGreg Kurz     if (credp->fc_mode != -1) {
427*e3187a45SGreg Kurz         mode = credp->fc_mode;
428*e3187a45SGreg Kurz     }
429*e3187a45SGreg Kurz     if (credp->fc_rdev != -1) {
430*e3187a45SGreg Kurz         rdev = credp->fc_rdev;
431*e3187a45SGreg Kurz     }
432*e3187a45SGreg Kurz 
433*e3187a45SGreg Kurz     if (uid != -1) {
434*e3187a45SGreg Kurz         fprintf(fp, "virtfs.uid=%d\n", uid);
435*e3187a45SGreg Kurz     }
436*e3187a45SGreg Kurz     if (gid != -1) {
437*e3187a45SGreg Kurz         fprintf(fp, "virtfs.gid=%d\n", gid);
438*e3187a45SGreg Kurz     }
439*e3187a45SGreg Kurz     if (mode != -1) {
440*e3187a45SGreg Kurz         fprintf(fp, "virtfs.mode=%d\n", mode);
441*e3187a45SGreg Kurz     }
442*e3187a45SGreg Kurz     if (rdev != -1) {
443*e3187a45SGreg Kurz         fprintf(fp, "virtfs.rdev=%d\n", rdev);
444*e3187a45SGreg Kurz     }
445*e3187a45SGreg Kurz     fclose(fp);
446*e3187a45SGreg Kurz 
447*e3187a45SGreg Kurz     return 0;
448*e3187a45SGreg Kurz }
449*e3187a45SGreg Kurz 
450*e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
451*e3187a45SGreg Kurz {
452*e3187a45SGreg Kurz     int fd, ret;
453*e3187a45SGreg Kurz 
454*e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
455*e3187a45SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet. As an
456*e3187a45SGreg Kurz      * alternative, let's open the file and use fchmod() instead. This
457*e3187a45SGreg Kurz      * may fail depending on the permissions of the file, but it is the
458*e3187a45SGreg Kurz      * best we can do to avoid TOCTTOU. We first try to open read-only
459*e3187a45SGreg Kurz      * in case name points to a directory. If that fails, we try write-only
460*e3187a45SGreg Kurz      * in case name doesn't point to a directory.
461*e3187a45SGreg Kurz      */
462*e3187a45SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY, 0);
463*e3187a45SGreg Kurz     if (fd == -1) {
464*e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
465*e3187a45SGreg Kurz         if (errno == EACCES) {
466*e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
467*e3187a45SGreg Kurz         }
468*e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
469*e3187a45SGreg Kurz             errno = EACCES;
470*e3187a45SGreg Kurz         }
471*e3187a45SGreg Kurz     }
472*e3187a45SGreg Kurz     if (fd == -1) {
473*e3187a45SGreg Kurz         return -1;
474*e3187a45SGreg Kurz     }
475*e3187a45SGreg Kurz     ret = fchmod(fd, mode);
476*e3187a45SGreg Kurz     close_preserve_errno(fd);
477*e3187a45SGreg Kurz     return ret;
478*e3187a45SGreg Kurz }
479*e3187a45SGreg Kurz 
480*e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
481*e3187a45SGreg Kurz {
482*e3187a45SGreg Kurz     int err;
483*e3187a45SGreg Kurz 
484*e3187a45SGreg Kurz     if (credp->fc_uid != -1) {
485*e3187a45SGreg Kurz         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
486*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
487*e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
488*e3187a45SGreg Kurz         if (err) {
489*e3187a45SGreg Kurz             return err;
490*e3187a45SGreg Kurz         }
491*e3187a45SGreg Kurz     }
492*e3187a45SGreg Kurz     if (credp->fc_gid != -1) {
493*e3187a45SGreg Kurz         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
494*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
495*e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
496*e3187a45SGreg Kurz         if (err) {
497*e3187a45SGreg Kurz             return err;
498*e3187a45SGreg Kurz         }
499*e3187a45SGreg Kurz     }
500*e3187a45SGreg Kurz     if (credp->fc_mode != -1) {
501*e3187a45SGreg Kurz         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
502*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
503*e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
504*e3187a45SGreg Kurz         if (err) {
505*e3187a45SGreg Kurz             return err;
506*e3187a45SGreg Kurz         }
507*e3187a45SGreg Kurz     }
508*e3187a45SGreg Kurz     if (credp->fc_rdev != -1) {
509*e3187a45SGreg Kurz         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
510*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
511*e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
512*e3187a45SGreg Kurz         if (err) {
513*e3187a45SGreg Kurz             return err;
514*e3187a45SGreg Kurz         }
515*e3187a45SGreg Kurz     }
516*e3187a45SGreg Kurz     return 0;
517*e3187a45SGreg Kurz }
518*e3187a45SGreg Kurz 
519f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
520f00d4f59SWei Liu                                          FsCred *credp)
521f00d4f59SWei Liu {
522f00d4f59SWei Liu     char *buffer;
523f00d4f59SWei Liu 
524f00d4f59SWei Liu     buffer = rpath(fs_ctx, path);
525f00d4f59SWei Liu     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
526f00d4f59SWei Liu         /*
527f00d4f59SWei Liu          * If we fail to change ownership and if we are
528f00d4f59SWei Liu          * using security model none. Ignore the error
529f00d4f59SWei Liu          */
530f00d4f59SWei Liu         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
531f00d4f59SWei Liu             goto err;
532f00d4f59SWei Liu         }
533f00d4f59SWei Liu     }
534f00d4f59SWei Liu 
535f00d4f59SWei Liu     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
536f00d4f59SWei Liu         goto err;
537f00d4f59SWei Liu     }
538f00d4f59SWei Liu 
539f00d4f59SWei Liu     g_free(buffer);
540f00d4f59SWei Liu     return 0;
541f00d4f59SWei Liu err:
542f00d4f59SWei Liu     g_free(buffer);
543f00d4f59SWei Liu     return -1;
544f00d4f59SWei Liu }
545f00d4f59SWei Liu 
546f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
547f00d4f59SWei Liu                               char *buf, size_t bufsz)
548f00d4f59SWei Liu {
549f00d4f59SWei Liu     ssize_t tsize = -1;
550f00d4f59SWei Liu 
551f00d4f59SWei Liu     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
552f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
553f00d4f59SWei Liu         int fd;
554bec1e954SGreg Kurz 
555bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
556f00d4f59SWei Liu         if (fd == -1) {
557f00d4f59SWei Liu             return -1;
558f00d4f59SWei Liu         }
559f00d4f59SWei Liu         do {
560f00d4f59SWei Liu             tsize = read(fd, (void *)buf, bufsz);
561f00d4f59SWei Liu         } while (tsize == -1 && errno == EINTR);
562bec1e954SGreg Kurz         close_preserve_errno(fd);
563f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
564f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
565bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
566bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
567bec1e954SGreg Kurz         int dirfd;
568bec1e954SGreg Kurz 
569bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
570bec1e954SGreg Kurz         if (dirfd == -1) {
571bec1e954SGreg Kurz             goto out;
572bec1e954SGreg Kurz         }
573bec1e954SGreg Kurz 
574bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
575bec1e954SGreg Kurz         close_preserve_errno(dirfd);
576bec1e954SGreg Kurz     out:
577bec1e954SGreg Kurz         g_free(name);
578bec1e954SGreg Kurz         g_free(dirpath);
579f00d4f59SWei Liu     }
580f00d4f59SWei Liu     return tsize;
581f00d4f59SWei Liu }
582f00d4f59SWei Liu 
583f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
584f00d4f59SWei Liu {
585f00d4f59SWei Liu     return close(fs->fd);
586f00d4f59SWei Liu }
587f00d4f59SWei Liu 
588f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
589f00d4f59SWei Liu {
590f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
591f00d4f59SWei Liu }
592f00d4f59SWei Liu 
593f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path,
594f00d4f59SWei Liu                       int flags, V9fsFidOpenState *fs)
595f00d4f59SWei Liu {
59621328e1eSGreg Kurz     int fd;
597f00d4f59SWei Liu 
598996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
59921328e1eSGreg Kurz     if (fd == -1) {
60021328e1eSGreg Kurz         return -1;
60121328e1eSGreg Kurz     }
60221328e1eSGreg Kurz     fs->fd = fd;
603f00d4f59SWei Liu     return fs->fd;
604f00d4f59SWei Liu }
605f00d4f59SWei Liu 
606f00d4f59SWei Liu static int local_opendir(FsContext *ctx,
607f00d4f59SWei Liu                          V9fsPath *fs_path, V9fsFidOpenState *fs)
608f00d4f59SWei Liu {
609996a0d76SGreg Kurz     int dirfd;
61021328e1eSGreg Kurz     DIR *stream;
611f00d4f59SWei Liu 
612996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
613996a0d76SGreg Kurz     if (dirfd == -1) {
614996a0d76SGreg Kurz         return -1;
615996a0d76SGreg Kurz     }
616996a0d76SGreg Kurz 
617996a0d76SGreg Kurz     stream = fdopendir(dirfd);
61821328e1eSGreg Kurz     if (!stream) {
619f00d4f59SWei Liu         return -1;
620f00d4f59SWei Liu     }
62121328e1eSGreg Kurz     fs->dir.stream = stream;
622f00d4f59SWei Liu     return 0;
623f00d4f59SWei Liu }
624f00d4f59SWei Liu 
625f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
626f00d4f59SWei Liu {
627f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
628f00d4f59SWei Liu }
629f00d4f59SWei Liu 
630f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
631f00d4f59SWei Liu {
632f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
633f00d4f59SWei Liu }
634f00d4f59SWei Liu 
635635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
636f00d4f59SWei Liu {
637635324e8SGreg Kurz     struct dirent *entry;
638f00d4f59SWei Liu 
639f00d4f59SWei Liu again:
640635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
641635324e8SGreg Kurz     if (!entry) {
642635324e8SGreg Kurz         return NULL;
643635324e8SGreg Kurz     }
644635324e8SGreg Kurz 
645f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED) {
646f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
647f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
648635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
649f00d4f59SWei Liu             /* skp the meta data directory */
650f00d4f59SWei Liu             goto again;
651f00d4f59SWei Liu         }
652f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
653f00d4f59SWei Liu     }
654635324e8SGreg Kurz 
655635324e8SGreg Kurz     return entry;
656f00d4f59SWei Liu }
657f00d4f59SWei Liu 
658f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
659f00d4f59SWei Liu {
660f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
661f00d4f59SWei Liu }
662f00d4f59SWei Liu 
663f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
664f00d4f59SWei Liu                             const struct iovec *iov,
665f00d4f59SWei Liu                             int iovcnt, off_t offset)
666f00d4f59SWei Liu {
667f00d4f59SWei Liu #ifdef CONFIG_PREADV
668f00d4f59SWei Liu     return preadv(fs->fd, iov, iovcnt, offset);
669f00d4f59SWei Liu #else
670f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
671f00d4f59SWei Liu     if (err == -1) {
672f00d4f59SWei Liu         return err;
673f00d4f59SWei Liu     } else {
674f00d4f59SWei Liu         return readv(fs->fd, iov, iovcnt);
675f00d4f59SWei Liu     }
676f00d4f59SWei Liu #endif
677f00d4f59SWei Liu }
678f00d4f59SWei Liu 
679f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
680f00d4f59SWei Liu                              const struct iovec *iov,
681f00d4f59SWei Liu                              int iovcnt, off_t offset)
682f00d4f59SWei Liu {
6836fe76accSGreg Kurz     ssize_t ret;
684f00d4f59SWei Liu #ifdef CONFIG_PREADV
685f00d4f59SWei Liu     ret = pwritev(fs->fd, iov, iovcnt, offset);
686f00d4f59SWei Liu #else
687f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
688f00d4f59SWei Liu     if (err == -1) {
689f00d4f59SWei Liu         return err;
690f00d4f59SWei Liu     } else {
691f00d4f59SWei Liu         ret = writev(fs->fd, iov, iovcnt);
692f00d4f59SWei Liu     }
693f00d4f59SWei Liu #endif
694f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE
695f00d4f59SWei Liu     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
696f00d4f59SWei Liu         /*
697f00d4f59SWei Liu          * Initiate a writeback. This is not a data integrity sync.
698f00d4f59SWei Liu          * We want to ensure that we don't leave dirty pages in the cache
699f00d4f59SWei Liu          * after write when writeout=immediate is sepcified.
700f00d4f59SWei Liu          */
701f00d4f59SWei Liu         sync_file_range(fs->fd, offset, ret,
702f00d4f59SWei Liu                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
703f00d4f59SWei Liu     }
704f00d4f59SWei Liu #endif
705f00d4f59SWei Liu     return ret;
706f00d4f59SWei Liu }
707f00d4f59SWei Liu 
708f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
709f00d4f59SWei Liu {
710*e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
711*e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
712f00d4f59SWei Liu     int ret = -1;
713*e3187a45SGreg Kurz     int dirfd;
714*e3187a45SGreg Kurz 
715*e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
716*e3187a45SGreg Kurz     if (dirfd == -1) {
717*e3187a45SGreg Kurz         goto out;
718*e3187a45SGreg Kurz     }
719f00d4f59SWei Liu 
720f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
721*e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
722f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
723*e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
724*e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
725*e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
726*e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
727f00d4f59SWei Liu     }
728*e3187a45SGreg Kurz     close_preserve_errno(dirfd);
729*e3187a45SGreg Kurz 
730*e3187a45SGreg Kurz out:
731*e3187a45SGreg Kurz     g_free(dirpath);
732*e3187a45SGreg Kurz     g_free(name);
733f00d4f59SWei Liu     return ret;
734f00d4f59SWei Liu }
735f00d4f59SWei Liu 
736f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
737f00d4f59SWei Liu                        const char *name, FsCred *credp)
738f00d4f59SWei Liu {
739f00d4f59SWei Liu     char *path;
740f00d4f59SWei Liu     int err = -1;
741f00d4f59SWei Liu     int serrno = 0;
742f00d4f59SWei Liu     V9fsString fullname;
743f00d4f59SWei Liu     char *buffer = NULL;
744f00d4f59SWei Liu 
745f00d4f59SWei Liu     v9fs_string_init(&fullname);
746f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
747f00d4f59SWei Liu     path = fullname.data;
748f00d4f59SWei Liu 
749f00d4f59SWei Liu     /* Determine the security model */
750f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
751f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
752f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
753f00d4f59SWei Liu         if (err == -1) {
754f00d4f59SWei Liu             goto out;
755f00d4f59SWei Liu         }
756f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
757f00d4f59SWei Liu         if (err == -1) {
758f00d4f59SWei Liu             serrno = errno;
759f00d4f59SWei Liu             goto err_end;
760f00d4f59SWei Liu         }
761f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
762f00d4f59SWei Liu 
763f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
764f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
765f00d4f59SWei Liu         if (err == -1) {
766f00d4f59SWei Liu             goto out;
767f00d4f59SWei Liu         }
768f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
769f00d4f59SWei Liu         if (err == -1) {
770f00d4f59SWei Liu             serrno = errno;
771f00d4f59SWei Liu             goto err_end;
772f00d4f59SWei Liu         }
773f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
774f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
775f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
776f00d4f59SWei Liu         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
777f00d4f59SWei Liu         if (err == -1) {
778f00d4f59SWei Liu             goto out;
779f00d4f59SWei Liu         }
780f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
781f00d4f59SWei Liu         if (err == -1) {
782f00d4f59SWei Liu             serrno = errno;
783f00d4f59SWei Liu             goto err_end;
784f00d4f59SWei Liu         }
785f00d4f59SWei Liu     }
786f00d4f59SWei Liu     goto out;
787f00d4f59SWei Liu 
788f00d4f59SWei Liu err_end:
789f00d4f59SWei Liu     remove(buffer);
790f00d4f59SWei Liu     errno = serrno;
791f00d4f59SWei Liu out:
792f00d4f59SWei Liu     g_free(buffer);
793f00d4f59SWei Liu     v9fs_string_free(&fullname);
794f00d4f59SWei Liu     return err;
795f00d4f59SWei Liu }
796f00d4f59SWei Liu 
797f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
798f00d4f59SWei Liu                        const char *name, FsCred *credp)
799f00d4f59SWei Liu {
800f00d4f59SWei Liu     char *path;
801f00d4f59SWei Liu     int err = -1;
802f00d4f59SWei Liu     int serrno = 0;
803f00d4f59SWei Liu     V9fsString fullname;
804f00d4f59SWei Liu     char *buffer = NULL;
805f00d4f59SWei Liu 
806f00d4f59SWei Liu     v9fs_string_init(&fullname);
807f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
808f00d4f59SWei Liu     path = fullname.data;
809f00d4f59SWei Liu 
810f00d4f59SWei Liu     /* Determine the security model */
811f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
812f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
813f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
814f00d4f59SWei Liu         if (err == -1) {
815f00d4f59SWei Liu             goto out;
816f00d4f59SWei Liu         }
817f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
818f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
819f00d4f59SWei Liu         if (err == -1) {
820f00d4f59SWei Liu             serrno = errno;
821f00d4f59SWei Liu             goto err_end;
822f00d4f59SWei Liu         }
823f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
824f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
825f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
826f00d4f59SWei Liu         if (err == -1) {
827f00d4f59SWei Liu             goto out;
828f00d4f59SWei Liu         }
829f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
830f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
831f00d4f59SWei Liu         if (err == -1) {
832f00d4f59SWei Liu             serrno = errno;
833f00d4f59SWei Liu             goto err_end;
834f00d4f59SWei Liu         }
835f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
836f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
837f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
838f00d4f59SWei Liu         err = mkdir(buffer, credp->fc_mode);
839f00d4f59SWei Liu         if (err == -1) {
840f00d4f59SWei Liu             goto out;
841f00d4f59SWei Liu         }
842f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
843f00d4f59SWei Liu         if (err == -1) {
844f00d4f59SWei Liu             serrno = errno;
845f00d4f59SWei Liu             goto err_end;
846f00d4f59SWei Liu         }
847f00d4f59SWei Liu     }
848f00d4f59SWei Liu     goto out;
849f00d4f59SWei Liu 
850f00d4f59SWei Liu err_end:
851f00d4f59SWei Liu     remove(buffer);
852f00d4f59SWei Liu     errno = serrno;
853f00d4f59SWei Liu out:
854f00d4f59SWei Liu     g_free(buffer);
855f00d4f59SWei Liu     v9fs_string_free(&fullname);
856f00d4f59SWei Liu     return err;
857f00d4f59SWei Liu }
858f00d4f59SWei Liu 
859f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type,
860f00d4f59SWei Liu                        V9fsFidOpenState *fs, struct stat *stbuf)
861f00d4f59SWei Liu {
862f00d4f59SWei Liu     int err, fd;
863f00d4f59SWei Liu 
864f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
865f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
866f00d4f59SWei Liu     } else {
867f00d4f59SWei Liu         fd = fs->fd;
868f00d4f59SWei Liu     }
869f00d4f59SWei Liu 
870f00d4f59SWei Liu     err = fstat(fd, stbuf);
871f00d4f59SWei Liu     if (err) {
872f00d4f59SWei Liu         return err;
873f00d4f59SWei Liu     }
874f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
875f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
876f00d4f59SWei Liu         uid_t tmp_uid;
877f00d4f59SWei Liu         gid_t tmp_gid;
878f00d4f59SWei Liu         mode_t tmp_mode;
879f00d4f59SWei Liu         dev_t tmp_dev;
880f00d4f59SWei Liu 
881f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
882f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
883f00d4f59SWei Liu         }
884f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
885f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
886f00d4f59SWei Liu         }
887f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
888f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
889f00d4f59SWei Liu         }
890f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
891f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
892f00d4f59SWei Liu         }
893f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
894f00d4f59SWei Liu         errno = EOPNOTSUPP;
895f00d4f59SWei Liu         return -1;
896f00d4f59SWei Liu     }
897f00d4f59SWei Liu     return err;
898f00d4f59SWei Liu }
899f00d4f59SWei Liu 
900f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
901f00d4f59SWei Liu                        int flags, FsCred *credp, V9fsFidOpenState *fs)
902f00d4f59SWei Liu {
903f00d4f59SWei Liu     char *path;
904f00d4f59SWei Liu     int fd = -1;
905f00d4f59SWei Liu     int err = -1;
906f00d4f59SWei Liu     int serrno = 0;
907f00d4f59SWei Liu     V9fsString fullname;
908f00d4f59SWei Liu     char *buffer = NULL;
909f00d4f59SWei Liu 
910f00d4f59SWei Liu     /*
911f00d4f59SWei Liu      * Mark all the open to not follow symlinks
912f00d4f59SWei Liu      */
913f00d4f59SWei Liu     flags |= O_NOFOLLOW;
914f00d4f59SWei Liu 
915f00d4f59SWei Liu     v9fs_string_init(&fullname);
916f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
917f00d4f59SWei Liu     path = fullname.data;
918f00d4f59SWei Liu 
919f00d4f59SWei Liu     /* Determine the security model */
920f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
921f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
922f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
923f00d4f59SWei Liu         if (fd == -1) {
924f00d4f59SWei Liu             err = fd;
925f00d4f59SWei Liu             goto out;
926f00d4f59SWei Liu         }
927f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
928f00d4f59SWei Liu         /* Set cleint credentials in xattr */
929f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
930f00d4f59SWei Liu         if (err == -1) {
931f00d4f59SWei Liu             serrno = errno;
932f00d4f59SWei Liu             goto err_end;
933f00d4f59SWei Liu         }
934f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
935f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
936f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
937f00d4f59SWei Liu         if (fd == -1) {
938f00d4f59SWei Liu             err = fd;
939f00d4f59SWei Liu             goto out;
940f00d4f59SWei Liu         }
941f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
942f00d4f59SWei Liu         /* Set client credentials in .virtfs_metadata directory files */
943f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
944f00d4f59SWei Liu         if (err == -1) {
945f00d4f59SWei Liu             serrno = errno;
946f00d4f59SWei Liu             goto err_end;
947f00d4f59SWei Liu         }
948f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
949f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
950f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
951f00d4f59SWei Liu         fd = open(buffer, flags, credp->fc_mode);
952f00d4f59SWei Liu         if (fd == -1) {
953f00d4f59SWei Liu             err = fd;
954f00d4f59SWei Liu             goto out;
955f00d4f59SWei Liu         }
956f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
957f00d4f59SWei Liu         if (err == -1) {
958f00d4f59SWei Liu             serrno = errno;
959f00d4f59SWei Liu             goto err_end;
960f00d4f59SWei Liu         }
961f00d4f59SWei Liu     }
962f00d4f59SWei Liu     err = fd;
963f00d4f59SWei Liu     fs->fd = fd;
964f00d4f59SWei Liu     goto out;
965f00d4f59SWei Liu 
966f00d4f59SWei Liu err_end:
967f00d4f59SWei Liu     close(fd);
968f00d4f59SWei Liu     remove(buffer);
969f00d4f59SWei Liu     errno = serrno;
970f00d4f59SWei Liu out:
971f00d4f59SWei Liu     g_free(buffer);
972f00d4f59SWei Liu     v9fs_string_free(&fullname);
973f00d4f59SWei Liu     return err;
974f00d4f59SWei Liu }
975f00d4f59SWei Liu 
976f00d4f59SWei Liu 
977f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath,
978f00d4f59SWei Liu                          V9fsPath *dir_path, const char *name, FsCred *credp)
979f00d4f59SWei Liu {
980f00d4f59SWei Liu     int err = -1;
981f00d4f59SWei Liu     int serrno = 0;
982f00d4f59SWei Liu     char *newpath;
983f00d4f59SWei Liu     V9fsString fullname;
984f00d4f59SWei Liu     char *buffer = NULL;
985f00d4f59SWei Liu 
986f00d4f59SWei Liu     v9fs_string_init(&fullname);
987f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
988f00d4f59SWei Liu     newpath = fullname.data;
989f00d4f59SWei Liu 
990f00d4f59SWei Liu     /* Determine the security model */
991f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
992f00d4f59SWei Liu         int fd;
993f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
994f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
995f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
996f00d4f59SWei Liu         if (fd == -1) {
997f00d4f59SWei Liu             err = fd;
998f00d4f59SWei Liu             goto out;
999f00d4f59SWei Liu         }
1000f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
1001f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
1002f00d4f59SWei Liu         do {
1003f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
1004f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
1005f00d4f59SWei Liu 
1006f00d4f59SWei Liu         if (write_size != oldpath_size) {
1007f00d4f59SWei Liu             serrno = errno;
1008f00d4f59SWei Liu             close(fd);
1009f00d4f59SWei Liu             err = -1;
1010f00d4f59SWei Liu             goto err_end;
1011f00d4f59SWei Liu         }
1012f00d4f59SWei Liu         close(fd);
1013f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
1014f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
1015f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
1016f00d4f59SWei Liu         if (err == -1) {
1017f00d4f59SWei Liu             serrno = errno;
1018f00d4f59SWei Liu             goto err_end;
1019f00d4f59SWei Liu         }
1020f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1021f00d4f59SWei Liu         int fd;
1022f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
1023f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
1024f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
1025f00d4f59SWei Liu         if (fd == -1) {
1026f00d4f59SWei Liu             err = fd;
1027f00d4f59SWei Liu             goto out;
1028f00d4f59SWei Liu         }
1029f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
1030f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
1031f00d4f59SWei Liu         do {
1032f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
1033f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
1034f00d4f59SWei Liu 
1035f00d4f59SWei Liu         if (write_size != oldpath_size) {
1036f00d4f59SWei Liu             serrno = errno;
1037f00d4f59SWei Liu             close(fd);
1038f00d4f59SWei Liu             err = -1;
1039f00d4f59SWei Liu             goto err_end;
1040f00d4f59SWei Liu         }
1041f00d4f59SWei Liu         close(fd);
1042f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
1043f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
1044f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
1045f00d4f59SWei Liu         if (err == -1) {
1046f00d4f59SWei Liu             serrno = errno;
1047f00d4f59SWei Liu             goto err_end;
1048f00d4f59SWei Liu         }
1049f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1050f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
1051f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
1052f00d4f59SWei Liu         err = symlink(oldpath, buffer);
1053f00d4f59SWei Liu         if (err) {
1054f00d4f59SWei Liu             goto out;
1055f00d4f59SWei Liu         }
1056f00d4f59SWei Liu         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
1057f00d4f59SWei Liu         if (err == -1) {
1058f00d4f59SWei Liu             /*
1059f00d4f59SWei Liu              * If we fail to change ownership and if we are
1060f00d4f59SWei Liu              * using security model none. Ignore the error
1061f00d4f59SWei Liu              */
1062f00d4f59SWei Liu             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1063f00d4f59SWei Liu                 serrno = errno;
1064f00d4f59SWei Liu                 goto err_end;
1065f00d4f59SWei Liu             } else
1066f00d4f59SWei Liu                 err = 0;
1067f00d4f59SWei Liu         }
1068f00d4f59SWei Liu     }
1069f00d4f59SWei Liu     goto out;
1070f00d4f59SWei Liu 
1071f00d4f59SWei Liu err_end:
1072f00d4f59SWei Liu     remove(buffer);
1073f00d4f59SWei Liu     errno = serrno;
1074f00d4f59SWei Liu out:
1075f00d4f59SWei Liu     g_free(buffer);
1076f00d4f59SWei Liu     v9fs_string_free(&fullname);
1077f00d4f59SWei Liu     return err;
1078f00d4f59SWei Liu }
1079f00d4f59SWei Liu 
1080f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath,
1081f00d4f59SWei Liu                       V9fsPath *dirpath, const char *name)
1082f00d4f59SWei Liu {
1083ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
1084ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
1085ad0b46e6SGreg Kurz     int ret = -1;
1086ad0b46e6SGreg Kurz     int odirfd, ndirfd;
1087f00d4f59SWei Liu 
1088ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
1089ad0b46e6SGreg Kurz     if (odirfd == -1) {
10906dd4b1f1SGreg Kurz         goto out;
10916dd4b1f1SGreg Kurz     }
1092f00d4f59SWei Liu 
1093ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
1094ad0b46e6SGreg Kurz     if (ndirfd == -1) {
1095ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
1096ad0b46e6SGreg Kurz         goto out;
1097ad0b46e6SGreg Kurz     }
1098ad0b46e6SGreg Kurz 
1099ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
1100ad0b46e6SGreg Kurz     if (ret < 0) {
1101ad0b46e6SGreg Kurz         goto out_close;
1102ad0b46e6SGreg Kurz     }
1103ad0b46e6SGreg Kurz 
1104f00d4f59SWei Liu     /* now link the virtfs_metadata files */
11056dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1106ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
11076dd4b1f1SGreg Kurz 
1108ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1109ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
1110ad0b46e6SGreg Kurz             goto err_undo_link;
1111f00d4f59SWei Liu         }
1112ad0b46e6SGreg Kurz 
1113ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1114ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
1115ad0b46e6SGreg Kurz             goto err;
1116ad0b46e6SGreg Kurz         }
1117ad0b46e6SGreg Kurz 
1118ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1119ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
1120ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
1121ad0b46e6SGreg Kurz             goto err;
1122ad0b46e6SGreg Kurz         }
1123ad0b46e6SGreg Kurz 
1124ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1125ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
1126ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
1127f00d4f59SWei Liu         if (ret < 0 && errno != ENOENT) {
1128ad0b46e6SGreg Kurz             goto err_undo_link;
1129f00d4f59SWei Liu         }
11306dd4b1f1SGreg Kurz 
1131ad0b46e6SGreg Kurz         ret = 0;
1132ad0b46e6SGreg Kurz     }
1133ad0b46e6SGreg Kurz     goto out_close;
1134ad0b46e6SGreg Kurz 
1135ad0b46e6SGreg Kurz err:
1136ad0b46e6SGreg Kurz     ret = -1;
1137ad0b46e6SGreg Kurz err_undo_link:
1138ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
1139ad0b46e6SGreg Kurz out_close:
1140ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
1141ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
11426dd4b1f1SGreg Kurz out:
1143ad0b46e6SGreg Kurz     g_free(oname);
1144ad0b46e6SGreg Kurz     g_free(odirpath);
1145f00d4f59SWei Liu     return ret;
1146f00d4f59SWei Liu }
1147f00d4f59SWei Liu 
1148f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1149f00d4f59SWei Liu {
1150ac125d99SGreg Kurz     int fd, ret;
1151f00d4f59SWei Liu 
1152ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1153ac125d99SGreg Kurz     if (fd == -1) {
1154ac125d99SGreg Kurz         return -1;
1155ac125d99SGreg Kurz     }
1156ac125d99SGreg Kurz     ret = ftruncate(fd, size);
1157ac125d99SGreg Kurz     close_preserve_errno(fd);
1158f00d4f59SWei Liu     return ret;
1159f00d4f59SWei Liu }
1160f00d4f59SWei Liu 
1161f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1162f00d4f59SWei Liu {
1163f00d4f59SWei Liu     char *buffer;
1164f00d4f59SWei Liu     int ret = -1;
1165f00d4f59SWei Liu     char *path = fs_path->data;
1166f00d4f59SWei Liu 
1167f00d4f59SWei Liu     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1168f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1169f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1170f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
1171f00d4f59SWei Liu         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
1172f00d4f59SWei Liu         g_free(buffer);
1173f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1174f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
1175f00d4f59SWei Liu         ret = local_set_xattr(buffer, credp);
1176f00d4f59SWei Liu         g_free(buffer);
1177f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1178f00d4f59SWei Liu         return local_set_mapped_file_attr(fs_ctx, path, credp);
1179f00d4f59SWei Liu     }
1180f00d4f59SWei Liu     return ret;
1181f00d4f59SWei Liu }
1182f00d4f59SWei Liu 
1183f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1184f00d4f59SWei Liu                            const struct timespec *buf)
1185f00d4f59SWei Liu {
1186a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1187a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1188a33eda0dSGreg Kurz     int dirfd, ret = -1;
1189f00d4f59SWei Liu 
1190a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1191a33eda0dSGreg Kurz     if (dirfd == -1) {
1192a33eda0dSGreg Kurz         goto out;
1193a33eda0dSGreg Kurz     }
1194a33eda0dSGreg Kurz 
1195a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1196a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1197a33eda0dSGreg Kurz out:
1198a33eda0dSGreg Kurz     g_free(dirpath);
1199a33eda0dSGreg Kurz     g_free(name);
1200f00d4f59SWei Liu     return ret;
1201f00d4f59SWei Liu }
1202f00d4f59SWei Liu 
1203df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1204df4938a6SGreg Kurz                                  int flags)
1205df4938a6SGreg Kurz {
1206df4938a6SGreg Kurz     int ret = -1;
1207df4938a6SGreg Kurz 
1208df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1209df4938a6SGreg Kurz         int map_dirfd;
1210df4938a6SGreg Kurz 
1211df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1212df4938a6SGreg Kurz             int fd;
1213df4938a6SGreg Kurz 
1214df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1215df4938a6SGreg Kurz             if (fd == -1) {
1216df4938a6SGreg Kurz                 goto err_out;
1217df4938a6SGreg Kurz             }
1218df4938a6SGreg Kurz             /*
1219df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1220df4938a6SGreg Kurz              * directory
1221df4938a6SGreg Kurz              */
1222df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1223df4938a6SGreg Kurz             close_preserve_errno(fd);
1224df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1225df4938a6SGreg Kurz                 /*
1226df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1227df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1228df4938a6SGreg Kurz                  */
1229df4938a6SGreg Kurz                 goto err_out;
1230df4938a6SGreg Kurz             }
1231df4938a6SGreg Kurz         }
1232df4938a6SGreg Kurz         /*
1233df4938a6SGreg Kurz          * Now remove the name from parent directory
1234df4938a6SGreg Kurz          * .virtfs_metadata directory.
1235df4938a6SGreg Kurz          */
1236df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1237df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1238df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1239df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1240df4938a6SGreg Kurz             /*
1241df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1242df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1243df4938a6SGreg Kurz              */
1244df4938a6SGreg Kurz             goto err_out;
1245df4938a6SGreg Kurz         }
1246df4938a6SGreg Kurz     }
1247df4938a6SGreg Kurz 
1248df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1249df4938a6SGreg Kurz err_out:
1250df4938a6SGreg Kurz     return ret;
1251df4938a6SGreg Kurz }
1252df4938a6SGreg Kurz 
1253f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path)
1254f00d4f59SWei Liu {
1255f00d4f59SWei Liu     struct stat stbuf;
1256a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1257a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1258a0e640a8SGreg Kurz     int flags = 0;
1259a0e640a8SGreg Kurz     int dirfd;
1260a0e640a8SGreg Kurz     int err = -1;
1261f00d4f59SWei Liu 
1262a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1263a0e640a8SGreg Kurz     if (dirfd) {
1264a0e640a8SGreg Kurz         goto out;
1265a0e640a8SGreg Kurz     }
1266a0e640a8SGreg Kurz 
1267a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1268f00d4f59SWei Liu         goto err_out;
1269f00d4f59SWei Liu     }
1270a0e640a8SGreg Kurz 
1271f00d4f59SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
1272a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1273f00d4f59SWei Liu     }
1274f00d4f59SWei Liu 
1275a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
1276f00d4f59SWei Liu err_out:
1277a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1278a0e640a8SGreg Kurz out:
1279a0e640a8SGreg Kurz     g_free(name);
1280a0e640a8SGreg Kurz     g_free(dirpath);
1281f00d4f59SWei Liu     return err;
1282f00d4f59SWei Liu }
1283f00d4f59SWei Liu 
1284f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type,
1285f00d4f59SWei Liu                        V9fsFidOpenState *fs, int datasync)
1286f00d4f59SWei Liu {
1287f00d4f59SWei Liu     int fd;
1288f00d4f59SWei Liu 
1289f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
1290f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
1291f00d4f59SWei Liu     } else {
1292f00d4f59SWei Liu         fd = fs->fd;
1293f00d4f59SWei Liu     }
1294f00d4f59SWei Liu 
1295f00d4f59SWei Liu     if (datasync) {
1296f00d4f59SWei Liu         return qemu_fdatasync(fd);
1297f00d4f59SWei Liu     } else {
1298f00d4f59SWei Liu         return fsync(fd);
1299f00d4f59SWei Liu     }
1300f00d4f59SWei Liu }
1301f00d4f59SWei Liu 
1302f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1303f00d4f59SWei Liu {
130431e51d1cSGreg Kurz     int fd, ret;
1305f00d4f59SWei Liu 
130631e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
130731e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
130831e51d1cSGreg Kurz     close_preserve_errno(fd);
1309f00d4f59SWei Liu     return ret;
1310f00d4f59SWei Liu }
1311f00d4f59SWei Liu 
1312f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1313f00d4f59SWei Liu                                const char *name, void *value, size_t size)
1314f00d4f59SWei Liu {
1315f00d4f59SWei Liu     char *path = fs_path->data;
1316f00d4f59SWei Liu 
1317f00d4f59SWei Liu     return v9fs_get_xattr(ctx, path, name, value, size);
1318f00d4f59SWei Liu }
1319f00d4f59SWei Liu 
1320f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1321f00d4f59SWei Liu                                 void *value, size_t size)
1322f00d4f59SWei Liu {
1323f00d4f59SWei Liu     char *path = fs_path->data;
1324f00d4f59SWei Liu 
1325f00d4f59SWei Liu     return v9fs_list_xattr(ctx, path, value, size);
1326f00d4f59SWei Liu }
1327f00d4f59SWei Liu 
1328f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1329f00d4f59SWei Liu                            void *value, size_t size, int flags)
1330f00d4f59SWei Liu {
1331f00d4f59SWei Liu     char *path = fs_path->data;
1332f00d4f59SWei Liu 
1333f00d4f59SWei Liu     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1334f00d4f59SWei Liu }
1335f00d4f59SWei Liu 
1336f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1337f00d4f59SWei Liu                               const char *name)
1338f00d4f59SWei Liu {
1339f00d4f59SWei Liu     char *path = fs_path->data;
1340f00d4f59SWei Liu 
1341f00d4f59SWei Liu     return v9fs_remove_xattr(ctx, path, name);
1342f00d4f59SWei Liu }
1343f00d4f59SWei Liu 
1344f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1345f00d4f59SWei Liu                               const char *name, V9fsPath *target)
1346f00d4f59SWei Liu {
1347f00d4f59SWei Liu     if (dir_path) {
1348e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1349f00d4f59SWei Liu     } else {
1350e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
1351f00d4f59SWei Liu     }
1352f00d4f59SWei Liu     return 0;
1353f00d4f59SWei Liu }
1354f00d4f59SWei Liu 
1355f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1356f00d4f59SWei Liu                           const char *old_name, V9fsPath *newdir,
1357f00d4f59SWei Liu                           const char *new_name)
1358f00d4f59SWei Liu {
1359f00d4f59SWei Liu     int ret;
136099f2cf4bSGreg Kurz     int odirfd, ndirfd;
1361f00d4f59SWei Liu 
136299f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
136399f2cf4bSGreg Kurz     if (odirfd == -1) {
136499f2cf4bSGreg Kurz         return -1;
136599f2cf4bSGreg Kurz     }
1366f00d4f59SWei Liu 
136799f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
136899f2cf4bSGreg Kurz     if (ndirfd == -1) {
136999f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
137099f2cf4bSGreg Kurz         return -1;
137199f2cf4bSGreg Kurz     }
1372f00d4f59SWei Liu 
137399f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
137499f2cf4bSGreg Kurz     if (ret < 0) {
137599f2cf4bSGreg Kurz         goto out;
137699f2cf4bSGreg Kurz     }
137799f2cf4bSGreg Kurz 
137899f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
137999f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
138099f2cf4bSGreg Kurz 
138199f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
138299f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
138399f2cf4bSGreg Kurz             goto err_undo_rename;
138499f2cf4bSGreg Kurz         }
138599f2cf4bSGreg Kurz 
13866dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
138799f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
138899f2cf4bSGreg Kurz             goto err;
138999f2cf4bSGreg Kurz         }
139099f2cf4bSGreg Kurz 
13916dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
139299f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
139399f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
139499f2cf4bSGreg Kurz             goto err;
139599f2cf4bSGreg Kurz         }
139699f2cf4bSGreg Kurz 
139799f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
139899f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
139999f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
140099f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
140199f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
140299f2cf4bSGreg Kurz             goto err_undo_rename;
140399f2cf4bSGreg Kurz         }
140499f2cf4bSGreg Kurz 
140599f2cf4bSGreg Kurz         ret = 0;
140699f2cf4bSGreg Kurz     }
140799f2cf4bSGreg Kurz     goto out;
140899f2cf4bSGreg Kurz 
140999f2cf4bSGreg Kurz err:
141099f2cf4bSGreg Kurz     ret = -1;
141199f2cf4bSGreg Kurz err_undo_rename:
141299f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
141399f2cf4bSGreg Kurz out:
141499f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
141599f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
1416f00d4f59SWei Liu     return ret;
1417f00d4f59SWei Liu }
1418f00d4f59SWei Liu 
1419d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1420d2767edeSGreg Kurz {
1421d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1422d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1423d2767edeSGreg Kurz }
1424d2767edeSGreg Kurz 
1425d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1426d2767edeSGreg Kurz                         const char *newpath)
1427d2767edeSGreg Kurz {
1428d2767edeSGreg Kurz     int err;
1429d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1430d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1431d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1432d2767edeSGreg Kurz 
1433d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1434d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1435d2767edeSGreg Kurz 
1436d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1437d2767edeSGreg Kurz 
1438d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1439d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1440d2767edeSGreg Kurz     g_free(nname);
1441d2767edeSGreg Kurz     g_free(oname);
1442d2767edeSGreg Kurz 
1443d2767edeSGreg Kurz     return err;
1444d2767edeSGreg Kurz }
1445d2767edeSGreg Kurz 
1446f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1447f00d4f59SWei Liu                           const char *name, int flags)
1448f00d4f59SWei Liu {
1449f00d4f59SWei Liu     int ret;
1450df4938a6SGreg Kurz     int dirfd;
1451f00d4f59SWei Liu 
1452df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1453df4938a6SGreg Kurz     if (dirfd == -1) {
1454df4938a6SGreg Kurz         return -1;
1455df4938a6SGreg Kurz     }
1456f00d4f59SWei Liu 
1457df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1458df4938a6SGreg Kurz     close_preserve_errno(dirfd);
1459f00d4f59SWei Liu     return ret;
1460f00d4f59SWei Liu }
1461f00d4f59SWei Liu 
1462f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1463f00d4f59SWei Liu                                 mode_t st_mode, uint64_t *st_gen)
1464f00d4f59SWei Liu {
1465f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION
1466f00d4f59SWei Liu     int err;
1467f00d4f59SWei Liu     V9fsFidOpenState fid_open;
1468f00d4f59SWei Liu 
1469f00d4f59SWei Liu     /*
1470f00d4f59SWei Liu      * Do not try to open special files like device nodes, fifos etc
1471f00d4f59SWei Liu      * We can get fd for regular files and directories only
1472f00d4f59SWei Liu      */
1473f00d4f59SWei Liu     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1474f00d4f59SWei Liu         errno = ENOTTY;
1475f00d4f59SWei Liu         return -1;
1476f00d4f59SWei Liu     }
1477f00d4f59SWei Liu     err = local_open(ctx, path, O_RDONLY, &fid_open);
1478f00d4f59SWei Liu     if (err < 0) {
1479f00d4f59SWei Liu         return err;
1480f00d4f59SWei Liu     }
1481f00d4f59SWei Liu     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1482f00d4f59SWei Liu     local_close(ctx, &fid_open);
1483f00d4f59SWei Liu     return err;
1484f00d4f59SWei Liu #else
1485f00d4f59SWei Liu     errno = ENOTTY;
1486f00d4f59SWei Liu     return -1;
1487f00d4f59SWei Liu #endif
1488f00d4f59SWei Liu }
1489f00d4f59SWei Liu 
1490f00d4f59SWei Liu static int local_init(FsContext *ctx)
1491f00d4f59SWei Liu {
1492f00d4f59SWei Liu     struct statfs stbuf;
14930e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
14940e35a378SGreg Kurz 
14950e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
14960e35a378SGreg Kurz     if (data->mountfd == -1) {
14970e35a378SGreg Kurz         goto err;
14980e35a378SGreg Kurz     }
1499f00d4f59SWei Liu 
150000c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
150100c90bd1SGreg Kurz     /*
150200c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
150300c90bd1SGreg Kurz      */
15040e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
15050e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
15060e35a378SGreg Kurz         goto err;
150700c90bd1SGreg Kurz     }
150800c90bd1SGreg Kurz     switch (stbuf.f_type) {
150900c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
151000c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
151100c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
151200c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
151300c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
151400c90bd1SGreg Kurz         break;
151500c90bd1SGreg Kurz     }
151600c90bd1SGreg Kurz #endif
151700c90bd1SGreg Kurz 
1518f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1519f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1520f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1521f00d4f59SWei Liu         ctx->xops = mapped_xattr_ops;
1522f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_NONE) {
1523f00d4f59SWei Liu         ctx->xops = none_xattr_ops;
1524f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1525f00d4f59SWei Liu         /*
1526f00d4f59SWei Liu          * xattr operation for mapped-file and passthrough
1527f00d4f59SWei Liu          * remain same.
1528f00d4f59SWei Liu          */
1529f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1530f00d4f59SWei Liu     }
1531f00d4f59SWei Liu     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
153200c90bd1SGreg Kurz 
15330e35a378SGreg Kurz     ctx->private = data;
153400c90bd1SGreg Kurz     return 0;
15350e35a378SGreg Kurz 
15360e35a378SGreg Kurz err:
15370e35a378SGreg Kurz     g_free(data);
15380e35a378SGreg Kurz     return -1;
15390e35a378SGreg Kurz }
15400e35a378SGreg Kurz 
15410e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
15420e35a378SGreg Kurz {
15430e35a378SGreg Kurz     LocalData *data = ctx->private;
15440e35a378SGreg Kurz 
15450e35a378SGreg Kurz     close(data->mountfd);
15460e35a378SGreg Kurz     g_free(data);
1547f00d4f59SWei Liu }
1548f00d4f59SWei Liu 
1549f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1550f00d4f59SWei Liu {
1551f00d4f59SWei Liu     const char *sec_model = qemu_opt_get(opts, "security_model");
1552f00d4f59SWei Liu     const char *path = qemu_opt_get(opts, "path");
1553f00d4f59SWei Liu 
1554f00d4f59SWei Liu     if (!sec_model) {
155563325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
155663325b18SGreg Kurz         error_printf("valid options are:"
155763325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1558f00d4f59SWei Liu         return -1;
1559f00d4f59SWei Liu     }
1560f00d4f59SWei Liu 
1561f00d4f59SWei Liu     if (!strcmp(sec_model, "passthrough")) {
1562f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1563f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped") ||
1564f00d4f59SWei Liu                !strcmp(sec_model, "mapped-xattr")) {
1565f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED;
1566f00d4f59SWei Liu     } else if (!strcmp(sec_model, "none")) {
1567f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_NONE;
1568f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped-file")) {
1569f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1570f00d4f59SWei Liu     } else {
157163325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
157263325b18SGreg Kurz         error_printf("valid options are:"
157363325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1574f00d4f59SWei Liu         return -1;
1575f00d4f59SWei Liu     }
1576f00d4f59SWei Liu 
1577f00d4f59SWei Liu     if (!path) {
157863325b18SGreg Kurz         error_report("fsdev: No path specified");
1579f00d4f59SWei Liu         return -1;
1580f00d4f59SWei Liu     }
1581f00d4f59SWei Liu     fse->path = g_strdup(path);
1582f00d4f59SWei Liu 
1583f00d4f59SWei Liu     return 0;
1584f00d4f59SWei Liu }
1585f00d4f59SWei Liu 
1586f00d4f59SWei Liu FileOperations local_ops = {
1587f00d4f59SWei Liu     .parse_opts = local_parse_opts,
1588f00d4f59SWei Liu     .init  = local_init,
15890e35a378SGreg Kurz     .cleanup = local_cleanup,
1590f00d4f59SWei Liu     .lstat = local_lstat,
1591f00d4f59SWei Liu     .readlink = local_readlink,
1592f00d4f59SWei Liu     .close = local_close,
1593f00d4f59SWei Liu     .closedir = local_closedir,
1594f00d4f59SWei Liu     .open = local_open,
1595f00d4f59SWei Liu     .opendir = local_opendir,
1596f00d4f59SWei Liu     .rewinddir = local_rewinddir,
1597f00d4f59SWei Liu     .telldir = local_telldir,
1598635324e8SGreg Kurz     .readdir = local_readdir,
1599f00d4f59SWei Liu     .seekdir = local_seekdir,
1600f00d4f59SWei Liu     .preadv = local_preadv,
1601f00d4f59SWei Liu     .pwritev = local_pwritev,
1602f00d4f59SWei Liu     .chmod = local_chmod,
1603f00d4f59SWei Liu     .mknod = local_mknod,
1604f00d4f59SWei Liu     .mkdir = local_mkdir,
1605f00d4f59SWei Liu     .fstat = local_fstat,
1606f00d4f59SWei Liu     .open2 = local_open2,
1607f00d4f59SWei Liu     .symlink = local_symlink,
1608f00d4f59SWei Liu     .link = local_link,
1609f00d4f59SWei Liu     .truncate = local_truncate,
1610f00d4f59SWei Liu     .rename = local_rename,
1611f00d4f59SWei Liu     .chown = local_chown,
1612f00d4f59SWei Liu     .utimensat = local_utimensat,
1613f00d4f59SWei Liu     .remove = local_remove,
1614f00d4f59SWei Liu     .fsync = local_fsync,
1615f00d4f59SWei Liu     .statfs = local_statfs,
1616f00d4f59SWei Liu     .lgetxattr = local_lgetxattr,
1617f00d4f59SWei Liu     .llistxattr = local_llistxattr,
1618f00d4f59SWei Liu     .lsetxattr = local_lsetxattr,
1619f00d4f59SWei Liu     .lremovexattr = local_lremovexattr,
1620f00d4f59SWei Liu     .name_to_path = local_name_to_path,
1621f00d4f59SWei Liu     .renameat  = local_renameat,
1622f00d4f59SWei Liu     .unlinkat = local_unlinkat,
1623f00d4f59SWei Liu };
1624