xref: /qemu/hw/9pfs/9p-local.c (revision 31e51d1c15b35dc98b88a301812914b70a2b55dc)
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 
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 
383996a0d76SGreg 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 {
394996a0d76SGreg Kurz     int dirfd;
39521328e1eSGreg Kurz     DIR *stream;
3962289be19SAneesh Kumar K.V 
397996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
398996a0d76SGreg Kurz     if (dirfd == -1) {
399996a0d76SGreg Kurz         return -1;
400996a0d76SGreg Kurz     }
401996a0d76SGreg Kurz 
402996a0d76SGreg 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 {
962a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
963a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
964a33eda0dSGreg Kurz     int dirfd, ret = -1;
9652289be19SAneesh Kumar K.V 
966a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
967a33eda0dSGreg Kurz     if (dirfd == -1) {
968a33eda0dSGreg Kurz         goto out;
969a33eda0dSGreg Kurz     }
970a33eda0dSGreg Kurz 
971a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
972a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
973a33eda0dSGreg Kurz out:
974a33eda0dSGreg Kurz     g_free(dirpath);
975a33eda0dSGreg Kurz     g_free(name);
9764fa4ce71SChen Gang     return ret;
9778cf89e00SAnthony Liguori }
9788cf89e00SAnthony Liguori 
979df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
980df4938a6SGreg Kurz                                  int flags)
981df4938a6SGreg Kurz {
982df4938a6SGreg Kurz     int ret = -1;
983df4938a6SGreg Kurz 
984df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
985df4938a6SGreg Kurz         int map_dirfd;
986df4938a6SGreg Kurz 
987df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
988df4938a6SGreg Kurz             int fd;
989df4938a6SGreg Kurz 
990df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
991df4938a6SGreg Kurz             if (fd == -1) {
992df4938a6SGreg Kurz                 goto err_out;
993df4938a6SGreg Kurz             }
994df4938a6SGreg Kurz             /*
995df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
996df4938a6SGreg Kurz              * directory
997df4938a6SGreg Kurz              */
998df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
999df4938a6SGreg Kurz             close_preserve_errno(fd);
1000df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1001df4938a6SGreg Kurz                 /*
1002df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1003df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1004df4938a6SGreg Kurz                  */
1005df4938a6SGreg Kurz                 goto err_out;
1006df4938a6SGreg Kurz             }
1007df4938a6SGreg Kurz         }
1008df4938a6SGreg Kurz         /*
1009df4938a6SGreg Kurz          * Now remove the name from parent directory
1010df4938a6SGreg Kurz          * .virtfs_metadata directory.
1011df4938a6SGreg Kurz          */
1012df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1013df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1014df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1015df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1016df4938a6SGreg Kurz             /*
1017df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1018df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1019df4938a6SGreg Kurz              */
1020df4938a6SGreg Kurz             goto err_out;
1021df4938a6SGreg Kurz         }
1022df4938a6SGreg Kurz     }
1023df4938a6SGreg Kurz 
1024df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1025df4938a6SGreg Kurz err_out:
1026df4938a6SGreg Kurz     return ret;
1027df4938a6SGreg Kurz }
1028df4938a6SGreg Kurz 
10295bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10305bae1900SAnthony Liguori {
10312c30dd74SAneesh Kumar K.V     struct stat stbuf;
1032a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1033a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1034a0e640a8SGreg Kurz     int flags = 0;
1035a0e640a8SGreg Kurz     int dirfd;
1036a0e640a8SGreg Kurz     int err = -1;
10372c30dd74SAneesh Kumar K.V 
1038a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1039a0e640a8SGreg Kurz     if (dirfd) {
1040a0e640a8SGreg Kurz         goto out;
1041a0e640a8SGreg Kurz     }
1042a0e640a8SGreg Kurz 
1043a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
10442c30dd74SAneesh Kumar K.V         goto err_out;
10452c30dd74SAneesh Kumar K.V     }
1046a0e640a8SGreg Kurz 
10472c30dd74SAneesh Kumar K.V     if (S_ISDIR(stbuf.st_mode)) {
1048a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
10492c30dd74SAneesh Kumar K.V     }
10504fa4ce71SChen Gang 
1051a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
10522c30dd74SAneesh Kumar K.V err_out:
1053a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1054a0e640a8SGreg Kurz out:
1055a0e640a8SGreg Kurz     g_free(name);
1056a0e640a8SGreg Kurz     g_free(dirpath);
10572c30dd74SAneesh Kumar K.V     return err;
10585bae1900SAnthony Liguori }
10595bae1900SAnthony Liguori 
10608b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
10618b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
10628cf89e00SAnthony Liguori {
10638b888272SAneesh Kumar K.V     int fd;
10648b888272SAneesh Kumar K.V 
10658b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1066f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
106749594973SVenkateswararao Jujjuri (JV)     } else {
10688b888272SAneesh Kumar K.V         fd = fs->fd;
10698b888272SAneesh Kumar K.V     }
10708b888272SAneesh Kumar K.V 
10718b888272SAneesh Kumar K.V     if (datasync) {
10728b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
10738b888272SAneesh Kumar K.V     } else {
10748b888272SAneesh Kumar K.V         return fsync(fd);
10758cf89e00SAnthony Liguori     }
107649594973SVenkateswararao Jujjuri (JV) }
10778cf89e00SAnthony Liguori 
10782289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1079be940c87SM. Mohan Kumar {
1080*31e51d1cSGreg Kurz     int fd, ret;
10812289be19SAneesh Kumar K.V 
1082*31e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1083*31e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
1084*31e51d1cSGreg Kurz     close_preserve_errno(fd);
10854fa4ce71SChen Gang     return ret;
1086be940c87SM. Mohan Kumar }
1087be940c87SM. Mohan Kumar 
10882289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1089fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1090fa32ef88SAneesh Kumar K.V {
10912289be19SAneesh Kumar K.V     char *path = fs_path->data;
10922289be19SAneesh Kumar K.V 
1093fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1094fa32ef88SAneesh Kumar K.V }
1095fa32ef88SAneesh Kumar K.V 
10962289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1097fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1098fa32ef88SAneesh Kumar K.V {
10992289be19SAneesh Kumar K.V     char *path = fs_path->data;
11002289be19SAneesh Kumar K.V 
1101fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
110261b6c499SAneesh Kumar K.V }
110361b6c499SAneesh Kumar K.V 
11042289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
110510b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
110610b468bdSAneesh Kumar K.V {
11072289be19SAneesh Kumar K.V     char *path = fs_path->data;
11082289be19SAneesh Kumar K.V 
1109fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
111010b468bdSAneesh Kumar K.V }
111110b468bdSAneesh Kumar K.V 
11122289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11132289be19SAneesh Kumar K.V                               const char *name)
11149ed3ef26SAneesh Kumar K.V {
11152289be19SAneesh Kumar K.V     char *path = fs_path->data;
11162289be19SAneesh Kumar K.V 
1117fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11189ed3ef26SAneesh Kumar K.V }
11199ed3ef26SAneesh Kumar K.V 
11202289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11212289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11222289be19SAneesh Kumar K.V {
11232289be19SAneesh Kumar K.V     if (dir_path) {
1124e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
11252289be19SAneesh Kumar K.V     } else {
1126e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
11272289be19SAneesh Kumar K.V     }
11282289be19SAneesh Kumar K.V     return 0;
11292289be19SAneesh Kumar K.V }
11302289be19SAneesh Kumar K.V 
11312289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
11322289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
11332289be19SAneesh Kumar K.V                           const char *new_name)
11342289be19SAneesh Kumar K.V {
11352289be19SAneesh Kumar K.V     int ret;
11362289be19SAneesh Kumar K.V     V9fsString old_full_name, new_full_name;
11372289be19SAneesh Kumar K.V 
11382289be19SAneesh Kumar K.V     v9fs_string_init(&old_full_name);
11392289be19SAneesh Kumar K.V     v9fs_string_init(&new_full_name);
11402289be19SAneesh Kumar K.V 
11412289be19SAneesh Kumar K.V     v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
11422289be19SAneesh Kumar K.V     v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
11432289be19SAneesh Kumar K.V 
11442289be19SAneesh Kumar K.V     ret = local_rename(ctx, old_full_name.data, new_full_name.data);
11452289be19SAneesh Kumar K.V     v9fs_string_free(&old_full_name);
11462289be19SAneesh Kumar K.V     v9fs_string_free(&new_full_name);
11472289be19SAneesh Kumar K.V     return ret;
11482289be19SAneesh Kumar K.V }
11492289be19SAneesh Kumar K.V 
11502289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
11512289be19SAneesh Kumar K.V                           const char *name, int flags)
11522289be19SAneesh Kumar K.V {
11532289be19SAneesh Kumar K.V     int ret;
1154df4938a6SGreg Kurz     int dirfd;
11552c30dd74SAneesh Kumar K.V 
1156df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1157df4938a6SGreg Kurz     if (dirfd == -1) {
1158df4938a6SGreg Kurz         return -1;
1159df4938a6SGreg Kurz     }
11602289be19SAneesh Kumar K.V 
1161df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1162df4938a6SGreg Kurz     close_preserve_errno(dirfd);
11632289be19SAneesh Kumar K.V     return ret;
11642289be19SAneesh Kumar K.V }
11659ed3ef26SAneesh Kumar K.V 
1166e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1167e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1168e06a765eSHarsh Prateek Bora {
1169ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
11700e5fc994SKirill A. Shutemov     int err;
1171cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1172cc720ddbSAneesh Kumar K.V 
1173e06a765eSHarsh Prateek Bora     /*
1174e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1175e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1176e06a765eSHarsh Prateek Bora      */
1177e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
11781a9978a5SKirill A. Shutemov         errno = ENOTTY;
11791a9978a5SKirill A. Shutemov         return -1;
1180e06a765eSHarsh Prateek Bora     }
1181cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1182cc720ddbSAneesh Kumar K.V     if (err < 0) {
1183cc720ddbSAneesh Kumar K.V         return err;
1184e06a765eSHarsh Prateek Bora     }
1185cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1186cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1187e06a765eSHarsh Prateek Bora     return err;
11880e5fc994SKirill A. Shutemov #else
11890e5fc994SKirill A. Shutemov     errno = ENOTTY;
11900e5fc994SKirill A. Shutemov     return -1;
11910e5fc994SKirill A. Shutemov #endif
1192e06a765eSHarsh Prateek Bora }
1193e06a765eSHarsh Prateek Bora 
11940174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
11950174fe73SAneesh Kumar K.V {
1196e06a765eSHarsh Prateek Bora     struct statfs stbuf;
11970e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
11980e35a378SGreg Kurz 
11990e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
12000e35a378SGreg Kurz     if (data->mountfd == -1) {
12010e35a378SGreg Kurz         goto err;
12020e35a378SGreg Kurz     }
1203e06a765eSHarsh Prateek Bora 
120400c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
120500c90bd1SGreg Kurz     /*
120600c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
120700c90bd1SGreg Kurz      */
12080e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
12090e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
12100e35a378SGreg Kurz         goto err;
121100c90bd1SGreg Kurz     }
121200c90bd1SGreg Kurz     switch (stbuf.f_type) {
121300c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
121400c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
121500c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
121600c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
121700c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
121800c90bd1SGreg Kurz         break;
121900c90bd1SGreg Kurz     }
122000c90bd1SGreg Kurz #endif
122100c90bd1SGreg Kurz 
12222c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
12232c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
12242c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
12252c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
12262c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
12272c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
12282c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
12292c30dd74SAneesh Kumar K.V         /*
12302c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
12312c30dd74SAneesh Kumar K.V          * remain same.
12322c30dd74SAneesh Kumar K.V          */
12332c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
12342c30dd74SAneesh Kumar K.V     }
1235c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
123600c90bd1SGreg Kurz 
12370e35a378SGreg Kurz     ctx->private = data;
123800c90bd1SGreg Kurz     return 0;
12390e35a378SGreg Kurz 
12400e35a378SGreg Kurz err:
12410e35a378SGreg Kurz     g_free(data);
12420e35a378SGreg Kurz     return -1;
12430e35a378SGreg Kurz }
12440e35a378SGreg Kurz 
12450e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
12460e35a378SGreg Kurz {
12470e35a378SGreg Kurz     LocalData *data = ctx->private;
12480e35a378SGreg Kurz 
12490e35a378SGreg Kurz     close(data->mountfd);
12500e35a378SGreg Kurz     g_free(data);
12510174fe73SAneesh Kumar K.V }
12520174fe73SAneesh Kumar K.V 
125399519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
125499519f0aSAneesh Kumar K.V {
125599519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
125699519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
125799519f0aSAneesh Kumar K.V 
125899519f0aSAneesh Kumar K.V     if (!sec_model) {
125963325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
126063325b18SGreg Kurz         error_printf("valid options are:"
126163325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
126299519f0aSAneesh Kumar K.V         return -1;
126399519f0aSAneesh Kumar K.V     }
126499519f0aSAneesh Kumar K.V 
126599519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
126699519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
12672c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
12682c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
126999519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
127099519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
127199519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
12722c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
12732c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
127499519f0aSAneesh Kumar K.V     } else {
127563325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
127663325b18SGreg Kurz         error_printf("valid options are:"
127763325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
127899519f0aSAneesh Kumar K.V         return -1;
127999519f0aSAneesh Kumar K.V     }
128099519f0aSAneesh Kumar K.V 
128199519f0aSAneesh Kumar K.V     if (!path) {
128263325b18SGreg Kurz         error_report("fsdev: No path specified");
128399519f0aSAneesh Kumar K.V         return -1;
128499519f0aSAneesh Kumar K.V     }
128599519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
128699519f0aSAneesh Kumar K.V 
128799519f0aSAneesh Kumar K.V     return 0;
128899519f0aSAneesh Kumar K.V }
128999519f0aSAneesh Kumar K.V 
12909f107513SAnthony Liguori FileOperations local_ops = {
129199519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
12920174fe73SAneesh Kumar K.V     .init  = local_init,
12930e35a378SGreg Kurz     .cleanup = local_cleanup,
1294131dcb25SAnthony Liguori     .lstat = local_lstat,
1295131dcb25SAnthony Liguori     .readlink = local_readlink,
1296131dcb25SAnthony Liguori     .close = local_close,
1297131dcb25SAnthony Liguori     .closedir = local_closedir,
1298a6568fe2SAnthony Liguori     .open = local_open,
1299a6568fe2SAnthony Liguori     .opendir = local_opendir,
1300a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1301a9231555SAnthony Liguori     .telldir = local_telldir,
1302635324e8SGreg Kurz     .readdir = local_readdir,
1303a9231555SAnthony Liguori     .seekdir = local_seekdir,
130456d15a53SSanchit Garg     .preadv = local_preadv,
130556d15a53SSanchit Garg     .pwritev = local_pwritev,
1306c494dd6fSAnthony Liguori     .chmod = local_chmod,
1307c494dd6fSAnthony Liguori     .mknod = local_mknod,
1308c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1309c494dd6fSAnthony Liguori     .fstat = local_fstat,
1310c494dd6fSAnthony Liguori     .open2 = local_open2,
1311c494dd6fSAnthony Liguori     .symlink = local_symlink,
1312c494dd6fSAnthony Liguori     .link = local_link,
13138cf89e00SAnthony Liguori     .truncate = local_truncate,
13148cf89e00SAnthony Liguori     .rename = local_rename,
13158cf89e00SAnthony Liguori     .chown = local_chown,
131674bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
13175bae1900SAnthony Liguori     .remove = local_remove,
13188cf89e00SAnthony Liguori     .fsync = local_fsync,
1319be940c87SM. Mohan Kumar     .statfs = local_statfs,
1320fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1321fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
132210b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
13239ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
13242289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
13252289be19SAneesh Kumar K.V     .renameat  = local_renameat,
13262289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
13279f107513SAnthony Liguori };
1328