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"
33 static int open_fd_rc
;
47 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
53 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
59 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
65 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
71 static int omode_to_uflags(int8_t mode
)
105 typedef struct DotlOpenflagMap
{
110 static int dotl_to_open_flags(int flags
)
114 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
115 * and P9_DOTL_NOACCESS
117 int oflags
= flags
& O_ACCMODE
;
119 DotlOpenflagMap dotl_oflag_map
[] = {
120 { P9_DOTL_CREATE
, O_CREAT
},
121 { P9_DOTL_EXCL
, O_EXCL
},
122 { P9_DOTL_NOCTTY
, O_NOCTTY
},
123 { P9_DOTL_TRUNC
, O_TRUNC
},
124 { P9_DOTL_APPEND
, O_APPEND
},
125 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
126 { P9_DOTL_DSYNC
, O_DSYNC
},
127 { P9_DOTL_FASYNC
, FASYNC
},
128 { P9_DOTL_DIRECT
, O_DIRECT
},
129 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
130 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
131 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
132 { P9_DOTL_NOATIME
, O_NOATIME
},
133 { P9_DOTL_SYNC
, O_SYNC
},
136 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
137 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
138 oflags
|= dotl_oflag_map
[i
].open_flag
;
145 void cred_init(FsCred
*credp
)
153 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
157 * Filter the client open flags
159 flags
= dotl_to_open_flags(oflags
);
160 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
162 * Ignore direct disk access hint until the server supports it.
168 void v9fs_path_init(V9fsPath
*path
)
174 void v9fs_path_free(V9fsPath
*path
)
182 void GCC_FMT_ATTR(2, 3)
183 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
187 v9fs_path_free(path
);
190 /* Bump the size for including terminating NULL */
191 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
195 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
198 dst
->size
= src
->size
;
199 dst
->data
= g_memdup(src
->data
, src
->size
);
202 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
203 const char *name
, V9fsPath
*path
)
206 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
214 * Return TRUE if s1 is an ancestor of s2.
216 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
217 * As a special case, We treat s1 as ancestor of s2 if they are same!
219 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
221 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
222 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
229 static size_t v9fs_string_size(V9fsString
*str
)
235 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
236 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
239 if (f
->fid_type
== P9_FID_FILE
) {
240 if (f
->fs
.fd
== -1) {
242 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
243 } while (err
== -EINTR
&& !pdu
->cancelled
);
245 } else if (f
->fid_type
== P9_FID_DIR
) {
246 if (f
->fs
.dir
.stream
== NULL
) {
248 err
= v9fs_co_opendir(pdu
, f
);
249 } while (err
== -EINTR
&& !pdu
->cancelled
);
255 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
259 V9fsState
*s
= pdu
->s
;
261 for (f
= s
->fid_list
; f
; f
= f
->next
) {
265 * Update the fid ref upfront so that
266 * we don't get reclaimed when we yield
271 * check whether we need to reopen the
272 * file. We might have closed the fd
273 * while trying to free up some file
276 err
= v9fs_reopen_fid(pdu
, f
);
282 * Mark the fid as referenced so that the LRU
283 * reclaim won't close the file descriptor
285 f
->flags
|= FID_REFERENCED
;
292 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
296 for (f
= s
->fid_list
; f
; f
= f
->next
) {
297 /* If fid is already there return NULL */
303 f
= g_malloc0(sizeof(V9fsFidState
));
305 f
->fid_type
= P9_FID_NONE
;
308 * Mark the fid as referenced so that the LRU
309 * reclaim won't close the file descriptor
311 f
->flags
|= FID_REFERENCED
;
312 f
->next
= s
->fid_list
;
315 v9fs_readdir_init(&f
->fs
.dir
);
316 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
321 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
325 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
326 /* getxattr/listxattr fid */
330 * if this is fid for setxattr. clunk should
331 * result in setxattr localcall
333 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
334 /* clunk after partial write */
338 if (fidp
->fs
.xattr
.len
) {
339 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
340 fidp
->fs
.xattr
.value
,
342 fidp
->fs
.xattr
.flags
);
344 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
347 v9fs_string_free(&fidp
->fs
.xattr
.name
);
349 g_free(fidp
->fs
.xattr
.value
);
353 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
357 if (fidp
->fid_type
== P9_FID_FILE
) {
358 /* If we reclaimed the fd no need to close */
359 if (fidp
->fs
.fd
!= -1) {
360 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
362 } else if (fidp
->fid_type
== P9_FID_DIR
) {
363 if (fidp
->fs
.dir
.stream
!= NULL
) {
364 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
366 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
367 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
369 v9fs_path_free(&fidp
->path
);
374 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
379 * Don't free the fid if it is in reclaim list
381 if (!fidp
->ref
&& fidp
->clunked
) {
382 if (fidp
->fid
== pdu
->s
->root_fid
) {
384 * if the clunked fid is root fid then we
385 * have unmounted the fs on the client side.
386 * delete the migration blocker. Ideally, this
387 * should be hooked to transport close notification
389 if (pdu
->s
->migration_blocker
) {
390 migrate_del_blocker(pdu
->s
->migration_blocker
);
391 error_free(pdu
->s
->migration_blocker
);
392 pdu
->s
->migration_blocker
= NULL
;
395 return free_fid(pdu
, fidp
);
400 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
402 V9fsFidState
**fidpp
, *fidp
;
404 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
405 if ((*fidpp
)->fid
== fid
) {
409 if (*fidpp
== NULL
) {
418 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
420 int reclaim_count
= 0;
421 V9fsState
*s
= pdu
->s
;
422 V9fsFidState
*f
, *reclaim_list
= NULL
;
424 for (f
= s
->fid_list
; f
; f
= f
->next
) {
426 * Unlink fids cannot be reclaimed. Check
427 * for them and skip them. Also skip fids
428 * currently being operated on.
430 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
434 * if it is a recently referenced fid
435 * we leave the fid untouched and clear the
436 * reference bit. We come back to it later
437 * in the next iteration. (a simple LRU without
438 * moving list elements around)
440 if (f
->flags
& FID_REFERENCED
) {
441 f
->flags
&= ~FID_REFERENCED
;
445 * Add fids to reclaim list.
447 if (f
->fid_type
== P9_FID_FILE
) {
448 if (f
->fs
.fd
!= -1) {
450 * Up the reference count so that
451 * a clunk request won't free this fid
454 f
->rclm_lst
= reclaim_list
;
456 f
->fs_reclaim
.fd
= f
->fs
.fd
;
460 } else if (f
->fid_type
== P9_FID_DIR
) {
461 if (f
->fs
.dir
.stream
!= NULL
) {
463 * Up the reference count so that
464 * a clunk request won't free this fid
467 f
->rclm_lst
= reclaim_list
;
469 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
470 f
->fs
.dir
.stream
= NULL
;
474 if (reclaim_count
>= open_fd_rc
) {
479 * Now close the fid in reclaim list. Free them if they
480 * are already clunked.
482 while (reclaim_list
) {
484 reclaim_list
= f
->rclm_lst
;
485 if (f
->fid_type
== P9_FID_FILE
) {
486 v9fs_co_close(pdu
, &f
->fs_reclaim
);
487 } else if (f
->fid_type
== P9_FID_DIR
) {
488 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
492 * Now drop the fid reference, free it
499 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
502 V9fsState
*s
= pdu
->s
;
503 V9fsFidState
*fidp
, head_fid
;
505 head_fid
.next
= s
->fid_list
;
506 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
507 if (fidp
->path
.size
!= path
->size
) {
510 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
511 /* Mark the fid non reclaimable. */
512 fidp
->flags
|= FID_NON_RECLAIMABLE
;
514 /* reopen the file/dir if already closed */
515 err
= v9fs_reopen_fid(pdu
, fidp
);
520 * Go back to head of fid list because
521 * the list could have got updated when
522 * switched to the worker thread
532 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
534 V9fsState
*s
= pdu
->s
;
538 while (s
->fid_list
) {
544 s
->fid_list
= fidp
->next
;
551 #define P9_QID_TYPE_DIR 0x80
552 #define P9_QID_TYPE_SYMLINK 0x02
554 #define P9_STAT_MODE_DIR 0x80000000
555 #define P9_STAT_MODE_APPEND 0x40000000
556 #define P9_STAT_MODE_EXCL 0x20000000
557 #define P9_STAT_MODE_MOUNT 0x10000000
558 #define P9_STAT_MODE_AUTH 0x08000000
559 #define P9_STAT_MODE_TMP 0x04000000
560 #define P9_STAT_MODE_SYMLINK 0x02000000
561 #define P9_STAT_MODE_LINK 0x01000000
562 #define P9_STAT_MODE_DEVICE 0x00800000
563 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
564 #define P9_STAT_MODE_SOCKET 0x00100000
565 #define P9_STAT_MODE_SETUID 0x00080000
566 #define P9_STAT_MODE_SETGID 0x00040000
567 #define P9_STAT_MODE_SETVTX 0x00010000
569 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
570 P9_STAT_MODE_SYMLINK | \
571 P9_STAT_MODE_LINK | \
572 P9_STAT_MODE_DEVICE | \
573 P9_STAT_MODE_NAMED_PIPE | \
576 /* creative abuse of tb_hash_func7, which is based on xxhash */
577 static uint32_t qpp_hash(QppEntry e
)
579 return qemu_xxhash7(e
.ino_prefix
, e
.dev
, 0, 0, 0);
582 static bool qpp_lookup_func(const void *obj
, const void *userp
)
584 const QppEntry
*e1
= obj
, *e2
= userp
;
585 return e1
->dev
== e2
->dev
&& e1
->ino_prefix
== e2
->ino_prefix
;
588 static void qpp_table_remove(void *p
, uint32_t h
, void *up
)
593 static void qpp_table_destroy(struct qht
*ht
)
595 if (!ht
|| !ht
->map
) {
598 qht_iter(ht
, qpp_table_remove
, NULL
);
602 static void qpp_table_init(struct qht
*ht
)
604 qht_init(ht
, qpp_lookup_func
, 1, QHT_MODE_AUTO_RESIZE
);
608 * stat_to_qid needs to map inode number (64 bits) and device id (32 bits)
609 * to a unique QID path (64 bits). To avoid having to map and keep track
610 * of up to 2^64 objects, we map only the 16 highest bits of the inode plus
611 * the device id to the 16 highest bits of the QID path. The 48 lowest bits
612 * of the QID path equal to the lowest bits of the inode number.
614 * This takes advantage of the fact that inode number are usually not
615 * random but allocated sequentially, so we have fewer items to keep
618 static int qid_path_prefixmap(V9fsPDU
*pdu
, const struct stat
*stbuf
,
622 .dev
= stbuf
->st_dev
,
623 .ino_prefix
= (uint16_t) (stbuf
->st_ino
>> 48)
625 uint32_t hash
= qpp_hash(lookup
);
627 val
= qht_lookup(&pdu
->s
->qpp_table
, &lookup
, hash
);
630 if (pdu
->s
->qp_prefix_next
== 0) {
631 /* we ran out of prefixes */
633 "9p: No more prefixes available for remapping inodes from "
639 val
= g_malloc0(sizeof(QppEntry
));
642 /* new unique inode prefix and device combo */
643 val
->qp_prefix
= pdu
->s
->qp_prefix_next
++;
644 qht_insert(&pdu
->s
->qpp_table
, val
, hash
, NULL
);
647 *path
= ((uint64_t)val
->qp_prefix
<< 48) | (stbuf
->st_ino
& QPATH_INO_MASK
);
651 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
656 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
657 /* map inode+device to qid path (fast path) */
658 err
= qid_path_prefixmap(pdu
, stbuf
, &qidp
->path
);
663 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
664 if (pdu
->s
->ctx
.export_flags
& V9FS_FORBID_MULTIDEVS
) {
666 "9p: Multiple devices detected in same VirtFS export. "
667 "Access of guest to additional devices is (partly) "
668 "denied due to virtfs option 'multidevs=forbid' being "
674 "9p: Multiple devices detected in same VirtFS export, "
675 "which might lead to file ID collisions and severe "
676 "misbehaviours on guest! You should either use a "
677 "separate export for each device shared from host or "
678 "use virtfs option 'multidevs=remap'!"
682 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
683 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
684 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
687 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
689 if (S_ISDIR(stbuf
->st_mode
)) {
690 qidp
->type
|= P9_QID_TYPE_DIR
;
692 if (S_ISLNK(stbuf
->st_mode
)) {
693 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
699 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
705 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
709 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
716 static int coroutine_fn
dirent_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
717 struct dirent
*dent
, V9fsQID
*qidp
)
723 v9fs_path_init(&path
);
725 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
729 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
733 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
736 v9fs_path_free(&path
);
740 V9fsPDU
*pdu_alloc(V9fsState
*s
)
744 if (!QLIST_EMPTY(&s
->free_list
)) {
745 pdu
= QLIST_FIRST(&s
->free_list
);
746 QLIST_REMOVE(pdu
, next
);
747 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
752 void pdu_free(V9fsPDU
*pdu
)
754 V9fsState
*s
= pdu
->s
;
756 g_assert(!pdu
->cancelled
);
757 QLIST_REMOVE(pdu
, next
);
758 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
761 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
763 int8_t id
= pdu
->id
+ 1; /* Response */
764 V9fsState
*s
= pdu
->s
;
768 * The 9p spec requires that successfully cancelled pdus receive no reply.
769 * Sending a reply would confuse clients because they would
770 * assume that any EINTR is the actual result of the operation,
771 * rather than a consequence of the cancellation. However, if
772 * the operation completed (succesfully or with an error other
773 * than caused be cancellation), we do send out that reply, both
774 * for efficiency and to avoid confusing the rest of the state machine
775 * that assumes passing a non-error here will mean a successful
776 * transmission of the reply.
778 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
780 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
789 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
792 str
.data
= strerror(err
);
793 str
.size
= strlen(str
.data
);
795 ret
= pdu_marshal(pdu
, len
, "s", &str
);
803 ret
= pdu_marshal(pdu
, len
, "d", err
);
809 if (s
->proto_version
== V9FS_PROTO_2000L
) {
812 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
815 /* fill out the header */
816 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
820 /* keep these in sync */
825 pdu
->s
->transport
->push_and_notify(pdu
);
827 /* Now wakeup anybody waiting in flush for this request */
828 if (!qemu_co_queue_next(&pdu
->complete
)) {
833 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
838 if (mode
& P9_STAT_MODE_DIR
) {
842 if (mode
& P9_STAT_MODE_SYMLINK
) {
845 if (mode
& P9_STAT_MODE_SOCKET
) {
848 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
851 if (mode
& P9_STAT_MODE_DEVICE
) {
852 if (extension
->size
&& extension
->data
[0] == 'c') {
863 if (mode
& P9_STAT_MODE_SETUID
) {
866 if (mode
& P9_STAT_MODE_SETGID
) {
869 if (mode
& P9_STAT_MODE_SETVTX
) {
876 static int donttouch_stat(V9fsStat
*stat
)
878 if (stat
->type
== -1 &&
880 stat
->qid
.type
== 0xff &&
881 stat
->qid
.version
== (uint32_t) -1 &&
882 stat
->qid
.path
== (uint64_t) -1 &&
886 stat
->length
== -1 &&
893 stat
->n_muid
== -1) {
900 static void v9fs_stat_init(V9fsStat
*stat
)
902 v9fs_string_init(&stat
->name
);
903 v9fs_string_init(&stat
->uid
);
904 v9fs_string_init(&stat
->gid
);
905 v9fs_string_init(&stat
->muid
);
906 v9fs_string_init(&stat
->extension
);
909 static void v9fs_stat_free(V9fsStat
*stat
)
911 v9fs_string_free(&stat
->name
);
912 v9fs_string_free(&stat
->uid
);
913 v9fs_string_free(&stat
->gid
);
914 v9fs_string_free(&stat
->muid
);
915 v9fs_string_free(&stat
->extension
);
918 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
922 mode
= stbuf
->st_mode
& 0777;
923 if (S_ISDIR(stbuf
->st_mode
)) {
924 mode
|= P9_STAT_MODE_DIR
;
927 if (S_ISLNK(stbuf
->st_mode
)) {
928 mode
|= P9_STAT_MODE_SYMLINK
;
931 if (S_ISSOCK(stbuf
->st_mode
)) {
932 mode
|= P9_STAT_MODE_SOCKET
;
935 if (S_ISFIFO(stbuf
->st_mode
)) {
936 mode
|= P9_STAT_MODE_NAMED_PIPE
;
939 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
940 mode
|= P9_STAT_MODE_DEVICE
;
943 if (stbuf
->st_mode
& S_ISUID
) {
944 mode
|= P9_STAT_MODE_SETUID
;
947 if (stbuf
->st_mode
& S_ISGID
) {
948 mode
|= P9_STAT_MODE_SETGID
;
951 if (stbuf
->st_mode
& S_ISVTX
) {
952 mode
|= P9_STAT_MODE_SETVTX
;
958 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
959 const char *basename
,
960 const struct stat
*stbuf
,
965 memset(v9stat
, 0, sizeof(*v9stat
));
967 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
971 v9stat
->mode
= stat_to_v9mode(stbuf
);
972 v9stat
->atime
= stbuf
->st_atime
;
973 v9stat
->mtime
= stbuf
->st_mtime
;
974 v9stat
->length
= stbuf
->st_size
;
976 v9fs_string_free(&v9stat
->uid
);
977 v9fs_string_free(&v9stat
->gid
);
978 v9fs_string_free(&v9stat
->muid
);
980 v9stat
->n_uid
= stbuf
->st_uid
;
981 v9stat
->n_gid
= stbuf
->st_gid
;
984 v9fs_string_free(&v9stat
->extension
);
986 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
987 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
991 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
992 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
993 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
994 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
995 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
996 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
997 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1000 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
1003 v9fs_string_size(&v9stat
->name
) +
1004 v9fs_string_size(&v9stat
->uid
) +
1005 v9fs_string_size(&v9stat
->gid
) +
1006 v9fs_string_size(&v9stat
->muid
) +
1007 v9fs_string_size(&v9stat
->extension
);
1011 #define P9_STATS_MODE 0x00000001ULL
1012 #define P9_STATS_NLINK 0x00000002ULL
1013 #define P9_STATS_UID 0x00000004ULL
1014 #define P9_STATS_GID 0x00000008ULL
1015 #define P9_STATS_RDEV 0x00000010ULL
1016 #define P9_STATS_ATIME 0x00000020ULL
1017 #define P9_STATS_MTIME 0x00000040ULL
1018 #define P9_STATS_CTIME 0x00000080ULL
1019 #define P9_STATS_INO 0x00000100ULL
1020 #define P9_STATS_SIZE 0x00000200ULL
1021 #define P9_STATS_BLOCKS 0x00000400ULL
1023 #define P9_STATS_BTIME 0x00000800ULL
1024 #define P9_STATS_GEN 0x00001000ULL
1025 #define P9_STATS_DATA_VERSION 0x00002000ULL
1027 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1028 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1031 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
1032 V9fsStatDotl
*v9lstat
)
1034 memset(v9lstat
, 0, sizeof(*v9lstat
));
1036 v9lstat
->st_mode
= stbuf
->st_mode
;
1037 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1038 v9lstat
->st_uid
= stbuf
->st_uid
;
1039 v9lstat
->st_gid
= stbuf
->st_gid
;
1040 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1041 v9lstat
->st_size
= stbuf
->st_size
;
1042 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1043 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1044 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1045 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1046 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1047 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1048 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1049 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1050 /* Currently we only support BASIC fields in stat */
1051 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1053 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
1056 static void print_sg(struct iovec
*sg
, int cnt
)
1060 printf("sg[%d]: {", cnt
);
1061 for (i
= 0; i
< cnt
; i
++) {
1065 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1070 /* Will call this only for path name based fid */
1071 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1074 v9fs_path_init(&str
);
1075 v9fs_path_copy(&str
, dst
);
1076 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
1077 v9fs_path_free(&str
);
1080 static inline bool is_ro_export(FsContext
*ctx
)
1082 return ctx
->export_flags
& V9FS_RDONLY
;
1085 static void coroutine_fn
v9fs_version(void *opaque
)
1088 V9fsPDU
*pdu
= opaque
;
1089 V9fsState
*s
= pdu
->s
;
1093 v9fs_string_init(&version
);
1094 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1098 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1102 if (!strcmp(version
.data
, "9P2000.u")) {
1103 s
->proto_version
= V9FS_PROTO_2000U
;
1104 } else if (!strcmp(version
.data
, "9P2000.L")) {
1105 s
->proto_version
= V9FS_PROTO_2000L
;
1107 v9fs_string_sprintf(&version
, "unknown");
1110 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1115 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1117 pdu_complete(pdu
, err
);
1118 v9fs_string_free(&version
);
1121 static void coroutine_fn
v9fs_attach(void *opaque
)
1123 V9fsPDU
*pdu
= opaque
;
1124 V9fsState
*s
= pdu
->s
;
1125 int32_t fid
, afid
, n_uname
;
1126 V9fsString uname
, aname
;
1131 Error
*local_err
= NULL
;
1133 v9fs_string_init(&uname
);
1134 v9fs_string_init(&aname
);
1135 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1136 &afid
, &uname
, &aname
, &n_uname
);
1140 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1142 fidp
= alloc_fid(s
, fid
);
1147 fidp
->uid
= n_uname
;
1148 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1154 err
= fid_to_qid(pdu
, fidp
, &qid
);
1162 * disable migration if we haven't done already.
1163 * attach could get called multiple times for the same export.
1165 if (!s
->migration_blocker
) {
1166 error_setg(&s
->migration_blocker
,
1167 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1168 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1169 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1171 error_free(local_err
);
1172 error_free(s
->migration_blocker
);
1173 s
->migration_blocker
= NULL
;
1180 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1187 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1188 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1189 qid
.type
, qid
.version
, qid
.path
);
1193 pdu_complete(pdu
, err
);
1194 v9fs_string_free(&uname
);
1195 v9fs_string_free(&aname
);
1198 static void coroutine_fn
v9fs_stat(void *opaque
)
1206 V9fsPDU
*pdu
= opaque
;
1209 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1213 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1215 fidp
= get_fid(pdu
, fid
);
1220 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1224 basename
= g_path_get_basename(fidp
->path
.data
);
1225 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1230 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1232 v9fs_stat_free(&v9stat
);
1235 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1236 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1238 v9fs_stat_free(&v9stat
);
1242 pdu_complete(pdu
, err
);
1245 static void coroutine_fn
v9fs_getattr(void *opaque
)
1252 uint64_t request_mask
;
1253 V9fsStatDotl v9stat_dotl
;
1254 V9fsPDU
*pdu
= opaque
;
1256 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1260 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1262 fidp
= get_fid(pdu
, fid
);
1268 * Currently we only support BASIC fields in stat, so there is no
1269 * need to look at request_mask.
1271 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1275 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1280 /* fill st_gen if requested and supported by underlying fs */
1281 if (request_mask
& P9_STATS_GEN
) {
1282 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1285 /* we have valid st_gen: update result mask */
1286 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1289 /* request cancelled, e.g. by Tflush */
1292 /* failed to get st_gen: not fatal, ignore */
1296 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1301 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1302 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1303 v9stat_dotl
.st_gid
);
1307 pdu_complete(pdu
, retval
);
1310 /* Attribute flags */
1311 #define P9_ATTR_MODE (1 << 0)
1312 #define P9_ATTR_UID (1 << 1)
1313 #define P9_ATTR_GID (1 << 2)
1314 #define P9_ATTR_SIZE (1 << 3)
1315 #define P9_ATTR_ATIME (1 << 4)
1316 #define P9_ATTR_MTIME (1 << 5)
1317 #define P9_ATTR_CTIME (1 << 6)
1318 #define P9_ATTR_ATIME_SET (1 << 7)
1319 #define P9_ATTR_MTIME_SET (1 << 8)
1321 #define P9_ATTR_MASK 127
1323 static void coroutine_fn
v9fs_setattr(void *opaque
)
1330 V9fsPDU
*pdu
= opaque
;
1332 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1337 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1338 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1339 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1341 fidp
= get_fid(pdu
, fid
);
1346 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1347 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1352 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1353 struct timespec times
[2];
1354 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1355 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1356 times
[0].tv_sec
= v9iattr
.atime_sec
;
1357 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1359 times
[0].tv_nsec
= UTIME_NOW
;
1362 times
[0].tv_nsec
= UTIME_OMIT
;
1364 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1365 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1366 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1367 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1369 times
[1].tv_nsec
= UTIME_NOW
;
1372 times
[1].tv_nsec
= UTIME_OMIT
;
1374 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1380 * If the only valid entry in iattr is ctime we can call
1381 * chown(-1,-1) to update the ctime of the file
1383 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1384 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1385 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1386 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1389 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1392 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1398 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1399 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1405 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1409 pdu_complete(pdu
, err
);
1412 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1418 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1423 for (i
= 0; i
< nwnames
; i
++) {
1424 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1433 static bool name_is_illegal(const char *name
)
1435 return !*name
|| strchr(name
, '/') != NULL
;
1438 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1441 qid1
->type
!= qid2
->type
||
1442 qid1
->version
!= qid2
->version
||
1443 qid1
->path
!= qid2
->path
;
1446 static void coroutine_fn
v9fs_walk(void *opaque
)
1449 V9fsQID
*qids
= NULL
;
1451 V9fsPath dpath
, path
;
1455 int32_t fid
, newfid
;
1456 V9fsString
*wnames
= NULL
;
1458 V9fsFidState
*newfidp
= NULL
;
1459 V9fsPDU
*pdu
= opaque
;
1460 V9fsState
*s
= pdu
->s
;
1463 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1465 pdu_complete(pdu
, err
);
1470 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1472 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1473 wnames
= g_new0(V9fsString
, nwnames
);
1474 qids
= g_new0(V9fsQID
, nwnames
);
1475 for (i
= 0; i
< nwnames
; i
++) {
1476 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1480 if (name_is_illegal(wnames
[i
].data
)) {
1486 } else if (nwnames
> P9_MAXWELEM
) {
1490 fidp
= get_fid(pdu
, fid
);
1496 v9fs_path_init(&dpath
);
1497 v9fs_path_init(&path
);
1499 err
= fid_to_qid(pdu
, fidp
, &qid
);
1505 * Both dpath and path initially poin to fidp.
1506 * Needed to handle request with nwnames == 0
1508 v9fs_path_copy(&dpath
, &fidp
->path
);
1509 v9fs_path_copy(&path
, &fidp
->path
);
1510 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1511 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1512 strcmp("..", wnames
[name_idx
].data
)) {
1513 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1519 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1523 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1527 v9fs_path_copy(&dpath
, &path
);
1529 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1531 if (fid
== newfid
) {
1532 if (fidp
->fid_type
!= P9_FID_NONE
) {
1536 v9fs_path_write_lock(s
);
1537 v9fs_path_copy(&fidp
->path
, &path
);
1538 v9fs_path_unlock(s
);
1540 newfidp
= alloc_fid(s
, newfid
);
1541 if (newfidp
== NULL
) {
1545 newfidp
->uid
= fidp
->uid
;
1546 v9fs_path_copy(&newfidp
->path
, &path
);
1548 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1549 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1553 put_fid(pdu
, newfidp
);
1555 v9fs_path_free(&dpath
);
1556 v9fs_path_free(&path
);
1558 pdu_complete(pdu
, err
);
1559 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1560 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1561 v9fs_string_free(&wnames
[name_idx
]);
1568 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1570 struct statfs stbuf
;
1572 V9fsState
*s
= pdu
->s
;
1575 * iounit should be multiples of f_bsize (host filesystem block size
1576 * and as well as less than (client msize - P9_IOHDRSZ))
1578 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1579 iounit
= stbuf
.f_bsize
;
1580 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1583 iounit
= s
->msize
- P9_IOHDRSZ
;
1588 static void coroutine_fn
v9fs_open(void *opaque
)
1599 V9fsPDU
*pdu
= opaque
;
1600 V9fsState
*s
= pdu
->s
;
1602 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1603 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1606 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1612 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1614 fidp
= get_fid(pdu
, fid
);
1619 if (fidp
->fid_type
!= P9_FID_NONE
) {
1624 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1628 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1632 if (S_ISDIR(stbuf
.st_mode
)) {
1633 err
= v9fs_co_opendir(pdu
, fidp
);
1637 fidp
->fid_type
= P9_FID_DIR
;
1638 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1644 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1645 flags
= get_dotl_openflags(s
, mode
);
1647 flags
= omode_to_uflags(mode
);
1649 if (is_ro_export(&s
->ctx
)) {
1650 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1651 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1656 err
= v9fs_co_open(pdu
, fidp
, flags
);
1660 fidp
->fid_type
= P9_FID_FILE
;
1661 fidp
->open_flags
= flags
;
1662 if (flags
& O_EXCL
) {
1664 * We let the host file system do O_EXCL check
1665 * We should not reclaim such fd
1667 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1669 iounit
= get_iounit(pdu
, &fidp
->path
);
1670 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1676 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1677 qid
.type
, qid
.version
, qid
.path
, iounit
);
1681 pdu_complete(pdu
, err
);
1684 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1686 int32_t dfid
, flags
, mode
;
1695 V9fsPDU
*pdu
= opaque
;
1697 v9fs_string_init(&name
);
1698 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1699 &name
, &flags
, &mode
, &gid
);
1703 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1705 if (name_is_illegal(name
.data
)) {
1710 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1715 fidp
= get_fid(pdu
, dfid
);
1720 if (fidp
->fid_type
!= P9_FID_NONE
) {
1725 flags
= get_dotl_openflags(pdu
->s
, flags
);
1726 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1727 flags
| O_CREAT
, mode
, &stbuf
);
1731 fidp
->fid_type
= P9_FID_FILE
;
1732 fidp
->open_flags
= flags
;
1733 if (flags
& O_EXCL
) {
1735 * We let the host file system do O_EXCL check
1736 * We should not reclaim such fd
1738 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1740 iounit
= get_iounit(pdu
, &fidp
->path
);
1741 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1745 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1750 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1751 qid
.type
, qid
.version
, qid
.path
, iounit
);
1755 pdu_complete(pdu
, err
);
1756 v9fs_string_free(&name
);
1759 static void coroutine_fn
v9fs_fsync(void *opaque
)
1766 V9fsPDU
*pdu
= opaque
;
1768 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1772 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1774 fidp
= get_fid(pdu
, fid
);
1779 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1785 pdu_complete(pdu
, err
);
1788 static void coroutine_fn
v9fs_clunk(void *opaque
)
1794 V9fsPDU
*pdu
= opaque
;
1795 V9fsState
*s
= pdu
->s
;
1797 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1801 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1803 fidp
= clunk_fid(s
, fid
);
1809 * Bump the ref so that put_fid will
1813 err
= put_fid(pdu
, fidp
);
1818 pdu_complete(pdu
, err
);
1822 * Create a QEMUIOVector for a sub-region of PDU iovecs
1824 * @qiov: uninitialized QEMUIOVector
1825 * @skip: number of bytes to skip from beginning of PDU
1826 * @size: number of bytes to include
1827 * @is_write: true - write, false - read
1829 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1830 * with qemu_iovec_destroy().
1832 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1833 size_t skip
, size_t size
,
1841 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1843 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1846 qemu_iovec_init_external(&elem
, iov
, niov
);
1847 qemu_iovec_init(qiov
, niov
);
1848 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1851 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1852 uint64_t off
, uint32_t max_count
)
1856 uint64_t read_count
;
1857 QEMUIOVector qiov_full
;
1859 if (fidp
->fs
.xattr
.len
< off
) {
1862 read_count
= fidp
->fs
.xattr
.len
- off
;
1864 if (read_count
> max_count
) {
1865 read_count
= max_count
;
1867 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1873 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
1874 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
1875 ((char *)fidp
->fs
.xattr
.value
) + off
,
1877 qemu_iovec_destroy(&qiov_full
);
1885 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1894 off_t saved_dir_pos
;
1895 struct dirent
*dent
;
1897 /* save the directory position */
1898 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1899 if (saved_dir_pos
< 0) {
1900 return saved_dir_pos
;
1904 v9fs_path_init(&path
);
1906 v9fs_readdir_lock(&fidp
->fs
.dir
);
1908 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1912 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1916 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1920 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
1924 if ((count
+ v9stat
.size
+ 2) > max_count
) {
1925 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1927 /* Ran out of buffer. Set dir back to old position and return */
1928 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1929 v9fs_stat_free(&v9stat
);
1930 v9fs_path_free(&path
);
1934 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1935 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1937 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1940 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1941 v9fs_stat_free(&v9stat
);
1942 v9fs_path_free(&path
);
1946 v9fs_stat_free(&v9stat
);
1947 v9fs_path_free(&path
);
1948 saved_dir_pos
= dent
->d_off
;
1951 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1953 v9fs_path_free(&path
);
1960 static void coroutine_fn
v9fs_read(void *opaque
)
1969 V9fsPDU
*pdu
= opaque
;
1970 V9fsState
*s
= pdu
->s
;
1972 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1976 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1978 fidp
= get_fid(pdu
, fid
);
1983 if (fidp
->fid_type
== P9_FID_DIR
) {
1986 v9fs_co_rewinddir(pdu
, fidp
);
1988 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1993 err
= pdu_marshal(pdu
, offset
, "d", count
);
1997 err
+= offset
+ count
;
1998 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1999 QEMUIOVector qiov_full
;
2003 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
2004 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2006 qemu_iovec_reset(&qiov
);
2007 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
2009 print_sg(qiov
.iov
, qiov
.niov
);
2011 /* Loop in case of EINTR */
2013 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2018 } while (len
== -EINTR
&& !pdu
->cancelled
);
2020 /* IO error return the error */
2022 goto out_free_iovec
;
2024 } while (count
< max_count
&& len
> 0);
2025 err
= pdu_marshal(pdu
, offset
, "d", count
);
2027 goto out_free_iovec
;
2029 err
+= offset
+ count
;
2031 qemu_iovec_destroy(&qiov
);
2032 qemu_iovec_destroy(&qiov_full
);
2033 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2034 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
2038 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
2042 pdu_complete(pdu
, err
);
2045 static size_t v9fs_readdir_data_size(V9fsString
*name
)
2048 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2049 * size of type (1) + size of name.size (2) + strlen(name.data)
2051 return 24 + v9fs_string_size(name
);
2054 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2062 off_t saved_dir_pos
;
2063 struct dirent
*dent
;
2065 /* save the directory position */
2066 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2067 if (saved_dir_pos
< 0) {
2068 return saved_dir_pos
;
2072 v9fs_readdir_lock(&fidp
->fs
.dir
);
2074 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
2078 v9fs_string_init(&name
);
2079 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2080 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
2081 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2083 /* Ran out of buffer. Set dir back to old position and return */
2084 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2085 v9fs_string_free(&name
);
2089 if (pdu
->s
->ctx
.export_flags
& V9FS_REMAP_INODES
) {
2091 * dirent_to_qid() implies expensive stat call for each entry,
2092 * we must do that here though since inode remapping requires
2093 * the device id, which in turn might be different for
2094 * different entries; we cannot make any assumption to avoid
2097 err
= dirent_to_qid(pdu
, fidp
, dent
, &qid
);
2099 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2100 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2101 v9fs_string_free(&name
);
2106 * Fill up just the path field of qid because the client uses
2107 * only that. To fill the entire qid structure we will have
2108 * to stat each dirent found, which is expensive. For the
2109 * latter reason we don't call dirent_to_qid() here. Only drawback
2110 * is that no multi-device export detection of stat_to_qid()
2111 * would be done and provided as error to the user here. But
2112 * user would get that error anyway when accessing those
2113 * files/dirs through other ways.
2115 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2116 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2117 /* Fill the other fields with dummy values */
2122 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2123 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2125 dent
->d_type
, &name
);
2127 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2130 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2131 v9fs_string_free(&name
);
2135 v9fs_string_free(&name
);
2136 saved_dir_pos
= dent
->d_off
;
2139 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2147 static void coroutine_fn
v9fs_readdir(void *opaque
)
2153 uint64_t initial_offset
;
2156 V9fsPDU
*pdu
= opaque
;
2158 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2159 &initial_offset
, &max_count
);
2163 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2165 fidp
= get_fid(pdu
, fid
);
2170 if (!fidp
->fs
.dir
.stream
) {
2174 if (initial_offset
== 0) {
2175 v9fs_co_rewinddir(pdu
, fidp
);
2177 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2179 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2184 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2188 retval
+= count
+ offset
;
2189 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2193 pdu_complete(pdu
, retval
);
2196 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2197 uint64_t off
, uint32_t count
,
2198 struct iovec
*sg
, int cnt
)
2202 uint64_t write_count
;
2206 if (fidp
->fs
.xattr
.len
< off
) {
2210 write_count
= fidp
->fs
.xattr
.len
- off
;
2211 if (write_count
> count
) {
2212 write_count
= count
;
2214 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2219 fidp
->fs
.xattr
.copied_len
+= write_count
;
2221 * Now copy the content from sg list
2223 for (i
= 0; i
< cnt
; i
++) {
2224 if (write_count
> sg
[i
].iov_len
) {
2225 to_copy
= sg
[i
].iov_len
;
2227 to_copy
= write_count
;
2229 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2230 /* updating vs->off since we are not using below */
2232 write_count
-= to_copy
;
2238 static void coroutine_fn
v9fs_write(void *opaque
)
2248 V9fsPDU
*pdu
= opaque
;
2249 V9fsState
*s
= pdu
->s
;
2250 QEMUIOVector qiov_full
;
2253 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2255 pdu_complete(pdu
, err
);
2259 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2260 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2262 fidp
= get_fid(pdu
, fid
);
2267 if (fidp
->fid_type
== P9_FID_FILE
) {
2268 if (fidp
->fs
.fd
== -1) {
2272 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2274 * setxattr operation
2276 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2277 qiov_full
.iov
, qiov_full
.niov
);
2283 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2285 qemu_iovec_reset(&qiov
);
2286 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2288 print_sg(qiov
.iov
, qiov
.niov
);
2290 /* Loop in case of EINTR */
2292 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2297 } while (len
== -EINTR
&& !pdu
->cancelled
);
2299 /* IO error return the error */
2303 } while (total
< count
&& len
> 0);
2306 err
= pdu_marshal(pdu
, offset
, "d", total
);
2311 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2313 qemu_iovec_destroy(&qiov
);
2317 qemu_iovec_destroy(&qiov_full
);
2318 pdu_complete(pdu
, err
);
2321 static void coroutine_fn
v9fs_create(void *opaque
)
2333 V9fsString extension
;
2335 V9fsPDU
*pdu
= opaque
;
2336 V9fsState
*s
= pdu
->s
;
2338 v9fs_path_init(&path
);
2339 v9fs_string_init(&name
);
2340 v9fs_string_init(&extension
);
2341 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2342 &perm
, &mode
, &extension
);
2346 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2348 if (name_is_illegal(name
.data
)) {
2353 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2358 fidp
= get_fid(pdu
, fid
);
2363 if (fidp
->fid_type
!= P9_FID_NONE
) {
2367 if (perm
& P9_STAT_MODE_DIR
) {
2368 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2369 fidp
->uid
, -1, &stbuf
);
2373 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2377 v9fs_path_write_lock(s
);
2378 v9fs_path_copy(&fidp
->path
, &path
);
2379 v9fs_path_unlock(s
);
2380 err
= v9fs_co_opendir(pdu
, fidp
);
2384 fidp
->fid_type
= P9_FID_DIR
;
2385 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2386 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2387 extension
.data
, -1 , &stbuf
);
2391 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2395 v9fs_path_write_lock(s
);
2396 v9fs_path_copy(&fidp
->path
, &path
);
2397 v9fs_path_unlock(s
);
2398 } else if (perm
& P9_STAT_MODE_LINK
) {
2399 int32_t ofid
= atoi(extension
.data
);
2400 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2401 if (ofidp
== NULL
) {
2405 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2406 put_fid(pdu
, ofidp
);
2410 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2412 fidp
->fid_type
= P9_FID_NONE
;
2415 v9fs_path_write_lock(s
);
2416 v9fs_path_copy(&fidp
->path
, &path
);
2417 v9fs_path_unlock(s
);
2418 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2420 fidp
->fid_type
= P9_FID_NONE
;
2423 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2425 uint32_t major
, minor
;
2428 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2445 nmode
|= perm
& 0777;
2446 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2447 makedev(major
, minor
), nmode
, &stbuf
);
2451 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2455 v9fs_path_write_lock(s
);
2456 v9fs_path_copy(&fidp
->path
, &path
);
2457 v9fs_path_unlock(s
);
2458 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2459 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2460 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2464 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2468 v9fs_path_write_lock(s
);
2469 v9fs_path_copy(&fidp
->path
, &path
);
2470 v9fs_path_unlock(s
);
2471 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2472 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2473 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2477 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2481 v9fs_path_write_lock(s
);
2482 v9fs_path_copy(&fidp
->path
, &path
);
2483 v9fs_path_unlock(s
);
2485 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2486 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2490 fidp
->fid_type
= P9_FID_FILE
;
2491 fidp
->open_flags
= omode_to_uflags(mode
);
2492 if (fidp
->open_flags
& O_EXCL
) {
2494 * We let the host file system do O_EXCL check
2495 * We should not reclaim such fd
2497 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2500 iounit
= get_iounit(pdu
, &fidp
->path
);
2501 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2505 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2510 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2511 qid
.type
, qid
.version
, qid
.path
, iounit
);
2515 pdu_complete(pdu
, err
);
2516 v9fs_string_free(&name
);
2517 v9fs_string_free(&extension
);
2518 v9fs_path_free(&path
);
2521 static void coroutine_fn
v9fs_symlink(void *opaque
)
2523 V9fsPDU
*pdu
= opaque
;
2526 V9fsFidState
*dfidp
;
2534 v9fs_string_init(&name
);
2535 v9fs_string_init(&symname
);
2536 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2540 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2542 if (name_is_illegal(name
.data
)) {
2547 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2552 dfidp
= get_fid(pdu
, dfid
);
2553 if (dfidp
== NULL
) {
2557 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2561 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2565 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2570 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2571 qid
.type
, qid
.version
, qid
.path
);
2573 put_fid(pdu
, dfidp
);
2575 pdu_complete(pdu
, err
);
2576 v9fs_string_free(&name
);
2577 v9fs_string_free(&symname
);
2580 static void coroutine_fn
v9fs_flush(void *opaque
)
2585 V9fsPDU
*cancel_pdu
= NULL
;
2586 V9fsPDU
*pdu
= opaque
;
2587 V9fsState
*s
= pdu
->s
;
2589 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2591 pdu_complete(pdu
, err
);
2594 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2596 if (pdu
->tag
== tag
) {
2597 warn_report("the guest sent a self-referencing 9P flush request");
2599 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2600 if (cancel_pdu
->tag
== tag
) {
2606 cancel_pdu
->cancelled
= 1;
2608 * Wait for pdu to complete.
2610 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2611 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2612 cancel_pdu
->cancelled
= 0;
2613 pdu_free(cancel_pdu
);
2616 pdu_complete(pdu
, 7);
2619 static void coroutine_fn
v9fs_link(void *opaque
)
2621 V9fsPDU
*pdu
= opaque
;
2622 int32_t dfid
, oldfid
;
2623 V9fsFidState
*dfidp
, *oldfidp
;
2628 v9fs_string_init(&name
);
2629 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2633 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2635 if (name_is_illegal(name
.data
)) {
2640 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2645 dfidp
= get_fid(pdu
, dfid
);
2646 if (dfidp
== NULL
) {
2651 oldfidp
= get_fid(pdu
, oldfid
);
2652 if (oldfidp
== NULL
) {
2656 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2660 put_fid(pdu
, oldfidp
);
2662 put_fid(pdu
, dfidp
);
2664 v9fs_string_free(&name
);
2665 pdu_complete(pdu
, err
);
2668 /* Only works with path name based fid */
2669 static void coroutine_fn
v9fs_remove(void *opaque
)
2675 V9fsPDU
*pdu
= opaque
;
2677 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2681 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2683 fidp
= get_fid(pdu
, fid
);
2688 /* if fs driver is not path based, return EOPNOTSUPP */
2689 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2694 * IF the file is unlinked, we cannot reopen
2695 * the file later. So don't reclaim fd
2697 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2701 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2706 /* For TREMOVE we need to clunk the fid even on failed remove */
2707 clunk_fid(pdu
->s
, fidp
->fid
);
2710 pdu_complete(pdu
, err
);
2713 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2717 int32_t dfid
, flags
, rflags
= 0;
2720 V9fsFidState
*dfidp
;
2721 V9fsPDU
*pdu
= opaque
;
2723 v9fs_string_init(&name
);
2724 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2729 if (name_is_illegal(name
.data
)) {
2734 if (!strcmp(".", name
.data
)) {
2739 if (!strcmp("..", name
.data
)) {
2744 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
2749 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
2750 rflags
|= AT_REMOVEDIR
;
2753 dfidp
= get_fid(pdu
, dfid
);
2754 if (dfidp
== NULL
) {
2759 * IF the file is unlinked, we cannot reopen
2760 * the file later. So don't reclaim fd
2762 v9fs_path_init(&path
);
2763 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2767 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2771 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
2776 put_fid(pdu
, dfidp
);
2777 v9fs_path_free(&path
);
2779 pdu_complete(pdu
, err
);
2780 v9fs_string_free(&name
);
2784 /* Only works with path name based fid */
2785 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2791 V9fsFidState
*tfidp
;
2792 V9fsState
*s
= pdu
->s
;
2793 V9fsFidState
*dirfidp
= NULL
;
2795 v9fs_path_init(&new_path
);
2796 if (newdirfid
!= -1) {
2797 dirfidp
= get_fid(pdu
, newdirfid
);
2798 if (dirfidp
== NULL
) {
2802 if (fidp
->fid_type
!= P9_FID_NONE
) {
2806 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2811 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
2814 v9fs_path_init(&dir_path
);
2815 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
2818 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
2819 v9fs_path_free(&dir_path
);
2824 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2829 * Fixup fid's pointing to the old name to
2830 * start pointing to the new name
2832 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2833 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2834 /* replace the name */
2835 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2840 put_fid(pdu
, dirfidp
);
2842 v9fs_path_free(&new_path
);
2847 /* Only works with path name based fid */
2848 static void coroutine_fn
v9fs_rename(void *opaque
)
2856 V9fsPDU
*pdu
= opaque
;
2857 V9fsState
*s
= pdu
->s
;
2859 v9fs_string_init(&name
);
2860 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2865 if (name_is_illegal(name
.data
)) {
2870 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2875 fidp
= get_fid(pdu
, fid
);
2880 if (fidp
->fid_type
!= P9_FID_NONE
) {
2884 /* if fs driver is not path based, return EOPNOTSUPP */
2885 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2889 v9fs_path_write_lock(s
);
2890 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2891 v9fs_path_unlock(s
);
2898 pdu_complete(pdu
, err
);
2899 v9fs_string_free(&name
);
2902 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2903 V9fsString
*old_name
,
2905 V9fsString
*new_name
)
2907 V9fsFidState
*tfidp
;
2908 V9fsPath oldpath
, newpath
;
2909 V9fsState
*s
= pdu
->s
;
2912 v9fs_path_init(&oldpath
);
2913 v9fs_path_init(&newpath
);
2914 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2918 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2924 * Fixup fid's pointing to the old name to
2925 * start pointing to the new name
2927 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2928 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2929 /* replace the name */
2930 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2934 v9fs_path_free(&oldpath
);
2935 v9fs_path_free(&newpath
);
2939 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2940 V9fsString
*old_name
,
2942 V9fsString
*new_name
)
2945 V9fsState
*s
= pdu
->s
;
2946 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2948 olddirfidp
= get_fid(pdu
, olddirfid
);
2949 if (olddirfidp
== NULL
) {
2953 if (newdirfid
!= -1) {
2954 newdirfidp
= get_fid(pdu
, newdirfid
);
2955 if (newdirfidp
== NULL
) {
2960 newdirfidp
= get_fid(pdu
, olddirfid
);
2963 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2964 &newdirfidp
->path
, new_name
);
2968 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2969 /* Only for path based fid we need to do the below fixup */
2970 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2971 &newdirfidp
->path
, new_name
);
2975 put_fid(pdu
, olddirfidp
);
2978 put_fid(pdu
, newdirfidp
);
2983 static void coroutine_fn
v9fs_renameat(void *opaque
)
2987 V9fsPDU
*pdu
= opaque
;
2988 V9fsState
*s
= pdu
->s
;
2989 int32_t olddirfid
, newdirfid
;
2990 V9fsString old_name
, new_name
;
2992 v9fs_string_init(&old_name
);
2993 v9fs_string_init(&new_name
);
2994 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2995 &old_name
, &newdirfid
, &new_name
);
3000 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
3005 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
3006 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
3011 v9fs_path_write_lock(s
);
3012 err
= v9fs_complete_renameat(pdu
, olddirfid
,
3013 &old_name
, newdirfid
, &new_name
);
3014 v9fs_path_unlock(s
);
3020 pdu_complete(pdu
, err
);
3021 v9fs_string_free(&old_name
);
3022 v9fs_string_free(&new_name
);
3025 static void coroutine_fn
v9fs_wstat(void *opaque
)
3034 V9fsPDU
*pdu
= opaque
;
3035 V9fsState
*s
= pdu
->s
;
3037 v9fs_stat_init(&v9stat
);
3038 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
3042 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
3043 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
3045 fidp
= get_fid(pdu
, fid
);
3050 /* do we need to sync the file? */
3051 if (donttouch_stat(&v9stat
)) {
3052 err
= v9fs_co_fsync(pdu
, fidp
, 0);
3055 if (v9stat
.mode
!= -1) {
3057 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
3061 v9_mode
= stat_to_v9mode(&stbuf
);
3062 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
3063 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
3064 /* Attempting to change the type */
3068 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
3069 v9mode_to_mode(v9stat
.mode
,
3070 &v9stat
.extension
));
3075 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
3076 struct timespec times
[2];
3077 if (v9stat
.atime
!= -1) {
3078 times
[0].tv_sec
= v9stat
.atime
;
3079 times
[0].tv_nsec
= 0;
3081 times
[0].tv_nsec
= UTIME_OMIT
;
3083 if (v9stat
.mtime
!= -1) {
3084 times
[1].tv_sec
= v9stat
.mtime
;
3085 times
[1].tv_nsec
= 0;
3087 times
[1].tv_nsec
= UTIME_OMIT
;
3089 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
3094 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
3095 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
3100 if (v9stat
.name
.size
!= 0) {
3101 v9fs_path_write_lock(s
);
3102 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
3103 v9fs_path_unlock(s
);
3108 if (v9stat
.length
!= -1) {
3109 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
3118 v9fs_stat_free(&v9stat
);
3119 pdu_complete(pdu
, err
);
3122 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
3134 int32_t bsize_factor
;
3137 * compute bsize factor based on host file system block size
3140 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
3141 if (!bsize_factor
) {
3144 f_type
= stbuf
->f_type
;
3145 f_bsize
= stbuf
->f_bsize
;
3146 f_bsize
*= bsize_factor
;
3148 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3149 * adjust(divide) the number of blocks, free blocks and available
3150 * blocks by bsize factor
3152 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
3153 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
3154 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
3155 f_files
= stbuf
->f_files
;
3156 f_ffree
= stbuf
->f_ffree
;
3157 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3158 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3159 f_namelen
= stbuf
->f_namelen
;
3161 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3162 f_type
, f_bsize
, f_blocks
, f_bfree
,
3163 f_bavail
, f_files
, f_ffree
,
3164 fsid_val
, f_namelen
);
3167 static void coroutine_fn
v9fs_statfs(void *opaque
)
3173 struct statfs stbuf
;
3174 V9fsPDU
*pdu
= opaque
;
3175 V9fsState
*s
= pdu
->s
;
3177 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3181 fidp
= get_fid(pdu
, fid
);
3186 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3190 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3198 pdu_complete(pdu
, retval
);
3201 static void coroutine_fn
v9fs_mknod(void *opaque
)
3214 V9fsPDU
*pdu
= opaque
;
3216 v9fs_string_init(&name
);
3217 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3218 &major
, &minor
, &gid
);
3222 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3224 if (name_is_illegal(name
.data
)) {
3229 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3234 fidp
= get_fid(pdu
, fid
);
3239 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3240 makedev(major
, minor
), mode
, &stbuf
);
3244 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3248 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3253 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3254 qid
.type
, qid
.version
, qid
.path
);
3258 pdu_complete(pdu
, err
);
3259 v9fs_string_free(&name
);
3263 * Implement posix byte range locking code
3264 * Server side handling of locking code is very simple, because 9p server in
3265 * QEMU can handle only one client. And most of the lock handling
3266 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3267 * do any thing in * qemu 9p server side lock code path.
3268 * So when a TLOCK request comes, always return success
3270 static void coroutine_fn
v9fs_lock(void *opaque
)
3276 int32_t fid
, err
= 0;
3277 V9fsPDU
*pdu
= opaque
;
3279 v9fs_string_init(&flock
.client_id
);
3280 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3281 &flock
.flags
, &flock
.start
, &flock
.length
,
3282 &flock
.proc_id
, &flock
.client_id
);
3286 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3287 flock
.type
, flock
.start
, flock
.length
);
3290 /* We support only block flag now (that too ignored currently) */
3291 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3295 fidp
= get_fid(pdu
, fid
);
3300 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3304 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3309 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3313 pdu_complete(pdu
, err
);
3314 v9fs_string_free(&flock
.client_id
);
3318 * When a TGETLOCK request comes, always return success because all lock
3319 * handling is done by client's VFS layer.
3321 static void coroutine_fn
v9fs_getlock(void *opaque
)
3327 int32_t fid
, err
= 0;
3328 V9fsPDU
*pdu
= opaque
;
3330 v9fs_string_init(&glock
.client_id
);
3331 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3332 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3337 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3338 glock
.type
, glock
.start
, glock
.length
);
3340 fidp
= get_fid(pdu
, fid
);
3345 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3349 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3350 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3351 glock
.start
, glock
.length
, glock
.proc_id
,
3357 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3358 glock
.length
, glock
.proc_id
);
3362 pdu_complete(pdu
, err
);
3363 v9fs_string_free(&glock
.client_id
);
3366 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3368 V9fsPDU
*pdu
= opaque
;
3379 v9fs_string_init(&name
);
3380 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3384 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3386 if (name_is_illegal(name
.data
)) {
3391 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3396 fidp
= get_fid(pdu
, fid
);
3401 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3405 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3409 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3414 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3415 qid
.type
, qid
.version
, qid
.path
, err
);
3419 pdu_complete(pdu
, err
);
3420 v9fs_string_free(&name
);
3423 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3429 int32_t fid
, newfid
;
3430 V9fsFidState
*file_fidp
;
3431 V9fsFidState
*xattr_fidp
= NULL
;
3432 V9fsPDU
*pdu
= opaque
;
3433 V9fsState
*s
= pdu
->s
;
3435 v9fs_string_init(&name
);
3436 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3440 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3442 file_fidp
= get_fid(pdu
, fid
);
3443 if (file_fidp
== NULL
) {
3447 xattr_fidp
= alloc_fid(s
, newfid
);
3448 if (xattr_fidp
== NULL
) {
3452 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3453 if (!v9fs_string_size(&name
)) {
3455 * listxattr request. Get the size first
3457 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3460 clunk_fid(s
, xattr_fidp
->fid
);
3464 * Read the xattr value
3466 xattr_fidp
->fs
.xattr
.len
= size
;
3467 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3468 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3469 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3471 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3472 xattr_fidp
->fs
.xattr
.value
,
3473 xattr_fidp
->fs
.xattr
.len
);
3475 clunk_fid(s
, xattr_fidp
->fid
);
3479 err
= pdu_marshal(pdu
, offset
, "q", size
);
3486 * specific xattr fid. We check for xattr
3487 * presence also collect the xattr size
3489 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3493 clunk_fid(s
, xattr_fidp
->fid
);
3497 * Read the xattr value
3499 xattr_fidp
->fs
.xattr
.len
= size
;
3500 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3501 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3502 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3504 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3505 &name
, xattr_fidp
->fs
.xattr
.value
,
3506 xattr_fidp
->fs
.xattr
.len
);
3508 clunk_fid(s
, xattr_fidp
->fid
);
3512 err
= pdu_marshal(pdu
, offset
, "q", size
);
3518 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3520 put_fid(pdu
, file_fidp
);
3522 put_fid(pdu
, xattr_fidp
);
3525 pdu_complete(pdu
, err
);
3526 v9fs_string_free(&name
);
3529 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3531 int flags
, rflags
= 0;
3537 V9fsFidState
*file_fidp
;
3538 V9fsFidState
*xattr_fidp
;
3539 V9fsPDU
*pdu
= opaque
;
3541 v9fs_string_init(&name
);
3542 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3546 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3548 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3553 if (flags
& P9_XATTR_CREATE
) {
3554 rflags
|= XATTR_CREATE
;
3557 if (flags
& P9_XATTR_REPLACE
) {
3558 rflags
|= XATTR_REPLACE
;
3561 if (size
> XATTR_SIZE_MAX
) {
3566 file_fidp
= get_fid(pdu
, fid
);
3567 if (file_fidp
== NULL
) {
3571 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3576 /* Make the file fid point to xattr */
3577 xattr_fidp
= file_fidp
;
3578 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3579 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3580 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3581 xattr_fidp
->fs
.xattr
.len
= size
;
3582 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3583 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3584 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3585 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3588 put_fid(pdu
, file_fidp
);
3590 pdu_complete(pdu
, err
);
3591 v9fs_string_free(&name
);
3594 static void coroutine_fn
v9fs_readlink(void *opaque
)
3596 V9fsPDU
*pdu
= opaque
;
3603 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3607 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3608 fidp
= get_fid(pdu
, fid
);
3614 v9fs_string_init(&target
);
3615 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3619 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3621 v9fs_string_free(&target
);
3625 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3626 v9fs_string_free(&target
);
3630 pdu_complete(pdu
, err
);
3633 static CoroutineEntry
*pdu_co_handlers
[] = {
3634 [P9_TREADDIR
] = v9fs_readdir
,
3635 [P9_TSTATFS
] = v9fs_statfs
,
3636 [P9_TGETATTR
] = v9fs_getattr
,
3637 [P9_TSETATTR
] = v9fs_setattr
,
3638 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3639 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3640 [P9_TMKNOD
] = v9fs_mknod
,
3641 [P9_TRENAME
] = v9fs_rename
,
3642 [P9_TLOCK
] = v9fs_lock
,
3643 [P9_TGETLOCK
] = v9fs_getlock
,
3644 [P9_TRENAMEAT
] = v9fs_renameat
,
3645 [P9_TREADLINK
] = v9fs_readlink
,
3646 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3647 [P9_TMKDIR
] = v9fs_mkdir
,
3648 [P9_TVERSION
] = v9fs_version
,
3649 [P9_TLOPEN
] = v9fs_open
,
3650 [P9_TATTACH
] = v9fs_attach
,
3651 [P9_TSTAT
] = v9fs_stat
,
3652 [P9_TWALK
] = v9fs_walk
,
3653 [P9_TCLUNK
] = v9fs_clunk
,
3654 [P9_TFSYNC
] = v9fs_fsync
,
3655 [P9_TOPEN
] = v9fs_open
,
3656 [P9_TREAD
] = v9fs_read
,
3658 [P9_TAUTH
] = v9fs_auth
,
3660 [P9_TFLUSH
] = v9fs_flush
,
3661 [P9_TLINK
] = v9fs_link
,
3662 [P9_TSYMLINK
] = v9fs_symlink
,
3663 [P9_TCREATE
] = v9fs_create
,
3664 [P9_TLCREATE
] = v9fs_lcreate
,
3665 [P9_TWRITE
] = v9fs_write
,
3666 [P9_TWSTAT
] = v9fs_wstat
,
3667 [P9_TREMOVE
] = v9fs_remove
,
3670 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3672 V9fsPDU
*pdu
= opaque
;
3673 pdu_complete(pdu
, -EOPNOTSUPP
);
3676 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3678 V9fsPDU
*pdu
= opaque
;
3679 pdu_complete(pdu
, -EROFS
);
3682 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3709 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
3712 CoroutineEntry
*handler
;
3713 V9fsState
*s
= pdu
->s
;
3715 pdu
->size
= le32_to_cpu(hdr
->size_le
);
3717 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
3719 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3720 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3721 handler
= v9fs_op_not_supp
;
3722 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3723 handler
= v9fs_fs_ro
;
3725 handler
= pdu_co_handlers
[pdu
->id
];
3728 qemu_co_queue_init(&pdu
->complete
);
3729 co
= qemu_coroutine_create(handler
, pdu
);
3730 qemu_coroutine_enter(co
);
3733 /* Returns 0 on success, 1 on failure. */
3734 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
3743 assert(!s
->transport
);
3746 /* initialize pdu allocator */
3747 QLIST_INIT(&s
->free_list
);
3748 QLIST_INIT(&s
->active_list
);
3749 for (i
= 0; i
< MAX_REQ
; i
++) {
3750 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3755 v9fs_path_init(&path
);
3757 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3760 /* We don't have a fsdev identified by fsdev_id */
3761 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3763 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3767 if (!s
->fsconf
.tag
) {
3768 /* we haven't specified a mount_tag */
3769 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3770 s
->fsconf
.fsdev_id
);
3774 s
->ctx
.export_flags
= fse
->export_flags
;
3775 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3776 s
->ctx
.exops
.get_st_gen
= NULL
;
3777 len
= strlen(s
->fsconf
.tag
);
3778 if (len
> MAX_TAG_LEN
- 1) {
3779 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3780 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3784 s
->tag
= g_strdup(s
->fsconf
.tag
);
3789 s
->ctx
.fmode
= fse
->fmode
;
3790 s
->ctx
.dmode
= fse
->dmode
;
3793 qemu_co_rwlock_init(&s
->rename_lock
);
3795 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
3796 error_prepend(errp
, "cannot initialize fsdev '%s': ",
3797 s
->fsconf
.fsdev_id
);
3802 * Check details of export path, We need to use fs driver
3803 * call back to do that. Since we are in the init path, we don't
3804 * use co-routines here.
3806 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3808 "error in converting name to path %s", strerror(errno
));
3811 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3812 error_setg(errp
, "share path %s does not exist", fse
->path
);
3814 } else if (!S_ISDIR(stat
.st_mode
)) {
3815 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3819 s
->dev_id
= stat
.st_dev
;
3821 qpp_table_init(&s
->qpp_table
);
3822 s
->qp_prefix_next
= 1; /* reserve 0 to detect overflow */
3824 s
->ctx
.fst
= &fse
->fst
;
3825 fsdev_throttle_init(s
->ctx
.fst
);
3830 v9fs_device_unrealize_common(s
, NULL
);
3832 v9fs_path_free(&path
);
3836 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3838 if (s
->ops
&& s
->ops
->cleanup
) {
3839 s
->ops
->cleanup(&s
->ctx
);
3842 fsdev_throttle_cleanup(s
->ctx
.fst
);
3845 qpp_table_destroy(&s
->qpp_table
);
3846 g_free(s
->ctx
.fs_root
);
3849 typedef struct VirtfsCoResetData
{
3852 } VirtfsCoResetData
;
3854 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3856 VirtfsCoResetData
*data
= opaque
;
3858 virtfs_reset(&data
->pdu
);
3862 void v9fs_reset(V9fsState
*s
)
3864 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3867 while (!QLIST_EMPTY(&s
->active_list
)) {
3868 aio_poll(qemu_get_aio_context(), true);
3871 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3872 qemu_coroutine_enter(co
);
3874 while (!data
.done
) {
3875 aio_poll(qemu_get_aio_context(), true);
3879 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3882 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3883 error_report("Failed to get the resource limit");
3886 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3887 open_fd_rc
= rlim
.rlim_cur
/2;