xref: /qemu/hw/9pfs/9p-local.c (revision ad0b46e6ac769b187cb4dcf0065675ef8a198a5e)
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 
78*ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
79*ad0b46e6SGreg Kurz {
80*ad0b46e6SGreg Kurz     int serrno = errno;
81*ad0b46e6SGreg Kurz     unlinkat(dirfd, path, flags);
82*ad0b46e6SGreg Kurz     errno = serrno;
83*ad0b46e6SGreg Kurz }
84*ad0b46e6SGreg Kurz 
852c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
862c30dd74SAneesh Kumar K.V 
874fa4ce71SChen Gang static char *local_mapped_attr_path(FsContext *ctx, const char *path)
882c30dd74SAneesh Kumar K.V {
891b6f85e2SMichael Tokarev     int dirlen;
901b6f85e2SMichael Tokarev     const char *name = strrchr(path, '/');
911b6f85e2SMichael Tokarev     if (name) {
921b6f85e2SMichael Tokarev         dirlen = name - path;
931b6f85e2SMichael Tokarev         ++name;
941b6f85e2SMichael Tokarev     } else {
951b6f85e2SMichael Tokarev         name = path;
961b6f85e2SMichael Tokarev         dirlen = 0;
971b6f85e2SMichael Tokarev     }
981b6f85e2SMichael Tokarev     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
991b6f85e2SMichael Tokarev                            dirlen, path, VIRTFS_META_DIR, name);
1002c30dd74SAneesh Kumar K.V }
1012c30dd74SAneesh Kumar K.V 
1020ceb092eSAneesh Kumar K.V static FILE *local_fopen(const char *path, const char *mode)
1030ceb092eSAneesh Kumar K.V {
1040ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
1050ceb092eSAneesh Kumar K.V     FILE *fp;
1060ceb092eSAneesh Kumar K.V     int flags = O_NOFOLLOW;
1070ceb092eSAneesh Kumar K.V     /*
1080ceb092eSAneesh Kumar K.V      * only supports two modes
1090ceb092eSAneesh Kumar K.V      */
1100ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
1110ceb092eSAneesh Kumar K.V         flags |= O_RDONLY;
1120ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
1130ceb092eSAneesh Kumar K.V         flags |= O_WRONLY | O_TRUNC | O_CREAT;
1140ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1150ceb092eSAneesh Kumar K.V     } else {
1160ceb092eSAneesh Kumar K.V         return NULL;
1170ceb092eSAneesh Kumar K.V     }
1180ceb092eSAneesh Kumar K.V     fd = open(path, flags, o_mode);
1190ceb092eSAneesh Kumar K.V     if (fd == -1) {
1200ceb092eSAneesh Kumar K.V         return NULL;
1210ceb092eSAneesh Kumar K.V     }
1220ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1230ceb092eSAneesh Kumar K.V     if (!fp) {
1240ceb092eSAneesh Kumar K.V         close(fd);
1250ceb092eSAneesh Kumar K.V     }
1260ceb092eSAneesh Kumar K.V     return fp;
1270ceb092eSAneesh Kumar K.V }
1280ceb092eSAneesh Kumar K.V 
129f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
130f9aef99bSGreg Kurz {
131f9aef99bSGreg Kurz     int fd, o_mode = 0;
132f9aef99bSGreg Kurz     FILE *fp;
133f9aef99bSGreg Kurz     int flags;
134f9aef99bSGreg Kurz     /*
135f9aef99bSGreg Kurz      * only supports two modes
136f9aef99bSGreg Kurz      */
137f9aef99bSGreg Kurz     if (mode[0] == 'r') {
138f9aef99bSGreg Kurz         flags = O_RDONLY;
139f9aef99bSGreg Kurz     } else if (mode[0] == 'w') {
140f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
141f9aef99bSGreg Kurz         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
142f9aef99bSGreg Kurz     } else {
143f9aef99bSGreg Kurz         return NULL;
144f9aef99bSGreg Kurz     }
145f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
146f9aef99bSGreg Kurz     if (fd == -1) {
147f9aef99bSGreg Kurz         return NULL;
148f9aef99bSGreg Kurz     }
149f9aef99bSGreg Kurz     fp = fdopen(fd, mode);
150f9aef99bSGreg Kurz     if (!fp) {
151f9aef99bSGreg Kurz         close(fd);
152f9aef99bSGreg Kurz     }
153f9aef99bSGreg Kurz     return fp;
154f9aef99bSGreg Kurz }
155f9aef99bSGreg Kurz 
1562c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
157f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
1582c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1592c30dd74SAneesh Kumar K.V {
1602c30dd74SAneesh Kumar K.V     FILE *fp;
1612c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
162f9aef99bSGreg Kurz     int map_dirfd;
1632c30dd74SAneesh Kumar K.V 
16499f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
165f9aef99bSGreg Kurz     if (map_dirfd == -1) {
166f9aef99bSGreg Kurz         return;
167f9aef99bSGreg Kurz     }
168f9aef99bSGreg Kurz 
169f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
170f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
1712c30dd74SAneesh Kumar K.V     if (!fp) {
1722c30dd74SAneesh Kumar K.V         return;
1732c30dd74SAneesh Kumar K.V     }
1742c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1752c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1762c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1772c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1782c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1792c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1802c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1812c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1822c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1832c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1842c30dd74SAneesh Kumar K.V         }
1852c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1862c30dd74SAneesh Kumar K.V     }
1872c30dd74SAneesh Kumar K.V     fclose(fp);
1882c30dd74SAneesh Kumar K.V }
1892c30dd74SAneesh Kumar K.V 
1902289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
191131dcb25SAnthony Liguori {
192f9aef99bSGreg Kurz     int err = -1;
193f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
194f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
195f9aef99bSGreg Kurz     int dirfd;
1962289be19SAneesh Kumar K.V 
197f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
198f9aef99bSGreg Kurz     if (dirfd == -1) {
199f9aef99bSGreg Kurz         goto out;
200f9aef99bSGreg Kurz     }
201f9aef99bSGreg Kurz 
202f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
2031237ad76SVenkateswararao Jujjuri (JV)     if (err) {
2044fa4ce71SChen Gang         goto err_out;
2051237ad76SVenkateswararao Jujjuri (JV)     }
206b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
2071237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
2081237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
2091237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
2101237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
2111237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
212f9aef99bSGreg Kurz 
213f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
214f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
215f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
2161237ad76SVenkateswararao Jujjuri (JV)         }
217f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
218f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
219f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
2201237ad76SVenkateswararao Jujjuri (JV)         }
221f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
222f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
223f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
2241237ad76SVenkateswararao Jujjuri (JV)         }
225f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
226f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
227f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
2281237ad76SVenkateswararao Jujjuri (JV)         }
2292c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
230f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
2311237ad76SVenkateswararao Jujjuri (JV)     }
2324fa4ce71SChen Gang 
2334fa4ce71SChen Gang err_out:
234f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
235f9aef99bSGreg Kurz out:
236f9aef99bSGreg Kurz     g_free(name);
237f9aef99bSGreg Kurz     g_free(dirpath);
2381237ad76SVenkateswararao Jujjuri (JV)     return err;
239131dcb25SAnthony Liguori }
240131dcb25SAnthony Liguori 
2412c30dd74SAneesh Kumar K.V static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
2422c30dd74SAneesh Kumar K.V {
2432c30dd74SAneesh Kumar K.V     int err;
2444fa4ce71SChen Gang     char *attr_dir;
245d3f8e138SMarkus Armbruster     char *tmp_path = g_strdup(path);
2462c30dd74SAneesh Kumar K.V 
2474fa4ce71SChen Gang     attr_dir = g_strdup_printf("%s/%s/%s",
2482c30dd74SAneesh Kumar K.V              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
2492c30dd74SAneesh Kumar K.V 
2502c30dd74SAneesh Kumar K.V     err = mkdir(attr_dir, 0700);
2512c30dd74SAneesh Kumar K.V     if (err < 0 && errno == EEXIST) {
2522c30dd74SAneesh Kumar K.V         err = 0;
2532c30dd74SAneesh Kumar K.V     }
2544fa4ce71SChen Gang     g_free(attr_dir);
255d3f8e138SMarkus Armbruster     g_free(tmp_path);
2562c30dd74SAneesh Kumar K.V     return err;
2572c30dd74SAneesh Kumar K.V }
2582c30dd74SAneesh Kumar K.V 
2592c30dd74SAneesh Kumar K.V static int local_set_mapped_file_attr(FsContext *ctx,
2602c30dd74SAneesh Kumar K.V                                       const char *path, FsCred *credp)
2612c30dd74SAneesh Kumar K.V {
2622c30dd74SAneesh Kumar K.V     FILE *fp;
2632c30dd74SAneesh Kumar K.V     int ret = 0;
2642c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2654fa4ce71SChen Gang     char *attr_path;
2662c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
2672c30dd74SAneesh Kumar K.V 
2684fa4ce71SChen Gang     attr_path = local_mapped_attr_path(ctx, path);
2694fa4ce71SChen Gang     fp = local_fopen(attr_path, "r");
2702c30dd74SAneesh Kumar K.V     if (!fp) {
2712c30dd74SAneesh Kumar K.V         goto create_map_file;
2722c30dd74SAneesh Kumar K.V     }
2732c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2742c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2752c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2762c30dd74SAneesh Kumar K.V             uid = atoi(buf+11);
2772c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2782c30dd74SAneesh Kumar K.V             gid = atoi(buf+11);
2792c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2802c30dd74SAneesh Kumar K.V             mode = atoi(buf+12);
2812c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2822c30dd74SAneesh Kumar K.V             rdev = atoi(buf+12);
2832c30dd74SAneesh Kumar K.V         }
2842c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2852c30dd74SAneesh Kumar K.V     }
2862c30dd74SAneesh Kumar K.V     fclose(fp);
2872c30dd74SAneesh Kumar K.V     goto update_map_file;
2882c30dd74SAneesh Kumar K.V 
2892c30dd74SAneesh Kumar K.V create_map_file:
2902c30dd74SAneesh Kumar K.V     ret = local_create_mapped_attr_dir(ctx, path);
2912c30dd74SAneesh Kumar K.V     if (ret < 0) {
2922c30dd74SAneesh Kumar K.V         goto err_out;
2932c30dd74SAneesh Kumar K.V     }
2942c30dd74SAneesh Kumar K.V 
2952c30dd74SAneesh Kumar K.V update_map_file:
2960ceb092eSAneesh Kumar K.V     fp = local_fopen(attr_path, "w");
2972c30dd74SAneesh Kumar K.V     if (!fp) {
2982c30dd74SAneesh Kumar K.V         ret = -1;
2992c30dd74SAneesh Kumar K.V         goto err_out;
3002c30dd74SAneesh Kumar K.V     }
3012c30dd74SAneesh Kumar K.V 
3022c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
3032c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
3042c30dd74SAneesh Kumar K.V     }
3052c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
3062c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
3072c30dd74SAneesh Kumar K.V     }
3082c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
3092c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
3102c30dd74SAneesh Kumar K.V     }
3112c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
3122c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
3132c30dd74SAneesh Kumar K.V     }
3142c30dd74SAneesh Kumar K.V 
3152c30dd74SAneesh Kumar K.V 
3162c30dd74SAneesh Kumar K.V     if (uid != -1) {
3172c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
3182c30dd74SAneesh Kumar K.V     }
3192c30dd74SAneesh Kumar K.V     if (gid != -1) {
3202c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
3212c30dd74SAneesh Kumar K.V     }
3222c30dd74SAneesh Kumar K.V     if (mode != -1) {
3232c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
3242c30dd74SAneesh Kumar K.V     }
3252c30dd74SAneesh Kumar K.V     if (rdev != -1) {
3262c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
3272c30dd74SAneesh Kumar K.V     }
3282c30dd74SAneesh Kumar K.V     fclose(fp);
3292c30dd74SAneesh Kumar K.V 
3302c30dd74SAneesh Kumar K.V err_out:
3314fa4ce71SChen Gang     g_free(attr_path);
3322c30dd74SAneesh Kumar K.V     return ret;
3332c30dd74SAneesh Kumar K.V }
3342c30dd74SAneesh Kumar K.V 
335758e8e38SVenkateswararao Jujjuri (JV) static int local_set_xattr(const char *path, FsCred *credp)
336131dcb25SAnthony Liguori {
337758e8e38SVenkateswararao Jujjuri (JV)     int err;
3382289be19SAneesh Kumar K.V 
339758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
340f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
341f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
342758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
343758e8e38SVenkateswararao Jujjuri (JV)             return err;
344131dcb25SAnthony Liguori         }
345131dcb25SAnthony Liguori     }
346758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
347f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
348f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
349758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
350758e8e38SVenkateswararao Jujjuri (JV)             return err;
351131dcb25SAnthony Liguori         }
352131dcb25SAnthony Liguori     }
353758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
354f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
355f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
356758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
357758e8e38SVenkateswararao Jujjuri (JV)             return err;
358131dcb25SAnthony Liguori         }
359131dcb25SAnthony Liguori     }
360758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
361f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
362f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
363758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
364758e8e38SVenkateswararao Jujjuri (JV)             return err;
365131dcb25SAnthony Liguori         }
366758e8e38SVenkateswararao Jujjuri (JV)     }
367131dcb25SAnthony Liguori     return 0;
368131dcb25SAnthony Liguori }
369131dcb25SAnthony Liguori 
3704750a96fSVenkateswararao Jujjuri (JV) static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
3714750a96fSVenkateswararao Jujjuri (JV)                                          FsCred *credp)
3724750a96fSVenkateswararao Jujjuri (JV) {
3734fa4ce71SChen Gang     char *buffer;
3742289be19SAneesh Kumar K.V 
3754fa4ce71SChen Gang     buffer = rpath(fs_ctx, path);
3764fa4ce71SChen Gang     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
37712848bfcSAneesh Kumar K.V         /*
37812848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
37912848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
38012848bfcSAneesh Kumar K.V          */
381b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
3824fa4ce71SChen Gang             goto err;
3834750a96fSVenkateswararao Jujjuri (JV)         }
38412848bfcSAneesh Kumar K.V     }
3852d40564aSM. Mohan Kumar 
3864fa4ce71SChen Gang     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
3874fa4ce71SChen Gang         goto err;
3882d40564aSM. Mohan Kumar     }
3894fa4ce71SChen Gang 
3904fa4ce71SChen Gang     g_free(buffer);
3914750a96fSVenkateswararao Jujjuri (JV)     return 0;
3924fa4ce71SChen Gang err:
3934fa4ce71SChen Gang     g_free(buffer);
3944fa4ce71SChen Gang     return -1;
3954750a96fSVenkateswararao Jujjuri (JV) }
3964750a96fSVenkateswararao Jujjuri (JV) 
3972289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
398131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
399131dcb25SAnthony Liguori {
400879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
4012289be19SAneesh Kumar K.V 
4022c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
4032c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
404879c2813SVenkateswararao Jujjuri (JV)         int fd;
405bec1e954SGreg Kurz 
406bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
407879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
408879c2813SVenkateswararao Jujjuri (JV)             return -1;
409879c2813SVenkateswararao Jujjuri (JV)         }
410879c2813SVenkateswararao Jujjuri (JV)         do {
411879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
412879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
413bec1e954SGreg Kurz         close_preserve_errno(fd);
414b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
415b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
416bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
417bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
418bec1e954SGreg Kurz         int dirfd;
419bec1e954SGreg Kurz 
420bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
421bec1e954SGreg Kurz         if (dirfd == -1) {
422bec1e954SGreg Kurz             goto out;
423bec1e954SGreg Kurz         }
424bec1e954SGreg Kurz 
425bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
426bec1e954SGreg Kurz         close_preserve_errno(dirfd);
427bec1e954SGreg Kurz     out:
428bec1e954SGreg Kurz         g_free(name);
429bec1e954SGreg Kurz         g_free(dirpath);
430879c2813SVenkateswararao Jujjuri (JV)     }
431879c2813SVenkateswararao Jujjuri (JV)     return tsize;
432131dcb25SAnthony Liguori }
433131dcb25SAnthony Liguori 
434cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
435131dcb25SAnthony Liguori {
436cc720ddbSAneesh Kumar K.V     return close(fs->fd);
437131dcb25SAnthony Liguori }
438131dcb25SAnthony Liguori 
439cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
440131dcb25SAnthony Liguori {
441f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
442131dcb25SAnthony Liguori }
4439f107513SAnthony Liguori 
444cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
445cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
446a6568fe2SAnthony Liguori {
44721328e1eSGreg Kurz     int fd;
4482289be19SAneesh Kumar K.V 
449996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
45021328e1eSGreg Kurz     if (fd == -1) {
45121328e1eSGreg Kurz         return -1;
45221328e1eSGreg Kurz     }
45321328e1eSGreg Kurz     fs->fd = fd;
454cc720ddbSAneesh Kumar K.V     return fs->fd;
455a6568fe2SAnthony Liguori }
456a6568fe2SAnthony Liguori 
457cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
458cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
459a6568fe2SAnthony Liguori {
460996a0d76SGreg Kurz     int dirfd;
46121328e1eSGreg Kurz     DIR *stream;
4622289be19SAneesh Kumar K.V 
463996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
464996a0d76SGreg Kurz     if (dirfd == -1) {
465996a0d76SGreg Kurz         return -1;
466996a0d76SGreg Kurz     }
467996a0d76SGreg Kurz 
468996a0d76SGreg Kurz     stream = fdopendir(dirfd);
46921328e1eSGreg Kurz     if (!stream) {
470cc720ddbSAneesh Kumar K.V         return -1;
471cc720ddbSAneesh Kumar K.V     }
47221328e1eSGreg Kurz     fs->dir.stream = stream;
473cc720ddbSAneesh Kumar K.V     return 0;
474a6568fe2SAnthony Liguori }
475a6568fe2SAnthony Liguori 
476cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
477a9231555SAnthony Liguori {
478f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
479a9231555SAnthony Liguori }
480a9231555SAnthony Liguori 
481cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
482a9231555SAnthony Liguori {
483f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
484a9231555SAnthony Liguori }
485a9231555SAnthony Liguori 
486635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
487a9231555SAnthony Liguori {
488635324e8SGreg Kurz     struct dirent *entry;
4892c30dd74SAneesh Kumar K.V 
4902c30dd74SAneesh Kumar K.V again:
491635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
492635324e8SGreg Kurz     if (!entry) {
493635324e8SGreg Kurz         return NULL;
494635324e8SGreg Kurz     }
495635324e8SGreg Kurz 
496840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
497840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
498840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
499635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
5002c30dd74SAneesh Kumar K.V             /* skp the meta data directory */
5012c30dd74SAneesh Kumar K.V             goto again;
5022c30dd74SAneesh Kumar K.V         }
503840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
5042c30dd74SAneesh Kumar K.V     }
505635324e8SGreg Kurz 
506635324e8SGreg Kurz     return entry;
507a9231555SAnthony Liguori }
508a9231555SAnthony Liguori 
509cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
510a9231555SAnthony Liguori {
511f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
512a9231555SAnthony Liguori }
513a9231555SAnthony Liguori 
514cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
515cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
51656d15a53SSanchit Garg                             int iovcnt, off_t offset)
517a9231555SAnthony Liguori {
51856d15a53SSanchit Garg #ifdef CONFIG_PREADV
519cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
52056d15a53SSanchit Garg #else
521cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
52256d15a53SSanchit Garg     if (err == -1) {
52356d15a53SSanchit Garg         return err;
52456d15a53SSanchit Garg     } else {
525cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
526a9231555SAnthony Liguori     }
52756d15a53SSanchit Garg #endif
528a9231555SAnthony Liguori }
529a9231555SAnthony Liguori 
530cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
531cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
53256d15a53SSanchit Garg                              int iovcnt, off_t offset)
5338449360cSAnthony Liguori {
5346fe76accSGreg Kurz     ssize_t ret;
53556d15a53SSanchit Garg #ifdef CONFIG_PREADV
536cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
53756d15a53SSanchit Garg #else
538cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
53956d15a53SSanchit Garg     if (err == -1) {
54056d15a53SSanchit Garg         return err;
54156d15a53SSanchit Garg     } else {
542cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
5438449360cSAnthony Liguori     }
54456d15a53SSanchit Garg #endif
545d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
546d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
547d3ab98e6SAneesh Kumar K.V         /*
548d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
549d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
550d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
551d3ab98e6SAneesh Kumar K.V          */
552cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
553d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
554d3ab98e6SAneesh Kumar K.V     }
555d3ab98e6SAneesh Kumar K.V #endif
556d3ab98e6SAneesh Kumar K.V     return ret;
55756d15a53SSanchit Garg }
5588449360cSAnthony Liguori 
5592289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
560c494dd6fSAnthony Liguori {
5614fa4ce71SChen Gang     char *buffer;
5624fa4ce71SChen Gang     int ret = -1;
5632289be19SAneesh Kumar K.V     char *path = fs_path->data;
5642289be19SAneesh Kumar K.V 
565b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5664fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5674fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
5684fa4ce71SChen Gang         g_free(buffer);
5692c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5702c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
571b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
572b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
5734fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5744fa4ce71SChen Gang         ret = chmod(buffer, credp->fc_mode);
5754fa4ce71SChen Gang         g_free(buffer);
576e95ead32SVenkateswararao Jujjuri (JV)     }
5774fa4ce71SChen Gang     return ret;
578c494dd6fSAnthony Liguori }
579c494dd6fSAnthony Liguori 
5802289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
5812289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
582c494dd6fSAnthony Liguori {
5832289be19SAneesh Kumar K.V     char *path;
5841c293312SVenkateswararao Jujjuri (JV)     int err = -1;
5851c293312SVenkateswararao Jujjuri (JV)     int serrno = 0;
5862289be19SAneesh Kumar K.V     V9fsString fullname;
5874ed7b2c3SStefan Weil     char *buffer = NULL;
5881c293312SVenkateswararao Jujjuri (JV) 
5892289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
5902289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
5912289be19SAneesh Kumar K.V     path = fullname.data;
5922289be19SAneesh Kumar K.V 
5931c293312SVenkateswararao Jujjuri (JV)     /* Determine the security model */
594b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5954fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5964fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
5971c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5982289be19SAneesh Kumar K.V             goto out;
5991c293312SVenkateswararao Jujjuri (JV)         }
6004fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
6011c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6021c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
6031c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6041c293312SVenkateswararao Jujjuri (JV)         }
6052c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6062c30dd74SAneesh Kumar K.V 
6074fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6084fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
6092c30dd74SAneesh Kumar K.V         if (err == -1) {
6102c30dd74SAneesh Kumar K.V             goto out;
6112c30dd74SAneesh Kumar K.V         }
6122c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
6132c30dd74SAneesh Kumar K.V         if (err == -1) {
6142c30dd74SAneesh Kumar K.V             serrno = errno;
6152c30dd74SAneesh Kumar K.V             goto err_end;
6162c30dd74SAneesh Kumar K.V         }
617b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
618b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
6194fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6204fa4ce71SChen Gang         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
6211c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6222289be19SAneesh Kumar K.V             goto out;
6231c293312SVenkateswararao Jujjuri (JV)         }
6241c293312SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
6251c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
6261c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
6271c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6281c293312SVenkateswararao Jujjuri (JV)         }
6291c293312SVenkateswararao Jujjuri (JV)     }
6302289be19SAneesh Kumar K.V     goto out;
6311c293312SVenkateswararao Jujjuri (JV) 
6321c293312SVenkateswararao Jujjuri (JV) err_end:
6334fa4ce71SChen Gang     remove(buffer);
6341c293312SVenkateswararao Jujjuri (JV)     errno = serrno;
6352289be19SAneesh Kumar K.V out:
6364ed7b2c3SStefan Weil     g_free(buffer);
6372289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
6381c293312SVenkateswararao Jujjuri (JV)     return err;
639c494dd6fSAnthony Liguori }
640c494dd6fSAnthony Liguori 
6412289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
6422289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
643c494dd6fSAnthony Liguori {
6442289be19SAneesh Kumar K.V     char *path;
64500ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
64600ec5c37SVenkateswararao Jujjuri (JV)     int serrno = 0;
6472289be19SAneesh Kumar K.V     V9fsString fullname;
6484ed7b2c3SStefan Weil     char *buffer = NULL;
64900ec5c37SVenkateswararao Jujjuri (JV) 
6502289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
6512289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
6522289be19SAneesh Kumar K.V     path = fullname.data;
6532289be19SAneesh Kumar K.V 
65400ec5c37SVenkateswararao Jujjuri (JV)     /* Determine the security model */
655b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6564fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6574fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
65800ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
6592289be19SAneesh Kumar K.V             goto out;
66000ec5c37SVenkateswararao Jujjuri (JV)         }
66100ec5c37SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFDIR;
6624fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
66300ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
66400ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
66500ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
66600ec5c37SVenkateswararao Jujjuri (JV)         }
6672c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6684fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6694fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
6702c30dd74SAneesh Kumar K.V         if (err == -1) {
6712c30dd74SAneesh Kumar K.V             goto out;
6722c30dd74SAneesh Kumar K.V         }
6732c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFDIR;
6742c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
6752c30dd74SAneesh Kumar K.V         if (err == -1) {
6762c30dd74SAneesh Kumar K.V             serrno = errno;
6772c30dd74SAneesh Kumar K.V             goto err_end;
6782c30dd74SAneesh Kumar K.V         }
679b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
680b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
6814fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6824fa4ce71SChen Gang         err = mkdir(buffer, credp->fc_mode);
68300ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
6842289be19SAneesh Kumar K.V             goto out;
68500ec5c37SVenkateswararao Jujjuri (JV)         }
68600ec5c37SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
68700ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
68800ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
68900ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
69000ec5c37SVenkateswararao Jujjuri (JV)         }
69100ec5c37SVenkateswararao Jujjuri (JV)     }
6922289be19SAneesh Kumar K.V     goto out;
69300ec5c37SVenkateswararao Jujjuri (JV) 
69400ec5c37SVenkateswararao Jujjuri (JV) err_end:
6954fa4ce71SChen Gang     remove(buffer);
69600ec5c37SVenkateswararao Jujjuri (JV)     errno = serrno;
6972289be19SAneesh Kumar K.V out:
6984ed7b2c3SStefan Weil     g_free(buffer);
6992289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
70000ec5c37SVenkateswararao Jujjuri (JV)     return err;
701c494dd6fSAnthony Liguori }
702c494dd6fSAnthony Liguori 
7038b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
704cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
705c494dd6fSAnthony Liguori {
7068b888272SAneesh Kumar K.V     int err, fd;
7078b888272SAneesh Kumar K.V 
7088b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
709f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
7108b888272SAneesh Kumar K.V     } else {
7118b888272SAneesh Kumar K.V         fd = fs->fd;
7128b888272SAneesh Kumar K.V     }
7138b888272SAneesh Kumar K.V 
7148b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
7151237ad76SVenkateswararao Jujjuri (JV)     if (err) {
7161237ad76SVenkateswararao Jujjuri (JV)         return err;
7171237ad76SVenkateswararao Jujjuri (JV)     }
718b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7191237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
7201237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
7211237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
7221237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
7231237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
7241237ad76SVenkateswararao Jujjuri (JV) 
725f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
726f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
7271237ad76SVenkateswararao Jujjuri (JV)         }
728f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
729f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
7301237ad76SVenkateswararao Jujjuri (JV)         }
731f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
732f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
7331237ad76SVenkateswararao Jujjuri (JV)         }
734f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
735f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
7361237ad76SVenkateswararao Jujjuri (JV)         }
7372c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7382c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
7392c30dd74SAneesh Kumar K.V         return -1;
7401237ad76SVenkateswararao Jujjuri (JV)     }
7411237ad76SVenkateswararao Jujjuri (JV)     return err;
742c494dd6fSAnthony Liguori }
743c494dd6fSAnthony Liguori 
7442289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
745cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
746c494dd6fSAnthony Liguori {
7472289be19SAneesh Kumar K.V     char *path;
7484750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
7494750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
7504750a96fSVenkateswararao Jujjuri (JV)     int serrno = 0;
7512289be19SAneesh Kumar K.V     V9fsString fullname;
7524ed7b2c3SStefan Weil     char *buffer = NULL;
7534750a96fSVenkateswararao Jujjuri (JV) 
7540ceb092eSAneesh Kumar K.V     /*
7550ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
7560ceb092eSAneesh Kumar K.V      */
7570ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
7580ceb092eSAneesh Kumar K.V 
7592289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
7602289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
7612289be19SAneesh Kumar K.V     path = fullname.data;
7622289be19SAneesh Kumar K.V 
7634750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
764b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7654fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7664fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7674750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7682289be19SAneesh Kumar K.V             err = fd;
7692289be19SAneesh Kumar K.V             goto out;
7704750a96fSVenkateswararao Jujjuri (JV)         }
7714750a96fSVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFREG;
7724750a96fSVenkateswararao Jujjuri (JV)         /* Set cleint credentials in xattr */
7734fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
7744750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7754750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
7764750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7774750a96fSVenkateswararao Jujjuri (JV)         }
7782c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7794fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7804fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7812c30dd74SAneesh Kumar K.V         if (fd == -1) {
7822c30dd74SAneesh Kumar K.V             err = fd;
7832c30dd74SAneesh Kumar K.V             goto out;
7842c30dd74SAneesh Kumar K.V         }
7852c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFREG;
7862c30dd74SAneesh Kumar K.V         /* Set client credentials in .virtfs_metadata directory files */
7872c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
7882c30dd74SAneesh Kumar K.V         if (err == -1) {
7892c30dd74SAneesh Kumar K.V             serrno = errno;
7902c30dd74SAneesh Kumar K.V             goto err_end;
7912c30dd74SAneesh Kumar K.V         }
792b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
793b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
7944fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7954fa4ce71SChen Gang         fd = open(buffer, flags, credp->fc_mode);
7964750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7972289be19SAneesh Kumar K.V             err = fd;
7982289be19SAneesh Kumar K.V             goto out;
7994750a96fSVenkateswararao Jujjuri (JV)         }
8004750a96fSVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
8014750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
8024750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
8034750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
8044750a96fSVenkateswararao Jujjuri (JV)         }
8054750a96fSVenkateswararao Jujjuri (JV)     }
8062289be19SAneesh Kumar K.V     err = fd;
807cc720ddbSAneesh Kumar K.V     fs->fd = fd;
8082289be19SAneesh Kumar K.V     goto out;
8094750a96fSVenkateswararao Jujjuri (JV) 
8104750a96fSVenkateswararao Jujjuri (JV) err_end:
8114750a96fSVenkateswararao Jujjuri (JV)     close(fd);
8124fa4ce71SChen Gang     remove(buffer);
8134750a96fSVenkateswararao Jujjuri (JV)     errno = serrno;
8142289be19SAneesh Kumar K.V out:
8154ed7b2c3SStefan Weil     g_free(buffer);
8162289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
8174750a96fSVenkateswararao Jujjuri (JV)     return err;
818c494dd6fSAnthony Liguori }
819c494dd6fSAnthony Liguori 
820758e8e38SVenkateswararao Jujjuri (JV) 
821879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
8222289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
823c494dd6fSAnthony Liguori {
824879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
825879c2813SVenkateswararao Jujjuri (JV)     int serrno = 0;
8262289be19SAneesh Kumar K.V     char *newpath;
8272289be19SAneesh Kumar K.V     V9fsString fullname;
8284ed7b2c3SStefan Weil     char *buffer = NULL;
829879c2813SVenkateswararao Jujjuri (JV) 
8302289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
8312289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
8322289be19SAneesh Kumar K.V     newpath = fullname.data;
8332289be19SAneesh Kumar K.V 
834879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
835b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
836879c2813SVenkateswararao Jujjuri (JV)         int fd;
837879c2813SVenkateswararao Jujjuri (JV)         ssize_t oldpath_size, write_size;
8384fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8394fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
840879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
8412289be19SAneesh Kumar K.V             err = fd;
8422289be19SAneesh Kumar K.V             goto out;
843879c2813SVenkateswararao Jujjuri (JV)         }
844879c2813SVenkateswararao Jujjuri (JV)         /* Write the oldpath (target) to the file. */
845f35bde2fSHarsh Prateek Bora         oldpath_size = strlen(oldpath);
846879c2813SVenkateswararao Jujjuri (JV)         do {
847879c2813SVenkateswararao Jujjuri (JV)             write_size = write(fd, (void *)oldpath, oldpath_size);
848879c2813SVenkateswararao Jujjuri (JV)         } while (write_size == -1 && errno == EINTR);
849879c2813SVenkateswararao Jujjuri (JV) 
850879c2813SVenkateswararao Jujjuri (JV)         if (write_size != oldpath_size) {
851879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
852879c2813SVenkateswararao Jujjuri (JV)             close(fd);
853879c2813SVenkateswararao Jujjuri (JV)             err = -1;
854879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
855879c2813SVenkateswararao Jujjuri (JV)         }
856879c2813SVenkateswararao Jujjuri (JV)         close(fd);
857879c2813SVenkateswararao Jujjuri (JV)         /* Set cleint credentials in symlink's xattr */
858879c2813SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFLNK;
8594fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
860879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
861879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
862879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
863879c2813SVenkateswararao Jujjuri (JV)         }
8642c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
8652c30dd74SAneesh Kumar K.V         int fd;
8662c30dd74SAneesh Kumar K.V         ssize_t oldpath_size, write_size;
8674fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8684fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
8692c30dd74SAneesh Kumar K.V         if (fd == -1) {
8702c30dd74SAneesh Kumar K.V             err = fd;
8712c30dd74SAneesh Kumar K.V             goto out;
8722c30dd74SAneesh Kumar K.V         }
8732c30dd74SAneesh Kumar K.V         /* Write the oldpath (target) to the file. */
8742c30dd74SAneesh Kumar K.V         oldpath_size = strlen(oldpath);
8752c30dd74SAneesh Kumar K.V         do {
8762c30dd74SAneesh Kumar K.V             write_size = write(fd, (void *)oldpath, oldpath_size);
8772c30dd74SAneesh Kumar K.V         } while (write_size == -1 && errno == EINTR);
8782c30dd74SAneesh Kumar K.V 
8792c30dd74SAneesh Kumar K.V         if (write_size != oldpath_size) {
8802c30dd74SAneesh Kumar K.V             serrno = errno;
8812c30dd74SAneesh Kumar K.V             close(fd);
8822c30dd74SAneesh Kumar K.V             err = -1;
8832c30dd74SAneesh Kumar K.V             goto err_end;
8842c30dd74SAneesh Kumar K.V         }
8852c30dd74SAneesh Kumar K.V         close(fd);
8862c30dd74SAneesh Kumar K.V         /* Set cleint credentials in symlink's xattr */
8872c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFLNK;
8882c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
8892c30dd74SAneesh Kumar K.V         if (err == -1) {
8902c30dd74SAneesh Kumar K.V             serrno = errno;
8912c30dd74SAneesh Kumar K.V             goto err_end;
8922c30dd74SAneesh Kumar K.V         }
893b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
894b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
8954fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8964fa4ce71SChen Gang         err = symlink(oldpath, buffer);
897879c2813SVenkateswararao Jujjuri (JV)         if (err) {
8982289be19SAneesh Kumar K.V             goto out;
899879c2813SVenkateswararao Jujjuri (JV)         }
9004fa4ce71SChen Gang         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
901879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
90212848bfcSAneesh Kumar K.V             /*
90312848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
90412848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
90512848bfcSAneesh Kumar K.V              */
906b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
907879c2813SVenkateswararao Jujjuri (JV)                 serrno = errno;
908879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
90912848bfcSAneesh Kumar K.V             } else
91012848bfcSAneesh Kumar K.V                 err = 0;
911879c2813SVenkateswararao Jujjuri (JV)         }
912879c2813SVenkateswararao Jujjuri (JV)     }
9132289be19SAneesh Kumar K.V     goto out;
914879c2813SVenkateswararao Jujjuri (JV) 
915879c2813SVenkateswararao Jujjuri (JV) err_end:
9164fa4ce71SChen Gang     remove(buffer);
917879c2813SVenkateswararao Jujjuri (JV)     errno = serrno;
9182289be19SAneesh Kumar K.V out:
9194ed7b2c3SStefan Weil     g_free(buffer);
9202289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
921879c2813SVenkateswararao Jujjuri (JV)     return err;
922c494dd6fSAnthony Liguori }
923c494dd6fSAnthony Liguori 
9242289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
9252289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
926c494dd6fSAnthony Liguori {
927*ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
928*ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
929*ad0b46e6SGreg Kurz     int ret = -1;
930*ad0b46e6SGreg Kurz     int odirfd, ndirfd;
931c494dd6fSAnthony Liguori 
932*ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
933*ad0b46e6SGreg Kurz     if (odirfd == -1) {
9346dd4b1f1SGreg Kurz         goto out;
9356dd4b1f1SGreg Kurz     }
9362c30dd74SAneesh Kumar K.V 
937*ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
938*ad0b46e6SGreg Kurz     if (ndirfd == -1) {
939*ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
940*ad0b46e6SGreg Kurz         goto out;
941*ad0b46e6SGreg Kurz     }
942*ad0b46e6SGreg Kurz 
943*ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
944*ad0b46e6SGreg Kurz     if (ret < 0) {
945*ad0b46e6SGreg Kurz         goto out_close;
946*ad0b46e6SGreg Kurz     }
947*ad0b46e6SGreg Kurz 
9482c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
9496dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
950*ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
9516dd4b1f1SGreg Kurz 
952*ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
953*ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
954*ad0b46e6SGreg Kurz             goto err_undo_link;
9552c30dd74SAneesh Kumar K.V         }
956*ad0b46e6SGreg Kurz 
957*ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
958*ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
959*ad0b46e6SGreg Kurz             goto err;
960*ad0b46e6SGreg Kurz         }
961*ad0b46e6SGreg Kurz 
962*ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
963*ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
964*ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
965*ad0b46e6SGreg Kurz             goto err;
966*ad0b46e6SGreg Kurz         }
967*ad0b46e6SGreg Kurz 
968*ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
969*ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
970*ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
9712c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
972*ad0b46e6SGreg Kurz             goto err_undo_link;
9732c30dd74SAneesh Kumar K.V         }
9746dd4b1f1SGreg Kurz 
975*ad0b46e6SGreg Kurz         ret = 0;
976*ad0b46e6SGreg Kurz     }
977*ad0b46e6SGreg Kurz     goto out_close;
978*ad0b46e6SGreg Kurz 
979*ad0b46e6SGreg Kurz err:
980*ad0b46e6SGreg Kurz     ret = -1;
981*ad0b46e6SGreg Kurz err_undo_link:
982*ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
983*ad0b46e6SGreg Kurz out_close:
984*ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
985*ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
9866dd4b1f1SGreg Kurz out:
987*ad0b46e6SGreg Kurz     g_free(oname);
988*ad0b46e6SGreg Kurz     g_free(odirpath);
9892289be19SAneesh Kumar K.V     return ret;
990c494dd6fSAnthony Liguori }
991c494dd6fSAnthony Liguori 
9922289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
9938cf89e00SAnthony Liguori {
994ac125d99SGreg Kurz     int fd, ret;
9952289be19SAneesh Kumar K.V 
996ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
997ac125d99SGreg Kurz     if (fd == -1) {
998ac125d99SGreg Kurz         return -1;
999ac125d99SGreg Kurz     }
1000ac125d99SGreg Kurz     ret = ftruncate(fd, size);
1001ac125d99SGreg Kurz     close_preserve_errno(fd);
10024fa4ce71SChen Gang     return ret;
10038cf89e00SAnthony Liguori }
10048cf89e00SAnthony Liguori 
10052289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
10068cf89e00SAnthony Liguori {
10074fa4ce71SChen Gang     char *buffer;
10084fa4ce71SChen Gang     int ret = -1;
10092289be19SAneesh Kumar K.V     char *path = fs_path->data;
10102289be19SAneesh Kumar K.V 
1011c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
101217b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
101317b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
10144fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
10154fa4ce71SChen Gang         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
10164fa4ce71SChen Gang         g_free(buffer);
1017b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
10184fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
10194fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
10204fa4ce71SChen Gang         g_free(buffer);
10212c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
10222c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
1023f7613beeSVenkateswararao Jujjuri (JV)     }
10244fa4ce71SChen Gang     return ret;
10258cf89e00SAnthony Liguori }
10268cf89e00SAnthony Liguori 
10272289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
102874bc02b2SM. Mohan Kumar                            const struct timespec *buf)
10298cf89e00SAnthony Liguori {
1030a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1031a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1032a33eda0dSGreg Kurz     int dirfd, ret = -1;
10332289be19SAneesh Kumar K.V 
1034a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1035a33eda0dSGreg Kurz     if (dirfd == -1) {
1036a33eda0dSGreg Kurz         goto out;
1037a33eda0dSGreg Kurz     }
1038a33eda0dSGreg Kurz 
1039a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1040a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1041a33eda0dSGreg Kurz out:
1042a33eda0dSGreg Kurz     g_free(dirpath);
1043a33eda0dSGreg Kurz     g_free(name);
10444fa4ce71SChen Gang     return ret;
10458cf89e00SAnthony Liguori }
10468cf89e00SAnthony Liguori 
1047df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1048df4938a6SGreg Kurz                                  int flags)
1049df4938a6SGreg Kurz {
1050df4938a6SGreg Kurz     int ret = -1;
1051df4938a6SGreg Kurz 
1052df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1053df4938a6SGreg Kurz         int map_dirfd;
1054df4938a6SGreg Kurz 
1055df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1056df4938a6SGreg Kurz             int fd;
1057df4938a6SGreg Kurz 
1058df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1059df4938a6SGreg Kurz             if (fd == -1) {
1060df4938a6SGreg Kurz                 goto err_out;
1061df4938a6SGreg Kurz             }
1062df4938a6SGreg Kurz             /*
1063df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1064df4938a6SGreg Kurz              * directory
1065df4938a6SGreg Kurz              */
1066df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1067df4938a6SGreg Kurz             close_preserve_errno(fd);
1068df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1069df4938a6SGreg Kurz                 /*
1070df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1071df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1072df4938a6SGreg Kurz                  */
1073df4938a6SGreg Kurz                 goto err_out;
1074df4938a6SGreg Kurz             }
1075df4938a6SGreg Kurz         }
1076df4938a6SGreg Kurz         /*
1077df4938a6SGreg Kurz          * Now remove the name from parent directory
1078df4938a6SGreg Kurz          * .virtfs_metadata directory.
1079df4938a6SGreg Kurz          */
1080df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1081df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1082df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1083df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1084df4938a6SGreg Kurz             /*
1085df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1086df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1087df4938a6SGreg Kurz              */
1088df4938a6SGreg Kurz             goto err_out;
1089df4938a6SGreg Kurz         }
1090df4938a6SGreg Kurz     }
1091df4938a6SGreg Kurz 
1092df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1093df4938a6SGreg Kurz err_out:
1094df4938a6SGreg Kurz     return ret;
1095df4938a6SGreg Kurz }
1096df4938a6SGreg Kurz 
10975bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10985bae1900SAnthony Liguori {
10992c30dd74SAneesh Kumar K.V     struct stat stbuf;
1100a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1101a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1102a0e640a8SGreg Kurz     int flags = 0;
1103a0e640a8SGreg Kurz     int dirfd;
1104a0e640a8SGreg Kurz     int err = -1;
11052c30dd74SAneesh Kumar K.V 
1106a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1107a0e640a8SGreg Kurz     if (dirfd) {
1108a0e640a8SGreg Kurz         goto out;
1109a0e640a8SGreg Kurz     }
1110a0e640a8SGreg Kurz 
1111a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
11122c30dd74SAneesh Kumar K.V         goto err_out;
11132c30dd74SAneesh Kumar K.V     }
1114a0e640a8SGreg Kurz 
11152c30dd74SAneesh Kumar K.V     if (S_ISDIR(stbuf.st_mode)) {
1116a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
11172c30dd74SAneesh Kumar K.V     }
11184fa4ce71SChen Gang 
1119a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
11202c30dd74SAneesh Kumar K.V err_out:
1121a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1122a0e640a8SGreg Kurz out:
1123a0e640a8SGreg Kurz     g_free(name);
1124a0e640a8SGreg Kurz     g_free(dirpath);
11252c30dd74SAneesh Kumar K.V     return err;
11265bae1900SAnthony Liguori }
11275bae1900SAnthony Liguori 
11288b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
11298b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
11308cf89e00SAnthony Liguori {
11318b888272SAneesh Kumar K.V     int fd;
11328b888272SAneesh Kumar K.V 
11338b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1134f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
113549594973SVenkateswararao Jujjuri (JV)     } else {
11368b888272SAneesh Kumar K.V         fd = fs->fd;
11378b888272SAneesh Kumar K.V     }
11388b888272SAneesh Kumar K.V 
11398b888272SAneesh Kumar K.V     if (datasync) {
11408b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
11418b888272SAneesh Kumar K.V     } else {
11428b888272SAneesh Kumar K.V         return fsync(fd);
11438cf89e00SAnthony Liguori     }
114449594973SVenkateswararao Jujjuri (JV) }
11458cf89e00SAnthony Liguori 
11462289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1147be940c87SM. Mohan Kumar {
114831e51d1cSGreg Kurz     int fd, ret;
11492289be19SAneesh Kumar K.V 
115031e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
115131e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
115231e51d1cSGreg Kurz     close_preserve_errno(fd);
11534fa4ce71SChen Gang     return ret;
1154be940c87SM. Mohan Kumar }
1155be940c87SM. Mohan Kumar 
11562289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1157fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1158fa32ef88SAneesh Kumar K.V {
11592289be19SAneesh Kumar K.V     char *path = fs_path->data;
11602289be19SAneesh Kumar K.V 
1161fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1162fa32ef88SAneesh Kumar K.V }
1163fa32ef88SAneesh Kumar K.V 
11642289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1165fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1166fa32ef88SAneesh Kumar K.V {
11672289be19SAneesh Kumar K.V     char *path = fs_path->data;
11682289be19SAneesh Kumar K.V 
1169fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
117061b6c499SAneesh Kumar K.V }
117161b6c499SAneesh Kumar K.V 
11722289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
117310b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
117410b468bdSAneesh Kumar K.V {
11752289be19SAneesh Kumar K.V     char *path = fs_path->data;
11762289be19SAneesh Kumar K.V 
1177fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
117810b468bdSAneesh Kumar K.V }
117910b468bdSAneesh Kumar K.V 
11802289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11812289be19SAneesh Kumar K.V                               const char *name)
11829ed3ef26SAneesh Kumar K.V {
11832289be19SAneesh Kumar K.V     char *path = fs_path->data;
11842289be19SAneesh Kumar K.V 
1185fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11869ed3ef26SAneesh Kumar K.V }
11879ed3ef26SAneesh Kumar K.V 
11882289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11892289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11902289be19SAneesh Kumar K.V {
11912289be19SAneesh Kumar K.V     if (dir_path) {
1192e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
11932289be19SAneesh Kumar K.V     } else {
1194e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
11952289be19SAneesh Kumar K.V     }
11962289be19SAneesh Kumar K.V     return 0;
11972289be19SAneesh Kumar K.V }
11982289be19SAneesh Kumar K.V 
11992289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
12002289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
12012289be19SAneesh Kumar K.V                           const char *new_name)
12022289be19SAneesh Kumar K.V {
12032289be19SAneesh Kumar K.V     int ret;
120499f2cf4bSGreg Kurz     int odirfd, ndirfd;
12052289be19SAneesh Kumar K.V 
120699f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
120799f2cf4bSGreg Kurz     if (odirfd == -1) {
120899f2cf4bSGreg Kurz         return -1;
120999f2cf4bSGreg Kurz     }
12102289be19SAneesh Kumar K.V 
121199f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
121299f2cf4bSGreg Kurz     if (ndirfd == -1) {
121399f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
121499f2cf4bSGreg Kurz         return -1;
121599f2cf4bSGreg Kurz     }
12162289be19SAneesh Kumar K.V 
121799f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
121899f2cf4bSGreg Kurz     if (ret < 0) {
121999f2cf4bSGreg Kurz         goto out;
122099f2cf4bSGreg Kurz     }
122199f2cf4bSGreg Kurz 
122299f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
122399f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
122499f2cf4bSGreg Kurz 
122599f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
122699f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
122799f2cf4bSGreg Kurz             goto err_undo_rename;
122899f2cf4bSGreg Kurz         }
122999f2cf4bSGreg Kurz 
12306dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
123199f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
123299f2cf4bSGreg Kurz             goto err;
123399f2cf4bSGreg Kurz         }
123499f2cf4bSGreg Kurz 
12356dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
123699f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
123799f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
123899f2cf4bSGreg Kurz             goto err;
123999f2cf4bSGreg Kurz         }
124099f2cf4bSGreg Kurz 
124199f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
124299f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
124399f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
124499f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
124599f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
124699f2cf4bSGreg Kurz             goto err_undo_rename;
124799f2cf4bSGreg Kurz         }
124899f2cf4bSGreg Kurz 
124999f2cf4bSGreg Kurz         ret = 0;
125099f2cf4bSGreg Kurz     }
125199f2cf4bSGreg Kurz     goto out;
125299f2cf4bSGreg Kurz 
125399f2cf4bSGreg Kurz err:
125499f2cf4bSGreg Kurz     ret = -1;
125599f2cf4bSGreg Kurz err_undo_rename:
125699f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
125799f2cf4bSGreg Kurz out:
125899f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
125999f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
12602289be19SAneesh Kumar K.V     return ret;
12612289be19SAneesh Kumar K.V }
12622289be19SAneesh Kumar K.V 
1263d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1264d2767edeSGreg Kurz {
1265d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1266d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1267d2767edeSGreg Kurz }
1268d2767edeSGreg Kurz 
1269d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1270d2767edeSGreg Kurz                         const char *newpath)
1271d2767edeSGreg Kurz {
1272d2767edeSGreg Kurz     int err;
1273d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1274d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1275d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1276d2767edeSGreg Kurz 
1277d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1278d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1279d2767edeSGreg Kurz 
1280d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1281d2767edeSGreg Kurz 
1282d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1283d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1284d2767edeSGreg Kurz     g_free(nname);
1285d2767edeSGreg Kurz     g_free(oname);
1286d2767edeSGreg Kurz 
1287d2767edeSGreg Kurz     return err;
1288d2767edeSGreg Kurz }
1289d2767edeSGreg Kurz 
12902289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
12912289be19SAneesh Kumar K.V                           const char *name, int flags)
12922289be19SAneesh Kumar K.V {
12932289be19SAneesh Kumar K.V     int ret;
1294df4938a6SGreg Kurz     int dirfd;
12952c30dd74SAneesh Kumar K.V 
1296df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1297df4938a6SGreg Kurz     if (dirfd == -1) {
1298df4938a6SGreg Kurz         return -1;
1299df4938a6SGreg Kurz     }
13002289be19SAneesh Kumar K.V 
1301df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1302df4938a6SGreg Kurz     close_preserve_errno(dirfd);
13032289be19SAneesh Kumar K.V     return ret;
13042289be19SAneesh Kumar K.V }
13059ed3ef26SAneesh Kumar K.V 
1306e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1307e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1308e06a765eSHarsh Prateek Bora {
1309ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
13100e5fc994SKirill A. Shutemov     int err;
1311cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1312cc720ddbSAneesh Kumar K.V 
1313e06a765eSHarsh Prateek Bora     /*
1314e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1315e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1316e06a765eSHarsh Prateek Bora      */
1317e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
13181a9978a5SKirill A. Shutemov         errno = ENOTTY;
13191a9978a5SKirill A. Shutemov         return -1;
1320e06a765eSHarsh Prateek Bora     }
1321cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1322cc720ddbSAneesh Kumar K.V     if (err < 0) {
1323cc720ddbSAneesh Kumar K.V         return err;
1324e06a765eSHarsh Prateek Bora     }
1325cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1326cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1327e06a765eSHarsh Prateek Bora     return err;
13280e5fc994SKirill A. Shutemov #else
13290e5fc994SKirill A. Shutemov     errno = ENOTTY;
13300e5fc994SKirill A. Shutemov     return -1;
13310e5fc994SKirill A. Shutemov #endif
1332e06a765eSHarsh Prateek Bora }
1333e06a765eSHarsh Prateek Bora 
13340174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
13350174fe73SAneesh Kumar K.V {
1336e06a765eSHarsh Prateek Bora     struct statfs stbuf;
13370e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13380e35a378SGreg Kurz 
13390e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13400e35a378SGreg Kurz     if (data->mountfd == -1) {
13410e35a378SGreg Kurz         goto err;
13420e35a378SGreg Kurz     }
1343e06a765eSHarsh Prateek Bora 
134400c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
134500c90bd1SGreg Kurz     /*
134600c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
134700c90bd1SGreg Kurz      */
13480e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13490e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13500e35a378SGreg Kurz         goto err;
135100c90bd1SGreg Kurz     }
135200c90bd1SGreg Kurz     switch (stbuf.f_type) {
135300c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
135400c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
135500c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
135600c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
135700c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
135800c90bd1SGreg Kurz         break;
135900c90bd1SGreg Kurz     }
136000c90bd1SGreg Kurz #endif
136100c90bd1SGreg Kurz 
13622c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
13632c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13642c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
13652c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
13662c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
13672c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
13682c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
13692c30dd74SAneesh Kumar K.V         /*
13702c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
13712c30dd74SAneesh Kumar K.V          * remain same.
13722c30dd74SAneesh Kumar K.V          */
13732c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13742c30dd74SAneesh Kumar K.V     }
1375c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
137600c90bd1SGreg Kurz 
13770e35a378SGreg Kurz     ctx->private = data;
137800c90bd1SGreg Kurz     return 0;
13790e35a378SGreg Kurz 
13800e35a378SGreg Kurz err:
13810e35a378SGreg Kurz     g_free(data);
13820e35a378SGreg Kurz     return -1;
13830e35a378SGreg Kurz }
13840e35a378SGreg Kurz 
13850e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13860e35a378SGreg Kurz {
13870e35a378SGreg Kurz     LocalData *data = ctx->private;
13880e35a378SGreg Kurz 
13890e35a378SGreg Kurz     close(data->mountfd);
13900e35a378SGreg Kurz     g_free(data);
13910174fe73SAneesh Kumar K.V }
13920174fe73SAneesh Kumar K.V 
139399519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
139499519f0aSAneesh Kumar K.V {
139599519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
139699519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
139799519f0aSAneesh Kumar K.V 
139899519f0aSAneesh Kumar K.V     if (!sec_model) {
139963325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
140063325b18SGreg Kurz         error_printf("valid options are:"
140163325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
140299519f0aSAneesh Kumar K.V         return -1;
140399519f0aSAneesh Kumar K.V     }
140499519f0aSAneesh Kumar K.V 
140599519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
140699519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
14072c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
14082c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
140999519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
141099519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
141199519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
14122c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
14132c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
141499519f0aSAneesh Kumar K.V     } else {
141563325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
141663325b18SGreg Kurz         error_printf("valid options are:"
141763325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
141899519f0aSAneesh Kumar K.V         return -1;
141999519f0aSAneesh Kumar K.V     }
142099519f0aSAneesh Kumar K.V 
142199519f0aSAneesh Kumar K.V     if (!path) {
142263325b18SGreg Kurz         error_report("fsdev: No path specified");
142399519f0aSAneesh Kumar K.V         return -1;
142499519f0aSAneesh Kumar K.V     }
142599519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
142699519f0aSAneesh Kumar K.V 
142799519f0aSAneesh Kumar K.V     return 0;
142899519f0aSAneesh Kumar K.V }
142999519f0aSAneesh Kumar K.V 
14309f107513SAnthony Liguori FileOperations local_ops = {
143199519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
14320174fe73SAneesh Kumar K.V     .init  = local_init,
14330e35a378SGreg Kurz     .cleanup = local_cleanup,
1434131dcb25SAnthony Liguori     .lstat = local_lstat,
1435131dcb25SAnthony Liguori     .readlink = local_readlink,
1436131dcb25SAnthony Liguori     .close = local_close,
1437131dcb25SAnthony Liguori     .closedir = local_closedir,
1438a6568fe2SAnthony Liguori     .open = local_open,
1439a6568fe2SAnthony Liguori     .opendir = local_opendir,
1440a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1441a9231555SAnthony Liguori     .telldir = local_telldir,
1442635324e8SGreg Kurz     .readdir = local_readdir,
1443a9231555SAnthony Liguori     .seekdir = local_seekdir,
144456d15a53SSanchit Garg     .preadv = local_preadv,
144556d15a53SSanchit Garg     .pwritev = local_pwritev,
1446c494dd6fSAnthony Liguori     .chmod = local_chmod,
1447c494dd6fSAnthony Liguori     .mknod = local_mknod,
1448c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1449c494dd6fSAnthony Liguori     .fstat = local_fstat,
1450c494dd6fSAnthony Liguori     .open2 = local_open2,
1451c494dd6fSAnthony Liguori     .symlink = local_symlink,
1452c494dd6fSAnthony Liguori     .link = local_link,
14538cf89e00SAnthony Liguori     .truncate = local_truncate,
14548cf89e00SAnthony Liguori     .rename = local_rename,
14558cf89e00SAnthony Liguori     .chown = local_chown,
145674bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
14575bae1900SAnthony Liguori     .remove = local_remove,
14588cf89e00SAnthony Liguori     .fsync = local_fsync,
1459be940c87SM. Mohan Kumar     .statfs = local_statfs,
1460fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1461fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
146210b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
14639ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
14642289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
14652289be19SAneesh Kumar K.V     .renameat  = local_renameat,
14662289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
14679f107513SAnthony Liguori };
1468