4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
19 #include "qemu/osdep.h"
20 #include <glib/gprintf.h>
21 #include "hw/virtio/virtio.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/sockets.h"
27 #include "virtio-9p.h"
28 #include "fsdev/qemu-fsdev.h"
32 #include "migration/blocker.h"
33 #include "qemu/xxhash.h"
35 #include <linux/limits.h>
39 static int open_fd_rc
;
53 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
59 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
65 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
71 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
77 static int omode_to_uflags(int8_t mode
)
100 if (mode
& Oappend
) {
111 typedef struct DotlOpenflagMap
{
116 static int dotl_to_open_flags(int flags
)
120 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
121 * and P9_DOTL_NOACCESS
123 int oflags
= flags
& O_ACCMODE
;
125 DotlOpenflagMap dotl_oflag_map
[] = {
126 { P9_DOTL_CREATE
, O_CREAT
},
127 { P9_DOTL_EXCL
, O_EXCL
},
128 { P9_DOTL_NOCTTY
, O_NOCTTY
},
129 { P9_DOTL_TRUNC
, O_TRUNC
},
130 { P9_DOTL_APPEND
, O_APPEND
},
131 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
132 { P9_DOTL_DSYNC
, O_DSYNC
},
133 { P9_DOTL_FASYNC
, FASYNC
},
134 { P9_DOTL_DIRECT
, O_DIRECT
},
135 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
136 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
137 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
138 { P9_DOTL_NOATIME
, O_NOATIME
},
139 { P9_DOTL_SYNC
, O_SYNC
},
142 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
143 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
144 oflags
|= dotl_oflag_map
[i
].open_flag
;
151 void cred_init(FsCred
*credp
)
159 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
163 * Filter the client open flags
165 flags
= dotl_to_open_flags(oflags
);
166 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
168 * Ignore direct disk access hint until the server supports it.
174 void v9fs_path_init(V9fsPath
*path
)
180 void v9fs_path_free(V9fsPath
*path
)
188 void GCC_FMT_ATTR(2, 3)
189 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
193 v9fs_path_free(path
);
196 /* Bump the size for including terminating NULL */
197 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
201 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
204 dst
->size
= src
->size
;
205 dst
->data
= g_memdup(src
->data
, src
->size
);
208 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
209 const char *name
, V9fsPath
*path
)
212 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
220 * Return TRUE if s1 is an ancestor of s2.
222 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
223 * As a special case, We treat s1 as ancestor of s2 if they are same!
225 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
227 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
228 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
235 static size_t v9fs_string_size(V9fsString
*str
)
241 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
242 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
245 if (f
->fid_type
== P9_FID_FILE
) {
246 if (f
->fs
.fd
== -1) {
248 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
249 } while (err
== -EINTR
&& !pdu
->cancelled
);
251 } else if (f
->fid_type
== P9_FID_DIR
) {
252 if (f
->fs
.dir
.stream
== NULL
) {
254 err
= v9fs_co_opendir(pdu
, f
);
255 } while (err
== -EINTR
&& !pdu
->cancelled
);
261 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
265 V9fsState
*s
= pdu
->s
;
267 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
271 * Update the fid ref upfront so that
272 * we don't get reclaimed when we yield
277 * check whether we need to reopen the
278 * file. We might have closed the fd
279 * while trying to free up some file
282 err
= v9fs_reopen_fid(pdu
, f
);
288 * Mark the fid as referenced so that the LRU
289 * reclaim won't close the file descriptor
291 f
->flags
|= FID_REFERENCED
;
298 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
302 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
303 /* If fid is already there return NULL */
309 f
= g_malloc0(sizeof(V9fsFidState
));
311 f
->fid_type
= P9_FID_NONE
;
314 * Mark the fid as referenced so that the LRU
315 * reclaim won't close the file descriptor
317 f
->flags
|= FID_REFERENCED
;
318 QSIMPLEQ_INSERT_TAIL(&s
->fid_list
, f
, next
);
320 v9fs_readdir_init(s
->proto_version
, &f
->fs
.dir
);
321 v9fs_readdir_init(s
->proto_version
, &f
->fs_reclaim
.dir
);
326 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
330 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
331 /* getxattr/listxattr fid */
335 * if this is fid for setxattr. clunk should
336 * result in setxattr localcall
338 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
339 /* clunk after partial write */
343 if (fidp
->fs
.xattr
.len
) {
344 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
345 fidp
->fs
.xattr
.value
,
347 fidp
->fs
.xattr
.flags
);
349 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
352 v9fs_string_free(&fidp
->fs
.xattr
.name
);
354 g_free(fidp
->fs
.xattr
.value
);
358 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
362 if (fidp
->fid_type
== P9_FID_FILE
) {
363 /* If we reclaimed the fd no need to close */
364 if (fidp
->fs
.fd
!= -1) {
365 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
367 } else if (fidp
->fid_type
== P9_FID_DIR
) {
368 if (fidp
->fs
.dir
.stream
!= NULL
) {
369 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
371 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
372 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
374 v9fs_path_free(&fidp
->path
);
379 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
384 * Don't free the fid if it is in reclaim list
386 if (!fidp
->ref
&& fidp
->clunked
) {
387 if (fidp
->fid
== pdu
->s
->root_fid
) {
389 * if the clunked fid is root fid then we
390 * have unmounted the fs on the client side.
391 * delete the migration blocker. Ideally, this
392 * should be hooked to transport close notification
394 if (pdu
->s
->migration_blocker
) {
395 migrate_del_blocker(pdu
->s
->migration_blocker
);
396 error_free(pdu
->s
->migration_blocker
);
397 pdu
->s
->migration_blocker
= NULL
;
400 return free_fid(pdu
, fidp
);
405 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
409 QSIMPLEQ_FOREACH(fidp
, &s
->fid_list
, next
) {
410 if (fidp
->fid
== fid
) {
411 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
412 fidp
->clunked
= true;
419 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
421 int reclaim_count
= 0;
422 V9fsState
*s
= pdu
->s
;
424 QSLIST_HEAD(, V9fsFidState
) reclaim_list
=
425 QSLIST_HEAD_INITIALIZER(reclaim_list
);
427 QSIMPLEQ_FOREACH(f
, &s
->fid_list
, next
) {
429 * Unlink fids cannot be reclaimed. Check
430 * for them and skip them. Also skip fids
431 * currently being operated on.
433 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
437 * if it is a recently referenced fid
438 * we leave the fid untouched and clear the
439 * reference bit. We come back to it later
440 * in the next iteration. (a simple LRU without
441 * moving list elements around)
443 if (f
->flags
& FID_REFERENCED
) {
444 f
->flags
&= ~FID_REFERENCED
;
448 * Add fids to reclaim list.
450 if (f
->fid_type
== P9_FID_FILE
) {
451 if (f
->fs
.fd
!= -1) {
453 * Up the reference count so that
454 * a clunk request won't free this fid
457 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
458 f
->fs_reclaim
.fd
= f
->fs
.fd
;
462 } else if (f
->fid_type
== P9_FID_DIR
) {
463 if (f
->fs
.dir
.stream
!= NULL
) {
465 * Up the reference count so that
466 * a clunk request won't free this fid
469 QSLIST_INSERT_HEAD(&reclaim_list
, f
, reclaim_next
);
470 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
471 f
->fs
.dir
.stream
= NULL
;
475 if (reclaim_count
>= open_fd_rc
) {
480 * Now close the fid in reclaim list. Free them if they
481 * are already clunked.
483 while (!QSLIST_EMPTY(&reclaim_list
)) {
484 f
= QSLIST_FIRST(&reclaim_list
);
485 QSLIST_REMOVE(&reclaim_list
, f
, V9fsFidState
, reclaim_next
);
486 if (f
->fid_type
== P9_FID_FILE
) {
487 v9fs_co_close(pdu
, &f
->fs_reclaim
);
488 } else if (f
->fid_type
== P9_FID_DIR
) {
489 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
492 * Now drop the fid reference, free it
499 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
502 V9fsState
*s
= pdu
->s
;
503 V9fsFidState
*fidp
, *fidp_next
;
505 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
511 * v9fs_reopen_fid() can yield : a reference on the fid must be held
512 * to ensure its pointer remains valid and we can safely pass it to
513 * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so
514 * we must keep a reference on the next fid as well. So the logic here
515 * is to get a reference on a fid and only put it back during the next
516 * iteration after we could get a reference on the next fid. Start with
519 for (fidp
->ref
++; fidp
; fidp
= fidp_next
) {
520 if (fidp
->path
.size
== path
->size
&&
521 !memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
522 /* Mark the fid non reclaimable. */
523 fidp
->flags
|= FID_NON_RECLAIMABLE
;
525 /* reopen the file/dir if already closed */
526 err
= v9fs_reopen_fid(pdu
, fidp
);
533 fidp_next
= QSIMPLEQ_NEXT(fidp
, next
);
537 * Ensure the next fid survives a potential clunk request during
538 * put_fid() below and v9fs_reopen_fid() in the next iteration.
543 /* We're done with this fid */
550 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
552 V9fsState
*s
= pdu
->s
;
556 while (!QSIMPLEQ_EMPTY(&s
->fid_list
)) {
558 fidp
= QSIMPLEQ_FIRST(&s
->fid_list
);
562 QSIMPLEQ_REMOVE(&s
->fid_list
, fidp
, V9fsFidState
, next
);
563 fidp
->clunked
= true;
569 #define P9_QID_TYPE_DIR 0x80
570 #define P9_QID_TYPE_SYMLINK 0x02
572 #define P9_STAT_MODE_DIR 0x80000000
573 #define P9_STAT_MODE_APPEND 0x40000000
574 #define P9_STAT_MODE_EXCL 0x20000000
575 #define P9_STAT_MODE_MOUNT 0x10000000
576 #define P9_STAT_MODE_AUTH 0x08000000
577 #define P9_STAT_MODE_TMP 0x04000000
578 #define P9_STAT_MODE_SYMLINK 0x02000000
579 #define P9_STAT_MODE_LINK 0x01000000
580 #define P9_STAT_MODE_DEVICE 0x00800000
581 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
582 #define P9_STAT_MODE_SOCKET 0x00100000
583 #define P9_STAT_MODE_SETUID 0x00080000
584 #define P9_STAT_MODE_SETGID 0x00040000
585 #define P9_STAT_MODE_SETVTX 0x00010000
587 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
588 P9_STAT_MODE_SYMLINK | \
589 P9_STAT_MODE_LINK | \
590 P9_STAT_MODE_DEVICE | \
591 P9_STAT_MODE_NAMED_PIPE | \
594 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
595 static inline uint8_t mirror8bit(uint8_t byte
)
597 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
600 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
601 static inline uint64_t mirror64bit(uint64_t value
)
603 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
604 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
605 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
606 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
607 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
608 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
609 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
610 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
614 * @brief Parameter k for the Exponential Golomb algorihm to be used.
616 * The smaller this value, the smaller the minimum bit count for the Exp.
617 * Golomb generated affixes will be (at lowest index) however for the
618 * price of having higher maximum bit count of generated affixes (at highest
619 * index). Likewise increasing this parameter yields in smaller maximum bit
620 * count for the price of having higher minimum bit count.
622 * In practice that means: a good value for k depends on the expected amount
623 * of devices to be exposed by one export. For a small amount of devices k
624 * should be small, for a large amount of devices k might be increased
625 * instead. The default of k=0 should be fine for most users though.
627 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
628 * k should not change as long as guest is still running! Because that would
629 * cause completely different inode numbers to be generated on guest.
631 #define EXP_GOLOMB_K 0
634 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
636 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
637 * with growing length and with the mathematical property of being
638 * "prefix-free". The latter means the generated prefixes can be prepended
639 * in front of arbitrary numbers and the resulting concatenated numbers are
640 * guaranteed to be always unique.
642 * This is a minor adjustment to the original Exp. Golomb algorithm in the
643 * sense that lowest allowed index (@param n) starts with 1, not with zero.
645 * @param n - natural number (or index) of the prefix to be generated
647 * @param k - parameter k of Exp. Golomb algorithm to be used
648 * (see comment on EXP_GOLOMB_K macro for details about k)
650 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
652 const uint64_t value
= n
+ (1 << k
) - 1;
653 const int bits
= (int) log2(value
) + 1;
654 return (VariLenAffix
) {
655 .type
= AffixType_Prefix
,
657 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
662 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
664 * Simply mirror all bits of the affix value, for the purpose to preserve
665 * respectively the mathematical "prefix-free" or "suffix-free" property
666 * after the conversion.
668 * If a passed prefix is suitable to create unique numbers, then the
669 * returned suffix is suitable to create unique numbers as well (and vice
672 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
674 return (VariLenAffix
) {
676 (affix
->type
== AffixType_Suffix
) ?
677 AffixType_Prefix
: AffixType_Suffix
,
679 mirror64bit(affix
->value
) >>
680 ((sizeof(affix
->value
) * 8) - affix
->bits
),
686 * @brief Generates suffix numbers with "suffix-free" property.
688 * This is just a wrapper function on top of the Exp. Golomb algorithm.
690 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
691 * this function converts the Exp. Golomb prefixes into appropriate suffixes
692 * which are still suitable for generating unique numbers.
694 * @param n - natural number (or index) of the suffix to be generated
697 static VariLenAffix
affixForIndex(uint64_t index
)
700 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
701 return invertAffix(&prefix
); /* convert prefix to suffix */
704 /* creative abuse of tb_hash_func7, which is based on xxhash */
705 static uint32_t qpp_hash(QppEntry e
)
707 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
710 static uint32_t qpf_hash(QpfEntry e
)
712 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
715 static bool qpd_cmp_func(const void *obj
, const void *userp
)
717 const QpdEntry
*e1
= obj
, *e2
= userp
;
718 return e1
->dev
== e2
->dev
;
721 static bool qpp_cmp_func(const void *obj
, const void *userp
)
723 const QppEntry
*e1
= obj
, *e2
= userp
;
724 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
727 static bool qpf_cmp_func(const void *obj
, const void *userp
)
729 const QpfEntry
*e1
= obj
, *e2
= userp
;
730 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
733 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
738 static void qp_table_destroy(struct qht
*ht
)
740 if (!ht
|| !ht
->map
) {
743 qht_iter(ht
, qp_table_remove
, NULL
);
747 static void qpd_table_init(struct qht
*ht
)
749 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
752 static void qpp_table_init(struct qht
*ht
)
754 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
757 static void qpf_table_init(struct qht
*ht
)
759 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
763 * Returns how many (high end) bits of inode numbers of the passed fs
764 * device shall be used (in combination with the device number) to
765 * generate hash values for qpp_table entries.
767 * This function is required if variable length suffixes are used for inode
768 * number mapping on guest level. Since a device may end up having multiple
769 * entries in qpp_table, each entry most probably with a different suffix
770 * length, we thus need this function in conjunction with qpd_table to
771 * "agree" about a fix amount of bits (per device) to be always used for
772 * generating hash values for the purpose of accessing qpp_table in order
773 * get consistent behaviour when accessing qpp_table.
775 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
783 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
785 val
= g_malloc0(sizeof(QpdEntry
));
787 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
788 val
->prefix_bits
= affix
.bits
;
789 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
790 pdu
->s
->qp_ndevices
++;
792 return val
->prefix_bits
;
796 * @brief Slow / full mapping host inode nr -> guest inode nr.
798 * This function performs a slower and much more costly remapping of an
799 * original file inode number on host to an appropriate different inode
800 * number on guest. For every (dev, inode) combination on host a new
801 * sequential number is generated, cached and exposed as inode number on
804 * This is just a "last resort" fallback solution if the much faster/cheaper
805 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
806 * expected ever to be used at all though.
808 * @see qid_path_suffixmap() for details
811 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
815 .dev
= stbuf
->st_dev
,
818 uint32_t hash
= qpf_hash(lookup
);
821 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
824 if (pdu
->s
->qp_fullpath_next
== 0) {
825 /* no more files can be mapped :'( */
827 "9p: No more prefixes available for remapping inodes from "
833 val
= g_malloc0(sizeof(QppEntry
));
836 /* new unique inode and device combo */
837 affix
= affixForIndex(
838 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
840 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
841 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
842 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
850 * @brief Quick mapping host inode nr -> guest inode nr.
852 * This function performs quick remapping of an original file inode number
853 * on host to an appropriate different inode number on guest. This remapping
854 * of inodes is required to avoid inode nr collisions on guest which would
855 * happen if the 9p export contains more than 1 exported file system (or
856 * more than 1 file system data set), because unlike on host level where the
857 * files would have different device nrs, all files exported by 9p would
858 * share the same device nr on guest (the device nr of the virtual 9p device
861 * Inode remapping is performed by chopping off high end bits of the original
862 * inode number from host, shifting the result upwards and then assigning a
863 * generated suffix number for the low end bits, where the same suffix number
864 * will be shared by all inodes with the same device id AND the same high end
865 * bits that have been chopped off. That approach utilizes the fact that inode
866 * numbers very likely share the same high end bits (i.e. due to their common
867 * sequential generation by file systems) and hence we only have to generate
868 * and track a very limited amount of suffixes in practice due to that.
870 * We generate variable size suffixes for that purpose. The 1st generated
871 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
872 * the original inode number. The subsequent suffixes being generated will
873 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
874 * generated will have 3 bits and hence we have to chop off 3 bits from their
875 * original inodes, and so on. That approach of using variable length suffixes
876 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
877 * limited amount of devices are shared by the same export (e.g. typically
878 * less than 2 dozen devices per 9p export), so in practice we need to chop
879 * off less bits than with fixed size prefixes and yet are flexible to add
880 * new devices at runtime below host's export directory at any time without
881 * having to reboot guest nor requiring to reconfigure guest for that. And due
882 * to the very limited amount of original high end bits that we chop off that
883 * way, the total amount of suffixes we need to generate is less than by using
884 * fixed size prefixes and hence it also improves performance of the inode
885 * remapping algorithm, and finally has the nice side effect that the inode
886 * numbers on guest will be much smaller & human friendly. ;-)
888 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
891 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
893 .dev
= stbuf
->st_dev
,
894 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
896 uint32_t hash
= qpp_hash(lookup
);
898 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
901 if (pdu
->s
->qp_affix_next
== 0) {
902 /* we ran out of affixes */
904 "9p: Potential degraded performance of inode remapping"
909 val
= g_malloc0(sizeof(QppEntry
));
912 /* new unique inode affix and device combo */
913 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
914 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
915 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
917 /* assuming generated affix to be suffix type, not prefix */
918 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
922 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
927 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
928 /* map inode+device to qid path (fast path) */
929 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
930 if (err
== -ENFILE
) {
931 /* fast path didn't work, fall back to full map */
932 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
938 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
939 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
941 "9p: Multiple devices detected in same VirtFS export. "
942 "Access of guest to additional devices is (partly) "
943 "denied due to virtfs option 'multidevs=forbid' being "
949 "9p: Multiple devices detected in same VirtFS export, "
950 "which might lead to file ID collisions and severe "
951 "misbehaviours on guest! You should either use a "
952 "separate export for each device shared from host or "
953 "use virtfs option 'multidevs=remap'!"
957 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
958 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
959 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
962 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
964 if (S_ISDIR(stbuf
->st_mode
)) {
965 qidp
->type
|= P9_QID_TYPE_DIR
;
967 if (S_ISLNK(stbuf
->st_mode
)) {
968 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
974 V9fsPDU
*pdu_alloc(V9fsState
*s
)
978 if (!QLIST_EMPTY(&s
->free_list
)) {
979 pdu
= QLIST_FIRST(&s
->free_list
);
980 QLIST_REMOVE(pdu
, next
);
981 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
986 void pdu_free(V9fsPDU
*pdu
)
988 V9fsState
*s
= pdu
->s
;
990 g_assert(!pdu
->cancelled
);
991 QLIST_REMOVE(pdu
, next
);
992 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
995 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
997 int8_t id
= pdu
->id
+ 1; /* Response */
998 V9fsState
*s
= pdu
->s
;
1002 * The 9p spec requires that successfully cancelled pdus receive no reply.
1003 * Sending a reply would confuse clients because they would
1004 * assume that any EINTR is the actual result of the operation,
1005 * rather than a consequence of the cancellation. However, if
1006 * the operation completed (succesfully or with an error other
1007 * than caused be cancellation), we do send out that reply, both
1008 * for efficiency and to avoid confusing the rest of the state machine
1009 * that assumes passing a non-error here will mean a successful
1010 * transmission of the reply.
1012 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1014 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1023 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1026 str
.data
= strerror(err
);
1027 str
.size
= strlen(str
.data
);
1029 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1037 ret
= pdu_marshal(pdu
, len
, "d", err
);
1043 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1046 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1049 /* fill out the header */
1050 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1054 /* keep these in sync */
1059 pdu
->s
->transport
->push_and_notify(pdu
);
1061 /* Now wakeup anybody waiting in flush for this request */
1062 if (!qemu_co_queue_next(&pdu
->complete
)) {
1067 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1072 if (mode
& P9_STAT_MODE_DIR
) {
1076 if (mode
& P9_STAT_MODE_SYMLINK
) {
1079 if (mode
& P9_STAT_MODE_SOCKET
) {
1082 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1085 if (mode
& P9_STAT_MODE_DEVICE
) {
1086 if (extension
->size
&& extension
->data
[0] == 'c') {
1093 if (!(ret
& ~0777)) {
1097 if (mode
& P9_STAT_MODE_SETUID
) {
1100 if (mode
& P9_STAT_MODE_SETGID
) {
1103 if (mode
& P9_STAT_MODE_SETVTX
) {
1110 static int donttouch_stat(V9fsStat
*stat
)
1112 if (stat
->type
== -1 &&
1114 stat
->qid
.type
== 0xff &&
1115 stat
->qid
.version
== (uint32_t) -1 &&
1116 stat
->qid
.path
== (uint64_t) -1 &&
1118 stat
->atime
== -1 &&
1119 stat
->mtime
== -1 &&
1120 stat
->length
== -1 &&
1125 stat
->n_uid
== -1 &&
1126 stat
->n_gid
== -1 &&
1127 stat
->n_muid
== -1) {
1134 static void v9fs_stat_init(V9fsStat
*stat
)
1136 v9fs_string_init(&stat
->name
);
1137 v9fs_string_init(&stat
->uid
);
1138 v9fs_string_init(&stat
->gid
);
1139 v9fs_string_init(&stat
->muid
);
1140 v9fs_string_init(&stat
->extension
);
1143 static void v9fs_stat_free(V9fsStat
*stat
)
1145 v9fs_string_free(&stat
->name
);
1146 v9fs_string_free(&stat
->uid
);
1147 v9fs_string_free(&stat
->gid
);
1148 v9fs_string_free(&stat
->muid
);
1149 v9fs_string_free(&stat
->extension
);
1152 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1156 mode
= stbuf
->st_mode
& 0777;
1157 if (S_ISDIR(stbuf
->st_mode
)) {
1158 mode
|= P9_STAT_MODE_DIR
;
1161 if (S_ISLNK(stbuf
->st_mode
)) {
1162 mode
|= P9_STAT_MODE_SYMLINK
;
1165 if (S_ISSOCK(stbuf
->st_mode
)) {
1166 mode
|= P9_STAT_MODE_SOCKET
;
1169 if (S_ISFIFO(stbuf
->st_mode
)) {
1170 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1173 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1174 mode
|= P9_STAT_MODE_DEVICE
;
1177 if (stbuf
->st_mode
& S_ISUID
) {
1178 mode
|= P9_STAT_MODE_SETUID
;
1181 if (stbuf
->st_mode
& S_ISGID
) {
1182 mode
|= P9_STAT_MODE_SETGID
;
1185 if (stbuf
->st_mode
& S_ISVTX
) {
1186 mode
|= P9_STAT_MODE_SETVTX
;
1192 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1193 const char *basename
,
1194 const struct stat
*stbuf
,
1199 memset(v9stat
, 0, sizeof(*v9stat
));
1201 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1205 v9stat
->mode
= stat_to_v9mode(stbuf
);
1206 v9stat
->atime
= stbuf
->st_atime
;
1207 v9stat
->mtime
= stbuf
->st_mtime
;
1208 v9stat
->length
= stbuf
->st_size
;
1210 v9fs_string_free(&v9stat
->uid
);
1211 v9fs_string_free(&v9stat
->gid
);
1212 v9fs_string_free(&v9stat
->muid
);
1214 v9stat
->n_uid
= stbuf
->st_uid
;
1215 v9stat
->n_gid
= stbuf
->st_gid
;
1218 v9fs_string_free(&v9stat
->extension
);
1220 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1221 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1225 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1226 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1227 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1228 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1229 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1230 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1231 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1234 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1237 v9fs_string_size(&v9stat
->name
) +
1238 v9fs_string_size(&v9stat
->uid
) +
1239 v9fs_string_size(&v9stat
->gid
) +
1240 v9fs_string_size(&v9stat
->muid
) +
1241 v9fs_string_size(&v9stat
->extension
);
1245 #define P9_STATS_MODE 0x00000001ULL
1246 #define P9_STATS_NLINK 0x00000002ULL
1247 #define P9_STATS_UID 0x00000004ULL
1248 #define P9_STATS_GID 0x00000008ULL
1249 #define P9_STATS_RDEV 0x00000010ULL
1250 #define P9_STATS_ATIME 0x00000020ULL
1251 #define P9_STATS_MTIME 0x00000040ULL
1252 #define P9_STATS_CTIME 0x00000080ULL
1253 #define P9_STATS_INO 0x00000100ULL
1254 #define P9_STATS_SIZE 0x00000200ULL
1255 #define P9_STATS_BLOCKS 0x00000400ULL
1257 #define P9_STATS_BTIME 0x00000800ULL
1258 #define P9_STATS_GEN 0x00001000ULL
1259 #define P9_STATS_DATA_VERSION 0x00002000ULL
1261 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1262 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1265 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1266 V9fsStatDotl
*v9lstat
)
1268 memset(v9lstat
, 0, sizeof(*v9lstat
));
1270 v9lstat
->st_mode
= stbuf
->st_mode
;
1271 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1272 v9lstat
->st_uid
= stbuf
->st_uid
;
1273 v9lstat
->st_gid
= stbuf
->st_gid
;
1274 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1275 v9lstat
->st_size
= stbuf
->st_size
;
1276 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1277 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1278 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1279 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1280 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1281 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1282 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1283 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1284 /* Currently we only support BASIC fields in stat */
1285 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1287 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1290 static void print_sg(struct iovec
*sg
, int cnt
)
1294 printf("sg[%d]: {", cnt
);
1295 for (i
= 0; i
< cnt
; i
++) {
1299 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1304 /* Will call this only for path name based fid */
1305 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1308 v9fs_path_init(&str
);
1309 v9fs_path_copy(&str
, dst
);
1310 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1311 v9fs_path_free(&str
);
1314 static inline bool is_ro_export(FsContext
*ctx
)
1316 return ctx
->export_flags
& V9FS_RDONLY
;
1319 static void coroutine_fn
v9fs_version(void *opaque
)
1322 V9fsPDU
*pdu
= opaque
;
1323 V9fsState
*s
= pdu
->s
;
1327 v9fs_string_init(&version
);
1328 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1332 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1336 if (!strcmp(version
.data
, "9P2000.u")) {
1337 s
->proto_version
= V9FS_PROTO_2000U
;
1338 } else if (!strcmp(version
.data
, "9P2000.L")) {
1339 s
->proto_version
= V9FS_PROTO_2000L
;
1341 v9fs_string_sprintf(&version
, "unknown");
1342 /* skip min. msize check, reporting invalid version has priority */
1346 if (s
->msize
< P9_MIN_MSIZE
) {
1349 "9pfs: Client requested msize < minimum msize ("
1350 stringify(P9_MIN_MSIZE
) ") supported by this server."
1355 /* 8192 is the default msize of Linux clients */
1356 if (s
->msize
<= 8192 && !(s
->ctx
.export_flags
& V9FS_NO_PERF_WARN
)) {
1358 "9p: degraded performance: a reasonable high msize should be "
1359 "chosen on client/guest side (chosen msize is <= 8192). See "
1360 "https://wiki.qemu.org/Documentation/9psetup#msize for details."
1365 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1370 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1372 pdu_complete(pdu
, err
);
1373 v9fs_string_free(&version
);
1376 static void coroutine_fn
v9fs_attach(void *opaque
)
1378 V9fsPDU
*pdu
= opaque
;
1379 V9fsState
*s
= pdu
->s
;
1380 int32_t fid
, afid
, n_uname
;
1381 V9fsString uname
, aname
;
1388 v9fs_string_init(&uname
);
1389 v9fs_string_init(&aname
);
1390 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1391 &afid
, &uname
, &aname
, &n_uname
);
1395 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1397 fidp
= alloc_fid(s
, fid
);
1402 fidp
->uid
= n_uname
;
1403 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1409 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1415 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1423 * disable migration if we haven't done already.
1424 * attach could get called multiple times for the same export.
1426 if (!s
->migration_blocker
) {
1427 error_setg(&s
->migration_blocker
,
1428 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1429 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1430 err
= migrate_add_blocker(s
->migration_blocker
, NULL
);
1432 error_free(s
->migration_blocker
);
1433 s
->migration_blocker
= NULL
;
1440 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1447 memcpy(&s
->root_st
, &stbuf
, sizeof(stbuf
));
1448 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1449 qid
.type
, qid
.version
, qid
.path
);
1453 pdu_complete(pdu
, err
);
1454 v9fs_string_free(&uname
);
1455 v9fs_string_free(&aname
);
1458 static void coroutine_fn
v9fs_stat(void *opaque
)
1466 V9fsPDU
*pdu
= opaque
;
1469 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1473 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1475 fidp
= get_fid(pdu
, fid
);
1480 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1484 basename
= g_path_get_basename(fidp
->path
.data
);
1485 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1490 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1492 v9fs_stat_free(&v9stat
);
1495 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1496 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1498 v9fs_stat_free(&v9stat
);
1502 pdu_complete(pdu
, err
);
1505 static void coroutine_fn
v9fs_getattr(void *opaque
)
1512 uint64_t request_mask
;
1513 V9fsStatDotl v9stat_dotl
;
1514 V9fsPDU
*pdu
= opaque
;
1516 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1520 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1522 fidp
= get_fid(pdu
, fid
);
1528 * Currently we only support BASIC fields in stat, so there is no
1529 * need to look at request_mask.
1531 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1535 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1540 /* fill st_gen if requested and supported by underlying fs */
1541 if (request_mask
& P9_STATS_GEN
) {
1542 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1545 /* we have valid st_gen: update result mask */
1546 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1549 /* request cancelled, e.g. by Tflush */
1552 /* failed to get st_gen: not fatal, ignore */
1556 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1561 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1562 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1563 v9stat_dotl
.st_gid
);
1567 pdu_complete(pdu
, retval
);
1570 /* Attribute flags */
1571 #define P9_ATTR_MODE (1 << 0)
1572 #define P9_ATTR_UID (1 << 1)
1573 #define P9_ATTR_GID (1 << 2)
1574 #define P9_ATTR_SIZE (1 << 3)
1575 #define P9_ATTR_ATIME (1 << 4)
1576 #define P9_ATTR_MTIME (1 << 5)
1577 #define P9_ATTR_CTIME (1 << 6)
1578 #define P9_ATTR_ATIME_SET (1 << 7)
1579 #define P9_ATTR_MTIME_SET (1 << 8)
1581 #define P9_ATTR_MASK 127
1583 static void coroutine_fn
v9fs_setattr(void *opaque
)
1590 V9fsPDU
*pdu
= opaque
;
1592 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1597 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1598 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1599 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1601 fidp
= get_fid(pdu
, fid
);
1606 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1607 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1612 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1613 struct timespec times
[2];
1614 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1615 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1616 times
[0].tv_sec
= v9iattr
.atime_sec
;
1617 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1619 times
[0].tv_nsec
= UTIME_NOW
;
1622 times
[0].tv_nsec
= UTIME_OMIT
;
1624 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1625 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1626 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1627 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1629 times
[1].tv_nsec
= UTIME_NOW
;
1632 times
[1].tv_nsec
= UTIME_OMIT
;
1634 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1640 * If the only valid entry in iattr is ctime we can call
1641 * chown(-1,-1) to update the ctime of the file
1643 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1644 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1645 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1646 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1649 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1652 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1658 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1659 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1665 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1669 pdu_complete(pdu
, err
);
1672 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1678 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1683 for (i
= 0; i
< nwnames
; i
++) {
1684 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1693 static bool name_is_illegal(const char *name
)
1695 return !*name
|| strchr(name
, '/') != NULL
;
1698 static bool same_stat_id(const struct stat
*a
, const struct stat
*b
)
1700 return a
->st_dev
== b
->st_dev
&& a
->st_ino
== b
->st_ino
;
1703 static void coroutine_fn
v9fs_walk(void *opaque
)
1706 g_autofree V9fsQID
*qids
= NULL
;
1708 V9fsPath dpath
, path
, *pathes
= NULL
;
1710 struct stat stbuf
, fidst
;
1711 g_autofree
struct stat
*stbufs
= NULL
;
1713 int32_t fid
, newfid
;
1714 V9fsString
*wnames
= NULL
;
1716 V9fsFidState
*newfidp
= NULL
;
1717 V9fsPDU
*pdu
= opaque
;
1718 V9fsState
*s
= pdu
->s
;
1721 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1723 pdu_complete(pdu
, err
);
1728 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1730 if (nwnames
> P9_MAXWELEM
) {
1735 wnames
= g_new0(V9fsString
, nwnames
);
1736 qids
= g_new0(V9fsQID
, nwnames
);
1737 stbufs
= g_new0(struct stat
, nwnames
);
1738 pathes
= g_new0(V9fsPath
, nwnames
);
1739 for (i
= 0; i
< nwnames
; i
++) {
1740 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1744 if (name_is_illegal(wnames
[i
].data
)) {
1751 fidp
= get_fid(pdu
, fid
);
1757 v9fs_path_init(&dpath
);
1758 v9fs_path_init(&path
);
1760 * Both dpath and path initially point to fidp.
1761 * Needed to handle request with nwnames == 0
1763 v9fs_path_copy(&dpath
, &fidp
->path
);
1764 v9fs_path_copy(&path
, &fidp
->path
);
1767 * To keep latency (i.e. overall execution time for processing this
1768 * Twalk client request) as small as possible, run all the required fs
1769 * driver code altogether inside the following block.
1771 v9fs_co_run_in_worker({
1772 if (v9fs_request_cancelled(pdu
)) {
1776 err
= s
->ops
->lstat(&s
->ctx
, &dpath
, &fidst
);
1782 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1783 if (v9fs_request_cancelled(pdu
)) {
1787 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1788 strcmp("..", wnames
[name_idx
].data
))
1790 err
= s
->ops
->name_to_path(&s
->ctx
, &dpath
,
1791 wnames
[name_idx
].data
,
1797 if (v9fs_request_cancelled(pdu
)) {
1801 err
= s
->ops
->lstat(&s
->ctx
, &pathes
[name_idx
], &stbuf
);
1806 stbufs
[name_idx
] = stbuf
;
1807 v9fs_path_copy(&dpath
, &pathes
[name_idx
]);
1812 * Handle all the rest of this Twalk request on main thread ...
1818 err
= stat_to_qid(pdu
, &fidst
, &qid
);
1824 /* reset dpath and path */
1825 v9fs_path_copy(&dpath
, &fidp
->path
);
1826 v9fs_path_copy(&path
, &fidp
->path
);
1828 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1829 if (!same_stat_id(&pdu
->s
->root_st
, &stbuf
) ||
1830 strcmp("..", wnames
[name_idx
].data
))
1832 stbuf
= stbufs
[name_idx
];
1833 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1837 v9fs_path_copy(&path
, &pathes
[name_idx
]);
1838 v9fs_path_copy(&dpath
, &path
);
1840 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1842 if (fid
== newfid
) {
1843 if (fidp
->fid_type
!= P9_FID_NONE
) {
1847 v9fs_path_write_lock(s
);
1848 v9fs_path_copy(&fidp
->path
, &path
);
1849 v9fs_path_unlock(s
);
1851 newfidp
= alloc_fid(s
, newfid
);
1852 if (newfidp
== NULL
) {
1856 newfidp
->uid
= fidp
->uid
;
1857 v9fs_path_copy(&newfidp
->path
, &path
);
1859 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1860 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1864 put_fid(pdu
, newfidp
);
1866 v9fs_path_free(&dpath
);
1867 v9fs_path_free(&path
);
1869 pdu_complete(pdu
, err
);
1870 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1871 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1872 v9fs_string_free(&wnames
[name_idx
]);
1873 v9fs_path_free(&pathes
[name_idx
]);
1880 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1882 struct statfs stbuf
;
1884 V9fsState
*s
= pdu
->s
;
1887 * iounit should be multiples of f_bsize (host filesystem block size
1888 * and as well as less than (client msize - P9_IOHDRSZ))
1890 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1891 if (stbuf
.f_bsize
) {
1892 iounit
= stbuf
.f_bsize
;
1893 iounit
*= (s
->msize
- P9_IOHDRSZ
) / stbuf
.f_bsize
;
1897 iounit
= s
->msize
- P9_IOHDRSZ
;
1902 static void coroutine_fn
v9fs_open(void *opaque
)
1913 V9fsPDU
*pdu
= opaque
;
1914 V9fsState
*s
= pdu
->s
;
1916 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1917 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1920 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1926 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1928 fidp
= get_fid(pdu
, fid
);
1933 if (fidp
->fid_type
!= P9_FID_NONE
) {
1938 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1942 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1946 if (S_ISDIR(stbuf
.st_mode
)) {
1947 err
= v9fs_co_opendir(pdu
, fidp
);
1951 fidp
->fid_type
= P9_FID_DIR
;
1952 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1958 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1959 flags
= get_dotl_openflags(s
, mode
);
1961 flags
= omode_to_uflags(mode
);
1963 if (is_ro_export(&s
->ctx
)) {
1964 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1965 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1970 err
= v9fs_co_open(pdu
, fidp
, flags
);
1974 fidp
->fid_type
= P9_FID_FILE
;
1975 fidp
->open_flags
= flags
;
1976 if (flags
& O_EXCL
) {
1978 * We let the host file system do O_EXCL check
1979 * We should not reclaim such fd
1981 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1983 iounit
= get_iounit(pdu
, &fidp
->path
);
1984 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1990 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1991 qid
.type
, qid
.version
, qid
.path
, iounit
);
1995 pdu_complete(pdu
, err
);
1998 static void coroutine_fn
v9fs_lcreate(void *opaque
)
2000 int32_t dfid
, flags
, mode
;
2009 V9fsPDU
*pdu
= opaque
;
2011 v9fs_string_init(&name
);
2012 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
2013 &name
, &flags
, &mode
, &gid
);
2017 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
2019 if (name_is_illegal(name
.data
)) {
2024 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2029 fidp
= get_fid(pdu
, dfid
);
2034 if (fidp
->fid_type
!= P9_FID_NONE
) {
2039 flags
= get_dotl_openflags(pdu
->s
, flags
);
2040 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
2041 flags
| O_CREAT
, mode
, &stbuf
);
2045 fidp
->fid_type
= P9_FID_FILE
;
2046 fidp
->open_flags
= flags
;
2047 if (flags
& O_EXCL
) {
2049 * We let the host file system do O_EXCL check
2050 * We should not reclaim such fd
2052 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2054 iounit
= get_iounit(pdu
, &fidp
->path
);
2055 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2059 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2064 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2065 qid
.type
, qid
.version
, qid
.path
, iounit
);
2069 pdu_complete(pdu
, err
);
2070 v9fs_string_free(&name
);
2073 static void coroutine_fn
v9fs_fsync(void *opaque
)
2080 V9fsPDU
*pdu
= opaque
;
2082 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2086 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2088 fidp
= get_fid(pdu
, fid
);
2093 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2099 pdu_complete(pdu
, err
);
2102 static void coroutine_fn
v9fs_clunk(void *opaque
)
2108 V9fsPDU
*pdu
= opaque
;
2109 V9fsState
*s
= pdu
->s
;
2111 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2115 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2117 fidp
= clunk_fid(s
, fid
);
2123 * Bump the ref so that put_fid will
2127 err
= put_fid(pdu
, fidp
);
2132 pdu_complete(pdu
, err
);
2136 * Create a QEMUIOVector for a sub-region of PDU iovecs
2138 * @qiov: uninitialized QEMUIOVector
2139 * @skip: number of bytes to skip from beginning of PDU
2140 * @size: number of bytes to include
2141 * @is_write: true - write, false - read
2143 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2144 * with qemu_iovec_destroy().
2146 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2147 size_t skip
, size_t size
,
2155 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2157 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2160 qemu_iovec_init_external(&elem
, iov
, niov
);
2161 qemu_iovec_init(qiov
, niov
);
2162 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
2165 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2166 uint64_t off
, uint32_t max_count
)
2170 uint64_t read_count
;
2171 QEMUIOVector qiov_full
;
2173 if (fidp
->fs
.xattr
.len
< off
) {
2176 read_count
= fidp
->fs
.xattr
.len
- off
;
2178 if (read_count
> max_count
) {
2179 read_count
= max_count
;
2181 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2187 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
2188 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2189 ((char *)fidp
->fs
.xattr
.value
) + off
,
2191 qemu_iovec_destroy(&qiov_full
);
2199 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2208 off_t saved_dir_pos
;
2209 struct dirent
*dent
;
2211 /* save the directory position */
2212 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2213 if (saved_dir_pos
< 0) {
2214 return saved_dir_pos
;
2218 v9fs_path_init(&path
);
2220 v9fs_readdir_lock(&fidp
->fs
.dir
);
2222 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2226 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2230 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2234 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2238 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2239 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2241 /* Ran out of buffer. Set dir back to old position and return */
2242 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2243 v9fs_stat_free(&v9stat
);
2244 v9fs_path_free(&path
);
2248 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2249 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2251 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2254 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2255 v9fs_stat_free(&v9stat
);
2256 v9fs_path_free(&path
);
2260 v9fs_stat_free(&v9stat
);
2261 v9fs_path_free(&path
);
2262 saved_dir_pos
= dent
->d_off
;
2265 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2267 v9fs_path_free(&path
);
2274 static void coroutine_fn
v9fs_read(void *opaque
)
2283 V9fsPDU
*pdu
= opaque
;
2284 V9fsState
*s
= pdu
->s
;
2286 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2290 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2292 fidp
= get_fid(pdu
, fid
);
2297 if (fidp
->fid_type
== P9_FID_DIR
) {
2298 if (s
->proto_version
!= V9FS_PROTO_2000U
) {
2300 "9p: bad client: T_read request on directory only expected "
2301 "with 9P2000.u protocol version"
2307 v9fs_co_rewinddir(pdu
, fidp
);
2309 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2314 err
= pdu_marshal(pdu
, offset
, "d", count
);
2318 err
+= offset
+ count
;
2319 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2320 QEMUIOVector qiov_full
;
2324 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2325 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2327 qemu_iovec_reset(&qiov
);
2328 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2330 print_sg(qiov
.iov
, qiov
.niov
);
2332 /* Loop in case of EINTR */
2334 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2339 } while (len
== -EINTR
&& !pdu
->cancelled
);
2341 /* IO error return the error */
2343 goto out_free_iovec
;
2345 } while (count
< max_count
&& len
> 0);
2346 err
= pdu_marshal(pdu
, offset
, "d", count
);
2348 goto out_free_iovec
;
2350 err
+= offset
+ count
;
2352 qemu_iovec_destroy(&qiov
);
2353 qemu_iovec_destroy(&qiov_full
);
2354 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2355 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2359 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2363 pdu_complete(pdu
, err
);
2367 * Returns size required in Rreaddir response for the passed dirent @p name.
2369 * @param name - directory entry's name (i.e. file name, directory name)
2370 * @returns required size in bytes
2372 size_t v9fs_readdir_response_size(V9fsString
*name
)
2375 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2376 * size of type (1) + size of name.size (2) + strlen(name.data)
2378 return 24 + v9fs_string_size(name
);
2381 static void v9fs_free_dirents(struct V9fsDirEnt
*e
)
2383 struct V9fsDirEnt
*next
= NULL
;
2385 for (; e
; e
= next
) {
2393 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2394 off_t offset
, int32_t max_count
)
2401 struct dirent
*dent
;
2403 struct V9fsDirEnt
*entries
= NULL
;
2406 * inode remapping requires the device id, which in turn might be
2407 * different for different directory entries, so if inode remapping is
2408 * enabled we have to make a full stat for each directory entry
2410 const bool dostat
= pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
;
2413 * Fetch all required directory entries altogether on a background IO
2414 * thread from fs driver. We don't want to do that for each entry
2415 * individually, because hopping between threads (this main IO thread
2416 * and background IO driver thread) would sum up to huge latencies.
2418 count
= v9fs_co_readdir_many(pdu
, fidp
, &entries
, offset
, max_count
,
2427 for (struct V9fsDirEnt
*e
= entries
; e
; e
= e
->next
) {
2430 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2432 /* e->st should never be NULL, but just to be sure */
2439 err
= stat_to_qid(pdu
, st
, &qid
);
2445 * Fill up just the path field of qid because the client uses
2446 * only that. To fill the entire qid structure we will have
2447 * to stat each dirent found, which is expensive. For the
2448 * latter reason we don't call stat_to_qid() here. Only drawback
2449 * is that no multi-device export detection of stat_to_qid()
2450 * would be done and provided as error to the user here. But
2451 * user would get that error anyway when accessing those
2452 * files/dirs through other ways.
2454 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2455 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2456 /* Fill the other fields with dummy values */
2461 v9fs_string_init(&name
);
2462 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2464 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2465 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2467 dent
->d_type
, &name
);
2469 v9fs_string_free(&name
);
2480 v9fs_free_dirents(entries
);
2487 static void coroutine_fn
v9fs_readdir(void *opaque
)
2493 uint64_t initial_offset
;
2496 V9fsPDU
*pdu
= opaque
;
2497 V9fsState
*s
= pdu
->s
;
2499 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2500 &initial_offset
, &max_count
);
2504 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2506 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2507 if (max_count
> s
->msize
- 11) {
2508 max_count
= s
->msize
- 11;
2510 "9p: bad client: T_readdir with count > msize - 11"
2514 fidp
= get_fid(pdu
, fid
);
2519 if (!fidp
->fs
.dir
.stream
) {
2523 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
2525 "9p: bad client: T_readdir request only expected with 9P2000.L "
2528 retval
= -EOPNOTSUPP
;
2531 count
= v9fs_do_readdir(pdu
, fidp
, (off_t
) initial_offset
, max_count
);
2536 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2540 retval
+= count
+ offset
;
2541 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2545 pdu_complete(pdu
, retval
);
2548 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2549 uint64_t off
, uint32_t count
,
2550 struct iovec
*sg
, int cnt
)
2554 uint64_t write_count
;
2558 if (fidp
->fs
.xattr
.len
< off
) {
2561 write_count
= fidp
->fs
.xattr
.len
- off
;
2562 if (write_count
> count
) {
2563 write_count
= count
;
2565 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2570 fidp
->fs
.xattr
.copied_len
+= write_count
;
2572 * Now copy the content from sg list
2574 for (i
= 0; i
< cnt
; i
++) {
2575 if (write_count
> sg
[i
].iov_len
) {
2576 to_copy
= sg
[i
].iov_len
;
2578 to_copy
= write_count
;
2580 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2581 /* updating vs->off since we are not using below */
2583 write_count
-= to_copy
;
2589 static void coroutine_fn
v9fs_write(void *opaque
)
2599 V9fsPDU
*pdu
= opaque
;
2600 V9fsState
*s
= pdu
->s
;
2601 QEMUIOVector qiov_full
;
2604 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2606 pdu_complete(pdu
, err
);
2610 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2611 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2613 fidp
= get_fid(pdu
, fid
);
2618 if (fidp
->fid_type
== P9_FID_FILE
) {
2619 if (fidp
->fs
.fd
== -1) {
2623 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2625 * setxattr operation
2627 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2628 qiov_full
.iov
, qiov_full
.niov
);
2634 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2636 qemu_iovec_reset(&qiov
);
2637 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2639 print_sg(qiov
.iov
, qiov
.niov
);
2641 /* Loop in case of EINTR */
2643 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2648 } while (len
== -EINTR
&& !pdu
->cancelled
);
2650 /* IO error return the error */
2654 } while (total
< count
&& len
> 0);
2657 err
= pdu_marshal(pdu
, offset
, "d", total
);
2662 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2664 qemu_iovec_destroy(&qiov
);
2668 qemu_iovec_destroy(&qiov_full
);
2669 pdu_complete(pdu
, err
);
2672 static void coroutine_fn
v9fs_create(void *opaque
)
2684 V9fsString extension
;
2686 V9fsPDU
*pdu
= opaque
;
2687 V9fsState
*s
= pdu
->s
;
2689 v9fs_path_init(&path
);
2690 v9fs_string_init(&name
);
2691 v9fs_string_init(&extension
);
2692 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2693 &perm
, &mode
, &extension
);
2697 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2699 if (name_is_illegal(name
.data
)) {
2704 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2709 fidp
= get_fid(pdu
, fid
);
2714 if (fidp
->fid_type
!= P9_FID_NONE
) {
2718 if (perm
& P9_STAT_MODE_DIR
) {
2719 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2720 fidp
->uid
, -1, &stbuf
);
2724 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2728 v9fs_path_write_lock(s
);
2729 v9fs_path_copy(&fidp
->path
, &path
);
2730 v9fs_path_unlock(s
);
2731 err
= v9fs_co_opendir(pdu
, fidp
);
2735 fidp
->fid_type
= P9_FID_DIR
;
2736 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2737 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2738 extension
.data
, -1 , &stbuf
);
2742 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2746 v9fs_path_write_lock(s
);
2747 v9fs_path_copy(&fidp
->path
, &path
);
2748 v9fs_path_unlock(s
);
2749 } else if (perm
& P9_STAT_MODE_LINK
) {
2750 int32_t ofid
= atoi(extension
.data
);
2751 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2752 if (ofidp
== NULL
) {
2756 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2757 put_fid(pdu
, ofidp
);
2761 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2763 fidp
->fid_type
= P9_FID_NONE
;
2766 v9fs_path_write_lock(s
);
2767 v9fs_path_copy(&fidp
->path
, &path
);
2768 v9fs_path_unlock(s
);
2769 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2771 fidp
->fid_type
= P9_FID_NONE
;
2774 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2776 uint32_t major
, minor
;
2779 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2796 nmode
|= perm
& 0777;
2797 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2798 makedev(major
, minor
), nmode
, &stbuf
);
2802 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2806 v9fs_path_write_lock(s
);
2807 v9fs_path_copy(&fidp
->path
, &path
);
2808 v9fs_path_unlock(s
);
2809 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2810 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2811 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2815 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2819 v9fs_path_write_lock(s
);
2820 v9fs_path_copy(&fidp
->path
, &path
);
2821 v9fs_path_unlock(s
);
2822 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2823 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2824 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2828 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2832 v9fs_path_write_lock(s
);
2833 v9fs_path_copy(&fidp
->path
, &path
);
2834 v9fs_path_unlock(s
);
2836 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2837 omode_to_uflags(mode
) | O_CREAT
, perm
, &stbuf
);
2841 fidp
->fid_type
= P9_FID_FILE
;
2842 fidp
->open_flags
= omode_to_uflags(mode
);
2843 if (fidp
->open_flags
& O_EXCL
) {
2845 * We let the host file system do O_EXCL check
2846 * We should not reclaim such fd
2848 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2851 iounit
= get_iounit(pdu
, &fidp
->path
);
2852 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2856 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2861 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2862 qid
.type
, qid
.version
, qid
.path
, iounit
);
2866 pdu_complete(pdu
, err
);
2867 v9fs_string_free(&name
);
2868 v9fs_string_free(&extension
);
2869 v9fs_path_free(&path
);
2872 static void coroutine_fn
v9fs_symlink(void *opaque
)
2874 V9fsPDU
*pdu
= opaque
;
2877 V9fsFidState
*dfidp
;
2885 v9fs_string_init(&name
);
2886 v9fs_string_init(&symname
);
2887 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2891 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2893 if (name_is_illegal(name
.data
)) {
2898 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2903 dfidp
= get_fid(pdu
, dfid
);
2904 if (dfidp
== NULL
) {
2908 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2912 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2916 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2921 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2922 qid
.type
, qid
.version
, qid
.path
);
2924 put_fid(pdu
, dfidp
);
2926 pdu_complete(pdu
, err
);
2927 v9fs_string_free(&name
);
2928 v9fs_string_free(&symname
);
2931 static void coroutine_fn
v9fs_flush(void *opaque
)
2936 V9fsPDU
*cancel_pdu
= NULL
;
2937 V9fsPDU
*pdu
= opaque
;
2938 V9fsState
*s
= pdu
->s
;
2940 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2942 pdu_complete(pdu
, err
);
2945 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2947 if (pdu
->tag
== tag
) {
2948 warn_report("the guest sent a self-referencing 9P flush request");
2950 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2951 if (cancel_pdu
->tag
== tag
) {
2957 cancel_pdu
->cancelled
= 1;
2959 * Wait for pdu to complete.
2961 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2962 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2963 cancel_pdu
->cancelled
= 0;
2964 pdu_free(cancel_pdu
);
2967 pdu_complete(pdu
, 7);
2970 static void coroutine_fn
v9fs_link(void *opaque
)
2972 V9fsPDU
*pdu
= opaque
;
2973 int32_t dfid
, oldfid
;
2974 V9fsFidState
*dfidp
, *oldfidp
;
2979 v9fs_string_init(&name
);
2980 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2984 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2986 if (name_is_illegal(name
.data
)) {
2991 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2996 dfidp
= get_fid(pdu
, dfid
);
2997 if (dfidp
== NULL
) {
3002 oldfidp
= get_fid(pdu
, oldfid
);
3003 if (oldfidp
== NULL
) {
3007 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
3011 put_fid(pdu
, oldfidp
);
3013 put_fid(pdu
, dfidp
);
3015 v9fs_string_free(&name
);
3016 pdu_complete(pdu
, err
);
3019 /* Only works with path name based fid */
3020 static void coroutine_fn
v9fs_remove(void *opaque
)
3026 V9fsPDU
*pdu
= opaque
;
3028 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3032 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
3034 fidp
= get_fid(pdu
, fid
);
3039 /* if fs driver is not path based, return EOPNOTSUPP */
3040 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3045 * IF the file is unlinked, we cannot reopen
3046 * the file later. So don't reclaim fd
3048 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
3052 err
= v9fs_co_remove(pdu
, &fidp
->path
);
3057 /* For TREMOVE we need to clunk the fid even on failed remove */
3058 clunk_fid(pdu
->s
, fidp
->fid
);
3061 pdu_complete(pdu
, err
);
3064 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
3068 int32_t dfid
, flags
, rflags
= 0;
3071 V9fsFidState
*dfidp
;
3072 V9fsPDU
*pdu
= opaque
;
3074 v9fs_string_init(&name
);
3075 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3080 if (name_is_illegal(name
.data
)) {
3085 if (!strcmp(".", name
.data
)) {
3090 if (!strcmp("..", name
.data
)) {
3095 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3100 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3101 rflags
|= AT_REMOVEDIR
;
3104 dfidp
= get_fid(pdu
, dfid
);
3105 if (dfidp
== NULL
) {
3110 * IF the file is unlinked, we cannot reopen
3111 * the file later. So don't reclaim fd
3113 v9fs_path_init(&path
);
3114 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3118 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3122 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3127 put_fid(pdu
, dfidp
);
3128 v9fs_path_free(&path
);
3130 pdu_complete(pdu
, err
);
3131 v9fs_string_free(&name
);
3135 /* Only works with path name based fid */
3136 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3142 V9fsFidState
*tfidp
;
3143 V9fsState
*s
= pdu
->s
;
3144 V9fsFidState
*dirfidp
= NULL
;
3146 v9fs_path_init(&new_path
);
3147 if (newdirfid
!= -1) {
3148 dirfidp
= get_fid(pdu
, newdirfid
);
3149 if (dirfidp
== NULL
) {
3152 if (fidp
->fid_type
!= P9_FID_NONE
) {
3156 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3161 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3164 v9fs_path_init(&dir_path
);
3165 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3168 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3169 v9fs_path_free(&dir_path
);
3174 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3179 * Fixup fid's pointing to the old name to
3180 * start pointing to the new name
3182 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3183 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3184 /* replace the name */
3185 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3190 put_fid(pdu
, dirfidp
);
3192 v9fs_path_free(&new_path
);
3196 /* Only works with path name based fid */
3197 static void coroutine_fn
v9fs_rename(void *opaque
)
3205 V9fsPDU
*pdu
= opaque
;
3206 V9fsState
*s
= pdu
->s
;
3208 v9fs_string_init(&name
);
3209 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3214 if (name_is_illegal(name
.data
)) {
3219 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3224 fidp
= get_fid(pdu
, fid
);
3229 if (fidp
->fid_type
!= P9_FID_NONE
) {
3233 /* if fs driver is not path based, return EOPNOTSUPP */
3234 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3238 v9fs_path_write_lock(s
);
3239 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3240 v9fs_path_unlock(s
);
3247 pdu_complete(pdu
, err
);
3248 v9fs_string_free(&name
);
3251 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3252 V9fsString
*old_name
,
3254 V9fsString
*new_name
)
3256 V9fsFidState
*tfidp
;
3257 V9fsPath oldpath
, newpath
;
3258 V9fsState
*s
= pdu
->s
;
3261 v9fs_path_init(&oldpath
);
3262 v9fs_path_init(&newpath
);
3263 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3267 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3273 * Fixup fid's pointing to the old name to
3274 * start pointing to the new name
3276 QSIMPLEQ_FOREACH(tfidp
, &s
->fid_list
, next
) {
3277 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3278 /* replace the name */
3279 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3283 v9fs_path_free(&oldpath
);
3284 v9fs_path_free(&newpath
);
3288 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3289 V9fsString
*old_name
,
3291 V9fsString
*new_name
)
3294 V9fsState
*s
= pdu
->s
;
3295 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3297 olddirfidp
= get_fid(pdu
, olddirfid
);
3298 if (olddirfidp
== NULL
) {
3302 if (newdirfid
!= -1) {
3303 newdirfidp
= get_fid(pdu
, newdirfid
);
3304 if (newdirfidp
== NULL
) {
3309 newdirfidp
= get_fid(pdu
, olddirfid
);
3312 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3313 &newdirfidp
->path
, new_name
);
3317 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3318 /* Only for path based fid we need to do the below fixup */
3319 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3320 &newdirfidp
->path
, new_name
);
3324 put_fid(pdu
, olddirfidp
);
3327 put_fid(pdu
, newdirfidp
);
3332 static void coroutine_fn
v9fs_renameat(void *opaque
)
3336 V9fsPDU
*pdu
= opaque
;
3337 V9fsState
*s
= pdu
->s
;
3338 int32_t olddirfid
, newdirfid
;
3339 V9fsString old_name
, new_name
;
3341 v9fs_string_init(&old_name
);
3342 v9fs_string_init(&new_name
);
3343 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3344 &old_name
, &newdirfid
, &new_name
);
3349 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3354 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3355 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3360 v9fs_path_write_lock(s
);
3361 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3362 &old_name
, newdirfid
, &new_name
);
3363 v9fs_path_unlock(s
);
3369 pdu_complete(pdu
, err
);
3370 v9fs_string_free(&old_name
);
3371 v9fs_string_free(&new_name
);
3374 static void coroutine_fn
v9fs_wstat(void *opaque
)
3383 V9fsPDU
*pdu
= opaque
;
3384 V9fsState
*s
= pdu
->s
;
3386 v9fs_stat_init(&v9stat
);
3387 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3391 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3392 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3394 fidp
= get_fid(pdu
, fid
);
3399 /* do we need to sync the file? */
3400 if (donttouch_stat(&v9stat
)) {
3401 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3404 if (v9stat
.mode
!= -1) {
3406 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3410 v9_mode
= stat_to_v9mode(&stbuf
);
3411 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3412 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3413 /* Attempting to change the type */
3417 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3418 v9mode_to_mode(v9stat
.mode
,
3419 &v9stat
.extension
));
3424 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3425 struct timespec times
[2];
3426 if (v9stat
.atime
!= -1) {
3427 times
[0].tv_sec
= v9stat
.atime
;
3428 times
[0].tv_nsec
= 0;
3430 times
[0].tv_nsec
= UTIME_OMIT
;
3432 if (v9stat
.mtime
!= -1) {
3433 times
[1].tv_sec
= v9stat
.mtime
;
3434 times
[1].tv_nsec
= 0;
3436 times
[1].tv_nsec
= UTIME_OMIT
;
3438 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3443 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3444 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3449 if (v9stat
.name
.size
!= 0) {
3450 v9fs_path_write_lock(s
);
3451 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3452 v9fs_path_unlock(s
);
3457 if (v9stat
.length
!= -1) {
3458 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3467 v9fs_stat_free(&v9stat
);
3468 pdu_complete(pdu
, err
);
3471 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3483 int32_t bsize_factor
;
3486 * compute bsize factor based on host file system block size
3489 bsize_factor
= (s
->msize
- P9_IOHDRSZ
) / stbuf
->f_bsize
;
3490 if (!bsize_factor
) {
3493 f_type
= stbuf
->f_type
;
3494 f_bsize
= stbuf
->f_bsize
;
3495 f_bsize
*= bsize_factor
;
3497 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3498 * adjust(divide) the number of blocks, free blocks and available
3499 * blocks by bsize factor
3501 f_blocks
= stbuf
->f_blocks
/ bsize_factor
;
3502 f_bfree
= stbuf
->f_bfree
/ bsize_factor
;
3503 f_bavail
= stbuf
->f_bavail
/ bsize_factor
;
3504 f_files
= stbuf
->f_files
;
3505 f_ffree
= stbuf
->f_ffree
;
3506 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3507 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3508 f_namelen
= stbuf
->f_namelen
;
3510 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3511 f_type
, f_bsize
, f_blocks
, f_bfree
,
3512 f_bavail
, f_files
, f_ffree
,
3513 fsid_val
, f_namelen
);
3516 static void coroutine_fn
v9fs_statfs(void *opaque
)
3522 struct statfs stbuf
;
3523 V9fsPDU
*pdu
= opaque
;
3524 V9fsState
*s
= pdu
->s
;
3526 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3530 fidp
= get_fid(pdu
, fid
);
3535 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3539 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3547 pdu_complete(pdu
, retval
);
3550 static void coroutine_fn
v9fs_mknod(void *opaque
)
3563 V9fsPDU
*pdu
= opaque
;
3565 v9fs_string_init(&name
);
3566 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3567 &major
, &minor
, &gid
);
3571 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3573 if (name_is_illegal(name
.data
)) {
3578 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3583 fidp
= get_fid(pdu
, fid
);
3588 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3589 makedev(major
, minor
), mode
, &stbuf
);
3593 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3597 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3602 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3603 qid
.type
, qid
.version
, qid
.path
);
3607 pdu_complete(pdu
, err
);
3608 v9fs_string_free(&name
);
3612 * Implement posix byte range locking code
3613 * Server side handling of locking code is very simple, because 9p server in
3614 * QEMU can handle only one client. And most of the lock handling
3615 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3616 * do any thing in * qemu 9p server side lock code path.
3617 * So when a TLOCK request comes, always return success
3619 static void coroutine_fn
v9fs_lock(void *opaque
)
3625 int32_t fid
, err
= 0;
3626 V9fsPDU
*pdu
= opaque
;
3628 v9fs_string_init(&flock
.client_id
);
3629 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3630 &flock
.flags
, &flock
.start
, &flock
.length
,
3631 &flock
.proc_id
, &flock
.client_id
);
3635 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3636 flock
.type
, flock
.start
, flock
.length
);
3639 /* We support only block flag now (that too ignored currently) */
3640 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3644 fidp
= get_fid(pdu
, fid
);
3649 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3653 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3658 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3662 pdu_complete(pdu
, err
);
3663 v9fs_string_free(&flock
.client_id
);
3667 * When a TGETLOCK request comes, always return success because all lock
3668 * handling is done by client's VFS layer.
3670 static void coroutine_fn
v9fs_getlock(void *opaque
)
3676 int32_t fid
, err
= 0;
3677 V9fsPDU
*pdu
= opaque
;
3679 v9fs_string_init(&glock
.client_id
);
3680 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3681 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3686 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3687 glock
.type
, glock
.start
, glock
.length
);
3689 fidp
= get_fid(pdu
, fid
);
3694 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3698 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3699 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3700 glock
.start
, glock
.length
, glock
.proc_id
,
3706 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3707 glock
.length
, glock
.proc_id
);
3711 pdu_complete(pdu
, err
);
3712 v9fs_string_free(&glock
.client_id
);
3715 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3717 V9fsPDU
*pdu
= opaque
;
3728 v9fs_string_init(&name
);
3729 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3733 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3735 if (name_is_illegal(name
.data
)) {
3740 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3745 fidp
= get_fid(pdu
, fid
);
3750 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3754 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3758 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3763 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3764 qid
.type
, qid
.version
, qid
.path
, err
);
3768 pdu_complete(pdu
, err
);
3769 v9fs_string_free(&name
);
3772 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3778 int32_t fid
, newfid
;
3779 V9fsFidState
*file_fidp
;
3780 V9fsFidState
*xattr_fidp
= NULL
;
3781 V9fsPDU
*pdu
= opaque
;
3782 V9fsState
*s
= pdu
->s
;
3784 v9fs_string_init(&name
);
3785 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3789 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3791 file_fidp
= get_fid(pdu
, fid
);
3792 if (file_fidp
== NULL
) {
3796 xattr_fidp
= alloc_fid(s
, newfid
);
3797 if (xattr_fidp
== NULL
) {
3801 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3802 if (!v9fs_string_size(&name
)) {
3804 * listxattr request. Get the size first
3806 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3809 clunk_fid(s
, xattr_fidp
->fid
);
3813 * Read the xattr value
3815 xattr_fidp
->fs
.xattr
.len
= size
;
3816 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3817 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3818 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3820 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3821 xattr_fidp
->fs
.xattr
.value
,
3822 xattr_fidp
->fs
.xattr
.len
);
3824 clunk_fid(s
, xattr_fidp
->fid
);
3828 err
= pdu_marshal(pdu
, offset
, "q", size
);
3835 * specific xattr fid. We check for xattr
3836 * presence also collect the xattr size
3838 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3842 clunk_fid(s
, xattr_fidp
->fid
);
3846 * Read the xattr value
3848 xattr_fidp
->fs
.xattr
.len
= size
;
3849 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3850 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3851 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3853 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3854 &name
, xattr_fidp
->fs
.xattr
.value
,
3855 xattr_fidp
->fs
.xattr
.len
);
3857 clunk_fid(s
, xattr_fidp
->fid
);
3861 err
= pdu_marshal(pdu
, offset
, "q", size
);
3867 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3869 put_fid(pdu
, file_fidp
);
3871 put_fid(pdu
, xattr_fidp
);
3874 pdu_complete(pdu
, err
);
3875 v9fs_string_free(&name
);
3878 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3880 int flags
, rflags
= 0;
3886 V9fsFidState
*file_fidp
;
3887 V9fsFidState
*xattr_fidp
;
3888 V9fsPDU
*pdu
= opaque
;
3890 v9fs_string_init(&name
);
3891 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3895 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3897 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3902 if (flags
& P9_XATTR_CREATE
) {
3903 rflags
|= XATTR_CREATE
;
3906 if (flags
& P9_XATTR_REPLACE
) {
3907 rflags
|= XATTR_REPLACE
;
3910 if (size
> XATTR_SIZE_MAX
) {
3915 file_fidp
= get_fid(pdu
, fid
);
3916 if (file_fidp
== NULL
) {
3920 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3925 /* Make the file fid point to xattr */
3926 xattr_fidp
= file_fidp
;
3927 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3928 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3929 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3930 xattr_fidp
->fs
.xattr
.len
= size
;
3931 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3932 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3933 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3934 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3937 put_fid(pdu
, file_fidp
);
3939 pdu_complete(pdu
, err
);
3940 v9fs_string_free(&name
);
3943 static void coroutine_fn
v9fs_readlink(void *opaque
)
3945 V9fsPDU
*pdu
= opaque
;
3952 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3956 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3957 fidp
= get_fid(pdu
, fid
);
3963 v9fs_string_init(&target
);
3964 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3968 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3970 v9fs_string_free(&target
);
3974 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3975 v9fs_string_free(&target
);
3979 pdu_complete(pdu
, err
);
3982 static CoroutineEntry
*pdu_co_handlers
[] = {
3983 [P9_TREADDIR
] = v9fs_readdir
,
3984 [P9_TSTATFS
] = v9fs_statfs
,
3985 [P9_TGETATTR
] = v9fs_getattr
,
3986 [P9_TSETATTR
] = v9fs_setattr
,
3987 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3988 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3989 [P9_TMKNOD
] = v9fs_mknod
,
3990 [P9_TRENAME
] = v9fs_rename
,
3991 [P9_TLOCK
] = v9fs_lock
,
3992 [P9_TGETLOCK
] = v9fs_getlock
,
3993 [P9_TRENAMEAT
] = v9fs_renameat
,
3994 [P9_TREADLINK
] = v9fs_readlink
,
3995 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3996 [P9_TMKDIR
] = v9fs_mkdir
,
3997 [P9_TVERSION
] = v9fs_version
,
3998 [P9_TLOPEN
] = v9fs_open
,
3999 [P9_TATTACH
] = v9fs_attach
,
4000 [P9_TSTAT
] = v9fs_stat
,
4001 [P9_TWALK
] = v9fs_walk
,
4002 [P9_TCLUNK
] = v9fs_clunk
,
4003 [P9_TFSYNC
] = v9fs_fsync
,
4004 [P9_TOPEN
] = v9fs_open
,
4005 [P9_TREAD
] = v9fs_read
,
4007 [P9_TAUTH
] = v9fs_auth
,
4009 [P9_TFLUSH
] = v9fs_flush
,
4010 [P9_TLINK
] = v9fs_link
,
4011 [P9_TSYMLINK
] = v9fs_symlink
,
4012 [P9_TCREATE
] = v9fs_create
,
4013 [P9_TLCREATE
] = v9fs_lcreate
,
4014 [P9_TWRITE
] = v9fs_write
,
4015 [P9_TWSTAT
] = v9fs_wstat
,
4016 [P9_TREMOVE
] = v9fs_remove
,
4019 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
4021 V9fsPDU
*pdu
= opaque
;
4022 pdu_complete(pdu
, -EOPNOTSUPP
);
4025 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
4027 V9fsPDU
*pdu
= opaque
;
4028 pdu_complete(pdu
, -EROFS
);
4031 static inline bool is_read_only_op(V9fsPDU
*pdu
)
4058 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
4061 CoroutineEntry
*handler
;
4062 V9fsState
*s
= pdu
->s
;
4064 pdu
->size
= le32_to_cpu(hdr
->size_le
);
4066 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
4068 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
4069 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
4070 handler
= v9fs_op_not_supp
;
4071 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4072 handler
= v9fs_fs_ro
;
4074 handler
= pdu_co_handlers
[pdu
->id
];
4077 qemu_co_queue_init(&pdu
->complete
);
4078 co
= qemu_coroutine_create(handler
, pdu
);
4079 qemu_coroutine_enter(co
);
4082 /* Returns 0 on success, 1 on failure. */
4083 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4093 assert(!s
->transport
);
4096 /* initialize pdu allocator */
4097 QLIST_INIT(&s
->free_list
);
4098 QLIST_INIT(&s
->active_list
);
4099 for (i
= 0; i
< MAX_REQ
; i
++) {
4100 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4105 v9fs_path_init(&path
);
4107 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4110 /* We don't have a fsdev identified by fsdev_id */
4111 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4113 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4117 if (!s
->fsconf
.tag
) {
4118 /* we haven't specified a mount_tag */
4119 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4120 s
->fsconf
.fsdev_id
);
4124 s
->ctx
.export_flags
= fse
->export_flags
;
4125 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4126 s
->ctx
.exops
.get_st_gen
= NULL
;
4127 len
= strlen(s
->fsconf
.tag
);
4128 if (len
> MAX_TAG_LEN
- 1) {
4129 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4130 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4134 s
->tag
= g_strdup(s
->fsconf
.tag
);
4139 s
->ctx
.fmode
= fse
->fmode
;
4140 s
->ctx
.dmode
= fse
->dmode
;
4142 QSIMPLEQ_INIT(&s
->fid_list
);
4143 qemu_co_rwlock_init(&s
->rename_lock
);
4145 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4146 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4147 s
->fsconf
.fsdev_id
);
4152 * Check details of export path, We need to use fs driver
4153 * call back to do that. Since we are in the init path, we don't
4154 * use co-routines here.
4156 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4158 "error in converting name to path %s", strerror(errno
));
4161 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4162 error_setg(errp
, "share path %s does not exist", fse
->path
);
4164 } else if (!S_ISDIR(stat
.st_mode
)) {
4165 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4169 s
->dev_id
= stat
.st_dev
;
4171 /* init inode remapping : */
4172 /* hash table for variable length inode suffixes */
4173 qpd_table_init(&s
->qpd_table
);
4174 /* hash table for slow/full inode remapping (most users won't need it) */
4175 qpf_table_init(&s
->qpf_table
);
4176 /* hash table for quick inode remapping */
4177 qpp_table_init(&s
->qpp_table
);
4179 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4180 s
->qp_fullpath_next
= 1;
4182 s
->ctx
.fst
= &fse
->fst
;
4183 fsdev_throttle_init(s
->ctx
.fst
);
4188 v9fs_device_unrealize_common(s
);
4190 v9fs_path_free(&path
);
4194 void v9fs_device_unrealize_common(V9fsState
*s
)
4196 if (s
->ops
&& s
->ops
->cleanup
) {
4197 s
->ops
->cleanup(&s
->ctx
);
4200 fsdev_throttle_cleanup(s
->ctx
.fst
);
4203 qp_table_destroy(&s
->qpd_table
);
4204 qp_table_destroy(&s
->qpp_table
);
4205 qp_table_destroy(&s
->qpf_table
);
4206 g_free(s
->ctx
.fs_root
);
4209 typedef struct VirtfsCoResetData
{
4212 } VirtfsCoResetData
;
4214 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4216 VirtfsCoResetData
*data
= opaque
;
4218 virtfs_reset(&data
->pdu
);
4222 void v9fs_reset(V9fsState
*s
)
4224 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4227 while (!QLIST_EMPTY(&s
->active_list
)) {
4228 aio_poll(qemu_get_aio_context(), true);
4231 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4232 qemu_coroutine_enter(co
);
4234 while (!data
.done
) {
4235 aio_poll(qemu_get_aio_context(), true);
4239 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4242 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4243 error_report("Failed to get the resource limit");
4246 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/ 3);
4247 open_fd_rc
= rlim
.rlim_cur
/ 2;