xref: /qemu/hw/9pfs/9p-local.c (revision 6dd4b1f1d026e478d9177b28169b377e212400f3)
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;
923*6dd4b1f1SGreg Kurz     int serrno;
924c494dd6fSAnthony Liguori 
9252289be19SAneesh Kumar K.V     v9fs_string_init(&newpath);
9262289be19SAneesh Kumar K.V     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
9272289be19SAneesh Kumar K.V 
9284fa4ce71SChen Gang     buffer = rpath(ctx, oldpath->data);
9294fa4ce71SChen Gang     buffer1 = rpath(ctx, newpath.data);
9304fa4ce71SChen Gang     ret = link(buffer, buffer1);
9314fa4ce71SChen Gang     g_free(buffer);
932*6dd4b1f1SGreg Kurz     if (ret < 0) {
933*6dd4b1f1SGreg Kurz         goto out;
934*6dd4b1f1SGreg Kurz     }
9352c30dd74SAneesh Kumar K.V 
9362c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
937*6dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938*6dd4b1f1SGreg Kurz         char *vbuffer, *vbuffer1;
939*6dd4b1f1SGreg Kurz 
9402c30dd74SAneesh Kumar K.V         /* Link the .virtfs_metadata files. Create the metada directory */
9412c30dd74SAneesh Kumar K.V         ret = local_create_mapped_attr_dir(ctx, newpath.data);
9422c30dd74SAneesh Kumar K.V         if (ret < 0) {
9432c30dd74SAneesh Kumar K.V             goto err_out;
9442c30dd74SAneesh Kumar K.V         }
945*6dd4b1f1SGreg Kurz         vbuffer = local_mapped_attr_path(ctx, oldpath->data);
946*6dd4b1f1SGreg Kurz         vbuffer1 = local_mapped_attr_path(ctx, newpath.data);
947*6dd4b1f1SGreg Kurz         ret = link(vbuffer, vbuffer1);
948*6dd4b1f1SGreg Kurz         g_free(vbuffer);
949*6dd4b1f1SGreg Kurz         g_free(vbuffer1);
9502c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
9512c30dd74SAneesh Kumar K.V             goto err_out;
9522c30dd74SAneesh Kumar K.V         }
9532c30dd74SAneesh Kumar K.V     }
954*6dd4b1f1SGreg Kurz     goto out;
955*6dd4b1f1SGreg Kurz 
9562c30dd74SAneesh Kumar K.V err_out:
957*6dd4b1f1SGreg Kurz     serrno = errno;
958*6dd4b1f1SGreg Kurz     remove(buffer1);
959*6dd4b1f1SGreg Kurz     errno = serrno;
960*6dd4b1f1SGreg Kurz out:
961*6dd4b1f1SGreg Kurz     g_free(buffer1);
9622289be19SAneesh Kumar K.V     v9fs_string_free(&newpath);
9632289be19SAneesh Kumar K.V     return ret;
964c494dd6fSAnthony Liguori }
965c494dd6fSAnthony Liguori 
9662289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
9678cf89e00SAnthony Liguori {
968ac125d99SGreg Kurz     int fd, ret;
9692289be19SAneesh Kumar K.V 
970ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
971ac125d99SGreg Kurz     if (fd == -1) {
972ac125d99SGreg Kurz         return -1;
973ac125d99SGreg Kurz     }
974ac125d99SGreg Kurz     ret = ftruncate(fd, size);
975ac125d99SGreg Kurz     close_preserve_errno(fd);
9764fa4ce71SChen Gang     return ret;
9778cf89e00SAnthony Liguori }
9788cf89e00SAnthony Liguori 
9792289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
9808cf89e00SAnthony Liguori {
9814fa4ce71SChen Gang     char *buffer;
9824fa4ce71SChen Gang     int ret = -1;
9832289be19SAneesh Kumar K.V     char *path = fs_path->data;
9842289be19SAneesh Kumar K.V 
985c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
98617b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
98717b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
9884fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9894fa4ce71SChen Gang         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
9904fa4ce71SChen Gang         g_free(buffer);
991b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
9924fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9934fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
9944fa4ce71SChen Gang         g_free(buffer);
9952c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9962c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
997f7613beeSVenkateswararao Jujjuri (JV)     }
9984fa4ce71SChen Gang     return ret;
9998cf89e00SAnthony Liguori }
10008cf89e00SAnthony Liguori 
10012289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
100274bc02b2SM. Mohan Kumar                            const struct timespec *buf)
10038cf89e00SAnthony Liguori {
1004a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1005a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1006a33eda0dSGreg Kurz     int dirfd, ret = -1;
10072289be19SAneesh Kumar K.V 
1008a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1009a33eda0dSGreg Kurz     if (dirfd == -1) {
1010a33eda0dSGreg Kurz         goto out;
1011a33eda0dSGreg Kurz     }
1012a33eda0dSGreg Kurz 
1013a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1014a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1015a33eda0dSGreg Kurz out:
1016a33eda0dSGreg Kurz     g_free(dirpath);
1017a33eda0dSGreg Kurz     g_free(name);
10184fa4ce71SChen Gang     return ret;
10198cf89e00SAnthony Liguori }
10208cf89e00SAnthony Liguori 
1021df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1022df4938a6SGreg Kurz                                  int flags)
1023df4938a6SGreg Kurz {
1024df4938a6SGreg Kurz     int ret = -1;
1025df4938a6SGreg Kurz 
1026df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1027df4938a6SGreg Kurz         int map_dirfd;
1028df4938a6SGreg Kurz 
1029df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1030df4938a6SGreg Kurz             int fd;
1031df4938a6SGreg Kurz 
1032df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1033df4938a6SGreg Kurz             if (fd == -1) {
1034df4938a6SGreg Kurz                 goto err_out;
1035df4938a6SGreg Kurz             }
1036df4938a6SGreg Kurz             /*
1037df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1038df4938a6SGreg Kurz              * directory
1039df4938a6SGreg Kurz              */
1040df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1041df4938a6SGreg Kurz             close_preserve_errno(fd);
1042df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1043df4938a6SGreg Kurz                 /*
1044df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1045df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1046df4938a6SGreg Kurz                  */
1047df4938a6SGreg Kurz                 goto err_out;
1048df4938a6SGreg Kurz             }
1049df4938a6SGreg Kurz         }
1050df4938a6SGreg Kurz         /*
1051df4938a6SGreg Kurz          * Now remove the name from parent directory
1052df4938a6SGreg Kurz          * .virtfs_metadata directory.
1053df4938a6SGreg Kurz          */
1054df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1055df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1056df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1057df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1058df4938a6SGreg Kurz             /*
1059df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1060df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1061df4938a6SGreg Kurz              */
1062df4938a6SGreg Kurz             goto err_out;
1063df4938a6SGreg Kurz         }
1064df4938a6SGreg Kurz     }
1065df4938a6SGreg Kurz 
1066df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1067df4938a6SGreg Kurz err_out:
1068df4938a6SGreg Kurz     return ret;
1069df4938a6SGreg Kurz }
1070df4938a6SGreg Kurz 
10715bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10725bae1900SAnthony Liguori {
10732c30dd74SAneesh Kumar K.V     struct stat stbuf;
1074a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1075a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1076a0e640a8SGreg Kurz     int flags = 0;
1077a0e640a8SGreg Kurz     int dirfd;
1078a0e640a8SGreg Kurz     int err = -1;
10792c30dd74SAneesh Kumar K.V 
1080a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1081a0e640a8SGreg Kurz     if (dirfd) {
1082a0e640a8SGreg Kurz         goto out;
1083a0e640a8SGreg Kurz     }
1084a0e640a8SGreg Kurz 
1085a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
10862c30dd74SAneesh Kumar K.V         goto err_out;
10872c30dd74SAneesh Kumar K.V     }
1088a0e640a8SGreg Kurz 
10892c30dd74SAneesh Kumar K.V     if (S_ISDIR(stbuf.st_mode)) {
1090a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
10912c30dd74SAneesh Kumar K.V     }
10924fa4ce71SChen Gang 
1093a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
10942c30dd74SAneesh Kumar K.V err_out:
1095a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1096a0e640a8SGreg Kurz out:
1097a0e640a8SGreg Kurz     g_free(name);
1098a0e640a8SGreg Kurz     g_free(dirpath);
10992c30dd74SAneesh Kumar K.V     return err;
11005bae1900SAnthony Liguori }
11015bae1900SAnthony Liguori 
11028b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
11038b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
11048cf89e00SAnthony Liguori {
11058b888272SAneesh Kumar K.V     int fd;
11068b888272SAneesh Kumar K.V 
11078b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1108f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
110949594973SVenkateswararao Jujjuri (JV)     } else {
11108b888272SAneesh Kumar K.V         fd = fs->fd;
11118b888272SAneesh Kumar K.V     }
11128b888272SAneesh Kumar K.V 
11138b888272SAneesh Kumar K.V     if (datasync) {
11148b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
11158b888272SAneesh Kumar K.V     } else {
11168b888272SAneesh Kumar K.V         return fsync(fd);
11178cf89e00SAnthony Liguori     }
111849594973SVenkateswararao Jujjuri (JV) }
11198cf89e00SAnthony Liguori 
11202289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1121be940c87SM. Mohan Kumar {
112231e51d1cSGreg Kurz     int fd, ret;
11232289be19SAneesh Kumar K.V 
112431e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
112531e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
112631e51d1cSGreg Kurz     close_preserve_errno(fd);
11274fa4ce71SChen Gang     return ret;
1128be940c87SM. Mohan Kumar }
1129be940c87SM. Mohan Kumar 
11302289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1131fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1132fa32ef88SAneesh Kumar K.V {
11332289be19SAneesh Kumar K.V     char *path = fs_path->data;
11342289be19SAneesh Kumar K.V 
1135fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1136fa32ef88SAneesh Kumar K.V }
1137fa32ef88SAneesh Kumar K.V 
11382289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1139fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1140fa32ef88SAneesh Kumar K.V {
11412289be19SAneesh Kumar K.V     char *path = fs_path->data;
11422289be19SAneesh Kumar K.V 
1143fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
114461b6c499SAneesh Kumar K.V }
114561b6c499SAneesh Kumar K.V 
11462289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
114710b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
114810b468bdSAneesh Kumar K.V {
11492289be19SAneesh Kumar K.V     char *path = fs_path->data;
11502289be19SAneesh Kumar K.V 
1151fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
115210b468bdSAneesh Kumar K.V }
115310b468bdSAneesh Kumar K.V 
11542289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11552289be19SAneesh Kumar K.V                               const char *name)
11569ed3ef26SAneesh Kumar K.V {
11572289be19SAneesh Kumar K.V     char *path = fs_path->data;
11582289be19SAneesh Kumar K.V 
1159fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11609ed3ef26SAneesh Kumar K.V }
11619ed3ef26SAneesh Kumar K.V 
11622289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11632289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11642289be19SAneesh Kumar K.V {
11652289be19SAneesh Kumar K.V     if (dir_path) {
1166e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
11672289be19SAneesh Kumar K.V     } else {
1168e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
11692289be19SAneesh Kumar K.V     }
11702289be19SAneesh Kumar K.V     return 0;
11712289be19SAneesh Kumar K.V }
11722289be19SAneesh Kumar K.V 
11732289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
11742289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
11752289be19SAneesh Kumar K.V                           const char *new_name)
11762289be19SAneesh Kumar K.V {
11772289be19SAneesh Kumar K.V     int ret;
117899f2cf4bSGreg Kurz     int odirfd, ndirfd;
11792289be19SAneesh Kumar K.V 
118099f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
118199f2cf4bSGreg Kurz     if (odirfd == -1) {
118299f2cf4bSGreg Kurz         return -1;
118399f2cf4bSGreg Kurz     }
11842289be19SAneesh Kumar K.V 
118599f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
118699f2cf4bSGreg Kurz     if (ndirfd == -1) {
118799f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
118899f2cf4bSGreg Kurz         return -1;
118999f2cf4bSGreg Kurz     }
11902289be19SAneesh Kumar K.V 
119199f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
119299f2cf4bSGreg Kurz     if (ret < 0) {
119399f2cf4bSGreg Kurz         goto out;
119499f2cf4bSGreg Kurz     }
119599f2cf4bSGreg Kurz 
119699f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
119799f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
119899f2cf4bSGreg Kurz 
119999f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
120099f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
120199f2cf4bSGreg Kurz             goto err_undo_rename;
120299f2cf4bSGreg Kurz         }
120399f2cf4bSGreg Kurz 
1204*6dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
120599f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
120699f2cf4bSGreg Kurz             goto err;
120799f2cf4bSGreg Kurz         }
120899f2cf4bSGreg Kurz 
1209*6dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
121099f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
121199f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
121299f2cf4bSGreg Kurz             goto err;
121399f2cf4bSGreg Kurz         }
121499f2cf4bSGreg Kurz 
121599f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
121699f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
121799f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
121899f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
121999f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
122099f2cf4bSGreg Kurz             goto err_undo_rename;
122199f2cf4bSGreg Kurz         }
122299f2cf4bSGreg Kurz 
122399f2cf4bSGreg Kurz         ret = 0;
122499f2cf4bSGreg Kurz     }
122599f2cf4bSGreg Kurz     goto out;
122699f2cf4bSGreg Kurz 
122799f2cf4bSGreg Kurz err:
122899f2cf4bSGreg Kurz     ret = -1;
122999f2cf4bSGreg Kurz err_undo_rename:
123099f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
123199f2cf4bSGreg Kurz out:
123299f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
123399f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
12342289be19SAneesh Kumar K.V     return ret;
12352289be19SAneesh Kumar K.V }
12362289be19SAneesh Kumar K.V 
1237d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1238d2767edeSGreg Kurz {
1239d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1240d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1241d2767edeSGreg Kurz }
1242d2767edeSGreg Kurz 
1243d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1244d2767edeSGreg Kurz                         const char *newpath)
1245d2767edeSGreg Kurz {
1246d2767edeSGreg Kurz     int err;
1247d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1248d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1249d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1250d2767edeSGreg Kurz 
1251d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1252d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1253d2767edeSGreg Kurz 
1254d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1255d2767edeSGreg Kurz 
1256d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1257d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1258d2767edeSGreg Kurz     g_free(nname);
1259d2767edeSGreg Kurz     g_free(oname);
1260d2767edeSGreg Kurz 
1261d2767edeSGreg Kurz     return err;
1262d2767edeSGreg Kurz }
1263d2767edeSGreg Kurz 
12642289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
12652289be19SAneesh Kumar K.V                           const char *name, int flags)
12662289be19SAneesh Kumar K.V {
12672289be19SAneesh Kumar K.V     int ret;
1268df4938a6SGreg Kurz     int dirfd;
12692c30dd74SAneesh Kumar K.V 
1270df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1271df4938a6SGreg Kurz     if (dirfd == -1) {
1272df4938a6SGreg Kurz         return -1;
1273df4938a6SGreg Kurz     }
12742289be19SAneesh Kumar K.V 
1275df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1276df4938a6SGreg Kurz     close_preserve_errno(dirfd);
12772289be19SAneesh Kumar K.V     return ret;
12782289be19SAneesh Kumar K.V }
12799ed3ef26SAneesh Kumar K.V 
1280e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1281e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1282e06a765eSHarsh Prateek Bora {
1283ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
12840e5fc994SKirill A. Shutemov     int err;
1285cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1286cc720ddbSAneesh Kumar K.V 
1287e06a765eSHarsh Prateek Bora     /*
1288e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1289e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1290e06a765eSHarsh Prateek Bora      */
1291e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
12921a9978a5SKirill A. Shutemov         errno = ENOTTY;
12931a9978a5SKirill A. Shutemov         return -1;
1294e06a765eSHarsh Prateek Bora     }
1295cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1296cc720ddbSAneesh Kumar K.V     if (err < 0) {
1297cc720ddbSAneesh Kumar K.V         return err;
1298e06a765eSHarsh Prateek Bora     }
1299cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1300cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1301e06a765eSHarsh Prateek Bora     return err;
13020e5fc994SKirill A. Shutemov #else
13030e5fc994SKirill A. Shutemov     errno = ENOTTY;
13040e5fc994SKirill A. Shutemov     return -1;
13050e5fc994SKirill A. Shutemov #endif
1306e06a765eSHarsh Prateek Bora }
1307e06a765eSHarsh Prateek Bora 
13080174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
13090174fe73SAneesh Kumar K.V {
1310e06a765eSHarsh Prateek Bora     struct statfs stbuf;
13110e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13120e35a378SGreg Kurz 
13130e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13140e35a378SGreg Kurz     if (data->mountfd == -1) {
13150e35a378SGreg Kurz         goto err;
13160e35a378SGreg Kurz     }
1317e06a765eSHarsh Prateek Bora 
131800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
131900c90bd1SGreg Kurz     /*
132000c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
132100c90bd1SGreg Kurz      */
13220e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13230e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13240e35a378SGreg Kurz         goto err;
132500c90bd1SGreg Kurz     }
132600c90bd1SGreg Kurz     switch (stbuf.f_type) {
132700c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
132800c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
132900c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
133000c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
133100c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
133200c90bd1SGreg Kurz         break;
133300c90bd1SGreg Kurz     }
133400c90bd1SGreg Kurz #endif
133500c90bd1SGreg Kurz 
13362c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
13372c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13382c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
13392c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
13402c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
13412c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
13422c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
13432c30dd74SAneesh Kumar K.V         /*
13442c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
13452c30dd74SAneesh Kumar K.V          * remain same.
13462c30dd74SAneesh Kumar K.V          */
13472c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13482c30dd74SAneesh Kumar K.V     }
1349c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
135000c90bd1SGreg Kurz 
13510e35a378SGreg Kurz     ctx->private = data;
135200c90bd1SGreg Kurz     return 0;
13530e35a378SGreg Kurz 
13540e35a378SGreg Kurz err:
13550e35a378SGreg Kurz     g_free(data);
13560e35a378SGreg Kurz     return -1;
13570e35a378SGreg Kurz }
13580e35a378SGreg Kurz 
13590e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13600e35a378SGreg Kurz {
13610e35a378SGreg Kurz     LocalData *data = ctx->private;
13620e35a378SGreg Kurz 
13630e35a378SGreg Kurz     close(data->mountfd);
13640e35a378SGreg Kurz     g_free(data);
13650174fe73SAneesh Kumar K.V }
13660174fe73SAneesh Kumar K.V 
136799519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
136899519f0aSAneesh Kumar K.V {
136999519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
137099519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
137199519f0aSAneesh Kumar K.V 
137299519f0aSAneesh Kumar K.V     if (!sec_model) {
137363325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
137463325b18SGreg Kurz         error_printf("valid options are:"
137563325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
137699519f0aSAneesh Kumar K.V         return -1;
137799519f0aSAneesh Kumar K.V     }
137899519f0aSAneesh Kumar K.V 
137999519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
138099519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
13812c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
13822c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
138399519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
138499519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
138599519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
13862c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
13872c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
138899519f0aSAneesh Kumar K.V     } else {
138963325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
139063325b18SGreg Kurz         error_printf("valid options are:"
139163325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
139299519f0aSAneesh Kumar K.V         return -1;
139399519f0aSAneesh Kumar K.V     }
139499519f0aSAneesh Kumar K.V 
139599519f0aSAneesh Kumar K.V     if (!path) {
139663325b18SGreg Kurz         error_report("fsdev: No path specified");
139799519f0aSAneesh Kumar K.V         return -1;
139899519f0aSAneesh Kumar K.V     }
139999519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
140099519f0aSAneesh Kumar K.V 
140199519f0aSAneesh Kumar K.V     return 0;
140299519f0aSAneesh Kumar K.V }
140399519f0aSAneesh Kumar K.V 
14049f107513SAnthony Liguori FileOperations local_ops = {
140599519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
14060174fe73SAneesh Kumar K.V     .init  = local_init,
14070e35a378SGreg Kurz     .cleanup = local_cleanup,
1408131dcb25SAnthony Liguori     .lstat = local_lstat,
1409131dcb25SAnthony Liguori     .readlink = local_readlink,
1410131dcb25SAnthony Liguori     .close = local_close,
1411131dcb25SAnthony Liguori     .closedir = local_closedir,
1412a6568fe2SAnthony Liguori     .open = local_open,
1413a6568fe2SAnthony Liguori     .opendir = local_opendir,
1414a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1415a9231555SAnthony Liguori     .telldir = local_telldir,
1416635324e8SGreg Kurz     .readdir = local_readdir,
1417a9231555SAnthony Liguori     .seekdir = local_seekdir,
141856d15a53SSanchit Garg     .preadv = local_preadv,
141956d15a53SSanchit Garg     .pwritev = local_pwritev,
1420c494dd6fSAnthony Liguori     .chmod = local_chmod,
1421c494dd6fSAnthony Liguori     .mknod = local_mknod,
1422c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1423c494dd6fSAnthony Liguori     .fstat = local_fstat,
1424c494dd6fSAnthony Liguori     .open2 = local_open2,
1425c494dd6fSAnthony Liguori     .symlink = local_symlink,
1426c494dd6fSAnthony Liguori     .link = local_link,
14278cf89e00SAnthony Liguori     .truncate = local_truncate,
14288cf89e00SAnthony Liguori     .rename = local_rename,
14298cf89e00SAnthony Liguori     .chown = local_chown,
143074bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
14315bae1900SAnthony Liguori     .remove = local_remove,
14328cf89e00SAnthony Liguori     .fsync = local_fsync,
1433be940c87SM. Mohan Kumar     .statfs = local_statfs,
1434fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1435fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
143610b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
14379ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
14382289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
14392289be19SAneesh Kumar K.V     .renameat  = local_renameat,
14402289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
14419f107513SAnthony Liguori };
1442