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"
21 #include <linux/limits.h>
25 #include <glib/gprintf.h>
26 #include "hw/virtio/virtio.h"
27 #include "qapi/error.h"
28 #include "qemu/error-report.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/sockets.h"
32 #include "virtio-9p.h"
33 #include "fsdev/qemu-fsdev.h"
38 #include "migration/blocker.h"
39 #include "qemu/xxhash.h"
44 static int open_fd_rc
;
58 P9ARRAY_DEFINE_TYPE(V9fsPath
, v9fs_path_free
);
60 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
66 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
72 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
78 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
84 static int omode_to_uflags(int8_t mode
)
107 if (mode
& Oappend
) {
118 typedef struct DotlOpenflagMap
{
123 static int dotl_to_open_flags(int flags
)
127 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
128 * and P9_DOTL_NOACCESS
130 int oflags
= flags
& O_ACCMODE
;
132 DotlOpenflagMap dotl_oflag_map
[] = {
133 { P9_DOTL_CREATE
, O_CREAT
},
134 { P9_DOTL_EXCL
, O_EXCL
},
135 { P9_DOTL_NOCTTY
, O_NOCTTY
},
136 { P9_DOTL_TRUNC
, O_TRUNC
},
137 { P9_DOTL_APPEND
, O_APPEND
},
138 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
139 { P9_DOTL_DSYNC
, O_DSYNC
},
140 { P9_DOTL_FASYNC
, FASYNC
},
141 #ifndef CONFIG_DARWIN
142 { P9_DOTL_NOATIME
, O_NOATIME
},
144 * On Darwin, we could map to F_NOCACHE, which is
145 * similar, but doesn't quite have the same
146 * semantics. However, we don't support O_DIRECT
147 * even on linux at the moment, so we just ignore
150 { P9_DOTL_DIRECT
, O_DIRECT
},
152 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
153 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
154 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
155 { P9_DOTL_SYNC
, O_SYNC
},
158 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
159 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
160 oflags
|= dotl_oflag_map
[i
].open_flag
;
167 void cred_init(FsCred
*credp
)
175 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
179 * Filter the client open flags
181 flags
= dotl_to_open_flags(oflags
);
182 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
183 #ifndef CONFIG_DARWIN
185 * Ignore direct disk access hint until the server supports it.
192 void v9fs_path_init(V9fsPath
*path
)
198 void v9fs_path_free(V9fsPath
*path
)
206 void G_GNUC_PRINTF(2, 3)
207 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
211 v9fs_path_free(path
);
214 /* Bump the size for including terminating NULL */
215 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
219 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
222 dst
->size
= src
->size
;
223 dst
->data
= g_memdup(src
->data
, src
->size
);
226 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
227 const char *name
, V9fsPath
*path
)
230 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
238 * Return TRUE if s1 is an ancestor of s2.
240 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
241 * As a special case, We treat s1 as ancestor of s2 if they are same!
243 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
245 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
246 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
253 static size_t v9fs_string_size(V9fsString
*str
)
259 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
260 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
263 if (f
->fid_type
== P9_FID_FILE
) {
264 if (f
->fs
.fd
== -1) {
266 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
267 } while (err
== -EINTR
&& !pdu
->cancelled
);
269 } else if (f
->fid_type
== P9_FID_DIR
) {
270 if (f
->fs
.dir
.stream
== NULL
) {
272 err
= v9fs_co_opendir(pdu
, f
);
273 } while (err
== -EINTR
&& !pdu
->cancelled
);
279 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
283 V9fsState
*s
= pdu
->s
;
285 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
289 * Update the fid ref upfront so that
290 * we don't get reclaimed when we yield
295 * check whether we need to reopen the
296 * file. We might have closed the fd
297 * while trying to free up some file
300 err
= v9fs_reopen_fid(pdu
, f
);
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
309 f
->flags
|= FID_REFERENCED
;
316 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
320 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
321 /* If fid is already there return NULL */
327 f
= g_new0(V9fsFidState
, 1);
329 f
->fid_type
= P9_FID_NONE
;
332 * Mark the fid as referenced so that the LRU
333 * reclaim won't close the file descriptor
335 f
->flags
|= FID_REFERENCED
;
336 QSIMPLEQ_INSERT_TAIL(&s
->fid_list
, f
, next
);
338 v9fs_readdir_init(s
->proto_version
, &f
->fs
.dir
);
339 v9fs_readdir_init(s
->proto_version
, &f
->fs_reclaim
.dir
);
344 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
348 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
349 /* getxattr/listxattr fid */
353 * if this is fid for setxattr. clunk should
354 * result in setxattr localcall
356 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
357 /* clunk after partial write */
361 if (fidp
->fs
.xattr
.len
) {
362 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
363 fidp
->fs
.xattr
.value
,
365 fidp
->fs
.xattr
.flags
);
367 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
370 v9fs_string_free(&fidp
->fs
.xattr
.name
);
372 g_free(fidp
->fs
.xattr
.value
);
376 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
380 if (fidp
->fid_type
== P9_FID_FILE
) {
381 /* If we reclaimed the fd no need to close */
382 if (fidp
->fs
.fd
!= -1) {
383 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
385 } else if (fidp
->fid_type
== P9_FID_DIR
) {
386 if (fidp
->fs
.dir
.stream
!= NULL
) {
387 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
389 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
390 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
392 v9fs_path_free(&fidp
->path
);
397 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
402 * Don't free the fid if it is in reclaim list
404 if (!fidp
->ref
&& fidp
->clunked
) {
405 if (fidp
->fid
== pdu
->s
->root_fid
) {
407 * if the clunked fid is root fid then we
408 * have unmounted the fs on the client side.
409 * delete the migration blocker. Ideally, this
410 * should be hooked to transport close notification
412 if (pdu
->s
->migration_blocker
) {
413 migrate_del_blocker(pdu
->s
->migration_blocker
);
414 error_free(pdu
->s
->migration_blocker
);
415 pdu
->s
->migration_blocker
= NULL
;
418 return free_fid(pdu
, fidp
);
423 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
427 QSIMPLEQ_FOREACH(fidp
, &s
->fid_list
, next
) {
428 if (fidp
->fid
== fid
) {
429 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
430 fidp
->clunked
= true;
437 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
439 int reclaim_count
= 0;
440 V9fsState
*s
= pdu
->s
;
442 QSLIST_HEAD(, V9fsFidState
) reclaim_list
=
443 QSLIST_HEAD_INITIALIZER(reclaim_list
);
445 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
447 * Unlink fids cannot be reclaimed. Check
448 * for them and skip them. Also skip fids
449 * currently being operated on.
451 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
455 * if it is a recently referenced fid
456 * we leave the fid untouched and clear the
457 * reference bit. We come back to it later
458 * in the next iteration. (a simple LRU without
459 * moving list elements around)
461 if (f
->flags
& FID_REFERENCED
) {
462 f
->flags
&= ~FID_REFERENCED
;
466 * Add fids to reclaim list.
468 if (f
->fid_type
== P9_FID_FILE
) {
469 if (f
->fs
.fd
!= -1) {
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
.fd
= f
->fs
.fd
;
480 } else if (f
->fid_type
== P9_FID_DIR
) {
481 if (f
->fs
.dir
.stream
!= NULL
) {
483 * Up the reference count so that
484 * a clunk request won't free this fid
487 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
488 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
489 f
->fs
.dir
.stream
= NULL
;
493 if (reclaim_count
>= open_fd_rc
) {
498 * Now close the fid in reclaim list. Free them if they
499 * are already clunked.
501 while (!QSLIST_EMPTY(&reclaim_list
)) {
502 f
= QSLIST_FIRST(&reclaim_list
);
503 QSLIST_REMOVE(&reclaim_list
, f
, V9fsFidState
, reclaim_next
);
504 if (f
->fid_type
== P9_FID_FILE
) {
505 v9fs_co_close(pdu
, &f
->fs_reclaim
);
506 } else if (f
->fid_type
== P9_FID_DIR
) {
507 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
510 * Now drop the fid reference, free it
517 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
520 V9fsState
*s
= pdu
->s
;
521 V9fsFidState
*fidp
, *fidp_next
;
523 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
529 * v9fs_reopen_fid() can yield : a reference on the fid must be held
530 * to ensure its pointer remains valid and we can safely pass it to
531 * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so
532 * we must keep a reference on the next fid as well. So the logic here
533 * is to get a reference on a fid and only put it back during the next
534 * iteration after we could get a reference on the next fid. Start with
537 for (fidp
->ref
++; fidp
; fidp
= fidp_next
) {
538 if (fidp
->path
.size
== path
->size
&&
539 !memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
540 /* Mark the fid non reclaimable. */
541 fidp
->flags
|= FID_NON_RECLAIMABLE
;
543 /* reopen the file/dir if already closed */
544 err
= v9fs_reopen_fid(pdu
, fidp
);
551 fidp_next
= QSIMPLEQ_NEXT(fidp
, next
);
555 * Ensure the next fid survives a potential clunk request during
556 * put_fid() below and v9fs_reopen_fid() in the next iteration.
561 /* We're done with this fid */
568 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
570 V9fsState
*s
= pdu
->s
;
574 while (!QSIMPLEQ_EMPTY(&s
->fid_list
)) {
576 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
580 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
581 fidp
->clunked
= true;
587 #define P9_QID_TYPE_DIR 0x80
588 #define P9_QID_TYPE_SYMLINK 0x02
590 #define P9_STAT_MODE_DIR 0x80000000
591 #define P9_STAT_MODE_APPEND 0x40000000
592 #define P9_STAT_MODE_EXCL 0x20000000
593 #define P9_STAT_MODE_MOUNT 0x10000000
594 #define P9_STAT_MODE_AUTH 0x08000000
595 #define P9_STAT_MODE_TMP 0x04000000
596 #define P9_STAT_MODE_SYMLINK 0x02000000
597 #define P9_STAT_MODE_LINK 0x01000000
598 #define P9_STAT_MODE_DEVICE 0x00800000
599 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
600 #define P9_STAT_MODE_SOCKET 0x00100000
601 #define P9_STAT_MODE_SETUID 0x00080000
602 #define P9_STAT_MODE_SETGID 0x00040000
603 #define P9_STAT_MODE_SETVTX 0x00010000
605 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
606 P9_STAT_MODE_SYMLINK | \
607 P9_STAT_MODE_LINK | \
608 P9_STAT_MODE_DEVICE | \
609 P9_STAT_MODE_NAMED_PIPE | \
612 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
613 static inline uint8_t mirror8bit(uint8_t byte
)
615 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
618 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
619 static inline uint64_t mirror64bit(uint64_t value
)
621 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
622 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
623 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
624 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
625 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
626 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
627 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
628 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
632 * Parameter k for the Exponential Golomb algorihm to be used.
634 * The smaller this value, the smaller the minimum bit count for the Exp.
635 * Golomb generated affixes will be (at lowest index) however for the
636 * price of having higher maximum bit count of generated affixes (at highest
637 * index). Likewise increasing this parameter yields in smaller maximum bit
638 * count for the price of having higher minimum bit count.
640 * In practice that means: a good value for k depends on the expected amount
641 * of devices to be exposed by one export. For a small amount of devices k
642 * should be small, for a large amount of devices k might be increased
643 * instead. The default of k=0 should be fine for most users though.
645 * IMPORTANT: In case this ever becomes a runtime parameter; the value of
646 * k should not change as long as guest is still running! Because that would
647 * cause completely different inode numbers to be generated on guest.
649 #define EXP_GOLOMB_K 0
652 * expGolombEncode() - Exponential Golomb algorithm for arbitrary k
655 * @n: natural number (or index) of the prefix to be generated
657 * @k: parameter k of Exp. Golomb algorithm to be used
658 * (see comment on EXP_GOLOMB_K macro for details about k)
659 * Return: prefix for given @n and @k
661 * The Exponential Golomb algorithm generates prefixes (NOT suffixes!)
662 * with growing length and with the mathematical property of being
663 * "prefix-free". The latter means the generated prefixes can be prepended
664 * in front of arbitrary numbers and the resulting concatenated numbers are
665 * guaranteed to be always unique.
667 * This is a minor adjustment to the original Exp. Golomb algorithm in the
668 * sense that lowest allowed index (@n) starts with 1, not with zero.
670 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
672 const uint64_t value
= n
+ (1 << k
) - 1;
673 const int bits
= (int) log2(value
) + 1;
674 return (VariLenAffix
) {
675 .type
= AffixType_Prefix
,
677 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
682 * invertAffix() - Converts a suffix into a prefix, or a prefix into a suffix.
683 * @affix: either suffix or prefix to be inverted
684 * Return: inversion of passed @affix
686 * Simply mirror all bits of the affix value, for the purpose to preserve
687 * respectively the mathematical "prefix-free" or "suffix-free" property
688 * after the conversion.
690 * If a passed prefix is suitable to create unique numbers, then the
691 * returned suffix is suitable to create unique numbers as well (and vice
694 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
696 return (VariLenAffix
) {
698 (affix
->type
== AffixType_Suffix
) ?
699 AffixType_Prefix
: AffixType_Suffix
,
701 mirror64bit(affix
->value
) >>
702 ((sizeof(affix
->value
) * 8) - affix
->bits
),
708 * affixForIndex() - Generates suffix numbers with "suffix-free" property.
709 * @index: natural number (or index) of the suffix to be generated
711 * Return: Suffix suitable to assemble unique number.
713 * This is just a wrapper function on top of the Exp. Golomb algorithm.
715 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
716 * this function converts the Exp. Golomb prefixes into appropriate suffixes
717 * which are still suitable for generating unique numbers.
719 static VariLenAffix
affixForIndex(uint64_t index
)
722 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
723 return invertAffix(&prefix
); /* convert prefix to suffix */
726 /* creative abuse of tb_hash_func7, which is based on xxhash */
727 static uint32_t qpp_hash(QppEntry e
)
729 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
732 static uint32_t qpf_hash(QpfEntry e
)
734 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
737 static bool qpd_cmp_func(const void *obj
, const void *userp
)
739 const QpdEntry
*e1
= obj
, *e2
= userp
;
740 return e1
->dev
== e2
->dev
;
743 static bool qpp_cmp_func(const void *obj
, const void *userp
)
745 const QppEntry
*e1
= obj
, *e2
= userp
;
746 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
749 static bool qpf_cmp_func(const void *obj
, const void *userp
)
751 const QpfEntry
*e1
= obj
, *e2
= userp
;
752 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
755 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
760 static void qp_table_destroy(struct qht
*ht
)
762 if (!ht
|| !ht
->map
) {
765 qht_iter(ht
, qp_table_remove
, NULL
);
769 static void qpd_table_init(struct qht
*ht
)
771 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
774 static void qpp_table_init(struct qht
*ht
)
776 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
779 static void qpf_table_init(struct qht
*ht
)
781 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
785 * Returns how many (high end) bits of inode numbers of the passed fs
786 * device shall be used (in combination with the device number) to
787 * generate hash values for qpp_table entries.
789 * This function is required if variable length suffixes are used for inode
790 * number mapping on guest level. Since a device may end up having multiple
791 * entries in qpp_table, each entry most probably with a different suffix
792 * length, we thus need this function in conjunction with qpd_table to
793 * "agree" about a fix amount of bits (per device) to be always used for
794 * generating hash values for the purpose of accessing qpp_table in order
795 * get consistent behaviour when accessing qpp_table.
797 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
805 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
807 val
= g_new0(QpdEntry
, 1);
809 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
810 val
->prefix_bits
= affix
.bits
;
811 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
812 pdu
->s
->qp_ndevices
++;
814 return val
->prefix_bits
;
818 * Slow / full mapping host inode nr -> guest inode nr.
820 * This function performs a slower and much more costly remapping of an
821 * original file inode number on host to an appropriate different inode
822 * number on guest. For every (dev, inode) combination on host a new
823 * sequential number is generated, cached and exposed as inode number on
826 * This is just a "last resort" fallback solution if the much faster/cheaper
827 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
828 * expected ever to be used at all though.
830 * See qid_path_suffixmap() for details
833 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
837 .dev
= stbuf
->st_dev
,
840 uint32_t hash
= qpf_hash(lookup
);
843 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
846 if (pdu
->s
->qp_fullpath_next
== 0) {
847 /* no more files can be mapped :'( */
849 "9p: No more prefixes available for remapping inodes from "
855 val
= g_new0(QpfEntry
, 1);
858 /* new unique inode and device combo */
859 affix
= affixForIndex(
860 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
862 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
863 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
864 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
872 * Quick mapping host inode nr -> guest inode nr.
874 * This function performs quick remapping of an original file inode number
875 * on host to an appropriate different inode number on guest. This remapping
876 * of inodes is required to avoid inode nr collisions on guest which would
877 * happen if the 9p export contains more than 1 exported file system (or
878 * more than 1 file system data set), because unlike on host level where the
879 * files would have different device nrs, all files exported by 9p would
880 * share the same device nr on guest (the device nr of the virtual 9p device
883 * Inode remapping is performed by chopping off high end bits of the original
884 * inode number from host, shifting the result upwards and then assigning a
885 * generated suffix number for the low end bits, where the same suffix number
886 * will be shared by all inodes with the same device id AND the same high end
887 * bits that have been chopped off. That approach utilizes the fact that inode
888 * numbers very likely share the same high end bits (i.e. due to their common
889 * sequential generation by file systems) and hence we only have to generate
890 * and track a very limited amount of suffixes in practice due to that.
892 * We generate variable size suffixes for that purpose. The 1st generated
893 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
894 * the original inode number. The subsequent suffixes being generated will
895 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
896 * generated will have 3 bits and hence we have to chop off 3 bits from their
897 * original inodes, and so on. That approach of using variable length suffixes
898 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
899 * limited amount of devices are shared by the same export (e.g. typically
900 * less than 2 dozen devices per 9p export), so in practice we need to chop
901 * off less bits than with fixed size prefixes and yet are flexible to add
902 * new devices at runtime below host's export directory at any time without
903 * having to reboot guest nor requiring to reconfigure guest for that. And due
904 * to the very limited amount of original high end bits that we chop off that
905 * way, the total amount of suffixes we need to generate is less than by using
906 * fixed size prefixes and hence it also improves performance of the inode
907 * remapping algorithm, and finally has the nice side effect that the inode
908 * numbers on guest will be much smaller & human friendly. ;-)
910 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
913 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
915 .dev
= stbuf
->st_dev
,
916 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
918 uint32_t hash
= qpp_hash(lookup
);
920 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
923 if (pdu
->s
->qp_affix_next
== 0) {
924 /* we ran out of affixes */
926 "9p: Potential degraded performance of inode remapping"
931 val
= g_new0(QppEntry
, 1);
934 /* new unique inode affix and device combo */
935 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
936 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
937 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
939 /* assuming generated affix to be suffix type, not prefix */
940 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
944 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
949 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
950 /* map inode+device to qid path (fast path) */
951 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
952 if (err
== -ENFILE
) {
953 /* fast path didn't work, fall back to full map */
954 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
960 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
961 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
963 "9p: Multiple devices detected in same VirtFS export. "
964 "Access of guest to additional devices is (partly) "
965 "denied due to virtfs option 'multidevs=forbid' being "
971 "9p: Multiple devices detected in same VirtFS export, "
972 "which might lead to file ID collisions and severe "
973 "misbehaviours on guest! You should either use a "
974 "separate export for each device shared from host or "
975 "use virtfs option 'multidevs=remap'!"
979 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
980 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
981 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
984 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
986 if (S_ISDIR(stbuf
->st_mode
)) {
987 qidp
->type
|= P9_QID_TYPE_DIR
;
989 if (S_ISLNK(stbuf
->st_mode
)) {
990 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
996 V9fsPDU
*pdu_alloc(V9fsState
*s
)
1000 if (!QLIST_EMPTY(&s
->free_list
)) {
1001 pdu
= QLIST_FIRST(&s
->free_list
);
1002 QLIST_REMOVE(pdu
, next
);
1003 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
1008 void pdu_free(V9fsPDU
*pdu
)
1010 V9fsState
*s
= pdu
->s
;
1012 g_assert(!pdu
->cancelled
);
1013 QLIST_REMOVE(pdu
, next
);
1014 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
1017 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
1019 int8_t id
= pdu
->id
+ 1; /* Response */
1020 V9fsState
*s
= pdu
->s
;
1024 * The 9p spec requires that successfully cancelled pdus receive no reply.
1025 * Sending a reply would confuse clients because they would
1026 * assume that any EINTR is the actual result of the operation,
1027 * rather than a consequence of the cancellation. However, if
1028 * the operation completed (succesfully or with an error other
1029 * than caused be cancellation), we do send out that reply, both
1030 * for efficiency and to avoid confusing the rest of the state machine
1031 * that assumes passing a non-error here will mean a successful
1032 * transmission of the reply.
1034 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1036 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1045 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1048 str
.data
= strerror(err
);
1049 str
.size
= strlen(str
.data
);
1051 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1058 err
= errno_to_dotl(err
);
1061 ret
= pdu_marshal(pdu
, len
, "d", err
);
1067 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1070 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1073 /* fill out the header */
1074 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1078 /* keep these in sync */
1083 pdu
->s
->transport
->push_and_notify(pdu
);
1085 /* Now wakeup anybody waiting in flush for this request */
1086 if (!qemu_co_queue_next(&pdu
->complete
)) {
1091 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1096 if (mode
& P9_STAT_MODE_DIR
) {
1100 if (mode
& P9_STAT_MODE_SYMLINK
) {
1103 if (mode
& P9_STAT_MODE_SOCKET
) {
1106 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1109 if (mode
& P9_STAT_MODE_DEVICE
) {
1110 if (extension
->size
&& extension
->data
[0] == 'c') {
1117 if (!(ret
& ~0777)) {
1121 if (mode
& P9_STAT_MODE_SETUID
) {
1124 if (mode
& P9_STAT_MODE_SETGID
) {
1127 if (mode
& P9_STAT_MODE_SETVTX
) {
1134 static int donttouch_stat(V9fsStat
*stat
)
1136 if (stat
->type
== -1 &&
1138 stat
->qid
.type
== 0xff &&
1139 stat
->qid
.version
== (uint32_t) -1 &&
1140 stat
->qid
.path
== (uint64_t) -1 &&
1142 stat
->atime
== -1 &&
1143 stat
->mtime
== -1 &&
1144 stat
->length
== -1 &&
1149 stat
->n_uid
== -1 &&
1150 stat
->n_gid
== -1 &&
1151 stat
->n_muid
== -1) {
1158 static void v9fs_stat_init(V9fsStat
*stat
)
1160 v9fs_string_init(&stat
->name
);
1161 v9fs_string_init(&stat
->uid
);
1162 v9fs_string_init(&stat
->gid
);
1163 v9fs_string_init(&stat
->muid
);
1164 v9fs_string_init(&stat
->extension
);
1167 static void v9fs_stat_free(V9fsStat
*stat
)
1169 v9fs_string_free(&stat
->name
);
1170 v9fs_string_free(&stat
->uid
);
1171 v9fs_string_free(&stat
->gid
);
1172 v9fs_string_free(&stat
->muid
);
1173 v9fs_string_free(&stat
->extension
);
1176 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1180 mode
= stbuf
->st_mode
& 0777;
1181 if (S_ISDIR(stbuf
->st_mode
)) {
1182 mode
|= P9_STAT_MODE_DIR
;
1185 if (S_ISLNK(stbuf
->st_mode
)) {
1186 mode
|= P9_STAT_MODE_SYMLINK
;
1189 if (S_ISSOCK(stbuf
->st_mode
)) {
1190 mode
|= P9_STAT_MODE_SOCKET
;
1193 if (S_ISFIFO(stbuf
->st_mode
)) {
1194 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1197 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1198 mode
|= P9_STAT_MODE_DEVICE
;
1201 if (stbuf
->st_mode
& S_ISUID
) {
1202 mode
|= P9_STAT_MODE_SETUID
;
1205 if (stbuf
->st_mode
& S_ISGID
) {
1206 mode
|= P9_STAT_MODE_SETGID
;
1209 if (stbuf
->st_mode
& S_ISVTX
) {
1210 mode
|= P9_STAT_MODE_SETVTX
;
1216 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1217 const char *basename
,
1218 const struct stat
*stbuf
,
1223 memset(v9stat
, 0, sizeof(*v9stat
));
1225 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1229 v9stat
->mode
= stat_to_v9mode(stbuf
);
1230 v9stat
->atime
= stbuf
->st_atime
;
1231 v9stat
->mtime
= stbuf
->st_mtime
;
1232 v9stat
->length
= stbuf
->st_size
;
1234 v9fs_string_free(&v9stat
->uid
);
1235 v9fs_string_free(&v9stat
->gid
);
1236 v9fs_string_free(&v9stat
->muid
);
1238 v9stat
->n_uid
= stbuf
->st_uid
;
1239 v9stat
->n_gid
= stbuf
->st_gid
;
1242 v9fs_string_free(&v9stat
->extension
);
1244 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1245 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1249 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1250 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1251 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1252 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1253 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1254 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1255 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1258 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1261 v9fs_string_size(&v9stat
->name
) +
1262 v9fs_string_size(&v9stat
->uid
) +
1263 v9fs_string_size(&v9stat
->gid
) +
1264 v9fs_string_size(&v9stat
->muid
) +
1265 v9fs_string_size(&v9stat
->extension
);
1269 #define P9_STATS_MODE 0x00000001ULL
1270 #define P9_STATS_NLINK 0x00000002ULL
1271 #define P9_STATS_UID 0x00000004ULL
1272 #define P9_STATS_GID 0x00000008ULL
1273 #define P9_STATS_RDEV 0x00000010ULL
1274 #define P9_STATS_ATIME 0x00000020ULL
1275 #define P9_STATS_MTIME 0x00000040ULL
1276 #define P9_STATS_CTIME 0x00000080ULL
1277 #define P9_STATS_INO 0x00000100ULL
1278 #define P9_STATS_SIZE 0x00000200ULL
1279 #define P9_STATS_BLOCKS 0x00000400ULL
1281 #define P9_STATS_BTIME 0x00000800ULL
1282 #define P9_STATS_GEN 0x00001000ULL
1283 #define P9_STATS_DATA_VERSION 0x00002000ULL
1285 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1286 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1290 * blksize_to_iounit() - Block size exposed to 9p client.
1291 * Return: block size
1293 * @pdu: 9p client request
1294 * @blksize: host filesystem's block size
1296 * Convert host filesystem's block size into an appropriate block size for
1297 * 9p client (guest OS side). The value returned suggests an "optimum" block
1298 * size for 9p I/O, i.e. to maximize performance.
1300 static int32_t blksize_to_iounit(const V9fsPDU
*pdu
, int32_t blksize
)
1303 V9fsState
*s
= pdu
->s
;
1306 * iounit should be multiples of blksize (host filesystem block size)
1307 * as well as less than (client msize - P9_IOHDRSZ)
1310 iounit
= QEMU_ALIGN_DOWN(s
->msize
- P9_IOHDRSZ
, blksize
);
1313 iounit
= s
->msize
- P9_IOHDRSZ
;
1318 static int32_t stat_to_iounit(const V9fsPDU
*pdu
, const struct stat
*stbuf
)
1320 return blksize_to_iounit(pdu
, stbuf
->st_blksize
);
1323 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1324 V9fsStatDotl
*v9lstat
)
1326 memset(v9lstat
, 0, sizeof(*v9lstat
));
1328 v9lstat
->st_mode
= stbuf
->st_mode
;
1329 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1330 v9lstat
->st_uid
= stbuf
->st_uid
;
1331 v9lstat
->st_gid
= stbuf
->st_gid
;
1332 v9lstat
->st_rdev
= host_dev_to_dotl_dev(stbuf
->st_rdev
);
1333 v9lstat
->st_size
= stbuf
->st_size
;
1334 v9lstat
->st_blksize
= stat_to_iounit(pdu
, stbuf
);
1335 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1336 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1337 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1338 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1339 #ifdef CONFIG_DARWIN
1340 v9lstat
->st_atime_nsec
= stbuf
->st_atimespec
.tv_nsec
;
1341 v9lstat
->st_mtime_nsec
= stbuf
->st_mtimespec
.tv_nsec
;
1342 v9lstat
->st_ctime_nsec
= stbuf
->st_ctimespec
.tv_nsec
;
1344 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1345 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1346 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1348 /* Currently we only support BASIC fields in stat */
1349 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1351 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1354 static void print_sg(struct iovec
*sg
, int cnt
)
1358 printf("sg[%d]: {", cnt
);
1359 for (i
= 0; i
< cnt
; i
++) {
1363 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1368 /* Will call this only for path name based fid */
1369 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1372 v9fs_path_init(&str
);
1373 v9fs_path_copy(&str
, dst
);
1374 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1375 v9fs_path_free(&str
);
1378 static inline bool is_ro_export(FsContext
*ctx
)
1380 return ctx
->export_flags
& V9FS_RDONLY
;
1383 static void coroutine_fn
v9fs_version(void *opaque
)
1386 V9fsPDU
*pdu
= opaque
;
1387 V9fsState
*s
= pdu
->s
;
1391 v9fs_string_init(&version
);
1392 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1396 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1400 if (!strcmp(version
.data
, "9P2000.u")) {
1401 s
->proto_version
= V9FS_PROTO_2000U
;
1402 } else if (!strcmp(version
.data
, "9P2000.L")) {
1403 s
->proto_version
= V9FS_PROTO_2000L
;
1405 v9fs_string_sprintf(&version
, "unknown");
1406 /* skip min. msize check, reporting invalid version has priority */
1410 if (s
->msize
< P9_MIN_MSIZE
) {
1413 "9pfs: Client requested msize < minimum msize ("
1414 stringify(P9_MIN_MSIZE
) ") supported by this server."
1419 /* 8192 is the default msize of Linux clients */
1420 if (s
->msize
<= 8192 && !(s
->ctx
.export_flags
& V9FS_NO_PERF_WARN
)) {
1422 "9p: degraded performance: a reasonable high msize should be "
1423 "chosen on client/guest side (chosen msize is <= 8192). See "
1424 "https://wiki.qemu.org/Documentation/9psetup#msize for details."
1429 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1434 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1436 pdu_complete(pdu
, err
);
1437 v9fs_string_free(&version
);
1440 static void coroutine_fn
v9fs_attach(void *opaque
)
1442 V9fsPDU
*pdu
= opaque
;
1443 V9fsState
*s
= pdu
->s
;
1444 int32_t fid
, afid
, n_uname
;
1445 V9fsString uname
, aname
;
1452 v9fs_string_init(&uname
);
1453 v9fs_string_init(&aname
);
1454 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1455 &afid
, &uname
, &aname
, &n_uname
);
1459 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1461 fidp
= alloc_fid(s
, fid
);
1466 fidp
->uid
= n_uname
;
1467 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1473 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1479 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1487 * disable migration if we haven't done already.
1488 * attach could get called multiple times for the same export.
1490 if (!s
->migration_blocker
) {
1491 error_setg(&s
->migration_blocker
,
1492 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1493 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1494 err
= migrate_add_blocker(s
->migration_blocker
, NULL
);
1496 error_free(s
->migration_blocker
);
1497 s
->migration_blocker
= NULL
;
1504 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1511 memcpy(&s
->root_st
, &stbuf
, sizeof(stbuf
));
1512 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1513 qid
.type
, qid
.version
, qid
.path
);
1517 pdu_complete(pdu
, err
);
1518 v9fs_string_free(&uname
);
1519 v9fs_string_free(&aname
);
1522 static void coroutine_fn
v9fs_stat(void *opaque
)
1530 V9fsPDU
*pdu
= opaque
;
1533 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1537 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1539 fidp
= get_fid(pdu
, fid
);
1544 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1548 basename
= g_path_get_basename(fidp
->path
.data
);
1549 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1554 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1556 v9fs_stat_free(&v9stat
);
1559 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1560 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1562 v9fs_stat_free(&v9stat
);
1566 pdu_complete(pdu
, err
);
1569 static void coroutine_fn
v9fs_getattr(void *opaque
)
1576 uint64_t request_mask
;
1577 V9fsStatDotl v9stat_dotl
;
1578 V9fsPDU
*pdu
= opaque
;
1580 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1584 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1586 fidp
= get_fid(pdu
, fid
);
1592 * Currently we only support BASIC fields in stat, so there is no
1593 * need to look at request_mask.
1595 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1599 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1604 /* fill st_gen if requested and supported by underlying fs */
1605 if (request_mask
& P9_STATS_GEN
) {
1606 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1609 /* we have valid st_gen: update result mask */
1610 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1613 /* request cancelled, e.g. by Tflush */
1616 /* failed to get st_gen: not fatal, ignore */
1620 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1625 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1626 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1627 v9stat_dotl
.st_gid
);
1631 pdu_complete(pdu
, retval
);
1634 /* Attribute flags */
1635 #define P9_ATTR_MODE (1 << 0)
1636 #define P9_ATTR_UID (1 << 1)
1637 #define P9_ATTR_GID (1 << 2)
1638 #define P9_ATTR_SIZE (1 << 3)
1639 #define P9_ATTR_ATIME (1 << 4)
1640 #define P9_ATTR_MTIME (1 << 5)
1641 #define P9_ATTR_CTIME (1 << 6)
1642 #define P9_ATTR_ATIME_SET (1 << 7)
1643 #define P9_ATTR_MTIME_SET (1 << 8)
1645 #define P9_ATTR_MASK 127
1647 static void coroutine_fn
v9fs_setattr(void *opaque
)
1654 V9fsPDU
*pdu
= opaque
;
1656 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1661 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1662 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1663 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1665 fidp
= get_fid(pdu
, fid
);
1670 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1671 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1676 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1677 struct timespec times
[2];
1678 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1679 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1680 times
[0].tv_sec
= v9iattr
.atime_sec
;
1681 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1683 times
[0].tv_nsec
= UTIME_NOW
;
1686 times
[0].tv_nsec
= UTIME_OMIT
;
1688 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1689 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1690 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1691 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1693 times
[1].tv_nsec
= UTIME_NOW
;
1696 times
[1].tv_nsec
= UTIME_OMIT
;
1698 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1704 * If the only valid entry in iattr is ctime we can call
1705 * chown(-1,-1) to update the ctime of the file
1707 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1708 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1709 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1710 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1713 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1716 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1722 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1723 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1729 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1733 pdu_complete(pdu
, err
);
1736 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1742 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1747 for (i
= 0; i
< nwnames
; i
++) {
1748 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1757 static bool name_is_illegal(const char *name
)
1759 return !*name
|| strchr(name
, '/') != NULL
;
1762 static bool same_stat_id(const struct stat
*a
, const struct stat
*b
)
1764 return a
->st_dev
== b
->st_dev
&& a
->st_ino
== b
->st_ino
;
1767 static void coroutine_fn
v9fs_walk(void *opaque
)
1769 int name_idx
, nwalked
;
1770 g_autofree V9fsQID
*qids
= NULL
;
1771 int i
, err
= 0, any_err
= 0;
1772 V9fsPath dpath
, path
;
1773 P9ARRAY_REF(V9fsPath
) pathes
= NULL
;
1775 struct stat stbuf
, fidst
;
1776 g_autofree
struct stat
*stbufs
= NULL
;
1778 int32_t fid
, newfid
;
1779 P9ARRAY_REF(V9fsString
) wnames
= NULL
;
1781 V9fsFidState
*newfidp
= NULL
;
1782 V9fsPDU
*pdu
= opaque
;
1783 V9fsState
*s
= pdu
->s
;
1786 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1788 pdu_complete(pdu
, err
);
1793 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1795 if (nwnames
> P9_MAXWELEM
) {
1800 P9ARRAY_NEW(V9fsString
, wnames
, nwnames
);
1801 qids
= g_new0(V9fsQID
, nwnames
);
1802 stbufs
= g_new0(struct stat
, nwnames
);
1803 P9ARRAY_NEW(V9fsPath
, pathes
, nwnames
);
1804 for (i
= 0; i
< nwnames
; i
++) {
1805 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1809 if (name_is_illegal(wnames
[i
].data
)) {
1816 fidp
= get_fid(pdu
, fid
);
1822 v9fs_path_init(&dpath
);
1823 v9fs_path_init(&path
);
1825 * Both dpath and path initially point to fidp.
1826 * Needed to handle request with nwnames == 0
1828 v9fs_path_copy(&dpath
, &fidp
->path
);
1829 v9fs_path_copy(&path
, &fidp
->path
);
1832 * To keep latency (i.e. overall execution time for processing this
1833 * Twalk client request) as small as possible, run all the required fs
1834 * driver code altogether inside the following block.
1836 v9fs_co_run_in_worker({
1838 if (v9fs_request_cancelled(pdu
)) {
1839 any_err
|= err
= -EINTR
;
1842 err
= s
->ops
->lstat(&s
->ctx
, &dpath
, &fidst
);
1844 any_err
|= err
= -errno
;
1848 for (; nwalked
< nwnames
; nwalked
++) {
1849 if (v9fs_request_cancelled(pdu
)) {
1850 any_err
|= err
= -EINTR
;
1853 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1854 strcmp("..", wnames
[nwalked
].data
))
1856 err
= s
->ops
->name_to_path(&s
->ctx
, &dpath
,
1857 wnames
[nwalked
].data
,
1860 any_err
|= err
= -errno
;
1863 if (v9fs_request_cancelled(pdu
)) {
1864 any_err
|= err
= -EINTR
;
1867 err
= s
->ops
->lstat(&s
->ctx
, &pathes
[nwalked
], &stbuf
);
1869 any_err
|= err
= -errno
;
1872 stbufs
[nwalked
] = stbuf
;
1873 v9fs_path_copy(&dpath
, &pathes
[nwalked
]);
1878 * Handle all the rest of this Twalk request on main thread ...
1880 * NOTE: -EINTR is an exception where we deviate from the protocol spec
1881 * and simply send a (R)Lerror response instead of bothering to assemble
1882 * a (deducted) Rwalk response; because -EINTR is always the result of a
1883 * Tflush request, so client would no longer wait for a response in this
1886 if ((err
< 0 && !nwalked
) || err
== -EINTR
) {
1890 any_err
|= err
= stat_to_qid(pdu
, &fidst
, &qid
);
1891 if (err
< 0 && !nwalked
) {
1896 /* reset dpath and path */
1897 v9fs_path_copy(&dpath
, &fidp
->path
);
1898 v9fs_path_copy(&path
, &fidp
->path
);
1900 for (name_idx
= 0; name_idx
< nwalked
; name_idx
++) {
1901 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1902 strcmp("..", wnames
[name_idx
].data
))
1904 stbuf
= stbufs
[name_idx
];
1905 any_err
|= err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1909 v9fs_path_copy(&path
, &pathes
[name_idx
]);
1910 v9fs_path_copy(&dpath
, &path
);
1912 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1916 /* don't send any QIDs, send Rlerror instead */
1919 /* send QIDs (not Rlerror), but fid MUST remain unaffected */
1923 if (fid
== newfid
) {
1924 if (fidp
->fid_type
!= P9_FID_NONE
) {
1928 v9fs_path_write_lock(s
);
1929 v9fs_path_copy(&fidp
->path
, &path
);
1930 v9fs_path_unlock(s
);
1932 newfidp
= alloc_fid(s
, newfid
);
1933 if (newfidp
== NULL
) {
1937 newfidp
->uid
= fidp
->uid
;
1938 v9fs_path_copy(&newfidp
->path
, &path
);
1941 err
= v9fs_walk_marshal(pdu
, name_idx
, qids
);
1942 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, name_idx
, qids
);
1946 put_fid(pdu
, newfidp
);
1948 v9fs_path_free(&dpath
);
1949 v9fs_path_free(&path
);
1951 pdu_complete(pdu
, err
);
1954 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1956 struct statfs stbuf
;
1957 int err
= v9fs_co_statfs(pdu
, path
, &stbuf
);
1959 return blksize_to_iounit(pdu
, (err
>= 0) ? stbuf
.f_bsize
: 0);
1962 static void coroutine_fn
v9fs_open(void *opaque
)
1973 V9fsPDU
*pdu
= opaque
;
1974 V9fsState
*s
= pdu
->s
;
1976 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1977 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1980 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1986 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1988 fidp
= get_fid(pdu
, fid
);
1993 if (fidp
->fid_type
!= P9_FID_NONE
) {
1998 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2002 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2006 if (S_ISDIR(stbuf
.st_mode
)) {
2007 err
= v9fs_co_opendir(pdu
, fidp
);
2011 fidp
->fid_type
= P9_FID_DIR
;
2012 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
2018 if (s
->proto_version
== V9FS_PROTO_2000L
) {
2019 flags
= get_dotl_openflags(s
, mode
);
2021 flags
= omode_to_uflags(mode
);
2023 if (is_ro_export(&s
->ctx
)) {
2024 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
2025 mode
& O_APPEND
|| mode
& O_TRUNC
) {
2030 err
= v9fs_co_open(pdu
, fidp
, flags
);
2034 fidp
->fid_type
= P9_FID_FILE
;
2035 fidp
->open_flags
= flags
;
2036 if (flags
& O_EXCL
) {
2038 * We let the host file system do O_EXCL check
2039 * We should not reclaim such fd
2041 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2043 iounit
= get_iounit(pdu
, &fidp
->path
);
2044 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2050 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
2051 qid
.type
, qid
.version
, qid
.path
, iounit
);
2055 pdu_complete(pdu
, err
);
2058 static void coroutine_fn
v9fs_lcreate(void *opaque
)
2060 int32_t dfid
, flags
, mode
;
2069 V9fsPDU
*pdu
= opaque
;
2071 v9fs_string_init(&name
);
2072 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
2073 &name
, &flags
, &mode
, &gid
);
2077 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
2079 if (name_is_illegal(name
.data
)) {
2084 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2089 fidp
= get_fid(pdu
, dfid
);
2094 if (fidp
->fid_type
!= P9_FID_NONE
) {
2099 flags
= get_dotl_openflags(pdu
->s
, flags
);
2100 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
2101 flags
| O_CREAT
, mode
, &stbuf
);
2105 fidp
->fid_type
= P9_FID_FILE
;
2106 fidp
->open_flags
= flags
;
2107 if (flags
& O_EXCL
) {
2109 * We let the host file system do O_EXCL check
2110 * We should not reclaim such fd
2112 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2114 iounit
= get_iounit(pdu
, &fidp
->path
);
2115 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2119 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2124 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2125 qid
.type
, qid
.version
, qid
.path
, iounit
);
2129 pdu_complete(pdu
, err
);
2130 v9fs_string_free(&name
);
2133 static void coroutine_fn
v9fs_fsync(void *opaque
)
2140 V9fsPDU
*pdu
= opaque
;
2142 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2146 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2148 fidp
= get_fid(pdu
, fid
);
2153 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2159 pdu_complete(pdu
, err
);
2162 static void coroutine_fn
v9fs_clunk(void *opaque
)
2168 V9fsPDU
*pdu
= opaque
;
2169 V9fsState
*s
= pdu
->s
;
2171 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2175 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2177 fidp
= clunk_fid(s
, fid
);
2183 * Bump the ref so that put_fid will
2187 err
= put_fid(pdu
, fidp
);
2192 pdu_complete(pdu
, err
);
2196 * Create a QEMUIOVector for a sub-region of PDU iovecs
2198 * @qiov: uninitialized QEMUIOVector
2199 * @skip: number of bytes to skip from beginning of PDU
2200 * @size: number of bytes to include
2201 * @is_write: true - write, false - read
2203 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2204 * with qemu_iovec_destroy().
2206 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2207 size_t skip
, size_t size
,
2215 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2217 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2220 qemu_iovec_init_external(&elem
, iov
, niov
);
2221 qemu_iovec_init(qiov
, niov
);
2222 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
2225 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2226 uint64_t off
, uint32_t max_count
)
2230 uint64_t read_count
;
2231 QEMUIOVector qiov_full
;
2233 if (fidp
->fs
.xattr
.len
< off
) {
2236 read_count
= fidp
->fs
.xattr
.len
- off
;
2238 if (read_count
> max_count
) {
2239 read_count
= max_count
;
2241 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2247 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
2248 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2249 ((char *)fidp
->fs
.xattr
.value
) + off
,
2251 qemu_iovec_destroy(&qiov_full
);
2259 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2268 off_t saved_dir_pos
;
2269 struct dirent
*dent
;
2271 /* save the directory position */
2272 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2273 if (saved_dir_pos
< 0) {
2274 return saved_dir_pos
;
2278 v9fs_path_init(&path
);
2280 v9fs_readdir_lock(&fidp
->fs
.dir
);
2282 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2286 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2290 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2294 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2298 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2299 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2301 /* Ran out of buffer. Set dir back to old position and return */
2302 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2303 v9fs_stat_free(&v9stat
);
2304 v9fs_path_free(&path
);
2308 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2309 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2311 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2314 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2315 v9fs_stat_free(&v9stat
);
2316 v9fs_path_free(&path
);
2320 v9fs_stat_free(&v9stat
);
2321 v9fs_path_free(&path
);
2322 saved_dir_pos
= qemu_dirent_off(dent
);
2325 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2327 v9fs_path_free(&path
);
2334 static void coroutine_fn
v9fs_read(void *opaque
)
2343 V9fsPDU
*pdu
= opaque
;
2344 V9fsState
*s
= pdu
->s
;
2346 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2350 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2352 fidp
= get_fid(pdu
, fid
);
2357 if (fidp
->fid_type
== P9_FID_DIR
) {
2358 if (s
->proto_version
!= V9FS_PROTO_2000U
) {
2360 "9p: bad client: T_read request on directory only expected "
2361 "with 9P2000.u protocol version"
2367 v9fs_co_rewinddir(pdu
, fidp
);
2369 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2374 err
= pdu_marshal(pdu
, offset
, "d", count
);
2378 err
+= offset
+ count
;
2379 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2380 QEMUIOVector qiov_full
;
2384 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2385 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2387 qemu_iovec_reset(&qiov
);
2388 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2390 print_sg(qiov
.iov
, qiov
.niov
);
2392 /* Loop in case of EINTR */
2394 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2399 } while (len
== -EINTR
&& !pdu
->cancelled
);
2401 /* IO error return the error */
2403 goto out_free_iovec
;
2405 } while (count
< max_count
&& len
> 0);
2406 err
= pdu_marshal(pdu
, offset
, "d", count
);
2408 goto out_free_iovec
;
2410 err
+= offset
+ count
;
2412 qemu_iovec_destroy(&qiov
);
2413 qemu_iovec_destroy(&qiov_full
);
2414 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2415 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2419 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2423 pdu_complete(pdu
, err
);
2427 * v9fs_readdir_response_size() - Returns size required in Rreaddir response
2428 * for the passed dirent @name.
2430 * @name: directory entry's name (i.e. file name, directory name)
2431 * Return: required size in bytes
2433 size_t v9fs_readdir_response_size(V9fsString
*name
)
2436 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2437 * size of type (1) + size of name.size (2) + strlen(name.data)
2439 return 24 + v9fs_string_size(name
);
2442 static void v9fs_free_dirents(struct V9fsDirEnt
*e
)
2444 struct V9fsDirEnt
*next
= NULL
;
2446 for (; e
; e
= next
) {
2454 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2455 off_t offset
, int32_t max_count
)
2463 struct dirent
*dent
;
2465 struct V9fsDirEnt
*entries
= NULL
;
2468 * inode remapping requires the device id, which in turn might be
2469 * different for different directory entries, so if inode remapping is
2470 * enabled we have to make a full stat for each directory entry
2472 const bool dostat
= pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
;
2475 * Fetch all required directory entries altogether on a background IO
2476 * thread from fs driver. We don't want to do that for each entry
2477 * individually, because hopping between threads (this main IO thread
2478 * and background IO driver thread) would sum up to huge latencies.
2480 count
= v9fs_co_readdir_many(pdu
, fidp
, &entries
, offset
, max_count
,
2489 for (struct V9fsDirEnt
*e
= entries
; e
; e
= e
->next
) {
2492 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2494 /* e->st should never be NULL, but just to be sure */
2501 err
= stat_to_qid(pdu
, st
, &qid
);
2507 * Fill up just the path field of qid because the client uses
2508 * only that. To fill the entire qid structure we will have
2509 * to stat each dirent found, which is expensive. For the
2510 * latter reason we don't call stat_to_qid() here. Only drawback
2511 * is that no multi-device export detection of stat_to_qid()
2512 * would be done and provided as error to the user here. But
2513 * user would get that error anyway when accessing those
2514 * files/dirs through other ways.
2516 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2517 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2518 /* Fill the other fields with dummy values */
2523 off
= qemu_dirent_off(dent
);
2524 v9fs_string_init(&name
);
2525 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2527 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2528 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2530 dent
->d_type
, &name
);
2532 v9fs_string_free(&name
);
2543 v9fs_free_dirents(entries
);
2550 static void coroutine_fn
v9fs_readdir(void *opaque
)
2556 uint64_t initial_offset
;
2559 V9fsPDU
*pdu
= opaque
;
2560 V9fsState
*s
= pdu
->s
;
2562 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2563 &initial_offset
, &max_count
);
2567 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2569 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2570 if (max_count
> s
->msize
- 11) {
2571 max_count
= s
->msize
- 11;
2573 "9p: bad client: T_readdir with count > msize - 11"
2577 fidp
= get_fid(pdu
, fid
);
2582 if (!fidp
->fs
.dir
.stream
) {
2586 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
2588 "9p: bad client: T_readdir request only expected with 9P2000.L "
2591 retval
= -EOPNOTSUPP
;
2594 count
= v9fs_do_readdir(pdu
, fidp
, (off_t
) initial_offset
, max_count
);
2599 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2603 retval
+= count
+ offset
;
2604 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2608 pdu_complete(pdu
, retval
);
2611 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2612 uint64_t off
, uint32_t count
,
2613 struct iovec
*sg
, int cnt
)
2617 uint64_t write_count
;
2621 if (fidp
->fs
.xattr
.len
< off
) {
2624 write_count
= fidp
->fs
.xattr
.len
- off
;
2625 if (write_count
> count
) {
2626 write_count
= count
;
2628 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2633 fidp
->fs
.xattr
.copied_len
+= write_count
;
2635 * Now copy the content from sg list
2637 for (i
= 0; i
< cnt
; i
++) {
2638 if (write_count
> sg
[i
].iov_len
) {
2639 to_copy
= sg
[i
].iov_len
;
2641 to_copy
= write_count
;
2643 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2644 /* updating vs->off since we are not using below */
2646 write_count
-= to_copy
;
2652 static void coroutine_fn
v9fs_write(void *opaque
)
2662 V9fsPDU
*pdu
= opaque
;
2663 V9fsState
*s
= pdu
->s
;
2664 QEMUIOVector qiov_full
;
2667 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2669 pdu_complete(pdu
, err
);
2673 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2674 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2676 fidp
= get_fid(pdu
, fid
);
2681 if (fidp
->fid_type
== P9_FID_FILE
) {
2682 if (fidp
->fs
.fd
== -1) {
2686 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2688 * setxattr operation
2690 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2691 qiov_full
.iov
, qiov_full
.niov
);
2697 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2699 qemu_iovec_reset(&qiov
);
2700 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2702 print_sg(qiov
.iov
, qiov
.niov
);
2704 /* Loop in case of EINTR */
2706 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2711 } while (len
== -EINTR
&& !pdu
->cancelled
);
2713 /* IO error return the error */
2717 } while (total
< count
&& len
> 0);
2720 err
= pdu_marshal(pdu
, offset
, "d", total
);
2725 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2727 qemu_iovec_destroy(&qiov
);
2731 qemu_iovec_destroy(&qiov_full
);
2732 pdu_complete(pdu
, err
);
2735 static void coroutine_fn
v9fs_create(void *opaque
)
2747 V9fsString extension
;
2749 V9fsPDU
*pdu
= opaque
;
2750 V9fsState
*s
= pdu
->s
;
2752 v9fs_path_init(&path
);
2753 v9fs_string_init(&name
);
2754 v9fs_string_init(&extension
);
2755 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2756 &perm
, &mode
, &extension
);
2760 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2762 if (name_is_illegal(name
.data
)) {
2767 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2772 fidp
= get_fid(pdu
, fid
);
2777 if (fidp
->fid_type
!= P9_FID_NONE
) {
2781 if (perm
& P9_STAT_MODE_DIR
) {
2782 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2783 fidp
->uid
, -1, &stbuf
);
2787 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2791 v9fs_path_write_lock(s
);
2792 v9fs_path_copy(&fidp
->path
, &path
);
2793 v9fs_path_unlock(s
);
2794 err
= v9fs_co_opendir(pdu
, fidp
);
2798 fidp
->fid_type
= P9_FID_DIR
;
2799 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2800 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2801 extension
.data
, -1 , &stbuf
);
2805 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2809 v9fs_path_write_lock(s
);
2810 v9fs_path_copy(&fidp
->path
, &path
);
2811 v9fs_path_unlock(s
);
2812 } else if (perm
& P9_STAT_MODE_LINK
) {
2813 int32_t ofid
= atoi(extension
.data
);
2814 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2815 if (ofidp
== NULL
) {
2819 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2820 put_fid(pdu
, ofidp
);
2824 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2826 fidp
->fid_type
= P9_FID_NONE
;
2829 v9fs_path_write_lock(s
);
2830 v9fs_path_copy(&fidp
->path
, &path
);
2831 v9fs_path_unlock(s
);
2832 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2834 fidp
->fid_type
= P9_FID_NONE
;
2837 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2839 uint32_t major
, minor
;
2842 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2859 nmode
|= perm
& 0777;
2860 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2861 makedev(major
, minor
), nmode
, &stbuf
);
2865 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2869 v9fs_path_write_lock(s
);
2870 v9fs_path_copy(&fidp
->path
, &path
);
2871 v9fs_path_unlock(s
);
2872 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2873 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2874 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2878 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2882 v9fs_path_write_lock(s
);
2883 v9fs_path_copy(&fidp
->path
, &path
);
2884 v9fs_path_unlock(s
);
2885 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2886 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2887 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2891 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2895 v9fs_path_write_lock(s
);
2896 v9fs_path_copy(&fidp
->path
, &path
);
2897 v9fs_path_unlock(s
);
2899 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2900 omode_to_uflags(mode
) | O_CREAT
, perm
, &stbuf
);
2904 fidp
->fid_type
= P9_FID_FILE
;
2905 fidp
->open_flags
= omode_to_uflags(mode
);
2906 if (fidp
->open_flags
& O_EXCL
) {
2908 * We let the host file system do O_EXCL check
2909 * We should not reclaim such fd
2911 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2914 iounit
= get_iounit(pdu
, &fidp
->path
);
2915 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2919 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2924 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2925 qid
.type
, qid
.version
, qid
.path
, iounit
);
2929 pdu_complete(pdu
, err
);
2930 v9fs_string_free(&name
);
2931 v9fs_string_free(&extension
);
2932 v9fs_path_free(&path
);
2935 static void coroutine_fn
v9fs_symlink(void *opaque
)
2937 V9fsPDU
*pdu
= opaque
;
2940 V9fsFidState
*dfidp
;
2948 v9fs_string_init(&name
);
2949 v9fs_string_init(&symname
);
2950 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2954 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2956 if (name_is_illegal(name
.data
)) {
2961 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2966 dfidp
= get_fid(pdu
, dfid
);
2967 if (dfidp
== NULL
) {
2971 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2975 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2979 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2984 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2985 qid
.type
, qid
.version
, qid
.path
);
2987 put_fid(pdu
, dfidp
);
2989 pdu_complete(pdu
, err
);
2990 v9fs_string_free(&name
);
2991 v9fs_string_free(&symname
);
2994 static void coroutine_fn
v9fs_flush(void *opaque
)
2999 V9fsPDU
*cancel_pdu
= NULL
;
3000 V9fsPDU
*pdu
= opaque
;
3001 V9fsState
*s
= pdu
->s
;
3003 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
3005 pdu_complete(pdu
, err
);
3008 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
3010 if (pdu
->tag
== tag
) {
3011 warn_report("the guest sent a self-referencing 9P flush request");
3013 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
3014 if (cancel_pdu
->tag
== tag
) {
3020 cancel_pdu
->cancelled
= 1;
3022 * Wait for pdu to complete.
3024 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
3025 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
3026 cancel_pdu
->cancelled
= 0;
3027 pdu_free(cancel_pdu
);
3030 pdu_complete(pdu
, 7);
3033 static void coroutine_fn
v9fs_link(void *opaque
)
3035 V9fsPDU
*pdu
= opaque
;
3036 int32_t dfid
, oldfid
;
3037 V9fsFidState
*dfidp
, *oldfidp
;
3042 v9fs_string_init(&name
);
3043 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
3047 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
3049 if (name_is_illegal(name
.data
)) {
3054 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3059 dfidp
= get_fid(pdu
, dfid
);
3060 if (dfidp
== NULL
) {
3065 oldfidp
= get_fid(pdu
, oldfid
);
3066 if (oldfidp
== NULL
) {
3070 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
3074 put_fid(pdu
, oldfidp
);
3076 put_fid(pdu
, dfidp
);
3078 v9fs_string_free(&name
);
3079 pdu_complete(pdu
, err
);
3082 /* Only works with path name based fid */
3083 static void coroutine_fn
v9fs_remove(void *opaque
)
3089 V9fsPDU
*pdu
= opaque
;
3091 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3095 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
3097 fidp
= get_fid(pdu
, fid
);
3102 /* if fs driver is not path based, return EOPNOTSUPP */
3103 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3108 * IF the file is unlinked, we cannot reopen
3109 * the file later. So don't reclaim fd
3111 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
3115 err
= v9fs_co_remove(pdu
, &fidp
->path
);
3120 /* For TREMOVE we need to clunk the fid even on failed remove */
3121 clunk_fid(pdu
->s
, fidp
->fid
);
3124 pdu_complete(pdu
, err
);
3127 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
3131 int32_t dfid
, flags
, rflags
= 0;
3134 V9fsFidState
*dfidp
;
3135 V9fsPDU
*pdu
= opaque
;
3137 v9fs_string_init(&name
);
3138 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3143 if (name_is_illegal(name
.data
)) {
3148 if (!strcmp(".", name
.data
)) {
3153 if (!strcmp("..", name
.data
)) {
3158 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3163 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3164 rflags
|= AT_REMOVEDIR
;
3167 dfidp
= get_fid(pdu
, dfid
);
3168 if (dfidp
== NULL
) {
3173 * IF the file is unlinked, we cannot reopen
3174 * the file later. So don't reclaim fd
3176 v9fs_path_init(&path
);
3177 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3181 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3185 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3190 put_fid(pdu
, dfidp
);
3191 v9fs_path_free(&path
);
3193 pdu_complete(pdu
, err
);
3194 v9fs_string_free(&name
);
3198 /* Only works with path name based fid */
3199 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3205 V9fsFidState
*tfidp
;
3206 V9fsState
*s
= pdu
->s
;
3207 V9fsFidState
*dirfidp
= NULL
;
3209 v9fs_path_init(&new_path
);
3210 if (newdirfid
!= -1) {
3211 dirfidp
= get_fid(pdu
, newdirfid
);
3212 if (dirfidp
== NULL
) {
3215 if (fidp
->fid_type
!= P9_FID_NONE
) {
3219 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3224 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3227 v9fs_path_init(&dir_path
);
3228 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3231 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3232 v9fs_path_free(&dir_path
);
3237 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3242 * Fixup fid's pointing to the old name to
3243 * start pointing to the new name
3245 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3246 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3247 /* replace the name */
3248 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3253 put_fid(pdu
, dirfidp
);
3255 v9fs_path_free(&new_path
);
3259 /* Only works with path name based fid */
3260 static void coroutine_fn
v9fs_rename(void *opaque
)
3268 V9fsPDU
*pdu
= opaque
;
3269 V9fsState
*s
= pdu
->s
;
3271 v9fs_string_init(&name
);
3272 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3277 if (name_is_illegal(name
.data
)) {
3282 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3287 fidp
= get_fid(pdu
, fid
);
3292 if (fidp
->fid_type
!= P9_FID_NONE
) {
3296 /* if fs driver is not path based, return EOPNOTSUPP */
3297 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3301 v9fs_path_write_lock(s
);
3302 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3303 v9fs_path_unlock(s
);
3310 pdu_complete(pdu
, err
);
3311 v9fs_string_free(&name
);
3314 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3315 V9fsString
*old_name
,
3317 V9fsString
*new_name
)
3319 V9fsFidState
*tfidp
;
3320 V9fsPath oldpath
, newpath
;
3321 V9fsState
*s
= pdu
->s
;
3324 v9fs_path_init(&oldpath
);
3325 v9fs_path_init(&newpath
);
3326 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3330 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3336 * Fixup fid's pointing to the old name to
3337 * start pointing to the new name
3339 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3340 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3341 /* replace the name */
3342 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3346 v9fs_path_free(&oldpath
);
3347 v9fs_path_free(&newpath
);
3351 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3352 V9fsString
*old_name
,
3354 V9fsString
*new_name
)
3357 V9fsState
*s
= pdu
->s
;
3358 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3360 olddirfidp
= get_fid(pdu
, olddirfid
);
3361 if (olddirfidp
== NULL
) {
3365 if (newdirfid
!= -1) {
3366 newdirfidp
= get_fid(pdu
, newdirfid
);
3367 if (newdirfidp
== NULL
) {
3372 newdirfidp
= get_fid(pdu
, olddirfid
);
3375 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3376 &newdirfidp
->path
, new_name
);
3380 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3381 /* Only for path based fid we need to do the below fixup */
3382 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3383 &newdirfidp
->path
, new_name
);
3387 put_fid(pdu
, olddirfidp
);
3390 put_fid(pdu
, newdirfidp
);
3395 static void coroutine_fn
v9fs_renameat(void *opaque
)
3399 V9fsPDU
*pdu
= opaque
;
3400 V9fsState
*s
= pdu
->s
;
3401 int32_t olddirfid
, newdirfid
;
3402 V9fsString old_name
, new_name
;
3404 v9fs_string_init(&old_name
);
3405 v9fs_string_init(&new_name
);
3406 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3407 &old_name
, &newdirfid
, &new_name
);
3412 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3417 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3418 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3423 v9fs_path_write_lock(s
);
3424 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3425 &old_name
, newdirfid
, &new_name
);
3426 v9fs_path_unlock(s
);
3432 pdu_complete(pdu
, err
);
3433 v9fs_string_free(&old_name
);
3434 v9fs_string_free(&new_name
);
3437 static void coroutine_fn
v9fs_wstat(void *opaque
)
3446 V9fsPDU
*pdu
= opaque
;
3447 V9fsState
*s
= pdu
->s
;
3449 v9fs_stat_init(&v9stat
);
3450 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3454 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3455 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3457 fidp
= get_fid(pdu
, fid
);
3462 /* do we need to sync the file? */
3463 if (donttouch_stat(&v9stat
)) {
3464 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3467 if (v9stat
.mode
!= -1) {
3469 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3473 v9_mode
= stat_to_v9mode(&stbuf
);
3474 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3475 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3476 /* Attempting to change the type */
3480 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3481 v9mode_to_mode(v9stat
.mode
,
3482 &v9stat
.extension
));
3487 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3488 struct timespec times
[2];
3489 if (v9stat
.atime
!= -1) {
3490 times
[0].tv_sec
= v9stat
.atime
;
3491 times
[0].tv_nsec
= 0;
3493 times
[0].tv_nsec
= UTIME_OMIT
;
3495 if (v9stat
.mtime
!= -1) {
3496 times
[1].tv_sec
= v9stat
.mtime
;
3497 times
[1].tv_nsec
= 0;
3499 times
[1].tv_nsec
= UTIME_OMIT
;
3501 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3506 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3507 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3512 if (v9stat
.name
.size
!= 0) {
3513 v9fs_path_write_lock(s
);
3514 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3515 v9fs_path_unlock(s
);
3520 if (v9stat
.length
!= -1) {
3521 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3530 v9fs_stat_free(&v9stat
);
3531 pdu_complete(pdu
, err
);
3534 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3546 int32_t bsize_factor
;
3549 * compute bsize factor based on host file system block size
3552 bsize_factor
= (s
->msize
- P9_IOHDRSZ
) / stbuf
->f_bsize
;
3553 if (!bsize_factor
) {
3556 f_type
= stbuf
->f_type
;
3557 f_bsize
= stbuf
->f_bsize
;
3558 f_bsize
*= bsize_factor
;
3560 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3561 * adjust(divide) the number of blocks, free blocks and available
3562 * blocks by bsize factor
3564 f_blocks
= stbuf
->f_blocks
/ bsize_factor
;
3565 f_bfree
= stbuf
->f_bfree
/ bsize_factor
;
3566 f_bavail
= stbuf
->f_bavail
/ bsize_factor
;
3567 f_files
= stbuf
->f_files
;
3568 f_ffree
= stbuf
->f_ffree
;
3569 #ifdef CONFIG_DARWIN
3570 fsid_val
= (unsigned int)stbuf
->f_fsid
.val
[0] |
3571 (unsigned long long)stbuf
->f_fsid
.val
[1] << 32;
3572 f_namelen
= NAME_MAX
;
3574 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3575 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3576 f_namelen
= stbuf
->f_namelen
;
3579 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3580 f_type
, f_bsize
, f_blocks
, f_bfree
,
3581 f_bavail
, f_files
, f_ffree
,
3582 fsid_val
, f_namelen
);
3585 static void coroutine_fn
v9fs_statfs(void *opaque
)
3591 struct statfs stbuf
;
3592 V9fsPDU
*pdu
= opaque
;
3593 V9fsState
*s
= pdu
->s
;
3595 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3599 fidp
= get_fid(pdu
, fid
);
3604 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3608 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3616 pdu_complete(pdu
, retval
);
3619 static void coroutine_fn
v9fs_mknod(void *opaque
)
3632 V9fsPDU
*pdu
= opaque
;
3634 v9fs_string_init(&name
);
3635 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3636 &major
, &minor
, &gid
);
3640 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3642 if (name_is_illegal(name
.data
)) {
3647 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3652 fidp
= get_fid(pdu
, fid
);
3657 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3658 makedev(major
, minor
), mode
, &stbuf
);
3662 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3666 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3671 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3672 qid
.type
, qid
.version
, qid
.path
);
3676 pdu_complete(pdu
, err
);
3677 v9fs_string_free(&name
);
3681 * Implement posix byte range locking code
3682 * Server side handling of locking code is very simple, because 9p server in
3683 * QEMU can handle only one client. And most of the lock handling
3684 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3685 * do any thing in * qemu 9p server side lock code path.
3686 * So when a TLOCK request comes, always return success
3688 static void coroutine_fn
v9fs_lock(void *opaque
)
3694 int32_t fid
, err
= 0;
3695 V9fsPDU
*pdu
= opaque
;
3697 v9fs_string_init(&flock
.client_id
);
3698 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3699 &flock
.flags
, &flock
.start
, &flock
.length
,
3700 &flock
.proc_id
, &flock
.client_id
);
3704 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3705 flock
.type
, flock
.start
, flock
.length
);
3708 /* We support only block flag now (that too ignored currently) */
3709 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3713 fidp
= get_fid(pdu
, fid
);
3718 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3722 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3727 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3731 pdu_complete(pdu
, err
);
3732 v9fs_string_free(&flock
.client_id
);
3736 * When a TGETLOCK request comes, always return success because all lock
3737 * handling is done by client's VFS layer.
3739 static void coroutine_fn
v9fs_getlock(void *opaque
)
3745 int32_t fid
, err
= 0;
3746 V9fsPDU
*pdu
= opaque
;
3748 v9fs_string_init(&glock
.client_id
);
3749 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3750 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3755 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3756 glock
.type
, glock
.start
, glock
.length
);
3758 fidp
= get_fid(pdu
, fid
);
3763 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3767 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3768 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3769 glock
.start
, glock
.length
, glock
.proc_id
,
3775 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3776 glock
.length
, glock
.proc_id
);
3780 pdu_complete(pdu
, err
);
3781 v9fs_string_free(&glock
.client_id
);
3784 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3786 V9fsPDU
*pdu
= opaque
;
3797 v9fs_string_init(&name
);
3798 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3802 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3804 if (name_is_illegal(name
.data
)) {
3809 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3814 fidp
= get_fid(pdu
, fid
);
3819 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3823 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3827 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3832 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3833 qid
.type
, qid
.version
, qid
.path
, err
);
3837 pdu_complete(pdu
, err
);
3838 v9fs_string_free(&name
);
3841 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3847 int32_t fid
, newfid
;
3848 V9fsFidState
*file_fidp
;
3849 V9fsFidState
*xattr_fidp
= NULL
;
3850 V9fsPDU
*pdu
= opaque
;
3851 V9fsState
*s
= pdu
->s
;
3853 v9fs_string_init(&name
);
3854 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3858 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3860 file_fidp
= get_fid(pdu
, fid
);
3861 if (file_fidp
== NULL
) {
3865 xattr_fidp
= alloc_fid(s
, newfid
);
3866 if (xattr_fidp
== NULL
) {
3870 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3871 if (!v9fs_string_size(&name
)) {
3873 * listxattr request. Get the size first
3875 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3878 clunk_fid(s
, xattr_fidp
->fid
);
3882 * Read the xattr value
3884 xattr_fidp
->fs
.xattr
.len
= size
;
3885 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3886 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3887 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3889 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3890 xattr_fidp
->fs
.xattr
.value
,
3891 xattr_fidp
->fs
.xattr
.len
);
3893 clunk_fid(s
, xattr_fidp
->fid
);
3897 err
= pdu_marshal(pdu
, offset
, "q", size
);
3904 * specific xattr fid. We check for xattr
3905 * presence also collect the xattr size
3907 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3911 clunk_fid(s
, xattr_fidp
->fid
);
3915 * Read the xattr value
3917 xattr_fidp
->fs
.xattr
.len
= size
;
3918 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3919 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3920 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3922 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3923 &name
, xattr_fidp
->fs
.xattr
.value
,
3924 xattr_fidp
->fs
.xattr
.len
);
3926 clunk_fid(s
, xattr_fidp
->fid
);
3930 err
= pdu_marshal(pdu
, offset
, "q", size
);
3936 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3938 put_fid(pdu
, file_fidp
);
3940 put_fid(pdu
, xattr_fidp
);
3943 pdu_complete(pdu
, err
);
3944 v9fs_string_free(&name
);
3947 #if defined(CONFIG_LINUX)
3948 /* Currently, only Linux has XATTR_SIZE_MAX */
3949 #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX
3950 #elif defined(CONFIG_DARWIN)
3952 * Darwin doesn't seem to define a maximum xattr size in its user
3953 * space header, so manually configure it across platforms as 64k.
3955 * Having no limit at all can lead to QEMU crashing during large g_malloc()
3956 * calls. Because QEMU does not currently support macOS guests, the below
3957 * preliminary solution only works due to its being a reflection of the limit of
3960 #define P9_XATTR_SIZE_MAX 65536
3962 #error Missing definition for P9_XATTR_SIZE_MAX for this host system
3965 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3967 int flags
, rflags
= 0;
3973 V9fsFidState
*file_fidp
;
3974 V9fsFidState
*xattr_fidp
;
3975 V9fsPDU
*pdu
= opaque
;
3977 v9fs_string_init(&name
);
3978 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3982 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3984 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3989 if (flags
& P9_XATTR_CREATE
) {
3990 rflags
|= XATTR_CREATE
;
3993 if (flags
& P9_XATTR_REPLACE
) {
3994 rflags
|= XATTR_REPLACE
;
3997 if (size
> P9_XATTR_SIZE_MAX
) {
4002 file_fidp
= get_fid(pdu
, fid
);
4003 if (file_fidp
== NULL
) {
4007 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
4012 /* Make the file fid point to xattr */
4013 xattr_fidp
= file_fidp
;
4014 xattr_fidp
->fid_type
= P9_FID_XATTR
;
4015 xattr_fidp
->fs
.xattr
.copied_len
= 0;
4016 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
4017 xattr_fidp
->fs
.xattr
.len
= size
;
4018 xattr_fidp
->fs
.xattr
.flags
= rflags
;
4019 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
4020 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
4021 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
4024 put_fid(pdu
, file_fidp
);
4026 pdu_complete(pdu
, err
);
4027 v9fs_string_free(&name
);
4030 static void coroutine_fn
v9fs_readlink(void *opaque
)
4032 V9fsPDU
*pdu
= opaque
;
4039 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
4043 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
4044 fidp
= get_fid(pdu
, fid
);
4050 v9fs_string_init(&target
);
4051 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
4055 err
= pdu_marshal(pdu
, offset
, "s", &target
);
4057 v9fs_string_free(&target
);
4061 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
4062 v9fs_string_free(&target
);
4066 pdu_complete(pdu
, err
);
4069 static CoroutineEntry
*pdu_co_handlers
[] = {
4070 [P9_TREADDIR
] = v9fs_readdir
,
4071 [P9_TSTATFS
] = v9fs_statfs
,
4072 [P9_TGETATTR
] = v9fs_getattr
,
4073 [P9_TSETATTR
] = v9fs_setattr
,
4074 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
4075 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
4076 [P9_TMKNOD
] = v9fs_mknod
,
4077 [P9_TRENAME
] = v9fs_rename
,
4078 [P9_TLOCK
] = v9fs_lock
,
4079 [P9_TGETLOCK
] = v9fs_getlock
,
4080 [P9_TRENAMEAT
] = v9fs_renameat
,
4081 [P9_TREADLINK
] = v9fs_readlink
,
4082 [P9_TUNLINKAT
] = v9fs_unlinkat
,
4083 [P9_TMKDIR
] = v9fs_mkdir
,
4084 [P9_TVERSION
] = v9fs_version
,
4085 [P9_TLOPEN
] = v9fs_open
,
4086 [P9_TATTACH
] = v9fs_attach
,
4087 [P9_TSTAT
] = v9fs_stat
,
4088 [P9_TWALK
] = v9fs_walk
,
4089 [P9_TCLUNK
] = v9fs_clunk
,
4090 [P9_TFSYNC
] = v9fs_fsync
,
4091 [P9_TOPEN
] = v9fs_open
,
4092 [P9_TREAD
] = v9fs_read
,
4094 [P9_TAUTH
] = v9fs_auth
,
4096 [P9_TFLUSH
] = v9fs_flush
,
4097 [P9_TLINK
] = v9fs_link
,
4098 [P9_TSYMLINK
] = v9fs_symlink
,
4099 [P9_TCREATE
] = v9fs_create
,
4100 [P9_TLCREATE
] = v9fs_lcreate
,
4101 [P9_TWRITE
] = v9fs_write
,
4102 [P9_TWSTAT
] = v9fs_wstat
,
4103 [P9_TREMOVE
] = v9fs_remove
,
4106 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
4108 V9fsPDU
*pdu
= opaque
;
4109 pdu_complete(pdu
, -EOPNOTSUPP
);
4112 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
4114 V9fsPDU
*pdu
= opaque
;
4115 pdu_complete(pdu
, -EROFS
);
4118 static inline bool is_read_only_op(V9fsPDU
*pdu
)
4145 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
4148 CoroutineEntry
*handler
;
4149 V9fsState
*s
= pdu
->s
;
4151 pdu
->size
= le32_to_cpu(hdr
->size_le
);
4153 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
4155 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
4156 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
4157 handler
= v9fs_op_not_supp
;
4158 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4159 handler
= v9fs_fs_ro
;
4161 handler
= pdu_co_handlers
[pdu
->id
];
4164 qemu_co_queue_init(&pdu
->complete
);
4165 co
= qemu_coroutine_create(handler
, pdu
);
4166 qemu_coroutine_enter(co
);
4169 /* Returns 0 on success, 1 on failure. */
4170 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4180 assert(!s
->transport
);
4183 /* initialize pdu allocator */
4184 QLIST_INIT(&s
->free_list
);
4185 QLIST_INIT(&s
->active_list
);
4186 for (i
= 0; i
< MAX_REQ
; i
++) {
4187 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4192 v9fs_path_init(&path
);
4194 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4197 /* We don't have a fsdev identified by fsdev_id */
4198 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4200 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4204 if (!s
->fsconf
.tag
) {
4205 /* we haven't specified a mount_tag */
4206 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4207 s
->fsconf
.fsdev_id
);
4211 s
->ctx
.export_flags
= fse
->export_flags
;
4212 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4213 s
->ctx
.exops
.get_st_gen
= NULL
;
4214 len
= strlen(s
->fsconf
.tag
);
4215 if (len
> MAX_TAG_LEN
- 1) {
4216 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4217 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4221 s
->tag
= g_strdup(s
->fsconf
.tag
);
4226 s
->ctx
.fmode
= fse
->fmode
;
4227 s
->ctx
.dmode
= fse
->dmode
;
4229 QSIMPLEQ_INIT(&s
->fid_list
);
4230 qemu_co_rwlock_init(&s
->rename_lock
);
4232 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4233 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4234 s
->fsconf
.fsdev_id
);
4239 * Check details of export path, We need to use fs driver
4240 * call back to do that. Since we are in the init path, we don't
4241 * use co-routines here.
4243 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4245 "error in converting name to path %s", strerror(errno
));
4248 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4249 error_setg(errp
, "share path %s does not exist", fse
->path
);
4251 } else if (!S_ISDIR(stat
.st_mode
)) {
4252 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4256 s
->dev_id
= stat
.st_dev
;
4258 /* init inode remapping : */
4259 /* hash table for variable length inode suffixes */
4260 qpd_table_init(&s
->qpd_table
);
4261 /* hash table for slow/full inode remapping (most users won't need it) */
4262 qpf_table_init(&s
->qpf_table
);
4263 /* hash table for quick inode remapping */
4264 qpp_table_init(&s
->qpp_table
);
4266 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4267 s
->qp_fullpath_next
= 1;
4269 s
->ctx
.fst
= &fse
->fst
;
4270 fsdev_throttle_init(s
->ctx
.fst
);
4275 v9fs_device_unrealize_common(s
);
4277 v9fs_path_free(&path
);
4281 void v9fs_device_unrealize_common(V9fsState
*s
)
4283 if (s
->ops
&& s
->ops
->cleanup
) {
4284 s
->ops
->cleanup(&s
->ctx
);
4287 fsdev_throttle_cleanup(s
->ctx
.fst
);
4290 qp_table_destroy(&s
->qpd_table
);
4291 qp_table_destroy(&s
->qpp_table
);
4292 qp_table_destroy(&s
->qpf_table
);
4293 g_free(s
->ctx
.fs_root
);
4296 typedef struct VirtfsCoResetData
{
4299 } VirtfsCoResetData
;
4301 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4303 VirtfsCoResetData
*data
= opaque
;
4305 virtfs_reset(&data
->pdu
);
4309 void v9fs_reset(V9fsState
*s
)
4311 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4314 while (!QLIST_EMPTY(&s
->active_list
)) {
4315 aio_poll(qemu_get_aio_context(), true);
4318 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4319 qemu_coroutine_enter(co
);
4321 while (!data
.done
) {
4322 aio_poll(qemu_get_aio_context(), true);
4326 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4329 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4330 error_report("Failed to get the resource limit");
4333 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/ 3);
4334 open_fd_rc
= rlim
.rlim_cur
/ 2;