blob: e2e03292de2cf8133d39a061bd379fe7bf85def9 [file] [log] [blame]
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301/*
Wei Liu494a8eb2015-11-18 18:21:14 +00002 * 9p Proxy callback
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05303 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * M. Mohan Kumar <mohan@in.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 */
Markus Armbrustere688df62018-02-01 12:18:31 +010012
Peter Maydellfbc04122016-01-26 18:17:10 +000013#include "qemu/osdep.h"
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +053014#include <sys/socket.h>
15#include <sys/un.h>
Wei Liuebe74f82016-01-07 18:18:02 +000016#include "9p.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010017#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020018#include "qemu/cutils.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010019#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010020#include "qemu/option.h"
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +053021#include "fsdev/qemu-fsdev.h"
Wei Liu494a8eb2015-11-18 18:21:14 +000022#include "9p-proxy.h"
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +053023
24typedef struct V9fsProxy {
25 int sockfd;
26 QemuMutex mutex;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +053027 struct iovec in_iovec;
28 struct iovec out_iovec;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +053029} V9fsProxy;
30
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +053031/*
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.
35 */
36static int v9fs_receivefd(int sockfd, int *status)
37{
38 struct iovec iov;
39 struct msghdr msg;
40 struct cmsghdr *cmsg;
41 int retval, data, fd;
42 union MsgControl msg_control;
43
44 iov.iov_base = &data;
45 iov.iov_len = sizeof(data);
46
47 memset(&msg, 0, sizeof(msg));
48 msg.msg_iov = &iov;
49 msg.msg_iovlen = 1;
50 msg.msg_control = &msg_control;
51 msg.msg_controllen = sizeof(msg_control);
52
53 do {
54 retval = recvmsg(sockfd, &msg, 0);
55 } while (retval < 0 && errno == EINTR);
56 if (retval <= 0) {
57 return retval;
58 }
59 /*
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.
63 */
64 if (data != V9FS_FD_VALID) {
65 *status = data;
66 return 0;
67 }
68 /*
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!
72 */
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) {
77 continue;
78 }
79 fd = *((int *)CMSG_DATA(cmsg));
80 *status = fd;
81 return 0;
82 }
83 *status = -ENFILE; /* Ancillary data sent but not received */
84 return 0;
85}
86
M. Mohan Kumar39f8c322011-12-14 13:58:45 +053087static ssize_t socket_read(int sockfd, void *buff, size_t size)
88{
89 ssize_t retval, total = 0;
90
91 while (size) {
92 retval = read(sockfd, buff, size);
93 if (retval == 0) {
94 return -EIO;
95 }
96 if (retval < 0) {
97 if (errno == EINTR) {
98 continue;
99 }
100 return -errno;
101 }
102 size -= retval;
103 buff += retval;
104 total += retval;
105 }
106 return total;
107}
108
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530109/* Converts proxy_statfs to VFS statfs structure */
110static void prstatfs_to_statfs(struct statfs *stfs, ProxyStatFS *prstfs)
111{
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;
Peter Maydell2afee492012-01-06 18:47:21 +0000120 stfs->f_fsid.__val[0] = prstfs->f_fsid[0] & 0xFFFFFFFFU;
121 stfs->f_fsid.__val[1] = prstfs->f_fsid[1] >> 32 & 0xFFFFFFFFU;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530122 stfs->f_namelen = prstfs->f_namelen;
123 stfs->f_frsize = prstfs->f_frsize;
124}
125
126/* Converts proxy_stat structure to VFS stat structure */
127static void prstat_to_stat(struct stat *stbuf, ProxyStat *prstat)
128{
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;
146}
147
148/*
149 * Response contains two parts
150 * {header, data}
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.
156 */
157static int v9fs_receive_response(V9fsProxy *proxy, int type,
158 int *status, void *response)
159{
160 int retval;
161 ProxyHeader header;
162 struct iovec *reply = &proxy->in_iovec;
163
164 *status = 0;
165 reply->iov_len = 0;
166 retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
167 if (retval < 0) {
168 return retval;
169 }
170 reply->iov_len = PROXY_HDR_SZ;
Greg Kurz262169a2017-03-21 09:12:47 +0100171 retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
172 assert(retval == 4 * 2);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530173 /*
174 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
175 * return -ENOBUFS
176 */
177 if (header.size > PROXY_MAX_IO_SZ) {
178 int count;
179 while (header.size > 0) {
180 count = MIN(PROXY_MAX_IO_SZ, header.size);
181 count = socket_read(proxy->sockfd, reply->iov_base, count);
182 if (count < 0) {
183 return count;
184 }
185 header.size -= count;
186 }
187 *status = -ENOBUFS;
188 return 0;
189 }
190
191 retval = socket_read(proxy->sockfd,
192 reply->iov_base + PROXY_HDR_SZ, header.size);
193 if (retval < 0) {
194 return retval;
195 }
196 reply->iov_len += header.size;
197 /* there was an error during processing request */
198 if (header.type == T_ERROR) {
199 int ret;
200 ret = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
Greg Kurz262169a2017-03-21 09:12:47 +0100201 assert(ret == 4);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530202 return 0;
203 }
204
205 switch (type) {
206 case T_LSTAT: {
207 ProxyStat prstat;
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,
214 &prstat.st_blocks,
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);
Greg Kurz262169a2017-03-21 09:12:47 +0100218 assert(retval == 8 * 3 + 4 * 3 + 8 * 10);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530219 prstat_to_stat(response, &prstat);
220 break;
221 }
222 case T_STATFS: {
223 ProxyStatFS prstfs;
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);
Greg Kurz262169a2017-03-21 09:12:47 +0100231 assert(retval == 8 * 11);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530232 prstatfs_to_statfs(response, &prstfs);
233 break;
234 }
235 case T_READLINK: {
236 V9fsString target;
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);
241 break;
242 }
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530243 case T_LGETXATTR:
244 case T_LLISTXATTR: {
245 V9fsString xattr;
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);
250 break;
251 }
M. Mohan Kumard090e452011-12-14 13:58:46 +0530252 case T_GETVERSION:
Greg Kurz262169a2017-03-21 09:12:47 +0100253 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "q", response);
254 assert(retval == 8);
M. Mohan Kumard090e452011-12-14 13:58:46 +0530255 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530256 default:
257 return -1;
258 }
259 if (retval < 0) {
260 *status = retval;
261 }
262 return 0;
263}
264
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530265/*
266 * return < 0 on transport error.
267 * *status is valid only if return >= 0
268 */
269static int v9fs_receive_status(V9fsProxy *proxy,
270 struct iovec *reply, int *status)
271{
272 int retval;
273 ProxyHeader header;
274
275 *status = 0;
276 reply->iov_len = 0;
277 retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
278 if (retval < 0) {
279 return retval;
280 }
281 reply->iov_len = PROXY_HDR_SZ;
Greg Kurz262169a2017-03-21 09:12:47 +0100282 retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
283 assert(retval == 4 * 2);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530284 retval = socket_read(proxy->sockfd,
285 reply->iov_base + PROXY_HDR_SZ, header.size);
286 if (retval < 0) {
287 return retval;
288 }
289 reply->iov_len += header.size;
Greg Kurz262169a2017-03-21 09:12:47 +0100290 retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
291 assert(retval == 4);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530292 return 0;
293}
294
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530295/*
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
299 */
Greg Kurz799fe082016-09-16 08:56:14 +0200300static int v9fs_request(V9fsProxy *proxy, int type, void *response, ...)
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530301{
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530302 dev_t rdev;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530303 va_list ap;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530304 int size = 0;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530305 int retval = 0;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530306 uint64_t offset;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530307 ProxyHeader header = { 0, 0};
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530308 struct timespec spec[2];
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530309 int flags, mode, uid, gid;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530310 V9fsString *name, *value;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530311 V9fsString *path, *oldpath;
312 struct iovec *iovec = NULL, *reply = NULL;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530313
314 qemu_mutex_lock(&proxy->mutex);
315
316 if (proxy->sockfd == -1) {
317 retval = -EIO;
318 goto err_out;
319 }
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530320 iovec = &proxy->out_iovec;
321 reply = &proxy->in_iovec;
Greg Kurz799fe082016-09-16 08:56:14 +0200322 va_start(ap, response);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530323 switch (type) {
324 case T_OPEN:
325 path = va_arg(ap, V9fsString *);
326 flags = va_arg(ap, int);
327 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags);
328 if (retval > 0) {
329 header.size = retval;
330 header.type = T_OPEN;
331 }
332 break;
333 case T_CREATE:
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);
341 if (retval > 0) {
342 header.size = retval;
343 header.type = T_CREATE;
344 }
345 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530346 case T_MKNOD:
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);
354 if (retval > 0) {
355 header.size = retval;
356 header.type = T_MKNOD;
357 }
358 break;
359 case T_MKDIR:
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);
366 if (retval > 0) {
367 header.size = retval;
368 header.type = T_MKDIR;
369 }
370 break;
371 case T_SYMLINK:
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);
378 if (retval > 0) {
379 header.size = retval;
380 header.type = T_SYMLINK;
381 }
382 break;
383 case T_LINK:
384 oldpath = va_arg(ap, V9fsString *);
385 path = va_arg(ap, V9fsString *);
386 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss",
387 oldpath, path);
388 if (retval > 0) {
389 header.size = retval;
390 header.type = T_LINK;
391 }
392 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530393 case T_LSTAT:
394 path = va_arg(ap, V9fsString *);
395 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
396 if (retval > 0) {
397 header.size = retval;
398 header.type = T_LSTAT;
399 }
400 break;
401 case T_READLINK:
402 path = va_arg(ap, V9fsString *);
403 size = va_arg(ap, int);
404 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, size);
405 if (retval > 0) {
406 header.size = retval;
407 header.type = T_READLINK;
408 }
409 break;
410 case T_STATFS:
411 path = va_arg(ap, V9fsString *);
412 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
413 if (retval > 0) {
414 header.size = retval;
415 header.type = T_STATFS;
416 }
417 break;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530418 case T_CHMOD:
419 path = va_arg(ap, V9fsString *);
420 mode = va_arg(ap, int);
421 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, mode);
422 if (retval > 0) {
423 header.size = retval;
424 header.type = T_CHMOD;
425 }
426 break;
427 case T_CHOWN:
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);
432 if (retval > 0) {
433 header.size = retval;
434 header.type = T_CHOWN;
435 }
436 break;
437 case T_TRUNCATE:
438 path = va_arg(ap, V9fsString *);
439 offset = va_arg(ap, uint64_t);
440 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sq", path, offset);
441 if (retval > 0) {
442 header.size = retval;
443 header.type = T_TRUNCATE;
444 }
445 break;
446 case T_UTIME:
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);
455 if (retval > 0) {
456 header.size = retval;
457 header.type = T_UTIME;
458 }
459 break;
460 case T_RENAME:
461 oldpath = va_arg(ap, V9fsString *);
462 path = va_arg(ap, V9fsString *);
463 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", oldpath, path);
464 if (retval > 0) {
465 header.size = retval;
466 header.type = T_RENAME;
467 }
468 break;
469 case T_REMOVE:
470 path = va_arg(ap, V9fsString *);
471 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
472 if (retval > 0) {
473 header.size = retval;
474 header.type = T_REMOVE;
475 }
476 break;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530477 case T_LGETXATTR:
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);
483 if (retval > 0) {
484 header.size = retval;
485 header.type = T_LGETXATTR;
486 }
487 break;
488 case T_LLISTXATTR:
489 size = va_arg(ap, int);
490 path = va_arg(ap, V9fsString *);
491 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ds", size, path);
492 if (retval > 0) {
493 header.size = retval;
494 header.type = T_LLISTXATTR;
495 }
496 break;
497 case T_LSETXATTR:
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);
505 if (retval > 0) {
506 header.size = retval;
507 header.type = T_LSETXATTR;
508 }
509 break;
510 case T_LREMOVEXATTR:
511 path = va_arg(ap, V9fsString *);
512 name = va_arg(ap, V9fsString *);
513 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", path, name);
514 if (retval > 0) {
515 header.size = retval;
516 header.type = T_LREMOVEXATTR;
517 }
518 break;
M. Mohan Kumard090e452011-12-14 13:58:46 +0530519 case T_GETVERSION:
520 path = va_arg(ap, V9fsString *);
521 retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
522 if (retval > 0) {
523 header.size = retval;
524 header.type = T_GETVERSION;
525 }
526 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530527 default:
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100528 error_report("Invalid type %d", type);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530529 retval = -EINVAL;
530 break;
531 }
532 va_end(ap);
533
534 if (retval < 0) {
535 goto err_out;
536 }
537
538 /* marshal the header details */
539 proxy_marshal(iovec, 0, "dd", header.type, header.size);
540 header.size += PROXY_HDR_SZ;
541
542 retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
543 if (retval != header.size) {
544 goto close_error;
545 }
546
547 switch (type) {
548 case T_OPEN:
549 case T_CREATE:
550 /*
551 * A file descriptor is returned as response for
552 * T_OPEN,T_CREATE on success
553 */
554 if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
555 goto close_error;
556 }
557 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530558 case T_MKNOD:
559 case T_MKDIR:
560 case T_SYMLINK:
561 case T_LINK:
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530562 case T_CHMOD:
563 case T_CHOWN:
564 case T_RENAME:
565 case T_TRUNCATE:
566 case T_UTIME:
567 case T_REMOVE:
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530568 case T_LSETXATTR:
569 case T_LREMOVEXATTR:
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530570 if (v9fs_receive_status(proxy, reply, &retval) < 0) {
571 goto close_error;
572 }
573 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530574 case T_LSTAT:
575 case T_READLINK:
576 case T_STATFS:
M. Mohan Kumard090e452011-12-14 13:58:46 +0530577 case T_GETVERSION:
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530578 if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
579 goto close_error;
580 }
581 break;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530582 case T_LGETXATTR:
583 case T_LLISTXATTR:
584 if (!size) {
585 if (v9fs_receive_status(proxy, reply, &retval) < 0) {
586 goto close_error;
587 }
588 } else {
589 if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
590 goto close_error;
591 }
592 }
593 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530594 }
595
596err_out:
597 qemu_mutex_unlock(&proxy->mutex);
598 return retval;
599
600close_error:
601 close(proxy->sockfd);
602 proxy->sockfd = -1;
603 qemu_mutex_unlock(&proxy->mutex);
604 return -EIO;
605}
606
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530607static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
608{
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530609 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200610 retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, fs_path);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530611 if (retval < 0) {
612 errno = -retval;
613 return -1;
614 }
615 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530616}
617
618static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
619 char *buf, size_t bufsz)
620{
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530621 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200622 retval = v9fs_request(fs_ctx->private, T_READLINK, buf, fs_path, bufsz);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530623 if (retval < 0) {
624 errno = -retval;
625 return -1;
626 }
627 return strlen(buf);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530628}
629
630static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
631{
632 return close(fs->fd);
633}
634
635static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
636{
Greg Kurzf314ea42016-06-06 11:52:34 +0200637 return closedir(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530638}
639
640static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
641 int flags, V9fsFidOpenState *fs)
642{
Greg Kurz799fe082016-09-16 08:56:14 +0200643 fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, flags);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530644 if (fs->fd < 0) {
645 errno = -fs->fd;
646 fs->fd = -1;
647 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530648 return fs->fd;
649}
650
651static int proxy_opendir(FsContext *ctx,
652 V9fsPath *fs_path, V9fsFidOpenState *fs)
653{
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530654 int serrno, fd;
655
Greg Kurzf314ea42016-06-06 11:52:34 +0200656 fs->dir.stream = NULL;
Greg Kurz799fe082016-09-16 08:56:14 +0200657 fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, O_DIRECTORY);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530658 if (fd < 0) {
659 errno = -fd;
660 return -1;
661 }
Greg Kurzf314ea42016-06-06 11:52:34 +0200662 fs->dir.stream = fdopendir(fd);
663 if (!fs->dir.stream) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530664 serrno = errno;
665 close(fd);
666 errno = serrno;
667 return -1;
668 }
669 return 0;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530670}
671
672static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
673{
Greg Kurzf314ea42016-06-06 11:52:34 +0200674 rewinddir(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530675}
676
677static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
678{
Greg Kurzf314ea42016-06-06 11:52:34 +0200679 return telldir(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530680}
681
Greg Kurz635324e2016-06-06 11:52:34 +0200682static struct dirent *proxy_readdir(FsContext *ctx, V9fsFidOpenState *fs)
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530683{
Greg Kurz635324e2016-06-06 11:52:34 +0200684 return readdir(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530685}
686
687static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
688{
Greg Kurzf314ea42016-06-06 11:52:34 +0200689 seekdir(fs->dir.stream, off);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530690}
691
692static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
693 const struct iovec *iov,
694 int iovcnt, off_t offset)
695{
Michael Tokarev7752efc2015-03-12 09:36:12 +0300696 ssize_t ret;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530697#ifdef CONFIG_PREADV
Michael Tokarev7752efc2015-03-12 09:36:12 +0300698 ret = preadv(fs->fd, iov, iovcnt, offset);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530699#else
Michael Tokarev7752efc2015-03-12 09:36:12 +0300700 ret = lseek(fs->fd, offset, SEEK_SET);
701 if (ret >= 0) {
702 ret = readv(fs->fd, iov, iovcnt);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530703 }
704#endif
Michael Tokarev7752efc2015-03-12 09:36:12 +0300705 return ret;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530706}
707
708static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
709 const struct iovec *iov,
710 int iovcnt, off_t offset)
711{
712 ssize_t ret;
713
714#ifdef CONFIG_PREADV
715 ret = pwritev(fs->fd, iov, iovcnt, offset);
716#else
Michael Tokarev7752efc2015-03-12 09:36:12 +0300717 ret = lseek(fs->fd, offset, SEEK_SET);
718 if (ret >= 0) {
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530719 ret = writev(fs->fd, iov, iovcnt);
720 }
721#endif
722#ifdef CONFIG_SYNC_FILE_RANGE
723 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
724 /*
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.
728 */
729 sync_file_range(fs->fd, offset, ret,
730 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
731 }
732#endif
733 return ret;
734}
735
736static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
737{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530738 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200739 retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, fs_path,
740 credp->fc_mode);
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530741 if (retval < 0) {
742 errno = -retval;
743 }
744 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530745}
746
747static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
748 const char *name, FsCred *credp)
749{
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530750 int retval;
751 V9fsString fullname;
752
753 v9fs_string_init(&fullname);
754 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
755
Greg Kurz799fe082016-09-16 08:56:14 +0200756 retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, &fullname,
757 credp->fc_mode, credp->fc_rdev,
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530758 credp->fc_uid, credp->fc_gid);
759 v9fs_string_free(&fullname);
760 if (retval < 0) {
761 errno = -retval;
762 retval = -1;
763 }
764 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530765}
766
767static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
768 const char *name, FsCred *credp)
769{
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530770 int retval;
771 V9fsString fullname;
772
773 v9fs_string_init(&fullname);
774 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
775
Greg Kurz799fe082016-09-16 08:56:14 +0200776 retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, &fullname,
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530777 credp->fc_mode, credp->fc_uid, credp->fc_gid);
778 v9fs_string_free(&fullname);
779 if (retval < 0) {
780 errno = -retval;
781 retval = -1;
782 }
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530783 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530784}
785
786static int proxy_fstat(FsContext *fs_ctx, int fid_type,
787 V9fsFidOpenState *fs, struct stat *stbuf)
788{
789 int fd;
790
791 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200792 fd = dirfd(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530793 } else {
794 fd = fs->fd;
795 }
796 return fstat(fd, stbuf);
797}
798
799static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
800 int flags, FsCred *credp, V9fsFidOpenState *fs)
801{
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530802 V9fsString fullname;
803
804 v9fs_string_init(&fullname);
805 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
806
Greg Kurz799fe082016-09-16 08:56:14 +0200807 fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, &fullname, flags,
808 credp->fc_mode, credp->fc_uid, credp->fc_gid);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530809 v9fs_string_free(&fullname);
810 if (fs->fd < 0) {
811 errno = -fs->fd;
812 fs->fd = -1;
813 }
814 return fs->fd;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530815}
816
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530817static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
818 V9fsPath *dir_path, const char *name, FsCred *credp)
819{
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530820 int retval;
821 V9fsString fullname, target;
822
823 v9fs_string_init(&fullname);
824 v9fs_string_init(&target);
825
826 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
827 v9fs_string_sprintf(&target, "%s", oldpath);
828
Greg Kurz799fe082016-09-16 08:56:14 +0200829 retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, &target, &fullname,
830 credp->fc_uid, credp->fc_gid);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530831 v9fs_string_free(&fullname);
832 v9fs_string_free(&target);
833 if (retval < 0) {
834 errno = -retval;
835 retval = -1;
836 }
837 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530838}
839
840static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
841 V9fsPath *dirpath, const char *name)
842{
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530843 int retval;
844 V9fsString newpath;
845
846 v9fs_string_init(&newpath);
847 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
848
Greg Kurz799fe082016-09-16 08:56:14 +0200849 retval = v9fs_request(ctx->private, T_LINK, NULL, oldpath, &newpath);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530850 v9fs_string_free(&newpath);
851 if (retval < 0) {
852 errno = -retval;
853 retval = -1;
854 }
855 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530856}
857
858static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
859{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530860 int retval;
861
Greg Kurz799fe082016-09-16 08:56:14 +0200862 retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, fs_path, size);
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530863 if (retval < 0) {
864 errno = -retval;
865 return -1;
866 }
867 return 0;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530868}
869
870static int proxy_rename(FsContext *ctx, const char *oldpath,
871 const char *newpath)
872{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530873 int retval;
874 V9fsString oldname, newname;
875
876 v9fs_string_init(&oldname);
877 v9fs_string_init(&newname);
878
879 v9fs_string_sprintf(&oldname, "%s", oldpath);
880 v9fs_string_sprintf(&newname, "%s", newpath);
Greg Kurz799fe082016-09-16 08:56:14 +0200881 retval = v9fs_request(ctx->private, T_RENAME, NULL, &oldname, &newname);
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530882 v9fs_string_free(&oldname);
883 v9fs_string_free(&newname);
884 if (retval < 0) {
885 errno = -retval;
886 }
887 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530888}
889
890static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
891{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530892 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200893 retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, fs_path,
894 credp->fc_uid, credp->fc_gid);
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530895 if (retval < 0) {
896 errno = -retval;
897 }
898 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530899}
900
901static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
902 const struct timespec *buf)
903{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530904 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200905 retval = v9fs_request(s->private, T_UTIME, NULL, fs_path,
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530906 buf[0].tv_sec, buf[0].tv_nsec,
907 buf[1].tv_sec, buf[1].tv_nsec);
908 if (retval < 0) {
909 errno = -retval;
910 }
911 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530912}
913
914static int proxy_remove(FsContext *ctx, const char *path)
915{
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530916 int retval;
917 V9fsString name;
918 v9fs_string_init(&name);
919 v9fs_string_sprintf(&name, "%s", path);
Greg Kurz799fe082016-09-16 08:56:14 +0200920 retval = v9fs_request(ctx->private, T_REMOVE, NULL, &name);
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530921 v9fs_string_free(&name);
922 if (retval < 0) {
923 errno = -retval;
924 }
925 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530926}
927
928static int proxy_fsync(FsContext *ctx, int fid_type,
929 V9fsFidOpenState *fs, int datasync)
930{
931 int fd;
932
933 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200934 fd = dirfd(fs->dir.stream);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530935 } else {
936 fd = fs->fd;
937 }
938
939 if (datasync) {
940 return qemu_fdatasync(fd);
941 } else {
942 return fsync(fd);
943 }
944}
945
946static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
947{
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530948 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200949 retval = v9fs_request(s->private, T_STATFS, stbuf, fs_path);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530950 if (retval < 0) {
951 errno = -retval;
952 return -1;
953 }
954 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530955}
956
957static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
958 const char *name, void *value, size_t size)
959{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530960 int retval;
961 V9fsString xname;
962
963 v9fs_string_init(&xname);
964 v9fs_string_sprintf(&xname, "%s", name);
Greg Kurz799fe082016-09-16 08:56:14 +0200965 retval = v9fs_request(ctx->private, T_LGETXATTR, value, size, fs_path,
966 &xname);
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530967 v9fs_string_free(&xname);
968 if (retval < 0) {
969 errno = -retval;
970 }
971 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530972}
973
974static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
975 void *value, size_t size)
976{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530977 int retval;
Greg Kurz799fe082016-09-16 08:56:14 +0200978 retval = v9fs_request(ctx->private, T_LLISTXATTR, value, size, fs_path);
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530979 if (retval < 0) {
980 errno = -retval;
981 }
982 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +0530983}
984
985static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
986 void *value, size_t size, int flags)
987{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530988 int retval;
989 V9fsString xname, xvalue;
990
991 v9fs_string_init(&xname);
992 v9fs_string_sprintf(&xname, "%s", name);
993
994 v9fs_string_init(&xvalue);
995 xvalue.size = size;
996 xvalue.data = g_malloc(size);
997 memcpy(xvalue.data, value, size);
998
Greg Kurz799fe082016-09-16 08:56:14 +0200999 retval = v9fs_request(ctx->private, T_LSETXATTR, value, fs_path, &xname,
1000 &xvalue, size, flags);
M. Mohan Kumard52b09e2011-12-14 13:58:46 +05301001 v9fs_string_free(&xname);
1002 v9fs_string_free(&xvalue);
1003 if (retval < 0) {
1004 errno = -retval;
1005 }
1006 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301007}
1008
1009static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1010 const char *name)
1011{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +05301012 int retval;
1013 V9fsString xname;
1014
1015 v9fs_string_init(&xname);
1016 v9fs_string_sprintf(&xname, "%s", name);
Greg Kurz799fe082016-09-16 08:56:14 +02001017 retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, fs_path, &xname);
M. Mohan Kumard52b09e2011-12-14 13:58:46 +05301018 v9fs_string_free(&xname);
1019 if (retval < 0) {
1020 errno = -retval;
1021 }
1022 return retval;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301023}
1024
1025static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1026 const char *name, V9fsPath *target)
1027{
1028 if (dir_path) {
Greg Kurze3e83f22016-09-16 08:56:15 +02001029 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301030 } else {
Greg Kurze3e83f22016-09-16 08:56:15 +02001031 v9fs_path_sprintf(target, "%s", name);
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301032 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301033 return 0;
1034}
1035
1036static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
1037 const char *old_name, V9fsPath *newdir,
1038 const char *new_name)
1039{
1040 int ret;
1041 V9fsString old_full_name, new_full_name;
1042
1043 v9fs_string_init(&old_full_name);
1044 v9fs_string_init(&new_full_name);
1045
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);
1048
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);
1052 return ret;
1053}
1054
1055static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
1056 const char *name, int flags)
1057{
1058 int ret;
1059 V9fsString fullname;
1060 v9fs_string_init(&fullname);
1061
1062 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1063 ret = proxy_remove(ctx, fullname.data);
1064 v9fs_string_free(&fullname);
1065
1066 return ret;
1067}
1068
M. Mohan Kumard090e452011-12-14 13:58:46 +05301069static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
1070 mode_t st_mode, uint64_t *st_gen)
1071{
1072 int err;
1073
1074 /* Do not try to open special files like device nodes, fifos etc
1075 * we can get fd for regular files and directories only
1076 */
1077 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
Kirill A. Shutemov1a9978a2014-01-28 17:08:26 +02001078 errno = ENOTTY;
1079 return -1;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301080 }
Greg Kurz799fe082016-09-16 08:56:14 +02001081 err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, path);
M. Mohan Kumard090e452011-12-14 13:58:46 +05301082 if (err < 0) {
1083 errno = -err;
1084 err = -1;
1085 }
1086 return err;
1087}
1088
Greg Kurz65603a82018-01-08 11:18:23 +01001089static int connect_namedsocket(const char *path, Error **errp)
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301090{
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301091 int sockfd, size;
1092 struct sockaddr_un helper;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301093
Shannon Zhao25ee9a72015-03-14 10:00:16 +08001094 if (strlen(path) >= sizeof(helper.sun_path)) {
Greg Kurz65603a82018-01-08 11:18:23 +01001095 error_setg(errp, "socket name too long");
Shannon Zhao25ee9a72015-03-14 10:00:16 +08001096 return -1;
1097 }
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301098 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1099 if (sockfd < 0) {
Greg Kurz65603a82018-01-08 11:18:23 +01001100 error_setg_errno(errp, errno, "failed to create client socket");
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301101 return -1;
1102 }
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301103 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) {
Greg Kurz65603a82018-01-08 11:18:23 +01001107 error_setg_errno(errp, errno, "failed to connect to '%s'", path);
Michael Tokarev660edd42014-10-30 10:53:02 +03001108 close(sockfd);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301109 return -1;
1110 }
1111
1112 /* remove the socket for security reasons */
1113 unlink(path);
1114 return sockfd;
1115}
1116
Greg Kurz91cda4e2018-01-08 11:18:23 +01001117static void error_append_socket_sockfd_hint(Error **errp)
1118{
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"
1122 " socket\n");
1123}
1124
1125static int proxy_parse_opts(QemuOpts *opts, FsDriverEntry *fs, Error **errp)
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301126{
1127 const char *socket = qemu_opt_get(opts, "socket");
1128 const char *sock_fd = qemu_opt_get(opts, "sock_fd");
1129
1130 if (!socket && !sock_fd) {
Greg Kurz91cda4e2018-01-08 11:18:23 +01001131 error_setg(errp, "both socket and sock_fd properties are missing");
1132 error_append_socket_sockfd_hint(errp);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301133 return -1;
1134 }
1135 if (socket && sock_fd) {
Greg Kurz91cda4e2018-01-08 11:18:23 +01001136 error_setg(errp, "both socket and sock_fd properties are set");
1137 error_append_socket_sockfd_hint(errp);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301138 return -1;
1139 }
1140 if (socket) {
1141 fs->path = g_strdup(socket);
1142 fs->export_flags = V9FS_PROXY_SOCK_NAME;
1143 } else {
1144 fs->path = g_strdup(sock_fd);
1145 fs->export_flags = V9FS_PROXY_SOCK_FD;
1146 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301147 return 0;
1148}
1149
Greg Kurz65603a82018-01-08 11:18:23 +01001150static int proxy_init(FsContext *ctx, Error **errp)
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301151{
1152 V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy));
1153 int sock_id;
1154
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301155 if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
Greg Kurz65603a82018-01-08 11:18:23 +01001156 sock_id = connect_namedsocket(ctx->fs_root, errp);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301157 } else {
1158 sock_id = atoi(ctx->fs_root);
1159 if (sock_id < 0) {
Greg Kurz65603a82018-01-08 11:18:23 +01001160 error_setg(errp, "socket descriptor not initialized");
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301161 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301162 }
Michael Tokarev6af76c62014-10-30 11:00:01 +03001163 if (sock_id < 0) {
1164 g_free(proxy);
1165 return -1;
1166 }
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301167 g_free(ctx->fs_root);
Stefan Weil80cba1b2013-06-16 12:14:36 +02001168 ctx->fs_root = NULL;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301169
M. Mohan Kumar39f8c322011-12-14 13:58:45 +05301170 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;
1174
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301175 ctx->private = proxy;
1176 proxy->sockfd = sock_id;
1177 qemu_mutex_init(&proxy->mutex);
1178
1179 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301180 ctx->exops.get_st_gen = proxy_ioc_getversion;
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301181 return 0;
1182}
1183
Li Qiang898ae902016-11-23 13:53:34 +01001184static void proxy_cleanup(FsContext *ctx)
1185{
1186 V9fsProxy *proxy = ctx->private;
1187
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);
1192 }
1193 g_free(proxy);
1194}
1195
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301196FileOperations proxy_ops = {
1197 .parse_opts = proxy_parse_opts,
1198 .init = proxy_init,
Li Qiang898ae902016-11-23 13:53:34 +01001199 .cleanup = proxy_cleanup,
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301200 .lstat = proxy_lstat,
1201 .readlink = proxy_readlink,
1202 .close = proxy_close,
1203 .closedir = proxy_closedir,
1204 .open = proxy_open,
1205 .opendir = proxy_opendir,
1206 .rewinddir = proxy_rewinddir,
1207 .telldir = proxy_telldir,
Greg Kurz635324e2016-06-06 11:52:34 +02001208 .readdir = proxy_readdir,
M. Mohan Kumar4c793dd2011-12-14 13:49:28 +05301209 .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,
1218 .link = proxy_link,
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,
1233};