xref: /openbmc/qemu/hw/9pfs/9p-local.c (revision a0e640a87210b1e986bcd4e7f7de03beb3db0a4a)
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 
70f00d4f59SWei Liu #define VIRTFS_META_DIR ".virtfs_metadata"
71f00d4f59SWei Liu 
72f00d4f59SWei Liu static char *local_mapped_attr_path(FsContext *ctx, const char *path)
73f00d4f59SWei Liu {
74f00d4f59SWei Liu     int dirlen;
75f00d4f59SWei Liu     const char *name = strrchr(path, '/');
76f00d4f59SWei Liu     if (name) {
77f00d4f59SWei Liu         dirlen = name - path;
78f00d4f59SWei Liu         ++name;
79f00d4f59SWei Liu     } else {
80f00d4f59SWei Liu         name = path;
81f00d4f59SWei Liu         dirlen = 0;
82f00d4f59SWei Liu     }
83f00d4f59SWei Liu     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
84f00d4f59SWei Liu                            dirlen, path, VIRTFS_META_DIR, name);
85f00d4f59SWei Liu }
86f00d4f59SWei Liu 
87f00d4f59SWei Liu static FILE *local_fopen(const char *path, const char *mode)
88f00d4f59SWei Liu {
89f00d4f59SWei Liu     int fd, o_mode = 0;
90f00d4f59SWei Liu     FILE *fp;
91f00d4f59SWei Liu     int flags = O_NOFOLLOW;
92f00d4f59SWei Liu     /*
93f00d4f59SWei Liu      * only supports two modes
94f00d4f59SWei Liu      */
95f00d4f59SWei Liu     if (mode[0] == 'r') {
96f00d4f59SWei Liu         flags |= O_RDONLY;
97f00d4f59SWei Liu     } else if (mode[0] == 'w') {
98f00d4f59SWei Liu         flags |= O_WRONLY | O_TRUNC | O_CREAT;
99f00d4f59SWei Liu         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
100f00d4f59SWei Liu     } else {
101f00d4f59SWei Liu         return NULL;
102f00d4f59SWei Liu     }
103f00d4f59SWei Liu     fd = open(path, flags, o_mode);
104f00d4f59SWei Liu     if (fd == -1) {
105f00d4f59SWei Liu         return NULL;
106f00d4f59SWei Liu     }
107f00d4f59SWei Liu     fp = fdopen(fd, mode);
108f00d4f59SWei Liu     if (!fp) {
109f00d4f59SWei Liu         close(fd);
110f00d4f59SWei Liu     }
111f00d4f59SWei Liu     return fp;
112f00d4f59SWei Liu }
113f00d4f59SWei Liu 
114f00d4f59SWei Liu #define ATTR_MAX 100
115f00d4f59SWei Liu static void local_mapped_file_attr(FsContext *ctx, const char *path,
116f00d4f59SWei Liu                                    struct stat *stbuf)
117f00d4f59SWei Liu {
118f00d4f59SWei Liu     FILE *fp;
119f00d4f59SWei Liu     char buf[ATTR_MAX];
120f00d4f59SWei Liu     char *attr_path;
121f00d4f59SWei Liu 
122f00d4f59SWei Liu     attr_path = local_mapped_attr_path(ctx, path);
123f00d4f59SWei Liu     fp = local_fopen(attr_path, "r");
124f00d4f59SWei Liu     g_free(attr_path);
125f00d4f59SWei Liu     if (!fp) {
126f00d4f59SWei Liu         return;
127f00d4f59SWei Liu     }
128f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
129f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
130f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
131f00d4f59SWei Liu             stbuf->st_uid = atoi(buf+11);
132f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
133f00d4f59SWei Liu             stbuf->st_gid = atoi(buf+11);
134f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
135f00d4f59SWei Liu             stbuf->st_mode = atoi(buf+12);
136f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
137f00d4f59SWei Liu             stbuf->st_rdev = atoi(buf+12);
138f00d4f59SWei Liu         }
139f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
140f00d4f59SWei Liu     }
141f00d4f59SWei Liu     fclose(fp);
142f00d4f59SWei Liu }
143f00d4f59SWei Liu 
144f00d4f59SWei Liu static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
145f00d4f59SWei Liu {
146f00d4f59SWei Liu     int err;
147f00d4f59SWei Liu     char *buffer;
148f00d4f59SWei Liu     char *path = fs_path->data;
149f00d4f59SWei Liu 
150f00d4f59SWei Liu     buffer = rpath(fs_ctx, path);
151f00d4f59SWei Liu     err =  lstat(buffer, stbuf);
152f00d4f59SWei Liu     if (err) {
153f00d4f59SWei Liu         goto err_out;
154f00d4f59SWei Liu     }
155f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
156f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
157f00d4f59SWei Liu         uid_t tmp_uid;
158f00d4f59SWei Liu         gid_t tmp_gid;
159f00d4f59SWei Liu         mode_t tmp_mode;
160f00d4f59SWei Liu         dev_t tmp_dev;
161f00d4f59SWei Liu         if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
162f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
163f00d4f59SWei Liu         }
164f00d4f59SWei Liu         if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
165f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
166f00d4f59SWei Liu         }
167f00d4f59SWei Liu         if (getxattr(buffer, "user.virtfs.mode",
168f00d4f59SWei Liu                     &tmp_mode, sizeof(mode_t)) > 0) {
169f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
170f00d4f59SWei Liu         }
171f00d4f59SWei Liu         if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
172f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
173f00d4f59SWei Liu         }
174f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
175f00d4f59SWei Liu         local_mapped_file_attr(fs_ctx, path, stbuf);
176f00d4f59SWei Liu     }
177f00d4f59SWei Liu 
178f00d4f59SWei Liu err_out:
179f00d4f59SWei Liu     g_free(buffer);
180f00d4f59SWei Liu     return err;
181f00d4f59SWei Liu }
182f00d4f59SWei Liu 
183f00d4f59SWei Liu static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
184f00d4f59SWei Liu {
185f00d4f59SWei Liu     int err;
186f00d4f59SWei Liu     char *attr_dir;
187f00d4f59SWei Liu     char *tmp_path = g_strdup(path);
188f00d4f59SWei Liu 
189f00d4f59SWei Liu     attr_dir = g_strdup_printf("%s/%s/%s",
190f00d4f59SWei Liu              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
191f00d4f59SWei Liu 
192f00d4f59SWei Liu     err = mkdir(attr_dir, 0700);
193f00d4f59SWei Liu     if (err < 0 && errno == EEXIST) {
194f00d4f59SWei Liu         err = 0;
195f00d4f59SWei Liu     }
196f00d4f59SWei Liu     g_free(attr_dir);
197f00d4f59SWei Liu     g_free(tmp_path);
198f00d4f59SWei Liu     return err;
199f00d4f59SWei Liu }
200f00d4f59SWei Liu 
201f00d4f59SWei Liu static int local_set_mapped_file_attr(FsContext *ctx,
202f00d4f59SWei Liu                                       const char *path, FsCred *credp)
203f00d4f59SWei Liu {
204f00d4f59SWei Liu     FILE *fp;
205f00d4f59SWei Liu     int ret = 0;
206f00d4f59SWei Liu     char buf[ATTR_MAX];
207f00d4f59SWei Liu     char *attr_path;
208f00d4f59SWei Liu     int uid = -1, gid = -1, mode = -1, rdev = -1;
209f00d4f59SWei Liu 
210f00d4f59SWei Liu     attr_path = local_mapped_attr_path(ctx, path);
211f00d4f59SWei Liu     fp = local_fopen(attr_path, "r");
212f00d4f59SWei Liu     if (!fp) {
213f00d4f59SWei Liu         goto create_map_file;
214f00d4f59SWei Liu     }
215f00d4f59SWei Liu     memset(buf, 0, ATTR_MAX);
216f00d4f59SWei Liu     while (fgets(buf, ATTR_MAX, fp)) {
217f00d4f59SWei Liu         if (!strncmp(buf, "virtfs.uid", 10)) {
218f00d4f59SWei Liu             uid = atoi(buf+11);
219f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.gid", 10)) {
220f00d4f59SWei Liu             gid = atoi(buf+11);
221f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.mode", 11)) {
222f00d4f59SWei Liu             mode = atoi(buf+12);
223f00d4f59SWei Liu         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
224f00d4f59SWei Liu             rdev = atoi(buf+12);
225f00d4f59SWei Liu         }
226f00d4f59SWei Liu         memset(buf, 0, ATTR_MAX);
227f00d4f59SWei Liu     }
228f00d4f59SWei Liu     fclose(fp);
229f00d4f59SWei Liu     goto update_map_file;
230f00d4f59SWei Liu 
231f00d4f59SWei Liu create_map_file:
232f00d4f59SWei Liu     ret = local_create_mapped_attr_dir(ctx, path);
233f00d4f59SWei Liu     if (ret < 0) {
234f00d4f59SWei Liu         goto err_out;
235f00d4f59SWei Liu     }
236f00d4f59SWei Liu 
237f00d4f59SWei Liu update_map_file:
238f00d4f59SWei Liu     fp = local_fopen(attr_path, "w");
239f00d4f59SWei Liu     if (!fp) {
240f00d4f59SWei Liu         ret = -1;
241f00d4f59SWei Liu         goto err_out;
242f00d4f59SWei Liu     }
243f00d4f59SWei Liu 
244f00d4f59SWei Liu     if (credp->fc_uid != -1) {
245f00d4f59SWei Liu         uid = credp->fc_uid;
246f00d4f59SWei Liu     }
247f00d4f59SWei Liu     if (credp->fc_gid != -1) {
248f00d4f59SWei Liu         gid = credp->fc_gid;
249f00d4f59SWei Liu     }
250f00d4f59SWei Liu     if (credp->fc_mode != -1) {
251f00d4f59SWei Liu         mode = credp->fc_mode;
252f00d4f59SWei Liu     }
253f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
254f00d4f59SWei Liu         rdev = credp->fc_rdev;
255f00d4f59SWei Liu     }
256f00d4f59SWei Liu 
257f00d4f59SWei Liu 
258f00d4f59SWei Liu     if (uid != -1) {
259f00d4f59SWei Liu         fprintf(fp, "virtfs.uid=%d\n", uid);
260f00d4f59SWei Liu     }
261f00d4f59SWei Liu     if (gid != -1) {
262f00d4f59SWei Liu         fprintf(fp, "virtfs.gid=%d\n", gid);
263f00d4f59SWei Liu     }
264f00d4f59SWei Liu     if (mode != -1) {
265f00d4f59SWei Liu         fprintf(fp, "virtfs.mode=%d\n", mode);
266f00d4f59SWei Liu     }
267f00d4f59SWei Liu     if (rdev != -1) {
268f00d4f59SWei Liu         fprintf(fp, "virtfs.rdev=%d\n", rdev);
269f00d4f59SWei Liu     }
270f00d4f59SWei Liu     fclose(fp);
271f00d4f59SWei Liu 
272f00d4f59SWei Liu err_out:
273f00d4f59SWei Liu     g_free(attr_path);
274f00d4f59SWei Liu     return ret;
275f00d4f59SWei Liu }
276f00d4f59SWei Liu 
277f00d4f59SWei Liu static int local_set_xattr(const char *path, FsCred *credp)
278f00d4f59SWei Liu {
279f00d4f59SWei Liu     int err;
280f00d4f59SWei Liu 
281f00d4f59SWei Liu     if (credp->fc_uid != -1) {
282f00d4f59SWei Liu         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
283f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
284f00d4f59SWei Liu         if (err) {
285f00d4f59SWei Liu             return err;
286f00d4f59SWei Liu         }
287f00d4f59SWei Liu     }
288f00d4f59SWei Liu     if (credp->fc_gid != -1) {
289f00d4f59SWei Liu         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
290f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
291f00d4f59SWei Liu         if (err) {
292f00d4f59SWei Liu             return err;
293f00d4f59SWei Liu         }
294f00d4f59SWei Liu     }
295f00d4f59SWei Liu     if (credp->fc_mode != -1) {
296f00d4f59SWei Liu         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
297f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
298f00d4f59SWei Liu         if (err) {
299f00d4f59SWei Liu             return err;
300f00d4f59SWei Liu         }
301f00d4f59SWei Liu     }
302f00d4f59SWei Liu     if (credp->fc_rdev != -1) {
303f00d4f59SWei Liu         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
304f00d4f59SWei Liu         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
305f00d4f59SWei Liu         if (err) {
306f00d4f59SWei Liu             return err;
307f00d4f59SWei Liu         }
308f00d4f59SWei Liu     }
309f00d4f59SWei Liu     return 0;
310f00d4f59SWei Liu }
311f00d4f59SWei Liu 
312f00d4f59SWei Liu static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
313f00d4f59SWei Liu                                          FsCred *credp)
314f00d4f59SWei Liu {
315f00d4f59SWei Liu     char *buffer;
316f00d4f59SWei Liu 
317f00d4f59SWei Liu     buffer = rpath(fs_ctx, path);
318f00d4f59SWei Liu     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
319f00d4f59SWei Liu         /*
320f00d4f59SWei Liu          * If we fail to change ownership and if we are
321f00d4f59SWei Liu          * using security model none. Ignore the error
322f00d4f59SWei Liu          */
323f00d4f59SWei Liu         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
324f00d4f59SWei Liu             goto err;
325f00d4f59SWei Liu         }
326f00d4f59SWei Liu     }
327f00d4f59SWei Liu 
328f00d4f59SWei Liu     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
329f00d4f59SWei Liu         goto err;
330f00d4f59SWei Liu     }
331f00d4f59SWei Liu 
332f00d4f59SWei Liu     g_free(buffer);
333f00d4f59SWei Liu     return 0;
334f00d4f59SWei Liu err:
335f00d4f59SWei Liu     g_free(buffer);
336f00d4f59SWei Liu     return -1;
337f00d4f59SWei Liu }
338f00d4f59SWei Liu 
339f00d4f59SWei Liu static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
340f00d4f59SWei Liu                               char *buf, size_t bufsz)
341f00d4f59SWei Liu {
342f00d4f59SWei Liu     ssize_t tsize = -1;
343f00d4f59SWei Liu     char *buffer;
344f00d4f59SWei Liu     char *path = fs_path->data;
345f00d4f59SWei Liu 
346f00d4f59SWei Liu     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
347f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
348f00d4f59SWei Liu         int fd;
349f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
350f00d4f59SWei Liu         fd = open(buffer, O_RDONLY | O_NOFOLLOW);
351f00d4f59SWei Liu         g_free(buffer);
352f00d4f59SWei Liu         if (fd == -1) {
353f00d4f59SWei Liu             return -1;
354f00d4f59SWei Liu         }
355f00d4f59SWei Liu         do {
356f00d4f59SWei Liu             tsize = read(fd, (void *)buf, bufsz);
357f00d4f59SWei Liu         } while (tsize == -1 && errno == EINTR);
358f00d4f59SWei Liu         close(fd);
359f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
360f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
361f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
362f00d4f59SWei Liu         tsize = readlink(buffer, buf, bufsz);
363f00d4f59SWei Liu         g_free(buffer);
364f00d4f59SWei Liu     }
365f00d4f59SWei Liu     return tsize;
366f00d4f59SWei Liu }
367f00d4f59SWei Liu 
368f00d4f59SWei Liu static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
369f00d4f59SWei Liu {
370f00d4f59SWei Liu     return close(fs->fd);
371f00d4f59SWei Liu }
372f00d4f59SWei Liu 
373f00d4f59SWei Liu static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
374f00d4f59SWei Liu {
375f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
376f00d4f59SWei Liu }
377f00d4f59SWei Liu 
378f00d4f59SWei Liu static int local_open(FsContext *ctx, V9fsPath *fs_path,
379f00d4f59SWei Liu                       int flags, V9fsFidOpenState *fs)
380f00d4f59SWei Liu {
38121328e1eSGreg Kurz     int fd;
382f00d4f59SWei Liu 
383996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
38421328e1eSGreg Kurz     if (fd == -1) {
38521328e1eSGreg Kurz         return -1;
38621328e1eSGreg Kurz     }
38721328e1eSGreg Kurz     fs->fd = fd;
388f00d4f59SWei Liu     return fs->fd;
389f00d4f59SWei Liu }
390f00d4f59SWei Liu 
391f00d4f59SWei Liu static int local_opendir(FsContext *ctx,
392f00d4f59SWei Liu                          V9fsPath *fs_path, V9fsFidOpenState *fs)
393f00d4f59SWei Liu {
394996a0d76SGreg Kurz     int dirfd;
39521328e1eSGreg Kurz     DIR *stream;
396f00d4f59SWei Liu 
397996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
398996a0d76SGreg Kurz     if (dirfd == -1) {
399996a0d76SGreg Kurz         return -1;
400996a0d76SGreg Kurz     }
401996a0d76SGreg Kurz 
402996a0d76SGreg Kurz     stream = fdopendir(dirfd);
40321328e1eSGreg Kurz     if (!stream) {
404f00d4f59SWei Liu         return -1;
405f00d4f59SWei Liu     }
40621328e1eSGreg Kurz     fs->dir.stream = stream;
407f00d4f59SWei Liu     return 0;
408f00d4f59SWei Liu }
409f00d4f59SWei Liu 
410f00d4f59SWei Liu static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
411f00d4f59SWei Liu {
412f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
413f00d4f59SWei Liu }
414f00d4f59SWei Liu 
415f00d4f59SWei Liu static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
416f00d4f59SWei Liu {
417f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
418f00d4f59SWei Liu }
419f00d4f59SWei Liu 
420635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
421f00d4f59SWei Liu {
422635324e8SGreg Kurz     struct dirent *entry;
423f00d4f59SWei Liu 
424f00d4f59SWei Liu again:
425635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
426635324e8SGreg Kurz     if (!entry) {
427635324e8SGreg Kurz         return NULL;
428635324e8SGreg Kurz     }
429635324e8SGreg Kurz 
430f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED) {
431f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
432f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
433635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
434f00d4f59SWei Liu             /* skp the meta data directory */
435f00d4f59SWei Liu             goto again;
436f00d4f59SWei Liu         }
437f00d4f59SWei Liu         entry->d_type = DT_UNKNOWN;
438f00d4f59SWei Liu     }
439635324e8SGreg Kurz 
440635324e8SGreg Kurz     return entry;
441f00d4f59SWei Liu }
442f00d4f59SWei Liu 
443f00d4f59SWei Liu static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
444f00d4f59SWei Liu {
445f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
446f00d4f59SWei Liu }
447f00d4f59SWei Liu 
448f00d4f59SWei Liu static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
449f00d4f59SWei Liu                             const struct iovec *iov,
450f00d4f59SWei Liu                             int iovcnt, off_t offset)
451f00d4f59SWei Liu {
452f00d4f59SWei Liu #ifdef CONFIG_PREADV
453f00d4f59SWei Liu     return preadv(fs->fd, iov, iovcnt, offset);
454f00d4f59SWei Liu #else
455f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
456f00d4f59SWei Liu     if (err == -1) {
457f00d4f59SWei Liu         return err;
458f00d4f59SWei Liu     } else {
459f00d4f59SWei Liu         return readv(fs->fd, iov, iovcnt);
460f00d4f59SWei Liu     }
461f00d4f59SWei Liu #endif
462f00d4f59SWei Liu }
463f00d4f59SWei Liu 
464f00d4f59SWei Liu static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
465f00d4f59SWei Liu                              const struct iovec *iov,
466f00d4f59SWei Liu                              int iovcnt, off_t offset)
467f00d4f59SWei Liu {
4686fe76accSGreg Kurz     ssize_t ret;
469f00d4f59SWei Liu #ifdef CONFIG_PREADV
470f00d4f59SWei Liu     ret = pwritev(fs->fd, iov, iovcnt, offset);
471f00d4f59SWei Liu #else
472f00d4f59SWei Liu     int err = lseek(fs->fd, offset, SEEK_SET);
473f00d4f59SWei Liu     if (err == -1) {
474f00d4f59SWei Liu         return err;
475f00d4f59SWei Liu     } else {
476f00d4f59SWei Liu         ret = writev(fs->fd, iov, iovcnt);
477f00d4f59SWei Liu     }
478f00d4f59SWei Liu #endif
479f00d4f59SWei Liu #ifdef CONFIG_SYNC_FILE_RANGE
480f00d4f59SWei Liu     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
481f00d4f59SWei Liu         /*
482f00d4f59SWei Liu          * Initiate a writeback. This is not a data integrity sync.
483f00d4f59SWei Liu          * We want to ensure that we don't leave dirty pages in the cache
484f00d4f59SWei Liu          * after write when writeout=immediate is sepcified.
485f00d4f59SWei Liu          */
486f00d4f59SWei Liu         sync_file_range(fs->fd, offset, ret,
487f00d4f59SWei Liu                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
488f00d4f59SWei Liu     }
489f00d4f59SWei Liu #endif
490f00d4f59SWei Liu     return ret;
491f00d4f59SWei Liu }
492f00d4f59SWei Liu 
493f00d4f59SWei Liu static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
494f00d4f59SWei Liu {
495f00d4f59SWei Liu     char *buffer;
496f00d4f59SWei Liu     int ret = -1;
497f00d4f59SWei Liu     char *path = fs_path->data;
498f00d4f59SWei Liu 
499f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
500f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
501f00d4f59SWei Liu         ret = local_set_xattr(buffer, credp);
502f00d4f59SWei Liu         g_free(buffer);
503f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
504f00d4f59SWei Liu         return local_set_mapped_file_attr(fs_ctx, path, credp);
505f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
506f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
507f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
508f00d4f59SWei Liu         ret = chmod(buffer, credp->fc_mode);
509f00d4f59SWei Liu         g_free(buffer);
510f00d4f59SWei Liu     }
511f00d4f59SWei Liu     return ret;
512f00d4f59SWei Liu }
513f00d4f59SWei Liu 
514f00d4f59SWei Liu static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
515f00d4f59SWei Liu                        const char *name, FsCred *credp)
516f00d4f59SWei Liu {
517f00d4f59SWei Liu     char *path;
518f00d4f59SWei Liu     int err = -1;
519f00d4f59SWei Liu     int serrno = 0;
520f00d4f59SWei Liu     V9fsString fullname;
521f00d4f59SWei Liu     char *buffer = NULL;
522f00d4f59SWei Liu 
523f00d4f59SWei Liu     v9fs_string_init(&fullname);
524f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
525f00d4f59SWei Liu     path = fullname.data;
526f00d4f59SWei Liu 
527f00d4f59SWei Liu     /* Determine the security model */
528f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
529f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
530f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
531f00d4f59SWei Liu         if (err == -1) {
532f00d4f59SWei Liu             goto out;
533f00d4f59SWei Liu         }
534f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
535f00d4f59SWei Liu         if (err == -1) {
536f00d4f59SWei Liu             serrno = errno;
537f00d4f59SWei Liu             goto err_end;
538f00d4f59SWei Liu         }
539f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
540f00d4f59SWei Liu 
541f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
542f00d4f59SWei Liu         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
543f00d4f59SWei Liu         if (err == -1) {
544f00d4f59SWei Liu             goto out;
545f00d4f59SWei Liu         }
546f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
547f00d4f59SWei Liu         if (err == -1) {
548f00d4f59SWei Liu             serrno = errno;
549f00d4f59SWei Liu             goto err_end;
550f00d4f59SWei Liu         }
551f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
552f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
553f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
554f00d4f59SWei Liu         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
555f00d4f59SWei Liu         if (err == -1) {
556f00d4f59SWei Liu             goto out;
557f00d4f59SWei Liu         }
558f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
559f00d4f59SWei Liu         if (err == -1) {
560f00d4f59SWei Liu             serrno = errno;
561f00d4f59SWei Liu             goto err_end;
562f00d4f59SWei Liu         }
563f00d4f59SWei Liu     }
564f00d4f59SWei Liu     goto out;
565f00d4f59SWei Liu 
566f00d4f59SWei Liu err_end:
567f00d4f59SWei Liu     remove(buffer);
568f00d4f59SWei Liu     errno = serrno;
569f00d4f59SWei Liu out:
570f00d4f59SWei Liu     g_free(buffer);
571f00d4f59SWei Liu     v9fs_string_free(&fullname);
572f00d4f59SWei Liu     return err;
573f00d4f59SWei Liu }
574f00d4f59SWei Liu 
575f00d4f59SWei Liu static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
576f00d4f59SWei Liu                        const char *name, FsCred *credp)
577f00d4f59SWei Liu {
578f00d4f59SWei Liu     char *path;
579f00d4f59SWei Liu     int err = -1;
580f00d4f59SWei Liu     int serrno = 0;
581f00d4f59SWei Liu     V9fsString fullname;
582f00d4f59SWei Liu     char *buffer = NULL;
583f00d4f59SWei Liu 
584f00d4f59SWei Liu     v9fs_string_init(&fullname);
585f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
586f00d4f59SWei Liu     path = fullname.data;
587f00d4f59SWei Liu 
588f00d4f59SWei Liu     /* Determine the security model */
589f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
590f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
591f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
592f00d4f59SWei Liu         if (err == -1) {
593f00d4f59SWei Liu             goto out;
594f00d4f59SWei Liu         }
595f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
596f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
597f00d4f59SWei Liu         if (err == -1) {
598f00d4f59SWei Liu             serrno = errno;
599f00d4f59SWei Liu             goto err_end;
600f00d4f59SWei Liu         }
601f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
602f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
603f00d4f59SWei Liu         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
604f00d4f59SWei Liu         if (err == -1) {
605f00d4f59SWei Liu             goto out;
606f00d4f59SWei Liu         }
607f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFDIR;
608f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
609f00d4f59SWei Liu         if (err == -1) {
610f00d4f59SWei Liu             serrno = errno;
611f00d4f59SWei Liu             goto err_end;
612f00d4f59SWei Liu         }
613f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
614f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
615f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
616f00d4f59SWei Liu         err = mkdir(buffer, credp->fc_mode);
617f00d4f59SWei Liu         if (err == -1) {
618f00d4f59SWei Liu             goto out;
619f00d4f59SWei Liu         }
620f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
621f00d4f59SWei Liu         if (err == -1) {
622f00d4f59SWei Liu             serrno = errno;
623f00d4f59SWei Liu             goto err_end;
624f00d4f59SWei Liu         }
625f00d4f59SWei Liu     }
626f00d4f59SWei Liu     goto out;
627f00d4f59SWei Liu 
628f00d4f59SWei Liu err_end:
629f00d4f59SWei Liu     remove(buffer);
630f00d4f59SWei Liu     errno = serrno;
631f00d4f59SWei Liu out:
632f00d4f59SWei Liu     g_free(buffer);
633f00d4f59SWei Liu     v9fs_string_free(&fullname);
634f00d4f59SWei Liu     return err;
635f00d4f59SWei Liu }
636f00d4f59SWei Liu 
637f00d4f59SWei Liu static int local_fstat(FsContext *fs_ctx, int fid_type,
638f00d4f59SWei Liu                        V9fsFidOpenState *fs, struct stat *stbuf)
639f00d4f59SWei Liu {
640f00d4f59SWei Liu     int err, fd;
641f00d4f59SWei Liu 
642f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
643f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
644f00d4f59SWei Liu     } else {
645f00d4f59SWei Liu         fd = fs->fd;
646f00d4f59SWei Liu     }
647f00d4f59SWei Liu 
648f00d4f59SWei Liu     err = fstat(fd, stbuf);
649f00d4f59SWei Liu     if (err) {
650f00d4f59SWei Liu         return err;
651f00d4f59SWei Liu     }
652f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
653f00d4f59SWei Liu         /* Actual credentials are part of extended attrs */
654f00d4f59SWei Liu         uid_t tmp_uid;
655f00d4f59SWei Liu         gid_t tmp_gid;
656f00d4f59SWei Liu         mode_t tmp_mode;
657f00d4f59SWei Liu         dev_t tmp_dev;
658f00d4f59SWei Liu 
659f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
660f00d4f59SWei Liu             stbuf->st_uid = le32_to_cpu(tmp_uid);
661f00d4f59SWei Liu         }
662f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
663f00d4f59SWei Liu             stbuf->st_gid = le32_to_cpu(tmp_gid);
664f00d4f59SWei Liu         }
665f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
666f00d4f59SWei Liu             stbuf->st_mode = le32_to_cpu(tmp_mode);
667f00d4f59SWei Liu         }
668f00d4f59SWei Liu         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
669f00d4f59SWei Liu             stbuf->st_rdev = le64_to_cpu(tmp_dev);
670f00d4f59SWei Liu         }
671f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
672f00d4f59SWei Liu         errno = EOPNOTSUPP;
673f00d4f59SWei Liu         return -1;
674f00d4f59SWei Liu     }
675f00d4f59SWei Liu     return err;
676f00d4f59SWei Liu }
677f00d4f59SWei Liu 
678f00d4f59SWei Liu static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
679f00d4f59SWei Liu                        int flags, FsCred *credp, V9fsFidOpenState *fs)
680f00d4f59SWei Liu {
681f00d4f59SWei Liu     char *path;
682f00d4f59SWei Liu     int fd = -1;
683f00d4f59SWei Liu     int err = -1;
684f00d4f59SWei Liu     int serrno = 0;
685f00d4f59SWei Liu     V9fsString fullname;
686f00d4f59SWei Liu     char *buffer = NULL;
687f00d4f59SWei Liu 
688f00d4f59SWei Liu     /*
689f00d4f59SWei Liu      * Mark all the open to not follow symlinks
690f00d4f59SWei Liu      */
691f00d4f59SWei Liu     flags |= O_NOFOLLOW;
692f00d4f59SWei Liu 
693f00d4f59SWei Liu     v9fs_string_init(&fullname);
694f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
695f00d4f59SWei Liu     path = fullname.data;
696f00d4f59SWei Liu 
697f00d4f59SWei Liu     /* Determine the security model */
698f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
699f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
700f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
701f00d4f59SWei Liu         if (fd == -1) {
702f00d4f59SWei Liu             err = fd;
703f00d4f59SWei Liu             goto out;
704f00d4f59SWei Liu         }
705f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
706f00d4f59SWei Liu         /* Set cleint credentials in xattr */
707f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
708f00d4f59SWei Liu         if (err == -1) {
709f00d4f59SWei Liu             serrno = errno;
710f00d4f59SWei Liu             goto err_end;
711f00d4f59SWei Liu         }
712f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
713f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
714f00d4f59SWei Liu         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
715f00d4f59SWei Liu         if (fd == -1) {
716f00d4f59SWei Liu             err = fd;
717f00d4f59SWei Liu             goto out;
718f00d4f59SWei Liu         }
719f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFREG;
720f00d4f59SWei Liu         /* Set client credentials in .virtfs_metadata directory files */
721f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, path, credp);
722f00d4f59SWei Liu         if (err == -1) {
723f00d4f59SWei Liu             serrno = errno;
724f00d4f59SWei Liu             goto err_end;
725f00d4f59SWei Liu         }
726f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
727f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
728f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
729f00d4f59SWei Liu         fd = open(buffer, flags, credp->fc_mode);
730f00d4f59SWei Liu         if (fd == -1) {
731f00d4f59SWei Liu             err = fd;
732f00d4f59SWei Liu             goto out;
733f00d4f59SWei Liu         }
734f00d4f59SWei Liu         err = local_post_create_passthrough(fs_ctx, path, credp);
735f00d4f59SWei Liu         if (err == -1) {
736f00d4f59SWei Liu             serrno = errno;
737f00d4f59SWei Liu             goto err_end;
738f00d4f59SWei Liu         }
739f00d4f59SWei Liu     }
740f00d4f59SWei Liu     err = fd;
741f00d4f59SWei Liu     fs->fd = fd;
742f00d4f59SWei Liu     goto out;
743f00d4f59SWei Liu 
744f00d4f59SWei Liu err_end:
745f00d4f59SWei Liu     close(fd);
746f00d4f59SWei Liu     remove(buffer);
747f00d4f59SWei Liu     errno = serrno;
748f00d4f59SWei Liu out:
749f00d4f59SWei Liu     g_free(buffer);
750f00d4f59SWei Liu     v9fs_string_free(&fullname);
751f00d4f59SWei Liu     return err;
752f00d4f59SWei Liu }
753f00d4f59SWei Liu 
754f00d4f59SWei Liu 
755f00d4f59SWei Liu static int local_symlink(FsContext *fs_ctx, const char *oldpath,
756f00d4f59SWei Liu                          V9fsPath *dir_path, const char *name, FsCred *credp)
757f00d4f59SWei Liu {
758f00d4f59SWei Liu     int err = -1;
759f00d4f59SWei Liu     int serrno = 0;
760f00d4f59SWei Liu     char *newpath;
761f00d4f59SWei Liu     V9fsString fullname;
762f00d4f59SWei Liu     char *buffer = NULL;
763f00d4f59SWei Liu 
764f00d4f59SWei Liu     v9fs_string_init(&fullname);
765f00d4f59SWei Liu     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
766f00d4f59SWei Liu     newpath = fullname.data;
767f00d4f59SWei Liu 
768f00d4f59SWei Liu     /* Determine the security model */
769f00d4f59SWei Liu     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
770f00d4f59SWei Liu         int fd;
771f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
772f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
773f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
774f00d4f59SWei Liu         if (fd == -1) {
775f00d4f59SWei Liu             err = fd;
776f00d4f59SWei Liu             goto out;
777f00d4f59SWei Liu         }
778f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
779f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
780f00d4f59SWei Liu         do {
781f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
782f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
783f00d4f59SWei Liu 
784f00d4f59SWei Liu         if (write_size != oldpath_size) {
785f00d4f59SWei Liu             serrno = errno;
786f00d4f59SWei Liu             close(fd);
787f00d4f59SWei Liu             err = -1;
788f00d4f59SWei Liu             goto err_end;
789f00d4f59SWei Liu         }
790f00d4f59SWei Liu         close(fd);
791f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
792f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
793f00d4f59SWei Liu         err = local_set_xattr(buffer, credp);
794f00d4f59SWei Liu         if (err == -1) {
795f00d4f59SWei Liu             serrno = errno;
796f00d4f59SWei Liu             goto err_end;
797f00d4f59SWei Liu         }
798f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
799f00d4f59SWei Liu         int fd;
800f00d4f59SWei Liu         ssize_t oldpath_size, write_size;
801f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
802f00d4f59SWei Liu         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
803f00d4f59SWei Liu         if (fd == -1) {
804f00d4f59SWei Liu             err = fd;
805f00d4f59SWei Liu             goto out;
806f00d4f59SWei Liu         }
807f00d4f59SWei Liu         /* Write the oldpath (target) to the file. */
808f00d4f59SWei Liu         oldpath_size = strlen(oldpath);
809f00d4f59SWei Liu         do {
810f00d4f59SWei Liu             write_size = write(fd, (void *)oldpath, oldpath_size);
811f00d4f59SWei Liu         } while (write_size == -1 && errno == EINTR);
812f00d4f59SWei Liu 
813f00d4f59SWei Liu         if (write_size != oldpath_size) {
814f00d4f59SWei Liu             serrno = errno;
815f00d4f59SWei Liu             close(fd);
816f00d4f59SWei Liu             err = -1;
817f00d4f59SWei Liu             goto err_end;
818f00d4f59SWei Liu         }
819f00d4f59SWei Liu         close(fd);
820f00d4f59SWei Liu         /* Set cleint credentials in symlink's xattr */
821f00d4f59SWei Liu         credp->fc_mode = credp->fc_mode|S_IFLNK;
822f00d4f59SWei Liu         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
823f00d4f59SWei Liu         if (err == -1) {
824f00d4f59SWei Liu             serrno = errno;
825f00d4f59SWei Liu             goto err_end;
826f00d4f59SWei Liu         }
827f00d4f59SWei Liu     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
828f00d4f59SWei Liu                (fs_ctx->export_flags & V9FS_SM_NONE)) {
829f00d4f59SWei Liu         buffer = rpath(fs_ctx, newpath);
830f00d4f59SWei Liu         err = symlink(oldpath, buffer);
831f00d4f59SWei Liu         if (err) {
832f00d4f59SWei Liu             goto out;
833f00d4f59SWei Liu         }
834f00d4f59SWei Liu         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
835f00d4f59SWei Liu         if (err == -1) {
836f00d4f59SWei Liu             /*
837f00d4f59SWei Liu              * If we fail to change ownership and if we are
838f00d4f59SWei Liu              * using security model none. Ignore the error
839f00d4f59SWei Liu              */
840f00d4f59SWei Liu             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
841f00d4f59SWei Liu                 serrno = errno;
842f00d4f59SWei Liu                 goto err_end;
843f00d4f59SWei Liu             } else
844f00d4f59SWei Liu                 err = 0;
845f00d4f59SWei Liu         }
846f00d4f59SWei Liu     }
847f00d4f59SWei Liu     goto out;
848f00d4f59SWei Liu 
849f00d4f59SWei Liu err_end:
850f00d4f59SWei Liu     remove(buffer);
851f00d4f59SWei Liu     errno = serrno;
852f00d4f59SWei Liu out:
853f00d4f59SWei Liu     g_free(buffer);
854f00d4f59SWei Liu     v9fs_string_free(&fullname);
855f00d4f59SWei Liu     return err;
856f00d4f59SWei Liu }
857f00d4f59SWei Liu 
858f00d4f59SWei Liu static int local_link(FsContext *ctx, V9fsPath *oldpath,
859f00d4f59SWei Liu                       V9fsPath *dirpath, const char *name)
860f00d4f59SWei Liu {
861f00d4f59SWei Liu     int ret;
862f00d4f59SWei Liu     V9fsString newpath;
863f00d4f59SWei Liu     char *buffer, *buffer1;
864f00d4f59SWei Liu 
865f00d4f59SWei Liu     v9fs_string_init(&newpath);
866f00d4f59SWei Liu     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
867f00d4f59SWei Liu 
868f00d4f59SWei Liu     buffer = rpath(ctx, oldpath->data);
869f00d4f59SWei Liu     buffer1 = rpath(ctx, newpath.data);
870f00d4f59SWei Liu     ret = link(buffer, buffer1);
871f00d4f59SWei Liu     g_free(buffer);
872f00d4f59SWei Liu     g_free(buffer1);
873f00d4f59SWei Liu 
874f00d4f59SWei Liu     /* now link the virtfs_metadata files */
875f00d4f59SWei Liu     if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
876f00d4f59SWei Liu         /* Link the .virtfs_metadata files. Create the metada directory */
877f00d4f59SWei Liu         ret = local_create_mapped_attr_dir(ctx, newpath.data);
878f00d4f59SWei Liu         if (ret < 0) {
879f00d4f59SWei Liu             goto err_out;
880f00d4f59SWei Liu         }
881f00d4f59SWei Liu         buffer = local_mapped_attr_path(ctx, oldpath->data);
882f00d4f59SWei Liu         buffer1 = local_mapped_attr_path(ctx, newpath.data);
883f00d4f59SWei Liu         ret = link(buffer, buffer1);
884f00d4f59SWei Liu         g_free(buffer);
885f00d4f59SWei Liu         g_free(buffer1);
886f00d4f59SWei Liu         if (ret < 0 && errno != ENOENT) {
887f00d4f59SWei Liu             goto err_out;
888f00d4f59SWei Liu         }
889f00d4f59SWei Liu     }
890f00d4f59SWei Liu err_out:
891f00d4f59SWei Liu     v9fs_string_free(&newpath);
892f00d4f59SWei Liu     return ret;
893f00d4f59SWei Liu }
894f00d4f59SWei Liu 
895f00d4f59SWei Liu static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
896f00d4f59SWei Liu {
897f00d4f59SWei Liu     char *buffer;
898f00d4f59SWei Liu     int ret;
899f00d4f59SWei Liu     char *path = fs_path->data;
900f00d4f59SWei Liu 
901f00d4f59SWei Liu     buffer = rpath(ctx, path);
902f00d4f59SWei Liu     ret = truncate(buffer, size);
903f00d4f59SWei Liu     g_free(buffer);
904f00d4f59SWei Liu     return ret;
905f00d4f59SWei Liu }
906f00d4f59SWei Liu 
907f00d4f59SWei Liu static int local_rename(FsContext *ctx, const char *oldpath,
908f00d4f59SWei Liu                         const char *newpath)
909f00d4f59SWei Liu {
910f00d4f59SWei Liu     int err;
911f00d4f59SWei Liu     char *buffer, *buffer1;
912f00d4f59SWei Liu 
913f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
914f00d4f59SWei Liu         err = local_create_mapped_attr_dir(ctx, newpath);
915f00d4f59SWei Liu         if (err < 0) {
916f00d4f59SWei Liu             return err;
917f00d4f59SWei Liu         }
918f00d4f59SWei Liu         /* rename the .virtfs_metadata files */
919f00d4f59SWei Liu         buffer = local_mapped_attr_path(ctx, oldpath);
920f00d4f59SWei Liu         buffer1 = local_mapped_attr_path(ctx, newpath);
921f00d4f59SWei Liu         err = rename(buffer, buffer1);
922f00d4f59SWei Liu         g_free(buffer);
923f00d4f59SWei Liu         g_free(buffer1);
924f00d4f59SWei Liu         if (err < 0 && errno != ENOENT) {
925f00d4f59SWei Liu             return err;
926f00d4f59SWei Liu         }
927f00d4f59SWei Liu     }
928f00d4f59SWei Liu 
929f00d4f59SWei Liu     buffer = rpath(ctx, oldpath);
930f00d4f59SWei Liu     buffer1 = rpath(ctx, newpath);
931f00d4f59SWei Liu     err = rename(buffer, buffer1);
932f00d4f59SWei Liu     g_free(buffer);
933f00d4f59SWei Liu     g_free(buffer1);
934f00d4f59SWei Liu     return err;
935f00d4f59SWei Liu }
936f00d4f59SWei Liu 
937f00d4f59SWei Liu static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
938f00d4f59SWei Liu {
939f00d4f59SWei Liu     char *buffer;
940f00d4f59SWei Liu     int ret = -1;
941f00d4f59SWei Liu     char *path = fs_path->data;
942f00d4f59SWei Liu 
943f00d4f59SWei Liu     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
944f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
945f00d4f59SWei Liu         (fs_ctx->export_flags & V9FS_SM_NONE)) {
946f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
947f00d4f59SWei Liu         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
948f00d4f59SWei Liu         g_free(buffer);
949f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
950f00d4f59SWei Liu         buffer = rpath(fs_ctx, path);
951f00d4f59SWei Liu         ret = local_set_xattr(buffer, credp);
952f00d4f59SWei Liu         g_free(buffer);
953f00d4f59SWei Liu     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
954f00d4f59SWei Liu         return local_set_mapped_file_attr(fs_ctx, path, credp);
955f00d4f59SWei Liu     }
956f00d4f59SWei Liu     return ret;
957f00d4f59SWei Liu }
958f00d4f59SWei Liu 
959f00d4f59SWei Liu static int local_utimensat(FsContext *s, V9fsPath *fs_path,
960f00d4f59SWei Liu                            const struct timespec *buf)
961f00d4f59SWei Liu {
962f00d4f59SWei Liu     char *buffer;
963f00d4f59SWei Liu     int ret;
964f00d4f59SWei Liu     char *path = fs_path->data;
965f00d4f59SWei Liu 
966f00d4f59SWei Liu     buffer = rpath(s, path);
967f00d4f59SWei Liu     ret = qemu_utimens(buffer, buf);
968f00d4f59SWei Liu     g_free(buffer);
969f00d4f59SWei Liu     return ret;
970f00d4f59SWei Liu }
971f00d4f59SWei Liu 
972df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
973df4938a6SGreg Kurz                                  int flags)
974df4938a6SGreg Kurz {
975df4938a6SGreg Kurz     int ret = -1;
976df4938a6SGreg Kurz 
977df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
978df4938a6SGreg Kurz         int map_dirfd;
979df4938a6SGreg Kurz 
980df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
981df4938a6SGreg Kurz             int fd;
982df4938a6SGreg Kurz 
983df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
984df4938a6SGreg Kurz             if (fd == -1) {
985df4938a6SGreg Kurz                 goto err_out;
986df4938a6SGreg Kurz             }
987df4938a6SGreg Kurz             /*
988df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
989df4938a6SGreg Kurz              * directory
990df4938a6SGreg Kurz              */
991df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
992df4938a6SGreg Kurz             close_preserve_errno(fd);
993df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
994df4938a6SGreg Kurz                 /*
995df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
996df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
997df4938a6SGreg Kurz                  */
998df4938a6SGreg Kurz                 goto err_out;
999df4938a6SGreg Kurz             }
1000df4938a6SGreg Kurz         }
1001df4938a6SGreg Kurz         /*
1002df4938a6SGreg Kurz          * Now remove the name from parent directory
1003df4938a6SGreg Kurz          * .virtfs_metadata directory.
1004df4938a6SGreg Kurz          */
1005df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1006df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1007df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1008df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1009df4938a6SGreg Kurz             /*
1010df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1011df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1012df4938a6SGreg Kurz              */
1013df4938a6SGreg Kurz             goto err_out;
1014df4938a6SGreg Kurz         }
1015df4938a6SGreg Kurz     }
1016df4938a6SGreg Kurz 
1017df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1018df4938a6SGreg Kurz err_out:
1019df4938a6SGreg Kurz     return ret;
1020df4938a6SGreg Kurz }
1021df4938a6SGreg Kurz 
1022f00d4f59SWei Liu static int local_remove(FsContext *ctx, const char *path)
1023f00d4f59SWei Liu {
1024f00d4f59SWei Liu     struct stat stbuf;
1025*a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1026*a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1027*a0e640a8SGreg Kurz     int flags = 0;
1028*a0e640a8SGreg Kurz     int dirfd;
1029*a0e640a8SGreg Kurz     int err = -1;
1030f00d4f59SWei Liu 
1031*a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1032*a0e640a8SGreg Kurz     if (dirfd) {
1033*a0e640a8SGreg Kurz         goto out;
1034*a0e640a8SGreg Kurz     }
1035*a0e640a8SGreg Kurz 
1036*a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1037f00d4f59SWei Liu         goto err_out;
1038f00d4f59SWei Liu     }
1039*a0e640a8SGreg Kurz 
1040f00d4f59SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
1041*a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
1042f00d4f59SWei Liu     }
1043f00d4f59SWei Liu 
1044*a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
1045f00d4f59SWei Liu err_out:
1046*a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1047*a0e640a8SGreg Kurz out:
1048*a0e640a8SGreg Kurz     g_free(name);
1049*a0e640a8SGreg Kurz     g_free(dirpath);
1050f00d4f59SWei Liu     return err;
1051f00d4f59SWei Liu }
1052f00d4f59SWei Liu 
1053f00d4f59SWei Liu static int local_fsync(FsContext *ctx, int fid_type,
1054f00d4f59SWei Liu                        V9fsFidOpenState *fs, int datasync)
1055f00d4f59SWei Liu {
1056f00d4f59SWei Liu     int fd;
1057f00d4f59SWei Liu 
1058f00d4f59SWei Liu     if (fid_type == P9_FID_DIR) {
1059f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
1060f00d4f59SWei Liu     } else {
1061f00d4f59SWei Liu         fd = fs->fd;
1062f00d4f59SWei Liu     }
1063f00d4f59SWei Liu 
1064f00d4f59SWei Liu     if (datasync) {
1065f00d4f59SWei Liu         return qemu_fdatasync(fd);
1066f00d4f59SWei Liu     } else {
1067f00d4f59SWei Liu         return fsync(fd);
1068f00d4f59SWei Liu     }
1069f00d4f59SWei Liu }
1070f00d4f59SWei Liu 
1071f00d4f59SWei Liu static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1072f00d4f59SWei Liu {
1073f00d4f59SWei Liu     char *buffer;
1074f00d4f59SWei Liu     int ret;
1075f00d4f59SWei Liu     char *path = fs_path->data;
1076f00d4f59SWei Liu 
1077f00d4f59SWei Liu     buffer = rpath(s, path);
1078f00d4f59SWei Liu     ret = statfs(buffer, stbuf);
1079f00d4f59SWei Liu     g_free(buffer);
1080f00d4f59SWei Liu     return ret;
1081f00d4f59SWei Liu }
1082f00d4f59SWei Liu 
1083f00d4f59SWei Liu static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1084f00d4f59SWei Liu                                const char *name, void *value, size_t size)
1085f00d4f59SWei Liu {
1086f00d4f59SWei Liu     char *path = fs_path->data;
1087f00d4f59SWei Liu 
1088f00d4f59SWei Liu     return v9fs_get_xattr(ctx, path, name, value, size);
1089f00d4f59SWei Liu }
1090f00d4f59SWei Liu 
1091f00d4f59SWei Liu static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1092f00d4f59SWei Liu                                 void *value, size_t size)
1093f00d4f59SWei Liu {
1094f00d4f59SWei Liu     char *path = fs_path->data;
1095f00d4f59SWei Liu 
1096f00d4f59SWei Liu     return v9fs_list_xattr(ctx, path, value, size);
1097f00d4f59SWei Liu }
1098f00d4f59SWei Liu 
1099f00d4f59SWei Liu static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1100f00d4f59SWei Liu                            void *value, size_t size, int flags)
1101f00d4f59SWei Liu {
1102f00d4f59SWei Liu     char *path = fs_path->data;
1103f00d4f59SWei Liu 
1104f00d4f59SWei Liu     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1105f00d4f59SWei Liu }
1106f00d4f59SWei Liu 
1107f00d4f59SWei Liu static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1108f00d4f59SWei Liu                               const char *name)
1109f00d4f59SWei Liu {
1110f00d4f59SWei Liu     char *path = fs_path->data;
1111f00d4f59SWei Liu 
1112f00d4f59SWei Liu     return v9fs_remove_xattr(ctx, path, name);
1113f00d4f59SWei Liu }
1114f00d4f59SWei Liu 
1115f00d4f59SWei Liu static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1116f00d4f59SWei Liu                               const char *name, V9fsPath *target)
1117f00d4f59SWei Liu {
1118f00d4f59SWei Liu     if (dir_path) {
1119e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1120f00d4f59SWei Liu     } else {
1121e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
1122f00d4f59SWei Liu     }
1123f00d4f59SWei Liu     return 0;
1124f00d4f59SWei Liu }
1125f00d4f59SWei Liu 
1126f00d4f59SWei Liu static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1127f00d4f59SWei Liu                           const char *old_name, V9fsPath *newdir,
1128f00d4f59SWei Liu                           const char *new_name)
1129f00d4f59SWei Liu {
1130f00d4f59SWei Liu     int ret;
1131f00d4f59SWei Liu     V9fsString old_full_name, new_full_name;
1132f00d4f59SWei Liu 
1133f00d4f59SWei Liu     v9fs_string_init(&old_full_name);
1134f00d4f59SWei Liu     v9fs_string_init(&new_full_name);
1135f00d4f59SWei Liu 
1136f00d4f59SWei Liu     v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1137f00d4f59SWei Liu     v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1138f00d4f59SWei Liu 
1139f00d4f59SWei Liu     ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1140f00d4f59SWei Liu     v9fs_string_free(&old_full_name);
1141f00d4f59SWei Liu     v9fs_string_free(&new_full_name);
1142f00d4f59SWei Liu     return ret;
1143f00d4f59SWei Liu }
1144f00d4f59SWei Liu 
1145f00d4f59SWei Liu static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1146f00d4f59SWei Liu                           const char *name, int flags)
1147f00d4f59SWei Liu {
1148f00d4f59SWei Liu     int ret;
1149df4938a6SGreg Kurz     int dirfd;
1150f00d4f59SWei Liu 
1151df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1152df4938a6SGreg Kurz     if (dirfd == -1) {
1153df4938a6SGreg Kurz         return -1;
1154df4938a6SGreg Kurz     }
1155f00d4f59SWei Liu 
1156df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1157df4938a6SGreg Kurz     close_preserve_errno(dirfd);
1158f00d4f59SWei Liu     return ret;
1159f00d4f59SWei Liu }
1160f00d4f59SWei Liu 
1161f00d4f59SWei Liu static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1162f00d4f59SWei Liu                                 mode_t st_mode, uint64_t *st_gen)
1163f00d4f59SWei Liu {
1164f00d4f59SWei Liu #ifdef FS_IOC_GETVERSION
1165f00d4f59SWei Liu     int err;
1166f00d4f59SWei Liu     V9fsFidOpenState fid_open;
1167f00d4f59SWei Liu 
1168f00d4f59SWei Liu     /*
1169f00d4f59SWei Liu      * Do not try to open special files like device nodes, fifos etc
1170f00d4f59SWei Liu      * We can get fd for regular files and directories only
1171f00d4f59SWei Liu      */
1172f00d4f59SWei Liu     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1173f00d4f59SWei Liu         errno = ENOTTY;
1174f00d4f59SWei Liu         return -1;
1175f00d4f59SWei Liu     }
1176f00d4f59SWei Liu     err = local_open(ctx, path, O_RDONLY, &fid_open);
1177f00d4f59SWei Liu     if (err < 0) {
1178f00d4f59SWei Liu         return err;
1179f00d4f59SWei Liu     }
1180f00d4f59SWei Liu     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1181f00d4f59SWei Liu     local_close(ctx, &fid_open);
1182f00d4f59SWei Liu     return err;
1183f00d4f59SWei Liu #else
1184f00d4f59SWei Liu     errno = ENOTTY;
1185f00d4f59SWei Liu     return -1;
1186f00d4f59SWei Liu #endif
1187f00d4f59SWei Liu }
1188f00d4f59SWei Liu 
1189f00d4f59SWei Liu static int local_init(FsContext *ctx)
1190f00d4f59SWei Liu {
1191f00d4f59SWei Liu     struct statfs stbuf;
11920e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
11930e35a378SGreg Kurz 
11940e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
11950e35a378SGreg Kurz     if (data->mountfd == -1) {
11960e35a378SGreg Kurz         goto err;
11970e35a378SGreg Kurz     }
1198f00d4f59SWei Liu 
119900c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
120000c90bd1SGreg Kurz     /*
120100c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
120200c90bd1SGreg Kurz      */
12030e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
12040e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
12050e35a378SGreg Kurz         goto err;
120600c90bd1SGreg Kurz     }
120700c90bd1SGreg Kurz     switch (stbuf.f_type) {
120800c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
120900c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
121000c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
121100c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
121200c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
121300c90bd1SGreg Kurz         break;
121400c90bd1SGreg Kurz     }
121500c90bd1SGreg Kurz #endif
121600c90bd1SGreg Kurz 
1217f00d4f59SWei Liu     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1218f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1219f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1220f00d4f59SWei Liu         ctx->xops = mapped_xattr_ops;
1221f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_NONE) {
1222f00d4f59SWei Liu         ctx->xops = none_xattr_ops;
1223f00d4f59SWei Liu     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1224f00d4f59SWei Liu         /*
1225f00d4f59SWei Liu          * xattr operation for mapped-file and passthrough
1226f00d4f59SWei Liu          * remain same.
1227f00d4f59SWei Liu          */
1228f00d4f59SWei Liu         ctx->xops = passthrough_xattr_ops;
1229f00d4f59SWei Liu     }
1230f00d4f59SWei Liu     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
123100c90bd1SGreg Kurz 
12320e35a378SGreg Kurz     ctx->private = data;
123300c90bd1SGreg Kurz     return 0;
12340e35a378SGreg Kurz 
12350e35a378SGreg Kurz err:
12360e35a378SGreg Kurz     g_free(data);
12370e35a378SGreg Kurz     return -1;
12380e35a378SGreg Kurz }
12390e35a378SGreg Kurz 
12400e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
12410e35a378SGreg Kurz {
12420e35a378SGreg Kurz     LocalData *data = ctx->private;
12430e35a378SGreg Kurz 
12440e35a378SGreg Kurz     close(data->mountfd);
12450e35a378SGreg Kurz     g_free(data);
1246f00d4f59SWei Liu }
1247f00d4f59SWei Liu 
1248f00d4f59SWei Liu static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1249f00d4f59SWei Liu {
1250f00d4f59SWei Liu     const char *sec_model = qemu_opt_get(opts, "security_model");
1251f00d4f59SWei Liu     const char *path = qemu_opt_get(opts, "path");
1252f00d4f59SWei Liu 
1253f00d4f59SWei Liu     if (!sec_model) {
125463325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
125563325b18SGreg Kurz         error_printf("valid options are:"
125663325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1257f00d4f59SWei Liu         return -1;
1258f00d4f59SWei Liu     }
1259f00d4f59SWei Liu 
1260f00d4f59SWei Liu     if (!strcmp(sec_model, "passthrough")) {
1261f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1262f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped") ||
1263f00d4f59SWei Liu                !strcmp(sec_model, "mapped-xattr")) {
1264f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED;
1265f00d4f59SWei Liu     } else if (!strcmp(sec_model, "none")) {
1266f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_NONE;
1267f00d4f59SWei Liu     } else if (!strcmp(sec_model, "mapped-file")) {
1268f00d4f59SWei Liu         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1269f00d4f59SWei Liu     } else {
127063325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
127163325b18SGreg Kurz         error_printf("valid options are:"
127263325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1273f00d4f59SWei Liu         return -1;
1274f00d4f59SWei Liu     }
1275f00d4f59SWei Liu 
1276f00d4f59SWei Liu     if (!path) {
127763325b18SGreg Kurz         error_report("fsdev: No path specified");
1278f00d4f59SWei Liu         return -1;
1279f00d4f59SWei Liu     }
1280f00d4f59SWei Liu     fse->path = g_strdup(path);
1281f00d4f59SWei Liu 
1282f00d4f59SWei Liu     return 0;
1283f00d4f59SWei Liu }
1284f00d4f59SWei Liu 
1285f00d4f59SWei Liu FileOperations local_ops = {
1286f00d4f59SWei Liu     .parse_opts = local_parse_opts,
1287f00d4f59SWei Liu     .init  = local_init,
12880e35a378SGreg Kurz     .cleanup = local_cleanup,
1289f00d4f59SWei Liu     .lstat = local_lstat,
1290f00d4f59SWei Liu     .readlink = local_readlink,
1291f00d4f59SWei Liu     .close = local_close,
1292f00d4f59SWei Liu     .closedir = local_closedir,
1293f00d4f59SWei Liu     .open = local_open,
1294f00d4f59SWei Liu     .opendir = local_opendir,
1295f00d4f59SWei Liu     .rewinddir = local_rewinddir,
1296f00d4f59SWei Liu     .telldir = local_telldir,
1297635324e8SGreg Kurz     .readdir = local_readdir,
1298f00d4f59SWei Liu     .seekdir = local_seekdir,
1299f00d4f59SWei Liu     .preadv = local_preadv,
1300f00d4f59SWei Liu     .pwritev = local_pwritev,
1301f00d4f59SWei Liu     .chmod = local_chmod,
1302f00d4f59SWei Liu     .mknod = local_mknod,
1303f00d4f59SWei Liu     .mkdir = local_mkdir,
1304f00d4f59SWei Liu     .fstat = local_fstat,
1305f00d4f59SWei Liu     .open2 = local_open2,
1306f00d4f59SWei Liu     .symlink = local_symlink,
1307f00d4f59SWei Liu     .link = local_link,
1308f00d4f59SWei Liu     .truncate = local_truncate,
1309f00d4f59SWei Liu     .rename = local_rename,
1310f00d4f59SWei Liu     .chown = local_chown,
1311f00d4f59SWei Liu     .utimensat = local_utimensat,
1312f00d4f59SWei Liu     .remove = local_remove,
1313f00d4f59SWei Liu     .fsync = local_fsync,
1314f00d4f59SWei Liu     .statfs = local_statfs,
1315f00d4f59SWei Liu     .lgetxattr = local_lgetxattr,
1316f00d4f59SWei Liu     .llistxattr = local_llistxattr,
1317f00d4f59SWei Liu     .lsetxattr = local_lsetxattr,
1318f00d4f59SWei Liu     .lremovexattr = local_lremovexattr,
1319f00d4f59SWei Liu     .name_to_path = local_name_to_path,
1320f00d4f59SWei Liu     .renameat  = local_renameat,
1321f00d4f59SWei Liu     .unlinkat = local_unlinkat,
1322f00d4f59SWei Liu };
1323