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