xref: /qemu/hw/9pfs/9p-local.c (revision 6a87e7929f97b86c5823d4616fa1aa7636b2f116)
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 
87f9aef99bSGreg Kurz static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
880ceb092eSAneesh Kumar K.V {
890ceb092eSAneesh Kumar K.V     int fd, o_mode = 0;
900ceb092eSAneesh Kumar K.V     FILE *fp;
91f9aef99bSGreg Kurz     int flags;
920ceb092eSAneesh Kumar K.V     /*
930ceb092eSAneesh Kumar K.V      * only supports two modes
940ceb092eSAneesh Kumar K.V      */
950ceb092eSAneesh Kumar K.V     if (mode[0] == 'r') {
96f9aef99bSGreg Kurz         flags = O_RDONLY;
970ceb092eSAneesh Kumar K.V     } else if (mode[0] == 'w') {
98f9aef99bSGreg Kurz         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     }
103f9aef99bSGreg Kurz     fd = openat_file(dirfd, name, 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
115f9aef99bSGreg Kurz static void local_mapped_file_attr(int dirfd, const char *name,
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];
120f9aef99bSGreg Kurz     int map_dirfd;
1212c30dd74SAneesh Kumar K.V 
12299f2cf4bSGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
123f9aef99bSGreg Kurz     if (map_dirfd == -1) {
124f9aef99bSGreg Kurz         return;
125f9aef99bSGreg Kurz     }
126f9aef99bSGreg Kurz 
127f9aef99bSGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
128f9aef99bSGreg Kurz     close_preserve_errno(map_dirfd);
1292c30dd74SAneesh Kumar K.V     if (!fp) {
1302c30dd74SAneesh Kumar K.V         return;
1312c30dd74SAneesh Kumar K.V     }
1322c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
1332c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
1342c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
1352c30dd74SAneesh Kumar K.V             stbuf->st_uid = atoi(buf+11);
1362c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
1372c30dd74SAneesh Kumar K.V             stbuf->st_gid = atoi(buf+11);
1382c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
1392c30dd74SAneesh Kumar K.V             stbuf->st_mode = atoi(buf+12);
1402c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
1412c30dd74SAneesh Kumar K.V             stbuf->st_rdev = atoi(buf+12);
1422c30dd74SAneesh Kumar K.V         }
1432c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
1442c30dd74SAneesh Kumar K.V     }
1452c30dd74SAneesh Kumar K.V     fclose(fp);
1462c30dd74SAneesh Kumar K.V }
1472c30dd74SAneesh Kumar K.V 
1482289be19SAneesh Kumar K.V static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
149131dcb25SAnthony Liguori {
150f9aef99bSGreg Kurz     int err = -1;
151f9aef99bSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
152f9aef99bSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
153f9aef99bSGreg Kurz     int dirfd;
1542289be19SAneesh Kumar K.V 
155f9aef99bSGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
156f9aef99bSGreg Kurz     if (dirfd == -1) {
157f9aef99bSGreg Kurz         goto out;
158f9aef99bSGreg Kurz     }
159f9aef99bSGreg Kurz 
160f9aef99bSGreg Kurz     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
1611237ad76SVenkateswararao Jujjuri (JV)     if (err) {
1624fa4ce71SChen Gang         goto err_out;
1631237ad76SVenkateswararao Jujjuri (JV)     }
164b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1651237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
1661237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
1671237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
1681237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
1691237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
170f9aef99bSGreg Kurz 
171f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
172f9aef99bSGreg Kurz                                  sizeof(uid_t)) > 0) {
173f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
1741237ad76SVenkateswararao Jujjuri (JV)         }
175f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
176f9aef99bSGreg Kurz                                  sizeof(gid_t)) > 0) {
177f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
1781237ad76SVenkateswararao Jujjuri (JV)         }
179f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
180f9aef99bSGreg Kurz                                  sizeof(mode_t)) > 0) {
181f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
1821237ad76SVenkateswararao Jujjuri (JV)         }
183f9aef99bSGreg Kurz         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
184f9aef99bSGreg Kurz                                  sizeof(dev_t)) > 0) {
185f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
1861237ad76SVenkateswararao Jujjuri (JV)         }
1872c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
188f9aef99bSGreg Kurz         local_mapped_file_attr(dirfd, name, stbuf);
1891237ad76SVenkateswararao Jujjuri (JV)     }
1904fa4ce71SChen Gang 
1914fa4ce71SChen Gang err_out:
192f9aef99bSGreg Kurz     close_preserve_errno(dirfd);
193f9aef99bSGreg Kurz out:
194f9aef99bSGreg Kurz     g_free(name);
195f9aef99bSGreg Kurz     g_free(dirpath);
1961237ad76SVenkateswararao Jujjuri (JV)     return err;
197131dcb25SAnthony Liguori }
198131dcb25SAnthony Liguori 
199e3187a45SGreg Kurz static int local_set_mapped_file_attrat(int dirfd, const char *name,
200e3187a45SGreg Kurz                                         FsCred *credp)
2012c30dd74SAneesh Kumar K.V {
2022c30dd74SAneesh Kumar K.V     FILE *fp;
203e3187a45SGreg Kurz     int ret;
2042c30dd74SAneesh Kumar K.V     char buf[ATTR_MAX];
2052c30dd74SAneesh Kumar K.V     int uid = -1, gid = -1, mode = -1, rdev = -1;
206e3187a45SGreg Kurz     int map_dirfd;
2072c30dd74SAneesh Kumar K.V 
208e3187a45SGreg Kurz     ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
209e3187a45SGreg Kurz     if (ret < 0 && errno != EEXIST) {
210e3187a45SGreg Kurz         return -1;
211e3187a45SGreg Kurz     }
212e3187a45SGreg Kurz 
213e3187a45SGreg Kurz     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
214e3187a45SGreg Kurz     if (map_dirfd == -1) {
215e3187a45SGreg Kurz         return -1;
216e3187a45SGreg Kurz     }
217e3187a45SGreg Kurz 
218e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "r");
2192c30dd74SAneesh Kumar K.V     if (!fp) {
220e3187a45SGreg Kurz         if (errno == ENOENT) {
221e3187a45SGreg Kurz             goto update_map_file;
222e3187a45SGreg Kurz         } else {
223e3187a45SGreg Kurz             close_preserve_errno(map_dirfd);
224e3187a45SGreg Kurz             return -1;
225e3187a45SGreg Kurz         }
2262c30dd74SAneesh Kumar K.V     }
2272c30dd74SAneesh Kumar K.V     memset(buf, 0, ATTR_MAX);
2282c30dd74SAneesh Kumar K.V     while (fgets(buf, ATTR_MAX, fp)) {
2292c30dd74SAneesh Kumar K.V         if (!strncmp(buf, "virtfs.uid", 10)) {
2302c30dd74SAneesh Kumar K.V             uid = atoi(buf + 11);
2312c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.gid", 10)) {
2322c30dd74SAneesh Kumar K.V             gid = atoi(buf + 11);
2332c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.mode", 11)) {
2342c30dd74SAneesh Kumar K.V             mode = atoi(buf + 12);
2352c30dd74SAneesh Kumar K.V         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
2362c30dd74SAneesh Kumar K.V             rdev = atoi(buf + 12);
2372c30dd74SAneesh Kumar K.V         }
2382c30dd74SAneesh Kumar K.V         memset(buf, 0, ATTR_MAX);
2392c30dd74SAneesh Kumar K.V     }
2402c30dd74SAneesh Kumar K.V     fclose(fp);
2412c30dd74SAneesh Kumar K.V 
2422c30dd74SAneesh Kumar K.V update_map_file:
243e3187a45SGreg Kurz     fp = local_fopenat(map_dirfd, name, "w");
244e3187a45SGreg Kurz     close_preserve_errno(map_dirfd);
2452c30dd74SAneesh Kumar K.V     if (!fp) {
246e3187a45SGreg Kurz         return -1;
2472c30dd74SAneesh Kumar K.V     }
2482c30dd74SAneesh Kumar K.V 
2492c30dd74SAneesh Kumar K.V     if (credp->fc_uid != -1) {
2502c30dd74SAneesh Kumar K.V         uid = credp->fc_uid;
2512c30dd74SAneesh Kumar K.V     }
2522c30dd74SAneesh Kumar K.V     if (credp->fc_gid != -1) {
2532c30dd74SAneesh Kumar K.V         gid = credp->fc_gid;
2542c30dd74SAneesh Kumar K.V     }
2552c30dd74SAneesh Kumar K.V     if (credp->fc_mode != -1) {
2562c30dd74SAneesh Kumar K.V         mode = credp->fc_mode;
2572c30dd74SAneesh Kumar K.V     }
2582c30dd74SAneesh Kumar K.V     if (credp->fc_rdev != -1) {
2592c30dd74SAneesh Kumar K.V         rdev = credp->fc_rdev;
2602c30dd74SAneesh Kumar K.V     }
2612c30dd74SAneesh Kumar K.V 
2622c30dd74SAneesh Kumar K.V     if (uid != -1) {
2632c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.uid=%d\n", uid);
2642c30dd74SAneesh Kumar K.V     }
2652c30dd74SAneesh Kumar K.V     if (gid != -1) {
2662c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.gid=%d\n", gid);
2672c30dd74SAneesh Kumar K.V     }
2682c30dd74SAneesh Kumar K.V     if (mode != -1) {
2692c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.mode=%d\n", mode);
2702c30dd74SAneesh Kumar K.V     }
2712c30dd74SAneesh Kumar K.V     if (rdev != -1) {
2722c30dd74SAneesh Kumar K.V         fprintf(fp, "virtfs.rdev=%d\n", rdev);
2732c30dd74SAneesh Kumar K.V     }
2742c30dd74SAneesh Kumar K.V     fclose(fp);
2752c30dd74SAneesh Kumar K.V 
276e3187a45SGreg Kurz     return 0;
277e3187a45SGreg Kurz }
278e3187a45SGreg Kurz 
279e3187a45SGreg Kurz static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
280e3187a45SGreg Kurz {
281e3187a45SGreg Kurz     int fd, ret;
282e3187a45SGreg Kurz 
283e3187a45SGreg Kurz     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
284e3187a45SGreg Kurz      * Unfortunately, the linux kernel doesn't implement it yet. As an
285e3187a45SGreg Kurz      * alternative, let's open the file and use fchmod() instead. This
286e3187a45SGreg Kurz      * may fail depending on the permissions of the file, but it is the
287e3187a45SGreg Kurz      * best we can do to avoid TOCTTOU. We first try to open read-only
288e3187a45SGreg Kurz      * in case name points to a directory. If that fails, we try write-only
289e3187a45SGreg Kurz      * in case name doesn't point to a directory.
290e3187a45SGreg Kurz      */
291e3187a45SGreg Kurz     fd = openat_file(dirfd, name, O_RDONLY, 0);
292e3187a45SGreg Kurz     if (fd == -1) {
293e3187a45SGreg Kurz         /* In case the file is writable-only and isn't a directory. */
294e3187a45SGreg Kurz         if (errno == EACCES) {
295e3187a45SGreg Kurz             fd = openat_file(dirfd, name, O_WRONLY, 0);
296e3187a45SGreg Kurz         }
297e3187a45SGreg Kurz         if (fd == -1 && errno == EISDIR) {
298e3187a45SGreg Kurz             errno = EACCES;
299e3187a45SGreg Kurz         }
300e3187a45SGreg Kurz     }
301e3187a45SGreg Kurz     if (fd == -1) {
302e3187a45SGreg Kurz         return -1;
303e3187a45SGreg Kurz     }
304e3187a45SGreg Kurz     ret = fchmod(fd, mode);
305e3187a45SGreg Kurz     close_preserve_errno(fd);
3062c30dd74SAneesh Kumar K.V     return ret;
3072c30dd74SAneesh Kumar K.V }
3082c30dd74SAneesh Kumar K.V 
309e3187a45SGreg Kurz static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
310131dcb25SAnthony Liguori {
311758e8e38SVenkateswararao Jujjuri (JV)     int err;
3122289be19SAneesh Kumar K.V 
313758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_uid != -1) {
314f8ad4a89SAneesh Kumar K.V         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
315e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
316e3187a45SGreg Kurz                                    sizeof(uid_t), 0);
317758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
318758e8e38SVenkateswararao Jujjuri (JV)             return err;
319131dcb25SAnthony Liguori         }
320131dcb25SAnthony Liguori     }
321758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_gid != -1) {
322f8ad4a89SAneesh Kumar K.V         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
323e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
324e3187a45SGreg Kurz                                    sizeof(gid_t), 0);
325758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
326758e8e38SVenkateswararao Jujjuri (JV)             return err;
327131dcb25SAnthony Liguori         }
328131dcb25SAnthony Liguori     }
329758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_mode != -1) {
330f8ad4a89SAneesh Kumar K.V         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
331e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
332e3187a45SGreg Kurz                                    sizeof(mode_t), 0);
333758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
334758e8e38SVenkateswararao Jujjuri (JV)             return err;
335131dcb25SAnthony Liguori         }
336131dcb25SAnthony Liguori     }
337758e8e38SVenkateswararao Jujjuri (JV)     if (credp->fc_rdev != -1) {
338f8ad4a89SAneesh Kumar K.V         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
339e3187a45SGreg Kurz         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
340e3187a45SGreg Kurz                                    sizeof(dev_t), 0);
341758e8e38SVenkateswararao Jujjuri (JV)         if (err) {
342758e8e38SVenkateswararao Jujjuri (JV)             return err;
343131dcb25SAnthony Liguori         }
344758e8e38SVenkateswararao Jujjuri (JV)     }
345131dcb25SAnthony Liguori     return 0;
346131dcb25SAnthony Liguori }
347131dcb25SAnthony Liguori 
348d815e721SGreg Kurz static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
349d815e721SGreg Kurz                                       const char *name, FsCred *credp)
3504750a96fSVenkateswararao Jujjuri (JV) {
351d815e721SGreg Kurz     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
352b314f6a0SGreg Kurz                  AT_SYMLINK_NOFOLLOW) < 0) {
35312848bfcSAneesh Kumar K.V         /*
35412848bfcSAneesh Kumar K.V          * If we fail to change ownership and if we are
35512848bfcSAneesh Kumar K.V          * using security model none. Ignore the error
35612848bfcSAneesh Kumar K.V          */
357b97400caSAneesh Kumar K.V         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
3584fa4ce71SChen Gang             return -1;
3594750a96fSVenkateswararao Jujjuri (JV)         }
360d815e721SGreg Kurz     }
361d815e721SGreg Kurz 
362d815e721SGreg Kurz     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
363d815e721SGreg Kurz }
3644750a96fSVenkateswararao Jujjuri (JV) 
3652289be19SAneesh Kumar K.V static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
366131dcb25SAnthony Liguori                               char *buf, size_t bufsz)
367131dcb25SAnthony Liguori {
368879c2813SVenkateswararao Jujjuri (JV)     ssize_t tsize = -1;
3692289be19SAneesh Kumar K.V 
3702c30dd74SAneesh Kumar K.V     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
3712c30dd74SAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
372879c2813SVenkateswararao Jujjuri (JV)         int fd;
373bec1e954SGreg Kurz 
374bec1e954SGreg Kurz         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
375879c2813SVenkateswararao Jujjuri (JV)         if (fd == -1) {
376879c2813SVenkateswararao Jujjuri (JV)             return -1;
377879c2813SVenkateswararao Jujjuri (JV)         }
378879c2813SVenkateswararao Jujjuri (JV)         do {
379879c2813SVenkateswararao Jujjuri (JV)             tsize = read(fd, (void *)buf, bufsz);
380879c2813SVenkateswararao Jujjuri (JV)         } while (tsize == -1 && errno == EINTR);
381bec1e954SGreg Kurz         close_preserve_errno(fd);
382b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
383b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
384bec1e954SGreg Kurz         char *dirpath = g_path_get_dirname(fs_path->data);
385bec1e954SGreg Kurz         char *name = g_path_get_basename(fs_path->data);
386bec1e954SGreg Kurz         int dirfd;
387bec1e954SGreg Kurz 
388bec1e954SGreg Kurz         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
389bec1e954SGreg Kurz         if (dirfd == -1) {
390bec1e954SGreg Kurz             goto out;
391bec1e954SGreg Kurz         }
392bec1e954SGreg Kurz 
393bec1e954SGreg Kurz         tsize = readlinkat(dirfd, name, buf, bufsz);
394bec1e954SGreg Kurz         close_preserve_errno(dirfd);
395bec1e954SGreg Kurz     out:
396bec1e954SGreg Kurz         g_free(name);
397bec1e954SGreg Kurz         g_free(dirpath);
398879c2813SVenkateswararao Jujjuri (JV)     }
399879c2813SVenkateswararao Jujjuri (JV)     return tsize;
400131dcb25SAnthony Liguori }
401131dcb25SAnthony Liguori 
402cc720ddbSAneesh Kumar K.V static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
403131dcb25SAnthony Liguori {
404cc720ddbSAneesh Kumar K.V     return close(fs->fd);
405131dcb25SAnthony Liguori }
406131dcb25SAnthony Liguori 
407cc720ddbSAneesh Kumar K.V static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
408131dcb25SAnthony Liguori {
409f314ea4eSGreg Kurz     return closedir(fs->dir.stream);
410131dcb25SAnthony Liguori }
4119f107513SAnthony Liguori 
412cc720ddbSAneesh Kumar K.V static int local_open(FsContext *ctx, V9fsPath *fs_path,
413cc720ddbSAneesh Kumar K.V                       int flags, V9fsFidOpenState *fs)
414a6568fe2SAnthony Liguori {
41521328e1eSGreg Kurz     int fd;
4162289be19SAneesh Kumar K.V 
417996a0d76SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
41821328e1eSGreg Kurz     if (fd == -1) {
41921328e1eSGreg Kurz         return -1;
42021328e1eSGreg Kurz     }
42121328e1eSGreg Kurz     fs->fd = fd;
422cc720ddbSAneesh Kumar K.V     return fs->fd;
423a6568fe2SAnthony Liguori }
424a6568fe2SAnthony Liguori 
425cc720ddbSAneesh Kumar K.V static int local_opendir(FsContext *ctx,
426cc720ddbSAneesh Kumar K.V                          V9fsPath *fs_path, V9fsFidOpenState *fs)
427a6568fe2SAnthony Liguori {
428996a0d76SGreg Kurz     int dirfd;
42921328e1eSGreg Kurz     DIR *stream;
4302289be19SAneesh Kumar K.V 
431996a0d76SGreg Kurz     dirfd = local_opendir_nofollow(ctx, fs_path->data);
432996a0d76SGreg Kurz     if (dirfd == -1) {
433cc720ddbSAneesh Kumar K.V         return -1;
434cc720ddbSAneesh Kumar K.V     }
435996a0d76SGreg Kurz 
436996a0d76SGreg Kurz     stream = fdopendir(dirfd);
43721328e1eSGreg Kurz     if (!stream) {
438faab207fSGreg Kurz         close(dirfd);
439cc720ddbSAneesh Kumar K.V         return -1;
440cc720ddbSAneesh Kumar K.V     }
44121328e1eSGreg Kurz     fs->dir.stream = stream;
442cc720ddbSAneesh Kumar K.V     return 0;
443a6568fe2SAnthony Liguori }
444a6568fe2SAnthony Liguori 
445cc720ddbSAneesh Kumar K.V static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
446a9231555SAnthony Liguori {
447f314ea4eSGreg Kurz     rewinddir(fs->dir.stream);
448a9231555SAnthony Liguori }
449a9231555SAnthony Liguori 
450cc720ddbSAneesh Kumar K.V static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
451a9231555SAnthony Liguori {
452f314ea4eSGreg Kurz     return telldir(fs->dir.stream);
453a9231555SAnthony Liguori }
454a9231555SAnthony Liguori 
4557a95434eSGreg Kurz static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
4567a95434eSGreg Kurz {
4577a95434eSGreg Kurz     return !strcmp(name, VIRTFS_META_DIR);
4587a95434eSGreg Kurz }
4597a95434eSGreg Kurz 
460635324e8SGreg Kurz static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
461a9231555SAnthony Liguori {
462635324e8SGreg Kurz     struct dirent *entry;
4632c30dd74SAneesh Kumar K.V 
4642c30dd74SAneesh Kumar K.V again:
465635324e8SGreg Kurz     entry = readdir(fs->dir.stream);
466635324e8SGreg Kurz     if (!entry) {
467635324e8SGreg Kurz         return NULL;
468635324e8SGreg Kurz     }
469635324e8SGreg Kurz 
470840a1bf2SBastian Blank     if (ctx->export_flags & V9FS_SM_MAPPED) {
471840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
472840a1bf2SBastian Blank     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4737a95434eSGreg Kurz         if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
4747a95434eSGreg Kurz             /* skip the meta data directory */
4752c30dd74SAneesh Kumar K.V             goto again;
4762c30dd74SAneesh Kumar K.V         }
477840a1bf2SBastian Blank         entry->d_type = DT_UNKNOWN;
4782c30dd74SAneesh Kumar K.V     }
479635324e8SGreg Kurz 
480635324e8SGreg Kurz     return entry;
481a9231555SAnthony Liguori }
482a9231555SAnthony Liguori 
483cc720ddbSAneesh Kumar K.V static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
484a9231555SAnthony Liguori {
485f314ea4eSGreg Kurz     seekdir(fs->dir.stream, off);
486a9231555SAnthony Liguori }
487a9231555SAnthony Liguori 
488cc720ddbSAneesh Kumar K.V static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
489cc720ddbSAneesh Kumar K.V                             const struct iovec *iov,
49056d15a53SSanchit Garg                             int iovcnt, off_t offset)
491a9231555SAnthony Liguori {
49256d15a53SSanchit Garg #ifdef CONFIG_PREADV
493cc720ddbSAneesh Kumar K.V     return preadv(fs->fd, iov, iovcnt, offset);
49456d15a53SSanchit Garg #else
495cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
49656d15a53SSanchit Garg     if (err == -1) {
49756d15a53SSanchit Garg         return err;
49856d15a53SSanchit Garg     } else {
499cc720ddbSAneesh Kumar K.V         return readv(fs->fd, iov, iovcnt);
500a9231555SAnthony Liguori     }
50156d15a53SSanchit Garg #endif
502a9231555SAnthony Liguori }
503a9231555SAnthony Liguori 
504cc720ddbSAneesh Kumar K.V static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
505cc720ddbSAneesh Kumar K.V                              const struct iovec *iov,
50656d15a53SSanchit Garg                              int iovcnt, off_t offset)
5078449360cSAnthony Liguori {
5086fe76accSGreg Kurz     ssize_t ret;
50956d15a53SSanchit Garg #ifdef CONFIG_PREADV
510cc720ddbSAneesh Kumar K.V     ret = pwritev(fs->fd, iov, iovcnt, offset);
51156d15a53SSanchit Garg #else
512cc720ddbSAneesh Kumar K.V     int err = lseek(fs->fd, offset, SEEK_SET);
51356d15a53SSanchit Garg     if (err == -1) {
51456d15a53SSanchit Garg         return err;
51556d15a53SSanchit Garg     } else {
516cc720ddbSAneesh Kumar K.V         ret = writev(fs->fd, iov, iovcnt);
5178449360cSAnthony Liguori     }
51856d15a53SSanchit Garg #endif
519d3ab98e6SAneesh Kumar K.V #ifdef CONFIG_SYNC_FILE_RANGE
520d3ab98e6SAneesh Kumar K.V     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
521d3ab98e6SAneesh Kumar K.V         /*
522d3ab98e6SAneesh Kumar K.V          * Initiate a writeback. This is not a data integrity sync.
523d3ab98e6SAneesh Kumar K.V          * We want to ensure that we don't leave dirty pages in the cache
524d3ab98e6SAneesh Kumar K.V          * after write when writeout=immediate is sepcified.
525d3ab98e6SAneesh Kumar K.V          */
526cc720ddbSAneesh Kumar K.V         sync_file_range(fs->fd, offset, ret,
527d3ab98e6SAneesh Kumar K.V                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
528d3ab98e6SAneesh Kumar K.V     }
529d3ab98e6SAneesh Kumar K.V #endif
530d3ab98e6SAneesh Kumar K.V     return ret;
53156d15a53SSanchit Garg }
5328449360cSAnthony Liguori 
5332289be19SAneesh Kumar K.V static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
534c494dd6fSAnthony Liguori {
535e3187a45SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
536e3187a45SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
5374fa4ce71SChen Gang     int ret = -1;
538e3187a45SGreg Kurz     int dirfd;
539e3187a45SGreg Kurz 
540e3187a45SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
541e3187a45SGreg Kurz     if (dirfd == -1) {
542e3187a45SGreg Kurz         goto out;
543e3187a45SGreg Kurz     }
5442289be19SAneesh Kumar K.V 
545b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
546e3187a45SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
5472c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
548e3187a45SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
549e3187a45SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
550e3187a45SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
551e3187a45SGreg Kurz         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
552e95ead32SVenkateswararao Jujjuri (JV)     }
553e3187a45SGreg Kurz     close_preserve_errno(dirfd);
554e3187a45SGreg Kurz 
555e3187a45SGreg Kurz out:
556e3187a45SGreg Kurz     g_free(dirpath);
557e3187a45SGreg Kurz     g_free(name);
5584fa4ce71SChen Gang     return ret;
559c494dd6fSAnthony Liguori }
560c494dd6fSAnthony Liguori 
5612289be19SAneesh Kumar K.V static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
5622289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
563c494dd6fSAnthony Liguori {
5641c293312SVenkateswararao Jujjuri (JV)     int err = -1;
565d815e721SGreg Kurz     int dirfd;
5661c293312SVenkateswararao Jujjuri (JV) 
5677a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
5687a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
5697a95434eSGreg Kurz         errno = EINVAL;
5707a95434eSGreg Kurz         return -1;
5717a95434eSGreg Kurz     }
5727a95434eSGreg Kurz 
573d815e721SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
574d815e721SGreg Kurz     if (dirfd == -1) {
575d815e721SGreg Kurz         return -1;
576d815e721SGreg Kurz     }
5772289be19SAneesh Kumar K.V 
578d815e721SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
579d815e721SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
580d815e721SGreg Kurz         err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
581d815e721SGreg Kurz         if (err == -1) {
582d815e721SGreg Kurz             goto out;
583d815e721SGreg Kurz         }
584d815e721SGreg Kurz 
585b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
586d815e721SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
587d815e721SGreg Kurz         } else {
588d815e721SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
5891c293312SVenkateswararao Jujjuri (JV)         }
5901c293312SVenkateswararao Jujjuri (JV)         if (err == -1) {
5911c293312SVenkateswararao Jujjuri (JV)             goto err_end;
5921c293312SVenkateswararao Jujjuri (JV)         }
593d815e721SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
594d815e721SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
595d815e721SGreg Kurz         err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
5962c30dd74SAneesh Kumar K.V         if (err == -1) {
5972c30dd74SAneesh Kumar K.V             goto out;
5982c30dd74SAneesh Kumar K.V         }
599d815e721SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
6002c30dd74SAneesh Kumar K.V         if (err == -1) {
6011c293312SVenkateswararao Jujjuri (JV)             goto err_end;
6021c293312SVenkateswararao Jujjuri (JV)         }
6031c293312SVenkateswararao Jujjuri (JV)     }
6042289be19SAneesh Kumar K.V     goto out;
6051c293312SVenkateswararao Jujjuri (JV) 
6061c293312SVenkateswararao Jujjuri (JV) err_end:
607d815e721SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
6082289be19SAneesh Kumar K.V out:
609d815e721SGreg Kurz     close_preserve_errno(dirfd);
6101c293312SVenkateswararao Jujjuri (JV)     return err;
611c494dd6fSAnthony Liguori }
612c494dd6fSAnthony Liguori 
6132289be19SAneesh Kumar K.V static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
6142289be19SAneesh Kumar K.V                        const char *name, FsCred *credp)
615c494dd6fSAnthony Liguori {
61600ec5c37SVenkateswararao Jujjuri (JV)     int err = -1;
6173f3a1699SGreg Kurz     int dirfd;
61800ec5c37SVenkateswararao Jujjuri (JV) 
6197a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
6207a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
6217a95434eSGreg Kurz         errno = EINVAL;
6227a95434eSGreg Kurz         return -1;
6237a95434eSGreg Kurz     }
6247a95434eSGreg Kurz 
6253f3a1699SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
6263f3a1699SGreg Kurz     if (dirfd == -1) {
6273f3a1699SGreg Kurz         return -1;
6283f3a1699SGreg Kurz     }
6292289be19SAneesh Kumar K.V 
6303f3a1699SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
6313f3a1699SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
6323f3a1699SGreg Kurz         err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS);
6333f3a1699SGreg Kurz         if (err == -1) {
6343f3a1699SGreg Kurz             goto out;
6353f3a1699SGreg Kurz         }
6363f3a1699SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFDIR;
6373f3a1699SGreg Kurz 
638b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6393f3a1699SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
6403f3a1699SGreg Kurz         } else {
6413f3a1699SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
64200ec5c37SVenkateswararao Jujjuri (JV)         }
64300ec5c37SVenkateswararao Jujjuri (JV)         if (err == -1) {
64400ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
64500ec5c37SVenkateswararao Jujjuri (JV)         }
6463f3a1699SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
6473f3a1699SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
6483f3a1699SGreg Kurz         err = mkdirat(dirfd, name, credp->fc_mode);
6492c30dd74SAneesh Kumar K.V         if (err == -1) {
6502c30dd74SAneesh Kumar K.V             goto out;
6512c30dd74SAneesh Kumar K.V         }
6523f3a1699SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
6532c30dd74SAneesh Kumar K.V         if (err == -1) {
65400ec5c37SVenkateswararao Jujjuri (JV)             goto err_end;
65500ec5c37SVenkateswararao Jujjuri (JV)         }
65600ec5c37SVenkateswararao Jujjuri (JV)     }
6572289be19SAneesh Kumar K.V     goto out;
65800ec5c37SVenkateswararao Jujjuri (JV) 
65900ec5c37SVenkateswararao Jujjuri (JV) err_end:
6603f3a1699SGreg Kurz     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
6612289be19SAneesh Kumar K.V out:
6623f3a1699SGreg Kurz     close_preserve_errno(dirfd);
66300ec5c37SVenkateswararao Jujjuri (JV)     return err;
664c494dd6fSAnthony Liguori }
665c494dd6fSAnthony Liguori 
6668b888272SAneesh Kumar K.V static int local_fstat(FsContext *fs_ctx, int fid_type,
667cc720ddbSAneesh Kumar K.V                        V9fsFidOpenState *fs, struct stat *stbuf)
668c494dd6fSAnthony Liguori {
6698b888272SAneesh Kumar K.V     int err, fd;
6708b888272SAneesh Kumar K.V 
6718b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
672f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
6738b888272SAneesh Kumar K.V     } else {
6748b888272SAneesh Kumar K.V         fd = fs->fd;
6758b888272SAneesh Kumar K.V     }
6768b888272SAneesh Kumar K.V 
6778b888272SAneesh Kumar K.V     err = fstat(fd, stbuf);
6781237ad76SVenkateswararao Jujjuri (JV)     if (err) {
6791237ad76SVenkateswararao Jujjuri (JV)         return err;
6801237ad76SVenkateswararao Jujjuri (JV)     }
681b97400caSAneesh Kumar K.V     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
6821237ad76SVenkateswararao Jujjuri (JV)         /* Actual credentials are part of extended attrs */
6831237ad76SVenkateswararao Jujjuri (JV)         uid_t tmp_uid;
6841237ad76SVenkateswararao Jujjuri (JV)         gid_t tmp_gid;
6851237ad76SVenkateswararao Jujjuri (JV)         mode_t tmp_mode;
6861237ad76SVenkateswararao Jujjuri (JV)         dev_t tmp_dev;
6871237ad76SVenkateswararao Jujjuri (JV) 
688f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
689f8ad4a89SAneesh Kumar K.V             stbuf->st_uid = le32_to_cpu(tmp_uid);
6901237ad76SVenkateswararao Jujjuri (JV)         }
691f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
692f8ad4a89SAneesh Kumar K.V             stbuf->st_gid = le32_to_cpu(tmp_gid);
6931237ad76SVenkateswararao Jujjuri (JV)         }
694f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
695f8ad4a89SAneesh Kumar K.V             stbuf->st_mode = le32_to_cpu(tmp_mode);
6961237ad76SVenkateswararao Jujjuri (JV)         }
697f8ad4a89SAneesh Kumar K.V         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
698f8ad4a89SAneesh Kumar K.V             stbuf->st_rdev = le64_to_cpu(tmp_dev);
6991237ad76SVenkateswararao Jujjuri (JV)         }
7002c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7012c30dd74SAneesh Kumar K.V         errno = EOPNOTSUPP;
7022c30dd74SAneesh Kumar K.V         return -1;
7031237ad76SVenkateswararao Jujjuri (JV)     }
7041237ad76SVenkateswararao Jujjuri (JV)     return err;
705c494dd6fSAnthony Liguori }
706c494dd6fSAnthony Liguori 
7072289be19SAneesh Kumar K.V static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
708cc720ddbSAneesh Kumar K.V                        int flags, FsCred *credp, V9fsFidOpenState *fs)
709c494dd6fSAnthony Liguori {
7104750a96fSVenkateswararao Jujjuri (JV)     int fd = -1;
7114750a96fSVenkateswararao Jujjuri (JV)     int err = -1;
712a565fea5SGreg Kurz     int dirfd;
7134750a96fSVenkateswararao Jujjuri (JV) 
7147a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7157a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7167a95434eSGreg Kurz         errno = EINVAL;
7177a95434eSGreg Kurz         return -1;
7187a95434eSGreg Kurz     }
7197a95434eSGreg Kurz 
7200ceb092eSAneesh Kumar K.V     /*
7210ceb092eSAneesh Kumar K.V      * Mark all the open to not follow symlinks
7220ceb092eSAneesh Kumar K.V      */
7230ceb092eSAneesh Kumar K.V     flags |= O_NOFOLLOW;
7240ceb092eSAneesh Kumar K.V 
725a565fea5SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
726a565fea5SGreg Kurz     if (dirfd == -1) {
727a565fea5SGreg Kurz         return -1;
728a565fea5SGreg Kurz     }
7292289be19SAneesh Kumar K.V 
7304750a96fSVenkateswararao Jujjuri (JV)     /* Determine the security model */
731a565fea5SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
732a565fea5SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
733a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS);
734a565fea5SGreg Kurz         if (fd == -1) {
735a565fea5SGreg Kurz             goto out;
736a565fea5SGreg Kurz         }
737a565fea5SGreg Kurz         credp->fc_mode = credp->fc_mode|S_IFREG;
738b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
7394750a96fSVenkateswararao Jujjuri (JV)             /* Set cleint credentials in xattr */
740a565fea5SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
741a565fea5SGreg Kurz         } else {
742a565fea5SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
7434750a96fSVenkateswararao Jujjuri (JV)         }
7442c30dd74SAneesh Kumar K.V         if (err == -1) {
7452c30dd74SAneesh Kumar K.V             goto err_end;
7462c30dd74SAneesh Kumar K.V         }
747b97400caSAneesh Kumar K.V     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
748b97400caSAneesh Kumar K.V                (fs_ctx->export_flags & V9FS_SM_NONE)) {
749a565fea5SGreg Kurz         fd = openat_file(dirfd, name, flags, credp->fc_mode);
7504750a96fSVenkateswararao Jujjuri (JV)         if (fd == -1) {
7512289be19SAneesh Kumar K.V             goto out;
7524750a96fSVenkateswararao Jujjuri (JV)         }
753a565fea5SGreg Kurz         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
7544750a96fSVenkateswararao Jujjuri (JV)         if (err == -1) {
7554750a96fSVenkateswararao Jujjuri (JV)             goto err_end;
7564750a96fSVenkateswararao Jujjuri (JV)         }
7574750a96fSVenkateswararao Jujjuri (JV)     }
7582289be19SAneesh Kumar K.V     err = fd;
759cc720ddbSAneesh Kumar K.V     fs->fd = fd;
7602289be19SAneesh Kumar K.V     goto out;
7614750a96fSVenkateswararao Jujjuri (JV) 
7624750a96fSVenkateswararao Jujjuri (JV) err_end:
763a565fea5SGreg Kurz     unlinkat_preserve_errno(dirfd, name,
764a565fea5SGreg Kurz                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
765a565fea5SGreg Kurz     close_preserve_errno(fd);
7662289be19SAneesh Kumar K.V out:
767a565fea5SGreg Kurz     close_preserve_errno(dirfd);
7684750a96fSVenkateswararao Jujjuri (JV)     return err;
769c494dd6fSAnthony Liguori }
770c494dd6fSAnthony Liguori 
771758e8e38SVenkateswararao Jujjuri (JV) 
772879c2813SVenkateswararao Jujjuri (JV) static int local_symlink(FsContext *fs_ctx, const char *oldpath,
7732289be19SAneesh Kumar K.V                          V9fsPath *dir_path, const char *name, FsCred *credp)
774c494dd6fSAnthony Liguori {
775879c2813SVenkateswararao Jujjuri (JV)     int err = -1;
77638771613SGreg Kurz     int dirfd;
777879c2813SVenkateswararao Jujjuri (JV) 
7787a95434eSGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
7797a95434eSGreg Kurz         local_is_mapped_file_metadata(fs_ctx, name)) {
7807a95434eSGreg Kurz         errno = EINVAL;
7817a95434eSGreg Kurz         return -1;
7827a95434eSGreg Kurz     }
7837a95434eSGreg Kurz 
78438771613SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
78538771613SGreg Kurz     if (dirfd == -1) {
78638771613SGreg Kurz         return -1;
78738771613SGreg Kurz     }
7882289be19SAneesh Kumar K.V 
789879c2813SVenkateswararao Jujjuri (JV)     /* Determine the security model */
79038771613SGreg Kurz     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
79138771613SGreg Kurz         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
79238771613SGreg Kurz         int fd;
79338771613SGreg Kurz         ssize_t oldpath_size, write_size;
79438771613SGreg Kurz 
79538771613SGreg Kurz         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
79638771613SGreg Kurz                          SM_LOCAL_MODE_BITS);
79738771613SGreg Kurz         if (fd == -1) {
79838771613SGreg Kurz             goto out;
79938771613SGreg Kurz         }
80038771613SGreg Kurz         /* Write the oldpath (target) to the file. */
80138771613SGreg Kurz         oldpath_size = strlen(oldpath);
80238771613SGreg Kurz         do {
80338771613SGreg Kurz             write_size = write(fd, (void *)oldpath, oldpath_size);
80438771613SGreg Kurz         } while (write_size == -1 && errno == EINTR);
80538771613SGreg Kurz         close_preserve_errno(fd);
80638771613SGreg Kurz 
80738771613SGreg Kurz         if (write_size != oldpath_size) {
80838771613SGreg Kurz             goto err_end;
80938771613SGreg Kurz         }
81038771613SGreg Kurz         /* Set cleint credentials in symlink's xattr */
81138771613SGreg Kurz         credp->fc_mode = credp->fc_mode | S_IFLNK;
81238771613SGreg Kurz 
813b97400caSAneesh Kumar K.V         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
81438771613SGreg Kurz             err = local_set_xattrat(dirfd, name, credp);
81538771613SGreg Kurz         } else {
81638771613SGreg Kurz             err = local_set_mapped_file_attrat(dirfd, name, credp);
817879c2813SVenkateswararao Jujjuri (JV)         }
818879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
819879c2813SVenkateswararao Jujjuri (JV)             goto err_end;
820879c2813SVenkateswararao Jujjuri (JV)         }
82138771613SGreg Kurz     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
82238771613SGreg Kurz                fs_ctx->export_flags & V9FS_SM_NONE) {
82338771613SGreg Kurz         err = symlinkat(oldpath, dirfd, name);
824879c2813SVenkateswararao Jujjuri (JV)         if (err) {
8252289be19SAneesh Kumar K.V             goto out;
826879c2813SVenkateswararao Jujjuri (JV)         }
82738771613SGreg Kurz         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
82838771613SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
829879c2813SVenkateswararao Jujjuri (JV)         if (err == -1) {
83012848bfcSAneesh Kumar K.V             /*
83112848bfcSAneesh Kumar K.V              * If we fail to change ownership and if we are
83212848bfcSAneesh Kumar K.V              * using security model none. Ignore the error
83312848bfcSAneesh Kumar K.V              */
834b97400caSAneesh Kumar K.V             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
835879c2813SVenkateswararao Jujjuri (JV)                 goto err_end;
83638771613SGreg Kurz             } else {
83712848bfcSAneesh Kumar K.V                 err = 0;
838879c2813SVenkateswararao Jujjuri (JV)             }
839879c2813SVenkateswararao Jujjuri (JV)         }
84038771613SGreg Kurz     }
8412289be19SAneesh Kumar K.V     goto out;
842879c2813SVenkateswararao Jujjuri (JV) 
843879c2813SVenkateswararao Jujjuri (JV) err_end:
84438771613SGreg Kurz     unlinkat_preserve_errno(dirfd, name, 0);
8452289be19SAneesh Kumar K.V out:
84638771613SGreg Kurz     close_preserve_errno(dirfd);
847879c2813SVenkateswararao Jujjuri (JV)     return err;
848c494dd6fSAnthony Liguori }
849c494dd6fSAnthony Liguori 
8502289be19SAneesh Kumar K.V static int local_link(FsContext *ctx, V9fsPath *oldpath,
8512289be19SAneesh Kumar K.V                       V9fsPath *dirpath, const char *name)
852c494dd6fSAnthony Liguori {
853ad0b46e6SGreg Kurz     char *odirpath = g_path_get_dirname(oldpath->data);
854ad0b46e6SGreg Kurz     char *oname = g_path_get_basename(oldpath->data);
855ad0b46e6SGreg Kurz     int ret = -1;
856ad0b46e6SGreg Kurz     int odirfd, ndirfd;
857c494dd6fSAnthony Liguori 
8587a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
8597a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
8607a95434eSGreg Kurz         errno = EINVAL;
8617a95434eSGreg Kurz         return -1;
8627a95434eSGreg Kurz     }
8637a95434eSGreg Kurz 
864ad0b46e6SGreg Kurz     odirfd = local_opendir_nofollow(ctx, odirpath);
865ad0b46e6SGreg Kurz     if (odirfd == -1) {
8666dd4b1f1SGreg Kurz         goto out;
8676dd4b1f1SGreg Kurz     }
8682289be19SAneesh Kumar K.V 
869ad0b46e6SGreg Kurz     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
870ad0b46e6SGreg Kurz     if (ndirfd == -1) {
871ad0b46e6SGreg Kurz         close_preserve_errno(odirfd);
872ad0b46e6SGreg Kurz         goto out;
873ad0b46e6SGreg Kurz     }
874ad0b46e6SGreg Kurz 
875ad0b46e6SGreg Kurz     ret = linkat(odirfd, oname, ndirfd, name, 0);
876ad0b46e6SGreg Kurz     if (ret < 0) {
877ad0b46e6SGreg Kurz         goto out_close;
878ad0b46e6SGreg Kurz     }
8792c30dd74SAneesh Kumar K.V 
8802c30dd74SAneesh Kumar K.V     /* now link the virtfs_metadata files */
8816dd4b1f1SGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
882ad0b46e6SGreg Kurz         int omap_dirfd, nmap_dirfd;
8836dd4b1f1SGreg Kurz 
884ad0b46e6SGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
885ad0b46e6SGreg Kurz         if (ret < 0 && errno != EEXIST) {
886ad0b46e6SGreg Kurz             goto err_undo_link;
8872c30dd74SAneesh Kumar K.V         }
888ad0b46e6SGreg Kurz 
889ad0b46e6SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
890ad0b46e6SGreg Kurz         if (omap_dirfd == -1) {
891ad0b46e6SGreg Kurz             goto err;
892ad0b46e6SGreg Kurz         }
893ad0b46e6SGreg Kurz 
894ad0b46e6SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
895ad0b46e6SGreg Kurz         if (nmap_dirfd == -1) {
896ad0b46e6SGreg Kurz             close_preserve_errno(omap_dirfd);
897ad0b46e6SGreg Kurz             goto err;
898ad0b46e6SGreg Kurz         }
899ad0b46e6SGreg Kurz 
900ad0b46e6SGreg Kurz         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
901ad0b46e6SGreg Kurz         close_preserve_errno(nmap_dirfd);
902ad0b46e6SGreg Kurz         close_preserve_errno(omap_dirfd);
9032c30dd74SAneesh Kumar K.V         if (ret < 0 && errno != ENOENT) {
904ad0b46e6SGreg Kurz             goto err_undo_link;
9052c30dd74SAneesh Kumar K.V         }
9066dd4b1f1SGreg Kurz 
907ad0b46e6SGreg Kurz         ret = 0;
9082c30dd74SAneesh Kumar K.V     }
909ad0b46e6SGreg Kurz     goto out_close;
910ad0b46e6SGreg Kurz 
911ad0b46e6SGreg Kurz err:
912ad0b46e6SGreg Kurz     ret = -1;
913ad0b46e6SGreg Kurz err_undo_link:
914ad0b46e6SGreg Kurz     unlinkat_preserve_errno(ndirfd, name, 0);
915ad0b46e6SGreg Kurz out_close:
916ad0b46e6SGreg Kurz     close_preserve_errno(ndirfd);
917ad0b46e6SGreg Kurz     close_preserve_errno(odirfd);
9186dd4b1f1SGreg Kurz out:
919ad0b46e6SGreg Kurz     g_free(oname);
920ad0b46e6SGreg Kurz     g_free(odirpath);
9212289be19SAneesh Kumar K.V     return ret;
922c494dd6fSAnthony Liguori }
923c494dd6fSAnthony Liguori 
9242289be19SAneesh Kumar K.V static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
9258cf89e00SAnthony Liguori {
926ac125d99SGreg Kurz     int fd, ret;
9272289be19SAneesh Kumar K.V 
928ac125d99SGreg Kurz     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
929ac125d99SGreg Kurz     if (fd == -1) {
930ac125d99SGreg Kurz         return -1;
931ac125d99SGreg Kurz     }
932ac125d99SGreg Kurz     ret = ftruncate(fd, size);
933ac125d99SGreg Kurz     close_preserve_errno(fd);
9344fa4ce71SChen Gang     return ret;
9358cf89e00SAnthony Liguori }
9368cf89e00SAnthony Liguori 
9372289be19SAneesh Kumar K.V static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
9388cf89e00SAnthony Liguori {
939d369f207SGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
940d369f207SGreg Kurz     char *name = g_path_get_basename(fs_path->data);
9414fa4ce71SChen Gang     int ret = -1;
942d369f207SGreg Kurz     int dirfd;
943d369f207SGreg Kurz 
944d369f207SGreg Kurz     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
945d369f207SGreg Kurz     if (dirfd == -1) {
946d369f207SGreg Kurz         goto out;
947d369f207SGreg Kurz     }
9482289be19SAneesh Kumar K.V 
949c79ce737SSripathi Kodi     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
95017b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
95117b1971fSAneesh Kumar K.V         (fs_ctx->export_flags & V9FS_SM_NONE)) {
952d369f207SGreg Kurz         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
953d369f207SGreg Kurz                        AT_SYMLINK_NOFOLLOW);
954b97400caSAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
955d369f207SGreg Kurz         ret = local_set_xattrat(dirfd, name, credp);
9562c30dd74SAneesh Kumar K.V     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
957d369f207SGreg Kurz         ret = local_set_mapped_file_attrat(dirfd, name, credp);
958f7613beeSVenkateswararao Jujjuri (JV)     }
959d369f207SGreg Kurz 
960d369f207SGreg Kurz     close_preserve_errno(dirfd);
961d369f207SGreg Kurz out:
962d369f207SGreg Kurz     g_free(name);
963d369f207SGreg Kurz     g_free(dirpath);
9644fa4ce71SChen Gang     return ret;
9658cf89e00SAnthony Liguori }
9668cf89e00SAnthony Liguori 
9672289be19SAneesh Kumar K.V static int local_utimensat(FsContext *s, V9fsPath *fs_path,
96874bc02b2SM. Mohan Kumar                            const struct timespec *buf)
9698cf89e00SAnthony Liguori {
970a33eda0dSGreg Kurz     char *dirpath = g_path_get_dirname(fs_path->data);
971a33eda0dSGreg Kurz     char *name = g_path_get_basename(fs_path->data);
972a33eda0dSGreg Kurz     int dirfd, ret = -1;
9732289be19SAneesh Kumar K.V 
974a33eda0dSGreg Kurz     dirfd = local_opendir_nofollow(s, dirpath);
975a33eda0dSGreg Kurz     if (dirfd == -1) {
976a33eda0dSGreg Kurz         goto out;
977a33eda0dSGreg Kurz     }
978a33eda0dSGreg Kurz 
979a33eda0dSGreg Kurz     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
980a33eda0dSGreg Kurz     close_preserve_errno(dirfd);
981a33eda0dSGreg Kurz out:
982a33eda0dSGreg Kurz     g_free(dirpath);
983a33eda0dSGreg Kurz     g_free(name);
9844fa4ce71SChen Gang     return ret;
9858cf89e00SAnthony Liguori }
9868cf89e00SAnthony Liguori 
987df4938a6SGreg Kurz static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
988df4938a6SGreg Kurz                                  int flags)
9895bae1900SAnthony Liguori {
990df4938a6SGreg Kurz     int ret = -1;
9912c30dd74SAneesh Kumar K.V 
9922c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
993df4938a6SGreg Kurz         int map_dirfd;
994df4938a6SGreg Kurz 
995*6a87e792SGreg Kurz         /* We need to remove the metadata as well:
996*6a87e792SGreg Kurz          * - the metadata directory if we're removing a directory
997*6a87e792SGreg Kurz          * - the metadata file in the parent's metadata directory
998*6a87e792SGreg Kurz          *
999*6a87e792SGreg Kurz          * If any of these are missing (ie, ENOENT) then we're probably
1000*6a87e792SGreg Kurz          * trying to remove something that wasn't created in mapped-file
1001*6a87e792SGreg Kurz          * mode. We just ignore the error.
1002*6a87e792SGreg Kurz          */
1003df4938a6SGreg Kurz         if (flags == AT_REMOVEDIR) {
1004df4938a6SGreg Kurz             int fd;
1005df4938a6SGreg Kurz 
1006b003fc0dSGreg Kurz             fd = openat_dir(dirfd, name);
1007df4938a6SGreg Kurz             if (fd == -1) {
10082c30dd74SAneesh Kumar K.V                 goto err_out;
10092c30dd74SAneesh Kumar K.V             }
1010df4938a6SGreg Kurz             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1011df4938a6SGreg Kurz             close_preserve_errno(fd);
1012df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
10132c30dd74SAneesh Kumar K.V                 goto err_out;
10142c30dd74SAneesh Kumar K.V             }
10152c30dd74SAneesh Kumar K.V         }
1016df4938a6SGreg Kurz         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1017*6a87e792SGreg Kurz         if (map_dirfd != -1) {
1018df4938a6SGreg Kurz             ret = unlinkat(map_dirfd, name, 0);
1019df4938a6SGreg Kurz             close_preserve_errno(map_dirfd);
1020df4938a6SGreg Kurz             if (ret < 0 && errno != ENOENT) {
1021*6a87e792SGreg Kurz                 goto err_out;
1022*6a87e792SGreg Kurz             }
1023*6a87e792SGreg Kurz         } else if (errno != ENOENT) {
10242c30dd74SAneesh Kumar K.V             goto err_out;
10252c30dd74SAneesh Kumar K.V         }
10262c30dd74SAneesh Kumar K.V     }
10274fa4ce71SChen Gang 
1028df4938a6SGreg Kurz     ret = unlinkat(dirfd, name, flags);
10292c30dd74SAneesh Kumar K.V err_out:
1030df4938a6SGreg Kurz     return ret;
1031df4938a6SGreg Kurz }
1032df4938a6SGreg Kurz 
10338cf89e00SAnthony Liguori static int local_remove(FsContext *ctx, const char *path)
10348cf89e00SAnthony Liguori {
10358cf89e00SAnthony Liguori     struct stat stbuf;
1036a0e640a8SGreg Kurz     char *dirpath = g_path_get_dirname(path);
1037a0e640a8SGreg Kurz     char *name = g_path_get_basename(path);
1038a0e640a8SGreg Kurz     int flags = 0;
1039a0e640a8SGreg Kurz     int dirfd;
1040a0e640a8SGreg Kurz     int err = -1;
10418cf89e00SAnthony Liguori 
1042a0e640a8SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dirpath);
1043b7361d46SGreg Kurz     if (dirfd == -1) {
1044a0e640a8SGreg Kurz         goto out;
1045a0e640a8SGreg Kurz     }
1046a0e640a8SGreg Kurz 
1047a0e640a8SGreg Kurz     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
10488cf89e00SAnthony Liguori         goto err_out;
10498cf89e00SAnthony Liguori     }
1050a0e640a8SGreg Kurz 
10518cf89e00SAnthony Liguori     if (S_ISDIR(stbuf.st_mode)) {
1052a0e640a8SGreg Kurz         flags |= AT_REMOVEDIR;
10538cf89e00SAnthony Liguori     }
10545bae1900SAnthony Liguori 
1055a0e640a8SGreg Kurz     err = local_unlinkat_common(ctx, dirfd, name, flags);
10562c30dd74SAneesh Kumar K.V err_out:
1057a0e640a8SGreg Kurz     close_preserve_errno(dirfd);
1058a0e640a8SGreg Kurz out:
1059a0e640a8SGreg Kurz     g_free(name);
1060a0e640a8SGreg Kurz     g_free(dirpath);
10612c30dd74SAneesh Kumar K.V     return err;
10625bae1900SAnthony Liguori }
10635bae1900SAnthony Liguori 
10648b888272SAneesh Kumar K.V static int local_fsync(FsContext *ctx, int fid_type,
10658b888272SAneesh Kumar K.V                        V9fsFidOpenState *fs, int datasync)
10668cf89e00SAnthony Liguori {
10678b888272SAneesh Kumar K.V     int fd;
10688b888272SAneesh Kumar K.V 
10698b888272SAneesh Kumar K.V     if (fid_type == P9_FID_DIR) {
1070f314ea4eSGreg Kurz         fd = dirfd(fs->dir.stream);
107149594973SVenkateswararao Jujjuri (JV)     } else {
10728b888272SAneesh Kumar K.V         fd = fs->fd;
10738b888272SAneesh Kumar K.V     }
10748b888272SAneesh Kumar K.V 
10758b888272SAneesh Kumar K.V     if (datasync) {
10768b888272SAneesh Kumar K.V         return qemu_fdatasync(fd);
10778b888272SAneesh Kumar K.V     } else {
10788b888272SAneesh Kumar K.V         return fsync(fd);
10798cf89e00SAnthony Liguori     }
108049594973SVenkateswararao Jujjuri (JV) }
10818cf89e00SAnthony Liguori 
10822289be19SAneesh Kumar K.V static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1083be940c87SM. Mohan Kumar {
108431e51d1cSGreg Kurz     int fd, ret;
10852289be19SAneesh Kumar K.V 
108631e51d1cSGreg Kurz     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
108723da0145SGreg Kurz     if (fd == -1) {
108823da0145SGreg Kurz         return -1;
108923da0145SGreg Kurz     }
109031e51d1cSGreg Kurz     ret = fstatfs(fd, stbuf);
109131e51d1cSGreg Kurz     close_preserve_errno(fd);
10924fa4ce71SChen Gang     return ret;
1093be940c87SM. Mohan Kumar }
1094be940c87SM. Mohan Kumar 
10952289be19SAneesh Kumar K.V static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1096fa32ef88SAneesh Kumar K.V                                const char *name, void *value, size_t size)
1097fa32ef88SAneesh Kumar K.V {
10982289be19SAneesh Kumar K.V     char *path = fs_path->data;
10992289be19SAneesh Kumar K.V 
1100fc22118dSAneesh Kumar K.V     return v9fs_get_xattr(ctx, path, name, value, size);
1101fa32ef88SAneesh Kumar K.V }
1102fa32ef88SAneesh Kumar K.V 
11032289be19SAneesh Kumar K.V static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1104fa32ef88SAneesh Kumar K.V                                 void *value, size_t size)
1105fa32ef88SAneesh Kumar K.V {
11062289be19SAneesh Kumar K.V     char *path = fs_path->data;
11072289be19SAneesh Kumar K.V 
1108fc22118dSAneesh Kumar K.V     return v9fs_list_xattr(ctx, path, value, size);
110961b6c499SAneesh Kumar K.V }
111061b6c499SAneesh Kumar K.V 
11112289be19SAneesh Kumar K.V static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
111210b468bdSAneesh Kumar K.V                            void *value, size_t size, int flags)
111310b468bdSAneesh Kumar K.V {
11142289be19SAneesh Kumar K.V     char *path = fs_path->data;
11152289be19SAneesh Kumar K.V 
1116fc22118dSAneesh Kumar K.V     return v9fs_set_xattr(ctx, path, name, value, size, flags);
111710b468bdSAneesh Kumar K.V }
111810b468bdSAneesh Kumar K.V 
11192289be19SAneesh Kumar K.V static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
11202289be19SAneesh Kumar K.V                               const char *name)
11219ed3ef26SAneesh Kumar K.V {
11222289be19SAneesh Kumar K.V     char *path = fs_path->data;
11232289be19SAneesh Kumar K.V 
1124fc22118dSAneesh Kumar K.V     return v9fs_remove_xattr(ctx, path, name);
11259ed3ef26SAneesh Kumar K.V }
11269ed3ef26SAneesh Kumar K.V 
11272289be19SAneesh Kumar K.V static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
11282289be19SAneesh Kumar K.V                               const char *name, V9fsPath *target)
11292289be19SAneesh Kumar K.V {
11307a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
11317a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
11327a95434eSGreg Kurz         errno = EINVAL;
11337a95434eSGreg Kurz         return -1;
11347a95434eSGreg Kurz     }
11357a95434eSGreg Kurz 
11362289be19SAneesh Kumar K.V     if (dir_path) {
1137e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
11389c6b899fSGreg Kurz     } else if (strcmp(name, "/")) {
1139e3e83f2eSGreg Kurz         v9fs_path_sprintf(target, "%s", name);
11409c6b899fSGreg Kurz     } else {
11419c6b899fSGreg Kurz         /* We want the path of the export root to be relative, otherwise
11429c6b899fSGreg Kurz          * "*at()" syscalls would treat it as "/" in the host.
11439c6b899fSGreg Kurz          */
11449c6b899fSGreg Kurz         v9fs_path_sprintf(target, "%s", ".");
11452289be19SAneesh Kumar K.V     }
11462289be19SAneesh Kumar K.V     return 0;
11472289be19SAneesh Kumar K.V }
11482289be19SAneesh Kumar K.V 
11492289be19SAneesh Kumar K.V static int local_renameat(FsContext *ctx, V9fsPath *olddir,
11502289be19SAneesh Kumar K.V                           const char *old_name, V9fsPath *newdir,
11512289be19SAneesh Kumar K.V                           const char *new_name)
11522289be19SAneesh Kumar K.V {
11532289be19SAneesh Kumar K.V     int ret;
115499f2cf4bSGreg Kurz     int odirfd, ndirfd;
11552289be19SAneesh Kumar K.V 
11567a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
11577a95434eSGreg Kurz         (local_is_mapped_file_metadata(ctx, old_name) ||
11587a95434eSGreg Kurz          local_is_mapped_file_metadata(ctx, new_name))) {
11597a95434eSGreg Kurz         errno = EINVAL;
11607a95434eSGreg Kurz         return -1;
11617a95434eSGreg Kurz     }
11627a95434eSGreg Kurz 
116399f2cf4bSGreg Kurz     odirfd = local_opendir_nofollow(ctx, olddir->data);
116499f2cf4bSGreg Kurz     if (odirfd == -1) {
116599f2cf4bSGreg Kurz         return -1;
116699f2cf4bSGreg Kurz     }
11672289be19SAneesh Kumar K.V 
116899f2cf4bSGreg Kurz     ndirfd = local_opendir_nofollow(ctx, newdir->data);
116999f2cf4bSGreg Kurz     if (ndirfd == -1) {
117099f2cf4bSGreg Kurz         close_preserve_errno(odirfd);
117199f2cf4bSGreg Kurz         return -1;
117299f2cf4bSGreg Kurz     }
11732289be19SAneesh Kumar K.V 
117499f2cf4bSGreg Kurz     ret = renameat(odirfd, old_name, ndirfd, new_name);
117599f2cf4bSGreg Kurz     if (ret < 0) {
117699f2cf4bSGreg Kurz         goto out;
117799f2cf4bSGreg Kurz     }
117899f2cf4bSGreg Kurz 
117999f2cf4bSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
118099f2cf4bSGreg Kurz         int omap_dirfd, nmap_dirfd;
118199f2cf4bSGreg Kurz 
118299f2cf4bSGreg Kurz         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
118399f2cf4bSGreg Kurz         if (ret < 0 && errno != EEXIST) {
118499f2cf4bSGreg Kurz             goto err_undo_rename;
118599f2cf4bSGreg Kurz         }
118699f2cf4bSGreg Kurz 
11876dd4b1f1SGreg Kurz         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
118899f2cf4bSGreg Kurz         if (omap_dirfd == -1) {
118999f2cf4bSGreg Kurz             goto err;
119099f2cf4bSGreg Kurz         }
119199f2cf4bSGreg Kurz 
11926dd4b1f1SGreg Kurz         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
119399f2cf4bSGreg Kurz         if (nmap_dirfd == -1) {
119499f2cf4bSGreg Kurz             close_preserve_errno(omap_dirfd);
119599f2cf4bSGreg Kurz             goto err;
119699f2cf4bSGreg Kurz         }
119799f2cf4bSGreg Kurz 
119899f2cf4bSGreg Kurz         /* rename the .virtfs_metadata files */
119999f2cf4bSGreg Kurz         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
120099f2cf4bSGreg Kurz         close_preserve_errno(nmap_dirfd);
120199f2cf4bSGreg Kurz         close_preserve_errno(omap_dirfd);
120299f2cf4bSGreg Kurz         if (ret < 0 && errno != ENOENT) {
120399f2cf4bSGreg Kurz             goto err_undo_rename;
120499f2cf4bSGreg Kurz         }
120599f2cf4bSGreg Kurz 
120699f2cf4bSGreg Kurz         ret = 0;
120799f2cf4bSGreg Kurz     }
120899f2cf4bSGreg Kurz     goto out;
120999f2cf4bSGreg Kurz 
121099f2cf4bSGreg Kurz err:
121199f2cf4bSGreg Kurz     ret = -1;
121299f2cf4bSGreg Kurz err_undo_rename:
121399f2cf4bSGreg Kurz     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
121499f2cf4bSGreg Kurz out:
121599f2cf4bSGreg Kurz     close_preserve_errno(ndirfd);
121699f2cf4bSGreg Kurz     close_preserve_errno(odirfd);
12172289be19SAneesh Kumar K.V     return ret;
12182289be19SAneesh Kumar K.V }
12192289be19SAneesh Kumar K.V 
1220d2767edeSGreg Kurz static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1221d2767edeSGreg Kurz {
1222d2767edeSGreg Kurz     path->data = g_path_get_dirname(str);
1223d2767edeSGreg Kurz     path->size = strlen(path->data) + 1;
1224d2767edeSGreg Kurz }
1225d2767edeSGreg Kurz 
1226d2767edeSGreg Kurz static int local_rename(FsContext *ctx, const char *oldpath,
1227d2767edeSGreg Kurz                         const char *newpath)
1228d2767edeSGreg Kurz {
1229d2767edeSGreg Kurz     int err;
1230d2767edeSGreg Kurz     char *oname = g_path_get_basename(oldpath);
1231d2767edeSGreg Kurz     char *nname = g_path_get_basename(newpath);
1232d2767edeSGreg Kurz     V9fsPath olddir, newdir;
1233d2767edeSGreg Kurz 
1234d2767edeSGreg Kurz     v9fs_path_init_dirname(&olddir, oldpath);
1235d2767edeSGreg Kurz     v9fs_path_init_dirname(&newdir, newpath);
1236d2767edeSGreg Kurz 
1237d2767edeSGreg Kurz     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1238d2767edeSGreg Kurz 
1239d2767edeSGreg Kurz     v9fs_path_free(&newdir);
1240d2767edeSGreg Kurz     v9fs_path_free(&olddir);
1241d2767edeSGreg Kurz     g_free(nname);
1242d2767edeSGreg Kurz     g_free(oname);
1243d2767edeSGreg Kurz 
1244d2767edeSGreg Kurz     return err;
1245d2767edeSGreg Kurz }
1246d2767edeSGreg Kurz 
12472289be19SAneesh Kumar K.V static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
12482289be19SAneesh Kumar K.V                           const char *name, int flags)
12492289be19SAneesh Kumar K.V {
12502289be19SAneesh Kumar K.V     int ret;
1251df4938a6SGreg Kurz     int dirfd;
12522c30dd74SAneesh Kumar K.V 
12537a95434eSGreg Kurz     if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
12547a95434eSGreg Kurz         local_is_mapped_file_metadata(ctx, name)) {
12557a95434eSGreg Kurz         errno = EINVAL;
12567a95434eSGreg Kurz         return -1;
12577a95434eSGreg Kurz     }
12587a95434eSGreg Kurz 
1259df4938a6SGreg Kurz     dirfd = local_opendir_nofollow(ctx, dir->data);
1260df4938a6SGreg Kurz     if (dirfd == -1) {
1261df4938a6SGreg Kurz         return -1;
1262df4938a6SGreg Kurz     }
12632289be19SAneesh Kumar K.V 
1264df4938a6SGreg Kurz     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1265df4938a6SGreg Kurz     close_preserve_errno(dirfd);
12662289be19SAneesh Kumar K.V     return ret;
12672289be19SAneesh Kumar K.V }
12689ed3ef26SAneesh Kumar K.V 
1269e06a765eSHarsh Prateek Bora static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1270e06a765eSHarsh Prateek Bora                                 mode_t st_mode, uint64_t *st_gen)
1271e06a765eSHarsh Prateek Bora {
1272ae0f940eSPaolo Bonzini #ifdef FS_IOC_GETVERSION
12730e5fc994SKirill A. Shutemov     int err;
1274cc720ddbSAneesh Kumar K.V     V9fsFidOpenState fid_open;
1275cc720ddbSAneesh Kumar K.V 
1276e06a765eSHarsh Prateek Bora     /*
1277e06a765eSHarsh Prateek Bora      * Do not try to open special files like device nodes, fifos etc
1278e06a765eSHarsh Prateek Bora      * We can get fd for regular files and directories only
1279e06a765eSHarsh Prateek Bora      */
1280e06a765eSHarsh Prateek Bora     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
12811a9978a5SKirill A. Shutemov         errno = ENOTTY;
12821a9978a5SKirill A. Shutemov         return -1;
1283e06a765eSHarsh Prateek Bora     }
1284cc720ddbSAneesh Kumar K.V     err = local_open(ctx, path, O_RDONLY, &fid_open);
1285cc720ddbSAneesh Kumar K.V     if (err < 0) {
1286cc720ddbSAneesh Kumar K.V         return err;
1287e06a765eSHarsh Prateek Bora     }
1288cc720ddbSAneesh Kumar K.V     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1289cc720ddbSAneesh Kumar K.V     local_close(ctx, &fid_open);
1290e06a765eSHarsh Prateek Bora     return err;
12910e5fc994SKirill A. Shutemov #else
12920e5fc994SKirill A. Shutemov     errno = ENOTTY;
12930e5fc994SKirill A. Shutemov     return -1;
12940e5fc994SKirill A. Shutemov #endif
1295e06a765eSHarsh Prateek Bora }
1296e06a765eSHarsh Prateek Bora 
12970174fe73SAneesh Kumar K.V static int local_init(FsContext *ctx)
12980174fe73SAneesh Kumar K.V {
1299e06a765eSHarsh Prateek Bora     struct statfs stbuf;
13000e35a378SGreg Kurz     LocalData *data = g_malloc(sizeof(*data));
13010e35a378SGreg Kurz 
13020e35a378SGreg Kurz     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
13030e35a378SGreg Kurz     if (data->mountfd == -1) {
13040e35a378SGreg Kurz         goto err;
13050e35a378SGreg Kurz     }
1306e06a765eSHarsh Prateek Bora 
130700c90bd1SGreg Kurz #ifdef FS_IOC_GETVERSION
130800c90bd1SGreg Kurz     /*
130900c90bd1SGreg Kurz      * use ioc_getversion only if the ioctl is definied
131000c90bd1SGreg Kurz      */
13110e35a378SGreg Kurz     if (fstatfs(data->mountfd, &stbuf) < 0) {
13120e35a378SGreg Kurz         close_preserve_errno(data->mountfd);
13130e35a378SGreg Kurz         goto err;
131400c90bd1SGreg Kurz     }
131500c90bd1SGreg Kurz     switch (stbuf.f_type) {
131600c90bd1SGreg Kurz     case EXT2_SUPER_MAGIC:
131700c90bd1SGreg Kurz     case BTRFS_SUPER_MAGIC:
131800c90bd1SGreg Kurz     case REISERFS_SUPER_MAGIC:
131900c90bd1SGreg Kurz     case XFS_SUPER_MAGIC:
132000c90bd1SGreg Kurz         ctx->exops.get_st_gen = local_ioc_getversion;
132100c90bd1SGreg Kurz         break;
132200c90bd1SGreg Kurz     }
132300c90bd1SGreg Kurz #endif
13240174fe73SAneesh Kumar K.V 
13252c30dd74SAneesh Kumar K.V     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
13262c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13272c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
13282c30dd74SAneesh Kumar K.V         ctx->xops = mapped_xattr_ops;
13292c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_NONE) {
13302c30dd74SAneesh Kumar K.V         ctx->xops = none_xattr_ops;
13312c30dd74SAneesh Kumar K.V     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
13322c30dd74SAneesh Kumar K.V         /*
13332c30dd74SAneesh Kumar K.V          * xattr operation for mapped-file and passthrough
13342c30dd74SAneesh Kumar K.V          * remain same.
13352c30dd74SAneesh Kumar K.V          */
13362c30dd74SAneesh Kumar K.V         ctx->xops = passthrough_xattr_ops;
13372c30dd74SAneesh Kumar K.V     }
1338c98f1d4aSAneesh Kumar K.V     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
133900c90bd1SGreg Kurz 
13400e35a378SGreg Kurz     ctx->private = data;
134100c90bd1SGreg Kurz     return 0;
13420e35a378SGreg Kurz 
13430e35a378SGreg Kurz err:
13440e35a378SGreg Kurz     g_free(data);
13450e35a378SGreg Kurz     return -1;
1346e06a765eSHarsh Prateek Bora }
13470e35a378SGreg Kurz 
13480e35a378SGreg Kurz static void local_cleanup(FsContext *ctx)
13490e35a378SGreg Kurz {
13500e35a378SGreg Kurz     LocalData *data = ctx->private;
13510e35a378SGreg Kurz 
13520e35a378SGreg Kurz     close(data->mountfd);
13530e35a378SGreg Kurz     g_free(data);
13540174fe73SAneesh Kumar K.V }
13550174fe73SAneesh Kumar K.V 
135699519f0aSAneesh Kumar K.V static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
135799519f0aSAneesh Kumar K.V {
135899519f0aSAneesh Kumar K.V     const char *sec_model = qemu_opt_get(opts, "security_model");
135999519f0aSAneesh Kumar K.V     const char *path = qemu_opt_get(opts, "path");
1360b8bbdb88SPradeep Jagadeesh     Error *err = NULL;
136199519f0aSAneesh Kumar K.V 
136299519f0aSAneesh Kumar K.V     if (!sec_model) {
136363325b18SGreg Kurz         error_report("Security model not specified, local fs needs security model");
136463325b18SGreg Kurz         error_printf("valid options are:"
136563325b18SGreg Kurz                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
136699519f0aSAneesh Kumar K.V         return -1;
136799519f0aSAneesh Kumar K.V     }
136899519f0aSAneesh Kumar K.V 
136999519f0aSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
137099519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_PASSTHROUGH;
13712c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped") ||
13722c30dd74SAneesh Kumar K.V                !strcmp(sec_model, "mapped-xattr")) {
137399519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED;
137499519f0aSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
137599519f0aSAneesh Kumar K.V         fse->export_flags |= V9FS_SM_NONE;
13762c30dd74SAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped-file")) {
13772c30dd74SAneesh Kumar K.V         fse->export_flags |= V9FS_SM_MAPPED_FILE;
137899519f0aSAneesh Kumar K.V     } else {
137963325b18SGreg Kurz         error_report("Invalid security model %s specified", sec_model);
138063325b18SGreg Kurz         error_printf("valid options are:"
138163325b18SGreg Kurz                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
138299519f0aSAneesh Kumar K.V         return -1;
138399519f0aSAneesh Kumar K.V     }
138499519f0aSAneesh Kumar K.V 
138599519f0aSAneesh Kumar K.V     if (!path) {
138663325b18SGreg Kurz         error_report("fsdev: No path specified");
138799519f0aSAneesh Kumar K.V         return -1;
138899519f0aSAneesh Kumar K.V     }
1389b8bbdb88SPradeep Jagadeesh 
1390b8bbdb88SPradeep Jagadeesh     fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1391b8bbdb88SPradeep Jagadeesh     if (err) {
1392b8bbdb88SPradeep Jagadeesh         error_reportf_err(err, "Throttle configuration is not valid: ");
1393b8bbdb88SPradeep Jagadeesh         return -1;
1394b8bbdb88SPradeep Jagadeesh     }
1395b8bbdb88SPradeep Jagadeesh 
139699519f0aSAneesh Kumar K.V     fse->path = g_strdup(path);
139799519f0aSAneesh Kumar K.V 
139899519f0aSAneesh Kumar K.V     return 0;
139999519f0aSAneesh Kumar K.V }
140099519f0aSAneesh Kumar K.V 
14019f107513SAnthony Liguori FileOperations local_ops = {
140299519f0aSAneesh Kumar K.V     .parse_opts = local_parse_opts,
14030174fe73SAneesh Kumar K.V     .init  = local_init,
14040e35a378SGreg Kurz     .cleanup = local_cleanup,
1405131dcb25SAnthony Liguori     .lstat = local_lstat,
1406131dcb25SAnthony Liguori     .readlink = local_readlink,
1407131dcb25SAnthony Liguori     .close = local_close,
1408131dcb25SAnthony Liguori     .closedir = local_closedir,
1409a6568fe2SAnthony Liguori     .open = local_open,
1410a6568fe2SAnthony Liguori     .opendir = local_opendir,
1411a9231555SAnthony Liguori     .rewinddir = local_rewinddir,
1412a9231555SAnthony Liguori     .telldir = local_telldir,
1413635324e8SGreg Kurz     .readdir = local_readdir,
1414a9231555SAnthony Liguori     .seekdir = local_seekdir,
141556d15a53SSanchit Garg     .preadv = local_preadv,
141656d15a53SSanchit Garg     .pwritev = local_pwritev,
1417c494dd6fSAnthony Liguori     .chmod = local_chmod,
1418c494dd6fSAnthony Liguori     .mknod = local_mknod,
1419c494dd6fSAnthony Liguori     .mkdir = local_mkdir,
1420c494dd6fSAnthony Liguori     .fstat = local_fstat,
1421c494dd6fSAnthony Liguori     .open2 = local_open2,
1422c494dd6fSAnthony Liguori     .symlink = local_symlink,
1423c494dd6fSAnthony Liguori     .link = local_link,
14248cf89e00SAnthony Liguori     .truncate = local_truncate,
14258cf89e00SAnthony Liguori     .rename = local_rename,
14268cf89e00SAnthony Liguori     .chown = local_chown,
142774bc02b2SM. Mohan Kumar     .utimensat = local_utimensat,
14285bae1900SAnthony Liguori     .remove = local_remove,
14298cf89e00SAnthony Liguori     .fsync = local_fsync,
1430be940c87SM. Mohan Kumar     .statfs = local_statfs,
1431fa32ef88SAneesh Kumar K.V     .lgetxattr = local_lgetxattr,
1432fa32ef88SAneesh Kumar K.V     .llistxattr = local_llistxattr,
143310b468bdSAneesh Kumar K.V     .lsetxattr = local_lsetxattr,
14349ed3ef26SAneesh Kumar K.V     .lremovexattr = local_lremovexattr,
14352289be19SAneesh Kumar K.V     .name_to_path = local_name_to_path,
14362289be19SAneesh Kumar K.V     .renameat  = local_renameat,
14372289be19SAneesh Kumar K.V     .unlinkat = local_unlinkat,
14389f107513SAnthony Liguori };
1439