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.
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
27 #include "migration/blocker.h"
28 #include "sysemu/qtest.h"
29 #include "qemu/xxhash.h"
34 static int open_fd_rc
;
48 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
54 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
60 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
66 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
72 static int omode_to_uflags(int8_t mode
)
106 typedef struct DotlOpenflagMap
{
111 static int dotl_to_open_flags(int flags
)
115 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
116 * and P9_DOTL_NOACCESS
118 int oflags
= flags
& O_ACCMODE
;
120 DotlOpenflagMap dotl_oflag_map
[] = {
121 { P9_DOTL_CREATE
, O_CREAT
},
122 { P9_DOTL_EXCL
, O_EXCL
},
123 { P9_DOTL_NOCTTY
, O_NOCTTY
},
124 { P9_DOTL_TRUNC
, O_TRUNC
},
125 { P9_DOTL_APPEND
, O_APPEND
},
126 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
127 { P9_DOTL_DSYNC
, O_DSYNC
},
128 { P9_DOTL_FASYNC
, FASYNC
},
129 { P9_DOTL_DIRECT
, O_DIRECT
},
130 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
131 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
132 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
133 { P9_DOTL_NOATIME
, O_NOATIME
},
134 { P9_DOTL_SYNC
, O_SYNC
},
137 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
138 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
139 oflags
|= dotl_oflag_map
[i
].open_flag
;
146 void cred_init(FsCred
*credp
)
154 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
158 * Filter the client open flags
160 flags
= dotl_to_open_flags(oflags
);
161 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
163 * Ignore direct disk access hint until the server supports it.
169 void v9fs_path_init(V9fsPath
*path
)
175 void v9fs_path_free(V9fsPath
*path
)
183 void GCC_FMT_ATTR(2, 3)
184 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
188 v9fs_path_free(path
);
191 /* Bump the size for including terminating NULL */
192 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
196 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
199 dst
->size
= src
->size
;
200 dst
->data
= g_memdup(src
->data
, src
->size
);
203 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
204 const char *name
, V9fsPath
*path
)
207 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
215 * Return TRUE if s1 is an ancestor of s2.
217 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
218 * As a special case, We treat s1 as ancestor of s2 if they are same!
220 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
222 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
223 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
230 static size_t v9fs_string_size(V9fsString
*str
)
236 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
237 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
240 if (f
->fid_type
== P9_FID_FILE
) {
241 if (f
->fs
.fd
== -1) {
243 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
244 } while (err
== -EINTR
&& !pdu
->cancelled
);
246 } else if (f
->fid_type
== P9_FID_DIR
) {
247 if (f
->fs
.dir
.stream
== NULL
) {
249 err
= v9fs_co_opendir(pdu
, f
);
250 } while (err
== -EINTR
&& !pdu
->cancelled
);
256 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
260 V9fsState
*s
= pdu
->s
;
262 for (f
= s
->fid_list
; f
; f
= f
->next
) {
266 * Update the fid ref upfront so that
267 * we don't get reclaimed when we yield
272 * check whether we need to reopen the
273 * file. We might have closed the fd
274 * while trying to free up some file
277 err
= v9fs_reopen_fid(pdu
, f
);
283 * Mark the fid as referenced so that the LRU
284 * reclaim won't close the file descriptor
286 f
->flags
|= FID_REFERENCED
;
293 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
297 for (f
= s
->fid_list
; f
; f
= f
->next
) {
298 /* If fid is already there return NULL */
304 f
= g_malloc0(sizeof(V9fsFidState
));
306 f
->fid_type
= P9_FID_NONE
;
309 * Mark the fid as referenced so that the LRU
310 * reclaim won't close the file descriptor
312 f
->flags
|= FID_REFERENCED
;
313 f
->next
= s
->fid_list
;
316 v9fs_readdir_init(&f
->fs
.dir
);
317 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
322 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
326 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
327 /* getxattr/listxattr fid */
331 * if this is fid for setxattr. clunk should
332 * result in setxattr localcall
334 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
335 /* clunk after partial write */
339 if (fidp
->fs
.xattr
.len
) {
340 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
341 fidp
->fs
.xattr
.value
,
343 fidp
->fs
.xattr
.flags
);
345 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
348 v9fs_string_free(&fidp
->fs
.xattr
.name
);
350 g_free(fidp
->fs
.xattr
.value
);
354 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
358 if (fidp
->fid_type
== P9_FID_FILE
) {
359 /* If we reclaimed the fd no need to close */
360 if (fidp
->fs
.fd
!= -1) {
361 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
363 } else if (fidp
->fid_type
== P9_FID_DIR
) {
364 if (fidp
->fs
.dir
.stream
!= NULL
) {
365 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
367 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
368 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
370 v9fs_path_free(&fidp
->path
);
375 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
380 * Don't free the fid if it is in reclaim list
382 if (!fidp
->ref
&& fidp
->clunked
) {
383 if (fidp
->fid
== pdu
->s
->root_fid
) {
385 * if the clunked fid is root fid then we
386 * have unmounted the fs on the client side.
387 * delete the migration blocker. Ideally, this
388 * should be hooked to transport close notification
390 if (pdu
->s
->migration_blocker
) {
391 migrate_del_blocker(pdu
->s
->migration_blocker
);
392 error_free(pdu
->s
->migration_blocker
);
393 pdu
->s
->migration_blocker
= NULL
;
396 return free_fid(pdu
, fidp
);
401 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
403 V9fsFidState
**fidpp
, *fidp
;
405 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
406 if ((*fidpp
)->fid
== fid
) {
410 if (*fidpp
== NULL
) {
419 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
421 int reclaim_count
= 0;
422 V9fsState
*s
= pdu
->s
;
423 V9fsFidState
*f
, *reclaim_list
= NULL
;
425 for (f
= s
->fid_list
; f
; f
= f
->next
) {
427 * Unlink fids cannot be reclaimed. Check
428 * for them and skip them. Also skip fids
429 * currently being operated on.
431 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
435 * if it is a recently referenced fid
436 * we leave the fid untouched and clear the
437 * reference bit. We come back to it later
438 * in the next iteration. (a simple LRU without
439 * moving list elements around)
441 if (f
->flags
& FID_REFERENCED
) {
442 f
->flags
&= ~FID_REFERENCED
;
446 * Add fids to reclaim list.
448 if (f
->fid_type
== P9_FID_FILE
) {
449 if (f
->fs
.fd
!= -1) {
451 * Up the reference count so that
452 * a clunk request won't free this fid
455 f
->rclm_lst
= reclaim_list
;
457 f
->fs_reclaim
.fd
= f
->fs
.fd
;
461 } else if (f
->fid_type
== P9_FID_DIR
) {
462 if (f
->fs
.dir
.stream
!= NULL
) {
464 * Up the reference count so that
465 * a clunk request won't free this fid
468 f
->rclm_lst
= reclaim_list
;
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 (reclaim_list
) {
485 reclaim_list
= f
->rclm_lst
;
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
);
493 * Now drop the fid reference, free it
500 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
503 V9fsState
*s
= pdu
->s
;
504 V9fsFidState
*fidp
, head_fid
;
506 head_fid
.next
= s
->fid_list
;
507 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
508 if (fidp
->path
.size
!= path
->size
) {
511 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
512 /* Mark the fid non reclaimable. */
513 fidp
->flags
|= FID_NON_RECLAIMABLE
;
515 /* reopen the file/dir if already closed */
516 err
= v9fs_reopen_fid(pdu
, fidp
);
521 * Go back to head of fid list because
522 * the list could have got updated when
523 * switched to the worker thread
533 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
535 V9fsState
*s
= pdu
->s
;
539 while (s
->fid_list
) {
545 s
->fid_list
= fidp
->next
;
552 #define P9_QID_TYPE_DIR 0x80
553 #define P9_QID_TYPE_SYMLINK 0x02
555 #define P9_STAT_MODE_DIR 0x80000000
556 #define P9_STAT_MODE_APPEND 0x40000000
557 #define P9_STAT_MODE_EXCL 0x20000000
558 #define P9_STAT_MODE_MOUNT 0x10000000
559 #define P9_STAT_MODE_AUTH 0x08000000
560 #define P9_STAT_MODE_TMP 0x04000000
561 #define P9_STAT_MODE_SYMLINK 0x02000000
562 #define P9_STAT_MODE_LINK 0x01000000
563 #define P9_STAT_MODE_DEVICE 0x00800000
564 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
565 #define P9_STAT_MODE_SOCKET 0x00100000
566 #define P9_STAT_MODE_SETUID 0x00080000
567 #define P9_STAT_MODE_SETGID 0x00040000
568 #define P9_STAT_MODE_SETVTX 0x00010000
570 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
571 P9_STAT_MODE_SYMLINK | \
572 P9_STAT_MODE_LINK | \
573 P9_STAT_MODE_DEVICE | \
574 P9_STAT_MODE_NAMED_PIPE | \
577 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
578 static inline uint8_t mirror8bit(uint8_t byte
)
580 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
583 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
584 static inline uint64_t mirror64bit(uint64_t value
)
586 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
587 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
588 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
589 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
590 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
591 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
592 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
593 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
597 * @brief Parameter k for the Exponential Golomb algorihm to be used.
599 * The smaller this value, the smaller the minimum bit count for the Exp.
600 * Golomb generated affixes will be (at lowest index) however for the
601 * price of having higher maximum bit count of generated affixes (at highest
602 * index). Likewise increasing this parameter yields in smaller maximum bit
603 * count for the price of having higher minimum bit count.
605 * In practice that means: a good value for k depends on the expected amount
606 * of devices to be exposed by one export. For a small amount of devices k
607 * should be small, for a large amount of devices k might be increased
608 * instead. The default of k=0 should be fine for most users though.
610 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
611 * k should not change as long as guest is still running! Because that would
612 * cause completely different inode numbers to be generated on guest.
614 #define EXP_GOLOMB_K 0
617 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
619 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
620 * with growing length and with the mathematical property of being
621 * "prefix-free". The latter means the generated prefixes can be prepended
622 * in front of arbitrary numbers and the resulting concatenated numbers are
623 * guaranteed to be always unique.
625 * This is a minor adjustment to the original Exp. Golomb algorithm in the
626 * sense that lowest allowed index (@param n) starts with 1, not with zero.
628 * @param n - natural number (or index) of the prefix to be generated
630 * @param k - parameter k of Exp. Golomb algorithm to be used
631 * (see comment on EXP_GOLOMB_K macro for details about k)
633 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
635 const uint64_t value
= n
+ (1 << k
) - 1;
636 const int bits
= (int) log2(value
) + 1;
637 return (VariLenAffix
) {
638 .type
= AffixType_Prefix
,
640 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
645 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
647 * Simply mirror all bits of the affix value, for the purpose to preserve
648 * respectively the mathematical "prefix-free" or "suffix-free" property
649 * after the conversion.
651 * If a passed prefix is suitable to create unique numbers, then the
652 * returned suffix is suitable to create unique numbers as well (and vice
655 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
657 return (VariLenAffix
) {
659 (affix
->type
== AffixType_Suffix
) ?
660 AffixType_Prefix
: AffixType_Suffix
,
662 mirror64bit(affix
->value
) >>
663 ((sizeof(affix
->value
) * 8) - affix
->bits
),
669 * @brief Generates suffix numbers with "suffix-free" property.
671 * This is just a wrapper function on top of the Exp. Golomb algorithm.
673 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
674 * this function converts the Exp. Golomb prefixes into appropriate suffixes
675 * which are still suitable for generating unique numbers.
677 * @param n - natural number (or index) of the suffix to be generated
680 static VariLenAffix
affixForIndex(uint64_t index
)
683 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
684 return invertAffix(&prefix
); /* convert prefix to suffix */
687 /* creative abuse of tb_hash_func7, which is based on xxhash */
688 static uint32_t qpp_hash(QppEntry e
)
690 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
693 static uint32_t qpf_hash(QpfEntry e
)
695 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
698 static bool qpd_cmp_func(const void *obj
, const void *userp
)
700 const QpdEntry
*e1
= obj
, *e2
= userp
;
701 return e1
->dev
== e2
->dev
;
704 static bool qpp_cmp_func(const void *obj
, const void *userp
)
706 const QppEntry
*e1
= obj
, *e2
= userp
;
707 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
710 static bool qpf_cmp_func(const void *obj
, const void *userp
)
712 const QpfEntry
*e1
= obj
, *e2
= userp
;
713 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
716 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
721 static void qp_table_destroy(struct qht
*ht
)
723 if (!ht
|| !ht
->map
) {
726 qht_iter(ht
, qp_table_remove
, NULL
);
730 static void qpd_table_init(struct qht
*ht
)
732 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
735 static void qpp_table_init(struct qht
*ht
)
737 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
740 static void qpf_table_init(struct qht
*ht
)
742 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
746 * Returns how many (high end) bits of inode numbers of the passed fs
747 * device shall be used (in combination with the device number) to
748 * generate hash values for qpp_table entries.
750 * This function is required if variable length suffixes are used for inode
751 * number mapping on guest level. Since a device may end up having multiple
752 * entries in qpp_table, each entry most probably with a different suffix
753 * length, we thus need this function in conjunction with qpd_table to
754 * "agree" about a fix amount of bits (per device) to be always used for
755 * generating hash values for the purpose of accessing qpp_table in order
756 * get consistent behaviour when accessing qpp_table.
758 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
766 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
768 val
= g_malloc0(sizeof(QpdEntry
));
770 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
771 val
->prefix_bits
= affix
.bits
;
772 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
773 pdu
->s
->qp_ndevices
++;
775 return val
->prefix_bits
;
779 * @brief Slow / full mapping host inode nr -> guest inode nr.
781 * This function performs a slower and much more costly remapping of an
782 * original file inode number on host to an appropriate different inode
783 * number on guest. For every (dev, inode) combination on host a new
784 * sequential number is generated, cached and exposed as inode number on
787 * This is just a "last resort" fallback solution if the much faster/cheaper
788 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
789 * expected ever to be used at all though.
791 * @see qid_path_suffixmap() for details
794 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
798 .dev
= stbuf
->st_dev
,
801 uint32_t hash
= qpf_hash(lookup
);
804 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
807 if (pdu
->s
->qp_fullpath_next
== 0) {
808 /* no more files can be mapped :'( */
810 "9p: No more prefixes available for remapping inodes from "
816 val
= g_malloc0(sizeof(QppEntry
));
819 /* new unique inode and device combo */
820 affix
= affixForIndex(
821 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
823 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
824 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
825 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
833 * @brief Quick mapping host inode nr -> guest inode nr.
835 * This function performs quick remapping of an original file inode number
836 * on host to an appropriate different inode number on guest. This remapping
837 * of inodes is required to avoid inode nr collisions on guest which would
838 * happen if the 9p export contains more than 1 exported file system (or
839 * more than 1 file system data set), because unlike on host level where the
840 * files would have different device nrs, all files exported by 9p would
841 * share the same device nr on guest (the device nr of the virtual 9p device
844 * Inode remapping is performed by chopping off high end bits of the original
845 * inode number from host, shifting the result upwards and then assigning a
846 * generated suffix number for the low end bits, where the same suffix number
847 * will be shared by all inodes with the same device id AND the same high end
848 * bits that have been chopped off. That approach utilizes the fact that inode
849 * numbers very likely share the same high end bits (i.e. due to their common
850 * sequential generation by file systems) and hence we only have to generate
851 * and track a very limited amount of suffixes in practice due to that.
853 * We generate variable size suffixes for that purpose. The 1st generated
854 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
855 * the original inode number. The subsequent suffixes being generated will
856 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
857 * generated will have 3 bits and hence we have to chop off 3 bits from their
858 * original inodes, and so on. That approach of using variable length suffixes
859 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
860 * limited amount of devices are shared by the same export (e.g. typically
861 * less than 2 dozen devices per 9p export), so in practice we need to chop
862 * off less bits than with fixed size prefixes and yet are flexible to add
863 * new devices at runtime below host's export directory at any time without
864 * having to reboot guest nor requiring to reconfigure guest for that. And due
865 * to the very limited amount of original high end bits that we chop off that
866 * way, the total amount of suffixes we need to generate is less than by using
867 * fixed size prefixes and hence it also improves performance of the inode
868 * remapping algorithm, and finally has the nice side effect that the inode
869 * numbers on guest will be much smaller & human friendly. ;-)
871 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
874 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
876 .dev
= stbuf
->st_dev
,
877 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
879 uint32_t hash
= qpp_hash(lookup
);
881 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
884 if (pdu
->s
->qp_affix_next
== 0) {
885 /* we ran out of affixes */
887 "9p: Potential degraded performance of inode remapping"
892 val
= g_malloc0(sizeof(QppEntry
));
895 /* new unique inode affix and device combo */
896 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
897 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
898 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
900 /* assuming generated affix to be suffix type, not prefix */
901 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
905 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
910 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
911 /* map inode+device to qid path (fast path) */
912 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
913 if (err
== -ENFILE
) {
914 /* fast path didn't work, fall back to full map */
915 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
921 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
922 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
924 "9p: Multiple devices detected in same VirtFS export. "
925 "Access of guest to additional devices is (partly) "
926 "denied due to virtfs option 'multidevs=forbid' being "
932 "9p: Multiple devices detected in same VirtFS export, "
933 "which might lead to file ID collisions and severe "
934 "misbehaviours on guest! You should either use a "
935 "separate export for each device shared from host or "
936 "use virtfs option 'multidevs=remap'!"
940 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
941 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
942 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
945 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
947 if (S_ISDIR(stbuf
->st_mode
)) {
948 qidp
->type
|= P9_QID_TYPE_DIR
;
950 if (S_ISLNK(stbuf
->st_mode
)) {
951 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
957 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
963 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
967 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
974 static int coroutine_fn
dirent_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
975 struct dirent
*dent
, V9fsQID
*qidp
)
981 v9fs_path_init(&path
);
983 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
987 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
991 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
994 v9fs_path_free(&path
);
998 V9fsPDU
*pdu_alloc(V9fsState
*s
)
1000 V9fsPDU
*pdu
= NULL
;
1002 if (!QLIST_EMPTY(&s
->free_list
)) {
1003 pdu
= QLIST_FIRST(&s
->free_list
);
1004 QLIST_REMOVE(pdu
, next
);
1005 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
1010 void pdu_free(V9fsPDU
*pdu
)
1012 V9fsState
*s
= pdu
->s
;
1014 g_assert(!pdu
->cancelled
);
1015 QLIST_REMOVE(pdu
, next
);
1016 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
1019 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
1021 int8_t id
= pdu
->id
+ 1; /* Response */
1022 V9fsState
*s
= pdu
->s
;
1026 * The 9p spec requires that successfully cancelled pdus receive no reply.
1027 * Sending a reply would confuse clients because they would
1028 * assume that any EINTR is the actual result of the operation,
1029 * rather than a consequence of the cancellation. However, if
1030 * the operation completed (succesfully or with an error other
1031 * than caused be cancellation), we do send out that reply, both
1032 * for efficiency and to avoid confusing the rest of the state machine
1033 * that assumes passing a non-error here will mean a successful
1034 * transmission of the reply.
1036 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1038 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1047 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1050 str
.data
= strerror(err
);
1051 str
.size
= strlen(str
.data
);
1053 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1061 ret
= pdu_marshal(pdu
, len
, "d", err
);
1067 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1070 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1073 /* fill out the header */
1074 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1078 /* keep these in sync */
1083 pdu
->s
->transport
->push_and_notify(pdu
);
1085 /* Now wakeup anybody waiting in flush for this request */
1086 if (!qemu_co_queue_next(&pdu
->complete
)) {
1091 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1096 if (mode
& P9_STAT_MODE_DIR
) {
1100 if (mode
& P9_STAT_MODE_SYMLINK
) {
1103 if (mode
& P9_STAT_MODE_SOCKET
) {
1106 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1109 if (mode
& P9_STAT_MODE_DEVICE
) {
1110 if (extension
->size
&& extension
->data
[0] == 'c') {
1121 if (mode
& P9_STAT_MODE_SETUID
) {
1124 if (mode
& P9_STAT_MODE_SETGID
) {
1127 if (mode
& P9_STAT_MODE_SETVTX
) {
1134 static int donttouch_stat(V9fsStat
*stat
)
1136 if (stat
->type
== -1 &&
1138 stat
->qid
.type
== 0xff &&
1139 stat
->qid
.version
== (uint32_t) -1 &&
1140 stat
->qid
.path
== (uint64_t) -1 &&
1142 stat
->atime
== -1 &&
1143 stat
->mtime
== -1 &&
1144 stat
->length
== -1 &&
1149 stat
->n_uid
== -1 &&
1150 stat
->n_gid
== -1 &&
1151 stat
->n_muid
== -1) {
1158 static void v9fs_stat_init(V9fsStat
*stat
)
1160 v9fs_string_init(&stat
->name
);
1161 v9fs_string_init(&stat
->uid
);
1162 v9fs_string_init(&stat
->gid
);
1163 v9fs_string_init(&stat
->muid
);
1164 v9fs_string_init(&stat
->extension
);
1167 static void v9fs_stat_free(V9fsStat
*stat
)
1169 v9fs_string_free(&stat
->name
);
1170 v9fs_string_free(&stat
->uid
);
1171 v9fs_string_free(&stat
->gid
);
1172 v9fs_string_free(&stat
->muid
);
1173 v9fs_string_free(&stat
->extension
);
1176 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1180 mode
= stbuf
->st_mode
& 0777;
1181 if (S_ISDIR(stbuf
->st_mode
)) {
1182 mode
|= P9_STAT_MODE_DIR
;
1185 if (S_ISLNK(stbuf
->st_mode
)) {
1186 mode
|= P9_STAT_MODE_SYMLINK
;
1189 if (S_ISSOCK(stbuf
->st_mode
)) {
1190 mode
|= P9_STAT_MODE_SOCKET
;
1193 if (S_ISFIFO(stbuf
->st_mode
)) {
1194 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1197 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1198 mode
|= P9_STAT_MODE_DEVICE
;
1201 if (stbuf
->st_mode
& S_ISUID
) {
1202 mode
|= P9_STAT_MODE_SETUID
;
1205 if (stbuf
->st_mode
& S_ISGID
) {
1206 mode
|= P9_STAT_MODE_SETGID
;
1209 if (stbuf
->st_mode
& S_ISVTX
) {
1210 mode
|= P9_STAT_MODE_SETVTX
;
1216 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1217 const char *basename
,
1218 const struct stat
*stbuf
,
1223 memset(v9stat
, 0, sizeof(*v9stat
));
1225 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1229 v9stat
->mode
= stat_to_v9mode(stbuf
);
1230 v9stat
->atime
= stbuf
->st_atime
;
1231 v9stat
->mtime
= stbuf
->st_mtime
;
1232 v9stat
->length
= stbuf
->st_size
;
1234 v9fs_string_free(&v9stat
->uid
);
1235 v9fs_string_free(&v9stat
->gid
);
1236 v9fs_string_free(&v9stat
->muid
);
1238 v9stat
->n_uid
= stbuf
->st_uid
;
1239 v9stat
->n_gid
= stbuf
->st_gid
;
1242 v9fs_string_free(&v9stat
->extension
);
1244 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1245 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1249 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1250 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1251 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1252 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1253 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1254 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1255 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1258 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1261 v9fs_string_size(&v9stat
->name
) +
1262 v9fs_string_size(&v9stat
->uid
) +
1263 v9fs_string_size(&v9stat
->gid
) +
1264 v9fs_string_size(&v9stat
->muid
) +
1265 v9fs_string_size(&v9stat
->extension
);
1269 #define P9_STATS_MODE 0x00000001ULL
1270 #define P9_STATS_NLINK 0x00000002ULL
1271 #define P9_STATS_UID 0x00000004ULL
1272 #define P9_STATS_GID 0x00000008ULL
1273 #define P9_STATS_RDEV 0x00000010ULL
1274 #define P9_STATS_ATIME 0x00000020ULL
1275 #define P9_STATS_MTIME 0x00000040ULL
1276 #define P9_STATS_CTIME 0x00000080ULL
1277 #define P9_STATS_INO 0x00000100ULL
1278 #define P9_STATS_SIZE 0x00000200ULL
1279 #define P9_STATS_BLOCKS 0x00000400ULL
1281 #define P9_STATS_BTIME 0x00000800ULL
1282 #define P9_STATS_GEN 0x00001000ULL
1283 #define P9_STATS_DATA_VERSION 0x00002000ULL
1285 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1286 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1289 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1290 V9fsStatDotl
*v9lstat
)
1292 memset(v9lstat
, 0, sizeof(*v9lstat
));
1294 v9lstat
->st_mode
= stbuf
->st_mode
;
1295 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1296 v9lstat
->st_uid
= stbuf
->st_uid
;
1297 v9lstat
->st_gid
= stbuf
->st_gid
;
1298 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1299 v9lstat
->st_size
= stbuf
->st_size
;
1300 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1301 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1302 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1303 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1304 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1305 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1306 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1307 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1308 /* Currently we only support BASIC fields in stat */
1309 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1311 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1314 static void print_sg(struct iovec
*sg
, int cnt
)
1318 printf("sg[%d]: {", cnt
);
1319 for (i
= 0; i
< cnt
; i
++) {
1323 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1328 /* Will call this only for path name based fid */
1329 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1332 v9fs_path_init(&str
);
1333 v9fs_path_copy(&str
, dst
);
1334 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1335 v9fs_path_free(&str
);
1338 static inline bool is_ro_export(FsContext
*ctx
)
1340 return ctx
->export_flags
& V9FS_RDONLY
;
1343 static void coroutine_fn
v9fs_version(void *opaque
)
1346 V9fsPDU
*pdu
= opaque
;
1347 V9fsState
*s
= pdu
->s
;
1351 v9fs_string_init(&version
);
1352 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1356 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1360 if (!strcmp(version
.data
, "9P2000.u")) {
1361 s
->proto_version
= V9FS_PROTO_2000U
;
1362 } else if (!strcmp(version
.data
, "9P2000.L")) {
1363 s
->proto_version
= V9FS_PROTO_2000L
;
1365 v9fs_string_sprintf(&version
, "unknown");
1366 /* skip min. msize check, reporting invalid version has priority */
1370 if (s
->msize
< P9_MIN_MSIZE
) {
1373 "9pfs: Client requested msize < minimum msize ("
1374 stringify(P9_MIN_MSIZE
) ") supported by this server."
1380 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1385 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1387 pdu_complete(pdu
, err
);
1388 v9fs_string_free(&version
);
1391 static void coroutine_fn
v9fs_attach(void *opaque
)
1393 V9fsPDU
*pdu
= opaque
;
1394 V9fsState
*s
= pdu
->s
;
1395 int32_t fid
, afid
, n_uname
;
1396 V9fsString uname
, aname
;
1401 Error
*local_err
= NULL
;
1403 v9fs_string_init(&uname
);
1404 v9fs_string_init(&aname
);
1405 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1406 &afid
, &uname
, &aname
, &n_uname
);
1410 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1412 fidp
= alloc_fid(s
, fid
);
1417 fidp
->uid
= n_uname
;
1418 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1424 err
= fid_to_qid(pdu
, fidp
, &qid
);
1432 * disable migration if we haven't done already.
1433 * attach could get called multiple times for the same export.
1435 if (!s
->migration_blocker
) {
1436 error_setg(&s
->migration_blocker
,
1437 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1438 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1439 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1441 error_free(local_err
);
1442 error_free(s
->migration_blocker
);
1443 s
->migration_blocker
= NULL
;
1450 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1457 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1458 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1459 qid
.type
, qid
.version
, qid
.path
);
1463 pdu_complete(pdu
, err
);
1464 v9fs_string_free(&uname
);
1465 v9fs_string_free(&aname
);
1468 static void coroutine_fn
v9fs_stat(void *opaque
)
1476 V9fsPDU
*pdu
= opaque
;
1479 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1483 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1485 fidp
= get_fid(pdu
, fid
);
1490 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1494 basename
= g_path_get_basename(fidp
->path
.data
);
1495 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1500 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1502 v9fs_stat_free(&v9stat
);
1505 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1506 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1508 v9fs_stat_free(&v9stat
);
1512 pdu_complete(pdu
, err
);
1515 static void coroutine_fn
v9fs_getattr(void *opaque
)
1522 uint64_t request_mask
;
1523 V9fsStatDotl v9stat_dotl
;
1524 V9fsPDU
*pdu
= opaque
;
1526 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1530 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1532 fidp
= get_fid(pdu
, fid
);
1538 * Currently we only support BASIC fields in stat, so there is no
1539 * need to look at request_mask.
1541 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1545 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1550 /* fill st_gen if requested and supported by underlying fs */
1551 if (request_mask
& P9_STATS_GEN
) {
1552 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1555 /* we have valid st_gen: update result mask */
1556 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1559 /* request cancelled, e.g. by Tflush */
1562 /* failed to get st_gen: not fatal, ignore */
1566 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1571 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1572 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1573 v9stat_dotl
.st_gid
);
1577 pdu_complete(pdu
, retval
);
1580 /* Attribute flags */
1581 #define P9_ATTR_MODE (1 << 0)
1582 #define P9_ATTR_UID (1 << 1)
1583 #define P9_ATTR_GID (1 << 2)
1584 #define P9_ATTR_SIZE (1 << 3)
1585 #define P9_ATTR_ATIME (1 << 4)
1586 #define P9_ATTR_MTIME (1 << 5)
1587 #define P9_ATTR_CTIME (1 << 6)
1588 #define P9_ATTR_ATIME_SET (1 << 7)
1589 #define P9_ATTR_MTIME_SET (1 << 8)
1591 #define P9_ATTR_MASK 127
1593 static void coroutine_fn
v9fs_setattr(void *opaque
)
1600 V9fsPDU
*pdu
= opaque
;
1602 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1607 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1608 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1609 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1611 fidp
= get_fid(pdu
, fid
);
1616 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1617 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1622 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1623 struct timespec times
[2];
1624 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1625 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1626 times
[0].tv_sec
= v9iattr
.atime_sec
;
1627 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1629 times
[0].tv_nsec
= UTIME_NOW
;
1632 times
[0].tv_nsec
= UTIME_OMIT
;
1634 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1635 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1636 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1637 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1639 times
[1].tv_nsec
= UTIME_NOW
;
1642 times
[1].tv_nsec
= UTIME_OMIT
;
1644 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1650 * If the only valid entry in iattr is ctime we can call
1651 * chown(-1,-1) to update the ctime of the file
1653 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1654 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1655 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1656 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1659 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1662 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1668 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1669 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1675 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1679 pdu_complete(pdu
, err
);
1682 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1688 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1693 for (i
= 0; i
< nwnames
; i
++) {
1694 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1703 static bool name_is_illegal(const char *name
)
1705 return !*name
|| strchr(name
, '/') != NULL
;
1708 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1711 qid1
->type
!= qid2
->type
||
1712 qid1
->version
!= qid2
->version
||
1713 qid1
->path
!= qid2
->path
;
1716 static void coroutine_fn
v9fs_walk(void *opaque
)
1719 V9fsQID
*qids
= NULL
;
1721 V9fsPath dpath
, path
;
1725 int32_t fid
, newfid
;
1726 V9fsString
*wnames
= NULL
;
1728 V9fsFidState
*newfidp
= NULL
;
1729 V9fsPDU
*pdu
= opaque
;
1730 V9fsState
*s
= pdu
->s
;
1733 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1735 pdu_complete(pdu
, err
);
1740 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1742 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1743 wnames
= g_new0(V9fsString
, nwnames
);
1744 qids
= g_new0(V9fsQID
, nwnames
);
1745 for (i
= 0; i
< nwnames
; i
++) {
1746 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1750 if (name_is_illegal(wnames
[i
].data
)) {
1756 } else if (nwnames
> P9_MAXWELEM
) {
1760 fidp
= get_fid(pdu
, fid
);
1766 v9fs_path_init(&dpath
);
1767 v9fs_path_init(&path
);
1769 err
= fid_to_qid(pdu
, fidp
, &qid
);
1775 * Both dpath and path initially poin to fidp.
1776 * Needed to handle request with nwnames == 0
1778 v9fs_path_copy(&dpath
, &fidp
->path
);
1779 v9fs_path_copy(&path
, &fidp
->path
);
1780 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1781 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1782 strcmp("..", wnames
[name_idx
].data
)) {
1783 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1789 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1793 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1797 v9fs_path_copy(&dpath
, &path
);
1799 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1801 if (fid
== newfid
) {
1802 if (fidp
->fid_type
!= P9_FID_NONE
) {
1806 v9fs_path_write_lock(s
);
1807 v9fs_path_copy(&fidp
->path
, &path
);
1808 v9fs_path_unlock(s
);
1810 newfidp
= alloc_fid(s
, newfid
);
1811 if (newfidp
== NULL
) {
1815 newfidp
->uid
= fidp
->uid
;
1816 v9fs_path_copy(&newfidp
->path
, &path
);
1818 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1819 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1823 put_fid(pdu
, newfidp
);
1825 v9fs_path_free(&dpath
);
1826 v9fs_path_free(&path
);
1828 pdu_complete(pdu
, err
);
1829 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1830 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1831 v9fs_string_free(&wnames
[name_idx
]);
1838 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1840 struct statfs stbuf
;
1842 V9fsState
*s
= pdu
->s
;
1845 * iounit should be multiples of f_bsize (host filesystem block size
1846 * and as well as less than (client msize - P9_IOHDRSZ))
1848 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1849 if (stbuf
.f_bsize
) {
1850 iounit
= stbuf
.f_bsize
;
1851 iounit
*= (s
->msize
- P9_IOHDRSZ
) / stbuf
.f_bsize
;
1855 iounit
= s
->msize
- P9_IOHDRSZ
;
1860 static void coroutine_fn
v9fs_open(void *opaque
)
1871 V9fsPDU
*pdu
= opaque
;
1872 V9fsState
*s
= pdu
->s
;
1874 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1875 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1878 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1884 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1886 fidp
= get_fid(pdu
, fid
);
1891 if (fidp
->fid_type
!= P9_FID_NONE
) {
1896 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1900 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1904 if (S_ISDIR(stbuf
.st_mode
)) {
1905 err
= v9fs_co_opendir(pdu
, fidp
);
1909 fidp
->fid_type
= P9_FID_DIR
;
1910 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1916 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1917 flags
= get_dotl_openflags(s
, mode
);
1919 flags
= omode_to_uflags(mode
);
1921 if (is_ro_export(&s
->ctx
)) {
1922 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1923 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1928 err
= v9fs_co_open(pdu
, fidp
, flags
);
1932 fidp
->fid_type
= P9_FID_FILE
;
1933 fidp
->open_flags
= flags
;
1934 if (flags
& O_EXCL
) {
1936 * We let the host file system do O_EXCL check
1937 * We should not reclaim such fd
1939 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1941 iounit
= get_iounit(pdu
, &fidp
->path
);
1942 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1948 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1949 qid
.type
, qid
.version
, qid
.path
, iounit
);
1953 pdu_complete(pdu
, err
);
1956 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1958 int32_t dfid
, flags
, mode
;
1967 V9fsPDU
*pdu
= opaque
;
1969 v9fs_string_init(&name
);
1970 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1971 &name
, &flags
, &mode
, &gid
);
1975 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1977 if (name_is_illegal(name
.data
)) {
1982 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1987 fidp
= get_fid(pdu
, dfid
);
1992 if (fidp
->fid_type
!= P9_FID_NONE
) {
1997 flags
= get_dotl_openflags(pdu
->s
, flags
);
1998 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1999 flags
| O_CREAT
, mode
, &stbuf
);
2003 fidp
->fid_type
= P9_FID_FILE
;
2004 fidp
->open_flags
= flags
;
2005 if (flags
& O_EXCL
) {
2007 * We let the host file system do O_EXCL check
2008 * We should not reclaim such fd
2010 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2012 iounit
= get_iounit(pdu
, &fidp
->path
);
2013 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2017 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2022 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2023 qid
.type
, qid
.version
, qid
.path
, iounit
);
2027 pdu_complete(pdu
, err
);
2028 v9fs_string_free(&name
);
2031 static void coroutine_fn
v9fs_fsync(void *opaque
)
2038 V9fsPDU
*pdu
= opaque
;
2040 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2044 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2046 fidp
= get_fid(pdu
, fid
);
2051 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2057 pdu_complete(pdu
, err
);
2060 static void coroutine_fn
v9fs_clunk(void *opaque
)
2066 V9fsPDU
*pdu
= opaque
;
2067 V9fsState
*s
= pdu
->s
;
2069 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2073 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2075 fidp
= clunk_fid(s
, fid
);
2081 * Bump the ref so that put_fid will
2085 err
= put_fid(pdu
, fidp
);
2090 pdu_complete(pdu
, err
);
2094 * Create a QEMUIOVector for a sub-region of PDU iovecs
2096 * @qiov: uninitialized QEMUIOVector
2097 * @skip: number of bytes to skip from beginning of PDU
2098 * @size: number of bytes to include
2099 * @is_write: true - write, false - read
2101 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2102 * with qemu_iovec_destroy().
2104 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2105 size_t skip
, size_t *size
,
2111 size_t alloc_size
= *size
+ skip
;
2114 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, alloc_size
);
2116 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, &alloc_size
);
2119 if (alloc_size
< skip
) {
2122 *size
= alloc_size
- skip
;
2125 qemu_iovec_init_external(&elem
, iov
, niov
);
2126 qemu_iovec_init(qiov
, niov
);
2127 qemu_iovec_concat(qiov
, &elem
, skip
, *size
);
2130 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2131 uint64_t off
, uint32_t max_count
)
2136 QEMUIOVector qiov_full
;
2138 if (fidp
->fs
.xattr
.len
< off
) {
2140 } else if (fidp
->fs
.xattr
.len
- off
< max_count
) {
2141 read_count
= fidp
->fs
.xattr
.len
- off
;
2143 read_count
= max_count
;
2145 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2151 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, &read_count
, false);
2152 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2153 ((char *)fidp
->fs
.xattr
.value
) + off
,
2155 qemu_iovec_destroy(&qiov_full
);
2163 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2172 off_t saved_dir_pos
;
2173 struct dirent
*dent
;
2175 /* save the directory position */
2176 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2177 if (saved_dir_pos
< 0) {
2178 return saved_dir_pos
;
2182 v9fs_path_init(&path
);
2184 v9fs_readdir_lock(&fidp
->fs
.dir
);
2186 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2190 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2194 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2198 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2202 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2203 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2205 /* Ran out of buffer. Set dir back to old position and return */
2206 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2207 v9fs_stat_free(&v9stat
);
2208 v9fs_path_free(&path
);
2212 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2213 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2215 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2218 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2219 v9fs_stat_free(&v9stat
);
2220 v9fs_path_free(&path
);
2224 v9fs_stat_free(&v9stat
);
2225 v9fs_path_free(&path
);
2226 saved_dir_pos
= dent
->d_off
;
2229 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2231 v9fs_path_free(&path
);
2238 static void coroutine_fn
v9fs_read(void *opaque
)
2247 V9fsPDU
*pdu
= opaque
;
2248 V9fsState
*s
= pdu
->s
;
2250 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2254 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2256 fidp
= get_fid(pdu
, fid
);
2261 if (fidp
->fid_type
== P9_FID_DIR
) {
2264 v9fs_co_rewinddir(pdu
, fidp
);
2266 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2271 err
= pdu_marshal(pdu
, offset
, "d", count
);
2275 err
+= offset
+ count
;
2276 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2277 QEMUIOVector qiov_full
;
2280 size_t size
= max_count
;
2282 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, &size
, false);
2283 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2286 qemu_iovec_reset(&qiov
);
2287 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2289 print_sg(qiov
.iov
, qiov
.niov
);
2291 /* Loop in case of EINTR */
2293 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2298 } while (len
== -EINTR
&& !pdu
->cancelled
);
2300 /* IO error return the error */
2302 goto out_free_iovec
;
2304 } while (count
< max_count
&& len
> 0);
2305 err
= pdu_marshal(pdu
, offset
, "d", count
);
2307 goto out_free_iovec
;
2309 err
+= offset
+ count
;
2311 qemu_iovec_destroy(&qiov
);
2312 qemu_iovec_destroy(&qiov_full
);
2313 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2314 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2318 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2322 pdu_complete(pdu
, err
);
2325 static size_t v9fs_readdir_data_size(V9fsString
*name
)
2328 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2329 * size of type (1) + size of name.size (2) + strlen(name.data)
2331 return 24 + v9fs_string_size(name
);
2334 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2342 off_t saved_dir_pos
;
2343 struct dirent
*dent
;
2345 /* save the directory position */
2346 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2347 if (saved_dir_pos
< 0) {
2348 return saved_dir_pos
;
2352 v9fs_readdir_lock(&fidp
->fs
.dir
);
2354 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2358 v9fs_string_init(&name
);
2359 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2360 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
2361 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2363 /* Ran out of buffer. Set dir back to old position and return */
2364 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2365 v9fs_string_free(&name
);
2369 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2371 * dirent_to_qid() implies expensive stat call for each entry,
2372 * we must do that here though since inode remapping requires
2373 * the device id, which in turn might be different for
2374 * different entries; we cannot make any assumption to avoid
2377 err
= dirent_to_qid(pdu
, fidp
, dent
, &qid
);
2379 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2380 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2381 v9fs_string_free(&name
);
2386 * Fill up just the path field of qid because the client uses
2387 * only that. To fill the entire qid structure we will have
2388 * to stat each dirent found, which is expensive. For the
2389 * latter reason we don't call dirent_to_qid() here. Only drawback
2390 * is that no multi-device export detection of stat_to_qid()
2391 * would be done and provided as error to the user here. But
2392 * user would get that error anyway when accessing those
2393 * files/dirs through other ways.
2395 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2396 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2397 /* Fill the other fields with dummy values */
2402 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2403 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2405 dent
->d_type
, &name
);
2407 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2410 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2411 v9fs_string_free(&name
);
2415 v9fs_string_free(&name
);
2416 saved_dir_pos
= dent
->d_off
;
2419 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2427 static void coroutine_fn
v9fs_readdir(void *opaque
)
2433 uint64_t initial_offset
;
2436 V9fsPDU
*pdu
= opaque
;
2437 V9fsState
*s
= pdu
->s
;
2439 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2440 &initial_offset
, &max_count
);
2444 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2446 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2447 if (max_count
> s
->msize
- 11) {
2448 max_count
= s
->msize
- 11;
2450 "9p: bad client: T_readdir with count > msize - 11"
2454 fidp
= get_fid(pdu
, fid
);
2459 if (!fidp
->fs
.dir
.stream
) {
2463 if (initial_offset
== 0) {
2464 v9fs_co_rewinddir(pdu
, fidp
);
2466 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2468 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2473 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2477 retval
+= count
+ offset
;
2478 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2482 pdu_complete(pdu
, retval
);
2485 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2486 uint64_t off
, uint32_t count
,
2487 struct iovec
*sg
, int cnt
)
2491 uint64_t write_count
;
2495 if (fidp
->fs
.xattr
.len
< off
) {
2498 write_count
= fidp
->fs
.xattr
.len
- off
;
2499 if (write_count
> count
) {
2500 write_count
= count
;
2502 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2507 fidp
->fs
.xattr
.copied_len
+= write_count
;
2509 * Now copy the content from sg list
2511 for (i
= 0; i
< cnt
; i
++) {
2512 if (write_count
> sg
[i
].iov_len
) {
2513 to_copy
= sg
[i
].iov_len
;
2515 to_copy
= write_count
;
2517 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2518 /* updating vs->off since we are not using below */
2520 write_count
-= to_copy
;
2526 static void coroutine_fn
v9fs_write(void *opaque
)
2537 V9fsPDU
*pdu
= opaque
;
2538 V9fsState
*s
= pdu
->s
;
2539 QEMUIOVector qiov_full
;
2542 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2544 pdu_complete(pdu
, err
);
2549 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, &size
, true);
2551 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2553 fidp
= get_fid(pdu
, fid
);
2558 if (fidp
->fid_type
== P9_FID_FILE
) {
2559 if (fidp
->fs
.fd
== -1) {
2563 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2565 * setxattr operation
2567 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2568 qiov_full
.iov
, qiov_full
.niov
);
2574 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2576 qemu_iovec_reset(&qiov
);
2577 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2579 print_sg(qiov
.iov
, qiov
.niov
);
2581 /* Loop in case of EINTR */
2583 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2588 } while (len
== -EINTR
&& !pdu
->cancelled
);
2590 /* IO error return the error */
2594 } while (total
< count
&& len
> 0);
2597 err
= pdu_marshal(pdu
, offset
, "d", total
);
2602 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2604 qemu_iovec_destroy(&qiov
);
2608 qemu_iovec_destroy(&qiov_full
);
2609 pdu_complete(pdu
, err
);
2612 static void coroutine_fn
v9fs_create(void *opaque
)
2624 V9fsString extension
;
2626 V9fsPDU
*pdu
= opaque
;
2627 V9fsState
*s
= pdu
->s
;
2629 v9fs_path_init(&path
);
2630 v9fs_string_init(&name
);
2631 v9fs_string_init(&extension
);
2632 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2633 &perm
, &mode
, &extension
);
2637 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2639 if (name_is_illegal(name
.data
)) {
2644 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2649 fidp
= get_fid(pdu
, fid
);
2654 if (fidp
->fid_type
!= P9_FID_NONE
) {
2658 if (perm
& P9_STAT_MODE_DIR
) {
2659 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2660 fidp
->uid
, -1, &stbuf
);
2664 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2668 v9fs_path_write_lock(s
);
2669 v9fs_path_copy(&fidp
->path
, &path
);
2670 v9fs_path_unlock(s
);
2671 err
= v9fs_co_opendir(pdu
, fidp
);
2675 fidp
->fid_type
= P9_FID_DIR
;
2676 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2677 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2678 extension
.data
, -1 , &stbuf
);
2682 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2686 v9fs_path_write_lock(s
);
2687 v9fs_path_copy(&fidp
->path
, &path
);
2688 v9fs_path_unlock(s
);
2689 } else if (perm
& P9_STAT_MODE_LINK
) {
2690 int32_t ofid
= atoi(extension
.data
);
2691 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2692 if (ofidp
== NULL
) {
2696 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2697 put_fid(pdu
, ofidp
);
2701 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2703 fidp
->fid_type
= P9_FID_NONE
;
2706 v9fs_path_write_lock(s
);
2707 v9fs_path_copy(&fidp
->path
, &path
);
2708 v9fs_path_unlock(s
);
2709 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2711 fidp
->fid_type
= P9_FID_NONE
;
2714 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2716 uint32_t major
, minor
;
2719 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2736 nmode
|= perm
& 0777;
2737 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2738 makedev(major
, minor
), nmode
, &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_NAMED_PIPE
) {
2750 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2751 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2755 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2759 v9fs_path_write_lock(s
);
2760 v9fs_path_copy(&fidp
->path
, &path
);
2761 v9fs_path_unlock(s
);
2762 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2763 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2764 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2768 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2772 v9fs_path_write_lock(s
);
2773 v9fs_path_copy(&fidp
->path
, &path
);
2774 v9fs_path_unlock(s
);
2776 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2777 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2781 fidp
->fid_type
= P9_FID_FILE
;
2782 fidp
->open_flags
= omode_to_uflags(mode
);
2783 if (fidp
->open_flags
& O_EXCL
) {
2785 * We let the host file system do O_EXCL check
2786 * We should not reclaim such fd
2788 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2791 iounit
= get_iounit(pdu
, &fidp
->path
);
2792 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2796 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2801 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2802 qid
.type
, qid
.version
, qid
.path
, iounit
);
2806 pdu_complete(pdu
, err
);
2807 v9fs_string_free(&name
);
2808 v9fs_string_free(&extension
);
2809 v9fs_path_free(&path
);
2812 static void coroutine_fn
v9fs_symlink(void *opaque
)
2814 V9fsPDU
*pdu
= opaque
;
2817 V9fsFidState
*dfidp
;
2825 v9fs_string_init(&name
);
2826 v9fs_string_init(&symname
);
2827 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2831 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2833 if (name_is_illegal(name
.data
)) {
2838 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2843 dfidp
= get_fid(pdu
, dfid
);
2844 if (dfidp
== NULL
) {
2848 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2852 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2856 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2861 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2862 qid
.type
, qid
.version
, qid
.path
);
2864 put_fid(pdu
, dfidp
);
2866 pdu_complete(pdu
, err
);
2867 v9fs_string_free(&name
);
2868 v9fs_string_free(&symname
);
2871 static void coroutine_fn
v9fs_flush(void *opaque
)
2876 V9fsPDU
*cancel_pdu
= NULL
;
2877 V9fsPDU
*pdu
= opaque
;
2878 V9fsState
*s
= pdu
->s
;
2880 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2882 pdu_complete(pdu
, err
);
2885 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2887 if (pdu
->tag
== tag
) {
2888 warn_report("the guest sent a self-referencing 9P flush request");
2890 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2891 if (cancel_pdu
->tag
== tag
) {
2897 cancel_pdu
->cancelled
= 1;
2899 * Wait for pdu to complete.
2901 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2902 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2903 cancel_pdu
->cancelled
= 0;
2904 pdu_free(cancel_pdu
);
2907 pdu_complete(pdu
, 7);
2910 static void coroutine_fn
v9fs_link(void *opaque
)
2912 V9fsPDU
*pdu
= opaque
;
2913 int32_t dfid
, oldfid
;
2914 V9fsFidState
*dfidp
, *oldfidp
;
2919 v9fs_string_init(&name
);
2920 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2924 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2926 if (name_is_illegal(name
.data
)) {
2931 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2936 dfidp
= get_fid(pdu
, dfid
);
2937 if (dfidp
== NULL
) {
2942 oldfidp
= get_fid(pdu
, oldfid
);
2943 if (oldfidp
== NULL
) {
2947 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2951 put_fid(pdu
, oldfidp
);
2953 put_fid(pdu
, dfidp
);
2955 v9fs_string_free(&name
);
2956 pdu_complete(pdu
, err
);
2959 /* Only works with path name based fid */
2960 static void coroutine_fn
v9fs_remove(void *opaque
)
2966 V9fsPDU
*pdu
= opaque
;
2968 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2972 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2974 fidp
= get_fid(pdu
, fid
);
2979 /* if fs driver is not path based, return EOPNOTSUPP */
2980 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2985 * IF the file is unlinked, we cannot reopen
2986 * the file later. So don't reclaim fd
2988 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2992 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2997 /* For TREMOVE we need to clunk the fid even on failed remove */
2998 clunk_fid(pdu
->s
, fidp
->fid
);
3001 pdu_complete(pdu
, err
);
3004 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
3008 int32_t dfid
, flags
, rflags
= 0;
3011 V9fsFidState
*dfidp
;
3012 V9fsPDU
*pdu
= opaque
;
3014 v9fs_string_init(&name
);
3015 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3020 if (name_is_illegal(name
.data
)) {
3025 if (!strcmp(".", name
.data
)) {
3030 if (!strcmp("..", name
.data
)) {
3035 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3040 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3041 rflags
|= AT_REMOVEDIR
;
3044 dfidp
= get_fid(pdu
, dfid
);
3045 if (dfidp
== NULL
) {
3050 * IF the file is unlinked, we cannot reopen
3051 * the file later. So don't reclaim fd
3053 v9fs_path_init(&path
);
3054 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3058 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3062 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3067 put_fid(pdu
, dfidp
);
3068 v9fs_path_free(&path
);
3070 pdu_complete(pdu
, err
);
3071 v9fs_string_free(&name
);
3075 /* Only works with path name based fid */
3076 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3082 V9fsFidState
*tfidp
;
3083 V9fsState
*s
= pdu
->s
;
3084 V9fsFidState
*dirfidp
= NULL
;
3086 v9fs_path_init(&new_path
);
3087 if (newdirfid
!= -1) {
3088 dirfidp
= get_fid(pdu
, newdirfid
);
3089 if (dirfidp
== NULL
) {
3092 if (fidp
->fid_type
!= P9_FID_NONE
) {
3096 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3101 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3104 v9fs_path_init(&dir_path
);
3105 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3108 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3109 v9fs_path_free(&dir_path
);
3114 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3119 * Fixup fid's pointing to the old name to
3120 * start pointing to the new name
3122 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
3123 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3124 /* replace the name */
3125 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3130 put_fid(pdu
, dirfidp
);
3132 v9fs_path_free(&new_path
);
3136 /* Only works with path name based fid */
3137 static void coroutine_fn
v9fs_rename(void *opaque
)
3145 V9fsPDU
*pdu
= opaque
;
3146 V9fsState
*s
= pdu
->s
;
3148 v9fs_string_init(&name
);
3149 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3154 if (name_is_illegal(name
.data
)) {
3159 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3164 fidp
= get_fid(pdu
, fid
);
3169 if (fidp
->fid_type
!= P9_FID_NONE
) {
3173 /* if fs driver is not path based, return EOPNOTSUPP */
3174 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3178 v9fs_path_write_lock(s
);
3179 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3180 v9fs_path_unlock(s
);
3187 pdu_complete(pdu
, err
);
3188 v9fs_string_free(&name
);
3191 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3192 V9fsString
*old_name
,
3194 V9fsString
*new_name
)
3196 V9fsFidState
*tfidp
;
3197 V9fsPath oldpath
, newpath
;
3198 V9fsState
*s
= pdu
->s
;
3201 v9fs_path_init(&oldpath
);
3202 v9fs_path_init(&newpath
);
3203 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3207 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3213 * Fixup fid's pointing to the old name to
3214 * start pointing to the new name
3216 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
3217 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3218 /* replace the name */
3219 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3223 v9fs_path_free(&oldpath
);
3224 v9fs_path_free(&newpath
);
3228 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3229 V9fsString
*old_name
,
3231 V9fsString
*new_name
)
3234 V9fsState
*s
= pdu
->s
;
3235 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3237 olddirfidp
= get_fid(pdu
, olddirfid
);
3238 if (olddirfidp
== NULL
) {
3242 if (newdirfid
!= -1) {
3243 newdirfidp
= get_fid(pdu
, newdirfid
);
3244 if (newdirfidp
== NULL
) {
3249 newdirfidp
= get_fid(pdu
, olddirfid
);
3252 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3253 &newdirfidp
->path
, new_name
);
3257 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3258 /* Only for path based fid we need to do the below fixup */
3259 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3260 &newdirfidp
->path
, new_name
);
3264 put_fid(pdu
, olddirfidp
);
3267 put_fid(pdu
, newdirfidp
);
3272 static void coroutine_fn
v9fs_renameat(void *opaque
)
3276 V9fsPDU
*pdu
= opaque
;
3277 V9fsState
*s
= pdu
->s
;
3278 int32_t olddirfid
, newdirfid
;
3279 V9fsString old_name
, new_name
;
3281 v9fs_string_init(&old_name
);
3282 v9fs_string_init(&new_name
);
3283 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3284 &old_name
, &newdirfid
, &new_name
);
3289 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3294 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3295 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3300 v9fs_path_write_lock(s
);
3301 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3302 &old_name
, newdirfid
, &new_name
);
3303 v9fs_path_unlock(s
);
3309 pdu_complete(pdu
, err
);
3310 v9fs_string_free(&old_name
);
3311 v9fs_string_free(&new_name
);
3314 static void coroutine_fn
v9fs_wstat(void *opaque
)
3323 V9fsPDU
*pdu
= opaque
;
3324 V9fsState
*s
= pdu
->s
;
3326 v9fs_stat_init(&v9stat
);
3327 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3331 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3332 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3334 fidp
= get_fid(pdu
, fid
);
3339 /* do we need to sync the file? */
3340 if (donttouch_stat(&v9stat
)) {
3341 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3344 if (v9stat
.mode
!= -1) {
3346 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3350 v9_mode
= stat_to_v9mode(&stbuf
);
3351 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3352 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3353 /* Attempting to change the type */
3357 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3358 v9mode_to_mode(v9stat
.mode
,
3359 &v9stat
.extension
));
3364 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3365 struct timespec times
[2];
3366 if (v9stat
.atime
!= -1) {
3367 times
[0].tv_sec
= v9stat
.atime
;
3368 times
[0].tv_nsec
= 0;
3370 times
[0].tv_nsec
= UTIME_OMIT
;
3372 if (v9stat
.mtime
!= -1) {
3373 times
[1].tv_sec
= v9stat
.mtime
;
3374 times
[1].tv_nsec
= 0;
3376 times
[1].tv_nsec
= UTIME_OMIT
;
3378 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3383 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3384 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3389 if (v9stat
.name
.size
!= 0) {
3390 v9fs_path_write_lock(s
);
3391 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3392 v9fs_path_unlock(s
);
3397 if (v9stat
.length
!= -1) {
3398 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3407 v9fs_stat_free(&v9stat
);
3408 pdu_complete(pdu
, err
);
3411 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3423 int32_t bsize_factor
;
3426 * compute bsize factor based on host file system block size
3429 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
3430 if (!bsize_factor
) {
3433 f_type
= stbuf
->f_type
;
3434 f_bsize
= stbuf
->f_bsize
;
3435 f_bsize
*= bsize_factor
;
3437 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3438 * adjust(divide) the number of blocks, free blocks and available
3439 * blocks by bsize factor
3441 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
3442 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
3443 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
3444 f_files
= stbuf
->f_files
;
3445 f_ffree
= stbuf
->f_ffree
;
3446 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3447 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3448 f_namelen
= stbuf
->f_namelen
;
3450 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3451 f_type
, f_bsize
, f_blocks
, f_bfree
,
3452 f_bavail
, f_files
, f_ffree
,
3453 fsid_val
, f_namelen
);
3456 static void coroutine_fn
v9fs_statfs(void *opaque
)
3462 struct statfs stbuf
;
3463 V9fsPDU
*pdu
= opaque
;
3464 V9fsState
*s
= pdu
->s
;
3466 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3470 fidp
= get_fid(pdu
, fid
);
3475 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3479 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3487 pdu_complete(pdu
, retval
);
3490 static void coroutine_fn
v9fs_mknod(void *opaque
)
3503 V9fsPDU
*pdu
= opaque
;
3505 v9fs_string_init(&name
);
3506 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3507 &major
, &minor
, &gid
);
3511 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3513 if (name_is_illegal(name
.data
)) {
3518 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3523 fidp
= get_fid(pdu
, fid
);
3528 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3529 makedev(major
, minor
), mode
, &stbuf
);
3533 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3537 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3542 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3543 qid
.type
, qid
.version
, qid
.path
);
3547 pdu_complete(pdu
, err
);
3548 v9fs_string_free(&name
);
3552 * Implement posix byte range locking code
3553 * Server side handling of locking code is very simple, because 9p server in
3554 * QEMU can handle only one client. And most of the lock handling
3555 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3556 * do any thing in * qemu 9p server side lock code path.
3557 * So when a TLOCK request comes, always return success
3559 static void coroutine_fn
v9fs_lock(void *opaque
)
3565 int32_t fid
, err
= 0;
3566 V9fsPDU
*pdu
= opaque
;
3568 v9fs_string_init(&flock
.client_id
);
3569 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3570 &flock
.flags
, &flock
.start
, &flock
.length
,
3571 &flock
.proc_id
, &flock
.client_id
);
3575 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3576 flock
.type
, flock
.start
, flock
.length
);
3579 /* We support only block flag now (that too ignored currently) */
3580 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3584 fidp
= get_fid(pdu
, fid
);
3589 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3593 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3598 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3602 pdu_complete(pdu
, err
);
3603 v9fs_string_free(&flock
.client_id
);
3607 * When a TGETLOCK request comes, always return success because all lock
3608 * handling is done by client's VFS layer.
3610 static void coroutine_fn
v9fs_getlock(void *opaque
)
3616 int32_t fid
, err
= 0;
3617 V9fsPDU
*pdu
= opaque
;
3619 v9fs_string_init(&glock
.client_id
);
3620 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3621 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3626 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3627 glock
.type
, glock
.start
, glock
.length
);
3629 fidp
= get_fid(pdu
, fid
);
3634 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3638 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3639 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3640 glock
.start
, glock
.length
, glock
.proc_id
,
3646 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3647 glock
.length
, glock
.proc_id
);
3651 pdu_complete(pdu
, err
);
3652 v9fs_string_free(&glock
.client_id
);
3655 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3657 V9fsPDU
*pdu
= opaque
;
3668 v9fs_string_init(&name
);
3669 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3673 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3675 if (name_is_illegal(name
.data
)) {
3680 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3685 fidp
= get_fid(pdu
, fid
);
3690 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3694 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3698 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3703 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3704 qid
.type
, qid
.version
, qid
.path
, err
);
3708 pdu_complete(pdu
, err
);
3709 v9fs_string_free(&name
);
3712 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3718 int32_t fid
, newfid
;
3719 V9fsFidState
*file_fidp
;
3720 V9fsFidState
*xattr_fidp
= NULL
;
3721 V9fsPDU
*pdu
= opaque
;
3722 V9fsState
*s
= pdu
->s
;
3724 v9fs_string_init(&name
);
3725 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3729 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3731 file_fidp
= get_fid(pdu
, fid
);
3732 if (file_fidp
== NULL
) {
3736 xattr_fidp
= alloc_fid(s
, newfid
);
3737 if (xattr_fidp
== NULL
) {
3741 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3742 if (!v9fs_string_size(&name
)) {
3744 * listxattr request. Get the size first
3746 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3749 clunk_fid(s
, xattr_fidp
->fid
);
3753 * Read the xattr value
3755 xattr_fidp
->fs
.xattr
.len
= size
;
3756 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3757 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3758 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3760 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3761 xattr_fidp
->fs
.xattr
.value
,
3762 xattr_fidp
->fs
.xattr
.len
);
3764 clunk_fid(s
, xattr_fidp
->fid
);
3768 err
= pdu_marshal(pdu
, offset
, "q", size
);
3775 * specific xattr fid. We check for xattr
3776 * presence also collect the xattr size
3778 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3782 clunk_fid(s
, xattr_fidp
->fid
);
3786 * Read the xattr value
3788 xattr_fidp
->fs
.xattr
.len
= size
;
3789 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3790 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3791 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3793 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3794 &name
, xattr_fidp
->fs
.xattr
.value
,
3795 xattr_fidp
->fs
.xattr
.len
);
3797 clunk_fid(s
, xattr_fidp
->fid
);
3801 err
= pdu_marshal(pdu
, offset
, "q", size
);
3807 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3809 put_fid(pdu
, file_fidp
);
3811 put_fid(pdu
, xattr_fidp
);
3814 pdu_complete(pdu
, err
);
3815 v9fs_string_free(&name
);
3818 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3820 int flags
, rflags
= 0;
3826 V9fsFidState
*file_fidp
;
3827 V9fsFidState
*xattr_fidp
;
3828 V9fsPDU
*pdu
= opaque
;
3830 v9fs_string_init(&name
);
3831 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3835 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3837 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3842 if (flags
& P9_XATTR_CREATE
) {
3843 rflags
|= XATTR_CREATE
;
3846 if (flags
& P9_XATTR_REPLACE
) {
3847 rflags
|= XATTR_REPLACE
;
3850 if (size
> XATTR_SIZE_MAX
) {
3855 file_fidp
= get_fid(pdu
, fid
);
3856 if (file_fidp
== NULL
) {
3860 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3865 /* Make the file fid point to xattr */
3866 xattr_fidp
= file_fidp
;
3867 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3868 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3869 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3870 xattr_fidp
->fs
.xattr
.len
= size
;
3871 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3872 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3873 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3874 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3877 put_fid(pdu
, file_fidp
);
3879 pdu_complete(pdu
, err
);
3880 v9fs_string_free(&name
);
3883 static void coroutine_fn
v9fs_readlink(void *opaque
)
3885 V9fsPDU
*pdu
= opaque
;
3892 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3896 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3897 fidp
= get_fid(pdu
, fid
);
3903 v9fs_string_init(&target
);
3904 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3908 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3910 v9fs_string_free(&target
);
3914 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3915 v9fs_string_free(&target
);
3919 pdu_complete(pdu
, err
);
3922 static CoroutineEntry
*pdu_co_handlers
[] = {
3923 [P9_TREADDIR
] = v9fs_readdir
,
3924 [P9_TSTATFS
] = v9fs_statfs
,
3925 [P9_TGETATTR
] = v9fs_getattr
,
3926 [P9_TSETATTR
] = v9fs_setattr
,
3927 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3928 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3929 [P9_TMKNOD
] = v9fs_mknod
,
3930 [P9_TRENAME
] = v9fs_rename
,
3931 [P9_TLOCK
] = v9fs_lock
,
3932 [P9_TGETLOCK
] = v9fs_getlock
,
3933 [P9_TRENAMEAT
] = v9fs_renameat
,
3934 [P9_TREADLINK
] = v9fs_readlink
,
3935 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3936 [P9_TMKDIR
] = v9fs_mkdir
,
3937 [P9_TVERSION
] = v9fs_version
,
3938 [P9_TLOPEN
] = v9fs_open
,
3939 [P9_TATTACH
] = v9fs_attach
,
3940 [P9_TSTAT
] = v9fs_stat
,
3941 [P9_TWALK
] = v9fs_walk
,
3942 [P9_TCLUNK
] = v9fs_clunk
,
3943 [P9_TFSYNC
] = v9fs_fsync
,
3944 [P9_TOPEN
] = v9fs_open
,
3945 [P9_TREAD
] = v9fs_read
,
3947 [P9_TAUTH
] = v9fs_auth
,
3949 [P9_TFLUSH
] = v9fs_flush
,
3950 [P9_TLINK
] = v9fs_link
,
3951 [P9_TSYMLINK
] = v9fs_symlink
,
3952 [P9_TCREATE
] = v9fs_create
,
3953 [P9_TLCREATE
] = v9fs_lcreate
,
3954 [P9_TWRITE
] = v9fs_write
,
3955 [P9_TWSTAT
] = v9fs_wstat
,
3956 [P9_TREMOVE
] = v9fs_remove
,
3959 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3961 V9fsPDU
*pdu
= opaque
;
3962 pdu_complete(pdu
, -EOPNOTSUPP
);
3965 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3967 V9fsPDU
*pdu
= opaque
;
3968 pdu_complete(pdu
, -EROFS
);
3971 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3998 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
4001 CoroutineEntry
*handler
;
4002 V9fsState
*s
= pdu
->s
;
4004 pdu
->size
= le32_to_cpu(hdr
->size_le
);
4006 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
4008 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
4009 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
4010 handler
= v9fs_op_not_supp
;
4011 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4012 handler
= v9fs_fs_ro
;
4014 handler
= pdu_co_handlers
[pdu
->id
];
4017 qemu_co_queue_init(&pdu
->complete
);
4018 co
= qemu_coroutine_create(handler
, pdu
);
4019 qemu_coroutine_enter(co
);
4022 /* Returns 0 on success, 1 on failure. */
4023 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4032 assert(!s
->transport
);
4035 /* initialize pdu allocator */
4036 QLIST_INIT(&s
->free_list
);
4037 QLIST_INIT(&s
->active_list
);
4038 for (i
= 0; i
< MAX_REQ
; i
++) {
4039 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4044 v9fs_path_init(&path
);
4046 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4049 /* We don't have a fsdev identified by fsdev_id */
4050 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4052 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4056 if (!s
->fsconf
.tag
) {
4057 /* we haven't specified a mount_tag */
4058 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4059 s
->fsconf
.fsdev_id
);
4063 s
->ctx
.export_flags
= fse
->export_flags
;
4064 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4065 s
->ctx
.exops
.get_st_gen
= NULL
;
4066 len
= strlen(s
->fsconf
.tag
);
4067 if (len
> MAX_TAG_LEN
- 1) {
4068 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4069 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4073 s
->tag
= g_strdup(s
->fsconf
.tag
);
4078 s
->ctx
.fmode
= fse
->fmode
;
4079 s
->ctx
.dmode
= fse
->dmode
;
4082 qemu_co_rwlock_init(&s
->rename_lock
);
4084 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4085 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4086 s
->fsconf
.fsdev_id
);
4091 * Check details of export path, We need to use fs driver
4092 * call back to do that. Since we are in the init path, we don't
4093 * use co-routines here.
4095 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4097 "error in converting name to path %s", strerror(errno
));
4100 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4101 error_setg(errp
, "share path %s does not exist", fse
->path
);
4103 } else if (!S_ISDIR(stat
.st_mode
)) {
4104 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4108 s
->dev_id
= stat
.st_dev
;
4110 /* init inode remapping : */
4111 /* hash table for variable length inode suffixes */
4112 qpd_table_init(&s
->qpd_table
);
4113 /* hash table for slow/full inode remapping (most users won't need it) */
4114 qpf_table_init(&s
->qpf_table
);
4115 /* hash table for quick inode remapping */
4116 qpp_table_init(&s
->qpp_table
);
4118 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4119 s
->qp_fullpath_next
= 1;
4121 s
->ctx
.fst
= &fse
->fst
;
4122 fsdev_throttle_init(s
->ctx
.fst
);
4127 v9fs_device_unrealize_common(s
, NULL
);
4129 v9fs_path_free(&path
);
4133 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
4135 if (s
->ops
&& s
->ops
->cleanup
) {
4136 s
->ops
->cleanup(&s
->ctx
);
4139 fsdev_throttle_cleanup(s
->ctx
.fst
);
4142 qp_table_destroy(&s
->qpd_table
);
4143 qp_table_destroy(&s
->qpp_table
);
4144 qp_table_destroy(&s
->qpf_table
);
4145 g_free(s
->ctx
.fs_root
);
4148 typedef struct VirtfsCoResetData
{
4151 } VirtfsCoResetData
;
4153 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4155 VirtfsCoResetData
*data
= opaque
;
4157 virtfs_reset(&data
->pdu
);
4161 void v9fs_reset(V9fsState
*s
)
4163 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4166 while (!QLIST_EMPTY(&s
->active_list
)) {
4167 aio_poll(qemu_get_aio_context(), true);
4170 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4171 qemu_coroutine_enter(co
);
4173 while (!data
.done
) {
4174 aio_poll(qemu_get_aio_context(), true);
4178 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4181 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4182 error_report("Failed to get the resource limit");
4185 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
4186 open_fd_rc
= rlim
.rlim_cur
/2;