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