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"
25 #include "qemu/option.h"
27 #ifdef CONFIG_LINUX_MAGIC_H
28 #include <linux/magic.h>
30 #include <sys/ioctl.h>
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
45 typedef struct HandleData
{
50 static inline int name_to_handle(int dirfd
, const char *name
,
51 struct file_handle
*fh
, int *mnt_id
, int flags
)
53 return name_to_handle_at(dirfd
, name
, fh
, mnt_id
, flags
);
56 static inline int open_by_handle(int mountfd
, const char *fh
, int flags
)
58 return open_by_handle_at(mountfd
, (struct file_handle
*)fh
, flags
);
61 static int handle_update_file_cred(int dirfd
, const char *name
, FsCred
*credp
)
64 fd
= openat(dirfd
, name
, O_NONBLOCK
| O_NOFOLLOW
);
68 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
72 ret
= fchmod(fd
, credp
->fc_mode
& 07777);
79 static int handle_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
83 HandleData
*data
= (HandleData
*) fs_ctx
->private;
85 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
89 ret
= fstatat(fd
, "", stbuf
, AT_EMPTY_PATH
);
94 static ssize_t
handle_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
95 char *buf
, size_t bufsz
)
98 HandleData
*data
= (HandleData
*) fs_ctx
->private;
100 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
104 ret
= readlinkat(fd
, "", buf
, bufsz
);
109 static int handle_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
111 return close(fs
->fd
);
114 static int handle_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
116 return closedir(fs
->dir
.stream
);
119 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
120 int flags
, V9fsFidOpenState
*fs
)
122 HandleData
*data
= (HandleData
*) ctx
->private;
124 fs
->fd
= open_by_handle(data
->mountfd
, fs_path
->data
, flags
);
128 static int handle_opendir(FsContext
*ctx
,
129 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
132 ret
= handle_open(ctx
, fs_path
, O_DIRECTORY
, fs
);
136 fs
->dir
.stream
= fdopendir(ret
);
137 if (!fs
->dir
.stream
) {
143 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
145 rewinddir(fs
->dir
.stream
);
148 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
150 return telldir(fs
->dir
.stream
);
153 static struct dirent
*handle_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
155 return readdir(fs
->dir
.stream
);
158 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
160 seekdir(fs
->dir
.stream
, 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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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
) {
264 fd
= dirfd(fs
->dir
.stream
);
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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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
)
383 HandleData
*data
= (HandleData
*) ctx
->private;
385 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
389 ret
= futimens(fd
, buf
);
394 static int handle_remove(FsContext
*ctx
, const char *path
)
400 static int handle_fsync(FsContext
*ctx
, int fid_type
,
401 V9fsFidOpenState
*fs
, int datasync
)
405 if (fid_type
== P9_FID_DIR
) {
406 fd
= dirfd(fs
->dir
.stream
);
412 return qemu_fdatasync(fd
);
418 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
419 struct statfs
*stbuf
)
422 HandleData
*data
= (HandleData
*) ctx
->private;
424 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
428 ret
= fstatfs(fd
, stbuf
);
433 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
434 const char *name
, void *value
, size_t size
)
437 HandleData
*data
= (HandleData
*) ctx
->private;
439 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
443 ret
= fgetxattr(fd
, name
, value
, size
);
448 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
449 void *value
, size_t size
)
452 HandleData
*data
= (HandleData
*) ctx
->private;
454 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
458 ret
= flistxattr(fd
, value
, size
);
463 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
464 void *value
, size_t size
, int flags
)
467 HandleData
*data
= (HandleData
*) ctx
->private;
469 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
473 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
478 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
482 HandleData
*data
= (HandleData
*) ctx
->private;
484 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
488 ret
= fremovexattr(fd
, name
);
493 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
494 const char *name
, V9fsPath
*target
)
497 struct file_handle
*fh
;
498 int dirfd
, ret
, mnt_id
;
499 HandleData
*data
= (HandleData
*) ctx
->private;
501 /* "." and ".." are not allowed */
502 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
508 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
510 /* relative to export root */
511 buffer
= rpath(ctx
, ".");
512 dirfd
= open(buffer
, O_DIRECTORY
);
518 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
519 fh
->handle_bytes
= data
->handle_bytes
;
520 /* add a "./" at the beginning of the path */
521 buffer
= g_strdup_printf("./%s", name
);
522 /* flag = 0 imply don't follow symlink */
523 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
525 target
->data
= (char *)fh
;
526 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
535 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
536 const char *old_name
, V9fsPath
*newdir
,
537 const char *new_name
)
539 int olddirfd
, newdirfd
, ret
;
540 HandleData
*data
= (HandleData
*) ctx
->private;
542 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
546 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
551 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
557 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
558 const char *name
, int flags
)
561 HandleData
*data
= (HandleData
*) ctx
->private;
564 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
570 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
571 rflags
|= AT_REMOVEDIR
;
574 ret
= unlinkat(dirfd
, name
, rflags
);
580 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
581 mode_t st_mode
, uint64_t *st_gen
)
583 #ifdef FS_IOC_GETVERSION
585 V9fsFidOpenState fid_open
;
588 * Do not try to open special files like device nodes, fifos etc
589 * We can get fd for regular files and directories only
591 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
);
608 static int handle_init(FsContext
*ctx
, Error
**errp
)
612 struct file_handle fh
;
613 HandleData
*data
= g_malloc(sizeof(HandleData
));
615 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
616 if (data
->mountfd
< 0) {
620 ret
= statfs(ctx
->fs_root
, &stbuf
);
622 switch (stbuf
.f_type
) {
623 case EXT2_SUPER_MAGIC
:
624 case BTRFS_SUPER_MAGIC
:
625 case REISERFS_SUPER_MAGIC
:
626 case XFS_SUPER_MAGIC
:
627 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
631 memset(&fh
, 0, sizeof(struct file_handle
));
632 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
633 if (ret
&& errno
== EOVERFLOW
) {
634 data
->handle_bytes
= fh
.handle_bytes
;
639 /* we got 0 byte handle ? */
641 close(data
->mountfd
);
648 static void handle_cleanup(FsContext
*ctx
)
650 HandleData
*data
= ctx
->private;
652 close(data
->mountfd
);
656 static int handle_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fse
, Error
**errp
)
658 const char *sec_model
= qemu_opt_get(opts
, "security_model");
659 const char *path
= qemu_opt_get(opts
, "path");
661 warn_report("handle backend is deprecated");
664 error_report("Invalid argument security_model specified with handle fsdriver");
669 error_report("fsdev: No path specified");
672 fse
->path
= g_strdup(path
);
677 FileOperations handle_ops
= {
678 .parse_opts
= handle_parse_opts
,
680 .cleanup
= handle_cleanup
,
681 .lstat
= handle_lstat
,
682 .readlink
= handle_readlink
,
683 .close
= handle_close
,
684 .closedir
= handle_closedir
,
686 .opendir
= handle_opendir
,
687 .rewinddir
= handle_rewinddir
,
688 .telldir
= handle_telldir
,
689 .readdir
= handle_readdir
,
690 .seekdir
= handle_seekdir
,
691 .preadv
= handle_preadv
,
692 .pwritev
= handle_pwritev
,
693 .chmod
= handle_chmod
,
694 .mknod
= handle_mknod
,
695 .mkdir
= handle_mkdir
,
696 .fstat
= handle_fstat
,
697 .open2
= handle_open2
,
698 .symlink
= handle_symlink
,
700 .truncate
= handle_truncate
,
701 .rename
= handle_rename
,
702 .chown
= handle_chown
,
703 .utimensat
= handle_utimensat
,
704 .remove
= handle_remove
,
705 .fsync
= handle_fsync
,
706 .statfs
= handle_statfs
,
707 .lgetxattr
= handle_lgetxattr
,
708 .llistxattr
= handle_llistxattr
,
709 .lsetxattr
= handle_lsetxattr
,
710 .lremovexattr
= handle_lremovexattr
,
711 .name_to_path
= handle_name_to_path
,
712 .renameat
= handle_renameat
,
713 .unlinkat
= handle_unlinkat
,