4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
19 #include "qemu/osdep.h"
20 #include <glib/gprintf.h>
21 #include "hw/virtio/virtio.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/sockets.h"
27 #include "virtio-9p.h"
28 #include "fsdev/qemu-fsdev.h"
32 #include "migration/blocker.h"
33 #include "qemu/xxhash.h"
36 #include <linux/limits.h>
43 static int open_fd_rc
;
57 P9ARRAY_DEFINE_TYPE(V9fsPath
, v9fs_path_free
);
59 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
65 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
71 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
77 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
83 static int omode_to_uflags(int8_t mode
)
106 if (mode
& Oappend
) {
117 typedef struct DotlOpenflagMap
{
122 static int dotl_to_open_flags(int flags
)
126 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
127 * and P9_DOTL_NOACCESS
129 int oflags
= flags
& O_ACCMODE
;
131 DotlOpenflagMap dotl_oflag_map
[] = {
132 { P9_DOTL_CREATE
, O_CREAT
},
133 { P9_DOTL_EXCL
, O_EXCL
},
134 { P9_DOTL_NOCTTY
, O_NOCTTY
},
135 { P9_DOTL_TRUNC
, O_TRUNC
},
136 { P9_DOTL_APPEND
, O_APPEND
},
137 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
138 { P9_DOTL_DSYNC
, O_DSYNC
},
139 { P9_DOTL_FASYNC
, FASYNC
},
140 { P9_DOTL_DIRECT
, O_DIRECT
},
141 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
142 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
143 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
144 { P9_DOTL_NOATIME
, O_NOATIME
},
145 { P9_DOTL_SYNC
, O_SYNC
},
148 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
149 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
150 oflags
|= dotl_oflag_map
[i
].open_flag
;
157 void cred_init(FsCred
*credp
)
165 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
169 * Filter the client open flags
171 flags
= dotl_to_open_flags(oflags
);
172 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
174 * Ignore direct disk access hint until the server supports it.
180 void v9fs_path_init(V9fsPath
*path
)
186 void v9fs_path_free(V9fsPath
*path
)
194 void GCC_FMT_ATTR(2, 3)
195 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
199 v9fs_path_free(path
);
202 /* Bump the size for including terminating NULL */
203 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
207 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
210 dst
->size
= src
->size
;
211 dst
->data
= g_memdup(src
->data
, src
->size
);
214 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
215 const char *name
, V9fsPath
*path
)
218 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
226 * Return TRUE if s1 is an ancestor of s2.
228 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
229 * As a special case, We treat s1 as ancestor of s2 if they are same!
231 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
233 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
234 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
241 static size_t v9fs_string_size(V9fsString
*str
)
247 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
248 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
251 if (f
->fid_type
== P9_FID_FILE
) {
252 if (f
->fs
.fd
== -1) {
254 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
255 } while (err
== -EINTR
&& !pdu
->cancelled
);
257 } else if (f
->fid_type
== P9_FID_DIR
) {
258 if (f
->fs
.dir
.stream
== NULL
) {
260 err
= v9fs_co_opendir(pdu
, f
);
261 } while (err
== -EINTR
&& !pdu
->cancelled
);
267 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
271 V9fsState
*s
= pdu
->s
;
273 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
277 * Update the fid ref upfront so that
278 * we don't get reclaimed when we yield
283 * check whether we need to reopen the
284 * file. We might have closed the fd
285 * while trying to free up some file
288 err
= v9fs_reopen_fid(pdu
, f
);
294 * Mark the fid as referenced so that the LRU
295 * reclaim won't close the file descriptor
297 f
->flags
|= FID_REFERENCED
;
304 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
308 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
309 /* If fid is already there return NULL */
315 f
= g_malloc0(sizeof(V9fsFidState
));
317 f
->fid_type
= P9_FID_NONE
;
320 * Mark the fid as referenced so that the LRU
321 * reclaim won't close the file descriptor
323 f
->flags
|= FID_REFERENCED
;
324 QSIMPLEQ_INSERT_TAIL(&s
->fid_list
, f
, next
);
326 v9fs_readdir_init(s
->proto_version
, &f
->fs
.dir
);
327 v9fs_readdir_init(s
->proto_version
, &f
->fs_reclaim
.dir
);
332 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
336 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
337 /* getxattr/listxattr fid */
341 * if this is fid for setxattr. clunk should
342 * result in setxattr localcall
344 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
345 /* clunk after partial write */
349 if (fidp
->fs
.xattr
.len
) {
350 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
351 fidp
->fs
.xattr
.value
,
353 fidp
->fs
.xattr
.flags
);
355 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
358 v9fs_string_free(&fidp
->fs
.xattr
.name
);
360 g_free(fidp
->fs
.xattr
.value
);
364 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
368 if (fidp
->fid_type
== P9_FID_FILE
) {
369 /* If we reclaimed the fd no need to close */
370 if (fidp
->fs
.fd
!= -1) {
371 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
373 } else if (fidp
->fid_type
== P9_FID_DIR
) {
374 if (fidp
->fs
.dir
.stream
!= NULL
) {
375 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
377 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
378 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
380 v9fs_path_free(&fidp
->path
);
385 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
390 * Don't free the fid if it is in reclaim list
392 if (!fidp
->ref
&& fidp
->clunked
) {
393 if (fidp
->fid
== pdu
->s
->root_fid
) {
395 * if the clunked fid is root fid then we
396 * have unmounted the fs on the client side.
397 * delete the migration blocker. Ideally, this
398 * should be hooked to transport close notification
400 if (pdu
->s
->migration_blocker
) {
401 migrate_del_blocker(pdu
->s
->migration_blocker
);
402 error_free(pdu
->s
->migration_blocker
);
403 pdu
->s
->migration_blocker
= NULL
;
406 return free_fid(pdu
, fidp
);
411 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
415 QSIMPLEQ_FOREACH(fidp
, &s
->fid_list
, next
) {
416 if (fidp
->fid
== fid
) {
417 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
418 fidp
->clunked
= true;
425 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
427 int reclaim_count
= 0;
428 V9fsState
*s
= pdu
->s
;
430 QSLIST_HEAD(, V9fsFidState
) reclaim_list
=
431 QSLIST_HEAD_INITIALIZER(reclaim_list
);
433 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
435 * Unlink fids cannot be reclaimed. Check
436 * for them and skip them. Also skip fids
437 * currently being operated on.
439 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
443 * if it is a recently referenced fid
444 * we leave the fid untouched and clear the
445 * reference bit. We come back to it later
446 * in the next iteration. (a simple LRU without
447 * moving list elements around)
449 if (f
->flags
& FID_REFERENCED
) {
450 f
->flags
&= ~FID_REFERENCED
;
454 * Add fids to reclaim list.
456 if (f
->fid_type
== P9_FID_FILE
) {
457 if (f
->fs
.fd
!= -1) {
459 * Up the reference count so that
460 * a clunk request won't free this fid
463 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
464 f
->fs_reclaim
.fd
= f
->fs
.fd
;
468 } else if (f
->fid_type
== P9_FID_DIR
) {
469 if (f
->fs
.dir
.stream
!= NULL
) {
471 * Up the reference count so that
472 * a clunk request won't free this fid
475 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
476 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
477 f
->fs
.dir
.stream
= NULL
;
481 if (reclaim_count
>= open_fd_rc
) {
486 * Now close the fid in reclaim list. Free them if they
487 * are already clunked.
489 while (!QSLIST_EMPTY(&reclaim_list
)) {
490 f
= QSLIST_FIRST(&reclaim_list
);
491 QSLIST_REMOVE(&reclaim_list
, f
, V9fsFidState
, reclaim_next
);
492 if (f
->fid_type
== P9_FID_FILE
) {
493 v9fs_co_close(pdu
, &f
->fs_reclaim
);
494 } else if (f
->fid_type
== P9_FID_DIR
) {
495 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
498 * Now drop the fid reference, free it
505 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
508 V9fsState
*s
= pdu
->s
;
509 V9fsFidState
*fidp
, *fidp_next
;
511 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
517 * v9fs_reopen_fid() can yield : a reference on the fid must be held
518 * to ensure its pointer remains valid and we can safely pass it to
519 * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so
520 * we must keep a reference on the next fid as well. So the logic here
521 * is to get a reference on a fid and only put it back during the next
522 * iteration after we could get a reference on the next fid. Start with
525 for (fidp
->ref
++; fidp
; fidp
= fidp_next
) {
526 if (fidp
->path
.size
== path
->size
&&
527 !memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
528 /* Mark the fid non reclaimable. */
529 fidp
->flags
|= FID_NON_RECLAIMABLE
;
531 /* reopen the file/dir if already closed */
532 err
= v9fs_reopen_fid(pdu
, fidp
);
539 fidp_next
= QSIMPLEQ_NEXT(fidp
, next
);
543 * Ensure the next fid survives a potential clunk request during
544 * put_fid() below and v9fs_reopen_fid() in the next iteration.
549 /* We're done with this fid */
556 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
558 V9fsState
*s
= pdu
->s
;
562 while (!QSIMPLEQ_EMPTY(&s
->fid_list
)) {
564 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
568 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
569 fidp
->clunked
= true;
575 #define P9_QID_TYPE_DIR 0x80
576 #define P9_QID_TYPE_SYMLINK 0x02
578 #define P9_STAT_MODE_DIR 0x80000000
579 #define P9_STAT_MODE_APPEND 0x40000000
580 #define P9_STAT_MODE_EXCL 0x20000000
581 #define P9_STAT_MODE_MOUNT 0x10000000
582 #define P9_STAT_MODE_AUTH 0x08000000
583 #define P9_STAT_MODE_TMP 0x04000000
584 #define P9_STAT_MODE_SYMLINK 0x02000000
585 #define P9_STAT_MODE_LINK 0x01000000
586 #define P9_STAT_MODE_DEVICE 0x00800000
587 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
588 #define P9_STAT_MODE_SOCKET 0x00100000
589 #define P9_STAT_MODE_SETUID 0x00080000
590 #define P9_STAT_MODE_SETGID 0x00040000
591 #define P9_STAT_MODE_SETVTX 0x00010000
593 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
594 P9_STAT_MODE_SYMLINK | \
595 P9_STAT_MODE_LINK | \
596 P9_STAT_MODE_DEVICE | \
597 P9_STAT_MODE_NAMED_PIPE | \
600 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
601 static inline uint8_t mirror8bit(uint8_t byte
)
603 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
606 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
607 static inline uint64_t mirror64bit(uint64_t value
)
609 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
610 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
611 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
612 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
613 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
614 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
615 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
616 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
620 * @brief Parameter k for the Exponential Golomb algorihm to be used.
622 * The smaller this value, the smaller the minimum bit count for the Exp.
623 * Golomb generated affixes will be (at lowest index) however for the
624 * price of having higher maximum bit count of generated affixes (at highest
625 * index). Likewise increasing this parameter yields in smaller maximum bit
626 * count for the price of having higher minimum bit count.
628 * In practice that means: a good value for k depends on the expected amount
629 * of devices to be exposed by one export. For a small amount of devices k
630 * should be small, for a large amount of devices k might be increased
631 * instead. The default of k=0 should be fine for most users though.
633 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
634 * k should not change as long as guest is still running! Because that would
635 * cause completely different inode numbers to be generated on guest.
637 #define EXP_GOLOMB_K 0
640 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
642 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
643 * with growing length and with the mathematical property of being
644 * "prefix-free". The latter means the generated prefixes can be prepended
645 * in front of arbitrary numbers and the resulting concatenated numbers are
646 * guaranteed to be always unique.
648 * This is a minor adjustment to the original Exp. Golomb algorithm in the
649 * sense that lowest allowed index (@param n) starts with 1, not with zero.
651 * @param n - natural number (or index) of the prefix to be generated
653 * @param k - parameter k of Exp. Golomb algorithm to be used
654 * (see comment on EXP_GOLOMB_K macro for details about k)
656 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
658 const uint64_t value
= n
+ (1 << k
) - 1;
659 const int bits
= (int) log2(value
) + 1;
660 return (VariLenAffix
) {
661 .type
= AffixType_Prefix
,
663 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
668 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
670 * Simply mirror all bits of the affix value, for the purpose to preserve
671 * respectively the mathematical "prefix-free" or "suffix-free" property
672 * after the conversion.
674 * If a passed prefix is suitable to create unique numbers, then the
675 * returned suffix is suitable to create unique numbers as well (and vice
678 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
680 return (VariLenAffix
) {
682 (affix
->type
== AffixType_Suffix
) ?
683 AffixType_Prefix
: AffixType_Suffix
,
685 mirror64bit(affix
->value
) >>
686 ((sizeof(affix
->value
) * 8) - affix
->bits
),
692 * @brief Generates suffix numbers with "suffix-free" property.
694 * This is just a wrapper function on top of the Exp. Golomb algorithm.
696 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
697 * this function converts the Exp. Golomb prefixes into appropriate suffixes
698 * which are still suitable for generating unique numbers.
700 * @param n - natural number (or index) of the suffix to be generated
703 static VariLenAffix
affixForIndex(uint64_t index
)
706 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
707 return invertAffix(&prefix
); /* convert prefix to suffix */
710 /* creative abuse of tb_hash_func7, which is based on xxhash */
711 static uint32_t qpp_hash(QppEntry e
)
713 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
716 static uint32_t qpf_hash(QpfEntry e
)
718 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
721 static bool qpd_cmp_func(const void *obj
, const void *userp
)
723 const QpdEntry
*e1
= obj
, *e2
= userp
;
724 return e1
->dev
== e2
->dev
;
727 static bool qpp_cmp_func(const void *obj
, const void *userp
)
729 const QppEntry
*e1
= obj
, *e2
= userp
;
730 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
733 static bool qpf_cmp_func(const void *obj
, const void *userp
)
735 const QpfEntry
*e1
= obj
, *e2
= userp
;
736 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
739 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
744 static void qp_table_destroy(struct qht
*ht
)
746 if (!ht
|| !ht
->map
) {
749 qht_iter(ht
, qp_table_remove
, NULL
);
753 static void qpd_table_init(struct qht
*ht
)
755 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
758 static void qpp_table_init(struct qht
*ht
)
760 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
763 static void qpf_table_init(struct qht
*ht
)
765 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
769 * Returns how many (high end) bits of inode numbers of the passed fs
770 * device shall be used (in combination with the device number) to
771 * generate hash values for qpp_table entries.
773 * This function is required if variable length suffixes are used for inode
774 * number mapping on guest level. Since a device may end up having multiple
775 * entries in qpp_table, each entry most probably with a different suffix
776 * length, we thus need this function in conjunction with qpd_table to
777 * "agree" about a fix amount of bits (per device) to be always used for
778 * generating hash values for the purpose of accessing qpp_table in order
779 * get consistent behaviour when accessing qpp_table.
781 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
789 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
791 val
= g_malloc0(sizeof(QpdEntry
));
793 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
794 val
->prefix_bits
= affix
.bits
;
795 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
796 pdu
->s
->qp_ndevices
++;
798 return val
->prefix_bits
;
802 * @brief Slow / full mapping host inode nr -> guest inode nr.
804 * This function performs a slower and much more costly remapping of an
805 * original file inode number on host to an appropriate different inode
806 * number on guest. For every (dev, inode) combination on host a new
807 * sequential number is generated, cached and exposed as inode number on
810 * This is just a "last resort" fallback solution if the much faster/cheaper
811 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
812 * expected ever to be used at all though.
814 * @see qid_path_suffixmap() for details
817 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
821 .dev
= stbuf
->st_dev
,
824 uint32_t hash
= qpf_hash(lookup
);
827 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
830 if (pdu
->s
->qp_fullpath_next
== 0) {
831 /* no more files can be mapped :'( */
833 "9p: No more prefixes available for remapping inodes from "
839 val
= g_malloc0(sizeof(QppEntry
));
842 /* new unique inode and device combo */
843 affix
= affixForIndex(
844 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
846 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
847 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
848 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
856 * @brief Quick mapping host inode nr -> guest inode nr.
858 * This function performs quick remapping of an original file inode number
859 * on host to an appropriate different inode number on guest. This remapping
860 * of inodes is required to avoid inode nr collisions on guest which would
861 * happen if the 9p export contains more than 1 exported file system (or
862 * more than 1 file system data set), because unlike on host level where the
863 * files would have different device nrs, all files exported by 9p would
864 * share the same device nr on guest (the device nr of the virtual 9p device
867 * Inode remapping is performed by chopping off high end bits of the original
868 * inode number from host, shifting the result upwards and then assigning a
869 * generated suffix number for the low end bits, where the same suffix number
870 * will be shared by all inodes with the same device id AND the same high end
871 * bits that have been chopped off. That approach utilizes the fact that inode
872 * numbers very likely share the same high end bits (i.e. due to their common
873 * sequential generation by file systems) and hence we only have to generate
874 * and track a very limited amount of suffixes in practice due to that.
876 * We generate variable size suffixes for that purpose. The 1st generated
877 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
878 * the original inode number. The subsequent suffixes being generated will
879 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
880 * generated will have 3 bits and hence we have to chop off 3 bits from their
881 * original inodes, and so on. That approach of using variable length suffixes
882 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
883 * limited amount of devices are shared by the same export (e.g. typically
884 * less than 2 dozen devices per 9p export), so in practice we need to chop
885 * off less bits than with fixed size prefixes and yet are flexible to add
886 * new devices at runtime below host's export directory at any time without
887 * having to reboot guest nor requiring to reconfigure guest for that. And due
888 * to the very limited amount of original high end bits that we chop off that
889 * way, the total amount of suffixes we need to generate is less than by using
890 * fixed size prefixes and hence it also improves performance of the inode
891 * remapping algorithm, and finally has the nice side effect that the inode
892 * numbers on guest will be much smaller & human friendly. ;-)
894 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
897 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
899 .dev
= stbuf
->st_dev
,
900 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
902 uint32_t hash
= qpp_hash(lookup
);
904 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
907 if (pdu
->s
->qp_affix_next
== 0) {
908 /* we ran out of affixes */
910 "9p: Potential degraded performance of inode remapping"
915 val
= g_malloc0(sizeof(QppEntry
));
918 /* new unique inode affix and device combo */
919 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
920 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
921 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
923 /* assuming generated affix to be suffix type, not prefix */
924 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
928 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
933 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
934 /* map inode+device to qid path (fast path) */
935 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
936 if (err
== -ENFILE
) {
937 /* fast path didn't work, fall back to full map */
938 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
944 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
945 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
947 "9p: Multiple devices detected in same VirtFS export. "
948 "Access of guest to additional devices is (partly) "
949 "denied due to virtfs option 'multidevs=forbid' being "
955 "9p: Multiple devices detected in same VirtFS export, "
956 "which might lead to file ID collisions and severe "
957 "misbehaviours on guest! You should either use a "
958 "separate export for each device shared from host or "
959 "use virtfs option 'multidevs=remap'!"
963 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
964 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
965 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
968 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
970 if (S_ISDIR(stbuf
->st_mode
)) {
971 qidp
->type
|= P9_QID_TYPE_DIR
;
973 if (S_ISLNK(stbuf
->st_mode
)) {
974 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
980 V9fsPDU
*pdu_alloc(V9fsState
*s
)
984 if (!QLIST_EMPTY(&s
->free_list
)) {
985 pdu
= QLIST_FIRST(&s
->free_list
);
986 QLIST_REMOVE(pdu
, next
);
987 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
992 void pdu_free(V9fsPDU
*pdu
)
994 V9fsState
*s
= pdu
->s
;
996 g_assert(!pdu
->cancelled
);
997 QLIST_REMOVE(pdu
, next
);
998 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
1001 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
1003 int8_t id
= pdu
->id
+ 1; /* Response */
1004 V9fsState
*s
= pdu
->s
;
1008 * The 9p spec requires that successfully cancelled pdus receive no reply.
1009 * Sending a reply would confuse clients because they would
1010 * assume that any EINTR is the actual result of the operation,
1011 * rather than a consequence of the cancellation. However, if
1012 * the operation completed (succesfully or with an error other
1013 * than caused be cancellation), we do send out that reply, both
1014 * for efficiency and to avoid confusing the rest of the state machine
1015 * that assumes passing a non-error here will mean a successful
1016 * transmission of the reply.
1018 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1020 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1029 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1032 str
.data
= strerror(err
);
1033 str
.size
= strlen(str
.data
);
1035 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1043 ret
= pdu_marshal(pdu
, len
, "d", err
);
1049 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1052 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1055 /* fill out the header */
1056 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1060 /* keep these in sync */
1065 pdu
->s
->transport
->push_and_notify(pdu
);
1067 /* Now wakeup anybody waiting in flush for this request */
1068 if (!qemu_co_queue_next(&pdu
->complete
)) {
1073 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1078 if (mode
& P9_STAT_MODE_DIR
) {
1082 if (mode
& P9_STAT_MODE_SYMLINK
) {
1085 if (mode
& P9_STAT_MODE_SOCKET
) {
1088 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1091 if (mode
& P9_STAT_MODE_DEVICE
) {
1092 if (extension
->size
&& extension
->data
[0] == 'c') {
1099 if (!(ret
& ~0777)) {
1103 if (mode
& P9_STAT_MODE_SETUID
) {
1106 if (mode
& P9_STAT_MODE_SETGID
) {
1109 if (mode
& P9_STAT_MODE_SETVTX
) {
1116 static int donttouch_stat(V9fsStat
*stat
)
1118 if (stat
->type
== -1 &&
1120 stat
->qid
.type
== 0xff &&
1121 stat
->qid
.version
== (uint32_t) -1 &&
1122 stat
->qid
.path
== (uint64_t) -1 &&
1124 stat
->atime
== -1 &&
1125 stat
->mtime
== -1 &&
1126 stat
->length
== -1 &&
1131 stat
->n_uid
== -1 &&
1132 stat
->n_gid
== -1 &&
1133 stat
->n_muid
== -1) {
1140 static void v9fs_stat_init(V9fsStat
*stat
)
1142 v9fs_string_init(&stat
->name
);
1143 v9fs_string_init(&stat
->uid
);
1144 v9fs_string_init(&stat
->gid
);
1145 v9fs_string_init(&stat
->muid
);
1146 v9fs_string_init(&stat
->extension
);
1149 static void v9fs_stat_free(V9fsStat
*stat
)
1151 v9fs_string_free(&stat
->name
);
1152 v9fs_string_free(&stat
->uid
);
1153 v9fs_string_free(&stat
->gid
);
1154 v9fs_string_free(&stat
->muid
);
1155 v9fs_string_free(&stat
->extension
);
1158 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1162 mode
= stbuf
->st_mode
& 0777;
1163 if (S_ISDIR(stbuf
->st_mode
)) {
1164 mode
|= P9_STAT_MODE_DIR
;
1167 if (S_ISLNK(stbuf
->st_mode
)) {
1168 mode
|= P9_STAT_MODE_SYMLINK
;
1171 if (S_ISSOCK(stbuf
->st_mode
)) {
1172 mode
|= P9_STAT_MODE_SOCKET
;
1175 if (S_ISFIFO(stbuf
->st_mode
)) {
1176 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1179 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1180 mode
|= P9_STAT_MODE_DEVICE
;
1183 if (stbuf
->st_mode
& S_ISUID
) {
1184 mode
|= P9_STAT_MODE_SETUID
;
1187 if (stbuf
->st_mode
& S_ISGID
) {
1188 mode
|= P9_STAT_MODE_SETGID
;
1191 if (stbuf
->st_mode
& S_ISVTX
) {
1192 mode
|= P9_STAT_MODE_SETVTX
;
1198 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1199 const char *basename
,
1200 const struct stat
*stbuf
,
1205 memset(v9stat
, 0, sizeof(*v9stat
));
1207 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1211 v9stat
->mode
= stat_to_v9mode(stbuf
);
1212 v9stat
->atime
= stbuf
->st_atime
;
1213 v9stat
->mtime
= stbuf
->st_mtime
;
1214 v9stat
->length
= stbuf
->st_size
;
1216 v9fs_string_free(&v9stat
->uid
);
1217 v9fs_string_free(&v9stat
->gid
);
1218 v9fs_string_free(&v9stat
->muid
);
1220 v9stat
->n_uid
= stbuf
->st_uid
;
1221 v9stat
->n_gid
= stbuf
->st_gid
;
1224 v9fs_string_free(&v9stat
->extension
);
1226 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1227 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1231 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1232 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1233 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1234 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1235 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1236 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1237 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1240 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1243 v9fs_string_size(&v9stat
->name
) +
1244 v9fs_string_size(&v9stat
->uid
) +
1245 v9fs_string_size(&v9stat
->gid
) +
1246 v9fs_string_size(&v9stat
->muid
) +
1247 v9fs_string_size(&v9stat
->extension
);
1251 #define P9_STATS_MODE 0x00000001ULL
1252 #define P9_STATS_NLINK 0x00000002ULL
1253 #define P9_STATS_UID 0x00000004ULL
1254 #define P9_STATS_GID 0x00000008ULL
1255 #define P9_STATS_RDEV 0x00000010ULL
1256 #define P9_STATS_ATIME 0x00000020ULL
1257 #define P9_STATS_MTIME 0x00000040ULL
1258 #define P9_STATS_CTIME 0x00000080ULL
1259 #define P9_STATS_INO 0x00000100ULL
1260 #define P9_STATS_SIZE 0x00000200ULL
1261 #define P9_STATS_BLOCKS 0x00000400ULL
1263 #define P9_STATS_BTIME 0x00000800ULL
1264 #define P9_STATS_GEN 0x00001000ULL
1265 #define P9_STATS_DATA_VERSION 0x00002000ULL
1267 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1268 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1272 * Convert host filesystem's block size into an appropriate block size for
1273 * 9p client (guest OS side). The value returned suggests an "optimum" block
1274 * size for 9p I/O, i.e. to maximize performance.
1276 * @pdu: 9p client request
1277 * @blksize: host filesystem's block size
1279 static int32_t blksize_to_iounit(const V9fsPDU
*pdu
, int32_t blksize
)
1282 V9fsState
*s
= pdu
->s
;
1285 * iounit should be multiples of blksize (host filesystem block size)
1286 * as well as less than (client msize - P9_IOHDRSZ)
1289 iounit
= QEMU_ALIGN_DOWN(s
->msize
- P9_IOHDRSZ
, blksize
);
1292 iounit
= s
->msize
- P9_IOHDRSZ
;
1297 static int32_t stat_to_iounit(const V9fsPDU
*pdu
, const struct stat
*stbuf
)
1299 return blksize_to_iounit(pdu
, stbuf
->st_blksize
);
1302 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1303 V9fsStatDotl
*v9lstat
)
1305 memset(v9lstat
, 0, sizeof(*v9lstat
));
1307 v9lstat
->st_mode
= stbuf
->st_mode
;
1308 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1309 v9lstat
->st_uid
= stbuf
->st_uid
;
1310 v9lstat
->st_gid
= stbuf
->st_gid
;
1311 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1312 v9lstat
->st_size
= stbuf
->st_size
;
1313 v9lstat
->st_blksize
= stat_to_iounit(pdu
, stbuf
);
1314 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1315 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1316 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1317 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1318 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1319 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1320 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1321 /* Currently we only support BASIC fields in stat */
1322 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1324 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1327 static void print_sg(struct iovec
*sg
, int cnt
)
1331 printf("sg[%d]: {", cnt
);
1332 for (i
= 0; i
< cnt
; i
++) {
1336 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1341 /* Will call this only for path name based fid */
1342 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1345 v9fs_path_init(&str
);
1346 v9fs_path_copy(&str
, dst
);
1347 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1348 v9fs_path_free(&str
);
1351 static inline bool is_ro_export(FsContext
*ctx
)
1353 return ctx
->export_flags
& V9FS_RDONLY
;
1356 static void coroutine_fn
v9fs_version(void *opaque
)
1359 V9fsPDU
*pdu
= opaque
;
1360 V9fsState
*s
= pdu
->s
;
1364 v9fs_string_init(&version
);
1365 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1369 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1373 if (!strcmp(version
.data
, "9P2000.u")) {
1374 s
->proto_version
= V9FS_PROTO_2000U
;
1375 } else if (!strcmp(version
.data
, "9P2000.L")) {
1376 s
->proto_version
= V9FS_PROTO_2000L
;
1378 v9fs_string_sprintf(&version
, "unknown");
1379 /* skip min. msize check, reporting invalid version has priority */
1383 if (s
->msize
< P9_MIN_MSIZE
) {
1386 "9pfs: Client requested msize < minimum msize ("
1387 stringify(P9_MIN_MSIZE
) ") supported by this server."
1392 /* 8192 is the default msize of Linux clients */
1393 if (s
->msize
<= 8192 && !(s
->ctx
.export_flags
& V9FS_NO_PERF_WARN
)) {
1395 "9p: degraded performance: a reasonable high msize should be "
1396 "chosen on client/guest side (chosen msize is <= 8192). See "
1397 "https://wiki.qemu.org/Documentation/9psetup#msize for details."
1402 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1407 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1409 pdu_complete(pdu
, err
);
1410 v9fs_string_free(&version
);
1413 static void coroutine_fn
v9fs_attach(void *opaque
)
1415 V9fsPDU
*pdu
= opaque
;
1416 V9fsState
*s
= pdu
->s
;
1417 int32_t fid
, afid
, n_uname
;
1418 V9fsString uname
, aname
;
1425 v9fs_string_init(&uname
);
1426 v9fs_string_init(&aname
);
1427 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1428 &afid
, &uname
, &aname
, &n_uname
);
1432 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1434 fidp
= alloc_fid(s
, fid
);
1439 fidp
->uid
= n_uname
;
1440 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1446 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1452 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1460 * disable migration if we haven't done already.
1461 * attach could get called multiple times for the same export.
1463 if (!s
->migration_blocker
) {
1464 error_setg(&s
->migration_blocker
,
1465 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1466 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1467 err
= migrate_add_blocker(s
->migration_blocker
, NULL
);
1469 error_free(s
->migration_blocker
);
1470 s
->migration_blocker
= NULL
;
1477 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1484 memcpy(&s
->root_st
, &stbuf
, sizeof(stbuf
));
1485 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1486 qid
.type
, qid
.version
, qid
.path
);
1490 pdu_complete(pdu
, err
);
1491 v9fs_string_free(&uname
);
1492 v9fs_string_free(&aname
);
1495 static void coroutine_fn
v9fs_stat(void *opaque
)
1503 V9fsPDU
*pdu
= opaque
;
1506 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1510 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1512 fidp
= get_fid(pdu
, fid
);
1517 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1521 basename
= g_path_get_basename(fidp
->path
.data
);
1522 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1527 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1529 v9fs_stat_free(&v9stat
);
1532 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1533 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1535 v9fs_stat_free(&v9stat
);
1539 pdu_complete(pdu
, err
);
1542 static void coroutine_fn
v9fs_getattr(void *opaque
)
1549 uint64_t request_mask
;
1550 V9fsStatDotl v9stat_dotl
;
1551 V9fsPDU
*pdu
= opaque
;
1553 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1557 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1559 fidp
= get_fid(pdu
, fid
);
1565 * Currently we only support BASIC fields in stat, so there is no
1566 * need to look at request_mask.
1568 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1572 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1577 /* fill st_gen if requested and supported by underlying fs */
1578 if (request_mask
& P9_STATS_GEN
) {
1579 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1582 /* we have valid st_gen: update result mask */
1583 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1586 /* request cancelled, e.g. by Tflush */
1589 /* failed to get st_gen: not fatal, ignore */
1593 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1598 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1599 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1600 v9stat_dotl
.st_gid
);
1604 pdu_complete(pdu
, retval
);
1607 /* Attribute flags */
1608 #define P9_ATTR_MODE (1 << 0)
1609 #define P9_ATTR_UID (1 << 1)
1610 #define P9_ATTR_GID (1 << 2)
1611 #define P9_ATTR_SIZE (1 << 3)
1612 #define P9_ATTR_ATIME (1 << 4)
1613 #define P9_ATTR_MTIME (1 << 5)
1614 #define P9_ATTR_CTIME (1 << 6)
1615 #define P9_ATTR_ATIME_SET (1 << 7)
1616 #define P9_ATTR_MTIME_SET (1 << 8)
1618 #define P9_ATTR_MASK 127
1620 static void coroutine_fn
v9fs_setattr(void *opaque
)
1627 V9fsPDU
*pdu
= opaque
;
1629 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1634 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1635 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1636 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1638 fidp
= get_fid(pdu
, fid
);
1643 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1644 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1649 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1650 struct timespec times
[2];
1651 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1652 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1653 times
[0].tv_sec
= v9iattr
.atime_sec
;
1654 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1656 times
[0].tv_nsec
= UTIME_NOW
;
1659 times
[0].tv_nsec
= UTIME_OMIT
;
1661 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1662 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1663 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1664 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1666 times
[1].tv_nsec
= UTIME_NOW
;
1669 times
[1].tv_nsec
= UTIME_OMIT
;
1671 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1677 * If the only valid entry in iattr is ctime we can call
1678 * chown(-1,-1) to update the ctime of the file
1680 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1681 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1682 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1683 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1686 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1689 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1695 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1696 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1702 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1706 pdu_complete(pdu
, err
);
1709 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1715 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1720 for (i
= 0; i
< nwnames
; i
++) {
1721 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1730 static bool name_is_illegal(const char *name
)
1732 return !*name
|| strchr(name
, '/') != NULL
;
1735 static bool same_stat_id(const struct stat
*a
, const struct stat
*b
)
1737 return a
->st_dev
== b
->st_dev
&& a
->st_ino
== b
->st_ino
;
1740 static void coroutine_fn
v9fs_walk(void *opaque
)
1743 g_autofree V9fsQID
*qids
= NULL
;
1745 V9fsPath dpath
, path
;
1746 P9ARRAY_REF(V9fsPath
) pathes
= NULL
;
1748 struct stat stbuf
, fidst
;
1749 g_autofree
struct stat
*stbufs
= NULL
;
1751 int32_t fid
, newfid
;
1752 P9ARRAY_REF(V9fsString
) wnames
= NULL
;
1754 V9fsFidState
*newfidp
= NULL
;
1755 V9fsPDU
*pdu
= opaque
;
1756 V9fsState
*s
= pdu
->s
;
1759 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1761 pdu_complete(pdu
, err
);
1766 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1768 if (nwnames
> P9_MAXWELEM
) {
1773 P9ARRAY_NEW(V9fsString
, wnames
, nwnames
);
1774 qids
= g_new0(V9fsQID
, nwnames
);
1775 stbufs
= g_new0(struct stat
, nwnames
);
1776 P9ARRAY_NEW(V9fsPath
, pathes
, nwnames
);
1777 for (i
= 0; i
< nwnames
; i
++) {
1778 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1782 if (name_is_illegal(wnames
[i
].data
)) {
1789 fidp
= get_fid(pdu
, fid
);
1795 v9fs_path_init(&dpath
);
1796 v9fs_path_init(&path
);
1798 * Both dpath and path initially point to fidp.
1799 * Needed to handle request with nwnames == 0
1801 v9fs_path_copy(&dpath
, &fidp
->path
);
1802 v9fs_path_copy(&path
, &fidp
->path
);
1805 * To keep latency (i.e. overall execution time for processing this
1806 * Twalk client request) as small as possible, run all the required fs
1807 * driver code altogether inside the following block.
1809 v9fs_co_run_in_worker({
1810 if (v9fs_request_cancelled(pdu
)) {
1814 err
= s
->ops
->lstat(&s
->ctx
, &dpath
, &fidst
);
1820 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1821 if (v9fs_request_cancelled(pdu
)) {
1825 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1826 strcmp("..", wnames
[name_idx
].data
))
1828 err
= s
->ops
->name_to_path(&s
->ctx
, &dpath
,
1829 wnames
[name_idx
].data
,
1835 if (v9fs_request_cancelled(pdu
)) {
1839 err
= s
->ops
->lstat(&s
->ctx
, &pathes
[name_idx
], &stbuf
);
1844 stbufs
[name_idx
] = stbuf
;
1845 v9fs_path_copy(&dpath
, &pathes
[name_idx
]);
1850 * Handle all the rest of this Twalk request on main thread ...
1856 err
= stat_to_qid(pdu
, &fidst
, &qid
);
1862 /* reset dpath and path */
1863 v9fs_path_copy(&dpath
, &fidp
->path
);
1864 v9fs_path_copy(&path
, &fidp
->path
);
1866 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1867 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1868 strcmp("..", wnames
[name_idx
].data
))
1870 stbuf
= stbufs
[name_idx
];
1871 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1875 v9fs_path_copy(&path
, &pathes
[name_idx
]);
1876 v9fs_path_copy(&dpath
, &path
);
1878 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1880 if (fid
== newfid
) {
1881 if (fidp
->fid_type
!= P9_FID_NONE
) {
1885 v9fs_path_write_lock(s
);
1886 v9fs_path_copy(&fidp
->path
, &path
);
1887 v9fs_path_unlock(s
);
1889 newfidp
= alloc_fid(s
, newfid
);
1890 if (newfidp
== NULL
) {
1894 newfidp
->uid
= fidp
->uid
;
1895 v9fs_path_copy(&newfidp
->path
, &path
);
1897 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1898 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1902 put_fid(pdu
, newfidp
);
1904 v9fs_path_free(&dpath
);
1905 v9fs_path_free(&path
);
1907 pdu_complete(pdu
, err
);
1910 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1912 struct statfs stbuf
;
1913 int err
= v9fs_co_statfs(pdu
, path
, &stbuf
);
1915 return blksize_to_iounit(pdu
, (err
>= 0) ? stbuf
.f_bsize
: 0);
1918 static void coroutine_fn
v9fs_open(void *opaque
)
1929 V9fsPDU
*pdu
= opaque
;
1930 V9fsState
*s
= pdu
->s
;
1932 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1933 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1936 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1942 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1944 fidp
= get_fid(pdu
, fid
);
1949 if (fidp
->fid_type
!= P9_FID_NONE
) {
1954 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1958 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1962 if (S_ISDIR(stbuf
.st_mode
)) {
1963 err
= v9fs_co_opendir(pdu
, fidp
);
1967 fidp
->fid_type
= P9_FID_DIR
;
1968 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1974 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1975 flags
= get_dotl_openflags(s
, mode
);
1977 flags
= omode_to_uflags(mode
);
1979 if (is_ro_export(&s
->ctx
)) {
1980 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1981 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1986 err
= v9fs_co_open(pdu
, fidp
, flags
);
1990 fidp
->fid_type
= P9_FID_FILE
;
1991 fidp
->open_flags
= flags
;
1992 if (flags
& O_EXCL
) {
1994 * We let the host file system do O_EXCL check
1995 * We should not reclaim such fd
1997 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1999 iounit
= get_iounit(pdu
, &fidp
->path
);
2000 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2006 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
2007 qid
.type
, qid
.version
, qid
.path
, iounit
);
2011 pdu_complete(pdu
, err
);
2014 static void coroutine_fn
v9fs_lcreate(void *opaque
)
2016 int32_t dfid
, flags
, mode
;
2025 V9fsPDU
*pdu
= opaque
;
2027 v9fs_string_init(&name
);
2028 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
2029 &name
, &flags
, &mode
, &gid
);
2033 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
2035 if (name_is_illegal(name
.data
)) {
2040 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2045 fidp
= get_fid(pdu
, dfid
);
2050 if (fidp
->fid_type
!= P9_FID_NONE
) {
2055 flags
= get_dotl_openflags(pdu
->s
, flags
);
2056 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
2057 flags
| O_CREAT
, mode
, &stbuf
);
2061 fidp
->fid_type
= P9_FID_FILE
;
2062 fidp
->open_flags
= flags
;
2063 if (flags
& O_EXCL
) {
2065 * We let the host file system do O_EXCL check
2066 * We should not reclaim such fd
2068 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2070 iounit
= get_iounit(pdu
, &fidp
->path
);
2071 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2075 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2080 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2081 qid
.type
, qid
.version
, qid
.path
, iounit
);
2085 pdu_complete(pdu
, err
);
2086 v9fs_string_free(&name
);
2089 static void coroutine_fn
v9fs_fsync(void *opaque
)
2096 V9fsPDU
*pdu
= opaque
;
2098 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2102 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2104 fidp
= get_fid(pdu
, fid
);
2109 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2115 pdu_complete(pdu
, err
);
2118 static void coroutine_fn
v9fs_clunk(void *opaque
)
2124 V9fsPDU
*pdu
= opaque
;
2125 V9fsState
*s
= pdu
->s
;
2127 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2131 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2133 fidp
= clunk_fid(s
, fid
);
2139 * Bump the ref so that put_fid will
2143 err
= put_fid(pdu
, fidp
);
2148 pdu_complete(pdu
, err
);
2152 * Create a QEMUIOVector for a sub-region of PDU iovecs
2154 * @qiov: uninitialized QEMUIOVector
2155 * @skip: number of bytes to skip from beginning of PDU
2156 * @size: number of bytes to include
2157 * @is_write: true - write, false - read
2159 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2160 * with qemu_iovec_destroy().
2162 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2163 size_t skip
, size_t size
,
2171 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2173 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2176 qemu_iovec_init_external(&elem
, iov
, niov
);
2177 qemu_iovec_init(qiov
, niov
);
2178 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
2181 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2182 uint64_t off
, uint32_t max_count
)
2186 uint64_t read_count
;
2187 QEMUIOVector qiov_full
;
2189 if (fidp
->fs
.xattr
.len
< off
) {
2192 read_count
= fidp
->fs
.xattr
.len
- off
;
2194 if (read_count
> max_count
) {
2195 read_count
= max_count
;
2197 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2203 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
2204 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2205 ((char *)fidp
->fs
.xattr
.value
) + off
,
2207 qemu_iovec_destroy(&qiov_full
);
2215 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2224 off_t saved_dir_pos
;
2225 struct dirent
*dent
;
2227 /* save the directory position */
2228 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2229 if (saved_dir_pos
< 0) {
2230 return saved_dir_pos
;
2234 v9fs_path_init(&path
);
2236 v9fs_readdir_lock(&fidp
->fs
.dir
);
2238 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2242 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2246 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2250 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2254 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2255 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2257 /* Ran out of buffer. Set dir back to old position and return */
2258 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2259 v9fs_stat_free(&v9stat
);
2260 v9fs_path_free(&path
);
2264 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2265 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2267 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2270 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2271 v9fs_stat_free(&v9stat
);
2272 v9fs_path_free(&path
);
2276 v9fs_stat_free(&v9stat
);
2277 v9fs_path_free(&path
);
2278 saved_dir_pos
= dent
->d_off
;
2281 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2283 v9fs_path_free(&path
);
2290 static void coroutine_fn
v9fs_read(void *opaque
)
2299 V9fsPDU
*pdu
= opaque
;
2300 V9fsState
*s
= pdu
->s
;
2302 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2306 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2308 fidp
= get_fid(pdu
, fid
);
2313 if (fidp
->fid_type
== P9_FID_DIR
) {
2314 if (s
->proto_version
!= V9FS_PROTO_2000U
) {
2316 "9p: bad client: T_read request on directory only expected "
2317 "with 9P2000.u protocol version"
2323 v9fs_co_rewinddir(pdu
, fidp
);
2325 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2330 err
= pdu_marshal(pdu
, offset
, "d", count
);
2334 err
+= offset
+ count
;
2335 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2336 QEMUIOVector qiov_full
;
2340 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2341 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2343 qemu_iovec_reset(&qiov
);
2344 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2346 print_sg(qiov
.iov
, qiov
.niov
);
2348 /* Loop in case of EINTR */
2350 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2355 } while (len
== -EINTR
&& !pdu
->cancelled
);
2357 /* IO error return the error */
2359 goto out_free_iovec
;
2361 } while (count
< max_count
&& len
> 0);
2362 err
= pdu_marshal(pdu
, offset
, "d", count
);
2364 goto out_free_iovec
;
2366 err
+= offset
+ count
;
2368 qemu_iovec_destroy(&qiov
);
2369 qemu_iovec_destroy(&qiov_full
);
2370 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2371 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2375 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2379 pdu_complete(pdu
, err
);
2383 * Returns size required in Rreaddir response for the passed dirent @p name.
2385 * @param name - directory entry's name (i.e. file name, directory name)
2386 * @returns required size in bytes
2388 size_t v9fs_readdir_response_size(V9fsString
*name
)
2391 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2392 * size of type (1) + size of name.size (2) + strlen(name.data)
2394 return 24 + v9fs_string_size(name
);
2397 static void v9fs_free_dirents(struct V9fsDirEnt
*e
)
2399 struct V9fsDirEnt
*next
= NULL
;
2401 for (; e
; e
= next
) {
2409 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2410 off_t offset
, int32_t max_count
)
2417 struct dirent
*dent
;
2419 struct V9fsDirEnt
*entries
= NULL
;
2422 * inode remapping requires the device id, which in turn might be
2423 * different for different directory entries, so if inode remapping is
2424 * enabled we have to make a full stat for each directory entry
2426 const bool dostat
= pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
;
2429 * Fetch all required directory entries altogether on a background IO
2430 * thread from fs driver. We don't want to do that for each entry
2431 * individually, because hopping between threads (this main IO thread
2432 * and background IO driver thread) would sum up to huge latencies.
2434 count
= v9fs_co_readdir_many(pdu
, fidp
, &entries
, offset
, max_count
,
2443 for (struct V9fsDirEnt
*e
= entries
; e
; e
= e
->next
) {
2446 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2448 /* e->st should never be NULL, but just to be sure */
2455 err
= stat_to_qid(pdu
, st
, &qid
);
2461 * Fill up just the path field of qid because the client uses
2462 * only that. To fill the entire qid structure we will have
2463 * to stat each dirent found, which is expensive. For the
2464 * latter reason we don't call stat_to_qid() here. Only drawback
2465 * is that no multi-device export detection of stat_to_qid()
2466 * would be done and provided as error to the user here. But
2467 * user would get that error anyway when accessing those
2468 * files/dirs through other ways.
2470 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2471 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2472 /* Fill the other fields with dummy values */
2477 v9fs_string_init(&name
);
2478 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2480 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2481 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2483 dent
->d_type
, &name
);
2485 v9fs_string_free(&name
);
2496 v9fs_free_dirents(entries
);
2503 static void coroutine_fn
v9fs_readdir(void *opaque
)
2509 uint64_t initial_offset
;
2512 V9fsPDU
*pdu
= opaque
;
2513 V9fsState
*s
= pdu
->s
;
2515 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2516 &initial_offset
, &max_count
);
2520 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2522 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2523 if (max_count
> s
->msize
- 11) {
2524 max_count
= s
->msize
- 11;
2526 "9p: bad client: T_readdir with count > msize - 11"
2530 fidp
= get_fid(pdu
, fid
);
2535 if (!fidp
->fs
.dir
.stream
) {
2539 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
2541 "9p: bad client: T_readdir request only expected with 9P2000.L "
2544 retval
= -EOPNOTSUPP
;
2547 count
= v9fs_do_readdir(pdu
, fidp
, (off_t
) initial_offset
, max_count
);
2552 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2556 retval
+= count
+ offset
;
2557 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2561 pdu_complete(pdu
, retval
);
2564 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2565 uint64_t off
, uint32_t count
,
2566 struct iovec
*sg
, int cnt
)
2570 uint64_t write_count
;
2574 if (fidp
->fs
.xattr
.len
< off
) {
2577 write_count
= fidp
->fs
.xattr
.len
- off
;
2578 if (write_count
> count
) {
2579 write_count
= count
;
2581 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2586 fidp
->fs
.xattr
.copied_len
+= write_count
;
2588 * Now copy the content from sg list
2590 for (i
= 0; i
< cnt
; i
++) {
2591 if (write_count
> sg
[i
].iov_len
) {
2592 to_copy
= sg
[i
].iov_len
;
2594 to_copy
= write_count
;
2596 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2597 /* updating vs->off since we are not using below */
2599 write_count
-= to_copy
;
2605 static void coroutine_fn
v9fs_write(void *opaque
)
2615 V9fsPDU
*pdu
= opaque
;
2616 V9fsState
*s
= pdu
->s
;
2617 QEMUIOVector qiov_full
;
2620 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2622 pdu_complete(pdu
, err
);
2626 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2627 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2629 fidp
= get_fid(pdu
, fid
);
2634 if (fidp
->fid_type
== P9_FID_FILE
) {
2635 if (fidp
->fs
.fd
== -1) {
2639 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2641 * setxattr operation
2643 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2644 qiov_full
.iov
, qiov_full
.niov
);
2650 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2652 qemu_iovec_reset(&qiov
);
2653 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2655 print_sg(qiov
.iov
, qiov
.niov
);
2657 /* Loop in case of EINTR */
2659 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2664 } while (len
== -EINTR
&& !pdu
->cancelled
);
2666 /* IO error return the error */
2670 } while (total
< count
&& len
> 0);
2673 err
= pdu_marshal(pdu
, offset
, "d", total
);
2678 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2680 qemu_iovec_destroy(&qiov
);
2684 qemu_iovec_destroy(&qiov_full
);
2685 pdu_complete(pdu
, err
);
2688 static void coroutine_fn
v9fs_create(void *opaque
)
2700 V9fsString extension
;
2702 V9fsPDU
*pdu
= opaque
;
2703 V9fsState
*s
= pdu
->s
;
2705 v9fs_path_init(&path
);
2706 v9fs_string_init(&name
);
2707 v9fs_string_init(&extension
);
2708 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2709 &perm
, &mode
, &extension
);
2713 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2715 if (name_is_illegal(name
.data
)) {
2720 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2725 fidp
= get_fid(pdu
, fid
);
2730 if (fidp
->fid_type
!= P9_FID_NONE
) {
2734 if (perm
& P9_STAT_MODE_DIR
) {
2735 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2736 fidp
->uid
, -1, &stbuf
);
2740 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2744 v9fs_path_write_lock(s
);
2745 v9fs_path_copy(&fidp
->path
, &path
);
2746 v9fs_path_unlock(s
);
2747 err
= v9fs_co_opendir(pdu
, fidp
);
2751 fidp
->fid_type
= P9_FID_DIR
;
2752 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2753 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2754 extension
.data
, -1 , &stbuf
);
2758 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2762 v9fs_path_write_lock(s
);
2763 v9fs_path_copy(&fidp
->path
, &path
);
2764 v9fs_path_unlock(s
);
2765 } else if (perm
& P9_STAT_MODE_LINK
) {
2766 int32_t ofid
= atoi(extension
.data
);
2767 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2768 if (ofidp
== NULL
) {
2772 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2773 put_fid(pdu
, ofidp
);
2777 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2779 fidp
->fid_type
= P9_FID_NONE
;
2782 v9fs_path_write_lock(s
);
2783 v9fs_path_copy(&fidp
->path
, &path
);
2784 v9fs_path_unlock(s
);
2785 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2787 fidp
->fid_type
= P9_FID_NONE
;
2790 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2792 uint32_t major
, minor
;
2795 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2812 nmode
|= perm
& 0777;
2813 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2814 makedev(major
, minor
), nmode
, &stbuf
);
2818 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2822 v9fs_path_write_lock(s
);
2823 v9fs_path_copy(&fidp
->path
, &path
);
2824 v9fs_path_unlock(s
);
2825 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2826 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2827 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2831 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2835 v9fs_path_write_lock(s
);
2836 v9fs_path_copy(&fidp
->path
, &path
);
2837 v9fs_path_unlock(s
);
2838 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2839 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2840 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2844 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2848 v9fs_path_write_lock(s
);
2849 v9fs_path_copy(&fidp
->path
, &path
);
2850 v9fs_path_unlock(s
);
2852 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2853 omode_to_uflags(mode
) | O_CREAT
, perm
, &stbuf
);
2857 fidp
->fid_type
= P9_FID_FILE
;
2858 fidp
->open_flags
= omode_to_uflags(mode
);
2859 if (fidp
->open_flags
& O_EXCL
) {
2861 * We let the host file system do O_EXCL check
2862 * We should not reclaim such fd
2864 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2867 iounit
= get_iounit(pdu
, &fidp
->path
);
2868 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2872 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2877 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2878 qid
.type
, qid
.version
, qid
.path
, iounit
);
2882 pdu_complete(pdu
, err
);
2883 v9fs_string_free(&name
);
2884 v9fs_string_free(&extension
);
2885 v9fs_path_free(&path
);
2888 static void coroutine_fn
v9fs_symlink(void *opaque
)
2890 V9fsPDU
*pdu
= opaque
;
2893 V9fsFidState
*dfidp
;
2901 v9fs_string_init(&name
);
2902 v9fs_string_init(&symname
);
2903 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2907 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2909 if (name_is_illegal(name
.data
)) {
2914 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2919 dfidp
= get_fid(pdu
, dfid
);
2920 if (dfidp
== NULL
) {
2924 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2928 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2932 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2937 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2938 qid
.type
, qid
.version
, qid
.path
);
2940 put_fid(pdu
, dfidp
);
2942 pdu_complete(pdu
, err
);
2943 v9fs_string_free(&name
);
2944 v9fs_string_free(&symname
);
2947 static void coroutine_fn
v9fs_flush(void *opaque
)
2952 V9fsPDU
*cancel_pdu
= NULL
;
2953 V9fsPDU
*pdu
= opaque
;
2954 V9fsState
*s
= pdu
->s
;
2956 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2958 pdu_complete(pdu
, err
);
2961 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2963 if (pdu
->tag
== tag
) {
2964 warn_report("the guest sent a self-referencing 9P flush request");
2966 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2967 if (cancel_pdu
->tag
== tag
) {
2973 cancel_pdu
->cancelled
= 1;
2975 * Wait for pdu to complete.
2977 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2978 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2979 cancel_pdu
->cancelled
= 0;
2980 pdu_free(cancel_pdu
);
2983 pdu_complete(pdu
, 7);
2986 static void coroutine_fn
v9fs_link(void *opaque
)
2988 V9fsPDU
*pdu
= opaque
;
2989 int32_t dfid
, oldfid
;
2990 V9fsFidState
*dfidp
, *oldfidp
;
2995 v9fs_string_init(&name
);
2996 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
3000 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
3002 if (name_is_illegal(name
.data
)) {
3007 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3012 dfidp
= get_fid(pdu
, dfid
);
3013 if (dfidp
== NULL
) {
3018 oldfidp
= get_fid(pdu
, oldfid
);
3019 if (oldfidp
== NULL
) {
3023 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
3027 put_fid(pdu
, oldfidp
);
3029 put_fid(pdu
, dfidp
);
3031 v9fs_string_free(&name
);
3032 pdu_complete(pdu
, err
);
3035 /* Only works with path name based fid */
3036 static void coroutine_fn
v9fs_remove(void *opaque
)
3042 V9fsPDU
*pdu
= opaque
;
3044 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3048 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
3050 fidp
= get_fid(pdu
, fid
);
3055 /* if fs driver is not path based, return EOPNOTSUPP */
3056 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3061 * IF the file is unlinked, we cannot reopen
3062 * the file later. So don't reclaim fd
3064 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
3068 err
= v9fs_co_remove(pdu
, &fidp
->path
);
3073 /* For TREMOVE we need to clunk the fid even on failed remove */
3074 clunk_fid(pdu
->s
, fidp
->fid
);
3077 pdu_complete(pdu
, err
);
3080 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
3084 int32_t dfid
, flags
, rflags
= 0;
3087 V9fsFidState
*dfidp
;
3088 V9fsPDU
*pdu
= opaque
;
3090 v9fs_string_init(&name
);
3091 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3096 if (name_is_illegal(name
.data
)) {
3101 if (!strcmp(".", name
.data
)) {
3106 if (!strcmp("..", name
.data
)) {
3111 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3116 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3117 rflags
|= AT_REMOVEDIR
;
3120 dfidp
= get_fid(pdu
, dfid
);
3121 if (dfidp
== NULL
) {
3126 * IF the file is unlinked, we cannot reopen
3127 * the file later. So don't reclaim fd
3129 v9fs_path_init(&path
);
3130 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3134 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3138 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3143 put_fid(pdu
, dfidp
);
3144 v9fs_path_free(&path
);
3146 pdu_complete(pdu
, err
);
3147 v9fs_string_free(&name
);
3151 /* Only works with path name based fid */
3152 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3158 V9fsFidState
*tfidp
;
3159 V9fsState
*s
= pdu
->s
;
3160 V9fsFidState
*dirfidp
= NULL
;
3162 v9fs_path_init(&new_path
);
3163 if (newdirfid
!= -1) {
3164 dirfidp
= get_fid(pdu
, newdirfid
);
3165 if (dirfidp
== NULL
) {
3168 if (fidp
->fid_type
!= P9_FID_NONE
) {
3172 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3177 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3180 v9fs_path_init(&dir_path
);
3181 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3184 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3185 v9fs_path_free(&dir_path
);
3190 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3195 * Fixup fid's pointing to the old name to
3196 * start pointing to the new name
3198 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3199 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3200 /* replace the name */
3201 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3206 put_fid(pdu
, dirfidp
);
3208 v9fs_path_free(&new_path
);
3212 /* Only works with path name based fid */
3213 static void coroutine_fn
v9fs_rename(void *opaque
)
3221 V9fsPDU
*pdu
= opaque
;
3222 V9fsState
*s
= pdu
->s
;
3224 v9fs_string_init(&name
);
3225 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3230 if (name_is_illegal(name
.data
)) {
3235 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3240 fidp
= get_fid(pdu
, fid
);
3245 if (fidp
->fid_type
!= P9_FID_NONE
) {
3249 /* if fs driver is not path based, return EOPNOTSUPP */
3250 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3254 v9fs_path_write_lock(s
);
3255 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3256 v9fs_path_unlock(s
);
3263 pdu_complete(pdu
, err
);
3264 v9fs_string_free(&name
);
3267 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3268 V9fsString
*old_name
,
3270 V9fsString
*new_name
)
3272 V9fsFidState
*tfidp
;
3273 V9fsPath oldpath
, newpath
;
3274 V9fsState
*s
= pdu
->s
;
3277 v9fs_path_init(&oldpath
);
3278 v9fs_path_init(&newpath
);
3279 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3283 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3289 * Fixup fid's pointing to the old name to
3290 * start pointing to the new name
3292 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3293 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3294 /* replace the name */
3295 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3299 v9fs_path_free(&oldpath
);
3300 v9fs_path_free(&newpath
);
3304 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3305 V9fsString
*old_name
,
3307 V9fsString
*new_name
)
3310 V9fsState
*s
= pdu
->s
;
3311 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3313 olddirfidp
= get_fid(pdu
, olddirfid
);
3314 if (olddirfidp
== NULL
) {
3318 if (newdirfid
!= -1) {
3319 newdirfidp
= get_fid(pdu
, newdirfid
);
3320 if (newdirfidp
== NULL
) {
3325 newdirfidp
= get_fid(pdu
, olddirfid
);
3328 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3329 &newdirfidp
->path
, new_name
);
3333 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3334 /* Only for path based fid we need to do the below fixup */
3335 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3336 &newdirfidp
->path
, new_name
);
3340 put_fid(pdu
, olddirfidp
);
3343 put_fid(pdu
, newdirfidp
);
3348 static void coroutine_fn
v9fs_renameat(void *opaque
)
3352 V9fsPDU
*pdu
= opaque
;
3353 V9fsState
*s
= pdu
->s
;
3354 int32_t olddirfid
, newdirfid
;
3355 V9fsString old_name
, new_name
;
3357 v9fs_string_init(&old_name
);
3358 v9fs_string_init(&new_name
);
3359 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3360 &old_name
, &newdirfid
, &new_name
);
3365 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3370 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3371 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3376 v9fs_path_write_lock(s
);
3377 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3378 &old_name
, newdirfid
, &new_name
);
3379 v9fs_path_unlock(s
);
3385 pdu_complete(pdu
, err
);
3386 v9fs_string_free(&old_name
);
3387 v9fs_string_free(&new_name
);
3390 static void coroutine_fn
v9fs_wstat(void *opaque
)
3399 V9fsPDU
*pdu
= opaque
;
3400 V9fsState
*s
= pdu
->s
;
3402 v9fs_stat_init(&v9stat
);
3403 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3407 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3408 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3410 fidp
= get_fid(pdu
, fid
);
3415 /* do we need to sync the file? */
3416 if (donttouch_stat(&v9stat
)) {
3417 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3420 if (v9stat
.mode
!= -1) {
3422 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3426 v9_mode
= stat_to_v9mode(&stbuf
);
3427 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3428 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3429 /* Attempting to change the type */
3433 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3434 v9mode_to_mode(v9stat
.mode
,
3435 &v9stat
.extension
));
3440 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3441 struct timespec times
[2];
3442 if (v9stat
.atime
!= -1) {
3443 times
[0].tv_sec
= v9stat
.atime
;
3444 times
[0].tv_nsec
= 0;
3446 times
[0].tv_nsec
= UTIME_OMIT
;
3448 if (v9stat
.mtime
!= -1) {
3449 times
[1].tv_sec
= v9stat
.mtime
;
3450 times
[1].tv_nsec
= 0;
3452 times
[1].tv_nsec
= UTIME_OMIT
;
3454 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3459 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3460 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3465 if (v9stat
.name
.size
!= 0) {
3466 v9fs_path_write_lock(s
);
3467 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3468 v9fs_path_unlock(s
);
3473 if (v9stat
.length
!= -1) {
3474 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3483 v9fs_stat_free(&v9stat
);
3484 pdu_complete(pdu
, err
);
3487 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3499 int32_t bsize_factor
;
3502 * compute bsize factor based on host file system block size
3505 bsize_factor
= (s
->msize
- P9_IOHDRSZ
) / stbuf
->f_bsize
;
3506 if (!bsize_factor
) {
3509 f_type
= stbuf
->f_type
;
3510 f_bsize
= stbuf
->f_bsize
;
3511 f_bsize
*= bsize_factor
;
3513 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3514 * adjust(divide) the number of blocks, free blocks and available
3515 * blocks by bsize factor
3517 f_blocks
= stbuf
->f_blocks
/ bsize_factor
;
3518 f_bfree
= stbuf
->f_bfree
/ bsize_factor
;
3519 f_bavail
= stbuf
->f_bavail
/ bsize_factor
;
3520 f_files
= stbuf
->f_files
;
3521 f_ffree
= stbuf
->f_ffree
;
3522 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3523 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3524 f_namelen
= stbuf
->f_namelen
;
3526 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3527 f_type
, f_bsize
, f_blocks
, f_bfree
,
3528 f_bavail
, f_files
, f_ffree
,
3529 fsid_val
, f_namelen
);
3532 static void coroutine_fn
v9fs_statfs(void *opaque
)
3538 struct statfs stbuf
;
3539 V9fsPDU
*pdu
= opaque
;
3540 V9fsState
*s
= pdu
->s
;
3542 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3546 fidp
= get_fid(pdu
, fid
);
3551 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3555 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3563 pdu_complete(pdu
, retval
);
3566 static void coroutine_fn
v9fs_mknod(void *opaque
)
3579 V9fsPDU
*pdu
= opaque
;
3581 v9fs_string_init(&name
);
3582 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3583 &major
, &minor
, &gid
);
3587 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3589 if (name_is_illegal(name
.data
)) {
3594 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3599 fidp
= get_fid(pdu
, fid
);
3604 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3605 makedev(major
, minor
), mode
, &stbuf
);
3609 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3613 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3618 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3619 qid
.type
, qid
.version
, qid
.path
);
3623 pdu_complete(pdu
, err
);
3624 v9fs_string_free(&name
);
3628 * Implement posix byte range locking code
3629 * Server side handling of locking code is very simple, because 9p server in
3630 * QEMU can handle only one client. And most of the lock handling
3631 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3632 * do any thing in * qemu 9p server side lock code path.
3633 * So when a TLOCK request comes, always return success
3635 static void coroutine_fn
v9fs_lock(void *opaque
)
3641 int32_t fid
, err
= 0;
3642 V9fsPDU
*pdu
= opaque
;
3644 v9fs_string_init(&flock
.client_id
);
3645 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3646 &flock
.flags
, &flock
.start
, &flock
.length
,
3647 &flock
.proc_id
, &flock
.client_id
);
3651 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3652 flock
.type
, flock
.start
, flock
.length
);
3655 /* We support only block flag now (that too ignored currently) */
3656 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3660 fidp
= get_fid(pdu
, fid
);
3665 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3669 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3674 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3678 pdu_complete(pdu
, err
);
3679 v9fs_string_free(&flock
.client_id
);
3683 * When a TGETLOCK request comes, always return success because all lock
3684 * handling is done by client's VFS layer.
3686 static void coroutine_fn
v9fs_getlock(void *opaque
)
3692 int32_t fid
, err
= 0;
3693 V9fsPDU
*pdu
= opaque
;
3695 v9fs_string_init(&glock
.client_id
);
3696 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3697 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3702 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3703 glock
.type
, glock
.start
, glock
.length
);
3705 fidp
= get_fid(pdu
, fid
);
3710 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3714 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3715 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3716 glock
.start
, glock
.length
, glock
.proc_id
,
3722 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3723 glock
.length
, glock
.proc_id
);
3727 pdu_complete(pdu
, err
);
3728 v9fs_string_free(&glock
.client_id
);
3731 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3733 V9fsPDU
*pdu
= opaque
;
3744 v9fs_string_init(&name
);
3745 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3749 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3751 if (name_is_illegal(name
.data
)) {
3756 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3761 fidp
= get_fid(pdu
, fid
);
3766 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3770 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3774 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3779 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3780 qid
.type
, qid
.version
, qid
.path
, err
);
3784 pdu_complete(pdu
, err
);
3785 v9fs_string_free(&name
);
3788 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3794 int32_t fid
, newfid
;
3795 V9fsFidState
*file_fidp
;
3796 V9fsFidState
*xattr_fidp
= NULL
;
3797 V9fsPDU
*pdu
= opaque
;
3798 V9fsState
*s
= pdu
->s
;
3800 v9fs_string_init(&name
);
3801 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3805 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3807 file_fidp
= get_fid(pdu
, fid
);
3808 if (file_fidp
== NULL
) {
3812 xattr_fidp
= alloc_fid(s
, newfid
);
3813 if (xattr_fidp
== NULL
) {
3817 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3818 if (!v9fs_string_size(&name
)) {
3820 * listxattr request. Get the size first
3822 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3825 clunk_fid(s
, xattr_fidp
->fid
);
3829 * Read the xattr value
3831 xattr_fidp
->fs
.xattr
.len
= size
;
3832 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3833 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3834 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3836 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3837 xattr_fidp
->fs
.xattr
.value
,
3838 xattr_fidp
->fs
.xattr
.len
);
3840 clunk_fid(s
, xattr_fidp
->fid
);
3844 err
= pdu_marshal(pdu
, offset
, "q", size
);
3851 * specific xattr fid. We check for xattr
3852 * presence also collect the xattr size
3854 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3858 clunk_fid(s
, xattr_fidp
->fid
);
3862 * Read the xattr value
3864 xattr_fidp
->fs
.xattr
.len
= size
;
3865 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3866 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3867 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3869 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3870 &name
, xattr_fidp
->fs
.xattr
.value
,
3871 xattr_fidp
->fs
.xattr
.len
);
3873 clunk_fid(s
, xattr_fidp
->fid
);
3877 err
= pdu_marshal(pdu
, offset
, "q", size
);
3883 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3885 put_fid(pdu
, file_fidp
);
3887 put_fid(pdu
, xattr_fidp
);
3890 pdu_complete(pdu
, err
);
3891 v9fs_string_free(&name
);
3894 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3896 int flags
, rflags
= 0;
3902 V9fsFidState
*file_fidp
;
3903 V9fsFidState
*xattr_fidp
;
3904 V9fsPDU
*pdu
= opaque
;
3906 v9fs_string_init(&name
);
3907 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3911 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3913 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3918 if (flags
& P9_XATTR_CREATE
) {
3919 rflags
|= XATTR_CREATE
;
3922 if (flags
& P9_XATTR_REPLACE
) {
3923 rflags
|= XATTR_REPLACE
;
3926 if (size
> XATTR_SIZE_MAX
) {
3931 file_fidp
= get_fid(pdu
, fid
);
3932 if (file_fidp
== NULL
) {
3936 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3941 /* Make the file fid point to xattr */
3942 xattr_fidp
= file_fidp
;
3943 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3944 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3945 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3946 xattr_fidp
->fs
.xattr
.len
= size
;
3947 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3948 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3949 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3950 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3953 put_fid(pdu
, file_fidp
);
3955 pdu_complete(pdu
, err
);
3956 v9fs_string_free(&name
);
3959 static void coroutine_fn
v9fs_readlink(void *opaque
)
3961 V9fsPDU
*pdu
= opaque
;
3968 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3972 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3973 fidp
= get_fid(pdu
, fid
);
3979 v9fs_string_init(&target
);
3980 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3984 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3986 v9fs_string_free(&target
);
3990 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3991 v9fs_string_free(&target
);
3995 pdu_complete(pdu
, err
);
3998 static CoroutineEntry
*pdu_co_handlers
[] = {
3999 [P9_TREADDIR
] = v9fs_readdir
,
4000 [P9_TSTATFS
] = v9fs_statfs
,
4001 [P9_TGETATTR
] = v9fs_getattr
,
4002 [P9_TSETATTR
] = v9fs_setattr
,
4003 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
4004 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
4005 [P9_TMKNOD
] = v9fs_mknod
,
4006 [P9_TRENAME
] = v9fs_rename
,
4007 [P9_TLOCK
] = v9fs_lock
,
4008 [P9_TGETLOCK
] = v9fs_getlock
,
4009 [P9_TRENAMEAT
] = v9fs_renameat
,
4010 [P9_TREADLINK
] = v9fs_readlink
,
4011 [P9_TUNLINKAT
] = v9fs_unlinkat
,
4012 [P9_TMKDIR
] = v9fs_mkdir
,
4013 [P9_TVERSION
] = v9fs_version
,
4014 [P9_TLOPEN
] = v9fs_open
,
4015 [P9_TATTACH
] = v9fs_attach
,
4016 [P9_TSTAT
] = v9fs_stat
,
4017 [P9_TWALK
] = v9fs_walk
,
4018 [P9_TCLUNK
] = v9fs_clunk
,
4019 [P9_TFSYNC
] = v9fs_fsync
,
4020 [P9_TOPEN
] = v9fs_open
,
4021 [P9_TREAD
] = v9fs_read
,
4023 [P9_TAUTH
] = v9fs_auth
,
4025 [P9_TFLUSH
] = v9fs_flush
,
4026 [P9_TLINK
] = v9fs_link
,
4027 [P9_TSYMLINK
] = v9fs_symlink
,
4028 [P9_TCREATE
] = v9fs_create
,
4029 [P9_TLCREATE
] = v9fs_lcreate
,
4030 [P9_TWRITE
] = v9fs_write
,
4031 [P9_TWSTAT
] = v9fs_wstat
,
4032 [P9_TREMOVE
] = v9fs_remove
,
4035 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
4037 V9fsPDU
*pdu
= opaque
;
4038 pdu_complete(pdu
, -EOPNOTSUPP
);
4041 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
4043 V9fsPDU
*pdu
= opaque
;
4044 pdu_complete(pdu
, -EROFS
);
4047 static inline bool is_read_only_op(V9fsPDU
*pdu
)
4074 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
4077 CoroutineEntry
*handler
;
4078 V9fsState
*s
= pdu
->s
;
4080 pdu
->size
= le32_to_cpu(hdr
->size_le
);
4082 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
4084 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
4085 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
4086 handler
= v9fs_op_not_supp
;
4087 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4088 handler
= v9fs_fs_ro
;
4090 handler
= pdu_co_handlers
[pdu
->id
];
4093 qemu_co_queue_init(&pdu
->complete
);
4094 co
= qemu_coroutine_create(handler
, pdu
);
4095 qemu_coroutine_enter(co
);
4098 /* Returns 0 on success, 1 on failure. */
4099 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4109 assert(!s
->transport
);
4112 /* initialize pdu allocator */
4113 QLIST_INIT(&s
->free_list
);
4114 QLIST_INIT(&s
->active_list
);
4115 for (i
= 0; i
< MAX_REQ
; i
++) {
4116 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4121 v9fs_path_init(&path
);
4123 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4126 /* We don't have a fsdev identified by fsdev_id */
4127 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4129 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4133 if (!s
->fsconf
.tag
) {
4134 /* we haven't specified a mount_tag */
4135 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4136 s
->fsconf
.fsdev_id
);
4140 s
->ctx
.export_flags
= fse
->export_flags
;
4141 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4142 s
->ctx
.exops
.get_st_gen
= NULL
;
4143 len
= strlen(s
->fsconf
.tag
);
4144 if (len
> MAX_TAG_LEN
- 1) {
4145 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4146 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4150 s
->tag
= g_strdup(s
->fsconf
.tag
);
4155 s
->ctx
.fmode
= fse
->fmode
;
4156 s
->ctx
.dmode
= fse
->dmode
;
4158 QSIMPLEQ_INIT(&s
->fid_list
);
4159 qemu_co_rwlock_init(&s
->rename_lock
);
4161 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4162 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4163 s
->fsconf
.fsdev_id
);
4168 * Check details of export path, We need to use fs driver
4169 * call back to do that. Since we are in the init path, we don't
4170 * use co-routines here.
4172 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4174 "error in converting name to path %s", strerror(errno
));
4177 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4178 error_setg(errp
, "share path %s does not exist", fse
->path
);
4180 } else if (!S_ISDIR(stat
.st_mode
)) {
4181 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4185 s
->dev_id
= stat
.st_dev
;
4187 /* init inode remapping : */
4188 /* hash table for variable length inode suffixes */
4189 qpd_table_init(&s
->qpd_table
);
4190 /* hash table for slow/full inode remapping (most users won't need it) */
4191 qpf_table_init(&s
->qpf_table
);
4192 /* hash table for quick inode remapping */
4193 qpp_table_init(&s
->qpp_table
);
4195 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4196 s
->qp_fullpath_next
= 1;
4198 s
->ctx
.fst
= &fse
->fst
;
4199 fsdev_throttle_init(s
->ctx
.fst
);
4204 v9fs_device_unrealize_common(s
);
4206 v9fs_path_free(&path
);
4210 void v9fs_device_unrealize_common(V9fsState
*s
)
4212 if (s
->ops
&& s
->ops
->cleanup
) {
4213 s
->ops
->cleanup(&s
->ctx
);
4216 fsdev_throttle_cleanup(s
->ctx
.fst
);
4219 qp_table_destroy(&s
->qpd_table
);
4220 qp_table_destroy(&s
->qpp_table
);
4221 qp_table_destroy(&s
->qpf_table
);
4222 g_free(s
->ctx
.fs_root
);
4225 typedef struct VirtfsCoResetData
{
4228 } VirtfsCoResetData
;
4230 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4232 VirtfsCoResetData
*data
= opaque
;
4234 virtfs_reset(&data
->pdu
);
4238 void v9fs_reset(V9fsState
*s
)
4240 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4243 while (!QLIST_EMPTY(&s
->active_list
)) {
4244 aio_poll(qemu_get_aio_context(), true);
4247 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4248 qemu_coroutine_enter(co
);
4250 while (!data
.done
) {
4251 aio_poll(qemu_get_aio_context(), true);
4255 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4258 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4259 error_report("Failed to get the resource limit");
4262 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/ 3);
4263 open_fd_rc
= rlim
.rlim_cur
/ 2;