xref: /qemu/hw/9pfs/9p-local.c (revision 996a0d76d7e756e4023ef79bc37bfe629b9eaca7)
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"
16*996a0d76SGreg 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 
52*996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53*996a0d76SGreg Kurz                         mode_t mode)
54*996a0d76SGreg Kurz {
55*996a0d76SGreg Kurz     LocalData *data = fs_ctx->private;
56*996a0d76SGreg Kurz 
57*996a0d76SGreg Kurz     /* All paths are relative to the path data->mountfd points to */
58*996a0d76SGreg Kurz     while (*path == '/') {
59*996a0d76SGreg Kurz         path++;
60*996a0d76SGreg Kurz     }
61*996a0d76SGreg Kurz 
62*996a0d76SGreg Kurz     return relative_openat_nofollow(data->mountfd, path, flags, mode);
63*996a0d76SGreg Kurz }
64*996a0d76SGreg Kurz 
65*996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66*996a0d76SGreg Kurz {
67*996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68*996a0d76SGreg Kurz }
69*996a0d76SGreg Kurz 
702c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
712c30dd74SAneesh Kumar K.V 
724fa4ce71SChen Gang static char *local_mapped_attr_path(FsContext *ctx, const char *path)
732c30dd74SAneesh Kumar K.V {
741b6f85e2SMichael Tokarev     int dirlen;
751b6f85e2SMichael Tokarev     const char *name = strrchr(path, '/');
761b6f85e2SMichael Tokarev     if (name) {
771b6f85e2SMichael Tokarev         dirlen = name - path;
781b6f85e2SMichael Tokarev         ++name;
791b6f85e2SMichael Tokarev     } else {
801b6f85e2SMichael Tokarev         name = path;
811b6f85e2SMichael Tokarev         dirlen = 0;
821b6f85e2SMichael Tokarev     }
831b6f85e2SMichael Tokarev     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
841b6f85e2SMichael Tokarev                            dirlen, path, VIRTFS_META_DIR, name);
852c30dd74SAneesh Kumar K.V }
862c30dd74SAneesh Kumar K.V 
870ceb092eSAneesh Kumar K.V static FILE *local_fopen(const char *path, const char *mode)
880ceb092eSAneesh Kumar K.V {
890ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
900ceb092eSAneesh Kumar K.V     FILE *fp;
910ceb092eSAneesh Kumar K.V     int flags = O_NOFOLLOW;
920ceb092eSAneesh Kumar K.V     /*
930ceb092eSAneesh Kumar K.V      * only supports two modes
940ceb092eSAneesh Kumar K.V      */
950ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
960ceb092eSAneesh Kumar K.V         flags |= O_RDONLY;
970ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
980ceb092eSAneesh Kumar K.V         flags |= O_WRONLY | O_TRUNC | O_CREAT;
990ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1000ceb092eSAneesh Kumar K.V     } else {
1010ceb092eSAneesh Kumar K.V         return NULL;
1020ceb092eSAneesh Kumar K.V     }
1030ceb092eSAneesh Kumar K.V     fd = open(path, flags, o_mode);
1040ceb092eSAneesh Kumar K.V     if (fd == -1) {
1050ceb092eSAneesh Kumar K.V         return NULL;
1060ceb092eSAneesh Kumar K.V     }
1070ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1080ceb092eSAneesh Kumar K.V     if (!fp) {
1090ceb092eSAneesh Kumar K.V         close(fd);
1100ceb092eSAneesh Kumar K.V     }
1110ceb092eSAneesh Kumar K.V     return fp;
1120ceb092eSAneesh Kumar K.V }
1130ceb092eSAneesh Kumar K.V 
1142c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
1152c30dd74SAneesh Kumar K.V static void local_mapped_file_attr(FsContext *ctx, const char *path,
1162c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1172c30dd74SAneesh Kumar K.V {
1182c30dd74SAneesh Kumar K.V     FILE *fp;
1192c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
1204fa4ce71SChen Gang     char *attr_path;
1212c30dd74SAneesh Kumar K.V 
1224fa4ce71SChen Gang     attr_path = local_mapped_attr_path(ctx, path);
1230ceb092eSAneesh Kumar K.V     fp = local_fopen(attr_path, "r");
1244fa4ce71SChen Gang     g_free(attr_path);
1252c30dd74SAneesh Kumar K.V     if (!fp) {
1262c30dd74SAneesh Kumar K.V         return;
1272c30dd74SAneesh Kumar K.V     }
1282c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1292c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1302c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1312c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1322c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1332c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1342c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1352c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1362c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1372c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1382c30dd74SAneesh Kumar K.V         }
1392c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1402c30dd74SAneesh Kumar K.V     }
1412c30dd74SAneesh Kumar K.V     fclose(fp);
1422c30dd74SAneesh Kumar K.V }
1432c30dd74SAneesh Kumar K.V 
1442289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
145131dcb25SAnthony Liguori {
1461237ad76SVenkateswararao Jujjuri (JV)     int err;
1474fa4ce71SChen Gang     char *buffer;
1482289be19SAneesh Kumar K.V     char *path = fs_path->data;
1492289be19SAneesh Kumar K.V 
1504fa4ce71SChen Gang     buffer = rpath(fs_ctx, path);
1514fa4ce71SChen Gang     err =  lstat(buffer, stbuf);
1521237ad76SVenkateswararao Jujjuri (JV)     if (err) {
1534fa4ce71SChen Gang         goto err_out;
1541237ad76SVenkateswararao Jujjuri (JV)     }
155b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1561237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
1571237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
1581237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
1591237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
1601237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
1614fa4ce71SChen Gang         if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
162f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
1631237ad76SVenkateswararao Jujjuri (JV)         }
1644fa4ce71SChen Gang         if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
165f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
1661237ad76SVenkateswararao Jujjuri (JV)         }
1674fa4ce71SChen Gang         if (getxattr(buffer, "user.virtfs.mode",
168faa44e3dSVenkateswararao Jujjuri (JV)                     &tmp_mode, sizeof(mode_t)) > 0) {
169f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
1701237ad76SVenkateswararao Jujjuri (JV)         }
1714fa4ce71SChen Gang         if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
172f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
1731237ad76SVenkateswararao Jujjuri (JV)         }
1742c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1752c30dd74SAneesh Kumar K.V         local_mapped_file_attr(fs_ctx, path, stbuf);
1761237ad76SVenkateswararao Jujjuri (JV)     }
1774fa4ce71SChen Gang 
1784fa4ce71SChen Gang err_out:
1794fa4ce71SChen Gang     g_free(buffer);
1801237ad76SVenkateswararao Jujjuri (JV)     return err;
181131dcb25SAnthony Liguori }
182131dcb25SAnthony Liguori 
1832c30dd74SAneesh Kumar K.V static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
1842c30dd74SAneesh Kumar K.V {
1852c30dd74SAneesh Kumar K.V     int err;
1864fa4ce71SChen Gang     char *attr_dir;
187d3f8e138SMarkus Armbruster     char *tmp_path = g_strdup(path);
1882c30dd74SAneesh Kumar K.V 
1894fa4ce71SChen Gang     attr_dir = g_strdup_printf("%s/%s/%s",
1902c30dd74SAneesh Kumar K.V              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
1912c30dd74SAneesh Kumar K.V 
1922c30dd74SAneesh Kumar K.V     err = mkdir(attr_dir, 0700);
1932c30dd74SAneesh Kumar K.V     if (err < 0 && errno == EEXIST) {
1942c30dd74SAneesh Kumar K.V         err = 0;
1952c30dd74SAneesh Kumar K.V     }
1964fa4ce71SChen Gang     g_free(attr_dir);
197d3f8e138SMarkus Armbruster     g_free(tmp_path);
1982c30dd74SAneesh Kumar K.V     return err;
1992c30dd74SAneesh Kumar K.V }
2002c30dd74SAneesh Kumar K.V 
2012c30dd74SAneesh Kumar K.V static int local_set_mapped_file_attr(FsContext *ctx,
2022c30dd74SAneesh Kumar K.V                                       const char *path, FsCred *credp)
2032c30dd74SAneesh Kumar K.V {
2042c30dd74SAneesh Kumar K.V     FILE *fp;
2052c30dd74SAneesh Kumar K.V     int ret = 0;
2062c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2074fa4ce71SChen Gang     char *attr_path;
2082c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
2092c30dd74SAneesh Kumar K.V 
2104fa4ce71SChen Gang     attr_path = local_mapped_attr_path(ctx, path);
2114fa4ce71SChen Gang     fp = local_fopen(attr_path, "r");
2122c30dd74SAneesh Kumar K.V     if (!fp) {
2132c30dd74SAneesh Kumar K.V         goto create_map_file;
2142c30dd74SAneesh Kumar K.V     }
2152c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2162c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2172c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2182c30dd74SAneesh Kumar K.V             uid = atoi(buf+11);
2192c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2202c30dd74SAneesh Kumar K.V             gid = atoi(buf+11);
2212c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2222c30dd74SAneesh Kumar K.V             mode = atoi(buf+12);
2232c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2242c30dd74SAneesh Kumar K.V             rdev = atoi(buf+12);
2252c30dd74SAneesh Kumar K.V         }
2262c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2272c30dd74SAneesh Kumar K.V     }
2282c30dd74SAneesh Kumar K.V     fclose(fp);
2292c30dd74SAneesh Kumar K.V     goto update_map_file;
2302c30dd74SAneesh Kumar K.V 
2312c30dd74SAneesh Kumar K.V create_map_file:
2322c30dd74SAneesh Kumar K.V     ret = local_create_mapped_attr_dir(ctx, path);
2332c30dd74SAneesh Kumar K.V     if (ret < 0) {
2342c30dd74SAneesh Kumar K.V         goto err_out;
2352c30dd74SAneesh Kumar K.V     }
2362c30dd74SAneesh Kumar K.V 
2372c30dd74SAneesh Kumar K.V update_map_file:
2380ceb092eSAneesh Kumar K.V     fp = local_fopen(attr_path, "w");
2392c30dd74SAneesh Kumar K.V     if (!fp) {
2402c30dd74SAneesh Kumar K.V         ret = -1;
2412c30dd74SAneesh Kumar K.V         goto err_out;
2422c30dd74SAneesh Kumar K.V     }
2432c30dd74SAneesh Kumar K.V 
2442c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
2452c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
2462c30dd74SAneesh Kumar K.V     }
2472c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
2482c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
2492c30dd74SAneesh Kumar K.V     }
2502c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
2512c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
2522c30dd74SAneesh Kumar K.V     }
2532c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
2542c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
2552c30dd74SAneesh Kumar K.V     }
2562c30dd74SAneesh Kumar K.V 
2572c30dd74SAneesh Kumar K.V 
2582c30dd74SAneesh Kumar K.V     if (uid != -1) {
2592c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
2602c30dd74SAneesh Kumar K.V     }
2612c30dd74SAneesh Kumar K.V     if (gid != -1) {
2622c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
2632c30dd74SAneesh Kumar K.V     }
2642c30dd74SAneesh Kumar K.V     if (mode != -1) {
2652c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
2662c30dd74SAneesh Kumar K.V     }
2672c30dd74SAneesh Kumar K.V     if (rdev != -1) {
2682c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
2692c30dd74SAneesh Kumar K.V     }
2702c30dd74SAneesh Kumar K.V     fclose(fp);
2712c30dd74SAneesh Kumar K.V 
2722c30dd74SAneesh Kumar K.V err_out:
2734fa4ce71SChen Gang     g_free(attr_path);
2742c30dd74SAneesh Kumar K.V     return ret;
2752c30dd74SAneesh Kumar K.V }
2762c30dd74SAneesh Kumar K.V 
277758e8e38SVenkateswararao Jujjuri (JV) static int local_set_xattr(const char *path, FsCred *credp)
278131dcb25SAnthony Liguori {
279758e8e38SVenkateswararao Jujjuri (JV)     int err;
2802289be19SAneesh Kumar K.V 
281758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
282f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
283f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
284758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
285758e8e38SVenkateswararao Jujjuri (JV)             return err;
286131dcb25SAnthony Liguori         }
287131dcb25SAnthony Liguori     }
288758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
289f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
290f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
291758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
292758e8e38SVenkateswararao Jujjuri (JV)             return err;
293131dcb25SAnthony Liguori         }
294131dcb25SAnthony Liguori     }
295758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
296f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
297f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
298758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
299758e8e38SVenkateswararao Jujjuri (JV)             return err;
300131dcb25SAnthony Liguori         }
301131dcb25SAnthony Liguori     }
302758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
303f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
304f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
305758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
306758e8e38SVenkateswararao Jujjuri (JV)             return err;
307131dcb25SAnthony Liguori         }
308758e8e38SVenkateswararao Jujjuri (JV)     }
309131dcb25SAnthony Liguori     return 0;
310131dcb25SAnthony Liguori }
311131dcb25SAnthony Liguori 
3124750a96fSVenkateswararao Jujjuri (JV) static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
3134750a96fSVenkateswararao Jujjuri (JV)                                          FsCred *credp)
3144750a96fSVenkateswararao Jujjuri (JV) {
3154fa4ce71SChen Gang     char *buffer;
3162289be19SAneesh Kumar K.V 
3174fa4ce71SChen Gang     buffer = rpath(fs_ctx, path);
3184fa4ce71SChen Gang     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
31912848bfcSAneesh Kumar K.V         /*
32012848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
32112848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
32212848bfcSAneesh Kumar K.V          */
323b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
3244fa4ce71SChen Gang             goto err;
3254750a96fSVenkateswararao Jujjuri (JV)         }
32612848bfcSAneesh Kumar K.V     }
3272d40564aSM. Mohan Kumar 
3284fa4ce71SChen Gang     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
3294fa4ce71SChen Gang         goto err;
3302d40564aSM. Mohan Kumar     }
3314fa4ce71SChen Gang 
3324fa4ce71SChen Gang     g_free(buffer);
3334750a96fSVenkateswararao Jujjuri (JV)     return 0;
3344fa4ce71SChen Gang err:
3354fa4ce71SChen Gang     g_free(buffer);
3364fa4ce71SChen Gang     return -1;
3374750a96fSVenkateswararao Jujjuri (JV) }
3384750a96fSVenkateswararao Jujjuri (JV) 
3392289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
340131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
341131dcb25SAnthony Liguori {
342879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
3434fa4ce71SChen Gang     char *buffer;
3442289be19SAneesh Kumar K.V     char *path = fs_path->data;
3452289be19SAneesh Kumar K.V 
3462c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
3472c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
348879c2813SVenkateswararao Jujjuri (JV)         int fd;
3494fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
3504fa4ce71SChen Gang         fd = open(buffer, O_RDONLY | O_NOFOLLOW);
3514fa4ce71SChen Gang         g_free(buffer);
352879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
353879c2813SVenkateswararao Jujjuri (JV)             return -1;
354879c2813SVenkateswararao Jujjuri (JV)         }
355879c2813SVenkateswararao Jujjuri (JV)         do {
356879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
357879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
358879c2813SVenkateswararao Jujjuri (JV)         close(fd);
359b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
360b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
3614fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
3624fa4ce71SChen Gang         tsize = readlink(buffer, buf, bufsz);
3634fa4ce71SChen Gang         g_free(buffer);
364879c2813SVenkateswararao Jujjuri (JV)     }
365879c2813SVenkateswararao Jujjuri (JV)     return tsize;
366131dcb25SAnthony Liguori }
367131dcb25SAnthony Liguori 
368cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
369131dcb25SAnthony Liguori {
370cc720ddbSAneesh Kumar K.V     return close(fs->fd);
371131dcb25SAnthony Liguori }
372131dcb25SAnthony Liguori 
373cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
374131dcb25SAnthony Liguori {
375f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
376131dcb25SAnthony Liguori }
3779f107513SAnthony Liguori 
378cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
379cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
380a6568fe2SAnthony Liguori {
38121328e1eSGreg Kurz     int fd;
3822289be19SAneesh Kumar K.V 
383*996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
38421328e1eSGreg Kurz     if (fd == -1) {
38521328e1eSGreg Kurz         return -1;
38621328e1eSGreg Kurz     }
38721328e1eSGreg Kurz     fs->fd = fd;
388cc720ddbSAneesh Kumar K.V     return fs->fd;
389a6568fe2SAnthony Liguori }
390a6568fe2SAnthony Liguori 
391cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
392cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
393a6568fe2SAnthony Liguori {
394*996a0d76SGreg Kurz     int dirfd;
39521328e1eSGreg Kurz     DIR *stream;
3962289be19SAneesh Kumar K.V 
397*996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
398*996a0d76SGreg Kurz     if (dirfd == -1) {
399*996a0d76SGreg Kurz         return -1;
400*996a0d76SGreg Kurz     }
401*996a0d76SGreg Kurz 
402*996a0d76SGreg Kurz     stream = fdopendir(dirfd);
40321328e1eSGreg Kurz     if (!stream) {
404cc720ddbSAneesh Kumar K.V         return -1;
405cc720ddbSAneesh Kumar K.V     }
40621328e1eSGreg Kurz     fs->dir.stream = stream;
407cc720ddbSAneesh Kumar K.V     return 0;
408a6568fe2SAnthony Liguori }
409a6568fe2SAnthony Liguori 
410cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
411a9231555SAnthony Liguori {
412f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
413a9231555SAnthony Liguori }
414a9231555SAnthony Liguori 
415cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
416a9231555SAnthony Liguori {
417f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
418a9231555SAnthony Liguori }
419a9231555SAnthony Liguori 
420635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
421a9231555SAnthony Liguori {
422635324e8SGreg Kurz     struct dirent *entry;
4232c30dd74SAneesh Kumar K.V 
4242c30dd74SAneesh Kumar K.V again:
425635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
426635324e8SGreg Kurz     if (!entry) {
427635324e8SGreg Kurz         return NULL;
428635324e8SGreg Kurz     }
429635324e8SGreg Kurz 
430840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
431840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
432840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
433635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
4342c30dd74SAneesh Kumar K.V             /* skp the meta data directory */
4352c30dd74SAneesh Kumar K.V             goto again;
4362c30dd74SAneesh Kumar K.V         }
437840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
4382c30dd74SAneesh Kumar K.V     }
439635324e8SGreg Kurz 
440635324e8SGreg Kurz     return entry;
441a9231555SAnthony Liguori }
442a9231555SAnthony Liguori 
443cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
444a9231555SAnthony Liguori {
445f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
446a9231555SAnthony Liguori }
447a9231555SAnthony Liguori 
448cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
449cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
45056d15a53SSanchit Garg                             int iovcnt, off_t offset)
451a9231555SAnthony Liguori {
45256d15a53SSanchit Garg #ifdef CONFIG_PREADV
453cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
45456d15a53SSanchit Garg #else
455cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
45656d15a53SSanchit Garg     if (err == -1) {
45756d15a53SSanchit Garg         return err;
45856d15a53SSanchit Garg     } else {
459cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
460a9231555SAnthony Liguori     }
46156d15a53SSanchit Garg #endif
462a9231555SAnthony Liguori }
463a9231555SAnthony Liguori 
464cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
465cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
46656d15a53SSanchit Garg                              int iovcnt, off_t offset)
4678449360cSAnthony Liguori {
4686fe76accSGreg Kurz     ssize_t ret;
46956d15a53SSanchit Garg #ifdef CONFIG_PREADV
470cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
47156d15a53SSanchit Garg #else
472cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
47356d15a53SSanchit Garg     if (err == -1) {
47456d15a53SSanchit Garg         return err;
47556d15a53SSanchit Garg     } else {
476cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
4778449360cSAnthony Liguori     }
47856d15a53SSanchit Garg #endif
479d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
480d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
481d3ab98e6SAneesh Kumar K.V         /*
482d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
483d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
484d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
485d3ab98e6SAneesh Kumar K.V          */
486cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
487d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
488d3ab98e6SAneesh Kumar K.V     }
489d3ab98e6SAneesh Kumar K.V #endif
490d3ab98e6SAneesh Kumar K.V     return ret;
49156d15a53SSanchit Garg }
4928449360cSAnthony Liguori 
4932289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
494c494dd6fSAnthony Liguori {
4954fa4ce71SChen Gang     char *buffer;
4964fa4ce71SChen Gang     int ret = -1;
4972289be19SAneesh Kumar K.V     char *path = fs_path->data;
4982289be19SAneesh Kumar K.V 
499b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5004fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5014fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
5024fa4ce71SChen Gang         g_free(buffer);
5032c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5042c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
505b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
506b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
5074fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5084fa4ce71SChen Gang         ret = chmod(buffer, credp->fc_mode);
5094fa4ce71SChen Gang         g_free(buffer);
510e95ead32SVenkateswararao Jujjuri (JV)     }
5114fa4ce71SChen Gang     return ret;
512c494dd6fSAnthony Liguori }
513c494dd6fSAnthony Liguori 
5142289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
5152289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
516c494dd6fSAnthony Liguori {
5172289be19SAneesh Kumar K.V     char *path;
5181c293312SVenkateswararao Jujjuri (JV)     int err = -1;
5191c293312SVenkateswararao Jujjuri (JV)     int serrno = 0;
5202289be19SAneesh Kumar K.V     V9fsString fullname;
5214ed7b2c3SStefan Weil     char *buffer = NULL;
5221c293312SVenkateswararao Jujjuri (JV) 
5232289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
5242289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
5252289be19SAneesh Kumar K.V     path = fullname.data;
5262289be19SAneesh Kumar K.V 
5271c293312SVenkateswararao Jujjuri (JV)     /* Determine the security model */
528b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5294fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5304fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
5311c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5322289be19SAneesh Kumar K.V             goto out;
5331c293312SVenkateswararao Jujjuri (JV)         }
5344fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
5351c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5361c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
5371c293312SVenkateswararao Jujjuri (JV)             goto err_end;
5381c293312SVenkateswararao Jujjuri (JV)         }
5392c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
5402c30dd74SAneesh Kumar K.V 
5414fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5424fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
5432c30dd74SAneesh Kumar K.V         if (err == -1) {
5442c30dd74SAneesh Kumar K.V             goto out;
5452c30dd74SAneesh Kumar K.V         }
5462c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
5472c30dd74SAneesh Kumar K.V         if (err == -1) {
5482c30dd74SAneesh Kumar K.V             serrno = errno;
5492c30dd74SAneesh Kumar K.V             goto err_end;
5502c30dd74SAneesh Kumar K.V         }
551b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
552b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
5534fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5544fa4ce71SChen Gang         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
5551c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5562289be19SAneesh Kumar K.V             goto out;
5571c293312SVenkateswararao Jujjuri (JV)         }
5581c293312SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
5591c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5601c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
5611c293312SVenkateswararao Jujjuri (JV)             goto err_end;
5621c293312SVenkateswararao Jujjuri (JV)         }
5631c293312SVenkateswararao Jujjuri (JV)     }
5642289be19SAneesh Kumar K.V     goto out;
5651c293312SVenkateswararao Jujjuri (JV) 
5661c293312SVenkateswararao Jujjuri (JV) err_end:
5674fa4ce71SChen Gang     remove(buffer);
5681c293312SVenkateswararao Jujjuri (JV)     errno = serrno;
5692289be19SAneesh Kumar K.V out:
5704ed7b2c3SStefan Weil     g_free(buffer);
5712289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
5721c293312SVenkateswararao Jujjuri (JV)     return err;
573c494dd6fSAnthony Liguori }
574c494dd6fSAnthony Liguori 
5752289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
5762289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
577c494dd6fSAnthony Liguori {
5782289be19SAneesh Kumar K.V     char *path;
57900ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
58000ec5c37SVenkateswararao Jujjuri (JV)     int serrno = 0;
5812289be19SAneesh Kumar K.V     V9fsString fullname;
5824ed7b2c3SStefan Weil     char *buffer = NULL;
58300ec5c37SVenkateswararao Jujjuri (JV) 
5842289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
5852289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
5862289be19SAneesh Kumar K.V     path = fullname.data;
5872289be19SAneesh Kumar K.V 
58800ec5c37SVenkateswararao Jujjuri (JV)     /* Determine the security model */
589b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
5904fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
5914fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
59200ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
5932289be19SAneesh Kumar K.V             goto out;
59400ec5c37SVenkateswararao Jujjuri (JV)         }
59500ec5c37SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFDIR;
5964fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
59700ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
59800ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
59900ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
60000ec5c37SVenkateswararao Jujjuri (JV)         }
6012c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6024fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6034fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
6042c30dd74SAneesh Kumar K.V         if (err == -1) {
6052c30dd74SAneesh Kumar K.V             goto out;
6062c30dd74SAneesh Kumar K.V         }
6072c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFDIR;
6082c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
6092c30dd74SAneesh Kumar K.V         if (err == -1) {
6102c30dd74SAneesh Kumar K.V             serrno = errno;
6112c30dd74SAneesh Kumar K.V             goto err_end;
6122c30dd74SAneesh Kumar K.V         }
613b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
614b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
6154fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
6164fa4ce71SChen Gang         err = mkdir(buffer, credp->fc_mode);
61700ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
6182289be19SAneesh Kumar K.V             goto out;
61900ec5c37SVenkateswararao Jujjuri (JV)         }
62000ec5c37SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
62100ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
62200ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
62300ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
62400ec5c37SVenkateswararao Jujjuri (JV)         }
62500ec5c37SVenkateswararao Jujjuri (JV)     }
6262289be19SAneesh Kumar K.V     goto out;
62700ec5c37SVenkateswararao Jujjuri (JV) 
62800ec5c37SVenkateswararao Jujjuri (JV) err_end:
6294fa4ce71SChen Gang     remove(buffer);
63000ec5c37SVenkateswararao Jujjuri (JV)     errno = serrno;
6312289be19SAneesh Kumar K.V out:
6324ed7b2c3SStefan Weil     g_free(buffer);
6332289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
63400ec5c37SVenkateswararao Jujjuri (JV)     return err;
635c494dd6fSAnthony Liguori }
636c494dd6fSAnthony Liguori 
6378b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
638cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
639c494dd6fSAnthony Liguori {
6408b888272SAneesh Kumar K.V     int err, fd;
6418b888272SAneesh Kumar K.V 
6428b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
643f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
6448b888272SAneesh Kumar K.V     } else {
6458b888272SAneesh Kumar K.V         fd = fs->fd;
6468b888272SAneesh Kumar K.V     }
6478b888272SAneesh Kumar K.V 
6488b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
6491237ad76SVenkateswararao Jujjuri (JV)     if (err) {
6501237ad76SVenkateswararao Jujjuri (JV)         return err;
6511237ad76SVenkateswararao Jujjuri (JV)     }
652b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6531237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
6541237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
6551237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
6561237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
6571237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
6581237ad76SVenkateswararao Jujjuri (JV) 
659f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
660f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
6611237ad76SVenkateswararao Jujjuri (JV)         }
662f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
663f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
6641237ad76SVenkateswararao Jujjuri (JV)         }
665f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
666f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
6671237ad76SVenkateswararao Jujjuri (JV)         }
668f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
669f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
6701237ad76SVenkateswararao Jujjuri (JV)         }
6712c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6722c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
6732c30dd74SAneesh Kumar K.V         return -1;
6741237ad76SVenkateswararao Jujjuri (JV)     }
6751237ad76SVenkateswararao Jujjuri (JV)     return err;
676c494dd6fSAnthony Liguori }
677c494dd6fSAnthony Liguori 
6782289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
679cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
680c494dd6fSAnthony Liguori {
6812289be19SAneesh Kumar K.V     char *path;
6824750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
6834750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
6844750a96fSVenkateswararao Jujjuri (JV)     int serrno = 0;
6852289be19SAneesh Kumar K.V     V9fsString fullname;
6864ed7b2c3SStefan Weil     char *buffer = NULL;
6874750a96fSVenkateswararao Jujjuri (JV) 
6880ceb092eSAneesh Kumar K.V     /*
6890ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
6900ceb092eSAneesh Kumar K.V      */
6910ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
6920ceb092eSAneesh Kumar K.V 
6932289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
6942289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
6952289be19SAneesh Kumar K.V     path = fullname.data;
6962289be19SAneesh Kumar K.V 
6974750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
698b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6994fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7004fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7014750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7022289be19SAneesh Kumar K.V             err = fd;
7032289be19SAneesh Kumar K.V             goto out;
7044750a96fSVenkateswararao Jujjuri (JV)         }
7054750a96fSVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFREG;
7064750a96fSVenkateswararao Jujjuri (JV)         /* Set cleint credentials in xattr */
7074fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
7084750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7094750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
7104750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7114750a96fSVenkateswararao Jujjuri (JV)         }
7122c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7134fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7144fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
7152c30dd74SAneesh Kumar K.V         if (fd == -1) {
7162c30dd74SAneesh Kumar K.V             err = fd;
7172c30dd74SAneesh Kumar K.V             goto out;
7182c30dd74SAneesh Kumar K.V         }
7192c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFREG;
7202c30dd74SAneesh Kumar K.V         /* Set client credentials in .virtfs_metadata directory files */
7212c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
7222c30dd74SAneesh Kumar K.V         if (err == -1) {
7232c30dd74SAneesh Kumar K.V             serrno = errno;
7242c30dd74SAneesh Kumar K.V             goto err_end;
7252c30dd74SAneesh Kumar K.V         }
726b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
727b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
7284fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7294fa4ce71SChen Gang         fd = open(buffer, flags, credp->fc_mode);
7304750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7312289be19SAneesh Kumar K.V             err = fd;
7322289be19SAneesh Kumar K.V             goto out;
7334750a96fSVenkateswararao Jujjuri (JV)         }
7344750a96fSVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
7354750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7364750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
7374750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7384750a96fSVenkateswararao Jujjuri (JV)         }
7394750a96fSVenkateswararao Jujjuri (JV)     }
7402289be19SAneesh Kumar K.V     err = fd;
741cc720ddbSAneesh Kumar K.V     fs->fd = fd;
7422289be19SAneesh Kumar K.V     goto out;
7434750a96fSVenkateswararao Jujjuri (JV) 
7444750a96fSVenkateswararao Jujjuri (JV) err_end:
7454750a96fSVenkateswararao Jujjuri (JV)     close(fd);
7464fa4ce71SChen Gang     remove(buffer);
7474750a96fSVenkateswararao Jujjuri (JV)     errno = serrno;
7482289be19SAneesh Kumar K.V out:
7494ed7b2c3SStefan Weil     g_free(buffer);
7502289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
7514750a96fSVenkateswararao Jujjuri (JV)     return err;
752c494dd6fSAnthony Liguori }
753c494dd6fSAnthony Liguori 
754758e8e38SVenkateswararao Jujjuri (JV) 
755879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
7562289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
757c494dd6fSAnthony Liguori {
758879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
759879c2813SVenkateswararao Jujjuri (JV)     int serrno = 0;
7602289be19SAneesh Kumar K.V     char *newpath;
7612289be19SAneesh Kumar K.V     V9fsString fullname;
7624ed7b2c3SStefan Weil     char *buffer = NULL;
763879c2813SVenkateswararao Jujjuri (JV) 
7642289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
7652289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
7662289be19SAneesh Kumar K.V     newpath = fullname.data;
7672289be19SAneesh Kumar K.V 
768879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
769b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
770879c2813SVenkateswararao Jujjuri (JV)         int fd;
771879c2813SVenkateswararao Jujjuri (JV)         ssize_t oldpath_size, write_size;
7724fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
7734fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
774879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
7752289be19SAneesh Kumar K.V             err = fd;
7762289be19SAneesh Kumar K.V             goto out;
777879c2813SVenkateswararao Jujjuri (JV)         }
778879c2813SVenkateswararao Jujjuri (JV)         /* Write the oldpath (target) to the file. */
779f35bde2fSHarsh Prateek Bora         oldpath_size = strlen(oldpath);
780879c2813SVenkateswararao Jujjuri (JV)         do {
781879c2813SVenkateswararao Jujjuri (JV)             write_size = write(fd, (void *)oldpath, oldpath_size);
782879c2813SVenkateswararao Jujjuri (JV)         } while (write_size == -1 && errno == EINTR);
783879c2813SVenkateswararao Jujjuri (JV) 
784879c2813SVenkateswararao Jujjuri (JV)         if (write_size != oldpath_size) {
785879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
786879c2813SVenkateswararao Jujjuri (JV)             close(fd);
787879c2813SVenkateswararao Jujjuri (JV)             err = -1;
788879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
789879c2813SVenkateswararao Jujjuri (JV)         }
790879c2813SVenkateswararao Jujjuri (JV)         close(fd);
791879c2813SVenkateswararao Jujjuri (JV)         /* Set cleint credentials in symlink's xattr */
792879c2813SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFLNK;
7934fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
794879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
795879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
796879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
797879c2813SVenkateswararao Jujjuri (JV)         }
7982c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7992c30dd74SAneesh Kumar K.V         int fd;
8002c30dd74SAneesh Kumar K.V         ssize_t oldpath_size, write_size;
8014fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8024fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
8032c30dd74SAneesh Kumar K.V         if (fd == -1) {
8042c30dd74SAneesh Kumar K.V             err = fd;
8052c30dd74SAneesh Kumar K.V             goto out;
8062c30dd74SAneesh Kumar K.V         }
8072c30dd74SAneesh Kumar K.V         /* Write the oldpath (target) to the file. */
8082c30dd74SAneesh Kumar K.V         oldpath_size = strlen(oldpath);
8092c30dd74SAneesh Kumar K.V         do {
8102c30dd74SAneesh Kumar K.V             write_size = write(fd, (void *)oldpath, oldpath_size);
8112c30dd74SAneesh Kumar K.V         } while (write_size == -1 && errno == EINTR);
8122c30dd74SAneesh Kumar K.V 
8132c30dd74SAneesh Kumar K.V         if (write_size != oldpath_size) {
8142c30dd74SAneesh Kumar K.V             serrno = errno;
8152c30dd74SAneesh Kumar K.V             close(fd);
8162c30dd74SAneesh Kumar K.V             err = -1;
8172c30dd74SAneesh Kumar K.V             goto err_end;
8182c30dd74SAneesh Kumar K.V         }
8192c30dd74SAneesh Kumar K.V         close(fd);
8202c30dd74SAneesh Kumar K.V         /* Set cleint credentials in symlink's xattr */
8212c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFLNK;
8222c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
8232c30dd74SAneesh Kumar K.V         if (err == -1) {
8242c30dd74SAneesh Kumar K.V             serrno = errno;
8252c30dd74SAneesh Kumar K.V             goto err_end;
8262c30dd74SAneesh Kumar K.V         }
827b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
828b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
8294fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
8304fa4ce71SChen Gang         err = symlink(oldpath, buffer);
831879c2813SVenkateswararao Jujjuri (JV)         if (err) {
8322289be19SAneesh Kumar K.V             goto out;
833879c2813SVenkateswararao Jujjuri (JV)         }
8344fa4ce71SChen Gang         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
835879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
83612848bfcSAneesh Kumar K.V             /*
83712848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
83812848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
83912848bfcSAneesh Kumar K.V              */
840b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
841879c2813SVenkateswararao Jujjuri (JV)                 serrno = errno;
842879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
84312848bfcSAneesh Kumar K.V             } else
84412848bfcSAneesh Kumar K.V                 err = 0;
845879c2813SVenkateswararao Jujjuri (JV)         }
846879c2813SVenkateswararao Jujjuri (JV)     }
8472289be19SAneesh Kumar K.V     goto out;
848879c2813SVenkateswararao Jujjuri (JV) 
849879c2813SVenkateswararao Jujjuri (JV) err_end:
8504fa4ce71SChen Gang     remove(buffer);
851879c2813SVenkateswararao Jujjuri (JV)     errno = serrno;
8522289be19SAneesh Kumar K.V out:
8534ed7b2c3SStefan Weil     g_free(buffer);
8542289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
855879c2813SVenkateswararao Jujjuri (JV)     return err;
856c494dd6fSAnthony Liguori }
857c494dd6fSAnthony Liguori 
8582289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
8592289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
860c494dd6fSAnthony Liguori {
8612289be19SAneesh Kumar K.V     int ret;
8622289be19SAneesh Kumar K.V     V9fsString newpath;
8634fa4ce71SChen Gang     char *buffer, *buffer1;
864c494dd6fSAnthony Liguori 
8652289be19SAneesh Kumar K.V     v9fs_string_init(&newpath);
8662289be19SAneesh Kumar K.V     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
8672289be19SAneesh Kumar K.V 
8684fa4ce71SChen Gang     buffer = rpath(ctx, oldpath->data);
8694fa4ce71SChen Gang     buffer1 = rpath(ctx, newpath.data);
8704fa4ce71SChen Gang     ret = link(buffer, buffer1);
8714fa4ce71SChen Gang     g_free(buffer);
8724fa4ce71SChen Gang     g_free(buffer1);
8732c30dd74SAneesh Kumar K.V 
8742c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
8752c30dd74SAneesh Kumar K.V     if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
8762c30dd74SAneesh Kumar K.V         /* Link the .virtfs_metadata files. Create the metada directory */
8772c30dd74SAneesh Kumar K.V         ret = local_create_mapped_attr_dir(ctx, newpath.data);
8782c30dd74SAneesh Kumar K.V         if (ret < 0) {
8792c30dd74SAneesh Kumar K.V             goto err_out;
8802c30dd74SAneesh Kumar K.V         }
8814fa4ce71SChen Gang         buffer = local_mapped_attr_path(ctx, oldpath->data);
8824fa4ce71SChen Gang         buffer1 = local_mapped_attr_path(ctx, newpath.data);
8834fa4ce71SChen Gang         ret = link(buffer, buffer1);
8844fa4ce71SChen Gang         g_free(buffer);
8854fa4ce71SChen Gang         g_free(buffer1);
8862c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
8872c30dd74SAneesh Kumar K.V             goto err_out;
8882c30dd74SAneesh Kumar K.V         }
8892c30dd74SAneesh Kumar K.V     }
8902c30dd74SAneesh Kumar K.V err_out:
8912289be19SAneesh Kumar K.V     v9fs_string_free(&newpath);
8922289be19SAneesh Kumar K.V     return ret;
893c494dd6fSAnthony Liguori }
894c494dd6fSAnthony Liguori 
8952289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
8968cf89e00SAnthony Liguori {
8974fa4ce71SChen Gang     char *buffer;
8984fa4ce71SChen Gang     int ret;
8992289be19SAneesh Kumar K.V     char *path = fs_path->data;
9002289be19SAneesh Kumar K.V 
9014fa4ce71SChen Gang     buffer = rpath(ctx, path);
9024fa4ce71SChen Gang     ret = truncate(buffer, size);
9034fa4ce71SChen Gang     g_free(buffer);
9044fa4ce71SChen Gang     return ret;
9058cf89e00SAnthony Liguori }
9068cf89e00SAnthony Liguori 
9078cf89e00SAnthony Liguori static int local_rename(FsContext *ctx, const char *oldpath,
9088cf89e00SAnthony Liguori                         const char *newpath)
9098cf89e00SAnthony Liguori {
9102c30dd74SAneesh Kumar K.V     int err;
9114fa4ce71SChen Gang     char *buffer, *buffer1;
9128cf89e00SAnthony Liguori 
9132c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9142c30dd74SAneesh Kumar K.V         err = local_create_mapped_attr_dir(ctx, newpath);
9152c30dd74SAneesh Kumar K.V         if (err < 0) {
9162c30dd74SAneesh Kumar K.V             return err;
9172c30dd74SAneesh Kumar K.V         }
9182c30dd74SAneesh Kumar K.V         /* rename the .virtfs_metadata files */
9194fa4ce71SChen Gang         buffer = local_mapped_attr_path(ctx, oldpath);
9204fa4ce71SChen Gang         buffer1 = local_mapped_attr_path(ctx, newpath);
9214fa4ce71SChen Gang         err = rename(buffer, buffer1);
9224fa4ce71SChen Gang         g_free(buffer);
9234fa4ce71SChen Gang         g_free(buffer1);
9242c30dd74SAneesh Kumar K.V         if (err < 0 && errno != ENOENT) {
9252c30dd74SAneesh Kumar K.V             return err;
9262c30dd74SAneesh Kumar K.V         }
9272c30dd74SAneesh Kumar K.V     }
9284fa4ce71SChen Gang 
9294fa4ce71SChen Gang     buffer = rpath(ctx, oldpath);
9304fa4ce71SChen Gang     buffer1 = rpath(ctx, newpath);
9314fa4ce71SChen Gang     err = rename(buffer, buffer1);
9324fa4ce71SChen Gang     g_free(buffer);
9334fa4ce71SChen Gang     g_free(buffer1);
9344fa4ce71SChen Gang     return err;
9358cf89e00SAnthony Liguori }
9368cf89e00SAnthony Liguori 
9372289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
9388cf89e00SAnthony Liguori {
9394fa4ce71SChen Gang     char *buffer;
9404fa4ce71SChen Gang     int ret = -1;
9412289be19SAneesh Kumar K.V     char *path = fs_path->data;
9422289be19SAneesh Kumar K.V 
943c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
94417b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
94517b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
9464fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9474fa4ce71SChen Gang         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
9484fa4ce71SChen Gang         g_free(buffer);
949b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
9504fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9514fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
9524fa4ce71SChen Gang         g_free(buffer);
9532c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9542c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
955f7613beeSVenkateswararao Jujjuri (JV)     }
9564fa4ce71SChen Gang     return ret;
9578cf89e00SAnthony Liguori }
9588cf89e00SAnthony Liguori 
9592289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
96074bc02b2SM. Mohan Kumar                            const struct timespec *buf)
9618cf89e00SAnthony Liguori {
9624fa4ce71SChen Gang     char *buffer;
9634fa4ce71SChen Gang     int ret;
9642289be19SAneesh Kumar K.V     char *path = fs_path->data;
9652289be19SAneesh Kumar K.V 
9664fa4ce71SChen Gang     buffer = rpath(s, path);
9674fa4ce71SChen Gang     ret = qemu_utimens(buffer, buf);
9684fa4ce71SChen Gang     g_free(buffer);
9694fa4ce71SChen Gang     return ret;
9708cf89e00SAnthony Liguori }
9718cf89e00SAnthony Liguori 
9725bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
9735bae1900SAnthony Liguori {
9742c30dd74SAneesh Kumar K.V     int err;
9752c30dd74SAneesh Kumar K.V     struct stat stbuf;
9764fa4ce71SChen Gang     char *buffer;
9772c30dd74SAneesh Kumar K.V 
9782c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9794fa4ce71SChen Gang         buffer = rpath(ctx, path);
9804fa4ce71SChen Gang         err =  lstat(buffer, &stbuf);
9814fa4ce71SChen Gang         g_free(buffer);
9822c30dd74SAneesh Kumar K.V         if (err) {
9832c30dd74SAneesh Kumar K.V             goto err_out;
9842c30dd74SAneesh Kumar K.V         }
9852c30dd74SAneesh Kumar K.V         /*
9862c30dd74SAneesh Kumar K.V          * If directory remove .virtfs_metadata contained in the
9872c30dd74SAneesh Kumar K.V          * directory
9882c30dd74SAneesh Kumar K.V          */
9892c30dd74SAneesh Kumar K.V         if (S_ISDIR(stbuf.st_mode)) {
9904fa4ce71SChen Gang             buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
9914fa4ce71SChen Gang                                      path, VIRTFS_META_DIR);
9922c30dd74SAneesh Kumar K.V             err = remove(buffer);
9934fa4ce71SChen Gang             g_free(buffer);
9942c30dd74SAneesh Kumar K.V             if (err < 0 && errno != ENOENT) {
9952c30dd74SAneesh Kumar K.V                 /*
9962c30dd74SAneesh Kumar K.V                  * We didn't had the .virtfs_metadata file. May be file created
9972c30dd74SAneesh Kumar K.V                  * in non-mapped mode ?. Ignore ENOENT.
9982c30dd74SAneesh Kumar K.V                  */
9992c30dd74SAneesh Kumar K.V                 goto err_out;
10002c30dd74SAneesh Kumar K.V             }
10012c30dd74SAneesh Kumar K.V         }
10022c30dd74SAneesh Kumar K.V         /*
10032c30dd74SAneesh Kumar K.V          * Now remove the name from parent directory
10042c30dd74SAneesh Kumar K.V          * .virtfs_metadata directory
10052c30dd74SAneesh Kumar K.V          */
10064fa4ce71SChen Gang         buffer = local_mapped_attr_path(ctx, path);
10074fa4ce71SChen Gang         err = remove(buffer);
10084fa4ce71SChen Gang         g_free(buffer);
10092c30dd74SAneesh Kumar K.V         if (err < 0 && errno != ENOENT) {
10102c30dd74SAneesh Kumar K.V             /*
10112c30dd74SAneesh Kumar K.V              * We didn't had the .virtfs_metadata file. May be file created
10122c30dd74SAneesh Kumar K.V              * in non-mapped mode ?. Ignore ENOENT.
10132c30dd74SAneesh Kumar K.V              */
10142c30dd74SAneesh Kumar K.V             goto err_out;
10152c30dd74SAneesh Kumar K.V         }
10162c30dd74SAneesh Kumar K.V     }
10174fa4ce71SChen Gang 
10184fa4ce71SChen Gang     buffer = rpath(ctx, path);
10194fa4ce71SChen Gang     err = remove(buffer);
10204fa4ce71SChen Gang     g_free(buffer);
10212c30dd74SAneesh Kumar K.V err_out:
10222c30dd74SAneesh Kumar K.V     return err;
10235bae1900SAnthony Liguori }
10245bae1900SAnthony Liguori 
10258b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
10268b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
10278cf89e00SAnthony Liguori {
10288b888272SAneesh Kumar K.V     int fd;
10298b888272SAneesh Kumar K.V 
10308b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1031f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
103249594973SVenkateswararao Jujjuri (JV)     } else {
10338b888272SAneesh Kumar K.V         fd = fs->fd;
10348b888272SAneesh Kumar K.V     }
10358b888272SAneesh Kumar K.V 
10368b888272SAneesh Kumar K.V     if (datasync) {
10378b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
10388b888272SAneesh Kumar K.V     } else {
10398b888272SAneesh Kumar K.V         return fsync(fd);
10408cf89e00SAnthony Liguori     }
104149594973SVenkateswararao Jujjuri (JV) }
10428cf89e00SAnthony Liguori 
10432289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1044be940c87SM. Mohan Kumar {
10454fa4ce71SChen Gang     char *buffer;
10464fa4ce71SChen Gang     int ret;
10472289be19SAneesh Kumar K.V     char *path = fs_path->data;
10482289be19SAneesh Kumar K.V 
10494fa4ce71SChen Gang     buffer = rpath(s, path);
10504fa4ce71SChen Gang     ret = statfs(buffer, stbuf);
10514fa4ce71SChen Gang     g_free(buffer);
10524fa4ce71SChen Gang     return ret;
1053be940c87SM. Mohan Kumar }
1054be940c87SM. Mohan Kumar 
10552289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1056fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1057fa32ef88SAneesh Kumar K.V {
10582289be19SAneesh Kumar K.V     char *path = fs_path->data;
10592289be19SAneesh Kumar K.V 
1060fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1061fa32ef88SAneesh Kumar K.V }
1062fa32ef88SAneesh Kumar K.V 
10632289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1064fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1065fa32ef88SAneesh Kumar K.V {
10662289be19SAneesh Kumar K.V     char *path = fs_path->data;
10672289be19SAneesh Kumar K.V 
1068fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
106961b6c499SAneesh Kumar K.V }
107061b6c499SAneesh Kumar K.V 
10712289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
107210b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
107310b468bdSAneesh Kumar K.V {
10742289be19SAneesh Kumar K.V     char *path = fs_path->data;
10752289be19SAneesh Kumar K.V 
1076fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
107710b468bdSAneesh Kumar K.V }
107810b468bdSAneesh Kumar K.V 
10792289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
10802289be19SAneesh Kumar K.V                               const char *name)
10819ed3ef26SAneesh Kumar K.V {
10822289be19SAneesh Kumar K.V     char *path = fs_path->data;
10832289be19SAneesh Kumar K.V 
1084fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
10859ed3ef26SAneesh Kumar K.V }
10869ed3ef26SAneesh Kumar K.V 
10872289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
10882289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
10892289be19SAneesh Kumar K.V {
10902289be19SAneesh Kumar K.V     if (dir_path) {
1091e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
10922289be19SAneesh Kumar K.V     } else {
1093e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
10942289be19SAneesh Kumar K.V     }
10952289be19SAneesh Kumar K.V     return 0;
10962289be19SAneesh Kumar K.V }
10972289be19SAneesh Kumar K.V 
10982289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
10992289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
11002289be19SAneesh Kumar K.V                           const char *new_name)
11012289be19SAneesh Kumar K.V {
11022289be19SAneesh Kumar K.V     int ret;
11032289be19SAneesh Kumar K.V     V9fsString old_full_name, new_full_name;
11042289be19SAneesh Kumar K.V 
11052289be19SAneesh Kumar K.V     v9fs_string_init(&old_full_name);
11062289be19SAneesh Kumar K.V     v9fs_string_init(&new_full_name);
11072289be19SAneesh Kumar K.V 
11082289be19SAneesh Kumar K.V     v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
11092289be19SAneesh Kumar K.V     v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
11102289be19SAneesh Kumar K.V 
11112289be19SAneesh Kumar K.V     ret = local_rename(ctx, old_full_name.data, new_full_name.data);
11122289be19SAneesh Kumar K.V     v9fs_string_free(&old_full_name);
11132289be19SAneesh Kumar K.V     v9fs_string_free(&new_full_name);
11142289be19SAneesh Kumar K.V     return ret;
11152289be19SAneesh Kumar K.V }
11162289be19SAneesh Kumar K.V 
11172289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
11182289be19SAneesh Kumar K.V                           const char *name, int flags)
11192289be19SAneesh Kumar K.V {
11202289be19SAneesh Kumar K.V     int ret;
11212289be19SAneesh Kumar K.V     V9fsString fullname;
11224fa4ce71SChen Gang     char *buffer;
11232c30dd74SAneesh Kumar K.V 
11242289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
11252289be19SAneesh Kumar K.V 
11262289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
11272c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
11282c30dd74SAneesh Kumar K.V         if (flags == AT_REMOVEDIR) {
11292c30dd74SAneesh Kumar K.V             /*
11302c30dd74SAneesh Kumar K.V              * If directory remove .virtfs_metadata contained in the
11312c30dd74SAneesh Kumar K.V              * directory
11322c30dd74SAneesh Kumar K.V              */
11334fa4ce71SChen Gang             buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
11342c30dd74SAneesh Kumar K.V                                      fullname.data, VIRTFS_META_DIR);
11352c30dd74SAneesh Kumar K.V             ret = remove(buffer);
11364fa4ce71SChen Gang             g_free(buffer);
11372c30dd74SAneesh Kumar K.V             if (ret < 0 && errno != ENOENT) {
11382c30dd74SAneesh Kumar K.V                 /*
11392c30dd74SAneesh Kumar K.V                  * We didn't had the .virtfs_metadata file. May be file created
11402c30dd74SAneesh Kumar K.V                  * in non-mapped mode ?. Ignore ENOENT.
11412c30dd74SAneesh Kumar K.V                  */
11422c30dd74SAneesh Kumar K.V                 goto err_out;
11432c30dd74SAneesh Kumar K.V             }
11442c30dd74SAneesh Kumar K.V         }
11452c30dd74SAneesh Kumar K.V         /*
11462c30dd74SAneesh Kumar K.V          * Now remove the name from parent directory
11472c30dd74SAneesh Kumar K.V          * .virtfs_metadata directory.
11482c30dd74SAneesh Kumar K.V          */
11494fa4ce71SChen Gang         buffer = local_mapped_attr_path(ctx, fullname.data);
11504fa4ce71SChen Gang         ret = remove(buffer);
11514fa4ce71SChen Gang         g_free(buffer);
11522c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
11532c30dd74SAneesh Kumar K.V             /*
11542c30dd74SAneesh Kumar K.V              * We didn't had the .virtfs_metadata file. May be file created
11552c30dd74SAneesh Kumar K.V              * in non-mapped mode ?. Ignore ENOENT.
11562c30dd74SAneesh Kumar K.V              */
11572c30dd74SAneesh Kumar K.V             goto err_out;
11582c30dd74SAneesh Kumar K.V         }
11592c30dd74SAneesh Kumar K.V     }
11602c30dd74SAneesh Kumar K.V     /* Remove the name finally */
11614fa4ce71SChen Gang     buffer = rpath(ctx, fullname.data);
11624fa4ce71SChen Gang     ret = remove(buffer);
11634fa4ce71SChen Gang     g_free(buffer);
11642289be19SAneesh Kumar K.V 
11652c30dd74SAneesh Kumar K.V err_out:
116675b7931eSChen Gang     v9fs_string_free(&fullname);
11672289be19SAneesh Kumar K.V     return ret;
11682289be19SAneesh Kumar K.V }
11699ed3ef26SAneesh Kumar K.V 
1170e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1171e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1172e06a765eSHarsh Prateek Bora {
1173ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
11740e5fc994SKirill A. Shutemov     int err;
1175cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1176cc720ddbSAneesh Kumar K.V 
1177e06a765eSHarsh Prateek Bora     /*
1178e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1179e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1180e06a765eSHarsh Prateek Bora      */
1181e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
11821a9978a5SKirill A. Shutemov         errno = ENOTTY;
11831a9978a5SKirill A. Shutemov         return -1;
1184e06a765eSHarsh Prateek Bora     }
1185cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1186cc720ddbSAneesh Kumar K.V     if (err < 0) {
1187cc720ddbSAneesh Kumar K.V         return err;
1188e06a765eSHarsh Prateek Bora     }
1189cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1190cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1191e06a765eSHarsh Prateek Bora     return err;
11920e5fc994SKirill A. Shutemov #else
11930e5fc994SKirill A. Shutemov     errno = ENOTTY;
11940e5fc994SKirill A. Shutemov     return -1;
11950e5fc994SKirill A. Shutemov #endif
1196e06a765eSHarsh Prateek Bora }
1197e06a765eSHarsh Prateek Bora 
11980174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
11990174fe73SAneesh Kumar K.V {
1200e06a765eSHarsh Prateek Bora     struct statfs stbuf;
12010e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
12020e35a378SGreg Kurz 
12030e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
12040e35a378SGreg Kurz     if (data->mountfd == -1) {
12050e35a378SGreg Kurz         goto err;
12060e35a378SGreg Kurz     }
1207e06a765eSHarsh Prateek Bora 
120800c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
120900c90bd1SGreg Kurz     /*
121000c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
121100c90bd1SGreg Kurz      */
12120e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
12130e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
12140e35a378SGreg Kurz         goto err;
121500c90bd1SGreg Kurz     }
121600c90bd1SGreg Kurz     switch (stbuf.f_type) {
121700c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
121800c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
121900c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
122000c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
122100c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
122200c90bd1SGreg Kurz         break;
122300c90bd1SGreg Kurz     }
122400c90bd1SGreg Kurz #endif
122500c90bd1SGreg Kurz 
12262c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
12272c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
12282c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
12292c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
12302c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
12312c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
12322c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
12332c30dd74SAneesh Kumar K.V         /*
12342c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
12352c30dd74SAneesh Kumar K.V          * remain same.
12362c30dd74SAneesh Kumar K.V          */
12372c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
12382c30dd74SAneesh Kumar K.V     }
1239c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
124000c90bd1SGreg Kurz 
12410e35a378SGreg Kurz     ctx->private = data;
124200c90bd1SGreg Kurz     return 0;
12430e35a378SGreg Kurz 
12440e35a378SGreg Kurz err:
12450e35a378SGreg Kurz     g_free(data);
12460e35a378SGreg Kurz     return -1;
12470e35a378SGreg Kurz }
12480e35a378SGreg Kurz 
12490e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
12500e35a378SGreg Kurz {
12510e35a378SGreg Kurz     LocalData *data = ctx->private;
12520e35a378SGreg Kurz 
12530e35a378SGreg Kurz     close(data->mountfd);
12540e35a378SGreg Kurz     g_free(data);
12550174fe73SAneesh Kumar K.V }
12560174fe73SAneesh Kumar K.V 
125799519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
125899519f0aSAneesh Kumar K.V {
125999519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
126099519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
126199519f0aSAneesh Kumar K.V 
126299519f0aSAneesh Kumar K.V     if (!sec_model) {
126363325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
126463325b18SGreg Kurz         error_printf("valid options are:"
126563325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
126699519f0aSAneesh Kumar K.V         return -1;
126799519f0aSAneesh Kumar K.V     }
126899519f0aSAneesh Kumar K.V 
126999519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
127099519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
12712c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
12722c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
127399519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
127499519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
127599519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
12762c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
12772c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
127899519f0aSAneesh Kumar K.V     } else {
127963325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
128063325b18SGreg Kurz         error_printf("valid options are:"
128163325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
128299519f0aSAneesh Kumar K.V         return -1;
128399519f0aSAneesh Kumar K.V     }
128499519f0aSAneesh Kumar K.V 
128599519f0aSAneesh Kumar K.V     if (!path) {
128663325b18SGreg Kurz         error_report("fsdev: No path specified");
128799519f0aSAneesh Kumar K.V         return -1;
128899519f0aSAneesh Kumar K.V     }
128999519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
129099519f0aSAneesh Kumar K.V 
129199519f0aSAneesh Kumar K.V     return 0;
129299519f0aSAneesh Kumar K.V }
129399519f0aSAneesh Kumar K.V 
12949f107513SAnthony Liguori FileOperations local_ops = {
129599519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
12960174fe73SAneesh Kumar K.V     .init  = local_init,
12970e35a378SGreg Kurz     .cleanup = local_cleanup,
1298131dcb25SAnthony Liguori     .lstat = local_lstat,
1299131dcb25SAnthony Liguori     .readlink = local_readlink,
1300131dcb25SAnthony Liguori     .close = local_close,
1301131dcb25SAnthony Liguori     .closedir = local_closedir,
1302a6568fe2SAnthony Liguori     .open = local_open,
1303a6568fe2SAnthony Liguori     .opendir = local_opendir,
1304a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1305a9231555SAnthony Liguori     .telldir = local_telldir,
1306635324e8SGreg Kurz     .readdir = local_readdir,
1307a9231555SAnthony Liguori     .seekdir = local_seekdir,
130856d15a53SSanchit Garg     .preadv = local_preadv,
130956d15a53SSanchit Garg     .pwritev = local_pwritev,
1310c494dd6fSAnthony Liguori     .chmod = local_chmod,
1311c494dd6fSAnthony Liguori     .mknod = local_mknod,
1312c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1313c494dd6fSAnthony Liguori     .fstat = local_fstat,
1314c494dd6fSAnthony Liguori     .open2 = local_open2,
1315c494dd6fSAnthony Liguori     .symlink = local_symlink,
1316c494dd6fSAnthony Liguori     .link = local_link,
13178cf89e00SAnthony Liguori     .truncate = local_truncate,
13188cf89e00SAnthony Liguori     .rename = local_rename,
13198cf89e00SAnthony Liguori     .chown = local_chown,
132074bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
13215bae1900SAnthony Liguori     .remove = local_remove,
13228cf89e00SAnthony Liguori     .fsync = local_fsync,
1323be940c87SM. Mohan Kumar     .statfs = local_statfs,
1324fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1325fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
132610b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
13279ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
13282289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
13292289be19SAneesh Kumar K.V     .renameat  = local_renameat,
13302289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
13319f107513SAnthony Liguori };
1332