]>
Commit | Line | Data |
---|---|---|
9f107513 AL |
1 | /* |
2 | * Virtio 9p backend | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include "virtio.h" | |
15 | #include "pc.h" | |
16 | #include "qemu_socket.h" | |
17 | #include "virtio-9p.h" | |
18 | #include "fsdev/qemu-fsdev.h" | |
19 | #include "virtio-9p-debug.h" | |
20 | ||
9f107513 AL |
21 | int debug_9p_pdu; |
22 | ||
fac4f111 VJ |
23 | enum { |
24 | Oread = 0x00, | |
25 | Owrite = 0x01, | |
26 | Ordwr = 0x02, | |
27 | Oexec = 0x03, | |
28 | Oexcl = 0x04, | |
29 | Otrunc = 0x10, | |
30 | Orexec = 0x20, | |
31 | Orclose = 0x40, | |
32 | Oappend = 0x80, | |
33 | }; | |
34 | ||
35 | static int omode_to_uflags(int8_t mode) | |
36 | { | |
37 | int ret = 0; | |
38 | ||
39 | switch (mode & 3) { | |
40 | case Oread: | |
41 | ret = O_RDONLY; | |
42 | break; | |
43 | case Ordwr: | |
44 | ret = O_RDWR; | |
45 | break; | |
46 | case Owrite: | |
47 | ret = O_WRONLY; | |
48 | break; | |
49 | case Oexec: | |
50 | ret = O_RDONLY; | |
51 | break; | |
52 | } | |
53 | ||
54 | if (mode & Otrunc) { | |
55 | ret |= O_TRUNC; | |
56 | } | |
57 | ||
58 | if (mode & Oappend) { | |
59 | ret |= O_APPEND; | |
60 | } | |
61 | ||
62 | if (mode & Oexcl) { | |
63 | ret |= O_EXCL; | |
64 | } | |
65 | ||
66 | return ret; | |
67 | } | |
68 | ||
758e8e38 | 69 | void cred_init(FsCred *credp) |
131dcb25 | 70 | { |
758e8e38 VJ |
71 | credp->fc_uid = -1; |
72 | credp->fc_gid = -1; | |
73 | credp->fc_mode = -1; | |
74 | credp->fc_rdev = -1; | |
131dcb25 AL |
75 | } |
76 | ||
758e8e38 | 77 | static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf) |
131dcb25 | 78 | { |
758e8e38 | 79 | return s->ops->lstat(&s->ctx, path->data, stbuf); |
131dcb25 AL |
80 | } |
81 | ||
82 | static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf) | |
83 | { | |
84 | ssize_t len; | |
85 | ||
86 | buf->data = qemu_malloc(1024); | |
87 | ||
88 | len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1); | |
89 | if (len > -1) { | |
90 | buf->size = len; | |
91 | buf->data[len] = 0; | |
92 | } | |
93 | ||
94 | return len; | |
95 | } | |
96 | ||
97 | static int v9fs_do_close(V9fsState *s, int fd) | |
98 | { | |
99 | return s->ops->close(&s->ctx, fd); | |
100 | } | |
101 | ||
102 | static int v9fs_do_closedir(V9fsState *s, DIR *dir) | |
103 | { | |
104 | return s->ops->closedir(&s->ctx, dir); | |
105 | } | |
106 | ||
a6568fe2 AL |
107 | static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags) |
108 | { | |
109 | return s->ops->open(&s->ctx, path->data, flags); | |
110 | } | |
111 | ||
112 | static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path) | |
113 | { | |
114 | return s->ops->opendir(&s->ctx, path->data); | |
115 | } | |
116 | ||
a9231555 AL |
117 | static void v9fs_do_rewinddir(V9fsState *s, DIR *dir) |
118 | { | |
119 | return s->ops->rewinddir(&s->ctx, dir); | |
120 | } | |
121 | ||
122 | static off_t v9fs_do_telldir(V9fsState *s, DIR *dir) | |
123 | { | |
124 | return s->ops->telldir(&s->ctx, dir); | |
125 | } | |
126 | ||
127 | static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir) | |
128 | { | |
129 | return s->ops->readdir(&s->ctx, dir); | |
130 | } | |
131 | ||
132 | static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off) | |
133 | { | |
134 | return s->ops->seekdir(&s->ctx, dir, off); | |
135 | } | |
136 | ||
137 | static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov, | |
138 | int iovcnt) | |
139 | { | |
140 | return s->ops->readv(&s->ctx, fd, iov, iovcnt); | |
141 | } | |
142 | ||
143 | static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence) | |
144 | { | |
145 | return s->ops->lseek(&s->ctx, fd, offset, whence); | |
146 | } | |
147 | ||
8449360c AL |
148 | static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov, |
149 | int iovcnt) | |
150 | { | |
151 | return s->ops->writev(&s->ctx, fd, iov, iovcnt); | |
152 | } | |
153 | ||
c494dd6f AL |
154 | static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode) |
155 | { | |
e95ead32 VJ |
156 | FsCred cred; |
157 | cred_init(&cred); | |
158 | cred.fc_mode = mode; | |
159 | return s->ops->chmod(&s->ctx, path->data, &cred); | |
c494dd6f AL |
160 | } |
161 | ||
5268cecc MK |
162 | static int v9fs_do_mknod(V9fsState *s, char *name, |
163 | mode_t mode, dev_t dev, uid_t uid, gid_t gid) | |
c494dd6f | 164 | { |
1c293312 VJ |
165 | FsCred cred; |
166 | cred_init(&cred); | |
5268cecc MK |
167 | cred.fc_uid = uid; |
168 | cred.fc_gid = gid; | |
1c293312 VJ |
169 | cred.fc_mode = mode; |
170 | cred.fc_rdev = dev; | |
5268cecc | 171 | return s->ops->mknod(&s->ctx, name, &cred); |
c494dd6f AL |
172 | } |
173 | ||
b67592ea MK |
174 | static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode, |
175 | uid_t uid, gid_t gid) | |
c494dd6f | 176 | { |
00ec5c37 VJ |
177 | FsCred cred; |
178 | ||
179 | cred_init(&cred); | |
b67592ea MK |
180 | cred.fc_uid = uid; |
181 | cred.fc_gid = gid; | |
182 | cred.fc_mode = mode; | |
00ec5c37 | 183 | |
b67592ea | 184 | return s->ops->mkdir(&s->ctx, name, &cred); |
c494dd6f AL |
185 | } |
186 | ||
187 | static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf) | |
188 | { | |
189 | return s->ops->fstat(&s->ctx, fd, stbuf); | |
190 | } | |
191 | ||
c1568af5 VJ |
192 | static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid, |
193 | int flags, int mode) | |
c494dd6f | 194 | { |
4750a96f | 195 | FsCred cred; |
4750a96f VJ |
196 | |
197 | cred_init(&cred); | |
c1568af5 VJ |
198 | cred.fc_uid = uid; |
199 | cred.fc_gid = gid; | |
200 | cred.fc_mode = mode & 07777; | |
201 | flags = flags; | |
4750a96f | 202 | |
c1568af5 | 203 | return s->ops->open2(&s->ctx, fullname, flags, &cred); |
c494dd6f AL |
204 | } |
205 | ||
08c60fc9 VJ |
206 | static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp, |
207 | const char *oldpath, const char *newpath, gid_t gid) | |
c494dd6f | 208 | { |
879c2813 VJ |
209 | FsCred cred; |
210 | cred_init(&cred); | |
08c60fc9 VJ |
211 | cred.fc_uid = fidp->uid; |
212 | cred.fc_gid = gid; | |
213 | cred.fc_mode = 0777; | |
879c2813 | 214 | |
08c60fc9 | 215 | return s->ops->symlink(&s->ctx, oldpath, newpath, &cred); |
c494dd6f AL |
216 | } |
217 | ||
218 | static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath) | |
219 | { | |
220 | return s->ops->link(&s->ctx, oldpath->data, newpath->data); | |
221 | } | |
222 | ||
8cf89e00 AL |
223 | static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size) |
224 | { | |
225 | return s->ops->truncate(&s->ctx, path->data, size); | |
226 | } | |
227 | ||
228 | static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath, | |
229 | V9fsString *newpath) | |
230 | { | |
231 | return s->ops->rename(&s->ctx, oldpath->data, newpath->data); | |
232 | } | |
233 | ||
234 | static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid) | |
235 | { | |
f7613bee VJ |
236 | FsCred cred; |
237 | cred_init(&cred); | |
238 | cred.fc_uid = uid; | |
239 | cred.fc_gid = gid; | |
240 | ||
241 | return s->ops->chown(&s->ctx, path->data, &cred); | |
8cf89e00 AL |
242 | } |
243 | ||
8fc39ae4 SK |
244 | static int v9fs_do_utimensat(V9fsState *s, V9fsString *path, |
245 | const struct timespec times[2]) | |
8cf89e00 | 246 | { |
8fc39ae4 | 247 | return s->ops->utimensat(&s->ctx, path->data, times); |
8cf89e00 AL |
248 | } |
249 | ||
5bae1900 AL |
250 | static int v9fs_do_remove(V9fsState *s, V9fsString *path) |
251 | { | |
252 | return s->ops->remove(&s->ctx, path->data); | |
253 | } | |
254 | ||
8cf89e00 AL |
255 | static int v9fs_do_fsync(V9fsState *s, int fd) |
256 | { | |
257 | return s->ops->fsync(&s->ctx, fd); | |
258 | } | |
259 | ||
5e94c103 MK |
260 | static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf) |
261 | { | |
262 | return s->ops->statfs(&s->ctx, path->data, stbuf); | |
263 | } | |
264 | ||
fa32ef88 AK |
265 | static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path, |
266 | V9fsString *xattr_name, | |
267 | void *value, size_t size) | |
268 | { | |
269 | return s->ops->lgetxattr(&s->ctx, path->data, | |
270 | xattr_name->data, value, size); | |
271 | } | |
272 | ||
273 | static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path, | |
274 | void *value, size_t size) | |
275 | { | |
276 | return s->ops->llistxattr(&s->ctx, path->data, | |
277 | value, size); | |
278 | } | |
279 | ||
10b468bd AK |
280 | static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path, |
281 | V9fsString *xattr_name, | |
282 | void *value, size_t size, int flags) | |
283 | { | |
284 | return s->ops->lsetxattr(&s->ctx, path->data, | |
285 | xattr_name->data, value, size, flags); | |
286 | } | |
287 | ||
9ed3ef26 AK |
288 | static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path, |
289 | V9fsString *xattr_name) | |
290 | { | |
291 | return s->ops->lremovexattr(&s->ctx, path->data, | |
292 | xattr_name->data); | |
293 | } | |
294 | ||
295 | ||
a03f7874 AL |
296 | static void v9fs_string_init(V9fsString *str) |
297 | { | |
298 | str->data = NULL; | |
299 | str->size = 0; | |
300 | } | |
301 | ||
302 | static void v9fs_string_free(V9fsString *str) | |
303 | { | |
304 | qemu_free(str->data); | |
305 | str->data = NULL; | |
306 | str->size = 0; | |
307 | } | |
308 | ||
309 | static void v9fs_string_null(V9fsString *str) | |
310 | { | |
311 | v9fs_string_free(str); | |
312 | } | |
313 | ||
314 | static int number_to_string(void *arg, char type) | |
315 | { | |
316 | unsigned int ret = 0; | |
317 | ||
318 | switch (type) { | |
319 | case 'u': { | |
320 | unsigned int num = *(unsigned int *)arg; | |
321 | ||
322 | do { | |
323 | ret++; | |
324 | num = num/10; | |
325 | } while (num); | |
326 | break; | |
327 | } | |
328 | default: | |
329 | printf("Number_to_string: Unknown number format\n"); | |
330 | return -1; | |
331 | } | |
332 | ||
333 | return ret; | |
334 | } | |
335 | ||
336 | static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap) | |
337 | { | |
338 | va_list ap2; | |
339 | char *iter = (char *)fmt; | |
340 | int len = 0; | |
341 | int nr_args = 0; | |
342 | char *arg_char_ptr; | |
343 | unsigned int arg_uint; | |
344 | ||
345 | /* Find the number of %'s that denotes an argument */ | |
346 | for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { | |
347 | nr_args++; | |
348 | iter++; | |
349 | } | |
350 | ||
351 | len = strlen(fmt) - 2*nr_args; | |
352 | ||
353 | if (!nr_args) { | |
354 | goto alloc_print; | |
355 | } | |
356 | ||
357 | va_copy(ap2, ap); | |
358 | ||
359 | iter = (char *)fmt; | |
360 | ||
361 | /* Now parse the format string */ | |
362 | for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { | |
363 | iter++; | |
364 | switch (*iter) { | |
365 | case 'u': | |
366 | arg_uint = va_arg(ap2, unsigned int); | |
367 | len += number_to_string((void *)&arg_uint, 'u'); | |
368 | break; | |
369 | case 's': | |
370 | arg_char_ptr = va_arg(ap2, char *); | |
371 | len += strlen(arg_char_ptr); | |
372 | break; | |
373 | case 'c': | |
374 | len += 1; | |
375 | break; | |
376 | default: | |
377 | fprintf(stderr, | |
378 | "v9fs_string_alloc_printf:Incorrect format %c", *iter); | |
379 | return -1; | |
380 | } | |
381 | iter++; | |
382 | } | |
383 | ||
384 | alloc_print: | |
385 | *strp = qemu_malloc((len + 1) * sizeof(**strp)); | |
386 | ||
387 | return vsprintf(*strp, fmt, ap); | |
388 | } | |
389 | ||
390 | static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) | |
391 | { | |
392 | va_list ap; | |
393 | int err; | |
394 | ||
395 | v9fs_string_free(str); | |
396 | ||
397 | va_start(ap, fmt); | |
398 | err = v9fs_string_alloc_printf(&str->data, fmt, ap); | |
399 | BUG_ON(err == -1); | |
400 | va_end(ap); | |
401 | ||
402 | str->size = err; | |
403 | } | |
404 | ||
405 | static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs) | |
406 | { | |
407 | v9fs_string_free(lhs); | |
408 | v9fs_string_sprintf(lhs, "%s", rhs->data); | |
409 | } | |
410 | ||
411 | static size_t v9fs_string_size(V9fsString *str) | |
412 | { | |
413 | return str->size; | |
414 | } | |
415 | ||
286d5652 AL |
416 | static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid) |
417 | { | |
418 | V9fsFidState *f; | |
419 | ||
420 | for (f = s->fid_list; f; f = f->next) { | |
421 | if (f->fid == fid) { | |
286d5652 AL |
422 | return f; |
423 | } | |
424 | } | |
425 | ||
426 | return NULL; | |
427 | } | |
428 | ||
429 | static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) | |
430 | { | |
431 | V9fsFidState *f; | |
432 | ||
433 | f = lookup_fid(s, fid); | |
434 | if (f) { | |
435 | return NULL; | |
436 | } | |
437 | ||
438 | f = qemu_mallocz(sizeof(V9fsFidState)); | |
439 | ||
440 | f->fid = fid; | |
d62dbb51 | 441 | f->fid_type = P9_FID_NONE; |
286d5652 AL |
442 | |
443 | f->next = s->fid_list; | |
444 | s->fid_list = f; | |
445 | ||
446 | return f; | |
447 | } | |
448 | ||
10b468bd AK |
449 | static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp) |
450 | { | |
451 | int retval = 0; | |
452 | ||
453 | if (fidp->fs.xattr.copied_len == -1) { | |
454 | /* getxattr/listxattr fid */ | |
455 | goto free_value; | |
456 | } | |
457 | /* | |
458 | * if this is fid for setxattr. clunk should | |
459 | * result in setxattr localcall | |
460 | */ | |
461 | if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { | |
462 | /* clunk after partial write */ | |
463 | retval = -EINVAL; | |
464 | goto free_out; | |
465 | } | |
9ed3ef26 AK |
466 | if (fidp->fs.xattr.len) { |
467 | retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name, | |
468 | fidp->fs.xattr.value, | |
469 | fidp->fs.xattr.len, | |
470 | fidp->fs.xattr.flags); | |
471 | } else { | |
472 | retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name); | |
473 | } | |
10b468bd AK |
474 | free_out: |
475 | v9fs_string_free(&fidp->fs.xattr.name); | |
476 | free_value: | |
477 | if (fidp->fs.xattr.value) { | |
478 | qemu_free(fidp->fs.xattr.value); | |
479 | } | |
480 | return retval; | |
481 | } | |
482 | ||
286d5652 AL |
483 | static int free_fid(V9fsState *s, int32_t fid) |
484 | { | |
10b468bd | 485 | int retval = 0; |
286d5652 AL |
486 | V9fsFidState **fidpp, *fidp; |
487 | ||
488 | for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) { | |
489 | if ((*fidpp)->fid == fid) { | |
490 | break; | |
491 | } | |
492 | } | |
493 | ||
494 | if (*fidpp == NULL) { | |
495 | return -ENOENT; | |
496 | } | |
497 | ||
498 | fidp = *fidpp; | |
499 | *fidpp = fidp->next; | |
500 | ||
d62dbb51 AK |
501 | if (fidp->fid_type == P9_FID_FILE) { |
502 | v9fs_do_close(s, fidp->fs.fd); | |
503 | } else if (fidp->fid_type == P9_FID_DIR) { | |
504 | v9fs_do_closedir(s, fidp->fs.dir); | |
505 | } else if (fidp->fid_type == P9_FID_XATTR) { | |
10b468bd | 506 | retval = v9fs_xattr_fid_clunk(s, fidp); |
286d5652 AL |
507 | } |
508 | v9fs_string_free(&fidp->path); | |
509 | qemu_free(fidp); | |
510 | ||
10b468bd | 511 | return retval; |
286d5652 AL |
512 | } |
513 | ||
514 | #define P9_QID_TYPE_DIR 0x80 | |
515 | #define P9_QID_TYPE_SYMLINK 0x02 | |
516 | ||
517 | #define P9_STAT_MODE_DIR 0x80000000 | |
518 | #define P9_STAT_MODE_APPEND 0x40000000 | |
519 | #define P9_STAT_MODE_EXCL 0x20000000 | |
520 | #define P9_STAT_MODE_MOUNT 0x10000000 | |
521 | #define P9_STAT_MODE_AUTH 0x08000000 | |
522 | #define P9_STAT_MODE_TMP 0x04000000 | |
523 | #define P9_STAT_MODE_SYMLINK 0x02000000 | |
524 | #define P9_STAT_MODE_LINK 0x01000000 | |
525 | #define P9_STAT_MODE_DEVICE 0x00800000 | |
526 | #define P9_STAT_MODE_NAMED_PIPE 0x00200000 | |
527 | #define P9_STAT_MODE_SOCKET 0x00100000 | |
528 | #define P9_STAT_MODE_SETUID 0x00080000 | |
529 | #define P9_STAT_MODE_SETGID 0x00040000 | |
530 | #define P9_STAT_MODE_SETVTX 0x00010000 | |
531 | ||
532 | #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ | |
533 | P9_STAT_MODE_SYMLINK | \ | |
534 | P9_STAT_MODE_LINK | \ | |
535 | P9_STAT_MODE_DEVICE | \ | |
536 | P9_STAT_MODE_NAMED_PIPE | \ | |
537 | P9_STAT_MODE_SOCKET) | |
538 | ||
539 | /* This is the algorithm from ufs in spfs */ | |
540 | static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp) | |
541 | { | |
542 | size_t size; | |
543 | ||
544 | size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); | |
545 | memcpy(&qidp->path, &stbuf->st_ino, size); | |
546 | qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); | |
547 | qidp->type = 0; | |
548 | if (S_ISDIR(stbuf->st_mode)) { | |
549 | qidp->type |= P9_QID_TYPE_DIR; | |
550 | } | |
551 | if (S_ISLNK(stbuf->st_mode)) { | |
552 | qidp->type |= P9_QID_TYPE_SYMLINK; | |
553 | } | |
554 | } | |
555 | ||
556 | static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp) | |
557 | { | |
558 | struct stat stbuf; | |
559 | int err; | |
560 | ||
561 | err = v9fs_do_lstat(s, &fidp->path, &stbuf); | |
562 | if (err) { | |
563 | return err; | |
564 | } | |
565 | ||
566 | stat_to_qid(&stbuf, qidp); | |
567 | return 0; | |
568 | } | |
569 | ||
9f107513 AL |
570 | static V9fsPDU *alloc_pdu(V9fsState *s) |
571 | { | |
572 | V9fsPDU *pdu = NULL; | |
573 | ||
574 | if (!QLIST_EMPTY(&s->free_list)) { | |
575 | pdu = QLIST_FIRST(&s->free_list); | |
576 | QLIST_REMOVE(pdu, next); | |
577 | } | |
578 | return pdu; | |
579 | } | |
580 | ||
581 | static void free_pdu(V9fsState *s, V9fsPDU *pdu) | |
582 | { | |
583 | if (pdu) { | |
584 | QLIST_INSERT_HEAD(&s->free_list, pdu, next); | |
585 | } | |
586 | } | |
587 | ||
588 | size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count, | |
589 | size_t offset, size_t size, int pack) | |
590 | { | |
591 | int i = 0; | |
592 | size_t copied = 0; | |
593 | ||
594 | for (i = 0; size && i < sg_count; i++) { | |
595 | size_t len; | |
596 | if (offset >= sg[i].iov_len) { | |
597 | /* skip this sg */ | |
598 | offset -= sg[i].iov_len; | |
599 | continue; | |
600 | } else { | |
601 | len = MIN(sg[i].iov_len - offset, size); | |
602 | if (pack) { | |
603 | memcpy(sg[i].iov_base + offset, addr, len); | |
604 | } else { | |
605 | memcpy(addr, sg[i].iov_base + offset, len); | |
606 | } | |
607 | size -= len; | |
608 | copied += len; | |
609 | addr += len; | |
610 | if (size) { | |
611 | offset = 0; | |
612 | continue; | |
613 | } | |
614 | } | |
615 | } | |
616 | ||
617 | return copied; | |
618 | } | |
619 | ||
405a549a AL |
620 | static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size) |
621 | { | |
622 | return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num, | |
623 | offset, size, 0); | |
624 | } | |
625 | ||
626 | static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src, | |
627 | size_t size) | |
628 | { | |
629 | return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num, | |
630 | offset, size, 1); | |
631 | } | |
632 | ||
633 | static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg) | |
634 | { | |
635 | size_t pos = 0; | |
636 | int i, j; | |
637 | struct iovec *src_sg; | |
638 | unsigned int num; | |
639 | ||
640 | if (rx) { | |
641 | src_sg = pdu->elem.in_sg; | |
642 | num = pdu->elem.in_num; | |
643 | } else { | |
644 | src_sg = pdu->elem.out_sg; | |
645 | num = pdu->elem.out_num; | |
646 | } | |
647 | ||
648 | j = 0; | |
649 | for (i = 0; i < num; i++) { | |
650 | if (offset <= pos) { | |
651 | sg[j].iov_base = src_sg[i].iov_base; | |
652 | sg[j].iov_len = src_sg[i].iov_len; | |
653 | j++; | |
654 | } else if (offset < (src_sg[i].iov_len + pos)) { | |
655 | sg[j].iov_base = src_sg[i].iov_base; | |
656 | sg[j].iov_len = src_sg[i].iov_len; | |
657 | sg[j].iov_base += (offset - pos); | |
658 | sg[j].iov_len -= (offset - pos); | |
659 | j++; | |
660 | } | |
661 | pos += src_sg[i].iov_len; | |
662 | } | |
663 | ||
664 | return j; | |
665 | } | |
666 | ||
667 | static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) | |
668 | { | |
669 | size_t old_offset = offset; | |
670 | va_list ap; | |
671 | int i; | |
672 | ||
673 | va_start(ap, fmt); | |
674 | for (i = 0; fmt[i]; i++) { | |
675 | switch (fmt[i]) { | |
676 | case 'b': { | |
677 | uint8_t *valp = va_arg(ap, uint8_t *); | |
678 | offset += pdu_unpack(valp, pdu, offset, sizeof(*valp)); | |
679 | break; | |
680 | } | |
681 | case 'w': { | |
682 | uint16_t val, *valp; | |
683 | valp = va_arg(ap, uint16_t *); | |
684 | val = le16_to_cpupu(valp); | |
685 | offset += pdu_unpack(&val, pdu, offset, sizeof(val)); | |
686 | *valp = val; | |
687 | break; | |
688 | } | |
689 | case 'd': { | |
690 | uint32_t val, *valp; | |
691 | valp = va_arg(ap, uint32_t *); | |
692 | val = le32_to_cpupu(valp); | |
693 | offset += pdu_unpack(&val, pdu, offset, sizeof(val)); | |
694 | *valp = val; | |
695 | break; | |
696 | } | |
697 | case 'q': { | |
698 | uint64_t val, *valp; | |
699 | valp = va_arg(ap, uint64_t *); | |
700 | val = le64_to_cpup(valp); | |
701 | offset += pdu_unpack(&val, pdu, offset, sizeof(val)); | |
702 | *valp = val; | |
703 | break; | |
704 | } | |
705 | case 'v': { | |
706 | struct iovec *iov = va_arg(ap, struct iovec *); | |
707 | int *iovcnt = va_arg(ap, int *); | |
708 | *iovcnt = pdu_copy_sg(pdu, offset, 0, iov); | |
709 | break; | |
710 | } | |
711 | case 's': { | |
712 | V9fsString *str = va_arg(ap, V9fsString *); | |
713 | offset += pdu_unmarshal(pdu, offset, "w", &str->size); | |
714 | /* FIXME: sanity check str->size */ | |
715 | str->data = qemu_malloc(str->size + 1); | |
716 | offset += pdu_unpack(str->data, pdu, offset, str->size); | |
717 | str->data[str->size] = 0; | |
718 | break; | |
719 | } | |
720 | case 'Q': { | |
721 | V9fsQID *qidp = va_arg(ap, V9fsQID *); | |
722 | offset += pdu_unmarshal(pdu, offset, "bdq", | |
723 | &qidp->type, &qidp->version, &qidp->path); | |
724 | break; | |
725 | } | |
726 | case 'S': { | |
727 | V9fsStat *statp = va_arg(ap, V9fsStat *); | |
728 | offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd", | |
729 | &statp->size, &statp->type, &statp->dev, | |
730 | &statp->qid, &statp->mode, &statp->atime, | |
731 | &statp->mtime, &statp->length, | |
732 | &statp->name, &statp->uid, &statp->gid, | |
733 | &statp->muid, &statp->extension, | |
734 | &statp->n_uid, &statp->n_gid, | |
735 | &statp->n_muid); | |
736 | break; | |
737 | } | |
c79ce737 SK |
738 | case 'I': { |
739 | V9fsIattr *iattr = va_arg(ap, V9fsIattr *); | |
740 | offset += pdu_unmarshal(pdu, offset, "ddddqqqqq", | |
741 | &iattr->valid, &iattr->mode, | |
742 | &iattr->uid, &iattr->gid, &iattr->size, | |
743 | &iattr->atime_sec, &iattr->atime_nsec, | |
744 | &iattr->mtime_sec, &iattr->mtime_nsec); | |
745 | break; | |
746 | } | |
405a549a AL |
747 | default: |
748 | break; | |
749 | } | |
750 | } | |
751 | ||
752 | va_end(ap); | |
753 | ||
754 | return offset - old_offset; | |
755 | } | |
756 | ||
757 | static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) | |
758 | { | |
759 | size_t old_offset = offset; | |
760 | va_list ap; | |
761 | int i; | |
762 | ||
763 | va_start(ap, fmt); | |
764 | for (i = 0; fmt[i]; i++) { | |
765 | switch (fmt[i]) { | |
766 | case 'b': { | |
767 | uint8_t val = va_arg(ap, int); | |
768 | offset += pdu_pack(pdu, offset, &val, sizeof(val)); | |
769 | break; | |
770 | } | |
771 | case 'w': { | |
772 | uint16_t val; | |
773 | cpu_to_le16w(&val, va_arg(ap, int)); | |
774 | offset += pdu_pack(pdu, offset, &val, sizeof(val)); | |
775 | break; | |
776 | } | |
777 | case 'd': { | |
778 | uint32_t val; | |
779 | cpu_to_le32w(&val, va_arg(ap, uint32_t)); | |
780 | offset += pdu_pack(pdu, offset, &val, sizeof(val)); | |
781 | break; | |
782 | } | |
783 | case 'q': { | |
784 | uint64_t val; | |
785 | cpu_to_le64w(&val, va_arg(ap, uint64_t)); | |
786 | offset += pdu_pack(pdu, offset, &val, sizeof(val)); | |
787 | break; | |
788 | } | |
789 | case 'v': { | |
790 | struct iovec *iov = va_arg(ap, struct iovec *); | |
791 | int *iovcnt = va_arg(ap, int *); | |
792 | *iovcnt = pdu_copy_sg(pdu, offset, 1, iov); | |
793 | break; | |
794 | } | |
795 | case 's': { | |
796 | V9fsString *str = va_arg(ap, V9fsString *); | |
797 | offset += pdu_marshal(pdu, offset, "w", str->size); | |
798 | offset += pdu_pack(pdu, offset, str->data, str->size); | |
799 | break; | |
800 | } | |
801 | case 'Q': { | |
802 | V9fsQID *qidp = va_arg(ap, V9fsQID *); | |
803 | offset += pdu_marshal(pdu, offset, "bdq", | |
804 | qidp->type, qidp->version, qidp->path); | |
805 | break; | |
806 | } | |
807 | case 'S': { | |
808 | V9fsStat *statp = va_arg(ap, V9fsStat *); | |
809 | offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd", | |
810 | statp->size, statp->type, statp->dev, | |
811 | &statp->qid, statp->mode, statp->atime, | |
812 | statp->mtime, statp->length, &statp->name, | |
813 | &statp->uid, &statp->gid, &statp->muid, | |
814 | &statp->extension, statp->n_uid, | |
815 | statp->n_gid, statp->n_muid); | |
816 | break; | |
817 | } | |
00ede4c2 SK |
818 | case 'A': { |
819 | V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *); | |
820 | offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq", | |
821 | statp->st_result_mask, | |
822 | &statp->qid, statp->st_mode, | |
823 | statp->st_uid, statp->st_gid, | |
824 | statp->st_nlink, statp->st_rdev, | |
825 | statp->st_size, statp->st_blksize, statp->st_blocks, | |
826 | statp->st_atime_sec, statp->st_atime_nsec, | |
827 | statp->st_mtime_sec, statp->st_mtime_nsec, | |
828 | statp->st_ctime_sec, statp->st_ctime_nsec, | |
829 | statp->st_btime_sec, statp->st_btime_nsec, | |
830 | statp->st_gen, statp->st_data_version); | |
831 | break; | |
832 | } | |
405a549a AL |
833 | default: |
834 | break; | |
835 | } | |
836 | } | |
837 | va_end(ap); | |
838 | ||
839 | return offset - old_offset; | |
840 | } | |
841 | ||
842 | static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len) | |
843 | { | |
844 | int8_t id = pdu->id + 1; /* Response */ | |
845 | ||
846 | if (len < 0) { | |
405a549a | 847 | int err = -len; |
8f4d1ca5 | 848 | len = 7; |
405a549a | 849 | |
8f4d1ca5 AB |
850 | if (s->proto_version != V9FS_PROTO_2000L) { |
851 | V9fsString str; | |
852 | ||
853 | str.data = strerror(err); | |
854 | str.size = strlen(str.data); | |
855 | ||
856 | len += pdu_marshal(pdu, len, "s", &str); | |
857 | id = P9_RERROR; | |
858 | } | |
405a549a | 859 | |
cf03eb2c | 860 | len += pdu_marshal(pdu, len, "d", err); |
405a549a | 861 | |
8f4d1ca5 AB |
862 | if (s->proto_version == V9FS_PROTO_2000L) { |
863 | id = P9_RLERROR; | |
864 | } | |
405a549a AL |
865 | } |
866 | ||
867 | /* fill out the header */ | |
868 | pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag); | |
869 | ||
870 | /* keep these in sync */ | |
871 | pdu->size = len; | |
872 | pdu->id = id; | |
873 | ||
874 | /* push onto queue and notify */ | |
875 | virtqueue_push(s->vq, &pdu->elem, len); | |
876 | ||
877 | /* FIXME: we should batch these completions */ | |
878 | virtio_notify(&s->vdev, s->vq); | |
879 | ||
880 | free_pdu(s, pdu); | |
881 | } | |
882 | ||
bb9e3216 AL |
883 | static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) |
884 | { | |
885 | mode_t ret; | |
886 | ||
887 | ret = mode & 0777; | |
888 | if (mode & P9_STAT_MODE_DIR) { | |
889 | ret |= S_IFDIR; | |
890 | } | |
891 | ||
cf03eb2c AB |
892 | if (mode & P9_STAT_MODE_SYMLINK) { |
893 | ret |= S_IFLNK; | |
894 | } | |
895 | if (mode & P9_STAT_MODE_SOCKET) { | |
896 | ret |= S_IFSOCK; | |
897 | } | |
898 | if (mode & P9_STAT_MODE_NAMED_PIPE) { | |
899 | ret |= S_IFIFO; | |
900 | } | |
901 | if (mode & P9_STAT_MODE_DEVICE) { | |
902 | if (extension && extension->data[0] == 'c') { | |
903 | ret |= S_IFCHR; | |
904 | } else { | |
905 | ret |= S_IFBLK; | |
bb9e3216 AL |
906 | } |
907 | } | |
908 | ||
909 | if (!(ret&~0777)) { | |
910 | ret |= S_IFREG; | |
911 | } | |
912 | ||
913 | if (mode & P9_STAT_MODE_SETUID) { | |
914 | ret |= S_ISUID; | |
915 | } | |
916 | if (mode & P9_STAT_MODE_SETGID) { | |
917 | ret |= S_ISGID; | |
918 | } | |
919 | if (mode & P9_STAT_MODE_SETVTX) { | |
920 | ret |= S_ISVTX; | |
921 | } | |
922 | ||
923 | return ret; | |
924 | } | |
925 | ||
926 | static int donttouch_stat(V9fsStat *stat) | |
927 | { | |
928 | if (stat->type == -1 && | |
929 | stat->dev == -1 && | |
930 | stat->qid.type == -1 && | |
931 | stat->qid.version == -1 && | |
932 | stat->qid.path == -1 && | |
933 | stat->mode == -1 && | |
934 | stat->atime == -1 && | |
935 | stat->mtime == -1 && | |
936 | stat->length == -1 && | |
937 | !stat->name.size && | |
938 | !stat->uid.size && | |
939 | !stat->gid.size && | |
940 | !stat->muid.size && | |
941 | stat->n_uid == -1 && | |
942 | stat->n_gid == -1 && | |
943 | stat->n_muid == -1) { | |
944 | return 1; | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | static void v9fs_stat_free(V9fsStat *stat) | |
951 | { | |
952 | v9fs_string_free(&stat->name); | |
953 | v9fs_string_free(&stat->uid); | |
954 | v9fs_string_free(&stat->gid); | |
955 | v9fs_string_free(&stat->muid); | |
956 | v9fs_string_free(&stat->extension); | |
957 | } | |
958 | ||
959 | static uint32_t stat_to_v9mode(const struct stat *stbuf) | |
960 | { | |
961 | uint32_t mode; | |
962 | ||
963 | mode = stbuf->st_mode & 0777; | |
964 | if (S_ISDIR(stbuf->st_mode)) { | |
965 | mode |= P9_STAT_MODE_DIR; | |
966 | } | |
967 | ||
cf03eb2c AB |
968 | if (S_ISLNK(stbuf->st_mode)) { |
969 | mode |= P9_STAT_MODE_SYMLINK; | |
970 | } | |
bb9e3216 | 971 | |
cf03eb2c AB |
972 | if (S_ISSOCK(stbuf->st_mode)) { |
973 | mode |= P9_STAT_MODE_SOCKET; | |
974 | } | |
bb9e3216 | 975 | |
cf03eb2c AB |
976 | if (S_ISFIFO(stbuf->st_mode)) { |
977 | mode |= P9_STAT_MODE_NAMED_PIPE; | |
978 | } | |
bb9e3216 | 979 | |
cf03eb2c AB |
980 | if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { |
981 | mode |= P9_STAT_MODE_DEVICE; | |
982 | } | |
bb9e3216 | 983 | |
cf03eb2c AB |
984 | if (stbuf->st_mode & S_ISUID) { |
985 | mode |= P9_STAT_MODE_SETUID; | |
986 | } | |
bb9e3216 | 987 | |
cf03eb2c AB |
988 | if (stbuf->st_mode & S_ISGID) { |
989 | mode |= P9_STAT_MODE_SETGID; | |
990 | } | |
bb9e3216 | 991 | |
cf03eb2c AB |
992 | if (stbuf->st_mode & S_ISVTX) { |
993 | mode |= P9_STAT_MODE_SETVTX; | |
bb9e3216 AL |
994 | } |
995 | ||
996 | return mode; | |
997 | } | |
998 | ||
999 | static int stat_to_v9stat(V9fsState *s, V9fsString *name, | |
1000 | const struct stat *stbuf, | |
1001 | V9fsStat *v9stat) | |
1002 | { | |
1003 | int err; | |
1004 | const char *str; | |
1005 | ||
1006 | memset(v9stat, 0, sizeof(*v9stat)); | |
1007 | ||
1008 | stat_to_qid(stbuf, &v9stat->qid); | |
1009 | v9stat->mode = stat_to_v9mode(stbuf); | |
1010 | v9stat->atime = stbuf->st_atime; | |
1011 | v9stat->mtime = stbuf->st_mtime; | |
1012 | v9stat->length = stbuf->st_size; | |
1013 | ||
1014 | v9fs_string_null(&v9stat->uid); | |
1015 | v9fs_string_null(&v9stat->gid); | |
1016 | v9fs_string_null(&v9stat->muid); | |
1017 | ||
cf03eb2c AB |
1018 | v9stat->n_uid = stbuf->st_uid; |
1019 | v9stat->n_gid = stbuf->st_gid; | |
1020 | v9stat->n_muid = 0; | |
bb9e3216 | 1021 | |
cf03eb2c | 1022 | v9fs_string_null(&v9stat->extension); |
bb9e3216 | 1023 | |
cf03eb2c AB |
1024 | if (v9stat->mode & P9_STAT_MODE_SYMLINK) { |
1025 | err = v9fs_do_readlink(s, name, &v9stat->extension); | |
1026 | if (err == -1) { | |
1027 | err = -errno; | |
1028 | return err; | |
bb9e3216 | 1029 | } |
cf03eb2c AB |
1030 | v9stat->extension.data[err] = 0; |
1031 | v9stat->extension.size = err; | |
1032 | } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { | |
1033 | v9fs_string_sprintf(&v9stat->extension, "%c %u %u", | |
1034 | S_ISCHR(stbuf->st_mode) ? 'c' : 'b', | |
1035 | major(stbuf->st_rdev), minor(stbuf->st_rdev)); | |
1036 | } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { | |
1037 | v9fs_string_sprintf(&v9stat->extension, "%s %u", | |
1038 | "HARDLINKCOUNT", stbuf->st_nlink); | |
bb9e3216 AL |
1039 | } |
1040 | ||
1041 | str = strrchr(name->data, '/'); | |
1042 | if (str) { | |
1043 | str += 1; | |
1044 | } else { | |
1045 | str = name->data; | |
1046 | } | |
1047 | ||
1048 | v9fs_string_sprintf(&v9stat->name, "%s", str); | |
1049 | ||
1050 | v9stat->size = 61 + | |
1051 | v9fs_string_size(&v9stat->name) + | |
1052 | v9fs_string_size(&v9stat->uid) + | |
1053 | v9fs_string_size(&v9stat->gid) + | |
1054 | v9fs_string_size(&v9stat->muid) + | |
1055 | v9fs_string_size(&v9stat->extension); | |
1056 | return 0; | |
1057 | } | |
1058 | ||
00ede4c2 SK |
1059 | #define P9_STATS_MODE 0x00000001ULL |
1060 | #define P9_STATS_NLINK 0x00000002ULL | |
1061 | #define P9_STATS_UID 0x00000004ULL | |
1062 | #define P9_STATS_GID 0x00000008ULL | |
1063 | #define P9_STATS_RDEV 0x00000010ULL | |
1064 | #define P9_STATS_ATIME 0x00000020ULL | |
1065 | #define P9_STATS_MTIME 0x00000040ULL | |
1066 | #define P9_STATS_CTIME 0x00000080ULL | |
1067 | #define P9_STATS_INO 0x00000100ULL | |
1068 | #define P9_STATS_SIZE 0x00000200ULL | |
1069 | #define P9_STATS_BLOCKS 0x00000400ULL | |
1070 | ||
1071 | #define P9_STATS_BTIME 0x00000800ULL | |
1072 | #define P9_STATS_GEN 0x00001000ULL | |
1073 | #define P9_STATS_DATA_VERSION 0x00002000ULL | |
1074 | ||
1075 | #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ | |
1076 | #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ | |
1077 | ||
1078 | ||
1079 | static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf, | |
1080 | V9fsStatDotl *v9lstat) | |
1081 | { | |
1082 | memset(v9lstat, 0, sizeof(*v9lstat)); | |
1083 | ||
1084 | v9lstat->st_mode = stbuf->st_mode; | |
1085 | v9lstat->st_nlink = stbuf->st_nlink; | |
1086 | v9lstat->st_uid = stbuf->st_uid; | |
1087 | v9lstat->st_gid = stbuf->st_gid; | |
1088 | v9lstat->st_rdev = stbuf->st_rdev; | |
1089 | v9lstat->st_size = stbuf->st_size; | |
1090 | v9lstat->st_blksize = stbuf->st_blksize; | |
1091 | v9lstat->st_blocks = stbuf->st_blocks; | |
1092 | v9lstat->st_atime_sec = stbuf->st_atime; | |
1093 | v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; | |
1094 | v9lstat->st_mtime_sec = stbuf->st_mtime; | |
1095 | v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; | |
1096 | v9lstat->st_ctime_sec = stbuf->st_ctime; | |
1097 | v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; | |
1098 | /* Currently we only support BASIC fields in stat */ | |
1099 | v9lstat->st_result_mask = P9_STATS_BASIC; | |
1100 | ||
1101 | stat_to_qid(stbuf, &v9lstat->qid); | |
1102 | } | |
1103 | ||
1f5a89bf AL |
1104 | static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt) |
1105 | { | |
1106 | while (len && *iovcnt) { | |
1107 | if (len < sg->iov_len) { | |
1108 | sg->iov_len -= len; | |
1109 | sg->iov_base += len; | |
1110 | len = 0; | |
1111 | } else { | |
1112 | len -= sg->iov_len; | |
1113 | sg++; | |
1114 | *iovcnt -= 1; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | return sg; | |
1119 | } | |
1120 | ||
1121 | static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt) | |
1122 | { | |
1123 | int i; | |
1124 | int total = 0; | |
1125 | ||
1126 | for (i = 0; i < *cnt; i++) { | |
1127 | if ((total + sg[i].iov_len) > cap) { | |
1128 | sg[i].iov_len -= ((total + sg[i].iov_len) - cap); | |
1129 | i++; | |
1130 | break; | |
1131 | } | |
1132 | total += sg[i].iov_len; | |
1133 | } | |
1134 | ||
1135 | *cnt = i; | |
1136 | ||
1137 | return sg; | |
1138 | } | |
1139 | ||
1140 | static void print_sg(struct iovec *sg, int cnt) | |
1141 | { | |
1142 | int i; | |
1143 | ||
1144 | printf("sg[%d]: {", cnt); | |
1145 | for (i = 0; i < cnt; i++) { | |
1146 | if (i) { | |
1147 | printf(", "); | |
1148 | } | |
1149 | printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); | |
1150 | } | |
1151 | printf("}\n"); | |
1152 | } | |
1153 | ||
8cf89e00 AL |
1154 | static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len) |
1155 | { | |
1156 | V9fsString str; | |
1157 | v9fs_string_init(&str); | |
1158 | v9fs_string_copy(&str, dst); | |
1159 | v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len); | |
1160 | v9fs_string_free(&str); | |
1161 | } | |
1162 | ||
9f107513 AL |
1163 | static void v9fs_version(V9fsState *s, V9fsPDU *pdu) |
1164 | { | |
92c1ad03 AL |
1165 | V9fsString version; |
1166 | size_t offset = 7; | |
1167 | ||
5e94c103 | 1168 | pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); |
92c1ad03 | 1169 | |
84151514 MK |
1170 | if (!strcmp(version.data, "9P2000.u")) { |
1171 | s->proto_version = V9FS_PROTO_2000U; | |
1172 | } else if (!strcmp(version.data, "9P2000.L")) { | |
1173 | s->proto_version = V9FS_PROTO_2000L; | |
1174 | } else { | |
92c1ad03 | 1175 | v9fs_string_sprintf(&version, "unknown"); |
9f107513 | 1176 | } |
92c1ad03 | 1177 | |
5e94c103 | 1178 | offset += pdu_marshal(pdu, offset, "ds", s->msize, &version); |
92c1ad03 AL |
1179 | complete_pdu(s, pdu, offset); |
1180 | ||
1181 | v9fs_string_free(&version); | |
9f107513 AL |
1182 | } |
1183 | ||
1184 | static void v9fs_attach(V9fsState *s, V9fsPDU *pdu) | |
1185 | { | |
955efc47 AL |
1186 | int32_t fid, afid, n_uname; |
1187 | V9fsString uname, aname; | |
1188 | V9fsFidState *fidp; | |
1189 | V9fsQID qid; | |
1190 | size_t offset = 7; | |
1191 | ssize_t err; | |
1192 | ||
1193 | pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname); | |
1194 | ||
1195 | fidp = alloc_fid(s, fid); | |
1196 | if (fidp == NULL) { | |
1197 | err = -EINVAL; | |
1198 | goto out; | |
9f107513 | 1199 | } |
955efc47 AL |
1200 | |
1201 | fidp->uid = n_uname; | |
1202 | ||
1203 | v9fs_string_sprintf(&fidp->path, "%s", "/"); | |
1204 | err = fid_to_qid(s, fidp, &qid); | |
1205 | if (err) { | |
1206 | err = -EINVAL; | |
1207 | free_fid(s, fid); | |
1208 | goto out; | |
1209 | } | |
1210 | ||
1211 | offset += pdu_marshal(pdu, offset, "Q", &qid); | |
1212 | ||
1213 | err = offset; | |
1214 | out: | |
1215 | complete_pdu(s, pdu, err); | |
1216 | v9fs_string_free(&uname); | |
1217 | v9fs_string_free(&aname); | |
9f107513 AL |
1218 | } |
1219 | ||
4da7d3fa AL |
1220 | static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err) |
1221 | { | |
1222 | if (err == -1) { | |
1223 | err = -errno; | |
1224 | goto out; | |
1225 | } | |
1226 | ||
1227 | err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat); | |
1228 | if (err) { | |
1229 | goto out; | |
1230 | } | |
1231 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat); | |
1232 | err = vs->offset; | |
1233 | ||
1234 | out: | |
1235 | complete_pdu(s, vs->pdu, err); | |
1236 | v9fs_stat_free(&vs->v9stat); | |
1237 | qemu_free(vs); | |
1238 | } | |
1239 | ||
9f107513 AL |
1240 | static void v9fs_stat(V9fsState *s, V9fsPDU *pdu) |
1241 | { | |
4da7d3fa AL |
1242 | int32_t fid; |
1243 | V9fsStatState *vs; | |
1244 | ssize_t err = 0; | |
1245 | ||
1246 | vs = qemu_malloc(sizeof(*vs)); | |
1247 | vs->pdu = pdu; | |
1248 | vs->offset = 7; | |
1249 | ||
1250 | memset(&vs->v9stat, 0, sizeof(vs->v9stat)); | |
1251 | ||
1252 | pdu_unmarshal(vs->pdu, vs->offset, "d", &fid); | |
1253 | ||
1254 | vs->fidp = lookup_fid(s, fid); | |
1255 | if (vs->fidp == NULL) { | |
1256 | err = -ENOENT; | |
1257 | goto out; | |
9f107513 | 1258 | } |
4da7d3fa AL |
1259 | |
1260 | err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); | |
1261 | v9fs_stat_post_lstat(s, vs, err); | |
1262 | return; | |
1263 | ||
1264 | out: | |
1265 | complete_pdu(s, vs->pdu, err); | |
1266 | v9fs_stat_free(&vs->v9stat); | |
1267 | qemu_free(vs); | |
9f107513 AL |
1268 | } |
1269 | ||
00ede4c2 SK |
1270 | static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs, |
1271 | int err) | |
1272 | { | |
1273 | if (err == -1) { | |
1274 | err = -errno; | |
1275 | goto out; | |
1276 | } | |
1277 | ||
1278 | stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl); | |
1279 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl); | |
1280 | err = vs->offset; | |
1281 | ||
1282 | out: | |
1283 | complete_pdu(s, vs->pdu, err); | |
1284 | qemu_free(vs); | |
1285 | } | |
1286 | ||
1287 | static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu) | |
1288 | { | |
1289 | int32_t fid; | |
1290 | V9fsStatStateDotl *vs; | |
1291 | ssize_t err = 0; | |
1292 | V9fsFidState *fidp; | |
1293 | uint64_t request_mask; | |
1294 | ||
1295 | vs = qemu_malloc(sizeof(*vs)); | |
1296 | vs->pdu = pdu; | |
1297 | vs->offset = 7; | |
1298 | ||
1299 | memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl)); | |
1300 | ||
1301 | pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask); | |
1302 | ||
1303 | fidp = lookup_fid(s, fid); | |
1304 | if (fidp == NULL) { | |
1305 | err = -ENOENT; | |
1306 | goto out; | |
1307 | } | |
1308 | ||
1309 | /* Currently we only support BASIC fields in stat, so there is no | |
1310 | * need to look at request_mask. | |
1311 | */ | |
1312 | err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf); | |
1313 | v9fs_getattr_post_lstat(s, vs, err); | |
1314 | return; | |
1315 | ||
1316 | out: | |
1317 | complete_pdu(s, vs->pdu, err); | |
1318 | qemu_free(vs); | |
1319 | } | |
1320 | ||
c79ce737 SK |
1321 | /* From Linux kernel code */ |
1322 | #define ATTR_MODE (1 << 0) | |
1323 | #define ATTR_UID (1 << 1) | |
1324 | #define ATTR_GID (1 << 2) | |
1325 | #define ATTR_SIZE (1 << 3) | |
1326 | #define ATTR_ATIME (1 << 4) | |
1327 | #define ATTR_MTIME (1 << 5) | |
1328 | #define ATTR_CTIME (1 << 6) | |
1329 | #define ATTR_MASK 127 | |
1330 | #define ATTR_ATIME_SET (1 << 7) | |
1331 | #define ATTR_MTIME_SET (1 << 8) | |
1332 | ||
1333 | static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs, | |
1334 | int err) | |
1335 | { | |
1336 | if (err == -1) { | |
1337 | err = -errno; | |
1338 | goto out; | |
1339 | } | |
1340 | err = vs->offset; | |
1341 | ||
1342 | out: | |
1343 | complete_pdu(s, vs->pdu, err); | |
1344 | qemu_free(vs); | |
1345 | } | |
1346 | ||
1347 | static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err) | |
1348 | { | |
1349 | if (err == -1) { | |
1350 | err = -errno; | |
1351 | goto out; | |
1352 | } | |
1353 | ||
1354 | if (vs->v9iattr.valid & (ATTR_SIZE)) { | |
1355 | err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size); | |
1356 | } | |
1357 | v9fs_setattr_post_truncate(s, vs, err); | |
1358 | return; | |
1359 | ||
1360 | out: | |
1361 | complete_pdu(s, vs->pdu, err); | |
1362 | qemu_free(vs); | |
1363 | } | |
1364 | ||
1365 | static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs, | |
1366 | int err) | |
1367 | { | |
1368 | if (err == -1) { | |
1369 | err = -errno; | |
1370 | goto out; | |
1371 | } | |
1372 | ||
1373 | /* If the only valid entry in iattr is ctime we can call | |
1374 | * chown(-1,-1) to update the ctime of the file | |
1375 | */ | |
1376 | if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) || | |
1377 | ((vs->v9iattr.valid & ATTR_CTIME) | |
1378 | && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) { | |
1379 | if (!(vs->v9iattr.valid & ATTR_UID)) { | |
1380 | vs->v9iattr.uid = -1; | |
1381 | } | |
1382 | if (!(vs->v9iattr.valid & ATTR_GID)) { | |
1383 | vs->v9iattr.gid = -1; | |
1384 | } | |
1385 | err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid, | |
1386 | vs->v9iattr.gid); | |
1387 | } | |
1388 | v9fs_setattr_post_chown(s, vs, err); | |
1389 | return; | |
1390 | ||
1391 | out: | |
1392 | complete_pdu(s, vs->pdu, err); | |
1393 | qemu_free(vs); | |
1394 | } | |
1395 | ||
1396 | static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err) | |
1397 | { | |
1398 | if (err == -1) { | |
1399 | err = -errno; | |
1400 | goto out; | |
1401 | } | |
1402 | ||
1403 | if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) { | |
1404 | struct timespec times[2]; | |
1405 | if (vs->v9iattr.valid & ATTR_ATIME) { | |
1406 | if (vs->v9iattr.valid & ATTR_ATIME_SET) { | |
1407 | times[0].tv_sec = vs->v9iattr.atime_sec; | |
1408 | times[0].tv_nsec = vs->v9iattr.atime_nsec; | |
1409 | } else { | |
1410 | times[0].tv_nsec = UTIME_NOW; | |
1411 | } | |
1412 | } else { | |
1413 | times[0].tv_nsec = UTIME_OMIT; | |
1414 | } | |
1415 | ||
1416 | if (vs->v9iattr.valid & ATTR_MTIME) { | |
1417 | if (vs->v9iattr.valid & ATTR_MTIME_SET) { | |
1418 | times[1].tv_sec = vs->v9iattr.mtime_sec; | |
1419 | times[1].tv_nsec = vs->v9iattr.mtime_nsec; | |
1420 | } else { | |
1421 | times[1].tv_nsec = UTIME_NOW; | |
1422 | } | |
1423 | } else { | |
1424 | times[1].tv_nsec = UTIME_OMIT; | |
1425 | } | |
1426 | err = v9fs_do_utimensat(s, &vs->fidp->path, times); | |
1427 | } | |
1428 | v9fs_setattr_post_utimensat(s, vs, err); | |
1429 | return; | |
1430 | ||
1431 | out: | |
1432 | complete_pdu(s, vs->pdu, err); | |
1433 | qemu_free(vs); | |
1434 | } | |
1435 | ||
1436 | static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu) | |
1437 | { | |
1438 | int32_t fid; | |
1439 | V9fsSetattrState *vs; | |
1440 | int err = 0; | |
1441 | ||
1442 | vs = qemu_malloc(sizeof(*vs)); | |
1443 | vs->pdu = pdu; | |
1444 | vs->offset = 7; | |
1445 | ||
1446 | pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr); | |
1447 | ||
1448 | vs->fidp = lookup_fid(s, fid); | |
1449 | if (vs->fidp == NULL) { | |
1450 | err = -EINVAL; | |
1451 | goto out; | |
1452 | } | |
1453 | ||
1454 | if (vs->v9iattr.valid & ATTR_MODE) { | |
1455 | err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode); | |
1456 | } | |
1457 | ||
1458 | v9fs_setattr_post_chmod(s, vs, err); | |
1459 | return; | |
1460 | ||
1461 | out: | |
1462 | complete_pdu(s, vs->pdu, err); | |
1463 | qemu_free(vs); | |
1464 | } | |
1465 | ||
ff5e54c9 AL |
1466 | static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err) |
1467 | { | |
1468 | complete_pdu(s, vs->pdu, err); | |
1469 | ||
1470 | if (vs->nwnames) { | |
1471 | for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) { | |
1472 | v9fs_string_free(&vs->wnames[vs->name_idx]); | |
1473 | } | |
1474 | ||
1475 | qemu_free(vs->wnames); | |
1476 | qemu_free(vs->qids); | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | static void v9fs_walk_marshal(V9fsWalkState *vs) | |
1481 | { | |
1482 | int i; | |
1483 | vs->offset = 7; | |
1484 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames); | |
1485 | ||
1486 | for (i = 0; i < vs->nwnames; i++) { | |
1487 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]); | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs, | |
1492 | int err) | |
1493 | { | |
1494 | if (err == -1) { | |
1495 | free_fid(s, vs->newfidp->fid); | |
1496 | v9fs_string_free(&vs->path); | |
1497 | err = -ENOENT; | |
1498 | goto out; | |
1499 | } | |
1500 | ||
1501 | stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]); | |
1502 | ||
1503 | vs->name_idx++; | |
1504 | if (vs->name_idx < vs->nwnames) { | |
1505 | v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data, | |
1506 | vs->wnames[vs->name_idx].data); | |
1507 | v9fs_string_copy(&vs->newfidp->path, &vs->path); | |
1508 | ||
1509 | err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf); | |
1510 | v9fs_walk_post_newfid_lstat(s, vs, err); | |
1511 | return; | |
1512 | } | |
1513 | ||
1514 | v9fs_string_free(&vs->path); | |
1515 | v9fs_walk_marshal(vs); | |
1516 | err = vs->offset; | |
1517 | out: | |
1518 | v9fs_walk_complete(s, vs, err); | |
1519 | } | |
1520 | ||
1521 | static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs, | |
1522 | int err) | |
1523 | { | |
1524 | if (err == -1) { | |
1525 | v9fs_string_free(&vs->path); | |
1526 | err = -ENOENT; | |
1527 | goto out; | |
1528 | } | |
1529 | ||
1530 | stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]); | |
1531 | vs->name_idx++; | |
1532 | if (vs->name_idx < vs->nwnames) { | |
1533 | ||
1534 | v9fs_string_sprintf(&vs->path, "%s/%s", | |
1535 | vs->fidp->path.data, vs->wnames[vs->name_idx].data); | |
1536 | v9fs_string_copy(&vs->fidp->path, &vs->path); | |
1537 | ||
1538 | err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); | |
1539 | v9fs_walk_post_oldfid_lstat(s, vs, err); | |
1540 | return; | |
1541 | } | |
1542 | ||
1543 | v9fs_string_free(&vs->path); | |
1544 | v9fs_walk_marshal(vs); | |
1545 | err = vs->offset; | |
1546 | out: | |
1547 | v9fs_walk_complete(s, vs, err); | |
1548 | } | |
1549 | ||
9f107513 AL |
1550 | static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) |
1551 | { | |
ff5e54c9 AL |
1552 | int32_t fid, newfid; |
1553 | V9fsWalkState *vs; | |
1554 | int err = 0; | |
1555 | int i; | |
1556 | ||
1557 | vs = qemu_malloc(sizeof(*vs)); | |
1558 | vs->pdu = pdu; | |
1559 | vs->wnames = NULL; | |
1560 | vs->qids = NULL; | |
1561 | vs->offset = 7; | |
1562 | ||
1563 | vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid, | |
1564 | &newfid, &vs->nwnames); | |
1565 | ||
1566 | if (vs->nwnames) { | |
1567 | vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames); | |
1568 | ||
1569 | vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames); | |
1570 | ||
1571 | for (i = 0; i < vs->nwnames; i++) { | |
1572 | vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s", | |
1573 | &vs->wnames[i]); | |
1574 | } | |
1575 | } | |
1576 | ||
1577 | vs->fidp = lookup_fid(s, fid); | |
1578 | if (vs->fidp == NULL) { | |
1579 | err = -ENOENT; | |
1580 | goto out; | |
1581 | } | |
1582 | ||
1583 | /* FIXME: is this really valid? */ | |
1584 | if (fid == newfid) { | |
1585 | ||
d62dbb51 | 1586 | BUG_ON(vs->fidp->fid_type != P9_FID_NONE); |
ff5e54c9 AL |
1587 | v9fs_string_init(&vs->path); |
1588 | vs->name_idx = 0; | |
1589 | ||
1590 | if (vs->name_idx < vs->nwnames) { | |
1591 | v9fs_string_sprintf(&vs->path, "%s/%s", | |
1592 | vs->fidp->path.data, vs->wnames[vs->name_idx].data); | |
1593 | v9fs_string_copy(&vs->fidp->path, &vs->path); | |
1594 | ||
1595 | err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); | |
1596 | v9fs_walk_post_oldfid_lstat(s, vs, err); | |
1597 | return; | |
1598 | } | |
1599 | } else { | |
1600 | vs->newfidp = alloc_fid(s, newfid); | |
1601 | if (vs->newfidp == NULL) { | |
1602 | err = -EINVAL; | |
1603 | goto out; | |
1604 | } | |
1605 | ||
1606 | vs->newfidp->uid = vs->fidp->uid; | |
1607 | v9fs_string_init(&vs->path); | |
1608 | vs->name_idx = 0; | |
1609 | v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path); | |
1610 | ||
1611 | if (vs->name_idx < vs->nwnames) { | |
1612 | v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data, | |
1613 | vs->wnames[vs->name_idx].data); | |
1614 | v9fs_string_copy(&vs->newfidp->path, &vs->path); | |
1615 | ||
1616 | err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf); | |
1617 | v9fs_walk_post_newfid_lstat(s, vs, err); | |
1618 | return; | |
1619 | } | |
9f107513 | 1620 | } |
ff5e54c9 AL |
1621 | |
1622 | v9fs_walk_marshal(vs); | |
1623 | err = vs->offset; | |
1624 | out: | |
1625 | v9fs_walk_complete(s, vs, err); | |
9f107513 AL |
1626 | } |
1627 | ||
5e94c103 MK |
1628 | static int32_t get_iounit(V9fsState *s, V9fsString *name) |
1629 | { | |
1630 | struct statfs stbuf; | |
1631 | int32_t iounit = 0; | |
1632 | ||
1633 | /* | |
1634 | * iounit should be multiples of f_bsize (host filesystem block size | |
1635 | * and as well as less than (client msize - P9_IOHDRSZ)) | |
1636 | */ | |
1637 | if (!v9fs_do_statfs(s, name, &stbuf)) { | |
1638 | iounit = stbuf.f_bsize; | |
1639 | iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize; | |
1640 | } | |
1641 | ||
1642 | if (!iounit) { | |
1643 | iounit = s->msize - P9_IOHDRSZ; | |
1644 | } | |
1645 | return iounit; | |
1646 | } | |
1647 | ||
a6568fe2 AL |
1648 | static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err) |
1649 | { | |
d62dbb51 | 1650 | if (vs->fidp->fs.dir == NULL) { |
a6568fe2 AL |
1651 | err = -errno; |
1652 | goto out; | |
1653 | } | |
d62dbb51 | 1654 | vs->fidp->fid_type = P9_FID_DIR; |
a6568fe2 AL |
1655 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0); |
1656 | err = vs->offset; | |
1657 | out: | |
1658 | complete_pdu(s, vs->pdu, err); | |
1659 | qemu_free(vs); | |
1660 | ||
1661 | } | |
1662 | ||
5e94c103 MK |
1663 | static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs) |
1664 | { | |
1665 | int err; | |
1666 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit); | |
1667 | err = vs->offset; | |
1668 | complete_pdu(s, vs->pdu, err); | |
1669 | qemu_free(vs); | |
1670 | } | |
1671 | ||
a6568fe2 AL |
1672 | static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err) |
1673 | { | |
d62dbb51 | 1674 | if (vs->fidp->fs.fd == -1) { |
a6568fe2 AL |
1675 | err = -errno; |
1676 | goto out; | |
1677 | } | |
d62dbb51 | 1678 | vs->fidp->fid_type = P9_FID_FILE; |
5e94c103 MK |
1679 | vs->iounit = get_iounit(s, &vs->fidp->path); |
1680 | v9fs_open_post_getiounit(s, vs); | |
1681 | return; | |
a6568fe2 AL |
1682 | out: |
1683 | complete_pdu(s, vs->pdu, err); | |
1684 | qemu_free(vs); | |
1685 | } | |
1686 | ||
771e9d4c MK |
1687 | static inline int valid_flags(int flag) |
1688 | { | |
1689 | if (flag & O_NOCTTY || flag & O_NONBLOCK || flag & O_ASYNC || | |
1690 | flag & O_CLOEXEC) | |
1691 | return 0; | |
1692 | else | |
1693 | return 1; | |
1694 | } | |
1695 | ||
a6568fe2 AL |
1696 | static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err) |
1697 | { | |
771e9d4c MK |
1698 | int flags; |
1699 | ||
a6568fe2 AL |
1700 | if (err) { |
1701 | err = -errno; | |
1702 | goto out; | |
1703 | } | |
1704 | ||
1705 | stat_to_qid(&vs->stbuf, &vs->qid); | |
1706 | ||
1707 | if (S_ISDIR(vs->stbuf.st_mode)) { | |
d62dbb51 | 1708 | vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path); |
a6568fe2 AL |
1709 | v9fs_open_post_opendir(s, vs, err); |
1710 | } else { | |
771e9d4c MK |
1711 | if (s->proto_version == V9FS_PROTO_2000L) { |
1712 | if (!valid_flags(vs->mode)) { | |
1713 | err = -EINVAL; | |
1714 | goto out; | |
1715 | } | |
1716 | flags = vs->mode; | |
1717 | } else { | |
1718 | flags = omode_to_uflags(vs->mode); | |
1719 | } | |
d62dbb51 | 1720 | vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags); |
a6568fe2 AL |
1721 | v9fs_open_post_open(s, vs, err); |
1722 | } | |
1723 | return; | |
1724 | out: | |
1725 | complete_pdu(s, vs->pdu, err); | |
1726 | qemu_free(vs); | |
9f107513 AL |
1727 | } |
1728 | ||
1729 | static void v9fs_open(V9fsState *s, V9fsPDU *pdu) | |
a6568fe2 AL |
1730 | { |
1731 | int32_t fid; | |
1732 | V9fsOpenState *vs; | |
1733 | ssize_t err = 0; | |
1734 | ||
a6568fe2 AL |
1735 | vs = qemu_malloc(sizeof(*vs)); |
1736 | vs->pdu = pdu; | |
1737 | vs->offset = 7; | |
771e9d4c | 1738 | vs->mode = 0; |
a6568fe2 | 1739 | |
771e9d4c MK |
1740 | if (s->proto_version == V9FS_PROTO_2000L) { |
1741 | pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode); | |
1742 | } else { | |
1743 | pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode); | |
1744 | } | |
a6568fe2 AL |
1745 | |
1746 | vs->fidp = lookup_fid(s, fid); | |
1747 | if (vs->fidp == NULL) { | |
1748 | err = -ENOENT; | |
1749 | goto out; | |
1750 | } | |
1751 | ||
d62dbb51 | 1752 | BUG_ON(vs->fidp->fid_type != P9_FID_NONE); |
a6568fe2 AL |
1753 | |
1754 | err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); | |
1755 | ||
1756 | v9fs_open_post_lstat(s, vs, err); | |
1757 | return; | |
1758 | out: | |
1759 | complete_pdu(s, pdu, err); | |
1760 | qemu_free(vs); | |
1761 | } | |
1762 | ||
c1568af5 VJ |
1763 | static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err) |
1764 | { | |
1765 | if (err == 0) { | |
1766 | v9fs_string_copy(&vs->fidp->path, &vs->fullname); | |
1767 | stat_to_qid(&vs->stbuf, &vs->qid); | |
1768 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, | |
1769 | &vs->iounit); | |
1770 | err = vs->offset; | |
1771 | } else { | |
d62dbb51 AK |
1772 | vs->fidp->fid_type = P9_FID_NONE; |
1773 | close(vs->fidp->fs.fd); | |
c1568af5 VJ |
1774 | err = -errno; |
1775 | } | |
1776 | ||
1777 | complete_pdu(s, vs->pdu, err); | |
1778 | v9fs_string_free(&vs->name); | |
1779 | v9fs_string_free(&vs->fullname); | |
1780 | qemu_free(vs); | |
1781 | } | |
1782 | ||
1783 | static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs, | |
1784 | int err) | |
1785 | { | |
1786 | if (err) { | |
1787 | err = -errno; | |
1788 | goto out; | |
1789 | } | |
1790 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
1791 | ||
1792 | out: | |
1793 | v9fs_post_lcreate(s, vs, err); | |
1794 | } | |
1795 | ||
1796 | static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs, | |
1797 | int err) | |
1798 | { | |
d62dbb51 | 1799 | if (vs->fidp->fs.fd == -1) { |
c1568af5 VJ |
1800 | err = -errno; |
1801 | goto out; | |
1802 | } | |
d62dbb51 | 1803 | vs->fidp->fid_type = P9_FID_FILE; |
c1568af5 VJ |
1804 | vs->iounit = get_iounit(s, &vs->fullname); |
1805 | v9fs_lcreate_post_get_iounit(s, vs, err); | |
1806 | return; | |
1807 | ||
1808 | out: | |
1809 | v9fs_post_lcreate(s, vs, err); | |
1810 | } | |
1811 | ||
1812 | static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu) | |
1813 | { | |
1814 | int32_t dfid, flags, mode; | |
1815 | gid_t gid; | |
1816 | V9fsLcreateState *vs; | |
1817 | ssize_t err = 0; | |
1818 | ||
1819 | vs = qemu_malloc(sizeof(*vs)); | |
1820 | vs->pdu = pdu; | |
1821 | vs->offset = 7; | |
1822 | ||
1823 | v9fs_string_init(&vs->fullname); | |
1824 | ||
1825 | pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags, | |
1826 | &mode, &gid); | |
1827 | ||
1828 | vs->fidp = lookup_fid(s, dfid); | |
1829 | if (vs->fidp == NULL) { | |
1830 | err = -ENOENT; | |
1831 | goto out; | |
1832 | } | |
1833 | ||
1834 | v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data, | |
1835 | vs->name.data); | |
1836 | ||
d62dbb51 | 1837 | vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid, |
c1568af5 VJ |
1838 | gid, flags, mode); |
1839 | v9fs_lcreate_post_do_open2(s, vs, err); | |
1840 | return; | |
1841 | ||
1842 | out: | |
1843 | complete_pdu(s, vs->pdu, err); | |
1844 | v9fs_string_free(&vs->name); | |
1845 | qemu_free(vs); | |
1846 | } | |
1847 | ||
a6568fe2 AL |
1848 | static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu) |
1849 | { | |
bbd5697b AL |
1850 | int32_t fid; |
1851 | size_t offset = 7; | |
1852 | int err; | |
1853 | ||
1854 | pdu_unmarshal(pdu, offset, "d", &fid); | |
1855 | ||
1856 | err = free_fid(s, fid); | |
1857 | if (err < 0) { | |
1858 | goto out; | |
a6568fe2 | 1859 | } |
bbd5697b AL |
1860 | |
1861 | offset = 7; | |
1862 | err = offset; | |
1863 | out: | |
1864 | complete_pdu(s, pdu, err); | |
9f107513 AL |
1865 | } |
1866 | ||
a9231555 AL |
1867 | static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t); |
1868 | ||
1869 | static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err) | |
1870 | { | |
1871 | if (err) { | |
1872 | goto out; | |
1873 | } | |
1874 | v9fs_stat_free(&vs->v9stat); | |
1875 | v9fs_string_free(&vs->name); | |
1876 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); | |
1877 | vs->offset += vs->count; | |
1878 | err = vs->offset; | |
1879 | out: | |
1880 | complete_pdu(s, vs->pdu, err); | |
1881 | qemu_free(vs); | |
1882 | return; | |
1883 | } | |
1884 | ||
1885 | static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs, | |
1886 | ssize_t err) | |
1887 | { | |
1888 | if (err) { | |
1889 | err = -errno; | |
1890 | goto out; | |
1891 | } | |
1892 | err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat); | |
1893 | if (err) { | |
1894 | goto out; | |
1895 | } | |
1896 | ||
1897 | vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S", | |
1898 | &vs->v9stat); | |
1899 | if ((vs->len != (vs->v9stat.size + 2)) || | |
1900 | ((vs->count + vs->len) > vs->max_count)) { | |
d62dbb51 | 1901 | v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos); |
a9231555 AL |
1902 | v9fs_read_post_seekdir(s, vs, err); |
1903 | return; | |
1904 | } | |
1905 | vs->count += vs->len; | |
1906 | v9fs_stat_free(&vs->v9stat); | |
1907 | v9fs_string_free(&vs->name); | |
1908 | vs->dir_pos = vs->dent->d_off; | |
d62dbb51 | 1909 | vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); |
a9231555 AL |
1910 | v9fs_read_post_readdir(s, vs, err); |
1911 | return; | |
1912 | out: | |
d62dbb51 | 1913 | v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos); |
a9231555 AL |
1914 | v9fs_read_post_seekdir(s, vs, err); |
1915 | return; | |
1916 | ||
1917 | } | |
1918 | ||
1919 | static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err) | |
1920 | { | |
1921 | if (vs->dent) { | |
1922 | memset(&vs->v9stat, 0, sizeof(vs->v9stat)); | |
1923 | v9fs_string_init(&vs->name); | |
1924 | v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data, | |
1925 | vs->dent->d_name); | |
1926 | err = v9fs_do_lstat(s, &vs->name, &vs->stbuf); | |
1927 | v9fs_read_post_dir_lstat(s, vs, err); | |
1928 | return; | |
1929 | } | |
1930 | ||
1931 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); | |
1932 | vs->offset += vs->count; | |
1933 | err = vs->offset; | |
1934 | complete_pdu(s, vs->pdu, err); | |
1935 | qemu_free(vs); | |
1936 | return; | |
1937 | } | |
1938 | ||
1939 | static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err) | |
1940 | { | |
d62dbb51 | 1941 | vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); |
a9231555 AL |
1942 | v9fs_read_post_readdir(s, vs, err); |
1943 | return; | |
1944 | } | |
1945 | ||
1946 | static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs, | |
1947 | ssize_t err) | |
1948 | { | |
d62dbb51 | 1949 | vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir); |
a9231555 AL |
1950 | v9fs_read_post_telldir(s, vs, err); |
1951 | return; | |
1952 | } | |
1953 | ||
1954 | static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err) | |
1955 | { | |
1956 | if (err < 0) { | |
1957 | /* IO error return the error */ | |
1958 | err = -errno; | |
1959 | goto out; | |
1960 | } | |
1961 | vs->total += vs->len; | |
1962 | vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt); | |
1963 | if (vs->total < vs->count && vs->len > 0) { | |
1964 | do { | |
1965 | if (0) { | |
1966 | print_sg(vs->sg, vs->cnt); | |
1967 | } | |
d62dbb51 | 1968 | vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt); |
a9231555 AL |
1969 | } while (vs->len == -1 && errno == EINTR); |
1970 | if (vs->len == -1) { | |
1971 | err = -errno; | |
1972 | } | |
1973 | v9fs_read_post_readv(s, vs, err); | |
1974 | return; | |
1975 | } | |
1976 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total); | |
1977 | vs->offset += vs->count; | |
1978 | err = vs->offset; | |
1979 | ||
1980 | out: | |
1981 | complete_pdu(s, vs->pdu, err); | |
1982 | qemu_free(vs); | |
1983 | } | |
1984 | ||
1985 | static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err) | |
1986 | { | |
1987 | if (err == -1) { | |
1988 | err = -errno; | |
1989 | goto out; | |
1990 | } | |
1991 | vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt); | |
1992 | ||
1993 | if (vs->total < vs->count) { | |
1994 | do { | |
1995 | if (0) { | |
1996 | print_sg(vs->sg, vs->cnt); | |
1997 | } | |
d62dbb51 | 1998 | vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt); |
a9231555 AL |
1999 | } while (vs->len == -1 && errno == EINTR); |
2000 | if (vs->len == -1) { | |
2001 | err = -errno; | |
2002 | } | |
2003 | v9fs_read_post_readv(s, vs, err); | |
2004 | return; | |
2005 | } | |
2006 | out: | |
2007 | complete_pdu(s, vs->pdu, err); | |
2008 | qemu_free(vs); | |
2009 | } | |
2010 | ||
fa32ef88 AK |
2011 | static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs) |
2012 | { | |
2013 | ssize_t err = 0; | |
2014 | int read_count; | |
2015 | int64_t xattr_len; | |
2016 | ||
2017 | xattr_len = vs->fidp->fs.xattr.len; | |
2018 | read_count = xattr_len - vs->off; | |
2019 | if (read_count > vs->count) { | |
2020 | read_count = vs->count; | |
2021 | } else if (read_count < 0) { | |
2022 | /* | |
2023 | * read beyond XATTR value | |
2024 | */ | |
2025 | read_count = 0; | |
2026 | } | |
2027 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count); | |
2028 | vs->offset += pdu_pack(vs->pdu, vs->offset, | |
2029 | ((char *)vs->fidp->fs.xattr.value) + vs->off, | |
2030 | read_count); | |
2031 | err = vs->offset; | |
2032 | complete_pdu(s, vs->pdu, err); | |
2033 | qemu_free(vs); | |
2034 | } | |
2035 | ||
9f107513 AL |
2036 | static void v9fs_read(V9fsState *s, V9fsPDU *pdu) |
2037 | { | |
a9231555 AL |
2038 | int32_t fid; |
2039 | V9fsReadState *vs; | |
2040 | ssize_t err = 0; | |
2041 | ||
2042 | vs = qemu_malloc(sizeof(*vs)); | |
2043 | vs->pdu = pdu; | |
2044 | vs->offset = 7; | |
2045 | vs->total = 0; | |
2046 | vs->len = 0; | |
2047 | vs->count = 0; | |
2048 | ||
2049 | pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count); | |
2050 | ||
2051 | vs->fidp = lookup_fid(s, fid); | |
2052 | if (vs->fidp == NULL) { | |
2053 | err = -EINVAL; | |
2054 | goto out; | |
2055 | } | |
2056 | ||
fa32ef88 | 2057 | if (vs->fidp->fid_type == P9_FID_DIR) { |
a9231555 AL |
2058 | vs->max_count = vs->count; |
2059 | vs->count = 0; | |
2060 | if (vs->off == 0) { | |
d62dbb51 | 2061 | v9fs_do_rewinddir(s, vs->fidp->fs.dir); |
a9231555 AL |
2062 | } |
2063 | v9fs_read_post_rewinddir(s, vs, err); | |
2064 | return; | |
fa32ef88 | 2065 | } else if (vs->fidp->fid_type == P9_FID_FILE) { |
a9231555 AL |
2066 | vs->sg = vs->iov; |
2067 | pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt); | |
d62dbb51 | 2068 | err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET); |
a9231555 AL |
2069 | v9fs_read_post_lseek(s, vs, err); |
2070 | return; | |
fa32ef88 AK |
2071 | } else if (vs->fidp->fid_type == P9_FID_XATTR) { |
2072 | v9fs_xattr_read(s, vs); | |
2073 | return; | |
a9231555 AL |
2074 | } else { |
2075 | err = -EINVAL; | |
9f107513 | 2076 | } |
a9231555 AL |
2077 | out: |
2078 | complete_pdu(s, pdu, err); | |
2079 | qemu_free(vs); | |
9f107513 AL |
2080 | } |
2081 | ||
c18e2f94 SK |
2082 | typedef struct V9fsReadDirState { |
2083 | V9fsPDU *pdu; | |
2084 | V9fsFidState *fidp; | |
2085 | V9fsQID qid; | |
2086 | off_t saved_dir_pos; | |
2087 | struct dirent *dent; | |
2088 | int32_t count; | |
2089 | int32_t max_count; | |
2090 | size_t offset; | |
2091 | int64_t initial_offset; | |
2092 | V9fsString name; | |
2093 | } V9fsReadDirState; | |
2094 | ||
2095 | static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs) | |
2096 | { | |
2097 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); | |
2098 | vs->offset += vs->count; | |
2099 | complete_pdu(s, vs->pdu, vs->offset); | |
2100 | qemu_free(vs); | |
2101 | return; | |
2102 | } | |
2103 | ||
2104 | /* Size of each dirent on the wire: size of qid (13) + size of offset (8) | |
2105 | * size of type (1) + size of name.size (2) + strlen(name.data) | |
2106 | */ | |
2107 | #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data)) | |
2108 | ||
2109 | static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs) | |
2110 | { | |
2111 | int len; | |
2112 | size_t size; | |
2113 | ||
2114 | if (vs->dent) { | |
2115 | v9fs_string_init(&vs->name); | |
2116 | v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name); | |
2117 | ||
2118 | if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) { | |
2119 | /* Ran out of buffer. Set dir back to old position and return */ | |
d62dbb51 | 2120 | v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos); |
c18e2f94 SK |
2121 | v9fs_readdir_post_seekdir(s, vs); |
2122 | return; | |
2123 | } | |
2124 | ||
2125 | /* Fill up just the path field of qid because the client uses | |
2126 | * only that. To fill the entire qid structure we will have | |
2127 | * to stat each dirent found, which is expensive | |
2128 | */ | |
2129 | size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path)); | |
2130 | memcpy(&vs->qid.path, &vs->dent->d_ino, size); | |
2131 | /* Fill the other fields with dummy values */ | |
2132 | vs->qid.type = 0; | |
2133 | vs->qid.version = 0; | |
2134 | ||
2135 | len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs", | |
2136 | &vs->qid, vs->dent->d_off, | |
2137 | vs->dent->d_type, &vs->name); | |
2138 | vs->count += len; | |
2139 | v9fs_string_free(&vs->name); | |
2140 | vs->saved_dir_pos = vs->dent->d_off; | |
d62dbb51 | 2141 | vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); |
c18e2f94 SK |
2142 | v9fs_readdir_post_readdir(s, vs); |
2143 | return; | |
2144 | } | |
2145 | ||
2146 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); | |
2147 | vs->offset += vs->count; | |
2148 | complete_pdu(s, vs->pdu, vs->offset); | |
2149 | qemu_free(vs); | |
2150 | return; | |
2151 | } | |
2152 | ||
2153 | static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs) | |
2154 | { | |
d62dbb51 | 2155 | vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); |
c18e2f94 SK |
2156 | v9fs_readdir_post_readdir(s, vs); |
2157 | return; | |
2158 | } | |
2159 | ||
2160 | static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs) | |
2161 | { | |
d62dbb51 | 2162 | vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir); |
c18e2f94 SK |
2163 | v9fs_readdir_post_telldir(s, vs); |
2164 | return; | |
2165 | } | |
2166 | ||
2167 | static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu) | |
2168 | { | |
2169 | int32_t fid; | |
2170 | V9fsReadDirState *vs; | |
2171 | ssize_t err = 0; | |
2172 | size_t offset = 7; | |
2173 | ||
2174 | vs = qemu_malloc(sizeof(*vs)); | |
2175 | vs->pdu = pdu; | |
2176 | vs->offset = 7; | |
2177 | vs->count = 0; | |
2178 | ||
2179 | pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset, | |
2180 | &vs->max_count); | |
2181 | ||
2182 | vs->fidp = lookup_fid(s, fid); | |
d62dbb51 | 2183 | if (vs->fidp == NULL || !(vs->fidp->fs.dir)) { |
c18e2f94 SK |
2184 | err = -EINVAL; |
2185 | goto out; | |
2186 | } | |
2187 | ||
2188 | if (vs->initial_offset == 0) { | |
d62dbb51 | 2189 | v9fs_do_rewinddir(s, vs->fidp->fs.dir); |
c18e2f94 | 2190 | } else { |
d62dbb51 | 2191 | v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset); |
c18e2f94 SK |
2192 | } |
2193 | ||
2194 | v9fs_readdir_post_setdir(s, vs); | |
2195 | return; | |
2196 | ||
2197 | out: | |
2198 | complete_pdu(s, pdu, err); | |
2199 | qemu_free(vs); | |
2200 | return; | |
2201 | } | |
2202 | ||
8449360c AL |
2203 | static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs, |
2204 | ssize_t err) | |
2205 | { | |
2206 | if (err < 0) { | |
2207 | /* IO error return the error */ | |
2208 | err = -errno; | |
2209 | goto out; | |
2210 | } | |
2211 | vs->total += vs->len; | |
2212 | vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt); | |
2213 | if (vs->total < vs->count && vs->len > 0) { | |
2214 | do { | |
2215 | if (0) { | |
2216 | print_sg(vs->sg, vs->cnt); | |
2217 | } | |
d62dbb51 | 2218 | vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt); |
8449360c AL |
2219 | } while (vs->len == -1 && errno == EINTR); |
2220 | if (vs->len == -1) { | |
2221 | err = -errno; | |
2222 | } | |
2223 | v9fs_write_post_writev(s, vs, err); | |
2224 | return; | |
2225 | } | |
2226 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total); | |
2227 | ||
2228 | err = vs->offset; | |
2229 | out: | |
2230 | complete_pdu(s, vs->pdu, err); | |
2231 | qemu_free(vs); | |
2232 | } | |
2233 | ||
2234 | static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err) | |
2235 | { | |
2236 | if (err == -1) { | |
2237 | err = -errno; | |
2238 | goto out; | |
2239 | } | |
2240 | vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt); | |
2241 | ||
2242 | if (vs->total < vs->count) { | |
2243 | do { | |
2244 | if (0) { | |
2245 | print_sg(vs->sg, vs->cnt); | |
2246 | } | |
d62dbb51 | 2247 | vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt); |
8449360c AL |
2248 | } while (vs->len == -1 && errno == EINTR); |
2249 | if (vs->len == -1) { | |
2250 | err = -errno; | |
2251 | } | |
2252 | v9fs_write_post_writev(s, vs, err); | |
2253 | return; | |
2254 | } | |
2255 | ||
2256 | out: | |
2257 | complete_pdu(s, vs->pdu, err); | |
2258 | qemu_free(vs); | |
2259 | } | |
2260 | ||
10b468bd AK |
2261 | static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs) |
2262 | { | |
2263 | int i, to_copy; | |
2264 | ssize_t err = 0; | |
2265 | int write_count; | |
2266 | int64_t xattr_len; | |
2267 | ||
2268 | xattr_len = vs->fidp->fs.xattr.len; | |
2269 | write_count = xattr_len - vs->off; | |
2270 | if (write_count > vs->count) { | |
2271 | write_count = vs->count; | |
2272 | } else if (write_count < 0) { | |
2273 | /* | |
2274 | * write beyond XATTR value len specified in | |
2275 | * xattrcreate | |
2276 | */ | |
2277 | err = -ENOSPC; | |
2278 | goto out; | |
2279 | } | |
2280 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count); | |
2281 | err = vs->offset; | |
2282 | vs->fidp->fs.xattr.copied_len += write_count; | |
2283 | /* | |
2284 | * Now copy the content from sg list | |
2285 | */ | |
2286 | for (i = 0; i < vs->cnt; i++) { | |
2287 | if (write_count > vs->sg[i].iov_len) { | |
2288 | to_copy = vs->sg[i].iov_len; | |
2289 | } else { | |
2290 | to_copy = write_count; | |
2291 | } | |
2292 | memcpy((char *)vs->fidp->fs.xattr.value + vs->off, | |
2293 | vs->sg[i].iov_base, to_copy); | |
2294 | /* updating vs->off since we are not using below */ | |
2295 | vs->off += to_copy; | |
2296 | write_count -= to_copy; | |
2297 | } | |
2298 | out: | |
2299 | complete_pdu(s, vs->pdu, err); | |
2300 | qemu_free(vs); | |
2301 | } | |
2302 | ||
9f107513 AL |
2303 | static void v9fs_write(V9fsState *s, V9fsPDU *pdu) |
2304 | { | |
8449360c AL |
2305 | int32_t fid; |
2306 | V9fsWriteState *vs; | |
2307 | ssize_t err; | |
2308 | ||
2309 | vs = qemu_malloc(sizeof(*vs)); | |
2310 | ||
2311 | vs->pdu = pdu; | |
2312 | vs->offset = 7; | |
2313 | vs->sg = vs->iov; | |
2314 | vs->total = 0; | |
2315 | vs->len = 0; | |
2316 | ||
2317 | pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count, | |
10b468bd | 2318 | vs->sg, &vs->cnt); |
8449360c AL |
2319 | |
2320 | vs->fidp = lookup_fid(s, fid); | |
2321 | if (vs->fidp == NULL) { | |
2322 | err = -EINVAL; | |
2323 | goto out; | |
9f107513 | 2324 | } |
8449360c | 2325 | |
10b468bd AK |
2326 | if (vs->fidp->fid_type == P9_FID_FILE) { |
2327 | if (vs->fidp->fs.fd == -1) { | |
2328 | err = -EINVAL; | |
2329 | goto out; | |
2330 | } | |
2331 | } else if (vs->fidp->fid_type == P9_FID_XATTR) { | |
2332 | /* | |
2333 | * setxattr operation | |
2334 | */ | |
2335 | v9fs_xattr_write(s, vs); | |
2336 | return; | |
2337 | } else { | |
8449360c AL |
2338 | err = -EINVAL; |
2339 | goto out; | |
2340 | } | |
d62dbb51 | 2341 | err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET); |
8449360c AL |
2342 | |
2343 | v9fs_write_post_lseek(s, vs, err); | |
2344 | return; | |
2345 | ||
2346 | out: | |
2347 | complete_pdu(s, vs->pdu, err); | |
2348 | qemu_free(vs); | |
9f107513 AL |
2349 | } |
2350 | ||
5e94c103 | 2351 | static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs) |
c494dd6f | 2352 | { |
5e94c103 MK |
2353 | int err; |
2354 | v9fs_string_copy(&vs->fidp->path, &vs->fullname); | |
2355 | stat_to_qid(&vs->stbuf, &vs->qid); | |
c494dd6f | 2356 | |
5e94c103 MK |
2357 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit); |
2358 | err = vs->offset; | |
c494dd6f | 2359 | |
5e94c103 MK |
2360 | complete_pdu(s, vs->pdu, err); |
2361 | v9fs_string_free(&vs->name); | |
2362 | v9fs_string_free(&vs->extension); | |
2363 | v9fs_string_free(&vs->fullname); | |
2364 | qemu_free(vs); | |
2365 | } | |
2366 | ||
2367 | static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err) | |
2368 | { | |
2369 | if (err == 0) { | |
2370 | vs->iounit = get_iounit(s, &vs->fidp->path); | |
2371 | v9fs_create_post_getiounit(s, vs); | |
2372 | return; | |
c494dd6f AL |
2373 | } |
2374 | ||
2375 | complete_pdu(s, vs->pdu, err); | |
2376 | v9fs_string_free(&vs->name); | |
2377 | v9fs_string_free(&vs->extension); | |
2378 | v9fs_string_free(&vs->fullname); | |
2379 | qemu_free(vs); | |
2380 | } | |
2381 | ||
2382 | static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err) | |
2383 | { | |
2384 | if (err) { | |
2385 | err = -errno; | |
2386 | } | |
2387 | v9fs_post_create(s, vs, err); | |
2388 | } | |
2389 | ||
2390 | static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs, | |
2391 | int err) | |
2392 | { | |
d62dbb51 | 2393 | if (!vs->fidp->fs.dir) { |
c494dd6f AL |
2394 | err = -errno; |
2395 | } | |
d62dbb51 | 2396 | vs->fidp->fid_type = P9_FID_DIR; |
c494dd6f AL |
2397 | v9fs_post_create(s, vs, err); |
2398 | } | |
2399 | ||
2400 | static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs, | |
2401 | int err) | |
2402 | { | |
2403 | if (err) { | |
2404 | err = -errno; | |
2405 | goto out; | |
2406 | } | |
2407 | ||
d62dbb51 | 2408 | vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname); |
c494dd6f AL |
2409 | v9fs_create_post_opendir(s, vs, err); |
2410 | return; | |
2411 | ||
2412 | out: | |
2413 | v9fs_post_create(s, vs, err); | |
2414 | } | |
2415 | ||
2416 | static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err) | |
2417 | { | |
2418 | if (err) { | |
2419 | err = -errno; | |
2420 | goto out; | |
2421 | } | |
2422 | ||
2423 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
2424 | v9fs_create_post_dir_lstat(s, vs, err); | |
2425 | return; | |
2426 | ||
2427 | out: | |
2428 | v9fs_post_create(s, vs, err); | |
2429 | } | |
2430 | ||
c494dd6f AL |
2431 | static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err) |
2432 | { | |
2433 | if (err) { | |
d62dbb51 AK |
2434 | vs->fidp->fid_type = P9_FID_NONE; |
2435 | close(vs->fidp->fs.fd); | |
c494dd6f AL |
2436 | err = -errno; |
2437 | } | |
c494dd6f AL |
2438 | v9fs_post_create(s, vs, err); |
2439 | return; | |
2440 | } | |
2441 | ||
2442 | static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err) | |
2443 | { | |
d62dbb51 | 2444 | if (vs->fidp->fs.fd == -1) { |
c494dd6f AL |
2445 | err = -errno; |
2446 | goto out; | |
2447 | } | |
d62dbb51 AK |
2448 | vs->fidp->fid_type = P9_FID_FILE; |
2449 | err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf); | |
c494dd6f AL |
2450 | v9fs_create_post_fstat(s, vs, err); |
2451 | ||
2452 | return; | |
2453 | ||
2454 | out: | |
2455 | v9fs_post_create(s, vs, err); | |
2456 | ||
2457 | } | |
2458 | ||
2459 | static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err) | |
2460 | { | |
2461 | ||
2462 | if (err == 0 || errno != ENOENT) { | |
2463 | err = -errno; | |
2464 | goto out; | |
2465 | } | |
2466 | ||
2467 | if (vs->perm & P9_STAT_MODE_DIR) { | |
b67592ea MK |
2468 | err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777, |
2469 | vs->fidp->uid, -1); | |
c494dd6f AL |
2470 | v9fs_create_post_mkdir(s, vs, err); |
2471 | } else if (vs->perm & P9_STAT_MODE_SYMLINK) { | |
08c60fc9 VJ |
2472 | err = v9fs_do_symlink(s, vs->fidp, vs->extension.data, |
2473 | vs->fullname.data, -1); | |
c494dd6f AL |
2474 | v9fs_create_post_perms(s, vs, err); |
2475 | } else if (vs->perm & P9_STAT_MODE_LINK) { | |
2476 | int32_t nfid = atoi(vs->extension.data); | |
2477 | V9fsFidState *nfidp = lookup_fid(s, nfid); | |
2478 | if (nfidp == NULL) { | |
2479 | err = -errno; | |
2480 | v9fs_post_create(s, vs, err); | |
2481 | } | |
2482 | err = v9fs_do_link(s, &nfidp->path, &vs->fullname); | |
2483 | v9fs_create_post_perms(s, vs, err); | |
2484 | } else if (vs->perm & P9_STAT_MODE_DEVICE) { | |
2485 | char ctype; | |
2486 | uint32_t major, minor; | |
2487 | mode_t nmode = 0; | |
2488 | ||
2489 | if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major, | |
2490 | &minor) != 3) { | |
2491 | err = -errno; | |
2492 | v9fs_post_create(s, vs, err); | |
2493 | } | |
2494 | ||
2495 | switch (ctype) { | |
2496 | case 'c': | |
2497 | nmode = S_IFCHR; | |
2498 | break; | |
2499 | case 'b': | |
2500 | nmode = S_IFBLK; | |
2501 | break; | |
2502 | default: | |
2503 | err = -EIO; | |
2504 | v9fs_post_create(s, vs, err); | |
2505 | } | |
2506 | ||
2507 | nmode |= vs->perm & 0777; | |
5268cecc MK |
2508 | err = v9fs_do_mknod(s, vs->fullname.data, nmode, |
2509 | makedev(major, minor), vs->fidp->uid, -1); | |
c494dd6f AL |
2510 | v9fs_create_post_perms(s, vs, err); |
2511 | } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) { | |
5268cecc MK |
2512 | err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777), |
2513 | 0, vs->fidp->uid, -1); | |
c494dd6f AL |
2514 | v9fs_post_create(s, vs, err); |
2515 | } else if (vs->perm & P9_STAT_MODE_SOCKET) { | |
5268cecc MK |
2516 | err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777), |
2517 | 0, vs->fidp->uid, -1); | |
63729c36 | 2518 | v9fs_post_create(s, vs, err); |
c494dd6f | 2519 | } else { |
d62dbb51 | 2520 | vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid, |
c1568af5 VJ |
2521 | -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm); |
2522 | ||
c494dd6f AL |
2523 | v9fs_create_post_open2(s, vs, err); |
2524 | } | |
2525 | ||
2526 | return; | |
2527 | ||
2528 | out: | |
2529 | v9fs_post_create(s, vs, err); | |
2530 | } | |
2531 | ||
9f107513 AL |
2532 | static void v9fs_create(V9fsState *s, V9fsPDU *pdu) |
2533 | { | |
c494dd6f AL |
2534 | int32_t fid; |
2535 | V9fsCreateState *vs; | |
2536 | int err = 0; | |
2537 | ||
2538 | vs = qemu_malloc(sizeof(*vs)); | |
2539 | vs->pdu = pdu; | |
2540 | vs->offset = 7; | |
2541 | ||
2542 | v9fs_string_init(&vs->fullname); | |
2543 | ||
2544 | pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name, | |
2545 | &vs->perm, &vs->mode, &vs->extension); | |
2546 | ||
2547 | vs->fidp = lookup_fid(s, fid); | |
2548 | if (vs->fidp == NULL) { | |
2549 | err = -EINVAL; | |
2550 | goto out; | |
9f107513 | 2551 | } |
c494dd6f AL |
2552 | |
2553 | v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data, | |
2554 | vs->name.data); | |
2555 | ||
2556 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
2557 | v9fs_create_post_lstat(s, vs, err); | |
2558 | return; | |
2559 | ||
2560 | out: | |
2561 | complete_pdu(s, vs->pdu, err); | |
2562 | v9fs_string_free(&vs->name); | |
2563 | v9fs_string_free(&vs->extension); | |
2564 | qemu_free(vs); | |
9f107513 AL |
2565 | } |
2566 | ||
08c60fc9 VJ |
2567 | static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err) |
2568 | { | |
2569 | if (err == 0) { | |
2570 | stat_to_qid(&vs->stbuf, &vs->qid); | |
2571 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid); | |
2572 | err = vs->offset; | |
2573 | } else { | |
2574 | err = -errno; | |
2575 | } | |
2576 | complete_pdu(s, vs->pdu, err); | |
2577 | v9fs_string_free(&vs->name); | |
2578 | v9fs_string_free(&vs->symname); | |
2579 | v9fs_string_free(&vs->fullname); | |
2580 | qemu_free(vs); | |
2581 | } | |
2582 | ||
2583 | static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs, | |
2584 | int err) | |
2585 | { | |
2586 | if (err) { | |
2587 | goto out; | |
2588 | } | |
2589 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
2590 | out: | |
2591 | v9fs_post_symlink(s, vs, err); | |
2592 | } | |
2593 | ||
2594 | static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu) | |
2595 | { | |
2596 | int32_t dfid; | |
2597 | V9fsSymlinkState *vs; | |
2598 | int err = 0; | |
2599 | gid_t gid; | |
2600 | ||
2601 | vs = qemu_malloc(sizeof(*vs)); | |
2602 | vs->pdu = pdu; | |
2603 | vs->offset = 7; | |
2604 | ||
2605 | v9fs_string_init(&vs->fullname); | |
2606 | ||
2607 | pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name, | |
2608 | &vs->symname, &gid); | |
2609 | ||
2610 | vs->dfidp = lookup_fid(s, dfid); | |
2611 | if (vs->dfidp == NULL) { | |
2612 | err = -EINVAL; | |
2613 | goto out; | |
2614 | } | |
2615 | ||
2616 | v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data, | |
2617 | vs->name.data); | |
2618 | err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data, | |
2619 | vs->fullname.data, gid); | |
2620 | v9fs_symlink_post_do_symlink(s, vs, err); | |
2621 | return; | |
2622 | ||
2623 | out: | |
2624 | complete_pdu(s, vs->pdu, err); | |
2625 | v9fs_string_free(&vs->name); | |
2626 | v9fs_string_free(&vs->symname); | |
2627 | qemu_free(vs); | |
2628 | } | |
2629 | ||
9f107513 AL |
2630 | static void v9fs_flush(V9fsState *s, V9fsPDU *pdu) |
2631 | { | |
9c5e9d89 AL |
2632 | /* A nop call with no return */ |
2633 | complete_pdu(s, pdu, 7); | |
9f107513 AL |
2634 | } |
2635 | ||
b2c224be VJ |
2636 | static void v9fs_link(V9fsState *s, V9fsPDU *pdu) |
2637 | { | |
2638 | int32_t dfid, oldfid; | |
2639 | V9fsFidState *dfidp, *oldfidp; | |
2640 | V9fsString name, fullname; | |
2641 | size_t offset = 7; | |
2642 | int err = 0; | |
2643 | ||
2644 | v9fs_string_init(&fullname); | |
2645 | ||
2646 | pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); | |
2647 | ||
2648 | dfidp = lookup_fid(s, dfid); | |
2649 | if (dfidp == NULL) { | |
2650 | err = -errno; | |
2651 | goto out; | |
2652 | } | |
2653 | ||
2654 | oldfidp = lookup_fid(s, oldfid); | |
2655 | if (oldfidp == NULL) { | |
2656 | err = -errno; | |
2657 | goto out; | |
2658 | } | |
2659 | ||
2660 | v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data); | |
2661 | err = offset; | |
2662 | err = v9fs_do_link(s, &oldfidp->path, &fullname); | |
2663 | if (err) { | |
2664 | err = -errno; | |
2665 | } | |
2666 | v9fs_string_free(&fullname); | |
2667 | ||
2668 | out: | |
2669 | v9fs_string_free(&name); | |
2670 | complete_pdu(s, pdu, err); | |
2671 | } | |
2672 | ||
5bae1900 AL |
2673 | static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs, |
2674 | int err) | |
2675 | { | |
5bae1900 | 2676 | if (err < 0) { |
926487b7 SK |
2677 | err = -errno; |
2678 | } else { | |
2679 | err = vs->offset; | |
5bae1900 AL |
2680 | } |
2681 | ||
926487b7 SK |
2682 | /* For TREMOVE we need to clunk the fid even on failed remove */ |
2683 | free_fid(s, vs->fidp->fid); | |
2684 | ||
5bae1900 AL |
2685 | complete_pdu(s, vs->pdu, err); |
2686 | qemu_free(vs); | |
2687 | } | |
2688 | ||
9f107513 AL |
2689 | static void v9fs_remove(V9fsState *s, V9fsPDU *pdu) |
2690 | { | |
5bae1900 AL |
2691 | int32_t fid; |
2692 | V9fsRemoveState *vs; | |
2693 | int err = 0; | |
2694 | ||
2695 | vs = qemu_malloc(sizeof(*vs)); | |
2696 | vs->pdu = pdu; | |
2697 | vs->offset = 7; | |
2698 | ||
2699 | pdu_unmarshal(vs->pdu, vs->offset, "d", &fid); | |
2700 | ||
2701 | vs->fidp = lookup_fid(s, fid); | |
2702 | if (vs->fidp == NULL) { | |
2703 | err = -EINVAL; | |
2704 | goto out; | |
9f107513 | 2705 | } |
5bae1900 AL |
2706 | |
2707 | err = v9fs_do_remove(s, &vs->fidp->path); | |
2708 | v9fs_remove_post_remove(s, vs, err); | |
2709 | return; | |
2710 | ||
2711 | out: | |
2712 | complete_pdu(s, pdu, err); | |
2713 | qemu_free(vs); | |
9f107513 AL |
2714 | } |
2715 | ||
8cf89e00 AL |
2716 | static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err) |
2717 | { | |
2718 | if (err < 0) { | |
2719 | goto out; | |
2720 | } | |
2721 | ||
2722 | err = vs->offset; | |
2723 | ||
2724 | out: | |
2725 | v9fs_stat_free(&vs->v9stat); | |
2726 | complete_pdu(s, vs->pdu, err); | |
2727 | qemu_free(vs); | |
2728 | } | |
2729 | ||
2730 | static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err) | |
2731 | { | |
2732 | if (err < 0) { | |
2733 | goto out; | |
2734 | } | |
8cf89e00 AL |
2735 | if (vs->v9stat.length != -1) { |
2736 | if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) { | |
2737 | err = -errno; | |
2738 | } | |
2739 | } | |
2740 | v9fs_wstat_post_truncate(s, vs, err); | |
2741 | return; | |
2742 | ||
2743 | out: | |
2744 | v9fs_stat_free(&vs->v9stat); | |
2745 | complete_pdu(s, vs->pdu, err); | |
2746 | qemu_free(vs); | |
2747 | } | |
2748 | ||
c7b4b0b3 | 2749 | static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs) |
8cf89e00 | 2750 | { |
c7b4b0b3 MK |
2751 | int err = 0; |
2752 | char *old_name, *new_name; | |
2753 | char *end; | |
8cf89e00 | 2754 | |
c7b4b0b3 MK |
2755 | if (vs->newdirfid != -1) { |
2756 | V9fsFidState *dirfidp; | |
2757 | dirfidp = lookup_fid(s, vs->newdirfid); | |
2758 | ||
2759 | if (dirfidp == NULL) { | |
2760 | err = -ENOENT; | |
2761 | goto out; | |
2762 | } | |
2763 | ||
d62dbb51 | 2764 | BUG_ON(dirfidp->fid_type != P9_FID_NONE); |
8cf89e00 | 2765 | |
c7b4b0b3 MK |
2766 | new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2); |
2767 | ||
2768 | strcpy(new_name, dirfidp->path.data); | |
2769 | strcat(new_name, "/"); | |
2770 | strcat(new_name + dirfidp->path.size, vs->name.data); | |
2771 | } else { | |
8cf89e00 AL |
2772 | old_name = vs->fidp->path.data; |
2773 | end = strrchr(old_name, '/'); | |
2774 | if (end) { | |
2775 | end++; | |
2776 | } else { | |
2777 | end = old_name; | |
2778 | } | |
c7b4b0b3 | 2779 | new_name = qemu_mallocz(end - old_name + vs->name.size + 1); |
8cf89e00 | 2780 | |
c7b4b0b3 MK |
2781 | strncat(new_name, old_name, end - old_name); |
2782 | strncat(new_name + (end - old_name), vs->name.data, vs->name.size); | |
2783 | } | |
8cf89e00 | 2784 | |
c7b4b0b3 MK |
2785 | v9fs_string_free(&vs->name); |
2786 | vs->name.data = qemu_strdup(new_name); | |
2787 | vs->name.size = strlen(new_name); | |
8cf89e00 | 2788 | |
c7b4b0b3 MK |
2789 | if (strcmp(new_name, vs->fidp->path.data) != 0) { |
2790 | if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) { | |
2791 | err = -errno; | |
2792 | } else { | |
2793 | V9fsFidState *fidp; | |
2794 | /* | |
2795 | * Fixup fid's pointing to the old name to | |
2796 | * start pointing to the new name | |
2797 | */ | |
2798 | for (fidp = s->fid_list; fidp; fidp = fidp->next) { | |
2799 | if (vs->fidp == fidp) { | |
2800 | /* | |
2801 | * we replace name of this fid towards the end | |
2802 | * so that our below strcmp will work | |
2803 | */ | |
2804 | continue; | |
2805 | } | |
2806 | if (!strncmp(vs->fidp->path.data, fidp->path.data, | |
2807 | strlen(vs->fidp->path.data))) { | |
2808 | /* replace the name */ | |
2809 | v9fs_fix_path(&fidp->path, &vs->name, | |
2810 | strlen(vs->fidp->path.data)); | |
8cf89e00 | 2811 | } |
8cf89e00 | 2812 | } |
c7b4b0b3 | 2813 | v9fs_string_copy(&vs->fidp->path, &vs->name); |
8cf89e00 AL |
2814 | } |
2815 | } | |
c7b4b0b3 MK |
2816 | out: |
2817 | v9fs_string_free(&vs->name); | |
2818 | return err; | |
2819 | } | |
2820 | ||
2821 | static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err) | |
2822 | { | |
2823 | complete_pdu(s, vs->pdu, err); | |
2824 | qemu_free(vs); | |
2825 | } | |
2826 | ||
2827 | static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err) | |
2828 | { | |
2829 | if (err < 0) { | |
2830 | goto out; | |
2831 | } | |
2832 | ||
2833 | if (vs->v9stat.name.size != 0) { | |
2834 | V9fsRenameState *vr; | |
2835 | ||
783f04e1 | 2836 | vr = qemu_mallocz(sizeof(V9fsRenameState)); |
c7b4b0b3 MK |
2837 | vr->newdirfid = -1; |
2838 | vr->pdu = vs->pdu; | |
2839 | vr->fidp = vs->fidp; | |
2840 | vr->offset = vs->offset; | |
2841 | vr->name.size = vs->v9stat.name.size; | |
2842 | vr->name.data = qemu_strdup(vs->v9stat.name.data); | |
2843 | ||
2844 | err = v9fs_complete_rename(s, vr); | |
2845 | qemu_free(vr); | |
2846 | } | |
8cf89e00 AL |
2847 | v9fs_wstat_post_rename(s, vs, err); |
2848 | return; | |
2849 | ||
2850 | out: | |
2851 | v9fs_stat_free(&vs->v9stat); | |
2852 | complete_pdu(s, vs->pdu, err); | |
2853 | qemu_free(vs); | |
2854 | } | |
2855 | ||
c7b4b0b3 MK |
2856 | static void v9fs_rename(V9fsState *s, V9fsPDU *pdu) |
2857 | { | |
2858 | int32_t fid; | |
2859 | V9fsRenameState *vs; | |
2860 | ssize_t err = 0; | |
2861 | ||
2862 | vs = qemu_malloc(sizeof(*vs)); | |
2863 | vs->pdu = pdu; | |
2864 | vs->offset = 7; | |
2865 | ||
2866 | pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name); | |
2867 | ||
2868 | vs->fidp = lookup_fid(s, fid); | |
2869 | if (vs->fidp == NULL) { | |
2870 | err = -ENOENT; | |
2871 | goto out; | |
2872 | } | |
2873 | ||
d62dbb51 | 2874 | BUG_ON(vs->fidp->fid_type != P9_FID_NONE); |
c7b4b0b3 MK |
2875 | |
2876 | err = v9fs_complete_rename(s, vs); | |
2877 | v9fs_rename_post_rename(s, vs, err); | |
2878 | return; | |
2879 | out: | |
2880 | complete_pdu(s, vs->pdu, err); | |
2881 | qemu_free(vs); | |
2882 | } | |
2883 | ||
8cf89e00 AL |
2884 | static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err) |
2885 | { | |
2886 | if (err < 0) { | |
2887 | goto out; | |
2888 | } | |
2889 | ||
f7613bee | 2890 | if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) { |
8cf89e00 AL |
2891 | if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid, |
2892 | vs->v9stat.n_gid)) { | |
2893 | err = -errno; | |
2894 | } | |
2895 | } | |
2896 | v9fs_wstat_post_chown(s, vs, err); | |
2897 | return; | |
2898 | ||
2899 | out: | |
2900 | v9fs_stat_free(&vs->v9stat); | |
2901 | complete_pdu(s, vs->pdu, err); | |
2902 | qemu_free(vs); | |
2903 | } | |
2904 | ||
2905 | static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err) | |
2906 | { | |
2907 | if (err < 0) { | |
2908 | goto out; | |
2909 | } | |
2910 | ||
74bc02b2 | 2911 | if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) { |
8fc39ae4 SK |
2912 | struct timespec times[2]; |
2913 | if (vs->v9stat.atime != -1) { | |
2914 | times[0].tv_sec = vs->v9stat.atime; | |
2915 | times[0].tv_nsec = 0; | |
2916 | } else { | |
2917 | times[0].tv_nsec = UTIME_OMIT; | |
2918 | } | |
2919 | if (vs->v9stat.mtime != -1) { | |
2920 | times[1].tv_sec = vs->v9stat.mtime; | |
2921 | times[1].tv_nsec = 0; | |
2922 | } else { | |
2923 | times[1].tv_nsec = UTIME_OMIT; | |
2924 | } | |
2925 | ||
2926 | if (v9fs_do_utimensat(s, &vs->fidp->path, times)) { | |
8cf89e00 AL |
2927 | err = -errno; |
2928 | } | |
2929 | } | |
2930 | ||
2931 | v9fs_wstat_post_utime(s, vs, err); | |
2932 | return; | |
2933 | ||
2934 | out: | |
2935 | v9fs_stat_free(&vs->v9stat); | |
2936 | complete_pdu(s, vs->pdu, err); | |
2937 | qemu_free(vs); | |
2938 | } | |
2939 | ||
2940 | static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err) | |
2941 | { | |
2942 | if (err == -1) { | |
2943 | err = -errno; | |
2944 | } | |
2945 | v9fs_stat_free(&vs->v9stat); | |
2946 | complete_pdu(s, vs->pdu, err); | |
2947 | qemu_free(vs); | |
2948 | } | |
2949 | ||
2950 | static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err) | |
2951 | { | |
2952 | uint32_t v9_mode; | |
2953 | ||
2954 | if (err == -1) { | |
2955 | err = -errno; | |
2956 | goto out; | |
2957 | } | |
2958 | ||
2959 | v9_mode = stat_to_v9mode(&vs->stbuf); | |
2960 | ||
2961 | if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) != | |
2962 | (v9_mode & P9_STAT_MODE_TYPE_BITS)) { | |
2963 | /* Attempting to change the type */ | |
2964 | err = -EIO; | |
2965 | goto out; | |
2966 | } | |
2967 | ||
2968 | if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode, | |
2969 | &vs->v9stat.extension))) { | |
2970 | err = -errno; | |
2971 | } | |
2972 | v9fs_wstat_post_chmod(s, vs, err); | |
2973 | return; | |
2974 | ||
2975 | out: | |
2976 | v9fs_stat_free(&vs->v9stat); | |
2977 | complete_pdu(s, vs->pdu, err); | |
2978 | qemu_free(vs); | |
2979 | } | |
2980 | ||
9f107513 AL |
2981 | static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu) |
2982 | { | |
8cf89e00 AL |
2983 | int32_t fid; |
2984 | V9fsWstatState *vs; | |
2985 | int err = 0; | |
2986 | ||
2987 | vs = qemu_malloc(sizeof(*vs)); | |
2988 | vs->pdu = pdu; | |
2989 | vs->offset = 7; | |
2990 | ||
2991 | pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat); | |
2992 | ||
2993 | vs->fidp = lookup_fid(s, fid); | |
2994 | if (vs->fidp == NULL) { | |
2995 | err = -EINVAL; | |
2996 | goto out; | |
9f107513 | 2997 | } |
8cf89e00 AL |
2998 | |
2999 | /* do we need to sync the file? */ | |
3000 | if (donttouch_stat(&vs->v9stat)) { | |
d62dbb51 | 3001 | err = v9fs_do_fsync(s, vs->fidp->fs.fd); |
8cf89e00 AL |
3002 | v9fs_wstat_post_fsync(s, vs, err); |
3003 | return; | |
3004 | } | |
3005 | ||
3006 | if (vs->v9stat.mode != -1) { | |
3007 | err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); | |
3008 | v9fs_wstat_post_lstat(s, vs, err); | |
3009 | return; | |
3010 | } | |
3011 | ||
3012 | v9fs_wstat_post_chmod(s, vs, err); | |
3013 | return; | |
3014 | ||
3015 | out: | |
3016 | v9fs_stat_free(&vs->v9stat); | |
3017 | complete_pdu(s, vs->pdu, err); | |
3018 | qemu_free(vs); | |
9f107513 AL |
3019 | } |
3020 | ||
be940c87 MK |
3021 | static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err) |
3022 | { | |
5e94c103 MK |
3023 | int32_t bsize_factor; |
3024 | ||
be940c87 MK |
3025 | if (err) { |
3026 | err = -errno; | |
3027 | goto out; | |
3028 | } | |
3029 | ||
5e94c103 MK |
3030 | /* |
3031 | * compute bsize factor based on host file system block size | |
3032 | * and client msize | |
3033 | */ | |
3034 | bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize; | |
3035 | if (!bsize_factor) { | |
3036 | bsize_factor = 1; | |
3037 | } | |
be940c87 MK |
3038 | vs->v9statfs.f_type = vs->stbuf.f_type; |
3039 | vs->v9statfs.f_bsize = vs->stbuf.f_bsize; | |
5e94c103 MK |
3040 | vs->v9statfs.f_bsize *= bsize_factor; |
3041 | /* | |
3042 | * f_bsize is adjusted(multiplied) by bsize factor, so we need to | |
3043 | * adjust(divide) the number of blocks, free blocks and available | |
3044 | * blocks by bsize factor | |
3045 | */ | |
3046 | vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor; | |
3047 | vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor; | |
3048 | vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor; | |
be940c87 MK |
3049 | vs->v9statfs.f_files = vs->stbuf.f_files; |
3050 | vs->v9statfs.f_ffree = vs->stbuf.f_ffree; | |
3051 | vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] | | |
3052 | (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32; | |
3053 | vs->v9statfs.f_namelen = vs->stbuf.f_namelen; | |
3054 | ||
3055 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd", | |
3056 | vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks, | |
3057 | vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files, | |
3058 | vs->v9statfs.f_ffree, vs->v9statfs.fsid_val, | |
3059 | vs->v9statfs.f_namelen); | |
3060 | ||
3061 | out: | |
3062 | complete_pdu(s, vs->pdu, vs->offset); | |
3063 | qemu_free(vs); | |
3064 | } | |
3065 | ||
3066 | static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu) | |
3067 | { | |
3068 | V9fsStatfsState *vs; | |
3069 | ssize_t err = 0; | |
3070 | ||
3071 | vs = qemu_malloc(sizeof(*vs)); | |
3072 | vs->pdu = pdu; | |
3073 | vs->offset = 7; | |
3074 | ||
3075 | memset(&vs->v9statfs, 0, sizeof(vs->v9statfs)); | |
3076 | ||
3077 | pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid); | |
3078 | ||
3079 | vs->fidp = lookup_fid(s, vs->fid); | |
3080 | if (vs->fidp == NULL) { | |
3081 | err = -ENOENT; | |
3082 | goto out; | |
3083 | } | |
3084 | ||
3085 | err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf); | |
3086 | v9fs_statfs_post_statfs(s, vs, err); | |
3087 | return; | |
3088 | ||
3089 | out: | |
3090 | complete_pdu(s, vs->pdu, err); | |
3091 | qemu_free(vs); | |
3092 | } | |
3093 | ||
5268cecc MK |
3094 | static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err) |
3095 | { | |
3096 | if (err == -1) { | |
3097 | err = -errno; | |
3098 | goto out; | |
3099 | } | |
3100 | ||
3101 | stat_to_qid(&vs->stbuf, &vs->qid); | |
3102 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid); | |
3103 | err = vs->offset; | |
3104 | out: | |
3105 | complete_pdu(s, vs->pdu, err); | |
3106 | v9fs_string_free(&vs->fullname); | |
3107 | v9fs_string_free(&vs->name); | |
3108 | qemu_free(vs); | |
3109 | } | |
3110 | ||
3111 | static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err) | |
3112 | { | |
3113 | if (err == -1) { | |
3114 | err = -errno; | |
3115 | goto out; | |
3116 | } | |
3117 | ||
3118 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
3119 | v9fs_mknod_post_lstat(s, vs, err); | |
3120 | return; | |
3121 | out: | |
3122 | complete_pdu(s, vs->pdu, err); | |
3123 | v9fs_string_free(&vs->fullname); | |
3124 | v9fs_string_free(&vs->name); | |
3125 | qemu_free(vs); | |
3126 | } | |
3127 | ||
3128 | static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu) | |
3129 | { | |
3130 | int32_t fid; | |
3131 | V9fsMkState *vs; | |
3132 | int err = 0; | |
3133 | V9fsFidState *fidp; | |
3134 | gid_t gid; | |
3135 | int mode; | |
3136 | int major, minor; | |
3137 | ||
3138 | vs = qemu_malloc(sizeof(*vs)); | |
3139 | vs->pdu = pdu; | |
3140 | vs->offset = 7; | |
3141 | ||
3142 | v9fs_string_init(&vs->fullname); | |
3143 | pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode, | |
3144 | &major, &minor, &gid); | |
3145 | ||
3146 | fidp = lookup_fid(s, fid); | |
3147 | if (fidp == NULL) { | |
3148 | err = -ENOENT; | |
3149 | goto out; | |
3150 | } | |
3151 | ||
3152 | v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data); | |
3153 | err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor), | |
3154 | fidp->uid, gid); | |
3155 | v9fs_mknod_post_mknod(s, vs, err); | |
3156 | return; | |
3157 | ||
3158 | out: | |
3159 | complete_pdu(s, vs->pdu, err); | |
3160 | v9fs_string_free(&vs->fullname); | |
3161 | v9fs_string_free(&vs->name); | |
3162 | qemu_free(vs); | |
3163 | } | |
3164 | ||
b67592ea MK |
3165 | static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err) |
3166 | { | |
3167 | if (err == -1) { | |
3168 | err = -errno; | |
3169 | goto out; | |
3170 | } | |
3171 | ||
3172 | stat_to_qid(&vs->stbuf, &vs->qid); | |
3173 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid); | |
3174 | err = vs->offset; | |
3175 | out: | |
3176 | complete_pdu(s, vs->pdu, err); | |
3177 | v9fs_string_free(&vs->fullname); | |
3178 | v9fs_string_free(&vs->name); | |
3179 | qemu_free(vs); | |
3180 | } | |
3181 | ||
3182 | static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err) | |
3183 | { | |
3184 | if (err == -1) { | |
3185 | err = -errno; | |
3186 | goto out; | |
3187 | } | |
3188 | ||
3189 | err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); | |
3190 | v9fs_mkdir_post_lstat(s, vs, err); | |
3191 | return; | |
3192 | out: | |
3193 | complete_pdu(s, vs->pdu, err); | |
3194 | v9fs_string_free(&vs->fullname); | |
3195 | v9fs_string_free(&vs->name); | |
3196 | qemu_free(vs); | |
3197 | } | |
3198 | ||
3199 | static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu) | |
3200 | { | |
3201 | int32_t fid; | |
3202 | V9fsMkState *vs; | |
3203 | int err = 0; | |
3204 | V9fsFidState *fidp; | |
3205 | gid_t gid; | |
3206 | int mode; | |
3207 | ||
3208 | vs = qemu_malloc(sizeof(*vs)); | |
3209 | vs->pdu = pdu; | |
3210 | vs->offset = 7; | |
3211 | ||
3212 | v9fs_string_init(&vs->fullname); | |
3213 | pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode, | |
3214 | &gid); | |
3215 | ||
3216 | fidp = lookup_fid(s, fid); | |
3217 | if (fidp == NULL) { | |
3218 | err = -ENOENT; | |
3219 | goto out; | |
3220 | } | |
3221 | ||
3222 | v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data); | |
3223 | err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid); | |
3224 | v9fs_mkdir_post_mkdir(s, vs, err); | |
3225 | return; | |
3226 | ||
3227 | out: | |
3228 | complete_pdu(s, vs->pdu, err); | |
3229 | v9fs_string_free(&vs->fullname); | |
3230 | v9fs_string_free(&vs->name); | |
3231 | qemu_free(vs); | |
3232 | } | |
3233 | ||
fa32ef88 AK |
3234 | static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err) |
3235 | { | |
3236 | ||
3237 | if (err < 0) { | |
3238 | err = -errno; | |
3239 | free_fid(s, vs->xattr_fidp->fid); | |
3240 | goto out; | |
3241 | } | |
3242 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size); | |
3243 | err = vs->offset; | |
3244 | out: | |
3245 | complete_pdu(s, vs->pdu, err); | |
3246 | v9fs_string_free(&vs->name); | |
3247 | qemu_free(vs); | |
3248 | return; | |
3249 | } | |
3250 | ||
3251 | static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err) | |
3252 | { | |
3253 | if (err < 0) { | |
3254 | err = -errno; | |
3255 | free_fid(s, vs->xattr_fidp->fid); | |
3256 | goto out; | |
3257 | } | |
3258 | /* | |
3259 | * Read the xattr value | |
3260 | */ | |
3261 | vs->xattr_fidp->fs.xattr.len = vs->size; | |
3262 | vs->xattr_fidp->fid_type = P9_FID_XATTR; | |
3263 | vs->xattr_fidp->fs.xattr.copied_len = -1; | |
3264 | if (vs->size) { | |
3265 | vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size); | |
3266 | err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path, | |
3267 | &vs->name, vs->xattr_fidp->fs.xattr.value, | |
3268 | vs->xattr_fidp->fs.xattr.len); | |
3269 | } | |
3270 | v9fs_post_xattr_getvalue(s, vs, err); | |
3271 | return; | |
3272 | out: | |
3273 | complete_pdu(s, vs->pdu, err); | |
3274 | v9fs_string_free(&vs->name); | |
3275 | qemu_free(vs); | |
3276 | } | |
3277 | ||
3278 | static void v9fs_post_lxattr_getvalue(V9fsState *s, | |
3279 | V9fsXattrState *vs, int err) | |
3280 | { | |
3281 | if (err < 0) { | |
3282 | err = -errno; | |
3283 | free_fid(s, vs->xattr_fidp->fid); | |
3284 | goto out; | |
3285 | } | |
3286 | vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size); | |
3287 | err = vs->offset; | |
3288 | out: | |
3289 | complete_pdu(s, vs->pdu, err); | |
3290 | v9fs_string_free(&vs->name); | |
3291 | qemu_free(vs); | |
3292 | return; | |
3293 | } | |
3294 | ||
3295 | static void v9fs_post_lxattr_check(V9fsState *s, | |
3296 | V9fsXattrState *vs, ssize_t err) | |
3297 | { | |
3298 | if (err < 0) { | |
3299 | err = -errno; | |
3300 | free_fid(s, vs->xattr_fidp->fid); | |
3301 | goto out; | |
3302 | } | |
3303 | /* | |
3304 | * Read the xattr value | |
3305 | */ | |
3306 | vs->xattr_fidp->fs.xattr.len = vs->size; | |
3307 | vs->xattr_fidp->fid_type = P9_FID_XATTR; | |
3308 | vs->xattr_fidp->fs.xattr.copied_len = -1; | |
3309 | if (vs->size) { | |
3310 | vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size); | |
3311 | err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path, | |
3312 | vs->xattr_fidp->fs.xattr.value, | |
3313 | vs->xattr_fidp->fs.xattr.len); | |
3314 | } | |
3315 | v9fs_post_lxattr_getvalue(s, vs, err); | |
3316 | return; | |
3317 | out: | |
3318 | complete_pdu(s, vs->pdu, err); | |
3319 | v9fs_string_free(&vs->name); | |
3320 | qemu_free(vs); | |
3321 | } | |
3322 | ||
3323 | static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu) | |
3324 | { | |
3325 | ssize_t err = 0; | |
3326 | V9fsXattrState *vs; | |
3327 | int32_t fid, newfid; | |
3328 | ||
3329 | vs = qemu_malloc(sizeof(*vs)); | |
3330 | vs->pdu = pdu; | |
3331 | vs->offset = 7; | |
3332 | ||
3333 | pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name); | |
3334 | vs->file_fidp = lookup_fid(s, fid); | |
3335 | if (vs->file_fidp == NULL) { | |
3336 | err = -ENOENT; | |
3337 | goto out; | |
3338 | } | |
3339 | ||
3340 | vs->xattr_fidp = alloc_fid(s, newfid); | |
3341 | if (vs->xattr_fidp == NULL) { | |
3342 | err = -EINVAL; | |
3343 | goto out; | |
3344 | } | |
3345 | ||
3346 | v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path); | |
3347 | if (vs->name.data[0] == 0) { | |
3348 | /* | |
3349 | * listxattr request. Get the size first | |
3350 | */ | |
3351 | vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path, | |
3352 | NULL, 0); | |
3353 | if (vs->size < 0) { | |
3354 | err = vs->size; | |
3355 | } | |
3356 | v9fs_post_lxattr_check(s, vs, err); | |
3357 | return; | |
3358 | } else { | |
3359 | /* | |
3360 | * specific xattr fid. We check for xattr | |
3361 | * presence also collect the xattr size | |
3362 | */ | |
3363 | vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path, | |
3364 | &vs->name, NULL, 0); | |
3365 | if (vs->size < 0) { | |
3366 | err = vs->size; | |
3367 | } | |
3368 | v9fs_post_xattr_check(s, vs, err); | |
3369 | return; | |
3370 | } | |
3371 | out: | |
3372 | complete_pdu(s, vs->pdu, err); | |
3373 | v9fs_string_free(&vs->name); | |
3374 | qemu_free(vs); | |
3375 | } | |
3376 | ||
10b468bd AK |
3377 | static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu) |
3378 | { | |
3379 | int flags; | |
3380 | int32_t fid; | |
3381 | ssize_t err = 0; | |
3382 | V9fsXattrState *vs; | |
3383 | ||
3384 | vs = qemu_malloc(sizeof(*vs)); | |
3385 | vs->pdu = pdu; | |
3386 | vs->offset = 7; | |
3387 | ||
3388 | pdu_unmarshal(vs->pdu, vs->offset, "dsqd", | |
3389 | &fid, &vs->name, &vs->size, &flags); | |
3390 | ||
3391 | vs->file_fidp = lookup_fid(s, fid); | |
3392 | if (vs->file_fidp == NULL) { | |
3393 | err = -EINVAL; | |
3394 | goto out; | |
3395 | } | |
3396 | ||
3397 | /* Make the file fid point to xattr */ | |
3398 | vs->xattr_fidp = vs->file_fidp; | |
3399 | vs->xattr_fidp->fid_type = P9_FID_XATTR; | |
3400 | vs->xattr_fidp->fs.xattr.copied_len = 0; | |
3401 | vs->xattr_fidp->fs.xattr.len = vs->size; | |
3402 | vs->xattr_fidp->fs.xattr.flags = flags; | |
3403 | v9fs_string_init(&vs->xattr_fidp->fs.xattr.name); | |
3404 | v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name); | |
9ed3ef26 AK |
3405 | if (vs->size) |
3406 | vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size); | |
3407 | else | |
3408 | vs->xattr_fidp->fs.xattr.value = NULL; | |
10b468bd AK |
3409 | |
3410 | out: | |
3411 | complete_pdu(s, vs->pdu, err); | |
3412 | v9fs_string_free(&vs->name); | |
3413 | qemu_free(vs); | |
3414 | } | |
fa32ef88 | 3415 | |
9f107513 AL |
3416 | typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu); |
3417 | ||
3418 | static pdu_handler_t *pdu_handlers[] = { | |
c18e2f94 | 3419 | [P9_TREADDIR] = v9fs_readdir, |
be940c87 | 3420 | [P9_TSTATFS] = v9fs_statfs, |
00ede4c2 | 3421 | [P9_TGETATTR] = v9fs_getattr, |
c79ce737 | 3422 | [P9_TSETATTR] = v9fs_setattr, |
fa32ef88 | 3423 | [P9_TXATTRWALK] = v9fs_xattrwalk, |
10b468bd | 3424 | [P9_TXATTRCREATE] = v9fs_xattrcreate, |
5268cecc | 3425 | [P9_TMKNOD] = v9fs_mknod, |
c7b4b0b3 | 3426 | [P9_TRENAME] = v9fs_rename, |
b67592ea | 3427 | [P9_TMKDIR] = v9fs_mkdir, |
9f107513 | 3428 | [P9_TVERSION] = v9fs_version, |
771e9d4c | 3429 | [P9_TLOPEN] = v9fs_open, |
9f107513 AL |
3430 | [P9_TATTACH] = v9fs_attach, |
3431 | [P9_TSTAT] = v9fs_stat, | |
3432 | [P9_TWALK] = v9fs_walk, | |
3433 | [P9_TCLUNK] = v9fs_clunk, | |
3434 | [P9_TOPEN] = v9fs_open, | |
3435 | [P9_TREAD] = v9fs_read, | |
3436 | #if 0 | |
3437 | [P9_TAUTH] = v9fs_auth, | |
3438 | #endif | |
3439 | [P9_TFLUSH] = v9fs_flush, | |
b2c224be | 3440 | [P9_TLINK] = v9fs_link, |
08c60fc9 | 3441 | [P9_TSYMLINK] = v9fs_symlink, |
9f107513 | 3442 | [P9_TCREATE] = v9fs_create, |
c1568af5 | 3443 | [P9_TLCREATE] = v9fs_lcreate, |
9f107513 AL |
3444 | [P9_TWRITE] = v9fs_write, |
3445 | [P9_TWSTAT] = v9fs_wstat, | |
3446 | [P9_TREMOVE] = v9fs_remove, | |
3447 | }; | |
3448 | ||
3449 | static void submit_pdu(V9fsState *s, V9fsPDU *pdu) | |
3450 | { | |
3451 | pdu_handler_t *handler; | |
3452 | ||
3453 | if (debug_9p_pdu) { | |
3454 | pprint_pdu(pdu); | |
3455 | } | |
3456 | ||
3457 | BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers)); | |
3458 | ||
3459 | handler = pdu_handlers[pdu->id]; | |
3460 | BUG_ON(handler == NULL); | |
3461 | ||
3462 | handler(s, pdu); | |
3463 | } | |
3464 | ||
3465 | static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) | |
3466 | { | |
3467 | V9fsState *s = (V9fsState *)vdev; | |
3468 | V9fsPDU *pdu; | |
3469 | ssize_t len; | |
3470 | ||
3471 | while ((pdu = alloc_pdu(s)) && | |
3472 | (len = virtqueue_pop(vq, &pdu->elem)) != 0) { | |
3473 | uint8_t *ptr; | |
3474 | ||
3475 | BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0); | |
3476 | BUG_ON(pdu->elem.out_sg[0].iov_len < 7); | |
3477 | ||
3478 | ptr = pdu->elem.out_sg[0].iov_base; | |
3479 | ||
3480 | memcpy(&pdu->size, ptr, 4); | |
3481 | pdu->id = ptr[4]; | |
3482 | memcpy(&pdu->tag, ptr + 5, 2); | |
3483 | ||
3484 | submit_pdu(s, pdu); | |
3485 | } | |
3486 | ||
3487 | free_pdu(s, pdu); | |
3488 | } | |
3489 | ||
3490 | static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features) | |
3491 | { | |
3492 | features |= 1 << VIRTIO_9P_MOUNT_TAG; | |
3493 | return features; | |
3494 | } | |
3495 | ||
3496 | static V9fsState *to_virtio_9p(VirtIODevice *vdev) | |
3497 | { | |
3498 | return (V9fsState *)vdev; | |
3499 | } | |
3500 | ||
3501 | static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) | |
3502 | { | |
3503 | struct virtio_9p_config *cfg; | |
3504 | V9fsState *s = to_virtio_9p(vdev); | |
3505 | ||
3506 | cfg = qemu_mallocz(sizeof(struct virtio_9p_config) + | |
3507 | s->tag_len); | |
3508 | stw_raw(&cfg->tag_len, s->tag_len); | |
3509 | memcpy(cfg->tag, s->tag, s->tag_len); | |
3510 | memcpy(config, cfg, s->config_size); | |
3511 | qemu_free(cfg); | |
3512 | } | |
3513 | ||
3514 | VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) | |
3515 | { | |
3516 | V9fsState *s; | |
3517 | int i, len; | |
3518 | struct stat stat; | |
3519 | FsTypeEntry *fse; | |
3520 | ||
3521 | ||
3522 | s = (V9fsState *)virtio_common_init("virtio-9p", | |
3523 | VIRTIO_ID_9P, | |
3524 | sizeof(struct virtio_9p_config)+ | |
3525 | MAX_TAG_LEN, | |
3526 | sizeof(V9fsState)); | |
3527 | ||
3528 | /* initialize pdu allocator */ | |
3529 | QLIST_INIT(&s->free_list); | |
3530 | for (i = 0; i < (MAX_REQ - 1); i++) { | |
3531 | QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); | |
3532 | } | |
3533 | ||
3534 | s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); | |
3535 | ||
3536 | fse = get_fsdev_fsentry(conf->fsdev_id); | |
3537 | ||
3538 | if (!fse) { | |
3539 | /* We don't have a fsdev identified by fsdev_id */ | |
3540 | fprintf(stderr, "Virtio-9p device couldn't find fsdev " | |
3541 | "with the id %s\n", conf->fsdev_id); | |
3542 | exit(1); | |
3543 | } | |
3544 | ||
3545 | if (!fse->path || !conf->tag) { | |
3546 | /* we haven't specified a mount_tag or the path */ | |
3547 | fprintf(stderr, "fsdev with id %s needs path " | |
3548 | "and Virtio-9p device needs mount_tag arguments\n", | |
3549 | conf->fsdev_id); | |
3550 | exit(1); | |
3551 | } | |
3552 | ||
758e8e38 VJ |
3553 | if (!strcmp(fse->security_model, "passthrough")) { |
3554 | /* Files on the Fileserver set to client user credentials */ | |
3555 | s->ctx.fs_sm = SM_PASSTHROUGH; | |
3556 | } else if (!strcmp(fse->security_model, "mapped")) { | |
3557 | /* Files on the fileserver are set to QEMU credentials. | |
3558 | * Client user credentials are saved in extended attributes. | |
3559 | */ | |
3560 | s->ctx.fs_sm = SM_MAPPED; | |
12848bfc AK |
3561 | } else if (!strcmp(fse->security_model, "none")) { |
3562 | /* | |
3563 | * Files on the fileserver are set to QEMU credentials. | |
3564 | */ | |
3565 | s->ctx.fs_sm = SM_NONE; | |
3566 | ||
758e8e38 | 3567 | } else { |
12848bfc AK |
3568 | fprintf(stderr, "Default to security_model=none. You may want" |
3569 | " enable advanced security model using " | |
9ce56db6 VJ |
3570 | "security option:\n\t security_model=passthrough \n\t " |
3571 | "security_model=mapped\n"); | |
12848bfc | 3572 | s->ctx.fs_sm = SM_NONE; |
9ce56db6 VJ |
3573 | } |
3574 | ||
9f107513 AL |
3575 | if (lstat(fse->path, &stat)) { |
3576 | fprintf(stderr, "share path %s does not exist\n", fse->path); | |
3577 | exit(1); | |
3578 | } else if (!S_ISDIR(stat.st_mode)) { | |
3579 | fprintf(stderr, "share path %s is not a directory \n", fse->path); | |
3580 | exit(1); | |
3581 | } | |
3582 | ||
3583 | s->ctx.fs_root = qemu_strdup(fse->path); | |
3584 | len = strlen(conf->tag); | |
3585 | if (len > MAX_TAG_LEN) { | |
3586 | len = MAX_TAG_LEN; | |
3587 | } | |
3588 | /* s->tag is non-NULL terminated string */ | |
3589 | s->tag = qemu_malloc(len); | |
3590 | memcpy(s->tag, conf->tag, len); | |
3591 | s->tag_len = len; | |
3592 | s->ctx.uid = -1; | |
3593 | ||
3594 | s->ops = fse->ops; | |
3595 | s->vdev.get_features = virtio_9p_get_features; | |
3596 | s->config_size = sizeof(struct virtio_9p_config) + | |
3597 | s->tag_len; | |
3598 | s->vdev.get_config = virtio_9p_get_config; | |
3599 | ||
3600 | return &s->vdev; | |
3601 | } |