4 * Copyright IBM, Corp. 2011
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>
16 #include "qemu-common.h"
18 #include "qapi/error.h"
19 #include "qemu/cutils.h"
20 #include "qemu/error-report.h"
21 #include "qemu/option.h"
22 #include "fsdev/qemu-fsdev.h"
25 typedef struct V9fsProxy {
28 struct iovec in_iovec;
29 struct iovec out_iovec;
33 * Return received file descriptor on success in *status.
34 * errno is also returned on *status (which will be < 0)
35 * return < 0 on transport error.
37 static int v9fs_receivefd(int sockfd, int *status)
43 union MsgControl msg_control;
46 iov.iov_len = sizeof(data);
48 memset(&msg, 0, sizeof(msg));
51 msg.msg_control = &msg_control;
52 msg.msg_controllen = sizeof(msg_control);
55 retval = recvmsg(sockfd, &msg, 0);
56 } while (retval < 0 && errno == EINTR);
61 * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
62 * request doesn't need ancillary data (fd) or an error occurred,
63 * data is set to negative errno value.
65 if (data != V9FS_FD_VALID) {
70 * File descriptor (fd) is sent in the ancillary data. Check if we
71 * indeed received it. One of the reasons to fail to receive it is if
72 * we exceeded the maximum number of file descriptors!
74 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
75 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
76 cmsg->cmsg_level != SOL_SOCKET ||
77 cmsg->cmsg_type != SCM_RIGHTS) {
80 fd = *((int *)CMSG_DATA(cmsg));
84 *status = -ENFILE; /* Ancillary data sent but not received */
88 static ssize_t socket_read(int sockfd, void *buff, size_t size)
90 ssize_t retval, total = 0;
93 retval = read(sockfd, buff, size);
110 /* Converts proxy_statfs to VFS statfs structure */
111 static void prstatfs_to_statfs(struct statfs *stfs, ProxyStatFS *prstfs)
113 memset(stfs, 0, sizeof(*stfs));
114 stfs->f_type = prstfs->f_type;
115 stfs->f_bsize = prstfs->f_bsize;
116 stfs->f_blocks = prstfs->f_blocks;
117 stfs->f_bfree = prstfs->f_bfree;
118 stfs->f_bavail = prstfs->f_bavail;
119 stfs->f_files = prstfs->f_files;
120 stfs->f_ffree = prstfs->f_ffree;
121 stfs->f_fsid.__val[0] = prstfs->f_fsid[0] & 0xFFFFFFFFU;
122 stfs->f_fsid.__val[1] = prstfs->f_fsid[1] >> 32 & 0xFFFFFFFFU;
123 stfs->f_namelen = prstfs->f_namelen;
124 stfs->f_frsize = prstfs->f_frsize;
127 /* Converts proxy_stat structure to VFS stat structure */
128 static void prstat_to_stat(struct stat *stbuf, ProxyStat *prstat)
130 memset(stbuf, 0, sizeof(*stbuf));
131 stbuf->st_dev = prstat->st_dev;
132 stbuf->st_ino = prstat->st_ino;
133 stbuf->st_nlink = prstat->st_nlink;
134 stbuf->st_mode = prstat->st_mode;
135 stbuf->st_uid = prstat->st_uid;
136 stbuf->st_gid = prstat->st_gid;
137 stbuf->st_rdev = prstat->st_rdev;
138 stbuf->st_size = prstat->st_size;
139 stbuf->st_blksize = prstat->st_blksize;
140 stbuf->st_blocks = prstat->st_blocks;
141 stbuf->st_atim.tv_sec = prstat->st_atim_sec;
142 stbuf->st_atim.tv_nsec = prstat->st_atim_nsec;
143 stbuf->st_mtime = prstat->st_mtim_sec;
144 stbuf->st_mtim.tv_nsec = prstat->st_mtim_nsec;
145 stbuf->st_ctime = prstat->st_ctim_sec;
146 stbuf->st_ctim.tv_nsec = prstat->st_ctim_nsec;
150 * Response contains two parts
152 * header.type == T_ERROR, data -> -errno
153 * header.type == T_SUCCESS, data -> response
154 * size of errno/response is given by header.size
155 * returns < 0, on transport error. response is
156 * valid only if status >= 0.
158 static int v9fs_receive_response(V9fsProxy *proxy, int type,
159 int *status, void *response)
163 struct iovec *reply = &proxy->in_iovec;
167 retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
171 reply->iov_len = PROXY_HDR_SZ;
172 retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
173 assert(retval == 4 * 2);
175 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
178 if (header.size > PROXY_MAX_IO_SZ) {
180 while (header.size > 0) {
181 count = MIN(PROXY_MAX_IO_SZ, header.size);
182 count = socket_read(proxy->sockfd, reply->iov_base, count);
186 header.size -= count;
192 retval = socket_read(proxy->sockfd,
193 reply->iov_base + PROXY_HDR_SZ, header.size);
197 reply->iov_len += header.size;
198 /* there was an error during processing request */
199 if (header.type == T_ERROR) {
201 ret = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
209 retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
210 "qqqdddqqqqqqqqqq", &prstat.st_dev,
211 &prstat.st_ino, &prstat.st_nlink,
212 &prstat.st_mode, &prstat.st_uid,
213 &prstat.st_gid, &prstat.st_rdev,
214 &prstat.st_size, &prstat.st_blksize,
216 &prstat.st_atim_sec, &prstat.st_atim_nsec,
217 &prstat.st_mtim_sec, &prstat.st_mtim_nsec,
218 &prstat.st_ctim_sec, &prstat.st_ctim_nsec);
219 assert(retval == 8 * 3 + 4 * 3 + 8 * 10);
220 prstat_to_stat(response, &prstat);
225 retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
226 "qqqqqqqqqqq", &prstfs.f_type,
227 &prstfs.f_bsize, &prstfs.f_blocks,
228 &prstfs.f_bfree, &prstfs.f_bavail,
229 &prstfs.f_files, &prstfs.f_ffree,
230 &prstfs.f_fsid[0], &prstfs.f_fsid[1],
231 &prstfs.f_namelen, &prstfs.f_frsize);
232 assert(retval == 8 * 11);
233 prstatfs_to_statfs(response, &prstfs);
238 v9fs_string_init(&target);
239 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &target);
240 strcpy(response, target.data);
241 v9fs_string_free(&target);
247 v9fs_string_init(&xattr);
248 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &xattr);
249 memcpy(response, xattr.data, xattr.size);
250 v9fs_string_free(&xattr);
254 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "q", response);
267 * return < 0 on transport error.
268 * *status is valid only if return >= 0
270 static int v9fs_receive_status(V9fsProxy *proxy,
271 struct iovec *reply, int *status)
278 retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
282 reply->iov_len = PROXY_HDR_SZ;
283 retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
284 assert(retval == 4 * 2);
285 retval = socket_read(proxy->sockfd,
286 reply->iov_base + PROXY_HDR_SZ, header.size);
290 reply->iov_len += header.size;
291 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
297 * Proxy->header and proxy->request written to socket by QEMU process.
298 * This request read by proxy helper process
299 * returns 0 on success and -errno on error
301 static int v9fs_request(V9fsProxy *proxy, int type, void *response, ...)
308 ProxyHeader header = { 0, 0};
309 struct timespec spec[2];
310 int flags, mode, uid, gid;
311 V9fsString *name, *value;
312 V9fsString *path, *oldpath;
313 struct iovec *iovec = NULL, *reply = NULL;
315 qemu_mutex_lock(&proxy->mutex);
317 if (proxy->sockfd == -1) {
321 iovec = &proxy->out_iovec;
322 reply = &proxy->in_iovec;
323 va_start(ap, response);
326 path = va_arg(ap, V9fsString *);
327 flags = va_arg(ap, int);
328 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags);
330 header.size = retval;
331 header.type = T_OPEN;
335 path = va_arg(ap, V9fsString *);
336 flags = va_arg(ap, int);
337 mode = va_arg(ap, int);
338 uid = va_arg(ap, int);
339 gid = va_arg(ap, int);
340 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdddd", path,
341 flags, mode, uid, gid);
343 header.size = retval;
344 header.type = T_CREATE;
348 path = va_arg(ap, V9fsString *);
349 mode = va_arg(ap, int);
350 rdev = va_arg(ap, long int);
351 uid = va_arg(ap, int);
352 gid = va_arg(ap, int);
353 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsdq",
354 uid, gid, path, mode, rdev);
356 header.size = retval;
357 header.type = T_MKNOD;
361 path = va_arg(ap, V9fsString *);
362 mode = va_arg(ap, int);
363 uid = va_arg(ap, int);
364 gid = va_arg(ap, int);
365 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsd",
366 uid, gid, path, mode);
368 header.size = retval;
369 header.type = T_MKDIR;
373 oldpath = va_arg(ap, V9fsString *);
374 path = va_arg(ap, V9fsString *);
375 uid = va_arg(ap, int);
376 gid = va_arg(ap, int);
377 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddss",
378 uid, gid, oldpath, path);
380 header.size = retval;
381 header.type = T_SYMLINK;
385 oldpath = va_arg(ap, V9fsString *);
386 path = va_arg(ap, V9fsString *);
387 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss",
390 header.size = retval;
391 header.type = T_LINK;
395 path = va_arg(ap, V9fsString *);
396 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
398 header.size = retval;
399 header.type = T_LSTAT;
403 path = va_arg(ap, V9fsString *);
404 size = va_arg(ap, int);
405 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, size);
407 header.size = retval;
408 header.type = T_READLINK;
412 path = va_arg(ap, V9fsString *);
413 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
415 header.size = retval;
416 header.type = T_STATFS;
420 path = va_arg(ap, V9fsString *);
421 mode = va_arg(ap, int);
422 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, mode);
424 header.size = retval;
425 header.type = T_CHMOD;
429 path = va_arg(ap, V9fsString *);
430 uid = va_arg(ap, int);
431 gid = va_arg(ap, int);
432 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdd", path, uid, gid);
434 header.size = retval;
435 header.type = T_CHOWN;
439 path = va_arg(ap, V9fsString *);
440 offset = va_arg(ap, uint64_t);
441 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sq", path, offset);
443 header.size = retval;
444 header.type = T_TRUNCATE;
448 path = va_arg(ap, V9fsString *);
449 spec[0].tv_sec = va_arg(ap, long);
450 spec[0].tv_nsec = va_arg(ap, long);
451 spec[1].tv_sec = va_arg(ap, long);
452 spec[1].tv_nsec = va_arg(ap, long);
453 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sqqqq", path,
454 spec[0].tv_sec, spec[1].tv_nsec,
455 spec[1].tv_sec, spec[1].tv_nsec);
457 header.size = retval;
458 header.type = T_UTIME;
462 oldpath = va_arg(ap, V9fsString *);
463 path = va_arg(ap, V9fsString *);
464 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", oldpath, path);
466 header.size = retval;
467 header.type = T_RENAME;
471 path = va_arg(ap, V9fsString *);
472 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
474 header.size = retval;
475 header.type = T_REMOVE;
479 size = va_arg(ap, int);
480 path = va_arg(ap, V9fsString *);
481 name = va_arg(ap, V9fsString *);
482 retval = proxy_marshal(iovec, PROXY_HDR_SZ,
483 "dss", size, path, name);
485 header.size = retval;
486 header.type = T_LGETXATTR;
490 size = va_arg(ap, int);
491 path = va_arg(ap, V9fsString *);
492 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ds", size, path);
494 header.size = retval;
495 header.type = T_LLISTXATTR;
499 path = va_arg(ap, V9fsString *);
500 name = va_arg(ap, V9fsString *);
501 value = va_arg(ap, V9fsString *);
502 size = va_arg(ap, int);
503 flags = va_arg(ap, int);
504 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sssdd",
505 path, name, value, size, flags);
507 header.size = retval;
508 header.type = T_LSETXATTR;
512 path = va_arg(ap, V9fsString *);
513 name = va_arg(ap, V9fsString *);
514 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", path, name);
516 header.size = retval;
517 header.type = T_LREMOVEXATTR;
521 path = va_arg(ap, V9fsString *);
522 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
524 header.size = retval;
525 header.type = T_GETVERSION;
529 error_report("Invalid type %d", type);
539 /* marshal the header details */
540 proxy_marshal(iovec, 0, "dd", header.type, header.size);
541 header.size += PROXY_HDR_SZ;
543 retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
544 if (retval != header.size) {
552 * A file descriptor is returned as response for
553 * T_OPEN,T_CREATE on success
555 if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
571 if (v9fs_receive_status(proxy, reply, &retval) < 0) {
579 if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
586 if (v9fs_receive_status(proxy, reply, &retval) < 0) {
590 if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
598 qemu_mutex_unlock(&proxy->mutex);
602 close(proxy->sockfd);
604 qemu_mutex_unlock(&proxy->mutex);
608 static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
611 retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, fs_path);
619 static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
620 char *buf, size_t bufsz)
623 retval = v9fs_request(fs_ctx->private, T_READLINK, buf, fs_path, bufsz);
631 static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
633 return close(fs->fd);
636 static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
638 return closedir(fs->dir.stream);
641 static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
642 int flags, V9fsFidOpenState *fs)
644 fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, flags);
652 static int proxy_opendir(FsContext *ctx,
653 V9fsPath *fs_path, V9fsFidOpenState *fs)
657 fs->dir.stream = NULL;
658 fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, O_DIRECTORY);
663 fs->dir.stream = fdopendir(fd);
664 if (!fs->dir.stream) {
673 static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
675 rewinddir(fs->dir.stream);
678 static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
680 return telldir(fs->dir.stream);
683 static struct dirent *proxy_readdir(FsContext *ctx, V9fsFidOpenState *fs)
685 return readdir(fs->dir.stream);
688 static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
690 seekdir(fs->dir.stream, off);
693 static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
694 const struct iovec *iov,
695 int iovcnt, off_t offset)
699 ret = preadv(fs->fd, iov, iovcnt, offset);
701 ret = lseek(fs->fd, offset, SEEK_SET);
703 ret = readv(fs->fd, iov, iovcnt);
709 static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
710 const struct iovec *iov,
711 int iovcnt, off_t offset)
716 ret = pwritev(fs->fd, iov, iovcnt, offset);
718 ret = lseek(fs->fd, offset, SEEK_SET);
720 ret = writev(fs->fd, iov, iovcnt);
723 #ifdef CONFIG_SYNC_FILE_RANGE
724 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
726 * Initiate a writeback. This is not a data integrity sync.
727 * We want to ensure that we don't leave dirty pages in the cache
728 * after write when writeout=immediate is sepcified.
730 sync_file_range(fs->fd, offset, ret,
731 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
737 static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
740 retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, fs_path,
748 static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
749 const char *name, FsCred *credp)
754 v9fs_string_init(&fullname);
755 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
757 retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, &fullname,
758 credp->fc_mode, credp->fc_rdev,
759 credp->fc_uid, credp->fc_gid);
760 v9fs_string_free(&fullname);
768 static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
769 const char *name, FsCred *credp)
774 v9fs_string_init(&fullname);
775 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
777 retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, &fullname,
778 credp->fc_mode, credp->fc_uid, credp->fc_gid);
779 v9fs_string_free(&fullname);
787 static int proxy_fstat(FsContext *fs_ctx, int fid_type,
788 V9fsFidOpenState *fs, struct stat *stbuf)
792 if (fid_type == P9_FID_DIR) {
793 fd = dirfd(fs->dir.stream);
797 return fstat(fd, stbuf);
800 static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
801 int flags, FsCred *credp, V9fsFidOpenState *fs)
805 v9fs_string_init(&fullname);
806 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
808 fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, &fullname, flags,
809 credp->fc_mode, 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, &target, &fullname,
831 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, 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, 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, &oldname, &newname);
883 v9fs_string_free(&oldname);
884 v9fs_string_free(&newname);
891 static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
894 retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, fs_path,
895 credp->fc_uid, credp->fc_gid);
902 static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
903 const struct timespec *buf)
906 retval = v9fs_request(s->private, T_UTIME, NULL, fs_path,
907 buf[0].tv_sec, buf[0].tv_nsec,
908 buf[1].tv_sec, buf[1].tv_nsec);
915 static int proxy_remove(FsContext *ctx, const char *path)
919 v9fs_string_init(&name);
920 v9fs_string_sprintf(&name, "%s", path);
921 retval = v9fs_request(ctx->private, T_REMOVE, NULL, &name);
922 v9fs_string_free(&name);
929 static int proxy_fsync(FsContext *ctx, int fid_type,
930 V9fsFidOpenState *fs, int datasync)
934 if (fid_type == P9_FID_DIR) {
935 fd = dirfd(fs->dir.stream);
941 return qemu_fdatasync(fd);
947 static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
950 retval = v9fs_request(s->private, T_STATFS, stbuf, fs_path);
958 static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
959 const char *name, void *value, size_t size)
964 v9fs_string_init(&xname);
965 v9fs_string_sprintf(&xname, "%s", name);
966 retval = v9fs_request(ctx->private, T_LGETXATTR, value, size, fs_path,
968 v9fs_string_free(&xname);
975 static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
976 void *value, size_t size)
979 retval = v9fs_request(ctx->private, T_LLISTXATTR, value, size, fs_path);
986 static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
987 void *value, size_t size, int flags)
990 V9fsString xname, xvalue;
992 v9fs_string_init(&xname);
993 v9fs_string_sprintf(&xname, "%s", name);
995 v9fs_string_init(&xvalue);
997 xvalue.data = g_malloc(size);
998 memcpy(xvalue.data, value, size);
1000 retval = v9fs_request(ctx->private, T_LSETXATTR, value, fs_path, &xname,
1001 &xvalue, size, flags);
1002 v9fs_string_free(&xname);
1003 v9fs_string_free(&xvalue);
1010 static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1016 v9fs_string_init(&xname);
1017 v9fs_string_sprintf(&xname, "%s", name);
1018 retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, fs_path, &xname);
1019 v9fs_string_free(&xname);
1026 static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1027 const char *name, V9fsPath *target)
1030 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1032 v9fs_path_sprintf(target, "%s", name);
1037 static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
1038 const char *old_name, V9fsPath *newdir,
1039 const char *new_name)
1042 V9fsString old_full_name, new_full_name;
1044 v9fs_string_init(&old_full_name);
1045 v9fs_string_init(&new_full_name);
1047 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1048 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1050 ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
1051 v9fs_string_free(&old_full_name);
1052 v9fs_string_free(&new_full_name);
1056 static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
1057 const char *name, int flags)
1060 V9fsString fullname;
1061 v9fs_string_init(&fullname);
1063 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1064 ret = proxy_remove(ctx, fullname.data);
1065 v9fs_string_free(&fullname);
1070 static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
1071 mode_t st_mode, uint64_t *st_gen)
1075 /* Do not try to open special files like device nodes, fifos etc
1076 * we can get fd for regular files and directories only
1078 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1082 err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, path);
1090 static int connect_namedsocket(const char *path, Error **errp)
1093 struct sockaddr_un helper;
1095 if (strlen(path) >= sizeof(helper.sun_path)) {
1096 error_setg(errp, "socket name too long");
1099 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1101 error_setg_errno(errp, errno, "failed to create client socket");
1104 strcpy(helper.sun_path, path);
1105 helper.sun_family = AF_UNIX;
1106 if (connect(sockfd, (struct sockaddr *)&helper, sizeof(helper)) < 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 *const *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;
1192 g_free(proxy->out_iovec.iov_base);
1193 g_free(proxy->in_iovec.iov_base);
1194 if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1195 close(proxy->sockfd);
1200 FileOperations proxy_ops = {
1201 .parse_opts = proxy_parse_opts,
1203 .cleanup = proxy_cleanup,
1204 .lstat = proxy_lstat,
1205 .readlink = proxy_readlink,
1206 .close = proxy_close,
1207 .closedir = proxy_closedir,
1209 .opendir = proxy_opendir,
1210 .rewinddir = proxy_rewinddir,
1211 .telldir = proxy_telldir,
1212 .readdir = proxy_readdir,
1213 .seekdir = proxy_seekdir,
1214 .preadv = proxy_preadv,
1215 .pwritev = proxy_pwritev,
1216 .chmod = proxy_chmod,
1217 .mknod = proxy_mknod,
1218 .mkdir = proxy_mkdir,
1219 .fstat = proxy_fstat,
1220 .open2 = proxy_open2,
1221 .symlink = proxy_symlink,
1223 .truncate = proxy_truncate,
1224 .rename = proxy_rename,
1225 .chown = proxy_chown,
1226 .utimensat = proxy_utimensat,
1227 .remove = proxy_remove,
1228 .fsync = proxy_fsync,
1229 .statfs = proxy_statfs,
1230 .lgetxattr = proxy_lgetxattr,
1231 .llistxattr = proxy_llistxattr,
1232 .lsetxattr = proxy_lsetxattr,
1233 .lremovexattr = proxy_lremovexattr,
1234 .name_to_path = proxy_name_to_path,
1235 .renameat = proxy_renameat,
1236 .unlinkat = proxy_unlinkat,