]> git.proxmox.com Git - mirror_qemu.git/blob - hw/9pfs/9p.c
9pfs: forbid illegal path names
[mirror_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 trace_v9fs_attach_return(pdu->tag, pdu->id,
1014 qid.type, qid.version, qid.path);
1015 /*
1016 * disable migration if we haven't done already.
1017 * attach could get called multiple times for the same export.
1018 */
1019 if (!s->migration_blocker) {
1020 s->root_fid = fid;
1021 error_setg(&s->migration_blocker,
1022 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1023 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1024 migrate_add_blocker(s->migration_blocker);
1025 }
1026 out:
1027 put_fid(pdu, fidp);
1028 out_nofid:
1029 pdu_complete(pdu, err);
1030 v9fs_string_free(&uname);
1031 v9fs_string_free(&aname);
1032 }
1033
1034 static void v9fs_stat(void *opaque)
1035 {
1036 int32_t fid;
1037 V9fsStat v9stat;
1038 ssize_t err = 0;
1039 size_t offset = 7;
1040 struct stat stbuf;
1041 V9fsFidState *fidp;
1042 V9fsPDU *pdu = opaque;
1043
1044 err = pdu_unmarshal(pdu, offset, "d", &fid);
1045 if (err < 0) {
1046 goto out_nofid;
1047 }
1048 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1049
1050 fidp = get_fid(pdu, fid);
1051 if (fidp == NULL) {
1052 err = -ENOENT;
1053 goto out_nofid;
1054 }
1055 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1056 if (err < 0) {
1057 goto out;
1058 }
1059 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1060 if (err < 0) {
1061 goto out;
1062 }
1063 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1064 if (err < 0) {
1065 v9fs_stat_free(&v9stat);
1066 goto out;
1067 }
1068 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1069 v9stat.atime, v9stat.mtime, v9stat.length);
1070 err += offset;
1071 v9fs_stat_free(&v9stat);
1072 out:
1073 put_fid(pdu, fidp);
1074 out_nofid:
1075 pdu_complete(pdu, err);
1076 }
1077
1078 static void v9fs_getattr(void *opaque)
1079 {
1080 int32_t fid;
1081 size_t offset = 7;
1082 ssize_t retval = 0;
1083 struct stat stbuf;
1084 V9fsFidState *fidp;
1085 uint64_t request_mask;
1086 V9fsStatDotl v9stat_dotl;
1087 V9fsPDU *pdu = opaque;
1088 V9fsState *s = pdu->s;
1089
1090 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1091 if (retval < 0) {
1092 goto out_nofid;
1093 }
1094 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1095
1096 fidp = get_fid(pdu, fid);
1097 if (fidp == NULL) {
1098 retval = -ENOENT;
1099 goto out_nofid;
1100 }
1101 /*
1102 * Currently we only support BASIC fields in stat, so there is no
1103 * need to look at request_mask.
1104 */
1105 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1106 if (retval < 0) {
1107 goto out;
1108 }
1109 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1110
1111 /* fill st_gen if requested and supported by underlying fs */
1112 if (request_mask & P9_STATS_GEN) {
1113 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1114 switch (retval) {
1115 case 0:
1116 /* we have valid st_gen: update result mask */
1117 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1118 break;
1119 case -EINTR:
1120 /* request cancelled, e.g. by Tflush */
1121 goto out;
1122 default:
1123 /* failed to get st_gen: not fatal, ignore */
1124 break;
1125 }
1126 }
1127 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1128 if (retval < 0) {
1129 goto out;
1130 }
1131 retval += offset;
1132 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1133 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1134 v9stat_dotl.st_gid);
1135 out:
1136 put_fid(pdu, fidp);
1137 out_nofid:
1138 pdu_complete(pdu, retval);
1139 }
1140
1141 /* Attribute flags */
1142 #define P9_ATTR_MODE (1 << 0)
1143 #define P9_ATTR_UID (1 << 1)
1144 #define P9_ATTR_GID (1 << 2)
1145 #define P9_ATTR_SIZE (1 << 3)
1146 #define P9_ATTR_ATIME (1 << 4)
1147 #define P9_ATTR_MTIME (1 << 5)
1148 #define P9_ATTR_CTIME (1 << 6)
1149 #define P9_ATTR_ATIME_SET (1 << 7)
1150 #define P9_ATTR_MTIME_SET (1 << 8)
1151
1152 #define P9_ATTR_MASK 127
1153
1154 static void v9fs_setattr(void *opaque)
1155 {
1156 int err = 0;
1157 int32_t fid;
1158 V9fsFidState *fidp;
1159 size_t offset = 7;
1160 V9fsIattr v9iattr;
1161 V9fsPDU *pdu = opaque;
1162
1163 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1164 if (err < 0) {
1165 goto out_nofid;
1166 }
1167
1168 fidp = get_fid(pdu, fid);
1169 if (fidp == NULL) {
1170 err = -EINVAL;
1171 goto out_nofid;
1172 }
1173 if (v9iattr.valid & P9_ATTR_MODE) {
1174 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1175 if (err < 0) {
1176 goto out;
1177 }
1178 }
1179 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1180 struct timespec times[2];
1181 if (v9iattr.valid & P9_ATTR_ATIME) {
1182 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1183 times[0].tv_sec = v9iattr.atime_sec;
1184 times[0].tv_nsec = v9iattr.atime_nsec;
1185 } else {
1186 times[0].tv_nsec = UTIME_NOW;
1187 }
1188 } else {
1189 times[0].tv_nsec = UTIME_OMIT;
1190 }
1191 if (v9iattr.valid & P9_ATTR_MTIME) {
1192 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1193 times[1].tv_sec = v9iattr.mtime_sec;
1194 times[1].tv_nsec = v9iattr.mtime_nsec;
1195 } else {
1196 times[1].tv_nsec = UTIME_NOW;
1197 }
1198 } else {
1199 times[1].tv_nsec = UTIME_OMIT;
1200 }
1201 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1202 if (err < 0) {
1203 goto out;
1204 }
1205 }
1206 /*
1207 * If the only valid entry in iattr is ctime we can call
1208 * chown(-1,-1) to update the ctime of the file
1209 */
1210 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1211 ((v9iattr.valid & P9_ATTR_CTIME)
1212 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1213 if (!(v9iattr.valid & P9_ATTR_UID)) {
1214 v9iattr.uid = -1;
1215 }
1216 if (!(v9iattr.valid & P9_ATTR_GID)) {
1217 v9iattr.gid = -1;
1218 }
1219 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1220 v9iattr.gid);
1221 if (err < 0) {
1222 goto out;
1223 }
1224 }
1225 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1226 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1227 if (err < 0) {
1228 goto out;
1229 }
1230 }
1231 err = offset;
1232 out:
1233 put_fid(pdu, fidp);
1234 out_nofid:
1235 pdu_complete(pdu, err);
1236 }
1237
1238 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1239 {
1240 int i;
1241 ssize_t err;
1242 size_t offset = 7;
1243
1244 err = pdu_marshal(pdu, offset, "w", nwnames);
1245 if (err < 0) {
1246 return err;
1247 }
1248 offset += err;
1249 for (i = 0; i < nwnames; i++) {
1250 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1251 if (err < 0) {
1252 return err;
1253 }
1254 offset += err;
1255 }
1256 return offset;
1257 }
1258
1259 static bool name_is_illegal(const char *name)
1260 {
1261 return !*name || strchr(name, '/') != NULL;
1262 }
1263
1264 static void v9fs_walk(void *opaque)
1265 {
1266 int name_idx;
1267 V9fsQID *qids = NULL;
1268 int i, err = 0;
1269 V9fsPath dpath, path;
1270 uint16_t nwnames;
1271 struct stat stbuf;
1272 size_t offset = 7;
1273 int32_t fid, newfid;
1274 V9fsString *wnames = NULL;
1275 V9fsFidState *fidp;
1276 V9fsFidState *newfidp = NULL;
1277 V9fsPDU *pdu = opaque;
1278 V9fsState *s = pdu->s;
1279
1280 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1281 if (err < 0) {
1282 pdu_complete(pdu, err);
1283 return ;
1284 }
1285 offset += err;
1286
1287 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1288
1289 if (nwnames && nwnames <= P9_MAXWELEM) {
1290 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1291 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1292 for (i = 0; i < nwnames; i++) {
1293 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1294 if (err < 0) {
1295 goto out_nofid;
1296 }
1297 if (name_is_illegal(wnames[i].data)) {
1298 err = -ENOENT;
1299 goto out_nofid;
1300 }
1301 offset += err;
1302 }
1303 } else if (nwnames > P9_MAXWELEM) {
1304 err = -EINVAL;
1305 goto out_nofid;
1306 }
1307 fidp = get_fid(pdu, fid);
1308 if (fidp == NULL) {
1309 err = -ENOENT;
1310 goto out_nofid;
1311 }
1312 v9fs_path_init(&dpath);
1313 v9fs_path_init(&path);
1314 /*
1315 * Both dpath and path initially poin to fidp.
1316 * Needed to handle request with nwnames == 0
1317 */
1318 v9fs_path_copy(&dpath, &fidp->path);
1319 v9fs_path_copy(&path, &fidp->path);
1320 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1321 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
1322 if (err < 0) {
1323 goto out;
1324 }
1325 err = v9fs_co_lstat(pdu, &path, &stbuf);
1326 if (err < 0) {
1327 goto out;
1328 }
1329 stat_to_qid(&stbuf, &qids[name_idx]);
1330 v9fs_path_copy(&dpath, &path);
1331 }
1332 if (fid == newfid) {
1333 BUG_ON(fidp->fid_type != P9_FID_NONE);
1334 v9fs_path_copy(&fidp->path, &path);
1335 } else {
1336 newfidp = alloc_fid(s, newfid);
1337 if (newfidp == NULL) {
1338 err = -EINVAL;
1339 goto out;
1340 }
1341 newfidp->uid = fidp->uid;
1342 v9fs_path_copy(&newfidp->path, &path);
1343 }
1344 err = v9fs_walk_marshal(pdu, nwnames, qids);
1345 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1346 out:
1347 put_fid(pdu, fidp);
1348 if (newfidp) {
1349 put_fid(pdu, newfidp);
1350 }
1351 v9fs_path_free(&dpath);
1352 v9fs_path_free(&path);
1353 out_nofid:
1354 pdu_complete(pdu, err);
1355 if (nwnames && nwnames <= P9_MAXWELEM) {
1356 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1357 v9fs_string_free(&wnames[name_idx]);
1358 }
1359 g_free(wnames);
1360 g_free(qids);
1361 }
1362 }
1363
1364 static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1365 {
1366 struct statfs stbuf;
1367 int32_t iounit = 0;
1368 V9fsState *s = pdu->s;
1369
1370 /*
1371 * iounit should be multiples of f_bsize (host filesystem block size
1372 * and as well as less than (client msize - P9_IOHDRSZ))
1373 */
1374 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1375 iounit = stbuf.f_bsize;
1376 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1377 }
1378 if (!iounit) {
1379 iounit = s->msize - P9_IOHDRSZ;
1380 }
1381 return iounit;
1382 }
1383
1384 static void v9fs_open(void *opaque)
1385 {
1386 int flags;
1387 int32_t fid;
1388 int32_t mode;
1389 V9fsQID qid;
1390 int iounit = 0;
1391 ssize_t err = 0;
1392 size_t offset = 7;
1393 struct stat stbuf;
1394 V9fsFidState *fidp;
1395 V9fsPDU *pdu = opaque;
1396 V9fsState *s = pdu->s;
1397
1398 if (s->proto_version == V9FS_PROTO_2000L) {
1399 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1400 } else {
1401 uint8_t modebyte;
1402 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1403 mode = modebyte;
1404 }
1405 if (err < 0) {
1406 goto out_nofid;
1407 }
1408 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1409
1410 fidp = get_fid(pdu, fid);
1411 if (fidp == NULL) {
1412 err = -ENOENT;
1413 goto out_nofid;
1414 }
1415 BUG_ON(fidp->fid_type != P9_FID_NONE);
1416
1417 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1418 if (err < 0) {
1419 goto out;
1420 }
1421 stat_to_qid(&stbuf, &qid);
1422 if (S_ISDIR(stbuf.st_mode)) {
1423 err = v9fs_co_opendir(pdu, fidp);
1424 if (err < 0) {
1425 goto out;
1426 }
1427 fidp->fid_type = P9_FID_DIR;
1428 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1429 if (err < 0) {
1430 goto out;
1431 }
1432 err += offset;
1433 } else {
1434 if (s->proto_version == V9FS_PROTO_2000L) {
1435 flags = get_dotl_openflags(s, mode);
1436 } else {
1437 flags = omode_to_uflags(mode);
1438 }
1439 if (is_ro_export(&s->ctx)) {
1440 if (mode & O_WRONLY || mode & O_RDWR ||
1441 mode & O_APPEND || mode & O_TRUNC) {
1442 err = -EROFS;
1443 goto out;
1444 }
1445 }
1446 err = v9fs_co_open(pdu, fidp, flags);
1447 if (err < 0) {
1448 goto out;
1449 }
1450 fidp->fid_type = P9_FID_FILE;
1451 fidp->open_flags = flags;
1452 if (flags & O_EXCL) {
1453 /*
1454 * We let the host file system do O_EXCL check
1455 * We should not reclaim such fd
1456 */
1457 fidp->flags |= FID_NON_RECLAIMABLE;
1458 }
1459 iounit = get_iounit(pdu, &fidp->path);
1460 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1461 if (err < 0) {
1462 goto out;
1463 }
1464 err += offset;
1465 }
1466 trace_v9fs_open_return(pdu->tag, pdu->id,
1467 qid.type, qid.version, qid.path, iounit);
1468 out:
1469 put_fid(pdu, fidp);
1470 out_nofid:
1471 pdu_complete(pdu, err);
1472 }
1473
1474 static void v9fs_lcreate(void *opaque)
1475 {
1476 int32_t dfid, flags, mode;
1477 gid_t gid;
1478 ssize_t err = 0;
1479 ssize_t offset = 7;
1480 V9fsString name;
1481 V9fsFidState *fidp;
1482 struct stat stbuf;
1483 V9fsQID qid;
1484 int32_t iounit;
1485 V9fsPDU *pdu = opaque;
1486
1487 v9fs_string_init(&name);
1488 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1489 &name, &flags, &mode, &gid);
1490 if (err < 0) {
1491 goto out_nofid;
1492 }
1493 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1494
1495 if (name_is_illegal(name.data)) {
1496 err = -ENOENT;
1497 goto out_nofid;
1498 }
1499
1500 fidp = get_fid(pdu, dfid);
1501 if (fidp == NULL) {
1502 err = -ENOENT;
1503 goto out_nofid;
1504 }
1505
1506 flags = get_dotl_openflags(pdu->s, flags);
1507 err = v9fs_co_open2(pdu, fidp, &name, gid,
1508 flags | O_CREAT, mode, &stbuf);
1509 if (err < 0) {
1510 goto out;
1511 }
1512 fidp->fid_type = P9_FID_FILE;
1513 fidp->open_flags = flags;
1514 if (flags & O_EXCL) {
1515 /*
1516 * We let the host file system do O_EXCL check
1517 * We should not reclaim such fd
1518 */
1519 fidp->flags |= FID_NON_RECLAIMABLE;
1520 }
1521 iounit = get_iounit(pdu, &fidp->path);
1522 stat_to_qid(&stbuf, &qid);
1523 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1524 if (err < 0) {
1525 goto out;
1526 }
1527 err += offset;
1528 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1529 qid.type, qid.version, qid.path, iounit);
1530 out:
1531 put_fid(pdu, fidp);
1532 out_nofid:
1533 pdu_complete(pdu, err);
1534 v9fs_string_free(&name);
1535 }
1536
1537 static void v9fs_fsync(void *opaque)
1538 {
1539 int err;
1540 int32_t fid;
1541 int datasync;
1542 size_t offset = 7;
1543 V9fsFidState *fidp;
1544 V9fsPDU *pdu = opaque;
1545
1546 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1547 if (err < 0) {
1548 goto out_nofid;
1549 }
1550 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1551
1552 fidp = get_fid(pdu, fid);
1553 if (fidp == NULL) {
1554 err = -ENOENT;
1555 goto out_nofid;
1556 }
1557 err = v9fs_co_fsync(pdu, fidp, datasync);
1558 if (!err) {
1559 err = offset;
1560 }
1561 put_fid(pdu, fidp);
1562 out_nofid:
1563 pdu_complete(pdu, err);
1564 }
1565
1566 static void v9fs_clunk(void *opaque)
1567 {
1568 int err;
1569 int32_t fid;
1570 size_t offset = 7;
1571 V9fsFidState *fidp;
1572 V9fsPDU *pdu = opaque;
1573 V9fsState *s = pdu->s;
1574
1575 err = pdu_unmarshal(pdu, offset, "d", &fid);
1576 if (err < 0) {
1577 goto out_nofid;
1578 }
1579 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1580
1581 fidp = clunk_fid(s, fid);
1582 if (fidp == NULL) {
1583 err = -ENOENT;
1584 goto out_nofid;
1585 }
1586 /*
1587 * Bump the ref so that put_fid will
1588 * free the fid.
1589 */
1590 fidp->ref++;
1591 err = put_fid(pdu, fidp);
1592 if (!err) {
1593 err = offset;
1594 }
1595 out_nofid:
1596 pdu_complete(pdu, err);
1597 }
1598
1599 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1600 uint64_t off, uint32_t max_count)
1601 {
1602 ssize_t err;
1603 size_t offset = 7;
1604 int read_count;
1605 int64_t xattr_len;
1606 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1607 VirtQueueElement *elem = v->elems[pdu->idx];
1608
1609 xattr_len = fidp->fs.xattr.len;
1610 read_count = xattr_len - off;
1611 if (read_count > max_count) {
1612 read_count = max_count;
1613 } else if (read_count < 0) {
1614 /*
1615 * read beyond XATTR value
1616 */
1617 read_count = 0;
1618 }
1619 err = pdu_marshal(pdu, offset, "d", read_count);
1620 if (err < 0) {
1621 return err;
1622 }
1623 offset += err;
1624
1625 err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1626 ((char *)fidp->fs.xattr.value) + off,
1627 read_count);
1628 if (err < 0) {
1629 return err;
1630 }
1631 offset += err;
1632 return offset;
1633 }
1634
1635 static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1636 V9fsFidState *fidp, uint32_t max_count)
1637 {
1638 V9fsPath path;
1639 V9fsStat v9stat;
1640 int len, err = 0;
1641 int32_t count = 0;
1642 struct stat stbuf;
1643 off_t saved_dir_pos;
1644 struct dirent *dent;
1645
1646 /* save the directory position */
1647 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1648 if (saved_dir_pos < 0) {
1649 return saved_dir_pos;
1650 }
1651
1652 while (1) {
1653 v9fs_path_init(&path);
1654
1655 v9fs_readdir_lock(&fidp->fs.dir);
1656
1657 err = v9fs_co_readdir(pdu, fidp, &dent);
1658 if (err || !dent) {
1659 break;
1660 }
1661 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1662 if (err < 0) {
1663 break;
1664 }
1665 err = v9fs_co_lstat(pdu, &path, &stbuf);
1666 if (err < 0) {
1667 break;
1668 }
1669 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1670 if (err < 0) {
1671 break;
1672 }
1673 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1674 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1675
1676 v9fs_readdir_unlock(&fidp->fs.dir);
1677
1678 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1679 /* Ran out of buffer. Set dir back to old position and return */
1680 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1681 v9fs_stat_free(&v9stat);
1682 v9fs_path_free(&path);
1683 return count;
1684 }
1685 count += len;
1686 v9fs_stat_free(&v9stat);
1687 v9fs_path_free(&path);
1688 saved_dir_pos = dent->d_off;
1689 }
1690
1691 v9fs_readdir_unlock(&fidp->fs.dir);
1692
1693 v9fs_path_free(&path);
1694 if (err < 0) {
1695 return err;
1696 }
1697 return count;
1698 }
1699
1700 /*
1701 * Create a QEMUIOVector for a sub-region of PDU iovecs
1702 *
1703 * @qiov: uninitialized QEMUIOVector
1704 * @skip: number of bytes to skip from beginning of PDU
1705 * @size: number of bytes to include
1706 * @is_write: true - write, false - read
1707 *
1708 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1709 * with qemu_iovec_destroy().
1710 */
1711 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1712 size_t skip, size_t size,
1713 bool is_write)
1714 {
1715 QEMUIOVector elem;
1716 struct iovec *iov;
1717 unsigned int niov;
1718
1719 virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1720
1721 qemu_iovec_init_external(&elem, iov, niov);
1722 qemu_iovec_init(qiov, niov);
1723 qemu_iovec_concat(qiov, &elem, skip, size);
1724 }
1725
1726 static void v9fs_read(void *opaque)
1727 {
1728 int32_t fid;
1729 uint64_t off;
1730 ssize_t err = 0;
1731 int32_t count = 0;
1732 size_t offset = 7;
1733 uint32_t max_count;
1734 V9fsFidState *fidp;
1735 V9fsPDU *pdu = opaque;
1736 V9fsState *s = pdu->s;
1737
1738 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1739 if (err < 0) {
1740 goto out_nofid;
1741 }
1742 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1743
1744 fidp = get_fid(pdu, fid);
1745 if (fidp == NULL) {
1746 err = -EINVAL;
1747 goto out_nofid;
1748 }
1749 if (fidp->fid_type == P9_FID_DIR) {
1750
1751 if (off == 0) {
1752 v9fs_co_rewinddir(pdu, fidp);
1753 }
1754 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1755 if (count < 0) {
1756 err = count;
1757 goto out;
1758 }
1759 err = pdu_marshal(pdu, offset, "d", count);
1760 if (err < 0) {
1761 goto out;
1762 }
1763 err += offset + count;
1764 } else if (fidp->fid_type == P9_FID_FILE) {
1765 QEMUIOVector qiov_full;
1766 QEMUIOVector qiov;
1767 int32_t len;
1768
1769 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1770 qemu_iovec_init(&qiov, qiov_full.niov);
1771 do {
1772 qemu_iovec_reset(&qiov);
1773 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1774 if (0) {
1775 print_sg(qiov.iov, qiov.niov);
1776 }
1777 /* Loop in case of EINTR */
1778 do {
1779 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1780 if (len >= 0) {
1781 off += len;
1782 count += len;
1783 }
1784 } while (len == -EINTR && !pdu->cancelled);
1785 if (len < 0) {
1786 /* IO error return the error */
1787 err = len;
1788 goto out;
1789 }
1790 } while (count < max_count && len > 0);
1791 err = pdu_marshal(pdu, offset, "d", count);
1792 if (err < 0) {
1793 goto out;
1794 }
1795 err += offset + count;
1796 qemu_iovec_destroy(&qiov);
1797 qemu_iovec_destroy(&qiov_full);
1798 } else if (fidp->fid_type == P9_FID_XATTR) {
1799 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1800 } else {
1801 err = -EINVAL;
1802 }
1803 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1804 out:
1805 put_fid(pdu, fidp);
1806 out_nofid:
1807 pdu_complete(pdu, err);
1808 }
1809
1810 static size_t v9fs_readdir_data_size(V9fsString *name)
1811 {
1812 /*
1813 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1814 * size of type (1) + size of name.size (2) + strlen(name.data)
1815 */
1816 return 24 + v9fs_string_size(name);
1817 }
1818
1819 static int v9fs_do_readdir(V9fsPDU *pdu,
1820 V9fsFidState *fidp, int32_t max_count)
1821 {
1822 size_t size;
1823 V9fsQID qid;
1824 V9fsString name;
1825 int len, err = 0;
1826 int32_t count = 0;
1827 off_t saved_dir_pos;
1828 struct dirent *dent;
1829
1830 /* save the directory position */
1831 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1832 if (saved_dir_pos < 0) {
1833 return saved_dir_pos;
1834 }
1835
1836 while (1) {
1837 v9fs_readdir_lock(&fidp->fs.dir);
1838
1839 err = v9fs_co_readdir(pdu, fidp, &dent);
1840 if (err || !dent) {
1841 break;
1842 }
1843 v9fs_string_init(&name);
1844 v9fs_string_sprintf(&name, "%s", dent->d_name);
1845 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1846 v9fs_readdir_unlock(&fidp->fs.dir);
1847
1848 /* Ran out of buffer. Set dir back to old position and return */
1849 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1850 v9fs_string_free(&name);
1851 return count;
1852 }
1853 /*
1854 * Fill up just the path field of qid because the client uses
1855 * only that. To fill the entire qid structure we will have
1856 * to stat each dirent found, which is expensive
1857 */
1858 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1859 memcpy(&qid.path, &dent->d_ino, size);
1860 /* Fill the other fields with dummy values */
1861 qid.type = 0;
1862 qid.version = 0;
1863
1864 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1865 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1866 &qid, dent->d_off,
1867 dent->d_type, &name);
1868
1869 v9fs_readdir_unlock(&fidp->fs.dir);
1870
1871 if (len < 0) {
1872 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1873 v9fs_string_free(&name);
1874 return len;
1875 }
1876 count += len;
1877 v9fs_string_free(&name);
1878 saved_dir_pos = dent->d_off;
1879 }
1880
1881 v9fs_readdir_unlock(&fidp->fs.dir);
1882
1883 if (err < 0) {
1884 return err;
1885 }
1886 return count;
1887 }
1888
1889 static void v9fs_readdir(void *opaque)
1890 {
1891 int32_t fid;
1892 V9fsFidState *fidp;
1893 ssize_t retval = 0;
1894 size_t offset = 7;
1895 uint64_t initial_offset;
1896 int32_t count;
1897 uint32_t max_count;
1898 V9fsPDU *pdu = opaque;
1899
1900 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1901 &initial_offset, &max_count);
1902 if (retval < 0) {
1903 goto out_nofid;
1904 }
1905 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1906
1907 fidp = get_fid(pdu, fid);
1908 if (fidp == NULL) {
1909 retval = -EINVAL;
1910 goto out_nofid;
1911 }
1912 if (!fidp->fs.dir.stream) {
1913 retval = -EINVAL;
1914 goto out;
1915 }
1916 if (initial_offset == 0) {
1917 v9fs_co_rewinddir(pdu, fidp);
1918 } else {
1919 v9fs_co_seekdir(pdu, fidp, initial_offset);
1920 }
1921 count = v9fs_do_readdir(pdu, fidp, max_count);
1922 if (count < 0) {
1923 retval = count;
1924 goto out;
1925 }
1926 retval = pdu_marshal(pdu, offset, "d", count);
1927 if (retval < 0) {
1928 goto out;
1929 }
1930 retval += count + offset;
1931 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1932 out:
1933 put_fid(pdu, fidp);
1934 out_nofid:
1935 pdu_complete(pdu, retval);
1936 }
1937
1938 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1939 uint64_t off, uint32_t count,
1940 struct iovec *sg, int cnt)
1941 {
1942 int i, to_copy;
1943 ssize_t err = 0;
1944 int write_count;
1945 int64_t xattr_len;
1946 size_t offset = 7;
1947
1948
1949 xattr_len = fidp->fs.xattr.len;
1950 write_count = xattr_len - off;
1951 if (write_count > count) {
1952 write_count = count;
1953 } else if (write_count < 0) {
1954 /*
1955 * write beyond XATTR value len specified in
1956 * xattrcreate
1957 */
1958 err = -ENOSPC;
1959 goto out;
1960 }
1961 err = pdu_marshal(pdu, offset, "d", write_count);
1962 if (err < 0) {
1963 return err;
1964 }
1965 err += offset;
1966 fidp->fs.xattr.copied_len += write_count;
1967 /*
1968 * Now copy the content from sg list
1969 */
1970 for (i = 0; i < cnt; i++) {
1971 if (write_count > sg[i].iov_len) {
1972 to_copy = sg[i].iov_len;
1973 } else {
1974 to_copy = write_count;
1975 }
1976 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1977 /* updating vs->off since we are not using below */
1978 off += to_copy;
1979 write_count -= to_copy;
1980 }
1981 out:
1982 return err;
1983 }
1984
1985 static void v9fs_write(void *opaque)
1986 {
1987 ssize_t err;
1988 int32_t fid;
1989 uint64_t off;
1990 uint32_t count;
1991 int32_t len = 0;
1992 int32_t total = 0;
1993 size_t offset = 7;
1994 V9fsFidState *fidp;
1995 V9fsPDU *pdu = opaque;
1996 V9fsState *s = pdu->s;
1997 QEMUIOVector qiov_full;
1998 QEMUIOVector qiov;
1999
2000 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2001 if (err < 0) {
2002 pdu_complete(pdu, err);
2003 return;
2004 }
2005 offset += err;
2006 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2007 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2008
2009 fidp = get_fid(pdu, fid);
2010 if (fidp == NULL) {
2011 err = -EINVAL;
2012 goto out_nofid;
2013 }
2014 if (fidp->fid_type == P9_FID_FILE) {
2015 if (fidp->fs.fd == -1) {
2016 err = -EINVAL;
2017 goto out;
2018 }
2019 } else if (fidp->fid_type == P9_FID_XATTR) {
2020 /*
2021 * setxattr operation
2022 */
2023 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2024 qiov_full.iov, qiov_full.niov);
2025 goto out;
2026 } else {
2027 err = -EINVAL;
2028 goto out;
2029 }
2030 qemu_iovec_init(&qiov, qiov_full.niov);
2031 do {
2032 qemu_iovec_reset(&qiov);
2033 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2034 if (0) {
2035 print_sg(qiov.iov, qiov.niov);
2036 }
2037 /* Loop in case of EINTR */
2038 do {
2039 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2040 if (len >= 0) {
2041 off += len;
2042 total += len;
2043 }
2044 } while (len == -EINTR && !pdu->cancelled);
2045 if (len < 0) {
2046 /* IO error return the error */
2047 err = len;
2048 goto out_qiov;
2049 }
2050 } while (total < count && len > 0);
2051
2052 offset = 7;
2053 err = pdu_marshal(pdu, offset, "d", total);
2054 if (err < 0) {
2055 goto out;
2056 }
2057 err += offset;
2058 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2059 out_qiov:
2060 qemu_iovec_destroy(&qiov);
2061 out:
2062 put_fid(pdu, fidp);
2063 out_nofid:
2064 qemu_iovec_destroy(&qiov_full);
2065 pdu_complete(pdu, err);
2066 }
2067
2068 static void v9fs_create(void *opaque)
2069 {
2070 int32_t fid;
2071 int err = 0;
2072 size_t offset = 7;
2073 V9fsFidState *fidp;
2074 V9fsQID qid;
2075 int32_t perm;
2076 int8_t mode;
2077 V9fsPath path;
2078 struct stat stbuf;
2079 V9fsString name;
2080 V9fsString extension;
2081 int iounit;
2082 V9fsPDU *pdu = opaque;
2083
2084 v9fs_path_init(&path);
2085 v9fs_string_init(&name);
2086 v9fs_string_init(&extension);
2087 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2088 &perm, &mode, &extension);
2089 if (err < 0) {
2090 goto out_nofid;
2091 }
2092 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2093
2094 if (name_is_illegal(name.data)) {
2095 err = -ENOENT;
2096 goto out_nofid;
2097 }
2098
2099 fidp = get_fid(pdu, fid);
2100 if (fidp == NULL) {
2101 err = -EINVAL;
2102 goto out_nofid;
2103 }
2104 if (perm & P9_STAT_MODE_DIR) {
2105 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2106 fidp->uid, -1, &stbuf);
2107 if (err < 0) {
2108 goto out;
2109 }
2110 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2111 if (err < 0) {
2112 goto out;
2113 }
2114 v9fs_path_copy(&fidp->path, &path);
2115 err = v9fs_co_opendir(pdu, fidp);
2116 if (err < 0) {
2117 goto out;
2118 }
2119 fidp->fid_type = P9_FID_DIR;
2120 } else if (perm & P9_STAT_MODE_SYMLINK) {
2121 err = v9fs_co_symlink(pdu, fidp, &name,
2122 extension.data, -1 , &stbuf);
2123 if (err < 0) {
2124 goto out;
2125 }
2126 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2127 if (err < 0) {
2128 goto out;
2129 }
2130 v9fs_path_copy(&fidp->path, &path);
2131 } else if (perm & P9_STAT_MODE_LINK) {
2132 int32_t ofid = atoi(extension.data);
2133 V9fsFidState *ofidp = get_fid(pdu, ofid);
2134 if (ofidp == NULL) {
2135 err = -EINVAL;
2136 goto out;
2137 }
2138 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2139 put_fid(pdu, ofidp);
2140 if (err < 0) {
2141 goto out;
2142 }
2143 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2144 if (err < 0) {
2145 fidp->fid_type = P9_FID_NONE;
2146 goto out;
2147 }
2148 v9fs_path_copy(&fidp->path, &path);
2149 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2150 if (err < 0) {
2151 fidp->fid_type = P9_FID_NONE;
2152 goto out;
2153 }
2154 } else if (perm & P9_STAT_MODE_DEVICE) {
2155 char ctype;
2156 uint32_t major, minor;
2157 mode_t nmode = 0;
2158
2159 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2160 err = -errno;
2161 goto out;
2162 }
2163
2164 switch (ctype) {
2165 case 'c':
2166 nmode = S_IFCHR;
2167 break;
2168 case 'b':
2169 nmode = S_IFBLK;
2170 break;
2171 default:
2172 err = -EIO;
2173 goto out;
2174 }
2175
2176 nmode |= perm & 0777;
2177 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2178 makedev(major, minor), nmode, &stbuf);
2179 if (err < 0) {
2180 goto out;
2181 }
2182 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2183 if (err < 0) {
2184 goto out;
2185 }
2186 v9fs_path_copy(&fidp->path, &path);
2187 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2188 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2189 0, S_IFIFO | (perm & 0777), &stbuf);
2190 if (err < 0) {
2191 goto out;
2192 }
2193 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2194 if (err < 0) {
2195 goto out;
2196 }
2197 v9fs_path_copy(&fidp->path, &path);
2198 } else if (perm & P9_STAT_MODE_SOCKET) {
2199 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2200 0, S_IFSOCK | (perm & 0777), &stbuf);
2201 if (err < 0) {
2202 goto out;
2203 }
2204 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2205 if (err < 0) {
2206 goto out;
2207 }
2208 v9fs_path_copy(&fidp->path, &path);
2209 } else {
2210 err = v9fs_co_open2(pdu, fidp, &name, -1,
2211 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2212 if (err < 0) {
2213 goto out;
2214 }
2215 fidp->fid_type = P9_FID_FILE;
2216 fidp->open_flags = omode_to_uflags(mode);
2217 if (fidp->open_flags & O_EXCL) {
2218 /*
2219 * We let the host file system do O_EXCL check
2220 * We should not reclaim such fd
2221 */
2222 fidp->flags |= FID_NON_RECLAIMABLE;
2223 }
2224 }
2225 iounit = get_iounit(pdu, &fidp->path);
2226 stat_to_qid(&stbuf, &qid);
2227 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2228 if (err < 0) {
2229 goto out;
2230 }
2231 err += offset;
2232 trace_v9fs_create_return(pdu->tag, pdu->id,
2233 qid.type, qid.version, qid.path, iounit);
2234 out:
2235 put_fid(pdu, fidp);
2236 out_nofid:
2237 pdu_complete(pdu, err);
2238 v9fs_string_free(&name);
2239 v9fs_string_free(&extension);
2240 v9fs_path_free(&path);
2241 }
2242
2243 static void v9fs_symlink(void *opaque)
2244 {
2245 V9fsPDU *pdu = opaque;
2246 V9fsString name;
2247 V9fsString symname;
2248 V9fsFidState *dfidp;
2249 V9fsQID qid;
2250 struct stat stbuf;
2251 int32_t dfid;
2252 int err = 0;
2253 gid_t gid;
2254 size_t offset = 7;
2255
2256 v9fs_string_init(&name);
2257 v9fs_string_init(&symname);
2258 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2259 if (err < 0) {
2260 goto out_nofid;
2261 }
2262 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2263
2264 if (name_is_illegal(name.data)) {
2265 err = -ENOENT;
2266 goto out_nofid;
2267 }
2268
2269 dfidp = get_fid(pdu, dfid);
2270 if (dfidp == NULL) {
2271 err = -EINVAL;
2272 goto out_nofid;
2273 }
2274 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2275 if (err < 0) {
2276 goto out;
2277 }
2278 stat_to_qid(&stbuf, &qid);
2279 err = pdu_marshal(pdu, offset, "Q", &qid);
2280 if (err < 0) {
2281 goto out;
2282 }
2283 err += offset;
2284 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2285 qid.type, qid.version, qid.path);
2286 out:
2287 put_fid(pdu, dfidp);
2288 out_nofid:
2289 pdu_complete(pdu, err);
2290 v9fs_string_free(&name);
2291 v9fs_string_free(&symname);
2292 }
2293
2294 static void v9fs_flush(void *opaque)
2295 {
2296 ssize_t err;
2297 int16_t tag;
2298 size_t offset = 7;
2299 V9fsPDU *cancel_pdu;
2300 V9fsPDU *pdu = opaque;
2301 V9fsState *s = pdu->s;
2302
2303 err = pdu_unmarshal(pdu, offset, "w", &tag);
2304 if (err < 0) {
2305 pdu_complete(pdu, err);
2306 return;
2307 }
2308 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2309
2310 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2311 if (cancel_pdu->tag == tag) {
2312 break;
2313 }
2314 }
2315 if (cancel_pdu) {
2316 cancel_pdu->cancelled = 1;
2317 /*
2318 * Wait for pdu to complete.
2319 */
2320 qemu_co_queue_wait(&cancel_pdu->complete);
2321 cancel_pdu->cancelled = 0;
2322 pdu_free(cancel_pdu);
2323 }
2324 pdu_complete(pdu, 7);
2325 }
2326
2327 static void v9fs_link(void *opaque)
2328 {
2329 V9fsPDU *pdu = opaque;
2330 int32_t dfid, oldfid;
2331 V9fsFidState *dfidp, *oldfidp;
2332 V9fsString name;
2333 size_t offset = 7;
2334 int err = 0;
2335
2336 v9fs_string_init(&name);
2337 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2338 if (err < 0) {
2339 goto out_nofid;
2340 }
2341 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2342
2343 if (name_is_illegal(name.data)) {
2344 err = -ENOENT;
2345 goto out_nofid;
2346 }
2347
2348 dfidp = get_fid(pdu, dfid);
2349 if (dfidp == NULL) {
2350 err = -ENOENT;
2351 goto out_nofid;
2352 }
2353
2354 oldfidp = get_fid(pdu, oldfid);
2355 if (oldfidp == NULL) {
2356 err = -ENOENT;
2357 goto out;
2358 }
2359 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2360 if (!err) {
2361 err = offset;
2362 }
2363 out:
2364 put_fid(pdu, dfidp);
2365 out_nofid:
2366 v9fs_string_free(&name);
2367 pdu_complete(pdu, err);
2368 }
2369
2370 /* Only works with path name based fid */
2371 static void v9fs_remove(void *opaque)
2372 {
2373 int32_t fid;
2374 int err = 0;
2375 size_t offset = 7;
2376 V9fsFidState *fidp;
2377 V9fsPDU *pdu = opaque;
2378
2379 err = pdu_unmarshal(pdu, offset, "d", &fid);
2380 if (err < 0) {
2381 goto out_nofid;
2382 }
2383 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2384
2385 fidp = get_fid(pdu, fid);
2386 if (fidp == NULL) {
2387 err = -EINVAL;
2388 goto out_nofid;
2389 }
2390 /* if fs driver is not path based, return EOPNOTSUPP */
2391 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2392 err = -EOPNOTSUPP;
2393 goto out_err;
2394 }
2395 /*
2396 * IF the file is unlinked, we cannot reopen
2397 * the file later. So don't reclaim fd
2398 */
2399 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2400 if (err < 0) {
2401 goto out_err;
2402 }
2403 err = v9fs_co_remove(pdu, &fidp->path);
2404 if (!err) {
2405 err = offset;
2406 }
2407 out_err:
2408 /* For TREMOVE we need to clunk the fid even on failed remove */
2409 clunk_fid(pdu->s, fidp->fid);
2410 put_fid(pdu, fidp);
2411 out_nofid:
2412 pdu_complete(pdu, err);
2413 }
2414
2415 static void v9fs_unlinkat(void *opaque)
2416 {
2417 int err = 0;
2418 V9fsString name;
2419 int32_t dfid, flags;
2420 size_t offset = 7;
2421 V9fsPath path;
2422 V9fsFidState *dfidp;
2423 V9fsPDU *pdu = opaque;
2424
2425 v9fs_string_init(&name);
2426 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2427 if (err < 0) {
2428 goto out_nofid;
2429 }
2430
2431 if (name_is_illegal(name.data)) {
2432 err = -ENOENT;
2433 goto out_nofid;
2434 }
2435
2436 dfidp = get_fid(pdu, dfid);
2437 if (dfidp == NULL) {
2438 err = -EINVAL;
2439 goto out_nofid;
2440 }
2441 /*
2442 * IF the file is unlinked, we cannot reopen
2443 * the file later. So don't reclaim fd
2444 */
2445 v9fs_path_init(&path);
2446 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2447 if (err < 0) {
2448 goto out_err;
2449 }
2450 err = v9fs_mark_fids_unreclaim(pdu, &path);
2451 if (err < 0) {
2452 goto out_err;
2453 }
2454 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2455 if (!err) {
2456 err = offset;
2457 }
2458 out_err:
2459 put_fid(pdu, dfidp);
2460 v9fs_path_free(&path);
2461 out_nofid:
2462 pdu_complete(pdu, err);
2463 v9fs_string_free(&name);
2464 }
2465
2466
2467 /* Only works with path name based fid */
2468 static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2469 int32_t newdirfid, V9fsString *name)
2470 {
2471 char *end;
2472 int err = 0;
2473 V9fsPath new_path;
2474 V9fsFidState *tfidp;
2475 V9fsState *s = pdu->s;
2476 V9fsFidState *dirfidp = NULL;
2477 char *old_name, *new_name;
2478
2479 v9fs_path_init(&new_path);
2480 if (newdirfid != -1) {
2481 dirfidp = get_fid(pdu, newdirfid);
2482 if (dirfidp == NULL) {
2483 err = -ENOENT;
2484 goto out_nofid;
2485 }
2486 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2487 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2488 } else {
2489 old_name = fidp->path.data;
2490 end = strrchr(old_name, '/');
2491 if (end) {
2492 end++;
2493 } else {
2494 end = old_name;
2495 }
2496 new_name = g_malloc0(end - old_name + name->size + 1);
2497 strncat(new_name, old_name, end - old_name);
2498 strncat(new_name + (end - old_name), name->data, name->size);
2499 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2500 g_free(new_name);
2501 }
2502 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2503 if (err < 0) {
2504 goto out;
2505 }
2506 /*
2507 * Fixup fid's pointing to the old name to
2508 * start pointing to the new name
2509 */
2510 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2511 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2512 /* replace the name */
2513 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2514 }
2515 }
2516 out:
2517 if (dirfidp) {
2518 put_fid(pdu, dirfidp);
2519 }
2520 v9fs_path_free(&new_path);
2521 out_nofid:
2522 return err;
2523 }
2524
2525 /* Only works with path name based fid */
2526 static void v9fs_rename(void *opaque)
2527 {
2528 int32_t fid;
2529 ssize_t err = 0;
2530 size_t offset = 7;
2531 V9fsString name;
2532 int32_t newdirfid;
2533 V9fsFidState *fidp;
2534 V9fsPDU *pdu = opaque;
2535 V9fsState *s = pdu->s;
2536
2537 v9fs_string_init(&name);
2538 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2539 if (err < 0) {
2540 goto out_nofid;
2541 }
2542
2543 if (name_is_illegal(name.data)) {
2544 err = -ENOENT;
2545 goto out_nofid;
2546 }
2547
2548 fidp = get_fid(pdu, fid);
2549 if (fidp == NULL) {
2550 err = -ENOENT;
2551 goto out_nofid;
2552 }
2553 BUG_ON(fidp->fid_type != P9_FID_NONE);
2554 /* if fs driver is not path based, return EOPNOTSUPP */
2555 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2556 err = -EOPNOTSUPP;
2557 goto out;
2558 }
2559 v9fs_path_write_lock(s);
2560 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2561 v9fs_path_unlock(s);
2562 if (!err) {
2563 err = offset;
2564 }
2565 out:
2566 put_fid(pdu, fidp);
2567 out_nofid:
2568 pdu_complete(pdu, err);
2569 v9fs_string_free(&name);
2570 }
2571
2572 static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2573 V9fsString *old_name, V9fsPath *newdir,
2574 V9fsString *new_name)
2575 {
2576 V9fsFidState *tfidp;
2577 V9fsPath oldpath, newpath;
2578 V9fsState *s = pdu->s;
2579
2580
2581 v9fs_path_init(&oldpath);
2582 v9fs_path_init(&newpath);
2583 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2584 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2585
2586 /*
2587 * Fixup fid's pointing to the old name to
2588 * start pointing to the new name
2589 */
2590 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2591 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2592 /* replace the name */
2593 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2594 }
2595 }
2596 v9fs_path_free(&oldpath);
2597 v9fs_path_free(&newpath);
2598 }
2599
2600 static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2601 V9fsString *old_name, int32_t newdirfid,
2602 V9fsString *new_name)
2603 {
2604 int err = 0;
2605 V9fsState *s = pdu->s;
2606 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2607
2608 olddirfidp = get_fid(pdu, olddirfid);
2609 if (olddirfidp == NULL) {
2610 err = -ENOENT;
2611 goto out;
2612 }
2613 if (newdirfid != -1) {
2614 newdirfidp = get_fid(pdu, newdirfid);
2615 if (newdirfidp == NULL) {
2616 err = -ENOENT;
2617 goto out;
2618 }
2619 } else {
2620 newdirfidp = get_fid(pdu, olddirfid);
2621 }
2622
2623 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2624 &newdirfidp->path, new_name);
2625 if (err < 0) {
2626 goto out;
2627 }
2628 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2629 /* Only for path based fid we need to do the below fixup */
2630 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2631 &newdirfidp->path, new_name);
2632 }
2633 out:
2634 if (olddirfidp) {
2635 put_fid(pdu, olddirfidp);
2636 }
2637 if (newdirfidp) {
2638 put_fid(pdu, newdirfidp);
2639 }
2640 return err;
2641 }
2642
2643 static void v9fs_renameat(void *opaque)
2644 {
2645 ssize_t err = 0;
2646 size_t offset = 7;
2647 V9fsPDU *pdu = opaque;
2648 V9fsState *s = pdu->s;
2649 int32_t olddirfid, newdirfid;
2650 V9fsString old_name, new_name;
2651
2652 v9fs_string_init(&old_name);
2653 v9fs_string_init(&new_name);
2654 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2655 &old_name, &newdirfid, &new_name);
2656 if (err < 0) {
2657 goto out_err;
2658 }
2659
2660 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2661 err = -ENOENT;
2662 goto out_err;
2663 }
2664
2665 v9fs_path_write_lock(s);
2666 err = v9fs_complete_renameat(pdu, olddirfid,
2667 &old_name, newdirfid, &new_name);
2668 v9fs_path_unlock(s);
2669 if (!err) {
2670 err = offset;
2671 }
2672
2673 out_err:
2674 pdu_complete(pdu, err);
2675 v9fs_string_free(&old_name);
2676 v9fs_string_free(&new_name);
2677 }
2678
2679 static void v9fs_wstat(void *opaque)
2680 {
2681 int32_t fid;
2682 int err = 0;
2683 int16_t unused;
2684 V9fsStat v9stat;
2685 size_t offset = 7;
2686 struct stat stbuf;
2687 V9fsFidState *fidp;
2688 V9fsPDU *pdu = opaque;
2689
2690 v9fs_stat_init(&v9stat);
2691 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2692 if (err < 0) {
2693 goto out_nofid;
2694 }
2695 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2696 v9stat.mode, v9stat.atime, v9stat.mtime);
2697
2698 fidp = get_fid(pdu, fid);
2699 if (fidp == NULL) {
2700 err = -EINVAL;
2701 goto out_nofid;
2702 }
2703 /* do we need to sync the file? */
2704 if (donttouch_stat(&v9stat)) {
2705 err = v9fs_co_fsync(pdu, fidp, 0);
2706 goto out;
2707 }
2708 if (v9stat.mode != -1) {
2709 uint32_t v9_mode;
2710 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2711 if (err < 0) {
2712 goto out;
2713 }
2714 v9_mode = stat_to_v9mode(&stbuf);
2715 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2716 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2717 /* Attempting to change the type */
2718 err = -EIO;
2719 goto out;
2720 }
2721 err = v9fs_co_chmod(pdu, &fidp->path,
2722 v9mode_to_mode(v9stat.mode,
2723 &v9stat.extension));
2724 if (err < 0) {
2725 goto out;
2726 }
2727 }
2728 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2729 struct timespec times[2];
2730 if (v9stat.atime != -1) {
2731 times[0].tv_sec = v9stat.atime;
2732 times[0].tv_nsec = 0;
2733 } else {
2734 times[0].tv_nsec = UTIME_OMIT;
2735 }
2736 if (v9stat.mtime != -1) {
2737 times[1].tv_sec = v9stat.mtime;
2738 times[1].tv_nsec = 0;
2739 } else {
2740 times[1].tv_nsec = UTIME_OMIT;
2741 }
2742 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2743 if (err < 0) {
2744 goto out;
2745 }
2746 }
2747 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2748 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2749 if (err < 0) {
2750 goto out;
2751 }
2752 }
2753 if (v9stat.name.size != 0) {
2754 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2755 if (err < 0) {
2756 goto out;
2757 }
2758 }
2759 if (v9stat.length != -1) {
2760 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2761 if (err < 0) {
2762 goto out;
2763 }
2764 }
2765 err = offset;
2766 out:
2767 put_fid(pdu, fidp);
2768 out_nofid:
2769 v9fs_stat_free(&v9stat);
2770 pdu_complete(pdu, err);
2771 }
2772
2773 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2774 {
2775 uint32_t f_type;
2776 uint32_t f_bsize;
2777 uint64_t f_blocks;
2778 uint64_t f_bfree;
2779 uint64_t f_bavail;
2780 uint64_t f_files;
2781 uint64_t f_ffree;
2782 uint64_t fsid_val;
2783 uint32_t f_namelen;
2784 size_t offset = 7;
2785 int32_t bsize_factor;
2786
2787 /*
2788 * compute bsize factor based on host file system block size
2789 * and client msize
2790 */
2791 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2792 if (!bsize_factor) {
2793 bsize_factor = 1;
2794 }
2795 f_type = stbuf->f_type;
2796 f_bsize = stbuf->f_bsize;
2797 f_bsize *= bsize_factor;
2798 /*
2799 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2800 * adjust(divide) the number of blocks, free blocks and available
2801 * blocks by bsize factor
2802 */
2803 f_blocks = stbuf->f_blocks/bsize_factor;
2804 f_bfree = stbuf->f_bfree/bsize_factor;
2805 f_bavail = stbuf->f_bavail/bsize_factor;
2806 f_files = stbuf->f_files;
2807 f_ffree = stbuf->f_ffree;
2808 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2809 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2810 f_namelen = stbuf->f_namelen;
2811
2812 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2813 f_type, f_bsize, f_blocks, f_bfree,
2814 f_bavail, f_files, f_ffree,
2815 fsid_val, f_namelen);
2816 }
2817
2818 static void v9fs_statfs(void *opaque)
2819 {
2820 int32_t fid;
2821 ssize_t retval = 0;
2822 size_t offset = 7;
2823 V9fsFidState *fidp;
2824 struct statfs stbuf;
2825 V9fsPDU *pdu = opaque;
2826 V9fsState *s = pdu->s;
2827
2828 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2829 if (retval < 0) {
2830 goto out_nofid;
2831 }
2832 fidp = get_fid(pdu, fid);
2833 if (fidp == NULL) {
2834 retval = -ENOENT;
2835 goto out_nofid;
2836 }
2837 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2838 if (retval < 0) {
2839 goto out;
2840 }
2841 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2842 if (retval < 0) {
2843 goto out;
2844 }
2845 retval += offset;
2846 out:
2847 put_fid(pdu, fidp);
2848 out_nofid:
2849 pdu_complete(pdu, retval);
2850 }
2851
2852 static void v9fs_mknod(void *opaque)
2853 {
2854
2855 int mode;
2856 gid_t gid;
2857 int32_t fid;
2858 V9fsQID qid;
2859 int err = 0;
2860 int major, minor;
2861 size_t offset = 7;
2862 V9fsString name;
2863 struct stat stbuf;
2864 V9fsFidState *fidp;
2865 V9fsPDU *pdu = opaque;
2866
2867 v9fs_string_init(&name);
2868 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2869 &major, &minor, &gid);
2870 if (err < 0) {
2871 goto out_nofid;
2872 }
2873 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2874
2875 if (name_is_illegal(name.data)) {
2876 err = -ENOENT;
2877 goto out_nofid;
2878 }
2879
2880 fidp = get_fid(pdu, fid);
2881 if (fidp == NULL) {
2882 err = -ENOENT;
2883 goto out_nofid;
2884 }
2885 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2886 makedev(major, minor), mode, &stbuf);
2887 if (err < 0) {
2888 goto out;
2889 }
2890 stat_to_qid(&stbuf, &qid);
2891 err = pdu_marshal(pdu, offset, "Q", &qid);
2892 if (err < 0) {
2893 goto out;
2894 }
2895 err += offset;
2896 trace_v9fs_mknod_return(pdu->tag, pdu->id,
2897 qid.type, qid.version, qid.path);
2898 out:
2899 put_fid(pdu, fidp);
2900 out_nofid:
2901 pdu_complete(pdu, err);
2902 v9fs_string_free(&name);
2903 }
2904
2905 /*
2906 * Implement posix byte range locking code
2907 * Server side handling of locking code is very simple, because 9p server in
2908 * QEMU can handle only one client. And most of the lock handling
2909 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2910 * do any thing in * qemu 9p server side lock code path.
2911 * So when a TLOCK request comes, always return success
2912 */
2913 static void v9fs_lock(void *opaque)
2914 {
2915 int8_t status;
2916 V9fsFlock flock;
2917 size_t offset = 7;
2918 struct stat stbuf;
2919 V9fsFidState *fidp;
2920 int32_t fid, err = 0;
2921 V9fsPDU *pdu = opaque;
2922
2923 status = P9_LOCK_ERROR;
2924 v9fs_string_init(&flock.client_id);
2925 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
2926 &flock.flags, &flock.start, &flock.length,
2927 &flock.proc_id, &flock.client_id);
2928 if (err < 0) {
2929 goto out_nofid;
2930 }
2931 trace_v9fs_lock(pdu->tag, pdu->id, fid,
2932 flock.type, flock.start, flock.length);
2933
2934
2935 /* We support only block flag now (that too ignored currently) */
2936 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
2937 err = -EINVAL;
2938 goto out_nofid;
2939 }
2940 fidp = get_fid(pdu, fid);
2941 if (fidp == NULL) {
2942 err = -ENOENT;
2943 goto out_nofid;
2944 }
2945 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2946 if (err < 0) {
2947 goto out;
2948 }
2949 status = P9_LOCK_SUCCESS;
2950 out:
2951 put_fid(pdu, fidp);
2952 out_nofid:
2953 err = pdu_marshal(pdu, offset, "b", status);
2954 if (err > 0) {
2955 err += offset;
2956 }
2957 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
2958 pdu_complete(pdu, err);
2959 v9fs_string_free(&flock.client_id);
2960 }
2961
2962 /*
2963 * When a TGETLOCK request comes, always return success because all lock
2964 * handling is done by client's VFS layer.
2965 */
2966 static void v9fs_getlock(void *opaque)
2967 {
2968 size_t offset = 7;
2969 struct stat stbuf;
2970 V9fsFidState *fidp;
2971 V9fsGetlock glock;
2972 int32_t fid, err = 0;
2973 V9fsPDU *pdu = opaque;
2974
2975 v9fs_string_init(&glock.client_id);
2976 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
2977 &glock.start, &glock.length, &glock.proc_id,
2978 &glock.client_id);
2979 if (err < 0) {
2980 goto out_nofid;
2981 }
2982 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
2983 glock.type, glock.start, glock.length);
2984
2985 fidp = get_fid(pdu, fid);
2986 if (fidp == NULL) {
2987 err = -ENOENT;
2988 goto out_nofid;
2989 }
2990 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2991 if (err < 0) {
2992 goto out;
2993 }
2994 glock.type = P9_LOCK_TYPE_UNLCK;
2995 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
2996 glock.start, glock.length, glock.proc_id,
2997 &glock.client_id);
2998 if (err < 0) {
2999 goto out;
3000 }
3001 err += offset;
3002 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3003 glock.length, glock.proc_id);
3004 out:
3005 put_fid(pdu, fidp);
3006 out_nofid:
3007 pdu_complete(pdu, err);
3008 v9fs_string_free(&glock.client_id);
3009 }
3010
3011 static void v9fs_mkdir(void *opaque)
3012 {
3013 V9fsPDU *pdu = opaque;
3014 size_t offset = 7;
3015 int32_t fid;
3016 struct stat stbuf;
3017 V9fsQID qid;
3018 V9fsString name;
3019 V9fsFidState *fidp;
3020 gid_t gid;
3021 int mode;
3022 int err = 0;
3023
3024 v9fs_string_init(&name);
3025 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3026 if (err < 0) {
3027 goto out_nofid;
3028 }
3029 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3030
3031 if (name_is_illegal(name.data)) {
3032 err = -ENOENT;
3033 goto out_nofid;
3034 }
3035
3036 fidp = get_fid(pdu, fid);
3037 if (fidp == NULL) {
3038 err = -ENOENT;
3039 goto out_nofid;
3040 }
3041 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3042 if (err < 0) {
3043 goto out;
3044 }
3045 stat_to_qid(&stbuf, &qid);
3046 err = pdu_marshal(pdu, offset, "Q", &qid);
3047 if (err < 0) {
3048 goto out;
3049 }
3050 err += offset;
3051 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3052 qid.type, qid.version, qid.path, err);
3053 out:
3054 put_fid(pdu, fidp);
3055 out_nofid:
3056 pdu_complete(pdu, err);
3057 v9fs_string_free(&name);
3058 }
3059
3060 static void v9fs_xattrwalk(void *opaque)
3061 {
3062 int64_t size;
3063 V9fsString name;
3064 ssize_t err = 0;
3065 size_t offset = 7;
3066 int32_t fid, newfid;
3067 V9fsFidState *file_fidp;
3068 V9fsFidState *xattr_fidp = NULL;
3069 V9fsPDU *pdu = opaque;
3070 V9fsState *s = pdu->s;
3071
3072 v9fs_string_init(&name);
3073 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3074 if (err < 0) {
3075 goto out_nofid;
3076 }
3077 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3078
3079 file_fidp = get_fid(pdu, fid);
3080 if (file_fidp == NULL) {
3081 err = -ENOENT;
3082 goto out_nofid;
3083 }
3084 xattr_fidp = alloc_fid(s, newfid);
3085 if (xattr_fidp == NULL) {
3086 err = -EINVAL;
3087 goto out;
3088 }
3089 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3090 if (name.data == NULL) {
3091 /*
3092 * listxattr request. Get the size first
3093 */
3094 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3095 if (size < 0) {
3096 err = size;
3097 clunk_fid(s, xattr_fidp->fid);
3098 goto out;
3099 }
3100 /*
3101 * Read the xattr value
3102 */
3103 xattr_fidp->fs.xattr.len = size;
3104 xattr_fidp->fid_type = P9_FID_XATTR;
3105 xattr_fidp->fs.xattr.copied_len = -1;
3106 if (size) {
3107 xattr_fidp->fs.xattr.value = g_malloc(size);
3108 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3109 xattr_fidp->fs.xattr.value,
3110 xattr_fidp->fs.xattr.len);
3111 if (err < 0) {
3112 clunk_fid(s, xattr_fidp->fid);
3113 goto out;
3114 }
3115 }
3116 err = pdu_marshal(pdu, offset, "q", size);
3117 if (err < 0) {
3118 goto out;
3119 }
3120 err += offset;
3121 } else {
3122 /*
3123 * specific xattr fid. We check for xattr
3124 * presence also collect the xattr size
3125 */
3126 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3127 &name, NULL, 0);
3128 if (size < 0) {
3129 err = size;
3130 clunk_fid(s, xattr_fidp->fid);
3131 goto out;
3132 }
3133 /*
3134 * Read the xattr value
3135 */
3136 xattr_fidp->fs.xattr.len = size;
3137 xattr_fidp->fid_type = P9_FID_XATTR;
3138 xattr_fidp->fs.xattr.copied_len = -1;
3139 if (size) {
3140 xattr_fidp->fs.xattr.value = g_malloc(size);
3141 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3142 &name, xattr_fidp->fs.xattr.value,
3143 xattr_fidp->fs.xattr.len);
3144 if (err < 0) {
3145 clunk_fid(s, xattr_fidp->fid);
3146 goto out;
3147 }
3148 }
3149 err = pdu_marshal(pdu, offset, "q", size);
3150 if (err < 0) {
3151 goto out;
3152 }
3153 err += offset;
3154 }
3155 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3156 out:
3157 put_fid(pdu, file_fidp);
3158 if (xattr_fidp) {
3159 put_fid(pdu, xattr_fidp);
3160 }
3161 out_nofid:
3162 pdu_complete(pdu, err);
3163 v9fs_string_free(&name);
3164 }
3165
3166 static void v9fs_xattrcreate(void *opaque)
3167 {
3168 int flags;
3169 int32_t fid;
3170 int64_t size;
3171 ssize_t err = 0;
3172 V9fsString name;
3173 size_t offset = 7;
3174 V9fsFidState *file_fidp;
3175 V9fsFidState *xattr_fidp;
3176 V9fsPDU *pdu = opaque;
3177
3178 v9fs_string_init(&name);
3179 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3180 if (err < 0) {
3181 goto out_nofid;
3182 }
3183 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3184
3185 file_fidp = get_fid(pdu, fid);
3186 if (file_fidp == NULL) {
3187 err = -EINVAL;
3188 goto out_nofid;
3189 }
3190 /* Make the file fid point to xattr */
3191 xattr_fidp = file_fidp;
3192 xattr_fidp->fid_type = P9_FID_XATTR;
3193 xattr_fidp->fs.xattr.copied_len = 0;
3194 xattr_fidp->fs.xattr.len = size;
3195 xattr_fidp->fs.xattr.flags = flags;
3196 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3197 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3198 xattr_fidp->fs.xattr.value = g_malloc(size);
3199 err = offset;
3200 put_fid(pdu, file_fidp);
3201 out_nofid:
3202 pdu_complete(pdu, err);
3203 v9fs_string_free(&name);
3204 }
3205
3206 static void v9fs_readlink(void *opaque)
3207 {
3208 V9fsPDU *pdu = opaque;
3209 size_t offset = 7;
3210 V9fsString target;
3211 int32_t fid;
3212 int err = 0;
3213 V9fsFidState *fidp;
3214
3215 err = pdu_unmarshal(pdu, offset, "d", &fid);
3216 if (err < 0) {
3217 goto out_nofid;
3218 }
3219 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3220 fidp = get_fid(pdu, fid);
3221 if (fidp == NULL) {
3222 err = -ENOENT;
3223 goto out_nofid;
3224 }
3225
3226 v9fs_string_init(&target);
3227 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3228 if (err < 0) {
3229 goto out;
3230 }
3231 err = pdu_marshal(pdu, offset, "s", &target);
3232 if (err < 0) {
3233 v9fs_string_free(&target);
3234 goto out;
3235 }
3236 err += offset;
3237 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3238 v9fs_string_free(&target);
3239 out:
3240 put_fid(pdu, fidp);
3241 out_nofid:
3242 pdu_complete(pdu, err);
3243 }
3244
3245 static CoroutineEntry *pdu_co_handlers[] = {
3246 [P9_TREADDIR] = v9fs_readdir,
3247 [P9_TSTATFS] = v9fs_statfs,
3248 [P9_TGETATTR] = v9fs_getattr,
3249 [P9_TSETATTR] = v9fs_setattr,
3250 [P9_TXATTRWALK] = v9fs_xattrwalk,
3251 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3252 [P9_TMKNOD] = v9fs_mknod,
3253 [P9_TRENAME] = v9fs_rename,
3254 [P9_TLOCK] = v9fs_lock,
3255 [P9_TGETLOCK] = v9fs_getlock,
3256 [P9_TRENAMEAT] = v9fs_renameat,
3257 [P9_TREADLINK] = v9fs_readlink,
3258 [P9_TUNLINKAT] = v9fs_unlinkat,
3259 [P9_TMKDIR] = v9fs_mkdir,
3260 [P9_TVERSION] = v9fs_version,
3261 [P9_TLOPEN] = v9fs_open,
3262 [P9_TATTACH] = v9fs_attach,
3263 [P9_TSTAT] = v9fs_stat,
3264 [P9_TWALK] = v9fs_walk,
3265 [P9_TCLUNK] = v9fs_clunk,
3266 [P9_TFSYNC] = v9fs_fsync,
3267 [P9_TOPEN] = v9fs_open,
3268 [P9_TREAD] = v9fs_read,
3269 #if 0
3270 [P9_TAUTH] = v9fs_auth,
3271 #endif
3272 [P9_TFLUSH] = v9fs_flush,
3273 [P9_TLINK] = v9fs_link,
3274 [P9_TSYMLINK] = v9fs_symlink,
3275 [P9_TCREATE] = v9fs_create,
3276 [P9_TLCREATE] = v9fs_lcreate,
3277 [P9_TWRITE] = v9fs_write,
3278 [P9_TWSTAT] = v9fs_wstat,
3279 [P9_TREMOVE] = v9fs_remove,
3280 };
3281
3282 static void v9fs_op_not_supp(void *opaque)
3283 {
3284 V9fsPDU *pdu = opaque;
3285 pdu_complete(pdu, -EOPNOTSUPP);
3286 }
3287
3288 static void v9fs_fs_ro(void *opaque)
3289 {
3290 V9fsPDU *pdu = opaque;
3291 pdu_complete(pdu, -EROFS);
3292 }
3293
3294 static inline bool is_read_only_op(V9fsPDU *pdu)
3295 {
3296 switch (pdu->id) {
3297 case P9_TREADDIR:
3298 case P9_TSTATFS:
3299 case P9_TGETATTR:
3300 case P9_TXATTRWALK:
3301 case P9_TLOCK:
3302 case P9_TGETLOCK:
3303 case P9_TREADLINK:
3304 case P9_TVERSION:
3305 case P9_TLOPEN:
3306 case P9_TATTACH:
3307 case P9_TSTAT:
3308 case P9_TWALK:
3309 case P9_TCLUNK:
3310 case P9_TFSYNC:
3311 case P9_TOPEN:
3312 case P9_TREAD:
3313 case P9_TAUTH:
3314 case P9_TFLUSH:
3315 return 1;
3316 default:
3317 return 0;
3318 }
3319 }
3320
3321 void pdu_submit(V9fsPDU *pdu)
3322 {
3323 Coroutine *co;
3324 CoroutineEntry *handler;
3325 V9fsState *s = pdu->s;
3326
3327 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3328 (pdu_co_handlers[pdu->id] == NULL)) {
3329 handler = v9fs_op_not_supp;
3330 } else {
3331 handler = pdu_co_handlers[pdu->id];
3332 }
3333
3334 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3335 handler = v9fs_fs_ro;
3336 }
3337 co = qemu_coroutine_create(handler, pdu);
3338 qemu_coroutine_enter(co);
3339 }
3340
3341 /* Returns 0 on success, 1 on failure. */
3342 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3343 {
3344 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3345 int i, len;
3346 struct stat stat;
3347 FsDriverEntry *fse;
3348 V9fsPath path;
3349 int rc = 1;
3350
3351 /* initialize pdu allocator */
3352 QLIST_INIT(&s->free_list);
3353 QLIST_INIT(&s->active_list);
3354 for (i = 0; i < (MAX_REQ - 1); i++) {
3355 QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
3356 v->pdus[i].s = s;
3357 v->pdus[i].idx = i;
3358 }
3359
3360 v9fs_path_init(&path);
3361
3362 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3363
3364 if (!fse) {
3365 /* We don't have a fsdev identified by fsdev_id */
3366 error_setg(errp, "9pfs device couldn't find fsdev with the "
3367 "id = %s",
3368 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3369 goto out;
3370 }
3371
3372 if (!s->fsconf.tag) {
3373 /* we haven't specified a mount_tag */
3374 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3375 s->fsconf.fsdev_id);
3376 goto out;
3377 }
3378
3379 s->ctx.export_flags = fse->export_flags;
3380 s->ctx.fs_root = g_strdup(fse->path);
3381 s->ctx.exops.get_st_gen = NULL;
3382 len = strlen(s->fsconf.tag);
3383 if (len > MAX_TAG_LEN - 1) {
3384 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3385 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3386 goto out;
3387 }
3388
3389 s->tag = g_strdup(s->fsconf.tag);
3390 s->ctx.uid = -1;
3391
3392 s->ops = fse->ops;
3393
3394 s->fid_list = NULL;
3395 qemu_co_rwlock_init(&s->rename_lock);
3396
3397 if (s->ops->init(&s->ctx) < 0) {
3398 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3399 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3400 goto out;
3401 }
3402
3403 /*
3404 * Check details of export path, We need to use fs driver
3405 * call back to do that. Since we are in the init path, we don't
3406 * use co-routines here.
3407 */
3408 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3409 error_setg(errp,
3410 "error in converting name to path %s", strerror(errno));
3411 goto out;
3412 }
3413 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3414 error_setg(errp, "share path %s does not exist", fse->path);
3415 goto out;
3416 } else if (!S_ISDIR(stat.st_mode)) {
3417 error_setg(errp, "share path %s is not a directory", fse->path);
3418 goto out;
3419 }
3420 v9fs_path_free(&path);
3421
3422 rc = 0;
3423 out:
3424 if (rc) {
3425 g_free(s->ctx.fs_root);
3426 g_free(s->tag);
3427 v9fs_path_free(&path);
3428 }
3429 return rc;
3430 }
3431
3432 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3433 {
3434 g_free(s->ctx.fs_root);
3435 g_free(s->tag);
3436 }
3437
3438 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3439 {
3440 struct rlimit rlim;
3441 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3442 error_report("Failed to get the resource limit");
3443 exit(1);
3444 }
3445 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3446 open_fd_rc = rlim.rlim_cur/2;
3447 }