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 <attr/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 #ifdef CONFIG_OPEN_BY_HANDLE
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
);
62 unsigned int handle_bytes
;
64 unsigned char handle
[0];
68 #define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname */
71 #define O_PATH 010000000
74 static inline int name_to_handle(int dirfd
, const char *name
,
75 struct file_handle
*fh
, int *mnt_id
, int flags
)
81 static inline int open_by_handle(int mountfd
, const char *fh
, int flags
)
88 static int handle_update_file_cred(int dirfd
, const char *name
, FsCred
*credp
)
91 fd
= openat(dirfd
, name
, O_NONBLOCK
| O_NOFOLLOW
);;
95 ret
= fchmod(fd
, credp
->fc_mode
& 07777);
99 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
106 static int handle_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
110 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
112 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
116 ret
= fstatat(fd
, "", stbuf
, AT_EMPTY_PATH
);
121 static ssize_t
handle_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
122 char *buf
, size_t bufsz
)
125 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
127 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
131 ret
= readlinkat(fd
, "", buf
, bufsz
);
136 static int handle_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
138 return close(fs
->fd
);
141 static int handle_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
143 return closedir(fs
->dir
);
146 static int handle_open(FsContext
*ctx
, V9fsPath
*fs_path
,
147 int flags
, V9fsFidOpenState
*fs
)
149 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
151 fs
->fd
= open_by_handle(data
->mountfd
, fs_path
->data
, flags
);
155 static int handle_opendir(FsContext
*ctx
,
156 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
159 ret
= handle_open(ctx
, fs_path
, O_DIRECTORY
, fs
);
163 fs
->dir
= fdopendir(ret
);
170 static void handle_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
172 return rewinddir(fs
->dir
);
175 static off_t
handle_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
177 return telldir(fs
->dir
);
180 static int handle_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
181 struct dirent
*entry
,
182 struct dirent
**result
)
184 return readdir_r(fs
->dir
, entry
, result
);
187 static void handle_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
189 return seekdir(fs
->dir
, off
);
192 static ssize_t
handle_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
193 const struct iovec
*iov
,
194 int iovcnt
, off_t offset
)
197 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
199 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
203 return readv(fs
->fd
, iov
, iovcnt
);
208 static ssize_t
handle_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
209 const struct iovec
*iov
,
210 int iovcnt
, off_t offset
)
214 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
216 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
220 ret
= writev(fs
->fd
, iov
, iovcnt
);
223 #ifdef CONFIG_SYNC_FILE_RANGE
224 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
226 * Initiate a writeback. This is not a data integrity sync.
227 * We want to ensure that we don't leave dirty pages in the cache
228 * after write when writeout=immediate is sepcified.
230 sync_file_range(fs
->fd
, offset
, ret
,
231 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
237 static int handle_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
240 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
242 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
246 ret
= fchmod(fd
, credp
->fc_mode
);
251 static int handle_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
252 const char *name
, FsCred
*credp
)
255 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
257 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
261 ret
= mknodat(dirfd
, name
, credp
->fc_mode
, credp
->fc_rdev
);
263 ret
= handle_update_file_cred(dirfd
, name
, credp
);
269 static int handle_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
270 const char *name
, FsCred
*credp
)
273 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
275 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
279 ret
= mkdirat(dirfd
, name
, credp
->fc_mode
);
281 ret
= handle_update_file_cred(dirfd
, name
, credp
);
287 static int handle_fstat(FsContext
*fs_ctx
, V9fsFidOpenState
*fs
,
290 return fstat(fs
->fd
, stbuf
);
293 static int handle_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
294 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
298 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
300 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
304 fd
= openat(dirfd
, name
, flags
| O_NOFOLLOW
, credp
->fc_mode
);
306 ret
= handle_update_file_cred(dirfd
, name
, credp
);
319 static int handle_symlink(FsContext
*fs_ctx
, const char *oldpath
,
320 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
323 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
325 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
329 ret
= symlinkat(oldpath
, dirfd
, name
);
331 fd
= openat(dirfd
, name
, O_PATH
| O_NOFOLLOW
);
336 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
344 static int handle_link(FsContext
*ctx
, V9fsPath
*oldpath
,
345 V9fsPath
*dirpath
, const char *name
)
347 int oldfd
, newdirfd
, ret
;
348 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
350 oldfd
= open_by_handle(data
->mountfd
, oldpath
->data
, O_PATH
);
354 newdirfd
= open_by_handle(data
->mountfd
, dirpath
->data
, O_PATH
);
359 ret
= linkat(oldfd
, "", newdirfd
, name
, AT_EMPTY_PATH
);
365 static int handle_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
368 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
370 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
| O_WRONLY
);
374 ret
= ftruncate(fd
, size
);
379 static int handle_rename(FsContext
*ctx
, const char *oldpath
,
386 static int handle_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
389 struct handle_data
*data
= (struct handle_data
*)fs_ctx
->private;
391 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_PATH
);
395 ret
= fchownat(fd
, "", credp
->fc_uid
, credp
->fc_gid
, AT_EMPTY_PATH
);
400 static int handle_utimensat(FsContext
*ctx
, V9fsPath
*fs_path
,
401 const struct timespec
*buf
)
404 #ifdef CONFIG_UTIMENSAT
406 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
408 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
412 ret
= futimens(fd
, buf
);
421 static int handle_remove(FsContext
*ctx
, const char *path
)
427 static int handle_fsync(FsContext
*ctx
, V9fsFidOpenState
*fs
, int datasync
)
430 return qemu_fdatasync(fs
->fd
);
432 return fsync(fs
->fd
);
436 static int handle_statfs(FsContext
*ctx
, V9fsPath
*fs_path
,
437 struct statfs
*stbuf
)
440 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
442 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
446 ret
= fstatfs(fd
, stbuf
);
451 static ssize_t
handle_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
452 const char *name
, void *value
, size_t size
)
455 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
457 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
461 ret
= fgetxattr(fd
, name
, value
, size
);
466 static ssize_t
handle_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
467 void *value
, size_t size
)
470 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
472 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
476 ret
= flistxattr(fd
, value
, size
);
481 static int handle_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
482 void *value
, size_t size
, int flags
)
485 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
487 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
491 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
496 static int handle_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
500 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
502 fd
= open_by_handle(data
->mountfd
, fs_path
->data
, O_NONBLOCK
);
506 ret
= fremovexattr(fd
, name
);
511 static int handle_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
512 const char *name
, V9fsPath
*target
)
514 char buffer
[PATH_MAX
];
515 struct file_handle
*fh
;
516 int dirfd
, ret
, mnt_id
;
517 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
519 /* "." and ".." are not allowed */
520 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
526 dirfd
= open_by_handle(data
->mountfd
, dir_path
->data
, O_PATH
);
528 /* relative to export root */
529 dirfd
= open(rpath(ctx
, ".", buffer
), O_DIRECTORY
);
534 fh
= g_malloc(sizeof(struct file_handle
) + data
->handle_bytes
);
535 fh
->handle_bytes
= data
->handle_bytes
;
536 /* add a "./" at the begining of the path */
537 snprintf(buffer
, PATH_MAX
, "./%s", name
);
538 /* flag = 0 imply don't follow symlink */
539 ret
= name_to_handle(dirfd
, buffer
, fh
, &mnt_id
, 0);
541 target
->data
= (char *)fh
;
542 target
->size
= sizeof(struct file_handle
) + data
->handle_bytes
;
550 static int handle_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
551 const char *old_name
, V9fsPath
*newdir
,
552 const char *new_name
)
554 int olddirfd
, newdirfd
, ret
;
555 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
557 olddirfd
= open_by_handle(data
->mountfd
, olddir
->data
, O_PATH
);
561 newdirfd
= open_by_handle(data
->mountfd
, newdir
->data
, O_PATH
);
566 ret
= renameat(olddirfd
, old_name
, newdirfd
, new_name
);
572 static int handle_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
573 const char *name
, int flags
)
576 struct handle_data
*data
= (struct handle_data
*)ctx
->private;
578 dirfd
= open_by_handle(data
->mountfd
, dir
->data
, O_PATH
);
583 ret
= unlinkat(dirfd
, name
, flags
);
588 static int handle_ioc_getversion(FsContext
*ctx
, V9fsPath
*path
,
589 mode_t st_mode
, uint64_t *st_gen
)
592 V9fsFidOpenState fid_open
;
595 * Do not try to open special files like device nodes, fifos etc
596 * We can get fd for regular files and directories only
598 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
);
610 static int handle_init(FsContext
*ctx
)
614 struct file_handle fh
;
615 struct handle_data
*data
= g_malloc(sizeof(struct handle_data
));
617 data
->mountfd
= open(ctx
->fs_root
, O_DIRECTORY
);
618 if (data
->mountfd
< 0) {
622 ret
= statfs(ctx
->fs_root
, &stbuf
);
624 switch (stbuf
.f_type
) {
625 case EXT2_SUPER_MAGIC
:
626 case BTRFS_SUPER_MAGIC
:
627 case REISERFS_SUPER_MAGIC
:
628 case XFS_SUPER_MAGIC
:
629 ctx
->exops
.get_st_gen
= handle_ioc_getversion
;
633 memset(&fh
, 0, sizeof(struct file_handle
));
634 ret
= name_to_handle(data
->mountfd
, ".", &fh
, &mnt_id
, 0);
635 if (ret
&& errno
== EOVERFLOW
) {
636 data
->handle_bytes
= fh
.handle_bytes
;
641 /* we got 0 byte handle ? */
643 close(data
->mountfd
);
650 FileOperations handle_ops
= {
652 .lstat
= handle_lstat
,
653 .readlink
= handle_readlink
,
654 .close
= handle_close
,
655 .closedir
= handle_closedir
,
657 .opendir
= handle_opendir
,
658 .rewinddir
= handle_rewinddir
,
659 .telldir
= handle_telldir
,
660 .readdir_r
= handle_readdir_r
,
661 .seekdir
= handle_seekdir
,
662 .preadv
= handle_preadv
,
663 .pwritev
= handle_pwritev
,
664 .chmod
= handle_chmod
,
665 .mknod
= handle_mknod
,
666 .mkdir
= handle_mkdir
,
667 .fstat
= handle_fstat
,
668 .open2
= handle_open2
,
669 .symlink
= handle_symlink
,
671 .truncate
= handle_truncate
,
672 .rename
= handle_rename
,
673 .chown
= handle_chown
,
674 .utimensat
= handle_utimensat
,
675 .remove
= handle_remove
,
676 .fsync
= handle_fsync
,
677 .statfs
= handle_statfs
,
678 .lgetxattr
= handle_lgetxattr
,
679 .llistxattr
= handle_llistxattr
,
680 .lsetxattr
= handle_lsetxattr
,
681 .lremovexattr
= handle_lremovexattr
,
682 .name_to_path
= handle_name_to_path
,
683 .renameat
= handle_renameat
,
684 .unlinkat
= handle_unlinkat
,