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
44 typedef struct HandleData
{
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 HandleData
*data
= (HandleData
*) 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 HandleData
*data
= (HandleData
*) 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
.stream
);
118 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
119 int flags
, V9fsFidOpenState
*fs
)
121 HandleData
*data
= (HandleData
*) 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
.stream
= fdopendir(ret
);
136 if (!fs
->dir
.stream
) {
142 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
144 rewinddir(fs
->dir
.stream
);
147 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
149 return telldir(fs
->dir
.stream
);
152 static struct dirent
*handle_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
154 return readdir(fs
->dir
.stream
);
157 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
159 seekdir(fs
->dir
.stream
, off
);
162 static ssize_t
handle_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
163 const struct iovec
*iov
,
164 int iovcnt
, off_t offset
)
167 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
169 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
173 return readv(fs
->fd
, iov
, iovcnt
);
178 static ssize_t
handle_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
179 const struct iovec
*iov
,
180 int iovcnt
, off_t offset
)
184 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
186 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
190 ret
= writev(fs
->fd
, iov
, iovcnt
);
193 #ifdef CONFIG_SYNC_FILE_RANGE
194 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
196 * Initiate a writeback. This is not a data integrity sync.
197 * We want to ensure that we don't leave dirty pages in the cache
198 * after write when writeout=immediate is sepcified.
200 sync_file_range(fs
->fd
, offset
, ret
,
201 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
207 static int handle_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
210 HandleData
*data
= (HandleData
*) fs_ctx
->private;
212 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
216 ret
= fchmod(fd
, credp
->fc_mode
);
221 static int handle_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
222 const char *name
, FsCred
*credp
)
225 HandleData
*data
= (HandleData
*) fs_ctx
->private;
227 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
231 ret
= mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
233 ret
= handle_update_file_cred(dirfd
, name
, credp
);
239 static int handle_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
240 const char *name
, FsCred
*credp
)
243 HandleData
*data
= (HandleData
*) fs_ctx
->private;
245 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
249 ret
= mkdirat(dirfd
, name
, credp
->fc_mode
);
251 ret
= handle_update_file_cred(dirfd
, name
, credp
);
257 static int handle_fstat(FsContext
*fs_ctx
, int fid_type
,
258 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
262 if (fid_type
== P9_FID_DIR
) {
263 fd
= dirfd(fs
->dir
.stream
);
267 return fstat(fd
, stbuf
);
270 static int handle_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
271 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
275 HandleData
*data
= (HandleData
*) fs_ctx
->private;
277 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
281 fd
= openat(dirfd
, name
, flags
| O_NOFOLLOW
, credp
->fc_mode
);
283 ret
= handle_update_file_cred(dirfd
, name
, credp
);
296 static int handle_symlink(FsContext
*fs_ctx
, const char *oldpath
,
297 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
300 HandleData
*data
= (HandleData
*) fs_ctx
->private;
302 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
306 ret
= symlinkat(oldpath
, dirfd
, name
);
308 fd
= openat(dirfd
, name
, O_PATH
| O_NOFOLLOW
);
313 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
321 static int handle_link(FsContext
*ctx
, V9fsPath
*oldpath
,
322 V9fsPath
*dirpath
, const char *name
)
324 int oldfd
, newdirfd
, ret
;
325 HandleData
*data
= (HandleData
*) ctx
->private;
327 oldfd
= open_by_handle(data
->mountfd
, oldpath
->data
, O_PATH
);
331 newdirfd
= open_by_handle(data
->mountfd
, dirpath
->data
, O_PATH
);
336 ret
= linkat(oldfd
, "", newdirfd
, name
, AT_EMPTY_PATH
);
342 static int handle_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
345 HandleData
*data
= (HandleData
*) ctx
->private;
347 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
| O_WRONLY
);
351 ret
= ftruncate(fd
, size
);
356 static int handle_rename(FsContext
*ctx
, const char *oldpath
,
363 static int handle_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
366 HandleData
*data
= (HandleData
*) fs_ctx
->private;
368 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
372 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
377 static int handle_utimensat(FsContext
*ctx
, V9fsPath
*fs_path
,
378 const struct timespec
*buf
)
382 HandleData
*data
= (HandleData
*) ctx
->private;
384 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
388 ret
= futimens(fd
, buf
);
393 static int handle_remove(FsContext
*ctx
, const char *path
)
399 static int handle_fsync(FsContext
*ctx
, int fid_type
,
400 V9fsFidOpenState
*fs
, int datasync
)
404 if (fid_type
== P9_FID_DIR
) {
405 fd
= dirfd(fs
->dir
.stream
);
411 return qemu_fdatasync(fd
);
417 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
418 struct statfs
*stbuf
)
421 HandleData
*data
= (HandleData
*) ctx
->private;
423 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
427 ret
= fstatfs(fd
, stbuf
);
432 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
433 const char *name
, void *value
, size_t size
)
436 HandleData
*data
= (HandleData
*) ctx
->private;
438 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
442 ret
= fgetxattr(fd
, name
, value
, size
);
447 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
448 void *value
, size_t size
)
451 HandleData
*data
= (HandleData
*) ctx
->private;
453 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
457 ret
= flistxattr(fd
, value
, size
);
462 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
463 void *value
, size_t size
, int flags
)
466 HandleData
*data
= (HandleData
*) ctx
->private;
468 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
472 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
477 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
481 HandleData
*data
= (HandleData
*) ctx
->private;
483 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
487 ret
= fremovexattr(fd
, name
);
492 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
493 const char *name
, V9fsPath
*target
)
496 struct file_handle
*fh
;
497 int dirfd
, ret
, mnt_id
;
498 HandleData
*data
= (HandleData
*) ctx
->private;
500 /* "." and ".." are not allowed */
501 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
507 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
509 /* relative to export root */
510 buffer
= rpath(ctx
, ".");
511 dirfd
= open(buffer
, O_DIRECTORY
);
517 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
518 fh
->handle_bytes
= data
->handle_bytes
;
519 /* add a "./" at the beginning of the path */
520 buffer
= g_strdup_printf("./%s", name
);
521 /* flag = 0 imply don't follow symlink */
522 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
524 target
->data
= (char *)fh
;
525 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
534 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
535 const char *old_name
, V9fsPath
*newdir
,
536 const char *new_name
)
538 int olddirfd
, newdirfd
, ret
;
539 HandleData
*data
= (HandleData
*) ctx
->private;
541 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
545 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
550 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
556 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
557 const char *name
, int flags
)
560 HandleData
*data
= (HandleData
*) ctx
->private;
563 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
569 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
570 rflags
|= AT_REMOVEDIR
;
573 ret
= unlinkat(dirfd
, name
, rflags
);
579 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
580 mode_t st_mode
, uint64_t *st_gen
)
582 #ifdef FS_IOC_GETVERSION
584 V9fsFidOpenState fid_open
;
587 * Do not try to open special files like device nodes, fifos etc
588 * We can get fd for regular files and directories only
590 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
594 err
= handle_open(ctx
, path
, O_RDONLY
, &fid_open
);
598 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
599 handle_close(ctx
, &fid_open
);
607 static int handle_init(FsContext
*ctx
, Error
**errp
)
611 struct file_handle fh
;
612 HandleData
*data
= g_malloc(sizeof(HandleData
));
614 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
615 if (data
->mountfd
< 0) {
619 ret
= statfs(ctx
->fs_root
, &stbuf
);
621 switch (stbuf
.f_type
) {
622 case EXT2_SUPER_MAGIC
:
623 case BTRFS_SUPER_MAGIC
:
624 case REISERFS_SUPER_MAGIC
:
625 case XFS_SUPER_MAGIC
:
626 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
630 memset(&fh
, 0, sizeof(struct file_handle
));
631 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
632 if (ret
&& errno
== EOVERFLOW
) {
633 data
->handle_bytes
= fh
.handle_bytes
;
638 /* we got 0 byte handle ? */
640 close(data
->mountfd
);
647 static void handle_cleanup(FsContext
*ctx
)
649 HandleData
*data
= ctx
->private;
651 close(data
->mountfd
);
655 static int handle_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fse
, Error
**errp
)
657 const char *sec_model
= qemu_opt_get(opts
, "security_model");
658 const char *path
= qemu_opt_get(opts
, "path");
660 warn_report("handle backend is deprecated");
663 error_report("Invalid argument security_model specified with handle fsdriver");
668 error_report("fsdev: No path specified");
671 fse
->path
= g_strdup(path
);
676 FileOperations handle_ops
= {
677 .parse_opts
= handle_parse_opts
,
679 .cleanup
= handle_cleanup
,
680 .lstat
= handle_lstat
,
681 .readlink
= handle_readlink
,
682 .close
= handle_close
,
683 .closedir
= handle_closedir
,
685 .opendir
= handle_opendir
,
686 .rewinddir
= handle_rewinddir
,
687 .telldir
= handle_telldir
,
688 .readdir
= handle_readdir
,
689 .seekdir
= handle_seekdir
,
690 .preadv
= handle_preadv
,
691 .pwritev
= handle_pwritev
,
692 .chmod
= handle_chmod
,
693 .mknod
= handle_mknod
,
694 .mkdir
= handle_mkdir
,
695 .fstat
= handle_fstat
,
696 .open2
= handle_open2
,
697 .symlink
= handle_symlink
,
699 .truncate
= handle_truncate
,
700 .rename
= handle_rename
,
701 .chown
= handle_chown
,
702 .utimensat
= handle_utimensat
,
703 .remove
= handle_remove
,
704 .fsync
= handle_fsync
,
705 .statfs
= handle_statfs
,
706 .lgetxattr
= handle_lgetxattr
,
707 .llistxattr
= handle_llistxattr
,
708 .lsetxattr
= handle_lsetxattr
,
709 .lremovexattr
= handle_lremovexattr
,
710 .name_to_path
= handle_name_to_path
,
711 .renameat
= handle_renameat
,
712 .unlinkat
= handle_unlinkat
,