9p: fix QEMU crash when renaming files
[qemu.git] / hw / 9pfs / 9p.c
1 /*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
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"
19 #include "qemu/iov.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
23 #include "9p-xattr.h"
24 #include "coth.h"
25 #include "trace.h"
26 #include "migration/blocker.h"
27 #include "sysemu/qtest.h"
28
29 int open_fd_hw;
30 int total_open_fd;
31 static int open_fd_rc;
32
33 enum {
34 Oread = 0x00,
35 Owrite = 0x01,
36 Ordwr = 0x02,
37 Oexec = 0x03,
38 Oexcl = 0x04,
39 Otrunc = 0x10,
40 Orexec = 0x20,
41 Orclose = 0x40,
42 Oappend = 0x80,
43 };
44
45 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
46 {
47 ssize_t ret;
48 va_list ap;
49
50 va_start(ap, fmt);
51 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
52 va_end(ap);
53
54 return ret;
55 }
56
57 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
58 {
59 ssize_t ret;
60 va_list ap;
61
62 va_start(ap, fmt);
63 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
64 va_end(ap);
65
66 return ret;
67 }
68
69 static int omode_to_uflags(int8_t mode)
70 {
71 int ret = 0;
72
73 switch (mode & 3) {
74 case Oread:
75 ret = O_RDONLY;
76 break;
77 case Ordwr:
78 ret = O_RDWR;
79 break;
80 case Owrite:
81 ret = O_WRONLY;
82 break;
83 case Oexec:
84 ret = O_RDONLY;
85 break;
86 }
87
88 if (mode & Otrunc) {
89 ret |= O_TRUNC;
90 }
91
92 if (mode & Oappend) {
93 ret |= O_APPEND;
94 }
95
96 if (mode & Oexcl) {
97 ret |= O_EXCL;
98 }
99
100 return ret;
101 }
102
103 typedef struct DotlOpenflagMap {
104 int dotl_flag;
105 int open_flag;
106 } DotlOpenflagMap;
107
108 static int dotl_to_open_flags(int flags)
109 {
110 int i;
111 /*
112 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
113 * and P9_DOTL_NOACCESS
114 */
115 int oflags = flags & O_ACCMODE;
116
117 DotlOpenflagMap dotl_oflag_map[] = {
118 { P9_DOTL_CREATE, O_CREAT },
119 { P9_DOTL_EXCL, O_EXCL },
120 { P9_DOTL_NOCTTY , O_NOCTTY },
121 { P9_DOTL_TRUNC, O_TRUNC },
122 { P9_DOTL_APPEND, O_APPEND },
123 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
124 { P9_DOTL_DSYNC, O_DSYNC },
125 { P9_DOTL_FASYNC, FASYNC },
126 { P9_DOTL_DIRECT, O_DIRECT },
127 { P9_DOTL_LARGEFILE, O_LARGEFILE },
128 { P9_DOTL_DIRECTORY, O_DIRECTORY },
129 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
130 { P9_DOTL_NOATIME, O_NOATIME },
131 { P9_DOTL_SYNC, O_SYNC },
132 };
133
134 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
135 if (flags & dotl_oflag_map[i].dotl_flag) {
136 oflags |= dotl_oflag_map[i].open_flag;
137 }
138 }
139
140 return oflags;
141 }
142
143 void cred_init(FsCred *credp)
144 {
145 credp->fc_uid = -1;
146 credp->fc_gid = -1;
147 credp->fc_mode = -1;
148 credp->fc_rdev = -1;
149 }
150
151 static int get_dotl_openflags(V9fsState *s, int oflags)
152 {
153 int flags;
154 /*
155 * Filter the client open flags
156 */
157 flags = dotl_to_open_flags(oflags);
158 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
159 /*
160 * Ignore direct disk access hint until the server supports it.
161 */
162 flags &= ~O_DIRECT;
163 return flags;
164 }
165
166 void v9fs_path_init(V9fsPath *path)
167 {
168 path->data = NULL;
169 path->size = 0;
170 }
171
172 void v9fs_path_free(V9fsPath *path)
173 {
174 g_free(path->data);
175 path->data = NULL;
176 path->size = 0;
177 }
178
179
180 void GCC_FMT_ATTR(2, 3)
181 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
182 {
183 va_list ap;
184
185 v9fs_path_free(path);
186
187 va_start(ap, fmt);
188 /* Bump the size for including terminating NULL */
189 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
190 va_end(ap);
191 }
192
193 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
194 {
195 v9fs_path_free(dst);
196 dst->size = src->size;
197 dst->data = g_memdup(src->data, src->size);
198 }
199
200 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
201 const char *name, V9fsPath *path)
202 {
203 int err;
204 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
205 if (err < 0) {
206 err = -errno;
207 }
208 return err;
209 }
210
211 /*
212 * Return TRUE if s1 is an ancestor of s2.
213 *
214 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
215 * As a special case, We treat s1 as ancestor of s2 if they are same!
216 */
217 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
218 {
219 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
220 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
221 return 1;
222 }
223 }
224 return 0;
225 }
226
227 static size_t v9fs_string_size(V9fsString *str)
228 {
229 return str->size;
230 }
231
232 /*
233 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
234 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
235 {
236 int err = 1;
237 if (f->fid_type == P9_FID_FILE) {
238 if (f->fs.fd == -1) {
239 do {
240 err = v9fs_co_open(pdu, f, f->open_flags);
241 } while (err == -EINTR && !pdu->cancelled);
242 }
243 } else if (f->fid_type == P9_FID_DIR) {
244 if (f->fs.dir.stream == NULL) {
245 do {
246 err = v9fs_co_opendir(pdu, f);
247 } while (err == -EINTR && !pdu->cancelled);
248 }
249 }
250 return err;
251 }
252
253 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
254 {
255 int err;
256 V9fsFidState *f;
257 V9fsState *s = pdu->s;
258
259 for (f = s->fid_list; f; f = f->next) {
260 BUG_ON(f->clunked);
261 if (f->fid == fid) {
262 /*
263 * Update the fid ref upfront so that
264 * we don't get reclaimed when we yield
265 * in open later.
266 */
267 f->ref++;
268 /*
269 * check whether we need to reopen the
270 * file. We might have closed the fd
271 * while trying to free up some file
272 * descriptors.
273 */
274 err = v9fs_reopen_fid(pdu, f);
275 if (err < 0) {
276 f->ref--;
277 return NULL;
278 }
279 /*
280 * Mark the fid as referenced so that the LRU
281 * reclaim won't close the file descriptor
282 */
283 f->flags |= FID_REFERENCED;
284 return f;
285 }
286 }
287 return NULL;
288 }
289
290 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
291 {
292 V9fsFidState *f;
293
294 for (f = s->fid_list; f; f = f->next) {
295 /* If fid is already there return NULL */
296 BUG_ON(f->clunked);
297 if (f->fid == fid) {
298 return NULL;
299 }
300 }
301 f = g_malloc0(sizeof(V9fsFidState));
302 f->fid = fid;
303 f->fid_type = P9_FID_NONE;
304 f->ref = 1;
305 /*
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
308 */
309 f->flags |= FID_REFERENCED;
310 f->next = s->fid_list;
311 s->fid_list = f;
312
313 v9fs_readdir_init(&f->fs.dir);
314 v9fs_readdir_init(&f->fs_reclaim.dir);
315
316 return f;
317 }
318
319 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
320 {
321 int retval = 0;
322
323 if (fidp->fs.xattr.xattrwalk_fid) {
324 /* getxattr/listxattr fid */
325 goto free_value;
326 }
327 /*
328 * if this is fid for setxattr. clunk should
329 * result in setxattr localcall
330 */
331 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
332 /* clunk after partial write */
333 retval = -EINVAL;
334 goto free_out;
335 }
336 if (fidp->fs.xattr.len) {
337 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
338 fidp->fs.xattr.value,
339 fidp->fs.xattr.len,
340 fidp->fs.xattr.flags);
341 } else {
342 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
343 }
344 free_out:
345 v9fs_string_free(&fidp->fs.xattr.name);
346 free_value:
347 g_free(fidp->fs.xattr.value);
348 return retval;
349 }
350
351 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
352 {
353 int retval = 0;
354
355 if (fidp->fid_type == P9_FID_FILE) {
356 /* If we reclaimed the fd no need to close */
357 if (fidp->fs.fd != -1) {
358 retval = v9fs_co_close(pdu, &fidp->fs);
359 }
360 } else if (fidp->fid_type == P9_FID_DIR) {
361 if (fidp->fs.dir.stream != NULL) {
362 retval = v9fs_co_closedir(pdu, &fidp->fs);
363 }
364 } else if (fidp->fid_type == P9_FID_XATTR) {
365 retval = v9fs_xattr_fid_clunk(pdu, fidp);
366 }
367 v9fs_path_free(&fidp->path);
368 g_free(fidp);
369 return retval;
370 }
371
372 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
373 {
374 BUG_ON(!fidp->ref);
375 fidp->ref--;
376 /*
377 * Don't free the fid if it is in reclaim list
378 */
379 if (!fidp->ref && fidp->clunked) {
380 if (fidp->fid == pdu->s->root_fid) {
381 /*
382 * if the clunked fid is root fid then we
383 * have unmounted the fs on the client side.
384 * delete the migration blocker. Ideally, this
385 * should be hooked to transport close notification
386 */
387 if (pdu->s->migration_blocker) {
388 migrate_del_blocker(pdu->s->migration_blocker);
389 error_free(pdu->s->migration_blocker);
390 pdu->s->migration_blocker = NULL;
391 }
392 }
393 return free_fid(pdu, fidp);
394 }
395 return 0;
396 }
397
398 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
399 {
400 V9fsFidState **fidpp, *fidp;
401
402 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
403 if ((*fidpp)->fid == fid) {
404 break;
405 }
406 }
407 if (*fidpp == NULL) {
408 return NULL;
409 }
410 fidp = *fidpp;
411 *fidpp = fidp->next;
412 fidp->clunked = 1;
413 return fidp;
414 }
415
416 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
417 {
418 int reclaim_count = 0;
419 V9fsState *s = pdu->s;
420 V9fsFidState *f, *reclaim_list = NULL;
421
422 for (f = s->fid_list; f; f = f->next) {
423 /*
424 * Unlink fids cannot be reclaimed. Check
425 * for them and skip them. Also skip fids
426 * currently being operated on.
427 */
428 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
429 continue;
430 }
431 /*
432 * if it is a recently referenced fid
433 * we leave the fid untouched and clear the
434 * reference bit. We come back to it later
435 * in the next iteration. (a simple LRU without
436 * moving list elements around)
437 */
438 if (f->flags & FID_REFERENCED) {
439 f->flags &= ~FID_REFERENCED;
440 continue;
441 }
442 /*
443 * Add fids to reclaim list.
444 */
445 if (f->fid_type == P9_FID_FILE) {
446 if (f->fs.fd != -1) {
447 /*
448 * Up the reference count so that
449 * a clunk request won't free this fid
450 */
451 f->ref++;
452 f->rclm_lst = reclaim_list;
453 reclaim_list = f;
454 f->fs_reclaim.fd = f->fs.fd;
455 f->fs.fd = -1;
456 reclaim_count++;
457 }
458 } else if (f->fid_type == P9_FID_DIR) {
459 if (f->fs.dir.stream != NULL) {
460 /*
461 * Up the reference count so that
462 * a clunk request won't free this fid
463 */
464 f->ref++;
465 f->rclm_lst = reclaim_list;
466 reclaim_list = f;
467 f->fs_reclaim.dir.stream = f->fs.dir.stream;
468 f->fs.dir.stream = NULL;
469 reclaim_count++;
470 }
471 }
472 if (reclaim_count >= open_fd_rc) {
473 break;
474 }
475 }
476 /*
477 * Now close the fid in reclaim list. Free them if they
478 * are already clunked.
479 */
480 while (reclaim_list) {
481 f = reclaim_list;
482 reclaim_list = f->rclm_lst;
483 if (f->fid_type == P9_FID_FILE) {
484 v9fs_co_close(pdu, &f->fs_reclaim);
485 } else if (f->fid_type == P9_FID_DIR) {
486 v9fs_co_closedir(pdu, &f->fs_reclaim);
487 }
488 f->rclm_lst = NULL;
489 /*
490 * Now drop the fid reference, free it
491 * if clunked.
492 */
493 put_fid(pdu, f);
494 }
495 }
496
497 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
498 {
499 int err;
500 V9fsState *s = pdu->s;
501 V9fsFidState *fidp, head_fid;
502
503 head_fid.next = s->fid_list;
504 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
505 if (fidp->path.size != path->size) {
506 continue;
507 }
508 if (!memcmp(fidp->path.data, path->data, path->size)) {
509 /* Mark the fid non reclaimable. */
510 fidp->flags |= FID_NON_RECLAIMABLE;
511
512 /* reopen the file/dir if already closed */
513 err = v9fs_reopen_fid(pdu, fidp);
514 if (err < 0) {
515 return err;
516 }
517 /*
518 * Go back to head of fid list because
519 * the list could have got updated when
520 * switched to the worker thread
521 */
522 if (err == 0) {
523 fidp = &head_fid;
524 }
525 }
526 }
527 return 0;
528 }
529
530 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
531 {
532 V9fsState *s = pdu->s;
533 V9fsFidState *fidp;
534
535 /* Free all fids */
536 while (s->fid_list) {
537 /* Get fid */
538 fidp = s->fid_list;
539 fidp->ref++;
540
541 /* Clunk fid */
542 s->fid_list = fidp->next;
543 fidp->clunked = 1;
544
545 put_fid(pdu, fidp);
546 }
547 }
548
549 #define P9_QID_TYPE_DIR 0x80
550 #define P9_QID_TYPE_SYMLINK 0x02
551
552 #define P9_STAT_MODE_DIR 0x80000000
553 #define P9_STAT_MODE_APPEND 0x40000000
554 #define P9_STAT_MODE_EXCL 0x20000000
555 #define P9_STAT_MODE_MOUNT 0x10000000
556 #define P9_STAT_MODE_AUTH 0x08000000
557 #define P9_STAT_MODE_TMP 0x04000000
558 #define P9_STAT_MODE_SYMLINK 0x02000000
559 #define P9_STAT_MODE_LINK 0x01000000
560 #define P9_STAT_MODE_DEVICE 0x00800000
561 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
562 #define P9_STAT_MODE_SOCKET 0x00100000
563 #define P9_STAT_MODE_SETUID 0x00080000
564 #define P9_STAT_MODE_SETGID 0x00040000
565 #define P9_STAT_MODE_SETVTX 0x00010000
566
567 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
568 P9_STAT_MODE_SYMLINK | \
569 P9_STAT_MODE_LINK | \
570 P9_STAT_MODE_DEVICE | \
571 P9_STAT_MODE_NAMED_PIPE | \
572 P9_STAT_MODE_SOCKET)
573
574 /* This is the algorithm from ufs in spfs */
575 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
576 {
577 size_t size;
578
579 memset(&qidp->path, 0, sizeof(qidp->path));
580 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
581 memcpy(&qidp->path, &stbuf->st_ino, size);
582 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
583 qidp->type = 0;
584 if (S_ISDIR(stbuf->st_mode)) {
585 qidp->type |= P9_QID_TYPE_DIR;
586 }
587 if (S_ISLNK(stbuf->st_mode)) {
588 qidp->type |= P9_QID_TYPE_SYMLINK;
589 }
590 }
591
592 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
593 V9fsQID *qidp)
594 {
595 struct stat stbuf;
596 int err;
597
598 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
599 if (err < 0) {
600 return err;
601 }
602 stat_to_qid(&stbuf, qidp);
603 return 0;
604 }
605
606 V9fsPDU *pdu_alloc(V9fsState *s)
607 {
608 V9fsPDU *pdu = NULL;
609
610 if (!QLIST_EMPTY(&s->free_list)) {
611 pdu = QLIST_FIRST(&s->free_list);
612 QLIST_REMOVE(pdu, next);
613 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
614 }
615 return pdu;
616 }
617
618 void pdu_free(V9fsPDU *pdu)
619 {
620 V9fsState *s = pdu->s;
621
622 g_assert(!pdu->cancelled);
623 QLIST_REMOVE(pdu, next);
624 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
625 }
626
627 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
628 {
629 int8_t id = pdu->id + 1; /* Response */
630 V9fsState *s = pdu->s;
631 int ret;
632
633 /*
634 * The 9p spec requires that successfully cancelled pdus receive no reply.
635 * Sending a reply would confuse clients because they would
636 * assume that any EINTR is the actual result of the operation,
637 * rather than a consequence of the cancellation. However, if
638 * the operation completed (succesfully or with an error other
639 * than caused be cancellation), we do send out that reply, both
640 * for efficiency and to avoid confusing the rest of the state machine
641 * that assumes passing a non-error here will mean a successful
642 * transmission of the reply.
643 */
644 bool discard = pdu->cancelled && len == -EINTR;
645 if (discard) {
646 trace_v9fs_rcancel(pdu->tag, pdu->id);
647 pdu->size = 0;
648 goto out_notify;
649 }
650
651 if (len < 0) {
652 int err = -len;
653 len = 7;
654
655 if (s->proto_version != V9FS_PROTO_2000L) {
656 V9fsString str;
657
658 str.data = strerror(err);
659 str.size = strlen(str.data);
660
661 ret = pdu_marshal(pdu, len, "s", &str);
662 if (ret < 0) {
663 goto out_notify;
664 }
665 len += ret;
666 id = P9_RERROR;
667 }
668
669 ret = pdu_marshal(pdu, len, "d", err);
670 if (ret < 0) {
671 goto out_notify;
672 }
673 len += ret;
674
675 if (s->proto_version == V9FS_PROTO_2000L) {
676 id = P9_RLERROR;
677 }
678 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
679 }
680
681 /* fill out the header */
682 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
683 goto out_notify;
684 }
685
686 /* keep these in sync */
687 pdu->size = len;
688 pdu->id = id;
689
690 out_notify:
691 pdu->s->transport->push_and_notify(pdu);
692
693 /* Now wakeup anybody waiting in flush for this request */
694 if (!qemu_co_queue_next(&pdu->complete)) {
695 pdu_free(pdu);
696 }
697 }
698
699 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
700 {
701 mode_t ret;
702
703 ret = mode & 0777;
704 if (mode & P9_STAT_MODE_DIR) {
705 ret |= S_IFDIR;
706 }
707
708 if (mode & P9_STAT_MODE_SYMLINK) {
709 ret |= S_IFLNK;
710 }
711 if (mode & P9_STAT_MODE_SOCKET) {
712 ret |= S_IFSOCK;
713 }
714 if (mode & P9_STAT_MODE_NAMED_PIPE) {
715 ret |= S_IFIFO;
716 }
717 if (mode & P9_STAT_MODE_DEVICE) {
718 if (extension->size && extension->data[0] == 'c') {
719 ret |= S_IFCHR;
720 } else {
721 ret |= S_IFBLK;
722 }
723 }
724
725 if (!(ret&~0777)) {
726 ret |= S_IFREG;
727 }
728
729 if (mode & P9_STAT_MODE_SETUID) {
730 ret |= S_ISUID;
731 }
732 if (mode & P9_STAT_MODE_SETGID) {
733 ret |= S_ISGID;
734 }
735 if (mode & P9_STAT_MODE_SETVTX) {
736 ret |= S_ISVTX;
737 }
738
739 return ret;
740 }
741
742 static int donttouch_stat(V9fsStat *stat)
743 {
744 if (stat->type == -1 &&
745 stat->dev == -1 &&
746 stat->qid.type == -1 &&
747 stat->qid.version == -1 &&
748 stat->qid.path == -1 &&
749 stat->mode == -1 &&
750 stat->atime == -1 &&
751 stat->mtime == -1 &&
752 stat->length == -1 &&
753 !stat->name.size &&
754 !stat->uid.size &&
755 !stat->gid.size &&
756 !stat->muid.size &&
757 stat->n_uid == -1 &&
758 stat->n_gid == -1 &&
759 stat->n_muid == -1) {
760 return 1;
761 }
762
763 return 0;
764 }
765
766 static void v9fs_stat_init(V9fsStat *stat)
767 {
768 v9fs_string_init(&stat->name);
769 v9fs_string_init(&stat->uid);
770 v9fs_string_init(&stat->gid);
771 v9fs_string_init(&stat->muid);
772 v9fs_string_init(&stat->extension);
773 }
774
775 static void v9fs_stat_free(V9fsStat *stat)
776 {
777 v9fs_string_free(&stat->name);
778 v9fs_string_free(&stat->uid);
779 v9fs_string_free(&stat->gid);
780 v9fs_string_free(&stat->muid);
781 v9fs_string_free(&stat->extension);
782 }
783
784 static uint32_t stat_to_v9mode(const struct stat *stbuf)
785 {
786 uint32_t mode;
787
788 mode = stbuf->st_mode & 0777;
789 if (S_ISDIR(stbuf->st_mode)) {
790 mode |= P9_STAT_MODE_DIR;
791 }
792
793 if (S_ISLNK(stbuf->st_mode)) {
794 mode |= P9_STAT_MODE_SYMLINK;
795 }
796
797 if (S_ISSOCK(stbuf->st_mode)) {
798 mode |= P9_STAT_MODE_SOCKET;
799 }
800
801 if (S_ISFIFO(stbuf->st_mode)) {
802 mode |= P9_STAT_MODE_NAMED_PIPE;
803 }
804
805 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
806 mode |= P9_STAT_MODE_DEVICE;
807 }
808
809 if (stbuf->st_mode & S_ISUID) {
810 mode |= P9_STAT_MODE_SETUID;
811 }
812
813 if (stbuf->st_mode & S_ISGID) {
814 mode |= P9_STAT_MODE_SETGID;
815 }
816
817 if (stbuf->st_mode & S_ISVTX) {
818 mode |= P9_STAT_MODE_SETVTX;
819 }
820
821 return mode;
822 }
823
824 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
825 const char *basename,
826 const struct stat *stbuf,
827 V9fsStat *v9stat)
828 {
829 int err;
830
831 memset(v9stat, 0, sizeof(*v9stat));
832
833 stat_to_qid(stbuf, &v9stat->qid);
834 v9stat->mode = stat_to_v9mode(stbuf);
835 v9stat->atime = stbuf->st_atime;
836 v9stat->mtime = stbuf->st_mtime;
837 v9stat->length = stbuf->st_size;
838
839 v9fs_string_free(&v9stat->uid);
840 v9fs_string_free(&v9stat->gid);
841 v9fs_string_free(&v9stat->muid);
842
843 v9stat->n_uid = stbuf->st_uid;
844 v9stat->n_gid = stbuf->st_gid;
845 v9stat->n_muid = 0;
846
847 v9fs_string_free(&v9stat->extension);
848
849 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
850 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
851 if (err < 0) {
852 return err;
853 }
854 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
855 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
856 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
857 major(stbuf->st_rdev), minor(stbuf->st_rdev));
858 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
859 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
860 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
861 }
862
863 v9fs_string_sprintf(&v9stat->name, "%s", basename);
864
865 v9stat->size = 61 +
866 v9fs_string_size(&v9stat->name) +
867 v9fs_string_size(&v9stat->uid) +
868 v9fs_string_size(&v9stat->gid) +
869 v9fs_string_size(&v9stat->muid) +
870 v9fs_string_size(&v9stat->extension);
871 return 0;
872 }
873
874 #define P9_STATS_MODE 0x00000001ULL
875 #define P9_STATS_NLINK 0x00000002ULL
876 #define P9_STATS_UID 0x00000004ULL
877 #define P9_STATS_GID 0x00000008ULL
878 #define P9_STATS_RDEV 0x00000010ULL
879 #define P9_STATS_ATIME 0x00000020ULL
880 #define P9_STATS_MTIME 0x00000040ULL
881 #define P9_STATS_CTIME 0x00000080ULL
882 #define P9_STATS_INO 0x00000100ULL
883 #define P9_STATS_SIZE 0x00000200ULL
884 #define P9_STATS_BLOCKS 0x00000400ULL
885
886 #define P9_STATS_BTIME 0x00000800ULL
887 #define P9_STATS_GEN 0x00001000ULL
888 #define P9_STATS_DATA_VERSION 0x00002000ULL
889
890 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
891 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
892
893
894 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
895 V9fsStatDotl *v9lstat)
896 {
897 memset(v9lstat, 0, sizeof(*v9lstat));
898
899 v9lstat->st_mode = stbuf->st_mode;
900 v9lstat->st_nlink = stbuf->st_nlink;
901 v9lstat->st_uid = stbuf->st_uid;
902 v9lstat->st_gid = stbuf->st_gid;
903 v9lstat->st_rdev = stbuf->st_rdev;
904 v9lstat->st_size = stbuf->st_size;
905 v9lstat->st_blksize = stbuf->st_blksize;
906 v9lstat->st_blocks = stbuf->st_blocks;
907 v9lstat->st_atime_sec = stbuf->st_atime;
908 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
909 v9lstat->st_mtime_sec = stbuf->st_mtime;
910 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
911 v9lstat->st_ctime_sec = stbuf->st_ctime;
912 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
913 /* Currently we only support BASIC fields in stat */
914 v9lstat->st_result_mask = P9_STATS_BASIC;
915
916 stat_to_qid(stbuf, &v9lstat->qid);
917 }
918
919 static void print_sg(struct iovec *sg, int cnt)
920 {
921 int i;
922
923 printf("sg[%d]: {", cnt);
924 for (i = 0; i < cnt; i++) {
925 if (i) {
926 printf(", ");
927 }
928 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
929 }
930 printf("}\n");
931 }
932
933 /* Will call this only for path name based fid */
934 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
935 {
936 V9fsPath str;
937 v9fs_path_init(&str);
938 v9fs_path_copy(&str, dst);
939 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
940 v9fs_path_free(&str);
941 }
942
943 static inline bool is_ro_export(FsContext *ctx)
944 {
945 return ctx->export_flags & V9FS_RDONLY;
946 }
947
948 static void coroutine_fn v9fs_version(void *opaque)
949 {
950 ssize_t err;
951 V9fsPDU *pdu = opaque;
952 V9fsState *s = pdu->s;
953 V9fsString version;
954 size_t offset = 7;
955
956 v9fs_string_init(&version);
957 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
958 if (err < 0) {
959 goto out;
960 }
961 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
962
963 virtfs_reset(pdu);
964
965 if (!strcmp(version.data, "9P2000.u")) {
966 s->proto_version = V9FS_PROTO_2000U;
967 } else if (!strcmp(version.data, "9P2000.L")) {
968 s->proto_version = V9FS_PROTO_2000L;
969 } else {
970 v9fs_string_sprintf(&version, "unknown");
971 }
972
973 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
974 if (err < 0) {
975 goto out;
976 }
977 err += offset;
978 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
979 out:
980 pdu_complete(pdu, err);
981 v9fs_string_free(&version);
982 }
983
984 static void coroutine_fn v9fs_attach(void *opaque)
985 {
986 V9fsPDU *pdu = opaque;
987 V9fsState *s = pdu->s;
988 int32_t fid, afid, n_uname;
989 V9fsString uname, aname;
990 V9fsFidState *fidp;
991 size_t offset = 7;
992 V9fsQID qid;
993 ssize_t err;
994 Error *local_err = NULL;
995
996 v9fs_string_init(&uname);
997 v9fs_string_init(&aname);
998 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
999 &afid, &uname, &aname, &n_uname);
1000 if (err < 0) {
1001 goto out_nofid;
1002 }
1003 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1004
1005 fidp = alloc_fid(s, fid);
1006 if (fidp == NULL) {
1007 err = -EINVAL;
1008 goto out_nofid;
1009 }
1010 fidp->uid = n_uname;
1011 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1012 if (err < 0) {
1013 err = -EINVAL;
1014 clunk_fid(s, fid);
1015 goto out;
1016 }
1017 err = fid_to_qid(pdu, fidp, &qid);
1018 if (err < 0) {
1019 err = -EINVAL;
1020 clunk_fid(s, fid);
1021 goto out;
1022 }
1023
1024 /*
1025 * disable migration if we haven't done already.
1026 * attach could get called multiple times for the same export.
1027 */
1028 if (!s->migration_blocker) {
1029 error_setg(&s->migration_blocker,
1030 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1031 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1032 err = migrate_add_blocker(s->migration_blocker, &local_err);
1033 if (local_err) {
1034 error_free(local_err);
1035 error_free(s->migration_blocker);
1036 s->migration_blocker = NULL;
1037 clunk_fid(s, fid);
1038 goto out;
1039 }
1040 s->root_fid = fid;
1041 }
1042
1043 err = pdu_marshal(pdu, offset, "Q", &qid);
1044 if (err < 0) {
1045 clunk_fid(s, fid);
1046 goto out;
1047 }
1048 err += offset;
1049
1050 memcpy(&s->root_qid, &qid, sizeof(qid));
1051 trace_v9fs_attach_return(pdu->tag, pdu->id,
1052 qid.type, qid.version, qid.path);
1053 out:
1054 put_fid(pdu, fidp);
1055 out_nofid:
1056 pdu_complete(pdu, err);
1057 v9fs_string_free(&uname);
1058 v9fs_string_free(&aname);
1059 }
1060
1061 static void coroutine_fn v9fs_stat(void *opaque)
1062 {
1063 int32_t fid;
1064 V9fsStat v9stat;
1065 ssize_t err = 0;
1066 size_t offset = 7;
1067 struct stat stbuf;
1068 V9fsFidState *fidp;
1069 V9fsPDU *pdu = opaque;
1070 char *basename;
1071
1072 err = pdu_unmarshal(pdu, offset, "d", &fid);
1073 if (err < 0) {
1074 goto out_nofid;
1075 }
1076 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1077
1078 fidp = get_fid(pdu, fid);
1079 if (fidp == NULL) {
1080 err = -ENOENT;
1081 goto out_nofid;
1082 }
1083 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1084 if (err < 0) {
1085 goto out;
1086 }
1087 basename = g_path_get_basename(fidp->path.data);
1088 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1089 g_free(basename);
1090 if (err < 0) {
1091 goto out;
1092 }
1093 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1094 if (err < 0) {
1095 v9fs_stat_free(&v9stat);
1096 goto out;
1097 }
1098 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1099 v9stat.atime, v9stat.mtime, v9stat.length);
1100 err += offset;
1101 v9fs_stat_free(&v9stat);
1102 out:
1103 put_fid(pdu, fidp);
1104 out_nofid:
1105 pdu_complete(pdu, err);
1106 }
1107
1108 static void coroutine_fn v9fs_getattr(void *opaque)
1109 {
1110 int32_t fid;
1111 size_t offset = 7;
1112 ssize_t retval = 0;
1113 struct stat stbuf;
1114 V9fsFidState *fidp;
1115 uint64_t request_mask;
1116 V9fsStatDotl v9stat_dotl;
1117 V9fsPDU *pdu = opaque;
1118 V9fsState *s = pdu->s;
1119
1120 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1121 if (retval < 0) {
1122 goto out_nofid;
1123 }
1124 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1125
1126 fidp = get_fid(pdu, fid);
1127 if (fidp == NULL) {
1128 retval = -ENOENT;
1129 goto out_nofid;
1130 }
1131 /*
1132 * Currently we only support BASIC fields in stat, so there is no
1133 * need to look at request_mask.
1134 */
1135 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1136 if (retval < 0) {
1137 goto out;
1138 }
1139 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1140
1141 /* fill st_gen if requested and supported by underlying fs */
1142 if (request_mask & P9_STATS_GEN) {
1143 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1144 switch (retval) {
1145 case 0:
1146 /* we have valid st_gen: update result mask */
1147 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1148 break;
1149 case -EINTR:
1150 /* request cancelled, e.g. by Tflush */
1151 goto out;
1152 default:
1153 /* failed to get st_gen: not fatal, ignore */
1154 break;
1155 }
1156 }
1157 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1158 if (retval < 0) {
1159 goto out;
1160 }
1161 retval += offset;
1162 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1163 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1164 v9stat_dotl.st_gid);
1165 out:
1166 put_fid(pdu, fidp);
1167 out_nofid:
1168 pdu_complete(pdu, retval);
1169 }
1170
1171 /* Attribute flags */
1172 #define P9_ATTR_MODE (1 << 0)
1173 #define P9_ATTR_UID (1 << 1)
1174 #define P9_ATTR_GID (1 << 2)
1175 #define P9_ATTR_SIZE (1 << 3)
1176 #define P9_ATTR_ATIME (1 << 4)
1177 #define P9_ATTR_MTIME (1 << 5)
1178 #define P9_ATTR_CTIME (1 << 6)
1179 #define P9_ATTR_ATIME_SET (1 << 7)
1180 #define P9_ATTR_MTIME_SET (1 << 8)
1181
1182 #define P9_ATTR_MASK 127
1183
1184 static void coroutine_fn v9fs_setattr(void *opaque)
1185 {
1186 int err = 0;
1187 int32_t fid;
1188 V9fsFidState *fidp;
1189 size_t offset = 7;
1190 V9fsIattr v9iattr;
1191 V9fsPDU *pdu = opaque;
1192
1193 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1194 if (err < 0) {
1195 goto out_nofid;
1196 }
1197
1198 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1199 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1200 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1201
1202 fidp = get_fid(pdu, fid);
1203 if (fidp == NULL) {
1204 err = -EINVAL;
1205 goto out_nofid;
1206 }
1207 if (v9iattr.valid & P9_ATTR_MODE) {
1208 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1209 if (err < 0) {
1210 goto out;
1211 }
1212 }
1213 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1214 struct timespec times[2];
1215 if (v9iattr.valid & P9_ATTR_ATIME) {
1216 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1217 times[0].tv_sec = v9iattr.atime_sec;
1218 times[0].tv_nsec = v9iattr.atime_nsec;
1219 } else {
1220 times[0].tv_nsec = UTIME_NOW;
1221 }
1222 } else {
1223 times[0].tv_nsec = UTIME_OMIT;
1224 }
1225 if (v9iattr.valid & P9_ATTR_MTIME) {
1226 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1227 times[1].tv_sec = v9iattr.mtime_sec;
1228 times[1].tv_nsec = v9iattr.mtime_nsec;
1229 } else {
1230 times[1].tv_nsec = UTIME_NOW;
1231 }
1232 } else {
1233 times[1].tv_nsec = UTIME_OMIT;
1234 }
1235 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1236 if (err < 0) {
1237 goto out;
1238 }
1239 }
1240 /*
1241 * If the only valid entry in iattr is ctime we can call
1242 * chown(-1,-1) to update the ctime of the file
1243 */
1244 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1245 ((v9iattr.valid & P9_ATTR_CTIME)
1246 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1247 if (!(v9iattr.valid & P9_ATTR_UID)) {
1248 v9iattr.uid = -1;
1249 }
1250 if (!(v9iattr.valid & P9_ATTR_GID)) {
1251 v9iattr.gid = -1;
1252 }
1253 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1254 v9iattr.gid);
1255 if (err < 0) {
1256 goto out;
1257 }
1258 }
1259 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1260 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1261 if (err < 0) {
1262 goto out;
1263 }
1264 }
1265 err = offset;
1266 trace_v9fs_setattr_return(pdu->tag, pdu->id);
1267 out:
1268 put_fid(pdu, fidp);
1269 out_nofid:
1270 pdu_complete(pdu, err);
1271 }
1272
1273 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1274 {
1275 int i;
1276 ssize_t err;
1277 size_t offset = 7;
1278
1279 err = pdu_marshal(pdu, offset, "w", nwnames);
1280 if (err < 0) {
1281 return err;
1282 }
1283 offset += err;
1284 for (i = 0; i < nwnames; i++) {
1285 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1286 if (err < 0) {
1287 return err;
1288 }
1289 offset += err;
1290 }
1291 return offset;
1292 }
1293
1294 static bool name_is_illegal(const char *name)
1295 {
1296 return !*name || strchr(name, '/') != NULL;
1297 }
1298
1299 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1300 {
1301 return
1302 qid1->type != qid2->type ||
1303 qid1->version != qid2->version ||
1304 qid1->path != qid2->path;
1305 }
1306
1307 static void coroutine_fn v9fs_walk(void *opaque)
1308 {
1309 int name_idx;
1310 V9fsQID *qids = NULL;
1311 int i, err = 0;
1312 V9fsPath dpath, path;
1313 uint16_t nwnames;
1314 struct stat stbuf;
1315 size_t offset = 7;
1316 int32_t fid, newfid;
1317 V9fsString *wnames = NULL;
1318 V9fsFidState *fidp;
1319 V9fsFidState *newfidp = NULL;
1320 V9fsPDU *pdu = opaque;
1321 V9fsState *s = pdu->s;
1322 V9fsQID qid;
1323
1324 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1325 if (err < 0) {
1326 pdu_complete(pdu, err);
1327 return ;
1328 }
1329 offset += err;
1330
1331 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1332
1333 if (nwnames && nwnames <= P9_MAXWELEM) {
1334 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1335 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1336 for (i = 0; i < nwnames; i++) {
1337 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1338 if (err < 0) {
1339 goto out_nofid;
1340 }
1341 if (name_is_illegal(wnames[i].data)) {
1342 err = -ENOENT;
1343 goto out_nofid;
1344 }
1345 offset += err;
1346 }
1347 } else if (nwnames > P9_MAXWELEM) {
1348 err = -EINVAL;
1349 goto out_nofid;
1350 }
1351 fidp = get_fid(pdu, fid);
1352 if (fidp == NULL) {
1353 err = -ENOENT;
1354 goto out_nofid;
1355 }
1356
1357 v9fs_path_init(&dpath);
1358 v9fs_path_init(&path);
1359
1360 err = fid_to_qid(pdu, fidp, &qid);
1361 if (err < 0) {
1362 goto out;
1363 }
1364
1365 /*
1366 * Both dpath and path initially poin to fidp.
1367 * Needed to handle request with nwnames == 0
1368 */
1369 v9fs_path_copy(&dpath, &fidp->path);
1370 v9fs_path_copy(&path, &fidp->path);
1371 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1372 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1373 strcmp("..", wnames[name_idx].data)) {
1374 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1375 &path);
1376 if (err < 0) {
1377 goto out;
1378 }
1379
1380 err = v9fs_co_lstat(pdu, &path, &stbuf);
1381 if (err < 0) {
1382 goto out;
1383 }
1384 stat_to_qid(&stbuf, &qid);
1385 v9fs_path_copy(&dpath, &path);
1386 }
1387 memcpy(&qids[name_idx], &qid, sizeof(qid));
1388 }
1389 if (fid == newfid) {
1390 if (fidp->fid_type != P9_FID_NONE) {
1391 err = -EINVAL;
1392 goto out;
1393 }
1394 v9fs_path_write_lock(s);
1395 v9fs_path_copy(&fidp->path, &path);
1396 v9fs_path_unlock(s);
1397 } else {
1398 newfidp = alloc_fid(s, newfid);
1399 if (newfidp == NULL) {
1400 err = -EINVAL;
1401 goto out;
1402 }
1403 newfidp->uid = fidp->uid;
1404 v9fs_path_copy(&newfidp->path, &path);
1405 }
1406 err = v9fs_walk_marshal(pdu, nwnames, qids);
1407 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1408 out:
1409 put_fid(pdu, fidp);
1410 if (newfidp) {
1411 put_fid(pdu, newfidp);
1412 }
1413 v9fs_path_free(&dpath);
1414 v9fs_path_free(&path);
1415 out_nofid:
1416 pdu_complete(pdu, err);
1417 if (nwnames && nwnames <= P9_MAXWELEM) {
1418 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1419 v9fs_string_free(&wnames[name_idx]);
1420 }
1421 g_free(wnames);
1422 g_free(qids);
1423 }
1424 }
1425
1426 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1427 {
1428 struct statfs stbuf;
1429 int32_t iounit = 0;
1430 V9fsState *s = pdu->s;
1431
1432 /*
1433 * iounit should be multiples of f_bsize (host filesystem block size
1434 * and as well as less than (client msize - P9_IOHDRSZ))
1435 */
1436 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1437 iounit = stbuf.f_bsize;
1438 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1439 }
1440 if (!iounit) {
1441 iounit = s->msize - P9_IOHDRSZ;
1442 }
1443 return iounit;
1444 }
1445
1446 static void coroutine_fn v9fs_open(void *opaque)
1447 {
1448 int flags;
1449 int32_t fid;
1450 int32_t mode;
1451 V9fsQID qid;
1452 int iounit = 0;
1453 ssize_t err = 0;
1454 size_t offset = 7;
1455 struct stat stbuf;
1456 V9fsFidState *fidp;
1457 V9fsPDU *pdu = opaque;
1458 V9fsState *s = pdu->s;
1459
1460 if (s->proto_version == V9FS_PROTO_2000L) {
1461 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1462 } else {
1463 uint8_t modebyte;
1464 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1465 mode = modebyte;
1466 }
1467 if (err < 0) {
1468 goto out_nofid;
1469 }
1470 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1471
1472 fidp = get_fid(pdu, fid);
1473 if (fidp == NULL) {
1474 err = -ENOENT;
1475 goto out_nofid;
1476 }
1477 if (fidp->fid_type != P9_FID_NONE) {
1478 err = -EINVAL;
1479 goto out;
1480 }
1481
1482 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1483 if (err < 0) {
1484 goto out;
1485 }
1486 stat_to_qid(&stbuf, &qid);
1487 if (S_ISDIR(stbuf.st_mode)) {
1488 err = v9fs_co_opendir(pdu, fidp);
1489 if (err < 0) {
1490 goto out;
1491 }
1492 fidp->fid_type = P9_FID_DIR;
1493 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1494 if (err < 0) {
1495 goto out;
1496 }
1497 err += offset;
1498 } else {
1499 if (s->proto_version == V9FS_PROTO_2000L) {
1500 flags = get_dotl_openflags(s, mode);
1501 } else {
1502 flags = omode_to_uflags(mode);
1503 }
1504 if (is_ro_export(&s->ctx)) {
1505 if (mode & O_WRONLY || mode & O_RDWR ||
1506 mode & O_APPEND || mode & O_TRUNC) {
1507 err = -EROFS;
1508 goto out;
1509 }
1510 }
1511 err = v9fs_co_open(pdu, fidp, flags);
1512 if (err < 0) {
1513 goto out;
1514 }
1515 fidp->fid_type = P9_FID_FILE;
1516 fidp->open_flags = flags;
1517 if (flags & O_EXCL) {
1518 /*
1519 * We let the host file system do O_EXCL check
1520 * We should not reclaim such fd
1521 */
1522 fidp->flags |= FID_NON_RECLAIMABLE;
1523 }
1524 iounit = get_iounit(pdu, &fidp->path);
1525 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1526 if (err < 0) {
1527 goto out;
1528 }
1529 err += offset;
1530 }
1531 trace_v9fs_open_return(pdu->tag, pdu->id,
1532 qid.type, qid.version, qid.path, iounit);
1533 out:
1534 put_fid(pdu, fidp);
1535 out_nofid:
1536 pdu_complete(pdu, err);
1537 }
1538
1539 static void coroutine_fn v9fs_lcreate(void *opaque)
1540 {
1541 int32_t dfid, flags, mode;
1542 gid_t gid;
1543 ssize_t err = 0;
1544 ssize_t offset = 7;
1545 V9fsString name;
1546 V9fsFidState *fidp;
1547 struct stat stbuf;
1548 V9fsQID qid;
1549 int32_t iounit;
1550 V9fsPDU *pdu = opaque;
1551
1552 v9fs_string_init(&name);
1553 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1554 &name, &flags, &mode, &gid);
1555 if (err < 0) {
1556 goto out_nofid;
1557 }
1558 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1559
1560 if (name_is_illegal(name.data)) {
1561 err = -ENOENT;
1562 goto out_nofid;
1563 }
1564
1565 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1566 err = -EEXIST;
1567 goto out_nofid;
1568 }
1569
1570 fidp = get_fid(pdu, dfid);
1571 if (fidp == NULL) {
1572 err = -ENOENT;
1573 goto out_nofid;
1574 }
1575 if (fidp->fid_type != P9_FID_NONE) {
1576 err = -EINVAL;
1577 goto out;
1578 }
1579
1580 flags = get_dotl_openflags(pdu->s, flags);
1581 err = v9fs_co_open2(pdu, fidp, &name, gid,
1582 flags | O_CREAT, mode, &stbuf);
1583 if (err < 0) {
1584 goto out;
1585 }
1586 fidp->fid_type = P9_FID_FILE;
1587 fidp->open_flags = flags;
1588 if (flags & O_EXCL) {
1589 /*
1590 * We let the host file system do O_EXCL check
1591 * We should not reclaim such fd
1592 */
1593 fidp->flags |= FID_NON_RECLAIMABLE;
1594 }
1595 iounit = get_iounit(pdu, &fidp->path);
1596 stat_to_qid(&stbuf, &qid);
1597 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1598 if (err < 0) {
1599 goto out;
1600 }
1601 err += offset;
1602 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1603 qid.type, qid.version, qid.path, iounit);
1604 out:
1605 put_fid(pdu, fidp);
1606 out_nofid:
1607 pdu_complete(pdu, err);
1608 v9fs_string_free(&name);
1609 }
1610
1611 static void coroutine_fn v9fs_fsync(void *opaque)
1612 {
1613 int err;
1614 int32_t fid;
1615 int datasync;
1616 size_t offset = 7;
1617 V9fsFidState *fidp;
1618 V9fsPDU *pdu = opaque;
1619
1620 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1621 if (err < 0) {
1622 goto out_nofid;
1623 }
1624 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1625
1626 fidp = get_fid(pdu, fid);
1627 if (fidp == NULL) {
1628 err = -ENOENT;
1629 goto out_nofid;
1630 }
1631 err = v9fs_co_fsync(pdu, fidp, datasync);
1632 if (!err) {
1633 err = offset;
1634 }
1635 put_fid(pdu, fidp);
1636 out_nofid:
1637 pdu_complete(pdu, err);
1638 }
1639
1640 static void coroutine_fn v9fs_clunk(void *opaque)
1641 {
1642 int err;
1643 int32_t fid;
1644 size_t offset = 7;
1645 V9fsFidState *fidp;
1646 V9fsPDU *pdu = opaque;
1647 V9fsState *s = pdu->s;
1648
1649 err = pdu_unmarshal(pdu, offset, "d", &fid);
1650 if (err < 0) {
1651 goto out_nofid;
1652 }
1653 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1654
1655 fidp = clunk_fid(s, fid);
1656 if (fidp == NULL) {
1657 err = -ENOENT;
1658 goto out_nofid;
1659 }
1660 /*
1661 * Bump the ref so that put_fid will
1662 * free the fid.
1663 */
1664 fidp->ref++;
1665 err = put_fid(pdu, fidp);
1666 if (!err) {
1667 err = offset;
1668 }
1669 out_nofid:
1670 pdu_complete(pdu, err);
1671 }
1672
1673 /*
1674 * Create a QEMUIOVector for a sub-region of PDU iovecs
1675 *
1676 * @qiov: uninitialized QEMUIOVector
1677 * @skip: number of bytes to skip from beginning of PDU
1678 * @size: number of bytes to include
1679 * @is_write: true - write, false - read
1680 *
1681 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1682 * with qemu_iovec_destroy().
1683 */
1684 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1685 size_t skip, size_t size,
1686 bool is_write)
1687 {
1688 QEMUIOVector elem;
1689 struct iovec *iov;
1690 unsigned int niov;
1691
1692 if (is_write) {
1693 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1694 } else {
1695 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1696 }
1697
1698 qemu_iovec_init_external(&elem, iov, niov);
1699 qemu_iovec_init(qiov, niov);
1700 qemu_iovec_concat(qiov, &elem, skip, size);
1701 }
1702
1703 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1704 uint64_t off, uint32_t max_count)
1705 {
1706 ssize_t err;
1707 size_t offset = 7;
1708 uint64_t read_count;
1709 QEMUIOVector qiov_full;
1710
1711 if (fidp->fs.xattr.len < off) {
1712 read_count = 0;
1713 } else {
1714 read_count = fidp->fs.xattr.len - off;
1715 }
1716 if (read_count > max_count) {
1717 read_count = max_count;
1718 }
1719 err = pdu_marshal(pdu, offset, "d", read_count);
1720 if (err < 0) {
1721 return err;
1722 }
1723 offset += err;
1724
1725 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1726 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1727 ((char *)fidp->fs.xattr.value) + off,
1728 read_count);
1729 qemu_iovec_destroy(&qiov_full);
1730 if (err < 0) {
1731 return err;
1732 }
1733 offset += err;
1734 return offset;
1735 }
1736
1737 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1738 V9fsFidState *fidp,
1739 uint32_t max_count)
1740 {
1741 V9fsPath path;
1742 V9fsStat v9stat;
1743 int len, err = 0;
1744 int32_t count = 0;
1745 struct stat stbuf;
1746 off_t saved_dir_pos;
1747 struct dirent *dent;
1748
1749 /* save the directory position */
1750 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1751 if (saved_dir_pos < 0) {
1752 return saved_dir_pos;
1753 }
1754
1755 while (1) {
1756 v9fs_path_init(&path);
1757
1758 v9fs_readdir_lock(&fidp->fs.dir);
1759
1760 err = v9fs_co_readdir(pdu, fidp, &dent);
1761 if (err || !dent) {
1762 break;
1763 }
1764 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1765 if (err < 0) {
1766 break;
1767 }
1768 err = v9fs_co_lstat(pdu, &path, &stbuf);
1769 if (err < 0) {
1770 break;
1771 }
1772 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1773 if (err < 0) {
1774 break;
1775 }
1776 if ((count + v9stat.size + 2) > max_count) {
1777 v9fs_readdir_unlock(&fidp->fs.dir);
1778
1779 /* Ran out of buffer. Set dir back to old position and return */
1780 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1781 v9fs_stat_free(&v9stat);
1782 v9fs_path_free(&path);
1783 return count;
1784 }
1785
1786 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1787 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1788
1789 v9fs_readdir_unlock(&fidp->fs.dir);
1790
1791 if (len < 0) {
1792 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1793 v9fs_stat_free(&v9stat);
1794 v9fs_path_free(&path);
1795 return len;
1796 }
1797 count += len;
1798 v9fs_stat_free(&v9stat);
1799 v9fs_path_free(&path);
1800 saved_dir_pos = dent->d_off;
1801 }
1802
1803 v9fs_readdir_unlock(&fidp->fs.dir);
1804
1805 v9fs_path_free(&path);
1806 if (err < 0) {
1807 return err;
1808 }
1809 return count;
1810 }
1811
1812 static void coroutine_fn v9fs_read(void *opaque)
1813 {
1814 int32_t fid;
1815 uint64_t off;
1816 ssize_t err = 0;
1817 int32_t count = 0;
1818 size_t offset = 7;
1819 uint32_t max_count;
1820 V9fsFidState *fidp;
1821 V9fsPDU *pdu = opaque;
1822 V9fsState *s = pdu->s;
1823
1824 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1825 if (err < 0) {
1826 goto out_nofid;
1827 }
1828 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1829
1830 fidp = get_fid(pdu, fid);
1831 if (fidp == NULL) {
1832 err = -EINVAL;
1833 goto out_nofid;
1834 }
1835 if (fidp->fid_type == P9_FID_DIR) {
1836
1837 if (off == 0) {
1838 v9fs_co_rewinddir(pdu, fidp);
1839 }
1840 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1841 if (count < 0) {
1842 err = count;
1843 goto out;
1844 }
1845 err = pdu_marshal(pdu, offset, "d", count);
1846 if (err < 0) {
1847 goto out;
1848 }
1849 err += offset + count;
1850 } else if (fidp->fid_type == P9_FID_FILE) {
1851 QEMUIOVector qiov_full;
1852 QEMUIOVector qiov;
1853 int32_t len;
1854
1855 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1856 qemu_iovec_init(&qiov, qiov_full.niov);
1857 do {
1858 qemu_iovec_reset(&qiov);
1859 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1860 if (0) {
1861 print_sg(qiov.iov, qiov.niov);
1862 }
1863 /* Loop in case of EINTR */
1864 do {
1865 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1866 if (len >= 0) {
1867 off += len;
1868 count += len;
1869 }
1870 } while (len == -EINTR && !pdu->cancelled);
1871 if (len < 0) {
1872 /* IO error return the error */
1873 err = len;
1874 goto out_free_iovec;
1875 }
1876 } while (count < max_count && len > 0);
1877 err = pdu_marshal(pdu, offset, "d", count);
1878 if (err < 0) {
1879 goto out_free_iovec;
1880 }
1881 err += offset + count;
1882 out_free_iovec:
1883 qemu_iovec_destroy(&qiov);
1884 qemu_iovec_destroy(&qiov_full);
1885 } else if (fidp->fid_type == P9_FID_XATTR) {
1886 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1887 } else {
1888 err = -EINVAL;
1889 }
1890 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1891 out:
1892 put_fid(pdu, fidp);
1893 out_nofid:
1894 pdu_complete(pdu, err);
1895 }
1896
1897 static size_t v9fs_readdir_data_size(V9fsString *name)
1898 {
1899 /*
1900 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1901 * size of type (1) + size of name.size (2) + strlen(name.data)
1902 */
1903 return 24 + v9fs_string_size(name);
1904 }
1905
1906 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1907 int32_t max_count)
1908 {
1909 size_t size;
1910 V9fsQID qid;
1911 V9fsString name;
1912 int len, err = 0;
1913 int32_t count = 0;
1914 off_t saved_dir_pos;
1915 struct dirent *dent;
1916
1917 /* save the directory position */
1918 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1919 if (saved_dir_pos < 0) {
1920 return saved_dir_pos;
1921 }
1922
1923 while (1) {
1924 v9fs_readdir_lock(&fidp->fs.dir);
1925
1926 err = v9fs_co_readdir(pdu, fidp, &dent);
1927 if (err || !dent) {
1928 break;
1929 }
1930 v9fs_string_init(&name);
1931 v9fs_string_sprintf(&name, "%s", dent->d_name);
1932 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1933 v9fs_readdir_unlock(&fidp->fs.dir);
1934
1935 /* Ran out of buffer. Set dir back to old position and return */
1936 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1937 v9fs_string_free(&name);
1938 return count;
1939 }
1940 /*
1941 * Fill up just the path field of qid because the client uses
1942 * only that. To fill the entire qid structure we will have
1943 * to stat each dirent found, which is expensive
1944 */
1945 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1946 memcpy(&qid.path, &dent->d_ino, size);
1947 /* Fill the other fields with dummy values */
1948 qid.type = 0;
1949 qid.version = 0;
1950
1951 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1952 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1953 &qid, dent->d_off,
1954 dent->d_type, &name);
1955
1956 v9fs_readdir_unlock(&fidp->fs.dir);
1957
1958 if (len < 0) {
1959 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1960 v9fs_string_free(&name);
1961 return len;
1962 }
1963 count += len;
1964 v9fs_string_free(&name);
1965 saved_dir_pos = dent->d_off;
1966 }
1967
1968 v9fs_readdir_unlock(&fidp->fs.dir);
1969
1970 if (err < 0) {
1971 return err;
1972 }
1973 return count;
1974 }
1975
1976 static void coroutine_fn v9fs_readdir(void *opaque)
1977 {
1978 int32_t fid;
1979 V9fsFidState *fidp;
1980 ssize_t retval = 0;
1981 size_t offset = 7;
1982 uint64_t initial_offset;
1983 int32_t count;
1984 uint32_t max_count;
1985 V9fsPDU *pdu = opaque;
1986
1987 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1988 &initial_offset, &max_count);
1989 if (retval < 0) {
1990 goto out_nofid;
1991 }
1992 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1993
1994 fidp = get_fid(pdu, fid);
1995 if (fidp == NULL) {
1996 retval = -EINVAL;
1997 goto out_nofid;
1998 }
1999 if (!fidp->fs.dir.stream) {
2000 retval = -EINVAL;
2001 goto out;
2002 }
2003 if (initial_offset == 0) {
2004 v9fs_co_rewinddir(pdu, fidp);
2005 } else {
2006 v9fs_co_seekdir(pdu, fidp, initial_offset);
2007 }
2008 count = v9fs_do_readdir(pdu, fidp, max_count);
2009 if (count < 0) {
2010 retval = count;
2011 goto out;
2012 }
2013 retval = pdu_marshal(pdu, offset, "d", count);
2014 if (retval < 0) {
2015 goto out;
2016 }
2017 retval += count + offset;
2018 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2019 out:
2020 put_fid(pdu, fidp);
2021 out_nofid:
2022 pdu_complete(pdu, retval);
2023 }
2024
2025 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2026 uint64_t off, uint32_t count,
2027 struct iovec *sg, int cnt)
2028 {
2029 int i, to_copy;
2030 ssize_t err = 0;
2031 uint64_t write_count;
2032 size_t offset = 7;
2033
2034
2035 if (fidp->fs.xattr.len < off) {
2036 err = -ENOSPC;
2037 goto out;
2038 }
2039 write_count = fidp->fs.xattr.len - off;
2040 if (write_count > count) {
2041 write_count = count;
2042 }
2043 err = pdu_marshal(pdu, offset, "d", write_count);
2044 if (err < 0) {
2045 return err;
2046 }
2047 err += offset;
2048 fidp->fs.xattr.copied_len += write_count;
2049 /*
2050 * Now copy the content from sg list
2051 */
2052 for (i = 0; i < cnt; i++) {
2053 if (write_count > sg[i].iov_len) {
2054 to_copy = sg[i].iov_len;
2055 } else {
2056 to_copy = write_count;
2057 }
2058 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2059 /* updating vs->off since we are not using below */
2060 off += to_copy;
2061 write_count -= to_copy;
2062 }
2063 out:
2064 return err;
2065 }
2066
2067 static void coroutine_fn v9fs_write(void *opaque)
2068 {
2069 ssize_t err;
2070 int32_t fid;
2071 uint64_t off;
2072 uint32_t count;
2073 int32_t len = 0;
2074 int32_t total = 0;
2075 size_t offset = 7;
2076 V9fsFidState *fidp;
2077 V9fsPDU *pdu = opaque;
2078 V9fsState *s = pdu->s;
2079 QEMUIOVector qiov_full;
2080 QEMUIOVector qiov;
2081
2082 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2083 if (err < 0) {
2084 pdu_complete(pdu, err);
2085 return;
2086 }
2087 offset += err;
2088 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2089 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2090
2091 fidp = get_fid(pdu, fid);
2092 if (fidp == NULL) {
2093 err = -EINVAL;
2094 goto out_nofid;
2095 }
2096 if (fidp->fid_type == P9_FID_FILE) {
2097 if (fidp->fs.fd == -1) {
2098 err = -EINVAL;
2099 goto out;
2100 }
2101 } else if (fidp->fid_type == P9_FID_XATTR) {
2102 /*
2103 * setxattr operation
2104 */
2105 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2106 qiov_full.iov, qiov_full.niov);
2107 goto out;
2108 } else {
2109 err = -EINVAL;
2110 goto out;
2111 }
2112 qemu_iovec_init(&qiov, qiov_full.niov);
2113 do {
2114 qemu_iovec_reset(&qiov);
2115 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2116 if (0) {
2117 print_sg(qiov.iov, qiov.niov);
2118 }
2119 /* Loop in case of EINTR */
2120 do {
2121 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2122 if (len >= 0) {
2123 off += len;
2124 total += len;
2125 }
2126 } while (len == -EINTR && !pdu->cancelled);
2127 if (len < 0) {
2128 /* IO error return the error */
2129 err = len;
2130 goto out_qiov;
2131 }
2132 } while (total < count && len > 0);
2133
2134 offset = 7;
2135 err = pdu_marshal(pdu, offset, "d", total);
2136 if (err < 0) {
2137 goto out_qiov;
2138 }
2139 err += offset;
2140 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2141 out_qiov:
2142 qemu_iovec_destroy(&qiov);
2143 out:
2144 put_fid(pdu, fidp);
2145 out_nofid:
2146 qemu_iovec_destroy(&qiov_full);
2147 pdu_complete(pdu, err);
2148 }
2149
2150 static void coroutine_fn v9fs_create(void *opaque)
2151 {
2152 int32_t fid;
2153 int err = 0;
2154 size_t offset = 7;
2155 V9fsFidState *fidp;
2156 V9fsQID qid;
2157 int32_t perm;
2158 int8_t mode;
2159 V9fsPath path;
2160 struct stat stbuf;
2161 V9fsString name;
2162 V9fsString extension;
2163 int iounit;
2164 V9fsPDU *pdu = opaque;
2165 V9fsState *s = pdu->s;
2166
2167 v9fs_path_init(&path);
2168 v9fs_string_init(&name);
2169 v9fs_string_init(&extension);
2170 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2171 &perm, &mode, &extension);
2172 if (err < 0) {
2173 goto out_nofid;
2174 }
2175 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2176
2177 if (name_is_illegal(name.data)) {
2178 err = -ENOENT;
2179 goto out_nofid;
2180 }
2181
2182 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2183 err = -EEXIST;
2184 goto out_nofid;
2185 }
2186
2187 fidp = get_fid(pdu, fid);
2188 if (fidp == NULL) {
2189 err = -EINVAL;
2190 goto out_nofid;
2191 }
2192 if (fidp->fid_type != P9_FID_NONE) {
2193 err = -EINVAL;
2194 goto out;
2195 }
2196 if (perm & P9_STAT_MODE_DIR) {
2197 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2198 fidp->uid, -1, &stbuf);
2199 if (err < 0) {
2200 goto out;
2201 }
2202 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2203 if (err < 0) {
2204 goto out;
2205 }
2206 v9fs_path_write_lock(s);
2207 v9fs_path_copy(&fidp->path, &path);
2208 v9fs_path_unlock(s);
2209 err = v9fs_co_opendir(pdu, fidp);
2210 if (err < 0) {
2211 goto out;
2212 }
2213 fidp->fid_type = P9_FID_DIR;
2214 } else if (perm & P9_STAT_MODE_SYMLINK) {
2215 err = v9fs_co_symlink(pdu, fidp, &name,
2216 extension.data, -1 , &stbuf);
2217 if (err < 0) {
2218 goto out;
2219 }
2220 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2221 if (err < 0) {
2222 goto out;
2223 }
2224 v9fs_path_write_lock(s);
2225 v9fs_path_copy(&fidp->path, &path);
2226 v9fs_path_unlock(s);
2227 } else if (perm & P9_STAT_MODE_LINK) {
2228 int32_t ofid = atoi(extension.data);
2229 V9fsFidState *ofidp = get_fid(pdu, ofid);
2230 if (ofidp == NULL) {
2231 err = -EINVAL;
2232 goto out;
2233 }
2234 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2235 put_fid(pdu, ofidp);
2236 if (err < 0) {
2237 goto out;
2238 }
2239 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2240 if (err < 0) {
2241 fidp->fid_type = P9_FID_NONE;
2242 goto out;
2243 }
2244 v9fs_path_write_lock(s);
2245 v9fs_path_copy(&fidp->path, &path);
2246 v9fs_path_unlock(s);
2247 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2248 if (err < 0) {
2249 fidp->fid_type = P9_FID_NONE;
2250 goto out;
2251 }
2252 } else if (perm & P9_STAT_MODE_DEVICE) {
2253 char ctype;
2254 uint32_t major, minor;
2255 mode_t nmode = 0;
2256
2257 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2258 err = -errno;
2259 goto out;
2260 }
2261
2262 switch (ctype) {
2263 case 'c':
2264 nmode = S_IFCHR;
2265 break;
2266 case 'b':
2267 nmode = S_IFBLK;
2268 break;
2269 default:
2270 err = -EIO;
2271 goto out;
2272 }
2273
2274 nmode |= perm & 0777;
2275 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2276 makedev(major, minor), nmode, &stbuf);
2277 if (err < 0) {
2278 goto out;
2279 }
2280 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2281 if (err < 0) {
2282 goto out;
2283 }
2284 v9fs_path_write_lock(s);
2285 v9fs_path_copy(&fidp->path, &path);
2286 v9fs_path_unlock(s);
2287 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2288 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2289 0, S_IFIFO | (perm & 0777), &stbuf);
2290 if (err < 0) {
2291 goto out;
2292 }
2293 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2294 if (err < 0) {
2295 goto out;
2296 }
2297 v9fs_path_write_lock(s);
2298 v9fs_path_copy(&fidp->path, &path);
2299 v9fs_path_unlock(s);
2300 } else if (perm & P9_STAT_MODE_SOCKET) {
2301 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2302 0, S_IFSOCK | (perm & 0777), &stbuf);
2303 if (err < 0) {
2304 goto out;
2305 }
2306 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2307 if (err < 0) {
2308 goto out;
2309 }
2310 v9fs_path_write_lock(s);
2311 v9fs_path_copy(&fidp->path, &path);
2312 v9fs_path_unlock(s);
2313 } else {
2314 err = v9fs_co_open2(pdu, fidp, &name, -1,
2315 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2316 if (err < 0) {
2317 goto out;
2318 }
2319 fidp->fid_type = P9_FID_FILE;
2320 fidp->open_flags = omode_to_uflags(mode);
2321 if (fidp->open_flags & O_EXCL) {
2322 /*
2323 * We let the host file system do O_EXCL check
2324 * We should not reclaim such fd
2325 */
2326 fidp->flags |= FID_NON_RECLAIMABLE;
2327 }
2328 }
2329 iounit = get_iounit(pdu, &fidp->path);
2330 stat_to_qid(&stbuf, &qid);
2331 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2332 if (err < 0) {
2333 goto out;
2334 }
2335 err += offset;
2336 trace_v9fs_create_return(pdu->tag, pdu->id,
2337 qid.type, qid.version, qid.path, iounit);
2338 out:
2339 put_fid(pdu, fidp);
2340 out_nofid:
2341 pdu_complete(pdu, err);
2342 v9fs_string_free(&name);
2343 v9fs_string_free(&extension);
2344 v9fs_path_free(&path);
2345 }
2346
2347 static void coroutine_fn v9fs_symlink(void *opaque)
2348 {
2349 V9fsPDU *pdu = opaque;
2350 V9fsString name;
2351 V9fsString symname;
2352 V9fsFidState *dfidp;
2353 V9fsQID qid;
2354 struct stat stbuf;
2355 int32_t dfid;
2356 int err = 0;
2357 gid_t gid;
2358 size_t offset = 7;
2359
2360 v9fs_string_init(&name);
2361 v9fs_string_init(&symname);
2362 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2363 if (err < 0) {
2364 goto out_nofid;
2365 }
2366 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2367
2368 if (name_is_illegal(name.data)) {
2369 err = -ENOENT;
2370 goto out_nofid;
2371 }
2372
2373 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2374 err = -EEXIST;
2375 goto out_nofid;
2376 }
2377
2378 dfidp = get_fid(pdu, dfid);
2379 if (dfidp == NULL) {
2380 err = -EINVAL;
2381 goto out_nofid;
2382 }
2383 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2384 if (err < 0) {
2385 goto out;
2386 }
2387 stat_to_qid(&stbuf, &qid);
2388 err = pdu_marshal(pdu, offset, "Q", &qid);
2389 if (err < 0) {
2390 goto out;
2391 }
2392 err += offset;
2393 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2394 qid.type, qid.version, qid.path);
2395 out:
2396 put_fid(pdu, dfidp);
2397 out_nofid:
2398 pdu_complete(pdu, err);
2399 v9fs_string_free(&name);
2400 v9fs_string_free(&symname);
2401 }
2402
2403 static void coroutine_fn v9fs_flush(void *opaque)
2404 {
2405 ssize_t err;
2406 int16_t tag;
2407 size_t offset = 7;
2408 V9fsPDU *cancel_pdu = NULL;
2409 V9fsPDU *pdu = opaque;
2410 V9fsState *s = pdu->s;
2411
2412 err = pdu_unmarshal(pdu, offset, "w", &tag);
2413 if (err < 0) {
2414 pdu_complete(pdu, err);
2415 return;
2416 }
2417 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2418
2419 if (pdu->tag == tag) {
2420 warn_report("the guest sent a self-referencing 9P flush request");
2421 } else {
2422 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2423 if (cancel_pdu->tag == tag) {
2424 break;
2425 }
2426 }
2427 }
2428 if (cancel_pdu) {
2429 cancel_pdu->cancelled = 1;
2430 /*
2431 * Wait for pdu to complete.
2432 */
2433 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2434 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2435 cancel_pdu->cancelled = 0;
2436 pdu_free(cancel_pdu);
2437 }
2438 }
2439 pdu_complete(pdu, 7);
2440 }
2441
2442 static void coroutine_fn v9fs_link(void *opaque)
2443 {
2444 V9fsPDU *pdu = opaque;
2445 int32_t dfid, oldfid;
2446 V9fsFidState *dfidp, *oldfidp;
2447 V9fsString name;
2448 size_t offset = 7;
2449 int err = 0;
2450
2451 v9fs_string_init(&name);
2452 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2453 if (err < 0) {
2454 goto out_nofid;
2455 }
2456 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2457
2458 if (name_is_illegal(name.data)) {
2459 err = -ENOENT;
2460 goto out_nofid;
2461 }
2462
2463 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2464 err = -EEXIST;
2465 goto out_nofid;
2466 }
2467
2468 dfidp = get_fid(pdu, dfid);
2469 if (dfidp == NULL) {
2470 err = -ENOENT;
2471 goto out_nofid;
2472 }
2473
2474 oldfidp = get_fid(pdu, oldfid);
2475 if (oldfidp == NULL) {
2476 err = -ENOENT;
2477 goto out;
2478 }
2479 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2480 if (!err) {
2481 err = offset;
2482 }
2483 put_fid(pdu, oldfidp);
2484 out:
2485 put_fid(pdu, dfidp);
2486 out_nofid:
2487 v9fs_string_free(&name);
2488 pdu_complete(pdu, err);
2489 }
2490
2491 /* Only works with path name based fid */
2492 static void coroutine_fn v9fs_remove(void *opaque)
2493 {
2494 int32_t fid;
2495 int err = 0;
2496 size_t offset = 7;
2497 V9fsFidState *fidp;
2498 V9fsPDU *pdu = opaque;
2499
2500 err = pdu_unmarshal(pdu, offset, "d", &fid);
2501 if (err < 0) {
2502 goto out_nofid;
2503 }
2504 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2505
2506 fidp = get_fid(pdu, fid);
2507 if (fidp == NULL) {
2508 err = -EINVAL;
2509 goto out_nofid;
2510 }
2511 /* if fs driver is not path based, return EOPNOTSUPP */
2512 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2513 err = -EOPNOTSUPP;
2514 goto out_err;
2515 }
2516 /*
2517 * IF the file is unlinked, we cannot reopen
2518 * the file later. So don't reclaim fd
2519 */
2520 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2521 if (err < 0) {
2522 goto out_err;
2523 }
2524 err = v9fs_co_remove(pdu, &fidp->path);
2525 if (!err) {
2526 err = offset;
2527 }
2528 out_err:
2529 /* For TREMOVE we need to clunk the fid even on failed remove */
2530 clunk_fid(pdu->s, fidp->fid);
2531 put_fid(pdu, fidp);
2532 out_nofid:
2533 pdu_complete(pdu, err);
2534 }
2535
2536 static void coroutine_fn v9fs_unlinkat(void *opaque)
2537 {
2538 int err = 0;
2539 V9fsString name;
2540 int32_t dfid, flags, rflags = 0;
2541 size_t offset = 7;
2542 V9fsPath path;
2543 V9fsFidState *dfidp;
2544 V9fsPDU *pdu = opaque;
2545
2546 v9fs_string_init(&name);
2547 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2548 if (err < 0) {
2549 goto out_nofid;
2550 }
2551
2552 if (name_is_illegal(name.data)) {
2553 err = -ENOENT;
2554 goto out_nofid;
2555 }
2556
2557 if (!strcmp(".", name.data)) {
2558 err = -EINVAL;
2559 goto out_nofid;
2560 }
2561
2562 if (!strcmp("..", name.data)) {
2563 err = -ENOTEMPTY;
2564 goto out_nofid;
2565 }
2566
2567 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
2568 err = -EINVAL;
2569 goto out_nofid;
2570 }
2571
2572 if (flags & P9_DOTL_AT_REMOVEDIR) {
2573 rflags |= AT_REMOVEDIR;
2574 }
2575
2576 dfidp = get_fid(pdu, dfid);
2577 if (dfidp == NULL) {
2578 err = -EINVAL;
2579 goto out_nofid;
2580 }
2581 /*
2582 * IF the file is unlinked, we cannot reopen
2583 * the file later. So don't reclaim fd
2584 */
2585 v9fs_path_init(&path);
2586 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2587 if (err < 0) {
2588 goto out_err;
2589 }
2590 err = v9fs_mark_fids_unreclaim(pdu, &path);
2591 if (err < 0) {
2592 goto out_err;
2593 }
2594 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
2595 if (!err) {
2596 err = offset;
2597 }
2598 out_err:
2599 put_fid(pdu, dfidp);
2600 v9fs_path_free(&path);
2601 out_nofid:
2602 pdu_complete(pdu, err);
2603 v9fs_string_free(&name);
2604 }
2605
2606
2607 /* Only works with path name based fid */
2608 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2609 int32_t newdirfid,
2610 V9fsString *name)
2611 {
2612 int err = 0;
2613 V9fsPath new_path;
2614 V9fsFidState *tfidp;
2615 V9fsState *s = pdu->s;
2616 V9fsFidState *dirfidp = NULL;
2617
2618 v9fs_path_init(&new_path);
2619 if (newdirfid != -1) {
2620 dirfidp = get_fid(pdu, newdirfid);
2621 if (dirfidp == NULL) {
2622 err = -ENOENT;
2623 goto out_nofid;
2624 }
2625 if (fidp->fid_type != P9_FID_NONE) {
2626 err = -EINVAL;
2627 goto out;
2628 }
2629 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2630 if (err < 0) {
2631 goto out;
2632 }
2633 } else {
2634 char *dir_name = g_path_get_dirname(fidp->path.data);
2635 V9fsPath dir_path;
2636
2637 v9fs_path_init(&dir_path);
2638 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2639 g_free(dir_name);
2640
2641 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2642 v9fs_path_free(&dir_path);
2643 if (err < 0) {
2644 goto out;
2645 }
2646 }
2647 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2648 if (err < 0) {
2649 goto out;
2650 }
2651 /*
2652 * Fixup fid's pointing to the old name to
2653 * start pointing to the new name
2654 */
2655 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2656 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2657 /* replace the name */
2658 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2659 }
2660 }
2661 out:
2662 if (dirfidp) {
2663 put_fid(pdu, dirfidp);
2664 }
2665 v9fs_path_free(&new_path);
2666 out_nofid:
2667 return err;
2668 }
2669
2670 /* Only works with path name based fid */
2671 static void coroutine_fn v9fs_rename(void *opaque)
2672 {
2673 int32_t fid;
2674 ssize_t err = 0;
2675 size_t offset = 7;
2676 V9fsString name;
2677 int32_t newdirfid;
2678 V9fsFidState *fidp;
2679 V9fsPDU *pdu = opaque;
2680 V9fsState *s = pdu->s;
2681
2682 v9fs_string_init(&name);
2683 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2684 if (err < 0) {
2685 goto out_nofid;
2686 }
2687
2688 if (name_is_illegal(name.data)) {
2689 err = -ENOENT;
2690 goto out_nofid;
2691 }
2692
2693 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2694 err = -EISDIR;
2695 goto out_nofid;
2696 }
2697
2698 fidp = get_fid(pdu, fid);
2699 if (fidp == NULL) {
2700 err = -ENOENT;
2701 goto out_nofid;
2702 }
2703 if (fidp->fid_type != P9_FID_NONE) {
2704 err = -EINVAL;
2705 goto out;
2706 }
2707 /* if fs driver is not path based, return EOPNOTSUPP */
2708 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2709 err = -EOPNOTSUPP;
2710 goto out;
2711 }
2712 v9fs_path_write_lock(s);
2713 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2714 v9fs_path_unlock(s);
2715 if (!err) {
2716 err = offset;
2717 }
2718 out:
2719 put_fid(pdu, fidp);
2720 out_nofid:
2721 pdu_complete(pdu, err);
2722 v9fs_string_free(&name);
2723 }
2724
2725 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2726 V9fsString *old_name,
2727 V9fsPath *newdir,
2728 V9fsString *new_name)
2729 {
2730 V9fsFidState *tfidp;
2731 V9fsPath oldpath, newpath;
2732 V9fsState *s = pdu->s;
2733 int err;
2734
2735 v9fs_path_init(&oldpath);
2736 v9fs_path_init(&newpath);
2737 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2738 if (err < 0) {
2739 goto out;
2740 }
2741 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2742 if (err < 0) {
2743 goto out;
2744 }
2745
2746 /*
2747 * Fixup fid's pointing to the old name to
2748 * start pointing to the new name
2749 */
2750 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2751 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2752 /* replace the name */
2753 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2754 }
2755 }
2756 out:
2757 v9fs_path_free(&oldpath);
2758 v9fs_path_free(&newpath);
2759 return err;
2760 }
2761
2762 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2763 V9fsString *old_name,
2764 int32_t newdirfid,
2765 V9fsString *new_name)
2766 {
2767 int err = 0;
2768 V9fsState *s = pdu->s;
2769 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2770
2771 olddirfidp = get_fid(pdu, olddirfid);
2772 if (olddirfidp == NULL) {
2773 err = -ENOENT;
2774 goto out;
2775 }
2776 if (newdirfid != -1) {
2777 newdirfidp = get_fid(pdu, newdirfid);
2778 if (newdirfidp == NULL) {
2779 err = -ENOENT;
2780 goto out;
2781 }
2782 } else {
2783 newdirfidp = get_fid(pdu, olddirfid);
2784 }
2785
2786 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2787 &newdirfidp->path, new_name);
2788 if (err < 0) {
2789 goto out;
2790 }
2791 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2792 /* Only for path based fid we need to do the below fixup */
2793 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2794 &newdirfidp->path, new_name);
2795 }
2796 out:
2797 if (olddirfidp) {
2798 put_fid(pdu, olddirfidp);
2799 }
2800 if (newdirfidp) {
2801 put_fid(pdu, newdirfidp);
2802 }
2803 return err;
2804 }
2805
2806 static void coroutine_fn v9fs_renameat(void *opaque)
2807 {
2808 ssize_t err = 0;
2809 size_t offset = 7;
2810 V9fsPDU *pdu = opaque;
2811 V9fsState *s = pdu->s;
2812 int32_t olddirfid, newdirfid;
2813 V9fsString old_name, new_name;
2814
2815 v9fs_string_init(&old_name);
2816 v9fs_string_init(&new_name);
2817 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2818 &old_name, &newdirfid, &new_name);
2819 if (err < 0) {
2820 goto out_err;
2821 }
2822
2823 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2824 err = -ENOENT;
2825 goto out_err;
2826 }
2827
2828 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2829 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2830 err = -EISDIR;
2831 goto out_err;
2832 }
2833
2834 v9fs_path_write_lock(s);
2835 err = v9fs_complete_renameat(pdu, olddirfid,
2836 &old_name, newdirfid, &new_name);
2837 v9fs_path_unlock(s);
2838 if (!err) {
2839 err = offset;
2840 }
2841
2842 out_err:
2843 pdu_complete(pdu, err);
2844 v9fs_string_free(&old_name);
2845 v9fs_string_free(&new_name);
2846 }
2847
2848 static void coroutine_fn v9fs_wstat(void *opaque)
2849 {
2850 int32_t fid;
2851 int err = 0;
2852 int16_t unused;
2853 V9fsStat v9stat;
2854 size_t offset = 7;
2855 struct stat stbuf;
2856 V9fsFidState *fidp;
2857 V9fsPDU *pdu = opaque;
2858 V9fsState *s = pdu->s;
2859
2860 v9fs_stat_init(&v9stat);
2861 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2862 if (err < 0) {
2863 goto out_nofid;
2864 }
2865 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2866 v9stat.mode, v9stat.atime, v9stat.mtime);
2867
2868 fidp = get_fid(pdu, fid);
2869 if (fidp == NULL) {
2870 err = -EINVAL;
2871 goto out_nofid;
2872 }
2873 /* do we need to sync the file? */
2874 if (donttouch_stat(&v9stat)) {
2875 err = v9fs_co_fsync(pdu, fidp, 0);
2876 goto out;
2877 }
2878 if (v9stat.mode != -1) {
2879 uint32_t v9_mode;
2880 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2881 if (err < 0) {
2882 goto out;
2883 }
2884 v9_mode = stat_to_v9mode(&stbuf);
2885 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2886 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2887 /* Attempting to change the type */
2888 err = -EIO;
2889 goto out;
2890 }
2891 err = v9fs_co_chmod(pdu, &fidp->path,
2892 v9mode_to_mode(v9stat.mode,
2893 &v9stat.extension));
2894 if (err < 0) {
2895 goto out;
2896 }
2897 }
2898 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2899 struct timespec times[2];
2900 if (v9stat.atime != -1) {
2901 times[0].tv_sec = v9stat.atime;
2902 times[0].tv_nsec = 0;
2903 } else {
2904 times[0].tv_nsec = UTIME_OMIT;
2905 }
2906 if (v9stat.mtime != -1) {
2907 times[1].tv_sec = v9stat.mtime;
2908 times[1].tv_nsec = 0;
2909 } else {
2910 times[1].tv_nsec = UTIME_OMIT;
2911 }
2912 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2913 if (err < 0) {
2914 goto out;
2915 }
2916 }
2917 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2918 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2919 if (err < 0) {
2920 goto out;
2921 }
2922 }
2923 if (v9stat.name.size != 0) {
2924 v9fs_path_write_lock(s);
2925 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2926 v9fs_path_unlock(s);
2927 if (err < 0) {
2928 goto out;
2929 }
2930 }
2931 if (v9stat.length != -1) {
2932 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2933 if (err < 0) {
2934 goto out;
2935 }
2936 }
2937 err = offset;
2938 out:
2939 put_fid(pdu, fidp);
2940 out_nofid:
2941 v9fs_stat_free(&v9stat);
2942 pdu_complete(pdu, err);
2943 }
2944
2945 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2946 {
2947 uint32_t f_type;
2948 uint32_t f_bsize;
2949 uint64_t f_blocks;
2950 uint64_t f_bfree;
2951 uint64_t f_bavail;
2952 uint64_t f_files;
2953 uint64_t f_ffree;
2954 uint64_t fsid_val;
2955 uint32_t f_namelen;
2956 size_t offset = 7;
2957 int32_t bsize_factor;
2958
2959 /*
2960 * compute bsize factor based on host file system block size
2961 * and client msize
2962 */
2963 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2964 if (!bsize_factor) {
2965 bsize_factor = 1;
2966 }
2967 f_type = stbuf->f_type;
2968 f_bsize = stbuf->f_bsize;
2969 f_bsize *= bsize_factor;
2970 /*
2971 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2972 * adjust(divide) the number of blocks, free blocks and available
2973 * blocks by bsize factor
2974 */
2975 f_blocks = stbuf->f_blocks/bsize_factor;
2976 f_bfree = stbuf->f_bfree/bsize_factor;
2977 f_bavail = stbuf->f_bavail/bsize_factor;
2978 f_files = stbuf->f_files;
2979 f_ffree = stbuf->f_ffree;
2980 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2981 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2982 f_namelen = stbuf->f_namelen;
2983
2984 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2985 f_type, f_bsize, f_blocks, f_bfree,
2986 f_bavail, f_files, f_ffree,
2987 fsid_val, f_namelen);
2988 }
2989
2990 static void coroutine_fn v9fs_statfs(void *opaque)
2991 {
2992 int32_t fid;
2993 ssize_t retval = 0;
2994 size_t offset = 7;
2995 V9fsFidState *fidp;
2996 struct statfs stbuf;
2997 V9fsPDU *pdu = opaque;
2998 V9fsState *s = pdu->s;
2999
3000 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3001 if (retval < 0) {
3002 goto out_nofid;
3003 }
3004 fidp = get_fid(pdu, fid);
3005 if (fidp == NULL) {
3006 retval = -ENOENT;
3007 goto out_nofid;
3008 }
3009 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3010 if (retval < 0) {
3011 goto out;
3012 }
3013 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3014 if (retval < 0) {
3015 goto out;
3016 }
3017 retval += offset;
3018 out:
3019 put_fid(pdu, fidp);
3020 out_nofid:
3021 pdu_complete(pdu, retval);
3022 }
3023
3024 static void coroutine_fn v9fs_mknod(void *opaque)
3025 {
3026
3027 int mode;
3028 gid_t gid;
3029 int32_t fid;
3030 V9fsQID qid;
3031 int err = 0;
3032 int major, minor;
3033 size_t offset = 7;
3034 V9fsString name;
3035 struct stat stbuf;
3036 V9fsFidState *fidp;
3037 V9fsPDU *pdu = opaque;
3038
3039 v9fs_string_init(&name);
3040 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3041 &major, &minor, &gid);
3042 if (err < 0) {
3043 goto out_nofid;
3044 }
3045 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3046
3047 if (name_is_illegal(name.data)) {
3048 err = -ENOENT;
3049 goto out_nofid;
3050 }
3051
3052 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3053 err = -EEXIST;
3054 goto out_nofid;
3055 }
3056
3057 fidp = get_fid(pdu, fid);
3058 if (fidp == NULL) {
3059 err = -ENOENT;
3060 goto out_nofid;
3061 }
3062 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3063 makedev(major, minor), mode, &stbuf);
3064 if (err < 0) {
3065 goto out;
3066 }
3067 stat_to_qid(&stbuf, &qid);
3068 err = pdu_marshal(pdu, offset, "Q", &qid);
3069 if (err < 0) {
3070 goto out;
3071 }
3072 err += offset;
3073 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3074 qid.type, qid.version, qid.path);
3075 out:
3076 put_fid(pdu, fidp);
3077 out_nofid:
3078 pdu_complete(pdu, err);
3079 v9fs_string_free(&name);
3080 }
3081
3082 /*
3083 * Implement posix byte range locking code
3084 * Server side handling of locking code is very simple, because 9p server in
3085 * QEMU can handle only one client. And most of the lock handling
3086 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3087 * do any thing in * qemu 9p server side lock code path.
3088 * So when a TLOCK request comes, always return success
3089 */
3090 static void coroutine_fn v9fs_lock(void *opaque)
3091 {
3092 V9fsFlock flock;
3093 size_t offset = 7;
3094 struct stat stbuf;
3095 V9fsFidState *fidp;
3096 int32_t fid, err = 0;
3097 V9fsPDU *pdu = opaque;
3098
3099 v9fs_string_init(&flock.client_id);
3100 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3101 &flock.flags, &flock.start, &flock.length,
3102 &flock.proc_id, &flock.client_id);
3103 if (err < 0) {
3104 goto out_nofid;
3105 }
3106 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3107 flock.type, flock.start, flock.length);
3108
3109
3110 /* We support only block flag now (that too ignored currently) */
3111 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3112 err = -EINVAL;
3113 goto out_nofid;
3114 }
3115 fidp = get_fid(pdu, fid);
3116 if (fidp == NULL) {
3117 err = -ENOENT;
3118 goto out_nofid;
3119 }
3120 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3121 if (err < 0) {
3122 goto out;
3123 }
3124 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3125 if (err < 0) {
3126 goto out;
3127 }
3128 err += offset;
3129 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3130 out:
3131 put_fid(pdu, fidp);
3132 out_nofid:
3133 pdu_complete(pdu, err);
3134 v9fs_string_free(&flock.client_id);
3135 }
3136
3137 /*
3138 * When a TGETLOCK request comes, always return success because all lock
3139 * handling is done by client's VFS layer.
3140 */
3141 static void coroutine_fn v9fs_getlock(void *opaque)
3142 {
3143 size_t offset = 7;
3144 struct stat stbuf;
3145 V9fsFidState *fidp;
3146 V9fsGetlock glock;
3147 int32_t fid, err = 0;
3148 V9fsPDU *pdu = opaque;
3149
3150 v9fs_string_init(&glock.client_id);
3151 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3152 &glock.start, &glock.length, &glock.proc_id,
3153 &glock.client_id);
3154 if (err < 0) {
3155 goto out_nofid;
3156 }
3157 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3158 glock.type, glock.start, glock.length);
3159
3160 fidp = get_fid(pdu, fid);
3161 if (fidp == NULL) {
3162 err = -ENOENT;
3163 goto out_nofid;
3164 }
3165 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3166 if (err < 0) {
3167 goto out;
3168 }
3169 glock.type = P9_LOCK_TYPE_UNLCK;
3170 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3171 glock.start, glock.length, glock.proc_id,
3172 &glock.client_id);
3173 if (err < 0) {
3174 goto out;
3175 }
3176 err += offset;
3177 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3178 glock.length, glock.proc_id);
3179 out:
3180 put_fid(pdu, fidp);
3181 out_nofid:
3182 pdu_complete(pdu, err);
3183 v9fs_string_free(&glock.client_id);
3184 }
3185
3186 static void coroutine_fn v9fs_mkdir(void *opaque)
3187 {
3188 V9fsPDU *pdu = opaque;
3189 size_t offset = 7;
3190 int32_t fid;
3191 struct stat stbuf;
3192 V9fsQID qid;
3193 V9fsString name;
3194 V9fsFidState *fidp;
3195 gid_t gid;
3196 int mode;
3197 int err = 0;
3198
3199 v9fs_string_init(&name);
3200 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3201 if (err < 0) {
3202 goto out_nofid;
3203 }
3204 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3205
3206 if (name_is_illegal(name.data)) {
3207 err = -ENOENT;
3208 goto out_nofid;
3209 }
3210
3211 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3212 err = -EEXIST;
3213 goto out_nofid;
3214 }
3215
3216 fidp = get_fid(pdu, fid);
3217 if (fidp == NULL) {
3218 err = -ENOENT;
3219 goto out_nofid;
3220 }
3221 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3222 if (err < 0) {
3223 goto out;
3224 }
3225 stat_to_qid(&stbuf, &qid);
3226 err = pdu_marshal(pdu, offset, "Q", &qid);
3227 if (err < 0) {
3228 goto out;
3229 }
3230 err += offset;
3231 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3232 qid.type, qid.version, qid.path, err);
3233 out:
3234 put_fid(pdu, fidp);
3235 out_nofid:
3236 pdu_complete(pdu, err);
3237 v9fs_string_free(&name);
3238 }
3239
3240 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3241 {
3242 int64_t size;
3243 V9fsString name;
3244 ssize_t err = 0;
3245 size_t offset = 7;
3246 int32_t fid, newfid;
3247 V9fsFidState *file_fidp;
3248 V9fsFidState *xattr_fidp = NULL;
3249 V9fsPDU *pdu = opaque;
3250 V9fsState *s = pdu->s;
3251
3252 v9fs_string_init(&name);
3253 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3254 if (err < 0) {
3255 goto out_nofid;
3256 }
3257 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3258
3259 file_fidp = get_fid(pdu, fid);
3260 if (file_fidp == NULL) {
3261 err = -ENOENT;
3262 goto out_nofid;
3263 }
3264 xattr_fidp = alloc_fid(s, newfid);
3265 if (xattr_fidp == NULL) {
3266 err = -EINVAL;
3267 goto out;
3268 }
3269 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3270 if (!v9fs_string_size(&name)) {
3271 /*
3272 * listxattr request. Get the size first
3273 */
3274 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3275 if (size < 0) {
3276 err = size;
3277 clunk_fid(s, xattr_fidp->fid);
3278 goto out;
3279 }
3280 /*
3281 * Read the xattr value
3282 */
3283 xattr_fidp->fs.xattr.len = size;
3284 xattr_fidp->fid_type = P9_FID_XATTR;
3285 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3286 xattr_fidp->fs.xattr.value = g_malloc0(size);
3287 if (size) {
3288 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3289 xattr_fidp->fs.xattr.value,
3290 xattr_fidp->fs.xattr.len);
3291 if (err < 0) {
3292 clunk_fid(s, xattr_fidp->fid);
3293 goto out;
3294 }
3295 }
3296 err = pdu_marshal(pdu, offset, "q", size);
3297 if (err < 0) {
3298 goto out;
3299 }
3300 err += offset;
3301 } else {
3302 /*
3303 * specific xattr fid. We check for xattr
3304 * presence also collect the xattr size
3305 */
3306 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3307 &name, NULL, 0);
3308 if (size < 0) {
3309 err = size;
3310 clunk_fid(s, xattr_fidp->fid);
3311 goto out;
3312 }
3313 /*
3314 * Read the xattr value
3315 */
3316 xattr_fidp->fs.xattr.len = size;
3317 xattr_fidp->fid_type = P9_FID_XATTR;
3318 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3319 xattr_fidp->fs.xattr.value = g_malloc0(size);
3320 if (size) {
3321 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3322 &name, xattr_fidp->fs.xattr.value,
3323 xattr_fidp->fs.xattr.len);
3324 if (err < 0) {
3325 clunk_fid(s, xattr_fidp->fid);
3326 goto out;
3327 }
3328 }
3329 err = pdu_marshal(pdu, offset, "q", size);
3330 if (err < 0) {
3331 goto out;
3332 }
3333 err += offset;
3334 }
3335 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3336 out:
3337 put_fid(pdu, file_fidp);
3338 if (xattr_fidp) {
3339 put_fid(pdu, xattr_fidp);
3340 }
3341 out_nofid:
3342 pdu_complete(pdu, err);
3343 v9fs_string_free(&name);
3344 }
3345
3346 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3347 {
3348 int flags, rflags = 0;
3349 int32_t fid;
3350 uint64_t size;
3351 ssize_t err = 0;
3352 V9fsString name;
3353 size_t offset = 7;
3354 V9fsFidState *file_fidp;
3355 V9fsFidState *xattr_fidp;
3356 V9fsPDU *pdu = opaque;
3357
3358 v9fs_string_init(&name);
3359 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3360 if (err < 0) {
3361 goto out_nofid;
3362 }
3363 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3364
3365 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3366 err = -EINVAL;
3367 goto out_nofid;
3368 }
3369
3370 if (flags & P9_XATTR_CREATE) {
3371 rflags |= XATTR_CREATE;
3372 }
3373
3374 if (flags & P9_XATTR_REPLACE) {
3375 rflags |= XATTR_REPLACE;
3376 }
3377
3378 if (size > XATTR_SIZE_MAX) {
3379 err = -E2BIG;
3380 goto out_nofid;
3381 }
3382
3383 file_fidp = get_fid(pdu, fid);
3384 if (file_fidp == NULL) {
3385 err = -EINVAL;
3386 goto out_nofid;
3387 }
3388 if (file_fidp->fid_type != P9_FID_NONE) {
3389 err = -EINVAL;
3390 goto out_put_fid;
3391 }
3392
3393 /* Make the file fid point to xattr */
3394 xattr_fidp = file_fidp;
3395 xattr_fidp->fid_type = P9_FID_XATTR;
3396 xattr_fidp->fs.xattr.copied_len = 0;
3397 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3398 xattr_fidp->fs.xattr.len = size;
3399 xattr_fidp->fs.xattr.flags = rflags;
3400 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3401 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3402 xattr_fidp->fs.xattr.value = g_malloc0(size);
3403 err = offset;
3404 out_put_fid:
3405 put_fid(pdu, file_fidp);
3406 out_nofid:
3407 pdu_complete(pdu, err);
3408 v9fs_string_free(&name);
3409 }
3410
3411 static void coroutine_fn v9fs_readlink(void *opaque)
3412 {
3413 V9fsPDU *pdu = opaque;
3