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