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"
31 #include <linux/limits.h>
35 static int open_fd_rc
;
49 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
55 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
61 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
67 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
73 static int omode_to_uflags(int8_t mode
)
107 typedef struct DotlOpenflagMap
{
112 static int dotl_to_open_flags(int flags
)
116 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
117 * and P9_DOTL_NOACCESS
119 int oflags
= flags
& O_ACCMODE
;
121 DotlOpenflagMap dotl_oflag_map
[] = {
122 { P9_DOTL_CREATE
, O_CREAT
},
123 { P9_DOTL_EXCL
, O_EXCL
},
124 { P9_DOTL_NOCTTY
, O_NOCTTY
},
125 { P9_DOTL_TRUNC
, O_TRUNC
},
126 { P9_DOTL_APPEND
, O_APPEND
},
127 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
128 { P9_DOTL_DSYNC
, O_DSYNC
},
129 { P9_DOTL_FASYNC
, FASYNC
},
130 { P9_DOTL_DIRECT
, O_DIRECT
},
131 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
132 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
133 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
134 { P9_DOTL_NOATIME
, O_NOATIME
},
135 { P9_DOTL_SYNC
, O_SYNC
},
138 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
139 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
140 oflags
|= dotl_oflag_map
[i
].open_flag
;
147 void cred_init(FsCred
*credp
)
155 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
159 * Filter the client open flags
161 flags
= dotl_to_open_flags(oflags
);
162 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
164 * Ignore direct disk access hint until the server supports it.
170 void v9fs_path_init(V9fsPath
*path
)
176 void v9fs_path_free(V9fsPath
*path
)
184 void GCC_FMT_ATTR(2, 3)
185 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
189 v9fs_path_free(path
);
192 /* Bump the size for including terminating NULL */
193 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
197 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
200 dst
->size
= src
->size
;
201 dst
->data
= g_memdup(src
->data
, src
->size
);
204 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
205 const char *name
, V9fsPath
*path
)
208 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
216 * Return TRUE if s1 is an ancestor of s2.
218 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
219 * As a special case, We treat s1 as ancestor of s2 if they are same!
221 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
223 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
224 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
231 static size_t v9fs_string_size(V9fsString
*str
)
237 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
238 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
241 if (f
->fid_type
== P9_FID_FILE
) {
242 if (f
->fs
.fd
== -1) {
244 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
245 } while (err
== -EINTR
&& !pdu
->cancelled
);
247 } else if (f
->fid_type
== P9_FID_DIR
) {
248 if (f
->fs
.dir
.stream
== NULL
) {
250 err
= v9fs_co_opendir(pdu
, f
);
251 } while (err
== -EINTR
&& !pdu
->cancelled
);
257 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
261 V9fsState
*s
= pdu
->s
;
263 for (f
= s
->fid_list
; f
; f
= f
->next
) {
267 * Update the fid ref upfront so that
268 * we don't get reclaimed when we yield
273 * check whether we need to reopen the
274 * file. We might have closed the fd
275 * while trying to free up some file
278 err
= v9fs_reopen_fid(pdu
, f
);
284 * Mark the fid as referenced so that the LRU
285 * reclaim won't close the file descriptor
287 f
->flags
|= FID_REFERENCED
;
294 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
298 for (f
= s
->fid_list
; f
; f
= f
->next
) {
299 /* If fid is already there return NULL */
305 f
= g_malloc0(sizeof(V9fsFidState
));
307 f
->fid_type
= P9_FID_NONE
;
310 * Mark the fid as referenced so that the LRU
311 * reclaim won't close the file descriptor
313 f
->flags
|= FID_REFERENCED
;
314 f
->next
= s
->fid_list
;
317 v9fs_readdir_init(&f
->fs
.dir
);
318 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
323 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
327 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
328 /* getxattr/listxattr fid */
332 * if this is fid for setxattr. clunk should
333 * result in setxattr localcall
335 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
336 /* clunk after partial write */
340 if (fidp
->fs
.xattr
.len
) {
341 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
342 fidp
->fs
.xattr
.value
,
344 fidp
->fs
.xattr
.flags
);
346 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
349 v9fs_string_free(&fidp
->fs
.xattr
.name
);
351 g_free(fidp
->fs
.xattr
.value
);
355 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
359 if (fidp
->fid_type
== P9_FID_FILE
) {
360 /* If we reclaimed the fd no need to close */
361 if (fidp
->fs
.fd
!= -1) {
362 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
364 } else if (fidp
->fid_type
== P9_FID_DIR
) {
365 if (fidp
->fs
.dir
.stream
!= NULL
) {
366 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
368 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
369 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
371 v9fs_path_free(&fidp
->path
);
376 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
381 * Don't free the fid if it is in reclaim list
383 if (!fidp
->ref
&& fidp
->clunked
) {
384 if (fidp
->fid
== pdu
->s
->root_fid
) {
386 * if the clunked fid is root fid then we
387 * have unmounted the fs on the client side.
388 * delete the migration blocker. Ideally, this
389 * should be hooked to transport close notification
391 if (pdu
->s
->migration_blocker
) {
392 migrate_del_blocker(pdu
->s
->migration_blocker
);
393 error_free(pdu
->s
->migration_blocker
);
394 pdu
->s
->migration_blocker
= NULL
;
397 return free_fid(pdu
, fidp
);
402 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
404 V9fsFidState
**fidpp
, *fidp
;
406 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
407 if ((*fidpp
)->fid
== fid
) {
411 if (*fidpp
== NULL
) {
420 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
422 int reclaim_count
= 0;
423 V9fsState
*s
= pdu
->s
;
424 V9fsFidState
*f
, *reclaim_list
= NULL
;
426 for (f
= s
->fid_list
; f
; f
= f
->next
) {
428 * Unlink fids cannot be reclaimed. Check
429 * for them and skip them. Also skip fids
430 * currently being operated on.
432 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
436 * if it is a recently referenced fid
437 * we leave the fid untouched and clear the
438 * reference bit. We come back to it later
439 * in the next iteration. (a simple LRU without
440 * moving list elements around)
442 if (f
->flags
& FID_REFERENCED
) {
443 f
->flags
&= ~FID_REFERENCED
;
447 * Add fids to reclaim list.
449 if (f
->fid_type
== P9_FID_FILE
) {
450 if (f
->fs
.fd
!= -1) {
452 * Up the reference count so that
453 * a clunk request won't free this fid
456 f
->rclm_lst
= reclaim_list
;
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 f
->rclm_lst
= reclaim_list
;
471 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
472 f
->fs
.dir
.stream
= NULL
;
476 if (reclaim_count
>= open_fd_rc
) {
481 * Now close the fid in reclaim list. Free them if they
482 * are already clunked.
484 while (reclaim_list
) {
486 reclaim_list
= f
->rclm_lst
;
487 if (f
->fid_type
== P9_FID_FILE
) {
488 v9fs_co_close(pdu
, &f
->fs_reclaim
);
489 } else if (f
->fid_type
== P9_FID_DIR
) {
490 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
494 * Now drop the fid reference, free it
501 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
504 V9fsState
*s
= pdu
->s
;
505 V9fsFidState
*fidp
, head_fid
;
507 head_fid
.next
= s
->fid_list
;
508 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
509 if (fidp
->path
.size
!= path
->size
) {
512 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
513 /* Mark the fid non reclaimable. */
514 fidp
->flags
|= FID_NON_RECLAIMABLE
;
516 /* reopen the file/dir if already closed */
517 err
= v9fs_reopen_fid(pdu
, fidp
);
522 * Go back to head of fid list because
523 * the list could have got updated when
524 * switched to the worker thread
534 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
536 V9fsState
*s
= pdu
->s
;
540 while (s
->fid_list
) {
546 s
->fid_list
= fidp
->next
;
553 #define P9_QID_TYPE_DIR 0x80
554 #define P9_QID_TYPE_SYMLINK 0x02
556 #define P9_STAT_MODE_DIR 0x80000000
557 #define P9_STAT_MODE_APPEND 0x40000000
558 #define P9_STAT_MODE_EXCL 0x20000000
559 #define P9_STAT_MODE_MOUNT 0x10000000
560 #define P9_STAT_MODE_AUTH 0x08000000
561 #define P9_STAT_MODE_TMP 0x04000000
562 #define P9_STAT_MODE_SYMLINK 0x02000000
563 #define P9_STAT_MODE_LINK 0x01000000
564 #define P9_STAT_MODE_DEVICE 0x00800000
565 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
566 #define P9_STAT_MODE_SOCKET 0x00100000
567 #define P9_STAT_MODE_SETUID 0x00080000
568 #define P9_STAT_MODE_SETGID 0x00040000
569 #define P9_STAT_MODE_SETVTX 0x00010000
571 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
572 P9_STAT_MODE_SYMLINK | \
573 P9_STAT_MODE_LINK | \
574 P9_STAT_MODE_DEVICE | \
575 P9_STAT_MODE_NAMED_PIPE | \
578 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
579 static inline uint8_t mirror8bit(uint8_t byte
)
581 return (byte
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023;
584 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
585 static inline uint64_t mirror64bit(uint64_t value
)
587 return ((uint64_t)mirror8bit(value
& 0xff) << 56) |
588 ((uint64_t)mirror8bit((value
>> 8) & 0xff) << 48) |
589 ((uint64_t)mirror8bit((value
>> 16) & 0xff) << 40) |
590 ((uint64_t)mirror8bit((value
>> 24) & 0xff) << 32) |
591 ((uint64_t)mirror8bit((value
>> 32) & 0xff) << 24) |
592 ((uint64_t)mirror8bit((value
>> 40) & 0xff) << 16) |
593 ((uint64_t)mirror8bit((value
>> 48) & 0xff) << 8) |
594 ((uint64_t)mirror8bit((value
>> 56) & 0xff));
598 * @brief Parameter k for the Exponential Golomb algorihm to be used.
600 * The smaller this value, the smaller the minimum bit count for the Exp.
601 * Golomb generated affixes will be (at lowest index) however for the
602 * price of having higher maximum bit count of generated affixes (at highest
603 * index). Likewise increasing this parameter yields in smaller maximum bit
604 * count for the price of having higher minimum bit count.
606 * In practice that means: a good value for k depends on the expected amount
607 * of devices to be exposed by one export. For a small amount of devices k
608 * should be small, for a large amount of devices k might be increased
609 * instead. The default of k=0 should be fine for most users though.
611 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
612 * k should not change as long as guest is still running! Because that would
613 * cause completely different inode numbers to be generated on guest.
615 #define EXP_GOLOMB_K 0
618 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
620 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
621 * with growing length and with the mathematical property of being
622 * "prefix-free". The latter means the generated prefixes can be prepended
623 * in front of arbitrary numbers and the resulting concatenated numbers are
624 * guaranteed to be always unique.
626 * This is a minor adjustment to the original Exp. Golomb algorithm in the
627 * sense that lowest allowed index (@param n) starts with 1, not with zero.
629 * @param n - natural number (or index) of the prefix to be generated
631 * @param k - parameter k of Exp. Golomb algorithm to be used
632 * (see comment on EXP_GOLOMB_K macro for details about k)
634 static VariLenAffix
expGolombEncode(uint64_t n
, int k
)
636 const uint64_t value
= n
+ (1 << k
) - 1;
637 const int bits
= (int) log2(value
) + 1;
638 return (VariLenAffix
) {
639 .type
= AffixType_Prefix
,
641 .bits
= bits
+ MAX((bits
- 1 - k
), 0)
646 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
648 * Simply mirror all bits of the affix value, for the purpose to preserve
649 * respectively the mathematical "prefix-free" or "suffix-free" property
650 * after the conversion.
652 * If a passed prefix is suitable to create unique numbers, then the
653 * returned suffix is suitable to create unique numbers as well (and vice
656 static VariLenAffix
invertAffix(const VariLenAffix
*affix
)
658 return (VariLenAffix
) {
660 (affix
->type
== AffixType_Suffix
) ?
661 AffixType_Prefix
: AffixType_Suffix
,
663 mirror64bit(affix
->value
) >>
664 ((sizeof(affix
->value
) * 8) - affix
->bits
),
670 * @brief Generates suffix numbers with "suffix-free" property.
672 * This is just a wrapper function on top of the Exp. Golomb algorithm.
674 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
675 * this function converts the Exp. Golomb prefixes into appropriate suffixes
676 * which are still suitable for generating unique numbers.
678 * @param n - natural number (or index) of the suffix to be generated
681 static VariLenAffix
affixForIndex(uint64_t index
)
684 prefix
= expGolombEncode(index
, EXP_GOLOMB_K
);
685 return invertAffix(&prefix
); /* convert prefix to suffix */
688 /* creative abuse of tb_hash_func7, which is based on xxhash */
689 static uint32_t qpp_hash(QppEntry e
)
691 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
694 static uint32_t qpf_hash(QpfEntry e
)
696 return qemu_xxhash7(e
.ino
, e
.dev
, 0, 0, 0);
699 static bool qpd_cmp_func(const void *obj
, const void *userp
)
701 const QpdEntry
*e1
= obj
, *e2
= userp
;
702 return e1
->dev
== e2
->dev
;
705 static bool qpp_cmp_func(const void *obj
, const void *userp
)
707 const QppEntry
*e1
= obj
, *e2
= userp
;
708 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
711 static bool qpf_cmp_func(const void *obj
, const void *userp
)
713 const QpfEntry
*e1
= obj
, *e2
= userp
;
714 return e1
->dev
== e2
->dev
&& e1
->ino
== e2
->ino
;
717 static void qp_table_remove(void *p
, uint32_t h
, void *up
)
722 static void qp_table_destroy(struct qht
*ht
)
724 if (!ht
|| !ht
->map
) {
727 qht_iter(ht
, qp_table_remove
, NULL
);
731 static void qpd_table_init(struct qht
*ht
)
733 qht_init(ht
, qpd_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
736 static void qpp_table_init(struct qht
*ht
)
738 qht_init(ht
, qpp_cmp_func
, 1, QHT_MODE_AUTO_RESIZE
);
741 static void qpf_table_init(struct qht
*ht
)
743 qht_init(ht
, qpf_cmp_func
, 1 << 16, QHT_MODE_AUTO_RESIZE
);
747 * Returns how many (high end) bits of inode numbers of the passed fs
748 * device shall be used (in combination with the device number) to
749 * generate hash values for qpp_table entries.
751 * This function is required if variable length suffixes are used for inode
752 * number mapping on guest level. Since a device may end up having multiple
753 * entries in qpp_table, each entry most probably with a different suffix
754 * length, we thus need this function in conjunction with qpd_table to
755 * "agree" about a fix amount of bits (per device) to be always used for
756 * generating hash values for the purpose of accessing qpp_table in order
757 * get consistent behaviour when accessing qpp_table.
759 static int qid_inode_prefix_hash_bits(V9fsPDU
*pdu
, dev_t dev
)
767 val
= qht_lookup(&pdu
->s
->qpd_table
, &lookup
, hash
);
769 val
= g_malloc0(sizeof(QpdEntry
));
771 affix
= affixForIndex(pdu
->s
->qp_affix_next
);
772 val
->prefix_bits
= affix
.bits
;
773 qht_insert(&pdu
->s
->qpd_table
, val
, hash
, NULL
);
774 pdu
->s
->qp_ndevices
++;
776 return val
->prefix_bits
;
780 * @brief Slow / full mapping host inode nr -> guest inode nr.
782 * This function performs a slower and much more costly remapping of an
783 * original file inode number on host to an appropriate different inode
784 * number on guest. For every (dev, inode) combination on host a new
785 * sequential number is generated, cached and exposed as inode number on
788 * This is just a "last resort" fallback solution if the much faster/cheaper
789 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
790 * expected ever to be used at all though.
792 * @see qid_path_suffixmap() for details
795 static int qid_path_fullmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
799 .dev
= stbuf
->st_dev
,
802 uint32_t hash
= qpf_hash(lookup
);
805 val
= qht_lookup(&pdu
->s
->qpf_table
, &lookup
, hash
);
808 if (pdu
->s
->qp_fullpath_next
== 0) {
809 /* no more files can be mapped :'( */
811 "9p: No more prefixes available for remapping inodes from "
817 val
= g_malloc0(sizeof(QppEntry
));
820 /* new unique inode and device combo */
821 affix
= affixForIndex(
822 1ULL << (sizeof(pdu
->s
->qp_affix_next
) * 8)
824 val
->path
= (pdu
->s
->qp_fullpath_next
++ << affix
.bits
) | affix
.value
;
825 pdu
->s
->qp_fullpath_next
&= ((1ULL << (64 - affix
.bits
)) - 1);
826 qht_insert(&pdu
->s
->qpf_table
, val
, hash
, NULL
);
834 * @brief Quick mapping host inode nr -> guest inode nr.
836 * This function performs quick remapping of an original file inode number
837 * on host to an appropriate different inode number on guest. This remapping
838 * of inodes is required to avoid inode nr collisions on guest which would
839 * happen if the 9p export contains more than 1 exported file system (or
840 * more than 1 file system data set), because unlike on host level where the
841 * files would have different device nrs, all files exported by 9p would
842 * share the same device nr on guest (the device nr of the virtual 9p device
845 * Inode remapping is performed by chopping off high end bits of the original
846 * inode number from host, shifting the result upwards and then assigning a
847 * generated suffix number for the low end bits, where the same suffix number
848 * will be shared by all inodes with the same device id AND the same high end
849 * bits that have been chopped off. That approach utilizes the fact that inode
850 * numbers very likely share the same high end bits (i.e. due to their common
851 * sequential generation by file systems) and hence we only have to generate
852 * and track a very limited amount of suffixes in practice due to that.
854 * We generate variable size suffixes for that purpose. The 1st generated
855 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
856 * the original inode number. The subsequent suffixes being generated will
857 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
858 * generated will have 3 bits and hence we have to chop off 3 bits from their
859 * original inodes, and so on. That approach of using variable length suffixes
860 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
861 * limited amount of devices are shared by the same export (e.g. typically
862 * less than 2 dozen devices per 9p export), so in practice we need to chop
863 * off less bits than with fixed size prefixes and yet are flexible to add
864 * new devices at runtime below host's export directory at any time without
865 * having to reboot guest nor requiring to reconfigure guest for that. And due
866 * to the very limited amount of original high end bits that we chop off that
867 * way, the total amount of suffixes we need to generate is less than by using
868 * fixed size prefixes and hence it also improves performance of the inode
869 * remapping algorithm, and finally has the nice side effect that the inode
870 * numbers on guest will be much smaller & human friendly. ;-)
872 static int qid_path_suffixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
875 const int ino_hash_bits
= qid_inode_prefix_hash_bits(pdu
, stbuf
->st_dev
);
877 .dev
= stbuf
->st_dev
,
878 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> (64 - ino_hash_bits
))
880 uint32_t hash
= qpp_hash(lookup
);
882 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
885 if (pdu
->s
->qp_affix_next
== 0) {
886 /* we ran out of affixes */
888 "9p: Potential degraded performance of inode remapping"
893 val
= g_malloc0(sizeof(QppEntry
));
896 /* new unique inode affix and device combo */
897 val
->qp_affix_index
= pdu
->s
->qp_affix_next
++;
898 val
->qp_affix
= affixForIndex(val
->qp_affix_index
);
899 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
901 /* assuming generated affix to be suffix type, not prefix */
902 *path
= (stbuf
->st_ino
<< val
->qp_affix
.bits
) | val
->qp_affix
.value
;
906 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
911 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
912 /* map inode+device to qid path (fast path) */
913 err
= qid_path_suffixmap(pdu
, stbuf
, &qidp
->path
);
914 if (err
== -ENFILE
) {
915 /* fast path didn't work, fall back to full map */
916 err
= qid_path_fullmap(pdu
, stbuf
, &qidp
->path
);
922 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
923 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
925 "9p: Multiple devices detected in same VirtFS export. "
926 "Access of guest to additional devices is (partly) "
927 "denied due to virtfs option 'multidevs=forbid' being "
933 "9p: Multiple devices detected in same VirtFS export, "
934 "which might lead to file ID collisions and severe "
935 "misbehaviours on guest! You should either use a "
936 "separate export for each device shared from host or "
937 "use virtfs option 'multidevs=remap'!"
941 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
942 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
943 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
946 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
948 if (S_ISDIR(stbuf
->st_mode
)) {
949 qidp
->type
|= P9_QID_TYPE_DIR
;
951 if (S_ISLNK(stbuf
->st_mode
)) {
952 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
958 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
964 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
968 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
975 static int coroutine_fn
dirent_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
976 struct dirent
*dent
, V9fsQID
*qidp
)
982 v9fs_path_init(&path
);
984 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
988 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
992 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
995 v9fs_path_free(&path
);
999 V9fsPDU
*pdu_alloc(V9fsState
*s
)
1001 V9fsPDU
*pdu
= NULL
;
1003 if (!QLIST_EMPTY(&s
->free_list
)) {
1004 pdu
= QLIST_FIRST(&s
->free_list
);
1005 QLIST_REMOVE(pdu
, next
);
1006 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
1011 void pdu_free(V9fsPDU
*pdu
)
1013 V9fsState
*s
= pdu
->s
;
1015 g_assert(!pdu
->cancelled
);
1016 QLIST_REMOVE(pdu
, next
);
1017 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
1020 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
1022 int8_t id
= pdu
->id
+ 1; /* Response */
1023 V9fsState
*s
= pdu
->s
;
1027 * The 9p spec requires that successfully cancelled pdus receive no reply.
1028 * Sending a reply would confuse clients because they would
1029 * assume that any EINTR is the actual result of the operation,
1030 * rather than a consequence of the cancellation. However, if
1031 * the operation completed (succesfully or with an error other
1032 * than caused be cancellation), we do send out that reply, both
1033 * for efficiency and to avoid confusing the rest of the state machine
1034 * that assumes passing a non-error here will mean a successful
1035 * transmission of the reply.
1037 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
1039 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
1048 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
1051 str
.data
= strerror(err
);
1052 str
.size
= strlen(str
.data
);
1054 ret
= pdu_marshal(pdu
, len
, "s", &str
);
1062 ret
= pdu_marshal(pdu
, len
, "d", err
);
1068 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1071 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
1074 /* fill out the header */
1075 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
1079 /* keep these in sync */
1084 pdu
->s
->transport
->push_and_notify(pdu
);
1086 /* Now wakeup anybody waiting in flush for this request */
1087 if (!qemu_co_queue_next(&pdu
->complete
)) {
1092 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
1097 if (mode
& P9_STAT_MODE_DIR
) {
1101 if (mode
& P9_STAT_MODE_SYMLINK
) {
1104 if (mode
& P9_STAT_MODE_SOCKET
) {
1107 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1110 if (mode
& P9_STAT_MODE_DEVICE
) {
1111 if (extension
->size
&& extension
->data
[0] == 'c') {
1122 if (mode
& P9_STAT_MODE_SETUID
) {
1125 if (mode
& P9_STAT_MODE_SETGID
) {
1128 if (mode
& P9_STAT_MODE_SETVTX
) {
1135 static int donttouch_stat(V9fsStat
*stat
)
1137 if (stat
->type
== -1 &&
1139 stat
->qid
.type
== 0xff &&
1140 stat
->qid
.version
== (uint32_t) -1 &&
1141 stat
->qid
.path
== (uint64_t) -1 &&
1143 stat
->atime
== -1 &&
1144 stat
->mtime
== -1 &&
1145 stat
->length
== -1 &&
1150 stat
->n_uid
== -1 &&
1151 stat
->n_gid
== -1 &&
1152 stat
->n_muid
== -1) {
1159 static void v9fs_stat_init(V9fsStat
*stat
)
1161 v9fs_string_init(&stat
->name
);
1162 v9fs_string_init(&stat
->uid
);
1163 v9fs_string_init(&stat
->gid
);
1164 v9fs_string_init(&stat
->muid
);
1165 v9fs_string_init(&stat
->extension
);
1168 static void v9fs_stat_free(V9fsStat
*stat
)
1170 v9fs_string_free(&stat
->name
);
1171 v9fs_string_free(&stat
->uid
);
1172 v9fs_string_free(&stat
->gid
);
1173 v9fs_string_free(&stat
->muid
);
1174 v9fs_string_free(&stat
->extension
);
1177 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1181 mode
= stbuf
->st_mode
& 0777;
1182 if (S_ISDIR(stbuf
->st_mode
)) {
1183 mode
|= P9_STAT_MODE_DIR
;
1186 if (S_ISLNK(stbuf
->st_mode
)) {
1187 mode
|= P9_STAT_MODE_SYMLINK
;
1190 if (S_ISSOCK(stbuf
->st_mode
)) {
1191 mode
|= P9_STAT_MODE_SOCKET
;
1194 if (S_ISFIFO(stbuf
->st_mode
)) {
1195 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1198 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1199 mode
|= P9_STAT_MODE_DEVICE
;
1202 if (stbuf
->st_mode
& S_ISUID
) {
1203 mode
|= P9_STAT_MODE_SETUID
;
1206 if (stbuf
->st_mode
& S_ISGID
) {
1207 mode
|= P9_STAT_MODE_SETGID
;
1210 if (stbuf
->st_mode
& S_ISVTX
) {
1211 mode
|= P9_STAT_MODE_SETVTX
;
1217 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
1218 const char *basename
,
1219 const struct stat
*stbuf
,
1224 memset(v9stat
, 0, sizeof(*v9stat
));
1226 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
1230 v9stat
->mode
= stat_to_v9mode(stbuf
);
1231 v9stat
->atime
= stbuf
->st_atime
;
1232 v9stat
->mtime
= stbuf
->st_mtime
;
1233 v9stat
->length
= stbuf
->st_size
;
1235 v9fs_string_free(&v9stat
->uid
);
1236 v9fs_string_free(&v9stat
->gid
);
1237 v9fs_string_free(&v9stat
->muid
);
1239 v9stat
->n_uid
= stbuf
->st_uid
;
1240 v9stat
->n_gid
= stbuf
->st_gid
;
1243 v9fs_string_free(&v9stat
->extension
);
1245 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1246 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
1250 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1251 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1252 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1253 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1254 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1255 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1256 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1259 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1262 v9fs_string_size(&v9stat
->name
) +
1263 v9fs_string_size(&v9stat
->uid
) +
1264 v9fs_string_size(&v9stat
->gid
) +
1265 v9fs_string_size(&v9stat
->muid
) +
1266 v9fs_string_size(&v9stat
->extension
);
1270 #define P9_STATS_MODE 0x00000001ULL
1271 #define P9_STATS_NLINK 0x00000002ULL
1272 #define P9_STATS_UID 0x00000004ULL
1273 #define P9_STATS_GID 0x00000008ULL
1274 #define P9_STATS_RDEV 0x00000010ULL
1275 #define P9_STATS_ATIME 0x00000020ULL
1276 #define P9_STATS_MTIME 0x00000040ULL
1277 #define P9_STATS_CTIME 0x00000080ULL
1278 #define P9_STATS_INO 0x00000100ULL
1279 #define P9_STATS_SIZE 0x00000200ULL
1280 #define P9_STATS_BLOCKS 0x00000400ULL
1282 #define P9_STATS_BTIME 0x00000800ULL
1283 #define P9_STATS_GEN 0x00001000ULL
1284 #define P9_STATS_DATA_VERSION 0x00002000ULL
1286 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1287 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1290 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1291 V9fsStatDotl
*v9lstat
)
1293 memset(v9lstat
, 0, sizeof(*v9lstat
));
1295 v9lstat
->st_mode
= stbuf
->st_mode
;
1296 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1297 v9lstat
->st_uid
= stbuf
->st_uid
;
1298 v9lstat
->st_gid
= stbuf
->st_gid
;
1299 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1300 v9lstat
->st_size
= stbuf
->st_size
;
1301 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1302 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1303 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1304 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1305 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1306 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1307 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1308 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1309 /* Currently we only support BASIC fields in stat */
1310 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1312 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1315 static void print_sg(struct iovec
*sg
, int cnt
)
1319 printf("sg[%d]: {", cnt
);
1320 for (i
= 0; i
< cnt
; i
++) {
1324 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1329 /* Will call this only for path name based fid */
1330 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1333 v9fs_path_init(&str
);
1334 v9fs_path_copy(&str
, dst
);
1335 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1336 v9fs_path_free(&str
);
1339 static inline bool is_ro_export(FsContext
*ctx
)
1341 return ctx
->export_flags
& V9FS_RDONLY
;
1344 static void coroutine_fn
v9fs_version(void *opaque
)
1347 V9fsPDU
*pdu
= opaque
;
1348 V9fsState
*s
= pdu
->s
;
1352 v9fs_string_init(&version
);
1353 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1357 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1361 if (!strcmp(version
.data
, "9P2000.u")) {
1362 s
->proto_version
= V9FS_PROTO_2000U
;
1363 } else if (!strcmp(version
.data
, "9P2000.L")) {
1364 s
->proto_version
= V9FS_PROTO_2000L
;
1366 v9fs_string_sprintf(&version
, "unknown");
1367 /* skip min. msize check, reporting invalid version has priority */
1371 if (s
->msize
< P9_MIN_MSIZE
) {
1374 "9pfs: Client requested msize < minimum msize ("
1375 stringify(P9_MIN_MSIZE
) ") supported by this server."
1381 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1386 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1388 pdu_complete(pdu
, err
);
1389 v9fs_string_free(&version
);
1392 static void coroutine_fn
v9fs_attach(void *opaque
)
1394 V9fsPDU
*pdu
= opaque
;
1395 V9fsState
*s
= pdu
->s
;
1396 int32_t fid
, afid
, n_uname
;
1397 V9fsString uname
, aname
;
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
, NULL
);
1441 error_free(s
->migration_blocker
);
1442 s
->migration_blocker
= NULL
;
1449 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1456 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1457 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1458 qid
.type
, qid
.version
, qid
.path
);
1462 pdu_complete(pdu
, err
);
1463 v9fs_string_free(&uname
);
1464 v9fs_string_free(&aname
);
1467 static void coroutine_fn
v9fs_stat(void *opaque
)
1475 V9fsPDU
*pdu
= opaque
;
1478 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1482 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1484 fidp
= get_fid(pdu
, fid
);
1489 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1493 basename
= g_path_get_basename(fidp
->path
.data
);
1494 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1499 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1501 v9fs_stat_free(&v9stat
);
1504 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1505 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1507 v9fs_stat_free(&v9stat
);
1511 pdu_complete(pdu
, err
);
1514 static void coroutine_fn
v9fs_getattr(void *opaque
)
1521 uint64_t request_mask
;
1522 V9fsStatDotl v9stat_dotl
;
1523 V9fsPDU
*pdu
= opaque
;
1525 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1529 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1531 fidp
= get_fid(pdu
, fid
);
1537 * Currently we only support BASIC fields in stat, so there is no
1538 * need to look at request_mask.
1540 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1544 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1549 /* fill st_gen if requested and supported by underlying fs */
1550 if (request_mask
& P9_STATS_GEN
) {
1551 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1554 /* we have valid st_gen: update result mask */
1555 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1558 /* request cancelled, e.g. by Tflush */
1561 /* failed to get st_gen: not fatal, ignore */
1565 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1570 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1571 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1572 v9stat_dotl
.st_gid
);
1576 pdu_complete(pdu
, retval
);
1579 /* Attribute flags */
1580 #define P9_ATTR_MODE (1 << 0)
1581 #define P9_ATTR_UID (1 << 1)
1582 #define P9_ATTR_GID (1 << 2)
1583 #define P9_ATTR_SIZE (1 << 3)
1584 #define P9_ATTR_ATIME (1 << 4)
1585 #define P9_ATTR_MTIME (1 << 5)
1586 #define P9_ATTR_CTIME (1 << 6)
1587 #define P9_ATTR_ATIME_SET (1 << 7)
1588 #define P9_ATTR_MTIME_SET (1 << 8)
1590 #define P9_ATTR_MASK 127
1592 static void coroutine_fn
v9fs_setattr(void *opaque
)
1599 V9fsPDU
*pdu
= opaque
;
1601 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1606 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1607 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1608 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1610 fidp
= get_fid(pdu
, fid
);
1615 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1616 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1621 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1622 struct timespec times
[2];
1623 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1624 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1625 times
[0].tv_sec
= v9iattr
.atime_sec
;
1626 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1628 times
[0].tv_nsec
= UTIME_NOW
;
1631 times
[0].tv_nsec
= UTIME_OMIT
;
1633 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1634 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1635 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1636 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1638 times
[1].tv_nsec
= UTIME_NOW
;
1641 times
[1].tv_nsec
= UTIME_OMIT
;
1643 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1649 * If the only valid entry in iattr is ctime we can call
1650 * chown(-1,-1) to update the ctime of the file
1652 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1653 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1654 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1655 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1658 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1661 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1667 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1668 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1674 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1678 pdu_complete(pdu
, err
);
1681 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1687 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1692 for (i
= 0; i
< nwnames
; i
++) {
1693 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1702 static bool name_is_illegal(const char *name
)
1704 return !*name
|| strchr(name
, '/') != NULL
;
1707 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1710 qid1
->type
!= qid2
->type
||
1711 qid1
->version
!= qid2
->version
||
1712 qid1
->path
!= qid2
->path
;
1715 static void coroutine_fn
v9fs_walk(void *opaque
)
1718 V9fsQID
*qids
= NULL
;
1720 V9fsPath dpath
, path
;
1724 int32_t fid
, newfid
;
1725 V9fsString
*wnames
= NULL
;
1727 V9fsFidState
*newfidp
= NULL
;
1728 V9fsPDU
*pdu
= opaque
;
1729 V9fsState
*s
= pdu
->s
;
1732 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1734 pdu_complete(pdu
, err
);
1739 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1741 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1742 wnames
= g_new0(V9fsString
, nwnames
);
1743 qids
= g_new0(V9fsQID
, nwnames
);
1744 for (i
= 0; i
< nwnames
; i
++) {
1745 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1749 if (name_is_illegal(wnames
[i
].data
)) {
1755 } else if (nwnames
> P9_MAXWELEM
) {
1759 fidp
= get_fid(pdu
, fid
);
1765 v9fs_path_init(&dpath
);
1766 v9fs_path_init(&path
);
1768 err
= fid_to_qid(pdu
, fidp
, &qid
);
1774 * Both dpath and path initially poin to fidp.
1775 * Needed to handle request with nwnames == 0
1777 v9fs_path_copy(&dpath
, &fidp
->path
);
1778 v9fs_path_copy(&path
, &fidp
->path
);
1779 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1780 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1781 strcmp("..", wnames
[name_idx
].data
)) {
1782 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1788 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1792 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1796 v9fs_path_copy(&dpath
, &path
);
1798 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1800 if (fid
== newfid
) {
1801 if (fidp
->fid_type
!= P9_FID_NONE
) {
1805 v9fs_path_write_lock(s
);
1806 v9fs_path_copy(&fidp
->path
, &path
);
1807 v9fs_path_unlock(s
);
1809 newfidp
= alloc_fid(s
, newfid
);
1810 if (newfidp
== NULL
) {
1814 newfidp
->uid
= fidp
->uid
;
1815 v9fs_path_copy(&newfidp
->path
, &path
);
1817 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1818 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1822 put_fid(pdu
, newfidp
);
1824 v9fs_path_free(&dpath
);
1825 v9fs_path_free(&path
);
1827 pdu_complete(pdu
, err
);
1828 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1829 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1830 v9fs_string_free(&wnames
[name_idx
]);
1837 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1839 struct statfs stbuf
;
1841 V9fsState
*s
= pdu
->s
;
1844 * iounit should be multiples of f_bsize (host filesystem block size
1845 * and as well as less than (client msize - P9_IOHDRSZ))
1847 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1848 if (stbuf
.f_bsize
) {
1849 iounit
= stbuf
.f_bsize
;
1850 iounit
*= (s
->msize
- P9_IOHDRSZ
) / stbuf
.f_bsize
;
1854 iounit
= s
->msize
- P9_IOHDRSZ
;
1859 static void coroutine_fn
v9fs_open(void *opaque
)
1870 V9fsPDU
*pdu
= opaque
;
1871 V9fsState
*s
= pdu
->s
;
1873 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1874 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1877 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1883 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1885 fidp
= get_fid(pdu
, fid
);
1890 if (fidp
->fid_type
!= P9_FID_NONE
) {
1895 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1899 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1903 if (S_ISDIR(stbuf
.st_mode
)) {
1904 err
= v9fs_co_opendir(pdu
, fidp
);
1908 fidp
->fid_type
= P9_FID_DIR
;
1909 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1915 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1916 flags
= get_dotl_openflags(s
, mode
);
1918 flags
= omode_to_uflags(mode
);
1920 if (is_ro_export(&s
->ctx
)) {
1921 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1922 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1927 err
= v9fs_co_open(pdu
, fidp
, flags
);
1931 fidp
->fid_type
= P9_FID_FILE
;
1932 fidp
->open_flags
= flags
;
1933 if (flags
& O_EXCL
) {
1935 * We let the host file system do O_EXCL check
1936 * We should not reclaim such fd
1938 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1940 iounit
= get_iounit(pdu
, &fidp
->path
);
1941 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1947 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1948 qid
.type
, qid
.version
, qid
.path
, iounit
);
1952 pdu_complete(pdu
, err
);
1955 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1957 int32_t dfid
, flags
, mode
;
1966 V9fsPDU
*pdu
= opaque
;
1968 v9fs_string_init(&name
);
1969 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1970 &name
, &flags
, &mode
, &gid
);
1974 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1976 if (name_is_illegal(name
.data
)) {
1981 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1986 fidp
= get_fid(pdu
, dfid
);
1991 if (fidp
->fid_type
!= P9_FID_NONE
) {
1996 flags
= get_dotl_openflags(pdu
->s
, flags
);
1997 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1998 flags
| O_CREAT
, mode
, &stbuf
);
2002 fidp
->fid_type
= P9_FID_FILE
;
2003 fidp
->open_flags
= flags
;
2004 if (flags
& O_EXCL
) {
2006 * We let the host file system do O_EXCL check
2007 * We should not reclaim such fd
2009 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2011 iounit
= get_iounit(pdu
, &fidp
->path
);
2012 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2016 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2021 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
2022 qid
.type
, qid
.version
, qid
.path
, iounit
);
2026 pdu_complete(pdu
, err
);
2027 v9fs_string_free(&name
);
2030 static void coroutine_fn
v9fs_fsync(void *opaque
)
2037 V9fsPDU
*pdu
= opaque
;
2039 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
2043 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
2045 fidp
= get_fid(pdu
, fid
);
2050 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
2056 pdu_complete(pdu
, err
);
2059 static void coroutine_fn
v9fs_clunk(void *opaque
)
2065 V9fsPDU
*pdu
= opaque
;
2066 V9fsState
*s
= pdu
->s
;
2068 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2072 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
2074 fidp
= clunk_fid(s
, fid
);
2080 * Bump the ref so that put_fid will
2084 err
= put_fid(pdu
, fidp
);
2089 pdu_complete(pdu
, err
);
2093 * Create a QEMUIOVector for a sub-region of PDU iovecs
2095 * @qiov: uninitialized QEMUIOVector
2096 * @skip: number of bytes to skip from beginning of PDU
2097 * @size: number of bytes to include
2098 * @is_write: true - write, false - read
2100 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2101 * with qemu_iovec_destroy().
2103 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
2104 size_t skip
, size_t size
,
2112 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2114 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
2117 qemu_iovec_init_external(&elem
, iov
, niov
);
2118 qemu_iovec_init(qiov
, niov
);
2119 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
2122 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2123 uint64_t off
, uint32_t max_count
)
2127 uint64_t read_count
;
2128 QEMUIOVector qiov_full
;
2130 if (fidp
->fs
.xattr
.len
< off
) {
2133 read_count
= fidp
->fs
.xattr
.len
- off
;
2135 if (read_count
> max_count
) {
2136 read_count
= max_count
;
2138 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
2144 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
2145 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
2146 ((char *)fidp
->fs
.xattr
.value
) + off
,
2148 qemu_iovec_destroy(&qiov_full
);
2156 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
2165 off_t saved_dir_pos
;
2166 struct dirent
*dent
;
2168 /* save the directory position */
2169 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2170 if (saved_dir_pos
< 0) {
2171 return saved_dir_pos
;
2175 v9fs_path_init(&path
);
2177 v9fs_readdir_lock(&fidp
->fs
.dir
);
2179 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2183 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
2187 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
2191 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
2195 if ((count
+ v9stat
.size
+ 2) > max_count
) {
2196 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2198 /* Ran out of buffer. Set dir back to old position and return */
2199 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2200 v9fs_stat_free(&v9stat
);
2201 v9fs_path_free(&path
);
2205 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2206 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
2208 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2211 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2212 v9fs_stat_free(&v9stat
);
2213 v9fs_path_free(&path
);
2217 v9fs_stat_free(&v9stat
);
2218 v9fs_path_free(&path
);
2219 saved_dir_pos
= dent
->d_off
;
2222 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2224 v9fs_path_free(&path
);
2231 static void coroutine_fn
v9fs_read(void *opaque
)
2240 V9fsPDU
*pdu
= opaque
;
2241 V9fsState
*s
= pdu
->s
;
2243 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
2247 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
2249 fidp
= get_fid(pdu
, fid
);
2254 if (fidp
->fid_type
== P9_FID_DIR
) {
2257 v9fs_co_rewinddir(pdu
, fidp
);
2259 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
2264 err
= pdu_marshal(pdu
, offset
, "d", count
);
2268 err
+= offset
+ count
;
2269 } else if (fidp
->fid_type
== P9_FID_FILE
) {
2270 QEMUIOVector qiov_full
;
2274 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2275 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2277 qemu_iovec_reset(&qiov
);
2278 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2280 print_sg(qiov
.iov
, qiov
.niov
);
2282 /* Loop in case of EINTR */
2284 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2289 } while (len
== -EINTR
&& !pdu
->cancelled
);
2291 /* IO error return the error */
2293 goto out_free_iovec
;
2295 } while (count
< max_count
&& len
> 0);
2296 err
= pdu_marshal(pdu
, offset
, "d", count
);
2298 goto out_free_iovec
;
2300 err
+= offset
+ count
;
2302 qemu_iovec_destroy(&qiov
);
2303 qemu_iovec_destroy(&qiov_full
);
2304 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2305 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2309 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2313 pdu_complete(pdu
, err
);
2316 static size_t v9fs_readdir_data_size(V9fsString
*name
)
2319 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2320 * size of type (1) + size of name.size (2) + strlen(name.data)
2322 return 24 + v9fs_string_size(name
);
2325 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2333 off_t saved_dir_pos
;
2334 struct dirent
*dent
;
2336 /* save the directory position */
2337 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2338 if (saved_dir_pos
< 0) {
2339 return saved_dir_pos
;
2343 v9fs_readdir_lock(&fidp
->fs
.dir
);
2345 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2349 v9fs_string_init(&name
);
2350 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2351 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
2352 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2354 /* Ran out of buffer. Set dir back to old position and return */
2355 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2356 v9fs_string_free(&name
);
2360 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2362 * dirent_to_qid() implies expensive stat call for each entry,
2363 * we must do that here though since inode remapping requires
2364 * the device id, which in turn might be different for
2365 * different entries; we cannot make any assumption to avoid
2368 err
= dirent_to_qid(pdu
, fidp
, dent
, &qid
);
2370 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2371 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2372 v9fs_string_free(&name
);
2377 * Fill up just the path field of qid because the client uses
2378 * only that. To fill the entire qid structure we will have
2379 * to stat each dirent found, which is expensive. For the
2380 * latter reason we don't call dirent_to_qid() here. Only drawback
2381 * is that no multi-device export detection of stat_to_qid()
2382 * would be done and provided as error to the user here. But
2383 * user would get that error anyway when accessing those
2384 * files/dirs through other ways.
2386 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2387 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2388 /* Fill the other fields with dummy values */
2393 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2394 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2396 dent
->d_type
, &name
);
2398 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2401 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2402 v9fs_string_free(&name
);
2406 v9fs_string_free(&name
);
2407 saved_dir_pos
= dent
->d_off
;
2410 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2418 static void coroutine_fn
v9fs_readdir(void *opaque
)
2424 uint64_t initial_offset
;
2427 V9fsPDU
*pdu
= opaque
;
2428 V9fsState
*s
= pdu
->s
;
2430 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2431 &initial_offset
, &max_count
);
2435 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2437 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2438 if (max_count
> s
->msize
- 11) {
2439 max_count
= s
->msize
- 11;
2441 "9p: bad client: T_readdir with count > msize - 11"
2445 fidp
= get_fid(pdu
, fid
);
2450 if (!fidp
->fs
.dir
.stream
) {
2454 if (initial_offset
== 0) {
2455 v9fs_co_rewinddir(pdu
, fidp
);
2457 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2459 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2464 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2468 retval
+= count
+ offset
;
2469 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2473 pdu_complete(pdu
, retval
);
2476 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2477 uint64_t off
, uint32_t count
,
2478 struct iovec
*sg
, int cnt
)
2482 uint64_t write_count
;
2486 if (fidp
->fs
.xattr
.len
< off
) {
2489 write_count
= fidp
->fs
.xattr
.len
- off
;
2490 if (write_count
> count
) {
2491 write_count
= count
;
2493 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2498 fidp
->fs
.xattr
.copied_len
+= write_count
;
2500 * Now copy the content from sg list
2502 for (i
= 0; i
< cnt
; i
++) {
2503 if (write_count
> sg
[i
].iov_len
) {
2504 to_copy
= sg
[i
].iov_len
;
2506 to_copy
= write_count
;
2508 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2509 /* updating vs->off since we are not using below */
2511 write_count
-= to_copy
;
2517 static void coroutine_fn
v9fs_write(void *opaque
)
2527 V9fsPDU
*pdu
= opaque
;
2528 V9fsState
*s
= pdu
->s
;
2529 QEMUIOVector qiov_full
;
2532 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2534 pdu_complete(pdu
, err
);
2538 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2539 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2541 fidp
= get_fid(pdu
, fid
);
2546 if (fidp
->fid_type
== P9_FID_FILE
) {
2547 if (fidp
->fs
.fd
== -1) {
2551 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2553 * setxattr operation
2555 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2556 qiov_full
.iov
, qiov_full
.niov
);
2562 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2564 qemu_iovec_reset(&qiov
);
2565 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2567 print_sg(qiov
.iov
, qiov
.niov
);
2569 /* Loop in case of EINTR */
2571 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2576 } while (len
== -EINTR
&& !pdu
->cancelled
);
2578 /* IO error return the error */
2582 } while (total
< count
&& len
> 0);
2585 err
= pdu_marshal(pdu
, offset
, "d", total
);
2590 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2592 qemu_iovec_destroy(&qiov
);
2596 qemu_iovec_destroy(&qiov_full
);
2597 pdu_complete(pdu
, err
);
2600 static void coroutine_fn
v9fs_create(void *opaque
)
2612 V9fsString extension
;
2614 V9fsPDU
*pdu
= opaque
;
2615 V9fsState
*s
= pdu
->s
;
2617 v9fs_path_init(&path
);
2618 v9fs_string_init(&name
);
2619 v9fs_string_init(&extension
);
2620 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2621 &perm
, &mode
, &extension
);
2625 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2627 if (name_is_illegal(name
.data
)) {
2632 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2637 fidp
= get_fid(pdu
, fid
);
2642 if (fidp
->fid_type
!= P9_FID_NONE
) {
2646 if (perm
& P9_STAT_MODE_DIR
) {
2647 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2648 fidp
->uid
, -1, &stbuf
);
2652 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2656 v9fs_path_write_lock(s
);
2657 v9fs_path_copy(&fidp
->path
, &path
);
2658 v9fs_path_unlock(s
);
2659 err
= v9fs_co_opendir(pdu
, fidp
);
2663 fidp
->fid_type
= P9_FID_DIR
;
2664 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2665 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2666 extension
.data
, -1 , &stbuf
);
2670 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2674 v9fs_path_write_lock(s
);
2675 v9fs_path_copy(&fidp
->path
, &path
);
2676 v9fs_path_unlock(s
);
2677 } else if (perm
& P9_STAT_MODE_LINK
) {
2678 int32_t ofid
= atoi(extension
.data
);
2679 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2680 if (ofidp
== NULL
) {
2684 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2685 put_fid(pdu
, ofidp
);
2689 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2691 fidp
->fid_type
= P9_FID_NONE
;
2694 v9fs_path_write_lock(s
);
2695 v9fs_path_copy(&fidp
->path
, &path
);
2696 v9fs_path_unlock(s
);
2697 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2699 fidp
->fid_type
= P9_FID_NONE
;
2702 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2704 uint32_t major
, minor
;
2707 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2724 nmode
|= perm
& 0777;
2725 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2726 makedev(major
, minor
), nmode
, &stbuf
);
2730 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2734 v9fs_path_write_lock(s
);
2735 v9fs_path_copy(&fidp
->path
, &path
);
2736 v9fs_path_unlock(s
);
2737 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2738 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2739 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2743 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2747 v9fs_path_write_lock(s
);
2748 v9fs_path_copy(&fidp
->path
, &path
);
2749 v9fs_path_unlock(s
);
2750 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2751 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2752 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2756 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2760 v9fs_path_write_lock(s
);
2761 v9fs_path_copy(&fidp
->path
, &path
);
2762 v9fs_path_unlock(s
);
2764 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2765 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2769 fidp
->fid_type
= P9_FID_FILE
;
2770 fidp
->open_flags
= omode_to_uflags(mode
);
2771 if (fidp
->open_flags
& O_EXCL
) {
2773 * We let the host file system do O_EXCL check
2774 * We should not reclaim such fd
2776 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2779 iounit
= get_iounit(pdu
, &fidp
->path
);
2780 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2784 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2789 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2790 qid
.type
, qid
.version
, qid
.path
, iounit
);
2794 pdu_complete(pdu
, err
);
2795 v9fs_string_free(&name
);
2796 v9fs_string_free(&extension
);
2797 v9fs_path_free(&path
);
2800 static void coroutine_fn
v9fs_symlink(void *opaque
)
2802 V9fsPDU
*pdu
= opaque
;
2805 V9fsFidState
*dfidp
;
2813 v9fs_string_init(&name
);
2814 v9fs_string_init(&symname
);
2815 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2819 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2821 if (name_is_illegal(name
.data
)) {
2826 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2831 dfidp
= get_fid(pdu
, dfid
);
2832 if (dfidp
== NULL
) {
2836 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2840 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2844 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2849 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2850 qid
.type
, qid
.version
, qid
.path
);
2852 put_fid(pdu
, dfidp
);
2854 pdu_complete(pdu
, err
);
2855 v9fs_string_free(&name
);
2856 v9fs_string_free(&symname
);
2859 static void coroutine_fn
v9fs_flush(void *opaque
)
2864 V9fsPDU
*cancel_pdu
= NULL
;
2865 V9fsPDU
*pdu
= opaque
;
2866 V9fsState
*s
= pdu
->s
;
2868 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2870 pdu_complete(pdu
, err
);
2873 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2875 if (pdu
->tag
== tag
) {
2876 warn_report("the guest sent a self-referencing 9P flush request");
2878 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2879 if (cancel_pdu
->tag
== tag
) {
2885 cancel_pdu
->cancelled
= 1;
2887 * Wait for pdu to complete.
2889 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2890 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2891 cancel_pdu
->cancelled
= 0;
2892 pdu_free(cancel_pdu
);
2895 pdu_complete(pdu
, 7);
2898 static void coroutine_fn
v9fs_link(void *opaque
)
2900 V9fsPDU
*pdu
= opaque
;
2901 int32_t dfid
, oldfid
;
2902 V9fsFidState
*dfidp
, *oldfidp
;
2907 v9fs_string_init(&name
);
2908 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2912 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2914 if (name_is_illegal(name
.data
)) {
2919 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2924 dfidp
= get_fid(pdu
, dfid
);
2925 if (dfidp
== NULL
) {
2930 oldfidp
= get_fid(pdu
, oldfid
);
2931 if (oldfidp
== NULL
) {
2935 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2939 put_fid(pdu
, oldfidp
);
2941 put_fid(pdu
, dfidp
);
2943 v9fs_string_free(&name
);
2944 pdu_complete(pdu
, err
);
2947 /* Only works with path name based fid */
2948 static void coroutine_fn
v9fs_remove(void *opaque
)
2954 V9fsPDU
*pdu
= opaque
;
2956 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2960 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2962 fidp
= get_fid(pdu
, fid
);
2967 /* if fs driver is not path based, return EOPNOTSUPP */
2968 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2973 * IF the file is unlinked, we cannot reopen
2974 * the file later. So don't reclaim fd
2976 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2980 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2985 /* For TREMOVE we need to clunk the fid even on failed remove */
2986 clunk_fid(pdu
->s
, fidp
->fid
);
2989 pdu_complete(pdu
, err
);
2992 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2996 int32_t dfid
, flags
, rflags
= 0;
2999 V9fsFidState
*dfidp
;
3000 V9fsPDU
*pdu
= opaque
;
3002 v9fs_string_init(&name
);
3003 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
3008 if (name_is_illegal(name
.data
)) {
3013 if (!strcmp(".", name
.data
)) {
3018 if (!strcmp("..", name
.data
)) {
3023 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
3028 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
3029 rflags
|= AT_REMOVEDIR
;
3032 dfidp
= get_fid(pdu
, dfid
);
3033 if (dfidp
== NULL
) {
3038 * IF the file is unlinked, we cannot reopen
3039 * the file later. So don't reclaim fd
3041 v9fs_path_init(&path
);
3042 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
3046 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
3050 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
3055 put_fid(pdu
, dfidp
);
3056 v9fs_path_free(&path
);
3058 pdu_complete(pdu
, err
);
3059 v9fs_string_free(&name
);
3063 /* Only works with path name based fid */
3064 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
3070 V9fsFidState
*tfidp
;
3071 V9fsState
*s
= pdu
->s
;
3072 V9fsFidState
*dirfidp
= NULL
;
3074 v9fs_path_init(&new_path
);
3075 if (newdirfid
!= -1) {
3076 dirfidp
= get_fid(pdu
, newdirfid
);
3077 if (dirfidp
== NULL
) {
3080 if (fidp
->fid_type
!= P9_FID_NONE
) {
3084 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
3089 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
3092 v9fs_path_init(&dir_path
);
3093 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
3096 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
3097 v9fs_path_free(&dir_path
);
3102 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
3107 * Fixup fid's pointing to the old name to
3108 * start pointing to the new name
3110 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
3111 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
3112 /* replace the name */
3113 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
3118 put_fid(pdu
, dirfidp
);
3120 v9fs_path_free(&new_path
);
3124 /* Only works with path name based fid */
3125 static void coroutine_fn
v9fs_rename(void *opaque
)
3133 V9fsPDU
*pdu
= opaque
;
3134 V9fsState
*s
= pdu
->s
;
3136 v9fs_string_init(&name
);
3137 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
3142 if (name_is_illegal(name
.data
)) {
3147 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3152 fidp
= get_fid(pdu
, fid
);
3157 if (fidp
->fid_type
!= P9_FID_NONE
) {
3161 /* if fs driver is not path based, return EOPNOTSUPP */
3162 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
3166 v9fs_path_write_lock(s
);
3167 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
3168 v9fs_path_unlock(s
);
3175 pdu_complete(pdu
, err
);
3176 v9fs_string_free(&name
);
3179 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
3180 V9fsString
*old_name
,
3182 V9fsString
*new_name
)
3184 V9fsFidState
*tfidp
;
3185 V9fsPath oldpath
, newpath
;
3186 V9fsState
*s
= pdu
->s
;
3189 v9fs_path_init(&oldpath
);
3190 v9fs_path_init(&newpath
);
3191 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
3195 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
3201 * Fixup fid's pointing to the old name to
3202 * start pointing to the new name
3204 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
3205 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
3206 /* replace the name */
3207 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
3211 v9fs_path_free(&oldpath
);
3212 v9fs_path_free(&newpath
);
3216 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
3217 V9fsString
*old_name
,
3219 V9fsString
*new_name
)
3222 V9fsState
*s
= pdu
->s
;
3223 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
3225 olddirfidp
= get_fid(pdu
, olddirfid
);
3226 if (olddirfidp
== NULL
) {
3230 if (newdirfid
!= -1) {
3231 newdirfidp
= get_fid(pdu
, newdirfid
);
3232 if (newdirfidp
== NULL
) {
3237 newdirfidp
= get_fid(pdu
, olddirfid
);
3240 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
3241 &newdirfidp
->path
, new_name
);
3245 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
3246 /* Only for path based fid we need to do the below fixup */
3247 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
3248 &newdirfidp
->path
, new_name
);
3252 put_fid(pdu
, olddirfidp
);
3255 put_fid(pdu
, newdirfidp
);
3260 static void coroutine_fn
v9fs_renameat(void *opaque
)
3264 V9fsPDU
*pdu
= opaque
;
3265 V9fsState
*s
= pdu
->s
;
3266 int32_t olddirfid
, newdirfid
;
3267 V9fsString old_name
, new_name
;
3269 v9fs_string_init(&old_name
);
3270 v9fs_string_init(&new_name
);
3271 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
3272 &old_name
, &newdirfid
, &new_name
);
3277 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3282 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3283 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3288 v9fs_path_write_lock(s
);
3289 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3290 &old_name
, newdirfid
, &new_name
);
3291 v9fs_path_unlock(s
);
3297 pdu_complete(pdu
, err
);
3298 v9fs_string_free(&old_name
);
3299 v9fs_string_free(&new_name
);
3302 static void coroutine_fn
v9fs_wstat(void *opaque
)
3311 V9fsPDU
*pdu
= opaque
;
3312 V9fsState
*s
= pdu
->s
;
3314 v9fs_stat_init(&v9stat
);
3315 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3319 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3320 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3322 fidp
= get_fid(pdu
, fid
);
3327 /* do we need to sync the file? */
3328 if (donttouch_stat(&v9stat
)) {
3329 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3332 if (v9stat
.mode
!= -1) {
3334 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3338 v9_mode
= stat_to_v9mode(&stbuf
);
3339 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3340 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3341 /* Attempting to change the type */
3345 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3346 v9mode_to_mode(v9stat
.mode
,
3347 &v9stat
.extension
));
3352 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3353 struct timespec times
[2];
3354 if (v9stat
.atime
!= -1) {
3355 times
[0].tv_sec
= v9stat
.atime
;
3356 times
[0].tv_nsec
= 0;
3358 times
[0].tv_nsec
= UTIME_OMIT
;
3360 if (v9stat
.mtime
!= -1) {
3361 times
[1].tv_sec
= v9stat
.mtime
;
3362 times
[1].tv_nsec
= 0;
3364 times
[1].tv_nsec
= UTIME_OMIT
;
3366 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3371 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3372 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3377 if (v9stat
.name
.size
!= 0) {
3378 v9fs_path_write_lock(s
);
3379 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3380 v9fs_path_unlock(s
);
3385 if (v9stat
.length
!= -1) {
3386 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3395 v9fs_stat_free(&v9stat
);
3396 pdu_complete(pdu
, err
);
3399 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3411 int32_t bsize_factor
;
3414 * compute bsize factor based on host file system block size
3417 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
3418 if (!bsize_factor
) {
3421 f_type
= stbuf
->f_type
;
3422 f_bsize
= stbuf
->f_bsize
;
3423 f_bsize
*= bsize_factor
;
3425 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3426 * adjust(divide) the number of blocks, free blocks and available
3427 * blocks by bsize factor
3429 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
3430 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
3431 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
3432 f_files
= stbuf
->f_files
;
3433 f_ffree
= stbuf
->f_ffree
;
3434 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3435 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3436 f_namelen
= stbuf
->f_namelen
;
3438 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3439 f_type
, f_bsize
, f_blocks
, f_bfree
,
3440 f_bavail
, f_files
, f_ffree
,
3441 fsid_val
, f_namelen
);
3444 static void coroutine_fn
v9fs_statfs(void *opaque
)
3450 struct statfs stbuf
;
3451 V9fsPDU
*pdu
= opaque
;
3452 V9fsState
*s
= pdu
->s
;
3454 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3458 fidp
= get_fid(pdu
, fid
);
3463 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3467 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3475 pdu_complete(pdu
, retval
);
3478 static void coroutine_fn
v9fs_mknod(void *opaque
)
3491 V9fsPDU
*pdu
= opaque
;
3493 v9fs_string_init(&name
);
3494 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3495 &major
, &minor
, &gid
);
3499 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3501 if (name_is_illegal(name
.data
)) {
3506 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3511 fidp
= get_fid(pdu
, fid
);
3516 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3517 makedev(major
, minor
), mode
, &stbuf
);
3521 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3525 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3530 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3531 qid
.type
, qid
.version
, qid
.path
);
3535 pdu_complete(pdu
, err
);
3536 v9fs_string_free(&name
);
3540 * Implement posix byte range locking code
3541 * Server side handling of locking code is very simple, because 9p server in
3542 * QEMU can handle only one client. And most of the lock handling
3543 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3544 * do any thing in * qemu 9p server side lock code path.
3545 * So when a TLOCK request comes, always return success
3547 static void coroutine_fn
v9fs_lock(void *opaque
)
3553 int32_t fid
, err
= 0;
3554 V9fsPDU
*pdu
= opaque
;
3556 v9fs_string_init(&flock
.client_id
);
3557 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3558 &flock
.flags
, &flock
.start
, &flock
.length
,
3559 &flock
.proc_id
, &flock
.client_id
);
3563 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3564 flock
.type
, flock
.start
, flock
.length
);
3567 /* We support only block flag now (that too ignored currently) */
3568 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3572 fidp
= get_fid(pdu
, fid
);
3577 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3581 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3586 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3590 pdu_complete(pdu
, err
);
3591 v9fs_string_free(&flock
.client_id
);
3595 * When a TGETLOCK request comes, always return success because all lock
3596 * handling is done by client's VFS layer.
3598 static void coroutine_fn
v9fs_getlock(void *opaque
)
3604 int32_t fid
, err
= 0;
3605 V9fsPDU
*pdu
= opaque
;
3607 v9fs_string_init(&glock
.client_id
);
3608 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3609 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3614 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3615 glock
.type
, glock
.start
, glock
.length
);
3617 fidp
= get_fid(pdu
, fid
);
3622 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3626 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3627 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3628 glock
.start
, glock
.length
, glock
.proc_id
,
3634 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3635 glock
.length
, glock
.proc_id
);
3639 pdu_complete(pdu
, err
);
3640 v9fs_string_free(&glock
.client_id
);
3643 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3645 V9fsPDU
*pdu
= opaque
;
3656 v9fs_string_init(&name
);
3657 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3661 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3663 if (name_is_illegal(name
.data
)) {
3668 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3673 fidp
= get_fid(pdu
, fid
);
3678 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3682 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3686 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3691 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3692 qid
.type
, qid
.version
, qid
.path
, err
);
3696 pdu_complete(pdu
, err
);
3697 v9fs_string_free(&name
);
3700 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3706 int32_t fid
, newfid
;
3707 V9fsFidState
*file_fidp
;
3708 V9fsFidState
*xattr_fidp
= NULL
;
3709 V9fsPDU
*pdu
= opaque
;
3710 V9fsState
*s
= pdu
->s
;
3712 v9fs_string_init(&name
);
3713 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3717 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3719 file_fidp
= get_fid(pdu
, fid
);
3720 if (file_fidp
== NULL
) {
3724 xattr_fidp
= alloc_fid(s
, newfid
);
3725 if (xattr_fidp
== NULL
) {
3729 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3730 if (!v9fs_string_size(&name
)) {
3732 * listxattr request. Get the size first
3734 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3737 clunk_fid(s
, xattr_fidp
->fid
);
3741 * Read the xattr value
3743 xattr_fidp
->fs
.xattr
.len
= size
;
3744 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3745 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3746 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3748 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3749 xattr_fidp
->fs
.xattr
.value
,
3750 xattr_fidp
->fs
.xattr
.len
);
3752 clunk_fid(s
, xattr_fidp
->fid
);
3756 err
= pdu_marshal(pdu
, offset
, "q", size
);
3763 * specific xattr fid. We check for xattr
3764 * presence also collect the xattr size
3766 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3770 clunk_fid(s
, xattr_fidp
->fid
);
3774 * Read the xattr value
3776 xattr_fidp
->fs
.xattr
.len
= size
;
3777 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3778 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3779 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3781 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3782 &name
, xattr_fidp
->fs
.xattr
.value
,
3783 xattr_fidp
->fs
.xattr
.len
);
3785 clunk_fid(s
, xattr_fidp
->fid
);
3789 err
= pdu_marshal(pdu
, offset
, "q", size
);
3795 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3797 put_fid(pdu
, file_fidp
);
3799 put_fid(pdu
, xattr_fidp
);
3802 pdu_complete(pdu
, err
);
3803 v9fs_string_free(&name
);
3806 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3808 int flags
, rflags
= 0;
3814 V9fsFidState
*file_fidp
;
3815 V9fsFidState
*xattr_fidp
;
3816 V9fsPDU
*pdu
= opaque
;
3818 v9fs_string_init(&name
);
3819 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3823 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3825 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3830 if (flags
& P9_XATTR_CREATE
) {
3831 rflags
|= XATTR_CREATE
;
3834 if (flags
& P9_XATTR_REPLACE
) {
3835 rflags
|= XATTR_REPLACE
;
3838 if (size
> XATTR_SIZE_MAX
) {
3843 file_fidp
= get_fid(pdu
, fid
);
3844 if (file_fidp
== NULL
) {
3848 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3853 /* Make the file fid point to xattr */
3854 xattr_fidp
= file_fidp
;
3855 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3856 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3857 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3858 xattr_fidp
->fs
.xattr
.len
= size
;
3859 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3860 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3861 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3862 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3865 put_fid(pdu
, file_fidp
);
3867 pdu_complete(pdu
, err
);
3868 v9fs_string_free(&name
);
3871 static void coroutine_fn
v9fs_readlink(void *opaque
)
3873 V9fsPDU
*pdu
= opaque
;
3880 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3884 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3885 fidp
= get_fid(pdu
, fid
);
3891 v9fs_string_init(&target
);
3892 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3896 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3898 v9fs_string_free(&target
);
3902 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3903 v9fs_string_free(&target
);
3907 pdu_complete(pdu
, err
);
3910 static CoroutineEntry
*pdu_co_handlers
[] = {
3911 [P9_TREADDIR
] = v9fs_readdir
,
3912 [P9_TSTATFS
] = v9fs_statfs
,
3913 [P9_TGETATTR
] = v9fs_getattr
,
3914 [P9_TSETATTR
] = v9fs_setattr
,
3915 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3916 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3917 [P9_TMKNOD
] = v9fs_mknod
,
3918 [P9_TRENAME
] = v9fs_rename
,
3919 [P9_TLOCK
] = v9fs_lock
,
3920 [P9_TGETLOCK
] = v9fs_getlock
,
3921 [P9_TRENAMEAT
] = v9fs_renameat
,
3922 [P9_TREADLINK
] = v9fs_readlink
,
3923 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3924 [P9_TMKDIR
] = v9fs_mkdir
,
3925 [P9_TVERSION
] = v9fs_version
,
3926 [P9_TLOPEN
] = v9fs_open
,
3927 [P9_TATTACH
] = v9fs_attach
,
3928 [P9_TSTAT
] = v9fs_stat
,
3929 [P9_TWALK
] = v9fs_walk
,
3930 [P9_TCLUNK
] = v9fs_clunk
,
3931 [P9_TFSYNC
] = v9fs_fsync
,
3932 [P9_TOPEN
] = v9fs_open
,
3933 [P9_TREAD
] = v9fs_read
,
3935 [P9_TAUTH
] = v9fs_auth
,
3937 [P9_TFLUSH
] = v9fs_flush
,
3938 [P9_TLINK
] = v9fs_link
,
3939 [P9_TSYMLINK
] = v9fs_symlink
,
3940 [P9_TCREATE
] = v9fs_create
,
3941 [P9_TLCREATE
] = v9fs_lcreate
,
3942 [P9_TWRITE
] = v9fs_write
,
3943 [P9_TWSTAT
] = v9fs_wstat
,
3944 [P9_TREMOVE
] = v9fs_remove
,
3947 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3949 V9fsPDU
*pdu
= opaque
;
3950 pdu_complete(pdu
, -EOPNOTSUPP
);
3953 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3955 V9fsPDU
*pdu
= opaque
;
3956 pdu_complete(pdu
, -EROFS
);
3959 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3986 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
3989 CoroutineEntry
*handler
;
3990 V9fsState
*s
= pdu
->s
;
3992 pdu
->size
= le32_to_cpu(hdr
->size_le
);
3994 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
3996 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3997 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3998 handler
= v9fs_op_not_supp
;
3999 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
4000 handler
= v9fs_fs_ro
;
4002 handler
= pdu_co_handlers
[pdu
->id
];
4005 qemu_co_queue_init(&pdu
->complete
);
4006 co
= qemu_coroutine_create(handler
, pdu
);
4007 qemu_coroutine_enter(co
);
4010 /* Returns 0 on success, 1 on failure. */
4011 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
4021 assert(!s
->transport
);
4024 /* initialize pdu allocator */
4025 QLIST_INIT(&s
->free_list
);
4026 QLIST_INIT(&s
->active_list
);
4027 for (i
= 0; i
< MAX_REQ
; i
++) {
4028 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
4033 v9fs_path_init(&path
);
4035 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
4038 /* We don't have a fsdev identified by fsdev_id */
4039 error_setg(errp
, "9pfs device couldn't find fsdev with the "
4041 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
4045 if (!s
->fsconf
.tag
) {
4046 /* we haven't specified a mount_tag */
4047 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
4048 s
->fsconf
.fsdev_id
);
4052 s
->ctx
.export_flags
= fse
->export_flags
;
4053 s
->ctx
.fs_root
= g_strdup(fse
->path
);
4054 s
->ctx
.exops
.get_st_gen
= NULL
;
4055 len
= strlen(s
->fsconf
.tag
);
4056 if (len
> MAX_TAG_LEN
- 1) {
4057 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
4058 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
4062 s
->tag
= g_strdup(s
->fsconf
.tag
);
4067 s
->ctx
.fmode
= fse
->fmode
;
4068 s
->ctx
.dmode
= fse
->dmode
;
4071 qemu_co_rwlock_init(&s
->rename_lock
);
4073 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
4074 error_prepend(errp
, "cannot initialize fsdev '%s': ",
4075 s
->fsconf
.fsdev_id
);
4080 * Check details of export path, We need to use fs driver
4081 * call back to do that. Since we are in the init path, we don't
4082 * use co-routines here.
4084 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
4086 "error in converting name to path %s", strerror(errno
));
4089 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
4090 error_setg(errp
, "share path %s does not exist", fse
->path
);
4092 } else if (!S_ISDIR(stat
.st_mode
)) {
4093 error_setg(errp
, "share path %s is not a directory", fse
->path
);
4097 s
->dev_id
= stat
.st_dev
;
4099 /* init inode remapping : */
4100 /* hash table for variable length inode suffixes */
4101 qpd_table_init(&s
->qpd_table
);
4102 /* hash table for slow/full inode remapping (most users won't need it) */
4103 qpf_table_init(&s
->qpf_table
);
4104 /* hash table for quick inode remapping */
4105 qpp_table_init(&s
->qpp_table
);
4107 s
->qp_affix_next
= 1; /* reserve 0 to detect overflow */
4108 s
->qp_fullpath_next
= 1;
4110 s
->ctx
.fst
= &fse
->fst
;
4111 fsdev_throttle_init(s
->ctx
.fst
);
4116 v9fs_device_unrealize_common(s
);
4118 v9fs_path_free(&path
);
4122 void v9fs_device_unrealize_common(V9fsState
*s
)
4124 if (s
->ops
&& s
->ops
->cleanup
) {
4125 s
->ops
->cleanup(&s
->ctx
);
4128 fsdev_throttle_cleanup(s
->ctx
.fst
);
4131 qp_table_destroy(&s
->qpd_table
);
4132 qp_table_destroy(&s
->qpp_table
);
4133 qp_table_destroy(&s
->qpf_table
);
4134 g_free(s
->ctx
.fs_root
);
4137 typedef struct VirtfsCoResetData
{
4140 } VirtfsCoResetData
;
4142 static void coroutine_fn
virtfs_co_reset(void *opaque
)
4144 VirtfsCoResetData
*data
= opaque
;
4146 virtfs_reset(&data
->pdu
);
4150 void v9fs_reset(V9fsState
*s
)
4152 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
4155 while (!QLIST_EMPTY(&s
->active_list
)) {
4156 aio_poll(qemu_get_aio_context(), true);
4159 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
4160 qemu_coroutine_enter(co
);
4162 while (!data
.done
) {
4163 aio_poll(qemu_get_aio_context(), true);
4167 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
4170 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
4171 error_report("Failed to get the resource limit");
4174 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
4175 open_fd_rc
= rlim
.rlim_cur
/2;