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
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/iov.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
24 #include "9p-xattr.h"
25 #include "coth.h"
26 #include "trace.h"
27 #include "migration/blocker.h"
28 #include "sysemu/qtest.h"
29 #include "qemu/xxhash.h"
30 #include <math.h>
31 #include <linux/limits.h>
32
33 int open_fd_hw;
34 int total_open_fd;
35 static int open_fd_rc;
36
37 enum {
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
pdu_marshal(V9fsPDU * pdu,size_t offset,const char * fmt,...)49 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
50 {
51 ssize_t ret;
52 va_list ap;
53
54 va_start(ap, fmt);
55 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
56 va_end(ap);
57
58 return ret;
59 }
60
pdu_unmarshal(V9fsPDU * pdu,size_t offset,const char * fmt,...)61 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
62 {
63 ssize_t ret;
64 va_list ap;
65
66 va_start(ap, fmt);
67 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
68 va_end(ap);
69
70 return ret;
71 }
72
omode_to_uflags(int8_t mode)73 static 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
107 typedef struct DotlOpenflagMap {
108 int dotl_flag;
109 int open_flag;
110 } DotlOpenflagMap;
111
dotl_to_open_flags(int flags)112 static 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
121 DotlOpenflagMap dotl_oflag_map[] = {
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
cred_init(FsCred * credp)147 void cred_init(FsCred *credp)
148 {
149 credp->fc_uid = -1;
150 credp->fc_gid = -1;
151 credp->fc_mode = -1;
152 credp->fc_rdev = -1;
153 }
154
get_dotl_openflags(V9fsState * s,int oflags)155 static int get_dotl_openflags(V9fsState *s, int oflags)
156 {
157 int flags;
158 /*
159 * Filter the client open flags
160 */
161 flags = dotl_to_open_flags(oflags);
162 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
163 /*
164 * Ignore direct disk access hint until the server supports it.
165 */
166 flags &= ~O_DIRECT;
167 return flags;
168 }
169
v9fs_path_init(V9fsPath * path)170 void v9fs_path_init(V9fsPath *path)
171 {
172 path->data = NULL;
173 path->size = 0;
174 }
175
v9fs_path_free(V9fsPath * path)176 void v9fs_path_free(V9fsPath *path)
177 {
178 g_free(path->data);
179 path->data = NULL;
180 path->size = 0;
181 }
182
183
184 void GCC_FMT_ATTR(2, 3)
v9fs_path_sprintf(V9fsPath * path,const char * fmt,...)185 v9fs_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
v9fs_path_copy(V9fsPath * dst,const V9fsPath * src)197 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
198 {
199 v9fs_path_free(dst);
200 dst->size = src->size;
201 dst->data = g_memdup(src->data, src->size);
202 }
203
v9fs_name_to_path(V9fsState * s,V9fsPath * dirpath,const char * name,V9fsPath * path)204 int 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
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 */
v9fs_path_is_ancestor(V9fsPath * s1,V9fsPath * s2)221 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
222 {
223 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
224 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
225 return 1;
226 }
227 }
228 return 0;
229 }
230
v9fs_string_size(V9fsString * str)231 static size_t v9fs_string_size(V9fsString *str)
232 {
233 return str->size;
234 }
235
236 /*
237 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
v9fs_reopen_fid(V9fsPDU * pdu,V9fsFidState * f)238 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
239 {
240 int err = 1;
241 if (f->fid_type == P9_FID_FILE) {
242 if (f->fs.fd == -1) {
243 do {
244 err = v9fs_co_open(pdu, f, f->open_flags);
245 } while (err == -EINTR && !pdu->cancelled);
246 }
247 } else if (f->fid_type == P9_FID_DIR) {
248 if (f->fs.dir.stream == NULL) {
249 do {
250 err = v9fs_co_opendir(pdu, f);
251 } while (err == -EINTR && !pdu->cancelled);
252 }
253 }
254 return err;
255 }
256
get_fid(V9fsPDU * pdu,int32_t fid)257 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
258 {
259 int err;
260 V9fsFidState *f;
261 V9fsState *s = pdu->s;
262
263 for (f = s->fid_list; f; f = f->next) {
264 BUG_ON(f->clunked);
265 if (f->fid == fid) {
266 /*
267 * Update the fid ref upfront so that
268 * we don't get reclaimed when we yield
269 * in open later.
270 */
271 f->ref++;
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 */
278 err = v9fs_reopen_fid(pdu, f);
279 if (err < 0) {
280 f->ref--;
281 return NULL;
282 }
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;
288 return f;
289 }
290 }
291 return NULL;
292 }
293
alloc_fid(V9fsState * s,int32_t fid)294 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
295 {
296 V9fsFidState *f;
297
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 }
304 }
305 f = g_malloc0(sizeof(V9fsFidState));
306 f->fid = fid;
307 f->fid_type = P9_FID_NONE;
308 f->ref = 1;
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;
314 f->next = s->fid_list;
315 s->fid_list = f;
316
317 v9fs_readdir_init(&f->fs.dir);
318 v9fs_readdir_init(&f->fs_reclaim.dir);
319
320 return f;
321 }
322
v9fs_xattr_fid_clunk(V9fsPDU * pdu,V9fsFidState * fidp)323 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
324 {
325 int retval = 0;
326
327 if (fidp->fs.xattr.xattrwalk_fid) {
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 }
340 if (fidp->fs.xattr.len) {
341 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
342 fidp->fs.xattr.value,
343 fidp->fs.xattr.len,
344 fidp->fs.xattr.flags);
345 } else {
346 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
347 }
348 free_out:
349 v9fs_string_free(&fidp->fs.xattr.name);
350 free_value:
351 g_free(fidp->fs.xattr.value);
352 return retval;
353 }
354
free_fid(V9fsPDU * pdu,V9fsFidState * fidp)355 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
356 {
357 int retval = 0;
358
359 if (fidp->fid_type == P9_FID_FILE) {
360 /* If we reclaimed the fd no need to close */
361 if (fidp->fs.fd != -1) {
362 retval = v9fs_co_close(pdu, &fidp->fs);
363 }
364 } else if (fidp->fid_type == P9_FID_DIR) {
365 if (fidp->fs.dir.stream != NULL) {
366 retval = v9fs_co_closedir(pdu, &fidp->fs);
367 }
368 } else if (fidp->fid_type == P9_FID_XATTR) {
369 retval = v9fs_xattr_fid_clunk(pdu, fidp);
370 }
371 v9fs_path_free(&fidp->path);
372 g_free(fidp);
373 return retval;
374 }
375
put_fid(V9fsPDU * pdu,V9fsFidState * fidp)376 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
377 {
378 BUG_ON(!fidp->ref);
379 fidp->ref--;
380 /*
381 * Don't free the fid if it is in reclaim list
382 */
383 if (!fidp->ref && fidp->clunked) {
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 }
397 return free_fid(pdu, fidp);
398 }
399 return 0;
400 }
401
clunk_fid(V9fsState * s,int32_t fid)402 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
403 {
404 V9fsFidState **fidpp, *fidp;
405
406 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
407 if ((*fidpp)->fid == fid) {
408 break;
409 }
410 }
411 if (*fidpp == NULL) {
412 return NULL;
413 }
414 fidp = *fidpp;
415 *fidpp = fidp->next;
416 fidp->clunked = 1;
417 return fidp;
418 }
419
v9fs_reclaim_fd(V9fsPDU * pdu)420 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
421 {
422 int reclaim_count = 0;
423 V9fsState *s = pdu->s;
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 }
462 } else if (f->fid_type == P9_FID_DIR) {
463 if (f->fs.dir.stream != NULL) {
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;
471 f->fs_reclaim.dir.stream = f->fs.dir.stream;
472 f->fs.dir.stream = NULL;
473 reclaim_count++;
474 }
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) {
488 v9fs_co_close(pdu, &f->fs_reclaim);
489 } else if (f->fid_type == P9_FID_DIR) {
490 v9fs_co_closedir(pdu, &f->fs_reclaim);
491 }
492 f->rclm_lst = NULL;
493 /*
494 * Now drop the fid reference, free it
495 * if clunked.
496 */
497 put_fid(pdu, f);
498 }
499 }
500
v9fs_mark_fids_unreclaim(V9fsPDU * pdu,V9fsPath * path)501 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
502 {
503 int err;
504 V9fsState *s = pdu->s;
505 V9fsFidState *fidp, head_fid;
506
507 head_fid.next = s->fid_list;
508 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
509 if (fidp->path.size != path->size) {
510 continue;
511 }
512 if (!memcmp(fidp->path.data, path->data, path->size)) {
513 /* Mark the fid non reclaimable. */
514 fidp->flags |= FID_NON_RECLAIMABLE;
515
516 /* reopen the file/dir if already closed */
517 err = v9fs_reopen_fid(pdu, fidp);
518 if (err < 0) {
519 return err;
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) {
527 fidp = &head_fid;
528 }
529 }
530 }
531 return 0;
532 }
533
virtfs_reset(V9fsPDU * pdu)534 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
535 {
536 V9fsState *s = pdu->s;
537 V9fsFidState *fidp;
538
539 /* Free all fids */
540 while (s->fid_list) {
541 /* Get fid */
542 fidp = s->fid_list;
543 fidp->ref++;
544
545 /* Clunk fid */
546 s->fid_list = fidp->next;
547 fidp->clunked = 1;
548
549 put_fid(pdu, fidp);
550 }
551 }
552
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
578 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
mirror8bit(uint8_t byte)579 static 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. */
mirror64bit(uint64_t value)585 static 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 */
expGolombEncode(uint64_t n,int k)634 static 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 */
invertAffix(const VariLenAffix * affix)656 static 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 */
affixForIndex(uint64_t index)681 static 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
688 /* creative abuse of tb_hash_func7, which is based on xxhash */
qpp_hash(QppEntry e)689 static uint32_t qpp_hash(QppEntry e)
690 {
691 return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
692 }
693
qpf_hash(QpfEntry e)694 static uint32_t qpf_hash(QpfEntry e)
695 {
696 return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
697 }
698
qpd_cmp_func(const void * obj,const void * userp)699 static 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
qpp_cmp_func(const void * obj,const void * userp)705 static bool qpp_cmp_func(const void *obj, const void *userp)
706 {
707 const QppEntry *e1 = obj, *e2 = userp;
708 return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
709 }
710
qpf_cmp_func(const void * obj,const void * userp)711 static bool qpf_cmp_func(const void *obj, const void *userp)
712 {
713 const QpfEntry *e1 = obj, *e2 = userp;
714 return e1->dev == e2->dev && e1->ino == e2->ino;
715 }
716
qp_table_remove(void * p,uint32_t h,void * up)717 static void qp_table_remove(void *p, uint32_t h, void *up)
718 {
719 g_free(p);
720 }
721
qp_table_destroy(struct qht * ht)722 static void qp_table_destroy(struct qht *ht)
723 {
724 if (!ht || !ht->map) {
725 return;
726 }
727 qht_iter(ht, qp_table_remove, NULL);
728 qht_destroy(ht);
729 }
730
qpd_table_init(struct qht * ht)731 static void qpd_table_init(struct qht *ht)
732 {
733 qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
734 }
735
qpp_table_init(struct qht * ht)736 static void qpp_table_init(struct qht *ht)
737 {
738 qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
739 }
740
qpf_table_init(struct qht * ht)741 static void qpf_table_init(struct qht *ht)
742 {
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 */
qid_inode_prefix_hash_bits(V9fsPDU * pdu,dev_t dev)759 static 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;
777 }
778
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 */
qid_path_fullmap(V9fsPDU * pdu,const struct stat * stbuf,uint64_t * path)795 static 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);
803 VariLenAffix affix;
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 */
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);
826 qht_insert(&pdu->s->qpf_table, val, hash, NULL);
827 }
828
829 *path = val->path;
830 return 0;
831 }
832
833 /**
834 * @brief Quick mapping host inode nr -> guest inode nr.
835 *
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. ;-)
871 */
qid_path_suffixmap(V9fsPDU * pdu,const struct stat * stbuf,uint64_t * path)872 static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
873 uint64_t *path)
874 {
875 const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
876 QppEntry lookup = {
877 .dev = stbuf->st_dev,
878 .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
879 }, *val;
880 uint32_t hash = qpp_hash(lookup);
881
882 val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
883
884 if (!val) {
885 if (pdu->s->qp_affix_next == 0) {
886 /* we ran out of affixes */
887 warn_report_once(
888 "9p: Potential degraded performance of inode remapping"
889 );
890 return -ENFILE;
891 }
892
893 val = g_malloc0(sizeof(QppEntry));
894 *val = lookup;
895
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);
899 qht_insert(&pdu->s->qpp_table, val, hash, NULL);
900 }
901 /* assuming generated affix to be suffix type, not prefix */
902 *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
903 return 0;
904 }
905
stat_to_qid(V9fsPDU * pdu,const struct stat * stbuf,V9fsQID * qidp)906 static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
907 {
908 int err;
909 size_t size;
910
911 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
912 /* map inode+device to qid path (fast path) */
913 err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
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 }
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);
944 }
945
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 }
954
955 return 0;
956 }
957
fid_to_qid(V9fsPDU * pdu,V9fsFidState * fidp,V9fsQID * qidp)958 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
959 V9fsQID *qidp)
960 {
961 struct stat stbuf;
962 int err;
963
964 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
965 if (err < 0) {
966 return err;
967 }
968 err = stat_to_qid(pdu, &stbuf, qidp);
969 if (err < 0) {
970 return err;
971 }
972 return 0;
973 }
974
dirent_to_qid(V9fsPDU * pdu,V9fsFidState * fidp,struct dirent * dent,V9fsQID * qidp)975 static int coroutine_fn dirent_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
976 struct dirent *dent, V9fsQID *qidp)
977 {
978 struct stat stbuf;
979 V9fsPath path;
980 int err;
981
982 v9fs_path_init(&path);
983
984 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
985 if (err < 0) {
986 goto out;
987 }
988 err = v9fs_co_lstat(pdu, &path, &stbuf);
989 if (err < 0) {
990 goto out;
991 }
992 err = stat_to_qid(pdu, &stbuf, qidp);
993
994 out:
995 v9fs_path_free(&path);
996 return err;
997 }
998
pdu_alloc(V9fsState * s)999 V9fsPDU *pdu_alloc(V9fsState *s)
1000 {
1001 V9fsPDU *pdu = NULL;
1002
1003 if (!QLIST_EMPTY(&s->free_list)) {
1004 pdu = QLIST_FIRST(&s->free_list);
1005 QLIST_REMOVE(pdu, next);
1006 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
1007 }
1008 return pdu;
1009 }
1010
pdu_free(V9fsPDU * pdu)1011 void pdu_free(V9fsPDU *pdu)
1012 {
1013 V9fsState *s = pdu->s;
1014
1015 g_assert(!pdu->cancelled);
1016 QLIST_REMOVE(pdu, next);
1017 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
1018 }
1019
pdu_complete(V9fsPDU * pdu,ssize_t len)1020 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
1021 {
1022 int8_t id = pdu->id + 1; /* Response */
1023 V9fsState *s = pdu->s;
1024 int ret;
1025
1026 /*
1027 * The 9p spec requires that successfully cancelled pdus receive no reply.
1028 * Sending a reply would confuse clients because they would
1029 * assume that any EINTR is the actual result of the operation,
1030 * rather than a consequence of the cancellation. However, if
1031 * the operation completed (succesfully or with an error other
1032 * than caused be cancellation), we do send out that reply, both
1033 * for efficiency and to avoid confusing the rest of the state machine
1034 * that assumes passing a non-error here will mean a successful
1035 * transmission of the reply.
1036 */
1037 bool discard = pdu->cancelled && len == -EINTR;
1038 if (discard) {
1039 trace_v9fs_rcancel(pdu->tag, pdu->id);
1040 pdu->size = 0;
1041 goto out_notify;
1042 }
1043
1044 if (len < 0) {
1045 int err = -len;
1046 len = 7;
1047
1048 if (s->proto_version != V9FS_PROTO_2000L) {
1049 V9fsString str;
1050
1051 str.data = strerror(err);
1052 str.size = strlen(str.data);
1053
1054 ret = pdu_marshal(pdu, len, "s", &str);
1055 if (ret < 0) {
1056 goto out_notify;
1057 }
1058 len += ret;
1059 id = P9_RERROR;
1060 }
1061
1062 ret = pdu_marshal(pdu, len, "d", err);
1063 if (ret < 0) {
1064 goto out_notify;
1065 }
1066 len += ret;
1067
1068 if (s->proto_version == V9FS_PROTO_2000L) {
1069 id = P9_RLERROR;
1070 }
1071 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
1072 }
1073
1074 /* fill out the header */
1075 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
1076 goto out_notify;
1077 }
1078
1079 /* keep these in sync */
1080 pdu->size = len;
1081 pdu->id = id;
1082
1083 out_notify:
1084 pdu->s->transport->push_and_notify(pdu);
1085
1086 /* Now wakeup anybody waiting in flush for this request */
1087 if (!qemu_co_queue_next(&pdu->complete)) {
1088 pdu_free(pdu);
1089 }
1090 }
1091
v9mode_to_mode(uint32_t mode,V9fsString * extension)1092 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
1093 {
1094 mode_t ret;
1095
1096 ret = mode & 0777;
1097 if (mode & P9_STAT_MODE_DIR) {
1098 ret |= S_IFDIR;
1099 }
1100
1101 if (mode & P9_STAT_MODE_SYMLINK) {
1102 ret |= S_IFLNK;
1103 }
1104 if (mode & P9_STAT_MODE_SOCKET) {
1105 ret |= S_IFSOCK;
1106 }
1107 if (mode & P9_STAT_MODE_NAMED_PIPE) {
1108 ret |= S_IFIFO;
1109 }
1110 if (mode & P9_STAT_MODE_DEVICE) {
1111 if (extension->size && extension->data[0] == 'c') {
1112 ret |= S_IFCHR;
1113 } else {
1114 ret |= S_IFBLK;
1115 }
1116 }
1117
1118 if (!(ret&~0777)) {
1119 ret |= S_IFREG;
1120 }
1121
1122 if (mode & P9_STAT_MODE_SETUID) {
1123 ret |= S_ISUID;
1124 }
1125 if (mode & P9_STAT_MODE_SETGID) {
1126 ret |= S_ISGID;
1127 }
1128 if (mode & P9_STAT_MODE_SETVTX) {
1129 ret |= S_ISVTX;
1130 }
1131
1132 return ret;
1133 }
1134
donttouch_stat(V9fsStat * stat)1135 static int donttouch_stat(V9fsStat *stat)
1136 {
1137 if (stat->type == -1 &&
1138 stat->dev == -1 &&
1139 stat->qid.type == 0xff &&
1140 stat->qid.version == (uint32_t) -1 &&
1141 stat->qid.path == (uint64_t) -1 &&
1142 stat->mode == -1 &&
1143 stat->atime == -1 &&
1144 stat->mtime == -1 &&
1145 stat->length == -1 &&
1146 !stat->name.size &&
1147 !stat->uid.size &&
1148 !stat->gid.size &&
1149 !stat->muid.size &&
1150 stat->n_uid == -1 &&
1151 stat->n_gid == -1 &&
1152 stat->n_muid == -1) {
1153 return 1;
1154 }
1155
1156 return 0;
1157 }
1158
v9fs_stat_init(V9fsStat * stat)1159 static void v9fs_stat_init(V9fsStat *stat)
1160 {
1161 v9fs_string_init(&stat->name);
1162 v9fs_string_init(&stat->uid);
1163 v9fs_string_init(&stat->gid);
1164 v9fs_string_init(&stat->muid);
1165 v9fs_string_init(&stat->extension);
1166 }
1167
v9fs_stat_free(V9fsStat * stat)1168 static void v9fs_stat_free(V9fsStat *stat)
1169 {
1170 v9fs_string_free(&stat->name);
1171 v9fs_string_free(&stat->uid);
1172 v9fs_string_free(&stat->gid);
1173 v9fs_string_free(&stat->muid);
1174 v9fs_string_free(&stat->extension);
1175 }
1176
stat_to_v9mode(const struct stat * stbuf)1177 static uint32_t stat_to_v9mode(const struct stat *stbuf)
1178 {
1179 uint32_t mode;
1180
1181 mode = stbuf->st_mode & 0777;
1182 if (S_ISDIR(stbuf->st_mode)) {
1183 mode |= P9_STAT_MODE_DIR;
1184 }
1185
1186 if (S_ISLNK(stbuf->st_mode)) {
1187 mode |= P9_STAT_MODE_SYMLINK;
1188 }
1189
1190 if (S_ISSOCK(stbuf->st_mode)) {
1191 mode |= P9_STAT_MODE_SOCKET;
1192 }
1193
1194 if (S_ISFIFO(stbuf->st_mode)) {
1195 mode |= P9_STAT_MODE_NAMED_PIPE;
1196 }
1197
1198 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
1199 mode |= P9_STAT_MODE_DEVICE;
1200 }
1201
1202 if (stbuf->st_mode & S_ISUID) {
1203 mode |= P9_STAT_MODE_SETUID;
1204 }
1205
1206 if (stbuf->st_mode & S_ISGID) {
1207 mode |= P9_STAT_MODE_SETGID;
1208 }
1209
1210 if (stbuf->st_mode & S_ISVTX) {
1211 mode |= P9_STAT_MODE_SETVTX;
1212 }
1213
1214 return mode;
1215 }
1216
stat_to_v9stat(V9fsPDU * pdu,V9fsPath * path,const char * basename,const struct stat * stbuf,V9fsStat * v9stat)1217 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
1218 const char *basename,
1219 const struct stat *stbuf,
1220 V9fsStat *v9stat)
1221 {
1222 int err;
1223
1224 memset(v9stat, 0, sizeof(*v9stat));
1225
1226 err = stat_to_qid(pdu, stbuf, &v9stat->qid);
1227 if (err < 0) {
1228 return err;
1229 }
1230 v9stat->mode = stat_to_v9mode(stbuf);
1231 v9stat->atime = stbuf->st_atime;
1232 v9stat->mtime = stbuf->st_mtime;
1233 v9stat->length = stbuf->st_size;
1234
1235 v9fs_string_free(&v9stat->uid);
1236 v9fs_string_free(&v9stat->gid);
1237 v9fs_string_free(&v9stat->muid);
1238
1239 v9stat->n_uid = stbuf->st_uid;
1240 v9stat->n_gid = stbuf->st_gid;
1241 v9stat->n_muid = 0;
1242
1243 v9fs_string_free(&v9stat->extension);
1244
1245 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1246 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
1247 if (err < 0) {
1248 return err;
1249 }
1250 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1251 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1252 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1253 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1254 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1255 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1256 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1257 }
1258
1259 v9fs_string_sprintf(&v9stat->name, "%s", basename);
1260
1261 v9stat->size = 61 +
1262 v9fs_string_size(&v9stat->name) +
1263 v9fs_string_size(&v9stat->uid) +
1264 v9fs_string_size(&v9stat->gid) +
1265 v9fs_string_size(&v9stat->muid) +
1266 v9fs_string_size(&v9stat->extension);
1267 return 0;
1268 }
1269
1270 #define P9_STATS_MODE 0x00000001ULL
1271 #define P9_STATS_NLINK 0x00000002ULL
1272 #define P9_STATS_UID 0x00000004ULL
1273 #define P9_STATS_GID 0x00000008ULL
1274 #define P9_STATS_RDEV 0x00000010ULL
1275 #define P9_STATS_ATIME 0x00000020ULL
1276 #define P9_STATS_MTIME 0x00000040ULL
1277 #define P9_STATS_CTIME 0x00000080ULL
1278 #define P9_STATS_INO 0x00000100ULL
1279 #define P9_STATS_SIZE 0x00000200ULL
1280 #define P9_STATS_BLOCKS 0x00000400ULL
1281
1282 #define P9_STATS_BTIME 0x00000800ULL
1283 #define P9_STATS_GEN 0x00001000ULL
1284 #define P9_STATS_DATA_VERSION 0x00002000ULL
1285
1286 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1287 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1288
1289
stat_to_v9stat_dotl(V9fsPDU * pdu,const struct stat * stbuf,V9fsStatDotl * v9lstat)1290 static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
1291 V9fsStatDotl *v9lstat)
1292 {
1293 memset(v9lstat, 0, sizeof(*v9lstat));
1294
1295 v9lstat->st_mode = stbuf->st_mode;
1296 v9lstat->st_nlink = stbuf->st_nlink;
1297 v9lstat->st_uid = stbuf->st_uid;
1298 v9lstat->st_gid = stbuf->st_gid;
1299 v9lstat->st_rdev = stbuf->st_rdev;
1300 v9lstat->st_size = stbuf->st_size;
1301 v9lstat->st_blksize = stbuf->st_blksize;
1302 v9lstat->st_blocks = stbuf->st_blocks;
1303 v9lstat->st_atime_sec = stbuf->st_atime;
1304 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1305 v9lstat->st_mtime_sec = stbuf->st_mtime;
1306 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1307 v9lstat->st_ctime_sec = stbuf->st_ctime;
1308 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1309 /* Currently we only support BASIC fields in stat */
1310 v9lstat->st_result_mask = P9_STATS_BASIC;
1311
1312 return stat_to_qid(pdu, stbuf, &v9lstat->qid);
1313 }
1314
print_sg(struct iovec * sg,int cnt)1315 static void print_sg(struct iovec *sg, int cnt)
1316 {
1317 int i;
1318
1319 printf("sg[%d]: {", cnt);
1320 for (i = 0; i < cnt; i++) {
1321 if (i) {
1322 printf(", ");
1323 }
1324 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1325 }
1326 printf("}\n");
1327 }
1328
1329 /* Will call this only for path name based fid */
v9fs_fix_path(V9fsPath * dst,V9fsPath * src,int len)1330 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
1331 {
1332 V9fsPath str;
1333 v9fs_path_init(&str);
1334 v9fs_path_copy(&str, dst);
1335 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
1336 v9fs_path_free(&str);
1337 }
1338
is_ro_export(FsContext * ctx)1339 static inline bool is_ro_export(FsContext *ctx)
1340 {
1341 return ctx->export_flags & V9FS_RDONLY;
1342 }
1343
v9fs_version(void * opaque)1344 static void coroutine_fn v9fs_version(void *opaque)
1345 {
1346 ssize_t err;
1347 V9fsPDU *pdu = opaque;
1348 V9fsState *s = pdu->s;
1349 V9fsString version;
1350 size_t offset = 7;
1351
1352 v9fs_string_init(&version);
1353 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1354 if (err < 0) {
1355 goto out;
1356 }
1357 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
1358
1359 virtfs_reset(pdu);
1360
1361 if (!strcmp(version.data, "9P2000.u")) {
1362 s->proto_version = V9FS_PROTO_2000U;
1363 } else if (!strcmp(version.data, "9P2000.L")) {
1364 s->proto_version = V9FS_PROTO_2000L;
1365 } else {
1366 v9fs_string_sprintf(&version, "unknown");
1367 }
1368
1369 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1370 if (err < 0) {
1371 goto out;
1372 }
1373 err += offset;
1374 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
1375 out:
1376 pdu_complete(pdu, err);
1377 v9fs_string_free(&version);
1378 }
1379
v9fs_attach(void * opaque)1380 static void coroutine_fn v9fs_attach(void *opaque)
1381 {
1382 V9fsPDU *pdu = opaque;
1383 V9fsState *s = pdu->s;
1384 int32_t fid, afid, n_uname;
1385 V9fsString uname, aname;
1386 V9fsFidState *fidp;
1387 size_t offset = 7;
1388 V9fsQID qid;
1389 ssize_t err;
1390 Error *local_err = NULL;
1391
1392 v9fs_string_init(&uname);
1393 v9fs_string_init(&aname);
1394 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1395 &afid, &uname, &aname, &n_uname);
1396 if (err < 0) {
1397 goto out_nofid;
1398 }
1399 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1400
1401 fidp = alloc_fid(s, fid);
1402 if (fidp == NULL) {
1403 err = -EINVAL;
1404 goto out_nofid;
1405 }
1406 fidp->uid = n_uname;
1407 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1408 if (err < 0) {
1409 err = -EINVAL;
1410 clunk_fid(s, fid);
1411 goto out;
1412 }
1413 err = fid_to_qid(pdu, fidp, &qid);
1414 if (err < 0) {
1415 err = -EINVAL;
1416 clunk_fid(s, fid);
1417 goto out;
1418 }
1419
1420 /*
1421 * disable migration if we haven't done already.
1422 * attach could get called multiple times for the same export.
1423 */
1424 if (!s->migration_blocker) {
1425 error_setg(&s->migration_blocker,
1426 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1427 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1428 err = migrate_add_blocker(s->migration_blocker, &local_err);
1429 if (local_err) {
1430 error_free(local_err);
1431 error_free(s->migration_blocker);
1432 s->migration_blocker = NULL;
1433 clunk_fid(s, fid);
1434 goto out;
1435 }
1436 s->root_fid = fid;
1437 }
1438
1439 err = pdu_marshal(pdu, offset, "Q", &qid);
1440 if (err < 0) {
1441 clunk_fid(s, fid);
1442 goto out;
1443 }
1444 err += offset;
1445
1446 memcpy(&s->root_qid, &qid, sizeof(qid));
1447 trace_v9fs_attach_return(pdu->tag, pdu->id,
1448 qid.type, qid.version, qid.path);
1449 out:
1450 put_fid(pdu, fidp);
1451 out_nofid:
1452 pdu_complete(pdu, err);
1453 v9fs_string_free(&uname);
1454 v9fs_string_free(&aname);
1455 }
1456
v9fs_stat(void * opaque)1457 static void coroutine_fn v9fs_stat(void *opaque)
1458 {
1459 int32_t fid;
1460 V9fsStat v9stat;
1461 ssize_t err = 0;
1462 size_t offset = 7;
1463 struct stat stbuf;
1464 V9fsFidState *fidp;
1465 V9fsPDU *pdu = opaque;
1466 char *basename;
1467
1468 err = pdu_unmarshal(pdu, offset, "d", &fid);
1469 if (err < 0) {
1470 goto out_nofid;
1471 }
1472 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1473
1474 fidp = get_fid(pdu, fid);
1475 if (fidp == NULL) {
1476 err = -ENOENT;
1477 goto out_nofid;
1478 }
1479 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1480 if (err < 0) {
1481 goto out;
1482 }
1483 basename = g_path_get_basename(fidp->path.data);
1484 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1485 g_free(basename);
1486 if (err < 0) {
1487 goto out;
1488 }
1489 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1490 if (err < 0) {
1491 v9fs_stat_free(&v9stat);
1492 goto out;
1493 }
1494 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1495 v9stat.atime, v9stat.mtime, v9stat.length);
1496 err += offset;
1497 v9fs_stat_free(&v9stat);
1498 out:
1499 put_fid(pdu, fidp);
1500 out_nofid:
1501 pdu_complete(pdu, err);
1502 }
1503
v9fs_getattr(void * opaque)1504 static void coroutine_fn v9fs_getattr(void *opaque)
1505 {
1506 int32_t fid;
1507 size_t offset = 7;
1508 ssize_t retval = 0;
1509 struct stat stbuf;
1510 V9fsFidState *fidp;
1511 uint64_t request_mask;
1512 V9fsStatDotl v9stat_dotl;
1513 V9fsPDU *pdu = opaque;
1514
1515 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1516 if (retval < 0) {
1517 goto out_nofid;
1518 }
1519 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1520
1521 fidp = get_fid(pdu, fid);
1522 if (fidp == NULL) {
1523 retval = -ENOENT;
1524 goto out_nofid;
1525 }
1526 /*
1527 * Currently we only support BASIC fields in stat, so there is no
1528 * need to look at request_mask.
1529 */
1530 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1531 if (retval < 0) {
1532 goto out;
1533 }
1534 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
1535 if (retval < 0) {
1536 goto out;
1537 }
1538
1539 /* fill st_gen if requested and supported by underlying fs */
1540 if (request_mask & P9_STATS_GEN) {
1541 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1542 switch (retval) {
1543 case 0:
1544 /* we have valid st_gen: update result mask */
1545 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1546 break;
1547 case -EINTR:
1548 /* request cancelled, e.g. by Tflush */
1549 goto out;
1550 default:
1551 /* failed to get st_gen: not fatal, ignore */
1552 break;
1553 }
1554 }
1555 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1556 if (retval < 0) {
1557 goto out;
1558 }
1559 retval += offset;
1560 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1561 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1562 v9stat_dotl.st_gid);
1563 out:
1564 put_fid(pdu, fidp);
1565 out_nofid:
1566 pdu_complete(pdu, retval);
1567 }
1568
1569 /* Attribute flags */
1570 #define P9_ATTR_MODE (1 << 0)
1571 #define P9_ATTR_UID (1 << 1)
1572 #define P9_ATTR_GID (1 << 2)
1573 #define P9_ATTR_SIZE (1 << 3)
1574 #define P9_ATTR_ATIME (1 << 4)
1575 #define P9_ATTR_MTIME (1 << 5)
1576 #define P9_ATTR_CTIME (1 << 6)
1577 #define P9_ATTR_ATIME_SET (1 << 7)
1578 #define P9_ATTR_MTIME_SET (1 << 8)
1579
1580 #define P9_ATTR_MASK 127
1581
v9fs_setattr(void * opaque)1582 static void coroutine_fn v9fs_setattr(void *opaque)
1583 {
1584 int err = 0;
1585 int32_t fid;
1586 V9fsFidState *fidp;
1587 size_t offset = 7;
1588 V9fsIattr v9iattr;
1589 V9fsPDU *pdu = opaque;
1590
1591 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1592 if (err < 0) {
1593 goto out_nofid;
1594 }
1595
1596 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1597 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1598 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1599
1600 fidp = get_fid(pdu, fid);
1601 if (fidp == NULL) {
1602 err = -EINVAL;
1603 goto out_nofid;
1604 }
1605 if (v9iattr.valid & P9_ATTR_MODE) {
1606 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1607 if (err < 0) {
1608 goto out;
1609 }
1610 }
1611 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1612 struct timespec times[2];
1613 if (v9iattr.valid & P9_ATTR_ATIME) {
1614 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1615 times[0].tv_sec = v9iattr.atime_sec;
1616 times[0].tv_nsec = v9iattr.atime_nsec;
1617 } else {
1618 times[0].tv_nsec = UTIME_NOW;
1619 }
1620 } else {
1621 times[0].tv_nsec = UTIME_OMIT;
1622 }
1623 if (v9iattr.valid & P9_ATTR_MTIME) {
1624 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1625 times[1].tv_sec = v9iattr.mtime_sec;
1626 times[1].tv_nsec = v9iattr.mtime_nsec;
1627 } else {
1628 times[1].tv_nsec = UTIME_NOW;
1629 }
1630 } else {
1631 times[1].tv_nsec = UTIME_OMIT;
1632 }
1633 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1634 if (err < 0) {
1635 goto out;
1636 }
1637 }
1638 /*
1639 * If the only valid entry in iattr is ctime we can call
1640 * chown(-1,-1) to update the ctime of the file
1641 */
1642 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1643 ((v9iattr.valid & P9_ATTR_CTIME)
1644 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1645 if (!(v9iattr.valid & P9_ATTR_UID)) {
1646 v9iattr.uid = -1;
1647 }
1648 if (!(v9iattr.valid & P9_ATTR_GID)) {
1649 v9iattr.gid = -1;
1650 }
1651 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1652 v9iattr.gid);
1653 if (err < 0) {
1654 goto out;
1655 }
1656 }
1657 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1658 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1659 if (err < 0) {
1660 goto out;
1661 }
1662 }
1663 err = offset;
1664 trace_v9fs_setattr_return(pdu->tag, pdu->id);
1665 out:
1666 put_fid(pdu, fidp);
1667 out_nofid:
1668 pdu_complete(pdu, err);
1669 }
1670
v9fs_walk_marshal(V9fsPDU * pdu,uint16_t nwnames,V9fsQID * qids)1671 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1672 {
1673 int i;
1674 ssize_t err;
1675 size_t offset = 7;
1676
1677 err = pdu_marshal(pdu, offset, "w", nwnames);
1678 if (err < 0) {
1679 return err;
1680 }
1681 offset += err;
1682 for (i = 0; i < nwnames; i++) {
1683 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1684 if (err < 0) {
1685 return err;
1686 }
1687 offset += err;
1688 }
1689 return offset;
1690 }
1691
name_is_illegal(const char * name)1692 static bool name_is_illegal(const char *name)
1693 {
1694 return !*name || strchr(name, '/') != NULL;
1695 }
1696
not_same_qid(const V9fsQID * qid1,const V9fsQID * qid2)1697 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1698 {
1699 return
1700 qid1->type != qid2->type ||
1701 qid1->version != qid2->version ||
1702 qid1->path != qid2->path;
1703 }
1704
v9fs_walk(void * opaque)1705 static void coroutine_fn v9fs_walk(void *opaque)
1706 {
1707 int name_idx;
1708 V9fsQID *qids = NULL;
1709 int i, err = 0;
1710 V9fsPath dpath, path;
1711 uint16_t nwnames;
1712 struct stat stbuf;
1713 size_t offset = 7;
1714 int32_t fid, newfid;
1715 V9fsString *wnames = NULL;
1716 V9fsFidState *fidp;
1717 V9fsFidState *newfidp = NULL;
1718 V9fsPDU *pdu = opaque;
1719 V9fsState *s = pdu->s;
1720 V9fsQID qid;
1721
1722 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1723 if (err < 0) {
1724 pdu_complete(pdu, err);
1725 return ;
1726 }
1727 offset += err;
1728
1729 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1730
1731 if (nwnames && nwnames <= P9_MAXWELEM) {
1732 wnames = g_new0(V9fsString, nwnames);
1733 qids = g_new0(V9fsQID, nwnames);
1734 for (i = 0; i < nwnames; i++) {
1735 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1736 if (err < 0) {
1737 goto out_nofid;
1738 }
1739 if (name_is_illegal(wnames[i].data)) {
1740 err = -ENOENT;
1741 goto out_nofid;
1742 }
1743 offset += err;
1744 }
1745 } else if (nwnames > P9_MAXWELEM) {
1746 err = -EINVAL;
1747 goto out_nofid;
1748 }
1749 fidp = get_fid(pdu, fid);
1750 if (fidp == NULL) {
1751 err = -ENOENT;
1752 goto out_nofid;
1753 }
1754
1755 v9fs_path_init(&dpath);
1756 v9fs_path_init(&path);
1757
1758 err = fid_to_qid(pdu, fidp, &qid);
1759 if (err < 0) {
1760 goto out;
1761 }
1762
1763 /*
1764 * Both dpath and path initially poin to fidp.
1765 * Needed to handle request with nwnames == 0
1766 */
1767 v9fs_path_copy(&dpath, &fidp->path);
1768 v9fs_path_copy(&path, &fidp->path);
1769 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1770 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1771 strcmp("..", wnames[name_idx].data)) {
1772 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1773 &path);
1774 if (err < 0) {
1775 goto out;
1776 }
1777
1778 err = v9fs_co_lstat(pdu, &path, &stbuf);
1779 if (err < 0) {
1780 goto out;
1781 }
1782 err = stat_to_qid(pdu, &stbuf, &qid);
1783 if (err < 0) {
1784 goto out;
1785 }
1786 v9fs_path_copy(&dpath, &path);
1787 }
1788 memcpy(&qids[name_idx], &qid, sizeof(qid));
1789 }
1790 if (fid == newfid) {
1791 if (fidp->fid_type != P9_FID_NONE) {
1792 err = -EINVAL;
1793 goto out;
1794 }
1795 v9fs_path_write_lock(s);
1796 v9fs_path_copy(&fidp->path, &path);
1797 v9fs_path_unlock(s);
1798 } else {
1799 newfidp = alloc_fid(s, newfid);
1800 if (newfidp == NULL) {
1801 err = -EINVAL;
1802 goto out;
1803 }
1804 newfidp->uid = fidp->uid;
1805 v9fs_path_copy(&newfidp->path, &path);
1806 }
1807 err = v9fs_walk_marshal(pdu, nwnames, qids);
1808 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1809 out:
1810 put_fid(pdu, fidp);
1811 if (newfidp) {
1812 put_fid(pdu, newfidp);
1813 }
1814 v9fs_path_free(&dpath);
1815 v9fs_path_free(&path);
1816 out_nofid:
1817 pdu_complete(pdu, err);
1818 if (nwnames && nwnames <= P9_MAXWELEM) {
1819 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1820 v9fs_string_free(&wnames[name_idx]);
1821 }
1822 g_free(wnames);
1823 g_free(qids);
1824 }
1825 }
1826
get_iounit(V9fsPDU * pdu,V9fsPath * path)1827 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1828 {
1829 struct statfs stbuf;
1830 int32_t iounit = 0;
1831 V9fsState *s = pdu->s;
1832
1833 /*
1834 * iounit should be multiples of f_bsize (host filesystem block size
1835 * and as well as less than (client msize - P9_IOHDRSZ))
1836 */
1837 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1838 if (stbuf.f_bsize) {
1839 iounit = stbuf.f_bsize;
1840 iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize;
1841 }
1842 }
1843 if (!iounit) {
1844 iounit = s->msize - P9_IOHDRSZ;
1845 }
1846 return iounit;
1847 }
1848
v9fs_open(void * opaque)1849 static void coroutine_fn v9fs_open(void *opaque)
1850 {
1851 int flags;
1852 int32_t fid;
1853 int32_t mode;
1854 V9fsQID qid;
1855 int iounit = 0;
1856 ssize_t err = 0;
1857 size_t offset = 7;
1858 struct stat stbuf;
1859 V9fsFidState *fidp;
1860 V9fsPDU *pdu = opaque;
1861 V9fsState *s = pdu->s;
1862
1863 if (s->proto_version == V9FS_PROTO_2000L) {
1864 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1865 } else {
1866 uint8_t modebyte;
1867 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1868 mode = modebyte;
1869 }
1870 if (err < 0) {
1871 goto out_nofid;
1872 }
1873 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1874
1875 fidp = get_fid(pdu, fid);
1876 if (fidp == NULL) {
1877 err = -ENOENT;
1878 goto out_nofid;
1879 }
1880 if (fidp->fid_type != P9_FID_NONE) {
1881 err = -EINVAL;
1882 goto out;
1883 }
1884
1885 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1886 if (err < 0) {
1887 goto out;
1888 }
1889 err = stat_to_qid(pdu, &stbuf, &qid);
1890 if (err < 0) {
1891 goto out;
1892 }
1893 if (S_ISDIR(stbuf.st_mode)) {
1894 err = v9fs_co_opendir(pdu, fidp);
1895 if (err < 0) {
1896 goto out;
1897 }
1898 fidp->fid_type = P9_FID_DIR;
1899 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1900 if (err < 0) {
1901 goto out;
1902 }
1903 err += offset;
1904 } else {
1905 if (s->proto_version == V9FS_PROTO_2000L) {
1906 flags = get_dotl_openflags(s, mode);
1907 } else {
1908 flags = omode_to_uflags(mode);
1909 }
1910 if (is_ro_export(&s->ctx)) {
1911 if (mode & O_WRONLY || mode & O_RDWR ||
1912 mode & O_APPEND || mode & O_TRUNC) {
1913 err = -EROFS;
1914 goto out;
1915 }
1916 }
1917 err = v9fs_co_open(pdu, fidp, flags);
1918 if (err < 0) {
1919 goto out;
1920 }
1921 fidp->fid_type = P9_FID_FILE;
1922 fidp->open_flags = flags;
1923 if (flags & O_EXCL) {
1924 /*
1925 * We let the host file system do O_EXCL check
1926 * We should not reclaim such fd
1927 */
1928 fidp->flags |= FID_NON_RECLAIMABLE;
1929 }
1930 iounit = get_iounit(pdu, &fidp->path);
1931 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1932 if (err < 0) {
1933 goto out;
1934 }
1935 err += offset;
1936 }
1937 trace_v9fs_open_return(pdu->tag, pdu->id,
1938 qid.type, qid.version, qid.path, iounit);
1939 out:
1940 put_fid(pdu, fidp);
1941 out_nofid:
1942 pdu_complete(pdu, err);
1943 }
1944
v9fs_lcreate(void * opaque)1945 static void coroutine_fn v9fs_lcreate(void *opaque)
1946 {
1947 int32_t dfid, flags, mode;
1948 gid_t gid;
1949 ssize_t err = 0;
1950 ssize_t offset = 7;
1951 V9fsString name;
1952 V9fsFidState *fidp;
1953 struct stat stbuf;
1954 V9fsQID qid;
1955 int32_t iounit;
1956 V9fsPDU *pdu = opaque;
1957
1958 v9fs_string_init(&name);
1959 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1960 &name, &flags, &mode, &gid);
1961 if (err < 0) {
1962 goto out_nofid;
1963 }
1964 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1965
1966 if (name_is_illegal(name.data)) {
1967 err = -ENOENT;
1968 goto out_nofid;
1969 }
1970
1971 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1972 err = -EEXIST;
1973 goto out_nofid;
1974 }
1975
1976 fidp = get_fid(pdu, dfid);
1977 if (fidp == NULL) {
1978 err = -ENOENT;
1979 goto out_nofid;
1980 }
1981 if (fidp->fid_type != P9_FID_NONE) {
1982 err = -EINVAL;
1983 goto out;
1984 }
1985
1986 flags = get_dotl_openflags(pdu->s, flags);
1987 err = v9fs_co_open2(pdu, fidp, &name, gid,
1988 flags | O_CREAT, mode, &stbuf);
1989 if (err < 0) {
1990 goto out;
1991 }
1992 fidp->fid_type = P9_FID_FILE;
1993 fidp->open_flags = flags;
1994 if (flags & O_EXCL) {
1995 /*
1996 * We let the host file system do O_EXCL check
1997 * We should not reclaim such fd
1998 */
1999 fidp->flags |= FID_NON_RECLAIMABLE;
2000 }
2001 iounit = get_iounit(pdu, &fidp->path);
2002 err = stat_to_qid(pdu, &stbuf, &qid);
2003 if (err < 0) {
2004 goto out;
2005 }
2006 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2007 if (err < 0) {
2008 goto out;
2009 }
2010 err += offset;
2011 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
2012 qid.type, qid.version, qid.path, iounit);
2013 out:
2014 put_fid(pdu, fidp);
2015 out_nofid:
2016 pdu_complete(pdu, err);
2017 v9fs_string_free(&name);
2018 }
2019
v9fs_fsync(void * opaque)2020 static void coroutine_fn v9fs_fsync(void *opaque)
2021 {
2022 int err;
2023 int32_t fid;
2024 int datasync;
2025 size_t offset = 7;
2026 V9fsFidState *fidp;
2027 V9fsPDU *pdu = opaque;
2028
2029 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
2030 if (err < 0) {
2031 goto out_nofid;
2032 }
2033 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
2034
2035 fidp = get_fid(pdu, fid);
2036 if (fidp == NULL) {
2037 err = -ENOENT;
2038 goto out_nofid;
2039 }
2040 err = v9fs_co_fsync(pdu, fidp, datasync);
2041 if (!err) {
2042 err = offset;
2043 }
2044 put_fid(pdu, fidp);
2045 out_nofid:
2046 pdu_complete(pdu, err);
2047 }
2048
v9fs_clunk(void * opaque)2049 static void coroutine_fn v9fs_clunk(void *opaque)
2050 {
2051 int err;
2052 int32_t fid;
2053 size_t offset = 7;
2054 V9fsFidState *fidp;
2055 V9fsPDU *pdu = opaque;
2056 V9fsState *s = pdu->s;
2057
2058 err = pdu_unmarshal(pdu, offset, "d", &fid);
2059 if (err < 0) {
2060 goto out_nofid;
2061 }
2062 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
2063
2064 fidp = clunk_fid(s, fid);
2065 if (fidp == NULL) {
2066 err = -ENOENT;
2067 goto out_nofid;
2068 }
2069 /*
2070 * Bump the ref so that put_fid will
2071 * free the fid.
2072 */
2073 fidp->ref++;
2074 err = put_fid(pdu, fidp);
2075 if (!err) {
2076 err = offset;
2077 }
2078 out_nofid:
2079 pdu_complete(pdu, err);
2080 }
2081
2082 /*
2083 * Create a QEMUIOVector for a sub-region of PDU iovecs
2084 *
2085 * @qiov: uninitialized QEMUIOVector
2086 * @skip: number of bytes to skip from beginning of PDU
2087 * @size: number of bytes to include
2088 * @is_write: true - write, false - read
2089 *
2090 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2091 * with qemu_iovec_destroy().
2092 */
v9fs_init_qiov_from_pdu(QEMUIOVector * qiov,V9fsPDU * pdu,size_t skip,size_t size,bool is_write)2093 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
2094 size_t skip, size_t size,
2095 bool is_write)
2096 {
2097 QEMUIOVector elem;
2098 struct iovec *iov;
2099 unsigned int niov;
2100
2101 if (is_write) {
2102 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
2103 } else {
2104 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
2105 }
2106
2107 qemu_iovec_init_external(&elem, iov, niov);
2108 qemu_iovec_init(qiov, niov);
2109 qemu_iovec_concat(qiov, &elem, skip, size);
2110 }
2111
v9fs_xattr_read(V9fsState * s,V9fsPDU * pdu,V9fsFidState * fidp,uint64_t off,uint32_t max_count)2112 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2113 uint64_t off, uint32_t max_count)
2114 {
2115 ssize_t err;
2116 size_t offset = 7;
2117 uint64_t read_count;
2118 QEMUIOVector qiov_full;
2119
2120 if (fidp->fs.xattr.len < off) {
2121 read_count = 0;
2122 } else {
2123 read_count = fidp->fs.xattr.len - off;
2124 }
2125 if (read_count > max_count) {
2126 read_count = max_count;
2127 }
2128 err = pdu_marshal(pdu, offset, "d", read_count);
2129 if (err < 0) {
2130 return err;
2131 }
2132 offset += err;
2133
2134 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
2135 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
2136 ((char *)fidp->fs.xattr.value) + off,
2137 read_count);
2138 qemu_iovec_destroy(&qiov_full);
2139 if (err < 0) {
2140 return err;
2141 }
2142 offset += err;
2143 return offset;
2144 }
2145
v9fs_do_readdir_with_stat(V9fsPDU * pdu,V9fsFidState * fidp,uint32_t max_count)2146 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
2147 V9fsFidState *fidp,
2148 uint32_t max_count)
2149 {
2150 V9fsPath path;
2151 V9fsStat v9stat;
2152 int len, err = 0;
2153 int32_t count = 0;
2154 struct stat stbuf;
2155 off_t saved_dir_pos;
2156 struct dirent *dent;
2157
2158 /* save the directory position */
2159 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
2160 if (saved_dir_pos < 0) {
2161 return saved_dir_pos;
2162 }
2163
2164 while (1) {
2165 v9fs_path_init(&path);
2166
2167 v9fs_readdir_lock(&fidp->fs.dir);
2168
2169 err = v9fs_co_readdir(pdu, fidp, &dent);
2170 if (err || !dent) {
2171 break;
2172 }
2173 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
2174 if (err < 0) {
2175 break;
2176 }
2177 err = v9fs_co_lstat(pdu, &path, &stbuf);
2178 if (err < 0) {
2179 break;
2180 }
2181 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
2182 if (err < 0) {
2183 break;
2184 }
2185 if ((count + v9stat.size + 2) > max_count) {
2186 v9fs_readdir_unlock(&fidp->fs.dir);
2187
2188 /* Ran out of buffer. Set dir back to old position and return */
2189 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2190 v9fs_stat_free(&v9stat);
2191 v9fs_path_free(&path);
2192 return count;
2193 }
2194
2195 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2196 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
2197
2198 v9fs_readdir_unlock(&fidp->fs.dir);
2199
2200 if (len < 0) {
2201 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2202 v9fs_stat_free(&v9stat);
2203 v9fs_path_free(&path);
2204 return len;
2205 }
2206 count += len;
2207 v9fs_stat_free(&v9stat);
2208 v9fs_path_free(&path);
2209 saved_dir_pos = dent->d_off;
2210 }
2211
2212 v9fs_readdir_unlock(&fidp->fs.dir);
2213
2214 v9fs_path_free(&path);
2215 if (err < 0) {
2216 return err;
2217 }
2218 return count;
2219 }
2220
v9fs_read(void * opaque)2221 static void coroutine_fn v9fs_read(void *opaque)
2222 {
2223 int32_t fid;
2224 uint64_t off;
2225 ssize_t err = 0;
2226 int32_t count = 0;
2227 size_t offset = 7;
2228 uint32_t max_count;
2229 V9fsFidState *fidp;
2230 V9fsPDU *pdu = opaque;
2231 V9fsState *s = pdu->s;
2232
2233 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
2234 if (err < 0) {
2235 goto out_nofid;
2236 }
2237 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
2238
2239 fidp = get_fid(pdu, fid);
2240 if (fidp == NULL) {
2241 err = -EINVAL;
2242 goto out_nofid;
2243 }
2244 if (fidp->fid_type == P9_FID_DIR) {
2245
2246 if (off == 0) {
2247 v9fs_co_rewinddir(pdu, fidp);
2248 }
2249 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
2250 if (count < 0) {
2251 err = count;
2252 goto out;
2253 }
2254 err = pdu_marshal(pdu, offset, "d", count);
2255 if (err < 0) {
2256 goto out;
2257 }
2258 err += offset + count;
2259 } else if (fidp->fid_type == P9_FID_FILE) {
2260 QEMUIOVector qiov_full;
2261 QEMUIOVector qiov;
2262 int32_t len;
2263
2264 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
2265 qemu_iovec_init(&qiov, qiov_full.niov);
2266 do {
2267 qemu_iovec_reset(&qiov);
2268 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
2269 if (0) {
2270 print_sg(qiov.iov, qiov.niov);
2271 }
2272 /* Loop in case of EINTR */
2273 do {
2274 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
2275 if (len >= 0) {
2276 off += len;
2277 count += len;
2278 }
2279 } while (len == -EINTR && !pdu->cancelled);
2280 if (len < 0) {
2281 /* IO error return the error */
2282 err = len;
2283 goto out_free_iovec;
2284 }
2285 } while (count < max_count && len > 0);
2286 err = pdu_marshal(pdu, offset, "d", count);
2287 if (err < 0) {
2288 goto out_free_iovec;
2289 }
2290 err += offset + count;
2291 out_free_iovec:
2292 qemu_iovec_destroy(&qiov);
2293 qemu_iovec_destroy(&qiov_full);
2294 } else if (fidp->fid_type == P9_FID_XATTR) {
2295 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
2296 } else {
2297 err = -EINVAL;
2298 }
2299 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
2300 out:
2301 put_fid(pdu, fidp);
2302 out_nofid:
2303 pdu_complete(pdu, err);
2304 }
2305
v9fs_readdir_data_size(V9fsString * name)2306 static size_t v9fs_readdir_data_size(V9fsString *name)
2307 {
2308 /*
2309 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2310 * size of type (1) + size of name.size (2) + strlen(name.data)
2311 */
2312 return 24 + v9fs_string_size(name);
2313 }
2314
v9fs_do_readdir(V9fsPDU * pdu,V9fsFidState * fidp,int32_t max_count)2315 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
2316 int32_t max_count)
2317 {
2318 size_t size;
2319 V9fsQID qid;
2320 V9fsString name;
2321 int len, err = 0;
2322 int32_t count = 0;
2323 off_t saved_dir_pos;
2324 struct dirent *dent;
2325
2326 /* save the directory position */
2327 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
2328 if (saved_dir_pos < 0) {
2329 return saved_dir_pos;
2330 }
2331
2332 while (1) {
2333 v9fs_readdir_lock(&fidp->fs.dir);
2334
2335 err = v9fs_co_readdir(pdu, fidp, &dent);
2336 if (err || !dent) {
2337 break;
2338 }
2339 v9fs_string_init(&name);
2340 v9fs_string_sprintf(&name, "%s", dent->d_name);
2341 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
2342 v9fs_readdir_unlock(&fidp->fs.dir);
2343
2344 /* Ran out of buffer. Set dir back to old position and return */
2345 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2346 v9fs_string_free(&name);
2347 return count;
2348 }
2349
2350 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
2351 /*
2352 * dirent_to_qid() implies expensive stat call for each entry,
2353 * we must do that here though since inode remapping requires
2354 * the device id, which in turn might be different for
2355 * different entries; we cannot make any assumption to avoid
2356 * that here.
2357 */
2358 err = dirent_to_qid(pdu, fidp, dent, &qid);
2359 if (err < 0) {
2360 v9fs_readdir_unlock(&fidp->fs.dir);
2361 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2362 v9fs_string_free(&name);
2363 return err;
2364 }
2365 } else {
2366 /*
2367 * Fill up just the path field of qid because the client uses
2368 * only that. To fill the entire qid structure we will have
2369 * to stat each dirent found, which is expensive. For the
2370 * latter reason we don't call dirent_to_qid() here. Only drawback
2371 * is that no multi-device export detection of stat_to_qid()
2372 * would be done and provided as error to the user here. But
2373 * user would get that error anyway when accessing those
2374 * files/dirs through other ways.
2375 */
2376 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2377 memcpy(&qid.path, &dent->d_ino, size);
2378 /* Fill the other fields with dummy values */
2379 qid.type = 0;
2380 qid.version = 0;
2381 }
2382
2383 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2384 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2385 &qid, dent->d_off,
2386 dent->d_type, &name);
2387
2388 v9fs_readdir_unlock(&fidp->fs.dir);
2389
2390 if (len < 0) {
2391 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2392 v9fs_string_free(&name);
2393 return len;
2394 }
2395 count += len;
2396 v9fs_string_free(&name);
2397 saved_dir_pos = dent->d_off;
2398 }
2399
2400 v9fs_readdir_unlock(&fidp->fs.dir);
2401
2402 if (err < 0) {
2403 return err;
2404 }
2405 return count;
2406 }
2407
v9fs_readdir(void * opaque)2408 static void coroutine_fn v9fs_readdir(void *opaque)
2409 {
2410 int32_t fid;
2411 V9fsFidState *fidp;
2412 ssize_t retval = 0;
2413 size_t offset = 7;
2414 uint64_t initial_offset;
2415 int32_t count;
2416 uint32_t max_count;
2417 V9fsPDU *pdu = opaque;
2418
2419 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2420 &initial_offset, &max_count);
2421 if (retval < 0) {
2422 goto out_nofid;
2423 }
2424 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2425
2426 fidp = get_fid(pdu, fid);
2427 if (fidp == NULL) {
2428 retval = -EINVAL;
2429 goto out_nofid;
2430 }
2431 if (!fidp->fs.dir.stream) {
2432 retval = -EINVAL;
2433 goto out;
2434 }
2435 if (initial_offset == 0) {
2436 v9fs_co_rewinddir(pdu, fidp);
2437 } else {
2438 v9fs_co_seekdir(pdu, fidp, initial_offset);
2439 }
2440 count = v9fs_do_readdir(pdu, fidp, max_count);
2441 if (count < 0) {
2442 retval = count;
2443 goto out;
2444 }
2445 retval = pdu_marshal(pdu, offset, "d", count);
2446 if (retval < 0) {
2447 goto out;
2448 }
2449 retval += count + offset;
2450 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2451 out:
2452 put_fid(pdu, fidp);
2453 out_nofid:
2454 pdu_complete(pdu, retval);
2455 }
2456
v9fs_xattr_write(V9fsState * s,V9fsPDU * pdu,V9fsFidState * fidp,uint64_t off,uint32_t count,struct iovec * sg,int cnt)2457 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2458 uint64_t off, uint32_t count,
2459 struct iovec *sg, int cnt)
2460 {
2461 int i, to_copy;
2462 ssize_t err = 0;
2463 uint64_t write_count;
2464 size_t offset = 7;
2465
2466
2467 if (fidp->fs.xattr.len < off) {
2468 err = -ENOSPC;
2469 goto out;
2470 }
2471 write_count = fidp->fs.xattr.len - off;
2472 if (write_count > count) {
2473 write_count = count;
2474 }
2475 err = pdu_marshal(pdu, offset, "d", write_count);
2476 if (err < 0) {
2477 return err;
2478 }
2479 err += offset;
2480 fidp->fs.xattr.copied_len += write_count;
2481 /*
2482 * Now copy the content from sg list
2483 */
2484 for (i = 0; i < cnt; i++) {
2485 if (write_count > sg[i].iov_len) {
2486 to_copy = sg[i].iov_len;
2487 } else {
2488 to_copy = write_count;
2489 }
2490 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2491 /* updating vs->off since we are not using below */
2492 off += to_copy;
2493 write_count -= to_copy;
2494 }
2495 out:
2496 return err;
2497 }
2498
v9fs_write(void * opaque)2499 static void coroutine_fn v9fs_write(void *opaque)
2500 {
2501 ssize_t err;
2502 int32_t fid;
2503 uint64_t off;
2504 uint32_t count;
2505 int32_t len = 0;
2506 int32_t total = 0;
2507 size_t offset = 7;
2508 V9fsFidState *fidp;
2509 V9fsPDU *pdu = opaque;
2510 V9fsState *s = pdu->s;
2511 QEMUIOVector qiov_full;
2512 QEMUIOVector qiov;
2513
2514 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2515 if (err < 0) {
2516 pdu_complete(pdu, err);
2517 return;
2518 }
2519 offset += err;
2520 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2521 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2522
2523 fidp = get_fid(pdu, fid);
2524 if (fidp == NULL) {
2525 err = -EINVAL;
2526 goto out_nofid;
2527 }
2528 if (fidp->fid_type == P9_FID_FILE) {
2529 if (fidp->fs.fd == -1) {
2530 err = -EINVAL;
2531 goto out;
2532 }
2533 } else if (fidp->fid_type == P9_FID_XATTR) {
2534 /*
2535 * setxattr operation
2536 */
2537 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2538 qiov_full.iov, qiov_full.niov);
2539 goto out;
2540 } else {
2541 err = -EINVAL;
2542 goto out;
2543 }
2544 qemu_iovec_init(&qiov, qiov_full.niov);
2545 do {
2546 qemu_iovec_reset(&qiov);
2547 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2548 if (0) {
2549 print_sg(qiov.iov, qiov.niov);
2550 }
2551 /* Loop in case of EINTR */
2552 do {
2553 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2554 if (len >= 0) {
2555 off += len;
2556 total += len;
2557 }
2558 } while (len == -EINTR && !pdu->cancelled);
2559 if (len < 0) {
2560 /* IO error return the error */
2561 err = len;
2562 goto out_qiov;
2563 }
2564 } while (total < count && len > 0);
2565
2566 offset = 7;
2567 err = pdu_marshal(pdu, offset, "d", total);
2568 if (err < 0) {
2569 goto out_qiov;
2570 }
2571 err += offset;
2572 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2573 out_qiov:
2574 qemu_iovec_destroy(&qiov);
2575 out:
2576 put_fid(pdu, fidp);
2577 out_nofid:
2578 qemu_iovec_destroy(&qiov_full);
2579 pdu_complete(pdu, err);
2580 }
2581
v9fs_create(void * opaque)2582 static void coroutine_fn v9fs_create(void *opaque)
2583 {
2584 int32_t fid;
2585 int err = 0;
2586 size_t offset = 7;
2587 V9fsFidState *fidp;
2588 V9fsQID qid;
2589 int32_t perm;
2590 int8_t mode;
2591 V9fsPath path;
2592 struct stat stbuf;
2593 V9fsString name;
2594 V9fsString extension;
2595 int iounit;
2596 V9fsPDU *pdu = opaque;
2597 V9fsState *s = pdu->s;
2598
2599 v9fs_path_init(&path);
2600 v9fs_string_init(&name);
2601 v9fs_string_init(&extension);
2602 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2603 &perm, &mode, &extension);
2604 if (err < 0) {
2605 goto out_nofid;
2606 }
2607 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2608
2609 if (name_is_illegal(name.data)) {
2610 err = -ENOENT;
2611 goto out_nofid;
2612 }
2613
2614 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2615 err = -EEXIST;
2616 goto out_nofid;
2617 }
2618
2619 fidp = get_fid(pdu, fid);
2620 if (fidp == NULL) {
2621 err = -EINVAL;
2622 goto out_nofid;
2623 }
2624 if (fidp->fid_type != P9_FID_NONE) {
2625 err = -EINVAL;
2626 goto out;
2627 }
2628 if (perm & P9_STAT_MODE_DIR) {
2629 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2630 fidp->uid, -1, &stbuf);
2631 if (err < 0) {
2632 goto out;
2633 }
2634 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2635 if (err < 0) {
2636 goto out;
2637 }
2638 v9fs_path_write_lock(s);
2639 v9fs_path_copy(&fidp->path, &path);
2640 v9fs_path_unlock(s);
2641 err = v9fs_co_opendir(pdu, fidp);
2642 if (err < 0) {
2643 goto out;
2644 }
2645 fidp->fid_type = P9_FID_DIR;
2646 } else if (perm & P9_STAT_MODE_SYMLINK) {
2647 err = v9fs_co_symlink(pdu, fidp, &name,
2648 extension.data, -1 , &stbuf);
2649 if (err < 0) {
2650 goto out;
2651 }
2652 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2653 if (err < 0) {
2654 goto out;
2655 }
2656 v9fs_path_write_lock(s);
2657 v9fs_path_copy(&fidp->path, &path);
2658 v9fs_path_unlock(s);
2659 } else if (perm & P9_STAT_MODE_LINK) {
2660 int32_t ofid = atoi(extension.data);
2661 V9fsFidState *ofidp = get_fid(pdu, ofid);
2662 if (ofidp == NULL) {
2663 err = -EINVAL;
2664 goto out;
2665 }
2666 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2667 put_fid(pdu, ofidp);
2668 if (err < 0) {
2669 goto out;
2670 }
2671 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2672 if (err < 0) {
2673 fidp->fid_type = P9_FID_NONE;
2674 goto out;
2675 }
2676 v9fs_path_write_lock(s);
2677 v9fs_path_copy(&fidp->path, &path);
2678 v9fs_path_unlock(s);
2679 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2680 if (err < 0) {
2681 fidp->fid_type = P9_FID_NONE;
2682 goto out;
2683 }
2684 } else if (perm & P9_STAT_MODE_DEVICE) {
2685 char ctype;
2686 uint32_t major, minor;
2687 mode_t nmode = 0;
2688
2689 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2690 err = -errno;
2691 goto out;
2692 }
2693
2694 switch (ctype) {
2695 case 'c':
2696 nmode = S_IFCHR;
2697 break;
2698 case 'b':
2699 nmode = S_IFBLK;
2700 break;
2701 default:
2702 err = -EIO;
2703 goto out;
2704 }
2705
2706 nmode |= perm & 0777;
2707 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2708 makedev(major, minor), nmode, &stbuf);
2709 if (err < 0) {
2710 goto out;
2711 }
2712 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2713 if (err < 0) {
2714 goto out;
2715 }
2716 v9fs_path_write_lock(s);
2717 v9fs_path_copy(&fidp->path, &path);
2718 v9fs_path_unlock(s);
2719 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2720 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2721 0, S_IFIFO | (perm & 0777), &stbuf);
2722 if (err < 0) {
2723 goto out;
2724 }
2725 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2726 if (err < 0) {
2727 goto out;
2728 }
2729 v9fs_path_write_lock(s);
2730 v9fs_path_copy(&fidp->path, &path);
2731 v9fs_path_unlock(s);
2732 } else if (perm & P9_STAT_MODE_SOCKET) {
2733 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2734 0, S_IFSOCK | (perm & 0777), &stbuf);
2735 if (err < 0) {
2736 goto out;
2737 }
2738 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2739 if (err < 0) {
2740 goto out;
2741 }
2742 v9fs_path_write_lock(s);
2743 v9fs_path_copy(&fidp->path, &path);
2744 v9fs_path_unlock(s);
2745 } else {
2746 err = v9fs_co_open2(pdu, fidp, &name, -1,
2747 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2748 if (err < 0) {
2749 goto out;
2750 }
2751 fidp->fid_type = P9_FID_FILE;
2752 fidp->open_flags = omode_to_uflags(mode);
2753 if (fidp->open_flags & O_EXCL) {
2754 /*
2755 * We let the host file system do O_EXCL check
2756 * We should not reclaim such fd
2757 */
2758 fidp->flags |= FID_NON_RECLAIMABLE;
2759 }
2760 }
2761 iounit = get_iounit(pdu, &fidp->path);
2762 err = stat_to_qid(pdu, &stbuf, &qid);
2763 if (err < 0) {
2764 goto out;
2765 }
2766 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2767 if (err < 0) {
2768 goto out;
2769 }
2770 err += offset;
2771 trace_v9fs_create_return(pdu->tag, pdu->id,
2772 qid.type, qid.version, qid.path, iounit);
2773 out:
2774 put_fid(pdu, fidp);
2775 out_nofid:
2776 pdu_complete(pdu, err);
2777 v9fs_string_free(&name);
2778 v9fs_string_free(&extension);
2779 v9fs_path_free(&path);
2780 }
2781
v9fs_symlink(void * opaque)2782 static void coroutine_fn v9fs_symlink(void *opaque)
2783 {
2784 V9fsPDU *pdu = opaque;
2785 V9fsString name;
2786 V9fsString symname;
2787 V9fsFidState *dfidp;
2788 V9fsQID qid;
2789 struct stat stbuf;
2790 int32_t dfid;
2791 int err = 0;
2792 gid_t gid;
2793 size_t offset = 7;
2794
2795 v9fs_string_init(&name);
2796 v9fs_string_init(&symname);
2797 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2798 if (err < 0) {
2799 goto out_nofid;
2800 }
2801 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2802
2803 if (name_is_illegal(name.data)) {
2804 err = -ENOENT;
2805 goto out_nofid;
2806 }
2807
2808 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2809 err = -EEXIST;
2810 goto out_nofid;
2811 }
2812
2813 dfidp = get_fid(pdu, dfid);
2814 if (dfidp == NULL) {
2815 err = -EINVAL;
2816 goto out_nofid;
2817 }
2818 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2819 if (err < 0) {
2820 goto out;
2821 }
2822 err = stat_to_qid(pdu, &stbuf, &qid);
2823 if (err < 0) {
2824 goto out;
2825 }
2826 err = pdu_marshal(pdu, offset, "Q", &qid);
2827 if (err < 0) {
2828 goto out;
2829 }
2830 err += offset;
2831 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2832 qid.type, qid.version, qid.path);
2833 out:
2834 put_fid(pdu, dfidp);
2835 out_nofid:
2836 pdu_complete(pdu, err);
2837 v9fs_string_free(&name);
2838 v9fs_string_free(&symname);
2839 }
2840
v9fs_flush(void * opaque)2841 static void coroutine_fn v9fs_flush(void *opaque)
2842 {
2843 ssize_t err;
2844 int16_t tag;
2845 size_t offset = 7;
2846 V9fsPDU *cancel_pdu = NULL;
2847 V9fsPDU *pdu = opaque;
2848 V9fsState *s = pdu->s;
2849
2850 err = pdu_unmarshal(pdu, offset, "w", &tag);
2851 if (err < 0) {
2852 pdu_complete(pdu, err);
2853 return;
2854 }
2855 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2856
2857 if (pdu->tag == tag) {
2858 warn_report("the guest sent a self-referencing 9P flush request");
2859 } else {
2860 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2861 if (cancel_pdu->tag == tag) {
2862 break;
2863 }
2864 }
2865 }
2866 if (cancel_pdu) {
2867 cancel_pdu->cancelled = 1;
2868 /*
2869 * Wait for pdu to complete.
2870 */
2871 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2872 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2873 cancel_pdu->cancelled = 0;
2874 pdu_free(cancel_pdu);
2875 }
2876 }
2877 pdu_complete(pdu, 7);
2878 }
2879
v9fs_link(void * opaque)2880 static void coroutine_fn v9fs_link(void *opaque)
2881 {
2882 V9fsPDU *pdu = opaque;
2883 int32_t dfid, oldfid;
2884 V9fsFidState *dfidp, *oldfidp;
2885 V9fsString name;
2886 size_t offset = 7;
2887 int err = 0;
2888
2889 v9fs_string_init(&name);
2890 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2891 if (err < 0) {
2892 goto out_nofid;
2893 }
2894 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2895
2896 if (name_is_illegal(name.data)) {
2897 err = -ENOENT;
2898 goto out_nofid;
2899 }
2900
2901 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2902 err = -EEXIST;
2903 goto out_nofid;
2904 }
2905
2906 dfidp = get_fid(pdu, dfid);
2907 if (dfidp == NULL) {
2908 err = -ENOENT;
2909 goto out_nofid;
2910 }
2911
2912 oldfidp = get_fid(pdu, oldfid);
2913 if (oldfidp == NULL) {
2914 err = -ENOENT;
2915 goto out;
2916 }
2917 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2918 if (!err) {
2919 err = offset;
2920 }
2921 put_fid(pdu, oldfidp);
2922 out:
2923 put_fid(pdu, dfidp);
2924 out_nofid:
2925 v9fs_string_free(&name);
2926 pdu_complete(pdu, err);
2927 }
2928
2929 /* Only works with path name based fid */
v9fs_remove(void * opaque)2930 static void coroutine_fn v9fs_remove(void *opaque)
2931 {
2932 int32_t fid;
2933 int err = 0;
2934 size_t offset = 7;
2935 V9fsFidState *fidp;
2936 V9fsPDU *pdu = opaque;
2937
2938 err = pdu_unmarshal(pdu, offset, "d", &fid);
2939 if (err < 0) {
2940 goto out_nofid;
2941 }
2942 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2943
2944 fidp = get_fid(pdu, fid);
2945 if (fidp == NULL) {
2946 err = -EINVAL;
2947 goto out_nofid;
2948 }
2949 /* if fs driver is not path based, return EOPNOTSUPP */
2950 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2951 err = -EOPNOTSUPP;
2952 goto out_err;
2953 }
2954 /*
2955 * IF the file is unlinked, we cannot reopen
2956 * the file later. So don't reclaim fd
2957 */
2958 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2959 if (err < 0) {
2960 goto out_err;
2961 }
2962 err = v9fs_co_remove(pdu, &fidp->path);
2963 if (!err) {
2964 err = offset;
2965 }
2966 out_err:
2967 /* For TREMOVE we need to clunk the fid even on failed remove */
2968 clunk_fid(pdu->s, fidp->fid);
2969 put_fid(pdu, fidp);
2970 out_nofid:
2971 pdu_complete(pdu, err);
2972 }
2973
v9fs_unlinkat(void * opaque)2974 static void coroutine_fn v9fs_unlinkat(void *opaque)
2975 {
2976 int err = 0;
2977 V9fsString name;
2978 int32_t dfid, flags, rflags = 0;
2979 size_t offset = 7;
2980 V9fsPath path;
2981 V9fsFidState *dfidp;
2982 V9fsPDU *pdu = opaque;
2983
2984 v9fs_string_init(&name);
2985 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2986 if (err < 0) {
2987 goto out_nofid;
2988 }
2989
2990 if (name_is_illegal(name.data)) {
2991 err = -ENOENT;
2992 goto out_nofid;
2993 }
2994
2995 if (!strcmp(".", name.data)) {
2996 err = -EINVAL;
2997 goto out_nofid;
2998 }
2999
3000 if (!strcmp("..", name.data)) {
3001 err = -ENOTEMPTY;
3002 goto out_nofid;
3003 }
3004
3005 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
3006 err = -EINVAL;
3007 goto out_nofid;
3008 }
3009
3010 if (flags & P9_DOTL_AT_REMOVEDIR) {
3011 rflags |= AT_REMOVEDIR;
3012 }
3013
3014 dfidp = get_fid(pdu, dfid);
3015 if (dfidp == NULL) {
3016 err = -EINVAL;
3017 goto out_nofid;
3018 }
3019 /*
3020 * IF the file is unlinked, we cannot reopen
3021 * the file later. So don't reclaim fd
3022 */
3023 v9fs_path_init(&path);
3024 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
3025 if (err < 0) {
3026 goto out_err;
3027 }
3028 err = v9fs_mark_fids_unreclaim(pdu, &path);
3029 if (err < 0) {
3030 goto out_err;
3031 }
3032 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
3033 if (!err) {
3034 err = offset;
3035 }
3036 out_err:
3037 put_fid(pdu, dfidp);
3038 v9fs_path_free(&path);
3039 out_nofid:
3040 pdu_complete(pdu, err);
3041 v9fs_string_free(&name);
3042 }
3043
3044
3045 /* Only works with path name based fid */
v9fs_complete_rename(V9fsPDU * pdu,V9fsFidState * fidp,int32_t newdirfid,V9fsString * name)3046 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
3047 int32_t newdirfid,
3048 V9fsString *name)
3049 {
3050 int err = 0;
3051 V9fsPath new_path;
3052 V9fsFidState *tfidp;
3053 V9fsState *s = pdu->s;
3054 V9fsFidState *dirfidp = NULL;
3055
3056 v9fs_path_init(&new_path);
3057 if (newdirfid != -1) {
3058 dirfidp = get_fid(pdu, newdirfid);
3059 if (dirfidp == NULL) {
3060 err = -ENOENT;
3061 goto out_nofid;
3062 }
3063 if (fidp->fid_type != P9_FID_NONE) {
3064 err = -EINVAL;
3065 goto out;
3066 }
3067 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
3068 if (err < 0) {
3069 goto out;
3070 }
3071 } else {
3072 char *dir_name = g_path_get_dirname(fidp->path.data);
3073 V9fsPath dir_path;
3074
3075 v9fs_path_init(&dir_path);
3076 v9fs_path_sprintf(&dir_path, "%s", dir_name);
3077 g_free(dir_name);
3078
3079 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
3080 v9fs_path_free(&dir_path);
3081 if (err < 0) {
3082 goto out;
3083 }
3084 }
3085 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
3086 if (err < 0) {
3087 goto out;
3088 }
3089 /*
3090 * Fixup fid's pointing to the old name to
3091 * start pointing to the new name
3092 */
3093 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3094 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
3095 /* replace the name */
3096 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
3097 }
3098 }
3099 out:
3100 if (dirfidp) {
3101 put_fid(pdu, dirfidp);
3102 }
3103 v9fs_path_free(&new_path);
3104 out_nofid:
3105 return err;
3106 }
3107
3108 /* Only works with path name based fid */
v9fs_rename(void * opaque)3109 static void coroutine_fn v9fs_rename(void *opaque)
3110 {
3111 int32_t fid;
3112 ssize_t err = 0;
3113 size_t offset = 7;
3114 V9fsString name;
3115 int32_t newdirfid;
3116 V9fsFidState *fidp;
3117 V9fsPDU *pdu = opaque;
3118 V9fsState *s = pdu->s;
3119
3120 v9fs_string_init(&name);
3121 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
3122 if (err < 0) {
3123 goto out_nofid;
3124 }
3125
3126 if (name_is_illegal(name.data)) {
3127 err = -ENOENT;
3128 goto out_nofid;
3129 }
3130
3131 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3132 err = -EISDIR;
3133 goto out_nofid;
3134 }
3135
3136 fidp = get_fid(pdu, fid);
3137 if (fidp == NULL) {
3138 err = -ENOENT;
3139 goto out_nofid;
3140 }
3141 if (fidp->fid_type != P9_FID_NONE) {
3142 err = -EINVAL;
3143 goto out;
3144 }
3145 /* if fs driver is not path based, return EOPNOTSUPP */
3146 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
3147 err = -EOPNOTSUPP;
3148 goto out;
3149 }
3150 v9fs_path_write_lock(s);
3151 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
3152 v9fs_path_unlock(s);
3153 if (!err) {
3154 err = offset;
3155 }
3156 out:
3157 put_fid(pdu, fidp);
3158 out_nofid:
3159 pdu_complete(pdu, err);
3160 v9fs_string_free(&name);
3161 }
3162
v9fs_fix_fid_paths(V9fsPDU * pdu,V9fsPath * olddir,V9fsString * old_name,V9fsPath * newdir,V9fsString * new_name)3163 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
3164 V9fsString *old_name,
3165 V9fsPath *newdir,
3166 V9fsString *new_name)
3167 {
3168 V9fsFidState *tfidp;
3169 V9fsPath oldpath, newpath;
3170 V9fsState *s = pdu->s;
3171 int err;
3172
3173 v9fs_path_init(&oldpath);
3174 v9fs_path_init(&newpath);
3175 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
3176 if (err < 0) {
3177 goto out;
3178 }
3179 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
3180 if (err < 0) {
3181 goto out;
3182 }
3183
3184 /*
3185 * Fixup fid's pointing to the old name to
3186 * start pointing to the new name
3187 */
3188 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3189 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
3190 /* replace the name */
3191 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
3192 }
3193 }
3194 out:
3195 v9fs_path_free(&oldpath);
3196 v9fs_path_free(&newpath);
3197 return err;
3198 }
3199
v9fs_complete_renameat(V9fsPDU * pdu,int32_t olddirfid,V9fsString * old_name,int32_t newdirfid,V9fsString * new_name)3200 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
3201 V9fsString *old_name,
3202 int32_t newdirfid,
3203 V9fsString *new_name)
3204 {
3205 int err = 0;
3206 V9fsState *s = pdu->s;
3207 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
3208
3209 olddirfidp = get_fid(pdu, olddirfid);
3210 if (olddirfidp == NULL) {
3211 err = -ENOENT;
3212 goto out;
3213 }
3214 if (newdirfid != -1) {
3215 newdirfidp = get_fid(pdu, newdirfid);
3216 if (newdirfidp == NULL) {
3217 err = -ENOENT;
3218 goto out;
3219 }
3220 } else {
3221 newdirfidp = get_fid(pdu, olddirfid);
3222 }
3223
3224 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
3225 &newdirfidp->path, new_name);
3226 if (err < 0) {
3227 goto out;
3228 }
3229 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
3230 /* Only for path based fid we need to do the below fixup */
3231 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
3232 &newdirfidp->path, new_name);
3233 }
3234 out:
3235 if (olddirfidp) {
3236 put_fid(pdu, olddirfidp);
3237 }
3238 if (newdirfidp) {
3239 put_fid(pdu, newdirfidp);
3240 }
3241 return err;
3242 }
3243
v9fs_renameat(void * opaque)3244 static void coroutine_fn v9fs_renameat(void *opaque)
3245 {
3246 ssize_t err = 0;
3247 size_t offset = 7;
3248 V9fsPDU *pdu = opaque;
3249 V9fsState *s = pdu->s;
3250 int32_t olddirfid, newdirfid;
3251 V9fsString old_name, new_name;
3252
3253 v9fs_string_init(&old_name);
3254 v9fs_string_init(&new_name);
3255 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
3256 &old_name, &newdirfid, &new_name);
3257 if (err < 0) {
3258 goto out_err;
3259 }
3260
3261 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3262 err = -ENOENT;
3263 goto out_err;
3264 }
3265
3266 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3267 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3268 err = -EISDIR;
3269 goto out_err;
3270 }
3271
3272 v9fs_path_write_lock(s);
3273 err = v9fs_complete_renameat(pdu, olddirfid,
3274 &old_name, newdirfid, &new_name);
3275 v9fs_path_unlock(s);
3276 if (!err) {
3277 err = offset;
3278 }
3279
3280 out_err:
3281 pdu_complete(pdu, err);
3282 v9fs_string_free(&old_name);
3283 v9fs_string_free(&new_name);
3284 }
3285
v9fs_wstat(void * opaque)3286 static void coroutine_fn v9fs_wstat(void *opaque)
3287 {
3288 int32_t fid;
3289 int err = 0;
3290 int16_t unused;
3291 V9fsStat v9stat;
3292 size_t offset = 7;
3293 struct stat stbuf;
3294 V9fsFidState *fidp;
3295 V9fsPDU *pdu = opaque;
3296 V9fsState *s = pdu->s;
3297
3298 v9fs_stat_init(&v9stat);
3299 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3300 if (err < 0) {
3301 goto out_nofid;
3302 }
3303 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3304 v9stat.mode, v9stat.atime, v9stat.mtime);
3305
3306 fidp = get_fid(pdu, fid);
3307 if (fidp == NULL) {
3308 err = -EINVAL;
3309 goto out_nofid;
3310 }
3311 /* do we need to sync the file? */
3312 if (donttouch_stat(&v9stat)) {
3313 err = v9fs_co_fsync(pdu, fidp, 0);
3314 goto out;
3315 }
3316 if (v9stat.mode != -1) {
3317 uint32_t v9_mode;
3318 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
3319 if (err < 0) {
3320 goto out;
3321 }
3322 v9_mode = stat_to_v9mode(&stbuf);
3323 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3324 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3325 /* Attempting to change the type */
3326 err = -EIO;
3327 goto out;
3328 }
3329 err = v9fs_co_chmod(pdu, &fidp->path,
3330 v9mode_to_mode(v9stat.mode,
3331 &v9stat.extension));
3332 if (err < 0) {
3333 goto out;
3334 }
3335 }
3336 if (v9stat.mtime != -1 || v9stat.atime != -1) {
3337 struct timespec times[2];
3338 if (v9stat.atime != -1) {
3339 times[0].tv_sec = v9stat.atime;
3340 times[0].tv_nsec = 0;
3341 } else {
3342 times[0].tv_nsec = UTIME_OMIT;
3343 }
3344 if (v9stat.mtime != -1) {
3345 times[1].tv_sec = v9stat.mtime;
3346 times[1].tv_nsec = 0;
3347 } else {
3348 times[1].tv_nsec = UTIME_OMIT;
3349 }
3350 err = v9fs_co_utimensat(pdu, &fidp->path, times);
3351 if (err < 0) {
3352 goto out;
3353 }
3354 }
3355 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
3356 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
3357 if (err < 0) {
3358 goto out;
3359 }
3360 }
3361 if (v9stat.name.size != 0) {
3362 v9fs_path_write_lock(s);
3363 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
3364 v9fs_path_unlock(s);
3365 if (err < 0) {
3366 goto out;
3367 }
3368 }
3369 if (v9stat.length != -1) {
3370 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
3371 if (err < 0) {
3372 goto out;
3373 }
3374 }
3375 err = offset;
3376 out:
3377 put_fid(pdu, fidp);
3378 out_nofid:
3379 v9fs_stat_free(&v9stat);
3380 pdu_complete(pdu, err);
3381 }
3382
v9fs_fill_statfs(V9fsState * s,V9fsPDU * pdu,struct statfs * stbuf)3383 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3384 {
3385 uint32_t f_type;
3386 uint32_t f_bsize;
3387 uint64_t f_blocks;
3388 uint64_t f_bfree;
3389 uint64_t f_bavail;
3390 uint64_t f_files;
3391 uint64_t f_ffree;
3392 uint64_t fsid_val;
3393 uint32_t f_namelen;
3394 size_t offset = 7;
3395 int32_t bsize_factor;
3396
3397 /*
3398 * compute bsize factor based on host file system block size
3399 * and client msize
3400 */
3401 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
3402 if (!bsize_factor) {
3403 bsize_factor = 1;
3404 }
3405 f_type = stbuf->f_type;
3406 f_bsize = stbuf->f_bsize;
3407 f_bsize *= bsize_factor;
3408 /*
3409 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3410 * adjust(divide) the number of blocks, free blocks and available
3411 * blocks by bsize factor
3412 */
3413 f_blocks = stbuf->f_blocks/bsize_factor;
3414 f_bfree = stbuf->f_bfree/bsize_factor;
3415 f_bavail = stbuf->f_bavail/bsize_factor;
3416 f_files = stbuf->f_files;
3417 f_ffree = stbuf->f_ffree;
3418 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3419 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3420 f_namelen = stbuf->f_namelen;
3421
3422 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3423 f_type, f_bsize, f_blocks, f_bfree,
3424 f_bavail, f_files, f_ffree,
3425 fsid_val, f_namelen);
3426 }
3427
v9fs_statfs(void * opaque)3428 static void coroutine_fn v9fs_statfs(void *opaque)
3429 {
3430 int32_t fid;
3431 ssize_t retval = 0;
3432 size_t offset = 7;
3433 V9fsFidState *fidp;
3434 struct statfs stbuf;
3435 V9fsPDU *pdu = opaque;
3436 V9fsState *s = pdu->s;
3437
3438 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3439 if (retval < 0) {
3440 goto out_nofid;
3441 }
3442 fidp = get_fid(pdu, fid);
3443 if (fidp == NULL) {
3444 retval = -ENOENT;
3445 goto out_nofid;
3446 }
3447 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3448 if (retval < 0) {
3449 goto out;
3450 }
3451 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3452 if (retval < 0) {
3453 goto out;
3454 }
3455 retval += offset;
3456 out:
3457 put_fid(pdu, fidp);
3458 out_nofid:
3459 pdu_complete(pdu, retval);
3460 }
3461
v9fs_mknod(void * opaque)3462 static void coroutine_fn v9fs_mknod(void *opaque)
3463 {
3464
3465 int mode;
3466 gid_t gid;
3467 int32_t fid;
3468 V9fsQID qid;
3469 int err = 0;
3470 int major, minor;
3471 size_t offset = 7;
3472 V9fsString name;
3473 struct stat stbuf;
3474 V9fsFidState *fidp;
3475 V9fsPDU *pdu = opaque;
3476
3477 v9fs_string_init(&name);
3478 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3479 &major, &minor, &gid);
3480 if (err < 0) {
3481 goto out_nofid;
3482 }
3483 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3484
3485 if (name_is_illegal(name.data)) {
3486 err = -ENOENT;
3487 goto out_nofid;
3488 }
3489
3490 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3491 err = -EEXIST;
3492 goto out_nofid;
3493 }
3494
3495 fidp = get_fid(pdu, fid);
3496 if (fidp == NULL) {
3497 err = -ENOENT;
3498 goto out_nofid;
3499 }
3500 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3501 makedev(major, minor), mode, &stbuf);
3502 if (err < 0) {
3503 goto out;
3504 }
3505 err = stat_to_qid(pdu, &stbuf, &qid);
3506 if (err < 0) {
3507 goto out;
3508 }
3509 err = pdu_marshal(pdu, offset, "Q", &qid);
3510 if (err < 0) {
3511 goto out;
3512 }
3513 err += offset;
3514 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3515 qid.type, qid.version, qid.path);
3516 out:
3517 put_fid(pdu, fidp);
3518 out_nofid:
3519 pdu_complete(pdu, err);
3520 v9fs_string_free(&name);
3521 }
3522
3523 /*
3524 * Implement posix byte range locking code
3525 * Server side handling of locking code is very simple, because 9p server in
3526 * QEMU can handle only one client. And most of the lock handling
3527 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3528 * do any thing in * qemu 9p server side lock code path.
3529 * So when a TLOCK request comes, always return success
3530 */
v9fs_lock(void * opaque)3531 static void coroutine_fn v9fs_lock(void *opaque)
3532 {
3533 V9fsFlock flock;
3534 size_t offset = 7;
3535 struct stat stbuf;
3536 V9fsFidState *fidp;
3537 int32_t fid, err = 0;
3538 V9fsPDU *pdu = opaque;
3539
3540 v9fs_string_init(&flock.client_id);
3541 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3542 &flock.flags, &flock.start, &flock.length,
3543 &flock.proc_id, &flock.client_id);
3544 if (err < 0) {
3545 goto out_nofid;
3546 }
3547 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3548 flock.type, flock.start, flock.length);
3549
3550
3551 /* We support only block flag now (that too ignored currently) */
3552 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3553 err = -EINVAL;
3554 goto out_nofid;
3555 }
3556 fidp = get_fid(pdu, fid);
3557 if (fidp == NULL) {
3558 err = -ENOENT;
3559 goto out_nofid;
3560 }
3561 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3562 if (err < 0) {
3563 goto out;
3564 }
3565 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3566 if (err < 0) {
3567 goto out;
3568 }
3569 err += offset;
3570 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3571 out:
3572 put_fid(pdu, fidp);
3573 out_nofid:
3574 pdu_complete(pdu, err);
3575 v9fs_string_free(&flock.client_id);
3576 }
3577
3578 /*
3579 * When a TGETLOCK request comes, always return success because all lock
3580 * handling is done by client's VFS layer.
3581 */
v9fs_getlock(void * opaque)3582 static void coroutine_fn v9fs_getlock(void *opaque)
3583 {
3584 size_t offset = 7;
3585 struct stat stbuf;
3586 V9fsFidState *fidp;
3587 V9fsGetlock glock;
3588 int32_t fid, err = 0;
3589 V9fsPDU *pdu = opaque;
3590
3591 v9fs_string_init(&glock.client_id);
3592 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3593 &glock.start, &glock.length, &glock.proc_id,
3594 &glock.client_id);
3595 if (err < 0) {
3596 goto out_nofid;
3597 }
3598 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3599 glock.type, glock.start, glock.length);
3600
3601 fidp = get_fid(pdu, fid);
3602 if (fidp == NULL) {
3603 err = -ENOENT;
3604 goto out_nofid;
3605 }
3606 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3607 if (err < 0) {
3608 goto out;
3609 }
3610 glock.type = P9_LOCK_TYPE_UNLCK;
3611 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3612 glock.start, glock.length, glock.proc_id,
3613 &glock.client_id);
3614 if (err < 0) {
3615 goto out;
3616 }
3617 err += offset;
3618 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3619 glock.length, glock.proc_id);
3620 out:
3621 put_fid(pdu, fidp);
3622 out_nofid:
3623 pdu_complete(pdu, err);
3624 v9fs_string_free(&glock.client_id);
3625 }
3626
v9fs_mkdir(void * opaque)3627 static void coroutine_fn v9fs_mkdir(void *opaque)
3628 {
3629 V9fsPDU *pdu = opaque;
3630 size_t offset = 7;
3631 int32_t fid;
3632 struct stat stbuf;
3633 V9fsQID qid;
3634 V9fsString name;
3635 V9fsFidState *fidp;
3636 gid_t gid;
3637 int mode;
3638 int err = 0;
3639
3640 v9fs_string_init(&name);
3641 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3642 if (err < 0) {
3643 goto out_nofid;
3644 }
3645 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3646
3647 if (name_is_illegal(name.data)) {
3648 err = -ENOENT;
3649 goto out_nofid;
3650 }
3651
3652 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3653 err = -EEXIST;
3654 goto out_nofid;
3655 }
3656
3657 fidp = get_fid(pdu, fid);
3658 if (fidp == NULL) {
3659 err = -ENOENT;
3660 goto out_nofid;
3661 }
3662 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3663 if (err < 0) {
3664 goto out;
3665 }
3666 err = stat_to_qid(pdu, &stbuf, &qid);
3667 if (err < 0) {
3668 goto out;
3669 }
3670 err = pdu_marshal(pdu, offset, "Q", &qid);
3671 if (err < 0) {
3672 goto out;
3673 }
3674 err += offset;
3675 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3676 qid.type, qid.version, qid.path, err);
3677 out:
3678 put_fid(pdu, fidp);
3679 out_nofid:
3680 pdu_complete(pdu, err);
3681 v9fs_string_free(&name);
3682 }
3683
v9fs_xattrwalk(void * opaque)3684 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3685 {
3686 int64_t size;
3687 V9fsString name;
3688 ssize_t err = 0;
3689 size_t offset = 7;
3690 int32_t fid, newfid;
3691 V9fsFidState *file_fidp;
3692 V9fsFidState *xattr_fidp = NULL;
3693 V9fsPDU *pdu = opaque;
3694 V9fsState *s = pdu->s;
3695
3696 v9fs_string_init(&name);
3697 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3698 if (err < 0) {
3699 goto out_nofid;
3700 }
3701 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3702
3703 file_fidp = get_fid(pdu, fid);
3704 if (file_fidp == NULL) {
3705 err = -ENOENT;
3706 goto out_nofid;
3707 }
3708 xattr_fidp = alloc_fid(s, newfid);
3709 if (xattr_fidp == NULL) {
3710 err = -EINVAL;
3711 goto out;
3712 }
3713 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3714 if (!v9fs_string_size(&name)) {
3715 /*
3716 * listxattr request. Get the size first
3717 */
3718 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3719 if (size < 0) {
3720 err = size;
3721 clunk_fid(s, xattr_fidp->fid);
3722 goto out;
3723 }
3724 /*
3725 * Read the xattr value
3726 */
3727 xattr_fidp->fs.xattr.len = size;
3728 xattr_fidp->fid_type = P9_FID_XATTR;
3729 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3730 xattr_fidp->fs.xattr.value = g_malloc0(size);
3731 if (size) {
3732 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3733 xattr_fidp->fs.xattr.value,
3734 xattr_fidp->fs.xattr.len);
3735 if (err < 0) {
3736 clunk_fid(s, xattr_fidp->fid);
3737 goto out;
3738 }
3739 }
3740 err = pdu_marshal(pdu, offset, "q", size);
3741 if (err < 0) {
3742 goto out;
3743 }
3744 err += offset;
3745 } else {
3746 /*
3747 * specific xattr fid. We check for xattr
3748 * presence also collect the xattr size
3749 */
3750 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3751 &name, NULL, 0);
3752 if (size < 0) {
3753 err = size;
3754 clunk_fid(s, xattr_fidp->fid);
3755 goto out;
3756 }
3757 /*
3758 * Read the xattr value
3759 */
3760 xattr_fidp->fs.xattr.len = size;
3761 xattr_fidp->fid_type = P9_FID_XATTR;
3762 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3763 xattr_fidp->fs.xattr.value = g_malloc0(size);
3764 if (size) {
3765 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3766 &name, xattr_fidp->fs.xattr.value,
3767 xattr_fidp->fs.xattr.len);
3768 if (err < 0) {
3769 clunk_fid(s, xattr_fidp->fid);
3770 goto out;
3771 }
3772 }
3773 err = pdu_marshal(pdu, offset, "q", size);
3774 if (err < 0) {
3775 goto out;
3776 }
3777 err += offset;
3778 }
3779 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3780 out:
3781 put_fid(pdu, file_fidp);
3782 if (xattr_fidp) {
3783 put_fid(pdu, xattr_fidp);
3784 }
3785 out_nofid:
3786 pdu_complete(pdu, err);
3787 v9fs_string_free(&name);
3788 }
3789
v9fs_xattrcreate(void * opaque)3790 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3791 {
3792 int flags, rflags = 0;
3793 int32_t fid;
3794 uint64_t size;
3795 ssize_t err = 0;
3796 V9fsString name;
3797 size_t offset = 7;
3798 V9fsFidState *file_fidp;
3799 V9fsFidState *xattr_fidp;
3800 V9fsPDU *pdu = opaque;
3801
3802 v9fs_string_init(&name);
3803 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3804 if (err < 0) {
3805 goto out_nofid;
3806 }
3807 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3808
3809 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3810 err = -EINVAL;
3811 goto out_nofid;
3812 }
3813
3814 if (flags & P9_XATTR_CREATE) {
3815 rflags |= XATTR_CREATE;
3816 }
3817
3818 if (flags & P9_XATTR_REPLACE) {
3819 rflags |= XATTR_REPLACE;
3820 }
3821
3822 if (size > XATTR_SIZE_MAX) {
3823 err = -E2BIG;
3824 goto out_nofid;
3825 }
3826
3827 file_fidp = get_fid(pdu, fid);
3828 if (file_fidp == NULL) {
3829 err = -EINVAL;
3830 goto out_nofid;
3831 }
3832 if (file_fidp->fid_type != P9_FID_NONE) {
3833 err = -EINVAL;
3834 goto out_put_fid;
3835 }
3836
3837 /* Make the file fid point to xattr */
3838 xattr_fidp = file_fidp;
3839 xattr_fidp->fid_type = P9_FID_XATTR;
3840 xattr_fidp->fs.xattr.copied_len = 0;
3841 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3842 xattr_fidp->fs.xattr.len = size;
3843 xattr_fidp->fs.xattr.flags = rflags;
3844 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3845 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3846 xattr_fidp->fs.xattr.value = g_malloc0(size);
3847 err = offset;
3848 out_put_fid:
3849 put_fid(pdu, file_fidp);
3850 out_nofid:
3851 pdu_complete(pdu, err);
3852 v9fs_string_free(&name);
3853 }
3854
v9fs_readlink(void * opaque)3855 static void coroutine_fn v9fs_readlink(void *opaque)
3856 {
3857 V9fsPDU *pdu = opaque;
3858 size_t offset = 7;
3859 V9fsString target;
3860 int32_t fid;
3861 int err = 0;
3862 V9fsFidState *fidp;
3863
3864 err = pdu_unmarshal(pdu, offset, "d", &fid);
3865 if (err < 0) {
3866 goto out_nofid;
3867 }
3868 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3869 fidp = get_fid(pdu, fid);
3870 if (fidp == NULL) {
3871 err = -ENOENT;
3872 goto out_nofid;
3873 }
3874
3875 v9fs_string_init(&target);
3876 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3877 if (err < 0) {
3878 goto out;
3879 }
3880 err = pdu_marshal(pdu, offset, "s", &target);
3881 if (err < 0) {
3882 v9fs_string_free(&target);
3883 goto out;
3884 }
3885 err += offset;
3886 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3887 v9fs_string_free(&target);
3888 out:
3889 put_fid(pdu, fidp);
3890 out_nofid:
3891 pdu_complete(pdu, err);
3892 }
3893
3894 static CoroutineEntry *pdu_co_handlers[] = {
3895 [P9_TREADDIR] = v9fs_readdir,
3896 [P9_TSTATFS] = v9fs_statfs,
3897 [P9_TGETATTR] = v9fs_getattr,
3898 [P9_TSETATTR] = v9fs_setattr,
3899 [P9_TXATTRWALK] = v9fs_xattrwalk,
3900 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3901 [P9_TMKNOD] = v9fs_mknod,
3902 [P9_TRENAME] = v9fs_rename,
3903 [P9_TLOCK] = v9fs_lock,
3904 [P9_TGETLOCK] = v9fs_getlock,
3905 [P9_TRENAMEAT] = v9fs_renameat,
3906 [P9_TREADLINK] = v9fs_readlink,
3907 [P9_TUNLINKAT] = v9fs_unlinkat,
3908 [P9_TMKDIR] = v9fs_mkdir,
3909 [P9_TVERSION] = v9fs_version,
3910 [P9_TLOPEN] = v9fs_open,
3911 [P9_TATTACH] = v9fs_attach,
3912 [P9_TSTAT] = v9fs_stat,
3913 [P9_TWALK] = v9fs_walk,
3914 [P9_TCLUNK] = v9fs_clunk,
3915 [P9_TFSYNC] = v9fs_fsync,
3916 [P9_TOPEN] = v9fs_open,
3917 [P9_TREAD] = v9fs_read,
3918 #if 0
3919 [P9_TAUTH] = v9fs_auth,
3920 #endif
3921 [P9_TFLUSH] = v9fs_flush,
3922 [P9_TLINK] = v9fs_link,
3923 [P9_TSYMLINK] = v9fs_symlink,
3924 [P9_TCREATE] = v9fs_create,
3925 [P9_TLCREATE] = v9fs_lcreate,
3926 [P9_TWRITE] = v9fs_write,
3927 [P9_TWSTAT] = v9fs_wstat,
3928 [P9_TREMOVE] = v9fs_remove,
3929 };
3930
v9fs_op_not_supp(void * opaque)3931 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3932 {
3933 V9fsPDU *pdu = opaque;
3934 pdu_complete(pdu, -EOPNOTSUPP);
3935 }
3936
v9fs_fs_ro(void * opaque)3937 static void coroutine_fn v9fs_fs_ro(void *opaque)
3938 {
3939 V9fsPDU *pdu = opaque;
3940 pdu_complete(pdu, -EROFS);
3941 }
3942
is_read_only_op(V9fsPDU * pdu)3943 static inline bool is_read_only_op(V9fsPDU *pdu)
3944 {
3945 switch (pdu->id) {
3946 case P9_TREADDIR:
3947 case P9_TSTATFS:
3948 case P9_TGETATTR:
3949 case P9_TXATTRWALK:
3950 case P9_TLOCK:
3951 case P9_TGETLOCK:
3952 case P9_TREADLINK:
3953 case P9_TVERSION:
3954 case P9_TLOPEN:
3955 case P9_TATTACH:
3956 case P9_TSTAT:
3957 case P9_TWALK:
3958 case P9_TCLUNK:
3959 case P9_TFSYNC:
3960 case P9_TOPEN:
3961 case P9_TREAD:
3962 case P9_TAUTH:
3963 case P9_TFLUSH:
3964 return 1;
3965 default:
3966 return 0;
3967 }
3968 }
3969
pdu_submit(V9fsPDU * pdu,P9MsgHeader * hdr)3970 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3971 {
3972 Coroutine *co;
3973 CoroutineEntry *handler;
3974 V9fsState *s = pdu->s;
3975
3976 pdu->size = le32_to_cpu(hdr->size_le);
3977 pdu->id = hdr->id;
3978 pdu->tag = le16_to_cpu(hdr->tag_le);
3979
3980 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3981 (pdu_co_handlers[pdu->id] == NULL)) {
3982 handler = v9fs_op_not_supp;
3983 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3984 handler = v9fs_fs_ro;
3985 } else {
3986 handler = pdu_co_handlers[pdu->id];
3987 }
3988
3989 qemu_co_queue_init(&pdu->complete);
3990 co = qemu_coroutine_create(handler, pdu);
3991 qemu_coroutine_enter(co);
3992 }
3993
3994 /* Returns 0 on success, 1 on failure. */
v9fs_device_realize_common(V9fsState * s,const V9fsTransport * t,Error ** errp)3995 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3996 Error **errp)
3997 {
3998 int i, len;
3999 struct stat stat;
4000 FsDriverEntry *fse;
4001 V9fsPath path;
4002 int rc = 1;
4003
4004 assert(!s->transport);
4005 s->transport = t;
4006
4007 /* initialize pdu allocator */
4008 QLIST_INIT(&s->free_list);
4009 QLIST_INIT(&s->active_list);
4010 for (i = 0; i < MAX_REQ; i++) {
4011 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4012 s->pdus[i].s = s;
4013 s->pdus[i].idx = i;
4014 }
4015
4016 v9fs_path_init(&path);
4017
4018 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
4019
4020 if (!fse) {
4021 /* We don't have a fsdev identified by fsdev_id */
4022 error_setg(errp, "9pfs device couldn't find fsdev with the "
4023 "id = %s",
4024 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
4025 goto out;
4026 }
4027
4028 if (!s->fsconf.tag) {
4029 /* we haven't specified a mount_tag */
4030 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
4031 s->fsconf.fsdev_id);
4032 goto out;
4033 }
4034
4035 s->ctx.export_flags = fse->export_flags;
4036 s->ctx.fs_root = g_strdup(fse->path);
4037 s->ctx.exops.get_st_gen = NULL;
4038 len = strlen(s->fsconf.tag);
4039 if (len > MAX_TAG_LEN - 1) {
4040 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
4041 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
4042 goto out;
4043 }
4044
4045 s->tag = g_strdup(s->fsconf.tag);
4046 s->ctx.uid = -1;
4047
4048 s->ops = fse->ops;
4049
4050 s->ctx.fmode = fse->fmode;
4051 s->ctx.dmode = fse->dmode;
4052
4053 s->fid_list = NULL;
4054 qemu_co_rwlock_init(&s->rename_lock);
4055
4056 if (s->ops->init(&s->ctx, errp) < 0) {
4057 error_prepend(errp, "cannot initialize fsdev '%s': ",
4058 s->fsconf.fsdev_id);
4059 goto out;
4060 }
4061
4062 /*
4063 * Check details of export path, We need to use fs driver
4064 * call back to do that. Since we are in the init path, we don't
4065 * use co-routines here.
4066 */
4067 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
4068 error_setg(errp,
4069 "error in converting name to path %s", strerror(errno));
4070 goto out;
4071 }
4072 if (s->ops->lstat(&s->ctx, &path, &stat)) {
4073 error_setg(errp, "share path %s does not exist", fse->path);
4074 goto out;
4075 } else if (!S_ISDIR(stat.st_mode)) {
4076 error_setg(errp, "share path %s is not a directory", fse->path);
4077 goto out;
4078 }
4079
4080 s->dev_id = stat.st_dev;
4081
4082 /* init inode remapping : */
4083 /* hash table for variable length inode suffixes */
4084 qpd_table_init(&s->qpd_table);
4085 /* hash table for slow/full inode remapping (most users won't need it) */
4086 qpf_table_init(&s->qpf_table);
4087 /* hash table for quick inode remapping */
4088 qpp_table_init(&s->qpp_table);
4089 s->qp_ndevices = 0;
4090 s->qp_affix_next = 1; /* reserve 0 to detect overflow */
4091 s->qp_fullpath_next = 1;
4092
4093 s->ctx.fst = &fse->fst;
4094 fsdev_throttle_init(s->ctx.fst);
4095
4096 rc = 0;
4097 out:
4098 if (rc) {
4099 v9fs_device_unrealize_common(s, NULL);
4100 }
4101 v9fs_path_free(&path);
4102 return rc;
4103 }
4104
v9fs_device_unrealize_common(V9fsState * s,Error ** errp)4105 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
4106 {
4107 if (s->ops && s->ops->cleanup) {
4108 s->ops->cleanup(&s->ctx);
4109 }
4110 if (s->ctx.fst) {
4111 fsdev_throttle_cleanup(s->ctx.fst);
4112 }
4113 g_free(s->tag);
4114 qp_table_destroy(&s->qpd_table);
4115 qp_table_destroy(&s->qpp_table);
4116 qp_table_destroy(&s->qpf_table);
4117 g_free(s->ctx.fs_root);
4118 }
4119
4120 typedef struct VirtfsCoResetData {
4121 V9fsPDU pdu;
4122 bool done;
4123 } VirtfsCoResetData;
4124
virtfs_co_reset(void * opaque)4125 static void coroutine_fn virtfs_co_reset(void *opaque)
4126 {
4127 VirtfsCoResetData *data = opaque;
4128
4129 virtfs_reset(&data->pdu);
4130 data->done = true;
4131 }
4132
v9fs_reset(V9fsState * s)4133 void v9fs_reset(V9fsState *s)
4134 {
4135 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
4136 Coroutine *co;
4137
4138 while (!QLIST_EMPTY(&s->active_list)) {
4139 aio_poll(qemu_get_aio_context(), true);
4140 }
4141
4142 co = qemu_coroutine_create(virtfs_co_reset, &data);
4143 qemu_coroutine_enter(co);
4144
4145 while (!data.done) {
4146 aio_poll(qemu_get_aio_context(), true);
4147 }
4148 }
4149
v9fs_set_fd_limit(void)4150 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
4151 {
4152 struct rlimit rlim;
4153 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
4154 error_report("Failed to get the resource limit");
4155 exit(1);
4156 }
4157 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
4158 open_fd_rc = rlim.rlim_cur/2;
4159 }
4160