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.
16 #include <arpa/inet.h>
19 #include <sys/socket.h>
21 #include "qemu/xattr.h"
24 #ifdef CONFIG_LINUX_MAGIC_H
25 #include <linux/magic.h>
27 #include <sys/ioctl.h>
29 #ifndef XFS_SUPER_MAGIC
30 #define XFS_SUPER_MAGIC 0x58465342
32 #ifndef EXT2_SUPER_MAGIC
33 #define EXT2_SUPER_MAGIC 0xEF53
35 #ifndef REISERFS_SUPER_MAGIC
36 #define REISERFS_SUPER_MAGIC 0x52654973
38 #ifndef BTRFS_SUPER_MAGIC
39 #define BTRFS_SUPER_MAGIC 0x9123683E
47 static inline int name_to_handle(int dirfd
, const char *name
,
48 struct file_handle
*fh
, int *mnt_id
, int flags
)
50 return name_to_handle_at(dirfd
, name
, fh
, mnt_id
, flags
);
53 static inline int open_by_handle(int mountfd
, const char *fh
, int flags
)
55 return open_by_handle_at(mountfd
, (struct file_handle
*)fh
, flags
);
58 static int handle_update_file_cred(int dirfd
, const char *name
, FsCred
*credp
)
61 fd
= openat(dirfd
, name
, O_NONBLOCK
| O_NOFOLLOW
);
65 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
69 ret
= fchmod(fd
, credp
->fc_mode
& 07777);
76 static int handle_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
80 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
82 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
86 ret
= fstatat(fd
, "", stbuf
, AT_EMPTY_PATH
);
91 static ssize_t
handle_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
92 char *buf
, size_t bufsz
)
95 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
97 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
101 ret
= readlinkat(fd
, "", buf
, bufsz
);
106 static int handle_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
108 return close(fs
->fd
);
111 static int handle_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
113 return closedir(fs
->dir
);
116 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
117 int flags
, V9fsFidOpenState
*fs
)
119 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
121 fs
->fd
= open_by_handle(data
->mountfd
, fs_path
->data
, flags
);
125 static int handle_opendir(FsContext
*ctx
,
126 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
129 ret
= handle_open(ctx
, fs_path
, O_DIRECTORY
, fs
);
133 fs
->dir
= fdopendir(ret
);
140 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
145 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
147 return telldir(fs
->dir
);
150 static int handle_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
151 struct dirent
*entry
,
152 struct dirent
**result
)
154 return readdir_r(fs
->dir
, entry
, result
);
157 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
159 seekdir(fs
->dir
, 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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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
) {
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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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 struct handle_data
*data
= (struct handle_data
*)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
)
381 #ifdef CONFIG_UTIMENSAT
383 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
385 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
389 ret
= futimens(fd
, buf
);
398 static int handle_remove(FsContext
*ctx
, const char *path
)
404 static int handle_fsync(FsContext
*ctx
, int fid_type
,
405 V9fsFidOpenState
*fs
, int datasync
)
409 if (fid_type
== P9_FID_DIR
) {
416 return qemu_fdatasync(fd
);
422 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
423 struct statfs
*stbuf
)
426 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
428 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
432 ret
= fstatfs(fd
, stbuf
);
437 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
438 const char *name
, void *value
, size_t size
)
441 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
443 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
447 ret
= fgetxattr(fd
, name
, value
, size
);
452 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
453 void *value
, size_t size
)
456 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
458 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
462 ret
= flistxattr(fd
, value
, size
);
467 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
468 void *value
, size_t size
, int flags
)
471 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
473 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
477 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
482 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
486 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
488 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
492 ret
= fremovexattr(fd
, name
);
497 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
498 const char *name
, V9fsPath
*target
)
501 struct file_handle
*fh
;
502 int dirfd
, ret
, mnt_id
;
503 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
505 /* "." and ".." are not allowed */
506 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
512 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
514 /* relative to export root */
515 buffer
= rpath(ctx
, ".");
516 dirfd
= open(buffer
, O_DIRECTORY
);
522 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
523 fh
->handle_bytes
= data
->handle_bytes
;
524 /* add a "./" at the beginning of the path */
525 buffer
= g_strdup_printf("./%s", name
);
526 /* flag = 0 imply don't follow symlink */
527 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
529 target
->data
= (char *)fh
;
530 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
539 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
540 const char *old_name
, V9fsPath
*newdir
,
541 const char *new_name
)
543 int olddirfd
, newdirfd
, ret
;
544 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
546 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
550 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
555 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
561 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
562 const char *name
, int flags
)
565 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
568 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
574 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
575 rflags
|= AT_REMOVEDIR
;
578 ret
= unlinkat(dirfd
, name
, rflags
);
584 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
585 mode_t st_mode
, uint64_t *st_gen
)
587 #ifdef FS_IOC_GETVERSION
589 V9fsFidOpenState fid_open
;
592 * Do not try to open special files like device nodes, fifos etc
593 * We can get fd for regular files and directories only
595 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
599 err
= handle_open(ctx
, path
, O_RDONLY
, &fid_open
);
603 err
= ioctl(fid_open
.fd
, FS_IOC_GETVERSION
, st_gen
);
604 handle_close(ctx
, &fid_open
);
612 static int handle_init(FsContext
*ctx
)
616 struct file_handle fh
;
617 struct handle_data
*data
= g_malloc(sizeof(struct handle_data
));
619 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
620 if (data
->mountfd
< 0) {
624 ret
= statfs(ctx
->fs_root
, &stbuf
);
626 switch (stbuf
.f_type
) {
627 case EXT2_SUPER_MAGIC
:
628 case BTRFS_SUPER_MAGIC
:
629 case REISERFS_SUPER_MAGIC
:
630 case XFS_SUPER_MAGIC
:
631 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
635 memset(&fh
, 0, sizeof(struct file_handle
));
636 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
637 if (ret
&& errno
== EOVERFLOW
) {
638 data
->handle_bytes
= fh
.handle_bytes
;
643 /* we got 0 byte handle ? */
645 close(data
->mountfd
);
652 static int handle_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fse
)
654 const char *sec_model
= qemu_opt_get(opts
, "security_model");
655 const char *path
= qemu_opt_get(opts
, "path");
658 fprintf(stderr
, "Invalid argument security_model specified with handle fsdriver\n");
663 fprintf(stderr
, "fsdev: No path specified.\n");
666 fse
->path
= g_strdup(path
);
671 FileOperations handle_ops
= {
672 .parse_opts
= handle_parse_opts
,
674 .lstat
= handle_lstat
,
675 .readlink
= handle_readlink
,
676 .close
= handle_close
,
677 .closedir
= handle_closedir
,
679 .opendir
= handle_opendir
,
680 .rewinddir
= handle_rewinddir
,
681 .telldir
= handle_telldir
,
682 .readdir_r
= handle_readdir_r
,
683 .seekdir
= handle_seekdir
,
684 .preadv
= handle_preadv
,
685 .pwritev
= handle_pwritev
,
686 .chmod
= handle_chmod
,
687 .mknod
= handle_mknod
,
688 .mkdir
= handle_mkdir
,
689 .fstat
= handle_fstat
,
690 .open2
= handle_open2
,
691 .symlink
= handle_symlink
,
693 .truncate
= handle_truncate
,
694 .rename
= handle_rename
,
695 .chown
= handle_chown
,
696 .utimensat
= handle_utimensat
,
697 .remove
= handle_remove
,
698 .fsync
= handle_fsync
,
699 .statfs
= handle_statfs
,
700 .lgetxattr
= handle_lgetxattr
,
701 .llistxattr
= handle_llistxattr
,
702 .lsetxattr
= handle_lsetxattr
,
703 .lremovexattr
= handle_lremovexattr
,
704 .name_to_path
= handle_name_to_path
,
705 .renameat
= handle_renameat
,
706 .unlinkat
= handle_unlinkat
,