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