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
261 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
264 if (f
->fid_type
== P9_FID_FILE
) {
265 if (f
->fs
.fd
== -1) {
267 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
268 } while (err
== -EINTR
&& !pdu
->cancelled
);
270 } else if (f
->fid_type
== P9_FID_DIR
) {
271 if (f
->fs
.dir
.stream
== NULL
) {
273 err
= v9fs_co_opendir(pdu
, f
);
274 } while (err
== -EINTR
&& !pdu
->cancelled
);
280 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
284 V9fsState
*s
= pdu
->s
;
286 f
= g_hash_table_lookup(s
->fids
, GINT_TO_POINTER(fid
));
290 * Update the fid ref upfront so that
291 * we don't get reclaimed when we yield
296 * check whether we need to reopen the
297 * file. We might have closed the fd
298 * while trying to free up some file
301 err
= v9fs_reopen_fid(pdu
, f
);
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
310 f
->flags
|= FID_REFERENCED
;
316 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
320 f
= g_hash_table_lookup(s
->fids
, GINT_TO_POINTER(fid
));
322 /* If fid is already there return NULL */
326 f
= g_new0(V9fsFidState
, 1);
328 f
->fid_type
= P9_FID_NONE
;
331 * Mark the fid as referenced so that the LRU
332 * reclaim won't close the file descriptor
334 f
->flags
|= FID_REFERENCED
;
335 g_hash_table_insert(s
->fids
, GINT_TO_POINTER(fid
), f
);
337 v9fs_readdir_init(s
->proto_version
, &f
->fs
.dir
);
338 v9fs_readdir_init(s
->proto_version
, &f
->fs_reclaim
.dir
);
343 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
347 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
348 /* getxattr/listxattr fid */
352 * if this is fid for setxattr. clunk should
353 * result in setxattr localcall
355 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
356 /* clunk after partial write */
360 if (fidp
->fs
.xattr
.len
) {
361 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
362 fidp
->fs
.xattr
.value
,
364 fidp
->fs
.xattr
.flags
);
366 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
369 v9fs_string_free(&fidp
->fs
.xattr
.name
);
371 g_free(fidp
->fs
.xattr
.value
);
375 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
379 if (fidp
->fid_type
== P9_FID_FILE
) {
380 /* If we reclaimed the fd no need to close */
381 if (fidp
->fs
.fd
!= -1) {
382 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
384 } else if (fidp
->fid_type
== P9_FID_DIR
) {
385 if (fidp
->fs
.dir
.stream
!= NULL
) {
386 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
388 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
389 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
391 v9fs_path_free(&fidp
->path
);
396 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
401 * Don't free the fid if it is in reclaim list
403 if (!fidp
->ref
&& fidp
->clunked
) {
404 if (fidp
->fid
== pdu
->s
->root_fid
) {
406 * if the clunked fid is root fid then we
407 * have unmounted the fs on the client side.
408 * delete the migration blocker. Ideally, this
409 * should be hooked to transport close notification
411 if (pdu
->s
->migration_blocker
) {
412 migrate_del_blocker(pdu
->s
->migration_blocker
);
413 error_free(pdu
->s
->migration_blocker
);
414 pdu
->s
->migration_blocker
= NULL
;
417 return free_fid(pdu
, fidp
);
422 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
426 /* TODO: Use g_hash_table_steal_extended() instead? */
427 fidp
= g_hash_table_lookup(s
->fids
, GINT_TO_POINTER(fid
));
429 g_hash_table_remove(s
->fids
, GINT_TO_POINTER(fid
));
430 fidp
->clunked
= true;
436 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
438 int reclaim_count
= 0;
439 V9fsState
*s
= pdu
->s
;
444 g_hash_table_iter_init(&iter
, s
->fids
);
446 QSLIST_HEAD(, V9fsFidState
) reclaim_list
=
447 QSLIST_HEAD_INITIALIZER(reclaim_list
);
449 while (g_hash_table_iter_next(&iter
, &fid
, (gpointer
*) &f
)) {
451 * Unlink fids cannot be reclaimed. Check
452 * for them and skip them. Also skip fids
453 * currently being operated on.
455 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
459 * if it is a recently referenced fid
460 * we leave the fid untouched and clear the
461 * reference bit. We come back to it later
462 * in the next iteration. (a simple LRU without
463 * moving list elements around)
465 if (f
->flags
& FID_REFERENCED
) {
466 f
->flags
&= ~FID_REFERENCED
;
470 * Add fids to reclaim list.
472 if (f
->fid_type
== P9_FID_FILE
) {
473 if (f
->fs
.fd
!= -1) {
475 * Up the reference count so that
476 * a clunk request won't free this fid
479 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
480 f
->fs_reclaim
.fd
= f
->fs
.fd
;
484 } else if (f
->fid_type
== P9_FID_DIR
) {
485 if (f
->fs
.dir
.stream
!= NULL
) {
487 * Up the reference count so that
488 * a clunk request won't free this fid
491 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
492 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
493 f
->fs
.dir
.stream
= NULL
;
497 if (reclaim_count
>= open_fd_rc
) {
502 * Now close the fid in reclaim list. Free them if they
503 * are already clunked.
505 while (!QSLIST_EMPTY(&reclaim_list
)) {
506 f
= QSLIST_FIRST(&reclaim_list
);
507 QSLIST_REMOVE(&reclaim_list
, f
, V9fsFidState
, reclaim_next
);
508 if (f
->fid_type
== P9_FID_FILE
) {
509 v9fs_co_close(pdu
, &f
->fs_reclaim
);
510 } else if (f
->fid_type
== P9_FID_DIR
) {
511 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
514 * Now drop the fid reference, free it
522 * This is used when a path is removed from the directory tree. Any
523 * fids that still reference it must not be closed from then on, since
524 * they cannot be reopened.
526 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
529 V9fsState
*s
= pdu
->s
;
534 * The most common case is probably that we have exactly one
535 * fid for the given path, so preallocate exactly one.
537 g_autoptr(GArray
) to_reopen
= g_array_sized_new(FALSE
, FALSE
,
538 sizeof(V9fsFidState
*), 1);
541 g_hash_table_iter_init(&iter
, s
->fids
);
544 * We iterate over the fid table looking for the entries we need
545 * to reopen, and store them in to_reopen. This is because
546 * v9fs_reopen_fid() and put_fid() yield. This allows the fid table
547 * to be modified in the meantime, invalidating our iterator.
549 while (g_hash_table_iter_next(&iter
, &fid
, (gpointer
*) &fidp
)) {
550 if (fidp
->path
.size
== path
->size
&&
551 !memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
553 * Ensure the fid survives a potential clunk request during
554 * v9fs_reopen_fid or put_fid.
557 fidp
->flags
|= FID_NON_RECLAIMABLE
;
558 g_array_append_val(to_reopen
, fidp
);
562 for (i
= 0; i
< to_reopen
->len
; i
++) {
563 fidp
= g_array_index(to_reopen
, V9fsFidState
*, i
);
564 /* reopen the file/dir if already closed */
565 err
= v9fs_reopen_fid(pdu
, fidp
);
571 for (i
= 0; i
< to_reopen
->len
; i
++) {
572 put_fid(pdu
, g_array_index(to_reopen
, V9fsFidState
*, i
));
577 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
579 V9fsState
*s
= pdu
->s
;
583 * Get a list of all the values (fid states) in the table, which
586 g_autoptr(GList
) fids
= g_hash_table_get_values(s
->fids
);
588 /* ... remove from the table, taking over ownership. */
589 g_hash_table_steal_all(s
->fids
);
592 * This allows us to release our references to them asynchronously without
593 * iterating over the hash table and risking iterator invalidation
594 * through concurrent modifications.
596 for (freeing
= fids
; freeing
; freeing
= freeing
->next
) {
597 fidp
= freeing
->data
;
599 fidp
->clunked
= true;
604 #define P9_QID_TYPE_DIR 0x80
605 #define P9_QID_TYPE_SYMLINK 0x02
607 #define P9_STAT_MODE_DIR 0x80000000
608 #define P9_STAT_MODE_APPEND 0x40000000
609 #define P9_STAT_MODE_EXCL 0x20000000
610 #define P9_STAT_MODE_MOUNT 0x10000000
611 #define P9_STAT_MODE_AUTH 0x08000000
612 #define P9_STAT_MODE_TMP 0x04000000
613 #define P9_STAT_MODE_SYMLINK 0x02000000
614 #define P9_STAT_MODE_LINK 0x01000000
615 #define P9_STAT_MODE_DEVICE 0x00800000
616 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
617 #define P9_STAT_MODE_SOCKET 0x00100000
618 #define P9_STAT_MODE_SETUID 0x00080000
619 #define P9_STAT_MODE_SETGID 0x00040000
620 #define P9_STAT_MODE_SETVTX 0x00010000
622 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
623 P9_STAT_MODE_SYMLINK | \
624 P9_STAT_MODE_LINK | \
625 P9_STAT_MODE_DEVICE | \
626 P9_STAT_MODE_NAMED_PIPE | \
629 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
630 static inline uint8_t mirror8bit(uint8_t byte
)
632 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
635 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
636 static inline uint64_t mirror64bit(uint64_t value
)
638 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
639 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
640 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
641 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
642 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
643 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
644 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
645 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
649 * Parameter k for the Exponential Golomb algorihm to be used.
651 * The smaller this value, the smaller the minimum bit count for the Exp.
652 * Golomb generated affixes will be (at lowest index) however for the
653 * price of having higher maximum bit count of generated affixes (at highest
654 * index). Likewise increasing this parameter yields in smaller maximum bit
655 * count for the price of having higher minimum bit count.
657 * In practice that means: a good value for k depends on the expected amount
658 * of devices to be exposed by one export. For a small amount of devices k
659 * should be small, for a large amount of devices k might be increased
660 * instead. The default of k=0 should be fine for most users though.
662 * IMPORTANT: In case this ever becomes a runtime parameter; the value of
663 * k should not change as long as guest is still running! Because that would
664 * cause completely different inode numbers to be generated on guest.
666 #define EXP_GOLOMB_K 0
669 * expGolombEncode() - Exponential Golomb algorithm for arbitrary k
672 * @n: natural number (or index) of the prefix to be generated
674 * @k: parameter k of Exp. Golomb algorithm to be used
675 * (see comment on EXP_GOLOMB_K macro for details about k)
676 * Return: prefix for given @n and @k
678 * The Exponential Golomb algorithm generates prefixes (NOT suffixes!)
679 * with growing length and with the mathematical property of being
680 * "prefix-free". The latter means the generated prefixes can be prepended
681 * in front of arbitrary numbers and the resulting concatenated numbers are
682 * guaranteed to be always unique.
684 * This is a minor adjustment to the original Exp. Golomb algorithm in the
685 * sense that lowest allowed index (@n) starts with 1, not with zero.
687 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
689 const uint64_t value
= n
+ (1 << k
) - 1;
690 const int bits
= (int) log2(value
) + 1;
691 return (VariLenAffix
) {
692 .type
= AffixType_Prefix
,
694 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
699 * invertAffix() - Converts a suffix into a prefix, or a prefix into a suffix.
700 * @affix: either suffix or prefix to be inverted
701 * Return: inversion of passed @affix
703 * Simply mirror all bits of the affix value, for the purpose to preserve
704 * respectively the mathematical "prefix-free" or "suffix-free" property
705 * after the conversion.
707 * If a passed prefix is suitable to create unique numbers, then the
708 * returned suffix is suitable to create unique numbers as well (and vice
711 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
713 return (VariLenAffix
) {
715 (affix
->type
== AffixType_Suffix
) ?
716 AffixType_Prefix
: AffixType_Suffix
,
718 mirror64bit(affix
->value
) >>
719 ((sizeof(affix
->value
) * 8) - affix
->bits
),
725 * affixForIndex() - Generates suffix numbers with "suffix-free" property.
726 * @index: natural number (or index) of the suffix to be generated
728 * Return: Suffix suitable to assemble unique number.
730 * This is just a wrapper function on top of the Exp. Golomb algorithm.
732 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
733 * this function converts the Exp. Golomb prefixes into appropriate suffixes
734 * which are still suitable for generating unique numbers.
736 static VariLenAffix
affixForIndex(uint64_t index
)
739 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
740 return invertAffix(&prefix
); /* convert prefix to suffix */
743 /* creative abuse of tb_hash_func7, which is based on xxhash */
744 static uint32_t qpp_hash(QppEntry e
)
746 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
749 static uint32_t qpf_hash(QpfEntry e
)
751 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
754 static bool qpd_cmp_func(const void *obj
, const void *userp
)
756 const QpdEntry
*e1
= obj
, *e2
= userp
;
757 return e1
->dev
== e2
->dev
;
760 static bool qpp_cmp_func(const void *obj
, const void *userp
)
762 const QppEntry
*e1
= obj
, *e2
= userp
;
763 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
766 static bool qpf_cmp_func(const void *obj
, const void *userp
)
768 const QpfEntry
*e1
= obj
, *e2
= userp
;
769 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
772 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
777 static void qp_table_destroy(struct qht
*ht
)
779 if (!ht
|| !ht
->map
) {
782 qht_iter(ht
, qp_table_remove
, NULL
);
786 static void qpd_table_init(struct qht
*ht
)
788 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
791 static void qpp_table_init(struct qht
*ht
)
793 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
796 static void qpf_table_init(struct qht
*ht
)
798 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
802 * Returns how many (high end) bits of inode numbers of the passed fs
803 * device shall be used (in combination with the device number) to
804 * generate hash values for qpp_table entries.
806 * This function is required if variable length suffixes are used for inode
807 * number mapping on guest level. Since a device may end up having multiple
808 * entries in qpp_table, each entry most probably with a different suffix
809 * length, we thus need this function in conjunction with qpd_table to
810 * "agree" about a fix amount of bits (per device) to be always used for
811 * generating hash values for the purpose of accessing qpp_table in order
812 * get consistent behaviour when accessing qpp_table.
814 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
822 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
824 val
= g_new0(QpdEntry
, 1);
826 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
827 val
->prefix_bits
= affix
.bits
;
828 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
829 pdu
->s
->qp_ndevices
++;
831 return val
->prefix_bits
;
835 * Slow / full mapping host inode nr -> guest inode nr.
837 * This function performs a slower and much more costly remapping of an
838 * original file inode number on host to an appropriate different inode
839 * number on guest. For every (dev, inode) combination on host a new
840 * sequential number is generated, cached and exposed as inode number on
843 * This is just a "last resort" fallback solution if the much faster/cheaper
844 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
845 * expected ever to be used at all though.
847 * See qid_path_suffixmap() for details
850 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
854 .dev
= stbuf
->st_dev
,
857 uint32_t hash
= qpf_hash(lookup
);
860 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
863 if (pdu
->s
->qp_fullpath_next
== 0) {
864 /* no more files can be mapped :'( */
866 "9p: No more prefixes available for remapping inodes from "
872 val
= g_new0(QpfEntry
, 1);
875 /* new unique inode and device combo */
876 affix
= affixForIndex(
877 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
879 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
880 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
881 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
889 * Quick mapping host inode nr -> guest inode nr.
891 * This function performs quick remapping of an original file inode number
892 * on host to an appropriate different inode number on guest. This remapping
893 * of inodes is required to avoid inode nr collisions on guest which would
894 * happen if the 9p export contains more than 1 exported file system (or
895 * more than 1 file system data set), because unlike on host level where the
896 * files would have different device nrs, all files exported by 9p would
897 * share the same device nr on guest (the device nr of the virtual 9p device
900 * Inode remapping is performed by chopping off high end bits of the original
901 * inode number from host, shifting the result upwards and then assigning a
902 * generated suffix number for the low end bits, where the same suffix number
903 * will be shared by all inodes with the same device id AND the same high end
904 * bits that have been chopped off. That approach utilizes the fact that inode
905 * numbers very likely share the same high end bits (i.e. due to their common
906 * sequential generation by file systems) and hence we only have to generate
907 * and track a very limited amount of suffixes in practice due to that.
909 * We generate variable size suffixes for that purpose. The 1st generated
910 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
911 * the original inode number. The subsequent suffixes being generated will
912 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
913 * generated will have 3 bits and hence we have to chop off 3 bits from their
914 * original inodes, and so on. That approach of using variable length suffixes
915 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
916 * limited amount of devices are shared by the same export (e.g. typically
917 * less than 2 dozen devices per 9p export), so in practice we need to chop
918 * off less bits than with fixed size prefixes and yet are flexible to add
919 * new devices at runtime below host's export directory at any time without
920 * having to reboot guest nor requiring to reconfigure guest for that. And due
921 * to the very limited amount of original high end bits that we chop off that
922 * way, the total amount of suffixes we need to generate is less than by using
923 * fixed size prefixes and hence it also improves performance of the inode
924 * remapping algorithm, and finally has the nice side effect that the inode
925 * numbers on guest will be much smaller & human friendly. ;-)
927 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
930 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
932 .dev
= stbuf
->st_dev
,
933 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
935 uint32_t hash
= qpp_hash(lookup
);
937 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
940 if (pdu
->s
->qp_affix_next
== 0) {
941 /* we ran out of affixes */
943 "9p: Potential degraded performance of inode remapping"
948 val
= g_new0(QppEntry
, 1);
951 /* new unique inode affix and device combo */
952 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
953 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
954 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
956 /* assuming generated affix to be suffix type, not prefix */
957 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
961 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
966 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
967 /* map inode+device to qid path (fast path) */
968 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
969 if (err
== -ENFILE
) {
970 /* fast path didn't work, fall back to full map */
971 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
977 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
978 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
980 "9p: Multiple devices detected in same VirtFS export. "
981 "Access of guest to additional devices is (partly) "
982 "denied due to virtfs option 'multidevs=forbid' being "
988 "9p: Multiple devices detected in same VirtFS export, "
989 "which might lead to file ID collisions and severe "
990 "misbehaviours on guest! You should either use a "
991 "separate export for each device shared from host or "
992 "use virtfs option 'multidevs=remap'!"
996 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
997 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
998 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
1001 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
1003 if (S_ISDIR(stbuf
->st_mode
)) {
1004 qidp
->type
|= P9_QID_TYPE_DIR
;
1006 if (S_ISLNK(stbuf
->st_mode
)) {
1007 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
1013 V9fsPDU
*pdu_alloc(V9fsState
*s
)
1015 V9fsPDU
*pdu
= NULL
;
1017 if (!QLIST_EMPTY(&s
->free_list
)) {
1018 pdu
= QLIST_FIRST(&s
->free_list
);
1019 QLIST_REMOVE(pdu
, next
);
1020 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
1025 void pdu_free(V9fsPDU
*pdu
)
1027 V9fsState
*s
= pdu
->s
;
1029 g_assert(!pdu
->cancelled
);
1030 QLIST_REMOVE(pdu
, next
);
1031 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
1034 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
1036 int8_t id
= pdu
->id
+ 1; /* Response */
1037 V9fsState
*s
= pdu
->s
;
1041 * The 9p spec requires that successfully cancelled pdus receive no reply.
1042 * Sending a reply would confuse clients because they would
1043 * assume that any EINTR is the actual result of the operation,
1044 * rather than a consequence of the cancellation. However, if
1045 * the operation completed (succesfully or with an error other
1046 * than caused be cancellation), we do send out that reply, both
1047 * for efficiency and to avoid confusing the rest of the state machine
1048 * that assumes passing a non-error here will mean a successful
1049 * transmission of the reply.
1051 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1053 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1062 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1065 str
.data
= strerror(err
);
1066 str
.size
= strlen(str
.data
);
1068 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1075 err
= errno_to_dotl(err
);
1078 ret
= pdu_marshal(pdu
, len
, "d", err
);
1084 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1087 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1090 /* fill out the header */
1091 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1095 /* keep these in sync */
1100 pdu
->s
->transport
->push_and_notify(pdu
);
1102 /* Now wakeup anybody waiting in flush for this request */
1103 if (!qemu_co_queue_next(&pdu
->complete
)) {
1108 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1113 if (mode
& P9_STAT_MODE_DIR
) {
1117 if (mode
& P9_STAT_MODE_SYMLINK
) {
1120 if (mode
& P9_STAT_MODE_SOCKET
) {
1123 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1126 if (mode
& P9_STAT_MODE_DEVICE
) {
1127 if (extension
->size
&& extension
->data
[0] == 'c') {
1134 if (!(ret
& ~0777)) {
1138 if (mode
& P9_STAT_MODE_SETUID
) {
1141 if (mode
& P9_STAT_MODE_SETGID
) {
1144 if (mode
& P9_STAT_MODE_SETVTX
) {
1151 static int donttouch_stat(V9fsStat
*stat
)
1153 if (stat
->type
== -1 &&
1155 stat
->qid
.type
== 0xff &&
1156 stat
->qid
.version
== (uint32_t) -1 &&
1157 stat
->qid
.path
== (uint64_t) -1 &&
1159 stat
->atime
== -1 &&
1160 stat
->mtime
== -1 &&
1161 stat
->length
== -1 &&
1166 stat
->n_uid
== -1 &&
1167 stat
->n_gid
== -1 &&
1168 stat
->n_muid
== -1) {
1175 static void v9fs_stat_init(V9fsStat
*stat
)
1177 v9fs_string_init(&stat
->name
);
1178 v9fs_string_init(&stat
->uid
);
1179 v9fs_string_init(&stat
->gid
);
1180 v9fs_string_init(&stat
->muid
);
1181 v9fs_string_init(&stat
->extension
);
1184 static void v9fs_stat_free(V9fsStat
*stat
)
1186 v9fs_string_free(&stat
->name
);
1187 v9fs_string_free(&stat
->uid
);
1188 v9fs_string_free(&stat
->gid
);
1189 v9fs_string_free(&stat
->muid
);
1190 v9fs_string_free(&stat
->extension
);
1193 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1197 mode
= stbuf
->st_mode
& 0777;
1198 if (S_ISDIR(stbuf
->st_mode
)) {
1199 mode
|= P9_STAT_MODE_DIR
;
1202 if (S_ISLNK(stbuf
->st_mode
)) {
1203 mode
|= P9_STAT_MODE_SYMLINK
;
1206 if (S_ISSOCK(stbuf
->st_mode
)) {
1207 mode
|= P9_STAT_MODE_SOCKET
;
1210 if (S_ISFIFO(stbuf
->st_mode
)) {
1211 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1214 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1215 mode
|= P9_STAT_MODE_DEVICE
;
1218 if (stbuf
->st_mode
& S_ISUID
) {
1219 mode
|= P9_STAT_MODE_SETUID
;
1222 if (stbuf
->st_mode
& S_ISGID
) {
1223 mode
|= P9_STAT_MODE_SETGID
;
1226 if (stbuf
->st_mode
& S_ISVTX
) {
1227 mode
|= P9_STAT_MODE_SETVTX
;
1233 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1234 const char *basename
,
1235 const struct stat
*stbuf
,
1240 memset(v9stat
, 0, sizeof(*v9stat
));
1242 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1246 v9stat
->mode
= stat_to_v9mode(stbuf
);
1247 v9stat
->atime
= stbuf
->st_atime
;
1248 v9stat
->mtime
= stbuf
->st_mtime
;
1249 v9stat
->length
= stbuf
->st_size
;
1251 v9fs_string_free(&v9stat
->uid
);
1252 v9fs_string_free(&v9stat
->gid
);
1253 v9fs_string_free(&v9stat
->muid
);
1255 v9stat
->n_uid
= stbuf
->st_uid
;
1256 v9stat
->n_gid
= stbuf
->st_gid
;
1259 v9fs_string_free(&v9stat
->extension
);
1261 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1262 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1266 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1267 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1268 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1269 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1270 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1271 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1272 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1275 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1278 v9fs_string_size(&v9stat
->name
) +
1279 v9fs_string_size(&v9stat
->uid
) +
1280 v9fs_string_size(&v9stat
->gid
) +
1281 v9fs_string_size(&v9stat
->muid
) +
1282 v9fs_string_size(&v9stat
->extension
);
1286 #define P9_STATS_MODE 0x00000001ULL
1287 #define P9_STATS_NLINK 0x00000002ULL
1288 #define P9_STATS_UID 0x00000004ULL
1289 #define P9_STATS_GID 0x00000008ULL
1290 #define P9_STATS_RDEV 0x00000010ULL
1291 #define P9_STATS_ATIME 0x00000020ULL
1292 #define P9_STATS_MTIME 0x00000040ULL
1293 #define P9_STATS_CTIME 0x00000080ULL
1294 #define P9_STATS_INO 0x00000100ULL
1295 #define P9_STATS_SIZE 0x00000200ULL
1296 #define P9_STATS_BLOCKS 0x00000400ULL
1298 #define P9_STATS_BTIME 0x00000800ULL
1299 #define P9_STATS_GEN 0x00001000ULL
1300 #define P9_STATS_DATA_VERSION 0x00002000ULL
1302 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1303 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1307 * blksize_to_iounit() - Block size exposed to 9p client.
1308 * Return: block size
1310 * @pdu: 9p client request
1311 * @blksize: host filesystem's block size
1313 * Convert host filesystem's block size into an appropriate block size for
1314 * 9p client (guest OS side). The value returned suggests an "optimum" block
1315 * size for 9p I/O, i.e. to maximize performance.
1317 static int32_t blksize_to_iounit(const V9fsPDU
*pdu
, int32_t blksize
)
1320 V9fsState
*s
= pdu
->s
;
1323 * iounit should be multiples of blksize (host filesystem block size)
1324 * as well as less than (client msize - P9_IOHDRSZ)
1327 iounit
= QEMU_ALIGN_DOWN(s
->msize
- P9_IOHDRSZ
, blksize
);
1330 iounit
= s
->msize
- P9_IOHDRSZ
;
1335 static int32_t stat_to_iounit(const V9fsPDU
*pdu
, const struct stat
*stbuf
)
1337 return blksize_to_iounit(pdu
, stbuf
->st_blksize
);
1340 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1341 V9fsStatDotl
*v9lstat
)
1343 memset(v9lstat
, 0, sizeof(*v9lstat
));
1345 v9lstat
->st_mode
= stbuf
->st_mode
;
1346 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1347 v9lstat
->st_uid
= stbuf
->st_uid
;
1348 v9lstat
->st_gid
= stbuf
->st_gid
;
1349 v9lstat
->st_rdev
= host_dev_to_dotl_dev(stbuf
->st_rdev
);
1350 v9lstat
->st_size
= stbuf
->st_size
;
1351 v9lstat
->st_blksize
= stat_to_iounit(pdu
, stbuf
);
1352 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1353 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1354 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1355 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1356 #ifdef CONFIG_DARWIN
1357 v9lstat
->st_atime_nsec
= stbuf
->st_atimespec
.tv_nsec
;
1358 v9lstat
->st_mtime_nsec
= stbuf
->st_mtimespec
.tv_nsec
;
1359 v9lstat
->st_ctime_nsec
= stbuf
->st_ctimespec
.tv_nsec
;
1361 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1362 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1363 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1365 /* Currently we only support BASIC fields in stat */
1366 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1368 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1371 static void print_sg(struct iovec
*sg
, int cnt
)
1375 printf("sg[%d]: {", cnt
);
1376 for (i
= 0; i
< cnt
; i
++) {
1380 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1385 /* Will call this only for path name based fid */
1386 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1389 v9fs_path_init(&str
);
1390 v9fs_path_copy(&str
, dst
);
1391 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1392 v9fs_path_free(&str
);
1395 static inline bool is_ro_export(FsContext
*ctx
)
1397 return ctx
->export_flags
& V9FS_RDONLY
;
1400 static void coroutine_fn
v9fs_version(void *opaque
)
1403 V9fsPDU
*pdu
= opaque
;
1404 V9fsState
*s
= pdu
->s
;
1408 v9fs_string_init(&version
);
1409 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1413 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1417 if (!strcmp(version
.data
, "9P2000.u")) {
1418 s
->proto_version
= V9FS_PROTO_2000U
;
1419 } else if (!strcmp(version
.data
, "9P2000.L")) {
1420 s
->proto_version
= V9FS_PROTO_2000L
;
1422 v9fs_string_sprintf(&version
, "unknown");
1423 /* skip min. msize check, reporting invalid version has priority */
1427 if (s
->msize
< P9_MIN_MSIZE
) {
1430 "9pfs: Client requested msize < minimum msize ("
1431 stringify(P9_MIN_MSIZE
) ") supported by this server."
1436 /* 8192 is the default msize of Linux clients */
1437 if (s
->msize
<= 8192 && !(s
->ctx
.export_flags
& V9FS_NO_PERF_WARN
)) {
1439 "9p: degraded performance: a reasonable high msize should be "
1440 "chosen on client/guest side (chosen msize is <= 8192). See "
1441 "https://wiki.qemu.org/Documentation/9psetup#msize for details."
1446 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1451 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1453 pdu_complete(pdu
, err
);
1454 v9fs_string_free(&version
);
1457 static void coroutine_fn
v9fs_attach(void *opaque
)
1459 V9fsPDU
*pdu
= opaque
;
1460 V9fsState
*s
= pdu
->s
;
1461 int32_t fid
, afid
, n_uname
;
1462 V9fsString uname
, aname
;
1469 v9fs_string_init(&uname
);
1470 v9fs_string_init(&aname
);
1471 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1472 &afid
, &uname
, &aname
, &n_uname
);
1476 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1478 fidp
= alloc_fid(s
, fid
);
1483 fidp
->uid
= n_uname
;
1484 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1490 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1496 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1504 * disable migration if we haven't done already.
1505 * attach could get called multiple times for the same export.
1507 if (!s
->migration_blocker
) {
1508 error_setg(&s
->migration_blocker
,
1509 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1510 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1511 err
= migrate_add_blocker(s
->migration_blocker
, NULL
);
1513 error_free(s
->migration_blocker
);
1514 s
->migration_blocker
= NULL
;
1521 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1528 memcpy(&s
->root_st
, &stbuf
, sizeof(stbuf
));
1529 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1530 qid
.type
, qid
.version
, qid
.path
);
1534 pdu_complete(pdu
, err
);
1535 v9fs_string_free(&uname
);
1536 v9fs_string_free(&aname
);
1539 static void coroutine_fn
v9fs_stat(void *opaque
)
1547 V9fsPDU
*pdu
= opaque
;
1550 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1554 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1556 fidp
= get_fid(pdu
, fid
);
1561 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1565 basename
= g_path_get_basename(fidp
->path
.data
);
1566 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1571 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1573 v9fs_stat_free(&v9stat
);
1576 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1577 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1579 v9fs_stat_free(&v9stat
);
1583 pdu_complete(pdu
, err
);
1586 static void coroutine_fn
v9fs_getattr(void *opaque
)
1593 uint64_t request_mask
;
1594 V9fsStatDotl v9stat_dotl
;
1595 V9fsPDU
*pdu
= opaque
;
1597 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1601 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1603 fidp
= get_fid(pdu
, fid
);
1609 * Currently we only support BASIC fields in stat, so there is no
1610 * need to look at request_mask.
1612 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1616 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1621 /* fill st_gen if requested and supported by underlying fs */
1622 if (request_mask
& P9_STATS_GEN
) {
1623 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1626 /* we have valid st_gen: update result mask */
1627 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1630 /* request cancelled, e.g. by Tflush */
1633 /* failed to get st_gen: not fatal, ignore */
1637 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1642 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1643 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1644 v9stat_dotl
.st_gid
);
1648 pdu_complete(pdu
, retval
);
1651 /* Attribute flags */
1652 #define P9_ATTR_MODE (1 << 0)
1653 #define P9_ATTR_UID (1 << 1)
1654 #define P9_ATTR_GID (1 << 2)
1655 #define P9_ATTR_SIZE (1 << 3)
1656 #define P9_ATTR_ATIME (1 << 4)
1657 #define P9_ATTR_MTIME (1 << 5)
1658 #define P9_ATTR_CTIME (1 << 6)
1659 #define P9_ATTR_ATIME_SET (1 << 7)
1660 #define P9_ATTR_MTIME_SET (1 << 8)
1662 #define P9_ATTR_MASK 127
1664 static void coroutine_fn
v9fs_setattr(void *opaque
)
1671 V9fsPDU
*pdu
= opaque
;
1673 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1678 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1679 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1680 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1682 fidp
= get_fid(pdu
, fid
);
1687 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1688 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1693 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1694 struct timespec times
[2];
1695 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1696 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1697 times
[0].tv_sec
= v9iattr
.atime_sec
;
1698 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1700 times
[0].tv_nsec
= UTIME_NOW
;
1703 times
[0].tv_nsec
= UTIME_OMIT
;
1705 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1706 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1707 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1708 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1710 times
[1].tv_nsec
= UTIME_NOW
;
1713 times
[1].tv_nsec
= UTIME_OMIT
;
1715 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1721 * If the only valid entry in iattr is ctime we can call
1722 * chown(-1,-1) to update the ctime of the file
1724 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1725 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1726 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1727 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1730 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1733 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1739 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1740 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1746 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1750 pdu_complete(pdu
, err
);
1753 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1759 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1764 for (i
= 0; i
< nwnames
; i
++) {
1765 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1774 static bool name_is_illegal(const char *name
)
1776 return !*name
|| strchr(name
, '/') != NULL
;
1779 static bool same_stat_id(const struct stat
*a
, const struct stat
*b
)
1781 return a
->st_dev
== b
->st_dev
&& a
->st_ino
== b
->st_ino
;
1784 static void coroutine_fn
v9fs_walk(void *opaque
)
1786 int name_idx
, nwalked
;
1787 g_autofree V9fsQID
*qids
= NULL
;
1788 int i
, err
= 0, any_err
= 0;
1789 V9fsPath dpath
, path
;
1790 P9ARRAY_REF(V9fsPath
) pathes
= NULL
;
1792 struct stat stbuf
, fidst
;
1793 g_autofree
struct stat
*stbufs
= NULL
;
1795 int32_t fid
, newfid
;
1796 P9ARRAY_REF(V9fsString
) wnames
= NULL
;
1798 V9fsFidState
*newfidp
= NULL
;
1799 V9fsPDU
*pdu
= opaque
;
1800 V9fsState
*s
= pdu
->s
;
1803 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1805 pdu_complete(pdu
, err
);
1810 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1812 if (nwnames
> P9_MAXWELEM
) {
1817 P9ARRAY_NEW(V9fsString
, wnames
, nwnames
);
1818 qids
= g_new0(V9fsQID
, nwnames
);
1819 stbufs
= g_new0(struct stat
, nwnames
);
1820 P9ARRAY_NEW(V9fsPath
, pathes
, nwnames
);
1821 for (i
= 0; i
< nwnames
; i
++) {
1822 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1826 if (name_is_illegal(wnames
[i
].data
)) {
1833 fidp
= get_fid(pdu
, fid
);
1839 v9fs_path_init(&dpath
);
1840 v9fs_path_init(&path
);
1842 * Both dpath and path initially point to fidp.
1843 * Needed to handle request with nwnames == 0
1845 v9fs_path_copy(&dpath
, &fidp
->path
);
1846 v9fs_path_copy(&path
, &fidp
->path
);
1849 * To keep latency (i.e. overall execution time for processing this
1850 * Twalk client request) as small as possible, run all the required fs
1851 * driver code altogether inside the following block.
1853 v9fs_co_run_in_worker({
1855 if (v9fs_request_cancelled(pdu
)) {
1856 any_err
|= err
= -EINTR
;
1859 err
= s
->ops
->lstat(&s
->ctx
, &dpath
, &fidst
);
1861 any_err
|= err
= -errno
;
1865 for (; nwalked
< nwnames
; nwalked
++) {
1866 if (v9fs_request_cancelled(pdu
)) {
1867 any_err
|= err
= -EINTR
;
1870 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1871 strcmp("..", wnames
[nwalked
].data
))
1873 err
= s
->ops
->name_to_path(&s
->ctx
, &dpath
,
1874 wnames
[nwalked
].data
,
1877 any_err
|= err
= -errno
;
1880 if (v9fs_request_cancelled(pdu
)) {
1881 any_err
|= err
= -EINTR
;
1884 err
= s
->ops
->lstat(&s
->ctx
, &pathes
[nwalked
], &stbuf
);
1886 any_err
|= err
= -errno
;
1889 stbufs
[nwalked
] = stbuf
;
1890 v9fs_path_copy(&dpath
, &pathes
[nwalked
]);
1895 * Handle all the rest of this Twalk request on main thread ...
1897 * NOTE: -EINTR is an exception where we deviate from the protocol spec
1898 * and simply send a (R)Lerror response instead of bothering to assemble
1899 * a (deducted) Rwalk response; because -EINTR is always the result of a
1900 * Tflush request, so client would no longer wait for a response in this
1903 if ((err
< 0 && !nwalked
) || err
== -EINTR
) {
1907 any_err
|= err
= stat_to_qid(pdu
, &fidst
, &qid
);
1908 if (err
< 0 && !nwalked
) {
1913 /* reset dpath and path */
1914 v9fs_path_copy(&dpath
, &fidp
->path
);
1915 v9fs_path_copy(&path
, &fidp
->path
);
1917 for (name_idx
= 0; name_idx
< nwalked
; name_idx
++) {
1918 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1919 strcmp("..", wnames
[name_idx
].data
))
1921 stbuf
= stbufs
[name_idx
];
1922 any_err
|= err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1926 v9fs_path_copy(&path
, &pathes
[name_idx
]);
1927 v9fs_path_copy(&dpath
, &path
);
1929 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1933 /* don't send any QIDs, send Rlerror instead */
1936 /* send QIDs (not Rlerror), but fid MUST remain unaffected */
1940 if (fid
== newfid
) {
1941 if (fidp
->fid_type
!= P9_FID_NONE
) {
1945 v9fs_path_write_lock(s
);
1946 v9fs_path_copy(&fidp
->path
, &path
);
1947 v9fs_path_unlock(s
);
1949 newfidp
= alloc_fid(s
, newfid
);
1950 if (newfidp
== NULL
) {
1954 newfidp
->uid
= fidp
->uid
;
1955 v9fs_path_copy(&newfidp
->path
, &path
);
1958 err
= v9fs_walk_marshal(pdu
, name_idx
, qids
);
1959 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, name_idx
, qids
);
1963 put_fid(pdu
, newfidp
);
1965 v9fs_path_free(&dpath
);
1966 v9fs_path_free(&path
);
1968 pdu_complete(pdu
, err
);
1971 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1973 struct statfs stbuf
;
1974 int err
= v9fs_co_statfs(pdu
, path
, &stbuf
);
1976 return blksize_to_iounit(pdu
, (err
>= 0) ? stbuf
.f_bsize
: 0);
1979 static void coroutine_fn
v9fs_open(void *opaque
)
1990 V9fsPDU
*pdu
= opaque
;
1991 V9fsState
*s
= pdu
->s
;
1993 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1994 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1997 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
2003 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
2005 fidp
= get_fid(pdu
, fid
);
2010 if (fidp
->fid_type
!= P9_FID_NONE
) {
2015 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2019 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2023 if (S_ISDIR(stbuf
.st_mode
)) {
2024 err
= v9fs_co_opendir(pdu
, fidp
);
2028 fidp
->fid_type
= P9_FID_DIR
;
2029 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
2035 if (s
->proto_version
== V9FS_PROTO_2000L
) {
2036 flags
= get_dotl_openflags(s
, mode
);
2038 flags
= omode_to_uflags(mode
);
2040 if (is_ro_export(&s
->ctx
)) {
2041 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
2042 mode
& O_APPEND
|| mode
& O_TRUNC
) {
2047 err
= v9fs_co_open(pdu
, fidp
, flags
);
2051 fidp
->fid_type
= P9_FID_FILE
;
2052 fidp
->open_flags
= flags
;
2053 if (flags
& O_EXCL
) {
2055 * We let the host file system do O_EXCL check
2056 * We should not reclaim such fd
2058 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2060 iounit
= get_iounit(pdu
, &fidp
->path
);
2061 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2067 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
2068 qid
.type
, qid
.version
, qid
.path
, iounit
);
2072 pdu_complete(pdu
, err
);
2075 static void coroutine_fn
v9fs_lcreate(void *opaque
)
2077 int32_t dfid
, flags
, mode
;
2086 V9fsPDU
*pdu
= opaque
;
2088 v9fs_string_init(&name
);
2089 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
2090 &name
, &flags
, &mode
, &gid
);
2094 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
2096 if (name_is_illegal(name
.data
)) {
2101 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2106 fidp
= get_fid(pdu
, dfid
);
2111 if (fidp
->fid_type
!= P9_FID_NONE
) {
2116 flags
= get_dotl_openflags(pdu
->s
, flags
);
2117 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
2118 flags
| O_CREAT
, mode
, &stbuf
);
2122 fidp
->fid_type
= P9_FID_FILE
;
2123 fidp
->open_flags
= flags
;
2124 if (flags
& O_EXCL
) {
2126 * We let the host file system do O_EXCL check
2127 * We should not reclaim such fd
2129 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2131 iounit
= get_iounit(pdu
, &fidp
->path
);
2132 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2136 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2141 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2142 qid
.type
, qid
.version
, qid
.path
, iounit
);
2146 pdu_complete(pdu
, err
);
2147 v9fs_string_free(&name
);
2150 static void coroutine_fn
v9fs_fsync(void *opaque
)
2157 V9fsPDU
*pdu
= opaque
;
2159 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2163 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2165 fidp
= get_fid(pdu
, fid
);
2170 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2176 pdu_complete(pdu
, err
);
2179 static void coroutine_fn
v9fs_clunk(void *opaque
)
2185 V9fsPDU
*pdu
= opaque
;
2186 V9fsState
*s
= pdu
->s
;
2188 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2192 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2194 fidp
= clunk_fid(s
, fid
);
2200 * Bump the ref so that put_fid will
2204 err
= put_fid(pdu
, fidp
);
2209 pdu_complete(pdu
, err
);
2213 * Create a QEMUIOVector for a sub-region of PDU iovecs
2215 * @qiov: uninitialized QEMUIOVector
2216 * @skip: number of bytes to skip from beginning of PDU
2217 * @size: number of bytes to include
2218 * @is_write: true - write, false - read
2220 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2221 * with qemu_iovec_destroy().
2223 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2224 size_t skip
, size_t size
,
2232 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2234 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2237 qemu_iovec_init_external(&elem
, iov
, niov
);
2238 qemu_iovec_init(qiov
, niov
);
2239 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
2242 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2243 uint64_t off
, uint32_t max_count
)
2247 uint64_t read_count
;
2248 QEMUIOVector qiov_full
;
2250 if (fidp
->fs
.xattr
.len
< off
) {
2253 read_count
= fidp
->fs
.xattr
.len
- off
;
2255 if (read_count
> max_count
) {
2256 read_count
= max_count
;
2258 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2264 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
2265 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2266 ((char *)fidp
->fs
.xattr
.value
) + off
,
2268 qemu_iovec_destroy(&qiov_full
);
2276 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2285 off_t saved_dir_pos
;
2286 struct dirent
*dent
;
2288 /* save the directory position */
2289 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2290 if (saved_dir_pos
< 0) {
2291 return saved_dir_pos
;
2295 v9fs_path_init(&path
);
2297 v9fs_readdir_lock(&fidp
->fs
.dir
);
2299 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2303 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2307 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2311 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2315 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2316 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2318 /* Ran out of buffer. Set dir back to old position and return */
2319 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2320 v9fs_stat_free(&v9stat
);
2321 v9fs_path_free(&path
);
2325 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2326 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2328 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2331 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2332 v9fs_stat_free(&v9stat
);
2333 v9fs_path_free(&path
);
2337 v9fs_stat_free(&v9stat
);
2338 v9fs_path_free(&path
);
2339 saved_dir_pos
= qemu_dirent_off(dent
);
2342 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2344 v9fs_path_free(&path
);
2351 static void coroutine_fn
v9fs_read(void *opaque
)
2360 V9fsPDU
*pdu
= opaque
;
2361 V9fsState
*s
= pdu
->s
;
2363 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2367 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2369 fidp
= get_fid(pdu
, fid
);
2374 if (fidp
->fid_type
== P9_FID_DIR
) {
2375 if (s
->proto_version
!= V9FS_PROTO_2000U
) {
2377 "9p: bad client: T_read request on directory only expected "
2378 "with 9P2000.u protocol version"
2384 v9fs_co_rewinddir(pdu
, fidp
);
2386 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2391 err
= pdu_marshal(pdu
, offset
, "d", count
);
2395 err
+= offset
+ count
;
2396 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2397 QEMUIOVector qiov_full
;
2401 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2402 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2404 qemu_iovec_reset(&qiov
);
2405 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2407 print_sg(qiov
.iov
, qiov
.niov
);
2409 /* Loop in case of EINTR */
2411 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2416 } while (len
== -EINTR
&& !pdu
->cancelled
);
2418 /* IO error return the error */
2420 goto out_free_iovec
;
2422 } while (count
< max_count
&& len
> 0);
2423 err
= pdu_marshal(pdu
, offset
, "d", count
);
2425 goto out_free_iovec
;
2427 err
+= offset
+ count
;
2429 qemu_iovec_destroy(&qiov
);
2430 qemu_iovec_destroy(&qiov_full
);
2431 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2432 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2436 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2440 pdu_complete(pdu
, err
);
2444 * v9fs_readdir_response_size() - Returns size required in Rreaddir response
2445 * for the passed dirent @name.
2447 * @name: directory entry's name (i.e. file name, directory name)
2448 * Return: required size in bytes
2450 size_t v9fs_readdir_response_size(V9fsString
*name
)
2453 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2454 * size of type (1) + size of name.size (2) + strlen(name.data)
2456 return 24 + v9fs_string_size(name
);
2459 static void v9fs_free_dirents(struct V9fsDirEnt
*e
)
2461 struct V9fsDirEnt
*next
= NULL
;
2463 for (; e
; e
= next
) {
2471 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2472 off_t offset
, int32_t max_count
)
2480 struct dirent
*dent
;
2482 struct V9fsDirEnt
*entries
= NULL
;
2485 * inode remapping requires the device id, which in turn might be
2486 * different for different directory entries, so if inode remapping is
2487 * enabled we have to make a full stat for each directory entry
2489 const bool dostat
= pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
;
2492 * Fetch all required directory entries altogether on a background IO
2493 * thread from fs driver. We don't want to do that for each entry
2494 * individually, because hopping between threads (this main IO thread
2495 * and background IO driver thread) would sum up to huge latencies.
2497 count
= v9fs_co_readdir_many(pdu
, fidp
, &entries
, offset
, max_count
,
2506 for (struct V9fsDirEnt
*e
= entries
; e
; e
= e
->next
) {
2509 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2511 /* e->st should never be NULL, but just to be sure */
2518 err
= stat_to_qid(pdu
, st
, &qid
);
2524 * Fill up just the path field of qid because the client uses
2525 * only that. To fill the entire qid structure we will have
2526 * to stat each dirent found, which is expensive. For the
2527 * latter reason we don't call stat_to_qid() here. Only drawback
2528 * is that no multi-device export detection of stat_to_qid()
2529 * would be done and provided as error to the user here. But
2530 * user would get that error anyway when accessing those
2531 * files/dirs through other ways.
2533 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2534 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2535 /* Fill the other fields with dummy values */
2540 off
= qemu_dirent_off(dent
);
2541 v9fs_string_init(&name
);
2542 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2544 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2545 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2547 dent
->d_type
, &name
);
2549 v9fs_string_free(&name
);
2560 v9fs_free_dirents(entries
);
2567 static void coroutine_fn
v9fs_readdir(void *opaque
)
2573 uint64_t initial_offset
;
2576 V9fsPDU
*pdu
= opaque
;
2577 V9fsState
*s
= pdu
->s
;
2579 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2580 &initial_offset
, &max_count
);
2584 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2586 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2587 if (max_count
> s
->msize
- 11) {
2588 max_count
= s
->msize
- 11;
2590 "9p: bad client: T_readdir with count > msize - 11"
2594 fidp
= get_fid(pdu
, fid
);
2599 if (!fidp
->fs
.dir
.stream
) {
2603 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
2605 "9p: bad client: T_readdir request only expected with 9P2000.L "
2608 retval
= -EOPNOTSUPP
;
2611 count
= v9fs_do_readdir(pdu
, fidp
, (off_t
) initial_offset
, max_count
);
2616 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2620 retval
+= count
+ offset
;
2621 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2625 pdu_complete(pdu
, retval
);
2628 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2629 uint64_t off
, uint32_t count
,
2630 struct iovec
*sg
, int cnt
)
2634 uint64_t write_count
;
2638 if (fidp
->fs
.xattr
.len
< off
) {
2641 write_count
= fidp
->fs
.xattr
.len
- off
;
2642 if (write_count
> count
) {
2643 write_count
= count
;
2645 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2650 fidp
->fs
.xattr
.copied_len
+= write_count
;
2652 * Now copy the content from sg list
2654 for (i
= 0; i
< cnt
; i
++) {
2655 if (write_count
> sg
[i
].iov_len
) {
2656 to_copy
= sg
[i
].iov_len
;
2658 to_copy
= write_count
;
2660 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2661 /* updating vs->off since we are not using below */
2663 write_count
-= to_copy
;
2669 static void coroutine_fn
v9fs_write(void *opaque
)
2679 V9fsPDU
*pdu
= opaque
;
2680 V9fsState
*s
= pdu
->s
;
2681 QEMUIOVector qiov_full
;
2684 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2686 pdu_complete(pdu
, err
);
2690 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2691 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2693 fidp
= get_fid(pdu
, fid
);
2698 if (fidp
->fid_type
== P9_FID_FILE
) {
2699 if (fidp
->fs
.fd
== -1) {
2703 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2705 * setxattr operation
2707 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2708 qiov_full
.iov
, qiov_full
.niov
);
2714 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2716 qemu_iovec_reset(&qiov
);
2717 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2719 print_sg(qiov
.iov
, qiov
.niov
);
2721 /* Loop in case of EINTR */
2723 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2728 } while (len
== -EINTR
&& !pdu
->cancelled
);
2730 /* IO error return the error */
2734 } while (total
< count
&& len
> 0);
2737 err
= pdu_marshal(pdu
, offset
, "d", total
);
2742 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2744 qemu_iovec_destroy(&qiov
);
2748 qemu_iovec_destroy(&qiov_full
);
2749 pdu_complete(pdu
, err
);
2752 static void coroutine_fn
v9fs_create(void *opaque
)
2764 V9fsString extension
;
2766 V9fsPDU
*pdu
= opaque
;
2767 V9fsState
*s
= pdu
->s
;
2769 v9fs_path_init(&path
);
2770 v9fs_string_init(&name
);
2771 v9fs_string_init(&extension
);
2772 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2773 &perm
, &mode
, &extension
);
2777 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2779 if (name_is_illegal(name
.data
)) {
2784 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2789 fidp
= get_fid(pdu
, fid
);
2794 if (fidp
->fid_type
!= P9_FID_NONE
) {
2798 if (perm
& P9_STAT_MODE_DIR
) {
2799 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2800 fidp
->uid
, -1, &stbuf
);
2804 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2808 v9fs_path_write_lock(s
);
2809 v9fs_path_copy(&fidp
->path
, &path
);
2810 v9fs_path_unlock(s
);
2811 err
= v9fs_co_opendir(pdu
, fidp
);
2815 fidp
->fid_type
= P9_FID_DIR
;
2816 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2817 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2818 extension
.data
, -1 , &stbuf
);
2822 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2826 v9fs_path_write_lock(s
);
2827 v9fs_path_copy(&fidp
->path
, &path
);
2828 v9fs_path_unlock(s
);
2829 } else if (perm
& P9_STAT_MODE_LINK
) {
2830 int32_t ofid
= atoi(extension
.data
);
2831 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2832 if (ofidp
== NULL
) {
2836 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2837 put_fid(pdu
, ofidp
);
2841 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2843 fidp
->fid_type
= P9_FID_NONE
;
2846 v9fs_path_write_lock(s
);
2847 v9fs_path_copy(&fidp
->path
, &path
);
2848 v9fs_path_unlock(s
);
2849 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2851 fidp
->fid_type
= P9_FID_NONE
;
2854 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2856 uint32_t major
, minor
;
2859 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2876 nmode
|= perm
& 0777;
2877 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2878 makedev(major
, minor
), nmode
, &stbuf
);
2882 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2886 v9fs_path_write_lock(s
);
2887 v9fs_path_copy(&fidp
->path
, &path
);
2888 v9fs_path_unlock(s
);
2889 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2890 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2891 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2895 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2899 v9fs_path_write_lock(s
);
2900 v9fs_path_copy(&fidp
->path
, &path
);
2901 v9fs_path_unlock(s
);
2902 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2903 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2904 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2908 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2912 v9fs_path_write_lock(s
);
2913 v9fs_path_copy(&fidp
->path
, &path
);
2914 v9fs_path_unlock(s
);
2916 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2917 omode_to_uflags(mode
) | O_CREAT
, perm
, &stbuf
);
2921 fidp
->fid_type
= P9_FID_FILE
;
2922 fidp
->open_flags
= omode_to_uflags(mode
);
2923 if (fidp
->open_flags
& O_EXCL
) {
2925 * We let the host file system do O_EXCL check
2926 * We should not reclaim such fd
2928 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2931 iounit
= get_iounit(pdu
, &fidp
->path
);
2932 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2936 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2941 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2942 qid
.type
, qid
.version
, qid
.path
, iounit
);
2946 pdu_complete(pdu
, err
);
2947 v9fs_string_free(&name
);
2948 v9fs_string_free(&extension
);
2949 v9fs_path_free(&path
);
2952 static void coroutine_fn
v9fs_symlink(void *opaque
)
2954 V9fsPDU
*pdu
= opaque
;
2957 V9fsFidState
*dfidp
;
2965 v9fs_string_init(&name
);
2966 v9fs_string_init(&symname
);
2967 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2971 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2973 if (name_is_illegal(name
.data
)) {
2978 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2983 dfidp
= get_fid(pdu
, dfid
);
2984 if (dfidp
== NULL
) {
2988 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2992 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2996 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3001 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
3002 qid
.type
, qid
.version
, qid
.path
);
3004 put_fid(pdu
, dfidp
);
3006 pdu_complete(pdu
, err
);
3007 v9fs_string_free(&name
);
3008 v9fs_string_free(&symname
);
3011 static void coroutine_fn
v9fs_flush(void *opaque
)
3016 V9fsPDU
*cancel_pdu
= NULL
;
3017 V9fsPDU
*pdu
= opaque
;
3018 V9fsState
*s
= pdu
->s
;
3020 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
3022 pdu_complete(pdu
, err
);
3025 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
3027 if (pdu
->tag
== tag
) {
3028 warn_report("the guest sent a self-referencing 9P flush request");
3030 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
3031 if (cancel_pdu
->tag
== tag
) {
3037 cancel_pdu
->cancelled
= 1;
3039 * Wait for pdu to complete.
3041 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
3042 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
3043 cancel_pdu
->cancelled
= 0;
3044 pdu_free(cancel_pdu
);
3047 pdu_complete(pdu
, 7);
3050 static void coroutine_fn
v9fs_link(void *opaque
)
3052 V9fsPDU
*pdu
= opaque
;
3053 int32_t dfid
, oldfid
;
3054 V9fsFidState
*dfidp
, *oldfidp
;
3059 v9fs_string_init(&name
);
3060 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
3064 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
3066 if (name_is_illegal(name
.data
)) {
3071 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3076 dfidp
= get_fid(pdu
, dfid
);
3077 if (dfidp
== NULL
) {
3082 oldfidp
= get_fid(pdu
, oldfid
);
3083 if (oldfidp
== NULL
) {
3087 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
3091 put_fid(pdu
, oldfidp
);
3093 put_fid(pdu
, dfidp
);
3095 v9fs_string_free(&name
);
3096 pdu_complete(pdu
, err
);
3099 /* Only works with path name based fid */
3100 static void coroutine_fn
v9fs_remove(void *opaque
)
3106 V9fsPDU
*pdu
= opaque
;
3108 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3112 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
3114 fidp
= get_fid(pdu
, fid
);
3119 /* if fs driver is not path based, return EOPNOTSUPP */
3120 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3125 * IF the file is unlinked, we cannot reopen
3126 * the file later. So don't reclaim fd
3128 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
3132 err
= v9fs_co_remove(pdu
, &fidp
->path
);
3137 /* For TREMOVE we need to clunk the fid even on failed remove */
3138 clunk_fid(pdu
->s
, fidp
->fid
);
3141 pdu_complete(pdu
, err
);
3144 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
3148 int32_t dfid
, flags
, rflags
= 0;
3151 V9fsFidState
*dfidp
;
3152 V9fsPDU
*pdu
= opaque
;
3154 v9fs_string_init(&name
);
3155 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3160 if (name_is_illegal(name
.data
)) {
3165 if (!strcmp(".", name
.data
)) {
3170 if (!strcmp("..", name
.data
)) {
3175 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3180 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3181 rflags
|= AT_REMOVEDIR
;
3184 dfidp
= get_fid(pdu
, dfid
);
3185 if (dfidp
== NULL
) {
3190 * IF the file is unlinked, we cannot reopen
3191 * the file later. So don't reclaim fd
3193 v9fs_path_init(&path
);
3194 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3198 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3202 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3207 put_fid(pdu
, dfidp
);
3208 v9fs_path_free(&path
);
3210 pdu_complete(pdu
, err
);
3211 v9fs_string_free(&name
);
3215 /* Only works with path name based fid */
3216 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3222 V9fsFidState
*tfidp
;
3223 V9fsState
*s
= pdu
->s
;
3224 V9fsFidState
*dirfidp
= NULL
;
3225 GHashTableIter iter
;
3228 v9fs_path_init(&new_path
);
3229 if (newdirfid
!= -1) {
3230 dirfidp
= get_fid(pdu
, newdirfid
);
3231 if (dirfidp
== NULL
) {
3234 if (fidp
->fid_type
!= P9_FID_NONE
) {
3238 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3243 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3246 v9fs_path_init(&dir_path
);
3247 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3250 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3251 v9fs_path_free(&dir_path
);
3256 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3262 * Fixup fid's pointing to the old name to
3263 * start pointing to the new name
3265 g_hash_table_iter_init(&iter
, s
->fids
);
3266 while (g_hash_table_iter_next(&iter
, &fid
, (gpointer
*) &tfidp
)) {
3267 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3268 /* replace the name */
3269 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3274 put_fid(pdu
, dirfidp
);
3276 v9fs_path_free(&new_path
);
3280 /* Only works with path name based fid */
3281 static void coroutine_fn
v9fs_rename(void *opaque
)
3289 V9fsPDU
*pdu
= opaque
;
3290 V9fsState
*s
= pdu
->s
;
3292 v9fs_string_init(&name
);
3293 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3298 if (name_is_illegal(name
.data
)) {
3303 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3308 fidp
= get_fid(pdu
, fid
);
3313 if (fidp
->fid_type
!= P9_FID_NONE
) {
3317 /* if fs driver is not path based, return EOPNOTSUPP */
3318 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3322 v9fs_path_write_lock(s
);
3323 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3324 v9fs_path_unlock(s
);
3331 pdu_complete(pdu
, err
);
3332 v9fs_string_free(&name
);
3335 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3336 V9fsString
*old_name
,
3338 V9fsString
*new_name
)
3340 V9fsFidState
*tfidp
;
3341 V9fsPath oldpath
, newpath
;
3342 V9fsState
*s
= pdu
->s
;
3344 GHashTableIter iter
;
3347 v9fs_path_init(&oldpath
);
3348 v9fs_path_init(&newpath
);
3349 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3353 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3359 * Fixup fid's pointing to the old name to
3360 * start pointing to the new name
3362 g_hash_table_iter_init(&iter
, s
->fids
);
3363 while (g_hash_table_iter_next(&iter
, &fid
, (gpointer
*) &tfidp
)) {
3364 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3365 /* replace the name */
3366 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3370 v9fs_path_free(&oldpath
);
3371 v9fs_path_free(&newpath
);
3375 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3376 V9fsString
*old_name
,
3378 V9fsString
*new_name
)
3381 V9fsState
*s
= pdu
->s
;
3382 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3384 olddirfidp
= get_fid(pdu
, olddirfid
);
3385 if (olddirfidp
== NULL
) {
3389 if (newdirfid
!= -1) {
3390 newdirfidp
= get_fid(pdu
, newdirfid
);
3391 if (newdirfidp
== NULL
) {
3396 newdirfidp
= get_fid(pdu
, olddirfid
);
3399 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3400 &newdirfidp
->path
, new_name
);
3404 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3405 /* Only for path based fid we need to do the below fixup */
3406 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3407 &newdirfidp
->path
, new_name
);
3411 put_fid(pdu
, olddirfidp
);
3414 put_fid(pdu
, newdirfidp
);
3419 static void coroutine_fn
v9fs_renameat(void *opaque
)
3423 V9fsPDU
*pdu
= opaque
;
3424 V9fsState
*s
= pdu
->s
;
3425 int32_t olddirfid
, newdirfid
;
3426 V9fsString old_name
, new_name
;
3428 v9fs_string_init(&old_name
);
3429 v9fs_string_init(&new_name
);
3430 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3431 &old_name
, &newdirfid
, &new_name
);
3436 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3441 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3442 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3447 v9fs_path_write_lock(s
);
3448 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3449 &old_name
, newdirfid
, &new_name
);
3450 v9fs_path_unlock(s
);
3456 pdu_complete(pdu
, err
);
3457 v9fs_string_free(&old_name
);
3458 v9fs_string_free(&new_name
);
3461 static void coroutine_fn
v9fs_wstat(void *opaque
)
3470 V9fsPDU
*pdu
= opaque
;
3471 V9fsState
*s
= pdu
->s
;
3473 v9fs_stat_init(&v9stat
);
3474 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3478 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3479 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3481 fidp
= get_fid(pdu
, fid
);
3486 /* do we need to sync the file? */
3487 if (donttouch_stat(&v9stat
)) {
3488 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3491 if (v9stat
.mode
!= -1) {
3493 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3497 v9_mode
= stat_to_v9mode(&stbuf
);
3498 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3499 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3500 /* Attempting to change the type */
3504 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3505 v9mode_to_mode(v9stat
.mode
,
3506 &v9stat
.extension
));
3511 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3512 struct timespec times
[2];
3513 if (v9stat
.atime
!= -1) {
3514 times
[0].tv_sec
= v9stat
.atime
;
3515 times
[0].tv_nsec
= 0;
3517 times
[0].tv_nsec
= UTIME_OMIT
;
3519 if (v9stat
.mtime
!= -1) {
3520 times
[1].tv_sec
= v9stat
.mtime
;
3521 times
[1].tv_nsec
= 0;
3523 times
[1].tv_nsec
= UTIME_OMIT
;
3525 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3530 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3531 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3536 if (v9stat
.name
.size
!= 0) {
3537 v9fs_path_write_lock(s
);
3538 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3539 v9fs_path_unlock(s
);
3544 if (v9stat
.length
!= -1) {
3545 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3554 v9fs_stat_free(&v9stat
);
3555 pdu_complete(pdu
, err
);
3558 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3570 int32_t bsize_factor
;
3573 * compute bsize factor based on host file system block size
3576 bsize_factor
= (s
->msize
- P9_IOHDRSZ
) / stbuf
->f_bsize
;
3577 if (!bsize_factor
) {
3580 f_type
= stbuf
->f_type
;
3581 f_bsize
= stbuf
->f_bsize
;
3582 f_bsize
*= bsize_factor
;
3584 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3585 * adjust(divide) the number of blocks, free blocks and available
3586 * blocks by bsize factor
3588 f_blocks
= stbuf
->f_blocks
/ bsize_factor
;
3589 f_bfree
= stbuf
->f_bfree
/ bsize_factor
;
3590 f_bavail
= stbuf
->f_bavail
/ bsize_factor
;
3591 f_files
= stbuf
->f_files
;
3592 f_ffree
= stbuf
->f_ffree
;
3593 #ifdef CONFIG_DARWIN
3594 fsid_val
= (unsigned int)stbuf
->f_fsid
.val
[0] |
3595 (unsigned long long)stbuf
->f_fsid
.val
[1] << 32;
3596 f_namelen
= NAME_MAX
;
3598 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3599 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3600 f_namelen
= stbuf
->f_namelen
;
3603 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3604 f_type
, f_bsize
, f_blocks
, f_bfree
,
3605 f_bavail
, f_files
, f_ffree
,
3606 fsid_val
, f_namelen
);
3609 static void coroutine_fn
v9fs_statfs(void *opaque
)
3615 struct statfs stbuf
;
3616 V9fsPDU
*pdu
= opaque
;
3617 V9fsState
*s
= pdu
->s
;
3619 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3623 fidp
= get_fid(pdu
, fid
);
3628 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3632 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3640 pdu_complete(pdu
, retval
);
3643 static void coroutine_fn
v9fs_mknod(void *opaque
)
3656 V9fsPDU
*pdu
= opaque
;
3658 v9fs_string_init(&name
);
3659 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3660 &major
, &minor
, &gid
);
3664 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3666 if (name_is_illegal(name
.data
)) {
3671 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3676 fidp
= get_fid(pdu
, fid
);
3681 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3682 makedev(major
, minor
), mode
, &stbuf
);
3686 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3690 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3695 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3696 qid
.type
, qid
.version
, qid
.path
);
3700 pdu_complete(pdu
, err
);
3701 v9fs_string_free(&name
);
3705 * Implement posix byte range locking code
3706 * Server side handling of locking code is very simple, because 9p server in
3707 * QEMU can handle only one client. And most of the lock handling
3708 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3709 * do any thing in * qemu 9p server side lock code path.
3710 * So when a TLOCK request comes, always return success
3712 static void coroutine_fn
v9fs_lock(void *opaque
)
3718 int32_t fid
, err
= 0;
3719 V9fsPDU
*pdu
= opaque
;
3721 v9fs_string_init(&flock
.client_id
);
3722 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3723 &flock
.flags
, &flock
.start
, &flock
.length
,
3724 &flock
.proc_id
, &flock
.client_id
);
3728 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3729 flock
.type
, flock
.start
, flock
.length
);
3732 /* We support only block flag now (that too ignored currently) */
3733 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3737 fidp
= get_fid(pdu
, fid
);
3742 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3746 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3751 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3755 pdu_complete(pdu
, err
);
3756 v9fs_string_free(&flock
.client_id
);
3760 * When a TGETLOCK request comes, always return success because all lock
3761 * handling is done by client's VFS layer.
3763 static void coroutine_fn
v9fs_getlock(void *opaque
)
3769 int32_t fid
, err
= 0;
3770 V9fsPDU
*pdu
= opaque
;
3772 v9fs_string_init(&glock
.client_id
);
3773 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3774 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3779 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3780 glock
.type
, glock
.start
, glock
.length
);
3782 fidp
= get_fid(pdu
, fid
);
3787 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3791 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3792 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3793 glock
.start
, glock
.length
, glock
.proc_id
,
3799 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3800 glock
.length
, glock
.proc_id
);
3804 pdu_complete(pdu
, err
);
3805 v9fs_string_free(&glock
.client_id
);
3808 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3810 V9fsPDU
*pdu
= opaque
;
3821 v9fs_string_init(&name
);
3822 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3826 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3828 if (name_is_illegal(name
.data
)) {
3833 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3838 fidp
= get_fid(pdu
, fid
);
3843 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3847 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3851 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3856 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3857 qid
.type
, qid
.version
, qid
.path
, err
);
3861 pdu_complete(pdu
, err
);
3862 v9fs_string_free(&name
);
3865 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3871 int32_t fid
, newfid
;
3872 V9fsFidState
*file_fidp
;
3873 V9fsFidState
*xattr_fidp
= NULL
;
3874 V9fsPDU
*pdu
= opaque
;
3875 V9fsState
*s
= pdu
->s
;
3877 v9fs_string_init(&name
);
3878 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3882 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3884 file_fidp
= get_fid(pdu
, fid
);
3885 if (file_fidp
== NULL
) {
3889 xattr_fidp
= alloc_fid(s
, newfid
);
3890 if (xattr_fidp
== NULL
) {
3894 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3895 if (!v9fs_string_size(&name
)) {
3897 * listxattr request. Get the size first
3899 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3902 clunk_fid(s
, xattr_fidp
->fid
);
3906 * Read the xattr value
3908 xattr_fidp
->fs
.xattr
.len
= size
;
3909 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3910 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3911 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3913 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3914 xattr_fidp
->fs
.xattr
.value
,
3915 xattr_fidp
->fs
.xattr
.len
);
3917 clunk_fid(s
, xattr_fidp
->fid
);
3921 err
= pdu_marshal(pdu
, offset
, "q", size
);
3928 * specific xattr fid. We check for xattr
3929 * presence also collect the xattr size
3931 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3935 clunk_fid(s
, xattr_fidp
->fid
);
3939 * Read the xattr value
3941 xattr_fidp
->fs
.xattr
.len
= size
;
3942 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3943 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3944 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3946 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3947 &name
, xattr_fidp
->fs
.xattr
.value
,
3948 xattr_fidp
->fs
.xattr
.len
);
3950 clunk_fid(s
, xattr_fidp
->fid
);
3954 err
= pdu_marshal(pdu
, offset
, "q", size
);
3960 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3962 put_fid(pdu
, file_fidp
);
3964 put_fid(pdu
, xattr_fidp
);
3967 pdu_complete(pdu
, err
);
3968 v9fs_string_free(&name
);
3971 #if defined(CONFIG_LINUX)
3972 /* Currently, only Linux has XATTR_SIZE_MAX */
3973 #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX
3974 #elif defined(CONFIG_DARWIN)
3976 * Darwin doesn't seem to define a maximum xattr size in its user
3977 * space header, so manually configure it across platforms as 64k.
3979 * Having no limit at all can lead to QEMU crashing during large g_malloc()
3980 * calls. Because QEMU does not currently support macOS guests, the below
3981 * preliminary solution only works due to its being a reflection of the limit of
3984 #define P9_XATTR_SIZE_MAX 65536
3986 #error Missing definition for P9_XATTR_SIZE_MAX for this host system
3989 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3991 int flags
, rflags
= 0;
3997 V9fsFidState
*file_fidp
;
3998 V9fsFidState
*xattr_fidp
;
3999 V9fsPDU
*pdu
= opaque
;
4001 v9fs_string_init(&name
);
4002 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
4006 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
4008 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
4013 if (flags
& P9_XATTR_CREATE
) {
4014 rflags
|= XATTR_CREATE
;
4017 if (flags
& P9_XATTR_REPLACE
) {
4018 rflags
|= XATTR_REPLACE
;
4021 if (size
> P9_XATTR_SIZE_MAX
) {
4026 file_fidp
= get_fid(pdu
, fid
);
4027 if (file_fidp
== NULL
) {
4031 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
4036 /* Make the file fid point to xattr */
4037 xattr_fidp
= file_fidp
;
4038 xattr_fidp
->fid_type
= P9_FID_XATTR
;
4039 xattr_fidp
->fs
.xattr
.copied_len
= 0;
4040 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
4041 xattr_fidp
->fs
.xattr
.len
= size
;
4042 xattr_fidp
->fs
.xattr
.flags
= rflags
;
4043 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
4044 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
4045 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
4048 put_fid(pdu
, file_fidp
);
4050 pdu_complete(pdu
, err
);
4051 v9fs_string_free(&name
);
4054 static void coroutine_fn
v9fs_readlink(void *opaque
)
4056 V9fsPDU
*pdu
= opaque
;
4063 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
4067 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
4068 fidp
= get_fid(pdu
, fid
);
4074 v9fs_string_init(&target
);
4075 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
4079 err
= pdu_marshal(pdu
, offset
, "s", &target
);
4081 v9fs_string_free(&target
);
4085 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
4086 v9fs_string_free(&target
);
4090 pdu_complete(pdu
, err
);
4093 static CoroutineEntry
*pdu_co_handlers
[] = {
4094 [P9_TREADDIR
] = v9fs_readdir
,
4095 [P9_TSTATFS
] = v9fs_statfs
,
4096 [P9_TGETATTR
] = v9fs_getattr
,
4097 [P9_TSETATTR
] = v9fs_setattr
,
4098 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
4099 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
4100 [P9_TMKNOD
] = v9fs_mknod
,
4101 [P9_TRENAME
] = v9fs_rename
,
4102 [P9_TLOCK
] = v9fs_lock
,
4103 [P9_TGETLOCK
] = v9fs_getlock
,
4104 [P9_TRENAMEAT
] = v9fs_renameat
,
4105 [P9_TREADLINK
] = v9fs_readlink
,
4106 [P9_TUNLINKAT
] = v9fs_unlinkat
,
4107 [P9_TMKDIR
] = v9fs_mkdir
,
4108 [P9_TVERSION
] = v9fs_version
,
4109 [P9_TLOPEN
] = v9fs_open
,
4110 [P9_TATTACH
] = v9fs_attach
,
4111 [P9_TSTAT
] = v9fs_stat
,
4112 [P9_TWALK
] = v9fs_walk
,
4113 [P9_TCLUNK
] = v9fs_clunk
,
4114 [P9_TFSYNC
] = v9fs_fsync
,
4115 [P9_TOPEN
] = v9fs_open
,
4116 [P9_TREAD
] = v9fs_read
,
4118 [P9_TAUTH
] = v9fs_auth
,
4120 [P9_TFLUSH
] = v9fs_flush
,
4121 [P9_TLINK
] = v9fs_link
,
4122 [P9_TSYMLINK
] = v9fs_symlink
,
4123 [P9_TCREATE
] = v9fs_create
,
4124 [P9_TLCREATE
] = v9fs_lcreate
,
4125 [P9_TWRITE
] = v9fs_write
,
4126 [P9_TWSTAT
] = v9fs_wstat
,
4127 [P9_TREMOVE
] = v9fs_remove
,
4130 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
4132 V9fsPDU
*pdu
= opaque
;
4133 pdu_complete(pdu
, -EOPNOTSUPP
);
4136 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
4138 V9fsPDU
*pdu
= opaque
;
4139 pdu_complete(pdu
, -EROFS
);
4142 static inline bool is_read_only_op(V9fsPDU
*pdu
)
4169 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
4172 CoroutineEntry
*handler
;
4173 V9fsState
*s
= pdu
->s
;
4175 pdu
->size
= le32_to_cpu(hdr
->size_le
);
4177 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
4179 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
4180 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
4181 handler
= v9fs_op_not_supp
;
4182 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4183 handler
= v9fs_fs_ro
;
4185 handler
= pdu_co_handlers
[pdu
->id
];
4188 qemu_co_queue_init(&pdu
->complete
);
4189 co
= qemu_coroutine_create(handler
, pdu
);
4190 qemu_coroutine_enter(co
);
4193 /* Returns 0 on success, 1 on failure. */
4194 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4204 assert(!s
->transport
);
4207 /* initialize pdu allocator */
4208 QLIST_INIT(&s
->free_list
);
4209 QLIST_INIT(&s
->active_list
);
4210 for (i
= 0; i
< MAX_REQ
; i
++) {
4211 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4216 v9fs_path_init(&path
);
4218 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4221 /* We don't have a fsdev identified by fsdev_id */
4222 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4224 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4228 if (!s
->fsconf
.tag
) {
4229 /* we haven't specified a mount_tag */
4230 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4231 s
->fsconf
.fsdev_id
);
4235 s
->ctx
.export_flags
= fse
->export_flags
;
4236 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4237 s
->ctx
.exops
.get_st_gen
= NULL
;
4238 len
= strlen(s
->fsconf
.tag
);
4239 if (len
> MAX_TAG_LEN
- 1) {
4240 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4241 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4245 s
->tag
= g_strdup(s
->fsconf
.tag
);
4250 s
->ctx
.fmode
= fse
->fmode
;
4251 s
->ctx
.dmode
= fse
->dmode
;
4253 s
->fids
= g_hash_table_new(NULL
, NULL
);
4254 qemu_co_rwlock_init(&s
->rename_lock
);
4256 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4257 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4258 s
->fsconf
.fsdev_id
);
4263 * Check details of export path, We need to use fs driver
4264 * call back to do that. Since we are in the init path, we don't
4265 * use co-routines here.
4267 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4269 "error in converting name to path %s", strerror(errno
));
4272 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4273 error_setg(errp
, "share path %s does not exist", fse
->path
);
4275 } else if (!S_ISDIR(stat
.st_mode
)) {
4276 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4280 s
->dev_id
= stat
.st_dev
;
4282 /* init inode remapping : */
4283 /* hash table for variable length inode suffixes */
4284 qpd_table_init(&s
->qpd_table
);
4285 /* hash table for slow/full inode remapping (most users won't need it) */
4286 qpf_table_init(&s
->qpf_table
);
4287 /* hash table for quick inode remapping */
4288 qpp_table_init(&s
->qpp_table
);
4290 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4291 s
->qp_fullpath_next
= 1;
4293 s
->ctx
.fst
= &fse
->fst
;
4294 fsdev_throttle_init(s
->ctx
.fst
);
4299 v9fs_device_unrealize_common(s
);
4301 v9fs_path_free(&path
);
4305 void v9fs_device_unrealize_common(V9fsState
*s
)
4307 if (s
->ops
&& s
->ops
->cleanup
) {
4308 s
->ops
->cleanup(&s
->ctx
);
4311 fsdev_throttle_cleanup(s
->ctx
.fst
);
4314 g_hash_table_destroy(s
->fids
);
4318 qp_table_destroy(&s
->qpd_table
);
4319 qp_table_destroy(&s
->qpp_table
);
4320 qp_table_destroy(&s
->qpf_table
);
4321 g_free(s
->ctx
.fs_root
);
4324 typedef struct VirtfsCoResetData
{
4327 } VirtfsCoResetData
;
4329 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4331 VirtfsCoResetData
*data
= opaque
;
4333 virtfs_reset(&data
->pdu
);
4337 void v9fs_reset(V9fsState
*s
)
4339 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4342 while (!QLIST_EMPTY(&s
->active_list
)) {
4343 aio_poll(qemu_get_aio_context(), true);
4346 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4347 qemu_coroutine_enter(co
);
4349 while (!data
.done
) {
4350 aio_poll(qemu_get_aio_context(), true);
4354 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4357 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4358 error_report("Failed to get the resource limit");
4361 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/ 3);
4362 open_fd_rc
= rlim
.rlim_cur
/ 2;