xref: /qemu/hw/9pfs/9p-local.c (revision e3187a45dd02a7490f9191c16527dc28a4ba45b9)
19f107513SAnthony Liguori /*
2f00d4f59SWei Liu  * 9p Posix callback
39f107513SAnthony Liguori  *
49f107513SAnthony Liguori  * Copyright IBM, Corp. 2010
59f107513SAnthony Liguori  *
69f107513SAnthony Liguori  * Authors:
79f107513SAnthony Liguori  *  Anthony Liguori   <aliguori@us.ibm.com>
89f107513SAnthony Liguori  *
99f107513SAnthony Liguori  * This work is licensed under the terms of the GNU GPL, version 2.  See
109f107513SAnthony Liguori  * the COPYING file in the top-level directory.
119f107513SAnthony Liguori  *
129f107513SAnthony Liguori  */
13873c3213SStefan Weil 
14fbc04127SPeter Maydell #include "qemu/osdep.h"
15ebe74f8bSWei Liu #include "9p.h"
16996a0d76SGreg Kurz #include "9p-local.h"
17267ae092SWei Liu #include "9p-xattr.h"
180e35a378SGreg Kurz #include "9p-util.h"
1969b15212SStefan Weil #include "fsdev/qemu-fsdev.h"   /* local_ops */
20c494dd6fSAnthony Liguori #include <arpa/inet.h>
21131dcb25SAnthony Liguori #include <pwd.h>
22131dcb25SAnthony Liguori #include <grp.h>
23c494dd6fSAnthony Liguori #include <sys/socket.h>
24c494dd6fSAnthony Liguori #include <sys/un.h>
251de7afc9SPaolo Bonzini #include "qemu/xattr.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
2763325b18SGreg Kurz #include "qemu/error-report.h"
282c30dd74SAneesh Kumar K.V #include <libgen.h>
29e06a765eSHarsh Prateek Bora #include <linux/fs.h>
30e06a765eSHarsh Prateek Bora #ifdef CONFIG_LINUX_MAGIC_H
31e06a765eSHarsh Prateek Bora #include <linux/magic.h>
32e06a765eSHarsh Prateek Bora #endif
33e06a765eSHarsh Prateek Bora #include <sys/ioctl.h>
34e06a765eSHarsh Prateek Bora 
35e06a765eSHarsh Prateek Bora #ifndef XFS_SUPER_MAGIC
36e06a765eSHarsh Prateek Bora #define XFS_SUPER_MAGIC  0x58465342
37e06a765eSHarsh Prateek Bora #endif
38e06a765eSHarsh Prateek Bora #ifndef EXT2_SUPER_MAGIC
39e06a765eSHarsh Prateek Bora #define EXT2_SUPER_MAGIC 0xEF53
40e06a765eSHarsh Prateek Bora #endif
41e06a765eSHarsh Prateek Bora #ifndef REISERFS_SUPER_MAGIC
42e06a765eSHarsh Prateek Bora #define REISERFS_SUPER_MAGIC 0x52654973
43e06a765eSHarsh Prateek Bora #endif
44e06a765eSHarsh Prateek Bora #ifndef BTRFS_SUPER_MAGIC
45e06a765eSHarsh Prateek Bora #define BTRFS_SUPER_MAGIC 0x9123683E
46e06a765eSHarsh Prateek Bora #endif
47131dcb25SAnthony Liguori 
480e35a378SGreg Kurz typedef struct {
490e35a378SGreg Kurz     int mountfd;
500e35a378SGreg Kurz } LocalData;
510e35a378SGreg Kurz 
52996a0d76SGreg Kurz int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53996a0d76SGreg Kurz                         mode_t mode)
54996a0d76SGreg Kurz {
55996a0d76SGreg Kurz     LocalData *data = fs_ctx->private;
56996a0d76SGreg Kurz 
57996a0d76SGreg Kurz     /* All paths are relative to the path data->mountfd points to */
58996a0d76SGreg Kurz     while (*path == '/') {
59996a0d76SGreg Kurz         path++;
60996a0d76SGreg Kurz     }
61996a0d76SGreg Kurz 
62996a0d76SGreg Kurz     return relative_openat_nofollow(data->mountfd, path, flags, mode);
63996a0d76SGreg Kurz }
64996a0d76SGreg Kurz 
65996a0d76SGreg Kurz int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66996a0d76SGreg Kurz {
67996a0d76SGreg Kurz     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68996a0d76SGreg Kurz }
69996a0d76SGreg Kurz 
7099f2cf4bSGreg Kurz static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
7199f2cf4bSGreg Kurz                                     const char *npath)
7299f2cf4bSGreg Kurz {
7399f2cf4bSGreg Kurz     int serrno = errno;
7499f2cf4bSGreg Kurz     renameat(odirfd, opath, ndirfd, npath);
7599f2cf4bSGreg Kurz     errno = serrno;
7699f2cf4bSGreg Kurz }
7799f2cf4bSGreg Kurz 
78ad0b46e6SGreg Kurz static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
79ad0b46e6SGreg Kurz {
80ad0b46e6SGreg Kurz     int serrno = errno;
81ad0b46e6SGreg Kurz     unlinkat(dirfd, path, flags);
82ad0b46e6SGreg Kurz     errno = serrno;
83ad0b46e6SGreg Kurz }
84ad0b46e6SGreg Kurz 
852c30dd74SAneesh Kumar K.V #define VIRTFS_META_DIR ".virtfs_metadata"
862c30dd74SAneesh Kumar K.V 
874fa4ce71SChen Gang static char *local_mapped_attr_path(FsContext *ctx, const char *path)
882c30dd74SAneesh Kumar K.V {
891b6f85e2SMichael Tokarev     int dirlen;
901b6f85e2SMichael Tokarev     const char *name = strrchr(path, '/');
911b6f85e2SMichael Tokarev     if (name) {
921b6f85e2SMichael Tokarev         dirlen = name - path;
931b6f85e2SMichael Tokarev         ++name;
941b6f85e2SMichael Tokarev     } else {
951b6f85e2SMichael Tokarev         name = path;
961b6f85e2SMichael Tokarev         dirlen = 0;
971b6f85e2SMichael Tokarev     }
981b6f85e2SMichael Tokarev     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
991b6f85e2SMichael Tokarev                            dirlen, path, VIRTFS_META_DIR, name);
1002c30dd74SAneesh Kumar K.V }
1012c30dd74SAneesh Kumar K.V 
1020ceb092eSAneesh Kumar K.V static FILE *local_fopen(const char *path, const char *mode)
1030ceb092eSAneesh Kumar K.V {
1040ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
1050ceb092eSAneesh Kumar K.V     FILE *fp;
1060ceb092eSAneesh Kumar K.V     int flags = O_NOFOLLOW;
1070ceb092eSAneesh Kumar K.V     /*
1080ceb092eSAneesh Kumar K.V      * only supports two modes
1090ceb092eSAneesh Kumar K.V      */
1100ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
1110ceb092eSAneesh Kumar K.V         flags |= O_RDONLY;
1120ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
1130ceb092eSAneesh Kumar K.V         flags |= O_WRONLY | O_TRUNC | O_CREAT;
1140ceb092eSAneesh Kumar K.V         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1150ceb092eSAneesh Kumar K.V     } else {
1160ceb092eSAneesh Kumar K.V         return NULL;
1170ceb092eSAneesh Kumar K.V     }
1180ceb092eSAneesh Kumar K.V     fd = open(path, flags, o_mode);
1190ceb092eSAneesh Kumar K.V     if (fd == -1) {
1200ceb092eSAneesh Kumar K.V         return NULL;
1210ceb092eSAneesh Kumar K.V     }
1220ceb092eSAneesh Kumar K.V     fp = fdopen(fd, mode);
1230ceb092eSAneesh Kumar K.V     if (!fp) {
1240ceb092eSAneesh Kumar K.V         close(fd);
1250ceb092eSAneesh Kumar K.V     }
1260ceb092eSAneesh Kumar K.V     return fp;
1270ceb092eSAneesh Kumar K.V }
1280ceb092eSAneesh Kumar K.V 
129f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
130f9aef99bSGreg Kurz {
131f9aef99bSGreg Kurz     int fd, o_mode = 0;
132f9aef99bSGreg Kurz     FILE *fp;
133f9aef99bSGreg Kurz     int flags;
134f9aef99bSGreg Kurz     /*
135f9aef99bSGreg Kurz      * only supports two modes
136f9aef99bSGreg Kurz      */
137f9aef99bSGreg Kurz     if (mode[0] == 'r') {
138f9aef99bSGreg Kurz         flags = O_RDONLY;
139f9aef99bSGreg Kurz     } else if (mode[0] == 'w') {
140f9aef99bSGreg Kurz         flags = O_WRONLY | O_TRUNC | O_CREAT;
141f9aef99bSGreg Kurz         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
142f9aef99bSGreg Kurz     } else {
143f9aef99bSGreg Kurz         return NULL;
144f9aef99bSGreg Kurz     }
145f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, flags, o_mode);
146f9aef99bSGreg Kurz     if (fd == -1) {
147f9aef99bSGreg Kurz         return NULL;
148f9aef99bSGreg Kurz     }
149f9aef99bSGreg Kurz     fp = fdopen(fd, mode);
150f9aef99bSGreg Kurz     if (!fp) {
151f9aef99bSGreg Kurz         close(fd);
152f9aef99bSGreg Kurz     }
153f9aef99bSGreg Kurz     return fp;
154f9aef99bSGreg Kurz }
155f9aef99bSGreg Kurz 
1562c30dd74SAneesh Kumar K.V #define ATTR_MAX 100
157f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
1582c30dd74SAneesh Kumar K.V                                    struct stat *stbuf)
1592c30dd74SAneesh Kumar K.V {
1602c30dd74SAneesh Kumar K.V     FILE *fp;
1612c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
162f9aef99bSGreg Kurz     int map_dirfd;
1632c30dd74SAneesh Kumar K.V 
16499f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
165f9aef99bSGreg Kurz     if (map_dirfd == -1) {
166f9aef99bSGreg Kurz         return;
167f9aef99bSGreg Kurz     }
168f9aef99bSGreg Kurz 
169f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
170f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
1712c30dd74SAneesh Kumar K.V     if (!fp) {
1722c30dd74SAneesh Kumar K.V         return;
1732c30dd74SAneesh Kumar K.V     }
1742c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1752c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1762c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1772c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1782c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1792c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1802c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1812c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1822c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1832c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1842c30dd74SAneesh Kumar K.V         }
1852c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1862c30dd74SAneesh Kumar K.V     }
1872c30dd74SAneesh Kumar K.V     fclose(fp);
1882c30dd74SAneesh Kumar K.V }
1892c30dd74SAneesh Kumar K.V 
1902289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
191131dcb25SAnthony Liguori {
192f9aef99bSGreg Kurz     int err = -1;
193f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
194f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
195f9aef99bSGreg Kurz     int dirfd;
1962289be19SAneesh Kumar K.V 
197f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
198f9aef99bSGreg Kurz     if (dirfd == -1) {
199f9aef99bSGreg Kurz         goto out;
200f9aef99bSGreg Kurz     }
201f9aef99bSGreg Kurz 
202f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
2031237ad76SVenkateswararao Jujjuri (JV)     if (err) {
2044fa4ce71SChen Gang         goto err_out;
2051237ad76SVenkateswararao Jujjuri (JV)     }
206b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
2071237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
2081237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
2091237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
2101237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
2111237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
212f9aef99bSGreg Kurz 
213f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
214f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
215f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
2161237ad76SVenkateswararao Jujjuri (JV)         }
217f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
218f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
219f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
2201237ad76SVenkateswararao Jujjuri (JV)         }
221f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
222f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
223f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
2241237ad76SVenkateswararao Jujjuri (JV)         }
225f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
226f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
227f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
2281237ad76SVenkateswararao Jujjuri (JV)         }
2292c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
230f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
2311237ad76SVenkateswararao Jujjuri (JV)     }
2324fa4ce71SChen Gang 
2334fa4ce71SChen Gang err_out:
234f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
235f9aef99bSGreg Kurz out:
236f9aef99bSGreg Kurz     g_free(name);
237f9aef99bSGreg Kurz     g_free(dirpath);
2381237ad76SVenkateswararao Jujjuri (JV)     return err;
239131dcb25SAnthony Liguori }
240131dcb25SAnthony Liguori 
2412c30dd74SAneesh Kumar K.V static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
2422c30dd74SAneesh Kumar K.V {
2432c30dd74SAneesh Kumar K.V     int err;
2444fa4ce71SChen Gang     char *attr_dir;
245d3f8e138SMarkus Armbruster     char *tmp_path = g_strdup(path);
2462c30dd74SAneesh Kumar K.V 
2474fa4ce71SChen Gang     attr_dir = g_strdup_printf("%s/%s/%s",
2482c30dd74SAneesh Kumar K.V              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
2492c30dd74SAneesh Kumar K.V 
2502c30dd74SAneesh Kumar K.V     err = mkdir(attr_dir, 0700);
2512c30dd74SAneesh Kumar K.V     if (err < 0 && errno == EEXIST) {
2522c30dd74SAneesh Kumar K.V         err = 0;
2532c30dd74SAneesh Kumar K.V     }
2544fa4ce71SChen Gang     g_free(attr_dir);
255d3f8e138SMarkus Armbruster     g_free(tmp_path);
2562c30dd74SAneesh Kumar K.V     return err;
2572c30dd74SAneesh Kumar K.V }
2582c30dd74SAneesh Kumar K.V 
2592c30dd74SAneesh Kumar K.V static int local_set_mapped_file_attr(FsContext *ctx,
2602c30dd74SAneesh Kumar K.V                                       const char *path, FsCred *credp)
2612c30dd74SAneesh Kumar K.V {
2622c30dd74SAneesh Kumar K.V     FILE *fp;
2632c30dd74SAneesh Kumar K.V     int ret = 0;
2642c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2654fa4ce71SChen Gang     char *attr_path;
2662c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
2672c30dd74SAneesh Kumar K.V 
2684fa4ce71SChen Gang     attr_path = local_mapped_attr_path(ctx, path);
2694fa4ce71SChen Gang     fp = local_fopen(attr_path, "r");
2702c30dd74SAneesh Kumar K.V     if (!fp) {
2712c30dd74SAneesh Kumar K.V         goto create_map_file;
2722c30dd74SAneesh Kumar K.V     }
2732c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2742c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2752c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2762c30dd74SAneesh Kumar K.V             uid = atoi(buf+11);
2772c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2782c30dd74SAneesh Kumar K.V             gid = atoi(buf+11);
2792c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2802c30dd74SAneesh Kumar K.V             mode = atoi(buf+12);
2812c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2822c30dd74SAneesh Kumar K.V             rdev = atoi(buf+12);
2832c30dd74SAneesh Kumar K.V         }
2842c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2852c30dd74SAneesh Kumar K.V     }
2862c30dd74SAneesh Kumar K.V     fclose(fp);
2872c30dd74SAneesh Kumar K.V     goto update_map_file;
2882c30dd74SAneesh Kumar K.V 
2892c30dd74SAneesh Kumar K.V create_map_file:
2902c30dd74SAneesh Kumar K.V     ret = local_create_mapped_attr_dir(ctx, path);
2912c30dd74SAneesh Kumar K.V     if (ret < 0) {
2922c30dd74SAneesh Kumar K.V         goto err_out;
2932c30dd74SAneesh Kumar K.V     }
2942c30dd74SAneesh Kumar K.V 
2952c30dd74SAneesh Kumar K.V update_map_file:
2960ceb092eSAneesh Kumar K.V     fp = local_fopen(attr_path, "w");
2972c30dd74SAneesh Kumar K.V     if (!fp) {
2982c30dd74SAneesh Kumar K.V         ret = -1;
2992c30dd74SAneesh Kumar K.V         goto err_out;
3002c30dd74SAneesh Kumar K.V     }
3012c30dd74SAneesh Kumar K.V 
3022c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
3032c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
3042c30dd74SAneesh Kumar K.V     }
3052c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
3062c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
3072c30dd74SAneesh Kumar K.V     }
3082c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
3092c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
3102c30dd74SAneesh Kumar K.V     }
3112c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
3122c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
3132c30dd74SAneesh Kumar K.V     }
3142c30dd74SAneesh Kumar K.V 
3152c30dd74SAneesh Kumar K.V 
3162c30dd74SAneesh Kumar K.V     if (uid != -1) {
3172c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
3182c30dd74SAneesh Kumar K.V     }
3192c30dd74SAneesh Kumar K.V     if (gid != -1) {
3202c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
3212c30dd74SAneesh Kumar K.V     }
3222c30dd74SAneesh Kumar K.V     if (mode != -1) {
3232c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
3242c30dd74SAneesh Kumar K.V     }
3252c30dd74SAneesh Kumar K.V     if (rdev != -1) {
3262c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
3272c30dd74SAneesh Kumar K.V     }
3282c30dd74SAneesh Kumar K.V     fclose(fp);
3292c30dd74SAneesh Kumar K.V 
3302c30dd74SAneesh Kumar K.V err_out:
3314fa4ce71SChen Gang     g_free(attr_path);
3322c30dd74SAneesh Kumar K.V     return ret;
3332c30dd74SAneesh Kumar K.V }
3342c30dd74SAneesh Kumar K.V 
335758e8e38SVenkateswararao Jujjuri (JV) static int local_set_xattr(const char *path, FsCred *credp)
336131dcb25SAnthony Liguori {
337758e8e38SVenkateswararao Jujjuri (JV)     int err;
3382289be19SAneesh Kumar K.V 
339758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
340f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
341f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
342758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
343758e8e38SVenkateswararao Jujjuri (JV)             return err;
344131dcb25SAnthony Liguori         }
345131dcb25SAnthony Liguori     }
346758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
347f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
348f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
349758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
350758e8e38SVenkateswararao Jujjuri (JV)             return err;
351131dcb25SAnthony Liguori         }
352131dcb25SAnthony Liguori     }
353758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
354f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
355f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
356758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
357758e8e38SVenkateswararao Jujjuri (JV)             return err;
358131dcb25SAnthony Liguori         }
359131dcb25SAnthony Liguori     }
360758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
361f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
362f8ad4a89SAneesh Kumar K.V         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
363758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
364758e8e38SVenkateswararao Jujjuri (JV)             return err;
365131dcb25SAnthony Liguori         }
366758e8e38SVenkateswararao Jujjuri (JV)     }
367131dcb25SAnthony Liguori     return 0;
368131dcb25SAnthony Liguori }
369131dcb25SAnthony Liguori 
370*e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
371*e3187a45SGreg Kurz                                         FsCred *credp)
372*e3187a45SGreg Kurz {
373*e3187a45SGreg Kurz     FILE *fp;
374*e3187a45SGreg Kurz     int ret;
375*e3187a45SGreg Kurz     char buf[ATTR_MAX];
376*e3187a45SGreg Kurz     int uid = -1, gid = -1, mode = -1, rdev = -1;
377*e3187a45SGreg Kurz     int map_dirfd;
378*e3187a45SGreg Kurz 
379*e3187a45SGreg Kurz     ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
380*e3187a45SGreg Kurz     if (ret < 0 && errno != EEXIST) {
381*e3187a45SGreg Kurz         return -1;
382*e3187a45SGreg Kurz     }
383*e3187a45SGreg Kurz 
384*e3187a45SGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
385*e3187a45SGreg Kurz     if (map_dirfd == -1) {
386*e3187a45SGreg Kurz         return -1;
387*e3187a45SGreg Kurz     }
388*e3187a45SGreg Kurz 
389*e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
390*e3187a45SGreg Kurz     if (!fp) {
391*e3187a45SGreg Kurz         if (errno == ENOENT) {
392*e3187a45SGreg Kurz             goto update_map_file;
393*e3187a45SGreg Kurz         } else {
394*e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
395*e3187a45SGreg Kurz             return -1;
396*e3187a45SGreg Kurz         }
397*e3187a45SGreg Kurz     }
398*e3187a45SGreg Kurz     memset(buf, 0, ATTR_MAX);
399*e3187a45SGreg Kurz     while (fgets(buf, ATTR_MAX, fp)) {
400*e3187a45SGreg Kurz         if (!strncmp(buf, "virtfs.uid", 10)) {
401*e3187a45SGreg Kurz             uid = atoi(buf + 11);
402*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.gid", 10)) {
403*e3187a45SGreg Kurz             gid = atoi(buf + 11);
404*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.mode", 11)) {
405*e3187a45SGreg Kurz             mode = atoi(buf + 12);
406*e3187a45SGreg Kurz         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
407*e3187a45SGreg Kurz             rdev = atoi(buf + 12);
408*e3187a45SGreg Kurz         }
409*e3187a45SGreg Kurz         memset(buf, 0, ATTR_MAX);
410*e3187a45SGreg Kurz     }
411*e3187a45SGreg Kurz     fclose(fp);
412*e3187a45SGreg Kurz 
413*e3187a45SGreg Kurz update_map_file:
414*e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "w");
415*e3187a45SGreg Kurz     close_preserve_errno(map_dirfd);
416*e3187a45SGreg Kurz     if (!fp) {
417*e3187a45SGreg Kurz         return -1;
418*e3187a45SGreg Kurz     }
419*e3187a45SGreg Kurz 
420*e3187a45SGreg Kurz     if (credp->fc_uid != -1) {
421*e3187a45SGreg Kurz         uid = credp->fc_uid;
422*e3187a45SGreg Kurz     }
423*e3187a45SGreg Kurz     if (credp->fc_gid != -1) {
424*e3187a45SGreg Kurz         gid = credp->fc_gid;
425*e3187a45SGreg Kurz     }
426*e3187a45SGreg Kurz     if (credp->fc_mode != -1) {
427*e3187a45SGreg Kurz         mode = credp->fc_mode;
428*e3187a45SGreg Kurz     }
429*e3187a45SGreg Kurz     if (credp->fc_rdev != -1) {
430*e3187a45SGreg Kurz         rdev = credp->fc_rdev;
431*e3187a45SGreg Kurz     }
432*e3187a45SGreg Kurz 
433*e3187a45SGreg Kurz     if (uid != -1) {
434*e3187a45SGreg Kurz         fprintf(fp, "virtfs.uid=%d\n", uid);
435*e3187a45SGreg Kurz     }
436*e3187a45SGreg Kurz     if (gid != -1) {
437*e3187a45SGreg Kurz         fprintf(fp, "virtfs.gid=%d\n", gid);
438*e3187a45SGreg Kurz     }
439*e3187a45SGreg Kurz     if (mode != -1) {
440*e3187a45SGreg Kurz         fprintf(fp, "virtfs.mode=%d\n", mode);
441*e3187a45SGreg Kurz     }
442*e3187a45SGreg Kurz     if (rdev != -1) {
443*e3187a45SGreg Kurz         fprintf(fp, "virtfs.rdev=%d\n", rdev);
444*e3187a45SGreg Kurz     }
445*e3187a45SGreg Kurz     fclose(fp);
446*e3187a45SGreg Kurz 
447*e3187a45SGreg Kurz     return 0;
448*e3187a45SGreg Kurz }
449*e3187a45SGreg Kurz 
450*e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
451*e3187a45SGreg Kurz {
452*e3187a45SGreg Kurz     int fd, ret;
453*e3187a45SGreg Kurz 
454*e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
455*e3187a45SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet. As an
456*e3187a45SGreg Kurz      * alternative, let's open the file and use fchmod() instead. This
457*e3187a45SGreg Kurz      * may fail depending on the permissions of the file, but it is the
458*e3187a45SGreg Kurz      * best we can do to avoid TOCTTOU. We first try to open read-only
459*e3187a45SGreg Kurz      * in case name points to a directory. If that fails, we try write-only
460*e3187a45SGreg Kurz      * in case name doesn't point to a directory.
461*e3187a45SGreg Kurz      */
462*e3187a45SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY, 0);
463*e3187a45SGreg Kurz     if (fd == -1) {
464*e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
465*e3187a45SGreg Kurz         if (errno == EACCES) {
466*e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
467*e3187a45SGreg Kurz         }
468*e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
469*e3187a45SGreg Kurz             errno = EACCES;
470*e3187a45SGreg Kurz         }
471*e3187a45SGreg Kurz     }
472*e3187a45SGreg Kurz     if (fd == -1) {
473*e3187a45SGreg Kurz         return -1;
474*e3187a45SGreg Kurz     }
475*e3187a45SGreg Kurz     ret = fchmod(fd, mode);
476*e3187a45SGreg Kurz     close_preserve_errno(fd);
477*e3187a45SGreg Kurz     return ret;
478*e3187a45SGreg Kurz }
479*e3187a45SGreg Kurz 
480*e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
481*e3187a45SGreg Kurz {
482*e3187a45SGreg Kurz     int err;
483*e3187a45SGreg Kurz 
484*e3187a45SGreg Kurz     if (credp->fc_uid != -1) {
485*e3187a45SGreg Kurz         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
486*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
487*e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
488*e3187a45SGreg Kurz         if (err) {
489*e3187a45SGreg Kurz             return err;
490*e3187a45SGreg Kurz         }
491*e3187a45SGreg Kurz     }
492*e3187a45SGreg Kurz     if (credp->fc_gid != -1) {
493*e3187a45SGreg Kurz         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
494*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
495*e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
496*e3187a45SGreg Kurz         if (err) {
497*e3187a45SGreg Kurz             return err;
498*e3187a45SGreg Kurz         }
499*e3187a45SGreg Kurz     }
500*e3187a45SGreg Kurz     if (credp->fc_mode != -1) {
501*e3187a45SGreg Kurz         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
502*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
503*e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
504*e3187a45SGreg Kurz         if (err) {
505*e3187a45SGreg Kurz             return err;
506*e3187a45SGreg Kurz         }
507*e3187a45SGreg Kurz     }
508*e3187a45SGreg Kurz     if (credp->fc_rdev != -1) {
509*e3187a45SGreg Kurz         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
510*e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
511*e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
512*e3187a45SGreg Kurz         if (err) {
513*e3187a45SGreg Kurz             return err;
514*e3187a45SGreg Kurz         }
515*e3187a45SGreg Kurz     }
516*e3187a45SGreg Kurz     return 0;
517*e3187a45SGreg Kurz }
518*e3187a45SGreg Kurz 
5194750a96fSVenkateswararao Jujjuri (JV) static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
5204750a96fSVenkateswararao Jujjuri (JV)                                          FsCred *credp)
5214750a96fSVenkateswararao Jujjuri (JV) {
5224fa4ce71SChen Gang     char *buffer;
5232289be19SAneesh Kumar K.V 
5244fa4ce71SChen Gang     buffer = rpath(fs_ctx, path);
5254fa4ce71SChen Gang     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
52612848bfcSAneesh Kumar K.V         /*
52712848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
52812848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
52912848bfcSAneesh Kumar K.V          */
530b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
5314fa4ce71SChen Gang             goto err;
5324750a96fSVenkateswararao Jujjuri (JV)         }
53312848bfcSAneesh Kumar K.V     }
5342d40564aSM. Mohan Kumar 
5354fa4ce71SChen Gang     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
5364fa4ce71SChen Gang         goto err;
5372d40564aSM. Mohan Kumar     }
5384fa4ce71SChen Gang 
5394fa4ce71SChen Gang     g_free(buffer);
5404750a96fSVenkateswararao Jujjuri (JV)     return 0;
5414fa4ce71SChen Gang err:
5424fa4ce71SChen Gang     g_free(buffer);
5434fa4ce71SChen Gang     return -1;
5444750a96fSVenkateswararao Jujjuri (JV) }
5454750a96fSVenkateswararao Jujjuri (JV) 
5462289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
547131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
548131dcb25SAnthony Liguori {
549879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
5502289be19SAneesh Kumar K.V 
5512c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
5522c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
553879c2813SVenkateswararao Jujjuri (JV)         int fd;
554bec1e954SGreg Kurz 
555bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
556879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
557879c2813SVenkateswararao Jujjuri (JV)             return -1;
558879c2813SVenkateswararao Jujjuri (JV)         }
559879c2813SVenkateswararao Jujjuri (JV)         do {
560879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
561879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
562bec1e954SGreg Kurz         close_preserve_errno(fd);
563b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
564b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
565bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
566bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
567bec1e954SGreg Kurz         int dirfd;
568bec1e954SGreg Kurz 
569bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
570bec1e954SGreg Kurz         if (dirfd == -1) {
571bec1e954SGreg Kurz             goto out;
572bec1e954SGreg Kurz         }
573bec1e954SGreg Kurz 
574bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
575bec1e954SGreg Kurz         close_preserve_errno(dirfd);
576bec1e954SGreg Kurz     out:
577bec1e954SGreg Kurz         g_free(name);
578bec1e954SGreg Kurz         g_free(dirpath);
579879c2813SVenkateswararao Jujjuri (JV)     }
580879c2813SVenkateswararao Jujjuri (JV)     return tsize;
581131dcb25SAnthony Liguori }
582131dcb25SAnthony Liguori 
583cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
584131dcb25SAnthony Liguori {
585cc720ddbSAneesh Kumar K.V     return close(fs->fd);
586131dcb25SAnthony Liguori }
587131dcb25SAnthony Liguori 
588cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
589131dcb25SAnthony Liguori {
590f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
591131dcb25SAnthony Liguori }
5929f107513SAnthony Liguori 
593cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
594cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
595a6568fe2SAnthony Liguori {
59621328e1eSGreg Kurz     int fd;
5972289be19SAneesh Kumar K.V 
598996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
59921328e1eSGreg Kurz     if (fd == -1) {
60021328e1eSGreg Kurz         return -1;
60121328e1eSGreg Kurz     }
60221328e1eSGreg Kurz     fs->fd = fd;
603cc720ddbSAneesh Kumar K.V     return fs->fd;
604a6568fe2SAnthony Liguori }
605a6568fe2SAnthony Liguori 
606cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
607cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
608a6568fe2SAnthony Liguori {
609996a0d76SGreg Kurz     int dirfd;
61021328e1eSGreg Kurz     DIR *stream;
6112289be19SAneesh Kumar K.V 
612996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
613996a0d76SGreg Kurz     if (dirfd == -1) {
614996a0d76SGreg Kurz         return -1;
615996a0d76SGreg Kurz     }
616996a0d76SGreg Kurz 
617996a0d76SGreg Kurz     stream = fdopendir(dirfd);
61821328e1eSGreg Kurz     if (!stream) {
619cc720ddbSAneesh Kumar K.V         return -1;
620cc720ddbSAneesh Kumar K.V     }
62121328e1eSGreg Kurz     fs->dir.stream = stream;
622cc720ddbSAneesh Kumar K.V     return 0;
623a6568fe2SAnthony Liguori }
624a6568fe2SAnthony Liguori 
625cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
626a9231555SAnthony Liguori {
627f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
628a9231555SAnthony Liguori }
629a9231555SAnthony Liguori 
630cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
631a9231555SAnthony Liguori {
632f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
633a9231555SAnthony Liguori }
634a9231555SAnthony Liguori 
635635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
636a9231555SAnthony Liguori {
637635324e8SGreg Kurz     struct dirent *entry;
6382c30dd74SAneesh Kumar K.V 
6392c30dd74SAneesh Kumar K.V again:
640635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
641635324e8SGreg Kurz     if (!entry) {
642635324e8SGreg Kurz         return NULL;
643635324e8SGreg Kurz     }
644635324e8SGreg Kurz 
645840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
646840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
647840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
648635324e8SGreg Kurz         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
6492c30dd74SAneesh Kumar K.V             /* skp the meta data directory */
6502c30dd74SAneesh Kumar K.V             goto again;
6512c30dd74SAneesh Kumar K.V         }
652840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
6532c30dd74SAneesh Kumar K.V     }
654635324e8SGreg Kurz 
655635324e8SGreg Kurz     return entry;
656a9231555SAnthony Liguori }
657a9231555SAnthony Liguori 
658cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
659a9231555SAnthony Liguori {
660f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
661a9231555SAnthony Liguori }
662a9231555SAnthony Liguori 
663cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
664cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
66556d15a53SSanchit Garg                             int iovcnt, off_t offset)
666a9231555SAnthony Liguori {
66756d15a53SSanchit Garg #ifdef CONFIG_PREADV
668cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
66956d15a53SSanchit Garg #else
670cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
67156d15a53SSanchit Garg     if (err == -1) {
67256d15a53SSanchit Garg         return err;
67356d15a53SSanchit Garg     } else {
674cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
675a9231555SAnthony Liguori     }
67656d15a53SSanchit Garg #endif
677a9231555SAnthony Liguori }
678a9231555SAnthony Liguori 
679cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
680cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
68156d15a53SSanchit Garg                              int iovcnt, off_t offset)
6828449360cSAnthony Liguori {
6836fe76accSGreg Kurz     ssize_t ret;
68456d15a53SSanchit Garg #ifdef CONFIG_PREADV
685cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
68656d15a53SSanchit Garg #else
687cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
68856d15a53SSanchit Garg     if (err == -1) {
68956d15a53SSanchit Garg         return err;
69056d15a53SSanchit Garg     } else {
691cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
6928449360cSAnthony Liguori     }
69356d15a53SSanchit Garg #endif
694d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
695d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
696d3ab98e6SAneesh Kumar K.V         /*
697d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
698d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
699d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
700d3ab98e6SAneesh Kumar K.V          */
701cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
702d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
703d3ab98e6SAneesh Kumar K.V     }
704d3ab98e6SAneesh Kumar K.V #endif
705d3ab98e6SAneesh Kumar K.V     return ret;
70656d15a53SSanchit Garg }
7078449360cSAnthony Liguori 
7082289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
709c494dd6fSAnthony Liguori {
710*e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
711*e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
7124fa4ce71SChen Gang     int ret = -1;
713*e3187a45SGreg Kurz     int dirfd;
714*e3187a45SGreg Kurz 
715*e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
716*e3187a45SGreg Kurz     if (dirfd == -1) {
717*e3187a45SGreg Kurz         goto out;
718*e3187a45SGreg Kurz     }
7192289be19SAneesh Kumar K.V 
720b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
721*e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
7222c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
723*e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
724*e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
725*e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
726*e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
727e95ead32SVenkateswararao Jujjuri (JV)     }
728*e3187a45SGreg Kurz     close_preserve_errno(dirfd);
729*e3187a45SGreg Kurz 
730*e3187a45SGreg Kurz out:
731*e3187a45SGreg Kurz     g_free(dirpath);
732*e3187a45SGreg Kurz     g_free(name);
7334fa4ce71SChen Gang     return ret;
734c494dd6fSAnthony Liguori }
735c494dd6fSAnthony Liguori 
7362289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
7372289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
738c494dd6fSAnthony Liguori {
7392289be19SAneesh Kumar K.V     char *path;
7401c293312SVenkateswararao Jujjuri (JV)     int err = -1;
7411c293312SVenkateswararao Jujjuri (JV)     int serrno = 0;
7422289be19SAneesh Kumar K.V     V9fsString fullname;
7434ed7b2c3SStefan Weil     char *buffer = NULL;
7441c293312SVenkateswararao Jujjuri (JV) 
7452289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
7462289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
7472289be19SAneesh Kumar K.V     path = fullname.data;
7482289be19SAneesh Kumar K.V 
7491c293312SVenkateswararao Jujjuri (JV)     /* Determine the security model */
750b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7514fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7524fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
7531c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
7542289be19SAneesh Kumar K.V             goto out;
7551c293312SVenkateswararao Jujjuri (JV)         }
7564fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
7571c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
7581c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
7591c293312SVenkateswararao Jujjuri (JV)             goto err_end;
7601c293312SVenkateswararao Jujjuri (JV)         }
7612c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7622c30dd74SAneesh Kumar K.V 
7634fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7644fa4ce71SChen Gang         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
7652c30dd74SAneesh Kumar K.V         if (err == -1) {
7662c30dd74SAneesh Kumar K.V             goto out;
7672c30dd74SAneesh Kumar K.V         }
7682c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
7692c30dd74SAneesh Kumar K.V         if (err == -1) {
7702c30dd74SAneesh Kumar K.V             serrno = errno;
7712c30dd74SAneesh Kumar K.V             goto err_end;
7722c30dd74SAneesh Kumar K.V         }
773b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
774b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
7754fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
7764fa4ce71SChen Gang         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
7771c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
7782289be19SAneesh Kumar K.V             goto out;
7791c293312SVenkateswararao Jujjuri (JV)         }
7801c293312SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
7811c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
7821c293312SVenkateswararao Jujjuri (JV)             serrno = errno;
7831c293312SVenkateswararao Jujjuri (JV)             goto err_end;
7841c293312SVenkateswararao Jujjuri (JV)         }
7851c293312SVenkateswararao Jujjuri (JV)     }
7862289be19SAneesh Kumar K.V     goto out;
7871c293312SVenkateswararao Jujjuri (JV) 
7881c293312SVenkateswararao Jujjuri (JV) err_end:
7894fa4ce71SChen Gang     remove(buffer);
7901c293312SVenkateswararao Jujjuri (JV)     errno = serrno;
7912289be19SAneesh Kumar K.V out:
7924ed7b2c3SStefan Weil     g_free(buffer);
7932289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
7941c293312SVenkateswararao Jujjuri (JV)     return err;
795c494dd6fSAnthony Liguori }
796c494dd6fSAnthony Liguori 
7972289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
7982289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
799c494dd6fSAnthony Liguori {
8002289be19SAneesh Kumar K.V     char *path;
80100ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
80200ec5c37SVenkateswararao Jujjuri (JV)     int serrno = 0;
8032289be19SAneesh Kumar K.V     V9fsString fullname;
8044ed7b2c3SStefan Weil     char *buffer = NULL;
80500ec5c37SVenkateswararao Jujjuri (JV) 
8062289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
8072289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
8082289be19SAneesh Kumar K.V     path = fullname.data;
8092289be19SAneesh Kumar K.V 
81000ec5c37SVenkateswararao Jujjuri (JV)     /* Determine the security model */
811b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
8124fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
8134fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
81400ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
8152289be19SAneesh Kumar K.V             goto out;
81600ec5c37SVenkateswararao Jujjuri (JV)         }
81700ec5c37SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFDIR;
8184fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
81900ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
82000ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
82100ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
82200ec5c37SVenkateswararao Jujjuri (JV)         }
8232c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
8244fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
8254fa4ce71SChen Gang         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
8262c30dd74SAneesh Kumar K.V         if (err == -1) {
8272c30dd74SAneesh Kumar K.V             goto out;
8282c30dd74SAneesh Kumar K.V         }
8292c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFDIR;
8302c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
8312c30dd74SAneesh Kumar K.V         if (err == -1) {
8322c30dd74SAneesh Kumar K.V             serrno = errno;
8332c30dd74SAneesh Kumar K.V             goto err_end;
8342c30dd74SAneesh Kumar K.V         }
835b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
836b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
8374fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
8384fa4ce71SChen Gang         err = mkdir(buffer, credp->fc_mode);
83900ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
8402289be19SAneesh Kumar K.V             goto out;
84100ec5c37SVenkateswararao Jujjuri (JV)         }
84200ec5c37SVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
84300ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
84400ec5c37SVenkateswararao Jujjuri (JV)             serrno = errno;
84500ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
84600ec5c37SVenkateswararao Jujjuri (JV)         }
84700ec5c37SVenkateswararao Jujjuri (JV)     }
8482289be19SAneesh Kumar K.V     goto out;
84900ec5c37SVenkateswararao Jujjuri (JV) 
85000ec5c37SVenkateswararao Jujjuri (JV) err_end:
8514fa4ce71SChen Gang     remove(buffer);
85200ec5c37SVenkateswararao Jujjuri (JV)     errno = serrno;
8532289be19SAneesh Kumar K.V out:
8544ed7b2c3SStefan Weil     g_free(buffer);
8552289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
85600ec5c37SVenkateswararao Jujjuri (JV)     return err;
857c494dd6fSAnthony Liguori }
858c494dd6fSAnthony Liguori 
8598b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
860cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
861c494dd6fSAnthony Liguori {
8628b888272SAneesh Kumar K.V     int err, fd;
8638b888272SAneesh Kumar K.V 
8648b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
865f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
8668b888272SAneesh Kumar K.V     } else {
8678b888272SAneesh Kumar K.V         fd = fs->fd;
8688b888272SAneesh Kumar K.V     }
8698b888272SAneesh Kumar K.V 
8708b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
8711237ad76SVenkateswararao Jujjuri (JV)     if (err) {
8721237ad76SVenkateswararao Jujjuri (JV)         return err;
8731237ad76SVenkateswararao Jujjuri (JV)     }
874b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
8751237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
8761237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
8771237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
8781237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
8791237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
8801237ad76SVenkateswararao Jujjuri (JV) 
881f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
882f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
8831237ad76SVenkateswararao Jujjuri (JV)         }
884f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
885f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
8861237ad76SVenkateswararao Jujjuri (JV)         }
887f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
888f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
8891237ad76SVenkateswararao Jujjuri (JV)         }
890f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
891f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
8921237ad76SVenkateswararao Jujjuri (JV)         }
8932c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
8942c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
8952c30dd74SAneesh Kumar K.V         return -1;
8961237ad76SVenkateswararao Jujjuri (JV)     }
8971237ad76SVenkateswararao Jujjuri (JV)     return err;
898c494dd6fSAnthony Liguori }
899c494dd6fSAnthony Liguori 
9002289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
901cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
902c494dd6fSAnthony Liguori {
9032289be19SAneesh Kumar K.V     char *path;
9044750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
9054750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
9064750a96fSVenkateswararao Jujjuri (JV)     int serrno = 0;
9072289be19SAneesh Kumar K.V     V9fsString fullname;
9084ed7b2c3SStefan Weil     char *buffer = NULL;
9094750a96fSVenkateswararao Jujjuri (JV) 
9100ceb092eSAneesh Kumar K.V     /*
9110ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
9120ceb092eSAneesh Kumar K.V      */
9130ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
9140ceb092eSAneesh Kumar K.V 
9152289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
9162289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
9172289be19SAneesh Kumar K.V     path = fullname.data;
9182289be19SAneesh Kumar K.V 
9194750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
920b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
9214fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9224fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
9234750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
9242289be19SAneesh Kumar K.V             err = fd;
9252289be19SAneesh Kumar K.V             goto out;
9264750a96fSVenkateswararao Jujjuri (JV)         }
9274750a96fSVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFREG;
9284750a96fSVenkateswararao Jujjuri (JV)         /* Set cleint credentials in xattr */
9294fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
9304750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
9314750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
9324750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
9334750a96fSVenkateswararao Jujjuri (JV)         }
9342c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
9354fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9364fa4ce71SChen Gang         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
9372c30dd74SAneesh Kumar K.V         if (fd == -1) {
9382c30dd74SAneesh Kumar K.V             err = fd;
9392c30dd74SAneesh Kumar K.V             goto out;
9402c30dd74SAneesh Kumar K.V         }
9412c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFREG;
9422c30dd74SAneesh Kumar K.V         /* Set client credentials in .virtfs_metadata directory files */
9432c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, path, credp);
9442c30dd74SAneesh Kumar K.V         if (err == -1) {
9452c30dd74SAneesh Kumar K.V             serrno = errno;
9462c30dd74SAneesh Kumar K.V             goto err_end;
9472c30dd74SAneesh Kumar K.V         }
948b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
949b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
9504fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
9514fa4ce71SChen Gang         fd = open(buffer, flags, credp->fc_mode);
9524750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
9532289be19SAneesh Kumar K.V             err = fd;
9542289be19SAneesh Kumar K.V             goto out;
9554750a96fSVenkateswararao Jujjuri (JV)         }
9564750a96fSVenkateswararao Jujjuri (JV)         err = local_post_create_passthrough(fs_ctx, path, credp);
9574750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
9584750a96fSVenkateswararao Jujjuri (JV)             serrno = errno;
9594750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
9604750a96fSVenkateswararao Jujjuri (JV)         }
9614750a96fSVenkateswararao Jujjuri (JV)     }
9622289be19SAneesh Kumar K.V     err = fd;
963cc720ddbSAneesh Kumar K.V     fs->fd = fd;
9642289be19SAneesh Kumar K.V     goto out;
9654750a96fSVenkateswararao Jujjuri (JV) 
9664750a96fSVenkateswararao Jujjuri (JV) err_end:
9674750a96fSVenkateswararao Jujjuri (JV)     close(fd);
9684fa4ce71SChen Gang     remove(buffer);
9694750a96fSVenkateswararao Jujjuri (JV)     errno = serrno;
9702289be19SAneesh Kumar K.V out:
9714ed7b2c3SStefan Weil     g_free(buffer);
9722289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
9734750a96fSVenkateswararao Jujjuri (JV)     return err;
974c494dd6fSAnthony Liguori }
975c494dd6fSAnthony Liguori 
976758e8e38SVenkateswararao Jujjuri (JV) 
977879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
9782289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
979c494dd6fSAnthony Liguori {
980879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
981879c2813SVenkateswararao Jujjuri (JV)     int serrno = 0;
9822289be19SAneesh Kumar K.V     char *newpath;
9832289be19SAneesh Kumar K.V     V9fsString fullname;
9844ed7b2c3SStefan Weil     char *buffer = NULL;
985879c2813SVenkateswararao Jujjuri (JV) 
9862289be19SAneesh Kumar K.V     v9fs_string_init(&fullname);
9872289be19SAneesh Kumar K.V     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
9882289be19SAneesh Kumar K.V     newpath = fullname.data;
9892289be19SAneesh Kumar K.V 
990879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
991b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
992879c2813SVenkateswararao Jujjuri (JV)         int fd;
993879c2813SVenkateswararao Jujjuri (JV)         ssize_t oldpath_size, write_size;
9944fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
9954fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
996879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
9972289be19SAneesh Kumar K.V             err = fd;
9982289be19SAneesh Kumar K.V             goto out;
999879c2813SVenkateswararao Jujjuri (JV)         }
1000879c2813SVenkateswararao Jujjuri (JV)         /* Write the oldpath (target) to the file. */
1001f35bde2fSHarsh Prateek Bora         oldpath_size = strlen(oldpath);
1002879c2813SVenkateswararao Jujjuri (JV)         do {
1003879c2813SVenkateswararao Jujjuri (JV)             write_size = write(fd, (void *)oldpath, oldpath_size);
1004879c2813SVenkateswararao Jujjuri (JV)         } while (write_size == -1 && errno == EINTR);
1005879c2813SVenkateswararao Jujjuri (JV) 
1006879c2813SVenkateswararao Jujjuri (JV)         if (write_size != oldpath_size) {
1007879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
1008879c2813SVenkateswararao Jujjuri (JV)             close(fd);
1009879c2813SVenkateswararao Jujjuri (JV)             err = -1;
1010879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
1011879c2813SVenkateswararao Jujjuri (JV)         }
1012879c2813SVenkateswararao Jujjuri (JV)         close(fd);
1013879c2813SVenkateswararao Jujjuri (JV)         /* Set cleint credentials in symlink's xattr */
1014879c2813SVenkateswararao Jujjuri (JV)         credp->fc_mode = credp->fc_mode|S_IFLNK;
10154fa4ce71SChen Gang         err = local_set_xattr(buffer, credp);
1016879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
1017879c2813SVenkateswararao Jujjuri (JV)             serrno = errno;
1018879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
1019879c2813SVenkateswararao Jujjuri (JV)         }
10202c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
10212c30dd74SAneesh Kumar K.V         int fd;
10222c30dd74SAneesh Kumar K.V         ssize_t oldpath_size, write_size;
10234fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
10244fa4ce71SChen Gang         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
10252c30dd74SAneesh Kumar K.V         if (fd == -1) {
10262c30dd74SAneesh Kumar K.V             err = fd;
10272c30dd74SAneesh Kumar K.V             goto out;
10282c30dd74SAneesh Kumar K.V         }
10292c30dd74SAneesh Kumar K.V         /* Write the oldpath (target) to the file. */
10302c30dd74SAneesh Kumar K.V         oldpath_size = strlen(oldpath);
10312c30dd74SAneesh Kumar K.V         do {
10322c30dd74SAneesh Kumar K.V             write_size = write(fd, (void *)oldpath, oldpath_size);
10332c30dd74SAneesh Kumar K.V         } while (write_size == -1 && errno == EINTR);
10342c30dd74SAneesh Kumar K.V 
10352c30dd74SAneesh Kumar K.V         if (write_size != oldpath_size) {
10362c30dd74SAneesh Kumar K.V             serrno = errno;
10372c30dd74SAneesh Kumar K.V             close(fd);
10382c30dd74SAneesh Kumar K.V             err = -1;
10392c30dd74SAneesh Kumar K.V             goto err_end;
10402c30dd74SAneesh Kumar K.V         }
10412c30dd74SAneesh Kumar K.V         close(fd);
10422c30dd74SAneesh Kumar K.V         /* Set cleint credentials in symlink's xattr */
10432c30dd74SAneesh Kumar K.V         credp->fc_mode = credp->fc_mode|S_IFLNK;
10442c30dd74SAneesh Kumar K.V         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
10452c30dd74SAneesh Kumar K.V         if (err == -1) {
10462c30dd74SAneesh Kumar K.V             serrno = errno;
10472c30dd74SAneesh Kumar K.V             goto err_end;
10482c30dd74SAneesh Kumar K.V         }
1049b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1050b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
10514fa4ce71SChen Gang         buffer = rpath(fs_ctx, newpath);
10524fa4ce71SChen Gang         err = symlink(oldpath, buffer);
1053879c2813SVenkateswararao Jujjuri (JV)         if (err) {
10542289be19SAneesh Kumar K.V             goto out;
1055879c2813SVenkateswararao Jujjuri (JV)         }
10564fa4ce71SChen Gang         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
1057879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
105812848bfcSAneesh Kumar K.V             /*
105912848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
106012848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
106112848bfcSAneesh Kumar K.V              */
1062b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1063879c2813SVenkateswararao Jujjuri (JV)                 serrno = errno;
1064879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
106512848bfcSAneesh Kumar K.V             } else
106612848bfcSAneesh Kumar K.V                 err = 0;
1067879c2813SVenkateswararao Jujjuri (JV)         }
1068879c2813SVenkateswararao Jujjuri (JV)     }
10692289be19SAneesh Kumar K.V     goto out;
1070879c2813SVenkateswararao Jujjuri (JV) 
1071879c2813SVenkateswararao Jujjuri (JV) err_end:
10724fa4ce71SChen Gang     remove(buffer);
1073879c2813SVenkateswararao Jujjuri (JV)     errno = serrno;
10742289be19SAneesh Kumar K.V out:
10754ed7b2c3SStefan Weil     g_free(buffer);
10762289be19SAneesh Kumar K.V     v9fs_string_free(&fullname);
1077879c2813SVenkateswararao Jujjuri (JV)     return err;
1078c494dd6fSAnthony Liguori }
1079c494dd6fSAnthony Liguori 
10802289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
10812289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
1082c494dd6fSAnthony Liguori {
1083ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
1084ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
1085ad0b46e6SGreg Kurz     int ret = -1;
1086ad0b46e6SGreg Kurz     int odirfd, ndirfd;
1087c494dd6fSAnthony Liguori 
1088ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
1089ad0b46e6SGreg Kurz     if (odirfd == -1) {
10906dd4b1f1SGreg Kurz         goto out;
10916dd4b1f1SGreg Kurz     }
10922c30dd74SAneesh Kumar K.V 
1093ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
1094ad0b46e6SGreg Kurz     if (ndirfd == -1) {
1095ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
1096ad0b46e6SGreg Kurz         goto out;
1097ad0b46e6SGreg Kurz     }
1098ad0b46e6SGreg Kurz 
1099ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
1100ad0b46e6SGreg Kurz     if (ret < 0) {
1101ad0b46e6SGreg Kurz         goto out_close;
1102ad0b46e6SGreg Kurz     }
1103ad0b46e6SGreg Kurz 
11042c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
11056dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1106ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
11076dd4b1f1SGreg Kurz 
1108ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1109ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
1110ad0b46e6SGreg Kurz             goto err_undo_link;
11112c30dd74SAneesh Kumar K.V         }
1112ad0b46e6SGreg Kurz 
1113ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1114ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
1115ad0b46e6SGreg Kurz             goto err;
1116ad0b46e6SGreg Kurz         }
1117ad0b46e6SGreg Kurz 
1118ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1119ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
1120ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
1121ad0b46e6SGreg Kurz             goto err;
1122ad0b46e6SGreg Kurz         }
1123ad0b46e6SGreg Kurz 
1124ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1125ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
1126ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
11272c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
1128ad0b46e6SGreg Kurz             goto err_undo_link;
11292c30dd74SAneesh Kumar K.V         }
11306dd4b1f1SGreg Kurz 
1131ad0b46e6SGreg Kurz         ret = 0;
1132ad0b46e6SGreg Kurz     }
1133ad0b46e6SGreg Kurz     goto out_close;
1134ad0b46e6SGreg Kurz 
1135ad0b46e6SGreg Kurz err:
1136ad0b46e6SGreg Kurz     ret = -1;
1137ad0b46e6SGreg Kurz err_undo_link:
1138ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
1139ad0b46e6SGreg Kurz out_close:
1140ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
1141ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
11426dd4b1f1SGreg Kurz out:
1143ad0b46e6SGreg Kurz     g_free(oname);
1144ad0b46e6SGreg Kurz     g_free(odirpath);
11452289be19SAneesh Kumar K.V     return ret;
1146c494dd6fSAnthony Liguori }
1147c494dd6fSAnthony Liguori 
11482289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
11498cf89e00SAnthony Liguori {
1150ac125d99SGreg Kurz     int fd, ret;
11512289be19SAneesh Kumar K.V 
1152ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1153ac125d99SGreg Kurz     if (fd == -1) {
1154ac125d99SGreg Kurz         return -1;
1155ac125d99SGreg Kurz     }
1156ac125d99SGreg Kurz     ret = ftruncate(fd, size);
1157ac125d99SGreg Kurz     close_preserve_errno(fd);
11584fa4ce71SChen Gang     return ret;
11598cf89e00SAnthony Liguori }
11608cf89e00SAnthony Liguori 
11612289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
11628cf89e00SAnthony Liguori {
11634fa4ce71SChen Gang     char *buffer;
11644fa4ce71SChen Gang     int ret = -1;
11652289be19SAneesh Kumar K.V     char *path = fs_path->data;
11662289be19SAneesh Kumar K.V 
1167c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
116817b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
116917b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
11704fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
11714fa4ce71SChen Gang         ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
11724fa4ce71SChen Gang         g_free(buffer);
1173b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
11744fa4ce71SChen Gang         buffer = rpath(fs_ctx, path);
11754fa4ce71SChen Gang         ret = local_set_xattr(buffer, credp);
11764fa4ce71SChen Gang         g_free(buffer);
11772c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
11782c30dd74SAneesh Kumar K.V         return local_set_mapped_file_attr(fs_ctx, path, credp);
1179f7613beeSVenkateswararao Jujjuri (JV)     }
11804fa4ce71SChen Gang     return ret;
11818cf89e00SAnthony Liguori }
11828cf89e00SAnthony Liguori 
11832289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
118474bc02b2SM. Mohan Kumar                            const struct timespec *buf)
11858cf89e00SAnthony Liguori {
1186a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
1187a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
1188a33eda0dSGreg Kurz     int dirfd, ret = -1;
11892289be19SAneesh Kumar K.V 
1190a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
1191a33eda0dSGreg Kurz     if (dirfd == -1) {
1192a33eda0dSGreg Kurz         goto out;
1193a33eda0dSGreg Kurz     }
1194a33eda0dSGreg Kurz 
1195a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1196a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
1197a33eda0dSGreg Kurz out:
1198a33eda0dSGreg Kurz     g_free(dirpath);
1199a33eda0dSGreg Kurz     g_free(name);
12004fa4ce71SChen Gang     return ret;
12018cf89e00SAnthony Liguori }
12028cf89e00SAnthony Liguori 
1203df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1204df4938a6SGreg Kurz                                  int flags)
1205df4938a6SGreg Kurz {
1206df4938a6SGreg Kurz     int ret = -1;
1207df4938a6SGreg Kurz 
1208df4938a6SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1209df4938a6SGreg Kurz         int map_dirfd;
1210df4938a6SGreg Kurz 
1211df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1212df4938a6SGreg Kurz             int fd;
1213df4938a6SGreg Kurz 
1214df4938a6SGreg Kurz             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1215df4938a6SGreg Kurz             if (fd == -1) {
1216df4938a6SGreg Kurz                 goto err_out;
1217df4938a6SGreg Kurz             }
1218df4938a6SGreg Kurz             /*
1219df4938a6SGreg Kurz              * If directory remove .virtfs_metadata contained in the
1220df4938a6SGreg Kurz              * directory
1221df4938a6SGreg Kurz              */
1222df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1223df4938a6SGreg Kurz             close_preserve_errno(fd);
1224df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1225df4938a6SGreg Kurz                 /*
1226df4938a6SGreg Kurz                  * We didn't had the .virtfs_metadata file. May be file created
1227df4938a6SGreg Kurz                  * in non-mapped mode ?. Ignore ENOENT.
1228df4938a6SGreg Kurz                  */
1229df4938a6SGreg Kurz                 goto err_out;
1230df4938a6SGreg Kurz             }
1231df4938a6SGreg Kurz         }
1232df4938a6SGreg Kurz         /*
1233df4938a6SGreg Kurz          * Now remove the name from parent directory
1234df4938a6SGreg Kurz          * .virtfs_metadata directory.
1235df4938a6SGreg Kurz          */
1236df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1237df4938a6SGreg Kurz         ret = unlinkat(map_dirfd, name, 0);
1238df4938a6SGreg Kurz         close_preserve_errno(map_dirfd);
1239df4938a6SGreg Kurz         if (ret < 0 && errno != ENOENT) {
1240df4938a6SGreg Kurz             /*
1241df4938a6SGreg Kurz              * We didn't had the .virtfs_metadata file. May be file created
1242df4938a6SGreg Kurz              * in non-mapped mode ?. Ignore ENOENT.
1243df4938a6SGreg Kurz              */
1244df4938a6SGreg Kurz             goto err_out;
1245df4938a6SGreg Kurz         }
1246df4938a6SGreg Kurz     }
1247df4938a6SGreg Kurz 
1248df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
1249df4938a6SGreg Kurz err_out:
1250df4938a6SGreg Kurz     return ret;
1251df4938a6SGreg Kurz }
1252df4938a6SGreg Kurz 
12535bae1900SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
12545bae1900SAnthony Liguori {
12552c30dd74SAneesh Kumar K.V     struct stat stbuf;
1256a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1257a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1258a0e640a8SGreg Kurz     int flags = 0;
1259a0e640a8SGreg Kurz     int dirfd;
1260a0e640a8SGreg Kurz     int err = -1;
12612c30dd74SAneesh Kumar K.V 
1262a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1263a0e640a8SGreg Kurz     if (dirfd) {
1264a0e640a8SGreg Kurz         goto out;
1265a0e640a8SGreg Kurz     }
1266a0e640a8SGreg Kurz 
1267a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
12682c30dd74SAneesh Kumar K.V         goto err_out;
12692c30dd74SAneesh Kumar K.V     }
1270a0e640a8SGreg Kurz 
12712c30dd74SAneesh Kumar K.V     if (S_ISDIR(stbuf.st_mode)) {
1272a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
12732c30dd74SAneesh Kumar K.V     }
12744fa4ce71SChen Gang 
1275a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
12762c30dd74SAneesh Kumar K.V err_out:
1277a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1278a0e640a8SGreg Kurz out:
1279a0e640a8SGreg Kurz     g_free(name);
1280a0e640a8SGreg Kurz     g_free(dirpath);
12812c30dd74SAneesh Kumar K.V     return err;
12825bae1900SAnthony Liguori }
12835bae1900SAnthony Liguori 
12848b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
12858b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
12868cf89e00SAnthony Liguori {
12878b888272SAneesh Kumar K.V     int fd;
12888b888272SAneesh Kumar K.V 
12898b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1290f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
129149594973SVenkateswararao Jujjuri (JV)     } else {
12928b888272SAneesh Kumar K.V         fd = fs->fd;
12938b888272SAneesh Kumar K.V     }
12948b888272SAneesh Kumar K.V 
12958b888272SAneesh Kumar K.V     if (datasync) {
12968b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
12978b888272SAneesh Kumar K.V     } else {
12988b888272SAneesh Kumar K.V         return fsync(fd);
12998cf89e00SAnthony Liguori     }
130049594973SVenkateswararao Jujjuri (JV) }
13018cf89e00SAnthony Liguori 
13022289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1303be940c87SM. Mohan Kumar {
130431e51d1cSGreg Kurz     int fd, ret;
13052289be19SAneesh Kumar K.V 
130631e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
130731e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
130831e51d1cSGreg Kurz     close_preserve_errno(fd);
13094fa4ce71SChen Gang     return ret;
1310be940c87SM. Mohan Kumar }
1311be940c87SM. Mohan Kumar 
13122289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1313fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1314fa32ef88SAneesh Kumar K.V {
13152289be19SAneesh Kumar K.V     char *path = fs_path->data;
13162289be19SAneesh Kumar K.V 
1317fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1318fa32ef88SAneesh Kumar K.V }
1319fa32ef88SAneesh Kumar K.V 
13202289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1321fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1322fa32ef88SAneesh Kumar K.V {
13232289be19SAneesh Kumar K.V     char *path = fs_path->data;
13242289be19SAneesh Kumar K.V 
1325fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
132661b6c499SAneesh Kumar K.V }
132761b6c499SAneesh Kumar K.V 
13282289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
132910b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
133010b468bdSAneesh Kumar K.V {
13312289be19SAneesh Kumar K.V     char *path = fs_path->data;
13322289be19SAneesh Kumar K.V 
1333fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
133410b468bdSAneesh Kumar K.V }
133510b468bdSAneesh Kumar K.V 
13362289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
13372289be19SAneesh Kumar K.V                               const char *name)
13389ed3ef26SAneesh Kumar K.V {
13392289be19SAneesh Kumar K.V     char *path = fs_path->data;
13402289be19SAneesh Kumar K.V 
1341fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
13429ed3ef26SAneesh Kumar K.V }
13439ed3ef26SAneesh Kumar K.V 
13442289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
13452289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
13462289be19SAneesh Kumar K.V {
13472289be19SAneesh Kumar K.V     if (dir_path) {
1348e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
13492289be19SAneesh Kumar K.V     } else {
1350e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
13512289be19SAneesh Kumar K.V     }
13522289be19SAneesh Kumar K.V     return 0;
13532289be19SAneesh Kumar K.V }
13542289be19SAneesh Kumar K.V 
13552289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
13562289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
13572289be19SAneesh Kumar K.V                           const char *new_name)
13582289be19SAneesh Kumar K.V {
13592289be19SAneesh Kumar K.V     int ret;
136099f2cf4bSGreg Kurz     int odirfd, ndirfd;
13612289be19SAneesh Kumar K.V 
136299f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
136399f2cf4bSGreg Kurz     if (odirfd == -1) {
136499f2cf4bSGreg Kurz         return -1;
136599f2cf4bSGreg Kurz     }
13662289be19SAneesh Kumar K.V 
136799f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
136899f2cf4bSGreg Kurz     if (ndirfd == -1) {
136999f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
137099f2cf4bSGreg Kurz         return -1;
137199f2cf4bSGreg Kurz     }
13722289be19SAneesh Kumar K.V 
137399f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
137499f2cf4bSGreg Kurz     if (ret < 0) {
137599f2cf4bSGreg Kurz         goto out;
137699f2cf4bSGreg Kurz     }
137799f2cf4bSGreg Kurz 
137899f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
137999f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
138099f2cf4bSGreg Kurz 
138199f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
138299f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
138399f2cf4bSGreg Kurz             goto err_undo_rename;
138499f2cf4bSGreg Kurz         }
138599f2cf4bSGreg Kurz 
13866dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
138799f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
138899f2cf4bSGreg Kurz             goto err;
138999f2cf4bSGreg Kurz         }
139099f2cf4bSGreg Kurz 
13916dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
139299f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
139399f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
139499f2cf4bSGreg Kurz             goto err;
139599f2cf4bSGreg Kurz         }
139699f2cf4bSGreg Kurz 
139799f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
139899f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
139999f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
140099f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
140199f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
140299f2cf4bSGreg Kurz             goto err_undo_rename;
140399f2cf4bSGreg Kurz         }
140499f2cf4bSGreg Kurz 
140599f2cf4bSGreg Kurz         ret = 0;
140699f2cf4bSGreg Kurz     }
140799f2cf4bSGreg Kurz     goto out;
140899f2cf4bSGreg Kurz 
140999f2cf4bSGreg Kurz err:
141099f2cf4bSGreg Kurz     ret = -1;
141199f2cf4bSGreg Kurz err_undo_rename:
141299f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
141399f2cf4bSGreg Kurz out:
141499f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
141599f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
14162289be19SAneesh Kumar K.V     return ret;
14172289be19SAneesh Kumar K.V }
14182289be19SAneesh Kumar K.V 
1419d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1420d2767edeSGreg Kurz {
1421d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1422d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1423d2767edeSGreg Kurz }
1424d2767edeSGreg Kurz 
1425d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1426d2767edeSGreg Kurz                         const char *newpath)
1427d2767edeSGreg Kurz {
1428d2767edeSGreg Kurz     int err;
1429d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1430d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1431d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1432d2767edeSGreg Kurz 
1433d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1434d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1435d2767edeSGreg Kurz 
1436d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1437d2767edeSGreg Kurz 
1438d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1439d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1440d2767edeSGreg Kurz     g_free(nname);
1441d2767edeSGreg Kurz     g_free(oname);
1442d2767edeSGreg Kurz 
1443d2767edeSGreg Kurz     return err;
1444d2767edeSGreg Kurz }
1445d2767edeSGreg Kurz 
14462289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
14472289be19SAneesh Kumar K.V                           const char *name, int flags)
14482289be19SAneesh Kumar K.V {
14492289be19SAneesh Kumar K.V     int ret;
1450df4938a6SGreg Kurz     int dirfd;
14512c30dd74SAneesh Kumar K.V 
1452df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1453df4938a6SGreg Kurz     if (dirfd == -1) {
1454df4938a6SGreg Kurz         return -1;
1455df4938a6SGreg Kurz     }
14562289be19SAneesh Kumar K.V 
1457df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1458df4938a6SGreg Kurz     close_preserve_errno(dirfd);
14592289be19SAneesh Kumar K.V     return ret;
14602289be19SAneesh Kumar K.V }
14619ed3ef26SAneesh Kumar K.V 
1462e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1463e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1464e06a765eSHarsh Prateek Bora {
1465ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
14660e5fc994SKirill A. Shutemov     int err;
1467cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1468cc720ddbSAneesh Kumar K.V 
1469e06a765eSHarsh Prateek Bora     /*
1470e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1471e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1472e06a765eSHarsh Prateek Bora      */
1473e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
14741a9978a5SKirill A. Shutemov         errno = ENOTTY;
14751a9978a5SKirill A. Shutemov         return -1;
1476e06a765eSHarsh Prateek Bora     }
1477cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1478cc720ddbSAneesh Kumar K.V     if (err < 0) {
1479cc720ddbSAneesh Kumar K.V         return err;
1480e06a765eSHarsh Prateek Bora     }
1481cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1482cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1483e06a765eSHarsh Prateek Bora     return err;
14840e5fc994SKirill A. Shutemov #else
14850e5fc994SKirill A. Shutemov     errno = ENOTTY;
14860e5fc994SKirill A. Shutemov     return -1;
14870e5fc994SKirill A. Shutemov #endif
1488e06a765eSHarsh Prateek Bora }
1489e06a765eSHarsh Prateek Bora 
14900174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
14910174fe73SAneesh Kumar K.V {
1492e06a765eSHarsh Prateek Bora     struct statfs stbuf;
14930e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
14940e35a378SGreg Kurz 
14950e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
14960e35a378SGreg Kurz     if (data->mountfd == -1) {
14970e35a378SGreg Kurz         goto err;
14980e35a378SGreg Kurz     }
1499e06a765eSHarsh Prateek Bora 
150000c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
150100c90bd1SGreg Kurz     /*
150200c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
150300c90bd1SGreg Kurz      */
15040e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
15050e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
15060e35a378SGreg Kurz         goto err;
150700c90bd1SGreg Kurz     }
150800c90bd1SGreg Kurz     switch (stbuf.f_type) {
150900c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
151000c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
151100c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
151200c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
151300c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
151400c90bd1SGreg Kurz         break;
151500c90bd1SGreg Kurz     }
151600c90bd1SGreg Kurz #endif
151700c90bd1SGreg Kurz 
15182c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
15192c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
15202c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
15212c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
15222c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
15232c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
15242c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
15252c30dd74SAneesh Kumar K.V         /*
15262c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
15272c30dd74SAneesh Kumar K.V          * remain same.
15282c30dd74SAneesh Kumar K.V          */
15292c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
15302c30dd74SAneesh Kumar K.V     }
1531c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
153200c90bd1SGreg Kurz 
15330e35a378SGreg Kurz     ctx->private = data;
153400c90bd1SGreg Kurz     return 0;
15350e35a378SGreg Kurz 
15360e35a378SGreg Kurz err:
15370e35a378SGreg Kurz     g_free(data);
15380e35a378SGreg Kurz     return -1;
15390e35a378SGreg Kurz }
15400e35a378SGreg Kurz 
15410e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
15420e35a378SGreg Kurz {
15430e35a378SGreg Kurz     LocalData *data = ctx->private;
15440e35a378SGreg Kurz 
15450e35a378SGreg Kurz     close(data->mountfd);
15460e35a378SGreg Kurz     g_free(data);
15470174fe73SAneesh Kumar K.V }
15480174fe73SAneesh Kumar K.V 
154999519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
155099519f0aSAneesh Kumar K.V {
155199519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
155299519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
155399519f0aSAneesh Kumar K.V 
155499519f0aSAneesh Kumar K.V     if (!sec_model) {
155563325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
155663325b18SGreg Kurz         error_printf("valid options are:"
155763325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
155899519f0aSAneesh Kumar K.V         return -1;
155999519f0aSAneesh Kumar K.V     }
156099519f0aSAneesh Kumar K.V 
156199519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
156299519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
15632c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
15642c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
156599519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
156699519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
156799519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
15682c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
15692c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
157099519f0aSAneesh Kumar K.V     } else {
157163325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
157263325b18SGreg Kurz         error_printf("valid options are:"
157363325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
157499519f0aSAneesh Kumar K.V         return -1;
157599519f0aSAneesh Kumar K.V     }
157699519f0aSAneesh Kumar K.V 
157799519f0aSAneesh Kumar K.V     if (!path) {
157863325b18SGreg Kurz         error_report("fsdev: No path specified");
157999519f0aSAneesh Kumar K.V         return -1;
158099519f0aSAneesh Kumar K.V     }
158199519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
158299519f0aSAneesh Kumar K.V 
158399519f0aSAneesh Kumar K.V     return 0;
158499519f0aSAneesh Kumar K.V }
158599519f0aSAneesh Kumar K.V 
15869f107513SAnthony Liguori FileOperations local_ops = {
158799519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
15880174fe73SAneesh Kumar K.V     .init  = local_init,
15890e35a378SGreg Kurz     .cleanup = local_cleanup,
1590131dcb25SAnthony Liguori     .lstat = local_lstat,
1591131dcb25SAnthony Liguori     .readlink = local_readlink,
1592131dcb25SAnthony Liguori     .close = local_close,
1593131dcb25SAnthony Liguori     .closedir = local_closedir,
1594a6568fe2SAnthony Liguori     .open = local_open,
1595a6568fe2SAnthony Liguori     .opendir = local_opendir,
1596a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1597a9231555SAnthony Liguori     .telldir = local_telldir,
1598635324e8SGreg Kurz     .readdir = local_readdir,
1599a9231555SAnthony Liguori     .seekdir = local_seekdir,
160056d15a53SSanchit Garg     .preadv = local_preadv,
160156d15a53SSanchit Garg     .pwritev = local_pwritev,
1602c494dd6fSAnthony Liguori     .chmod = local_chmod,
1603c494dd6fSAnthony Liguori     .mknod = local_mknod,
1604c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1605c494dd6fSAnthony Liguori     .fstat = local_fstat,
1606c494dd6fSAnthony Liguori     .open2 = local_open2,
1607c494dd6fSAnthony Liguori     .symlink = local_symlink,
1608c494dd6fSAnthony Liguori     .link = local_link,
16098cf89e00SAnthony Liguori     .truncate = local_truncate,
16108cf89e00SAnthony Liguori     .rename = local_rename,
16118cf89e00SAnthony Liguori     .chown = local_chown,
161274bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
16135bae1900SAnthony Liguori     .remove = local_remove,
16148cf89e00SAnthony Liguori     .fsync = local_fsync,
1615be940c87SM. Mohan Kumar     .statfs = local_statfs,
1616fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1617fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
161810b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
16199ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
16202289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
16212289be19SAneesh Kumar K.V     .renameat  = local_renameat,
16222289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
16239f107513SAnthony Liguori };
1624