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
,
298 void *response
, const char *fmt
, ...)
305 ProxyHeader header
= { 0, 0};
306 struct timespec spec
[2];
307 int flags
, mode
, uid
, gid
;
308 V9fsString
*name
, *value
;
309 V9fsString
*path
, *oldpath
;
310 struct iovec
*iovec
= NULL
, *reply
= NULL
;
312 qemu_mutex_lock(&proxy
->mutex
);
314 if (proxy
->sockfd
== -1) {
318 iovec
= &proxy
->out_iovec
;
319 reply
= &proxy
->in_iovec
;
323 path
= va_arg(ap
, V9fsString
*);
324 flags
= va_arg(ap
, int);
325 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, flags
);
327 header
.size
= retval
;
328 header
.type
= T_OPEN
;
332 path
= va_arg(ap
, V9fsString
*);
333 flags
= va_arg(ap
, int);
334 mode
= va_arg(ap
, int);
335 uid
= va_arg(ap
, int);
336 gid
= va_arg(ap
, int);
337 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdddd", path
,
338 flags
, mode
, uid
, gid
);
340 header
.size
= retval
;
341 header
.type
= T_CREATE
;
345 path
= va_arg(ap
, V9fsString
*);
346 mode
= va_arg(ap
, int);
347 rdev
= va_arg(ap
, long int);
348 uid
= va_arg(ap
, int);
349 gid
= va_arg(ap
, int);
350 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsdq",
351 uid
, gid
, path
, mode
, rdev
);
353 header
.size
= retval
;
354 header
.type
= T_MKNOD
;
358 path
= va_arg(ap
, V9fsString
*);
359 mode
= va_arg(ap
, int);
360 uid
= va_arg(ap
, int);
361 gid
= va_arg(ap
, int);
362 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsd",
363 uid
, gid
, path
, mode
);
365 header
.size
= retval
;
366 header
.type
= T_MKDIR
;
370 oldpath
= va_arg(ap
, V9fsString
*);
371 path
= va_arg(ap
, V9fsString
*);
372 uid
= va_arg(ap
, int);
373 gid
= va_arg(ap
, int);
374 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddss",
375 uid
, gid
, oldpath
, path
);
377 header
.size
= retval
;
378 header
.type
= T_SYMLINK
;
382 oldpath
= va_arg(ap
, V9fsString
*);
383 path
= va_arg(ap
, V9fsString
*);
384 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss",
387 header
.size
= retval
;
388 header
.type
= T_LINK
;
392 path
= va_arg(ap
, V9fsString
*);
393 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
395 header
.size
= retval
;
396 header
.type
= T_LSTAT
;
400 path
= va_arg(ap
, V9fsString
*);
401 size
= va_arg(ap
, int);
402 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, size
);
404 header
.size
= retval
;
405 header
.type
= T_READLINK
;
409 path
= va_arg(ap
, V9fsString
*);
410 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
412 header
.size
= retval
;
413 header
.type
= T_STATFS
;
417 path
= va_arg(ap
, V9fsString
*);
418 mode
= va_arg(ap
, int);
419 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, mode
);
421 header
.size
= retval
;
422 header
.type
= T_CHMOD
;
426 path
= va_arg(ap
, V9fsString
*);
427 uid
= va_arg(ap
, int);
428 gid
= va_arg(ap
, int);
429 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdd", path
, uid
, gid
);
431 header
.size
= retval
;
432 header
.type
= T_CHOWN
;
436 path
= va_arg(ap
, V9fsString
*);
437 offset
= va_arg(ap
, uint64_t);
438 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sq", path
, offset
);
440 header
.size
= retval
;
441 header
.type
= T_TRUNCATE
;
445 path
= va_arg(ap
, V9fsString
*);
446 spec
[0].tv_sec
= va_arg(ap
, long);
447 spec
[0].tv_nsec
= va_arg(ap
, long);
448 spec
[1].tv_sec
= va_arg(ap
, long);
449 spec
[1].tv_nsec
= va_arg(ap
, long);
450 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sqqqq", path
,
451 spec
[0].tv_sec
, spec
[1].tv_nsec
,
452 spec
[1].tv_sec
, spec
[1].tv_nsec
);
454 header
.size
= retval
;
455 header
.type
= T_UTIME
;
459 oldpath
= va_arg(ap
, V9fsString
*);
460 path
= va_arg(ap
, V9fsString
*);
461 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", oldpath
, path
);
463 header
.size
= retval
;
464 header
.type
= T_RENAME
;
468 path
= va_arg(ap
, V9fsString
*);
469 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
471 header
.size
= retval
;
472 header
.type
= T_REMOVE
;
476 size
= va_arg(ap
, int);
477 path
= va_arg(ap
, V9fsString
*);
478 name
= va_arg(ap
, V9fsString
*);
479 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
,
480 "dss", size
, path
, name
);
482 header
.size
= retval
;
483 header
.type
= T_LGETXATTR
;
487 size
= va_arg(ap
, int);
488 path
= va_arg(ap
, V9fsString
*);
489 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ds", size
, path
);
491 header
.size
= retval
;
492 header
.type
= T_LLISTXATTR
;
496 path
= va_arg(ap
, V9fsString
*);
497 name
= va_arg(ap
, V9fsString
*);
498 value
= va_arg(ap
, V9fsString
*);
499 size
= va_arg(ap
, int);
500 flags
= va_arg(ap
, int);
501 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sssdd",
502 path
, name
, value
, size
, flags
);
504 header
.size
= retval
;
505 header
.type
= T_LSETXATTR
;
509 path
= va_arg(ap
, V9fsString
*);
510 name
= va_arg(ap
, V9fsString
*);
511 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", path
, name
);
513 header
.size
= retval
;
514 header
.type
= T_LREMOVEXATTR
;
518 path
= va_arg(ap
, V9fsString
*);
519 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
521 header
.size
= retval
;
522 header
.type
= T_GETVERSION
;
526 error_report("Invalid type %d", type
);
536 /* marshal the header details */
537 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
538 header
.size
+= PROXY_HDR_SZ
;
540 retval
= qemu_write_full(proxy
->sockfd
, iovec
->iov_base
, header
.size
);
541 if (retval
!= header
.size
) {
549 * A file descriptor is returned as response for
550 * T_OPEN,T_CREATE on success
552 if (v9fs_receivefd(proxy
->sockfd
, &retval
) < 0) {
568 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
576 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
583 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
587 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
595 qemu_mutex_unlock(&proxy
->mutex
);
599 close(proxy
->sockfd
);
601 qemu_mutex_unlock(&proxy
->mutex
);
605 static int proxy_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
608 retval
= v9fs_request(fs_ctx
->private, T_LSTAT
, stbuf
, "s", fs_path
);
616 static ssize_t
proxy_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
617 char *buf
, size_t bufsz
)
620 retval
= v9fs_request(fs_ctx
->private, T_READLINK
, buf
, "sd",
629 static int proxy_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
631 return close(fs
->fd
);
634 static int proxy_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
636 return closedir(fs
->dir
.stream
);
639 static int proxy_open(FsContext
*ctx
, V9fsPath
*fs_path
,
640 int flags
, V9fsFidOpenState
*fs
)
642 fs
->fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, "sd", fs_path
, flags
);
650 static int proxy_opendir(FsContext
*ctx
,
651 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
655 fs
->dir
.stream
= NULL
;
656 fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, "sd", fs_path
, O_DIRECTORY
);
661 fs
->dir
.stream
= fdopendir(fd
);
662 if (!fs
->dir
.stream
) {
671 static void proxy_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
673 rewinddir(fs
->dir
.stream
);
676 static off_t
proxy_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
678 return telldir(fs
->dir
.stream
);
681 static struct dirent
*proxy_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
683 return readdir(fs
->dir
.stream
);
686 static void proxy_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
688 seekdir(fs
->dir
.stream
, off
);
691 static ssize_t
proxy_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
692 const struct iovec
*iov
,
693 int iovcnt
, off_t offset
)
697 ret
= preadv(fs
->fd
, iov
, iovcnt
, offset
);
699 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
701 ret
= readv(fs
->fd
, iov
, iovcnt
);
707 static ssize_t
proxy_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
708 const struct iovec
*iov
,
709 int iovcnt
, off_t offset
)
714 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
716 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
718 ret
= writev(fs
->fd
, iov
, iovcnt
);
721 #ifdef CONFIG_SYNC_FILE_RANGE
722 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
724 * Initiate a writeback. This is not a data integrity sync.
725 * We want to ensure that we don't leave dirty pages in the cache
726 * after write when writeout=immediate is sepcified.
728 sync_file_range(fs
->fd
, offset
, ret
,
729 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
735 static int proxy_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
738 retval
= v9fs_request(fs_ctx
->private, T_CHMOD
, NULL
, "sd",
739 fs_path
, credp
->fc_mode
);
746 static int proxy_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
747 const char *name
, FsCred
*credp
)
752 v9fs_string_init(&fullname
);
753 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
755 retval
= v9fs_request(fs_ctx
->private, T_MKNOD
, NULL
, "sdqdd",
756 &fullname
, credp
->fc_mode
, credp
->fc_rdev
,
757 credp
->fc_uid
, credp
->fc_gid
);
758 v9fs_string_free(&fullname
);
766 static int proxy_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
767 const char *name
, FsCred
*credp
)
772 v9fs_string_init(&fullname
);
773 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
775 retval
= v9fs_request(fs_ctx
->private, T_MKDIR
, NULL
, "sddd", &fullname
,
776 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
777 v9fs_string_free(&fullname
);
782 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
, "sdddd",
808 &fullname
, flags
, credp
->fc_mode
,
809 credp
->fc_uid
, credp
->fc_gid
);
810 v9fs_string_free(&fullname
);
818 static int proxy_symlink(FsContext
*fs_ctx
, const char *oldpath
,
819 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
822 V9fsString fullname
, target
;
824 v9fs_string_init(&fullname
);
825 v9fs_string_init(&target
);
827 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
828 v9fs_string_sprintf(&target
, "%s", oldpath
);
830 retval
= v9fs_request(fs_ctx
->private, T_SYMLINK
, NULL
, "ssdd",
831 &target
, &fullname
, credp
->fc_uid
, credp
->fc_gid
);
832 v9fs_string_free(&fullname
);
833 v9fs_string_free(&target
);
841 static int proxy_link(FsContext
*ctx
, V9fsPath
*oldpath
,
842 V9fsPath
*dirpath
, const char *name
)
847 v9fs_string_init(&newpath
);
848 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
850 retval
= v9fs_request(ctx
->private, T_LINK
, NULL
, "ss", oldpath
, &newpath
);
851 v9fs_string_free(&newpath
);
859 static int proxy_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
863 retval
= v9fs_request(ctx
->private, T_TRUNCATE
, NULL
, "sq", fs_path
, size
);
871 static int proxy_rename(FsContext
*ctx
, const char *oldpath
,
875 V9fsString oldname
, newname
;
877 v9fs_string_init(&oldname
);
878 v9fs_string_init(&newname
);
880 v9fs_string_sprintf(&oldname
, "%s", oldpath
);
881 v9fs_string_sprintf(&newname
, "%s", newpath
);
882 retval
= v9fs_request(ctx
->private, T_RENAME
, NULL
, "ss",
884 v9fs_string_free(&oldname
);
885 v9fs_string_free(&newname
);
892 static int proxy_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
895 retval
= v9fs_request(fs_ctx
->private, T_CHOWN
, NULL
, "sdd",
896 fs_path
, credp
->fc_uid
, credp
->fc_gid
);
903 static int proxy_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
904 const struct timespec
*buf
)
907 retval
= v9fs_request(s
->private, T_UTIME
, NULL
, "sqqqq",
909 buf
[0].tv_sec
, buf
[0].tv_nsec
,
910 buf
[1].tv_sec
, buf
[1].tv_nsec
);
917 static int proxy_remove(FsContext
*ctx
, const char *path
)
921 v9fs_string_init(&name
);
922 v9fs_string_sprintf(&name
, "%s", path
);
923 retval
= v9fs_request(ctx
->private, T_REMOVE
, NULL
, "s", &name
);
924 v9fs_string_free(&name
);
931 static int proxy_fsync(FsContext
*ctx
, int fid_type
,
932 V9fsFidOpenState
*fs
, int datasync
)
936 if (fid_type
== P9_FID_DIR
) {
937 fd
= dirfd(fs
->dir
.stream
);
943 return qemu_fdatasync(fd
);
949 static int proxy_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
952 retval
= v9fs_request(s
->private, T_STATFS
, stbuf
, "s", fs_path
);
960 static ssize_t
proxy_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
961 const char *name
, void *value
, size_t size
)
966 v9fs_string_init(&xname
);
967 v9fs_string_sprintf(&xname
, "%s", name
);
968 retval
= v9fs_request(ctx
->private, T_LGETXATTR
, value
, "dss", size
,
970 v9fs_string_free(&xname
);
977 static ssize_t
proxy_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
978 void *value
, size_t size
)
981 retval
= v9fs_request(ctx
->private, T_LLISTXATTR
, value
, "ds", size
,
989 static int proxy_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
990 void *value
, size_t size
, int flags
)
993 V9fsString xname
, xvalue
;
995 v9fs_string_init(&xname
);
996 v9fs_string_sprintf(&xname
, "%s", name
);
998 v9fs_string_init(&xvalue
);
1000 xvalue
.data
= g_malloc(size
);
1001 memcpy(xvalue
.data
, value
, size
);
1003 retval
= v9fs_request(ctx
->private, T_LSETXATTR
, value
, "sssdd",
1004 fs_path
, &xname
, &xvalue
, size
, flags
);
1005 v9fs_string_free(&xname
);
1006 v9fs_string_free(&xvalue
);
1013 static int proxy_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1019 v9fs_string_init(&xname
);
1020 v9fs_string_sprintf(&xname
, "%s", name
);
1021 retval
= v9fs_request(ctx
->private, T_LREMOVEXATTR
, NULL
, "ss",
1023 v9fs_string_free(&xname
);
1030 static int proxy_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1031 const char *name
, V9fsPath
*target
)
1034 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1035 dir_path
->data
, name
);
1037 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1039 /* Bump the size for including terminating NULL */
1044 static int proxy_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1045 const char *old_name
, V9fsPath
*newdir
,
1046 const char *new_name
)
1049 V9fsString old_full_name
, new_full_name
;
1051 v9fs_string_init(&old_full_name
);
1052 v9fs_string_init(&new_full_name
);
1054 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1055 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1057 ret
= proxy_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1058 v9fs_string_free(&old_full_name
);
1059 v9fs_string_free(&new_full_name
);
1063 static int proxy_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1064 const char *name
, int flags
)
1067 V9fsString fullname
;
1068 v9fs_string_init(&fullname
);
1070 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1071 ret
= proxy_remove(ctx
, fullname
.data
);
1072 v9fs_string_free(&fullname
);
1077 static int proxy_ioc_getversion(FsContext
*fs_ctx
, V9fsPath
*path
,
1078 mode_t st_mode
, uint64_t *st_gen
)
1082 /* Do not try to open special files like device nodes, fifos etc
1083 * we can get fd for regular files and directories only
1085 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1089 err
= v9fs_request(fs_ctx
->private, T_GETVERSION
, st_gen
, "s", path
);
1097 static int connect_namedsocket(const char *path
)
1100 struct sockaddr_un helper
;
1102 if (strlen(path
) >= sizeof(helper
.sun_path
)) {
1103 error_report("Socket name too long");
1106 sockfd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1108 error_report("Failed to create socket: %s", strerror(errno
));
1111 strcpy(helper
.sun_path
, path
);
1112 helper
.sun_family
= AF_UNIX
;
1113 size
= strlen(helper
.sun_path
) + sizeof(helper
.sun_family
);
1114 if (connect(sockfd
, (struct sockaddr
*)&helper
, size
) < 0) {
1115 error_report("Failed to connect to %s: %s", path
, strerror(errno
));
1120 /* remove the socket for security reasons */
1125 static int proxy_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fs
)
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_report("Must specify either socket or sock_fd");
1134 if (socket
&& sock_fd
) {
1135 error_report("Both socket and sock_fd options specified");
1139 fs
->path
= g_strdup(socket
);
1140 fs
->export_flags
= V9FS_PROXY_SOCK_NAME
;
1142 fs
->path
= g_strdup(sock_fd
);
1143 fs
->export_flags
= V9FS_PROXY_SOCK_FD
;
1148 static int proxy_init(FsContext
*ctx
)
1150 V9fsProxy
*proxy
= g_malloc(sizeof(V9fsProxy
));
1153 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1154 sock_id
= connect_namedsocket(ctx
->fs_root
);
1156 sock_id
= atoi(ctx
->fs_root
);
1158 error_report("Socket descriptor not initialized");
1165 g_free(ctx
->fs_root
);
1166 ctx
->fs_root
= NULL
;
1168 proxy
->in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1169 proxy
->in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1170 proxy
->out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1171 proxy
->out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1173 ctx
->private = proxy
;
1174 proxy
->sockfd
= sock_id
;
1175 qemu_mutex_init(&proxy
->mutex
);
1177 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1178 ctx
->exops
.get_st_gen
= proxy_ioc_getversion
;
1182 FileOperations proxy_ops
= {
1183 .parse_opts
= proxy_parse_opts
,
1185 .lstat
= proxy_lstat
,
1186 .readlink
= proxy_readlink
,
1187 .close
= proxy_close
,
1188 .closedir
= proxy_closedir
,
1190 .opendir
= proxy_opendir
,
1191 .rewinddir
= proxy_rewinddir
,
1192 .telldir
= proxy_telldir
,
1193 .readdir
= proxy_readdir
,
1194 .seekdir
= proxy_seekdir
,
1195 .preadv
= proxy_preadv
,
1196 .pwritev
= proxy_pwritev
,
1197 .chmod
= proxy_chmod
,
1198 .mknod
= proxy_mknod
,
1199 .mkdir
= proxy_mkdir
,
1200 .fstat
= proxy_fstat
,
1201 .open2
= proxy_open2
,
1202 .symlink
= proxy_symlink
,
1204 .truncate
= proxy_truncate
,
1205 .rename
= proxy_rename
,
1206 .chown
= proxy_chown
,
1207 .utimensat
= proxy_utimensat
,
1208 .remove
= proxy_remove
,
1209 .fsync
= proxy_fsync
,
1210 .statfs
= proxy_statfs
,
1211 .lgetxattr
= proxy_lgetxattr
,
1212 .llistxattr
= proxy_llistxattr
,
1213 .lsetxattr
= proxy_lsetxattr
,
1214 .lremovexattr
= proxy_lremovexattr
,
1215 .name_to_path
= proxy_name_to_path
,
1216 .renameat
= proxy_renameat
,
1217 .unlinkat
= proxy_unlinkat
,