xref: /qemu/hw/9pfs/9p-local.c (revision d369f20763a857eac544a5289a046d0285a91df8)
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 ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
547                               char *buf, size_t bufsz)
548 {
549     ssize_t tsize = -1;
550 
551     if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
552         (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
553         int fd;
554 
555         fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
556         if (fd == -1) {
557             return -1;
558         }
559         do {
560             tsize = read(fd, (void *)buf, bufsz);
561         } while (tsize == -1 && errno == EINTR);
562         close_preserve_errno(fd);
563     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
564                (fs_ctx->export_flags & V9FS_SM_NONE)) {
565         char *dirpath = g_path_get_dirname(fs_path->data);
566         char *name = g_path_get_basename(fs_path->data);
567         int dirfd;
568 
569         dirfd = local_opendir_nofollow(fs_ctx, dirpath);
570         if (dirfd == -1) {
571             goto out;
572         }
573 
574         tsize = readlinkat(dirfd, name, buf, bufsz);
575         close_preserve_errno(dirfd);
576     out:
577         g_free(name);
578         g_free(dirpath);
579     }
580     return tsize;
581 }
582 
583 static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
584 {
585     return close(fs->fd);
586 }
587 
588 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
589 {
590     return closedir(fs->dir.stream);
591 }
592 
593 static int local_open(FsContext *ctx, V9fsPath *fs_path,
594                       int flags, V9fsFidOpenState *fs)
595 {
596     int fd;
597 
598     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
599     if (fd == -1) {
600         return -1;
601     }
602     fs->fd = fd;
603     return fs->fd;
604 }
605 
606 static int local_opendir(FsContext *ctx,
607                          V9fsPath *fs_path, V9fsFidOpenState *fs)
608 {
609     int dirfd;
610     DIR *stream;
611 
612     dirfd = local_opendir_nofollow(ctx, fs_path->data);
613     if (dirfd == -1) {
614         return -1;
615     }
616 
617     stream = fdopendir(dirfd);
618     if (!stream) {
619         return -1;
620     }
621     fs->dir.stream = stream;
622     return 0;
623 }
624 
625 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
626 {
627     rewinddir(fs->dir.stream);
628 }
629 
630 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
631 {
632     return telldir(fs->dir.stream);
633 }
634 
635 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
636 {
637     struct dirent *entry;
638 
639 again:
640     entry = readdir(fs->dir.stream);
641     if (!entry) {
642         return NULL;
643     }
644 
645     if (ctx->export_flags & V9FS_SM_MAPPED) {
646         entry->d_type = DT_UNKNOWN;
647     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
648         if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
649             /* skp the meta data directory */
650             goto again;
651         }
652         entry->d_type = DT_UNKNOWN;
653     }
654 
655     return entry;
656 }
657 
658 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
659 {
660     seekdir(fs->dir.stream, off);
661 }
662 
663 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
664                             const struct iovec *iov,
665                             int iovcnt, off_t offset)
666 {
667 #ifdef CONFIG_PREADV
668     return preadv(fs->fd, iov, iovcnt, offset);
669 #else
670     int err = lseek(fs->fd, offset, SEEK_SET);
671     if (err == -1) {
672         return err;
673     } else {
674         return readv(fs->fd, iov, iovcnt);
675     }
676 #endif
677 }
678 
679 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
680                              const struct iovec *iov,
681                              int iovcnt, off_t offset)
682 {
683     ssize_t ret;
684 #ifdef CONFIG_PREADV
685     ret = pwritev(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         ret = writev(fs->fd, iov, iovcnt);
692     }
693 #endif
694 #ifdef CONFIG_SYNC_FILE_RANGE
695     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
696         /*
697          * Initiate a writeback. This is not a data integrity sync.
698          * We want to ensure that we don't leave dirty pages in the cache
699          * after write when writeout=immediate is sepcified.
700          */
701         sync_file_range(fs->fd, offset, ret,
702                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
703     }
704 #endif
705     return ret;
706 }
707 
708 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
709 {
710     char *dirpath = g_path_get_dirname(fs_path->data);
711     char *name = g_path_get_basename(fs_path->data);
712     int ret = -1;
713     int dirfd;
714 
715     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
716     if (dirfd == -1) {
717         goto out;
718     }
719 
720     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
721         ret = local_set_xattrat(dirfd, name, credp);
722     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
723         ret = local_set_mapped_file_attrat(dirfd, name, credp);
724     } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
725                fs_ctx->export_flags & V9FS_SM_NONE) {
726         ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
727     }
728     close_preserve_errno(dirfd);
729 
730 out:
731     g_free(dirpath);
732     g_free(name);
733     return ret;
734 }
735 
736 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
737                        const char *name, FsCred *credp)
738 {
739     char *path;
740     int err = -1;
741     int serrno = 0;
742     V9fsString fullname;
743     char *buffer = NULL;
744 
745     v9fs_string_init(&fullname);
746     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
747     path = fullname.data;
748 
749     /* Determine the security model */
750     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
751         buffer = rpath(fs_ctx, path);
752         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
753         if (err == -1) {
754             goto out;
755         }
756         err = local_set_xattr(buffer, credp);
757         if (err == -1) {
758             serrno = errno;
759             goto err_end;
760         }
761     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
762 
763         buffer = rpath(fs_ctx, path);
764         err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
765         if (err == -1) {
766             goto out;
767         }
768         err = local_set_mapped_file_attr(fs_ctx, path, credp);
769         if (err == -1) {
770             serrno = errno;
771             goto err_end;
772         }
773     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
774                (fs_ctx->export_flags & V9FS_SM_NONE)) {
775         buffer = rpath(fs_ctx, path);
776         err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
777         if (err == -1) {
778             goto out;
779         }
780         err = local_post_create_passthrough(fs_ctx, path, credp);
781         if (err == -1) {
782             serrno = errno;
783             goto err_end;
784         }
785     }
786     goto out;
787 
788 err_end:
789     remove(buffer);
790     errno = serrno;
791 out:
792     g_free(buffer);
793     v9fs_string_free(&fullname);
794     return err;
795 }
796 
797 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
798                        const char *name, FsCred *credp)
799 {
800     char *path;
801     int err = -1;
802     int serrno = 0;
803     V9fsString fullname;
804     char *buffer = NULL;
805 
806     v9fs_string_init(&fullname);
807     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
808     path = fullname.data;
809 
810     /* Determine the security model */
811     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
812         buffer = rpath(fs_ctx, path);
813         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
814         if (err == -1) {
815             goto out;
816         }
817         credp->fc_mode = credp->fc_mode|S_IFDIR;
818         err = local_set_xattr(buffer, credp);
819         if (err == -1) {
820             serrno = errno;
821             goto err_end;
822         }
823     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
824         buffer = rpath(fs_ctx, path);
825         err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
826         if (err == -1) {
827             goto out;
828         }
829         credp->fc_mode = credp->fc_mode|S_IFDIR;
830         err = local_set_mapped_file_attr(fs_ctx, path, credp);
831         if (err == -1) {
832             serrno = errno;
833             goto err_end;
834         }
835     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
836                (fs_ctx->export_flags & V9FS_SM_NONE)) {
837         buffer = rpath(fs_ctx, path);
838         err = mkdir(buffer, credp->fc_mode);
839         if (err == -1) {
840             goto out;
841         }
842         err = local_post_create_passthrough(fs_ctx, path, credp);
843         if (err == -1) {
844             serrno = errno;
845             goto err_end;
846         }
847     }
848     goto out;
849 
850 err_end:
851     remove(buffer);
852     errno = serrno;
853 out:
854     g_free(buffer);
855     v9fs_string_free(&fullname);
856     return err;
857 }
858 
859 static int local_fstat(FsContext *fs_ctx, int fid_type,
860                        V9fsFidOpenState *fs, struct stat *stbuf)
861 {
862     int err, fd;
863 
864     if (fid_type == P9_FID_DIR) {
865         fd = dirfd(fs->dir.stream);
866     } else {
867         fd = fs->fd;
868     }
869 
870     err = fstat(fd, stbuf);
871     if (err) {
872         return err;
873     }
874     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
875         /* Actual credentials are part of extended attrs */
876         uid_t tmp_uid;
877         gid_t tmp_gid;
878         mode_t tmp_mode;
879         dev_t tmp_dev;
880 
881         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
882             stbuf->st_uid = le32_to_cpu(tmp_uid);
883         }
884         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
885             stbuf->st_gid = le32_to_cpu(tmp_gid);
886         }
887         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
888             stbuf->st_mode = le32_to_cpu(tmp_mode);
889         }
890         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
891             stbuf->st_rdev = le64_to_cpu(tmp_dev);
892         }
893     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
894         errno = EOPNOTSUPP;
895         return -1;
896     }
897     return err;
898 }
899 
900 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
901                        int flags, FsCred *credp, V9fsFidOpenState *fs)
902 {
903     char *path;
904     int fd = -1;
905     int err = -1;
906     int serrno = 0;
907     V9fsString fullname;
908     char *buffer = NULL;
909 
910     /*
911      * Mark all the open to not follow symlinks
912      */
913     flags |= O_NOFOLLOW;
914 
915     v9fs_string_init(&fullname);
916     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
917     path = fullname.data;
918 
919     /* Determine the security model */
920     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
921         buffer = rpath(fs_ctx, path);
922         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
923         if (fd == -1) {
924             err = fd;
925             goto out;
926         }
927         credp->fc_mode = credp->fc_mode|S_IFREG;
928         /* Set cleint credentials in xattr */
929         err = local_set_xattr(buffer, credp);
930         if (err == -1) {
931             serrno = errno;
932             goto err_end;
933         }
934     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
935         buffer = rpath(fs_ctx, path);
936         fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
937         if (fd == -1) {
938             err = fd;
939             goto out;
940         }
941         credp->fc_mode = credp->fc_mode|S_IFREG;
942         /* Set client credentials in .virtfs_metadata directory files */
943         err = local_set_mapped_file_attr(fs_ctx, path, credp);
944         if (err == -1) {
945             serrno = errno;
946             goto err_end;
947         }
948     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
949                (fs_ctx->export_flags & V9FS_SM_NONE)) {
950         buffer = rpath(fs_ctx, path);
951         fd = open(buffer, flags, credp->fc_mode);
952         if (fd == -1) {
953             err = fd;
954             goto out;
955         }
956         err = local_post_create_passthrough(fs_ctx, path, credp);
957         if (err == -1) {
958             serrno = errno;
959             goto err_end;
960         }
961     }
962     err = fd;
963     fs->fd = fd;
964     goto out;
965 
966 err_end:
967     close(fd);
968     remove(buffer);
969     errno = serrno;
970 out:
971     g_free(buffer);
972     v9fs_string_free(&fullname);
973     return err;
974 }
975 
976 
977 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
978                          V9fsPath *dir_path, const char *name, FsCred *credp)
979 {
980     int err = -1;
981     int serrno = 0;
982     char *newpath;
983     V9fsString fullname;
984     char *buffer = NULL;
985 
986     v9fs_string_init(&fullname);
987     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
988     newpath = fullname.data;
989 
990     /* Determine the security model */
991     if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
992         int fd;
993         ssize_t oldpath_size, write_size;
994         buffer = rpath(fs_ctx, newpath);
995         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
996         if (fd == -1) {
997             err = fd;
998             goto out;
999         }
1000         /* Write the oldpath (target) to the file. */
1001         oldpath_size = strlen(oldpath);
1002         do {
1003             write_size = write(fd, (void *)oldpath, oldpath_size);
1004         } while (write_size == -1 && errno == EINTR);
1005 
1006         if (write_size != oldpath_size) {
1007             serrno = errno;
1008             close(fd);
1009             err = -1;
1010             goto err_end;
1011         }
1012         close(fd);
1013         /* Set cleint credentials in symlink's xattr */
1014         credp->fc_mode = credp->fc_mode|S_IFLNK;
1015         err = local_set_xattr(buffer, credp);
1016         if (err == -1) {
1017             serrno = errno;
1018             goto err_end;
1019         }
1020     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1021         int fd;
1022         ssize_t oldpath_size, write_size;
1023         buffer = rpath(fs_ctx, newpath);
1024         fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
1025         if (fd == -1) {
1026             err = fd;
1027             goto out;
1028         }
1029         /* Write the oldpath (target) to the file. */
1030         oldpath_size = strlen(oldpath);
1031         do {
1032             write_size = write(fd, (void *)oldpath, oldpath_size);
1033         } while (write_size == -1 && errno == EINTR);
1034 
1035         if (write_size != oldpath_size) {
1036             serrno = errno;
1037             close(fd);
1038             err = -1;
1039             goto err_end;
1040         }
1041         close(fd);
1042         /* Set cleint credentials in symlink's xattr */
1043         credp->fc_mode = credp->fc_mode|S_IFLNK;
1044         err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
1045         if (err == -1) {
1046             serrno = errno;
1047             goto err_end;
1048         }
1049     } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1050                (fs_ctx->export_flags & V9FS_SM_NONE)) {
1051         buffer = rpath(fs_ctx, newpath);
1052         err = symlink(oldpath, buffer);
1053         if (err) {
1054             goto out;
1055         }
1056         err = lchown(buffer, credp->fc_uid, credp->fc_gid);
1057         if (err == -1) {
1058             /*
1059              * If we fail to change ownership and if we are
1060              * using security model none. Ignore the error
1061              */
1062             if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
1063                 serrno = errno;
1064                 goto err_end;
1065             } else
1066                 err = 0;
1067         }
1068     }
1069     goto out;
1070 
1071 err_end:
1072     remove(buffer);
1073     errno = serrno;
1074 out:
1075     g_free(buffer);
1076     v9fs_string_free(&fullname);
1077     return err;
1078 }
1079 
1080 static int local_link(FsContext *ctx, V9fsPath *oldpath,
1081                       V9fsPath *dirpath, const char *name)
1082 {
1083     char *odirpath = g_path_get_dirname(oldpath->data);
1084     char *oname = g_path_get_basename(oldpath->data);
1085     int ret = -1;
1086     int odirfd, ndirfd;
1087 
1088     odirfd = local_opendir_nofollow(ctx, odirpath);
1089     if (odirfd == -1) {
1090         goto out;
1091     }
1092 
1093     ndirfd = local_opendir_nofollow(ctx, dirpath->data);
1094     if (ndirfd == -1) {
1095         close_preserve_errno(odirfd);
1096         goto out;
1097     }
1098 
1099     ret = linkat(odirfd, oname, ndirfd, name, 0);
1100     if (ret < 0) {
1101         goto out_close;
1102     }
1103 
1104     /* now link the virtfs_metadata files */
1105     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1106         int omap_dirfd, nmap_dirfd;
1107 
1108         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1109         if (ret < 0 && errno != EEXIST) {
1110             goto err_undo_link;
1111         }
1112 
1113         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1114         if (omap_dirfd == -1) {
1115             goto err;
1116         }
1117 
1118         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1119         if (nmap_dirfd == -1) {
1120             close_preserve_errno(omap_dirfd);
1121             goto err;
1122         }
1123 
1124         ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
1125         close_preserve_errno(nmap_dirfd);
1126         close_preserve_errno(omap_dirfd);
1127         if (ret < 0 && errno != ENOENT) {
1128             goto err_undo_link;
1129         }
1130 
1131         ret = 0;
1132     }
1133     goto out_close;
1134 
1135 err:
1136     ret = -1;
1137 err_undo_link:
1138     unlinkat_preserve_errno(ndirfd, name, 0);
1139 out_close:
1140     close_preserve_errno(ndirfd);
1141     close_preserve_errno(odirfd);
1142 out:
1143     g_free(oname);
1144     g_free(odirpath);
1145     return ret;
1146 }
1147 
1148 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
1149 {
1150     int fd, ret;
1151 
1152     fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1153     if (fd == -1) {
1154         return -1;
1155     }
1156     ret = ftruncate(fd, size);
1157     close_preserve_errno(fd);
1158     return ret;
1159 }
1160 
1161 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
1162 {
1163     char *dirpath = g_path_get_dirname(fs_path->data);
1164     char *name = g_path_get_basename(fs_path->data);
1165     int ret = -1;
1166     int dirfd;
1167 
1168     dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1169     if (dirfd == -1) {
1170         goto out;
1171     }
1172 
1173     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
1174         (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1175         (fs_ctx->export_flags & V9FS_SM_NONE)) {
1176         ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1177                        AT_SYMLINK_NOFOLLOW);
1178     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1179         ret = local_set_xattrat(dirfd, name, credp);
1180     } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1181         ret = local_set_mapped_file_attrat(dirfd, name, credp);
1182     }
1183 
1184     close_preserve_errno(dirfd);
1185 out:
1186     g_free(name);
1187     g_free(dirpath);
1188     return ret;
1189 }
1190 
1191 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
1192                            const struct timespec *buf)
1193 {
1194     char *dirpath = g_path_get_dirname(fs_path->data);
1195     char *name = g_path_get_basename(fs_path->data);
1196     int dirfd, ret = -1;
1197 
1198     dirfd = local_opendir_nofollow(s, dirpath);
1199     if (dirfd == -1) {
1200         goto out;
1201     }
1202 
1203     ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1204     close_preserve_errno(dirfd);
1205 out:
1206     g_free(dirpath);
1207     g_free(name);
1208     return ret;
1209 }
1210 
1211 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1212                                  int flags)
1213 {
1214     int ret = -1;
1215 
1216     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1217         int map_dirfd;
1218 
1219         if (flags == AT_REMOVEDIR) {
1220             int fd;
1221 
1222             fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1223             if (fd == -1) {
1224                 goto err_out;
1225             }
1226             /*
1227              * If directory remove .virtfs_metadata contained in the
1228              * directory
1229              */
1230             ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1231             close_preserve_errno(fd);
1232             if (ret < 0 && errno != ENOENT) {
1233                 /*
1234                  * We didn't had the .virtfs_metadata file. May be file created
1235                  * in non-mapped mode ?. Ignore ENOENT.
1236                  */
1237                 goto err_out;
1238             }
1239         }
1240         /*
1241          * Now remove the name from parent directory
1242          * .virtfs_metadata directory.
1243          */
1244         map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1245         ret = unlinkat(map_dirfd, name, 0);
1246         close_preserve_errno(map_dirfd);
1247         if (ret < 0 && errno != ENOENT) {
1248             /*
1249              * We didn't had the .virtfs_metadata file. May be file created
1250              * in non-mapped mode ?. Ignore ENOENT.
1251              */
1252             goto err_out;
1253         }
1254     }
1255 
1256     ret = unlinkat(dirfd, name, flags);
1257 err_out:
1258     return ret;
1259 }
1260 
1261 static int local_remove(FsContext *ctx, const char *path)
1262 {
1263     struct stat stbuf;
1264     char *dirpath = g_path_get_dirname(path);
1265     char *name = g_path_get_basename(path);
1266     int flags = 0;
1267     int dirfd;
1268     int err = -1;
1269 
1270     dirfd = local_opendir_nofollow(ctx, dirpath);
1271     if (dirfd) {
1272         goto out;
1273     }
1274 
1275     if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1276         goto err_out;
1277     }
1278 
1279     if (S_ISDIR(stbuf.st_mode)) {
1280         flags |= AT_REMOVEDIR;
1281     }
1282 
1283     err = local_unlinkat_common(ctx, dirfd, name, flags);
1284 err_out:
1285     close_preserve_errno(dirfd);
1286 out:
1287     g_free(name);
1288     g_free(dirpath);
1289     return err;
1290 }
1291 
1292 static int local_fsync(FsContext *ctx, int fid_type,
1293                        V9fsFidOpenState *fs, int datasync)
1294 {
1295     int fd;
1296 
1297     if (fid_type == P9_FID_DIR) {
1298         fd = dirfd(fs->dir.stream);
1299     } else {
1300         fd = fs->fd;
1301     }
1302 
1303     if (datasync) {
1304         return qemu_fdatasync(fd);
1305     } else {
1306         return fsync(fd);
1307     }
1308 }
1309 
1310 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
1311 {
1312     int fd, ret;
1313 
1314     fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1315     ret = fstatfs(fd, stbuf);
1316     close_preserve_errno(fd);
1317     return ret;
1318 }
1319 
1320 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
1321                                const char *name, void *value, size_t size)
1322 {
1323     char *path = fs_path->data;
1324 
1325     return v9fs_get_xattr(ctx, path, name, value, size);
1326 }
1327 
1328 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
1329                                 void *value, size_t size)
1330 {
1331     char *path = fs_path->data;
1332 
1333     return v9fs_list_xattr(ctx, path, value, size);
1334 }
1335 
1336 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
1337                            void *value, size_t size, int flags)
1338 {
1339     char *path = fs_path->data;
1340 
1341     return v9fs_set_xattr(ctx, path, name, value, size, flags);
1342 }
1343 
1344 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1345                               const char *name)
1346 {
1347     char *path = fs_path->data;
1348 
1349     return v9fs_remove_xattr(ctx, path, name);
1350 }
1351 
1352 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1353                               const char *name, V9fsPath *target)
1354 {
1355     if (dir_path) {
1356         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1357     } else {
1358         v9fs_path_sprintf(target, "%s", name);
1359     }
1360     return 0;
1361 }
1362 
1363 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1364                           const char *old_name, V9fsPath *newdir,
1365                           const char *new_name)
1366 {
1367     int ret;
1368     int odirfd, ndirfd;
1369 
1370     odirfd = local_opendir_nofollow(ctx, olddir->data);
1371     if (odirfd == -1) {
1372         return -1;
1373     }
1374 
1375     ndirfd = local_opendir_nofollow(ctx, newdir->data);
1376     if (ndirfd == -1) {
1377         close_preserve_errno(odirfd);
1378         return -1;
1379     }
1380 
1381     ret = renameat(odirfd, old_name, ndirfd, new_name);
1382     if (ret < 0) {
1383         goto out;
1384     }
1385 
1386     if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1387         int omap_dirfd, nmap_dirfd;
1388 
1389         ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1390         if (ret < 0 && errno != EEXIST) {
1391             goto err_undo_rename;
1392         }
1393 
1394         omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
1395         if (omap_dirfd == -1) {
1396             goto err;
1397         }
1398 
1399         nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
1400         if (nmap_dirfd == -1) {
1401             close_preserve_errno(omap_dirfd);
1402             goto err;
1403         }
1404 
1405         /* rename the .virtfs_metadata files */
1406         ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1407         close_preserve_errno(nmap_dirfd);
1408         close_preserve_errno(omap_dirfd);
1409         if (ret < 0 && errno != ENOENT) {
1410             goto err_undo_rename;
1411         }
1412 
1413         ret = 0;
1414     }
1415     goto out;
1416 
1417 err:
1418     ret = -1;
1419 err_undo_rename:
1420     renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1421 out:
1422     close_preserve_errno(ndirfd);
1423     close_preserve_errno(odirfd);
1424     return ret;
1425 }
1426 
1427 static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1428 {
1429     path->data = g_path_get_dirname(str);
1430     path->size = strlen(path->data) + 1;
1431 }
1432 
1433 static int local_rename(FsContext *ctx, const char *oldpath,
1434                         const char *newpath)
1435 {
1436     int err;
1437     char *oname = g_path_get_basename(oldpath);
1438     char *nname = g_path_get_basename(newpath);
1439     V9fsPath olddir, newdir;
1440 
1441     v9fs_path_init_dirname(&olddir, oldpath);
1442     v9fs_path_init_dirname(&newdir, newpath);
1443 
1444     err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1445 
1446     v9fs_path_free(&newdir);
1447     v9fs_path_free(&olddir);
1448     g_free(nname);
1449     g_free(oname);
1450 
1451     return err;
1452 }
1453 
1454 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1455                           const char *name, int flags)
1456 {
1457     int ret;
1458     int dirfd;
1459 
1460     dirfd = local_opendir_nofollow(ctx, dir->data);
1461     if (dirfd == -1) {
1462         return -1;
1463     }
1464 
1465     ret = local_unlinkat_common(ctx, dirfd, name, flags);
1466     close_preserve_errno(dirfd);
1467     return ret;
1468 }
1469 
1470 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1471                                 mode_t st_mode, uint64_t *st_gen)
1472 {
1473 #ifdef FS_IOC_GETVERSION
1474     int err;
1475     V9fsFidOpenState fid_open;
1476 
1477     /*
1478      * Do not try to open special files like device nodes, fifos etc
1479      * We can get fd for regular files and directories only
1480      */
1481     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1482         errno = ENOTTY;
1483         return -1;
1484     }
1485     err = local_open(ctx, path, O_RDONLY, &fid_open);
1486     if (err < 0) {
1487         return err;
1488     }
1489     err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1490     local_close(ctx, &fid_open);
1491     return err;
1492 #else
1493     errno = ENOTTY;
1494     return -1;
1495 #endif
1496 }
1497 
1498 static int local_init(FsContext *ctx)
1499 {
1500     struct statfs stbuf;
1501     LocalData *data = g_malloc(sizeof(*data));
1502 
1503     data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1504     if (data->mountfd == -1) {
1505         goto err;
1506     }
1507 
1508 #ifdef FS_IOC_GETVERSION
1509     /*
1510      * use ioc_getversion only if the ioctl is definied
1511      */
1512     if (fstatfs(data->mountfd, &stbuf) < 0) {
1513         close_preserve_errno(data->mountfd);
1514         goto err;
1515     }
1516     switch (stbuf.f_type) {
1517     case EXT2_SUPER_MAGIC:
1518     case BTRFS_SUPER_MAGIC:
1519     case REISERFS_SUPER_MAGIC:
1520     case XFS_SUPER_MAGIC:
1521         ctx->exops.get_st_gen = local_ioc_getversion;
1522         break;
1523     }
1524 #endif
1525 
1526     if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1527         ctx->xops = passthrough_xattr_ops;
1528     } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1529         ctx->xops = mapped_xattr_ops;
1530     } else if (ctx->export_flags & V9FS_SM_NONE) {
1531         ctx->xops = none_xattr_ops;
1532     } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1533         /*
1534          * xattr operation for mapped-file and passthrough
1535          * remain same.
1536          */
1537         ctx->xops = passthrough_xattr_ops;
1538     }
1539     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1540 
1541     ctx->private = data;
1542     return 0;
1543 
1544 err:
1545     g_free(data);
1546     return -1;
1547 }
1548 
1549 static void local_cleanup(FsContext *ctx)
1550 {
1551     LocalData *data = ctx->private;
1552 
1553     close(data->mountfd);
1554     g_free(data);
1555 }
1556 
1557 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1558 {
1559     const char *sec_model = qemu_opt_get(opts, "security_model");
1560     const char *path = qemu_opt_get(opts, "path");
1561 
1562     if (!sec_model) {
1563         error_report("Security model not specified, local fs needs security model");
1564         error_printf("valid options are:"
1565                      "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
1566         return -1;
1567     }
1568 
1569     if (!strcmp(sec_model, "passthrough")) {
1570         fse->export_flags |= V9FS_SM_PASSTHROUGH;
1571     } else if (!strcmp(sec_model, "mapped") ||
1572                !strcmp(sec_model, "mapped-xattr")) {
1573         fse->export_flags |= V9FS_SM_MAPPED;
1574     } else if (!strcmp(sec_model, "none")) {
1575         fse->export_flags |= V9FS_SM_NONE;
1576     } else if (!strcmp(sec_model, "mapped-file")) {
1577         fse->export_flags |= V9FS_SM_MAPPED_FILE;
1578     } else {
1579         error_report("Invalid security model %s specified", sec_model);
1580         error_printf("valid options are:"
1581                      "\t[passthrough|mapped-xattr|mapped-file|none]\n");
1582         return -1;
1583     }
1584 
1585     if (!path) {
1586         error_report("fsdev: No path specified");
1587         return -1;
1588     }
1589     fse->path = g_strdup(path);
1590 
1591     return 0;
1592 }
1593 
1594 FileOperations local_ops = {
1595     .parse_opts = local_parse_opts,
1596     .init  = local_init,
1597     .cleanup = local_cleanup,
1598     .lstat = local_lstat,
1599     .readlink = local_readlink,
1600     .close = local_close,
1601     .closedir = local_closedir,
1602     .open = local_open,
1603     .opendir = local_opendir,
1604     .rewinddir = local_rewinddir,
1605     .telldir = local_telldir,
1606     .readdir = local_readdir,
1607     .seekdir = local_seekdir,
1608     .preadv = local_preadv,
1609     .pwritev = local_pwritev,
1610     .chmod = local_chmod,
1611     .mknod = local_mknod,
1612     .mkdir = local_mkdir,
1613     .fstat = local_fstat,
1614     .open2 = local_open2,
1615     .symlink = local_symlink,
1616     .link = local_link,
1617     .truncate = local_truncate,
1618     .rename = local_rename,
1619     .chown = local_chown,
1620     .utimensat = local_utimensat,
1621     .remove = local_remove,
1622     .fsync = local_fsync,
1623     .statfs = local_statfs,
1624     .lgetxattr = local_lgetxattr,
1625     .llistxattr = local_llistxattr,
1626     .lsetxattr = local_lsetxattr,
1627     .lremovexattr = local_lremovexattr,
1628     .name_to_path = local_name_to_path,
1629     .renameat  = local_renameat,
1630     .unlinkat = local_unlinkat,
1631 };
1632