xref: /qemu/hw/9pfs/9p-local.c (revision d815e7219036d6911fce12efe3e59906264c8536)
1 /*
2  * 9p Posix callback
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "9p.h"
16 #include "9p-local.h"
17 #include "9p-xattr.h"
18 #include "9p-util.h"
19 #include "fsdev/qemu-fsdev.h"   /* local_ops */
20 #include <arpa/inet.h>
21 #include <pwd.h>
22 #include <grp.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include "qemu/xattr.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
28 #include <libgen.h>
29 #include <linux/fs.h>
30 #ifdef CONFIG_LINUX_MAGIC_H
31 #include <linux/magic.h>
32 #endif
33 #include <sys/ioctl.h>
34 
35 #ifndef XFS_SUPER_MAGIC
36 #define XFS_SUPER_MAGIC  0x58465342
37 #endif
38 #ifndef EXT2_SUPER_MAGIC
39 #define EXT2_SUPER_MAGIC 0xEF53
40 #endif
41 #ifndef REISERFS_SUPER_MAGIC
42 #define REISERFS_SUPER_MAGIC 0x52654973
43 #endif
44 #ifndef BTRFS_SUPER_MAGIC
45 #define BTRFS_SUPER_MAGIC 0x9123683E
46 #endif
47 
48 typedef struct {
49     int mountfd;
50 } LocalData;
51 
52 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53                         mode_t mode)
54 {
55     LocalData *data = fs_ctx->private;
56 
57     /* All paths are relative to the path data->mountfd points to */
58     while (*path == '/') {
59         path++;
60     }
61 
62     return relative_openat_nofollow(data->mountfd, path, flags, mode);
63 }
64 
65 int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66 {
67     return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68 }
69 
70 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
71                                     const char *npath)
72 {
73     int serrno = errno;
74     renameat(odirfd, opath, ndirfd, npath);
75     errno = serrno;
76 }
77 
78 static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
79 {
80     int serrno = errno;
81     unlinkat(dirfd, path, flags);
82     errno = serrno;
83 }
84 
85 #define VIRTFS_META_DIR ".virtfs_metadata"
86 
87 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
88 {
89     int dirlen;
90     const char *name = strrchr(path, '/');
91     if (name) {
92         dirlen = name - path;
93         ++name;
94     } else {
95         name = path;
96         dirlen = 0;
97     }
98     return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
99                            dirlen, path, VIRTFS_META_DIR, name);
100 }
101 
102 static FILE *local_fopen(const char *path, const char *mode)
103 {
104     int fd, o_mode = 0;
105     FILE *fp;
106     int flags = O_NOFOLLOW;
107     /*
108      * only supports two modes
109      */
110     if (mode[0] == 'r') {
111         flags |= O_RDONLY;
112     } else if (mode[0] == 'w') {
113         flags |= O_WRONLY | O_TRUNC | O_CREAT;
114         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
115     } else {
116         return NULL;
117     }
118     fd = open(path, flags, o_mode);
119     if (fd == -1) {
120         return NULL;
121     }
122     fp = fdopen(fd, mode);
123     if (!fp) {
124         close(fd);
125     }
126     return fp;
127 }
128 
129 static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
130 {
131     int fd, o_mode = 0;
132     FILE *fp;
133     int flags;
134     /*
135      * only supports two modes
136      */
137     if (mode[0] == 'r') {
138         flags = O_RDONLY;
139     } else if (mode[0] == 'w') {
140         flags = O_WRONLY | O_TRUNC | O_CREAT;
141         o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
142     } else {
143         return NULL;
144     }
145     fd = openat_file(dirfd, name, flags, o_mode);
146     if (fd == -1) {
147         return NULL;
148     }
149     fp = fdopen(fd, mode);
150     if (!fp) {
151         close(fd);
152     }
153     return fp;
154 }
155 
156 #define ATTR_MAX 100
157 static void local_mapped_file_attr(int dirfd, const char *name,
158                                    struct stat *stbuf)
159 {
160     FILE *fp;
161     char buf[ATTR_MAX];
162     int map_dirfd;
163 
164     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
165     if (map_dirfd == -1) {
166         return;
167     }
168 
169     fp = local_fopenat(map_dirfd, name, "r");
170     close_preserve_errno(map_dirfd);
171     if (!fp) {
172         return;
173     }
174     memset(buf, 0, ATTR_MAX);
175     while (fgets(buf, ATTR_MAX, fp)) {
176         if (!strncmp(buf, "virtfs.uid", 10)) {
177             stbuf->st_uid = atoi(buf+11);
178         } else if (!strncmp(buf, "virtfs.gid", 10)) {
179             stbuf->st_gid = atoi(buf+11);
180         } else if (!strncmp(buf, "virtfs.mode", 11)) {
181             stbuf->st_mode = atoi(buf+12);
182         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
183             stbuf->st_rdev = atoi(buf+12);
184         }
185         memset(buf, 0, ATTR_MAX);
186     }
187     fclose(fp);
188 }
189 
190 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
191 {
192     int err = -1;
193     char *dirpath = g_path_get_dirname(fs_path->data);
194     char *name = g_path_get_basename(fs_path->data);
195     int dirfd;
196 
197     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
198     if (dirfd == -1) {
199         goto out;
200     }
201 
202     err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
203     if (err) {
204         goto err_out;
205     }
206     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
207         /* Actual credentials are part of extended attrs */
208         uid_t tmp_uid;
209         gid_t tmp_gid;
210         mode_t tmp_mode;
211         dev_t tmp_dev;
212 
213         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
214                                  sizeof(uid_t)) > 0) {
215             stbuf->st_uid = le32_to_cpu(tmp_uid);
216         }
217         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
218                                  sizeof(gid_t)) > 0) {
219             stbuf->st_gid = le32_to_cpu(tmp_gid);
220         }
221         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
222                                  sizeof(mode_t)) > 0) {
223             stbuf->st_mode = le32_to_cpu(tmp_mode);
224         }
225         if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
226                                  sizeof(dev_t)) > 0) {
227             stbuf->st_rdev = le64_to_cpu(tmp_dev);
228         }
229     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
230         local_mapped_file_attr(dirfd, name, stbuf);
231     }
232 
233 err_out:
234     close_preserve_errno(dirfd);
235 out:
236     g_free(name);
237     g_free(dirpath);
238     return err;
239 }
240 
241 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
242 {
243     int err;
244     char *attr_dir;
245     char *tmp_path = g_strdup(path);
246 
247     attr_dir = g_strdup_printf("%s/%s/%s",
248              ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
249 
250     err = mkdir(attr_dir, 0700);
251     if (err < 0 && errno == EEXIST) {
252         err = 0;
253     }
254     g_free(attr_dir);
255     g_free(tmp_path);
256     return err;
257 }
258 
259 static int local_set_mapped_file_attr(FsContext *ctx,
260                                       const char *path, FsCred *credp)
261 {
262     FILE *fp;
263     int ret = 0;
264     char buf[ATTR_MAX];
265     char *attr_path;
266     int uid = -1, gid = -1, mode = -1, rdev = -1;
267 
268     attr_path = local_mapped_attr_path(ctx, path);
269     fp = local_fopen(attr_path, "r");
270     if (!fp) {
271         goto create_map_file;
272     }
273     memset(buf, 0, ATTR_MAX);
274     while (fgets(buf, ATTR_MAX, fp)) {
275         if (!strncmp(buf, "virtfs.uid", 10)) {
276             uid = atoi(buf+11);
277         } else if (!strncmp(buf, "virtfs.gid", 10)) {
278             gid = atoi(buf+11);
279         } else if (!strncmp(buf, "virtfs.mode", 11)) {
280             mode = atoi(buf+12);
281         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
282             rdev = atoi(buf+12);
283         }
284         memset(buf, 0, ATTR_MAX);
285     }
286     fclose(fp);
287     goto update_map_file;
288 
289 create_map_file:
290     ret = local_create_mapped_attr_dir(ctx, path);
291     if (ret < 0) {
292         goto err_out;
293     }
294 
295 update_map_file:
296     fp = local_fopen(attr_path, "w");
297     if (!fp) {
298         ret = -1;
299         goto err_out;
300     }
301 
302     if (credp->fc_uid != -1) {
303         uid = credp->fc_uid;
304     }
305     if (credp->fc_gid != -1) {
306         gid = credp->fc_gid;
307     }
308     if (credp->fc_mode != -1) {
309         mode = credp->fc_mode;
310     }
311     if (credp->fc_rdev != -1) {
312         rdev = credp->fc_rdev;
313     }
314 
315 
316     if (uid != -1) {
317         fprintf(fp, "virtfs.uid=%d\n", uid);
318     }
319     if (gid != -1) {
320         fprintf(fp, "virtfs.gid=%d\n", gid);
321     }
322     if (mode != -1) {
323         fprintf(fp, "virtfs.mode=%d\n", mode);
324     }
325     if (rdev != -1) {
326         fprintf(fp, "virtfs.rdev=%d\n", rdev);
327     }
328     fclose(fp);
329 
330 err_out:
331     g_free(attr_path);
332     return ret;
333 }
334 
335 static int local_set_xattr(const char *path, FsCred *credp)
336 {
337     int err;
338 
339     if (credp->fc_uid != -1) {
340         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
341         err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
342         if (err) {
343             return err;
344         }
345     }
346     if (credp->fc_gid != -1) {
347         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
348         err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
349         if (err) {
350             return err;
351         }
352     }
353     if (credp->fc_mode != -1) {
354         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
355         err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
356         if (err) {
357             return err;
358         }
359     }
360     if (credp->fc_rdev != -1) {
361         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
362         err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
363         if (err) {
364             return err;
365         }
366     }
367     return 0;
368 }
369 
370 static int local_set_mapped_file_attrat(int dirfd, const char *name,
371                                         FsCred *credp)
372 {
373     FILE *fp;
374     int ret;
375     char buf[ATTR_MAX];
376     int uid = -1, gid = -1, mode = -1, rdev = -1;
377     int map_dirfd;
378 
379     ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
380     if (ret < 0 && errno != EEXIST) {
381         return -1;
382     }
383 
384     map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
385     if (map_dirfd == -1) {
386         return -1;
387     }
388 
389     fp = local_fopenat(map_dirfd, name, "r");
390     if (!fp) {
391         if (errno == ENOENT) {
392             goto update_map_file;
393         } else {
394             close_preserve_errno(map_dirfd);
395             return -1;
396         }
397     }
398     memset(buf, 0, ATTR_MAX);
399     while (fgets(buf, ATTR_MAX, fp)) {
400         if (!strncmp(buf, "virtfs.uid", 10)) {
401             uid = atoi(buf + 11);
402         } else if (!strncmp(buf, "virtfs.gid", 10)) {
403             gid = atoi(buf + 11);
404         } else if (!strncmp(buf, "virtfs.mode", 11)) {
405             mode = atoi(buf + 12);
406         } else if (!strncmp(buf, "virtfs.rdev", 11)) {
407             rdev = atoi(buf + 12);
408         }
409         memset(buf, 0, ATTR_MAX);
410     }
411     fclose(fp);
412 
413 update_map_file:
414     fp = local_fopenat(map_dirfd, name, "w");
415     close_preserve_errno(map_dirfd);
416     if (!fp) {
417         return -1;
418     }
419 
420     if (credp->fc_uid != -1) {
421         uid = credp->fc_uid;
422     }
423     if (credp->fc_gid != -1) {
424         gid = credp->fc_gid;
425     }
426     if (credp->fc_mode != -1) {
427         mode = credp->fc_mode;
428     }
429     if (credp->fc_rdev != -1) {
430         rdev = credp->fc_rdev;
431     }
432 
433     if (uid != -1) {
434         fprintf(fp, "virtfs.uid=%d\n", uid);
435     }
436     if (gid != -1) {
437         fprintf(fp, "virtfs.gid=%d\n", gid);
438     }
439     if (mode != -1) {
440         fprintf(fp, "virtfs.mode=%d\n", mode);
441     }
442     if (rdev != -1) {
443         fprintf(fp, "virtfs.rdev=%d\n", rdev);
444     }
445     fclose(fp);
446 
447     return 0;
448 }
449 
450 static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
451 {
452     int fd, ret;
453 
454     /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
455      * Unfortunately, the linux kernel doesn't implement it yet. As an
456      * alternative, let's open the file and use fchmod() instead. This
457      * may fail depending on the permissions of the file, but it is the
458      * best we can do to avoid TOCTTOU. We first try to open read-only
459      * in case name points to a directory. If that fails, we try write-only
460      * in case name doesn't point to a directory.
461      */
462     fd = openat_file(dirfd, name, O_RDONLY, 0);
463     if (fd == -1) {
464         /* In case the file is writable-only and isn't a directory. */
465         if (errno == EACCES) {
466             fd = openat_file(dirfd, name, O_WRONLY, 0);
467         }
468         if (fd == -1 && errno == EISDIR) {
469             errno = EACCES;
470         }
471     }
472     if (fd == -1) {
473         return -1;
474     }
475     ret = fchmod(fd, mode);
476     close_preserve_errno(fd);
477     return ret;
478 }
479 
480 static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
481 {
482     int err;
483 
484     if (credp->fc_uid != -1) {
485         uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
486         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
487                                    sizeof(uid_t), 0);
488         if (err) {
489             return err;
490         }
491     }
492     if (credp->fc_gid != -1) {
493         uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
494         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
495                                    sizeof(gid_t), 0);
496         if (err) {
497             return err;
498         }
499     }
500     if (credp->fc_mode != -1) {
501         uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
502         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
503                                    sizeof(mode_t), 0);
504         if (err) {
505             return err;
506         }
507     }
508     if (credp->fc_rdev != -1) {
509         uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
510         err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
511                                    sizeof(dev_t), 0);
512         if (err) {
513             return err;
514         }
515     }
516     return 0;
517 }
518 
519 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
520                                          FsCred *credp)
521 {
522     char *buffer;
523 
524     buffer = rpath(fs_ctx, path);
525     if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
526         /*
527          * If we fail to change ownership and if we are
528          * using security model none. Ignore the error
529          */
530         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
531             goto err;
532         }
533     }
534 
535     if (chmod(buffer, credp->fc_mode & 07777) < 0) {
536         goto err;
537     }
538 
539     g_free(buffer);
540     return 0;
541 err:
542     g_free(buffer);
543     return -1;
544 }
545 
546 static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
547                                       const char *name, FsCred *credp)
548 {
549     if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
550                  AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 0) {
551         /*
552          * If we fail to change ownership and if we are
553          * using security model none. Ignore the error
554          */
555         if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
556             return -1;
557         }
558     }
559 
560     return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
561 }
562 
563 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
564                               char *buf, size_t bufsz)
565 {
566     ssize_t tsize = -1;
567 
568     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
569         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
570         int fd;
571 
572         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
573         if (fd == -1) {
574             return -1;
575         }
576         do {
577             tsize = read(fd, (void *)buf, bufsz);
578         } while (tsize == -1 && errno == EINTR);
579         close_preserve_errno(fd);
580     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
581                (fs_ctx->export_flags & V9FS_SM_NONE)) {
582         char *dirpath = g_path_get_dirname(fs_path->data);
583         char *name = g_path_get_basename(fs_path->data);
584         int dirfd;
585 
586         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
587         if (dirfd == -1) {
588             goto out;
589         }
590 
591         tsize = readlinkat(dirfd, name, buf, bufsz);
592         close_preserve_errno(dirfd);
593     out:
594         g_free(name);
595         g_free(dirpath);
596     }
597     return tsize;
598 }
599 
600 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
601 {
602     return close(fs->fd);
603 }
604 
605 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
606 {
607     return closedir(fs->dir.stream);
608 }
609 
610 static int local_open(FsContext *ctx, V9fsPath *fs_path,
611                       int flags, V9fsFidOpenState *fs)
612 {
613     int fd;
614 
615     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
616     if (fd == -1) {
617         return -1;
618     }
619     fs->fd = fd;
620     return fs->fd;
621 }
622 
623 static int local_opendir(FsContext *ctx,
624                          V9fsPath *fs_path, V9fsFidOpenState *fs)
625 {
626     int dirfd;
627     DIR *stream;
628 
629     dirfd = local_opendir_nofollow(ctx, fs_path->data);
630     if (dirfd == -1) {
631         return -1;
632     }
633 
634     stream = fdopendir(dirfd);
635     if (!stream) {
636         return -1;
637     }
638     fs->dir.stream = stream;
639     return 0;
640 }
641 
642 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
643 {
644     rewinddir(fs->dir.stream);
645 }
646 
647 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
648 {
649     return telldir(fs->dir.stream);
650 }
651 
652 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
653 {
654     struct dirent *entry;
655 
656 again:
657     entry = readdir(fs->dir.stream);
658     if (!entry) {
659         return NULL;
660     }
661 
662     if (ctx->export_flags & V9FS_SM_MAPPED) {
663         entry->d_type = DT_UNKNOWN;
664     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
665         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
666             /* skp the meta data directory */
667             goto again;
668         }
669         entry->d_type = DT_UNKNOWN;
670     }
671 
672     return entry;
673 }
674 
675 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
676 {
677     seekdir(fs->dir.stream, off);
678 }
679 
680 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
681                             const struct iovec *iov,
682                             int iovcnt, off_t offset)
683 {
684 #ifdef CONFIG_PREADV
685     return preadv(fs->fd, iov, iovcnt, offset);
686 #else
687     int err = lseek(fs->fd, offset, SEEK_SET);
688     if (err == -1) {
689         return err;
690     } else {
691         return readv(fs->fd, iov, iovcnt);
692     }
693 #endif
694 }
695 
696 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
697                              const struct iovec *iov,
698                              int iovcnt, off_t offset)
699 {
700     ssize_t ret;
701 #ifdef CONFIG_PREADV
702     ret = pwritev(fs->fd, iov, iovcnt, offset);
703 #else
704     int err = lseek(fs->fd, offset, SEEK_SET);
705     if (err == -1) {
706         return err;
707     } else {
708         ret = writev(fs->fd, iov, iovcnt);
709     }
710 #endif
711 #ifdef CONFIG_SYNC_FILE_RANGE
712     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
713         /*
714          * Initiate a writeback. This is not a data integrity sync.
715          * We want to ensure that we don't leave dirty pages in the cache
716          * after write when writeout=immediate is sepcified.
717          */
718         sync_file_range(fs->fd, offset, ret,
719                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
720     }
721 #endif
722     return ret;
723 }
724 
725 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
726 {
727     char *dirpath = g_path_get_dirname(fs_path->data);
728     char *name = g_path_get_basename(fs_path->data);
729     int ret = -1;
730     int dirfd;
731 
732     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
733     if (dirfd == -1) {
734         goto out;
735     }
736 
737     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
738         ret = local_set_xattrat(dirfd, name, credp);
739     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
740         ret = local_set_mapped_file_attrat(dirfd, name, credp);
741     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
742                fs_ctx->export_flags & V9FS_SM_NONE) {
743         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
744     }
745     close_preserve_errno(dirfd);
746 
747 out:
748     g_free(dirpath);
749     g_free(name);
750     return ret;
751 }
752 
753 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
754                        const char *name, FsCred *credp)
755 {
756     int err = -1;
757     int dirfd;
758 
759     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
760     if (dirfd == -1) {
761         return -1;
762     }
763 
764     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
765         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
766         err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
767         if (err == -1) {
768             goto out;
769         }
770 
771         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
772             err = local_set_xattrat(dirfd, name, credp);
773         } else {
774             err = local_set_mapped_file_attrat(dirfd, name, credp);
775         }
776         if (err == -1) {
777             goto err_end;
778         }
779     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
780                fs_ctx->export_flags & V9FS_SM_NONE) {
781         err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
782         if (err == -1) {
783             goto out;
784         }
785         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
786         if (err == -1) {
787             goto err_end;
788         }
789     }
790     goto out;
791 
792 err_end:
793     unlinkat_preserve_errno(dirfd, name, 0);
794 out:
795     close_preserve_errno(dirfd);
796     return err;
797 }
798 
799 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
800                        const char *name, FsCred *credp)
801 {
802     char *path;
803     int err = -1;
804     int serrno = 0;
805     V9fsString fullname;
806     char *buffer = NULL;
807 
808     v9fs_string_init(&fullname);
809     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
810     path = fullname.data;
811 
812     /* Determine the security model */
813     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
814         buffer = rpath(fs_ctx, path);
815         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
816         if (err == -1) {
817             goto out;
818         }
819         credp->fc_mode = credp->fc_mode|S_IFDIR;
820         err = local_set_xattr(buffer, credp);
821         if (err == -1) {
822             serrno = errno;
823             goto err_end;
824         }
825     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
826         buffer = rpath(fs_ctx, path);
827         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
828         if (err == -1) {
829             goto out;
830         }
831         credp->fc_mode = credp->fc_mode|S_IFDIR;
832         err = local_set_mapped_file_attr(fs_ctx, path, credp);
833         if (err == -1) {
834             serrno = errno;
835             goto err_end;
836         }
837     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
838                (fs_ctx->export_flags & V9FS_SM_NONE)) {
839         buffer = rpath(fs_ctx, path);
840         err = mkdir(buffer, credp->fc_mode);
841         if (err == -1) {
842             goto out;
843         }
844         err = local_post_create_passthrough(fs_ctx, path, credp);
845         if (err == -1) {
846             serrno = errno;
847             goto err_end;
848         }
849     }
850     goto out;
851 
852 err_end:
853     remove(buffer);
854     errno = serrno;
855 out:
856     g_free(buffer);
857     v9fs_string_free(&fullname);
858     return err;
859 }
860 
861 static int local_fstat(FsContext *fs_ctx, int fid_type,
862                        V9fsFidOpenState *fs, struct stat *stbuf)
863 {
864     int err, fd;
865 
866     if (fid_type == P9_FID_DIR) {
867         fd = dirfd(fs->dir.stream);
868     } else {
869         fd = fs->fd;
870     }
871 
872     err = fstat(fd, stbuf);
873     if (err) {
874         return err;
875     }
876     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
877         /* Actual credentials are part of extended attrs */
878         uid_t tmp_uid;
879         gid_t tmp_gid;
880         mode_t tmp_mode;
881         dev_t tmp_dev;
882 
883         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
884             stbuf->st_uid = le32_to_cpu(tmp_uid);
885         }
886         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
887             stbuf->st_gid = le32_to_cpu(tmp_gid);
888         }
889         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
890             stbuf->st_mode = le32_to_cpu(tmp_mode);
891         }
892         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
893             stbuf->st_rdev = le64_to_cpu(tmp_dev);
894         }
895     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
896         errno = EOPNOTSUPP;
897         return -1;
898     }
899     return err;
900 }
901 
902 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
903                        int flags, FsCred *credp, V9fsFidOpenState *fs)
904 {
905     char *path;
906     int fd = -1;
907     int err = -1;
908     int serrno = 0;
909     V9fsString fullname;
910     char *buffer = NULL;
911 
912     /*
913      * Mark all the open to not follow symlinks
914      */
915     flags |= O_NOFOLLOW;
916 
917     v9fs_string_init(&fullname);
918     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
919     path = fullname.data;
920 
921     /* Determine the security model */
922     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
923         buffer = rpath(fs_ctx, path);
924         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
925         if (fd == -1) {
926             err = fd;
927             goto out;
928         }
929         credp->fc_mode = credp->fc_mode|S_IFREG;
930         /* Set cleint credentials in xattr */
931         err = local_set_xattr(buffer, credp);
932         if (err == -1) {
933             serrno = errno;
934             goto err_end;
935         }
936     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
937         buffer = rpath(fs_ctx, path);
938         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
939         if (fd == -1) {
940             err = fd;
941             goto out;
942         }
943         credp->fc_mode = credp->fc_mode|S_IFREG;
944         /* Set client credentials in .virtfs_metadata directory files */
945         err = local_set_mapped_file_attr(fs_ctx, path, credp);
946         if (err == -1) {
947             serrno = errno;
948             goto err_end;
949         }
950     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
951                (fs_ctx->export_flags & V9FS_SM_NONE)) {
952         buffer = rpath(fs_ctx, path);
953         fd = open(buffer, flags, credp->fc_mode);
954         if (fd == -1) {
955             err = fd;
956             goto out;
957         }
958         err = local_post_create_passthrough(fs_ctx, path, credp);
959         if (err == -1) {
960             serrno = errno;
961             goto err_end;
962         }
963     }
964     err = fd;
965     fs->fd = fd;
966     goto out;
967 
968 err_end:
969     close(fd);
970     remove(buffer);
971     errno = serrno;
972 out:
973     g_free(buffer);
974     v9fs_string_free(&fullname);
975     return err;
976 }
977 
978 
979 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
980                          V9fsPath *dir_path, const char *name, FsCred *credp)
981 {
982     int err = -1;
983     int dirfd;
984 
985     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
986     if (dirfd == -1) {
987         return -1;
988     }
989 
990     /* Determine the security model */
991     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
992         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
993         int fd;
994         ssize_t oldpath_size, write_size;
995 
996         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
997                          SM_LOCAL_MODE_BITS);
998         if (fd == -1) {
999             goto out;
1000         }
1001         /* Write the oldpath (target) to the file. */
1002         oldpath_size = strlen(oldpath);
1003         do {
1004             write_size = write(fd, (void *)oldpath, oldpath_size);
1005         } while (write_size == -1 && errno == EINTR);
1006         close_preserve_errno(fd);
1007 
1008         if (write_size != oldpath_size) {
1009             goto err_end;
1010         }
1011         /* Set cleint credentials in symlink's xattr */
1012         credp->fc_mode = credp->fc_mode | S_IFLNK;
1013 
1014         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1015             err = local_set_xattrat(dirfd, name, credp);
1016         } else {
1017             err = local_set_mapped_file_attrat(dirfd, name, credp);
1018         }
1019         if (err == -1) {
1020             goto err_end;
1021         }
1022     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
1023                fs_ctx->export_flags & V9FS_SM_NONE) {
1024         err = symlinkat(oldpath, dirfd, name);
1025         if (err) {
1026             goto out;
1027         }
1028         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1029                        AT_SYMLINK_NOFOLLOW);
1030         if (err == -1) {
1031             /*
1032              * If we fail to change ownership and if we are
1033              * using security model none. Ignore the error
1034              */
1035             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1036                 goto err_end;
1037             } else {
1038                 err = 0;
1039             }
1040         }
1041     }
1042     goto out;
1043 
1044 err_end:
1045     unlinkat_preserve_errno(dirfd, name, 0);
1046 out:
1047     close_preserve_errno(dirfd);
1048     return err;
1049 }
1050 
1051 static int local_link(FsContext *ctx, V9fsPath *oldpath,
1052                       V9fsPath *dirpath, const char *name)
1053 {
1054     char *odirpath = g_path_get_dirname(oldpath->data);
1055     char *oname = g_path_get_basename(oldpath->data);
1056     int ret = -1;
1057     int odirfd, ndirfd;
1058 
1059     odirfd = local_opendir_nofollow(ctx, odirpath);
1060     if (odirfd == -1) {
1061         goto out;
1062     }
1063 
1064     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
1065     if (ndirfd == -1) {
1066         close_preserve_errno(odirfd);
1067         goto out;
1068     }
1069 
1070     ret = linkat(odirfd, oname, ndirfd, name, 0);
1071     if (ret < 0) {
1072         goto out_close;
1073     }
1074 
1075     /* now link the virtfs_metadata files */
1076     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1077         int omap_dirfd, nmap_dirfd;
1078 
1079         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1080         if (ret < 0 && errno != EEXIST) {
1081             goto err_undo_link;
1082         }
1083 
1084         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1085         if (omap_dirfd == -1) {
1086             goto err;
1087         }
1088 
1089         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1090         if (nmap_dirfd == -1) {
1091             close_preserve_errno(omap_dirfd);
1092             goto err;
1093         }
1094 
1095         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1096         close_preserve_errno(nmap_dirfd);
1097         close_preserve_errno(omap_dirfd);
1098         if (ret < 0 && errno != ENOENT) {
1099             goto err_undo_link;
1100         }
1101 
1102         ret = 0;
1103     }
1104     goto out_close;
1105 
1106 err:
1107     ret = -1;
1108 err_undo_link:
1109     unlinkat_preserve_errno(ndirfd, name, 0);
1110 out_close:
1111     close_preserve_errno(ndirfd);
1112     close_preserve_errno(odirfd);
1113 out:
1114     g_free(oname);
1115     g_free(odirpath);
1116     return ret;
1117 }
1118 
1119 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1120 {
1121     int fd, ret;
1122 
1123     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1124     if (fd == -1) {
1125         return -1;
1126     }
1127     ret = ftruncate(fd, size);
1128     close_preserve_errno(fd);
1129     return ret;
1130 }
1131 
1132 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1133 {
1134     char *dirpath = g_path_get_dirname(fs_path->data);
1135     char *name = g_path_get_basename(fs_path->data);
1136     int ret = -1;
1137     int dirfd;
1138 
1139     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1140     if (dirfd == -1) {
1141         goto out;
1142     }
1143 
1144     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1145         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1146         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1147         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1148                        AT_SYMLINK_NOFOLLOW);
1149     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1150         ret = local_set_xattrat(dirfd, name, credp);
1151     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1152         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1153     }
1154 
1155     close_preserve_errno(dirfd);
1156 out:
1157     g_free(name);
1158     g_free(dirpath);
1159     return ret;
1160 }
1161 
1162 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1163                            const struct timespec *buf)
1164 {
1165     char *dirpath = g_path_get_dirname(fs_path->data);
1166     char *name = g_path_get_basename(fs_path->data);
1167     int dirfd, ret = -1;
1168 
1169     dirfd = local_opendir_nofollow(s, dirpath);
1170     if (dirfd == -1) {
1171         goto out;
1172     }
1173 
1174     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1175     close_preserve_errno(dirfd);
1176 out:
1177     g_free(dirpath);
1178     g_free(name);
1179     return ret;
1180 }
1181 
1182 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1183                                  int flags)
1184 {
1185     int ret = -1;
1186 
1187     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1188         int map_dirfd;
1189 
1190         if (flags == AT_REMOVEDIR) {
1191             int fd;
1192 
1193             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1194             if (fd == -1) {
1195                 goto err_out;
1196             }
1197             /*
1198              * If directory remove .virtfs_metadata contained in the
1199              * directory
1200              */
1201             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1202             close_preserve_errno(fd);
1203             if (ret < 0 && errno != ENOENT) {
1204                 /*
1205                  * We didn't had the .virtfs_metadata file. May be file created
1206                  * in non-mapped mode ?. Ignore ENOENT.
1207                  */
1208                 goto err_out;
1209             }
1210         }
1211         /*
1212          * Now remove the name from parent directory
1213          * .virtfs_metadata directory.
1214          */
1215         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1216         ret = unlinkat(map_dirfd, name, 0);
1217         close_preserve_errno(map_dirfd);
1218         if (ret < 0 && errno != ENOENT) {
1219             /*
1220              * We didn't had the .virtfs_metadata file. May be file created
1221              * in non-mapped mode ?. Ignore ENOENT.
1222              */
1223             goto err_out;
1224         }
1225     }
1226 
1227     ret = unlinkat(dirfd, name, flags);
1228 err_out:
1229     return ret;
1230 }
1231 
1232 static int local_remove(FsContext *ctx, const char *path)
1233 {
1234     struct stat stbuf;
1235     char *dirpath = g_path_get_dirname(path);
1236     char *name = g_path_get_basename(path);
1237     int flags = 0;
1238     int dirfd;
1239     int err = -1;
1240 
1241     dirfd = local_opendir_nofollow(ctx, dirpath);
1242     if (dirfd) {
1243         goto out;
1244     }
1245 
1246     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1247         goto err_out;
1248     }
1249 
1250     if (S_ISDIR(stbuf.st_mode)) {
1251         flags |= AT_REMOVEDIR;
1252     }
1253 
1254     err = local_unlinkat_common(ctx, dirfd, name, flags);
1255 err_out:
1256     close_preserve_errno(dirfd);
1257 out:
1258     g_free(name);
1259     g_free(dirpath);
1260     return err;
1261 }
1262 
1263 static int local_fsync(FsContext *ctx, int fid_type,
1264                        V9fsFidOpenState *fs, int datasync)
1265 {
1266     int fd;
1267 
1268     if (fid_type == P9_FID_DIR) {
1269         fd = dirfd(fs->dir.stream);
1270     } else {
1271         fd = fs->fd;
1272     }
1273 
1274     if (datasync) {
1275         return qemu_fdatasync(fd);
1276     } else {
1277         return fsync(fd);
1278     }
1279 }
1280 
1281 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1282 {
1283     int fd, ret;
1284 
1285     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1286     ret = fstatfs(fd, stbuf);
1287     close_preserve_errno(fd);
1288     return ret;
1289 }
1290 
1291 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1292                                const char *name, void *value, size_t size)
1293 {
1294     char *path = fs_path->data;
1295 
1296     return v9fs_get_xattr(ctx, path, name, value, size);
1297 }
1298 
1299 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1300                                 void *value, size_t size)
1301 {
1302     char *path = fs_path->data;
1303 
1304     return v9fs_list_xattr(ctx, path, value, size);
1305 }
1306 
1307 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1308                            void *value, size_t size, int flags)
1309 {
1310     char *path = fs_path->data;
1311 
1312     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1313 }
1314 
1315 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1316                               const char *name)
1317 {
1318     char *path = fs_path->data;
1319 
1320     return v9fs_remove_xattr(ctx, path, name);
1321 }
1322 
1323 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1324                               const char *name, V9fsPath *target)
1325 {
1326     if (dir_path) {
1327         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1328     } else {
1329         v9fs_path_sprintf(target, "%s", name);
1330     }
1331     return 0;
1332 }
1333 
1334 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1335                           const char *old_name, V9fsPath *newdir,
1336                           const char *new_name)
1337 {
1338     int ret;
1339     int odirfd, ndirfd;
1340 
1341     odirfd = local_opendir_nofollow(ctx, olddir->data);
1342     if (odirfd == -1) {
1343         return -1;
1344     }
1345 
1346     ndirfd = local_opendir_nofollow(ctx, newdir->data);
1347     if (ndirfd == -1) {
1348         close_preserve_errno(odirfd);
1349         return -1;
1350     }
1351 
1352     ret = renameat(odirfd, old_name, ndirfd, new_name);
1353     if (ret < 0) {
1354         goto out;
1355     }
1356 
1357     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1358         int omap_dirfd, nmap_dirfd;
1359 
1360         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1361         if (ret < 0 && errno != EEXIST) {
1362             goto err_undo_rename;
1363         }
1364 
1365         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1366         if (omap_dirfd == -1) {
1367             goto err;
1368         }
1369 
1370         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1371         if (nmap_dirfd == -1) {
1372             close_preserve_errno(omap_dirfd);
1373             goto err;
1374         }
1375 
1376         /* rename the .virtfs_metadata files */
1377         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1378         close_preserve_errno(nmap_dirfd);
1379         close_preserve_errno(omap_dirfd);
1380         if (ret < 0 && errno != ENOENT) {
1381             goto err_undo_rename;
1382         }
1383 
1384         ret = 0;
1385     }
1386     goto out;
1387 
1388 err:
1389     ret = -1;
1390 err_undo_rename:
1391     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1392 out:
1393     close_preserve_errno(ndirfd);
1394     close_preserve_errno(odirfd);
1395     return ret;
1396 }
1397 
1398 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1399 {
1400     path->data = g_path_get_dirname(str);
1401     path->size = strlen(path->data) + 1;
1402 }
1403 
1404 static int local_rename(FsContext *ctx, const char *oldpath,
1405                         const char *newpath)
1406 {
1407     int err;
1408     char *oname = g_path_get_basename(oldpath);
1409     char *nname = g_path_get_basename(newpath);
1410     V9fsPath olddir, newdir;
1411 
1412     v9fs_path_init_dirname(&olddir, oldpath);
1413     v9fs_path_init_dirname(&newdir, newpath);
1414 
1415     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1416 
1417     v9fs_path_free(&newdir);
1418     v9fs_path_free(&olddir);
1419     g_free(nname);
1420     g_free(oname);
1421 
1422     return err;
1423 }
1424 
1425 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1426                           const char *name, int flags)
1427 {
1428     int ret;
1429     int dirfd;
1430 
1431     dirfd = local_opendir_nofollow(ctx, dir->data);
1432     if (dirfd == -1) {
1433         return -1;
1434     }
1435 
1436     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1437     close_preserve_errno(dirfd);
1438     return ret;
1439 }
1440 
1441 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1442                                 mode_t st_mode, uint64_t *st_gen)
1443 {
1444 #ifdef FS_IOC_GETVERSION
1445     int err;
1446     V9fsFidOpenState fid_open;
1447 
1448     /*
1449      * Do not try to open special files like device nodes, fifos etc
1450      * We can get fd for regular files and directories only
1451      */
1452     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1453         errno = ENOTTY;
1454         return -1;
1455     }
1456     err = local_open(ctx, path, O_RDONLY, &fid_open);
1457     if (err < 0) {
1458         return err;
1459     }
1460     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1461     local_close(ctx, &fid_open);
1462     return err;
1463 #else
1464     errno = ENOTTY;
1465     return -1;
1466 #endif
1467 }
1468 
1469 static int local_init(FsContext *ctx)
1470 {
1471     struct statfs stbuf;
1472     LocalData *data = g_malloc(sizeof(*data));
1473 
1474     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1475     if (data->mountfd == -1) {
1476         goto err;
1477     }
1478 
1479 #ifdef FS_IOC_GETVERSION
1480     /*
1481      * use ioc_getversion only if the ioctl is definied
1482      */
1483     if (fstatfs(data->mountfd, &stbuf) < 0) {
1484         close_preserve_errno(data->mountfd);
1485         goto err;
1486     }
1487     switch (stbuf.f_type) {
1488     case EXT2_SUPER_MAGIC:
1489     case BTRFS_SUPER_MAGIC:
1490     case REISERFS_SUPER_MAGIC:
1491     case XFS_SUPER_MAGIC:
1492         ctx->exops.get_st_gen = local_ioc_getversion;
1493         break;
1494     }
1495 #endif
1496 
1497     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1498         ctx->xops = passthrough_xattr_ops;
1499     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1500         ctx->xops = mapped_xattr_ops;
1501     } else if (ctx->export_flags & V9FS_SM_NONE) {
1502         ctx->xops = none_xattr_ops;
1503     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1504         /*
1505          * xattr operation for mapped-file and passthrough
1506          * remain same.
1507          */
1508         ctx->xops = passthrough_xattr_ops;
1509     }
1510     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1511 
1512     ctx->private = data;
1513     return 0;
1514 
1515 err:
1516     g_free(data);
1517     return -1;
1518 }
1519 
1520 static void local_cleanup(FsContext *ctx)
1521 {
1522     LocalData *data = ctx->private;
1523 
1524     close(data->mountfd);
1525     g_free(data);
1526 }
1527 
1528 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1529 {
1530     const char *sec_model = qemu_opt_get(opts, "security_model");
1531     const char *path = qemu_opt_get(opts, "path");
1532 
1533     if (!sec_model) {
1534         error_report("Security model not specified, local fs needs security model");
1535         error_printf("valid options are:"
1536                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1537         return -1;
1538     }
1539 
1540     if (!strcmp(sec_model, "passthrough")) {
1541         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1542     } else if (!strcmp(sec_model, "mapped") ||
1543                !strcmp(sec_model, "mapped-xattr")) {
1544         fse->export_flags |= V9FS_SM_MAPPED;
1545     } else if (!strcmp(sec_model, "none")) {
1546         fse->export_flags |= V9FS_SM_NONE;
1547     } else if (!strcmp(sec_model, "mapped-file")) {
1548         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1549     } else {
1550         error_report("Invalid security model %s specified", sec_model);
1551         error_printf("valid options are:"
1552                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1553         return -1;
1554     }
1555 
1556     if (!path) {
1557         error_report("fsdev: No path specified");
1558         return -1;
1559     }
1560     fse->path = g_strdup(path);
1561 
1562     return 0;
1563 }
1564 
1565 FileOperations local_ops = {
1566     .parse_opts = local_parse_opts,
1567     .init  = local_init,
1568     .cleanup = local_cleanup,
1569     .lstat = local_lstat,
1570     .readlink = local_readlink,
1571     .close = local_close,
1572     .closedir = local_closedir,
1573     .open = local_open,
1574     .opendir = local_opendir,
1575     .rewinddir = local_rewinddir,
1576     .telldir = local_telldir,
1577     .readdir = local_readdir,
1578     .seekdir = local_seekdir,
1579     .preadv = local_preadv,
1580     .pwritev = local_pwritev,
1581     .chmod = local_chmod,
1582     .mknod = local_mknod,
1583     .mkdir = local_mkdir,
1584     .fstat = local_fstat,
1585     .open2 = local_open2,
1586     .symlink = local_symlink,
1587     .link = local_link,
1588     .truncate = local_truncate,
1589     .rename = local_rename,
1590     .chown = local_chown,
1591     .utimensat = local_utimensat,
1592     .remove = local_remove,
1593     .fsync = local_fsync,
1594     .statfs = local_statfs,
1595     .lgetxattr = local_lgetxattr,
1596     .llistxattr = local_llistxattr,
1597     .lsetxattr = local_lsetxattr,
1598     .lremovexattr = local_lremovexattr,
1599     .name_to_path = local_name_to_path,
1600     .renameat  = local_renameat,
1601     .unlinkat = local_unlinkat,
1602 };
1603