xref: /qemu/hw/9pfs/9p-local.c (revision a565fea56546e254b7610305b07711f0a3bda0c7)
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     int err = -1;
803     int dirfd;
804 
805     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
806     if (dirfd == -1) {
807         return -1;
808     }
809 
810     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
811         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
812         err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS);
813         if (err == -1) {
814             goto out;
815         }
816         credp->fc_mode = credp->fc_mode | S_IFDIR;
817 
818         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
819             err = local_set_xattrat(dirfd, name, credp);
820         } else {
821             err = local_set_mapped_file_attrat(dirfd, name, credp);
822         }
823         if (err == -1) {
824             goto err_end;
825         }
826     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
827                fs_ctx->export_flags & V9FS_SM_NONE) {
828         err = mkdirat(dirfd, name, credp->fc_mode);
829         if (err == -1) {
830             goto out;
831         }
832         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
833         if (err == -1) {
834             goto err_end;
835         }
836     }
837     goto out;
838 
839 err_end:
840     unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
841 out:
842     close_preserve_errno(dirfd);
843     return err;
844 }
845 
846 static int local_fstat(FsContext *fs_ctx, int fid_type,
847                        V9fsFidOpenState *fs, struct stat *stbuf)
848 {
849     int err, fd;
850 
851     if (fid_type == P9_FID_DIR) {
852         fd = dirfd(fs->dir.stream);
853     } else {
854         fd = fs->fd;
855     }
856 
857     err = fstat(fd, stbuf);
858     if (err) {
859         return err;
860     }
861     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
862         /* Actual credentials are part of extended attrs */
863         uid_t tmp_uid;
864         gid_t tmp_gid;
865         mode_t tmp_mode;
866         dev_t tmp_dev;
867 
868         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
869             stbuf->st_uid = le32_to_cpu(tmp_uid);
870         }
871         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
872             stbuf->st_gid = le32_to_cpu(tmp_gid);
873         }
874         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
875             stbuf->st_mode = le32_to_cpu(tmp_mode);
876         }
877         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
878             stbuf->st_rdev = le64_to_cpu(tmp_dev);
879         }
880     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
881         errno = EOPNOTSUPP;
882         return -1;
883     }
884     return err;
885 }
886 
887 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
888                        int flags, FsCred *credp, V9fsFidOpenState *fs)
889 {
890     int fd = -1;
891     int err = -1;
892     int dirfd;
893 
894     /*
895      * Mark all the open to not follow symlinks
896      */
897     flags |= O_NOFOLLOW;
898 
899     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
900     if (dirfd == -1) {
901         return -1;
902     }
903 
904     /* Determine the security model */
905     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
906         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
907         fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS);
908         if (fd == -1) {
909             goto out;
910         }
911         credp->fc_mode = credp->fc_mode|S_IFREG;
912         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
913             /* Set cleint credentials in xattr */
914             err = local_set_xattrat(dirfd, name, credp);
915         } else {
916             err = local_set_mapped_file_attrat(dirfd, name, credp);
917         }
918         if (err == -1) {
919             goto err_end;
920         }
921     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
922                (fs_ctx->export_flags & V9FS_SM_NONE)) {
923         fd = openat_file(dirfd, name, flags, credp->fc_mode);
924         if (fd == -1) {
925             goto out;
926         }
927         err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
928         if (err == -1) {
929             goto err_end;
930         }
931     }
932     err = fd;
933     fs->fd = fd;
934     goto out;
935 
936 err_end:
937     unlinkat_preserve_errno(dirfd, name,
938                             flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
939     close_preserve_errno(fd);
940 out:
941     close_preserve_errno(dirfd);
942     return err;
943 }
944 
945 
946 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
947                          V9fsPath *dir_path, const char *name, FsCred *credp)
948 {
949     int err = -1;
950     int dirfd;
951 
952     dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
953     if (dirfd == -1) {
954         return -1;
955     }
956 
957     /* Determine the security model */
958     if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
959         fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
960         int fd;
961         ssize_t oldpath_size, write_size;
962 
963         fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
964                          SM_LOCAL_MODE_BITS);
965         if (fd == -1) {
966             goto out;
967         }
968         /* Write the oldpath (target) to the file. */
969         oldpath_size = strlen(oldpath);
970         do {
971             write_size = write(fd, (void *)oldpath, oldpath_size);
972         } while (write_size == -1 && errno == EINTR);
973         close_preserve_errno(fd);
974 
975         if (write_size != oldpath_size) {
976             goto err_end;
977         }
978         /* Set cleint credentials in symlink's xattr */
979         credp->fc_mode = credp->fc_mode | S_IFLNK;
980 
981         if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
982             err = local_set_xattrat(dirfd, name, credp);
983         } else {
984             err = local_set_mapped_file_attrat(dirfd, name, credp);
985         }
986         if (err == -1) {
987             goto err_end;
988         }
989     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
990                fs_ctx->export_flags & V9FS_SM_NONE) {
991         err = symlinkat(oldpath, dirfd, name);
992         if (err) {
993             goto out;
994         }
995         err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
996                        AT_SYMLINK_NOFOLLOW);
997         if (err == -1) {
998             /*
999              * If we fail to change ownership and if we are
1000              * using security model none. Ignore the error
1001              */
1002             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1003                 goto err_end;
1004             } else {
1005                 err = 0;
1006             }
1007         }
1008     }
1009     goto out;
1010 
1011 err_end:
1012     unlinkat_preserve_errno(dirfd, name, 0);
1013 out:
1014     close_preserve_errno(dirfd);
1015     return err;
1016 }
1017 
1018 static int local_link(FsContext *ctx, V9fsPath *oldpath,
1019                       V9fsPath *dirpath, const char *name)
1020 {
1021     char *odirpath = g_path_get_dirname(oldpath->data);
1022     char *oname = g_path_get_basename(oldpath->data);
1023     int ret = -1;
1024     int odirfd, ndirfd;
1025 
1026     odirfd = local_opendir_nofollow(ctx, odirpath);
1027     if (odirfd == -1) {
1028         goto out;
1029     }
1030 
1031     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
1032     if (ndirfd == -1) {
1033         close_preserve_errno(odirfd);
1034         goto out;
1035     }
1036 
1037     ret = linkat(odirfd, oname, ndirfd, name, 0);
1038     if (ret < 0) {
1039         goto out_close;
1040     }
1041 
1042     /* now link the virtfs_metadata files */
1043     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1044         int omap_dirfd, nmap_dirfd;
1045 
1046         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1047         if (ret < 0 && errno != EEXIST) {
1048             goto err_undo_link;
1049         }
1050 
1051         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1052         if (omap_dirfd == -1) {
1053             goto err;
1054         }
1055 
1056         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1057         if (nmap_dirfd == -1) {
1058             close_preserve_errno(omap_dirfd);
1059             goto err;
1060         }
1061 
1062         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1063         close_preserve_errno(nmap_dirfd);
1064         close_preserve_errno(omap_dirfd);
1065         if (ret < 0 && errno != ENOENT) {
1066             goto err_undo_link;
1067         }
1068 
1069         ret = 0;
1070     }
1071     goto out_close;
1072 
1073 err:
1074     ret = -1;
1075 err_undo_link:
1076     unlinkat_preserve_errno(ndirfd, name, 0);
1077 out_close:
1078     close_preserve_errno(ndirfd);
1079     close_preserve_errno(odirfd);
1080 out:
1081     g_free(oname);
1082     g_free(odirpath);
1083     return ret;
1084 }
1085 
1086 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1087 {
1088     int fd, ret;
1089 
1090     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1091     if (fd == -1) {
1092         return -1;
1093     }
1094     ret = ftruncate(fd, size);
1095     close_preserve_errno(fd);
1096     return ret;
1097 }
1098 
1099 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1100 {
1101     char *dirpath = g_path_get_dirname(fs_path->data);
1102     char *name = g_path_get_basename(fs_path->data);
1103     int ret = -1;
1104     int dirfd;
1105 
1106     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1107     if (dirfd == -1) {
1108         goto out;
1109     }
1110 
1111     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1112         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1113         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1114         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1115                        AT_SYMLINK_NOFOLLOW);
1116     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1117         ret = local_set_xattrat(dirfd, name, credp);
1118     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1119         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1120     }
1121 
1122     close_preserve_errno(dirfd);
1123 out:
1124     g_free(name);
1125     g_free(dirpath);
1126     return ret;
1127 }
1128 
1129 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1130                            const struct timespec *buf)
1131 {
1132     char *dirpath = g_path_get_dirname(fs_path->data);
1133     char *name = g_path_get_basename(fs_path->data);
1134     int dirfd, ret = -1;
1135 
1136     dirfd = local_opendir_nofollow(s, dirpath);
1137     if (dirfd == -1) {
1138         goto out;
1139     }
1140 
1141     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1142     close_preserve_errno(dirfd);
1143 out:
1144     g_free(dirpath);
1145     g_free(name);
1146     return ret;
1147 }
1148 
1149 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1150                                  int flags)
1151 {
1152     int ret = -1;
1153 
1154     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1155         int map_dirfd;
1156 
1157         if (flags == AT_REMOVEDIR) {
1158             int fd;
1159 
1160             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1161             if (fd == -1) {
1162                 goto err_out;
1163             }
1164             /*
1165              * If directory remove .virtfs_metadata contained in the
1166              * directory
1167              */
1168             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1169             close_preserve_errno(fd);
1170             if (ret < 0 && errno != ENOENT) {
1171                 /*
1172                  * We didn't had the .virtfs_metadata file. May be file created
1173                  * in non-mapped mode ?. Ignore ENOENT.
1174                  */
1175                 goto err_out;
1176             }
1177         }
1178         /*
1179          * Now remove the name from parent directory
1180          * .virtfs_metadata directory.
1181          */
1182         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1183         ret = unlinkat(map_dirfd, name, 0);
1184         close_preserve_errno(map_dirfd);
1185         if (ret < 0 && errno != ENOENT) {
1186             /*
1187              * We didn't had the .virtfs_metadata file. May be file created
1188              * in non-mapped mode ?. Ignore ENOENT.
1189              */
1190             goto err_out;
1191         }
1192     }
1193 
1194     ret = unlinkat(dirfd, name, flags);
1195 err_out:
1196     return ret;
1197 }
1198 
1199 static int local_remove(FsContext *ctx, const char *path)
1200 {
1201     struct stat stbuf;
1202     char *dirpath = g_path_get_dirname(path);
1203     char *name = g_path_get_basename(path);
1204     int flags = 0;
1205     int dirfd;
1206     int err = -1;
1207 
1208     dirfd = local_opendir_nofollow(ctx, dirpath);
1209     if (dirfd) {
1210         goto out;
1211     }
1212 
1213     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1214         goto err_out;
1215     }
1216 
1217     if (S_ISDIR(stbuf.st_mode)) {
1218         flags |= AT_REMOVEDIR;
1219     }
1220 
1221     err = local_unlinkat_common(ctx, dirfd, name, flags);
1222 err_out:
1223     close_preserve_errno(dirfd);
1224 out:
1225     g_free(name);
1226     g_free(dirpath);
1227     return err;
1228 }
1229 
1230 static int local_fsync(FsContext *ctx, int fid_type,
1231                        V9fsFidOpenState *fs, int datasync)
1232 {
1233     int fd;
1234 
1235     if (fid_type == P9_FID_DIR) {
1236         fd = dirfd(fs->dir.stream);
1237     } else {
1238         fd = fs->fd;
1239     }
1240 
1241     if (datasync) {
1242         return qemu_fdatasync(fd);
1243     } else {
1244         return fsync(fd);
1245     }
1246 }
1247 
1248 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1249 {
1250     int fd, ret;
1251 
1252     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1253     ret = fstatfs(fd, stbuf);
1254     close_preserve_errno(fd);
1255     return ret;
1256 }
1257 
1258 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1259                                const char *name, void *value, size_t size)
1260 {
1261     char *path = fs_path->data;
1262 
1263     return v9fs_get_xattr(ctx, path, name, value, size);
1264 }
1265 
1266 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1267                                 void *value, size_t size)
1268 {
1269     char *path = fs_path->data;
1270 
1271     return v9fs_list_xattr(ctx, path, value, size);
1272 }
1273 
1274 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1275                            void *value, size_t size, int flags)
1276 {
1277     char *path = fs_path->data;
1278 
1279     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1280 }
1281 
1282 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1283                               const char *name)
1284 {
1285     char *path = fs_path->data;
1286 
1287     return v9fs_remove_xattr(ctx, path, name);
1288 }
1289 
1290 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1291                               const char *name, V9fsPath *target)
1292 {
1293     if (dir_path) {
1294         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1295     } else {
1296         v9fs_path_sprintf(target, "%s", name);
1297     }
1298     return 0;
1299 }
1300 
1301 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1302                           const char *old_name, V9fsPath *newdir,
1303                           const char *new_name)
1304 {
1305     int ret;
1306     int odirfd, ndirfd;
1307 
1308     odirfd = local_opendir_nofollow(ctx, olddir->data);
1309     if (odirfd == -1) {
1310         return -1;
1311     }
1312 
1313     ndirfd = local_opendir_nofollow(ctx, newdir->data);
1314     if (ndirfd == -1) {
1315         close_preserve_errno(odirfd);
1316         return -1;
1317     }
1318 
1319     ret = renameat(odirfd, old_name, ndirfd, new_name);
1320     if (ret < 0) {
1321         goto out;
1322     }
1323 
1324     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1325         int omap_dirfd, nmap_dirfd;
1326 
1327         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1328         if (ret < 0 && errno != EEXIST) {
1329             goto err_undo_rename;
1330         }
1331 
1332         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1333         if (omap_dirfd == -1) {
1334             goto err;
1335         }
1336 
1337         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1338         if (nmap_dirfd == -1) {
1339             close_preserve_errno(omap_dirfd);
1340             goto err;
1341         }
1342 
1343         /* rename the .virtfs_metadata files */
1344         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1345         close_preserve_errno(nmap_dirfd);
1346         close_preserve_errno(omap_dirfd);
1347         if (ret < 0 && errno != ENOENT) {
1348             goto err_undo_rename;
1349         }
1350 
1351         ret = 0;
1352     }
1353     goto out;
1354 
1355 err:
1356     ret = -1;
1357 err_undo_rename:
1358     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1359 out:
1360     close_preserve_errno(ndirfd);
1361     close_preserve_errno(odirfd);
1362     return ret;
1363 }
1364 
1365 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1366 {
1367     path->data = g_path_get_dirname(str);
1368     path->size = strlen(path->data) + 1;
1369 }
1370 
1371 static int local_rename(FsContext *ctx, const char *oldpath,
1372                         const char *newpath)
1373 {
1374     int err;
1375     char *oname = g_path_get_basename(oldpath);
1376     char *nname = g_path_get_basename(newpath);
1377     V9fsPath olddir, newdir;
1378 
1379     v9fs_path_init_dirname(&olddir, oldpath);
1380     v9fs_path_init_dirname(&newdir, newpath);
1381 
1382     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1383 
1384     v9fs_path_free(&newdir);
1385     v9fs_path_free(&olddir);
1386     g_free(nname);
1387     g_free(oname);
1388 
1389     return err;
1390 }
1391 
1392 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1393                           const char *name, int flags)
1394 {
1395     int ret;
1396     int dirfd;
1397 
1398     dirfd = local_opendir_nofollow(ctx, dir->data);
1399     if (dirfd == -1) {
1400         return -1;
1401     }
1402 
1403     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1404     close_preserve_errno(dirfd);
1405     return ret;
1406 }
1407 
1408 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1409                                 mode_t st_mode, uint64_t *st_gen)
1410 {
1411 #ifdef FS_IOC_GETVERSION
1412     int err;
1413     V9fsFidOpenState fid_open;
1414 
1415     /*
1416      * Do not try to open special files like device nodes, fifos etc
1417      * We can get fd for regular files and directories only
1418      */
1419     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1420         errno = ENOTTY;
1421         return -1;
1422     }
1423     err = local_open(ctx, path, O_RDONLY, &fid_open);
1424     if (err < 0) {
1425         return err;
1426     }
1427     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1428     local_close(ctx, &fid_open);
1429     return err;
1430 #else
1431     errno = ENOTTY;
1432     return -1;
1433 #endif
1434 }
1435 
1436 static int local_init(FsContext *ctx)
1437 {
1438     struct statfs stbuf;
1439     LocalData *data = g_malloc(sizeof(*data));
1440 
1441     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1442     if (data->mountfd == -1) {
1443         goto err;
1444     }
1445 
1446 #ifdef FS_IOC_GETVERSION
1447     /*
1448      * use ioc_getversion only if the ioctl is definied
1449      */
1450     if (fstatfs(data->mountfd, &stbuf) < 0) {
1451         close_preserve_errno(data->mountfd);
1452         goto err;
1453     }
1454     switch (stbuf.f_type) {
1455     case EXT2_SUPER_MAGIC:
1456     case BTRFS_SUPER_MAGIC:
1457     case REISERFS_SUPER_MAGIC:
1458     case XFS_SUPER_MAGIC:
1459         ctx->exops.get_st_gen = local_ioc_getversion;
1460         break;
1461     }
1462 #endif
1463 
1464     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1465         ctx->xops = passthrough_xattr_ops;
1466     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1467         ctx->xops = mapped_xattr_ops;
1468     } else if (ctx->export_flags & V9FS_SM_NONE) {
1469         ctx->xops = none_xattr_ops;
1470     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1471         /*
1472          * xattr operation for mapped-file and passthrough
1473          * remain same.
1474          */
1475         ctx->xops = passthrough_xattr_ops;
1476     }
1477     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1478 
1479     ctx->private = data;
1480     return 0;
1481 
1482 err:
1483     g_free(data);
1484     return -1;
1485 }
1486 
1487 static void local_cleanup(FsContext *ctx)
1488 {
1489     LocalData *data = ctx->private;
1490 
1491     close(data->mountfd);
1492     g_free(data);
1493 }
1494 
1495 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1496 {
1497     const char *sec_model = qemu_opt_get(opts, "security_model");
1498     const char *path = qemu_opt_get(opts, "path");
1499 
1500     if (!sec_model) {
1501         error_report("Security model not specified, local fs needs security model");
1502         error_printf("valid options are:"
1503                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1504         return -1;
1505     }
1506 
1507     if (!strcmp(sec_model, "passthrough")) {
1508         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1509     } else if (!strcmp(sec_model, "mapped") ||
1510                !strcmp(sec_model, "mapped-xattr")) {
1511         fse->export_flags |= V9FS_SM_MAPPED;
1512     } else if (!strcmp(sec_model, "none")) {
1513         fse->export_flags |= V9FS_SM_NONE;
1514     } else if (!strcmp(sec_model, "mapped-file")) {
1515         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1516     } else {
1517         error_report("Invalid security model %s specified", sec_model);
1518         error_printf("valid options are:"
1519                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1520         return -1;
1521     }
1522 
1523     if (!path) {
1524         error_report("fsdev: No path specified");
1525         return -1;
1526     }
1527     fse->path = g_strdup(path);
1528 
1529     return 0;
1530 }
1531 
1532 FileOperations local_ops = {
1533     .parse_opts = local_parse_opts,
1534     .init  = local_init,
1535     .cleanup = local_cleanup,
1536     .lstat = local_lstat,
1537     .readlink = local_readlink,
1538     .close = local_close,
1539     .closedir = local_closedir,
1540     .open = local_open,
1541     .opendir = local_opendir,
1542     .rewinddir = local_rewinddir,
1543     .telldir = local_telldir,
1544     .readdir = local_readdir,
1545     .seekdir = local_seekdir,
1546     .preadv = local_preadv,
1547     .pwritev = local_pwritev,
1548     .chmod = local_chmod,
1549     .mknod = local_mknod,
1550     .mkdir = local_mkdir,
1551     .fstat = local_fstat,
1552     .open2 = local_open2,
1553     .symlink = local_symlink,
1554     .link = local_link,
1555     .truncate = local_truncate,
1556     .rename = local_rename,
1557     .chown = local_chown,
1558     .utimensat = local_utimensat,
1559     .remove = local_remove,
1560     .fsync = local_fsync,
1561     .statfs = local_statfs,
1562     .lgetxattr = local_lgetxattr,
1563     .llistxattr = local_llistxattr,
1564     .lsetxattr = local_lsetxattr,
1565     .lremovexattr = local_lremovexattr,
1566     .name_to_path = local_name_to_path,
1567     .renameat  = local_renameat,
1568     .unlinkat = local_unlinkat,
1569 };
1570