4 * Copyright IBM, Corp. 2011
7 * M. Mohan Kumar <mohan@in.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.
12 #include "qemu/osdep.h"
13 #include <sys/socket.h>
16 #include "qemu/cutils.h"
17 #include "qemu/error-report.h"
18 #include "fsdev/qemu-fsdev.h"
21 typedef struct V9fsProxy
{
24 struct iovec in_iovec
;
25 struct iovec out_iovec
;
29 * Return received file descriptor on success in *status.
30 * errno is also returned on *status (which will be < 0)
31 * return < 0 on transport error.
33 static int v9fs_receivefd(int sockfd
, int *status
)
39 union MsgControl msg_control
;
42 iov
.iov_len
= sizeof(data
);
44 memset(&msg
, 0, sizeof(msg
));
47 msg
.msg_control
= &msg_control
;
48 msg
.msg_controllen
= sizeof(msg_control
);
51 retval
= recvmsg(sockfd
, &msg
, 0);
52 } while (retval
< 0 && errno
== EINTR
);
57 * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
58 * request doesn't need ancillary data (fd) or an error occurred,
59 * data is set to negative errno value.
61 if (data
!= V9FS_FD_VALID
) {
66 * File descriptor (fd) is sent in the ancillary data. Check if we
67 * indeed received it. One of the reasons to fail to receive it is if
68 * we exceeded the maximum number of file descriptors!
70 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
71 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
72 cmsg
->cmsg_level
!= SOL_SOCKET
||
73 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
76 fd
= *((int *)CMSG_DATA(cmsg
));
80 *status
= -ENFILE
; /* Ancillary data sent but not received */
84 static ssize_t
socket_read(int sockfd
, void *buff
, size_t size
)
86 ssize_t retval
, total
= 0;
89 retval
= read(sockfd
, buff
, size
);
106 /* Converts proxy_statfs to VFS statfs structure */
107 static void prstatfs_to_statfs(struct statfs
*stfs
, ProxyStatFS
*prstfs
)
109 memset(stfs
, 0, sizeof(*stfs
));
110 stfs
->f_type
= prstfs
->f_type
;
111 stfs
->f_bsize
= prstfs
->f_bsize
;
112 stfs
->f_blocks
= prstfs
->f_blocks
;
113 stfs
->f_bfree
= prstfs
->f_bfree
;
114 stfs
->f_bavail
= prstfs
->f_bavail
;
115 stfs
->f_files
= prstfs
->f_files
;
116 stfs
->f_ffree
= prstfs
->f_ffree
;
117 stfs
->f_fsid
.__val
[0] = prstfs
->f_fsid
[0] & 0xFFFFFFFFU
;
118 stfs
->f_fsid
.__val
[1] = prstfs
->f_fsid
[1] >> 32 & 0xFFFFFFFFU
;
119 stfs
->f_namelen
= prstfs
->f_namelen
;
120 stfs
->f_frsize
= prstfs
->f_frsize
;
123 /* Converts proxy_stat structure to VFS stat structure */
124 static void prstat_to_stat(struct stat
*stbuf
, ProxyStat
*prstat
)
126 memset(stbuf
, 0, sizeof(*stbuf
));
127 stbuf
->st_dev
= prstat
->st_dev
;
128 stbuf
->st_ino
= prstat
->st_ino
;
129 stbuf
->st_nlink
= prstat
->st_nlink
;
130 stbuf
->st_mode
= prstat
->st_mode
;
131 stbuf
->st_uid
= prstat
->st_uid
;
132 stbuf
->st_gid
= prstat
->st_gid
;
133 stbuf
->st_rdev
= prstat
->st_rdev
;
134 stbuf
->st_size
= prstat
->st_size
;
135 stbuf
->st_blksize
= prstat
->st_blksize
;
136 stbuf
->st_blocks
= prstat
->st_blocks
;
137 stbuf
->st_atim
.tv_sec
= prstat
->st_atim_sec
;
138 stbuf
->st_atim
.tv_nsec
= prstat
->st_atim_nsec
;
139 stbuf
->st_mtime
= prstat
->st_mtim_sec
;
140 stbuf
->st_mtim
.tv_nsec
= prstat
->st_mtim_nsec
;
141 stbuf
->st_ctime
= prstat
->st_ctim_sec
;
142 stbuf
->st_ctim
.tv_nsec
= prstat
->st_ctim_nsec
;
146 * Response contains two parts
148 * header.type == T_ERROR, data -> -errno
149 * header.type == T_SUCCESS, data -> response
150 * size of errno/response is given by header.size
151 * returns < 0, on transport error. response is
152 * valid only if status >= 0.
154 static int v9fs_receive_response(V9fsProxy
*proxy
, int type
,
155 int *status
, void *response
)
159 struct iovec
*reply
= &proxy
->in_iovec
;
163 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
167 reply
->iov_len
= PROXY_HDR_SZ
;
168 proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
170 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
173 if (header
.size
> PROXY_MAX_IO_SZ
) {
175 while (header
.size
> 0) {
176 count
= MIN(PROXY_MAX_IO_SZ
, header
.size
);
177 count
= socket_read(proxy
->sockfd
, reply
->iov_base
, count
);
181 header
.size
-= count
;
187 retval
= socket_read(proxy
->sockfd
,
188 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
192 reply
->iov_len
+= header
.size
;
193 /* there was an error during processing request */
194 if (header
.type
== T_ERROR
) {
196 ret
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
206 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
207 "qqqdddqqqqqqqqqq", &prstat
.st_dev
,
208 &prstat
.st_ino
, &prstat
.st_nlink
,
209 &prstat
.st_mode
, &prstat
.st_uid
,
210 &prstat
.st_gid
, &prstat
.st_rdev
,
211 &prstat
.st_size
, &prstat
.st_blksize
,
213 &prstat
.st_atim_sec
, &prstat
.st_atim_nsec
,
214 &prstat
.st_mtim_sec
, &prstat
.st_mtim_nsec
,
215 &prstat
.st_ctim_sec
, &prstat
.st_ctim_nsec
);
216 prstat_to_stat(response
, &prstat
);
221 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
222 "qqqqqqqqqqq", &prstfs
.f_type
,
223 &prstfs
.f_bsize
, &prstfs
.f_blocks
,
224 &prstfs
.f_bfree
, &prstfs
.f_bavail
,
225 &prstfs
.f_files
, &prstfs
.f_ffree
,
226 &prstfs
.f_fsid
[0], &prstfs
.f_fsid
[1],
227 &prstfs
.f_namelen
, &prstfs
.f_frsize
);
228 prstatfs_to_statfs(response
, &prstfs
);
233 v9fs_string_init(&target
);
234 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &target
);
235 strcpy(response
, target
.data
);
236 v9fs_string_free(&target
);
242 v9fs_string_init(&xattr
);
243 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &xattr
);
244 memcpy(response
, xattr
.data
, xattr
.size
);
245 v9fs_string_free(&xattr
);
249 proxy_unmarshal(reply
, PROXY_HDR_SZ
, "q", response
);
261 * return < 0 on transport error.
262 * *status is valid only if return >= 0
264 static int v9fs_receive_status(V9fsProxy
*proxy
,
265 struct iovec
*reply
, int *status
)
272 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
276 reply
->iov_len
= PROXY_HDR_SZ
;
277 proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
278 if (header
.size
!= sizeof(int)) {
282 retval
= socket_read(proxy
->sockfd
,
283 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
287 reply
->iov_len
+= header
.size
;
288 proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
293 * Proxy->header and proxy->request written to socket by QEMU process.
294 * This request read by proxy helper process
295 * returns 0 on success and -errno on error
297 static int v9fs_request(V9fsProxy
*proxy
, int type
, void *response
, ...)
304 ProxyHeader header
= { 0, 0};
305 struct timespec spec
[2];
306 int flags
, mode
, uid
, gid
;
307 V9fsString
*name
, *value
;
308 V9fsString
*path
, *oldpath
;
309 struct iovec
*iovec
= NULL
, *reply
= NULL
;
311 qemu_mutex_lock(&proxy
->mutex
);
313 if (proxy
->sockfd
== -1) {
317 iovec
= &proxy
->out_iovec
;
318 reply
= &proxy
->in_iovec
;
319 va_start(ap
, response
);
322 path
= va_arg(ap
, V9fsString
*);
323 flags
= va_arg(ap
, int);
324 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, flags
);
326 header
.size
= retval
;
327 header
.type
= T_OPEN
;
331 path
= va_arg(ap
, V9fsString
*);
332 flags
= va_arg(ap
, int);
333 mode
= va_arg(ap
, int);
334 uid
= va_arg(ap
, int);
335 gid
= va_arg(ap
, int);
336 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdddd", path
,
337 flags
, mode
, uid
, gid
);
339 header
.size
= retval
;
340 header
.type
= T_CREATE
;
344 path
= va_arg(ap
, V9fsString
*);
345 mode
= va_arg(ap
, int);
346 rdev
= va_arg(ap
, long int);
347 uid
= va_arg(ap
, int);
348 gid
= va_arg(ap
, int);
349 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsdq",
350 uid
, gid
, path
, mode
, rdev
);
352 header
.size
= retval
;
353 header
.type
= T_MKNOD
;
357 path
= va_arg(ap
, V9fsString
*);
358 mode
= va_arg(ap
, int);
359 uid
= va_arg(ap
, int);
360 gid
= va_arg(ap
, int);
361 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsd",
362 uid
, gid
, path
, mode
);
364 header
.size
= retval
;
365 header
.type
= T_MKDIR
;
369 oldpath
= va_arg(ap
, V9fsString
*);
370 path
= va_arg(ap
, V9fsString
*);
371 uid
= va_arg(ap
, int);
372 gid
= va_arg(ap
, int);
373 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddss",
374 uid
, gid
, oldpath
, path
);
376 header
.size
= retval
;
377 header
.type
= T_SYMLINK
;
381 oldpath
= va_arg(ap
, V9fsString
*);
382 path
= va_arg(ap
, V9fsString
*);
383 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss",
386 header
.size
= retval
;
387 header
.type
= T_LINK
;
391 path
= va_arg(ap
, V9fsString
*);
392 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
394 header
.size
= retval
;
395 header
.type
= T_LSTAT
;
399 path
= va_arg(ap
, V9fsString
*);
400 size
= va_arg(ap
, int);
401 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, size
);
403 header
.size
= retval
;
404 header
.type
= T_READLINK
;
408 path
= va_arg(ap
, V9fsString
*);
409 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
411 header
.size
= retval
;
412 header
.type
= T_STATFS
;
416 path
= va_arg(ap
, V9fsString
*);
417 mode
= va_arg(ap
, int);
418 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, mode
);
420 header
.size
= retval
;
421 header
.type
= T_CHMOD
;
425 path
= va_arg(ap
, V9fsString
*);
426 uid
= va_arg(ap
, int);
427 gid
= va_arg(ap
, int);
428 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdd", path
, uid
, gid
);
430 header
.size
= retval
;
431 header
.type
= T_CHOWN
;
435 path
= va_arg(ap
, V9fsString
*);
436 offset
= va_arg(ap
, uint64_t);
437 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sq", path
, offset
);
439 header
.size
= retval
;
440 header
.type
= T_TRUNCATE
;
444 path
= va_arg(ap
, V9fsString
*);
445 spec
[0].tv_sec
= va_arg(ap
, long);
446 spec
[0].tv_nsec
= va_arg(ap
, long);
447 spec
[1].tv_sec
= va_arg(ap
, long);
448 spec
[1].tv_nsec
= va_arg(ap
, long);
449 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sqqqq", path
,
450 spec
[0].tv_sec
, spec
[1].tv_nsec
,
451 spec
[1].tv_sec
, spec
[1].tv_nsec
);
453 header
.size
= retval
;
454 header
.type
= T_UTIME
;
458 oldpath
= va_arg(ap
, V9fsString
*);
459 path
= va_arg(ap
, V9fsString
*);
460 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", oldpath
, path
);
462 header
.size
= retval
;
463 header
.type
= T_RENAME
;
467 path
= va_arg(ap
, V9fsString
*);
468 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
470 header
.size
= retval
;
471 header
.type
= T_REMOVE
;
475 size
= va_arg(ap
, int);
476 path
= va_arg(ap
, V9fsString
*);
477 name
= va_arg(ap
, V9fsString
*);
478 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
,
479 "dss", size
, path
, name
);
481 header
.size
= retval
;
482 header
.type
= T_LGETXATTR
;
486 size
= va_arg(ap
, int);
487 path
= va_arg(ap
, V9fsString
*);
488 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ds", size
, path
);
490 header
.size
= retval
;
491 header
.type
= T_LLISTXATTR
;
495 path
= va_arg(ap
, V9fsString
*);
496 name
= va_arg(ap
, V9fsString
*);
497 value
= va_arg(ap
, V9fsString
*);
498 size
= va_arg(ap
, int);
499 flags
= va_arg(ap
, int);
500 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sssdd",
501 path
, name
, value
, size
, flags
);
503 header
.size
= retval
;
504 header
.type
= T_LSETXATTR
;
508 path
= va_arg(ap
, V9fsString
*);
509 name
= va_arg(ap
, V9fsString
*);
510 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", path
, name
);
512 header
.size
= retval
;
513 header
.type
= T_LREMOVEXATTR
;
517 path
= va_arg(ap
, V9fsString
*);
518 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
520 header
.size
= retval
;
521 header
.type
= T_GETVERSION
;
525 error_report("Invalid type %d", type
);
535 /* marshal the header details */
536 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
537 header
.size
+= PROXY_HDR_SZ
;
539 retval
= qemu_write_full(proxy
->sockfd
, iovec
->iov_base
, header
.size
);
540 if (retval
!= header
.size
) {
548 * A file descriptor is returned as response for
549 * T_OPEN,T_CREATE on success
551 if (v9fs_receivefd(proxy
->sockfd
, &retval
) < 0) {
567 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
575 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
582 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
586 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
594 qemu_mutex_unlock(&proxy
->mutex
);
598 close(proxy
->sockfd
);
600 qemu_mutex_unlock(&proxy
->mutex
);
604 static int proxy_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
607 retval
= v9fs_request(fs_ctx
->private, T_LSTAT
, stbuf
, fs_path
);
615 static ssize_t
proxy_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
616 char *buf
, size_t bufsz
)
619 retval
= v9fs_request(fs_ctx
->private, T_READLINK
, buf
, fs_path
, bufsz
);
627 static int proxy_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
629 return close(fs
->fd
);
632 static int proxy_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
634 return closedir(fs
->dir
.stream
);
637 static int proxy_open(FsContext
*ctx
, V9fsPath
*fs_path
,
638 int flags
, V9fsFidOpenState
*fs
)
640 fs
->fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, flags
);
648 static int proxy_opendir(FsContext
*ctx
,
649 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
653 fs
->dir
.stream
= NULL
;
654 fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, O_DIRECTORY
);
659 fs
->dir
.stream
= fdopendir(fd
);
660 if (!fs
->dir
.stream
) {
669 static void proxy_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
671 rewinddir(fs
->dir
.stream
);
674 static off_t
proxy_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
676 return telldir(fs
->dir
.stream
);
679 static struct dirent
*proxy_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
681 return readdir(fs
->dir
.stream
);
684 static void proxy_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
686 seekdir(fs
->dir
.stream
, off
);
689 static ssize_t
proxy_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
690 const struct iovec
*iov
,
691 int iovcnt
, off_t offset
)
695 ret
= preadv(fs
->fd
, iov
, iovcnt
, offset
);
697 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
699 ret
= readv(fs
->fd
, iov
, iovcnt
);
705 static ssize_t
proxy_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
706 const struct iovec
*iov
,
707 int iovcnt
, off_t offset
)
712 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
714 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
716 ret
= writev(fs
->fd
, iov
, iovcnt
);
719 #ifdef CONFIG_SYNC_FILE_RANGE
720 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
722 * Initiate a writeback. This is not a data integrity sync.
723 * We want to ensure that we don't leave dirty pages in the cache
724 * after write when writeout=immediate is sepcified.
726 sync_file_range(fs
->fd
, offset
, ret
,
727 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
733 static int proxy_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
736 retval
= v9fs_request(fs_ctx
->private, T_CHMOD
, NULL
, fs_path
,
744 static int proxy_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
745 const char *name
, FsCred
*credp
)
750 v9fs_string_init(&fullname
);
751 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
753 retval
= v9fs_request(fs_ctx
->private, T_MKNOD
, NULL
, &fullname
,
754 credp
->fc_mode
, credp
->fc_rdev
,
755 credp
->fc_uid
, credp
->fc_gid
);
756 v9fs_string_free(&fullname
);
764 static int proxy_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
765 const char *name
, FsCred
*credp
)
770 v9fs_string_init(&fullname
);
771 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
773 retval
= v9fs_request(fs_ctx
->private, T_MKDIR
, NULL
, &fullname
,
774 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
775 v9fs_string_free(&fullname
);
783 static int proxy_fstat(FsContext
*fs_ctx
, int fid_type
,
784 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
788 if (fid_type
== P9_FID_DIR
) {
789 fd
= dirfd(fs
->dir
.stream
);
793 return fstat(fd
, stbuf
);
796 static int proxy_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
797 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
801 v9fs_string_init(&fullname
);
802 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
804 fs
->fd
= v9fs_request(fs_ctx
->private, T_CREATE
, NULL
, &fullname
, flags
,
805 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
806 v9fs_string_free(&fullname
);
814 static int proxy_symlink(FsContext
*fs_ctx
, const char *oldpath
,
815 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
818 V9fsString fullname
, target
;
820 v9fs_string_init(&fullname
);
821 v9fs_string_init(&target
);
823 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
824 v9fs_string_sprintf(&target
, "%s", oldpath
);
826 retval
= v9fs_request(fs_ctx
->private, T_SYMLINK
, NULL
, &target
, &fullname
,
827 credp
->fc_uid
, credp
->fc_gid
);
828 v9fs_string_free(&fullname
);
829 v9fs_string_free(&target
);
837 static int proxy_link(FsContext
*ctx
, V9fsPath
*oldpath
,
838 V9fsPath
*dirpath
, const char *name
)
843 v9fs_string_init(&newpath
);
844 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
846 retval
= v9fs_request(ctx
->private, T_LINK
, NULL
, oldpath
, &newpath
);
847 v9fs_string_free(&newpath
);
855 static int proxy_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
859 retval
= v9fs_request(ctx
->private, T_TRUNCATE
, NULL
, fs_path
, size
);
867 static int proxy_rename(FsContext
*ctx
, const char *oldpath
,
871 V9fsString oldname
, newname
;
873 v9fs_string_init(&oldname
);
874 v9fs_string_init(&newname
);
876 v9fs_string_sprintf(&oldname
, "%s", oldpath
);
877 v9fs_string_sprintf(&newname
, "%s", newpath
);
878 retval
= v9fs_request(ctx
->private, T_RENAME
, NULL
, &oldname
, &newname
);
879 v9fs_string_free(&oldname
);
880 v9fs_string_free(&newname
);
887 static int proxy_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
890 retval
= v9fs_request(fs_ctx
->private, T_CHOWN
, NULL
, fs_path
,
891 credp
->fc_uid
, credp
->fc_gid
);
898 static int proxy_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
899 const struct timespec
*buf
)
902 retval
= v9fs_request(s
->private, T_UTIME
, NULL
, fs_path
,
903 buf
[0].tv_sec
, buf
[0].tv_nsec
,
904 buf
[1].tv_sec
, buf
[1].tv_nsec
);
911 static int proxy_remove(FsContext
*ctx
, const char *path
)
915 v9fs_string_init(&name
);
916 v9fs_string_sprintf(&name
, "%s", path
);
917 retval
= v9fs_request(ctx
->private, T_REMOVE
, NULL
, &name
);
918 v9fs_string_free(&name
);
925 static int proxy_fsync(FsContext
*ctx
, int fid_type
,
926 V9fsFidOpenState
*fs
, int datasync
)
930 if (fid_type
== P9_FID_DIR
) {
931 fd
= dirfd(fs
->dir
.stream
);
937 return qemu_fdatasync(fd
);
943 static int proxy_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
946 retval
= v9fs_request(s
->private, T_STATFS
, stbuf
, fs_path
);
954 static ssize_t
proxy_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
955 const char *name
, void *value
, size_t size
)
960 v9fs_string_init(&xname
);
961 v9fs_string_sprintf(&xname
, "%s", name
);
962 retval
= v9fs_request(ctx
->private, T_LGETXATTR
, value
, size
, fs_path
,
964 v9fs_string_free(&xname
);
971 static ssize_t
proxy_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
972 void *value
, size_t size
)
975 retval
= v9fs_request(ctx
->private, T_LLISTXATTR
, value
, size
, fs_path
);
982 static int proxy_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
983 void *value
, size_t size
, int flags
)
986 V9fsString xname
, xvalue
;
988 v9fs_string_init(&xname
);
989 v9fs_string_sprintf(&xname
, "%s", name
);
991 v9fs_string_init(&xvalue
);
993 xvalue
.data
= g_malloc(size
);
994 memcpy(xvalue
.data
, value
, size
);
996 retval
= v9fs_request(ctx
->private, T_LSETXATTR
, value
, fs_path
, &xname
,
997 &xvalue
, size
, flags
);
998 v9fs_string_free(&xname
);
999 v9fs_string_free(&xvalue
);
1006 static int proxy_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1012 v9fs_string_init(&xname
);
1013 v9fs_string_sprintf(&xname
, "%s", name
);
1014 retval
= v9fs_request(ctx
->private, T_LREMOVEXATTR
, NULL
, fs_path
, &xname
);
1015 v9fs_string_free(&xname
);
1022 static int proxy_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1023 const char *name
, V9fsPath
*target
)
1026 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1028 v9fs_path_sprintf(target
, "%s", name
);
1033 static int proxy_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1034 const char *old_name
, V9fsPath
*newdir
,
1035 const char *new_name
)
1038 V9fsString old_full_name
, new_full_name
;
1040 v9fs_string_init(&old_full_name
);
1041 v9fs_string_init(&new_full_name
);
1043 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1044 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1046 ret
= proxy_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1047 v9fs_string_free(&old_full_name
);
1048 v9fs_string_free(&new_full_name
);
1052 static int proxy_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1053 const char *name
, int flags
)
1056 V9fsString fullname
;
1057 v9fs_string_init(&fullname
);
1059 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1060 ret
= proxy_remove(ctx
, fullname
.data
);
1061 v9fs_string_free(&fullname
);
1066 static int proxy_ioc_getversion(FsContext
*fs_ctx
, V9fsPath
*path
,
1067 mode_t st_mode
, uint64_t *st_gen
)
1071 /* Do not try to open special files like device nodes, fifos etc
1072 * we can get fd for regular files and directories only
1074 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1078 err
= v9fs_request(fs_ctx
->private, T_GETVERSION
, st_gen
, path
);
1086 static int connect_namedsocket(const char *path
)
1089 struct sockaddr_un helper
;
1091 if (strlen(path
) >= sizeof(helper
.sun_path
)) {
1092 error_report("Socket name too long");
1095 sockfd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1097 error_report("Failed to create socket: %s", strerror(errno
));
1100 strcpy(helper
.sun_path
, path
);
1101 helper
.sun_family
= AF_UNIX
;
1102 size
= strlen(helper
.sun_path
) + sizeof(helper
.sun_family
);
1103 if (connect(sockfd
, (struct sockaddr
*)&helper
, size
) < 0) {
1104 error_report("Failed to connect to %s: %s", path
, strerror(errno
));
1109 /* remove the socket for security reasons */
1114 static int proxy_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fs
)
1116 const char *socket
= qemu_opt_get(opts
, "socket");
1117 const char *sock_fd
= qemu_opt_get(opts
, "sock_fd");
1119 if (!socket
&& !sock_fd
) {
1120 error_report("Must specify either socket or sock_fd");
1123 if (socket
&& sock_fd
) {
1124 error_report("Both socket and sock_fd options specified");
1128 fs
->path
= g_strdup(socket
);
1129 fs
->export_flags
= V9FS_PROXY_SOCK_NAME
;
1131 fs
->path
= g_strdup(sock_fd
);
1132 fs
->export_flags
= V9FS_PROXY_SOCK_FD
;
1137 static int proxy_init(FsContext
*ctx
)
1139 V9fsProxy
*proxy
= g_malloc(sizeof(V9fsProxy
));
1142 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1143 sock_id
= connect_namedsocket(ctx
->fs_root
);
1145 sock_id
= atoi(ctx
->fs_root
);
1147 error_report("Socket descriptor not initialized");
1154 g_free(ctx
->fs_root
);
1155 ctx
->fs_root
= NULL
;
1157 proxy
->in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1158 proxy
->in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1159 proxy
->out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1160 proxy
->out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1162 ctx
->private = proxy
;
1163 proxy
->sockfd
= sock_id
;
1164 qemu_mutex_init(&proxy
->mutex
);
1166 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1167 ctx
->exops
.get_st_gen
= proxy_ioc_getversion
;
1171 static void proxy_cleanup(FsContext
*ctx
)
1173 V9fsProxy
*proxy
= ctx
->private;
1175 g_free(proxy
->out_iovec
.iov_base
);
1176 g_free(proxy
->in_iovec
.iov_base
);
1177 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1178 close(proxy
->sockfd
);
1183 FileOperations proxy_ops
= {
1184 .parse_opts
= proxy_parse_opts
,
1186 .cleanup
= proxy_cleanup
,
1187 .lstat
= proxy_lstat
,
1188 .readlink
= proxy_readlink
,
1189 .close
= proxy_close
,
1190 .closedir
= proxy_closedir
,
1192 .opendir
= proxy_opendir
,
1193 .rewinddir
= proxy_rewinddir
,
1194 .telldir
= proxy_telldir
,
1195 .readdir
= proxy_readdir
,
1196 .seekdir
= proxy_seekdir
,
1197 .preadv
= proxy_preadv
,
1198 .pwritev
= proxy_pwritev
,
1199 .chmod
= proxy_chmod
,
1200 .mknod
= proxy_mknod
,
1201 .mkdir
= proxy_mkdir
,
1202 .fstat
= proxy_fstat
,
1203 .open2
= proxy_open2
,
1204 .symlink
= proxy_symlink
,
1206 .truncate
= proxy_truncate
,
1207 .rename
= proxy_rename
,
1208 .chown
= proxy_chown
,
1209 .utimensat
= proxy_utimensat
,
1210 .remove
= proxy_remove
,
1211 .fsync
= proxy_fsync
,
1212 .statfs
= proxy_statfs
,
1213 .lgetxattr
= proxy_lgetxattr
,
1214 .llistxattr
= proxy_llistxattr
,
1215 .lsetxattr
= proxy_lsetxattr
,
1216 .lremovexattr
= proxy_lremovexattr
,
1217 .name_to_path
= proxy_name_to_path
,
1218 .renameat
= proxy_renameat
,
1219 .unlinkat
= proxy_unlinkat
,