]> git.proxmox.com Git - qemu.git/blob - hw/9pfs/virtio-9p.c
Merge remote-tracking branch 'stefanha/net' into staging
[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 /*
594 * We don't do error checking for pdu_marshal/unmarshal here
595 * because we always expect to have enough space to encode
596 * error details
597 */
598 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
599 {
600 int8_t id = pdu->id + 1; /* Response */
601
602 if (len < 0) {
603 int err = -len;
604 len = 7;
605
606 if (s->proto_version != V9FS_PROTO_2000L) {
607 V9fsString str;
608
609 str.data = strerror(err);
610 str.size = strlen(str.data);
611
612 len += pdu_marshal(pdu, len, "s", &str);
613 id = P9_RERROR;
614 }
615
616 len += pdu_marshal(pdu, len, "d", err);
617
618 if (s->proto_version == V9FS_PROTO_2000L) {
619 id = P9_RLERROR;
620 }
621 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
622 }
623
624 /* fill out the header */
625 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
626
627 /* keep these in sync */
628 pdu->size = len;
629 pdu->id = id;
630
631 /* push onto queue and notify */
632 virtqueue_push(s->vq, &pdu->elem, len);
633
634 /* FIXME: we should batch these completions */
635 virtio_notify(&s->vdev, s->vq);
636
637 /* Now wakeup anybody waiting in flush for this request */
638 qemu_co_queue_next(&pdu->complete);
639
640 free_pdu(s, pdu);
641 }
642
643 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
644 {
645 mode_t ret;
646
647 ret = mode & 0777;
648 if (mode & P9_STAT_MODE_DIR) {
649 ret |= S_IFDIR;
650 }
651
652 if (mode & P9_STAT_MODE_SYMLINK) {
653 ret |= S_IFLNK;
654 }
655 if (mode & P9_STAT_MODE_SOCKET) {
656 ret |= S_IFSOCK;
657 }
658 if (mode & P9_STAT_MODE_NAMED_PIPE) {
659 ret |= S_IFIFO;
660 }
661 if (mode & P9_STAT_MODE_DEVICE) {
662 if (extension && extension->data[0] == 'c') {
663 ret |= S_IFCHR;
664 } else {
665 ret |= S_IFBLK;
666 }
667 }
668
669 if (!(ret&~0777)) {
670 ret |= S_IFREG;
671 }
672
673 if (mode & P9_STAT_MODE_SETUID) {
674 ret |= S_ISUID;
675 }
676 if (mode & P9_STAT_MODE_SETGID) {
677 ret |= S_ISGID;
678 }
679 if (mode & P9_STAT_MODE_SETVTX) {
680 ret |= S_ISVTX;
681 }
682
683 return ret;
684 }
685
686 static int donttouch_stat(V9fsStat *stat)
687 {
688 if (stat->type == -1 &&
689 stat->dev == -1 &&
690 stat->qid.type == -1 &&
691 stat->qid.version == -1 &&
692 stat->qid.path == -1 &&
693 stat->mode == -1 &&
694 stat->atime == -1 &&
695 stat->mtime == -1 &&
696 stat->length == -1 &&
697 !stat->name.size &&
698 !stat->uid.size &&
699 !stat->gid.size &&
700 !stat->muid.size &&
701 stat->n_uid == -1 &&
702 stat->n_gid == -1 &&
703 stat->n_muid == -1) {
704 return 1;
705 }
706
707 return 0;
708 }
709
710 static void v9fs_stat_init(V9fsStat *stat)
711 {
712 v9fs_string_init(&stat->name);
713 v9fs_string_init(&stat->uid);
714 v9fs_string_init(&stat->gid);
715 v9fs_string_init(&stat->muid);
716 v9fs_string_init(&stat->extension);
717 }
718
719 static void v9fs_stat_free(V9fsStat *stat)
720 {
721 v9fs_string_free(&stat->name);
722 v9fs_string_free(&stat->uid);
723 v9fs_string_free(&stat->gid);
724 v9fs_string_free(&stat->muid);
725 v9fs_string_free(&stat->extension);
726 }
727
728 static uint32_t stat_to_v9mode(const struct stat *stbuf)
729 {
730 uint32_t mode;
731
732 mode = stbuf->st_mode & 0777;
733 if (S_ISDIR(stbuf->st_mode)) {
734 mode |= P9_STAT_MODE_DIR;
735 }
736
737 if (S_ISLNK(stbuf->st_mode)) {
738 mode |= P9_STAT_MODE_SYMLINK;
739 }
740
741 if (S_ISSOCK(stbuf->st_mode)) {
742 mode |= P9_STAT_MODE_SOCKET;
743 }
744
745 if (S_ISFIFO(stbuf->st_mode)) {
746 mode |= P9_STAT_MODE_NAMED_PIPE;
747 }
748
749 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
750 mode |= P9_STAT_MODE_DEVICE;
751 }
752
753 if (stbuf->st_mode & S_ISUID) {
754 mode |= P9_STAT_MODE_SETUID;
755 }
756
757 if (stbuf->st_mode & S_ISGID) {
758 mode |= P9_STAT_MODE_SETGID;
759 }
760
761 if (stbuf->st_mode & S_ISVTX) {
762 mode |= P9_STAT_MODE_SETVTX;
763 }
764
765 return mode;
766 }
767
768 static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
769 const struct stat *stbuf,
770 V9fsStat *v9stat)
771 {
772 int err;
773 const char *str;
774
775 memset(v9stat, 0, sizeof(*v9stat));
776
777 stat_to_qid(stbuf, &v9stat->qid);
778 v9stat->mode = stat_to_v9mode(stbuf);
779 v9stat->atime = stbuf->st_atime;
780 v9stat->mtime = stbuf->st_mtime;
781 v9stat->length = stbuf->st_size;
782
783 v9fs_string_null(&v9stat->uid);
784 v9fs_string_null(&v9stat->gid);
785 v9fs_string_null(&v9stat->muid);
786
787 v9stat->n_uid = stbuf->st_uid;
788 v9stat->n_gid = stbuf->st_gid;
789 v9stat->n_muid = 0;
790
791 v9fs_string_null(&v9stat->extension);
792
793 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
794 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
795 if (err < 0) {
796 return err;
797 }
798 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
799 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
800 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
801 major(stbuf->st_rdev), minor(stbuf->st_rdev));
802 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
803 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
804 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
805 }
806
807 str = strrchr(name->data, '/');
808 if (str) {
809 str += 1;
810 } else {
811 str = name->data;
812 }
813
814 v9fs_string_sprintf(&v9stat->name, "%s", str);
815
816 v9stat->size = 61 +
817 v9fs_string_size(&v9stat->name) +
818 v9fs_string_size(&v9stat->uid) +
819 v9fs_string_size(&v9stat->gid) +
820 v9fs_string_size(&v9stat->muid) +
821 v9fs_string_size(&v9stat->extension);
822 return 0;
823 }
824
825 #define P9_STATS_MODE 0x00000001ULL
826 #define P9_STATS_NLINK 0x00000002ULL
827 #define P9_STATS_UID 0x00000004ULL
828 #define P9_STATS_GID 0x00000008ULL
829 #define P9_STATS_RDEV 0x00000010ULL
830 #define P9_STATS_ATIME 0x00000020ULL
831 #define P9_STATS_MTIME 0x00000040ULL
832 #define P9_STATS_CTIME 0x00000080ULL
833 #define P9_STATS_INO 0x00000100ULL
834 #define P9_STATS_SIZE 0x00000200ULL
835 #define P9_STATS_BLOCKS 0x00000400ULL
836
837 #define P9_STATS_BTIME 0x00000800ULL
838 #define P9_STATS_GEN 0x00001000ULL
839 #define P9_STATS_DATA_VERSION 0x00002000ULL
840
841 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
842 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
843
844
845 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
846 V9fsStatDotl *v9lstat)
847 {
848 memset(v9lstat, 0, sizeof(*v9lstat));
849
850 v9lstat->st_mode = stbuf->st_mode;
851 v9lstat->st_nlink = stbuf->st_nlink;
852 v9lstat->st_uid = stbuf->st_uid;
853 v9lstat->st_gid = stbuf->st_gid;
854 v9lstat->st_rdev = stbuf->st_rdev;
855 v9lstat->st_size = stbuf->st_size;
856 v9lstat->st_blksize = stbuf->st_blksize;
857 v9lstat->st_blocks = stbuf->st_blocks;
858 v9lstat->st_atime_sec = stbuf->st_atime;
859 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
860 v9lstat->st_mtime_sec = stbuf->st_mtime;
861 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
862 v9lstat->st_ctime_sec = stbuf->st_ctime;
863 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
864 /* Currently we only support BASIC fields in stat */
865 v9lstat->st_result_mask = P9_STATS_BASIC;
866
867 stat_to_qid(stbuf, &v9lstat->qid);
868 }
869
870 static void print_sg(struct iovec *sg, int cnt)
871 {
872 int i;
873
874 printf("sg[%d]: {", cnt);
875 for (i = 0; i < cnt; i++) {
876 if (i) {
877 printf(", ");
878 }
879 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
880 }
881 printf("}\n");
882 }
883
884 /* Will call this only for path name based fid */
885 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
886 {
887 V9fsPath str;
888 v9fs_path_init(&str);
889 v9fs_path_copy(&str, dst);
890 v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
891 v9fs_path_free(&str);
892 /* +1 to include terminating NULL */
893 dst->size++;
894 }
895
896 static inline bool is_ro_export(FsContext *ctx)
897 {
898 return ctx->export_flags & V9FS_RDONLY;
899 }
900
901 static void v9fs_version(void *opaque)
902 {
903 ssize_t err;
904 V9fsPDU *pdu = opaque;
905 V9fsState *s = pdu->s;
906 V9fsString version;
907 size_t offset = 7;
908
909 v9fs_string_init(&version);
910 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
911 if (err < 0) {
912 offset = err;
913 goto out;
914 }
915 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
916
917 virtfs_reset(pdu);
918
919 if (!strcmp(version.data, "9P2000.u")) {
920 s->proto_version = V9FS_PROTO_2000U;
921 } else if (!strcmp(version.data, "9P2000.L")) {
922 s->proto_version = V9FS_PROTO_2000L;
923 } else {
924 v9fs_string_sprintf(&version, "unknown");
925 }
926
927 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
928 if (err < 0) {
929 offset = err;
930 goto out;
931 }
932 offset += err;
933 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
934 out:
935 complete_pdu(s, pdu, offset);
936 v9fs_string_free(&version);
937 return;
938 }
939
940 static void v9fs_attach(void *opaque)
941 {
942 V9fsPDU *pdu = opaque;
943 V9fsState *s = pdu->s;
944 int32_t fid, afid, n_uname;
945 V9fsString uname, aname;
946 V9fsFidState *fidp;
947 size_t offset = 7;
948 V9fsQID qid;
949 ssize_t err;
950
951 v9fs_string_init(&uname);
952 v9fs_string_init(&aname);
953 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
954 &afid, &uname, &aname, &n_uname);
955 if (err < 0) {
956 goto out_nofid;
957 }
958 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
959
960 fidp = alloc_fid(s, fid);
961 if (fidp == NULL) {
962 err = -EINVAL;
963 goto out_nofid;
964 }
965 fidp->uid = n_uname;
966 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
967 if (err < 0) {
968 err = -EINVAL;
969 clunk_fid(s, fid);
970 goto out;
971 }
972 err = fid_to_qid(pdu, fidp, &qid);
973 if (err < 0) {
974 err = -EINVAL;
975 clunk_fid(s, fid);
976 goto out;
977 }
978 err = pdu_marshal(pdu, offset, "Q", &qid);
979 if (err < 0) {
980 clunk_fid(s, fid);
981 goto out;
982 }
983 err += offset;
984 trace_v9fs_attach_return(pdu->tag, pdu->id,
985 qid.type, qid.version, qid.path);
986 /*
987 * disable migration if we haven't done already.
988 * attach could get called multiple times for the same export.
989 */
990 if (!s->migration_blocker) {
991 s->root_fid = fid;
992 error_set(&s->migration_blocker, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
993 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
994 migrate_add_blocker(s->migration_blocker);
995 }
996 out:
997 put_fid(pdu, fidp);
998 out_nofid:
999 complete_pdu(s, pdu, err);
1000 v9fs_string_free(&uname);
1001 v9fs_string_free(&aname);
1002 }
1003
1004 static void v9fs_stat(void *opaque)
1005 {
1006 int32_t fid;
1007 V9fsStat v9stat;
1008 ssize_t err = 0;
1009 size_t offset = 7;
1010 struct stat stbuf;
1011 V9fsFidState *fidp;
1012 V9fsPDU *pdu = opaque;
1013 V9fsState *s = pdu->s;
1014
1015 err = pdu_unmarshal(pdu, offset, "d", &fid);
1016 if (err < 0) {
1017 goto out_nofid;
1018 }
1019 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1020
1021 fidp = get_fid(pdu, fid);
1022 if (fidp == NULL) {
1023 err = -ENOENT;
1024 goto out_nofid;
1025 }
1026 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1027 if (err < 0) {
1028 goto out;
1029 }
1030 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1031 if (err < 0) {
1032 goto out;
1033 }
1034 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1035 if (err < 0) {
1036 v9fs_stat_free(&v9stat);
1037 goto out;
1038 }
1039 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1040 v9stat.atime, v9stat.mtime, v9stat.length);
1041 err += offset;
1042 v9fs_stat_free(&v9stat);
1043 out:
1044 put_fid(pdu, fidp);
1045 out_nofid:
1046 complete_pdu(s, pdu, err);
1047 }
1048
1049 static void v9fs_getattr(void *opaque)
1050 {
1051 int32_t fid;
1052 size_t offset = 7;
1053 ssize_t retval = 0;
1054 struct stat stbuf;
1055 V9fsFidState *fidp;
1056 uint64_t request_mask;
1057 V9fsStatDotl v9stat_dotl;
1058 V9fsPDU *pdu = opaque;
1059 V9fsState *s = pdu->s;
1060
1061 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1062 if (retval < 0) {
1063 goto out_nofid;
1064 }
1065 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1066
1067 fidp = get_fid(pdu, fid);
1068 if (fidp == NULL) {
1069 retval = -ENOENT;
1070 goto out_nofid;
1071 }
1072 /*
1073 * Currently we only support BASIC fields in stat, so there is no
1074 * need to look at request_mask.
1075 */
1076 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1077 if (retval < 0) {
1078 goto out;
1079 }
1080 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1081
1082 /* fill st_gen if requested and supported by underlying fs */
1083 if (request_mask & P9_STATS_GEN) {
1084 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1085 if (retval < 0) {
1086 goto out;
1087 }
1088 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1089 }
1090 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1091 if (retval < 0) {
1092 goto out;
1093 }
1094 retval += offset;
1095 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1096 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1097 v9stat_dotl.st_gid);
1098 out:
1099 put_fid(pdu, fidp);
1100 out_nofid:
1101 complete_pdu(s, pdu, retval);
1102 }
1103
1104 /* Attribute flags */
1105 #define P9_ATTR_MODE (1 << 0)
1106 #define P9_ATTR_UID (1 << 1)
1107 #define P9_ATTR_GID (1 << 2)
1108 #define P9_ATTR_SIZE (1 << 3)
1109 #define P9_ATTR_ATIME (1 << 4)
1110 #define P9_ATTR_MTIME (1 << 5)
1111 #define P9_ATTR_CTIME (1 << 6)
1112 #define P9_ATTR_ATIME_SET (1 << 7)
1113 #define P9_ATTR_MTIME_SET (1 << 8)
1114
1115 #define P9_ATTR_MASK 127
1116
1117 static void v9fs_setattr(void *opaque)
1118 {
1119 int err = 0;
1120 int32_t fid;
1121 V9fsFidState *fidp;
1122 size_t offset = 7;
1123 V9fsIattr v9iattr;
1124 V9fsPDU *pdu = opaque;
1125 V9fsState *s = pdu->s;
1126
1127 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1128 if (err < 0) {
1129 goto out_nofid;
1130 }
1131
1132 fidp = get_fid(pdu, fid);
1133 if (fidp == NULL) {
1134 err = -EINVAL;
1135 goto out_nofid;
1136 }
1137 if (v9iattr.valid & P9_ATTR_MODE) {
1138 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1139 if (err < 0) {
1140 goto out;
1141 }
1142 }
1143 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1144 struct timespec times[2];
1145 if (v9iattr.valid & P9_ATTR_ATIME) {
1146 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1147 times[0].tv_sec = v9iattr.atime_sec;
1148 times[0].tv_nsec = v9iattr.atime_nsec;
1149 } else {
1150 times[0].tv_nsec = UTIME_NOW;
1151 }
1152 } else {
1153 times[0].tv_nsec = UTIME_OMIT;
1154 }
1155 if (v9iattr.valid & P9_ATTR_MTIME) {
1156 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1157 times[1].tv_sec = v9iattr.mtime_sec;
1158 times[1].tv_nsec = v9iattr.mtime_nsec;
1159 } else {
1160 times[1].tv_nsec = UTIME_NOW;
1161 }
1162 } else {
1163 times[1].tv_nsec = UTIME_OMIT;
1164 }
1165 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1166 if (err < 0) {
1167 goto out;
1168 }
1169 }
1170 /*
1171 * If the only valid entry in iattr is ctime we can call
1172 * chown(-1,-1) to update the ctime of the file
1173 */
1174 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1175 ((v9iattr.valid & P9_ATTR_CTIME)
1176 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1177 if (!(v9iattr.valid & P9_ATTR_UID)) {
1178 v9iattr.uid = -1;
1179 }
1180 if (!(v9iattr.valid & P9_ATTR_GID)) {
1181 v9iattr.gid = -1;
1182 }
1183 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1184 v9iattr.gid);
1185 if (err < 0) {
1186 goto out;
1187 }
1188 }
1189 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1190 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1191 if (err < 0) {
1192 goto out;
1193 }
1194 }
1195 err = offset;
1196 out:
1197 put_fid(pdu, fidp);
1198 out_nofid:
1199 complete_pdu(s, pdu, err);
1200 }
1201
1202 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1203 {
1204 int i;
1205 ssize_t err;
1206 size_t offset = 7;
1207
1208 err = pdu_marshal(pdu, offset, "w", nwnames);
1209 if (err < 0) {
1210 return err;
1211 }
1212 offset += err;
1213 for (i = 0; i < nwnames; i++) {
1214 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1215 if (err < 0) {
1216 return err;
1217 }
1218 offset += err;
1219 }
1220 return offset;
1221 }
1222
1223 static void v9fs_walk(void *opaque)
1224 {
1225 int name_idx;
1226 V9fsQID *qids = NULL;
1227 int i, err = 0;
1228 V9fsPath dpath, path;
1229 uint16_t nwnames;
1230 struct stat stbuf;
1231 size_t offset = 7;
1232 int32_t fid, newfid;
1233 V9fsString *wnames = NULL;
1234 V9fsFidState *fidp;
1235 V9fsFidState *newfidp = NULL;
1236 V9fsPDU *pdu = opaque;
1237 V9fsState *s = pdu->s;
1238
1239 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1240 if (err < 0) {
1241 complete_pdu(s, pdu, err);
1242 return ;
1243 }
1244 offset += err;
1245
1246 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1247
1248 if (nwnames && nwnames <= P9_MAXWELEM) {
1249 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1250 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1251 for (i = 0; i < nwnames; i++) {
1252 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1253 if (err < 0) {
1254 goto out_nofid;
1255 }
1256 offset += err;
1257 }
1258 } else if (nwnames > P9_MAXWELEM) {
1259 err = -EINVAL;
1260 goto out_nofid;
1261 }
1262 fidp = get_fid(pdu, fid);
1263 if (fidp == NULL) {
1264 err = -ENOENT;
1265 goto out_nofid;
1266 }
1267 v9fs_path_init(&dpath);
1268 v9fs_path_init(&path);
1269 /*
1270 * Both dpath and path initially poin to fidp.
1271 * Needed to handle request with nwnames == 0
1272 */
1273 v9fs_path_copy(&dpath, &fidp->path);
1274 v9fs_path_copy(&path, &fidp->path);
1275 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1276 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
1277 if (err < 0) {
1278 goto out;
1279 }
1280 err = v9fs_co_lstat(pdu, &path, &stbuf);
1281 if (err < 0) {
1282 goto out;
1283 }
1284 stat_to_qid(&stbuf, &qids[name_idx]);
1285 v9fs_path_copy(&dpath, &path);
1286 }
1287 if (fid == newfid) {
1288 BUG_ON(fidp->fid_type != P9_FID_NONE);
1289 v9fs_path_copy(&fidp->path, &path);
1290 } else {
1291 newfidp = alloc_fid(s, newfid);
1292 if (newfidp == NULL) {
1293 err = -EINVAL;
1294 goto out;
1295 }
1296 newfidp->uid = fidp->uid;
1297 v9fs_path_copy(&newfidp->path, &path);
1298 }
1299 err = v9fs_walk_marshal(pdu, nwnames, qids);
1300 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1301 out:
1302 put_fid(pdu, fidp);
1303 if (newfidp) {
1304 put_fid(pdu, newfidp);
1305 }
1306 v9fs_path_free(&dpath);
1307 v9fs_path_free(&path);
1308 out_nofid:
1309 complete_pdu(s, pdu, err);
1310 if (nwnames && nwnames <= P9_MAXWELEM) {
1311 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1312 v9fs_string_free(&wnames[name_idx]);
1313 }
1314 g_free(wnames);
1315 g_free(qids);
1316 }
1317 return;
1318 }
1319
1320 static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1321 {
1322 struct statfs stbuf;
1323 int32_t iounit = 0;
1324 V9fsState *s = pdu->s;
1325
1326 /*
1327 * iounit should be multiples of f_bsize (host filesystem block size
1328 * and as well as less than (client msize - P9_IOHDRSZ))
1329 */
1330 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1331 iounit = stbuf.f_bsize;
1332 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1333 }
1334 if (!iounit) {
1335 iounit = s->msize - P9_IOHDRSZ;
1336 }
1337 return iounit;
1338 }
1339
1340 static void v9fs_open(void *opaque)
1341 {
1342 int flags;
1343 int32_t fid;
1344 int32_t mode;
1345 V9fsQID qid;
1346 int iounit = 0;
1347 ssize_t err = 0;
1348 size_t offset = 7;
1349 struct stat stbuf;
1350 V9fsFidState *fidp;
1351 V9fsPDU *pdu = opaque;
1352 V9fsState *s = pdu->s;
1353
1354 if (s->proto_version == V9FS_PROTO_2000L) {
1355 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1356 } else {
1357 uint8_t modebyte;
1358 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1359 mode = modebyte;
1360 }
1361 if (err < 0) {
1362 goto out_nofid;
1363 }
1364 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1365
1366 fidp = get_fid(pdu, fid);
1367 if (fidp == NULL) {
1368 err = -ENOENT;
1369 goto out_nofid;
1370 }
1371 BUG_ON(fidp->fid_type != P9_FID_NONE);
1372
1373 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1374 if (err < 0) {
1375 goto out;
1376 }
1377 stat_to_qid(&stbuf, &qid);
1378 if (S_ISDIR(stbuf.st_mode)) {
1379 err = v9fs_co_opendir(pdu, fidp);
1380 if (err < 0) {
1381 goto out;
1382 }
1383 fidp->fid_type = P9_FID_DIR;
1384 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1385 if (err < 0) {
1386 goto out;
1387 }
1388 err += offset;
1389 } else {
1390 if (s->proto_version == V9FS_PROTO_2000L) {
1391 flags = get_dotl_openflags(s, mode);
1392 } else {
1393 flags = omode_to_uflags(mode);
1394 }
1395 if (is_ro_export(&s->ctx)) {
1396 if (mode & O_WRONLY || mode & O_RDWR ||
1397 mode & O_APPEND || mode & O_TRUNC) {
1398 err = -EROFS;
1399 goto out;
1400 }
1401 }
1402 err = v9fs_co_open(pdu, fidp, flags);
1403 if (err < 0) {
1404 goto out;
1405 }
1406 fidp->fid_type = P9_FID_FILE;
1407 fidp->open_flags = flags;
1408 if (flags & O_EXCL) {
1409 /*
1410 * We let the host file system do O_EXCL check
1411 * We should not reclaim such fd
1412 */
1413 fidp->flags |= FID_NON_RECLAIMABLE;
1414 }
1415 iounit = get_iounit(pdu, &fidp->path);
1416 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1417 if (err < 0) {
1418 goto out;
1419 }
1420 err += offset;
1421 }
1422 trace_v9fs_open_return(pdu->tag, pdu->id,
1423 qid.type, qid.version, qid.path, iounit);
1424 out:
1425 put_fid(pdu, fidp);
1426 out_nofid:
1427 complete_pdu(s, pdu, err);
1428 }
1429
1430 static void v9fs_lcreate(void *opaque)
1431 {
1432 int32_t dfid, flags, mode;
1433 gid_t gid;
1434 ssize_t err = 0;
1435 ssize_t offset = 7;
1436 V9fsString name;
1437 V9fsFidState *fidp;
1438 struct stat stbuf;
1439 V9fsQID qid;
1440 int32_t iounit;
1441 V9fsPDU *pdu = opaque;
1442
1443 v9fs_string_init(&name);
1444 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1445 &name, &flags, &mode, &gid);
1446 if (err < 0) {
1447 goto out_nofid;
1448 }
1449 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1450
1451 fidp = get_fid(pdu, dfid);
1452 if (fidp == NULL) {
1453 err = -ENOENT;
1454 goto out_nofid;
1455 }
1456
1457 flags = get_dotl_openflags(pdu->s, flags);
1458 err = v9fs_co_open2(pdu, fidp, &name, gid,
1459 flags | O_CREAT, mode, &stbuf);
1460 if (err < 0) {
1461 goto out;
1462 }
1463 fidp->fid_type = P9_FID_FILE;
1464 fidp->open_flags = flags;
1465 if (flags & O_EXCL) {
1466 /*
1467 * We let the host file system do O_EXCL check
1468 * We should not reclaim such fd
1469 */
1470 fidp->flags |= FID_NON_RECLAIMABLE;
1471 }
1472 iounit = get_iounit(pdu, &fidp->path);
1473 stat_to_qid(&stbuf, &qid);
1474 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1475 if (err < 0) {
1476 goto out;
1477 }
1478 err += offset;
1479 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1480 qid.type, qid.version, qid.path, iounit);
1481 out:
1482 put_fid(pdu, fidp);
1483 out_nofid:
1484 complete_pdu(pdu->s, pdu, err);
1485 v9fs_string_free(&name);
1486 }
1487
1488 static void v9fs_fsync(void *opaque)
1489 {
1490 int err;
1491 int32_t fid;
1492 int datasync;
1493 size_t offset = 7;
1494 V9fsFidState *fidp;
1495 V9fsPDU *pdu = opaque;
1496 V9fsState *s = pdu->s;
1497
1498 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1499 if (err < 0) {
1500 goto out_nofid;
1501 }
1502 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1503
1504 fidp = get_fid(pdu, fid);
1505 if (fidp == NULL) {
1506 err = -ENOENT;
1507 goto out_nofid;
1508 }
1509 err = v9fs_co_fsync(pdu, fidp, datasync);
1510 if (!err) {
1511 err = offset;
1512 }
1513 put_fid(pdu, fidp);
1514 out_nofid:
1515 complete_pdu(s, pdu, err);
1516 }
1517
1518 static void v9fs_clunk(void *opaque)
1519 {
1520 int err;
1521 int32_t fid;
1522 size_t offset = 7;
1523 V9fsFidState *fidp;
1524 V9fsPDU *pdu = opaque;
1525 V9fsState *s = pdu->s;
1526
1527 err = pdu_unmarshal(pdu, offset, "d", &fid);
1528 if (err < 0) {
1529 goto out_nofid;
1530 }
1531 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1532
1533 fidp = clunk_fid(s, fid);
1534 if (fidp == NULL) {
1535 err = -ENOENT;
1536 goto out_nofid;
1537 }
1538 /*
1539 * Bump the ref so that put_fid will
1540 * free the fid.
1541 */
1542 fidp->ref++;
1543 err = offset;
1544
1545 put_fid(pdu, fidp);
1546 out_nofid:
1547 complete_pdu(s, pdu, err);
1548 }
1549
1550 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1551 uint64_t off, uint32_t max_count)
1552 {
1553 ssize_t err;
1554 size_t offset = 7;
1555 int read_count;
1556 int64_t xattr_len;
1557
1558 xattr_len = fidp->fs.xattr.len;
1559 read_count = xattr_len - off;
1560 if (read_count > max_count) {
1561 read_count = max_count;
1562 } else if (read_count < 0) {
1563 /*
1564 * read beyond XATTR value
1565 */
1566 read_count = 0;
1567 }
1568 err = pdu_marshal(pdu, offset, "d", read_count);
1569 if (err < 0) {
1570 return err;
1571 }
1572 offset += err;
1573 err = v9fs_pack(pdu->elem.in_sg, pdu->elem.in_num, offset,
1574 ((char *)fidp->fs.xattr.value) + off,
1575 read_count);
1576 if (err < 0) {
1577 return err;
1578 }
1579 offset += err;
1580 return offset;
1581 }
1582
1583 static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1584 V9fsFidState *fidp, uint32_t max_count)
1585 {
1586 V9fsPath path;
1587 V9fsStat v9stat;
1588 int len, err = 0;
1589 int32_t count = 0;
1590 struct stat stbuf;
1591 off_t saved_dir_pos;
1592 struct dirent *dent, *result;
1593
1594 /* save the directory position */
1595 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1596 if (saved_dir_pos < 0) {
1597 return saved_dir_pos;
1598 }
1599
1600 dent = g_malloc(sizeof(struct dirent));
1601
1602 while (1) {
1603 v9fs_path_init(&path);
1604 err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1605 if (err || !result) {
1606 break;
1607 }
1608 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1609 if (err < 0) {
1610 goto out;
1611 }
1612 err = v9fs_co_lstat(pdu, &path, &stbuf);
1613 if (err < 0) {
1614 goto out;
1615 }
1616 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1617 if (err < 0) {
1618 goto out;
1619 }
1620 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1621 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1622 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1623 /* Ran out of buffer. Set dir back to old position and return */
1624 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1625 v9fs_stat_free(&v9stat);
1626 v9fs_path_free(&path);
1627 g_free(dent);
1628 return count;
1629 }
1630 count += len;
1631 v9fs_stat_free(&v9stat);
1632 v9fs_path_free(&path);
1633 saved_dir_pos = dent->d_off;
1634 }
1635 out:
1636 g_free(dent);
1637 v9fs_path_free(&path);
1638 if (err < 0) {
1639 return err;
1640 }
1641 return count;
1642 }
1643
1644 /*
1645 * Create a QEMUIOVector for a sub-region of PDU iovecs
1646 *
1647 * @qiov: uninitialized QEMUIOVector
1648 * @skip: number of bytes to skip from beginning of PDU
1649 * @size: number of bytes to include
1650 * @is_write: true - write, false - read
1651 *
1652 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1653 * with qemu_iovec_destroy().
1654 */
1655 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1656 size_t skip, size_t size,
1657 bool is_write)
1658 {
1659 QEMUIOVector elem;
1660 struct iovec *iov;
1661 unsigned int niov;
1662
1663 if (is_write) {
1664 iov = pdu->elem.out_sg;
1665 niov = pdu->elem.out_num;
1666 } else {
1667 iov = pdu->elem.in_sg;
1668 niov = pdu->elem.in_num;
1669 }
1670
1671 qemu_iovec_init_external(&elem, iov, niov);
1672 qemu_iovec_init(qiov, niov);
1673 qemu_iovec_concat(qiov, &elem, skip, size);
1674 }
1675
1676 static void v9fs_read(void *opaque)
1677 {
1678 int32_t fid;
1679 uint64_t off;
1680 ssize_t err = 0;
1681 int32_t count = 0;
1682 size_t offset = 7;
1683 uint32_t max_count;
1684 V9fsFidState *fidp;
1685 V9fsPDU *pdu = opaque;
1686 V9fsState *s = pdu->s;
1687
1688 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1689 if (err < 0) {
1690 goto out_nofid;
1691 }
1692 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1693
1694 fidp = get_fid(pdu, fid);
1695 if (fidp == NULL) {
1696 err = -EINVAL;
1697 goto out_nofid;
1698 }
1699 if (fidp->fid_type == P9_FID_DIR) {
1700
1701 if (off == 0) {
1702 v9fs_co_rewinddir(pdu, fidp);
1703 }
1704 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1705 if (count < 0) {
1706 err = count;
1707 goto out;
1708 }
1709 err = pdu_marshal(pdu, offset, "d", count);
1710 if (err < 0) {
1711 goto out;
1712 }
1713 err += offset + count;
1714 } else if (fidp->fid_type == P9_FID_FILE) {
1715 QEMUIOVector qiov_full;
1716 QEMUIOVector qiov;
1717 int32_t len;
1718
1719 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1720 qemu_iovec_init(&qiov, qiov_full.niov);
1721 do {
1722 qemu_iovec_reset(&qiov);
1723 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1724 if (0) {
1725 print_sg(qiov.iov, qiov.niov);
1726 }
1727 /* Loop in case of EINTR */
1728 do {
1729 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1730 if (len >= 0) {
1731 off += len;
1732 count += len;
1733 }
1734 } while (len == -EINTR && !pdu->cancelled);
1735 if (len < 0) {
1736 /* IO error return the error */
1737 err = len;
1738 goto out;
1739 }
1740 } while (count < max_count && len > 0);
1741 err = pdu_marshal(pdu, offset, "d", count);
1742 if (err < 0) {
1743 goto out;
1744 }
1745 err += offset + count;
1746 qemu_iovec_destroy(&qiov);
1747 qemu_iovec_destroy(&qiov_full);
1748 } else if (fidp->fid_type == P9_FID_XATTR) {
1749 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1750 } else {
1751 err = -EINVAL;
1752 }
1753 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1754 out:
1755 put_fid(pdu, fidp);
1756 out_nofid:
1757 complete_pdu(s, pdu, err);
1758 }
1759
1760 static size_t v9fs_readdir_data_size(V9fsString *name)
1761 {
1762 /*
1763 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1764 * size of type (1) + size of name.size (2) + strlen(name.data)
1765 */
1766 return 24 + v9fs_string_size(name);
1767 }
1768
1769 static int v9fs_do_readdir(V9fsPDU *pdu,
1770 V9fsFidState *fidp, int32_t max_count)
1771 {
1772 size_t size;
1773 V9fsQID qid;
1774 V9fsString name;
1775 int len, err = 0;
1776 int32_t count = 0;
1777 off_t saved_dir_pos;
1778 struct dirent *dent, *result;
1779
1780 /* save the directory position */
1781 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1782 if (saved_dir_pos < 0) {
1783 return saved_dir_pos;
1784 }
1785
1786 dent = g_malloc(sizeof(struct dirent));
1787
1788 while (1) {
1789 err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1790 if (err || !result) {
1791 break;
1792 }
1793 v9fs_string_init(&name);
1794 v9fs_string_sprintf(&name, "%s", dent->d_name);
1795 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1796 /* Ran out of buffer. Set dir back to old position and return */
1797 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1798 v9fs_string_free(&name);
1799 g_free(dent);
1800 return count;
1801 }
1802 /*
1803 * Fill up just the path field of qid because the client uses
1804 * only that. To fill the entire qid structure we will have
1805 * to stat each dirent found, which is expensive
1806 */
1807 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1808 memcpy(&qid.path, &dent->d_ino, size);
1809 /* Fill the other fields with dummy values */
1810 qid.type = 0;
1811 qid.version = 0;
1812
1813 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1814 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1815 &qid, dent->d_off,
1816 dent->d_type, &name);
1817 if (len < 0) {
1818 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1819 v9fs_string_free(&name);
1820 g_free(dent);
1821 return len;
1822 }
1823 count += len;
1824 v9fs_string_free(&name);
1825 saved_dir_pos = dent->d_off;
1826 }
1827 g_free(dent);
1828 if (err < 0) {
1829 return err;
1830 }
1831 return count;
1832 }
1833
1834 static void v9fs_readdir(void *opaque)
1835 {
1836 int32_t fid;
1837 V9fsFidState *fidp;
1838 ssize_t retval = 0;
1839 size_t offset = 7;
1840 uint64_t initial_offset;
1841 int32_t count;
1842 uint32_t max_count;
1843 V9fsPDU *pdu = opaque;
1844 V9fsState *s = pdu->s;
1845
1846 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1847 &initial_offset, &max_count);
1848 if (retval < 0) {
1849 goto out_nofid;
1850 }
1851 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1852
1853 fidp = get_fid(pdu, fid);
1854 if (fidp == NULL) {
1855 retval = -EINVAL;
1856 goto out_nofid;
1857 }
1858 if (!fidp->fs.dir) {
1859 retval = -EINVAL;
1860 goto out;
1861 }
1862 if (initial_offset == 0) {
1863 v9fs_co_rewinddir(pdu, fidp);
1864 } else {
1865 v9fs_co_seekdir(pdu, fidp, initial_offset);
1866 }
1867 count = v9fs_do_readdir(pdu, fidp, max_count);
1868 if (count < 0) {
1869 retval = count;
1870 goto out;
1871 }
1872 retval = pdu_marshal(pdu, offset, "d", count);
1873 if (retval < 0) {
1874 goto out;
1875 }
1876 retval += count + offset;
1877 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1878 out:
1879 put_fid(pdu, fidp);
1880 out_nofid:
1881 complete_pdu(s, pdu, retval);
1882 }
1883
1884 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1885 uint64_t off, uint32_t count,
1886 struct iovec *sg, int cnt)
1887 {
1888 int i, to_copy;
1889 ssize_t err = 0;
1890 int write_count;
1891 int64_t xattr_len;
1892 size_t offset = 7;
1893
1894
1895 xattr_len = fidp->fs.xattr.len;
1896 write_count = xattr_len - off;
1897 if (write_count > count) {
1898 write_count = count;
1899 } else if (write_count < 0) {
1900 /*
1901 * write beyond XATTR value len specified in
1902 * xattrcreate
1903 */
1904 err = -ENOSPC;
1905 goto out;
1906 }
1907 err = pdu_marshal(pdu, offset, "d", write_count);
1908 if (err < 0) {
1909 return err;
1910 }
1911 err += offset;
1912 fidp->fs.xattr.copied_len += write_count;
1913 /*
1914 * Now copy the content from sg list
1915 */
1916 for (i = 0; i < cnt; i++) {
1917 if (write_count > sg[i].iov_len) {
1918 to_copy = sg[i].iov_len;
1919 } else {
1920 to_copy = write_count;
1921 }
1922 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1923 /* updating vs->off since we are not using below */
1924 off += to_copy;
1925 write_count -= to_copy;
1926 }
1927 out:
1928 return err;
1929 }
1930
1931 static void v9fs_write(void *opaque)
1932 {
1933 ssize_t err;
1934 int32_t fid;
1935 uint64_t off;
1936 uint32_t count;
1937 int32_t len = 0;
1938 int32_t total = 0;
1939 size_t offset = 7;
1940 V9fsFidState *fidp;
1941 V9fsPDU *pdu = opaque;
1942 V9fsState *s = pdu->s;
1943 QEMUIOVector qiov_full;
1944 QEMUIOVector qiov;
1945
1946 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
1947 if (err < 0) {
1948 return complete_pdu(s, pdu, err);
1949 }
1950 offset += err;
1951 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
1952 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
1953
1954 fidp = get_fid(pdu, fid);
1955 if (fidp == NULL) {
1956 err = -EINVAL;
1957 goto out_nofid;
1958 }
1959 if (fidp->fid_type == P9_FID_FILE) {
1960 if (fidp->fs.fd == -1) {
1961 err = -EINVAL;
1962 goto out;
1963 }
1964 } else if (fidp->fid_type == P9_FID_XATTR) {
1965 /*
1966 * setxattr operation
1967 */
1968 err = v9fs_xattr_write(s, pdu, fidp, off, count,
1969 qiov_full.iov, qiov_full.niov);
1970 goto out;
1971 } else {
1972 err = -EINVAL;
1973 goto out;
1974 }
1975 qemu_iovec_init(&qiov, qiov_full.niov);
1976 do {
1977 qemu_iovec_reset(&qiov);
1978 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
1979 if (0) {
1980 print_sg(qiov.iov, qiov.niov);
1981 }
1982 /* Loop in case of EINTR */
1983 do {
1984 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
1985 if (len >= 0) {
1986 off += len;
1987 total += len;
1988 }
1989 } while (len == -EINTR && !pdu->cancelled);
1990 if (len < 0) {
1991 /* IO error return the error */
1992 err = len;
1993 goto out_qiov;
1994 }
1995 } while (total < count && len > 0);
1996
1997 offset = 7;
1998 err = pdu_marshal(pdu, offset, "d", total);
1999 if (err < 0) {
2000 goto out;
2001 }
2002 err += offset;
2003 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2004 out_qiov:
2005 qemu_iovec_destroy(&qiov);
2006 out:
2007 put_fid(pdu, fidp);
2008 out_nofid:
2009 qemu_iovec_destroy(&qiov_full);
2010 complete_pdu(s, pdu, err);
2011 }
2012
2013 static void v9fs_create(void *opaque)
2014 {
2015 int32_t fid;
2016 int err = 0;
2017 size_t offset = 7;
2018 V9fsFidState *fidp;
2019 V9fsQID qid;
2020 int32_t perm;
2021 int8_t mode;
2022 V9fsPath path;
2023 struct stat stbuf;
2024 V9fsString name;
2025 V9fsString extension;
2026 int iounit;
2027 V9fsPDU *pdu = opaque;
2028
2029 v9fs_path_init(&path);
2030 v9fs_string_init(&name);
2031 v9fs_string_init(&extension);
2032 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2033 &perm, &mode, &extension);
2034 if (err < 0) {
2035 goto out_nofid;
2036 }
2037 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2038
2039 fidp = get_fid(pdu, fid);
2040 if (fidp == NULL) {
2041 err = -EINVAL;
2042 goto out_nofid;
2043 }
2044 if (perm & P9_STAT_MODE_DIR) {
2045 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2046 fidp->uid, -1, &stbuf);
2047 if (err < 0) {
2048 goto out;
2049 }
2050 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2051 if (err < 0) {
2052 goto out;
2053 }
2054 v9fs_path_copy(&fidp->path, &path);
2055 err = v9fs_co_opendir(pdu, fidp);
2056 if (err < 0) {
2057 goto out;
2058 }
2059 fidp->fid_type = P9_FID_DIR;
2060 } else if (perm & P9_STAT_MODE_SYMLINK) {
2061 err = v9fs_co_symlink(pdu, fidp, &name,
2062 extension.data, -1 , &stbuf);
2063 if (err < 0) {
2064 goto out;
2065 }
2066 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2067 if (err < 0) {
2068 goto out;
2069 }
2070 v9fs_path_copy(&fidp->path, &path);
2071 } else if (perm & P9_STAT_MODE_LINK) {
2072 int32_t ofid = atoi(extension.data);
2073 V9fsFidState *ofidp = get_fid(pdu, ofid);
2074 if (ofidp == NULL) {
2075 err = -EINVAL;
2076 goto out;
2077 }
2078 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2079 put_fid(pdu, ofidp);
2080 if (err < 0) {
2081 goto out;
2082 }
2083 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2084 if (err < 0) {
2085 fidp->fid_type = P9_FID_NONE;
2086 goto out;
2087 }
2088 v9fs_path_copy(&fidp->path, &path);
2089 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2090 if (err < 0) {
2091 fidp->fid_type = P9_FID_NONE;
2092 goto out;
2093 }
2094 } else if (perm & P9_STAT_MODE_DEVICE) {
2095 char ctype;
2096 uint32_t major, minor;
2097 mode_t nmode = 0;
2098
2099 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2100 err = -errno;
2101 goto out;
2102 }
2103
2104 switch (ctype) {
2105 case 'c':
2106 nmode = S_IFCHR;
2107 break;
2108 case 'b':
2109 nmode = S_IFBLK;
2110 break;
2111 default:
2112 err = -EIO;
2113 goto out;
2114 }
2115
2116 nmode |= perm & 0777;
2117 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2118 makedev(major, minor), nmode, &stbuf);
2119 if (err < 0) {
2120 goto out;
2121 }
2122 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2123 if (err < 0) {
2124 goto out;
2125 }
2126 v9fs_path_copy(&fidp->path, &path);
2127 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2128 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2129 0, S_IFIFO | (perm & 0777), &stbuf);
2130 if (err < 0) {
2131 goto out;
2132 }
2133 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2134 if (err < 0) {
2135 goto out;
2136 }
2137 v9fs_path_copy(&fidp->path, &path);
2138 } else if (perm & P9_STAT_MODE_SOCKET) {
2139 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2140 0, S_IFSOCK | (perm & 0777), &stbuf);
2141 if (err < 0) {
2142 goto out;
2143 }
2144 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2145 if (err < 0) {
2146 goto out;
2147 }
2148 v9fs_path_copy(&fidp->path, &path);
2149 } else {
2150 err = v9fs_co_open2(pdu, fidp, &name, -1,
2151 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2152 if (err < 0) {
2153 goto out;
2154 }
2155 fidp->fid_type = P9_FID_FILE;
2156 fidp->open_flags = omode_to_uflags(mode);
2157 if (fidp->open_flags & O_EXCL) {
2158 /*
2159 * We let the host file system do O_EXCL check
2160 * We should not reclaim such fd
2161 */
2162 fidp->flags |= FID_NON_RECLAIMABLE;
2163 }
2164 }
2165 iounit = get_iounit(pdu, &fidp->path);
2166 stat_to_qid(&stbuf, &qid);
2167 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2168 if (err < 0) {
2169 goto out;
2170 }
2171 err += offset;
2172 trace_v9fs_create_return(pdu->tag, pdu->id,
2173 qid.type, qid.version, qid.path, iounit);
2174 out:
2175 put_fid(pdu, fidp);
2176 out_nofid:
2177 complete_pdu(pdu->s, pdu, err);
2178 v9fs_string_free(&name);
2179 v9fs_string_free(&extension);
2180 v9fs_path_free(&path);
2181 }
2182
2183 static void v9fs_symlink(void *opaque)
2184 {
2185 V9fsPDU *pdu = opaque;
2186 V9fsString name;
2187 V9fsString symname;
2188 V9fsFidState *dfidp;
2189 V9fsQID qid;
2190 struct stat stbuf;
2191 int32_t dfid;
2192 int err = 0;
2193 gid_t gid;
2194 size_t offset = 7;
2195
2196 v9fs_string_init(&name);
2197 v9fs_string_init(&symname);
2198 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2199 if (err < 0) {
2200 goto out_nofid;
2201 }
2202 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2203
2204 dfidp = get_fid(pdu, dfid);
2205 if (dfidp == NULL) {
2206 err = -EINVAL;
2207 goto out_nofid;
2208 }
2209 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2210 if (err < 0) {
2211 goto out;
2212 }
2213 stat_to_qid(&stbuf, &qid);
2214 err = pdu_marshal(pdu, offset, "Q", &qid);
2215 if (err < 0) {
2216 goto out;
2217 }
2218 err += offset;
2219 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2220 qid.type, qid.version, qid.path);
2221 out:
2222 put_fid(pdu, dfidp);
2223 out_nofid:
2224 complete_pdu(pdu->s, pdu, err);
2225 v9fs_string_free(&name);
2226 v9fs_string_free(&symname);
2227 }
2228
2229 static void v9fs_flush(void *opaque)
2230 {
2231 ssize_t err;
2232 int16_t tag;
2233 size_t offset = 7;
2234 V9fsPDU *cancel_pdu;
2235 V9fsPDU *pdu = opaque;
2236 V9fsState *s = pdu->s;
2237
2238 err = pdu_unmarshal(pdu, offset, "w", &tag);
2239 if (err < 0) {
2240 complete_pdu(s, pdu, err);
2241 return;
2242 }
2243 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2244
2245 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2246 if (cancel_pdu->tag == tag) {
2247 break;
2248 }
2249 }
2250 if (cancel_pdu) {
2251 cancel_pdu->cancelled = 1;
2252 /*
2253 * Wait for pdu to complete.
2254 */
2255 qemu_co_queue_wait(&cancel_pdu->complete);
2256 cancel_pdu->cancelled = 0;
2257 free_pdu(pdu->s, cancel_pdu);
2258 }
2259 complete_pdu(s, pdu, 7);
2260 return;
2261 }
2262
2263 static void v9fs_link(void *opaque)
2264 {
2265 V9fsPDU *pdu = opaque;
2266 V9fsState *s = pdu->s;
2267 int32_t dfid, oldfid;
2268 V9fsFidState *dfidp, *oldfidp;
2269 V9fsString name;
2270 size_t offset = 7;
2271 int err = 0;
2272
2273 v9fs_string_init(&name);
2274 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2275 if (err < 0) {
2276 goto out_nofid;
2277 }
2278 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2279
2280 dfidp = get_fid(pdu, dfid);
2281 if (dfidp == NULL) {
2282 err = -ENOENT;
2283 goto out_nofid;
2284 }
2285
2286 oldfidp = get_fid(pdu, oldfid);
2287 if (oldfidp == NULL) {
2288 err = -ENOENT;
2289 goto out;
2290 }
2291 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2292 if (!err) {
2293 err = offset;
2294 }
2295 out:
2296 put_fid(pdu, dfidp);
2297 out_nofid:
2298 v9fs_string_free(&name);
2299 complete_pdu(s, pdu, err);
2300 }
2301
2302 /* Only works with path name based fid */
2303 static void v9fs_remove(void *opaque)
2304 {
2305 int32_t fid;
2306 int err = 0;
2307 size_t offset = 7;
2308 V9fsFidState *fidp;
2309 V9fsPDU *pdu = opaque;
2310
2311 err = pdu_unmarshal(pdu, offset, "d", &fid);
2312 if (err < 0) {
2313 goto out_nofid;
2314 }
2315 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2316
2317 fidp = get_fid(pdu, fid);
2318 if (fidp == NULL) {
2319 err = -EINVAL;
2320 goto out_nofid;
2321 }
2322 /* if fs driver is not path based, return EOPNOTSUPP */
2323 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2324 err = -EOPNOTSUPP;
2325 goto out_err;
2326 }
2327 /*
2328 * IF the file is unlinked, we cannot reopen
2329 * the file later. So don't reclaim fd
2330 */
2331 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2332 if (err < 0) {
2333 goto out_err;
2334 }
2335 err = v9fs_co_remove(pdu, &fidp->path);
2336 if (!err) {
2337 err = offset;
2338 }
2339 out_err:
2340 /* For TREMOVE we need to clunk the fid even on failed remove */
2341 clunk_fid(pdu->s, fidp->fid);
2342 put_fid(pdu, fidp);
2343 out_nofid:
2344 complete_pdu(pdu->s, pdu, err);
2345 }
2346
2347 static void v9fs_unlinkat(void *opaque)
2348 {
2349 int err = 0;
2350 V9fsString name;
2351 int32_t dfid, flags;
2352 size_t offset = 7;
2353 V9fsPath path;
2354 V9fsFidState *dfidp;
2355 V9fsPDU *pdu = opaque;
2356
2357 v9fs_string_init(&name);
2358 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2359 if (err < 0) {
2360 goto out_nofid;
2361 }
2362 dfidp = get_fid(pdu, dfid);
2363 if (dfidp == NULL) {
2364 err = -EINVAL;
2365 goto out_nofid;
2366 }
2367 /*
2368 * IF the file is unlinked, we cannot reopen
2369 * the file later. So don't reclaim fd
2370 */
2371 v9fs_path_init(&path);
2372 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2373 if (err < 0) {
2374 goto out_err;
2375 }
2376 err = v9fs_mark_fids_unreclaim(pdu, &path);
2377 if (err < 0) {
2378 goto out_err;
2379 }
2380 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2381 if (!err) {
2382 err = offset;
2383 }
2384 out_err:
2385 put_fid(pdu, dfidp);
2386 v9fs_path_free(&path);
2387 out_nofid:
2388 complete_pdu(pdu->s, pdu, err);
2389 v9fs_string_free(&name);
2390 }
2391
2392
2393 /* Only works with path name based fid */
2394 static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2395 int32_t newdirfid, V9fsString *name)
2396 {
2397 char *end;
2398 int err = 0;
2399 V9fsPath new_path;
2400 V9fsFidState *tfidp;
2401 V9fsState *s = pdu->s;
2402 V9fsFidState *dirfidp = NULL;
2403 char *old_name, *new_name;
2404
2405 v9fs_path_init(&new_path);
2406 if (newdirfid != -1) {
2407 dirfidp = get_fid(pdu, newdirfid);
2408 if (dirfidp == NULL) {
2409 err = -ENOENT;
2410 goto out_nofid;
2411 }
2412 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2413 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2414 } else {
2415 old_name = fidp->path.data;
2416 end = strrchr(old_name, '/');
2417 if (end) {
2418 end++;
2419 } else {
2420 end = old_name;
2421 }
2422 new_name = g_malloc0(end - old_name + name->size + 1);
2423 strncat(new_name, old_name, end - old_name);
2424 strncat(new_name + (end - old_name), name->data, name->size);
2425 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2426 g_free(new_name);
2427 }
2428 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2429 if (err < 0) {
2430 goto out;
2431 }
2432 /*
2433 * Fixup fid's pointing to the old name to
2434 * start pointing to the new name
2435 */
2436 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2437 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2438 /* replace the name */
2439 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2440 }
2441 }
2442 out:
2443 if (dirfidp) {
2444 put_fid(pdu, dirfidp);
2445 }
2446 v9fs_path_free(&new_path);
2447 out_nofid:
2448 return err;
2449 }
2450
2451 /* Only works with path name based fid */
2452 static void v9fs_rename(void *opaque)
2453 {
2454 int32_t fid;
2455 ssize_t err = 0;
2456 size_t offset = 7;
2457 V9fsString name;
2458 int32_t newdirfid;
2459 V9fsFidState *fidp;
2460 V9fsPDU *pdu = opaque;
2461 V9fsState *s = pdu->s;
2462
2463 v9fs_string_init(&name);
2464 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2465 if (err < 0) {
2466 goto out_nofid;
2467 }
2468 fidp = get_fid(pdu, fid);
2469 if (fidp == NULL) {
2470 err = -ENOENT;
2471 goto out_nofid;
2472 }
2473 BUG_ON(fidp->fid_type != P9_FID_NONE);
2474 /* if fs driver is not path based, return EOPNOTSUPP */
2475 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2476 err = -EOPNOTSUPP;
2477 goto out;
2478 }
2479 v9fs_path_write_lock(s);
2480 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2481 v9fs_path_unlock(s);
2482 if (!err) {
2483 err = offset;
2484 }
2485 out:
2486 put_fid(pdu, fidp);
2487 out_nofid:
2488 complete_pdu(s, pdu, err);
2489 v9fs_string_free(&name);
2490 }
2491
2492 static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2493 V9fsString *old_name, V9fsPath *newdir,
2494 V9fsString *new_name)
2495 {
2496 V9fsFidState *tfidp;
2497 V9fsPath oldpath, newpath;
2498 V9fsState *s = pdu->s;
2499
2500
2501 v9fs_path_init(&oldpath);
2502 v9fs_path_init(&newpath);
2503 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2504 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2505
2506 /*
2507 * Fixup fid's pointing to the old name to
2508 * start pointing to the new name
2509 */
2510 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2511 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2512 /* replace the name */
2513 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2514 }
2515 }
2516 v9fs_path_free(&oldpath);
2517 v9fs_path_free(&newpath);
2518 }
2519
2520 static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2521 V9fsString *old_name, int32_t newdirfid,
2522 V9fsString *new_name)
2523 {
2524 int err = 0;
2525 V9fsState *s = pdu->s;
2526 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2527
2528 olddirfidp = get_fid(pdu, olddirfid);
2529 if (olddirfidp == NULL) {
2530 err = -ENOENT;
2531 goto out;
2532 }
2533 if (newdirfid != -1) {
2534 newdirfidp = get_fid(pdu, newdirfid);
2535 if (newdirfidp == NULL) {
2536 err = -ENOENT;
2537 goto out;
2538 }
2539 } else {
2540 newdirfidp = get_fid(pdu, olddirfid);
2541 }
2542
2543 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2544 &newdirfidp->path, new_name);
2545 if (err < 0) {
2546 goto out;
2547 }
2548 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2549 /* Only for path based fid we need to do the below fixup */
2550 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2551 &newdirfidp->path, new_name);
2552 }
2553 out:
2554 if (olddirfidp) {
2555 put_fid(pdu, olddirfidp);
2556 }
2557 if (newdirfidp) {
2558 put_fid(pdu, newdirfidp);
2559 }
2560 return err;
2561 }
2562
2563 static void v9fs_renameat(void *opaque)
2564 {
2565 ssize_t err = 0;
2566 size_t offset = 7;
2567 V9fsPDU *pdu = opaque;
2568 V9fsState *s = pdu->s;
2569 int32_t olddirfid, newdirfid;
2570 V9fsString old_name, new_name;
2571
2572 v9fs_string_init(&old_name);
2573 v9fs_string_init(&new_name);
2574 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2575 &old_name, &newdirfid, &new_name);
2576 if (err < 0) {
2577 goto out_err;
2578 }
2579
2580 v9fs_path_write_lock(s);
2581 err = v9fs_complete_renameat(pdu, olddirfid,
2582 &old_name, newdirfid, &new_name);
2583 v9fs_path_unlock(s);
2584 if (!err) {
2585 err = offset;
2586 }
2587
2588 out_err:
2589 complete_pdu(s, pdu, err);
2590 v9fs_string_free(&old_name);
2591 v9fs_string_free(&new_name);
2592 }
2593
2594 static void v9fs_wstat(void *opaque)
2595 {
2596 int32_t fid;
2597 int err = 0;
2598 int16_t unused;
2599 V9fsStat v9stat;
2600 size_t offset = 7;
2601 struct stat stbuf;
2602 V9fsFidState *fidp;
2603 V9fsPDU *pdu = opaque;
2604 V9fsState *s = pdu->s;
2605
2606 v9fs_stat_init(&v9stat);
2607 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2608 if (err < 0) {
2609 goto out_nofid;
2610 }
2611 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2612 v9stat.mode, v9stat.atime, v9stat.mtime);
2613
2614 fidp = get_fid(pdu, fid);
2615 if (fidp == NULL) {
2616 err = -EINVAL;
2617 goto out_nofid;
2618 }
2619 /* do we need to sync the file? */
2620 if (donttouch_stat(&v9stat)) {
2621 err = v9fs_co_fsync(pdu, fidp, 0);
2622 goto out;
2623 }
2624 if (v9stat.mode != -1) {
2625 uint32_t v9_mode;
2626 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2627 if (err < 0) {
2628 goto out;
2629 }
2630 v9_mode = stat_to_v9mode(&stbuf);
2631 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2632 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2633 /* Attempting to change the type */
2634 err = -EIO;
2635 goto out;
2636 }
2637 err = v9fs_co_chmod(pdu, &fidp->path,
2638 v9mode_to_mode(v9stat.mode,
2639 &v9stat.extension));
2640 if (err < 0) {
2641 goto out;
2642 }
2643 }
2644 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2645 struct timespec times[2];
2646 if (v9stat.atime != -1) {
2647 times[0].tv_sec = v9stat.atime;
2648 times[0].tv_nsec = 0;
2649 } else {
2650 times[0].tv_nsec = UTIME_OMIT;
2651 }
2652 if (v9stat.mtime != -1) {
2653 times[1].tv_sec = v9stat.mtime;
2654 times[1].tv_nsec = 0;
2655 } else {
2656 times[1].tv_nsec = UTIME_OMIT;
2657 }
2658 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2659 if (err < 0) {
2660 goto out;
2661 }
2662 }
2663 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2664 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2665 if (err < 0) {
2666 goto out;
2667 }
2668 }
2669 if (v9stat.name.size != 0) {
2670 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2671 if (err < 0) {
2672 goto out;
2673 }
2674 }
2675 if (v9stat.length != -1) {
2676 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2677 if (err < 0) {
2678 goto out;
2679 }
2680 }
2681 err = offset;
2682 out:
2683 put_fid(pdu, fidp);
2684 out_nofid:
2685 v9fs_stat_free(&v9stat);
2686 complete_pdu(s, pdu, err);
2687 }
2688
2689 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2690 {
2691 uint32_t f_type;
2692 uint32_t f_bsize;
2693 uint64_t f_blocks;
2694 uint64_t f_bfree;
2695 uint64_t f_bavail;
2696 uint64_t f_files;
2697 uint64_t f_ffree;
2698 uint64_t fsid_val;
2699 uint32_t f_namelen;
2700 size_t offset = 7;
2701 int32_t bsize_factor;
2702
2703 /*
2704 * compute bsize factor based on host file system block size
2705 * and client msize
2706 */
2707 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2708 if (!bsize_factor) {
2709 bsize_factor = 1;
2710 }
2711 f_type = stbuf->f_type;
2712 f_bsize = stbuf->f_bsize;
2713 f_bsize *= bsize_factor;
2714 /*
2715 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2716 * adjust(divide) the number of blocks, free blocks and available
2717 * blocks by bsize factor
2718 */
2719 f_blocks = stbuf->f_blocks/bsize_factor;
2720 f_bfree = stbuf->f_bfree/bsize_factor;
2721 f_bavail = stbuf->f_bavail/bsize_factor;
2722 f_files = stbuf->f_files;
2723 f_ffree = stbuf->f_ffree;
2724 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2725 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2726 f_namelen = stbuf->f_namelen;
2727
2728 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2729 f_type, f_bsize, f_blocks, f_bfree,
2730 f_bavail, f_files, f_ffree,
2731 fsid_val, f_namelen);
2732 }
2733
2734 static void v9fs_statfs(void *opaque)
2735 {
2736 int32_t fid;
2737 ssize_t retval = 0;
2738 size_t offset = 7;
2739 V9fsFidState *fidp;
2740 struct statfs stbuf;
2741 V9fsPDU *pdu = opaque;
2742 V9fsState *s = pdu->s;
2743
2744 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2745 if (retval < 0) {
2746 goto out_nofid;
2747 }
2748 fidp = get_fid(pdu, fid);
2749 if (fidp == NULL) {
2750 retval = -ENOENT;
2751 goto out_nofid;
2752 }
2753 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2754 if (retval < 0) {
2755 goto out;
2756 }
2757 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2758 if (retval < 0) {
2759 goto out;
2760 }
2761 retval += offset;
2762 out:
2763 put_fid(pdu, fidp);
2764 out_nofid:
2765 complete_pdu(s, pdu, retval);
2766 return;
2767 }
2768
2769 static void v9fs_mknod(void *opaque)
2770 {
2771
2772 int mode;
2773 gid_t gid;
2774 int32_t fid;
2775 V9fsQID qid;
2776 int err = 0;
2777 int major, minor;
2778 size_t offset = 7;
2779 V9fsString name;
2780 struct stat stbuf;
2781 V9fsFidState *fidp;
2782 V9fsPDU *pdu = opaque;
2783 V9fsState *s = pdu->s;
2784
2785 v9fs_string_init(&name);
2786 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2787 &major, &minor, &gid);
2788 if (err < 0) {
2789 goto out_nofid;
2790 }
2791 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2792
2793 fidp = get_fid(pdu, fid);
2794 if (fidp == NULL) {
2795 err = -ENOENT;
2796 goto out_nofid;
2797 }
2798 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2799 makedev(major, minor), mode, &stbuf);
2800 if (err < 0) {
2801 goto out;
2802 }
2803 stat_to_qid(&stbuf, &qid);
2804 err = pdu_marshal(pdu, offset, "Q", &qid);
2805 if (err < 0) {
2806 goto out;
2807 }
2808 err += offset;
2809 trace_v9fs_mknod_return(pdu->tag, pdu->id,
2810 qid.type, qid.version, qid.path);
2811 out:
2812 put_fid(pdu, fidp);
2813 out_nofid:
2814 complete_pdu(s, pdu, err);
2815 v9fs_string_free(&name);
2816 }
2817
2818 /*
2819 * Implement posix byte range locking code
2820 * Server side handling of locking code is very simple, because 9p server in
2821 * QEMU can handle only one client. And most of the lock handling
2822 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2823 * do any thing in * qemu 9p server side lock code path.
2824 * So when a TLOCK request comes, always return success
2825 */
2826 static void v9fs_lock(void *opaque)
2827 {
2828 int8_t status;
2829 V9fsFlock flock;
2830 size_t offset = 7;
2831 struct stat stbuf;
2832 V9fsFidState *fidp;
2833 int32_t fid, err = 0;
2834 V9fsPDU *pdu = opaque;
2835 V9fsState *s = pdu->s;
2836
2837 status = P9_LOCK_ERROR;
2838 v9fs_string_init(&flock.client_id);
2839 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
2840 &flock.flags, &flock.start, &flock.length,
2841 &flock.proc_id, &flock.client_id);
2842 if (err < 0) {
2843 goto out_nofid;
2844 }
2845 trace_v9fs_lock(pdu->tag, pdu->id, fid,
2846 flock.type, flock.start, flock.length);
2847
2848
2849 /* We support only block flag now (that too ignored currently) */
2850 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
2851 err = -EINVAL;
2852 goto out_nofid;
2853 }
2854 fidp = get_fid(pdu, fid);
2855 if (fidp == NULL) {
2856 err = -ENOENT;
2857 goto out_nofid;
2858 }
2859 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2860 if (err < 0) {
2861 goto out;
2862 }
2863 status = P9_LOCK_SUCCESS;
2864 out:
2865 put_fid(pdu, fidp);
2866 out_nofid:
2867 err = pdu_marshal(pdu, offset, "b", status);
2868 if (err > 0) {
2869 err += offset;
2870 }
2871 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
2872 complete_pdu(s, pdu, err);
2873 v9fs_string_free(&flock.client_id);
2874 }
2875
2876 /*
2877 * When a TGETLOCK request comes, always return success because all lock
2878 * handling is done by client's VFS layer.
2879 */
2880 static void v9fs_getlock(void *opaque)
2881 {
2882 size_t offset = 7;
2883 struct stat stbuf;
2884 V9fsFidState *fidp;
2885 V9fsGetlock glock;
2886 int32_t fid, err = 0;
2887 V9fsPDU *pdu = opaque;
2888 V9fsState *s = pdu->s;
2889
2890 v9fs_string_init(&glock.client_id);
2891 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
2892 &glock.start, &glock.length, &glock.proc_id,
2893 &glock.client_id);
2894 if (err < 0) {
2895 goto out_nofid;
2896 }
2897 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
2898 glock.type, glock.start, glock.length);
2899
2900 fidp = get_fid(pdu, fid);
2901 if (fidp == NULL) {
2902 err = -ENOENT;
2903 goto out_nofid;
2904 }
2905 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2906 if (err < 0) {
2907 goto out;
2908 }
2909 glock.type = P9_LOCK_TYPE_UNLCK;
2910 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
2911 glock.start, glock.length, glock.proc_id,
2912 &glock.client_id);
2913 if (err < 0) {
2914 goto out;
2915 }
2916 err += offset;
2917 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
2918 glock.length, glock.proc_id);
2919 out:
2920 put_fid(pdu, fidp);
2921 out_nofid:
2922 complete_pdu(s, pdu, err);
2923 v9fs_string_free(&glock.client_id);
2924 }
2925
2926 static void v9fs_mkdir(void *opaque)
2927 {
2928 V9fsPDU *pdu = opaque;
2929 size_t offset = 7;
2930 int32_t fid;
2931 struct stat stbuf;
2932 V9fsQID qid;
2933 V9fsString name;
2934 V9fsFidState *fidp;
2935 gid_t gid;
2936 int mode;
2937 int err = 0;
2938
2939 v9fs_string_init(&name);
2940 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2941 if (err < 0) {
2942 goto out_nofid;
2943 }
2944 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
2945
2946 fidp = get_fid(pdu, fid);
2947 if (fidp == NULL) {
2948 err = -ENOENT;
2949 goto out_nofid;
2950 }
2951 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
2952 if (err < 0) {
2953 goto out;
2954 }
2955 stat_to_qid(&stbuf, &qid);
2956 err = pdu_marshal(pdu, offset, "Q", &qid);
2957 if (err < 0) {
2958 goto out;
2959 }
2960 err += offset;
2961 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
2962 qid.type, qid.version, qid.path, err);
2963 out:
2964 put_fid(pdu, fidp);
2965 out_nofid:
2966 complete_pdu(pdu->s, pdu, err);
2967 v9fs_string_free(&name);
2968 }
2969
2970 static void v9fs_xattrwalk(void *opaque)
2971 {
2972 int64_t size;
2973 V9fsString name;
2974 ssize_t err = 0;
2975 size_t offset = 7;
2976 int32_t fid, newfid;
2977 V9fsFidState *file_fidp;
2978 V9fsFidState *xattr_fidp = NULL;
2979 V9fsPDU *pdu = opaque;
2980 V9fsState *s = pdu->s;
2981
2982 v9fs_string_init(&name);
2983 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2984 if (err < 0) {
2985 goto out_nofid;
2986 }
2987 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
2988
2989 file_fidp = get_fid(pdu, fid);
2990 if (file_fidp == NULL) {
2991 err = -ENOENT;
2992 goto out_nofid;
2993 }
2994 xattr_fidp = alloc_fid(s, newfid);
2995 if (xattr_fidp == NULL) {
2996 err = -EINVAL;
2997 goto out;
2998 }
2999 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3000 if (name.data == NULL) {
3001 /*
3002 * listxattr request. Get the size first
3003 */
3004 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3005 if (size < 0) {
3006 err = size;
3007 clunk_fid(s, xattr_fidp->fid);
3008 goto out;
3009 }
3010 /*
3011 * Read the xattr value
3012 */
3013 xattr_fidp->fs.xattr.len = size;
3014 xattr_fidp->fid_type = P9_FID_XATTR;
3015 xattr_fidp->fs.xattr.copied_len = -1;
3016 if (size) {
3017 xattr_fidp->fs.xattr.value = g_malloc(size);
3018 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3019 xattr_fidp->fs.xattr.value,
3020 xattr_fidp->fs.xattr.len);
3021 if (err < 0) {
3022 clunk_fid(s, xattr_fidp->fid);
3023 goto out;
3024 }
3025 }
3026 err = pdu_marshal(pdu, offset, "q", size);
3027 if (err < 0) {
3028 goto out;
3029 }
3030 err += offset;
3031 } else {
3032 /*
3033 * specific xattr fid. We check for xattr
3034 * presence also collect the xattr size
3035 */
3036 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3037 &name, NULL, 0);
3038 if (size < 0) {
3039 err = size;
3040 clunk_fid(s, xattr_fidp->fid);
3041 goto out;
3042 }
3043 /*
3044 * Read the xattr value
3045 */
3046 xattr_fidp->fs.xattr.len = size;
3047 xattr_fidp->fid_type = P9_FID_XATTR;
3048 xattr_fidp->fs.xattr.copied_len = -1;
3049 if (size) {
3050 xattr_fidp->fs.xattr.value = g_malloc(size);
3051 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3052 &name, xattr_fidp->fs.xattr.value,
3053 xattr_fidp->fs.xattr.len);
3054 if (err < 0) {
3055 clunk_fid(s, xattr_fidp->fid);
3056 goto out;
3057 }
3058 }
3059 err = pdu_marshal(pdu, offset, "q", size);
3060 if (err < 0) {
3061 goto out;
3062 }
3063 err += offset;
3064 }
3065 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3066 out:
3067 put_fid(pdu, file_fidp);
3068 if (xattr_fidp) {
3069 put_fid(pdu, xattr_fidp);
3070 }
3071 out_nofid:
3072 complete_pdu(s, pdu, err);
3073 v9fs_string_free(&name);
3074 }
3075
3076 static void v9fs_xattrcreate(void *opaque)
3077 {
3078 int flags;
3079 int32_t fid;
3080 int64_t size;
3081 ssize_t err = 0;
3082 V9fsString name;
3083 size_t offset = 7;
3084 V9fsFidState *file_fidp;
3085 V9fsFidState *xattr_fidp;
3086 V9fsPDU *pdu = opaque;
3087 V9fsState *s = pdu->s;
3088
3089 v9fs_string_init(&name);
3090 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3091 if (err < 0) {
3092 goto out_nofid;
3093 }
3094 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3095
3096 file_fidp = get_fid(pdu, fid);
3097 if (file_fidp == NULL) {
3098 err = -EINVAL;
3099 goto out_nofid;
3100 }
3101 /* Make the file fid point to xattr */
3102 xattr_fidp = file_fidp;
3103 xattr_fidp->fid_type = P9_FID_XATTR;
3104 xattr_fidp->fs.xattr.copied_len = 0;
3105 xattr_fidp->fs.xattr.len = size;
3106 xattr_fidp->fs.xattr.flags = flags;
3107 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3108 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3109 if (size) {
3110 xattr_fidp->fs.xattr.value = g_malloc(size);
3111 } else {
3112 xattr_fidp->fs.xattr.value = NULL;
3113 }
3114 err = offset;
3115 put_fid(pdu, file_fidp);
3116 out_nofid:
3117 complete_pdu(s, pdu, err);
3118 v9fs_string_free(&name);
3119 }
3120
3121 static void v9fs_readlink(void *opaque)
3122 {
3123 V9fsPDU *pdu = opaque;
3124 size_t offset = 7;
3125 V9fsString target;
3126 int32_t fid;
3127 int err = 0;
3128 V9fsFidState *fidp;
3129
3130 err = pdu_unmarshal(pdu, offset, "d", &fid);
3131 if (err < 0) {
3132 goto out_nofid;
3133 }
3134 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3135 fidp = get_fid(pdu, fid);
3136 if (fidp == NULL) {
3137 err = -ENOENT;
3138 goto out_nofid;
3139 }
3140
3141 v9fs_string_init(&target);
3142 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3143 if (err < 0) {
3144 goto out;
3145 }
3146 err = pdu_marshal(pdu, offset, "s", &target);
3147 if (err < 0) {
3148 v9fs_string_free(&target);
3149 goto out;
3150 }
3151 err += offset;
3152 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3153 v9fs_string_free(&target);
3154 out:
3155 put_fid(pdu, fidp);
3156 out_nofid:
3157 complete_pdu(pdu->s, pdu, err);
3158 }
3159
3160 static CoroutineEntry *pdu_co_handlers[] = {
3161 [P9_TREADDIR] = v9fs_readdir,
3162 [P9_TSTATFS] = v9fs_statfs,
3163 [P9_TGETATTR] = v9fs_getattr,
3164 [P9_TSETATTR] = v9fs_setattr,
3165 [P9_TXATTRWALK] = v9fs_xattrwalk,
3166 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3167 [P9_TMKNOD] = v9fs_mknod,
3168 [P9_TRENAME] = v9fs_rename,
3169 [P9_TLOCK] = v9fs_lock,
3170 [P9_TGETLOCK] = v9fs_getlock,
3171 [P9_TRENAMEAT] = v9fs_renameat,
3172 [P9_TREADLINK] = v9fs_readlink,
3173 [P9_TUNLINKAT] = v9fs_unlinkat,
3174 [P9_TMKDIR] = v9fs_mkdir,
3175 [P9_TVERSION] = v9fs_version,
3176 [P9_TLOPEN] = v9fs_open,
3177 [P9_TATTACH] = v9fs_attach,
3178 [P9_TSTAT] = v9fs_stat,
3179 [P9_TWALK] = v9fs_walk,
3180 [P9_TCLUNK] = v9fs_clunk,
3181 [P9_TFSYNC] = v9fs_fsync,
3182 [P9_TOPEN] = v9fs_open,
3183 [P9_TREAD] = v9fs_read,
3184 #if 0
3185 [P9_TAUTH] = v9fs_auth,
3186 #endif
3187 [P9_TFLUSH] = v9fs_flush,
3188 [P9_TLINK] = v9fs_link,
3189 [P9_TSYMLINK] = v9fs_symlink,
3190 [P9_TCREATE] = v9fs_create,
3191 [P9_TLCREATE] = v9fs_lcreate,
3192 [P9_TWRITE] = v9fs_write,
3193 [P9_TWSTAT] = v9fs_wstat,
3194 [P9_TREMOVE] = v9fs_remove,
3195 };
3196
3197 static void v9fs_op_not_supp(void *opaque)
3198 {
3199 V9fsPDU *pdu = opaque;
3200 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3201 }
3202
3203 static void v9fs_fs_ro(void *opaque)
3204 {
3205 V9fsPDU *pdu = opaque;
3206 complete_pdu(pdu->s, pdu, -EROFS);
3207 }
3208
3209 static inline bool is_read_only_op(V9fsPDU *pdu)
3210 {
3211 switch (pdu->id) {
3212 case P9_TREADDIR:
3213 case P9_TSTATFS:
3214 case P9_TGETATTR:
3215 case P9_TXATTRWALK:
3216 case P9_TLOCK:
3217 case P9_TGETLOCK:
3218 case P9_TREADLINK:
3219 case P9_TVERSION:
3220 case P9_TLOPEN:
3221 case P9_TATTACH:
3222 case P9_TSTAT:
3223 case P9_TWALK:
3224 case P9_TCLUNK:
3225 case P9_TFSYNC:
3226 case P9_TOPEN:
3227 case P9_TREAD:
3228 case P9_TAUTH:
3229 case P9_TFLUSH:
3230 return 1;
3231 default:
3232 return 0;
3233 }
3234 }
3235
3236 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3237 {
3238 Coroutine *co;
3239 CoroutineEntry *handler;
3240
3241 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3242 (pdu_co_handlers[pdu->id] == NULL)) {
3243 handler = v9fs_op_not_supp;
3244 } else {
3245 handler = pdu_co_handlers[pdu->id];
3246 }
3247
3248 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3249 handler = v9fs_fs_ro;
3250 }
3251 co = qemu_coroutine_create(handler);
3252 qemu_coroutine_enter(co, pdu);
3253 }
3254
3255 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3256 {
3257 V9fsState *s = (V9fsState *)vdev;
3258 V9fsPDU *pdu;
3259 ssize_t len;
3260
3261 while ((pdu = alloc_pdu(s)) &&
3262 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3263 uint8_t *ptr;
3264 pdu->s = s;
3265 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3266 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3267
3268 ptr = pdu->elem.out_sg[0].iov_base;
3269
3270 pdu->size = le32_to_cpu(*(uint32_t *)ptr);
3271 pdu->id = ptr[4];
3272 pdu->tag = le16_to_cpu(*(uint16_t *)(ptr + 5));
3273 qemu_co_queue_init(&pdu->complete);
3274 submit_pdu(s, pdu);
3275 }
3276 free_pdu(s, pdu);
3277 }
3278
3279 void virtio_9p_set_fd_limit(void)
3280 {
3281 struct rlimit rlim;
3282 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3283 fprintf(stderr, "Failed to get the resource limit\n");
3284 exit(1);
3285 }
3286 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3287 open_fd_rc = rlim.rlim_cur/2;
3288 }