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 "qemu/osdep.h"
17 #include <arpa/inet.h>
20 #include <sys/socket.h>
22 #include "qemu/xattr.h"
23 #include "qemu/cutils.h"
24 #include "qemu/error-report.h"
26 #ifdef CONFIG_LINUX_MAGIC_H
27 #include <linux/magic.h>
29 #include <sys/ioctl.h>
31 #ifndef XFS_SUPER_MAGIC
32 #define XFS_SUPER_MAGIC 0x58465342
34 #ifndef EXT2_SUPER_MAGIC
35 #define EXT2_SUPER_MAGIC 0xEF53
37 #ifndef REISERFS_SUPER_MAGIC
38 #define REISERFS_SUPER_MAGIC 0x52654973
40 #ifndef BTRFS_SUPER_MAGIC
41 #define BTRFS_SUPER_MAGIC 0x9123683E
49 static inline int name_to_handle(int dirfd
, const char *name
,
50 struct file_handle
*fh
, int *mnt_id
, int flags
)
52 return name_to_handle_at(dirfd
, name
, fh
, mnt_id
, flags
);
55 static inline int open_by_handle(int mountfd
, const char *fh
, int flags
)
57 return open_by_handle_at(mountfd
, (struct file_handle
*)fh
, flags
);
60 static int handle_update_file_cred(int dirfd
, const char *name
, FsCred
*credp
)
63 fd
= openat(dirfd
, name
, O_NONBLOCK
| O_NOFOLLOW
);
67 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
71 ret
= fchmod(fd
, credp
->fc_mode
& 07777);
78 static int handle_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
82 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
84 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
88 ret
= fstatat(fd
, "", stbuf
, AT_EMPTY_PATH
);
93 static ssize_t
handle_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
94 char *buf
, size_t bufsz
)
97 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
99 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
103 ret
= readlinkat(fd
, "", buf
, bufsz
);
108 static int handle_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
110 return close(fs
->fd
);
113 static int handle_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
115 return closedir(fs
->dir
);
118 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
119 int flags
, V9fsFidOpenState
*fs
)
121 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
123 fs
->fd
= open_by_handle(data
->mountfd
, fs_path
->data
, flags
);
127 static int handle_opendir(FsContext
*ctx
,
128 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
131 ret
= handle_open(ctx
, fs_path
, O_DIRECTORY
, fs
);
135 fs
->dir
= fdopendir(ret
);
142 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
147 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
149 return telldir(fs
->dir
);
152 static int handle_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
153 struct dirent
*entry
,
154 struct dirent
**result
)
156 return readdir_r(fs
->dir
, entry
, result
);
159 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
161 seekdir(fs
->dir
, off
);
164 static ssize_t
handle_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
165 const struct iovec
*iov
,
166 int iovcnt
, off_t offset
)
169 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
171 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
175 return readv(fs
->fd
, iov
, iovcnt
);
180 static ssize_t
handle_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
181 const struct iovec
*iov
,
182 int iovcnt
, off_t offset
)
186 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
188 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
192 ret
= writev(fs
->fd
, iov
, iovcnt
);
195 #ifdef CONFIG_SYNC_FILE_RANGE
196 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
198 * Initiate a writeback. This is not a data integrity sync.
199 * We want to ensure that we don't leave dirty pages in the cache
200 * after write when writeout=immediate is sepcified.
202 sync_file_range(fs
->fd
, offset
, ret
,
203 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
209 static int handle_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
212 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
214 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
218 ret
= fchmod(fd
, credp
->fc_mode
);
223 static int handle_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
224 const char *name
, FsCred
*credp
)
227 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
229 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
233 ret
= mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
235 ret
= handle_update_file_cred(dirfd
, name
, credp
);
241 static int handle_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
242 const char *name
, FsCred
*credp
)
245 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
247 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
251 ret
= mkdirat(dirfd
, name
, credp
->fc_mode
);
253 ret
= handle_update_file_cred(dirfd
, name
, credp
);
259 static int handle_fstat(FsContext
*fs_ctx
, int fid_type
,
260 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
264 if (fid_type
== P9_FID_DIR
) {
269 return fstat(fd
, stbuf
);
272 static int handle_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
273 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
277 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
279 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
283 fd
= openat(dirfd
, name
, flags
| O_NOFOLLOW
, credp
->fc_mode
);
285 ret
= handle_update_file_cred(dirfd
, name
, credp
);
298 static int handle_symlink(FsContext
*fs_ctx
, const char *oldpath
,
299 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
302 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
304 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
308 ret
= symlinkat(oldpath
, dirfd
, name
);
310 fd
= openat(dirfd
, name
, O_PATH
| O_NOFOLLOW
);
315 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
323 static int handle_link(FsContext
*ctx
, V9fsPath
*oldpath
,
324 V9fsPath
*dirpath
, const char *name
)
326 int oldfd
, newdirfd
, ret
;
327 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
329 oldfd
= open_by_handle(data
->mountfd
, oldpath
->data
, O_PATH
);
333 newdirfd
= open_by_handle(data
->mountfd
, dirpath
->data
, O_PATH
);
338 ret
= linkat(oldfd
, "", newdirfd
, name
, AT_EMPTY_PATH
);
344 static int handle_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
347 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
349 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
| O_WRONLY
);
353 ret
= ftruncate(fd
, size
);
358 static int handle_rename(FsContext
*ctx
, const char *oldpath
,
365 static int handle_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
368 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
370 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
374 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
379 static int handle_utimensat(FsContext
*ctx
, V9fsPath
*fs_path
,
380 const struct timespec
*buf
)
383 #ifdef CONFIG_UTIMENSAT
385 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
387 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
391 ret
= futimens(fd
, buf
);
400 static int handle_remove(FsContext
*ctx
, const char *path
)
406 static int handle_fsync(FsContext
*ctx
, int fid_type
,
407 V9fsFidOpenState
*fs
, int datasync
)
411 if (fid_type
== P9_FID_DIR
) {
418 return qemu_fdatasync(fd
);
424 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
425 struct statfs
*stbuf
)
428 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
430 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
434 ret
= fstatfs(fd
, stbuf
);
439 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
440 const char *name
, void *value
, size_t size
)
443 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
445 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
449 ret
= fgetxattr(fd
, name
, value
, size
);
454 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
455 void *value
, size_t size
)
458 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
460 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
464 ret
= flistxattr(fd
, value
, size
);
469 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
470 void *value
, size_t size
, int flags
)
473 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
475 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
479 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
484 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
488 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
490 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
494 ret
= fremovexattr(fd
, name
);
499 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
500 const char *name
, V9fsPath
*target
)
503 struct file_handle
*fh
;
504 int dirfd
, ret
, mnt_id
;
505 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
507 /* "." and ".." are not allowed */
508 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
514 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
516 /* relative to export root */
517 buffer
= rpath(ctx
, ".");
518 dirfd
= open(buffer
, O_DIRECTORY
);
524 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
525 fh
->handle_bytes
= data
->handle_bytes
;
526 /* add a "./" at the beginning of the path */
527 buffer
= g_strdup_printf("./%s", name
);
528 /* flag = 0 imply don't follow symlink */
529 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
531 target
->data
= (char *)fh
;
532 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
541 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
542 const char *old_name
, V9fsPath
*newdir
,
543 const char *new_name
)
545 int olddirfd
, newdirfd
, ret
;
546 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
548 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
552 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
557 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
563 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
564 const char *name
, int flags
)
567 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
570 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
576 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
577 rflags
|= AT_REMOVEDIR
;
580 ret
= unlinkat(dirfd
, name
, rflags
);
586 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
587 mode_t st_mode
, uint64_t *st_gen
)
589 #ifdef FS_IOC_GETVERSION
591 V9fsFidOpenState fid_open
;
594 * Do not try to open special files like device nodes, fifos etc
595 * We can get fd for regular files and directories only
597 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
601 err
= handle_open(ctx
, path
, O_RDONLY
, &fid_open
);
605 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
606 handle_close(ctx
, &fid_open
);
614 static int handle_init(FsContext
*ctx
)
618 struct file_handle fh
;
619 struct handle_data
*data
= g_malloc(sizeof(struct handle_data
));
621 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
622 if (data
->mountfd
< 0) {
626 ret
= statfs(ctx
->fs_root
, &stbuf
);
628 switch (stbuf
.f_type
) {
629 case EXT2_SUPER_MAGIC
:
630 case BTRFS_SUPER_MAGIC
:
631 case REISERFS_SUPER_MAGIC
:
632 case XFS_SUPER_MAGIC
:
633 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
637 memset(&fh
, 0, sizeof(struct file_handle
));
638 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
639 if (ret
&& errno
== EOVERFLOW
) {
640 data
->handle_bytes
= fh
.handle_bytes
;
645 /* we got 0 byte handle ? */
647 close(data
->mountfd
);
654 static int handle_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
656 const char *sec_model
= qemu_opt_get(opts
, "security_model");
657 const char *path
= qemu_opt_get(opts
, "path");
660 error_report("Invalid argument security_model specified with handle fsdriver");
665 error_report("fsdev: No path specified");
668 fse
->path
= g_strdup(path
);
673 FileOperations handle_ops
= {
674 .parse_opts
= handle_parse_opts
,
676 .lstat
= handle_lstat
,
677 .readlink
= handle_readlink
,
678 .close
= handle_close
,
679 .closedir
= handle_closedir
,
681 .opendir
= handle_opendir
,
682 .rewinddir
= handle_rewinddir
,
683 .telldir
= handle_telldir
,
684 .readdir_r
= handle_readdir_r
,
685 .seekdir
= handle_seekdir
,
686 .preadv
= handle_preadv
,
687 .pwritev
= handle_pwritev
,
688 .chmod
= handle_chmod
,
689 .mknod
= handle_mknod
,
690 .mkdir
= handle_mkdir
,
691 .fstat
= handle_fstat
,
692 .open2
= handle_open2
,
693 .symlink
= handle_symlink
,
695 .truncate
= handle_truncate
,
696 .rename
= handle_rename
,
697 .chown
= handle_chown
,
698 .utimensat
= handle_utimensat
,
699 .remove
= handle_remove
,
700 .fsync
= handle_fsync
,
701 .statfs
= handle_statfs
,
702 .lgetxattr
= handle_lgetxattr
,
703 .llistxattr
= handle_llistxattr
,
704 .lsetxattr
= handle_lsetxattr
,
705 .lremovexattr
= handle_lremovexattr
,
706 .name_to_path
= handle_name_to_path
,
707 .renameat
= handle_renameat
,
708 .unlinkat
= handle_unlinkat
,