]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/9p.c
9pfs: Improve unreclaim loop
[mirror_qemu.git] / hw / 9pfs / 9p.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.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 *
12 */
13
fbc04127 14#include "qemu/osdep.h"
e3e83f2e 15#include <glib/gprintf.h>
0d09e41a 16#include "hw/virtio/virtio.h"
da34e65c 17#include "qapi/error.h"
d49b6836 18#include "qemu/error-report.h"
cd4bfbb2 19#include "qemu/iov.h"
db725815 20#include "qemu/main-loop.h"
1de7afc9 21#include "qemu/sockets.h"
9f107513
AL
22#include "virtio-9p.h"
23#include "fsdev/qemu-fsdev.h"
267ae092 24#include "9p-xattr.h"
fe52840c 25#include "coth.h"
c572f23a 26#include "trace.h"
795c40b8 27#include "migration/blocker.h"
357e2f7f 28#include "sysemu/qtest.h"
1a6ed33c 29#include "qemu/xxhash.h"
6b6aa828 30#include <math.h>
03556ea9 31#include <linux/limits.h>
9f107513 32
7a462745
AK
33int open_fd_hw;
34int total_open_fd;
35static int open_fd_rc;
9f107513 36
fac4f111
VJ
37enum {
38 Oread = 0x00,
39 Owrite = 0x01,
40 Ordwr = 0x02,
41 Oexec = 0x03,
42 Oexcl = 0x04,
43 Otrunc = 0x10,
44 Orexec = 0x20,
45 Orclose = 0x40,
46 Oappend = 0x80,
47};
48
75673590 49static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
0e2082d9
WL
50{
51 ssize_t ret;
52 va_list ap;
53
54 va_start(ap, fmt);
ea83441c 55 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
0e2082d9
WL
56 va_end(ap);
57
58 return ret;
59}
60
75673590 61static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
0e2082d9
WL
62{
63 ssize_t ret;
64 va_list ap;
65
66 va_start(ap, fmt);
ea83441c 67 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
0e2082d9
WL
68 va_end(ap);
69
70 return ret;
71}
72
fac4f111
VJ
73static int omode_to_uflags(int8_t mode)
74{
75 int ret = 0;
76
77 switch (mode & 3) {
78 case Oread:
79 ret = O_RDONLY;
80 break;
81 case Ordwr:
82 ret = O_RDWR;
83 break;
84 case Owrite:
85 ret = O_WRONLY;
86 break;
87 case Oexec:
88 ret = O_RDONLY;
89 break;
90 }
91
92 if (mode & Otrunc) {
93 ret |= O_TRUNC;
94 }
95
96 if (mode & Oappend) {
97 ret |= O_APPEND;
98 }
99
100 if (mode & Oexcl) {
101 ret |= O_EXCL;
102 }
103
104 return ret;
105}
106
8e71b96c 107typedef struct DotlOpenflagMap {
9844081b
MK
108 int dotl_flag;
109 int open_flag;
8e71b96c 110} DotlOpenflagMap;
9844081b
MK
111
112static int dotl_to_open_flags(int flags)
113{
114 int i;
115 /*
116 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
117 * and P9_DOTL_NOACCESS
118 */
119 int oflags = flags & O_ACCMODE;
120
8e71b96c 121 DotlOpenflagMap dotl_oflag_map[] = {
9844081b
MK
122 { P9_DOTL_CREATE, O_CREAT },
123 { P9_DOTL_EXCL, O_EXCL },
124 { P9_DOTL_NOCTTY , O_NOCTTY },
125 { P9_DOTL_TRUNC, O_TRUNC },
126 { P9_DOTL_APPEND, O_APPEND },
127 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
128 { P9_DOTL_DSYNC, O_DSYNC },
129 { P9_DOTL_FASYNC, FASYNC },
130 { P9_DOTL_DIRECT, O_DIRECT },
131 { P9_DOTL_LARGEFILE, O_LARGEFILE },
132 { P9_DOTL_DIRECTORY, O_DIRECTORY },
133 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
134 { P9_DOTL_NOATIME, O_NOATIME },
135 { P9_DOTL_SYNC, O_SYNC },
136 };
137
138 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
139 if (flags & dotl_oflag_map[i].dotl_flag) {
140 oflags |= dotl_oflag_map[i].open_flag;
141 }
142 }
143
144 return oflags;
145}
146
758e8e38 147void cred_init(FsCred *credp)
131dcb25 148{
758e8e38
VJ
149 credp->fc_uid = -1;
150 credp->fc_gid = -1;
151 credp->fc_mode = -1;
152 credp->fc_rdev = -1;
131dcb25
AL
153}
154
d3ab98e6
AK
155static int get_dotl_openflags(V9fsState *s, int oflags)
156{
157 int flags;
158 /*
159 * Filter the client open flags
160 */
9844081b
MK
161 flags = dotl_to_open_flags(oflags);
162 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
d3ab98e6
AK
163 /*
164 * Ignore direct disk access hint until the server supports it.
165 */
166 flags &= ~O_DIRECT;
167 return flags;
168}
169
2289be19
AK
170void v9fs_path_init(V9fsPath *path)
171{
172 path->data = NULL;
173 path->size = 0;
174}
175
176void v9fs_path_free(V9fsPath *path)
177{
178 g_free(path->data);
179 path->data = NULL;
180 path->size = 0;
181}
182
e3e83f2e
GK
183
184void GCC_FMT_ATTR(2, 3)
185v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
186{
187 va_list ap;
188
189 v9fs_path_free(path);
190
191 va_start(ap, fmt);
192 /* Bump the size for including terminating NULL */
193 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
194 va_end(ap);
195}
196
e446a1eb 197void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
2289be19 198{
e446a1eb
MAL
199 v9fs_path_free(dst);
200 dst->size = src->size;
201 dst->data = g_memdup(src->data, src->size);
2289be19
AK
202}
203
204int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
205 const char *name, V9fsPath *path)
206{
207 int err;
208 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
209 if (err < 0) {
210 err = -errno;
211 }
212 return err;
213}
214
936532a4
MN
215/*
216 * Return TRUE if s1 is an ancestor of s2.
217 *
218 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
219 * As a special case, We treat s1 as ancestor of s2 if they are same!
220 */
2289be19 221static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
936532a4 222{
2289be19
AK
223 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
224 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
936532a4
MN
225 return 1;
226 }
227 }
228 return 0;
229}
230
a03f7874
AL
231static size_t v9fs_string_size(V9fsString *str)
232{
233 return str->size;
234}
235
b9cb88b0
AK
236/*
237 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
8440e22e 238static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
b9cb88b0
AK
239{
240 int err = 1;
241 if (f->fid_type == P9_FID_FILE) {
242 if (f->fs.fd == -1) {
243 do {
bccacf6c
AK
244 err = v9fs_co_open(pdu, f, f->open_flags);
245 } while (err == -EINTR && !pdu->cancelled);
b9cb88b0
AK
246 }
247 } else if (f->fid_type == P9_FID_DIR) {
f314ea4e 248 if (f->fs.dir.stream == NULL) {
b9cb88b0 249 do {
bccacf6c
AK
250 err = v9fs_co_opendir(pdu, f);
251 } while (err == -EINTR && !pdu->cancelled);
b9cb88b0
AK
252 }
253 }
254 return err;
255}
256
8440e22e 257static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
286d5652 258{
7a462745 259 int err;
286d5652 260 V9fsFidState *f;
bccacf6c 261 V9fsState *s = pdu->s;
286d5652 262
feabd6cf 263 QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
84dfb926 264 BUG_ON(f->clunked);
286d5652 265 if (f->fid == fid) {
7a462745
AK
266 /*
267 * Update the fid ref upfront so that
268 * we don't get reclaimed when we yield
269 * in open later.
270 */
84dfb926 271 f->ref++;
7a462745
AK
272 /*
273 * check whether we need to reopen the
274 * file. We might have closed the fd
275 * while trying to free up some file
276 * descriptors.
277 */
bccacf6c 278 err = v9fs_reopen_fid(pdu, f);
b9cb88b0
AK
279 if (err < 0) {
280 f->ref--;
281 return NULL;
282 }
7a462745
AK
283 /*
284 * Mark the fid as referenced so that the LRU
285 * reclaim won't close the file descriptor
286 */
287 f->flags |= FID_REFERENCED;
286d5652
AL
288 return f;
289 }
290 }
286d5652
AL
291 return NULL;
292}
293
294static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
295{
296 V9fsFidState *f;
297
feabd6cf 298 QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
84dfb926
AK
299 /* If fid is already there return NULL */
300 BUG_ON(f->clunked);
301 if (f->fid == fid) {
302 return NULL;
303 }
286d5652 304 }
7267c094 305 f = g_malloc0(sizeof(V9fsFidState));
286d5652 306 f->fid = fid;
d62dbb51 307 f->fid_type = P9_FID_NONE;
84dfb926 308 f->ref = 1;
7a462745
AK
309 /*
310 * Mark the fid as referenced so that the LRU
311 * reclaim won't close the file descriptor
312 */
313 f->flags |= FID_REFERENCED;
20b7f45b 314 QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next);
286d5652 315
d2c5cf7c
CS
316 v9fs_readdir_init(s->proto_version, &f->fs.dir);
317 v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir);
7cde47d4 318
286d5652
AL
319 return f;
320}
321
8440e22e 322static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
10b468bd
AK
323{
324 int retval = 0;
325
dd28fbbc 326 if (fidp->fs.xattr.xattrwalk_fid) {
10b468bd
AK
327 /* getxattr/listxattr fid */
328 goto free_value;
329 }
330 /*
331 * if this is fid for setxattr. clunk should
332 * result in setxattr localcall
333 */
334 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
335 /* clunk after partial write */
336 retval = -EINVAL;
337 goto free_out;
338 }
9ed3ef26 339 if (fidp->fs.xattr.len) {
bccacf6c 340 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
9ed3ef26
AK
341 fidp->fs.xattr.value,
342 fidp->fs.xattr.len,
343 fidp->fs.xattr.flags);
344 } else {
bccacf6c 345 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
9ed3ef26 346 }
10b468bd
AK
347free_out:
348 v9fs_string_free(&fidp->fs.xattr.name);
349free_value:
9e288406 350 g_free(fidp->fs.xattr.value);
10b468bd
AK
351 return retval;
352}
353
8440e22e 354static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
286d5652 355{
10b468bd 356 int retval = 0;
84dfb926
AK
357
358 if (fidp->fid_type == P9_FID_FILE) {
7a462745
AK
359 /* If we reclaimed the fd no need to close */
360 if (fidp->fs.fd != -1) {
cc720ddb 361 retval = v9fs_co_close(pdu, &fidp->fs);
7a462745 362 }
84dfb926 363 } else if (fidp->fid_type == P9_FID_DIR) {
f314ea4e 364 if (fidp->fs.dir.stream != NULL) {
cc720ddb 365 retval = v9fs_co_closedir(pdu, &fidp->fs);
95f65511 366 }
84dfb926 367 } else if (fidp->fid_type == P9_FID_XATTR) {
bccacf6c 368 retval = v9fs_xattr_fid_clunk(pdu, fidp);
84dfb926 369 }
2289be19 370 v9fs_path_free(&fidp->path);
84dfb926
AK
371 g_free(fidp);
372 return retval;
373}
374
8440e22e 375static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
84dfb926
AK
376{
377 BUG_ON(!fidp->ref);
378 fidp->ref--;
7a462745
AK
379 /*
380 * Don't free the fid if it is in reclaim list
381 */
84dfb926 382 if (!fidp->ref && fidp->clunked) {
e9a0152b
AK
383 if (fidp->fid == pdu->s->root_fid) {
384 /*
385 * if the clunked fid is root fid then we
386 * have unmounted the fs on the client side.
387 * delete the migration blocker. Ideally, this
388 * should be hooked to transport close notification
389 */
390 if (pdu->s->migration_blocker) {
391 migrate_del_blocker(pdu->s->migration_blocker);
392 error_free(pdu->s->migration_blocker);
393 pdu->s->migration_blocker = NULL;
394 }
395 }
a911a182 396 return free_fid(pdu, fidp);
84dfb926 397 }
a911a182 398 return 0;
84dfb926
AK
399}
400
ce421a19 401static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
84dfb926 402{
feabd6cf 403 V9fsFidState *fidp;
286d5652 404
feabd6cf
GK
405 QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) {
406 if (fidp->fid == fid) {
407 QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next);
408 fidp->clunked = true;
409 return fidp;
286d5652
AL
410 }
411 }
feabd6cf 412 return NULL;
286d5652
AL
413}
414
8440e22e 415void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
7a462745
AK
416{
417 int reclaim_count = 0;
bccacf6c 418 V9fsState *s = pdu->s;
7a462745
AK
419 V9fsFidState *f, *reclaim_list = NULL;
420
feabd6cf 421 QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
7a462745
AK
422 /*
423 * Unlink fids cannot be reclaimed. Check
424 * for them and skip them. Also skip fids
425 * currently being operated on.
426 */
427 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
428 continue;
429 }
430 /*
431 * if it is a recently referenced fid
432 * we leave the fid untouched and clear the
433 * reference bit. We come back to it later
434 * in the next iteration. (a simple LRU without
435 * moving list elements around)
436 */
437 if (f->flags & FID_REFERENCED) {
438 f->flags &= ~FID_REFERENCED;
439 continue;
440 }
441 /*
442 * Add fids to reclaim list.
443 */
444 if (f->fid_type == P9_FID_FILE) {
445 if (f->fs.fd != -1) {
446 /*
447 * Up the reference count so that
448 * a clunk request won't free this fid
449 */
450 f->ref++;
451 f->rclm_lst = reclaim_list;
452 reclaim_list = f;
453 f->fs_reclaim.fd = f->fs.fd;
454 f->fs.fd = -1;
455 reclaim_count++;
456 }
95f65511 457 } else if (f->fid_type == P9_FID_DIR) {
f314ea4e 458 if (f->fs.dir.stream != NULL) {
95f65511
AK
459 /*
460 * Up the reference count so that
461 * a clunk request won't free this fid
462 */
463 f->ref++;
464 f->rclm_lst = reclaim_list;
465 reclaim_list = f;
f314ea4e
GK
466 f->fs_reclaim.dir.stream = f->fs.dir.stream;
467 f->fs.dir.stream = NULL;
95f65511
AK
468 reclaim_count++;
469 }
7a462745
AK
470 }
471 if (reclaim_count >= open_fd_rc) {
472 break;
473 }
474 }
475 /*
476 * Now close the fid in reclaim list. Free them if they
477 * are already clunked.
478 */
479 while (reclaim_list) {
480 f = reclaim_list;
481 reclaim_list = f->rclm_lst;
482 if (f->fid_type == P9_FID_FILE) {
cc720ddb 483 v9fs_co_close(pdu, &f->fs_reclaim);
95f65511 484 } else if (f->fid_type == P9_FID_DIR) {
cc720ddb 485 v9fs_co_closedir(pdu, &f->fs_reclaim);
7a462745
AK
486 }
487 f->rclm_lst = NULL;
488 /*
489 * Now drop the fid reference, free it
490 * if clunked.
491 */
bccacf6c 492 put_fid(pdu, f);
7a462745
AK
493 }
494}
495
8440e22e 496static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
7a462745
AK
497{
498 int err;
bccacf6c 499 V9fsState *s = pdu->s;
20b7f45b 500 V9fsFidState *fidp, *fidp_next;
7a462745 501
20b7f45b
GK
502 fidp = QSIMPLEQ_FIRST(&s->fid_list);
503 if (!fidp) {
504 return 0;
505 }
506
507 /*
508 * v9fs_reopen_fid() can yield : a reference on the fid must be held
509 * to ensure its pointer remains valid and we can safely pass it to
510 * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so
511 * we must keep a reference on the next fid as well. So the logic here
512 * is to get a reference on a fid and only put it back during the next
513 * iteration after we could get a reference on the next fid. Start with
514 * the first one.
515 */
516 for (fidp->ref++; fidp; fidp = fidp_next) {
517 if (fidp->path.size == path->size &&
518 !memcmp(fidp->path.data, path->data, path->size)) {
7a462745
AK
519 /* Mark the fid non reclaimable. */
520 fidp->flags |= FID_NON_RECLAIMABLE;
b9cb88b0
AK
521
522 /* reopen the file/dir if already closed */
bccacf6c 523 err = v9fs_reopen_fid(pdu, fidp);
b9cb88b0 524 if (err < 0) {
20b7f45b 525 put_fid(pdu, fidp);
267fcadf 526 return err;
b9cb88b0 527 }
20b7f45b
GK
528 }
529
530 fidp_next = QSIMPLEQ_NEXT(fidp, next);
531
532 if (fidp_next) {
b9cb88b0 533 /*
20b7f45b
GK
534 * Ensure the next fid survives a potential clunk request during
535 * put_fid() below and v9fs_reopen_fid() in the next iteration.
b9cb88b0 536 */
20b7f45b 537 fidp_next->ref++;
7a462745 538 }
20b7f45b
GK
539
540 /* We're done with this fid */
541 put_fid(pdu, fidp);
7a462745 542 }
20b7f45b 543
7a462745
AK
544 return 0;
545}
546
8440e22e 547static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
b41e2992
DS
548{
549 V9fsState *s = pdu->s;
79decce3 550 V9fsFidState *fidp;
b41e2992
DS
551
552 /* Free all fids */
feabd6cf 553 while (!QSIMPLEQ_EMPTY(&s->fid_list)) {
6d54af0e 554 /* Get fid */
feabd6cf 555 fidp = QSIMPLEQ_FIRST(&s->fid_list);
6d54af0e
GK
556 fidp->ref++;
557
558 /* Clunk fid */
feabd6cf 559 QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next);
2e53160f 560 fidp->clunked = true;
b41e2992 561
6d54af0e 562 put_fid(pdu, fidp);
b41e2992 563 }
b41e2992
DS
564}
565
286d5652
AL
566#define P9_QID_TYPE_DIR 0x80
567#define P9_QID_TYPE_SYMLINK 0x02
568
569#define P9_STAT_MODE_DIR 0x80000000
570#define P9_STAT_MODE_APPEND 0x40000000
571#define P9_STAT_MODE_EXCL 0x20000000
572#define P9_STAT_MODE_MOUNT 0x10000000
573#define P9_STAT_MODE_AUTH 0x08000000
574#define P9_STAT_MODE_TMP 0x04000000
575#define P9_STAT_MODE_SYMLINK 0x02000000
576#define P9_STAT_MODE_LINK 0x01000000
577#define P9_STAT_MODE_DEVICE 0x00800000
578#define P9_STAT_MODE_NAMED_PIPE 0x00200000
579#define P9_STAT_MODE_SOCKET 0x00100000
580#define P9_STAT_MODE_SETUID 0x00080000
581#define P9_STAT_MODE_SETGID 0x00040000
582#define P9_STAT_MODE_SETVTX 0x00010000
583
584#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
585 P9_STAT_MODE_SYMLINK | \
586 P9_STAT_MODE_LINK | \
587 P9_STAT_MODE_DEVICE | \
588 P9_STAT_MODE_NAMED_PIPE | \
589 P9_STAT_MODE_SOCKET)
590
6b6aa828
CS
591/* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
592static inline uint8_t mirror8bit(uint8_t byte)
593{
594 return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
595}
596
597/* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
598static inline uint64_t mirror64bit(uint64_t value)
599{
600 return ((uint64_t)mirror8bit(value & 0xff) << 56) |
601 ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) |
602 ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
603 ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
604 ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
605 ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
606 ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) |
607 ((uint64_t)mirror8bit((value >> 56) & 0xff));
608}
609
610/**
611 * @brief Parameter k for the Exponential Golomb algorihm to be used.
612 *
613 * The smaller this value, the smaller the minimum bit count for the Exp.
614 * Golomb generated affixes will be (at lowest index) however for the
615 * price of having higher maximum bit count of generated affixes (at highest
616 * index). Likewise increasing this parameter yields in smaller maximum bit
617 * count for the price of having higher minimum bit count.
618 *
619 * In practice that means: a good value for k depends on the expected amount
620 * of devices to be exposed by one export. For a small amount of devices k
621 * should be small, for a large amount of devices k might be increased
622 * instead. The default of k=0 should be fine for most users though.
623 *
624 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
625 * k should not change as long as guest is still running! Because that would
626 * cause completely different inode numbers to be generated on guest.
627 */
628#define EXP_GOLOMB_K 0
629
630/**
631 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
632 *
633 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
634 * with growing length and with the mathematical property of being
635 * "prefix-free". The latter means the generated prefixes can be prepended
636 * in front of arbitrary numbers and the resulting concatenated numbers are
637 * guaranteed to be always unique.
638 *
639 * This is a minor adjustment to the original Exp. Golomb algorithm in the
640 * sense that lowest allowed index (@param n) starts with 1, not with zero.
641 *
642 * @param n - natural number (or index) of the prefix to be generated
643 * (1, 2, 3, ...)
644 * @param k - parameter k of Exp. Golomb algorithm to be used
645 * (see comment on EXP_GOLOMB_K macro for details about k)
646 */
647static VariLenAffix expGolombEncode(uint64_t n, int k)
648{
649 const uint64_t value = n + (1 << k) - 1;
650 const int bits = (int) log2(value) + 1;
651 return (VariLenAffix) {
652 .type = AffixType_Prefix,
653 .value = value,
654 .bits = bits + MAX((bits - 1 - k), 0)
655 };
656}
657
658/**
659 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
660 *
661 * Simply mirror all bits of the affix value, for the purpose to preserve
662 * respectively the mathematical "prefix-free" or "suffix-free" property
663 * after the conversion.
664 *
665 * If a passed prefix is suitable to create unique numbers, then the
666 * returned suffix is suitable to create unique numbers as well (and vice
667 * versa).
668 */
669static VariLenAffix invertAffix(const VariLenAffix *affix)
670{
671 return (VariLenAffix) {
672 .type =
673 (affix->type == AffixType_Suffix) ?
674 AffixType_Prefix : AffixType_Suffix,
675 .value =
676 mirror64bit(affix->value) >>
677 ((sizeof(affix->value) * 8) - affix->bits),
678 .bits = affix->bits
679 };
680}
681
682/**
683 * @brief Generates suffix numbers with "suffix-free" property.
684 *
685 * This is just a wrapper function on top of the Exp. Golomb algorithm.
686 *
687 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
688 * this function converts the Exp. Golomb prefixes into appropriate suffixes
689 * which are still suitable for generating unique numbers.
690 *
691 * @param n - natural number (or index) of the suffix to be generated
692 * (1, 2, 3, ...)
693 */
694static VariLenAffix affixForIndex(uint64_t index)
695{
696 VariLenAffix prefix;
697 prefix = expGolombEncode(index, EXP_GOLOMB_K);
698 return invertAffix(&prefix); /* convert prefix to suffix */
699}
700
1a6ed33c
AM
701/* creative abuse of tb_hash_func7, which is based on xxhash */
702static uint32_t qpp_hash(QppEntry e)
703{
704 return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
705}
706
f3fe4a2d
AM
707static uint32_t qpf_hash(QpfEntry e)
708{
709 return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
710}
711
6b6aa828
CS
712static bool qpd_cmp_func(const void *obj, const void *userp)
713{
714 const QpdEntry *e1 = obj, *e2 = userp;
715 return e1->dev == e2->dev;
716}
717
718static bool qpp_cmp_func(const void *obj, const void *userp)
1a6ed33c
AM
719{
720 const QppEntry *e1 = obj, *e2 = userp;
721 return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
722}
723
6b6aa828 724static bool qpf_cmp_func(const void *obj, const void *userp)
f3fe4a2d
AM
725{
726 const QpfEntry *e1 = obj, *e2 = userp;
727 return e1->dev == e2->dev && e1->ino == e2->ino;
728}
729
730static void qp_table_remove(void *p, uint32_t h, void *up)
1a6ed33c
AM
731{
732 g_free(p);
733}
734
f3fe4a2d 735static void qp_table_destroy(struct qht *ht)
1a6ed33c
AM
736{
737 if (!ht || !ht->map) {
738 return;
739 }
f3fe4a2d 740 qht_iter(ht, qp_table_remove, NULL);
1a6ed33c
AM
741 qht_destroy(ht);
742}
743
6b6aa828
CS
744static void qpd_table_init(struct qht *ht)
745{
746 qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
747}
748
1a6ed33c
AM
749static void qpp_table_init(struct qht *ht)
750{
6b6aa828 751 qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
1a6ed33c
AM
752}
753
f3fe4a2d
AM
754static void qpf_table_init(struct qht *ht)
755{
6b6aa828
CS
756 qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
757}
758
759/*
760 * Returns how many (high end) bits of inode numbers of the passed fs
761 * device shall be used (in combination with the device number) to
762 * generate hash values for qpp_table entries.
763 *
764 * This function is required if variable length suffixes are used for inode
765 * number mapping on guest level. Since a device may end up having multiple
766 * entries in qpp_table, each entry most probably with a different suffix
767 * length, we thus need this function in conjunction with qpd_table to
768 * "agree" about a fix amount of bits (per device) to be always used for
769 * generating hash values for the purpose of accessing qpp_table in order
770 * get consistent behaviour when accessing qpp_table.
771 */
772static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
773{
774 QpdEntry lookup = {
775 .dev = dev
776 }, *val;
777 uint32_t hash = dev;
778 VariLenAffix affix;
779
780 val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
781 if (!val) {
782 val = g_malloc0(sizeof(QpdEntry));
783 *val = lookup;
784 affix = affixForIndex(pdu->s->qp_affix_next);
785 val->prefix_bits = affix.bits;
786 qht_insert(&pdu->s->qpd_table, val, hash, NULL);
787 pdu->s->qp_ndevices++;
788 }
789 return val->prefix_bits;
f3fe4a2d
AM
790}
791
6b6aa828
CS
792/**
793 * @brief Slow / full mapping host inode nr -> guest inode nr.
794 *
795 * This function performs a slower and much more costly remapping of an
796 * original file inode number on host to an appropriate different inode
797 * number on guest. For every (dev, inode) combination on host a new
798 * sequential number is generated, cached and exposed as inode number on
799 * guest.
800 *
801 * This is just a "last resort" fallback solution if the much faster/cheaper
802 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
803 * expected ever to be used at all though.
804 *
805 * @see qid_path_suffixmap() for details
806 *
807 */
f3fe4a2d
AM
808static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
809 uint64_t *path)
810{
811 QpfEntry lookup = {
812 .dev = stbuf->st_dev,
813 .ino = stbuf->st_ino
814 }, *val;
815 uint32_t hash = qpf_hash(lookup);
6b6aa828 816 VariLenAffix affix;
f3fe4a2d
AM
817
818 val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
819
820 if (!val) {
821 if (pdu->s->qp_fullpath_next == 0) {
822 /* no more files can be mapped :'( */
823 error_report_once(
824 "9p: No more prefixes available for remapping inodes from "
825 "host to guest."
826 );
827 return -ENFILE;
828 }
829
830 val = g_malloc0(sizeof(QppEntry));
831 *val = lookup;
832
833 /* new unique inode and device combo */
6b6aa828
CS
834 affix = affixForIndex(
835 1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
836 );
837 val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
838 pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
f3fe4a2d
AM
839 qht_insert(&pdu->s->qpf_table, val, hash, NULL);
840 }
841
842 *path = val->path;
843 return 0;
844}
845
6b6aa828
CS
846/**
847 * @brief Quick mapping host inode nr -> guest inode nr.
1a6ed33c 848 *
6b6aa828
CS
849 * This function performs quick remapping of an original file inode number
850 * on host to an appropriate different inode number on guest. This remapping
851 * of inodes is required to avoid inode nr collisions on guest which would
852 * happen if the 9p export contains more than 1 exported file system (or
853 * more than 1 file system data set), because unlike on host level where the
854 * files would have different device nrs, all files exported by 9p would
855 * share the same device nr on guest (the device nr of the virtual 9p device
856 * that is).
857 *
858 * Inode remapping is performed by chopping off high end bits of the original
859 * inode number from host, shifting the result upwards and then assigning a
860 * generated suffix number for the low end bits, where the same suffix number
861 * will be shared by all inodes with the same device id AND the same high end
862 * bits that have been chopped off. That approach utilizes the fact that inode
863 * numbers very likely share the same high end bits (i.e. due to their common
864 * sequential generation by file systems) and hence we only have to generate
865 * and track a very limited amount of suffixes in practice due to that.
866 *
867 * We generate variable size suffixes for that purpose. The 1st generated
868 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
869 * the original inode number. The subsequent suffixes being generated will
870 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
871 * generated will have 3 bits and hence we have to chop off 3 bits from their
872 * original inodes, and so on. That approach of using variable length suffixes
873 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
874 * limited amount of devices are shared by the same export (e.g. typically
875 * less than 2 dozen devices per 9p export), so in practice we need to chop
876 * off less bits than with fixed size prefixes and yet are flexible to add
877 * new devices at runtime below host's export directory at any time without
878 * having to reboot guest nor requiring to reconfigure guest for that. And due
879 * to the very limited amount of original high end bits that we chop off that
880 * way, the total amount of suffixes we need to generate is less than by using
881 * fixed size prefixes and hence it also improves performance of the inode
882 * remapping algorithm, and finally has the nice side effect that the inode
883 * numbers on guest will be much smaller & human friendly. ;-)
1a6ed33c 884 */
6b6aa828 885static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
1a6ed33c
AM
886 uint64_t *path)
887{
6b6aa828 888 const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
1a6ed33c
AM
889 QppEntry lookup = {
890 .dev = stbuf->st_dev,
6b6aa828 891 .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
1a6ed33c
AM
892 }, *val;
893 uint32_t hash = qpp_hash(lookup);
894
895 val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
896
897 if (!val) {
6b6aa828
CS
898 if (pdu->s->qp_affix_next == 0) {
899 /* we ran out of affixes */
f3fe4a2d
AM
900 warn_report_once(
901 "9p: Potential degraded performance of inode remapping"
1a6ed33c
AM
902 );
903 return -ENFILE;
904 }
905
906 val = g_malloc0(sizeof(QppEntry));
907 *val = lookup;
908
6b6aa828
CS
909 /* new unique inode affix and device combo */
910 val->qp_affix_index = pdu->s->qp_affix_next++;
911 val->qp_affix = affixForIndex(val->qp_affix_index);
1a6ed33c
AM
912 qht_insert(&pdu->s->qpp_table, val, hash, NULL);
913 }
6b6aa828
CS
914 /* assuming generated affix to be suffix type, not prefix */
915 *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
1a6ed33c
AM
916 return 0;
917}
918
3b5ee9e8 919static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
286d5652 920{
1a6ed33c 921 int err;
286d5652
AL
922 size_t size;
923
1a6ed33c
AM
924 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
925 /* map inode+device to qid path (fast path) */
6b6aa828 926 err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
f3fe4a2d
AM
927 if (err == -ENFILE) {
928 /* fast path didn't work, fall back to full map */
929 err = qid_path_fullmap(pdu, stbuf, &qidp->path);
930 }
1a6ed33c
AM
931 if (err) {
932 return err;
933 }
934 } else {
935 if (pdu->s->dev_id != stbuf->st_dev) {
936 if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
937 error_report_once(
938 "9p: Multiple devices detected in same VirtFS export. "
939 "Access of guest to additional devices is (partly) "
940 "denied due to virtfs option 'multidevs=forbid' being "
941 "effective."
942 );
943 return -ENODEV;
944 } else {
945 warn_report_once(
946 "9p: Multiple devices detected in same VirtFS export, "
947 "which might lead to file ID collisions and severe "
948 "misbehaviours on guest! You should either use a "
949 "separate export for each device shared from host or "
950 "use virtfs option 'multidevs=remap'!"
951 );
952 }
953 }
954 memset(&qidp->path, 0, sizeof(qidp->path));
955 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
956 memcpy(&qidp->path, &stbuf->st_ino, size);
3b5ee9e8
AM
957 }
958
286d5652
AL
959 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
960 qidp->type = 0;
961 if (S_ISDIR(stbuf->st_mode)) {
962 qidp->type |= P9_QID_TYPE_DIR;
963 }
964 if (S_ISLNK(stbuf->st_mode)) {
965 qidp->type |= P9_QID_TYPE_SYMLINK;
966 }
3b5ee9e8
AM
967
968 return 0;
286d5652
AL
969}
970
8440e22e
GK
971static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
972 V9fsQID *qidp)
286d5652
AL
973{
974 struct stat stbuf;
975 int err;
976
bccacf6c 977 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
8c158561 978 if (err < 0) {
286d5652
AL
979 return err;
980 }
3b5ee9e8
AM
981 err = stat_to_qid(pdu, &stbuf, qidp);
982 if (err < 0) {
983 return err;
984 }
286d5652
AL
985 return 0;
986}
987
4b311c5f 988V9fsPDU *pdu_alloc(V9fsState *s)
9f107513
AL
989{
990 V9fsPDU *pdu = NULL;
991
992 if (!QLIST_EMPTY(&s->free_list)) {
bccacf6c
AK
993 pdu = QLIST_FIRST(&s->free_list);
994 QLIST_REMOVE(pdu, next);
995 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
9f107513
AL
996 }
997 return pdu;
998}
999
4b311c5f 1000void pdu_free(V9fsPDU *pdu)
9f107513 1001{
6868a420 1002 V9fsState *s = pdu->s;
f74e27bf
GK
1003
1004 g_assert(!pdu->cancelled);
1005 QLIST_REMOVE(pdu, next);
1006 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
9f107513
AL
1007}
1008
8440e22e 1009static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
405a549a
AL
1010{
1011 int8_t id = pdu->id + 1; /* Response */
ad38ce9e 1012 V9fsState *s = pdu->s;
06a37db7 1013 int ret;
405a549a 1014
fc78d5ee
KF
1015 /*
1016 * The 9p spec requires that successfully cancelled pdus receive no reply.
1017 * Sending a reply would confuse clients because they would
1018 * assume that any EINTR is the actual result of the operation,
1019 * rather than a consequence of the cancellation. However, if
1020 * the operation completed (succesfully or with an error other
1021 * than caused be cancellation), we do send out that reply, both
1022 * for efficiency and to avoid confusing the rest of the state machine
1023 * that assumes passing a non-error here will mean a successful
1024 * transmission of the reply.
1025 */
1026 bool discard = pdu->cancelled && len == -EINTR;
1027 if (discard) {
1028 trace_v9fs_rcancel(pdu->tag, pdu->id);
1029 pdu->size = 0;
1030 goto out_notify;
1031 }
1032
405a549a 1033 if (len < 0) {
405a549a 1034 int err = -len;
8f4d1ca5 1035 len = 7;
405a549a 1036
8f4d1ca5
AB
1037 if (s->proto_version != V9FS_PROTO_2000L) {
1038 V9fsString str;
1039
1040 str.data = strerror(err);
1041 str.size = strlen(str.data);
1042
06a37db7
GK
1043 ret = pdu_marshal(pdu, len, "s", &str);
1044 if (ret < 0) {
1045 goto out_notify;
1046 }
1047 len += ret;
8f4d1ca5
AB
1048 id = P9_RERROR;
1049 }
405a549a 1050
06a37db7
GK
1051 ret = pdu_marshal(pdu, len, "d", err);
1052 if (ret < 0) {
1053 goto out_notify;
1054 }
1055 len += ret;
405a549a 1056
8f4d1ca5
AB
1057 if (s->proto_version == V9FS_PROTO_2000L) {
1058 id = P9_RLERROR;
1059 }
7999f7e1 1060 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
405a549a
AL
1061 }
1062
1063 /* fill out the header */
06a37db7
GK
1064 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
1065 goto out_notify;
1066 }
405a549a
AL
1067
1068 /* keep these in sync */
1069 pdu->size = len;
1070 pdu->id = id;
1071
06a37db7 1072out_notify:
a17d8659 1073 pdu->s->transport->push_and_notify(pdu);
405a549a 1074
bccacf6c 1075 /* Now wakeup anybody waiting in flush for this request */
f74e27bf
GK
1076 if (!qemu_co_queue_next(&pdu->complete)) {
1077 pdu_free(pdu);
1078 }
405a549a
AL
1079}
1080
bb9e3216
AL
1081static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
1082{
1083 mode_t ret;
1084
1085 ret = mode & 0777;
1086 if (mode & P9_STAT_MODE_DIR) {
1087 ret |= S_IFDIR;
1088 }
1089
cf03eb2c
AB
1090 if (mode & P9_STAT_MODE_SYMLINK) {
1091 ret |= S_IFLNK;
1092 }
1093 if (mode & P9_STAT_MODE_SOCKET) {
1094 ret |= S_IFSOCK;
1095 }
1096 if (mode & P9_STAT_MODE_NAMED_PIPE) {
1097 ret |= S_IFIFO;
1098 }
1099 if (mode & P9_STAT_MODE_DEVICE) {
c7e587b7 1100 if (extension->size && extension->data[0] == 'c') {
cf03eb2c
AB
1101 ret |= S_IFCHR;
1102 } else {
1103 ret |= S_IFBLK;
bb9e3216
AL
1104 }
1105 }
1106
01011733 1107 if (!(ret & ~0777)) {
bb9e3216
AL
1108 ret |= S_IFREG;
1109 }
1110
1111 if (mode & P9_STAT_MODE_SETUID) {
1112 ret |= S_ISUID;
1113 }
1114 if (mode & P9_STAT_MODE_SETGID) {
1115 ret |= S_ISGID;
1116 }
1117 if (mode & P9_STAT_MODE_SETVTX) {
1118 ret |= S_ISVTX;
1119 }
1120
1121 return ret;
1122}
1123
1124static int donttouch_stat(V9fsStat *stat)
1125{
1126 if (stat->type == -1 &&
1127 stat->dev == -1 &&
87032833
AM
1128 stat->qid.type == 0xff &&
1129 stat->qid.version == (uint32_t) -1 &&
1130 stat->qid.path == (uint64_t) -1 &&
bb9e3216
AL
1131 stat->mode == -1 &&
1132 stat->atime == -1 &&
1133 stat->mtime == -1 &&
1134 stat->length == -1 &&
1135 !stat->name.size &&
1136 !stat->uid.size &&
1137 !stat->gid.size &&
1138 !stat->muid.size &&
1139 stat->n_uid == -1 &&
1140 stat->n_gid == -1 &&
1141 stat->n_muid == -1) {
1142 return 1;
1143 }
1144
1145 return 0;
1146}
1147
ddca7f86
MK
1148static void v9fs_stat_init(V9fsStat *stat)
1149{
1150 v9fs_string_init(&stat->name);
1151 v9fs_string_init(&stat->uid);
1152 v9fs_string_init(&stat->gid);
1153 v9fs_string_init(&stat->muid);
1154 v9fs_string_init(&stat->extension);
1155}
1156
bb9e3216
AL
1157static void v9fs_stat_free(V9fsStat *stat)
1158{
1159 v9fs_string_free(&stat->name);
1160 v9fs_string_free(&stat->uid);
1161 v9fs_string_free(&stat->gid);
1162 v9fs_string_free(&stat->muid);
1163 v9fs_string_free(&stat->extension);
1164}
1165
1166static uint32_t stat_to_v9mode(const struct stat *stbuf)
1167{
1168 uint32_t mode;
1169
1170 mode = stbuf->st_mode & 0777;
1171 if (S_ISDIR(stbuf->st_mode)) {
1172 mode |= P9_STAT_MODE_DIR;
1173 }
1174
cf03eb2c
AB
1175 if (S_ISLNK(stbuf->st_mode)) {
1176 mode |= P9_STAT_MODE_SYMLINK;
1177 }
bb9e3216 1178
cf03eb2c
AB
1179 if (S_ISSOCK(stbuf->st_mode)) {
1180 mode |= P9_STAT_MODE_SOCKET;
1181 }
bb9e3216 1182
cf03eb2c
AB
1183 if (S_ISFIFO(stbuf->st_mode)) {
1184 mode |= P9_STAT_MODE_NAMED_PIPE;
1185 }
bb9e3216 1186
cf03eb2c
AB
1187 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
1188 mode |= P9_STAT_MODE_DEVICE;
1189 }
bb9e3216 1190
cf03eb2c
AB
1191 if (stbuf->st_mode & S_ISUID) {
1192 mode |= P9_STAT_MODE_SETUID;
1193 }
bb9e3216 1194
cf03eb2c
AB
1195 if (stbuf->st_mode & S_ISGID) {
1196 mode |= P9_STAT_MODE_SETGID;
1197 }
bb9e3216 1198
cf03eb2c
AB
1199 if (stbuf->st_mode & S_ISVTX) {
1200 mode |= P9_STAT_MODE_SETVTX;
bb9e3216
AL
1201 }
1202
1203 return mode;
1204}
1205
6069537f
JD
1206static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
1207 const char *basename,
8440e22e
GK
1208 const struct stat *stbuf,
1209 V9fsStat *v9stat)
bb9e3216
AL
1210{
1211 int err;
bb9e3216
AL
1212
1213 memset(v9stat, 0, sizeof(*v9stat));
1214
3b5ee9e8
AM
1215 err = stat_to_qid(pdu, stbuf, &v9stat->qid);
1216 if (err < 0) {
1217 return err;
1218 }
bb9e3216
AL
1219 v9stat->mode = stat_to_v9mode(stbuf);
1220 v9stat->atime = stbuf->st_atime;
1221 v9stat->mtime = stbuf->st_mtime;
1222 v9stat->length = stbuf->st_size;
1223
abdf0086
GK
1224 v9fs_string_free(&v9stat->uid);
1225 v9fs_string_free(&v9stat->gid);
1226 v9fs_string_free(&v9stat->muid);
bb9e3216 1227
cf03eb2c
AB
1228 v9stat->n_uid = stbuf->st_uid;
1229 v9stat->n_gid = stbuf->st_gid;
1230 v9stat->n_muid = 0;
bb9e3216 1231
abdf0086 1232 v9fs_string_free(&v9stat->extension);
bb9e3216 1233
cf03eb2c 1234 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
6069537f 1235 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
7a5ca31e 1236 if (err < 0) {
cf03eb2c 1237 return err;
bb9e3216 1238 }
cf03eb2c
AB
1239 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1240 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1241 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1242 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1243 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
c9ba47dc
SW
1244 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1245 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
bb9e3216
AL
1246 }
1247
6069537f 1248 v9fs_string_sprintf(&v9stat->name, "%s", basename);
bb9e3216
AL
1249
1250 v9stat->size = 61 +
1251 v9fs_string_size(&v9stat->name) +
1252 v9fs_string_size(&v9stat->uid) +
1253 v9fs_string_size(&v9stat->gid) +
1254 v9fs_string_size(&v9stat->muid) +
1255 v9fs_string_size(&v9stat->extension);
1256 return 0;
1257}
1258
00ede4c2
SK
1259#define P9_STATS_MODE 0x00000001ULL
1260#define P9_STATS_NLINK 0x00000002ULL
1261#define P9_STATS_UID 0x00000004ULL
1262#define P9_STATS_GID 0x00000008ULL
1263#define P9_STATS_RDEV 0x00000010ULL
1264#define P9_STATS_ATIME 0x00000020ULL
1265#define P9_STATS_MTIME 0x00000040ULL
1266#define P9_STATS_CTIME 0x00000080ULL
1267#define P9_STATS_INO 0x00000100ULL
1268#define P9_STATS_SIZE 0x00000200ULL
1269#define P9_STATS_BLOCKS 0x00000400ULL
1270
1271#define P9_STATS_BTIME 0x00000800ULL
1272#define P9_STATS_GEN 0x00001000ULL
1273#define P9_STATS_DATA_VERSION 0x00002000ULL
1274
1275#define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1276#define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1277
1278
3b5ee9e8 1279static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
8db21ce7 1280 V9fsStatDotl *v9lstat)
00ede4c2
SK
1281{
1282 memset(v9lstat, 0, sizeof(*v9lstat));
1283
1284 v9lstat->st_mode = stbuf->st_mode;
1285 v9lstat->st_nlink = stbuf->st_nlink;
1286 v9lstat->st_uid = stbuf->st_uid;
1287 v9lstat->st_gid = stbuf->st_gid;
1288 v9lstat->st_rdev = stbuf->st_rdev;
1289 v9lstat->st_size = stbuf->st_size;
1290 v9lstat->st_blksize = stbuf->st_blksize;
1291 v9lstat->st_blocks = stbuf->st_blocks;
1292 v9lstat->st_atime_sec = stbuf->st_atime;
1293 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1294 v9lstat->st_mtime_sec = stbuf->st_mtime;
1295 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1296 v9lstat->st_ctime_sec = stbuf->st_ctime;
1297 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1298 /* Currently we only support BASIC fields in stat */
1299 v9lstat->st_result_mask = P9_STATS_BASIC;
1300
3b5ee9e8 1301 return stat_to_qid(pdu, stbuf, &v9lstat->qid);
00ede4c2
SK
1302}
1303
1f5a89bf
AL
1304static void print_sg(struct iovec *sg, int cnt)
1305{
1306 int i;
1307
1308 printf("sg[%d]: {", cnt);
1309 for (i = 0; i < cnt; i++) {
1310 if (i) {
1311 printf(", ");
1312 }
1313 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1314 }
1315 printf("}\n");
1316}
1317
2289be19
AK
1318/* Will call this only for path name based fid */
1319static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
8cf89e00 1320{
2289be19
AK
1321 V9fsPath str;
1322 v9fs_path_init(&str);
1323 v9fs_path_copy(&str, dst);
e3e83f2e 1324 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
2289be19 1325 v9fs_path_free(&str);
8cf89e00
AL
1326}
1327
2c74c2cb
MK
1328static inline bool is_ro_export(FsContext *ctx)
1329{
1330 return ctx->export_flags & V9FS_RDONLY;
1331}
1332
8440e22e 1333static void coroutine_fn v9fs_version(void *opaque)
9f107513 1334{
ddca7f86 1335 ssize_t err;
ff06030f
VJ
1336 V9fsPDU *pdu = opaque;
1337 V9fsState *s = pdu->s;
92c1ad03
AL
1338 V9fsString version;
1339 size_t offset = 7;
1340
ddca7f86
MK
1341 v9fs_string_init(&version);
1342 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1343 if (err < 0) {
ddca7f86
MK
1344 goto out;
1345 }
c572f23a 1346 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
92c1ad03 1347
b41e2992
DS
1348 virtfs_reset(pdu);
1349
84151514
MK
1350 if (!strcmp(version.data, "9P2000.u")) {
1351 s->proto_version = V9FS_PROTO_2000U;
1352 } else if (!strcmp(version.data, "9P2000.L")) {
1353 s->proto_version = V9FS_PROTO_2000L;
1354 } else {
92c1ad03 1355 v9fs_string_sprintf(&version, "unknown");
e16453a3
CS
1356 /* skip min. msize check, reporting invalid version has priority */
1357 goto marshal;
9f107513 1358 }
92c1ad03 1359
e16453a3
CS
1360 if (s->msize < P9_MIN_MSIZE) {
1361 err = -EMSGSIZE;
1362 error_report(
1363 "9pfs: Client requested msize < minimum msize ("
1364 stringify(P9_MIN_MSIZE) ") supported by this server."
1365 );
1366 goto out;
1367 }
1368
62777d82 1369 /* 8192 is the default msize of Linux clients */
c418f935 1370 if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) {
62777d82
CS
1371 warn_report_once(
1372 "9p: degraded performance: a reasonable high msize should be "
1373 "chosen on client/guest side (chosen msize is <= 8192). See "
1374 "https://wiki.qemu.org/Documentation/9psetup#msize for details."
1375 );
1376 }
1377
e16453a3 1378marshal:
ddca7f86
MK
1379 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1380 if (err < 0) {
ddca7f86
MK
1381 goto out;
1382 }
403a905b 1383 err += offset;
c572f23a 1384 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
ddca7f86 1385out:
403a905b 1386 pdu_complete(pdu, err);
92c1ad03 1387 v9fs_string_free(&version);
9f107513
AL
1388}
1389
8440e22e 1390static void coroutine_fn v9fs_attach(void *opaque)
9f107513 1391{
ff06030f
VJ
1392 V9fsPDU *pdu = opaque;
1393 V9fsState *s = pdu->s;
955efc47
AL
1394 int32_t fid, afid, n_uname;
1395 V9fsString uname, aname;
1396 V9fsFidState *fidp;
955efc47 1397 size_t offset = 7;
8c158561 1398 V9fsQID qid;
955efc47
AL
1399 ssize_t err;
1400
ddca7f86
MK
1401 v9fs_string_init(&uname);
1402 v9fs_string_init(&aname);
1403 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1404 &afid, &uname, &aname, &n_uname);
1405 if (err < 0) {
1406 goto out_nofid;
1407 }
c572f23a 1408 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
955efc47
AL
1409
1410 fidp = alloc_fid(s, fid);
1411 if (fidp == NULL) {
1412 err = -EINVAL;
84dfb926 1413 goto out_nofid;
9f107513 1414 }
955efc47 1415 fidp->uid = n_uname;
bccacf6c 1416 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
2289be19
AK
1417 if (err < 0) {
1418 err = -EINVAL;
1419 clunk_fid(s, fid);
1420 goto out;
1421 }
bccacf6c 1422 err = fid_to_qid(pdu, fidp, &qid);
8c158561 1423 if (err < 0) {
955efc47 1424 err = -EINVAL;
84dfb926 1425 clunk_fid(s, fid);
955efc47
AL
1426 goto out;
1427 }
fe44dc91 1428
4cdc0789
AK
1429 /*
1430 * disable migration if we haven't done already.
1431 * attach could get called multiple times for the same export.
1432 */
1433 if (!s->migration_blocker) {
f231b88d
CR
1434 error_setg(&s->migration_blocker,
1435 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1436 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
9261ef5e
MA
1437 err = migrate_add_blocker(s->migration_blocker, NULL);
1438 if (err < 0) {
fe44dc91
AA
1439 error_free(s->migration_blocker);
1440 s->migration_blocker = NULL;
1441 clunk_fid(s, fid);
1442 goto out;
1443 }
1444 s->root_fid = fid;
1445 }
1446
1447 err = pdu_marshal(pdu, offset, "Q", &qid);
1448 if (err < 0) {
1449 clunk_fid(s, fid);
1450 goto out;
4cdc0789 1451 }
fe44dc91
AA
1452 err += offset;
1453
1454 memcpy(&s->root_qid, &qid, sizeof(qid));
1455 trace_v9fs_attach_return(pdu->tag, pdu->id,
1456 qid.type, qid.version, qid.path);
955efc47 1457out:
bccacf6c 1458 put_fid(pdu, fidp);
84dfb926 1459out_nofid:
dc295f83 1460 pdu_complete(pdu, err);
955efc47
AL
1461 v9fs_string_free(&uname);
1462 v9fs_string_free(&aname);
9f107513
AL
1463}
1464
8440e22e 1465static void coroutine_fn v9fs_stat(void *opaque)
9f107513 1466{
4da7d3fa 1467 int32_t fid;
d8e0c29e 1468 V9fsStat v9stat;
4da7d3fa 1469 ssize_t err = 0;
d8e0c29e
AK
1470 size_t offset = 7;
1471 struct stat stbuf;
1472 V9fsFidState *fidp;
1473 V9fsPDU *pdu = opaque;
6069537f 1474 char *basename;
4da7d3fa 1475
ddca7f86
MK
1476 err = pdu_unmarshal(pdu, offset, "d", &fid);
1477 if (err < 0) {
1478 goto out_nofid;
1479 }
c572f23a 1480 trace_v9fs_stat(pdu->tag, pdu->id, fid);
84dfb926 1481
bccacf6c 1482 fidp = get_fid(pdu, fid);
d8e0c29e 1483 if (fidp == NULL) {
4da7d3fa 1484 err = -ENOENT;
84dfb926 1485 goto out_nofid;
9f107513 1486 }
bccacf6c 1487 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
d8e0c29e
AK
1488 if (err < 0) {
1489 goto out;
1490 }
6069537f
JD
1491 basename = g_path_get_basename(fidp->path.data);
1492 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1493 g_free(basename);
d8e0c29e
AK
1494 if (err < 0) {
1495 goto out;
1496 }
ddca7f86
MK
1497 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1498 if (err < 0) {
1499 v9fs_stat_free(&v9stat);
1500 goto out;
1501 }
7999f7e1
AK
1502 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1503 v9stat.atime, v9stat.mtime, v9stat.length);
ddca7f86 1504 err += offset;
d8e0c29e 1505 v9fs_stat_free(&v9stat);
4da7d3fa 1506out:
bccacf6c 1507 put_fid(pdu, fidp);
84dfb926 1508out_nofid:
dc295f83 1509 pdu_complete(pdu, err);
9f107513
AL
1510}
1511
8440e22e 1512static void coroutine_fn v9fs_getattr(void *opaque)
00ede4c2
SK
1513{
1514 int32_t fid;
8db21ce7
AK
1515 size_t offset = 7;
1516 ssize_t retval = 0;
1517 struct stat stbuf;
00ede4c2
SK
1518 V9fsFidState *fidp;
1519 uint64_t request_mask;
8db21ce7
AK
1520 V9fsStatDotl v9stat_dotl;
1521 V9fsPDU *pdu = opaque;
00ede4c2 1522
ddca7f86
MK
1523 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1524 if (retval < 0) {
1525 goto out_nofid;
1526 }
c572f23a 1527 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
00ede4c2 1528
bccacf6c 1529 fidp = get_fid(pdu, fid);
00ede4c2 1530 if (fidp == NULL) {
8db21ce7 1531 retval = -ENOENT;
84dfb926 1532 goto out_nofid;
00ede4c2 1533 }
8db21ce7
AK
1534 /*
1535 * Currently we only support BASIC fields in stat, so there is no
00ede4c2
SK
1536 * need to look at request_mask.
1537 */
bccacf6c 1538 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
8db21ce7
AK
1539 if (retval < 0) {
1540 goto out;
1541 }
3b5ee9e8
AM
1542 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
1543 if (retval < 0) {
1544 goto out;
1545 }
e06a765e
HPB
1546
1547 /* fill st_gen if requested and supported by underlying fs */
1548 if (request_mask & P9_STATS_GEN) {
1549 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
f8b7ee38
KS
1550 switch (retval) {
1551 case 0:
1552 /* we have valid st_gen: update result mask */
1553 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1554 break;
1555 case -EINTR:
1556 /* request cancelled, e.g. by Tflush */
e06a765e 1557 goto out;
f8b7ee38
KS
1558 default:
1559 /* failed to get st_gen: not fatal, ignore */
1560 break;
e06a765e 1561 }
e06a765e 1562 }
ddca7f86
MK
1563 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1564 if (retval < 0) {
1565 goto out;
1566 }
1567 retval += offset;
c572f23a
HPB
1568 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1569 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1570 v9stat_dotl.st_gid);
7999f7e1
AK
1571out:
1572 put_fid(pdu, fidp);
1573out_nofid:
dc295f83 1574 pdu_complete(pdu, retval);
00ede4c2
SK
1575}
1576
e4027caf
AK
1577/* Attribute flags */
1578#define P9_ATTR_MODE (1 << 0)
1579#define P9_ATTR_UID (1 << 1)
1580#define P9_ATTR_GID (1 << 2)
1581#define P9_ATTR_SIZE (1 << 3)
1582#define P9_ATTR_ATIME (1 << 4)
1583#define P9_ATTR_MTIME (1 << 5)
1584#define P9_ATTR_CTIME (1 << 6)
1585#define P9_ATTR_ATIME_SET (1 << 7)
1586#define P9_ATTR_MTIME_SET (1 << 8)
1587
1588#define P9_ATTR_MASK 127
c79ce737 1589
8440e22e 1590static void coroutine_fn v9fs_setattr(void *opaque)
c79ce737 1591{
65c05f9a
AK
1592 int err = 0;
1593 int32_t fid;
1594 V9fsFidState *fidp;
1595 size_t offset = 7;
1596 V9fsIattr v9iattr;
1597 V9fsPDU *pdu = opaque;
c79ce737 1598
ddca7f86
MK
1599 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1600 if (err < 0) {
1601 goto out_nofid;
1602 }
c79ce737 1603
8f9c64bf
GK
1604 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1605 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1606 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1607
bccacf6c 1608 fidp = get_fid(pdu, fid);
65c05f9a
AK
1609 if (fidp == NULL) {
1610 err = -EINVAL;
84dfb926 1611 goto out_nofid;
c79ce737 1612 }
e4027caf 1613 if (v9iattr.valid & P9_ATTR_MODE) {
bccacf6c 1614 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
65c05f9a
AK
1615 if (err < 0) {
1616 goto out;
c79ce737 1617 }
c79ce737 1618 }
e4027caf 1619 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
c79ce737 1620 struct timespec times[2];
e4027caf
AK
1621 if (v9iattr.valid & P9_ATTR_ATIME) {
1622 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
65c05f9a
AK
1623 times[0].tv_sec = v9iattr.atime_sec;
1624 times[0].tv_nsec = v9iattr.atime_nsec;
c79ce737
SK
1625 } else {
1626 times[0].tv_nsec = UTIME_NOW;
1627 }
1628 } else {
1629 times[0].tv_nsec = UTIME_OMIT;
1630 }
e4027caf
AK
1631 if (v9iattr.valid & P9_ATTR_MTIME) {
1632 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
65c05f9a
AK
1633 times[1].tv_sec = v9iattr.mtime_sec;
1634 times[1].tv_nsec = v9iattr.mtime_nsec;
c79ce737
SK
1635 } else {
1636 times[1].tv_nsec = UTIME_NOW;
1637 }
1638 } else {
1639 times[1].tv_nsec = UTIME_OMIT;
1640 }
bccacf6c 1641 err = v9fs_co_utimensat(pdu, &fidp->path, times);
65c05f9a
AK
1642 if (err < 0) {
1643 goto out;
1644 }
c79ce737 1645 }
65c05f9a
AK
1646 /*
1647 * If the only valid entry in iattr is ctime we can call
1648 * chown(-1,-1) to update the ctime of the file
1649 */
e4027caf
AK
1650 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1651 ((v9iattr.valid & P9_ATTR_CTIME)
1652 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1653 if (!(v9iattr.valid & P9_ATTR_UID)) {
65c05f9a
AK
1654 v9iattr.uid = -1;
1655 }
e4027caf 1656 if (!(v9iattr.valid & P9_ATTR_GID)) {
65c05f9a
AK
1657 v9iattr.gid = -1;
1658 }
bccacf6c 1659 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
65c05f9a
AK
1660 v9iattr.gid);
1661 if (err < 0) {
1662 goto out;
1663 }
c79ce737 1664 }
e4027caf 1665 if (v9iattr.valid & (P9_ATTR_SIZE)) {
bccacf6c 1666 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
65c05f9a
AK
1667 if (err < 0) {
1668 goto out;
1669 }
c79ce737 1670 }
65c05f9a 1671 err = offset;
8f9c64bf 1672 trace_v9fs_setattr_return(pdu->tag, pdu->id);
c79ce737 1673out:
bccacf6c 1674 put_fid(pdu, fidp);
84dfb926 1675out_nofid:
dc295f83 1676 pdu_complete(pdu, err);
c79ce737
SK
1677}
1678
3cc19c0c 1679static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
ff5e54c9
AL
1680{
1681 int i;
ddca7f86 1682 ssize_t err;
3cc19c0c 1683 size_t offset = 7;
ddca7f86
MK
1684
1685 err = pdu_marshal(pdu, offset, "w", nwnames);
1686 if (err < 0) {
1687 return err;
1688 }
1689 offset += err;
3cc19c0c 1690 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1691 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1692 if (err < 0) {
1693 return err;
1694 }
1695 offset += err;
ff5e54c9 1696 }
3cc19c0c 1697 return offset;
ff5e54c9
AL
1698}
1699
fff39a7a
GK
1700static bool name_is_illegal(const char *name)
1701{
1702 return !*name || strchr(name, '/') != NULL;
1703}
1704
56f101ec
GK
1705static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1706{
1707 return
1708 qid1->type != qid2->type ||
1709 qid1->version != qid2->version ||
1710 qid1->path != qid2->path;
1711}
1712
8440e22e 1713static void coroutine_fn v9fs_walk(void *opaque)
9f107513 1714{
3cc19c0c
AK
1715 int name_idx;
1716 V9fsQID *qids = NULL;
1717 int i, err = 0;
2289be19 1718 V9fsPath dpath, path;
3cc19c0c
AK
1719 uint16_t nwnames;
1720 struct stat stbuf;
1721 size_t offset = 7;
1722 int32_t fid, newfid;
1723 V9fsString *wnames = NULL;
1724 V9fsFidState *fidp;
3a93113a 1725 V9fsFidState *newfidp = NULL;
ff06030f
VJ
1726 V9fsPDU *pdu = opaque;
1727 V9fsState *s = pdu->s;
56f101ec 1728 V9fsQID qid;
ff5e54c9 1729
ddca7f86
MK
1730 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1731 if (err < 0) {
dc295f83 1732 pdu_complete(pdu, err);
ddca7f86
MK
1733 return ;
1734 }
1735 offset += err;
ff5e54c9 1736
c572f23a
HPB
1737 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1738
3cc19c0c 1739 if (nwnames && nwnames <= P9_MAXWELEM) {
1923923b
GK
1740 wnames = g_new0(V9fsString, nwnames);
1741 qids = g_new0(V9fsQID, nwnames);
3cc19c0c 1742 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1743 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1744 if (err < 0) {
1745 goto out_nofid;
1746 }
fff39a7a
GK
1747 if (name_is_illegal(wnames[i].data)) {
1748 err = -ENOENT;
1749 goto out_nofid;
1750 }
ddca7f86 1751 offset += err;
ff5e54c9 1752 }
3cc19c0c 1753 } else if (nwnames > P9_MAXWELEM) {
4f8dee2d 1754 err = -EINVAL;
84dfb926 1755 goto out_nofid;
ff5e54c9 1756 }
bccacf6c 1757 fidp = get_fid(pdu, fid);
3cc19c0c 1758 if (fidp == NULL) {
ff5e54c9 1759 err = -ENOENT;
84dfb926 1760 goto out_nofid;
ff5e54c9 1761 }
56f101ec 1762
13fd08e6
GK
1763 v9fs_path_init(&dpath);
1764 v9fs_path_init(&path);
1765
56f101ec
GK
1766 err = fid_to_qid(pdu, fidp, &qid);
1767 if (err < 0) {
1768 goto out;
1769 }
1770
2289be19
AK
1771 /*
1772 * Both dpath and path initially poin to fidp.
1773 * Needed to handle request with nwnames == 0
1774 */
1775 v9fs_path_copy(&dpath, &fidp->path);
1776 v9fs_path_copy(&path, &fidp->path);
1777 for (name_idx = 0; name_idx < nwnames; name_idx++) {
56f101ec
GK
1778 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1779 strcmp("..", wnames[name_idx].data)) {
1780 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1781 &path);
1782 if (err < 0) {
1783 goto out;
1784 }
1785
1786 err = v9fs_co_lstat(pdu, &path, &stbuf);
1787 if (err < 0) {
1788 goto out;
1789 }
3b5ee9e8
AM
1790 err = stat_to_qid(pdu, &stbuf, &qid);
1791 if (err < 0) {
1792 goto out;
1793 }
56f101ec 1794 v9fs_path_copy(&dpath, &path);
2289be19 1795 }
56f101ec 1796 memcpy(&qids[name_idx], &qid, sizeof(qid));
2289be19 1797 }
ff5e54c9 1798 if (fid == newfid) {
49dd946b
GK
1799 if (fidp->fid_type != P9_FID_NONE) {
1800 err = -EINVAL;
1801 goto out;
1802 }
5b3c77aa 1803 v9fs_path_write_lock(s);
2289be19 1804 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 1805 v9fs_path_unlock(s);
ff5e54c9 1806 } else {
3cc19c0c
AK
1807 newfidp = alloc_fid(s, newfid);
1808 if (newfidp == NULL) {
ff5e54c9
AL
1809 err = -EINVAL;
1810 goto out;
1811 }
3cc19c0c 1812 newfidp->uid = fidp->uid;
2289be19 1813 v9fs_path_copy(&newfidp->path, &path);
9f107513 1814 }
3cc19c0c 1815 err = v9fs_walk_marshal(pdu, nwnames, qids);
7999f7e1 1816 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
ff5e54c9 1817out:
bccacf6c 1818 put_fid(pdu, fidp);
84dfb926 1819 if (newfidp) {
bccacf6c 1820 put_fid(pdu, newfidp);
84dfb926 1821 }
2289be19
AK
1822 v9fs_path_free(&dpath);
1823 v9fs_path_free(&path);
84dfb926 1824out_nofid:
dc295f83 1825 pdu_complete(pdu, err);
3cc19c0c
AK
1826 if (nwnames && nwnames <= P9_MAXWELEM) {
1827 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1828 v9fs_string_free(&wnames[name_idx]);
1829 }
1830 g_free(wnames);
1831 g_free(qids);
1832 }
9f107513
AL
1833}
1834
8440e22e 1835static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
5e94c103
MK
1836{
1837 struct statfs stbuf;
1838 int32_t iounit = 0;
bccacf6c 1839 V9fsState *s = pdu->s;
5e94c103
MK
1840
1841 /*
1842 * iounit should be multiples of f_bsize (host filesystem block size
1843 * and as well as less than (client msize - P9_IOHDRSZ))
1844 */
bccacf6c 1845 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
68d654da
DS
1846 if (stbuf.f_bsize) {
1847 iounit = stbuf.f_bsize;
1848 iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize;
1849 }
5e94c103 1850 }
5e94c103
MK
1851 if (!iounit) {
1852 iounit = s->msize - P9_IOHDRSZ;
1853 }
1854 return iounit;
1855}
1856
8440e22e 1857static void coroutine_fn v9fs_open(void *opaque)
5e94c103 1858{
857bc158 1859 int flags;
857bc158
AK
1860 int32_t fid;
1861 int32_t mode;
1862 V9fsQID qid;
7999f7e1 1863 int iounit = 0;
857bc158
AK
1864 ssize_t err = 0;
1865 size_t offset = 7;
1866 struct stat stbuf;
1867 V9fsFidState *fidp;
1868 V9fsPDU *pdu = opaque;
1869 V9fsState *s = pdu->s;
5e94c103 1870
857bc158 1871 if (s->proto_version == V9FS_PROTO_2000L) {
ddca7f86 1872 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
857bc158 1873 } else {
67d6fa53
BH
1874 uint8_t modebyte;
1875 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1876 mode = modebyte;
ddca7f86
MK
1877 }
1878 if (err < 0) {
1879 goto out_nofid;
857bc158 1880 }
c572f23a
HPB
1881 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1882
bccacf6c 1883 fidp = get_fid(pdu, fid);
857bc158
AK
1884 if (fidp == NULL) {
1885 err = -ENOENT;
84dfb926 1886 goto out_nofid;
a6568fe2 1887 }
49dd946b
GK
1888 if (fidp->fid_type != P9_FID_NONE) {
1889 err = -EINVAL;
1890 goto out;
1891 }
a6568fe2 1892
bccacf6c 1893 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
857bc158 1894 if (err < 0) {
a6568fe2
AL
1895 goto out;
1896 }
3b5ee9e8
AM
1897 err = stat_to_qid(pdu, &stbuf, &qid);
1898 if (err < 0) {
1899 goto out;
1900 }
857bc158 1901 if (S_ISDIR(stbuf.st_mode)) {
bccacf6c 1902 err = v9fs_co_opendir(pdu, fidp);
857bc158
AK
1903 if (err < 0) {
1904 goto out;
1905 }
1906 fidp->fid_type = P9_FID_DIR;
ddca7f86
MK
1907 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1908 if (err < 0) {
1909 goto out;
1910 }
1911 err += offset;
a6568fe2 1912 } else {
771e9d4c 1913 if (s->proto_version == V9FS_PROTO_2000L) {
d3ab98e6 1914 flags = get_dotl_openflags(s, mode);
771e9d4c 1915 } else {
857bc158 1916 flags = omode_to_uflags(mode);
771e9d4c 1917 }
2c74c2cb
MK
1918 if (is_ro_export(&s->ctx)) {
1919 if (mode & O_WRONLY || mode & O_RDWR ||
1920 mode & O_APPEND || mode & O_TRUNC) {
1921 err = -EROFS;
1922 goto out;
1923 }
2c74c2cb 1924 }
bccacf6c 1925 err = v9fs_co_open(pdu, fidp, flags);
857bc158
AK
1926 if (err < 0) {
1927 goto out;
1928 }
1929 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1930 fidp->open_flags = flags;
1931 if (flags & O_EXCL) {
1932 /*
1933 * We let the host file system do O_EXCL check
1934 * We should not reclaim such fd
1935 */
1936 fidp->flags |= FID_NON_RECLAIMABLE;
1937 }
bccacf6c 1938 iounit = get_iounit(pdu, &fidp->path);
ddca7f86
MK
1939 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1940 if (err < 0) {
1941 goto out;
1942 }
1943 err += offset;
a6568fe2 1944 }
7999f7e1
AK
1945 trace_v9fs_open_return(pdu->tag, pdu->id,
1946 qid.type, qid.version, qid.path, iounit);
a6568fe2 1947out:
bccacf6c 1948 put_fid(pdu, fidp);
84dfb926 1949out_nofid:
dc295f83 1950 pdu_complete(pdu, err);
a6568fe2
AL
1951}
1952
8440e22e 1953static void coroutine_fn v9fs_lcreate(void *opaque)
c1568af5
VJ
1954{
1955 int32_t dfid, flags, mode;
1956 gid_t gid;
c1568af5 1957 ssize_t err = 0;
36f8981f 1958 ssize_t offset = 7;
36f8981f
VJ
1959 V9fsString name;
1960 V9fsFidState *fidp;
1961 struct stat stbuf;
1962 V9fsQID qid;
1963 int32_t iounit;
1964 V9fsPDU *pdu = opaque;
c1568af5 1965
ddca7f86
MK
1966 v9fs_string_init(&name);
1967 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1968 &name, &flags, &mode, &gid);
1969 if (err < 0) {
1970 goto out_nofid;
1971 }
c572f23a 1972 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
c1568af5 1973
fff39a7a
GK
1974 if (name_is_illegal(name.data)) {
1975 err = -ENOENT;
1976 goto out_nofid;
1977 }
1978
805b5d98
GK
1979 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1980 err = -EEXIST;
1981 goto out_nofid;
1982 }
1983
bccacf6c 1984 fidp = get_fid(pdu, dfid);
36f8981f 1985 if (fidp == NULL) {
c1568af5 1986 err = -ENOENT;
84dfb926 1987 goto out_nofid;
c1568af5 1988 }
d63fb193
LQ
1989 if (fidp->fid_type != P9_FID_NONE) {
1990 err = -EINVAL;
1991 goto out;
1992 }
c1568af5 1993
d3ab98e6 1994 flags = get_dotl_openflags(pdu->s, flags);
bccacf6c 1995 err = v9fs_co_open2(pdu, fidp, &name, gid,
02cb7f3a 1996 flags | O_CREAT, mode, &stbuf);
36f8981f
VJ
1997 if (err < 0) {
1998 goto out;
1999 }
2000 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2001 fidp->open_flags = flags;
2002 if (flags & O_EXCL) {
2003 /*
2004 * We let the host file system do O_EXCL check
2005 * We should not reclaim such fd
2006 */
2007 fidp->flags |= FID_NON_RECLAIMABLE;
2008 }
bccacf6c 2009 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2010 err = stat_to_qid(pdu, &stbuf, &qid);
2011 if (err < 0) {
2012 goto out;
2013 }
ddca7f86
MK
2014 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2015 if (err < 0) {
2016 goto out;
2017 }
2018 err += offset;
7999f7e1
AK
2019 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
2020 qid.type, qid.version, qid.path, iounit);
c1568af5 2021out:
bccacf6c 2022 put_fid(pdu, fidp);
84dfb926 2023out_nofid:
dc295f83 2024 pdu_complete(pdu, err);
36f8981f 2025 v9fs_string_free(&name);
c1568af5
VJ
2026}
2027
a1bf8b74 2028static void coroutine_fn v9fs_fsync(void *opaque)
b41e95d3 2029{
4e9ad444 2030 int err;
b41e95d3 2031 int32_t fid;
4e9ad444 2032 int datasync;
b41e95d3
VJ
2033 size_t offset = 7;
2034 V9fsFidState *fidp;
4e9ad444 2035 V9fsPDU *pdu = opaque;
b41e95d3 2036
ddca7f86
MK
2037 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
2038 if (err < 0) {
2039 goto out_nofid;
2040 }
c572f23a
HPB
2041 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
2042
bccacf6c 2043 fidp = get_fid(pdu, fid);
b41e95d3
VJ
2044 if (fidp == NULL) {
2045 err = -ENOENT;
84dfb926 2046 goto out_nofid;
b41e95d3 2047 }
bccacf6c 2048 err = v9fs_co_fsync(pdu, fidp, datasync);
4e9ad444
AK
2049 if (!err) {
2050 err = offset;
2051 }
bccacf6c 2052 put_fid(pdu, fidp);
84dfb926 2053out_nofid:
dc295f83 2054 pdu_complete(pdu, err);
b41e95d3
VJ
2055}
2056
8440e22e 2057static void coroutine_fn v9fs_clunk(void *opaque)
a6568fe2 2058{
c540ee51 2059 int err;
bbd5697b
AL
2060 int32_t fid;
2061 size_t offset = 7;
84dfb926 2062 V9fsFidState *fidp;
c540ee51
AK
2063 V9fsPDU *pdu = opaque;
2064 V9fsState *s = pdu->s;
bbd5697b 2065
ddca7f86
MK
2066 err = pdu_unmarshal(pdu, offset, "d", &fid);
2067 if (err < 0) {
2068 goto out_nofid;
2069 }
c572f23a 2070 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
84dfb926 2071
ce421a19 2072 fidp = clunk_fid(s, fid);
84dfb926
AK
2073 if (fidp == NULL) {
2074 err = -ENOENT;
2075 goto out_nofid;
2076 }
ce421a19
AK
2077 /*
2078 * Bump the ref so that put_fid will
2079 * free the fid.
2080 */
2081 fidp->ref++;
a911a182
AK
2082 err = put_fid(pdu, fidp);
2083 if (!err) {
2084 err = offset;
2085 }
84dfb926 2086out_nofid:
dc295f83 2087 pdu_complete(pdu, err);
9f107513
AL
2088}
2089
bcb8998f
SS
2090/*
2091 * Create a QEMUIOVector for a sub-region of PDU iovecs
2092 *
2093 * @qiov: uninitialized QEMUIOVector
2094 * @skip: number of bytes to skip from beginning of PDU
2095 * @size: number of bytes to include
2096 * @is_write: true - write, false - read
2097 *
2098 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2099 * with qemu_iovec_destroy().
2100 */
2101static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
cf45183b 2102 size_t skip, size_t size,
bcb8998f
SS
2103 bool is_write)
2104{
2105 QEMUIOVector elem;
2106 struct iovec *iov;
2107 unsigned int niov;
2108
88da0b03 2109 if (is_write) {
cf45183b 2110 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
88da0b03 2111 } else {
cf45183b 2112 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
88da0b03 2113 }
bcb8998f
SS
2114
2115 qemu_iovec_init_external(&elem, iov, niov);
2116 qemu_iovec_init(qiov, niov);
cf45183b 2117 qemu_iovec_concat(qiov, &elem, skip, size);
bcb8998f
SS
2118}
2119
2f008a8c
AK
2120static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2121 uint64_t off, uint32_t max_count)
a9231555 2122{
ddca7f86 2123 ssize_t err;
d208a0e0 2124 size_t offset = 7;
cf45183b 2125 uint64_t read_count;
bcb8998f 2126 QEMUIOVector qiov_full;
a9231555 2127
7e55d65c
LQ
2128 if (fidp->fs.xattr.len < off) {
2129 read_count = 0;
16724a17 2130 } else {
cf45183b
SS
2131 read_count = fidp->fs.xattr.len - off;
2132 }
2133 if (read_count > max_count) {
d208a0e0 2134 read_count = max_count;
a9231555 2135 }
ddca7f86
MK
2136 err = pdu_marshal(pdu, offset, "d", read_count);
2137 if (err < 0) {
2138 return err;
2139 }
2140 offset += err;
00588a0a 2141
cf45183b 2142 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
fa0eb5c5 2143 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
ddca7f86
MK
2144 ((char *)fidp->fs.xattr.value) + off,
2145 read_count);
bcb8998f 2146 qemu_iovec_destroy(&qiov_full);
ddca7f86
MK
2147 if (err < 0) {
2148 return err;
2149 }
2150 offset += err;
d208a0e0 2151 return offset;
a9231555
AL
2152}
2153
8440e22e
GK
2154static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
2155 V9fsFidState *fidp,
2156 uint32_t max_count)
a9231555 2157{
2289be19 2158 V9fsPath path;
d208a0e0
AK
2159 V9fsStat v9stat;
2160 int len, err = 0;
2161 int32_t count = 0;
2162 struct stat stbuf;
2163 off_t saved_dir_pos;
635324e8 2164 struct dirent *dent;
a9231555 2165
d208a0e0 2166 /* save the directory position */
bccacf6c 2167 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
d208a0e0
AK
2168 if (saved_dir_pos < 0) {
2169 return saved_dir_pos;
a9231555 2170 }
5f524c1e 2171
d208a0e0 2172 while (1) {
2289be19 2173 v9fs_path_init(&path);
7cde47d4
GK
2174
2175 v9fs_readdir_lock(&fidp->fs.dir);
2176
635324e8
GK
2177 err = v9fs_co_readdir(pdu, fidp, &dent);
2178 if (err || !dent) {
d208a0e0
AK
2179 break;
2180 }
bccacf6c 2181 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
d208a0e0 2182 if (err < 0) {
8762a46d 2183 break;
d208a0e0 2184 }
bccacf6c 2185 err = v9fs_co_lstat(pdu, &path, &stbuf);
2289be19 2186 if (err < 0) {
8762a46d 2187 break;
2289be19 2188 }
6069537f 2189 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
d208a0e0 2190 if (err < 0) {
8762a46d 2191 break;
a9231555 2192 }
772a7369
JD
2193 if ((count + v9stat.size + 2) > max_count) {
2194 v9fs_readdir_unlock(&fidp->fs.dir);
2195
2196 /* Ran out of buffer. Set dir back to old position and return */
2197 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2198 v9fs_stat_free(&v9stat);
2199 v9fs_path_free(&path);
2200 return count;
2201 }
2202
d208a0e0
AK
2203 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2204 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
7cde47d4
GK
2205
2206 v9fs_readdir_unlock(&fidp->fs.dir);
2207
772a7369 2208 if (len < 0) {
bccacf6c 2209 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
d208a0e0 2210 v9fs_stat_free(&v9stat);
2289be19 2211 v9fs_path_free(&path);
772a7369 2212 return len;
d208a0e0
AK
2213 }
2214 count += len;
2215 v9fs_stat_free(&v9stat);
2289be19 2216 v9fs_path_free(&path);
d208a0e0 2217 saved_dir_pos = dent->d_off;
a9231555 2218 }
8762a46d 2219
7cde47d4
GK
2220 v9fs_readdir_unlock(&fidp->fs.dir);
2221
2289be19 2222 v9fs_path_free(&path);
d208a0e0
AK
2223 if (err < 0) {
2224 return err;
fa32ef88 2225 }
d208a0e0 2226 return count;
fa32ef88
AK
2227}
2228
8440e22e 2229static void coroutine_fn v9fs_read(void *opaque)
9f107513 2230{
a9231555 2231 int32_t fid;
2f008a8c 2232 uint64_t off;
a9231555 2233 ssize_t err = 0;
d208a0e0
AK
2234 int32_t count = 0;
2235 size_t offset = 7;
2f008a8c 2236 uint32_t max_count;
d208a0e0
AK
2237 V9fsFidState *fidp;
2238 V9fsPDU *pdu = opaque;
2239 V9fsState *s = pdu->s;
a9231555 2240
ddca7f86
MK
2241 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
2242 if (err < 0) {
2243 goto out_nofid;
2244 }
c572f23a 2245 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
84dfb926 2246
bccacf6c 2247 fidp = get_fid(pdu, fid);
d208a0e0 2248 if (fidp == NULL) {
a9231555 2249 err = -EINVAL;
84dfb926 2250 goto out_nofid;
a9231555 2251 }
d208a0e0 2252 if (fidp->fid_type == P9_FID_DIR) {
d2c5cf7c
CS
2253 if (s->proto_version != V9FS_PROTO_2000U) {
2254 warn_report_once(
2255 "9p: bad client: T_read request on directory only expected "
2256 "with 9P2000.u protocol version"
2257 );
2258 err = -EOPNOTSUPP;
2259 goto out;
2260 }
d208a0e0 2261 if (off == 0) {
bccacf6c 2262 v9fs_co_rewinddir(pdu, fidp);
a9231555 2263 }
bccacf6c 2264 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
d208a0e0
AK
2265 if (count < 0) {
2266 err = count;
2267 goto out;
56d15a53 2268 }
ddca7f86
MK
2269 err = pdu_marshal(pdu, offset, "d", count);
2270 if (err < 0) {
2271 goto out;
2272 }
2273 err += offset + count;
d208a0e0 2274 } else if (fidp->fid_type == P9_FID_FILE) {
302a0d3e
SH
2275 QEMUIOVector qiov_full;
2276 QEMUIOVector qiov;
d208a0e0 2277 int32_t len;
d208a0e0 2278
cf45183b 2279 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
302a0d3e 2280 qemu_iovec_init(&qiov, qiov_full.niov);
d208a0e0 2281 do {
302a0d3e 2282 qemu_iovec_reset(&qiov);
1b093c48 2283 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
d208a0e0 2284 if (0) {
302a0d3e 2285 print_sg(qiov.iov, qiov.niov);
d208a0e0
AK
2286 }
2287 /* Loop in case of EINTR */
2288 do {
302a0d3e 2289 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
d208a0e0
AK
2290 if (len >= 0) {
2291 off += len;
2292 count += len;
2293 }
bccacf6c 2294 } while (len == -EINTR && !pdu->cancelled);
d208a0e0
AK
2295 if (len < 0) {
2296 /* IO error return the error */
2297 err = len;
e95c9a49 2298 goto out_free_iovec;
d208a0e0 2299 }
d208a0e0 2300 } while (count < max_count && len > 0);
ddca7f86
MK
2301 err = pdu_marshal(pdu, offset, "d", count);
2302 if (err < 0) {
e95c9a49 2303 goto out_free_iovec;
ddca7f86
MK
2304 }
2305 err += offset + count;
e95c9a49 2306out_free_iovec:
302a0d3e
SH
2307 qemu_iovec_destroy(&qiov);
2308 qemu_iovec_destroy(&qiov_full);
d208a0e0
AK
2309 } else if (fidp->fid_type == P9_FID_XATTR) {
2310 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
a9231555
AL
2311 } else {
2312 err = -EINVAL;
9f107513 2313 }
7999f7e1 2314 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
a9231555 2315out:
bccacf6c 2316 put_fid(pdu, fidp);
84dfb926 2317out_nofid:
dc295f83 2318 pdu_complete(pdu, err);
9f107513
AL
2319}
2320
29c9d2ca
CS
2321/**
2322 * Returns size required in Rreaddir response for the passed dirent @p name.
2323 *
2324 * @param name - directory entry's name (i.e. file name, directory name)
2325 * @returns required size in bytes
2326 */
2327size_t v9fs_readdir_response_size(V9fsString *name)
c18e2f94 2328{
5e4eaa79
AK
2329 /*
2330 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2331 * size of type (1) + size of name.size (2) + strlen(name.data)
2332 */
2333 return 24 + v9fs_string_size(name);
c18e2f94
SK
2334}
2335
0c4356ba
CS
2336static void v9fs_free_dirents(struct V9fsDirEnt *e)
2337{
2338 struct V9fsDirEnt *next = NULL;
2339
2340 for (; e; e = next) {
2341 next = e->next;
2342 g_free(e->dent);
2343 g_free(e->st);
2344 g_free(e);
2345 }
2346}
2347
8440e22e 2348static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
0c4356ba 2349 off_t offset, int32_t max_count)
c18e2f94 2350{
c18e2f94 2351 size_t size;
5e4eaa79
AK
2352 V9fsQID qid;
2353 V9fsString name;
2354 int len, err = 0;
2355 int32_t count = 0;
635324e8 2356 struct dirent *dent;
0c4356ba
CS
2357 struct stat *st;
2358 struct V9fsDirEnt *entries = NULL;
c18e2f94 2359
0c4356ba
CS
2360 /*
2361 * inode remapping requires the device id, which in turn might be
2362 * different for different directory entries, so if inode remapping is
2363 * enabled we have to make a full stat for each directory entry
2364 */
2365 const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES;
7cde47d4 2366
0c4356ba
CS
2367 /*
2368 * Fetch all required directory entries altogether on a background IO
2369 * thread from fs driver. We don't want to do that for each entry
2370 * individually, because hopping between threads (this main IO thread
2371 * and background IO driver thread) would sum up to huge latencies.
2372 */
2373 count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count,
2374 dostat);
2375 if (count < 0) {
2376 err = count;
2377 count = 0;
2378 goto out;
2379 }
2380 count = 0;
7cde47d4 2381
0c4356ba
CS
2382 for (struct V9fsDirEnt *e = entries; e; e = e->next) {
2383 dent = e->dent;
1a6ed33c
AM
2384
2385 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
0c4356ba
CS
2386 st = e->st;
2387 /* e->st should never be NULL, but just to be sure */
2388 if (!st) {
2389 err = -1;
2390 break;
2391 }
2392
2393 /* remap inode */
2394 err = stat_to_qid(pdu, st, &qid);
1a6ed33c 2395 if (err < 0) {
0c4356ba 2396 break;
1a6ed33c
AM
2397 }
2398 } else {
2399 /*
2400 * Fill up just the path field of qid because the client uses
2401 * only that. To fill the entire qid structure we will have
2402 * to stat each dirent found, which is expensive. For the
0c4356ba 2403 * latter reason we don't call stat_to_qid() here. Only drawback
1a6ed33c
AM
2404 * is that no multi-device export detection of stat_to_qid()
2405 * would be done and provided as error to the user here. But
2406 * user would get that error anyway when accessing those
2407 * files/dirs through other ways.
2408 */
2409 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2410 memcpy(&qid.path, &dent->d_ino, size);
2411 /* Fill the other fields with dummy values */
2412 qid.type = 0;
2413 qid.version = 0;
2414 }
c18e2f94 2415
0c4356ba
CS
2416 v9fs_string_init(&name);
2417 v9fs_string_sprintf(&name, "%s", dent->d_name);
2418
5e4eaa79
AK
2419 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2420 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2421 &qid, dent->d_off,
2422 dent->d_type, &name);
7cde47d4 2423
0c4356ba 2424 v9fs_string_free(&name);
7cde47d4 2425
ddca7f86 2426 if (len < 0) {
0c4356ba
CS
2427 err = len;
2428 break;
ddca7f86 2429 }
0c4356ba 2430
5e4eaa79 2431 count += len;
5e4eaa79 2432 }
7cde47d4 2433
0c4356ba
CS
2434out:
2435 v9fs_free_dirents(entries);
5e4eaa79
AK
2436 if (err < 0) {
2437 return err;
2438 }
2439 return count;
c18e2f94
SK
2440}
2441
8440e22e 2442static void coroutine_fn v9fs_readdir(void *opaque)
c18e2f94
SK
2443{
2444 int32_t fid;
5e4eaa79
AK
2445 V9fsFidState *fidp;
2446 ssize_t retval = 0;
c18e2f94 2447 size_t offset = 7;
2f008a8c
AK
2448 uint64_t initial_offset;
2449 int32_t count;
2450 uint32_t max_count;
5e4eaa79 2451 V9fsPDU *pdu = opaque;
d36a5c22 2452 V9fsState *s = pdu->s;
c18e2f94 2453
ddca7f86
MK
2454 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2455 &initial_offset, &max_count);
2456 if (retval < 0) {
2457 goto out_nofid;
2458 }
c572f23a
HPB
2459 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2460
d36a5c22
CS
2461 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2462 if (max_count > s->msize - 11) {
2463 max_count = s->msize - 11;
2464 warn_report_once(
2465 "9p: bad client: T_readdir with count > msize - 11"
2466 );
2467 }
2468
bccacf6c 2469 fidp = get_fid(pdu, fid);
84dfb926
AK
2470 if (fidp == NULL) {
2471 retval = -EINVAL;
2472 goto out_nofid;
2473 }
f314ea4e 2474 if (!fidp->fs.dir.stream) {
5e4eaa79 2475 retval = -EINVAL;
c18e2f94
SK
2476 goto out;
2477 }
d2c5cf7c
CS
2478 if (s->proto_version != V9FS_PROTO_2000L) {
2479 warn_report_once(
2480 "9p: bad client: T_readdir request only expected with 9P2000.L "
2481 "protocol version"
2482 );
2483 retval = -EOPNOTSUPP;
2484 goto out;
2485 }
0c4356ba 2486 count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count);
5e4eaa79
AK
2487 if (count < 0) {
2488 retval = count;
2489 goto out;
2490 }
ddca7f86
MK
2491 retval = pdu_marshal(pdu, offset, "d", count);
2492 if (retval < 0) {
2493 goto out;
2494 }
2495 retval += count + offset;
7999f7e1 2496 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
c18e2f94 2497out:
bccacf6c 2498 put_fid(pdu, fidp);
84dfb926 2499out_nofid:
dc295f83 2500 pdu_complete(pdu, retval);
c18e2f94
SK
2501}
2502
d7a90491 2503static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2f008a8c 2504 uint64_t off, uint32_t count,
d7a90491 2505 struct iovec *sg, int cnt)
10b468bd
AK
2506{
2507 int i, to_copy;
2508 ssize_t err = 0;
7e55d65c 2509 uint64_t write_count;
d7a90491 2510 size_t offset = 7;
10b468bd 2511
d7a90491 2512
7e55d65c 2513 if (fidp->fs.xattr.len < off) {
b858e80a 2514 return -ENOSPC;
10b468bd 2515 }
7e55d65c
LQ
2516 write_count = fidp->fs.xattr.len - off;
2517 if (write_count > count) {
2518 write_count = count;
2519 }
ddca7f86
MK
2520 err = pdu_marshal(pdu, offset, "d", write_count);
2521 if (err < 0) {
2522 return err;
2523 }
2524 err += offset;
d7a90491 2525 fidp->fs.xattr.copied_len += write_count;
10b468bd
AK
2526 /*
2527 * Now copy the content from sg list
2528 */
d7a90491
AK
2529 for (i = 0; i < cnt; i++) {
2530 if (write_count > sg[i].iov_len) {
2531 to_copy = sg[i].iov_len;
10b468bd
AK
2532 } else {
2533 to_copy = write_count;
2534 }
d7a90491 2535 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
10b468bd 2536 /* updating vs->off since we are not using below */
d7a90491 2537 off += to_copy;
10b468bd
AK
2538 write_count -= to_copy;
2539 }
b858e80a 2540
d7a90491 2541 return err;
10b468bd
AK
2542}
2543
8440e22e 2544static void coroutine_fn v9fs_write(void *opaque)
9f107513 2545{
d7a90491
AK
2546 ssize_t err;
2547 int32_t fid;
2f008a8c
AK
2548 uint64_t off;
2549 uint32_t count;
d7a90491
AK
2550 int32_t len = 0;
2551 int32_t total = 0;
2552 size_t offset = 7;
2553 V9fsFidState *fidp;
ff06030f
VJ
2554 V9fsPDU *pdu = opaque;
2555 V9fsState *s = pdu->s;
302a0d3e
SH
2556 QEMUIOVector qiov_full;
2557 QEMUIOVector qiov;
8449360c 2558
ddca7f86
MK
2559 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2560 if (err < 0) {
dc295f83 2561 pdu_complete(pdu, err);
0289a412 2562 return;
ddca7f86
MK
2563 }
2564 offset += err;
cf45183b 2565 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
302a0d3e 2566 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
84dfb926 2567
bccacf6c 2568 fidp = get_fid(pdu, fid);
d7a90491 2569 if (fidp == NULL) {
8449360c 2570 err = -EINVAL;
84dfb926 2571 goto out_nofid;
9f107513 2572 }
d7a90491
AK
2573 if (fidp->fid_type == P9_FID_FILE) {
2574 if (fidp->fs.fd == -1) {
10b468bd
AK
2575 err = -EINVAL;
2576 goto out;
2577 }
d7a90491 2578 } else if (fidp->fid_type == P9_FID_XATTR) {
10b468bd
AK
2579 /*
2580 * setxattr operation
2581 */
302a0d3e
SH
2582 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2583 qiov_full.iov, qiov_full.niov);
d7a90491 2584 goto out;
10b468bd 2585 } else {
8449360c
AL
2586 err = -EINVAL;
2587 goto out;
2588 }
302a0d3e 2589 qemu_iovec_init(&qiov, qiov_full.niov);
d7a90491 2590 do {
302a0d3e 2591 qemu_iovec_reset(&qiov);
1b093c48 2592 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
d7a90491 2593 if (0) {
302a0d3e 2594 print_sg(qiov.iov, qiov.niov);
56d15a53 2595 }
d7a90491
AK
2596 /* Loop in case of EINTR */
2597 do {
302a0d3e 2598 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
d7a90491
AK
2599 if (len >= 0) {
2600 off += len;
2601 total += len;
2602 }
bccacf6c 2603 } while (len == -EINTR && !pdu->cancelled);
d7a90491
AK
2604 if (len < 0) {
2605 /* IO error return the error */
2606 err = len;
302a0d3e 2607 goto out_qiov;
d7a90491 2608 }
d7a90491 2609 } while (total < count && len > 0);
302a0d3e
SH
2610
2611 offset = 7;
ddca7f86
MK
2612 err = pdu_marshal(pdu, offset, "d", total);
2613 if (err < 0) {
fdfcc9ae 2614 goto out_qiov;
ddca7f86
MK
2615 }
2616 err += offset;
7999f7e1 2617 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
302a0d3e
SH
2618out_qiov:
2619 qemu_iovec_destroy(&qiov);
8449360c 2620out:
bccacf6c 2621 put_fid(pdu, fidp);
84dfb926 2622out_nofid:
302a0d3e 2623 qemu_iovec_destroy(&qiov_full);
dc295f83 2624 pdu_complete(pdu, err);
9f107513
AL
2625}
2626
8440e22e 2627static void coroutine_fn v9fs_create(void *opaque)
5e94c103 2628{
baaa86d9
VJ
2629 int32_t fid;
2630 int err = 0;
2631 size_t offset = 7;
2632 V9fsFidState *fidp;
2633 V9fsQID qid;
2634 int32_t perm;
2635 int8_t mode;
2289be19 2636 V9fsPath path;
baaa86d9
VJ
2637 struct stat stbuf;
2638 V9fsString name;
2639 V9fsString extension;
baaa86d9
VJ
2640 int iounit;
2641 V9fsPDU *pdu = opaque;
5b3c77aa 2642 V9fsState *s = pdu->s;
c494dd6f 2643
2289be19 2644 v9fs_path_init(&path);
ddca7f86
MK
2645 v9fs_string_init(&name);
2646 v9fs_string_init(&extension);
2647 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2648 &perm, &mode, &extension);
2649 if (err < 0) {
2650 goto out_nofid;
2651 }
c572f23a
HPB
2652 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2653
fff39a7a
GK
2654 if (name_is_illegal(name.data)) {
2655 err = -ENOENT;
2656 goto out_nofid;
2657 }
2658
805b5d98
GK
2659 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2660 err = -EEXIST;
2661 goto out_nofid;
2662 }
2663
bccacf6c 2664 fidp = get_fid(pdu, fid);
baaa86d9
VJ
2665 if (fidp == NULL) {
2666 err = -EINVAL;
84dfb926 2667 goto out_nofid;
c494dd6f 2668 }
d63fb193
LQ
2669 if (fidp->fid_type != P9_FID_NONE) {
2670 err = -EINVAL;
2671 goto out;
2672 }
baaa86d9 2673 if (perm & P9_STAT_MODE_DIR) {
bccacf6c 2674 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
02cb7f3a 2675 fidp->uid, -1, &stbuf);
baaa86d9
VJ
2676 if (err < 0) {
2677 goto out;
2678 }
bccacf6c 2679 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2680 if (err < 0) {
2681 goto out;
2682 }
5b3c77aa 2683 v9fs_path_write_lock(s);
2289be19 2684 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2685 v9fs_path_unlock(s);
bccacf6c 2686 err = v9fs_co_opendir(pdu, fidp);
baaa86d9
VJ
2687 if (err < 0) {
2688 goto out;
2689 }
2690 fidp->fid_type = P9_FID_DIR;
2691 } else if (perm & P9_STAT_MODE_SYMLINK) {
bccacf6c 2692 err = v9fs_co_symlink(pdu, fidp, &name,
02cb7f3a 2693 extension.data, -1 , &stbuf);
baaa86d9 2694 if (err < 0) {
baaa86d9
VJ
2695 goto out;
2696 }
bccacf6c 2697 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2698 if (err < 0) {
2699 goto out;
2700 }
5b3c77aa 2701 v9fs_path_write_lock(s);
2289be19 2702 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2703 v9fs_path_unlock(s);
baaa86d9 2704 } else if (perm & P9_STAT_MODE_LINK) {
2289be19 2705 int32_t ofid = atoi(extension.data);
bccacf6c 2706 V9fsFidState *ofidp = get_fid(pdu, ofid);
2289be19 2707 if (ofidp == NULL) {
baaa86d9
VJ
2708 err = -EINVAL;
2709 goto out;
2710 }
bccacf6c
AK
2711 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2712 put_fid(pdu, ofidp);
2289be19
AK
2713 if (err < 0) {
2714 goto out;
2715 }
bccacf6c 2716 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
baaa86d9 2717 if (err < 0) {
2289be19 2718 fidp->fid_type = P9_FID_NONE;
baaa86d9 2719 goto out;
c494dd6f 2720 }
5b3c77aa 2721 v9fs_path_write_lock(s);
2289be19 2722 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2723 v9fs_path_unlock(s);
bccacf6c 2724 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
02cb7f3a
AK
2725 if (err < 0) {
2726 fidp->fid_type = P9_FID_NONE;
2727 goto out;
2728 }
baaa86d9 2729 } else if (perm & P9_STAT_MODE_DEVICE) {
c494dd6f
AL
2730 char ctype;
2731 uint32_t major, minor;
2732 mode_t nmode = 0;
2733
baaa86d9 2734 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
c494dd6f 2735 err = -errno;
baaa86d9 2736 goto out;
c494dd6f
AL
2737 }
2738
2739 switch (ctype) {
2740 case 'c':
2741 nmode = S_IFCHR;
2742 break;
2743 case 'b':
2744 nmode = S_IFBLK;
2745 break;
2746 default:
2747 err = -EIO;
baaa86d9
VJ
2748 goto out;
2749 }
c1568af5 2750
baaa86d9 2751 nmode |= perm & 0777;
bccacf6c 2752 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2753 makedev(major, minor), nmode, &stbuf);
baaa86d9
VJ
2754 if (err < 0) {
2755 goto out;
2756 }
bccacf6c 2757 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2758 if (err < 0) {
2759 goto out;
2760 }
5b3c77aa 2761 v9fs_path_write_lock(s);
2289be19 2762 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2763 v9fs_path_unlock(s);
baaa86d9 2764 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
bccacf6c 2765 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2766 0, S_IFIFO | (perm & 0777), &stbuf);
baaa86d9
VJ
2767 if (err < 0) {
2768 goto out;
2769 }
bccacf6c 2770 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2771 if (err < 0) {
2772 goto out;
2773 }
5b3c77aa 2774 v9fs_path_write_lock(s);
2289be19 2775 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2776 v9fs_path_unlock(s);
baaa86d9 2777 } else if (perm & P9_STAT_MODE_SOCKET) {
bccacf6c 2778 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2779 0, S_IFSOCK | (perm & 0777), &stbuf);
baaa86d9
VJ
2780 if (err < 0) {
2781 goto out;
2782 }
bccacf6c 2783 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2784 if (err < 0) {
2785 goto out;
2786 }
5b3c77aa 2787 v9fs_path_write_lock(s);
2289be19 2788 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2789 v9fs_path_unlock(s);
baaa86d9 2790 } else {
bccacf6c 2791 err = v9fs_co_open2(pdu, fidp, &name, -1,
01011733 2792 omode_to_uflags(mode) | O_CREAT, perm, &stbuf);
baaa86d9
VJ
2793 if (err < 0) {
2794 goto out;
2795 }
2796 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2797 fidp->open_flags = omode_to_uflags(mode);
2798 if (fidp->open_flags & O_EXCL) {
2799 /*
2800 * We let the host file system do O_EXCL check
2801 * We should not reclaim such fd
2802 */
2803 fidp->flags |= FID_NON_RECLAIMABLE;
2804 }
c494dd6f 2805 }
bccacf6c 2806 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2807 err = stat_to_qid(pdu, &stbuf, &qid);
2808 if (err < 0) {
2809 goto out;
2810 }
ddca7f86
MK
2811 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2812 if (err < 0) {
2813 goto out;
2814 }
2815 err += offset;
7999f7e1
AK
2816 trace_v9fs_create_return(pdu->tag, pdu->id,
2817 qid.type, qid.version, qid.path, iounit);
c494dd6f 2818out:
bccacf6c 2819 put_fid(pdu, fidp);
84dfb926 2820out_nofid:
dc295f83 2821 pdu_complete(pdu, err);
baaa86d9
VJ
2822 v9fs_string_free(&name);
2823 v9fs_string_free(&extension);
2289be19 2824 v9fs_path_free(&path);
9f107513
AL
2825}
2826
8440e22e 2827static void coroutine_fn v9fs_symlink(void *opaque)
08c60fc9 2828{
ff06030f 2829 V9fsPDU *pdu = opaque;
3fa2a8d1
VJ
2830 V9fsString name;
2831 V9fsString symname;
3fa2a8d1
VJ
2832 V9fsFidState *dfidp;
2833 V9fsQID qid;
2834 struct stat stbuf;
08c60fc9 2835 int32_t dfid;
08c60fc9
VJ
2836 int err = 0;
2837 gid_t gid;
3fa2a8d1 2838 size_t offset = 7;
08c60fc9 2839
ddca7f86
MK
2840 v9fs_string_init(&name);
2841 v9fs_string_init(&symname);
2842 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2843 if (err < 0) {
2844 goto out_nofid;
2845 }
c572f23a 2846 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
08c60fc9 2847
fff39a7a
GK
2848 if (name_is_illegal(name.data)) {
2849 err = -ENOENT;
2850 goto out_nofid;
2851 }
2852
805b5d98
GK
2853 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2854 err = -EEXIST;
2855 goto out_nofid;
2856 }
2857
bccacf6c 2858 dfidp = get_fid(pdu, dfid);
3fa2a8d1 2859 if (dfidp == NULL) {
08c60fc9 2860 err = -EINVAL;
84dfb926 2861 goto out_nofid;
08c60fc9 2862 }
bccacf6c 2863 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
3fa2a8d1
VJ
2864 if (err < 0) {
2865 goto out;
2866 }
3b5ee9e8
AM
2867 err = stat_to_qid(pdu, &stbuf, &qid);
2868 if (err < 0) {
2869 goto out;
2870 }
ddca7f86
MK
2871 err = pdu_marshal(pdu, offset, "Q", &qid);
2872 if (err < 0) {
2873 goto out;
2874 }
2875 err += offset;
7999f7e1
AK
2876 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2877 qid.type, qid.version, qid.path);
08c60fc9 2878out:
bccacf6c 2879 put_fid(pdu, dfidp);
84dfb926 2880out_nofid:
dc295f83 2881 pdu_complete(pdu, err);
3fa2a8d1
VJ
2882 v9fs_string_free(&name);
2883 v9fs_string_free(&symname);
08c60fc9
VJ
2884}
2885
a1bf8b74 2886static void coroutine_fn v9fs_flush(void *opaque)
9f107513 2887{
ddca7f86 2888 ssize_t err;
bccacf6c
AK
2889 int16_t tag;
2890 size_t offset = 7;
d5f2af7b 2891 V9fsPDU *cancel_pdu = NULL;
ff06030f
VJ
2892 V9fsPDU *pdu = opaque;
2893 V9fsState *s = pdu->s;
bccacf6c 2894
ddca7f86
MK
2895 err = pdu_unmarshal(pdu, offset, "w", &tag);
2896 if (err < 0) {
dc295f83 2897 pdu_complete(pdu, err);
ddca7f86
MK
2898 return;
2899 }
c572f23a 2900 trace_v9fs_flush(pdu->tag, pdu->id, tag);
bccacf6c 2901
d5f2af7b 2902 if (pdu->tag == tag) {
3dc6f869 2903 warn_report("the guest sent a self-referencing 9P flush request");
d5f2af7b
GK
2904 } else {
2905 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2906 if (cancel_pdu->tag == tag) {
2907 break;
2908 }
bccacf6c
AK
2909 }
2910 }
2911 if (cancel_pdu) {
2912 cancel_pdu->cancelled = 1;
2913 /*
2914 * Wait for pdu to complete.
2915 */
1ace7cea 2916 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
18adde86
GK
2917 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2918 cancel_pdu->cancelled = 0;
2919 pdu_free(cancel_pdu);
2920 }
bccacf6c 2921 }
dc295f83 2922 pdu_complete(pdu, 7);
9f107513
AL
2923}
2924
8440e22e 2925static void coroutine_fn v9fs_link(void *opaque)
b2c224be 2926{
ff06030f 2927 V9fsPDU *pdu = opaque;
b2c224be
VJ
2928 int32_t dfid, oldfid;
2929 V9fsFidState *dfidp, *oldfidp;
3a93113a 2930 V9fsString name;
b2c224be
VJ
2931 size_t offset = 7;
2932 int err = 0;
2933
ddca7f86
MK
2934 v9fs_string_init(&name);
2935 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2936 if (err < 0) {
2937 goto out_nofid;
2938 }
c572f23a 2939 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
b2c224be 2940
fff39a7a
GK
2941 if (name_is_illegal(name.data)) {
2942 err = -ENOENT;
2943 goto out_nofid;
2944 }
2945
805b5d98
GK
2946 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2947 err = -EEXIST;
2948 goto out_nofid;
2949 }
2950
bccacf6c 2951 dfidp = get_fid(pdu, dfid);
b2c224be 2952 if (dfidp == NULL) {
ffd66876 2953 err = -ENOENT;
84dfb926 2954 goto out_nofid;
b2c224be
VJ
2955 }
2956
bccacf6c 2957 oldfidp = get_fid(pdu, oldfid);
b2c224be 2958 if (oldfidp == NULL) {
ffd66876 2959 err = -ENOENT;
b2c224be
VJ
2960 goto out;
2961 }
bccacf6c 2962 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
ffd66876
VJ
2963 if (!err) {
2964 err = offset;
b2c224be 2965 }
4c158678 2966 put_fid(pdu, oldfidp);
b2c224be 2967out:
bccacf6c 2968 put_fid(pdu, dfidp);
84dfb926 2969out_nofid:
b2c224be 2970 v9fs_string_free(&name);
dc295f83 2971 pdu_complete(pdu, err);
b2c224be
VJ
2972}
2973
532decb7 2974/* Only works with path name based fid */
8440e22e 2975static void coroutine_fn v9fs_remove(void *opaque)
9f107513 2976{
5bae1900 2977 int32_t fid;
5bae1900 2978 int err = 0;
ae1ef571
VJ
2979 size_t offset = 7;
2980 V9fsFidState *fidp;
2981 V9fsPDU *pdu = opaque;
5bae1900 2982
ddca7f86
MK
2983 err = pdu_unmarshal(pdu, offset, "d", &fid);
2984 if (err < 0) {
2985 goto out_nofid;
2986 }
c572f23a 2987 trace_v9fs_remove(pdu->tag, pdu->id, fid);
5bae1900 2988
bccacf6c 2989 fidp = get_fid(pdu, fid);
ae1ef571 2990 if (fidp == NULL) {
5bae1900 2991 err = -EINVAL;
84dfb926 2992 goto out_nofid;
9f107513 2993 }
532decb7 2994 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 2995 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
2996 err = -EOPNOTSUPP;
2997 goto out_err;
2998 }
7a462745
AK
2999 /*
3000 * IF the file is unlinked, we cannot reopen
3001 * the file later. So don't reclaim fd
3002 */
bccacf6c 3003 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
7a462745
AK
3004 if (err < 0) {
3005 goto out_err;
3006 }
bccacf6c 3007 err = v9fs_co_remove(pdu, &fidp->path);
ae1ef571
VJ
3008 if (!err) {
3009 err = offset;
3010 }
7a462745 3011out_err:
ae1ef571 3012 /* For TREMOVE we need to clunk the fid even on failed remove */
84dfb926 3013 clunk_fid(pdu->s, fidp->fid);
bccacf6c 3014 put_fid(pdu, fidp);
84dfb926 3015out_nofid:
dc295f83 3016 pdu_complete(pdu, err);
9f107513
AL
3017}
3018
8440e22e 3019static void coroutine_fn v9fs_unlinkat(void *opaque)
7834cf77
AK
3020{
3021 int err = 0;
3022 V9fsString name;
67e87345 3023 int32_t dfid, flags, rflags = 0;
7834cf77 3024 size_t offset = 7;
2289be19 3025 V9fsPath path;
7834cf77
AK
3026 V9fsFidState *dfidp;
3027 V9fsPDU *pdu = opaque;
7834cf77 3028
ddca7f86
MK
3029 v9fs_string_init(&name);
3030 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
3031 if (err < 0) {
3032 goto out_nofid;
3033 }
fff39a7a
GK
3034
3035 if (name_is_illegal(name.data)) {
3036 err = -ENOENT;
3037 goto out_nofid;
3038 }
3039
805b5d98
GK
3040 if (!strcmp(".", name.data)) {
3041 err = -EINVAL;
3042 goto out_nofid;
3043 }
3044
3045 if (!strcmp("..", name.data)) {
3046 err = -ENOTEMPTY;
3047 goto out_nofid;
3048 }
3049
67e87345
KF
3050 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
3051 err = -EINVAL;
3052 goto out_nofid;
3053 }
3054
3055 if (flags & P9_DOTL_AT_REMOVEDIR) {
3056 rflags |= AT_REMOVEDIR;
3057 }
3058
bccacf6c 3059 dfidp = get_fid(pdu, dfid);
7834cf77
AK
3060 if (dfidp == NULL) {
3061 err = -EINVAL;
3062 goto out_nofid;
3063 }
7834cf77
AK
3064 /*
3065 * IF the file is unlinked, we cannot reopen
3066 * the file later. So don't reclaim fd
3067 */
2289be19 3068 v9fs_path_init(&path);
bccacf6c 3069 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2289be19
AK
3070 if (err < 0) {
3071 goto out_err;
3072 }
bccacf6c 3073 err = v9fs_mark_fids_unreclaim(pdu, &path);
7834cf77
AK
3074 if (err < 0) {
3075 goto out_err;
3076 }
67e87345 3077 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
7834cf77
AK
3078 if (!err) {
3079 err = offset;
3080 }
3081out_err:
bccacf6c 3082 put_fid(pdu, dfidp);
2289be19 3083 v9fs_path_free(&path);
7834cf77 3084out_nofid:
dc295f83 3085 pdu_complete(pdu, err);
7834cf77
AK
3086 v9fs_string_free(&name);
3087}
3088
2289be19
AK
3089
3090/* Only works with path name based fid */
8440e22e
GK
3091static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
3092 int32_t newdirfid,
3093 V9fsString *name)
8cf89e00 3094{
c7b4b0b3 3095 int err = 0;
2289be19
AK
3096 V9fsPath new_path;
3097 V9fsFidState *tfidp;
bccacf6c 3098 V9fsState *s = pdu->s;
84dfb926 3099 V9fsFidState *dirfidp = NULL;
8cf89e00 3100
2289be19 3101 v9fs_path_init(&new_path);
930b1e17 3102 if (newdirfid != -1) {
bccacf6c 3103 dirfidp = get_fid(pdu, newdirfid);
c7b4b0b3 3104 if (dirfidp == NULL) {
b858e80a 3105 return -ENOENT;
c7b4b0b3 3106 }
49dd946b
GK
3107 if (fidp->fid_type != P9_FID_NONE) {
3108 err = -EINVAL;
3109 goto out;
3110 }
4fa62005
GK
3111 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
3112 if (err < 0) {
3113 goto out;
3114 }
c7b4b0b3 3115 } else {
4d8bc733
JD
3116 char *dir_name = g_path_get_dirname(fidp->path.data);
3117 V9fsPath dir_path;
3118
3119 v9fs_path_init(&dir_path);
3120 v9fs_path_sprintf(&dir_path, "%s", dir_name);
3121 g_free(dir_name);
3122
3123 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
3124 v9fs_path_free(&dir_path);
4fa62005
GK
3125 if (err < 0) {
3126 goto out;
3127 }
c7b4b0b3 3128 }
bccacf6c 3129 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2289be19
AK
3130 if (err < 0) {
3131 goto out;
3132 }
3133 /*
3134 * Fixup fid's pointing to the old name to
3135 * start pointing to the new name
3136 */
feabd6cf 3137 QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) {
2289be19
AK
3138 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
3139 /* replace the name */
3140 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
8cf89e00
AL
3141 }
3142 }
c7b4b0b3 3143out:
84dfb926 3144 if (dirfidp) {
bccacf6c 3145 put_fid(pdu, dirfidp);
84dfb926 3146 }
2289be19 3147 v9fs_path_free(&new_path);
c7b4b0b3
MK
3148 return err;
3149}
3150
532decb7 3151/* Only works with path name based fid */
8440e22e 3152static void coroutine_fn v9fs_rename(void *opaque)
c7b4b0b3
MK
3153{
3154 int32_t fid;
c7b4b0b3 3155 ssize_t err = 0;
930b1e17
AK
3156 size_t offset = 7;
3157 V9fsString name;
3158 int32_t newdirfid;
3159 V9fsFidState *fidp;
3160 V9fsPDU *pdu = opaque;
3161 V9fsState *s = pdu->s;
c7b4b0b3 3162
ddca7f86
MK
3163 v9fs_string_init(&name);
3164 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
3165 if (err < 0) {
3166 goto out_nofid;
3167 }
fff39a7a
GK
3168
3169 if (name_is_illegal(name.data)) {
3170 err = -ENOENT;
3171 goto out_nofid;
3172 }
3173
805b5d98
GK
3174 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3175 err = -EISDIR;
3176 goto out_nofid;
3177 }
3178
bccacf6c 3179 fidp = get_fid(pdu, fid);
930b1e17 3180 if (fidp == NULL) {
c7b4b0b3 3181 err = -ENOENT;
84dfb926 3182 goto out_nofid;
c7b4b0b3 3183 }
49dd946b
GK
3184 if (fidp->fid_type != P9_FID_NONE) {
3185 err = -EINVAL;
3186 goto out;
3187 }
532decb7 3188 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 3189 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
3190 err = -EOPNOTSUPP;
3191 goto out;
3192 }
3193 v9fs_path_write_lock(s);
bccacf6c 3194 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
532decb7 3195 v9fs_path_unlock(s);
930b1e17
AK
3196 if (!err) {
3197 err = offset;
3198 }
532decb7 3199out:
bccacf6c 3200 put_fid(pdu, fidp);
84dfb926 3201out_nofid:
dc295f83 3202 pdu_complete(pdu, err);
930b1e17 3203 v9fs_string_free(&name);
c7b4b0b3
MK
3204}
3205
4fa62005
GK
3206static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
3207 V9fsString *old_name,
3208 V9fsPath *newdir,
3209 V9fsString *new_name)
2289be19
AK
3210{
3211 V9fsFidState *tfidp;
3212 V9fsPath oldpath, newpath;
bccacf6c 3213 V9fsState *s = pdu->s;
4fa62005 3214 int err;
2289be19
AK
3215
3216 v9fs_path_init(&oldpath);
3217 v9fs_path_init(&newpath);
4fa62005
GK
3218 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
3219 if (err < 0) {
3220 goto out;
3221 }
3222 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
3223 if (err < 0) {
3224 goto out;
3225 }
2289be19
AK
3226
3227 /*
3228 * Fixup fid's pointing to the old name to
3229 * start pointing to the new name
3230 */
feabd6cf 3231 QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) {
2289be19
AK
3232 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
3233 /* replace the name */
3234 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
3235 }
3236 }
4fa62005 3237out:
2289be19
AK
3238 v9fs_path_free(&oldpath);
3239 v9fs_path_free(&newpath);
4fa62005 3240 return err;
2289be19
AK
3241}
3242
8440e22e
GK
3243static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
3244 V9fsString *old_name,
3245 int32_t newdirfid,
3246 V9fsString *new_name)
89bf6593
AK
3247{
3248 int err = 0;
bccacf6c 3249 V9fsState *s = pdu->s;
89bf6593
AK
3250 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
3251
bccacf6c 3252 olddirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3253 if (olddirfidp == NULL) {
3254 err = -ENOENT;
3255 goto out;
3256 }
89bf6593 3257 if (newdirfid != -1) {
bccacf6c 3258 newdirfidp = get_fid(pdu, newdirfid);
89bf6593
AK
3259 if (newdirfidp == NULL) {
3260 err = -ENOENT;
3261 goto out;
3262 }
89bf6593 3263 } else {
bccacf6c 3264 newdirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3265 }
3266
bccacf6c 3267 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2289be19
AK
3268 &newdirfidp->path, new_name);
3269 if (err < 0) {
3270 goto out;
89bf6593 3271 }
c98f1d4a 3272 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
532decb7 3273 /* Only for path based fid we need to do the below fixup */
4fa62005
GK
3274 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
3275 &newdirfidp->path, new_name);
532decb7 3276 }
89bf6593
AK
3277out:
3278 if (olddirfidp) {
bccacf6c 3279 put_fid(pdu, olddirfidp);
89bf6593
AK
3280 }
3281 if (newdirfidp) {
bccacf6c 3282 put_fid(pdu, newdirfidp);
89bf6593 3283 }
89bf6593
AK
3284 return err;
3285}
3286
8440e22e 3287static void coroutine_fn v9fs_renameat(void *opaque)
89bf6593
AK
3288{
3289 ssize_t err = 0;
3290 size_t offset = 7;
3291 V9fsPDU *pdu = opaque;
3292 V9fsState *s = pdu->s;
3293 int32_t olddirfid, newdirfid;
3294 V9fsString old_name, new_name;
3295
ddca7f86
MK
3296 v9fs_string_init(&old_name);
3297 v9fs_string_init(&new_name);
3298 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
3299 &old_name, &newdirfid, &new_name);
3300 if (err < 0) {
3301 goto out_err;
3302 }
89bf6593 3303
fff39a7a
GK
3304 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3305 err = -ENOENT;
3306 goto out_err;
3307 }
3308
805b5d98
GK
3309 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3310 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3311 err = -EISDIR;
3312 goto out_err;
3313 }
3314
532decb7 3315 v9fs_path_write_lock(s);
bccacf6c
AK
3316 err = v9fs_complete_renameat(pdu, olddirfid,
3317 &old_name, newdirfid, &new_name);
532decb7 3318 v9fs_path_unlock(s);
89bf6593
AK
3319 if (!err) {
3320 err = offset;
3321 }
ddca7f86
MK
3322
3323out_err:
dc295f83 3324 pdu_complete(pdu, err);
89bf6593
AK
3325 v9fs_string_free(&old_name);
3326 v9fs_string_free(&new_name);
3327}
3328
8440e22e 3329static void coroutine_fn v9fs_wstat(void *opaque)
8cf89e00 3330{
b81d685e
AK
3331 int32_t fid;
3332 int err = 0;
3333 int16_t unused;
3334 V9fsStat v9stat;
3335 size_t offset = 7;
3336 struct stat stbuf;
3337 V9fsFidState *fidp;
3338 V9fsPDU *pdu = opaque;
1d203986 3339 V9fsState *s = pdu->s;
8cf89e00 3340
ddca7f86
MK
3341 v9fs_stat_init(&v9stat);
3342 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3343 if (err < 0) {
3344 goto out_nofid;
3345 }
c572f23a
HPB
3346 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3347 v9stat.mode, v9stat.atime, v9stat.mtime);
84dfb926 3348
bccacf6c 3349 fidp = get_fid(pdu, fid);
b81d685e
AK
3350 if (fidp == NULL) {
3351 err = -EINVAL;
84dfb926 3352 goto out_nofid;
8cf89e00 3353 }
b81d685e
AK
3354 /* do we need to sync the file? */
3355 if (donttouch_stat(&v9stat)) {
bccacf6c 3356 err = v9fs_co_fsync(pdu, fidp, 0);
8cf89e00
AL
3357 goto out;
3358 }
b81d685e
AK
3359 if (v9stat.mode != -1) {
3360 uint32_t v9_mode;
bccacf6c 3361 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
b81d685e
AK
3362 if (err < 0) {
3363 goto out;
3364 }
3365 v9_mode = stat_to_v9mode(&stbuf);
3366 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3367 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3368 /* Attempting to change the type */
3369 err = -EIO;
3370 goto out;
3371 }
bccacf6c 3372 err = v9fs_co_chmod(pdu, &fidp->path,
b81d685e
AK
3373 v9mode_to_mode(v9stat.mode,
3374 &v9stat.extension));
3375 if (err < 0) {
3376 goto out;
3377 }
3378 }
3379 if (v9stat.mtime != -1 || v9stat.atime != -1) {
8fc39ae4 3380 struct timespec times[2];
b81d685e
AK
3381 if (v9stat.atime != -1) {
3382 times[0].tv_sec = v9stat.atime;
8fc39ae4
SK
3383 times[0].tv_nsec = 0;
3384 } else {
3385 times[0].tv_nsec = UTIME_OMIT;
3386 }
b81d685e
AK
3387 if (v9stat.mtime != -1) {
3388 times[1].tv_sec = v9stat.mtime;
8fc39ae4
SK
3389 times[1].tv_nsec = 0;
3390 } else {
3391 times[1].tv_nsec = UTIME_OMIT;
3392 }
bccacf6c 3393 err = v9fs_co_utimensat(pdu, &fidp->path, times);
b81d685e
AK
3394 if (err < 0) {
3395 goto out;
8cf89e00
AL
3396 }
3397 }
b81d685e 3398 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
bccacf6c 3399 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
b81d685e 3400 if (err < 0) {
8cf89e00 3401 goto out;
b81d685e 3402 }
8cf89e00 3403 }
b81d685e 3404 if (v9stat.name.size != 0) {
1d203986 3405 v9fs_path_write_lock(s);
bccacf6c 3406 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
1d203986 3407 v9fs_path_unlock(s);
b81d685e
AK
3408 if (err < 0) {
3409 goto out;
3410 }
8cf89e00 3411 }
b81d685e 3412 if (v9stat.length != -1) {
bccacf6c 3413 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
b81d685e
AK
3414 if (err < 0) {
3415 goto out;
3416 }
8cf89e00 3417 }
b81d685e 3418 err = offset;
8cf89e00 3419out:
bccacf6c 3420 put_fid(pdu, fidp);
84dfb926 3421out_nofid:
b81d685e 3422 v9fs_stat_free(&v9stat);
dc295f83 3423 pdu_complete(pdu, err);
9f107513
AL
3424}
3425
88a4763e
AK
3426static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3427{
3428 uint32_t f_type;
3429 uint32_t f_bsize;
3430 uint64_t f_blocks;
3431 uint64_t f_bfree;
3432 uint64_t f_bavail;
3433 uint64_t f_files;
3434 uint64_t f_ffree;
3435 uint64_t fsid_val;
3436 uint32_t f_namelen;
3437 size_t offset = 7;
5e94c103
MK
3438 int32_t bsize_factor;
3439
5e94c103
MK
3440 /*
3441 * compute bsize factor based on host file system block size
3442 * and client msize
3443 */
01011733 3444 bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize;
5e94c103
MK
3445 if (!bsize_factor) {
3446 bsize_factor = 1;
3447 }
88a4763e
AK
3448 f_type = stbuf->f_type;
3449 f_bsize = stbuf->f_bsize;
3450 f_bsize *= bsize_factor;
5e94c103
MK
3451 /*
3452 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3453 * adjust(divide) the number of blocks, free blocks and available
3454 * blocks by bsize factor
3455 */
01011733
XZ
3456 f_blocks = stbuf->f_blocks / bsize_factor;
3457 f_bfree = stbuf->f_bfree / bsize_factor;
3458 f_bavail = stbuf->f_bavail / bsize_factor;
88a4763e
AK
3459 f_files = stbuf->f_files;
3460 f_ffree = stbuf->f_ffree;
3461 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3462 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3463 f_namelen = stbuf->f_namelen;
be940c87 3464
88a4763e
AK
3465 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3466 f_type, f_bsize, f_blocks, f_bfree,
3467 f_bavail, f_files, f_ffree,
3468 fsid_val, f_namelen);
be940c87
MK
3469}
3470
8440e22e 3471static void coroutine_fn v9fs_statfs(void *opaque)
be940c87 3472{
88a4763e
AK
3473 int32_t fid;
3474 ssize_t retval = 0;
3475 size_t offset = 7;
3476 V9fsFidState *fidp;
3477 struct statfs stbuf;
ff06030f
VJ
3478 V9fsPDU *pdu = opaque;
3479 V9fsState *s = pdu->s;
be940c87 3480
ddca7f86
MK
3481 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3482 if (retval < 0) {
3483 goto out_nofid;
3484 }
bccacf6c 3485 fidp = get_fid(pdu, fid);
88a4763e
AK
3486 if (fidp == NULL) {
3487 retval = -ENOENT;
84dfb926 3488 goto out_nofid;
be940c87 3489 }
bccacf6c 3490 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
88a4763e
AK
3491 if (retval < 0) {
3492 goto out;
3493 }
ddca7f86
MK
3494 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3495 if (retval < 0) {
3496 goto out;
3497 }
3498 retval += offset;
be940c87 3499out:
bccacf6c 3500 put_fid(pdu, fidp);
84dfb926 3501out_nofid:
dc295f83 3502 pdu_complete(pdu, retval);
be940c87
MK
3503}
3504
8440e22e 3505static void coroutine_fn v9fs_mknod(void *opaque)
5268cecc 3506{
1b733fed
AK
3507
3508 int mode;
3509 gid_t gid;
5268cecc 3510 int32_t fid;
1b733fed 3511 V9fsQID qid;
5268cecc 3512 int err = 0;
5268cecc 3513 int major, minor;
1b733fed
AK
3514 size_t offset = 7;
3515 V9fsString name;
3516 struct stat stbuf;
1b733fed
AK
3517 V9fsFidState *fidp;
3518 V9fsPDU *pdu = opaque;
5268cecc 3519
ddca7f86
MK
3520 v9fs_string_init(&name);
3521 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3522 &major, &minor, &gid);
3523 if (err < 0) {
3524 goto out_nofid;
3525 }
c572f23a 3526 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
5268cecc 3527
fff39a7a
GK
3528 if (name_is_illegal(name.data)) {
3529 err = -ENOENT;
3530 goto out_nofid;
3531 }
3532
805b5d98
GK
3533 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3534 err = -EEXIST;
3535 goto out_nofid;
3536 }
3537
bccacf6c 3538 fidp = get_fid(pdu, fid);
5268cecc
MK
3539 if (fidp == NULL) {
3540 err = -ENOENT;
84dfb926 3541 goto out_nofid;
5268cecc 3542 }
bccacf6c 3543 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
02cb7f3a 3544 makedev(major, minor), mode, &stbuf);
1b733fed
AK
3545 if (err < 0) {
3546 goto out;
3547 }
3b5ee9e8
AM
3548 err = stat_to_qid(pdu, &stbuf, &qid);
3549 if (err < 0) {
3550 goto out;
3551 }
ddca7f86
MK
3552 err = pdu_marshal(pdu, offset, "Q", &qid);
3553 if (err < 0) {
3554 goto out;
3555 }
3556 err += offset;
7999f7e1
AK
3557 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3558 qid.type, qid.version, qid.path);
5268cecc 3559out:
bccacf6c 3560 put_fid(pdu, fidp);
84dfb926 3561out_nofid:
dc295f83 3562 pdu_complete(pdu, err);
1b733fed 3563 v9fs_string_free(&name);
5268cecc
MK
3564}
3565
82cc3ee8
MK
3566/*
3567 * Implement posix byte range locking code
3568 * Server side handling of locking code is very simple, because 9p server in
3569 * QEMU can handle only one client. And most of the lock handling
3570 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3571 * do any thing in * qemu 9p server side lock code path.
3572 * So when a TLOCK request comes, always return success
3573 */
8440e22e 3574static void coroutine_fn v9fs_lock(void *opaque)
82cc3ee8 3575{
ddca7f86 3576 V9fsFlock flock;
0c27bf2a
AK
3577 size_t offset = 7;
3578 struct stat stbuf;
3579 V9fsFidState *fidp;
3580 int32_t fid, err = 0;
ff06030f 3581 V9fsPDU *pdu = opaque;
82cc3ee8 3582
ddca7f86
MK
3583 v9fs_string_init(&flock.client_id);
3584 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3585 &flock.flags, &flock.start, &flock.length,
3586 &flock.proc_id, &flock.client_id);
3587 if (err < 0) {
3588 goto out_nofid;
3589 }
c572f23a 3590 trace_v9fs_lock(pdu->tag, pdu->id, fid,
ddca7f86 3591 flock.type, flock.start, flock.length);
c572f23a 3592
82cc3ee8
MK
3593
3594 /* We support only block flag now (that too ignored currently) */
ddca7f86 3595 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
82cc3ee8 3596 err = -EINVAL;
84dfb926 3597 goto out_nofid;
82cc3ee8 3598 }
bccacf6c 3599 fidp = get_fid(pdu, fid);
0c27bf2a 3600 if (fidp == NULL) {
82cc3ee8 3601 err = -ENOENT;
84dfb926 3602 goto out_nofid;
82cc3ee8 3603 }
cc720ddb 3604 err = v9fs_co_fstat(pdu, fidp, &stbuf);
82cc3ee8 3605 if (err < 0) {
82cc3ee8
MK
3606 goto out;
3607 }
4bae2b39
PB
3608 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3609 if (err < 0) {
3610 goto out;
3611 }
3612 err += offset;
3613 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
82cc3ee8 3614out:
bccacf6c 3615 put_fid(pdu, fidp);
84dfb926 3616out_nofid:
dc295f83 3617 pdu_complete(pdu, err);
ddca7f86 3618 v9fs_string_free(&flock.client_id);
82cc3ee8
MK
3619}
3620
8f354003
MK
3621/*
3622 * When a TGETLOCK request comes, always return success because all lock
3623 * handling is done by client's VFS layer.
3624 */
8440e22e 3625static void coroutine_fn v9fs_getlock(void *opaque)
8f354003 3626{
e4e414a4
AK
3627 size_t offset = 7;
3628 struct stat stbuf;
3629 V9fsFidState *fidp;
ddca7f86 3630 V9fsGetlock glock;
e4e414a4 3631 int32_t fid, err = 0;
ff06030f 3632 V9fsPDU *pdu = opaque;
8f354003 3633
ddca7f86
MK
3634 v9fs_string_init(&glock.client_id);
3635 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3636 &glock.start, &glock.length, &glock.proc_id,
3637 &glock.client_id);
3638 if (err < 0) {
3639 goto out_nofid;
3640 }
c572f23a 3641 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
ddca7f86 3642 glock.type, glock.start, glock.length);
c572f23a 3643
bccacf6c 3644 fidp = get_fid(pdu, fid);
e4e414a4 3645 if (fidp == NULL) {
8f354003 3646 err = -ENOENT;
84dfb926 3647 goto out_nofid;
8f354003 3648 }
cc720ddb 3649 err = v9fs_co_fstat(pdu, fidp, &stbuf);
8f354003 3650 if (err < 0) {
8f354003
MK
3651 goto out;
3652 }
ddca7f86
MK
3653 glock.type = P9_LOCK_TYPE_UNLCK;
3654 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3655 glock.start, glock.length, glock.proc_id,
3656 &glock.client_id);
3657 if (err < 0) {
3658 goto out;
3659 }
3660 err += offset;
3661 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3662 glock.length, glock.proc_id);
8f354003 3663out:
bccacf6c 3664 put_fid(pdu, fidp);
84dfb926 3665out_nofid:
dc295f83 3666 pdu_complete(pdu, err);
ddca7f86 3667 v9fs_string_free(&glock.client_id);
8f354003
MK
3668}
3669
8440e22e 3670static void coroutine_fn v9fs_mkdir(void *opaque)
b67592ea 3671{
ff06030f 3672 V9fsPDU *pdu = opaque;
e84861f7 3673 size_t offset = 7;
b67592ea 3674 int32_t fid;
e84861f7 3675 struct stat stbuf;
e84861f7 3676 V9fsQID qid;
02cb7f3a 3677 V9fsString name;
b67592ea
MK
3678 V9fsFidState *fidp;
3679 gid_t gid;
3680 int mode;
e84861f7 3681 int err = 0;
b67592ea 3682
ddca7f86
MK
3683 v9fs_string_init(&name);
3684 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3685 if (err < 0) {
3686 goto out_nofid;
3687 }
c572f23a
HPB
3688 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3689
fff39a7a
GK
3690 if (name_is_illegal(name.data)) {
3691 err = -ENOENT;
3692 goto out_nofid;
3693 }
3694
805b5d98
GK
3695 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3696 err = -EEXIST;
3697 goto out_nofid;
3698 }
3699
bccacf6c 3700 fidp = get_fid(pdu, fid);
b67592ea
MK
3701 if (fidp == NULL) {
3702 err = -ENOENT;
84dfb926 3703 goto out_nofid;
b67592ea 3704 }
bccacf6c 3705 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
e84861f7
VJ
3706 if (err < 0) {
3707 goto out;
3708 }
3b5ee9e8
AM
3709 err = stat_to_qid(pdu, &stbuf, &qid);
3710 if (err < 0) {
3711 goto out;
3712 }
ddca7f86
MK
3713 err = pdu_marshal(pdu, offset, "Q", &qid);
3714 if (err < 0) {
3715 goto out;
3716 }
3717 err += offset;
7999f7e1
AK
3718 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3719 qid.type, qid.version, qid.path, err);
b67592ea 3720out:
bccacf6c 3721 put_fid(pdu, fidp);
84dfb926 3722out_nofid:
dc295f83 3723 pdu_complete(pdu, err);
e84861f7 3724 v9fs_string_free(&name);
b67592ea
MK
3725}
3726
8440e22e 3727static void coroutine_fn v9fs_xattrwalk(void *opaque)
fa32ef88 3728{
670185a6
AK
3729 int64_t size;
3730 V9fsString name;
fa32ef88 3731 ssize_t err = 0;
670185a6 3732 size_t offset = 7;
fa32ef88 3733 int32_t fid, newfid;
670185a6 3734 V9fsFidState *file_fidp;
84dfb926 3735 V9fsFidState *xattr_fidp = NULL;
670185a6
AK
3736 V9fsPDU *pdu = opaque;
3737 V9fsState *s = pdu->s;
fa32ef88 3738
ddca7f86
MK
3739 v9fs_string_init(&name);
3740 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3741 if (err < 0) {
3742 goto out_nofid;
3743 }
c572f23a
HPB
3744 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3745
bccacf6c 3746 file_fidp = get_fid(pdu, fid);
670185a6 3747 if (file_fidp == NULL) {
fa32ef88 3748 err = -ENOENT;
84dfb926 3749 goto out_nofid;
fa32ef88 3750 }
670185a6
AK
3751 xattr_fidp = alloc_fid(s, newfid);
3752 if (xattr_fidp == NULL) {
fa32ef88
AK
3753 err = -EINVAL;
3754 goto out;
3755 }
2289be19 3756 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
ba42ebb8 3757 if (!v9fs_string_size(&name)) {
fa32ef88
AK
3758 /*
3759 * listxattr request. Get the size first
3760 */
bccacf6c 3761 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
670185a6
AK
3762 if (size < 0) {
3763 err = size;
84dfb926 3764 clunk_fid(s, xattr_fidp->fid);
670185a6 3765 goto out;
fa32ef88 3766 }
670185a6
AK
3767 /*
3768 * Read the xattr value
3769 */
3770 xattr_fidp->fs.xattr.len = size;
3771 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3772 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3773 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3774 if (size) {
bccacf6c 3775 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
670185a6
AK
3776 xattr_fidp->fs.xattr.value,
3777 xattr_fidp->fs.xattr.len);
3778 if (err < 0) {
84dfb926 3779 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3780 goto out;
3781 }
3782 }
ddca7f86
MK
3783 err = pdu_marshal(pdu, offset, "q", size);
3784 if (err < 0) {
3785 goto out;
3786 }
3787 err += offset;
fa32ef88
AK
3788 } else {
3789 /*
3790 * specific xattr fid. We check for xattr
3791 * presence also collect the xattr size
3792 */
bccacf6c 3793 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3794 &name, NULL, 0);
3795 if (size < 0) {
3796 err = size;
84dfb926 3797 clunk_fid(s, xattr_fidp->fid);
670185a6 3798 goto out;
fa32ef88 3799 }
670185a6
AK
3800 /*
3801 * Read the xattr value
3802 */
3803 xattr_fidp->fs.xattr.len = size;
3804 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3805 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3806 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3807 if (size) {
bccacf6c 3808 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3809 &name, xattr_fidp->fs.xattr.value,
3810 xattr_fidp->fs.xattr.len);
3811 if (err < 0) {
84dfb926 3812 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3813 goto out;
3814 }
3815 }
ddca7f86
MK
3816 err = pdu_marshal(pdu, offset, "q", size);
3817 if (err < 0) {
3818 goto out;
3819 }
3820 err += offset;
fa32ef88 3821 }
7999f7e1 3822 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
fa32ef88 3823out:
bccacf6c 3824 put_fid(pdu, file_fidp);
84dfb926 3825 if (xattr_fidp) {
bccacf6c 3826 put_fid(pdu, xattr_fidp);
84dfb926
AK
3827 }
3828out_nofid:
dc295f83 3829 pdu_complete(pdu, err);
670185a6 3830 v9fs_string_free(&name);
fa32ef88
AK
3831}
3832
8440e22e 3833static void coroutine_fn v9fs_xattrcreate(void *opaque)
10b468bd 3834{
aca6897f 3835 int flags, rflags = 0;
10b468bd 3836 int32_t fid;
3b79ef2c 3837 uint64_t size;
10b468bd 3838 ssize_t err = 0;
f10ff58d
AK
3839 V9fsString name;
3840 size_t offset = 7;
3841 V9fsFidState *file_fidp;
3842 V9fsFidState *xattr_fidp;
3843 V9fsPDU *pdu = opaque;
10b468bd 3844
ddca7f86
MK
3845 v9fs_string_init(&name);
3846 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3847 if (err < 0) {
3848 goto out_nofid;
3849 }
c572f23a 3850 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
10b468bd 3851
aca6897f
KF
3852 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3853 err = -EINVAL;
3854 goto out_nofid;
3855 }
3856
3857 if (flags & P9_XATTR_CREATE) {
3858 rflags |= XATTR_CREATE;
3859 }
3860
3861 if (flags & P9_XATTR_REPLACE) {
3862 rflags |= XATTR_REPLACE;
3863 }
3864
3b79ef2c
GK
3865 if (size > XATTR_SIZE_MAX) {
3866 err = -E2BIG;
3867 goto out_nofid;
3868 }
3869
bccacf6c 3870 file_fidp = get_fid(pdu, fid);
f10ff58d 3871 if (file_fidp == NULL) {
10b468bd 3872 err = -EINVAL;
84dfb926 3873 goto out_nofid;
10b468bd 3874 }
dd654e03
GK
3875 if (file_fidp->fid_type != P9_FID_NONE) {
3876 err = -EINVAL;
3877 goto out_put_fid;
3878 }
3879
10b468bd 3880 /* Make the file fid point to xattr */
f10ff58d
AK
3881 xattr_fidp = file_fidp;
3882 xattr_fidp->fid_type = P9_FID_XATTR;
3883 xattr_fidp->fs.xattr.copied_len = 0;
dd28fbbc 3884 xattr_fidp->fs.xattr.xattrwalk_fid = false;
f10ff58d 3885 xattr_fidp->fs.xattr.len = size;
aca6897f 3886 xattr_fidp->fs.xattr.flags = rflags;
f10ff58d
AK
3887 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3888 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
eb687602 3889 xattr_fidp->fs.xattr.value = g_malloc0(size);
f10ff58d 3890 err = offset;
dd654e03 3891out_put_fid:
bccacf6c 3892 put_fid(pdu, file_fidp);
84dfb926 3893out_nofid:
dc295f83 3894 pdu_complete(pdu, err);
f10ff58d 3895 v9fs_string_free(&name);
10b468bd 3896}
fa32ef88 3897
8440e22e 3898static void coroutine_fn v9fs_readlink(void *opaque)
df0973a4 3899{
ff06030f 3900 V9fsPDU *pdu = opaque;
7a5ca31e
VJ
3901 size_t offset = 7;
3902 V9fsString target;
df0973a4 3903 int32_t fid;
df0973a4
MK
3904 int err = 0;
3905 V9fsFidState *fidp;
3906
ddca7f86
MK
3907 err = pdu_unmarshal(pdu, offset, "d", &fid);
3908 if (err < 0) {
3909 goto out_nofid;
3910 }
c572f23a 3911 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
bccacf6c 3912 fidp = get_fid(pdu, fid);
df0973a4
MK
3913 if (fidp == NULL) {
3914 err = -ENOENT;
84dfb926 3915 goto out_nofid;
df0973a4
MK
3916 }
3917
7a5ca31e 3918 v9fs_string_init(&target);
bccacf6c 3919 err = v9fs_co_readlink(pdu, &fidp->path, &target);
7a5ca31e
VJ
3920 if (err < 0) {
3921 goto out;
3922 }
ddca7f86
MK
3923 err = pdu_marshal(pdu, offset, "s", &target);
3924 if (err < 0) {
3925 v9fs_string_free(&target);
3926 goto out;
3927 }
3928 err += offset;
7999f7e1 3929 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
7a5ca31e 3930 v9fs_string_free(&target);
df0973a4 3931out:
bccacf6c 3932 put_fid(pdu, fidp);
84dfb926 3933out_nofid:
dc295f83 3934 pdu_complete(pdu, err);
df0973a4
MK
3935}
3936
ff06030f 3937static CoroutineEntry *pdu_co_handlers[] = {
c18e2f94 3938 [P9_TREADDIR] = v9fs_readdir,
be940c87 3939 [P9_TSTATFS] = v9fs_statfs,
00ede4c2 3940 [P9_TGETATTR] = v9fs_getattr,
c79ce737 3941 [P9_TSETATTR] = v9fs_setattr,
fa32ef88 3942 [P9_TXATTRWALK] = v9fs_xattrwalk,
10b468bd 3943 [P9_TXATTRCREATE] = v9fs_xattrcreate,
5268cecc 3944 [P9_TMKNOD] = v9fs_mknod,
c7b4b0b3 3945 [P9_TRENAME] = v9fs_rename,
82cc3ee8 3946 [P9_TLOCK] = v9fs_lock,
8f354003 3947 [P9_TGETLOCK] = v9fs_getlock,
89bf6593 3948 [P9_TRENAMEAT] = v9fs_renameat,
df0973a4 3949 [P9_TREADLINK] = v9fs_readlink,
7834cf77 3950 [P9_TUNLINKAT] = v9fs_unlinkat,
b67592ea 3951 [P9_TMKDIR] = v9fs_mkdir,
9f107513 3952 [P9_TVERSION] = v9fs_version,
771e9d4c 3953 [P9_TLOPEN] = v9fs_open,
9f107513
AL
3954 [P9_TATTACH] = v9fs_attach,
3955 [P9_TSTAT] = v9fs_stat,
3956 [P9_TWALK] = v9fs_walk,
3957 [P9_TCLUNK] = v9fs_clunk,
b41e95d3 3958 [P9_TFSYNC] = v9fs_fsync,
9f107513
AL
3959 [P9_TOPEN] = v9fs_open,
3960 [P9_TREAD] = v9fs_read,
3961#if 0
3962 [P9_TAUTH] = v9fs_auth,
3963#endif
3964 [P9_TFLUSH] = v9fs_flush,
b2c224be 3965 [P9_TLINK] = v9fs_link,
08c60fc9 3966 [P9_TSYMLINK] = v9fs_symlink,
9f107513 3967 [P9_TCREATE] = v9fs_create,
c1568af5 3968 [P9_TLCREATE] = v9fs_lcreate,
9f107513
AL
3969 [P9_TWRITE] = v9fs_write,
3970 [P9_TWSTAT] = v9fs_wstat,
3971 [P9_TREMOVE] = v9fs_remove,
3972};
3973
8440e22e 3974static void coroutine_fn v9fs_op_not_supp(void *opaque)
5c3234c6 3975{
ff06030f 3976 V9fsPDU *pdu = opaque;
dc295f83 3977 pdu_complete(pdu, -EOPNOTSUPP);
5c3234c6
AK
3978}
3979
8440e22e 3980static void coroutine_fn v9fs_fs_ro(void *opaque)
2c74c2cb
MK
3981{
3982 V9fsPDU *pdu = opaque;
dc295f83 3983 pdu_complete(pdu, -EROFS);
2c74c2cb
MK
3984}
3985
3986static inline bool is_read_only_op(V9fsPDU *pdu)
3987{
3988 switch (pdu->id) {
3989 case P9_TREADDIR:
3990 case P9_TSTATFS:
3991 case P9_TGETATTR:
3992 case P9_TXATTRWALK:
3993 case P9_TLOCK:
3994 case P9_TGETLOCK:
3995 case P9_TREADLINK:
3996 case P9_TVERSION:
3997 case P9_TLOPEN:
3998 case P9_TATTACH:
3999 case P9_TSTAT:
4000 case P9_TWALK:
4001 case P9_TCLUNK:
4002 case P9_TFSYNC:
4003 case P9_TOPEN:
4004 case P9_TREAD:
4005 case P9_TAUTH:
4006 case P9_TFLUSH:
4007 return 1;
4008 default:
4009 return 0;
4010 }
4011}
4012
506f3275 4013void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
9f107513 4014{
ff06030f
VJ
4015 Coroutine *co;
4016 CoroutineEntry *handler;
ad38ce9e 4017 V9fsState *s = pdu->s;
9f107513 4018
506f3275
GK
4019 pdu->size = le32_to_cpu(hdr->size_le);
4020 pdu->id = hdr->id;
4021 pdu->tag = le16_to_cpu(hdr->tag_le);
4022
ff06030f
VJ
4023 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
4024 (pdu_co_handlers[pdu->id] == NULL)) {
5c3234c6 4025 handler = v9fs_op_not_supp;
d1471233
GK
4026 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
4027 handler = v9fs_fs_ro;
5c3234c6 4028 } else {
ff06030f 4029 handler = pdu_co_handlers[pdu->id];
5c3234c6 4030 }
2c74c2cb 4031
506f3275 4032 qemu_co_queue_init(&pdu->complete);
0b8b8753
PB
4033 co = qemu_coroutine_create(handler, pdu);
4034 qemu_coroutine_enter(co);
9f107513
AL
4035}
4036
2a0c56aa 4037/* Returns 0 on success, 1 on failure. */
066eb006
GK
4038int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4039 Error **errp)
2a0c56aa 4040{
92c45122 4041 ERRP_GUARD();
2a0c56aa
WL
4042 int i, len;
4043 struct stat stat;
4044 FsDriverEntry *fse;
4045 V9fsPath path;
4046 int rc = 1;
4047
066eb006
GK
4048 assert(!s->transport);
4049 s->transport = t;
4050
2a0c56aa
WL
4051 /* initialize pdu allocator */
4052 QLIST_INIT(&s->free_list);
4053 QLIST_INIT(&s->active_list);
0d78289c 4054 for (i = 0; i < MAX_REQ; i++) {
583f21f8
SS
4055 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4056 s->pdus[i].s = s;
4057 s->pdus[i].idx = i;
2a0c56aa
WL
4058 }
4059
4060 v9fs_path_init(&path);
4061
4062 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
4063
4064 if (!fse) {
4065 /* We don't have a fsdev identified by fsdev_id */
4066 error_setg(errp, "9pfs device couldn't find fsdev with the "
4067 "id = %s",
4068 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
4069 goto out;
4070 }
4071
4072 if (!s->fsconf.tag) {
4073 /* we haven't specified a mount_tag */
4074 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
4075 s->fsconf.fsdev_id);
4076 goto out;
4077 }
4078
4079 s->ctx.export_flags = fse->export_flags;
4080 s->ctx.fs_root = g_strdup(fse->path);
4081 s->ctx.exops.get_st_gen = NULL;
4082 len = strlen(s->fsconf.tag);
4083 if (len > MAX_TAG_LEN - 1) {
4084 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
4085 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
4086 goto out;
4087 }
4088
4089 s->tag = g_strdup(s->fsconf.tag);
4090 s->ctx.uid = -1;
4091
4092 s->ops = fse->ops;
4093
b96feb2c
TS
4094 s->ctx.fmode = fse->fmode;
4095 s->ctx.dmode = fse->dmode;
4096
feabd6cf 4097 QSIMPLEQ_INIT(&s->fid_list);
2a0c56aa
WL
4098 qemu_co_rwlock_init(&s->rename_lock);
4099
65603a80
GK
4100 if (s->ops->init(&s->ctx, errp) < 0) {
4101 error_prepend(errp, "cannot initialize fsdev '%s': ",
4102 s->fsconf.fsdev_id);
2a0c56aa
WL
4103 goto out;
4104 }
4105
4106 /*
4107 * Check details of export path, We need to use fs driver
4108 * call back to do that. Since we are in the init path, we don't
4109 * use co-routines here.
4110 */
4111 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
4112 error_setg(errp,
4113 "error in converting name to path %s", strerror(errno));
4114 goto out;
4115 }
4116 if (s->ops->lstat(&s->ctx, &path, &stat)) {
4117 error_setg(errp, "share path %s does not exist", fse->path);
4118 goto out;
4119 } else if (!S_ISDIR(stat.st_mode)) {
4120 error_setg(errp, "share path %s is not a directory", fse->path);
4121 goto out;
4122 }
b8bbdb88 4123
3b5ee9e8
AM
4124 s->dev_id = stat.st_dev;
4125
6b6aa828
CS
4126 /* init inode remapping : */
4127 /* hash table for variable length inode suffixes */
4128 qpd_table_init(&s->qpd_table);
4129 /* hash table for slow/full inode remapping (most users won't need it) */
4130 qpf_table_init(&s->qpf_table);
4131 /* hash table for quick inode remapping */
1a6ed33c 4132 qpp_table_init(&s->qpp_table);
6b6aa828
CS
4133 s->qp_ndevices = 0;
4134 s->qp_affix_next = 1; /* reserve 0 to detect overflow */
f3fe4a2d 4135 s->qp_fullpath_next = 1;
1a6ed33c 4136
b8bbdb88
PJ
4137 s->ctx.fst = &fse->fst;
4138 fsdev_throttle_init(s->ctx.fst);
4139
2a0c56aa
WL
4140 rc = 0;
4141out:
4142 if (rc) {
b69c3c21 4143 v9fs_device_unrealize_common(s);
2a0c56aa 4144 }
c0da0cb7 4145 v9fs_path_free(&path);
2a0c56aa
WL
4146 return rc;
4147}
4148
b69c3c21 4149void v9fs_device_unrealize_common(V9fsState *s)
2a0c56aa 4150{
c0da0cb7 4151 if (s->ops && s->ops->cleanup) {
702dbcc2
LQ
4152 s->ops->cleanup(&s->ctx);
4153 }
c0da0cb7
GK
4154 if (s->ctx.fst) {
4155 fsdev_throttle_cleanup(s->ctx.fst);
4156 }
2a0c56aa 4157 g_free(s->tag);
6b6aa828 4158 qp_table_destroy(&s->qpd_table);
f3fe4a2d
AM
4159 qp_table_destroy(&s->qpp_table);
4160 qp_table_destroy(&s->qpf_table);
4774718e 4161 g_free(s->ctx.fs_root);
2a0c56aa
WL
4162}
4163
0e44a0fd
GK
4164typedef struct VirtfsCoResetData {
4165 V9fsPDU pdu;
4166 bool done;
4167} VirtfsCoResetData;
4168
4169static void coroutine_fn virtfs_co_reset(void *opaque)
4170{
4171 VirtfsCoResetData *data = opaque;
4172
4173 virtfs_reset(&data->pdu);
4174 data->done = true;
4175}
4176
4177void v9fs_reset(V9fsState *s)
4178{
4179 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
4180 Coroutine *co;
4181
4182 while (!QLIST_EMPTY(&s->active_list)) {
4183 aio_poll(qemu_get_aio_context(), true);
4184 }
4185
4186 co = qemu_coroutine_create(virtfs_co_reset, &data);
4187 qemu_coroutine_enter(co);
4188
4189 while (!data.done) {
4190 aio_poll(qemu_get_aio_context(), true);
4191 }
4192}
4193
72a18977 4194static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
7a462745
AK
4195{
4196 struct rlimit rlim;
4197 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
63325b18 4198 error_report("Failed to get the resource limit");
7a462745
AK
4199 exit(1);
4200 }
01011733
XZ
4201 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3);
4202 open_fd_rc = rlim.rlim_cur / 2;
7a462745 4203}