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.
13 #include "qemu/osdep.h"
14 #include <sys/socket.h>
17 #include "qapi/error.h"
18 #include "qemu/cutils.h"
19 #include "qemu/error-report.h"
20 #include "qemu/option.h"
21 #include "fsdev/qemu-fsdev.h"
24 typedef struct V9fsProxy
{
27 struct iovec in_iovec
;
28 struct iovec out_iovec
;
32 * Return received file descriptor on success in *status.
33 * errno is also returned on *status (which will be < 0)
34 * return < 0 on transport error.
36 static int v9fs_receivefd(int sockfd
, int *status
)
42 union MsgControl msg_control
;
45 iov
.iov_len
= sizeof(data
);
47 memset(&msg
, 0, sizeof(msg
));
50 msg
.msg_control
= &msg_control
;
51 msg
.msg_controllen
= sizeof(msg_control
);
54 retval
= recvmsg(sockfd
, &msg
, 0);
55 } while (retval
< 0 && errno
== EINTR
);
60 * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
61 * request doesn't need ancillary data (fd) or an error occurred,
62 * data is set to negative errno value.
64 if (data
!= V9FS_FD_VALID
) {
69 * File descriptor (fd) is sent in the ancillary data. Check if we
70 * indeed received it. One of the reasons to fail to receive it is if
71 * we exceeded the maximum number of file descriptors!
73 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
74 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
75 cmsg
->cmsg_level
!= SOL_SOCKET
||
76 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
79 fd
= *((int *)CMSG_DATA(cmsg
));
83 *status
= -ENFILE
; /* Ancillary data sent but not received */
87 static ssize_t
socket_read(int sockfd
, void *buff
, size_t size
)
89 ssize_t retval
, total
= 0;
92 retval
= read(sockfd
, buff
, size
);
109 /* Converts proxy_statfs to VFS statfs structure */
110 static void prstatfs_to_statfs(struct statfs
*stfs
, ProxyStatFS
*prstfs
)
112 memset(stfs
, 0, sizeof(*stfs
));
113 stfs
->f_type
= prstfs
->f_type
;
114 stfs
->f_bsize
= prstfs
->f_bsize
;
115 stfs
->f_blocks
= prstfs
->f_blocks
;
116 stfs
->f_bfree
= prstfs
->f_bfree
;
117 stfs
->f_bavail
= prstfs
->f_bavail
;
118 stfs
->f_files
= prstfs
->f_files
;
119 stfs
->f_ffree
= prstfs
->f_ffree
;
120 stfs
->f_fsid
.__val
[0] = prstfs
->f_fsid
[0] & 0xFFFFFFFFU
;
121 stfs
->f_fsid
.__val
[1] = prstfs
->f_fsid
[1] >> 32 & 0xFFFFFFFFU
;
122 stfs
->f_namelen
= prstfs
->f_namelen
;
123 stfs
->f_frsize
= prstfs
->f_frsize
;
126 /* Converts proxy_stat structure to VFS stat structure */
127 static void prstat_to_stat(struct stat
*stbuf
, ProxyStat
*prstat
)
129 memset(stbuf
, 0, sizeof(*stbuf
));
130 stbuf
->st_dev
= prstat
->st_dev
;
131 stbuf
->st_ino
= prstat
->st_ino
;
132 stbuf
->st_nlink
= prstat
->st_nlink
;
133 stbuf
->st_mode
= prstat
->st_mode
;
134 stbuf
->st_uid
= prstat
->st_uid
;
135 stbuf
->st_gid
= prstat
->st_gid
;
136 stbuf
->st_rdev
= prstat
->st_rdev
;
137 stbuf
->st_size
= prstat
->st_size
;
138 stbuf
->st_blksize
= prstat
->st_blksize
;
139 stbuf
->st_blocks
= prstat
->st_blocks
;
140 stbuf
->st_atim
.tv_sec
= prstat
->st_atim_sec
;
141 stbuf
->st_atim
.tv_nsec
= prstat
->st_atim_nsec
;
142 stbuf
->st_mtime
= prstat
->st_mtim_sec
;
143 stbuf
->st_mtim
.tv_nsec
= prstat
->st_mtim_nsec
;
144 stbuf
->st_ctime
= prstat
->st_ctim_sec
;
145 stbuf
->st_ctim
.tv_nsec
= prstat
->st_ctim_nsec
;
149 * Response contains two parts
151 * header.type == T_ERROR, data -> -errno
152 * header.type == T_SUCCESS, data -> response
153 * size of errno/response is given by header.size
154 * returns < 0, on transport error. response is
155 * valid only if status >= 0.
157 static int v9fs_receive_response(V9fsProxy
*proxy
, int type
,
158 int *status
, void *response
)
162 struct iovec
*reply
= &proxy
->in_iovec
;
166 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
170 reply
->iov_len
= PROXY_HDR_SZ
;
171 retval
= proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
172 assert(retval
== 4 * 2);
174 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
177 if (header
.size
> PROXY_MAX_IO_SZ
) {
179 while (header
.size
> 0) {
180 count
= MIN(PROXY_MAX_IO_SZ
, header
.size
);
181 count
= socket_read(proxy
->sockfd
, reply
->iov_base
, count
);
185 header
.size
-= count
;
191 retval
= socket_read(proxy
->sockfd
,
192 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
196 reply
->iov_len
+= header
.size
;
197 /* there was an error during processing request */
198 if (header
.type
== T_ERROR
) {
200 ret
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
208 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
209 "qqqdddqqqqqqqqqq", &prstat
.st_dev
,
210 &prstat
.st_ino
, &prstat
.st_nlink
,
211 &prstat
.st_mode
, &prstat
.st_uid
,
212 &prstat
.st_gid
, &prstat
.st_rdev
,
213 &prstat
.st_size
, &prstat
.st_blksize
,
215 &prstat
.st_atim_sec
, &prstat
.st_atim_nsec
,
216 &prstat
.st_mtim_sec
, &prstat
.st_mtim_nsec
,
217 &prstat
.st_ctim_sec
, &prstat
.st_ctim_nsec
);
218 assert(retval
== 8 * 3 + 4 * 3 + 8 * 10);
219 prstat_to_stat(response
, &prstat
);
224 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
225 "qqqqqqqqqqq", &prstfs
.f_type
,
226 &prstfs
.f_bsize
, &prstfs
.f_blocks
,
227 &prstfs
.f_bfree
, &prstfs
.f_bavail
,
228 &prstfs
.f_files
, &prstfs
.f_ffree
,
229 &prstfs
.f_fsid
[0], &prstfs
.f_fsid
[1],
230 &prstfs
.f_namelen
, &prstfs
.f_frsize
);
231 assert(retval
== 8 * 11);
232 prstatfs_to_statfs(response
, &prstfs
);
237 v9fs_string_init(&target
);
238 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &target
);
239 strcpy(response
, target
.data
);
240 v9fs_string_free(&target
);
246 v9fs_string_init(&xattr
);
247 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &xattr
);
248 memcpy(response
, xattr
.data
, xattr
.size
);
249 v9fs_string_free(&xattr
);
253 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "q", response
);
266 * return < 0 on transport error.
267 * *status is valid only if return >= 0
269 static int v9fs_receive_status(V9fsProxy
*proxy
,
270 struct iovec
*reply
, int *status
)
277 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
281 reply
->iov_len
= PROXY_HDR_SZ
;
282 retval
= proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
283 assert(retval
== 4 * 2);
284 retval
= socket_read(proxy
->sockfd
,
285 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
289 reply
->iov_len
+= header
.size
;
290 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
296 * Proxy->header and proxy->request written to socket by QEMU process.
297 * This request read by proxy helper process
298 * returns 0 on success and -errno on error
300 static int v9fs_request(V9fsProxy
*proxy
, int type
, void *response
, ...)
307 ProxyHeader header
= { 0, 0};
308 struct timespec spec
[2];
309 int flags
, mode
, uid
, gid
;
310 V9fsString
*name
, *value
;
311 V9fsString
*path
, *oldpath
;
312 struct iovec
*iovec
= NULL
, *reply
= NULL
;
314 qemu_mutex_lock(&proxy
->mutex
);
316 if (proxy
->sockfd
== -1) {
320 iovec
= &proxy
->out_iovec
;
321 reply
= &proxy
->in_iovec
;
322 va_start(ap
, response
);
325 path
= va_arg(ap
, V9fsString
*);
326 flags
= va_arg(ap
, int);
327 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, flags
);
329 header
.size
= retval
;
330 header
.type
= T_OPEN
;
334 path
= va_arg(ap
, V9fsString
*);
335 flags
= va_arg(ap
, int);
336 mode
= va_arg(ap
, int);
337 uid
= va_arg(ap
, int);
338 gid
= va_arg(ap
, int);
339 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdddd", path
,
340 flags
, mode
, uid
, gid
);
342 header
.size
= retval
;
343 header
.type
= T_CREATE
;
347 path
= va_arg(ap
, V9fsString
*);
348 mode
= va_arg(ap
, int);
349 rdev
= va_arg(ap
, long int);
350 uid
= va_arg(ap
, int);
351 gid
= va_arg(ap
, int);
352 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsdq",
353 uid
, gid
, path
, mode
, rdev
);
355 header
.size
= retval
;
356 header
.type
= T_MKNOD
;
360 path
= va_arg(ap
, V9fsString
*);
361 mode
= va_arg(ap
, int);
362 uid
= va_arg(ap
, int);
363 gid
= va_arg(ap
, int);
364 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsd",
365 uid
, gid
, path
, mode
);
367 header
.size
= retval
;
368 header
.type
= T_MKDIR
;
372 oldpath
= va_arg(ap
, V9fsString
*);
373 path
= va_arg(ap
, V9fsString
*);
374 uid
= va_arg(ap
, int);
375 gid
= va_arg(ap
, int);
376 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddss",
377 uid
, gid
, oldpath
, path
);
379 header
.size
= retval
;
380 header
.type
= T_SYMLINK
;
384 oldpath
= va_arg(ap
, V9fsString
*);
385 path
= va_arg(ap
, V9fsString
*);
386 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss",
389 header
.size
= retval
;
390 header
.type
= T_LINK
;
394 path
= va_arg(ap
, V9fsString
*);
395 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
397 header
.size
= retval
;
398 header
.type
= T_LSTAT
;
402 path
= va_arg(ap
, V9fsString
*);
403 size
= va_arg(ap
, int);
404 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, size
);
406 header
.size
= retval
;
407 header
.type
= T_READLINK
;
411 path
= va_arg(ap
, V9fsString
*);
412 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
414 header
.size
= retval
;
415 header
.type
= T_STATFS
;
419 path
= va_arg(ap
, V9fsString
*);
420 mode
= va_arg(ap
, int);
421 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, mode
);
423 header
.size
= retval
;
424 header
.type
= T_CHMOD
;
428 path
= va_arg(ap
, V9fsString
*);
429 uid
= va_arg(ap
, int);
430 gid
= va_arg(ap
, int);
431 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdd", path
, uid
, gid
);
433 header
.size
= retval
;
434 header
.type
= T_CHOWN
;
438 path
= va_arg(ap
, V9fsString
*);
439 offset
= va_arg(ap
, uint64_t);
440 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sq", path
, offset
);
442 header
.size
= retval
;
443 header
.type
= T_TRUNCATE
;
447 path
= va_arg(ap
, V9fsString
*);
448 spec
[0].tv_sec
= va_arg(ap
, long);
449 spec
[0].tv_nsec
= va_arg(ap
, long);
450 spec
[1].tv_sec
= va_arg(ap
, long);
451 spec
[1].tv_nsec
= va_arg(ap
, long);
452 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sqqqq", path
,
453 spec
[0].tv_sec
, spec
[1].tv_nsec
,
454 spec
[1].tv_sec
, spec
[1].tv_nsec
);
456 header
.size
= retval
;
457 header
.type
= T_UTIME
;
461 oldpath
= va_arg(ap
, V9fsString
*);
462 path
= va_arg(ap
, V9fsString
*);
463 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", oldpath
, path
);
465 header
.size
= retval
;
466 header
.type
= T_RENAME
;
470 path
= va_arg(ap
, V9fsString
*);
471 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
473 header
.size
= retval
;
474 header
.type
= T_REMOVE
;
478 size
= va_arg(ap
, int);
479 path
= va_arg(ap
, V9fsString
*);
480 name
= va_arg(ap
, V9fsString
*);
481 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
,
482 "dss", size
, path
, name
);
484 header
.size
= retval
;
485 header
.type
= T_LGETXATTR
;
489 size
= va_arg(ap
, int);
490 path
= va_arg(ap
, V9fsString
*);
491 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ds", size
, path
);
493 header
.size
= retval
;
494 header
.type
= T_LLISTXATTR
;
498 path
= va_arg(ap
, V9fsString
*);
499 name
= va_arg(ap
, V9fsString
*);
500 value
= va_arg(ap
, V9fsString
*);
501 size
= va_arg(ap
, int);
502 flags
= va_arg(ap
, int);
503 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sssdd",
504 path
, name
, value
, size
, flags
);
506 header
.size
= retval
;
507 header
.type
= T_LSETXATTR
;
511 path
= va_arg(ap
, V9fsString
*);
512 name
= va_arg(ap
, V9fsString
*);
513 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", path
, name
);
515 header
.size
= retval
;
516 header
.type
= T_LREMOVEXATTR
;
520 path
= va_arg(ap
, V9fsString
*);
521 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
523 header
.size
= retval
;
524 header
.type
= T_GETVERSION
;
528 error_report("Invalid type %d", type
);
538 /* marshal the header details */
539 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
540 header
.size
+= PROXY_HDR_SZ
;
542 retval
= qemu_write_full(proxy
->sockfd
, iovec
->iov_base
, header
.size
);
543 if (retval
!= header
.size
) {
551 * A file descriptor is returned as response for
552 * T_OPEN,T_CREATE on success
554 if (v9fs_receivefd(proxy
->sockfd
, &retval
) < 0) {
570 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
578 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
585 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
589 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
597 qemu_mutex_unlock(&proxy
->mutex
);
601 close(proxy
->sockfd
);
603 qemu_mutex_unlock(&proxy
->mutex
);
607 static int proxy_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
610 retval
= v9fs_request(fs_ctx
->private, T_LSTAT
, stbuf
, fs_path
);
618 static ssize_t
proxy_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
619 char *buf
, size_t bufsz
)
622 retval
= v9fs_request(fs_ctx
->private, T_READLINK
, buf
, fs_path
, bufsz
);
630 static int proxy_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
632 return close(fs
->fd
);
635 static int proxy_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
637 return closedir(fs
->dir
.stream
);
640 static int proxy_open(FsContext
*ctx
, V9fsPath
*fs_path
,
641 int flags
, V9fsFidOpenState
*fs
)
643 fs
->fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, flags
);
651 static int proxy_opendir(FsContext
*ctx
,
652 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
656 fs
->dir
.stream
= NULL
;
657 fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, O_DIRECTORY
);
662 fs
->dir
.stream
= fdopendir(fd
);
663 if (!fs
->dir
.stream
) {
672 static void proxy_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
674 rewinddir(fs
->dir
.stream
);
677 static off_t
proxy_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
679 return telldir(fs
->dir
.stream
);
682 static struct dirent
*proxy_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
684 return readdir(fs
->dir
.stream
);
687 static void proxy_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
689 seekdir(fs
->dir
.stream
, off
);
692 static ssize_t
proxy_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
693 const struct iovec
*iov
,
694 int iovcnt
, off_t offset
)
698 ret
= preadv(fs
->fd
, iov
, iovcnt
, offset
);
700 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
702 ret
= readv(fs
->fd
, iov
, iovcnt
);
708 static ssize_t
proxy_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
709 const struct iovec
*iov
,
710 int iovcnt
, off_t offset
)
715 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
717 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
719 ret
= writev(fs
->fd
, iov
, iovcnt
);
722 #ifdef CONFIG_SYNC_FILE_RANGE
723 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
725 * Initiate a writeback. This is not a data integrity sync.
726 * We want to ensure that we don't leave dirty pages in the cache
727 * after write when writeout=immediate is sepcified.
729 sync_file_range(fs
->fd
, offset
, ret
,
730 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
736 static int proxy_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
739 retval
= v9fs_request(fs_ctx
->private, T_CHMOD
, NULL
, fs_path
,
747 static int proxy_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
748 const char *name
, FsCred
*credp
)
753 v9fs_string_init(&fullname
);
754 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
756 retval
= v9fs_request(fs_ctx
->private, T_MKNOD
, NULL
, &fullname
,
757 credp
->fc_mode
, credp
->fc_rdev
,
758 credp
->fc_uid
, credp
->fc_gid
);
759 v9fs_string_free(&fullname
);
767 static int proxy_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
768 const char *name
, FsCred
*credp
)
773 v9fs_string_init(&fullname
);
774 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
776 retval
= v9fs_request(fs_ctx
->private, T_MKDIR
, NULL
, &fullname
,
777 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
778 v9fs_string_free(&fullname
);
786 static int proxy_fstat(FsContext
*fs_ctx
, int fid_type
,
787 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
791 if (fid_type
== P9_FID_DIR
) {
792 fd
= dirfd(fs
->dir
.stream
);
796 return fstat(fd
, stbuf
);
799 static int proxy_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
800 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
804 v9fs_string_init(&fullname
);
805 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
807 fs
->fd
= v9fs_request(fs_ctx
->private, T_CREATE
, NULL
, &fullname
, flags
,
808 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
809 v9fs_string_free(&fullname
);
817 static int proxy_symlink(FsContext
*fs_ctx
, const char *oldpath
,
818 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
821 V9fsString fullname
, target
;
823 v9fs_string_init(&fullname
);
824 v9fs_string_init(&target
);
826 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
827 v9fs_string_sprintf(&target
, "%s", oldpath
);
829 retval
= v9fs_request(fs_ctx
->private, T_SYMLINK
, NULL
, &target
, &fullname
,
830 credp
->fc_uid
, credp
->fc_gid
);
831 v9fs_string_free(&fullname
);
832 v9fs_string_free(&target
);
840 static int proxy_link(FsContext
*ctx
, V9fsPath
*oldpath
,
841 V9fsPath
*dirpath
, const char *name
)
846 v9fs_string_init(&newpath
);
847 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
849 retval
= v9fs_request(ctx
->private, T_LINK
, NULL
, oldpath
, &newpath
);
850 v9fs_string_free(&newpath
);
858 static int proxy_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
862 retval
= v9fs_request(ctx
->private, T_TRUNCATE
, NULL
, fs_path
, size
);
870 static int proxy_rename(FsContext
*ctx
, const char *oldpath
,
874 V9fsString oldname
, newname
;
876 v9fs_string_init(&oldname
);
877 v9fs_string_init(&newname
);
879 v9fs_string_sprintf(&oldname
, "%s", oldpath
);
880 v9fs_string_sprintf(&newname
, "%s", newpath
);
881 retval
= v9fs_request(ctx
->private, T_RENAME
, NULL
, &oldname
, &newname
);
882 v9fs_string_free(&oldname
);
883 v9fs_string_free(&newname
);
890 static int proxy_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
893 retval
= v9fs_request(fs_ctx
->private, T_CHOWN
, NULL
, fs_path
,
894 credp
->fc_uid
, credp
->fc_gid
);
901 static int proxy_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
902 const struct timespec
*buf
)
905 retval
= v9fs_request(s
->private, T_UTIME
, NULL
, fs_path
,
906 buf
[0].tv_sec
, buf
[0].tv_nsec
,
907 buf
[1].tv_sec
, buf
[1].tv_nsec
);
914 static int proxy_remove(FsContext
*ctx
, const char *path
)
918 v9fs_string_init(&name
);
919 v9fs_string_sprintf(&name
, "%s", path
);
920 retval
= v9fs_request(ctx
->private, T_REMOVE
, NULL
, &name
);
921 v9fs_string_free(&name
);
928 static int proxy_fsync(FsContext
*ctx
, int fid_type
,
929 V9fsFidOpenState
*fs
, int datasync
)
933 if (fid_type
== P9_FID_DIR
) {
934 fd
= dirfd(fs
->dir
.stream
);
940 return qemu_fdatasync(fd
);
946 static int proxy_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
949 retval
= v9fs_request(s
->private, T_STATFS
, stbuf
, fs_path
);
957 static ssize_t
proxy_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
958 const char *name
, void *value
, size_t size
)
963 v9fs_string_init(&xname
);
964 v9fs_string_sprintf(&xname
, "%s", name
);
965 retval
= v9fs_request(ctx
->private, T_LGETXATTR
, value
, size
, fs_path
,
967 v9fs_string_free(&xname
);
974 static ssize_t
proxy_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
975 void *value
, size_t size
)
978 retval
= v9fs_request(ctx
->private, T_LLISTXATTR
, value
, size
, fs_path
);
985 static int proxy_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
986 void *value
, size_t size
, int flags
)
989 V9fsString xname
, xvalue
;
991 v9fs_string_init(&xname
);
992 v9fs_string_sprintf(&xname
, "%s", name
);
994 v9fs_string_init(&xvalue
);
996 xvalue
.data
= g_malloc(size
);
997 memcpy(xvalue
.data
, value
, size
);
999 retval
= v9fs_request(ctx
->private, T_LSETXATTR
, value
, fs_path
, &xname
,
1000 &xvalue
, size
, flags
);
1001 v9fs_string_free(&xname
);
1002 v9fs_string_free(&xvalue
);
1009 static int proxy_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1015 v9fs_string_init(&xname
);
1016 v9fs_string_sprintf(&xname
, "%s", name
);
1017 retval
= v9fs_request(ctx
->private, T_LREMOVEXATTR
, NULL
, fs_path
, &xname
);
1018 v9fs_string_free(&xname
);
1025 static int proxy_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1026 const char *name
, V9fsPath
*target
)
1029 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1031 v9fs_path_sprintf(target
, "%s", name
);
1036 static int proxy_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1037 const char *old_name
, V9fsPath
*newdir
,
1038 const char *new_name
)
1041 V9fsString old_full_name
, new_full_name
;
1043 v9fs_string_init(&old_full_name
);
1044 v9fs_string_init(&new_full_name
);
1046 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1047 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1049 ret
= proxy_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1050 v9fs_string_free(&old_full_name
);
1051 v9fs_string_free(&new_full_name
);
1055 static int proxy_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1056 const char *name
, int flags
)
1059 V9fsString fullname
;
1060 v9fs_string_init(&fullname
);
1062 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1063 ret
= proxy_remove(ctx
, fullname
.data
);
1064 v9fs_string_free(&fullname
);
1069 static int proxy_ioc_getversion(FsContext
*fs_ctx
, V9fsPath
*path
,
1070 mode_t st_mode
, uint64_t *st_gen
)
1074 /* Do not try to open special files like device nodes, fifos etc
1075 * we can get fd for regular files and directories only
1077 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1081 err
= v9fs_request(fs_ctx
->private, T_GETVERSION
, st_gen
, path
);
1089 static int connect_namedsocket(const char *path
, Error
**errp
)
1092 struct sockaddr_un helper
;
1094 if (strlen(path
) >= sizeof(helper
.sun_path
)) {
1095 error_setg(errp
, "socket name too long");
1098 sockfd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1100 error_setg_errno(errp
, errno
, "failed to create client socket");
1103 strcpy(helper
.sun_path
, path
);
1104 helper
.sun_family
= AF_UNIX
;
1105 size
= strlen(helper
.sun_path
) + sizeof(helper
.sun_family
);
1106 if (connect(sockfd
, (struct sockaddr
*)&helper
, size
) < 0) {
1107 error_setg_errno(errp
, errno
, "failed to connect to '%s'", path
);
1112 /* remove the socket for security reasons */
1117 static void error_append_socket_sockfd_hint(Error
**errp
)
1119 error_append_hint(errp
, "Either specify socket=/some/path where /some/path"
1120 " points to a listening AF_UNIX socket or sock_fd=fd"
1121 " where fd is a file descriptor to a connected AF_UNIX"
1125 static int proxy_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fs
, Error
**errp
)
1127 const char *socket
= qemu_opt_get(opts
, "socket");
1128 const char *sock_fd
= qemu_opt_get(opts
, "sock_fd");
1130 if (!socket
&& !sock_fd
) {
1131 error_setg(errp
, "both socket and sock_fd properties are missing");
1132 error_append_socket_sockfd_hint(errp
);
1135 if (socket
&& sock_fd
) {
1136 error_setg(errp
, "both socket and sock_fd properties are set");
1137 error_append_socket_sockfd_hint(errp
);
1141 fs
->path
= g_strdup(socket
);
1142 fs
->export_flags
= V9FS_PROXY_SOCK_NAME
;
1144 fs
->path
= g_strdup(sock_fd
);
1145 fs
->export_flags
= V9FS_PROXY_SOCK_FD
;
1150 static int proxy_init(FsContext
*ctx
, Error
**errp
)
1152 V9fsProxy
*proxy
= g_malloc(sizeof(V9fsProxy
));
1155 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1156 sock_id
= connect_namedsocket(ctx
->fs_root
, errp
);
1158 sock_id
= atoi(ctx
->fs_root
);
1160 error_setg(errp
, "socket descriptor not initialized");
1167 g_free(ctx
->fs_root
);
1168 ctx
->fs_root
= NULL
;
1170 proxy
->in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1171 proxy
->in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1172 proxy
->out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1173 proxy
->out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1175 ctx
->private = proxy
;
1176 proxy
->sockfd
= sock_id
;
1177 qemu_mutex_init(&proxy
->mutex
);
1179 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1180 ctx
->exops
.get_st_gen
= proxy_ioc_getversion
;
1184 static void proxy_cleanup(FsContext
*ctx
)
1186 V9fsProxy
*proxy
= ctx
->private;
1188 g_free(proxy
->out_iovec
.iov_base
);
1189 g_free(proxy
->in_iovec
.iov_base
);
1190 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1191 close(proxy
->sockfd
);
1196 FileOperations proxy_ops
= {
1197 .parse_opts
= proxy_parse_opts
,
1199 .cleanup
= proxy_cleanup
,
1200 .lstat
= proxy_lstat
,
1201 .readlink
= proxy_readlink
,
1202 .close
= proxy_close
,
1203 .closedir
= proxy_closedir
,
1205 .opendir
= proxy_opendir
,
1206 .rewinddir
= proxy_rewinddir
,
1207 .telldir
= proxy_telldir
,
1208 .readdir
= proxy_readdir
,
1209 .seekdir
= proxy_seekdir
,
1210 .preadv
= proxy_preadv
,
1211 .pwritev
= proxy_pwritev
,
1212 .chmod
= proxy_chmod
,
1213 .mknod
= proxy_mknod
,
1214 .mkdir
= proxy_mkdir
,
1215 .fstat
= proxy_fstat
,
1216 .open2
= proxy_open2
,
1217 .symlink
= proxy_symlink
,
1219 .truncate
= proxy_truncate
,
1220 .rename
= proxy_rename
,
1221 .chown
= proxy_chown
,
1222 .utimensat
= proxy_utimensat
,
1223 .remove
= proxy_remove
,
1224 .fsync
= proxy_fsync
,
1225 .statfs
= proxy_statfs
,
1226 .lgetxattr
= proxy_lgetxattr
,
1227 .llistxattr
= proxy_llistxattr
,
1228 .lsetxattr
= proxy_lsetxattr
,
1229 .lremovexattr
= proxy_lremovexattr
,
1230 .name_to_path
= proxy_name_to_path
,
1231 .renameat
= proxy_renameat
,
1232 .unlinkat
= proxy_unlinkat
,