2 * Virtio 9p handle callback
4 * Copyright IBM, Corp. 2011
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "hw/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
20 #include <sys/socket.h>
22 #include "qemu-xattr.h"
25 #ifdef CONFIG_LINUX_MAGIC_H
26 #include <linux/magic.h>
28 #include <sys/ioctl.h>
30 #ifndef XFS_SUPER_MAGIC
31 #define XFS_SUPER_MAGIC 0x58465342
33 #ifndef EXT2_SUPER_MAGIC
34 #define EXT2_SUPER_MAGIC 0xEF53
36 #ifndef REISERFS_SUPER_MAGIC
37 #define REISERFS_SUPER_MAGIC 0x52654973
39 #ifndef BTRFS_SUPER_MAGIC
40 #define BTRFS_SUPER_MAGIC 0x9123683E
48 static inline int name_to_handle(int dirfd
, const char *name
,
49 struct file_handle
*fh
, int *mnt_id
, int flags
)
51 return name_to_handle_at(dirfd
, name
, fh
, mnt_id
, flags
);
54 static inline int open_by_handle(int mountfd
, const char *fh
, int flags
)
56 return open_by_handle_at(mountfd
, (struct file_handle
*)fh
, flags
);
59 static int handle_update_file_cred(int dirfd
, const char *name
, FsCred
*credp
)
62 fd
= openat(dirfd
, name
, O_NONBLOCK
| O_NOFOLLOW
);
66 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
70 ret
= fchmod(fd
, credp
->fc_mode
& 07777);
77 static int handle_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
81 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
83 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
87 ret
= fstatat(fd
, "", stbuf
, AT_EMPTY_PATH
);
92 static ssize_t
handle_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
93 char *buf
, size_t bufsz
)
96 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
98 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
102 ret
= readlinkat(fd
, "", buf
, bufsz
);
107 static int handle_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
109 return close(fs
->fd
);
112 static int handle_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
114 return closedir(fs
->dir
);
117 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
118 int flags
, V9fsFidOpenState
*fs
)
120 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
122 fs
->fd
= open_by_handle(data
->mountfd
, fs_path
->data
, flags
);
126 static int handle_opendir(FsContext
*ctx
,
127 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
130 ret
= handle_open(ctx
, fs_path
, O_DIRECTORY
, fs
);
134 fs
->dir
= fdopendir(ret
);
141 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
143 return rewinddir(fs
->dir
);
146 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
148 return telldir(fs
->dir
);
151 static int handle_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
152 struct dirent
*entry
,
153 struct dirent
**result
)
155 return readdir_r(fs
->dir
, entry
, result
);
158 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
160 return seekdir(fs
->dir
, off
);
163 static ssize_t
handle_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
164 const struct iovec
*iov
,
165 int iovcnt
, off_t offset
)
168 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
170 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
174 return readv(fs
->fd
, iov
, iovcnt
);
179 static ssize_t
handle_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
180 const struct iovec
*iov
,
181 int iovcnt
, off_t offset
)
185 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
187 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
191 ret
= writev(fs
->fd
, iov
, iovcnt
);
194 #ifdef CONFIG_SYNC_FILE_RANGE
195 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
197 * Initiate a writeback. This is not a data integrity sync.
198 * We want to ensure that we don't leave dirty pages in the cache
199 * after write when writeout=immediate is sepcified.
201 sync_file_range(fs
->fd
, offset
, ret
,
202 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
208 static int handle_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
211 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
213 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
217 ret
= fchmod(fd
, credp
->fc_mode
);
222 static int handle_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
223 const char *name
, FsCred
*credp
)
226 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
228 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
232 ret
= mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
234 ret
= handle_update_file_cred(dirfd
, name
, credp
);
240 static int handle_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
241 const char *name
, FsCred
*credp
)
244 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
246 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
250 ret
= mkdirat(dirfd
, name
, credp
->fc_mode
);
252 ret
= handle_update_file_cred(dirfd
, name
, credp
);
258 static int handle_fstat(FsContext
*fs_ctx
, int fid_type
,
259 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
263 if (fid_type
== P9_FID_DIR
) {
268 return fstat(fd
, stbuf
);
271 static int handle_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
272 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
276 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
278 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
282 fd
= openat(dirfd
, name
, flags
| O_NOFOLLOW
, credp
->fc_mode
);
284 ret
= handle_update_file_cred(dirfd
, name
, credp
);
297 static int handle_symlink(FsContext
*fs_ctx
, const char *oldpath
,
298 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
301 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
303 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
307 ret
= symlinkat(oldpath
, dirfd
, name
);
309 fd
= openat(dirfd
, name
, O_PATH
| O_NOFOLLOW
);
314 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
322 static int handle_link(FsContext
*ctx
, V9fsPath
*oldpath
,
323 V9fsPath
*dirpath
, const char *name
)
325 int oldfd
, newdirfd
, ret
;
326 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
328 oldfd
= open_by_handle(data
->mountfd
, oldpath
->data
, O_PATH
);
332 newdirfd
= open_by_handle(data
->mountfd
, dirpath
->data
, O_PATH
);
337 ret
= linkat(oldfd
, "", newdirfd
, name
, AT_EMPTY_PATH
);
343 static int handle_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
346 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
348 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
| O_WRONLY
);
352 ret
= ftruncate(fd
, size
);
357 static int handle_rename(FsContext
*ctx
, const char *oldpath
,
364 static int handle_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
367 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
369 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
373 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
378 static int handle_utimensat(FsContext
*ctx
, V9fsPath
*fs_path
,
379 const struct timespec
*buf
)
382 #ifdef CONFIG_UTIMENSAT
384 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
386 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
390 ret
= futimens(fd
, buf
);
399 static int handle_remove(FsContext
*ctx
, const char *path
)
405 static int handle_fsync(FsContext
*ctx
, int fid_type
,
406 V9fsFidOpenState
*fs
, int datasync
)
410 if (fid_type
== P9_FID_DIR
) {
417 return qemu_fdatasync(fd
);
423 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
424 struct statfs
*stbuf
)
427 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
429 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
433 ret
= fstatfs(fd
, stbuf
);
438 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
439 const char *name
, void *value
, size_t size
)
442 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
444 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
448 ret
= fgetxattr(fd
, name
, value
, size
);
453 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
454 void *value
, size_t size
)
457 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
459 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
463 ret
= flistxattr(fd
, value
, size
);
468 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
469 void *value
, size_t size
, int flags
)
472 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
474 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
478 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
483 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
487 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
489 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
493 ret
= fremovexattr(fd
, name
);
498 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
499 const char *name
, V9fsPath
*target
)
501 char buffer
[PATH_MAX
];
502 struct file_handle
*fh
;
503 int dirfd
, ret
, mnt_id
;
504 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
506 /* "." and ".." are not allowed */
507 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
513 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
515 /* relative to export root */
516 dirfd
= open(rpath(ctx
, ".", buffer
), O_DIRECTORY
);
521 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
522 fh
->handle_bytes
= data
->handle_bytes
;
523 /* add a "./" at the beginning of the path */
524 snprintf(buffer
, PATH_MAX
, "./%s", name
);
525 /* flag = 0 imply don't follow symlink */
526 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
528 target
->data
= (char *)fh
;
529 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
537 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
538 const char *old_name
, V9fsPath
*newdir
,
539 const char *new_name
)
541 int olddirfd
, newdirfd
, ret
;
542 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
544 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
548 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
553 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
559 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
560 const char *name
, int flags
)
563 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
566 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
572 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
573 rflags
|= AT_REMOVEDIR
;
576 ret
= unlinkat(dirfd
, name
, rflags
);
582 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
583 mode_t st_mode
, uint64_t *st_gen
)
586 V9fsFidOpenState fid_open
;
589 * Do not try to open special files like device nodes, fifos etc
590 * We can get fd for regular files and directories only
592 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
595 err
= handle_open(ctx
, path
, O_RDONLY
, &fid_open
);
599 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
600 handle_close(ctx
, &fid_open
);
604 static int handle_init(FsContext
*ctx
)
608 struct file_handle fh
;
609 struct handle_data
*data
= g_malloc(sizeof(struct handle_data
));
611 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
612 if (data
->mountfd
< 0) {
616 ret
= statfs(ctx
->fs_root
, &stbuf
);
618 switch (stbuf
.f_type
) {
619 case EXT2_SUPER_MAGIC
:
620 case BTRFS_SUPER_MAGIC
:
621 case REISERFS_SUPER_MAGIC
:
622 case XFS_SUPER_MAGIC
:
623 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
627 memset(&fh
, 0, sizeof(struct file_handle
));
628 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
629 if (ret
&& errno
== EOVERFLOW
) {
630 data
->handle_bytes
= fh
.handle_bytes
;
635 /* we got 0 byte handle ? */
637 close(data
->mountfd
);
644 static int handle_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
646 const char *sec_model
= qemu_opt_get(opts
, "security_model");
647 const char *path
= qemu_opt_get(opts
, "path");
650 fprintf(stderr
, "Invalid argument security_model specified with handle fsdriver\n");
655 fprintf(stderr
, "fsdev: No path specified.\n");
658 fse
->path
= g_strdup(path
);
663 FileOperations handle_ops
= {
664 .parse_opts
= handle_parse_opts
,
666 .lstat
= handle_lstat
,
667 .readlink
= handle_readlink
,
668 .close
= handle_close
,
669 .closedir
= handle_closedir
,
671 .opendir
= handle_opendir
,
672 .rewinddir
= handle_rewinddir
,
673 .telldir
= handle_telldir
,
674 .readdir_r
= handle_readdir_r
,
675 .seekdir
= handle_seekdir
,
676 .preadv
= handle_preadv
,
677 .pwritev
= handle_pwritev
,
678 .chmod
= handle_chmod
,
679 .mknod
= handle_mknod
,
680 .mkdir
= handle_mkdir
,
681 .fstat
= handle_fstat
,
682 .open2
= handle_open2
,
683 .symlink
= handle_symlink
,
685 .truncate
= handle_truncate
,
686 .rename
= handle_rename
,
687 .chown
= handle_chown
,
688 .utimensat
= handle_utimensat
,
689 .remove
= handle_remove
,
690 .fsync
= handle_fsync
,
691 .statfs
= handle_statfs
,
692 .lgetxattr
= handle_lgetxattr
,
693 .llistxattr
= handle_llistxattr
,
694 .lsetxattr
= handle_lsetxattr
,
695 .lremovexattr
= handle_lremovexattr
,
696 .name_to_path
= handle_name_to_path
,
697 .renameat
= handle_renameat
,
698 .unlinkat
= handle_unlinkat
,