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