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