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