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