xref: /qemu/hw/9pfs/9p-local.c (revision d2767edec582558f1e6c52e1dd9370d62e2b30fc)
19f107513SAnthony Liguori /*
2f00d4f59SWei Liu  * 9p Posix callback
39f107513SAnthony Liguori  *
49f107513SAnthony Liguori  * Copyright IBM, Corp. 2010
59f107513SAnthony Liguori  *
69f107513SAnthony Liguori  * Authors:
79f107513SAnthony Liguori  *  Anthony Liguori   <aliguori@us.ibm.com>
89f107513SAnthony Liguori  *
99f107513SAnthony Liguori  * This work is licensed under the terms of the GNU GPL, version 2.  See
109f107513SAnthony Liguori  * the COPYING file in the top-level directory.
119f107513SAnthony Liguori  *
129f107513SAnthony Liguori  */
13873c3213SStefan Weil 
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"
1969b15212SStefan Weil #include "fsdev/qemu-fsdev.h"   /* local_ops */
20c494dd6fSAnthony Liguori #include <arpa/inet.h>
21131dcb25SAnthony Liguori #include <pwd.h>
22131dcb25SAnthony Liguori #include <grp.h>
23c494dd6fSAnthony Liguori #include <sys/socket.h>
24c494dd6fSAnthony Liguori #include <sys/un.h>
251de7afc9SPaolo Bonzini #include "qemu/xattr.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
2763325b18SGreg Kurz #include "qemu/error-report.h"
282c30dd74SAneesh Kumar K.V #include <libgen.h>
29e06a765eSHarsh Prateek Bora #include <linux/fs.h>
30e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H
31e06a765eSHarsh Prateek Bora #include <linux/magic.h>
32e06a765eSHarsh Prateek Bora #endif
33e06a765eSHarsh Prateek Bora #include <sys/ioctl.h>
34e06a765eSHarsh Prateek Bora 
35e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC
36e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC  0x58465342
37e06a765eSHarsh Prateek Bora #endif
38e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC
39e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53
40e06a765eSHarsh Prateek Bora #endif
41e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC
42e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973
43e06a765eSHarsh Prateek Bora #endif
44e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC
45e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E
46e06a765eSHarsh Prateek Bora #endif
47131dcb25SAnthony Liguori 
480e35a378SGreg Kurz typedef struct {
490e35a378SGreg Kurz     int mountfd;
500e35a378SGreg Kurz } LocalData;
510e35a378SGreg Kurz 
52996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53996a0d76SGreg Kurz                         mode_t mode)
54996a0d76SGreg Kurz {
55996a0d76SGreg Kurz     LocalData *data = fs_ctx->private;
56996a0d76SGreg Kurz 
57996a0d76SGreg Kurz     /* All paths are relative to the path data->mountfd points to */
58996a0d76SGreg Kurz     while (*path == '/') {
59996a0d76SGreg Kurz         path++;
60996a0d76SGreg Kurz     }
61996a0d76SGreg Kurz 
62996a0d76SGreg Kurz     return relative_openat_nofollow(data->mountfd, path, flags, mode);
63996a0d76SGreg Kurz }
64996a0d76SGreg Kurz 
65996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66996a0d76SGreg Kurz {
67996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68996a0d76SGreg Kurz }
69996a0d76SGreg Kurz 
7099f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
7199f2cf4bSGreg Kurz                                     const char *npath)
7299f2cf4bSGreg Kurz {
7399f2cf4bSGreg Kurz     int serrno = errno;
7499f2cf4bSGreg Kurz     renameat(odirfd, opath, ndirfd, npath);
7599f2cf4bSGreg Kurz     errno = serrno;
7699f2cf4bSGreg Kurz }
7799f2cf4bSGreg Kurz 
782c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
792c30dd74SAneesh Kumar K.V 
804fa4ce71SChen Gang static char *local_mapped_attr_path(FsContext *ctx, const char *path)
812c30dd74SAneesh Kumar K.V {
821b6f85e2SMichael Tokarev     int dirlen;
831b6f85e2SMichael Tokarev     const char *name = strrchr(path, '/');
841b6f85e2SMichael Tokarev     if (name) {
851b6f85e2SMichael Tokarev         dirlen = name - path;
861b6f85e2SMichael Tokarev         ++name;
871b6f85e2SMichael Tokarev     } else {
881b6f85e2SMichael Tokarev         name = path;
891b6f85e2SMichael Tokarev         dirlen = 0;
901b6f85e2SMichael Tokarev     }
911b6f85e2SMichael Tokarev     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
921b6f85e2SMichael Tokarev                            dirlen, path, VIRTFS_META_DIR, name);
932c30dd74SAneesh Kumar K.V }
942c30dd74SAneesh Kumar K.V 
950ceb092eSAneesh Kumar K.V static FILE *local_fopen(const char *path, const char *mode)
960ceb092eSAneesh Kumar K.V {
970ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
980ceb092eSAneesh Kumar K.V     FILE *fp;
990ceb092eSAneesh Kumar K.V     int flags = O_NOFOLLOW;
1000ceb092eSAneesh Kumar K.V     /*
1010ceb092eSAneesh Kumar K.V      * only supports two modes
1020ceb092eSAneesh Kumar K.V      */
1030ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
1040ceb092eSAneesh Kumar K.V         flags |= O_RDONLY;
1050ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
1060ceb092eSAneesh Kumar K.V         flags |= O_WRONLY | O_TRUNC | O_CREAT;
1070ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1080ceb092eSAneesh Kumar K.V     } else {
1090ceb092eSAneesh Kumar K.V         return NULL;
1100ceb092eSAneesh Kumar K.V     }
1110ceb092eSAneesh Kumar K.V     fd = open(path, flags, o_mode);
1120ceb092eSAneesh Kumar K.V     if (fd == -1) {
1130ceb092eSAneesh Kumar K.V         return NULL;
1140ceb092eSAneesh Kumar K.V     }
1150ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1160ceb092eSAneesh Kumar K.V     if (!fp) {
1170ceb092eSAneesh Kumar K.V         close(fd);
1180ceb092eSAneesh Kumar K.V     }
1190ceb092eSAneesh Kumar K.V     return fp;
1200ceb092eSAneesh Kumar K.V }
1210ceb092eSAneesh Kumar K.V 
122f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
123f9aef99bSGreg Kurz {
124f9aef99bSGreg Kurz     int fd, o_mode = 0;
125f9aef99bSGreg Kurz     FILE *fp;
126f9aef99bSGreg Kurz     int flags;
127f9aef99bSGreg Kurz     /*
128f9aef99bSGreg Kurz      * only supports two modes
129f9aef99bSGreg Kurz      */
130f9aef99bSGreg Kurz     if (mode[0] == 'r') {
131f9aef99bSGreg Kurz         flags = O_RDONLY;
132f9aef99bSGreg Kurz     } else if (mode[0] == 'w') {
133f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
134f9aef99bSGreg Kurz         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
135f9aef99bSGreg Kurz     } else {
136f9aef99bSGreg Kurz         return NULL;
137f9aef99bSGreg Kurz     }
138f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
139f9aef99bSGreg Kurz     if (fd == -1) {
140f9aef99bSGreg Kurz         return NULL;
141f9aef99bSGreg Kurz     }
142f9aef99bSGreg Kurz     fp = fdopen(fd, mode);
143f9aef99bSGreg Kurz     if (!fp) {
144f9aef99bSGreg Kurz         close(fd);
145f9aef99bSGreg Kurz     }
146f9aef99bSGreg Kurz     return fp;
147f9aef99bSGreg Kurz }
148f9aef99bSGreg Kurz 
1492c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
150f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
1512c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1522c30dd74SAneesh Kumar K.V {
1532c30dd74SAneesh Kumar K.V     FILE *fp;
1542c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
155f9aef99bSGreg Kurz     int map_dirfd;
1562c30dd74SAneesh Kumar K.V 
15799f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
158f9aef99bSGreg Kurz     if (map_dirfd == -1) {
159f9aef99bSGreg Kurz         return;
160f9aef99bSGreg Kurz     }
161f9aef99bSGreg Kurz 
162f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
163f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
1642c30dd74SAneesh Kumar K.V     if (!fp) {
1652c30dd74SAneesh Kumar K.V         return;
1662c30dd74SAneesh Kumar K.V     }
1672c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1682c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1692c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1702c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1712c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1722c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1732c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1742c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1752c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1762c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1772c30dd74SAneesh Kumar K.V         }
1782c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1792c30dd74SAneesh Kumar K.V     }
1802c30dd74SAneesh Kumar K.V     fclose(fp);
1812c30dd74SAneesh Kumar K.V }
1822c30dd74SAneesh Kumar K.V 
1832289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
184131dcb25SAnthony Liguori {
185f9aef99bSGreg Kurz     int err = -1;
186f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
187f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
188f9aef99bSGreg Kurz     int dirfd;
1892289be19SAneesh Kumar K.V 
190f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
191f9aef99bSGreg Kurz     if (dirfd == -1) {
192f9aef99bSGreg Kurz         goto out;
193f9aef99bSGreg Kurz     }
194f9aef99bSGreg Kurz 
195f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
1961237ad76SVenkateswararao Jujjuri (JV)     if (err) {
1974fa4ce71SChen Gang         goto err_out;
1981237ad76SVenkateswararao Jujjuri (JV)     }
199b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
2001237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
2011237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
2021237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
2031237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
2041237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
205f9aef99bSGreg Kurz 
206f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
207f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
208f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
2091237ad76SVenkateswararao Jujjuri (JV)         }
210f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
211f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
212f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
2131237ad76SVenkateswararao Jujjuri (JV)         }
214f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
215f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
216f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
2171237ad76SVenkateswararao Jujjuri (JV)         }
218f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
219f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
220f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
2211237ad76SVenkateswararao Jujjuri (JV)         }
2222c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
223f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
2241237ad76SVenkateswararao Jujjuri (JV)     }
2254fa4ce71SChen Gang 
2264fa4ce71SChen Gang err_out:
227f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
228f9aef99bSGreg Kurz out:
229f9aef99bSGreg Kurz     g_free(name);
230f9aef99bSGreg Kurz     g_free(dirpath);
2311237ad76SVenkateswararao Jujjuri (JV)     return err;
232131dcb25SAnthony Liguori }
233131dcb25SAnthony Liguori 
2342c30dd74SAneesh Kumar K.V static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
2352c30dd74SAneesh Kumar K.V {
2362c30dd74SAneesh Kumar K.V     int err;
2374fa4ce71SChen Gang     char *attr_dir;
238d3f8e138SMarkus Armbruster     char *tmp_path = g_strdup(path);
2392c30dd74SAneesh Kumar K.V 
2404fa4ce71SChen Gang     attr_dir = g_strdup_printf("%s/%s/%s",
2412c30dd74SAneesh Kumar K.V              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
2422c30dd74SAneesh Kumar K.V 
2432c30dd74SAneesh Kumar K.V     err = mkdir(attr_dir, 0700);
2442c30dd74SAneesh Kumar K.V     if (err < 0 && errno == EEXIST) {
2452c30dd74SAneesh Kumar K.V         err = 0;
2462c30dd74SAneesh Kumar K.V     }
2474fa4ce71SChen Gang     g_free(attr_dir);
248d3f8e138SMarkus Armbruster     g_free(tmp_path);
2492c30dd74SAneesh Kumar K.V     return err;
2502c30dd74SAneesh Kumar K.V }
2512c30dd74SAneesh Kumar K.V 
2522c30dd74SAneesh Kumar K.V static int local_set_mapped_file_attr(FsContext *ctx,
2532c30dd74SAneesh Kumar K.V                                       const char *path, FsCred *credp)
2542c30dd74SAneesh Kumar K.V {
2552c30dd74SAneesh Kumar K.V     FILE *fp;
2562c30dd74SAneesh Kumar K.V     int ret = 0;
2572c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2584fa4ce71SChen Gang     char *attr_path;
2592c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
2602c30dd74SAneesh Kumar K.V 
2614fa4ce71SChen Gang     attr_path = local_mapped_attr_path(ctx, path);
2624fa4ce71SChen Gang     fp = local_fopen(attr_path, "r");
2632c30dd74SAneesh Kumar K.V     if (!fp) {
2642c30dd74SAneesh Kumar K.V         goto create_map_file;
2652c30dd74SAneesh Kumar K.V     }
2662c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2672c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2682c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2692c30dd74SAneesh Kumar K.V             uid = atoi(buf+11);
2702c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2712c30dd74SAneesh Kumar K.V             gid = atoi(buf+11);
2722c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2732c30dd74SAneesh Kumar K.V             mode = atoi(buf+12);
2742c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2752c30dd74SAneesh Kumar K.V             rdev = atoi(buf+12);
2762c30dd74SAneesh Kumar K.V         }
2772c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2782c30dd74SAneesh Kumar K.V     }
2792c30dd74SAneesh Kumar K.V     fclose(fp);
2802c30dd74SAneesh Kumar K.V     goto update_map_file;
2812c30dd74SAneesh Kumar K.V 
2822c30dd74SAneesh Kumar K.V create_map_file:
2832c30dd74SAneesh Kumar K.V     ret = local_create_mapped_attr_dir(ctx, path);
2842c30dd74SAneesh Kumar K.V     if (ret < 0) {
2852c30dd74SAneesh Kumar K.V         goto err_out;
2862c30dd74SAneesh Kumar K.V     }
2872c30dd74SAneesh Kumar K.V 
2882c30dd74SAneesh Kumar K.V update_map_file:
2890ceb092eSAneesh Kumar K.V     fp = local_fopen(attr_path, "w");
2902c30dd74SAneesh Kumar K.V     if (!fp) {
2912c30dd74SAneesh Kumar K.V         ret = -1;
2922c30dd74SAneesh Kumar K.V         goto err_out;
2932c30dd74SAneesh Kumar K.V     }
2942c30dd74SAneesh Kumar K.V 
2952c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
2962c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
2972c30dd74SAneesh Kumar K.V     }
2982c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
2992c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
3002c30dd74SAneesh Kumar K.V     }
3012c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
3022c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
3032c30dd74SAneesh Kumar K.V     }
3042c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
3052c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
3062c30dd74SAneesh Kumar K.V     }
3072c30dd74SAneesh Kumar K.V 
3082c30dd74SAneesh Kumar K.V 
3092c30dd74SAneesh Kumar K.V     if (uid != -1) {
3102c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
3112c30dd74SAneesh Kumar K.V     }
3122c30dd74SAneesh Kumar K.V     if (gid != -1) {
3132c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
3142c30dd74SAneesh Kumar K.V     }
3152c30dd74SAneesh Kumar K.V     if (mode != -1) {
3162c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
3172c30dd74SAneesh Kumar K.V     }
3182c30dd74SAneesh Kumar K.V     if (rdev != -1) {
3192c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
3202c30dd74SAneesh Kumar K.V     }
3212c30dd74SAneesh Kumar K.V     fclose(fp);
3222c30dd74SAneesh Kumar K.V 
3232c30dd74SAneesh Kumar K.V err_out:
3244fa4ce71SChen Gang     g_free(attr_path);
3252c30dd74SAneesh Kumar K.V     return ret;
3262c30dd74SAneesh Kumar K.V }
3272c30dd74SAneesh Kumar K.V 
328758e8e38SVenkateswararao Jujjuri (JV) static int local_set_xattr(const char *path, FsCred *credp)
329131dcb25SAnthony Liguori {
330758e8e38SVenkateswararao Jujjuri (JV)     int err;
3312289be19SAneesh Kumar K.V 
332758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
333f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
334f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
335758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
336758e8e38SVenkateswararao Jujjuri (JV)             return err;
337131dcb25SAnthony Liguori         }
338131dcb25SAnthony Liguori     }
339758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
340f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
341f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
342758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
343758e8e38SVenkateswararao Jujjuri (JV)             return err;
344131dcb25SAnthony Liguori         }
345131dcb25SAnthony Liguori     }
346758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
347f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
348f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
349758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
350758e8e38SVenkateswararao Jujjuri (JV)             return err;
351131dcb25SAnthony Liguori         }
352131dcb25SAnthony Liguori     }
353758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
354f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
355f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
356758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
357758e8e38SVenkateswararao Jujjuri (JV)             return err;
358131dcb25SAnthony Liguori         }
359758e8e38SVenkateswararao Jujjuri (JV)     }
360131dcb25SAnthony Liguori     return 0;
361131dcb25SAnthony Liguori }
362131dcb25SAnthony Liguori 
3634750a96fSVenkateswararao Jujjuri (JV) static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
3644750a96fSVenkateswararao Jujjuri (JV)                                          FsCred *credp)
3654750a96fSVenkateswararao Jujjuri (JV) {
3664fa4ce71SChen Gang     char *buffer;
3672289be19SAneesh Kumar K.V 
3684fa4ce71SChen Gang     buffer = rpath(fs_ctx, path);
3694fa4ce71SChen Gang     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
37012848bfcSAneesh Kumar K.V         /*
37112848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
37212848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
37312848bfcSAneesh Kumar K.V          */
374b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
3754fa4ce71SChen Gang             goto err;
3764750a96fSVenkateswararao Jujjuri (JV)         }
37712848bfcSAneesh Kumar K.V     }
3782d40564aSM. Mohan Kumar 
3794fa4ce71SChen Gang     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
3804fa4ce71SChen Gang         goto err;
3812d40564aSM. Mohan Kumar     }
3824fa4ce71SChen Gang 
3834fa4ce71SChen Gang     g_free(buffer);
3844750a96fSVenkateswararao Jujjuri (JV)     return 0;
3854fa4ce71SChen Gang err:
3864fa4ce71SChen Gang     g_free(buffer);
3874fa4ce71SChen Gang     return -1;
3884750a96fSVenkateswararao Jujjuri (JV) }
3894750a96fSVenkateswararao Jujjuri (JV) 
3902289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
391131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
392131dcb25SAnthony Liguori {
393879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
3942289be19SAneesh Kumar K.V 
3952c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
3962c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
397879c2813SVenkateswararao Jujjuri (JV)         int fd;
398bec1e954SGreg Kurz 
399bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
400879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
401879c2813SVenkateswararao Jujjuri (JV)             return -1;
402879c2813SVenkateswararao Jujjuri (JV)         }
403879c2813SVenkateswararao Jujjuri (JV)         do {
404879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
405879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
406bec1e954SGreg Kurz         close_preserve_errno(fd);
407b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
408b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
409bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
410bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
411bec1e954SGreg Kurz         int dirfd;
412bec1e954SGreg Kurz 
413bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
414bec1e954SGreg Kurz         if (dirfd == -1) {
415bec1e954SGreg Kurz             goto out;
416bec1e954SGreg Kurz         }
417bec1e954SGreg Kurz 
418bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
419bec1e954SGreg Kurz         close_preserve_errno(dirfd);
420bec1e954SGreg Kurz     out:
421bec1e954SGreg Kurz         g_free(name);
422bec1e954SGreg Kurz         g_free(dirpath);
423879c2813SVenkateswararao Jujjuri (JV)     }
424879c2813SVenkateswararao Jujjuri (JV)     return tsize;
425131dcb25SAnthony Liguori }
426131dcb25SAnthony Liguori 
427cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
428131dcb25SAnthony Liguori {
429cc720ddbSAneesh Kumar K.V     return close(fs->fd);
430131dcb25SAnthony Liguori }
431131dcb25SAnthony Liguori 
432cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
433131dcb25SAnthony Liguori {
434f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
435131dcb25SAnthony Liguori }
4369f107513SAnthony Liguori 
437cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
438cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
439a6568fe2SAnthony Liguori {
44021328e1eSGreg Kurz     int fd;
4412289be19SAneesh Kumar K.V 
442996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
44321328e1eSGreg Kurz     if (fd == -1) {
44421328e1eSGreg Kurz         return -1;
44521328e1eSGreg Kurz     }
44621328e1eSGreg Kurz     fs->fd = fd;
447cc720ddbSAneesh Kumar K.V     return fs->fd;
448a6568fe2SAnthony Liguori }
449a6568fe2SAnthony Liguori 
450cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
451cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
452a6568fe2SAnthony Liguori {
453996a0d76SGreg Kurz     int dirfd;
45421328e1eSGreg Kurz     DIR *stream;
4552289be19SAneesh Kumar K.V 
456996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
457996a0d76SGreg Kurz     if (dirfd == -1) {
458996a0d76SGreg Kurz         return -1;
459996a0d76SGreg Kurz     }
460996a0d76SGreg Kurz 
461996a0d76SGreg Kurz     stream = fdopendir(dirfd);
46221328e1eSGreg Kurz     if (!stream) {
463cc720ddbSAneesh Kumar K.V         return -1;
464cc720ddbSAneesh Kumar K.V     }
46521328e1eSGreg Kurz     fs->dir.stream = stream;
466cc720ddbSAneesh Kumar K.V     return 0;
467a6568fe2SAnthony Liguori }
468a6568fe2SAnthony Liguori 
469cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
470a9231555SAnthony Liguori {
471f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
472a9231555SAnthony Liguori }
473a9231555SAnthony Liguori 
474cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
475a9231555SAnthony Liguori {
476f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
477a9231555SAnthony Liguori }
478a9231555SAnthony Liguori 
479635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
480a9231555SAnthony Liguori {
481635324e8SGreg Kurz     struct dirent *entry;
4822c30dd74SAneesh Kumar K.V 
4832c30dd74SAneesh Kumar K.V again:
484635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
485635324e8SGreg Kurz     if (!entry) {
486635324e8SGreg Kurz         return NULL;
487635324e8SGreg Kurz     }
488635324e8SGreg Kurz 
489840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
490840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
491840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
492635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
4932c30dd74SAneesh Kumar K.V             /* skp the meta data directory */
4942c30dd74SAneesh Kumar K.V             goto again;
4952c30dd74SAneesh Kumar K.V         }
496840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
4972c30dd74SAneesh Kumar K.V     }
498635324e8SGreg Kurz 
499635324e8SGreg Kurz     return entry;
500a9231555SAnthony Liguori }
501a9231555SAnthony Liguori 
502cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
503a9231555SAnthony Liguori {
504f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
505a9231555SAnthony Liguori }
506a9231555SAnthony Liguori 
507cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
508cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
50956d15a53SSanchit Garg                             int iovcnt, off_t offset)
510a9231555SAnthony Liguori {
51156d15a53SSanchit Garg #ifdef CONFIG_PREADV
512cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
51356d15a53SSanchit Garg #else
514cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
51556d15a53SSanchit Garg     if (err == -1) {
51656d15a53SSanchit Garg         return err;
51756d15a53SSanchit Garg     } else {
518cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
519a9231555SAnthony Liguori     }
52056d15a53SSanchit Garg #endif
521a9231555SAnthony Liguori }
522a9231555SAnthony Liguori 
523cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
524cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
52556d15a53SSanchit Garg                              int iovcnt, off_t offset)
5268449360cSAnthony Liguori {
5276fe76accSGreg Kurz     ssize_t ret;
52856d15a53SSanchit Garg #ifdef CONFIG_PREADV
529cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
53056d15a53SSanchit Garg #else
531cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
53256d15a53SSanchit Garg     if (err == -1) {
53356d15a53SSanchit Garg         return err;
53456d15a53SSanchit Garg     } else {
535cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
5368449360cSAnthony Liguori     }
53756d15a53SSanchit Garg #endif
538d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
539d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
540d3ab98e6SAneesh Kumar K.V         /*
541d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
542d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
543d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
544d3ab98e6SAneesh Kumar K.V          */
545cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
546d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
547d3ab98e6SAneesh Kumar K.V     }
548d3ab98e6SAneesh Kumar K.V #endif
549d3ab98e6SAneesh Kumar K.V     return ret;
55056d15a53SSanchit Garg }
5518449360cSAnthony Liguori 
5522289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
553c494dd6fSAnthony Liguori {
5544fa4ce71SChen Gang     char *buffer;
5554fa4ce71SChen Gang     int ret = -1;
5562289be19SAneesh Kumar K.V     char *path = fs_path->data;
5572289be19SAneesh Kumar K.V 
558b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5594fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5604fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
5614fa4ce71SChen Gang         g_free(buffer);
5622c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5632c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
564b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
565b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
5664fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5674fa4ce71SChen Gang         ret = chmod(buffer, credp->fc_mode);
5684fa4ce71SChen Gang         g_free(buffer);
569e95ead32SVenkateswararao Jujjuri (JV)     }
5704fa4ce71SChen Gang     return ret;
571c494dd6fSAnthony Liguori }
572c494dd6fSAnthony Liguori 
5732289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
5742289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
575c494dd6fSAnthony Liguori {
5762289be19SAneesh Kumar K.V     char *path;
5771c293312SVenkateswararao Jujjuri (JV)     int err = -1;
5781c293312SVenkateswararao Jujjuri (JV)     int serrno = 0;
5792289be19SAneesh Kumar K.V     V9fsString fullname;
5804ed7b2c3SStefan Weil     char *buffer = NULL;
5811c293312SVenkateswararao Jujjuri (JV) 
5822289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
5832289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
5842289be19SAneesh Kumar K.V     path = fullname.data;
5852289be19SAneesh Kumar K.V 
5861c293312SVenkateswararao Jujjuri (JV)     /* Determine the security model */
587b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5884fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5894fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
5901c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5912289be19SAneesh Kumar K.V             goto out;
5921c293312SVenkateswararao Jujjuri (JV)         }
5934fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
5941c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5951c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
5961c293312SVenkateswararao Jujjuri (JV)             goto err_end;
5971c293312SVenkateswararao Jujjuri (JV)         }
5982c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5992c30dd74SAneesh Kumar K.V 
6004fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6014fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
6022c30dd74SAneesh Kumar K.V         if (err == -1) {
6032c30dd74SAneesh Kumar K.V             goto out;
6042c30dd74SAneesh Kumar K.V         }
6052c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
6062c30dd74SAneesh Kumar K.V         if (err == -1) {
6072c30dd74SAneesh Kumar K.V             serrno = errno;
6082c30dd74SAneesh Kumar K.V             goto err_end;
6092c30dd74SAneesh Kumar K.V         }
610b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
611b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
6124fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6134fa4ce71SChen Gang         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
6141c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6152289be19SAneesh Kumar K.V             goto out;
6161c293312SVenkateswararao Jujjuri (JV)         }
6171c293312SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
6181c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6191c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
6201c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6211c293312SVenkateswararao Jujjuri (JV)         }
6221c293312SVenkateswararao Jujjuri (JV)     }
6232289be19SAneesh Kumar K.V     goto out;
6241c293312SVenkateswararao Jujjuri (JV) 
6251c293312SVenkateswararao Jujjuri (JV) err_end:
6264fa4ce71SChen Gang     remove(buffer);
6271c293312SVenkateswararao Jujjuri (JV)     errno = serrno;
6282289be19SAneesh Kumar K.V out:
6294ed7b2c3SStefan Weil     g_free(buffer);
6302289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
6311c293312SVenkateswararao Jujjuri (JV)     return err;
632c494dd6fSAnthony Liguori }
633c494dd6fSAnthony Liguori 
6342289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
6352289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
636c494dd6fSAnthony Liguori {
6372289be19SAneesh Kumar K.V     char *path;
63800ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
63900ec5c37SVenkateswararao Jujjuri (JV)     int serrno = 0;
6402289be19SAneesh Kumar K.V     V9fsString fullname;
6414ed7b2c3SStefan Weil     char *buffer = NULL;
64200ec5c37SVenkateswararao Jujjuri (JV) 
6432289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
6442289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
6452289be19SAneesh Kumar K.V     path = fullname.data;
6462289be19SAneesh Kumar K.V 
64700ec5c37SVenkateswararao Jujjuri (JV)     /* Determine the security model */
648b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6494fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6504fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
65100ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
6522289be19SAneesh Kumar K.V             goto out;
65300ec5c37SVenkateswararao Jujjuri (JV)         }
65400ec5c37SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFDIR;
6554fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
65600ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
65700ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
65800ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
65900ec5c37SVenkateswararao Jujjuri (JV)         }
6602c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6614fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6624fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
6632c30dd74SAneesh Kumar K.V         if (err == -1) {
6642c30dd74SAneesh Kumar K.V             goto out;
6652c30dd74SAneesh Kumar K.V         }
6662c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFDIR;
6672c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
6682c30dd74SAneesh Kumar K.V         if (err == -1) {
6692c30dd74SAneesh Kumar K.V             serrno = errno;
6702c30dd74SAneesh Kumar K.V             goto err_end;
6712c30dd74SAneesh Kumar K.V         }
672b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
673b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
6744fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6754fa4ce71SChen Gang         err = mkdir(buffer, credp->fc_mode);
67600ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
6772289be19SAneesh Kumar K.V             goto out;
67800ec5c37SVenkateswararao Jujjuri (JV)         }
67900ec5c37SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
68000ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
68100ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
68200ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
68300ec5c37SVenkateswararao Jujjuri (JV)         }
68400ec5c37SVenkateswararao Jujjuri (JV)     }
6852289be19SAneesh Kumar K.V     goto out;
68600ec5c37SVenkateswararao Jujjuri (JV) 
68700ec5c37SVenkateswararao Jujjuri (JV) err_end:
6884fa4ce71SChen Gang     remove(buffer);
68900ec5c37SVenkateswararao Jujjuri (JV)     errno = serrno;
6902289be19SAneesh Kumar K.V out:
6914ed7b2c3SStefan Weil     g_free(buffer);
6922289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
69300ec5c37SVenkateswararao Jujjuri (JV)     return err;
694c494dd6fSAnthony Liguori }
695c494dd6fSAnthony Liguori 
6968b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
697cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
698c494dd6fSAnthony Liguori {
6998b888272SAneesh Kumar K.V     int err, fd;
7008b888272SAneesh Kumar K.V 
7018b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
702f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
7038b888272SAneesh Kumar K.V     } else {
7048b888272SAneesh Kumar K.V         fd = fs->fd;
7058b888272SAneesh Kumar K.V     }
7068b888272SAneesh Kumar K.V 
7078b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
7081237ad76SVenkateswararao Jujjuri (JV)     if (err) {
7091237ad76SVenkateswararao Jujjuri (JV)         return err;
7101237ad76SVenkateswararao Jujjuri (JV)     }
711b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7121237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
7131237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
7141237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
7151237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
7161237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
7171237ad76SVenkateswararao Jujjuri (JV) 
718f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
719f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
7201237ad76SVenkateswararao Jujjuri (JV)         }
721f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
722f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
7231237ad76SVenkateswararao Jujjuri (JV)         }
724f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
725f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
7261237ad76SVenkateswararao Jujjuri (JV)         }
727f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
728f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
7291237ad76SVenkateswararao Jujjuri (JV)         }
7302c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7312c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
7322c30dd74SAneesh Kumar K.V         return -1;
7331237ad76SVenkateswararao Jujjuri (JV)     }
7341237ad76SVenkateswararao Jujjuri (JV)     return err;
735c494dd6fSAnthony Liguori }
736c494dd6fSAnthony Liguori 
7372289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
738cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
739c494dd6fSAnthony Liguori {
7402289be19SAneesh Kumar K.V     char *path;
7414750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
7424750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
7434750a96fSVenkateswararao Jujjuri (JV)     int serrno = 0;
7442289be19SAneesh Kumar K.V     V9fsString fullname;
7454ed7b2c3SStefan Weil     char *buffer = NULL;
7464750a96fSVenkateswararao Jujjuri (JV) 
7470ceb092eSAneesh Kumar K.V     /*
7480ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
7490ceb092eSAneesh Kumar K.V      */
7500ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
7510ceb092eSAneesh Kumar K.V 
7522289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
7532289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
7542289be19SAneesh Kumar K.V     path = fullname.data;
7552289be19SAneesh Kumar K.V 
7564750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
757b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7584fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7594fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7604750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7612289be19SAneesh Kumar K.V             err = fd;
7622289be19SAneesh Kumar K.V             goto out;
7634750a96fSVenkateswararao Jujjuri (JV)         }
7644750a96fSVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFREG;
7654750a96fSVenkateswararao Jujjuri (JV)         /* Set cleint credentials in xattr */
7664fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
7674750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7684750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
7694750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7704750a96fSVenkateswararao Jujjuri (JV)         }
7712c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7724fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7734fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7742c30dd74SAneesh Kumar K.V         if (fd == -1) {
7752c30dd74SAneesh Kumar K.V             err = fd;
7762c30dd74SAneesh Kumar K.V             goto out;
7772c30dd74SAneesh Kumar K.V         }
7782c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFREG;
7792c30dd74SAneesh Kumar K.V         /* Set client credentials in .virtfs_metadata directory files */
7802c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
7812c30dd74SAneesh Kumar K.V         if (err == -1) {
7822c30dd74SAneesh Kumar K.V             serrno = errno;
7832c30dd74SAneesh Kumar K.V             goto err_end;
7842c30dd74SAneesh Kumar K.V         }
785b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
786b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
7874fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7884fa4ce71SChen Gang         fd = open(buffer, flags, credp->fc_mode);
7894750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7902289be19SAneesh Kumar K.V             err = fd;
7912289be19SAneesh Kumar K.V             goto out;
7924750a96fSVenkateswararao Jujjuri (JV)         }
7934750a96fSVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
7944750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7954750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
7964750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7974750a96fSVenkateswararao Jujjuri (JV)         }
7984750a96fSVenkateswararao Jujjuri (JV)     }
7992289be19SAneesh Kumar K.V     err = fd;
800cc720ddbSAneesh Kumar K.V     fs->fd = fd;
8012289be19SAneesh Kumar K.V     goto out;
8024750a96fSVenkateswararao Jujjuri (JV) 
8034750a96fSVenkateswararao Jujjuri (JV) err_end:
8044750a96fSVenkateswararao Jujjuri (JV)     close(fd);
8054fa4ce71SChen Gang     remove(buffer);
8064750a96fSVenkateswararao Jujjuri (JV)     errno = serrno;
8072289be19SAneesh Kumar K.V out:
8084ed7b2c3SStefan Weil     g_free(buffer);
8092289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
8104750a96fSVenkateswararao Jujjuri (JV)     return err;
811c494dd6fSAnthony Liguori }
812c494dd6fSAnthony Liguori 
813758e8e38SVenkateswararao Jujjuri (JV) 
814879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
8152289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
816c494dd6fSAnthony Liguori {
817879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
818879c2813SVenkateswararao Jujjuri (JV)     int serrno = 0;
8192289be19SAneesh Kumar K.V     char *newpath;
8202289be19SAneesh Kumar K.V     V9fsString fullname;
8214ed7b2c3SStefan Weil     char *buffer = NULL;
822879c2813SVenkateswararao Jujjuri (JV) 
8232289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
8242289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
8252289be19SAneesh Kumar K.V     newpath = fullname.data;
8262289be19SAneesh Kumar K.V 
827879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
828b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
829879c2813SVenkateswararao Jujjuri (JV)         int fd;
830879c2813SVenkateswararao Jujjuri (JV)         ssize_t oldpath_size, write_size;
8314fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8324fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
833879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
8342289be19SAneesh Kumar K.V             err = fd;
8352289be19SAneesh Kumar K.V             goto out;
836879c2813SVenkateswararao Jujjuri (JV)         }
837879c2813SVenkateswararao Jujjuri (JV)         /* Write the oldpath (target) to the file. */
838f35bde2fSHarsh Prateek Bora         oldpath_size = strlen(oldpath);
839879c2813SVenkateswararao Jujjuri (JV)         do {
840879c2813SVenkateswararao Jujjuri (JV)             write_size = write(fd, (void *)oldpath, oldpath_size);
841879c2813SVenkateswararao Jujjuri (JV)         } while (write_size == -1 && errno == EINTR);
842879c2813SVenkateswararao Jujjuri (JV) 
843879c2813SVenkateswararao Jujjuri (JV)         if (write_size != oldpath_size) {
844879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
845879c2813SVenkateswararao Jujjuri (JV)             close(fd);
846879c2813SVenkateswararao Jujjuri (JV)             err = -1;
847879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
848879c2813SVenkateswararao Jujjuri (JV)         }
849879c2813SVenkateswararao Jujjuri (JV)         close(fd);
850879c2813SVenkateswararao Jujjuri (JV)         /* Set cleint credentials in symlink's xattr */
851879c2813SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFLNK;
8524fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
853879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
854879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
855879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
856879c2813SVenkateswararao Jujjuri (JV)         }
8572c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
8582c30dd74SAneesh Kumar K.V         int fd;
8592c30dd74SAneesh Kumar K.V         ssize_t oldpath_size, write_size;
8604fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8614fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
8622c30dd74SAneesh Kumar K.V         if (fd == -1) {
8632c30dd74SAneesh Kumar K.V             err = fd;
8642c30dd74SAneesh Kumar K.V             goto out;
8652c30dd74SAneesh Kumar K.V         }
8662c30dd74SAneesh Kumar K.V         /* Write the oldpath (target) to the file. */
8672c30dd74SAneesh Kumar K.V         oldpath_size = strlen(oldpath);
8682c30dd74SAneesh Kumar K.V         do {
8692c30dd74SAneesh Kumar K.V             write_size = write(fd, (void *)oldpath, oldpath_size);
8702c30dd74SAneesh Kumar K.V         } while (write_size == -1 && errno == EINTR);
8712c30dd74SAneesh Kumar K.V 
8722c30dd74SAneesh Kumar K.V         if (write_size != oldpath_size) {
8732c30dd74SAneesh Kumar K.V             serrno = errno;
8742c30dd74SAneesh Kumar K.V             close(fd);
8752c30dd74SAneesh Kumar K.V             err = -1;
8762c30dd74SAneesh Kumar K.V             goto err_end;
8772c30dd74SAneesh Kumar K.V         }
8782c30dd74SAneesh Kumar K.V         close(fd);
8792c30dd74SAneesh Kumar K.V         /* Set cleint credentials in symlink's xattr */
8802c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFLNK;
8812c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
8822c30dd74SAneesh Kumar K.V         if (err == -1) {
8832c30dd74SAneesh Kumar K.V             serrno = errno;
8842c30dd74SAneesh Kumar K.V             goto err_end;
8852c30dd74SAneesh Kumar K.V         }
886b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
887b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
8884fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8894fa4ce71SChen Gang         err = symlink(oldpath, buffer);
890879c2813SVenkateswararao Jujjuri (JV)         if (err) {
8912289be19SAneesh Kumar K.V             goto out;
892879c2813SVenkateswararao Jujjuri (JV)         }
8934fa4ce71SChen Gang         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
894879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
89512848bfcSAneesh Kumar K.V             /*
89612848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
89712848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
89812848bfcSAneesh Kumar K.V              */
899b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
900879c2813SVenkateswararao Jujjuri (JV)                 serrno = errno;
901879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
90212848bfcSAneesh Kumar K.V             } else
90312848bfcSAneesh Kumar K.V                 err = 0;
904879c2813SVenkateswararao Jujjuri (JV)         }
905879c2813SVenkateswararao Jujjuri (JV)     }
9062289be19SAneesh Kumar K.V     goto out;
907879c2813SVenkateswararao Jujjuri (JV) 
908879c2813SVenkateswararao Jujjuri (JV) err_end:
9094fa4ce71SChen Gang     remove(buffer);
910879c2813SVenkateswararao Jujjuri (JV)     errno = serrno;
9112289be19SAneesh Kumar K.V out:
9124ed7b2c3SStefan Weil     g_free(buffer);
9132289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
914879c2813SVenkateswararao Jujjuri (JV)     return err;
915c494dd6fSAnthony Liguori }
916c494dd6fSAnthony Liguori 
9172289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
9182289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
919c494dd6fSAnthony Liguori {
9202289be19SAneesh Kumar K.V     int ret;
9212289be19SAneesh Kumar K.V     V9fsString newpath;
9224fa4ce71SChen Gang     char *buffer, *buffer1;
923c494dd6fSAnthony Liguori 
9242289be19SAneesh Kumar K.V     v9fs_string_init(&newpath);
9252289be19SAneesh Kumar K.V     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
9262289be19SAneesh Kumar K.V 
9274fa4ce71SChen Gang     buffer = rpath(ctx, oldpath->data);
9284fa4ce71SChen Gang     buffer1 = rpath(ctx, newpath.data);
9294fa4ce71SChen Gang     ret = link(buffer, buffer1);
9304fa4ce71SChen Gang     g_free(buffer);
9314fa4ce71SChen Gang     g_free(buffer1);
9322c30dd74SAneesh Kumar K.V 
9332c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
9342c30dd74SAneesh Kumar K.V     if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
9352c30dd74SAneesh Kumar K.V         /* Link the .virtfs_metadata files. Create the metada directory */
9362c30dd74SAneesh Kumar K.V         ret = local_create_mapped_attr_dir(ctx, newpath.data);
9372c30dd74SAneesh Kumar K.V         if (ret < 0) {
9382c30dd74SAneesh Kumar K.V             goto err_out;
9392c30dd74SAneesh Kumar K.V         }
9404fa4ce71SChen Gang         buffer = local_mapped_attr_path(ctx, oldpath->data);
9414fa4ce71SChen Gang         buffer1 = local_mapped_attr_path(ctx, newpath.data);
9424fa4ce71SChen Gang         ret = link(buffer, buffer1);
9434fa4ce71SChen Gang         g_free(buffer);
9444fa4ce71SChen Gang         g_free(buffer1);
9452c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
9462c30dd74SAneesh Kumar K.V             goto err_out;
9472c30dd74SAneesh Kumar K.V         }
9482c30dd74SAneesh Kumar K.V     }
9492c30dd74SAneesh Kumar K.V err_out:
9502289be19SAneesh Kumar K.V     v9fs_string_free(&newpath);
9512289be19SAneesh Kumar K.V     return ret;
952c494dd6fSAnthony Liguori }
953c494dd6fSAnthony Liguori 
9542289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
9558cf89e00SAnthony Liguori {
956ac125d99SGreg Kurz     int fd, ret;
9572289be19SAneesh Kumar K.V 
958ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
959ac125d99SGreg Kurz     if (fd == -1) {
960ac125d99SGreg Kurz         return -1;
961ac125d99SGreg Kurz     }
962ac125d99SGreg Kurz     ret = ftruncate(fd, size);
963ac125d99SGreg Kurz     close_preserve_errno(fd);
9644fa4ce71SChen Gang     return ret;
9658cf89e00SAnthony Liguori }
9668cf89e00SAnthony Liguori 
9672289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
9688cf89e00SAnthony Liguori {
9694fa4ce71SChen Gang     char *buffer;
9704fa4ce71SChen Gang     int ret = -1;
9712289be19SAneesh Kumar K.V     char *path = fs_path->data;
9722289be19SAneesh Kumar K.V 
973c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
97417b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
97517b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
9764fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9774fa4ce71SChen Gang         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
9784fa4ce71SChen Gang         g_free(buffer);
979b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
9804fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9814fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
9824fa4ce71SChen Gang         g_free(buffer);
9832c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9842c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
985f7613beeSVenkateswararao Jujjuri (JV)     }
9864fa4ce71SChen Gang     return ret;
9878cf89e00SAnthony Liguori }
9888cf89e00SAnthony Liguori 
9892289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
99074bc02b2SM. Mohan Kumar                            const struct timespec *buf)
9918cf89e00SAnthony Liguori {
992a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
993a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
994a33eda0dSGreg Kurz     int dirfd, ret = -1;
9952289be19SAneesh Kumar K.V 
996a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
997a33eda0dSGreg Kurz     if (dirfd == -1) {
998a33eda0dSGreg Kurz         goto out;
999a33eda0dSGreg Kurz     }
1000a33eda0dSGreg Kurz 
1001a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1002a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1003a33eda0dSGreg Kurz out:
1004a33eda0dSGreg Kurz     g_free(dirpath);
1005a33eda0dSGreg Kurz     g_free(name);
10064fa4ce71SChen Gang     return ret;
10078cf89e00SAnthony Liguori }
10088cf89e00SAnthony Liguori 
1009df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1010df4938a6SGreg Kurz                                  int flags)
1011df4938a6SGreg Kurz {
1012df4938a6SGreg Kurz     int ret = -1;
1013df4938a6SGreg Kurz 
1014df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1015df4938a6SGreg Kurz         int map_dirfd;
1016df4938a6SGreg Kurz 
1017df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1018df4938a6SGreg Kurz             int fd;
1019df4938a6SGreg Kurz 
1020df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1021df4938a6SGreg Kurz             if (fd == -1) {
1022df4938a6SGreg Kurz                 goto err_out;
1023df4938a6SGreg Kurz             }
1024df4938a6SGreg Kurz             /*
1025df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1026df4938a6SGreg Kurz              * directory
1027df4938a6SGreg Kurz              */
1028df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1029df4938a6SGreg Kurz             close_preserve_errno(fd);
1030df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1031df4938a6SGreg Kurz                 /*
1032df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1033df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1034df4938a6SGreg Kurz                  */
1035df4938a6SGreg Kurz                 goto err_out;
1036df4938a6SGreg Kurz             }
1037df4938a6SGreg Kurz         }
1038df4938a6SGreg Kurz         /*
1039df4938a6SGreg Kurz          * Now remove the name from parent directory
1040df4938a6SGreg Kurz          * .virtfs_metadata directory.
1041df4938a6SGreg Kurz          */
1042df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1043df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1044df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1045df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1046df4938a6SGreg Kurz             /*
1047df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1048df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1049df4938a6SGreg Kurz              */
1050df4938a6SGreg Kurz             goto err_out;
1051df4938a6SGreg Kurz         }
1052df4938a6SGreg Kurz     }
1053df4938a6SGreg Kurz 
1054df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1055df4938a6SGreg Kurz err_out:
1056df4938a6SGreg Kurz     return ret;
1057df4938a6SGreg Kurz }
1058df4938a6SGreg Kurz 
10595bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10605bae1900SAnthony Liguori {
10612c30dd74SAneesh Kumar K.V     struct stat stbuf;
1062a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1063a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1064a0e640a8SGreg Kurz     int flags = 0;
1065a0e640a8SGreg Kurz     int dirfd;
1066a0e640a8SGreg Kurz     int err = -1;
10672c30dd74SAneesh Kumar K.V 
1068a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1069a0e640a8SGreg Kurz     if (dirfd) {
1070a0e640a8SGreg Kurz         goto out;
1071a0e640a8SGreg Kurz     }
1072a0e640a8SGreg Kurz 
1073a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
10742c30dd74SAneesh Kumar K.V         goto err_out;
10752c30dd74SAneesh Kumar K.V     }
1076a0e640a8SGreg Kurz 
10772c30dd74SAneesh Kumar K.V     if (S_ISDIR(stbuf.st_mode)) {
1078a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
10792c30dd74SAneesh Kumar K.V     }
10804fa4ce71SChen Gang 
1081a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
10822c30dd74SAneesh Kumar K.V err_out:
1083a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1084a0e640a8SGreg Kurz out:
1085a0e640a8SGreg Kurz     g_free(name);
1086a0e640a8SGreg Kurz     g_free(dirpath);
10872c30dd74SAneesh Kumar K.V     return err;
10885bae1900SAnthony Liguori }
10895bae1900SAnthony Liguori 
10908b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
10918b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
10928cf89e00SAnthony Liguori {
10938b888272SAneesh Kumar K.V     int fd;
10948b888272SAneesh Kumar K.V 
10958b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1096f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
109749594973SVenkateswararao Jujjuri (JV)     } else {
10988b888272SAneesh Kumar K.V         fd = fs->fd;
10998b888272SAneesh Kumar K.V     }
11008b888272SAneesh Kumar K.V 
11018b888272SAneesh Kumar K.V     if (datasync) {
11028b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
11038b888272SAneesh Kumar K.V     } else {
11048b888272SAneesh Kumar K.V         return fsync(fd);
11058cf89e00SAnthony Liguori     }
110649594973SVenkateswararao Jujjuri (JV) }
11078cf89e00SAnthony Liguori 
11082289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1109be940c87SM. Mohan Kumar {
111031e51d1cSGreg Kurz     int fd, ret;
11112289be19SAneesh Kumar K.V 
111231e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
111331e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
111431e51d1cSGreg Kurz     close_preserve_errno(fd);
11154fa4ce71SChen Gang     return ret;
1116be940c87SM. Mohan Kumar }
1117be940c87SM. Mohan Kumar 
11182289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1119fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1120fa32ef88SAneesh Kumar K.V {
11212289be19SAneesh Kumar K.V     char *path = fs_path->data;
11222289be19SAneesh Kumar K.V 
1123fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1124fa32ef88SAneesh Kumar K.V }
1125fa32ef88SAneesh Kumar K.V 
11262289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1127fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1128fa32ef88SAneesh Kumar K.V {
11292289be19SAneesh Kumar K.V     char *path = fs_path->data;
11302289be19SAneesh Kumar K.V 
1131fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
113261b6c499SAneesh Kumar K.V }
113361b6c499SAneesh Kumar K.V 
11342289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
113510b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
113610b468bdSAneesh Kumar K.V {
11372289be19SAneesh Kumar K.V     char *path = fs_path->data;
11382289be19SAneesh Kumar K.V 
1139fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
114010b468bdSAneesh Kumar K.V }
114110b468bdSAneesh Kumar K.V 
11422289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11432289be19SAneesh Kumar K.V                               const char *name)
11449ed3ef26SAneesh Kumar K.V {
11452289be19SAneesh Kumar K.V     char *path = fs_path->data;
11462289be19SAneesh Kumar K.V 
1147fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11489ed3ef26SAneesh Kumar K.V }
11499ed3ef26SAneesh Kumar K.V 
11502289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11512289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11522289be19SAneesh Kumar K.V {
11532289be19SAneesh Kumar K.V     if (dir_path) {
1154e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
11552289be19SAneesh Kumar K.V     } else {
1156e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
11572289be19SAneesh Kumar K.V     }
11582289be19SAneesh Kumar K.V     return 0;
11592289be19SAneesh Kumar K.V }
11602289be19SAneesh Kumar K.V 
11612289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
11622289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
11632289be19SAneesh Kumar K.V                           const char *new_name)
11642289be19SAneesh Kumar K.V {
11652289be19SAneesh Kumar K.V     int ret;
116699f2cf4bSGreg Kurz     int odirfd, ndirfd;
11672289be19SAneesh Kumar K.V 
116899f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
116999f2cf4bSGreg Kurz     if (odirfd == -1) {
117099f2cf4bSGreg Kurz         return -1;
117199f2cf4bSGreg Kurz     }
11722289be19SAneesh Kumar K.V 
117399f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
117499f2cf4bSGreg Kurz     if (ndirfd == -1) {
117599f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
117699f2cf4bSGreg Kurz         return -1;
117799f2cf4bSGreg Kurz     }
11782289be19SAneesh Kumar K.V 
117999f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
118099f2cf4bSGreg Kurz     if (ret < 0) {
118199f2cf4bSGreg Kurz         goto out;
118299f2cf4bSGreg Kurz     }
118399f2cf4bSGreg Kurz 
118499f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
118599f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
118699f2cf4bSGreg Kurz 
118799f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
118899f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
118999f2cf4bSGreg Kurz             goto err_undo_rename;
119099f2cf4bSGreg Kurz         }
119199f2cf4bSGreg Kurz 
119299f2cf4bSGreg Kurz         omap_dirfd = openat(odirfd, VIRTFS_META_DIR,
119399f2cf4bSGreg Kurz                             O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
119499f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
119599f2cf4bSGreg Kurz             goto err;
119699f2cf4bSGreg Kurz         }
119799f2cf4bSGreg Kurz 
119899f2cf4bSGreg Kurz         nmap_dirfd = openat(ndirfd, VIRTFS_META_DIR,
119999f2cf4bSGreg Kurz                             O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
120099f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
120199f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
120299f2cf4bSGreg Kurz             goto err;
120399f2cf4bSGreg Kurz         }
120499f2cf4bSGreg Kurz 
120599f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
120699f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
120799f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
120899f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
120999f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
121099f2cf4bSGreg Kurz             goto err_undo_rename;
121199f2cf4bSGreg Kurz         }
121299f2cf4bSGreg Kurz 
121399f2cf4bSGreg Kurz         ret = 0;
121499f2cf4bSGreg Kurz     }
121599f2cf4bSGreg Kurz     goto out;
121699f2cf4bSGreg Kurz 
121799f2cf4bSGreg Kurz err:
121899f2cf4bSGreg Kurz     ret = -1;
121999f2cf4bSGreg Kurz err_undo_rename:
122099f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
122199f2cf4bSGreg Kurz out:
122299f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
122399f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
12242289be19SAneesh Kumar K.V     return ret;
12252289be19SAneesh Kumar K.V }
12262289be19SAneesh Kumar K.V 
1227*d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1228*d2767edeSGreg Kurz {
1229*d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1230*d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1231*d2767edeSGreg Kurz }
1232*d2767edeSGreg Kurz 
1233*d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1234*d2767edeSGreg Kurz                         const char *newpath)
1235*d2767edeSGreg Kurz {
1236*d2767edeSGreg Kurz     int err;
1237*d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1238*d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1239*d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1240*d2767edeSGreg Kurz 
1241*d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1242*d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1243*d2767edeSGreg Kurz 
1244*d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1245*d2767edeSGreg Kurz 
1246*d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1247*d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1248*d2767edeSGreg Kurz     g_free(nname);
1249*d2767edeSGreg Kurz     g_free(oname);
1250*d2767edeSGreg Kurz 
1251*d2767edeSGreg Kurz     return err;
1252*d2767edeSGreg Kurz }
1253*d2767edeSGreg Kurz 
12542289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
12552289be19SAneesh Kumar K.V                           const char *name, int flags)
12562289be19SAneesh Kumar K.V {
12572289be19SAneesh Kumar K.V     int ret;
1258df4938a6SGreg Kurz     int dirfd;
12592c30dd74SAneesh Kumar K.V 
1260df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1261df4938a6SGreg Kurz     if (dirfd == -1) {
1262df4938a6SGreg Kurz         return -1;
1263df4938a6SGreg Kurz     }
12642289be19SAneesh Kumar K.V 
1265df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1266df4938a6SGreg Kurz     close_preserve_errno(dirfd);
12672289be19SAneesh Kumar K.V     return ret;
12682289be19SAneesh Kumar K.V }
12699ed3ef26SAneesh Kumar K.V 
1270e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1271e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1272e06a765eSHarsh Prateek Bora {
1273ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
12740e5fc994SKirill A. Shutemov     int err;
1275cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1276cc720ddbSAneesh Kumar K.V 
1277e06a765eSHarsh Prateek Bora     /*
1278e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1279e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1280e06a765eSHarsh Prateek Bora      */
1281e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
12821a9978a5SKirill A. Shutemov         errno = ENOTTY;
12831a9978a5SKirill A. Shutemov         return -1;
1284e06a765eSHarsh Prateek Bora     }
1285cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1286cc720ddbSAneesh Kumar K.V     if (err < 0) {
1287cc720ddbSAneesh Kumar K.V         return err;
1288e06a765eSHarsh Prateek Bora     }
1289cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1290cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1291e06a765eSHarsh Prateek Bora     return err;
12920e5fc994SKirill A. Shutemov #else
12930e5fc994SKirill A. Shutemov     errno = ENOTTY;
12940e5fc994SKirill A. Shutemov     return -1;
12950e5fc994SKirill A. Shutemov #endif
1296e06a765eSHarsh Prateek Bora }
1297e06a765eSHarsh Prateek Bora 
12980174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
12990174fe73SAneesh Kumar K.V {
1300e06a765eSHarsh Prateek Bora     struct statfs stbuf;
13010e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13020e35a378SGreg Kurz 
13030e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13040e35a378SGreg Kurz     if (data->mountfd == -1) {
13050e35a378SGreg Kurz         goto err;
13060e35a378SGreg Kurz     }
1307e06a765eSHarsh Prateek Bora 
130800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
130900c90bd1SGreg Kurz     /*
131000c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
131100c90bd1SGreg Kurz      */
13120e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13130e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13140e35a378SGreg Kurz         goto err;
131500c90bd1SGreg Kurz     }
131600c90bd1SGreg Kurz     switch (stbuf.f_type) {
131700c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
131800c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
131900c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
132000c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
132100c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
132200c90bd1SGreg Kurz         break;
132300c90bd1SGreg Kurz     }
132400c90bd1SGreg Kurz #endif
132500c90bd1SGreg Kurz 
13262c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
13272c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13282c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
13292c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
13302c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
13312c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
13322c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
13332c30dd74SAneesh Kumar K.V         /*
13342c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
13352c30dd74SAneesh Kumar K.V          * remain same.
13362c30dd74SAneesh Kumar K.V          */
13372c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13382c30dd74SAneesh Kumar K.V     }
1339c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
134000c90bd1SGreg Kurz 
13410e35a378SGreg Kurz     ctx->private = data;
134200c90bd1SGreg Kurz     return 0;
13430e35a378SGreg Kurz 
13440e35a378SGreg Kurz err:
13450e35a378SGreg Kurz     g_free(data);
13460e35a378SGreg Kurz     return -1;
13470e35a378SGreg Kurz }
13480e35a378SGreg Kurz 
13490e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13500e35a378SGreg Kurz {
13510e35a378SGreg Kurz     LocalData *data = ctx->private;
13520e35a378SGreg Kurz 
13530e35a378SGreg Kurz     close(data->mountfd);
13540e35a378SGreg Kurz     g_free(data);
13550174fe73SAneesh Kumar K.V }
13560174fe73SAneesh Kumar K.V 
135799519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
135899519f0aSAneesh Kumar K.V {
135999519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
136099519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
136199519f0aSAneesh Kumar K.V 
136299519f0aSAneesh Kumar K.V     if (!sec_model) {
136363325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
136463325b18SGreg Kurz         error_printf("valid options are:"
136563325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
136699519f0aSAneesh Kumar K.V         return -1;
136799519f0aSAneesh Kumar K.V     }
136899519f0aSAneesh Kumar K.V 
136999519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
137099519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
13712c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
13722c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
137399519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
137499519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
137599519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
13762c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
13772c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
137899519f0aSAneesh Kumar K.V     } else {
137963325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
138063325b18SGreg Kurz         error_printf("valid options are:"
138163325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
138299519f0aSAneesh Kumar K.V         return -1;
138399519f0aSAneesh Kumar K.V     }
138499519f0aSAneesh Kumar K.V 
138599519f0aSAneesh Kumar K.V     if (!path) {
138663325b18SGreg Kurz         error_report("fsdev: No path specified");
138799519f0aSAneesh Kumar K.V         return -1;
138899519f0aSAneesh Kumar K.V     }
138999519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
139099519f0aSAneesh Kumar K.V 
139199519f0aSAneesh Kumar K.V     return 0;
139299519f0aSAneesh Kumar K.V }
139399519f0aSAneesh Kumar K.V 
13949f107513SAnthony Liguori FileOperations local_ops = {
139599519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
13960174fe73SAneesh Kumar K.V     .init  = local_init,
13970e35a378SGreg Kurz     .cleanup = local_cleanup,
1398131dcb25SAnthony Liguori     .lstat = local_lstat,
1399131dcb25SAnthony Liguori     .readlink = local_readlink,
1400131dcb25SAnthony Liguori     .close = local_close,
1401131dcb25SAnthony Liguori     .closedir = local_closedir,
1402a6568fe2SAnthony Liguori     .open = local_open,
1403a6568fe2SAnthony Liguori     .opendir = local_opendir,
1404a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1405a9231555SAnthony Liguori     .telldir = local_telldir,
1406635324e8SGreg Kurz     .readdir = local_readdir,
1407a9231555SAnthony Liguori     .seekdir = local_seekdir,
140856d15a53SSanchit Garg     .preadv = local_preadv,
140956d15a53SSanchit Garg     .pwritev = local_pwritev,
1410c494dd6fSAnthony Liguori     .chmod = local_chmod,
1411c494dd6fSAnthony Liguori     .mknod = local_mknod,
1412c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1413c494dd6fSAnthony Liguori     .fstat = local_fstat,
1414c494dd6fSAnthony Liguori     .open2 = local_open2,
1415c494dd6fSAnthony Liguori     .symlink = local_symlink,
1416c494dd6fSAnthony Liguori     .link = local_link,
14178cf89e00SAnthony Liguori     .truncate = local_truncate,
14188cf89e00SAnthony Liguori     .rename = local_rename,
14198cf89e00SAnthony Liguori     .chown = local_chown,
142074bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
14215bae1900SAnthony Liguori     .remove = local_remove,
14228cf89e00SAnthony Liguori     .fsync = local_fsync,
1423be940c87SM. Mohan Kumar     .statfs = local_statfs,
1424fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1425fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
142610b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
14279ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
14282289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
14292289be19SAneesh Kumar K.V     .renameat  = local_renameat,
14302289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
14319f107513SAnthony Liguori };
1432