xref: /openbmc/qemu/hw/9pfs/9p-local.c (revision 6dd4b1f1d026e478d9177b28169b377e212400f3)
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 
78f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata"
79f00d4f59SWei Liu 
80f00d4f59SWei Liu static char *local_mapped_attr_path(FsContext *ctx, const char *path)
81f00d4f59SWei Liu {
82f00d4f59SWei Liu     int dirlen;
83f00d4f59SWei Liu     const char *name = strrchr(path, '/');
84f00d4f59SWei Liu     if (name) {
85f00d4f59SWei Liu         dirlen = name - path;
86f00d4f59SWei Liu         ++name;
87f00d4f59SWei Liu     } else {
88f00d4f59SWei Liu         name = path;
89f00d4f59SWei Liu         dirlen = 0;
90f00d4f59SWei Liu     }
91f00d4f59SWei Liu     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
92f00d4f59SWei Liu                            dirlen, path, VIRTFS_META_DIR, name);
93f00d4f59SWei Liu }
94f00d4f59SWei Liu 
95f00d4f59SWei Liu static FILE *local_fopen(const char *path, const char *mode)
96f00d4f59SWei Liu {
97f00d4f59SWei Liu     int fd, o_mode = 0;
98f00d4f59SWei Liu     FILE *fp;
99f00d4f59SWei Liu     int flags = O_NOFOLLOW;
100f00d4f59SWei Liu     /*
101f00d4f59SWei Liu      * only supports two modes
102f00d4f59SWei Liu      */
103f00d4f59SWei Liu     if (mode[0] == 'r') {
104f00d4f59SWei Liu         flags |= O_RDONLY;
105f00d4f59SWei Liu     } else if (mode[0] == 'w') {
106f00d4f59SWei Liu         flags |= O_WRONLY | O_TRUNC | O_CREAT;
107f00d4f59SWei Liu         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
108f00d4f59SWei Liu     } else {
109f00d4f59SWei Liu         return NULL;
110f00d4f59SWei Liu     }
111f00d4f59SWei Liu     fd = open(path, flags, o_mode);
112f00d4f59SWei Liu     if (fd == -1) {
113f00d4f59SWei Liu         return NULL;
114f00d4f59SWei Liu     }
115f00d4f59SWei Liu     fp = fdopen(fd, mode);
116f00d4f59SWei Liu     if (!fp) {
117f00d4f59SWei Liu         close(fd);
118f00d4f59SWei Liu     }
119f00d4f59SWei Liu     return fp;
120f00d4f59SWei Liu }
121f00d4f59SWei Liu 
122f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
123f9aef99bSGreg Kurz {
124f9aef99bSGreg Kurz     int fd, o_mode = 0;
125f9aef99bSGreg Kurz     FILE *fp;
126f9aef99bSGreg Kurz     int flags;
127f9aef99bSGreg Kurz     /*
128f9aef99bSGreg Kurz      * only supports two modes
129f9aef99bSGreg Kurz      */
130f9aef99bSGreg Kurz     if (mode[0] == 'r') {
131f9aef99bSGreg Kurz         flags = O_RDONLY;
132f9aef99bSGreg Kurz     } else if (mode[0] == 'w') {
133f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
134f9aef99bSGreg Kurz         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
135f9aef99bSGreg Kurz     } else {
136f9aef99bSGreg Kurz         return NULL;
137f9aef99bSGreg Kurz     }
138f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
139f9aef99bSGreg Kurz     if (fd == -1) {
140f9aef99bSGreg Kurz         return NULL;
141f9aef99bSGreg Kurz     }
142f9aef99bSGreg Kurz     fp = fdopen(fd, mode);
143f9aef99bSGreg Kurz     if (!fp) {
144f9aef99bSGreg Kurz         close(fd);
145f9aef99bSGreg Kurz     }
146f9aef99bSGreg Kurz     return fp;
147f9aef99bSGreg Kurz }
148f9aef99bSGreg Kurz 
149f00d4f59SWei Liu #define ATTR_MAX 100
150f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
151f00d4f59SWei Liu                                    struct stat *stbuf)
152f00d4f59SWei Liu {
153f00d4f59SWei Liu     FILE *fp;
154f00d4f59SWei Liu     char buf[ATTR_MAX];
155f9aef99bSGreg Kurz     int map_dirfd;
156f00d4f59SWei Liu 
15799f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
158f9aef99bSGreg Kurz     if (map_dirfd == -1) {
159f9aef99bSGreg Kurz         return;
160f9aef99bSGreg Kurz     }
161f9aef99bSGreg Kurz 
162f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
163f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
164f00d4f59SWei Liu     if (!fp) {
165f00d4f59SWei Liu         return;
166f00d4f59SWei Liu     }
167f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
168f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
169f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
170f00d4f59SWei Liu             stbuf->st_uid = atoi(buf+11);
171f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
172f00d4f59SWei Liu             stbuf->st_gid = atoi(buf+11);
173f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
174f00d4f59SWei Liu             stbuf->st_mode = atoi(buf+12);
175f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
176f00d4f59SWei Liu             stbuf->st_rdev = atoi(buf+12);
177f00d4f59SWei Liu         }
178f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
179f00d4f59SWei Liu     }
180f00d4f59SWei Liu     fclose(fp);
181f00d4f59SWei Liu }
182f00d4f59SWei Liu 
183f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
184f00d4f59SWei Liu {
185f9aef99bSGreg Kurz     int err = -1;
186f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
187f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
188f9aef99bSGreg Kurz     int dirfd;
189f00d4f59SWei Liu 
190f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
191f9aef99bSGreg Kurz     if (dirfd == -1) {
192f9aef99bSGreg Kurz         goto out;
193f9aef99bSGreg Kurz     }
194f9aef99bSGreg Kurz 
195f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
196f00d4f59SWei Liu     if (err) {
197f00d4f59SWei Liu         goto err_out;
198f00d4f59SWei Liu     }
199f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
200f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
201f00d4f59SWei Liu         uid_t tmp_uid;
202f00d4f59SWei Liu         gid_t tmp_gid;
203f00d4f59SWei Liu         mode_t tmp_mode;
204f00d4f59SWei Liu         dev_t tmp_dev;
205f9aef99bSGreg Kurz 
206f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
207f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
208f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
209f00d4f59SWei Liu         }
210f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
211f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
212f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
213f00d4f59SWei Liu         }
214f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
215f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
216f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
217f00d4f59SWei Liu         }
218f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
219f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
220f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
221f00d4f59SWei Liu         }
222f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
223f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
224f00d4f59SWei Liu     }
225f00d4f59SWei Liu 
226f00d4f59SWei Liu err_out:
227f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
228f9aef99bSGreg Kurz out:
229f9aef99bSGreg Kurz     g_free(name);
230f9aef99bSGreg Kurz     g_free(dirpath);
231f00d4f59SWei Liu     return err;
232f00d4f59SWei Liu }
233f00d4f59SWei Liu 
234f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
235f00d4f59SWei Liu {
236f00d4f59SWei Liu     int err;
237f00d4f59SWei Liu     char *attr_dir;
238f00d4f59SWei Liu     char *tmp_path = g_strdup(path);
239f00d4f59SWei Liu 
240f00d4f59SWei Liu     attr_dir = g_strdup_printf("%s/%s/%s",
241f00d4f59SWei Liu              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
242f00d4f59SWei Liu 
243f00d4f59SWei Liu     err = mkdir(attr_dir, 0700);
244f00d4f59SWei Liu     if (err < 0 && errno == EEXIST) {
245f00d4f59SWei Liu         err = 0;
246f00d4f59SWei Liu     }
247f00d4f59SWei Liu     g_free(attr_dir);
248f00d4f59SWei Liu     g_free(tmp_path);
249f00d4f59SWei Liu     return err;
250f00d4f59SWei Liu }
251f00d4f59SWei Liu 
252f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx,
253f00d4f59SWei Liu                                       const char *path, FsCred *credp)
254f00d4f59SWei Liu {
255f00d4f59SWei Liu     FILE *fp;
256f00d4f59SWei Liu     int ret = 0;
257f00d4f59SWei Liu     char buf[ATTR_MAX];
258f00d4f59SWei Liu     char *attr_path;
259f00d4f59SWei Liu     int uid = -1, gid = -1, mode = -1, rdev = -1;
260f00d4f59SWei Liu 
261f00d4f59SWei Liu     attr_path = local_mapped_attr_path(ctx, path);
262f00d4f59SWei Liu     fp = local_fopen(attr_path, "r");
263f00d4f59SWei Liu     if (!fp) {
264f00d4f59SWei Liu         goto create_map_file;
265f00d4f59SWei Liu     }
266f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
267f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
268f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
269f00d4f59SWei Liu             uid = atoi(buf+11);
270f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
271f00d4f59SWei Liu             gid = atoi(buf+11);
272f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
273f00d4f59SWei Liu             mode = atoi(buf+12);
274f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
275f00d4f59SWei Liu             rdev = atoi(buf+12);
276f00d4f59SWei Liu         }
277f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
278f00d4f59SWei Liu     }
279f00d4f59SWei Liu     fclose(fp);
280f00d4f59SWei Liu     goto update_map_file;
281f00d4f59SWei Liu 
282f00d4f59SWei Liu create_map_file:
283f00d4f59SWei Liu     ret = local_create_mapped_attr_dir(ctx, path);
284f00d4f59SWei Liu     if (ret < 0) {
285f00d4f59SWei Liu         goto err_out;
286f00d4f59SWei Liu     }
287f00d4f59SWei Liu 
288f00d4f59SWei Liu update_map_file:
289f00d4f59SWei Liu     fp = local_fopen(attr_path, "w");
290f00d4f59SWei Liu     if (!fp) {
291f00d4f59SWei Liu         ret = -1;
292f00d4f59SWei Liu         goto err_out;
293f00d4f59SWei Liu     }
294f00d4f59SWei Liu 
295f00d4f59SWei Liu     if (credp->fc_uid != -1) {
296f00d4f59SWei Liu         uid = credp->fc_uid;
297f00d4f59SWei Liu     }
298f00d4f59SWei Liu     if (credp->fc_gid != -1) {
299f00d4f59SWei Liu         gid = credp->fc_gid;
300f00d4f59SWei Liu     }
301f00d4f59SWei Liu     if (credp->fc_mode != -1) {
302f00d4f59SWei Liu         mode = credp->fc_mode;
303f00d4f59SWei Liu     }
304f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
305f00d4f59SWei Liu         rdev = credp->fc_rdev;
306f00d4f59SWei Liu     }
307f00d4f59SWei Liu 
308f00d4f59SWei Liu 
309f00d4f59SWei Liu     if (uid != -1) {
310f00d4f59SWei Liu         fprintf(fp, "virtfs.uid=%d\n", uid);
311f00d4f59SWei Liu     }
312f00d4f59SWei Liu     if (gid != -1) {
313f00d4f59SWei Liu         fprintf(fp, "virtfs.gid=%d\n", gid);
314f00d4f59SWei Liu     }
315f00d4f59SWei Liu     if (mode != -1) {
316f00d4f59SWei Liu         fprintf(fp, "virtfs.mode=%d\n", mode);
317f00d4f59SWei Liu     }
318f00d4f59SWei Liu     if (rdev != -1) {
319f00d4f59SWei Liu         fprintf(fp, "virtfs.rdev=%d\n", rdev);
320f00d4f59SWei Liu     }
321f00d4f59SWei Liu     fclose(fp);
322f00d4f59SWei Liu 
323f00d4f59SWei Liu err_out:
324f00d4f59SWei Liu     g_free(attr_path);
325f00d4f59SWei Liu     return ret;
326f00d4f59SWei Liu }
327f00d4f59SWei Liu 
328f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp)
329f00d4f59SWei Liu {
330f00d4f59SWei Liu     int err;
331f00d4f59SWei Liu 
332f00d4f59SWei Liu     if (credp->fc_uid != -1) {
333f00d4f59SWei Liu         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
334f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
335f00d4f59SWei Liu         if (err) {
336f00d4f59SWei Liu             return err;
337f00d4f59SWei Liu         }
338f00d4f59SWei Liu     }
339f00d4f59SWei Liu     if (credp->fc_gid != -1) {
340f00d4f59SWei Liu         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
341f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
342f00d4f59SWei Liu         if (err) {
343f00d4f59SWei Liu             return err;
344f00d4f59SWei Liu         }
345f00d4f59SWei Liu     }
346f00d4f59SWei Liu     if (credp->fc_mode != -1) {
347f00d4f59SWei Liu         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
348f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
349f00d4f59SWei Liu         if (err) {
350f00d4f59SWei Liu             return err;
351f00d4f59SWei Liu         }
352f00d4f59SWei Liu     }
353f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
354f00d4f59SWei Liu         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
355f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
356f00d4f59SWei Liu         if (err) {
357f00d4f59SWei Liu             return err;
358f00d4f59SWei Liu         }
359f00d4f59SWei Liu     }
360f00d4f59SWei Liu     return 0;
361f00d4f59SWei Liu }
362f00d4f59SWei Liu 
363f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
364f00d4f59SWei Liu                                          FsCred *credp)
365f00d4f59SWei Liu {
366f00d4f59SWei Liu     char *buffer;
367f00d4f59SWei Liu 
368f00d4f59SWei Liu     buffer = rpath(fs_ctx, path);
369f00d4f59SWei Liu     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
370f00d4f59SWei Liu         /*
371f00d4f59SWei Liu          * If we fail to change ownership and if we are
372f00d4f59SWei Liu          * using security model none. Ignore the error
373f00d4f59SWei Liu          */
374f00d4f59SWei Liu         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
375f00d4f59SWei Liu             goto err;
376f00d4f59SWei Liu         }
377f00d4f59SWei Liu     }
378f00d4f59SWei Liu 
379f00d4f59SWei Liu     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
380f00d4f59SWei Liu         goto err;
381f00d4f59SWei Liu     }
382f00d4f59SWei Liu 
383f00d4f59SWei Liu     g_free(buffer);
384f00d4f59SWei Liu     return 0;
385f00d4f59SWei Liu err:
386f00d4f59SWei Liu     g_free(buffer);
387f00d4f59SWei Liu     return -1;
388f00d4f59SWei Liu }
389f00d4f59SWei Liu 
390f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
391f00d4f59SWei Liu                               char *buf, size_t bufsz)
392f00d4f59SWei Liu {
393f00d4f59SWei Liu     ssize_t tsize = -1;
394f00d4f59SWei Liu 
395f00d4f59SWei Liu     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
396f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
397f00d4f59SWei Liu         int fd;
398bec1e954SGreg Kurz 
399bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
400f00d4f59SWei Liu         if (fd == -1) {
401f00d4f59SWei Liu             return -1;
402f00d4f59SWei Liu         }
403f00d4f59SWei Liu         do {
404f00d4f59SWei Liu             tsize = read(fd, (void *)buf, bufsz);
405f00d4f59SWei Liu         } while (tsize == -1 && errno == EINTR);
406bec1e954SGreg Kurz         close_preserve_errno(fd);
407f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
408f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
409bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
410bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
411bec1e954SGreg Kurz         int dirfd;
412bec1e954SGreg Kurz 
413bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
414bec1e954SGreg Kurz         if (dirfd == -1) {
415bec1e954SGreg Kurz             goto out;
416bec1e954SGreg Kurz         }
417bec1e954SGreg Kurz 
418bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
419bec1e954SGreg Kurz         close_preserve_errno(dirfd);
420bec1e954SGreg Kurz     out:
421bec1e954SGreg Kurz         g_free(name);
422bec1e954SGreg Kurz         g_free(dirpath);
423f00d4f59SWei Liu     }
424f00d4f59SWei Liu     return tsize;
425f00d4f59SWei Liu }
426f00d4f59SWei Liu 
427f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
428f00d4f59SWei Liu {
429f00d4f59SWei Liu     return close(fs->fd);
430f00d4f59SWei Liu }
431f00d4f59SWei Liu 
432f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
433f00d4f59SWei Liu {
434f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
435f00d4f59SWei Liu }
436f00d4f59SWei Liu 
437f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path,
438f00d4f59SWei Liu                       int flags, V9fsFidOpenState *fs)
439f00d4f59SWei Liu {
44021328e1eSGreg Kurz     int fd;
441f00d4f59SWei Liu 
442996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
44321328e1eSGreg Kurz     if (fd == -1) {
44421328e1eSGreg Kurz         return -1;
44521328e1eSGreg Kurz     }
44621328e1eSGreg Kurz     fs->fd = fd;
447f00d4f59SWei Liu     return fs->fd;
448f00d4f59SWei Liu }
449f00d4f59SWei Liu 
450f00d4f59SWei Liu static int local_opendir(FsContext *ctx,
451f00d4f59SWei Liu                          V9fsPath *fs_path, V9fsFidOpenState *fs)
452f00d4f59SWei Liu {
453996a0d76SGreg Kurz     int dirfd;
45421328e1eSGreg Kurz     DIR *stream;
455f00d4f59SWei Liu 
456996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
457996a0d76SGreg Kurz     if (dirfd == -1) {
458996a0d76SGreg Kurz         return -1;
459996a0d76SGreg Kurz     }
460996a0d76SGreg Kurz 
461996a0d76SGreg Kurz     stream = fdopendir(dirfd);
46221328e1eSGreg Kurz     if (!stream) {
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 
479635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
480f00d4f59SWei Liu {
481635324e8SGreg Kurz     struct dirent *entry;
482f00d4f59SWei Liu 
483f00d4f59SWei Liu again:
484635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
485635324e8SGreg Kurz     if (!entry) {
486635324e8SGreg Kurz         return NULL;
487635324e8SGreg Kurz     }
488635324e8SGreg Kurz 
489f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED) {
490f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
491f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
492635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
493f00d4f59SWei Liu             /* skp the meta data directory */
494f00d4f59SWei Liu             goto again;
495f00d4f59SWei Liu         }
496f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
497f00d4f59SWei Liu     }
498635324e8SGreg Kurz 
499635324e8SGreg Kurz     return entry;
500f00d4f59SWei Liu }
501f00d4f59SWei Liu 
502f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
503f00d4f59SWei Liu {
504f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
505f00d4f59SWei Liu }
506f00d4f59SWei Liu 
507f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
508f00d4f59SWei Liu                             const struct iovec *iov,
509f00d4f59SWei Liu                             int iovcnt, off_t offset)
510f00d4f59SWei Liu {
511f00d4f59SWei Liu #ifdef CONFIG_PREADV
512f00d4f59SWei Liu     return preadv(fs->fd, iov, iovcnt, offset);
513f00d4f59SWei Liu #else
514f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
515f00d4f59SWei Liu     if (err == -1) {
516f00d4f59SWei Liu         return err;
517f00d4f59SWei Liu     } else {
518f00d4f59SWei Liu         return readv(fs->fd, iov, iovcnt);
519f00d4f59SWei Liu     }
520f00d4f59SWei Liu #endif
521f00d4f59SWei Liu }
522f00d4f59SWei Liu 
523f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
524f00d4f59SWei Liu                              const struct iovec *iov,
525f00d4f59SWei Liu                              int iovcnt, off_t offset)
526f00d4f59SWei Liu {
5276fe76accSGreg Kurz     ssize_t ret;
528f00d4f59SWei Liu #ifdef CONFIG_PREADV
529f00d4f59SWei Liu     ret = pwritev(fs->fd, iov, iovcnt, offset);
530f00d4f59SWei Liu #else
531f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
532f00d4f59SWei Liu     if (err == -1) {
533f00d4f59SWei Liu         return err;
534f00d4f59SWei Liu     } else {
535f00d4f59SWei Liu         ret = writev(fs->fd, iov, iovcnt);
536f00d4f59SWei Liu     }
537f00d4f59SWei Liu #endif
538f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE
539f00d4f59SWei Liu     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
540f00d4f59SWei Liu         /*
541f00d4f59SWei Liu          * Initiate a writeback. This is not a data integrity sync.
542f00d4f59SWei Liu          * We want to ensure that we don't leave dirty pages in the cache
543f00d4f59SWei Liu          * after write when writeout=immediate is sepcified.
544f00d4f59SWei Liu          */
545f00d4f59SWei Liu         sync_file_range(fs->fd, offset, ret,
546f00d4f59SWei Liu                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
547f00d4f59SWei Liu     }
548f00d4f59SWei Liu #endif
549f00d4f59SWei Liu     return ret;
550f00d4f59SWei Liu }
551f00d4f59SWei Liu 
552f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
553f00d4f59SWei Liu {
554f00d4f59SWei Liu     char *buffer;
555f00d4f59SWei Liu     int ret = -1;
556f00d4f59SWei Liu     char *path = fs_path->data;
557f00d4f59SWei Liu 
558f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
559f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
560f00d4f59SWei Liu         ret = local_set_xattr(buffer, credp);
561f00d4f59SWei Liu         g_free(buffer);
562f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
563f00d4f59SWei Liu         return local_set_mapped_file_attr(fs_ctx, path, credp);
564f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
565f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
566f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
567f00d4f59SWei Liu         ret = chmod(buffer, credp->fc_mode);
568f00d4f59SWei Liu         g_free(buffer);
569f00d4f59SWei Liu     }
570f00d4f59SWei Liu     return ret;
571f00d4f59SWei Liu }
572f00d4f59SWei Liu 
573f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
574f00d4f59SWei Liu                        const char *name, FsCred *credp)
575f00d4f59SWei Liu {
576f00d4f59SWei Liu     char *path;
577f00d4f59SWei Liu     int err = -1;
578f00d4f59SWei Liu     int serrno = 0;
579f00d4f59SWei Liu     V9fsString fullname;
580f00d4f59SWei Liu     char *buffer = NULL;
581f00d4f59SWei Liu 
582f00d4f59SWei Liu     v9fs_string_init(&fullname);
583f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
584f00d4f59SWei Liu     path = fullname.data;
585f00d4f59SWei Liu 
586f00d4f59SWei Liu     /* Determine the security model */
587f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
588f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
589f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
590f00d4f59SWei Liu         if (err == -1) {
591f00d4f59SWei Liu             goto out;
592f00d4f59SWei Liu         }
593f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
594f00d4f59SWei Liu         if (err == -1) {
595f00d4f59SWei Liu             serrno = errno;
596f00d4f59SWei Liu             goto err_end;
597f00d4f59SWei Liu         }
598f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
599f00d4f59SWei Liu 
600f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
601f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
602f00d4f59SWei Liu         if (err == -1) {
603f00d4f59SWei Liu             goto out;
604f00d4f59SWei Liu         }
605f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
606f00d4f59SWei Liu         if (err == -1) {
607f00d4f59SWei Liu             serrno = errno;
608f00d4f59SWei Liu             goto err_end;
609f00d4f59SWei Liu         }
610f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
611f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
612f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
613f00d4f59SWei Liu         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
614f00d4f59SWei Liu         if (err == -1) {
615f00d4f59SWei Liu             goto out;
616f00d4f59SWei Liu         }
617f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
618f00d4f59SWei Liu         if (err == -1) {
619f00d4f59SWei Liu             serrno = errno;
620f00d4f59SWei Liu             goto err_end;
621f00d4f59SWei Liu         }
622f00d4f59SWei Liu     }
623f00d4f59SWei Liu     goto out;
624f00d4f59SWei Liu 
625f00d4f59SWei Liu err_end:
626f00d4f59SWei Liu     remove(buffer);
627f00d4f59SWei Liu     errno = serrno;
628f00d4f59SWei Liu out:
629f00d4f59SWei Liu     g_free(buffer);
630f00d4f59SWei Liu     v9fs_string_free(&fullname);
631f00d4f59SWei Liu     return err;
632f00d4f59SWei Liu }
633f00d4f59SWei Liu 
634f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
635f00d4f59SWei Liu                        const char *name, FsCred *credp)
636f00d4f59SWei Liu {
637f00d4f59SWei Liu     char *path;
638f00d4f59SWei Liu     int err = -1;
639f00d4f59SWei Liu     int serrno = 0;
640f00d4f59SWei Liu     V9fsString fullname;
641f00d4f59SWei Liu     char *buffer = NULL;
642f00d4f59SWei Liu 
643f00d4f59SWei Liu     v9fs_string_init(&fullname);
644f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
645f00d4f59SWei Liu     path = fullname.data;
646f00d4f59SWei Liu 
647f00d4f59SWei Liu     /* Determine the security model */
648f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
649f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
650f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
651f00d4f59SWei Liu         if (err == -1) {
652f00d4f59SWei Liu             goto out;
653f00d4f59SWei Liu         }
654f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
655f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
656f00d4f59SWei Liu         if (err == -1) {
657f00d4f59SWei Liu             serrno = errno;
658f00d4f59SWei Liu             goto err_end;
659f00d4f59SWei Liu         }
660f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
661f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
662f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
663f00d4f59SWei Liu         if (err == -1) {
664f00d4f59SWei Liu             goto out;
665f00d4f59SWei Liu         }
666f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
667f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
668f00d4f59SWei Liu         if (err == -1) {
669f00d4f59SWei Liu             serrno = errno;
670f00d4f59SWei Liu             goto err_end;
671f00d4f59SWei Liu         }
672f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
673f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
674f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
675f00d4f59SWei Liu         err = mkdir(buffer, credp->fc_mode);
676f00d4f59SWei Liu         if (err == -1) {
677f00d4f59SWei Liu             goto out;
678f00d4f59SWei Liu         }
679f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
680f00d4f59SWei Liu         if (err == -1) {
681f00d4f59SWei Liu             serrno = errno;
682f00d4f59SWei Liu             goto err_end;
683f00d4f59SWei Liu         }
684f00d4f59SWei Liu     }
685f00d4f59SWei Liu     goto out;
686f00d4f59SWei Liu 
687f00d4f59SWei Liu err_end:
688f00d4f59SWei Liu     remove(buffer);
689f00d4f59SWei Liu     errno = serrno;
690f00d4f59SWei Liu out:
691f00d4f59SWei Liu     g_free(buffer);
692f00d4f59SWei Liu     v9fs_string_free(&fullname);
693f00d4f59SWei Liu     return err;
694f00d4f59SWei Liu }
695f00d4f59SWei Liu 
696f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type,
697f00d4f59SWei Liu                        V9fsFidOpenState *fs, struct stat *stbuf)
698f00d4f59SWei Liu {
699f00d4f59SWei Liu     int err, fd;
700f00d4f59SWei Liu 
701f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
702f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
703f00d4f59SWei Liu     } else {
704f00d4f59SWei Liu         fd = fs->fd;
705f00d4f59SWei Liu     }
706f00d4f59SWei Liu 
707f00d4f59SWei Liu     err = fstat(fd, stbuf);
708f00d4f59SWei Liu     if (err) {
709f00d4f59SWei Liu         return err;
710f00d4f59SWei Liu     }
711f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
712f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
713f00d4f59SWei Liu         uid_t tmp_uid;
714f00d4f59SWei Liu         gid_t tmp_gid;
715f00d4f59SWei Liu         mode_t tmp_mode;
716f00d4f59SWei Liu         dev_t tmp_dev;
717f00d4f59SWei Liu 
718f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
719f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
720f00d4f59SWei Liu         }
721f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
722f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
723f00d4f59SWei Liu         }
724f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
725f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
726f00d4f59SWei Liu         }
727f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
728f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
729f00d4f59SWei Liu         }
730f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
731f00d4f59SWei Liu         errno = EOPNOTSUPP;
732f00d4f59SWei Liu         return -1;
733f00d4f59SWei Liu     }
734f00d4f59SWei Liu     return err;
735f00d4f59SWei Liu }
736f00d4f59SWei Liu 
737f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
738f00d4f59SWei Liu                        int flags, FsCred *credp, V9fsFidOpenState *fs)
739f00d4f59SWei Liu {
740f00d4f59SWei Liu     char *path;
741f00d4f59SWei Liu     int fd = -1;
742f00d4f59SWei Liu     int err = -1;
743f00d4f59SWei Liu     int serrno = 0;
744f00d4f59SWei Liu     V9fsString fullname;
745f00d4f59SWei Liu     char *buffer = NULL;
746f00d4f59SWei Liu 
747f00d4f59SWei Liu     /*
748f00d4f59SWei Liu      * Mark all the open to not follow symlinks
749f00d4f59SWei Liu      */
750f00d4f59SWei Liu     flags |= O_NOFOLLOW;
751f00d4f59SWei Liu 
752f00d4f59SWei Liu     v9fs_string_init(&fullname);
753f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
754f00d4f59SWei Liu     path = fullname.data;
755f00d4f59SWei Liu 
756f00d4f59SWei Liu     /* Determine the security model */
757f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
758f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
759f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
760f00d4f59SWei Liu         if (fd == -1) {
761f00d4f59SWei Liu             err = fd;
762f00d4f59SWei Liu             goto out;
763f00d4f59SWei Liu         }
764f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
765f00d4f59SWei Liu         /* Set cleint credentials in xattr */
766f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
767f00d4f59SWei Liu         if (err == -1) {
768f00d4f59SWei Liu             serrno = errno;
769f00d4f59SWei Liu             goto err_end;
770f00d4f59SWei Liu         }
771f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
772f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
773f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
774f00d4f59SWei Liu         if (fd == -1) {
775f00d4f59SWei Liu             err = fd;
776f00d4f59SWei Liu             goto out;
777f00d4f59SWei Liu         }
778f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
779f00d4f59SWei Liu         /* Set client credentials in .virtfs_metadata directory files */
780f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
781f00d4f59SWei Liu         if (err == -1) {
782f00d4f59SWei Liu             serrno = errno;
783f00d4f59SWei Liu             goto err_end;
784f00d4f59SWei Liu         }
785f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
786f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
787f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
788f00d4f59SWei Liu         fd = open(buffer, flags, credp->fc_mode);
789f00d4f59SWei Liu         if (fd == -1) {
790f00d4f59SWei Liu             err = fd;
791f00d4f59SWei Liu             goto out;
792f00d4f59SWei Liu         }
793f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
794f00d4f59SWei Liu         if (err == -1) {
795f00d4f59SWei Liu             serrno = errno;
796f00d4f59SWei Liu             goto err_end;
797f00d4f59SWei Liu         }
798f00d4f59SWei Liu     }
799f00d4f59SWei Liu     err = fd;
800f00d4f59SWei Liu     fs->fd = fd;
801f00d4f59SWei Liu     goto out;
802f00d4f59SWei Liu 
803f00d4f59SWei Liu err_end:
804f00d4f59SWei Liu     close(fd);
805f00d4f59SWei Liu     remove(buffer);
806f00d4f59SWei Liu     errno = serrno;
807f00d4f59SWei Liu out:
808f00d4f59SWei Liu     g_free(buffer);
809f00d4f59SWei Liu     v9fs_string_free(&fullname);
810f00d4f59SWei Liu     return err;
811f00d4f59SWei Liu }
812f00d4f59SWei Liu 
813f00d4f59SWei Liu 
814f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath,
815f00d4f59SWei Liu                          V9fsPath *dir_path, const char *name, FsCred *credp)
816f00d4f59SWei Liu {
817f00d4f59SWei Liu     int err = -1;
818f00d4f59SWei Liu     int serrno = 0;
819f00d4f59SWei Liu     char *newpath;
820f00d4f59SWei Liu     V9fsString fullname;
821f00d4f59SWei Liu     char *buffer = NULL;
822f00d4f59SWei Liu 
823f00d4f59SWei Liu     v9fs_string_init(&fullname);
824f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
825f00d4f59SWei Liu     newpath = fullname.data;
826f00d4f59SWei Liu 
827f00d4f59SWei Liu     /* Determine the security model */
828f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
829f00d4f59SWei Liu         int fd;
830f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
831f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
832f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
833f00d4f59SWei Liu         if (fd == -1) {
834f00d4f59SWei Liu             err = fd;
835f00d4f59SWei Liu             goto out;
836f00d4f59SWei Liu         }
837f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
838f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
839f00d4f59SWei Liu         do {
840f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
841f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
842f00d4f59SWei Liu 
843f00d4f59SWei Liu         if (write_size != oldpath_size) {
844f00d4f59SWei Liu             serrno = errno;
845f00d4f59SWei Liu             close(fd);
846f00d4f59SWei Liu             err = -1;
847f00d4f59SWei Liu             goto err_end;
848f00d4f59SWei Liu         }
849f00d4f59SWei Liu         close(fd);
850f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
851f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
852f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
853f00d4f59SWei Liu         if (err == -1) {
854f00d4f59SWei Liu             serrno = errno;
855f00d4f59SWei Liu             goto err_end;
856f00d4f59SWei Liu         }
857f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
858f00d4f59SWei Liu         int fd;
859f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
860f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
861f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
862f00d4f59SWei Liu         if (fd == -1) {
863f00d4f59SWei Liu             err = fd;
864f00d4f59SWei Liu             goto out;
865f00d4f59SWei Liu         }
866f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
867f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
868f00d4f59SWei Liu         do {
869f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
870f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
871f00d4f59SWei Liu 
872f00d4f59SWei Liu         if (write_size != oldpath_size) {
873f00d4f59SWei Liu             serrno = errno;
874f00d4f59SWei Liu             close(fd);
875f00d4f59SWei Liu             err = -1;
876f00d4f59SWei Liu             goto err_end;
877f00d4f59SWei Liu         }
878f00d4f59SWei Liu         close(fd);
879f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
880f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
881f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
882f00d4f59SWei Liu         if (err == -1) {
883f00d4f59SWei Liu             serrno = errno;
884f00d4f59SWei Liu             goto err_end;
885f00d4f59SWei Liu         }
886f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
887f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
888f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
889f00d4f59SWei Liu         err = symlink(oldpath, buffer);
890f00d4f59SWei Liu         if (err) {
891f00d4f59SWei Liu             goto out;
892f00d4f59SWei Liu         }
893f00d4f59SWei Liu         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
894f00d4f59SWei Liu         if (err == -1) {
895f00d4f59SWei Liu             /*
896f00d4f59SWei Liu              * If we fail to change ownership and if we are
897f00d4f59SWei Liu              * using security model none. Ignore the error
898f00d4f59SWei Liu              */
899f00d4f59SWei Liu             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
900f00d4f59SWei Liu                 serrno = errno;
901f00d4f59SWei Liu                 goto err_end;
902f00d4f59SWei Liu             } else
903f00d4f59SWei Liu                 err = 0;
904f00d4f59SWei Liu         }
905f00d4f59SWei Liu     }
906f00d4f59SWei Liu     goto out;
907f00d4f59SWei Liu 
908f00d4f59SWei Liu err_end:
909f00d4f59SWei Liu     remove(buffer);
910f00d4f59SWei Liu     errno = serrno;
911f00d4f59SWei Liu out:
912f00d4f59SWei Liu     g_free(buffer);
913f00d4f59SWei Liu     v9fs_string_free(&fullname);
914f00d4f59SWei Liu     return err;
915f00d4f59SWei Liu }
916f00d4f59SWei Liu 
917f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath,
918f00d4f59SWei Liu                       V9fsPath *dirpath, const char *name)
919f00d4f59SWei Liu {
920f00d4f59SWei Liu     int ret;
921f00d4f59SWei Liu     V9fsString newpath;
922f00d4f59SWei Liu     char *buffer, *buffer1;
923*6dd4b1f1SGreg Kurz     int serrno;
924f00d4f59SWei Liu 
925f00d4f59SWei Liu     v9fs_string_init(&newpath);
926f00d4f59SWei Liu     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
927f00d4f59SWei Liu 
928f00d4f59SWei Liu     buffer = rpath(ctx, oldpath->data);
929f00d4f59SWei Liu     buffer1 = rpath(ctx, newpath.data);
930f00d4f59SWei Liu     ret = link(buffer, buffer1);
931f00d4f59SWei Liu     g_free(buffer);
932*6dd4b1f1SGreg Kurz     if (ret < 0) {
933*6dd4b1f1SGreg Kurz         goto out;
934*6dd4b1f1SGreg Kurz     }
935f00d4f59SWei Liu 
936f00d4f59SWei Liu     /* now link the virtfs_metadata files */
937*6dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938*6dd4b1f1SGreg Kurz         char *vbuffer, *vbuffer1;
939*6dd4b1f1SGreg Kurz 
940f00d4f59SWei Liu         /* Link the .virtfs_metadata files. Create the metada directory */
941f00d4f59SWei Liu         ret = local_create_mapped_attr_dir(ctx, newpath.data);
942f00d4f59SWei Liu         if (ret < 0) {
943f00d4f59SWei Liu             goto err_out;
944f00d4f59SWei Liu         }
945*6dd4b1f1SGreg Kurz         vbuffer = local_mapped_attr_path(ctx, oldpath->data);
946*6dd4b1f1SGreg Kurz         vbuffer1 = local_mapped_attr_path(ctx, newpath.data);
947*6dd4b1f1SGreg Kurz         ret = link(vbuffer, vbuffer1);
948*6dd4b1f1SGreg Kurz         g_free(vbuffer);
949*6dd4b1f1SGreg Kurz         g_free(vbuffer1);
950f00d4f59SWei Liu         if (ret < 0 && errno != ENOENT) {
951f00d4f59SWei Liu             goto err_out;
952f00d4f59SWei Liu         }
953f00d4f59SWei Liu     }
954*6dd4b1f1SGreg Kurz     goto out;
955*6dd4b1f1SGreg Kurz 
956f00d4f59SWei Liu err_out:
957*6dd4b1f1SGreg Kurz     serrno = errno;
958*6dd4b1f1SGreg Kurz     remove(buffer1);
959*6dd4b1f1SGreg Kurz     errno = serrno;
960*6dd4b1f1SGreg Kurz out:
961*6dd4b1f1SGreg Kurz     g_free(buffer1);
962f00d4f59SWei Liu     v9fs_string_free(&newpath);
963f00d4f59SWei Liu     return ret;
964f00d4f59SWei Liu }
965f00d4f59SWei Liu 
966f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
967f00d4f59SWei Liu {
968ac125d99SGreg Kurz     int fd, ret;
969f00d4f59SWei Liu 
970ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
971ac125d99SGreg Kurz     if (fd == -1) {
972ac125d99SGreg Kurz         return -1;
973ac125d99SGreg Kurz     }
974ac125d99SGreg Kurz     ret = ftruncate(fd, size);
975ac125d99SGreg Kurz     close_preserve_errno(fd);
976f00d4f59SWei Liu     return ret;
977f00d4f59SWei Liu }
978f00d4f59SWei Liu 
979f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
980f00d4f59SWei Liu {
981f00d4f59SWei Liu     char *buffer;
982f00d4f59SWei Liu     int ret = -1;
983f00d4f59SWei Liu     char *path = fs_path->data;
984f00d4f59SWei Liu 
985f00d4f59SWei Liu     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
986f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
987f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_NONE)) {
988f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
989f00d4f59SWei Liu         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
990f00d4f59SWei Liu         g_free(buffer);
991f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
992f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
993f00d4f59SWei Liu         ret = local_set_xattr(buffer, credp);
994f00d4f59SWei Liu         g_free(buffer);
995f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
996f00d4f59SWei Liu         return local_set_mapped_file_attr(fs_ctx, path, credp);
997f00d4f59SWei Liu     }
998f00d4f59SWei Liu     return ret;
999f00d4f59SWei Liu }
1000f00d4f59SWei Liu 
1001f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1002f00d4f59SWei Liu                            const struct timespec *buf)
1003f00d4f59SWei Liu {
1004a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1005a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1006a33eda0dSGreg Kurz     int dirfd, ret = -1;
1007f00d4f59SWei Liu 
1008a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1009a33eda0dSGreg Kurz     if (dirfd == -1) {
1010a33eda0dSGreg Kurz         goto out;
1011a33eda0dSGreg Kurz     }
1012a33eda0dSGreg Kurz 
1013a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1014a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1015a33eda0dSGreg Kurz out:
1016a33eda0dSGreg Kurz     g_free(dirpath);
1017a33eda0dSGreg Kurz     g_free(name);
1018f00d4f59SWei Liu     return ret;
1019f00d4f59SWei Liu }
1020f00d4f59SWei Liu 
1021df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1022df4938a6SGreg Kurz                                  int flags)
1023df4938a6SGreg Kurz {
1024df4938a6SGreg Kurz     int ret = -1;
1025df4938a6SGreg Kurz 
1026df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1027df4938a6SGreg Kurz         int map_dirfd;
1028df4938a6SGreg Kurz 
1029df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1030df4938a6SGreg Kurz             int fd;
1031df4938a6SGreg Kurz 
1032df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1033df4938a6SGreg Kurz             if (fd == -1) {
1034df4938a6SGreg Kurz                 goto err_out;
1035df4938a6SGreg Kurz             }
1036df4938a6SGreg Kurz             /*
1037df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1038df4938a6SGreg Kurz              * directory
1039df4938a6SGreg Kurz              */
1040df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1041df4938a6SGreg Kurz             close_preserve_errno(fd);
1042df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1043df4938a6SGreg Kurz                 /*
1044df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1045df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1046df4938a6SGreg Kurz                  */
1047df4938a6SGreg Kurz                 goto err_out;
1048df4938a6SGreg Kurz             }
1049df4938a6SGreg Kurz         }
1050df4938a6SGreg Kurz         /*
1051df4938a6SGreg Kurz          * Now remove the name from parent directory
1052df4938a6SGreg Kurz          * .virtfs_metadata directory.
1053df4938a6SGreg Kurz          */
1054df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1055df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1056df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1057df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1058df4938a6SGreg Kurz             /*
1059df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1060df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1061df4938a6SGreg Kurz              */
1062df4938a6SGreg Kurz             goto err_out;
1063df4938a6SGreg Kurz         }
1064df4938a6SGreg Kurz     }
1065df4938a6SGreg Kurz 
1066df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1067df4938a6SGreg Kurz err_out:
1068df4938a6SGreg Kurz     return ret;
1069df4938a6SGreg Kurz }
1070df4938a6SGreg Kurz 
1071f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path)
1072f00d4f59SWei Liu {
1073f00d4f59SWei Liu     struct stat stbuf;
1074a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1075a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1076a0e640a8SGreg Kurz     int flags = 0;
1077a0e640a8SGreg Kurz     int dirfd;
1078a0e640a8SGreg Kurz     int err = -1;
1079f00d4f59SWei Liu 
1080a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1081a0e640a8SGreg Kurz     if (dirfd) {
1082a0e640a8SGreg Kurz         goto out;
1083a0e640a8SGreg Kurz     }
1084a0e640a8SGreg Kurz 
1085a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1086f00d4f59SWei Liu         goto err_out;
1087f00d4f59SWei Liu     }
1088a0e640a8SGreg Kurz 
1089f00d4f59SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
1090a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1091f00d4f59SWei Liu     }
1092f00d4f59SWei Liu 
1093a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
1094f00d4f59SWei Liu err_out:
1095a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1096a0e640a8SGreg Kurz out:
1097a0e640a8SGreg Kurz     g_free(name);
1098a0e640a8SGreg Kurz     g_free(dirpath);
1099f00d4f59SWei Liu     return err;
1100f00d4f59SWei Liu }
1101f00d4f59SWei Liu 
1102f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type,
1103f00d4f59SWei Liu                        V9fsFidOpenState *fs, int datasync)
1104f00d4f59SWei Liu {
1105f00d4f59SWei Liu     int fd;
1106f00d4f59SWei Liu 
1107f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
1108f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
1109f00d4f59SWei Liu     } else {
1110f00d4f59SWei Liu         fd = fs->fd;
1111f00d4f59SWei Liu     }
1112f00d4f59SWei Liu 
1113f00d4f59SWei Liu     if (datasync) {
1114f00d4f59SWei Liu         return qemu_fdatasync(fd);
1115f00d4f59SWei Liu     } else {
1116f00d4f59SWei Liu         return fsync(fd);
1117f00d4f59SWei Liu     }
1118f00d4f59SWei Liu }
1119f00d4f59SWei Liu 
1120f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1121f00d4f59SWei Liu {
112231e51d1cSGreg Kurz     int fd, ret;
1123f00d4f59SWei Liu 
112431e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
112531e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
112631e51d1cSGreg Kurz     close_preserve_errno(fd);
1127f00d4f59SWei Liu     return ret;
1128f00d4f59SWei Liu }
1129f00d4f59SWei Liu 
1130f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1131f00d4f59SWei Liu                                const char *name, void *value, size_t size)
1132f00d4f59SWei Liu {
1133f00d4f59SWei Liu     char *path = fs_path->data;
1134f00d4f59SWei Liu 
1135f00d4f59SWei Liu     return v9fs_get_xattr(ctx, path, name, value, size);
1136f00d4f59SWei Liu }
1137f00d4f59SWei Liu 
1138f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1139f00d4f59SWei Liu                                 void *value, size_t size)
1140f00d4f59SWei Liu {
1141f00d4f59SWei Liu     char *path = fs_path->data;
1142f00d4f59SWei Liu 
1143f00d4f59SWei Liu     return v9fs_list_xattr(ctx, path, value, size);
1144f00d4f59SWei Liu }
1145f00d4f59SWei Liu 
1146f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1147f00d4f59SWei Liu                            void *value, size_t size, int flags)
1148f00d4f59SWei Liu {
1149f00d4f59SWei Liu     char *path = fs_path->data;
1150f00d4f59SWei Liu 
1151f00d4f59SWei Liu     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1152f00d4f59SWei Liu }
1153f00d4f59SWei Liu 
1154f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1155f00d4f59SWei Liu                               const char *name)
1156f00d4f59SWei Liu {
1157f00d4f59SWei Liu     char *path = fs_path->data;
1158f00d4f59SWei Liu 
1159f00d4f59SWei Liu     return v9fs_remove_xattr(ctx, path, name);
1160f00d4f59SWei Liu }
1161f00d4f59SWei Liu 
1162f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1163f00d4f59SWei Liu                               const char *name, V9fsPath *target)
1164f00d4f59SWei Liu {
1165f00d4f59SWei Liu     if (dir_path) {
1166e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1167f00d4f59SWei Liu     } else {
1168e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
1169f00d4f59SWei Liu     }
1170f00d4f59SWei Liu     return 0;
1171f00d4f59SWei Liu }
1172f00d4f59SWei Liu 
1173f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1174f00d4f59SWei Liu                           const char *old_name, V9fsPath *newdir,
1175f00d4f59SWei Liu                           const char *new_name)
1176f00d4f59SWei Liu {
1177f00d4f59SWei Liu     int ret;
117899f2cf4bSGreg Kurz     int odirfd, ndirfd;
1179f00d4f59SWei Liu 
118099f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
118199f2cf4bSGreg Kurz     if (odirfd == -1) {
118299f2cf4bSGreg Kurz         return -1;
118399f2cf4bSGreg Kurz     }
1184f00d4f59SWei Liu 
118599f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
118699f2cf4bSGreg Kurz     if (ndirfd == -1) {
118799f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
118899f2cf4bSGreg Kurz         return -1;
118999f2cf4bSGreg Kurz     }
1190f00d4f59SWei Liu 
119199f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
119299f2cf4bSGreg Kurz     if (ret < 0) {
119399f2cf4bSGreg Kurz         goto out;
119499f2cf4bSGreg Kurz     }
119599f2cf4bSGreg Kurz 
119699f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
119799f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
119899f2cf4bSGreg Kurz 
119999f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
120099f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
120199f2cf4bSGreg Kurz             goto err_undo_rename;
120299f2cf4bSGreg Kurz         }
120399f2cf4bSGreg Kurz 
1204*6dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
120599f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
120699f2cf4bSGreg Kurz             goto err;
120799f2cf4bSGreg Kurz         }
120899f2cf4bSGreg Kurz 
1209*6dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
121099f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
121199f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
121299f2cf4bSGreg Kurz             goto err;
121399f2cf4bSGreg Kurz         }
121499f2cf4bSGreg Kurz 
121599f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
121699f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
121799f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
121899f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
121999f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
122099f2cf4bSGreg Kurz             goto err_undo_rename;
122199f2cf4bSGreg Kurz         }
122299f2cf4bSGreg Kurz 
122399f2cf4bSGreg Kurz         ret = 0;
122499f2cf4bSGreg Kurz     }
122599f2cf4bSGreg Kurz     goto out;
122699f2cf4bSGreg Kurz 
122799f2cf4bSGreg Kurz err:
122899f2cf4bSGreg Kurz     ret = -1;
122999f2cf4bSGreg Kurz err_undo_rename:
123099f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
123199f2cf4bSGreg Kurz out:
123299f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
123399f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
1234f00d4f59SWei Liu     return ret;
1235f00d4f59SWei Liu }
1236f00d4f59SWei Liu 
1237d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1238d2767edeSGreg Kurz {
1239d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1240d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1241d2767edeSGreg Kurz }
1242d2767edeSGreg Kurz 
1243d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1244d2767edeSGreg Kurz                         const char *newpath)
1245d2767edeSGreg Kurz {
1246d2767edeSGreg Kurz     int err;
1247d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1248d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1249d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1250d2767edeSGreg Kurz 
1251d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1252d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1253d2767edeSGreg Kurz 
1254d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1255d2767edeSGreg Kurz 
1256d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1257d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1258d2767edeSGreg Kurz     g_free(nname);
1259d2767edeSGreg Kurz     g_free(oname);
1260d2767edeSGreg Kurz 
1261d2767edeSGreg Kurz     return err;
1262d2767edeSGreg Kurz }
1263d2767edeSGreg Kurz 
1264f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1265f00d4f59SWei Liu                           const char *name, int flags)
1266f00d4f59SWei Liu {
1267f00d4f59SWei Liu     int ret;
1268df4938a6SGreg Kurz     int dirfd;
1269f00d4f59SWei Liu 
1270df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1271df4938a6SGreg Kurz     if (dirfd == -1) {
1272df4938a6SGreg Kurz         return -1;
1273df4938a6SGreg Kurz     }
1274f00d4f59SWei Liu 
1275df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1276df4938a6SGreg Kurz     close_preserve_errno(dirfd);
1277f00d4f59SWei Liu     return ret;
1278f00d4f59SWei Liu }
1279f00d4f59SWei Liu 
1280f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1281f00d4f59SWei Liu                                 mode_t st_mode, uint64_t *st_gen)
1282f00d4f59SWei Liu {
1283f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION
1284f00d4f59SWei Liu     int err;
1285f00d4f59SWei Liu     V9fsFidOpenState fid_open;
1286f00d4f59SWei Liu 
1287f00d4f59SWei Liu     /*
1288f00d4f59SWei Liu      * Do not try to open special files like device nodes, fifos etc
1289f00d4f59SWei Liu      * We can get fd for regular files and directories only
1290f00d4f59SWei Liu      */
1291f00d4f59SWei Liu     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1292f00d4f59SWei Liu         errno = ENOTTY;
1293f00d4f59SWei Liu         return -1;
1294f00d4f59SWei Liu     }
1295f00d4f59SWei Liu     err = local_open(ctx, path, O_RDONLY, &fid_open);
1296f00d4f59SWei Liu     if (err < 0) {
1297f00d4f59SWei Liu         return err;
1298f00d4f59SWei Liu     }
1299f00d4f59SWei Liu     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1300f00d4f59SWei Liu     local_close(ctx, &fid_open);
1301f00d4f59SWei Liu     return err;
1302f00d4f59SWei Liu #else
1303f00d4f59SWei Liu     errno = ENOTTY;
1304f00d4f59SWei Liu     return -1;
1305f00d4f59SWei Liu #endif
1306f00d4f59SWei Liu }
1307f00d4f59SWei Liu 
1308f00d4f59SWei Liu static int local_init(FsContext *ctx)
1309f00d4f59SWei Liu {
1310f00d4f59SWei Liu     struct statfs stbuf;
13110e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13120e35a378SGreg Kurz 
13130e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13140e35a378SGreg Kurz     if (data->mountfd == -1) {
13150e35a378SGreg Kurz         goto err;
13160e35a378SGreg Kurz     }
1317f00d4f59SWei Liu 
131800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
131900c90bd1SGreg Kurz     /*
132000c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
132100c90bd1SGreg Kurz      */
13220e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13230e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13240e35a378SGreg Kurz         goto err;
132500c90bd1SGreg Kurz     }
132600c90bd1SGreg Kurz     switch (stbuf.f_type) {
132700c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
132800c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
132900c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
133000c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
133100c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
133200c90bd1SGreg Kurz         break;
133300c90bd1SGreg Kurz     }
133400c90bd1SGreg Kurz #endif
133500c90bd1SGreg Kurz 
1336f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1337f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1338f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1339f00d4f59SWei Liu         ctx->xops = mapped_xattr_ops;
1340f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_NONE) {
1341f00d4f59SWei Liu         ctx->xops = none_xattr_ops;
1342f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1343f00d4f59SWei Liu         /*
1344f00d4f59SWei Liu          * xattr operation for mapped-file and passthrough
1345f00d4f59SWei Liu          * remain same.
1346f00d4f59SWei Liu          */
1347f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1348f00d4f59SWei Liu     }
1349f00d4f59SWei Liu     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
135000c90bd1SGreg Kurz 
13510e35a378SGreg Kurz     ctx->private = data;
135200c90bd1SGreg Kurz     return 0;
13530e35a378SGreg Kurz 
13540e35a378SGreg Kurz err:
13550e35a378SGreg Kurz     g_free(data);
13560e35a378SGreg Kurz     return -1;
13570e35a378SGreg Kurz }
13580e35a378SGreg Kurz 
13590e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13600e35a378SGreg Kurz {
13610e35a378SGreg Kurz     LocalData *data = ctx->private;
13620e35a378SGreg Kurz 
13630e35a378SGreg Kurz     close(data->mountfd);
13640e35a378SGreg Kurz     g_free(data);
1365f00d4f59SWei Liu }
1366f00d4f59SWei Liu 
1367f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1368f00d4f59SWei Liu {
1369f00d4f59SWei Liu     const char *sec_model = qemu_opt_get(opts, "security_model");
1370f00d4f59SWei Liu     const char *path = qemu_opt_get(opts, "path");
1371f00d4f59SWei Liu 
1372f00d4f59SWei Liu     if (!sec_model) {
137363325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
137463325b18SGreg Kurz         error_printf("valid options are:"
137563325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1376f00d4f59SWei Liu         return -1;
1377f00d4f59SWei Liu     }
1378f00d4f59SWei Liu 
1379f00d4f59SWei Liu     if (!strcmp(sec_model, "passthrough")) {
1380f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1381f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped") ||
1382f00d4f59SWei Liu                !strcmp(sec_model, "mapped-xattr")) {
1383f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED;
1384f00d4f59SWei Liu     } else if (!strcmp(sec_model, "none")) {
1385f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_NONE;
1386f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped-file")) {
1387f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1388f00d4f59SWei Liu     } else {
138963325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
139063325b18SGreg Kurz         error_printf("valid options are:"
139163325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1392f00d4f59SWei Liu         return -1;
1393f00d4f59SWei Liu     }
1394f00d4f59SWei Liu 
1395f00d4f59SWei Liu     if (!path) {
139663325b18SGreg Kurz         error_report("fsdev: No path specified");
1397f00d4f59SWei Liu         return -1;
1398f00d4f59SWei Liu     }
1399f00d4f59SWei Liu     fse->path = g_strdup(path);
1400f00d4f59SWei Liu 
1401f00d4f59SWei Liu     return 0;
1402f00d4f59SWei Liu }
1403f00d4f59SWei Liu 
1404f00d4f59SWei Liu FileOperations local_ops = {
1405f00d4f59SWei Liu     .parse_opts = local_parse_opts,
1406f00d4f59SWei Liu     .init  = local_init,
14070e35a378SGreg Kurz     .cleanup = local_cleanup,
1408f00d4f59SWei Liu     .lstat = local_lstat,
1409f00d4f59SWei Liu     .readlink = local_readlink,
1410f00d4f59SWei Liu     .close = local_close,
1411f00d4f59SWei Liu     .closedir = local_closedir,
1412f00d4f59SWei Liu     .open = local_open,
1413f00d4f59SWei Liu     .opendir = local_opendir,
1414f00d4f59SWei Liu     .rewinddir = local_rewinddir,
1415f00d4f59SWei Liu     .telldir = local_telldir,
1416635324e8SGreg Kurz     .readdir = local_readdir,
1417f00d4f59SWei Liu     .seekdir = local_seekdir,
1418f00d4f59SWei Liu     .preadv = local_preadv,
1419f00d4f59SWei Liu     .pwritev = local_pwritev,
1420f00d4f59SWei Liu     .chmod = local_chmod,
1421f00d4f59SWei Liu     .mknod = local_mknod,
1422f00d4f59SWei Liu     .mkdir = local_mkdir,
1423f00d4f59SWei Liu     .fstat = local_fstat,
1424f00d4f59SWei Liu     .open2 = local_open2,
1425f00d4f59SWei Liu     .symlink = local_symlink,
1426f00d4f59SWei Liu     .link = local_link,
1427f00d4f59SWei Liu     .truncate = local_truncate,
1428f00d4f59SWei Liu     .rename = local_rename,
1429f00d4f59SWei Liu     .chown = local_chown,
1430f00d4f59SWei Liu     .utimensat = local_utimensat,
1431f00d4f59SWei Liu     .remove = local_remove,
1432f00d4f59SWei Liu     .fsync = local_fsync,
1433f00d4f59SWei Liu     .statfs = local_statfs,
1434f00d4f59SWei Liu     .lgetxattr = local_lgetxattr,
1435f00d4f59SWei Liu     .llistxattr = local_llistxattr,
1436f00d4f59SWei Liu     .lsetxattr = local_lsetxattr,
1437f00d4f59SWei Liu     .lremovexattr = local_lremovexattr,
1438f00d4f59SWei Liu     .name_to_path = local_name_to_path,
1439f00d4f59SWei Liu     .renameat  = local_renameat,
1440f00d4f59SWei Liu     .unlinkat = local_unlinkat,
1441f00d4f59SWei Liu };
1442