root / hw / virtio-9p.c @ 84151514
History | View | Annotate | Download (55.4 kB)
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 |
|
21 |
int dotu = 1; |
22 |
int debug_9p_pdu;
|
23 |
|
24 |
enum {
|
25 |
Oread = 0x00,
|
26 |
Owrite = 0x01,
|
27 |
Ordwr = 0x02,
|
28 |
Oexec = 0x03,
|
29 |
Oexcl = 0x04,
|
30 |
Otrunc = 0x10,
|
31 |
Orexec = 0x20,
|
32 |
Orclose = 0x40,
|
33 |
Oappend = 0x80,
|
34 |
}; |
35 |
|
36 |
static int omode_to_uflags(int8_t mode) |
37 |
{ |
38 |
int ret = 0; |
39 |
|
40 |
switch (mode & 3) { |
41 |
case Oread:
|
42 |
ret = O_RDONLY; |
43 |
break;
|
44 |
case Ordwr:
|
45 |
ret = O_RDWR; |
46 |
break;
|
47 |
case Owrite:
|
48 |
ret = O_WRONLY; |
49 |
break;
|
50 |
case Oexec:
|
51 |
ret = O_RDONLY; |
52 |
break;
|
53 |
} |
54 |
|
55 |
if (mode & Otrunc) {
|
56 |
ret |= O_TRUNC; |
57 |
} |
58 |
|
59 |
if (mode & Oappend) {
|
60 |
ret |= O_APPEND; |
61 |
} |
62 |
|
63 |
if (mode & Oexcl) {
|
64 |
ret |= O_EXCL; |
65 |
} |
66 |
|
67 |
return ret;
|
68 |
} |
69 |
|
70 |
void cred_init(FsCred *credp)
|
71 |
{ |
72 |
credp->fc_uid = -1;
|
73 |
credp->fc_gid = -1;
|
74 |
credp->fc_mode = -1;
|
75 |
credp->fc_rdev = -1;
|
76 |
} |
77 |
|
78 |
static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf) |
79 |
{ |
80 |
return s->ops->lstat(&s->ctx, path->data, stbuf);
|
81 |
} |
82 |
|
83 |
static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
|
84 |
{ |
85 |
ssize_t len; |
86 |
|
87 |
buf->data = qemu_malloc(1024);
|
88 |
|
89 |
len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1); |
90 |
if (len > -1) { |
91 |
buf->size = len; |
92 |
buf->data[len] = 0;
|
93 |
} |
94 |
|
95 |
return len;
|
96 |
} |
97 |
|
98 |
static int v9fs_do_close(V9fsState *s, int fd) |
99 |
{ |
100 |
return s->ops->close(&s->ctx, fd);
|
101 |
} |
102 |
|
103 |
static int v9fs_do_closedir(V9fsState *s, DIR *dir) |
104 |
{ |
105 |
return s->ops->closedir(&s->ctx, dir);
|
106 |
} |
107 |
|
108 |
static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags) |
109 |
{ |
110 |
return s->ops->open(&s->ctx, path->data, flags);
|
111 |
} |
112 |
|
113 |
static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
|
114 |
{ |
115 |
return s->ops->opendir(&s->ctx, path->data);
|
116 |
} |
117 |
|
118 |
static void v9fs_do_rewinddir(V9fsState *s, DIR *dir) |
119 |
{ |
120 |
return s->ops->rewinddir(&s->ctx, dir);
|
121 |
} |
122 |
|
123 |
static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
|
124 |
{ |
125 |
return s->ops->telldir(&s->ctx, dir);
|
126 |
} |
127 |
|
128 |
static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir) |
129 |
{ |
130 |
return s->ops->readdir(&s->ctx, dir);
|
131 |
} |
132 |
|
133 |
static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off) |
134 |
{ |
135 |
return s->ops->seekdir(&s->ctx, dir, off);
|
136 |
} |
137 |
|
138 |
static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov, |
139 |
int iovcnt)
|
140 |
{ |
141 |
return s->ops->readv(&s->ctx, fd, iov, iovcnt);
|
142 |
} |
143 |
|
144 |
static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence) |
145 |
{ |
146 |
return s->ops->lseek(&s->ctx, fd, offset, whence);
|
147 |
} |
148 |
|
149 |
static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov, |
150 |
int iovcnt)
|
151 |
{ |
152 |
return s->ops->writev(&s->ctx, fd, iov, iovcnt);
|
153 |
} |
154 |
|
155 |
static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode) |
156 |
{ |
157 |
FsCred cred; |
158 |
cred_init(&cred); |
159 |
cred.fc_mode = mode; |
160 |
return s->ops->chmod(&s->ctx, path->data, &cred);
|
161 |
} |
162 |
|
163 |
static int v9fs_do_mknod(V9fsState *s, V9fsCreateState *vs, mode_t mode, |
164 |
dev_t dev) |
165 |
{ |
166 |
FsCred cred; |
167 |
cred_init(&cred); |
168 |
cred.fc_uid = vs->fidp->uid; |
169 |
cred.fc_mode = mode; |
170 |
cred.fc_rdev = dev; |
171 |
return s->ops->mknod(&s->ctx, vs->fullname.data, &cred);
|
172 |
} |
173 |
|
174 |
static int v9fs_do_mkdir(V9fsState *s, V9fsCreateState *vs) |
175 |
{ |
176 |
FsCred cred; |
177 |
|
178 |
cred_init(&cred); |
179 |
cred.fc_uid = vs->fidp->uid; |
180 |
cred.fc_mode = vs->perm & 0777;
|
181 |
|
182 |
return s->ops->mkdir(&s->ctx, vs->fullname.data, &cred);
|
183 |
} |
184 |
|
185 |
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf) |
186 |
{ |
187 |
return s->ops->fstat(&s->ctx, fd, stbuf);
|
188 |
} |
189 |
|
190 |
static int v9fs_do_open2(V9fsState *s, V9fsCreateState *vs) |
191 |
{ |
192 |
FsCred cred; |
193 |
int flags;
|
194 |
|
195 |
cred_init(&cred); |
196 |
cred.fc_uid = vs->fidp->uid; |
197 |
cred.fc_mode = vs->perm & 0777;
|
198 |
flags = omode_to_uflags(vs->mode) | O_CREAT; |
199 |
|
200 |
return s->ops->open2(&s->ctx, vs->fullname.data, flags, &cred);
|
201 |
} |
202 |
|
203 |
static int v9fs_do_symlink(V9fsState *s, V9fsCreateState *vs) |
204 |
{ |
205 |
FsCred cred; |
206 |
cred_init(&cred); |
207 |
cred.fc_uid = vs->fidp->uid; |
208 |
cred.fc_mode = vs->perm | 0777;
|
209 |
|
210 |
return s->ops->symlink(&s->ctx, vs->extension.data, vs->fullname.data,
|
211 |
&cred); |
212 |
} |
213 |
|
214 |
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath) |
215 |
{ |
216 |
return s->ops->link(&s->ctx, oldpath->data, newpath->data);
|
217 |
} |
218 |
|
219 |
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size) |
220 |
{ |
221 |
return s->ops->truncate(&s->ctx, path->data, size);
|
222 |
} |
223 |
|
224 |
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath, |
225 |
V9fsString *newpath) |
226 |
{ |
227 |
return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
|
228 |
} |
229 |
|
230 |
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid) |
231 |
{ |
232 |
FsCred cred; |
233 |
cred_init(&cred); |
234 |
cred.fc_uid = uid; |
235 |
cred.fc_gid = gid; |
236 |
|
237 |
return s->ops->chown(&s->ctx, path->data, &cred);
|
238 |
} |
239 |
|
240 |
static int v9fs_do_utime(V9fsState *s, V9fsString *path, |
241 |
const struct utimbuf *buf) |
242 |
{ |
243 |
return s->ops->utime(&s->ctx, path->data, buf);
|
244 |
} |
245 |
|
246 |
static int v9fs_do_remove(V9fsState *s, V9fsString *path) |
247 |
{ |
248 |
return s->ops->remove(&s->ctx, path->data);
|
249 |
} |
250 |
|
251 |
static int v9fs_do_fsync(V9fsState *s, int fd) |
252 |
{ |
253 |
return s->ops->fsync(&s->ctx, fd);
|
254 |
} |
255 |
|
256 |
static void v9fs_string_init(V9fsString *str) |
257 |
{ |
258 |
str->data = NULL;
|
259 |
str->size = 0;
|
260 |
} |
261 |
|
262 |
static void v9fs_string_free(V9fsString *str) |
263 |
{ |
264 |
qemu_free(str->data); |
265 |
str->data = NULL;
|
266 |
str->size = 0;
|
267 |
} |
268 |
|
269 |
static void v9fs_string_null(V9fsString *str) |
270 |
{ |
271 |
v9fs_string_free(str); |
272 |
} |
273 |
|
274 |
static int number_to_string(void *arg, char type) |
275 |
{ |
276 |
unsigned int ret = 0; |
277 |
|
278 |
switch (type) {
|
279 |
case 'u': { |
280 |
unsigned int num = *(unsigned int *)arg; |
281 |
|
282 |
do {
|
283 |
ret++; |
284 |
num = num/10;
|
285 |
} while (num);
|
286 |
break;
|
287 |
} |
288 |
default:
|
289 |
printf("Number_to_string: Unknown number format\n");
|
290 |
return -1; |
291 |
} |
292 |
|
293 |
return ret;
|
294 |
} |
295 |
|
296 |
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap) |
297 |
{ |
298 |
va_list ap2; |
299 |
char *iter = (char *)fmt; |
300 |
int len = 0; |
301 |
int nr_args = 0; |
302 |
char *arg_char_ptr;
|
303 |
unsigned int arg_uint; |
304 |
|
305 |
/* Find the number of %'s that denotes an argument */
|
306 |
for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { |
307 |
nr_args++; |
308 |
iter++; |
309 |
} |
310 |
|
311 |
len = strlen(fmt) - 2*nr_args;
|
312 |
|
313 |
if (!nr_args) {
|
314 |
goto alloc_print;
|
315 |
} |
316 |
|
317 |
va_copy(ap2, ap); |
318 |
|
319 |
iter = (char *)fmt;
|
320 |
|
321 |
/* Now parse the format string */
|
322 |
for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { |
323 |
iter++; |
324 |
switch (*iter) {
|
325 |
case 'u': |
326 |
arg_uint = va_arg(ap2, unsigned int); |
327 |
len += number_to_string((void *)&arg_uint, 'u'); |
328 |
break;
|
329 |
case 's': |
330 |
arg_char_ptr = va_arg(ap2, char *);
|
331 |
len += strlen(arg_char_ptr); |
332 |
break;
|
333 |
case 'c': |
334 |
len += 1;
|
335 |
break;
|
336 |
default:
|
337 |
fprintf(stderr, |
338 |
"v9fs_string_alloc_printf:Incorrect format %c", *iter);
|
339 |
return -1; |
340 |
} |
341 |
iter++; |
342 |
} |
343 |
|
344 |
alloc_print:
|
345 |
*strp = qemu_malloc((len + 1) * sizeof(**strp)); |
346 |
|
347 |
return vsprintf(*strp, fmt, ap);
|
348 |
} |
349 |
|
350 |
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) |
351 |
{ |
352 |
va_list ap; |
353 |
int err;
|
354 |
|
355 |
v9fs_string_free(str); |
356 |
|
357 |
va_start(ap, fmt); |
358 |
err = v9fs_string_alloc_printf(&str->data, fmt, ap); |
359 |
BUG_ON(err == -1);
|
360 |
va_end(ap); |
361 |
|
362 |
str->size = err; |
363 |
} |
364 |
|
365 |
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs) |
366 |
{ |
367 |
v9fs_string_free(lhs); |
368 |
v9fs_string_sprintf(lhs, "%s", rhs->data);
|
369 |
} |
370 |
|
371 |
static size_t v9fs_string_size(V9fsString *str)
|
372 |
{ |
373 |
return str->size;
|
374 |
} |
375 |
|
376 |
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
|
377 |
{ |
378 |
V9fsFidState *f; |
379 |
|
380 |
for (f = s->fid_list; f; f = f->next) {
|
381 |
if (f->fid == fid) {
|
382 |
return f;
|
383 |
} |
384 |
} |
385 |
|
386 |
return NULL; |
387 |
} |
388 |
|
389 |
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
|
390 |
{ |
391 |
V9fsFidState *f; |
392 |
|
393 |
f = lookup_fid(s, fid); |
394 |
if (f) {
|
395 |
return NULL; |
396 |
} |
397 |
|
398 |
f = qemu_mallocz(sizeof(V9fsFidState));
|
399 |
|
400 |
f->fid = fid; |
401 |
f->fd = -1;
|
402 |
f->dir = NULL;
|
403 |
|
404 |
f->next = s->fid_list; |
405 |
s->fid_list = f; |
406 |
|
407 |
return f;
|
408 |
} |
409 |
|
410 |
static int free_fid(V9fsState *s, int32_t fid) |
411 |
{ |
412 |
V9fsFidState **fidpp, *fidp; |
413 |
|
414 |
for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
|
415 |
if ((*fidpp)->fid == fid) {
|
416 |
break;
|
417 |
} |
418 |
} |
419 |
|
420 |
if (*fidpp == NULL) { |
421 |
return -ENOENT;
|
422 |
} |
423 |
|
424 |
fidp = *fidpp; |
425 |
*fidpp = fidp->next; |
426 |
|
427 |
if (fidp->fd != -1) { |
428 |
v9fs_do_close(s, fidp->fd); |
429 |
} |
430 |
if (fidp->dir) {
|
431 |
v9fs_do_closedir(s, fidp->dir); |
432 |
} |
433 |
v9fs_string_free(&fidp->path); |
434 |
qemu_free(fidp); |
435 |
|
436 |
return 0; |
437 |
} |
438 |
|
439 |
#define P9_QID_TYPE_DIR 0x80 |
440 |
#define P9_QID_TYPE_SYMLINK 0x02 |
441 |
|
442 |
#define P9_STAT_MODE_DIR 0x80000000 |
443 |
#define P9_STAT_MODE_APPEND 0x40000000 |
444 |
#define P9_STAT_MODE_EXCL 0x20000000 |
445 |
#define P9_STAT_MODE_MOUNT 0x10000000 |
446 |
#define P9_STAT_MODE_AUTH 0x08000000 |
447 |
#define P9_STAT_MODE_TMP 0x04000000 |
448 |
#define P9_STAT_MODE_SYMLINK 0x02000000 |
449 |
#define P9_STAT_MODE_LINK 0x01000000 |
450 |
#define P9_STAT_MODE_DEVICE 0x00800000 |
451 |
#define P9_STAT_MODE_NAMED_PIPE 0x00200000 |
452 |
#define P9_STAT_MODE_SOCKET 0x00100000 |
453 |
#define P9_STAT_MODE_SETUID 0x00080000 |
454 |
#define P9_STAT_MODE_SETGID 0x00040000 |
455 |
#define P9_STAT_MODE_SETVTX 0x00010000 |
456 |
|
457 |
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
|
458 |
P9_STAT_MODE_SYMLINK | \ |
459 |
P9_STAT_MODE_LINK | \ |
460 |
P9_STAT_MODE_DEVICE | \ |
461 |
P9_STAT_MODE_NAMED_PIPE | \ |
462 |
P9_STAT_MODE_SOCKET) |
463 |
|
464 |
/* This is the algorithm from ufs in spfs */
|
465 |
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp) |
466 |
{ |
467 |
size_t size; |
468 |
|
469 |
size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); |
470 |
memcpy(&qidp->path, &stbuf->st_ino, size); |
471 |
qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
|
472 |
qidp->type = 0;
|
473 |
if (S_ISDIR(stbuf->st_mode)) {
|
474 |
qidp->type |= P9_QID_TYPE_DIR; |
475 |
} |
476 |
if (S_ISLNK(stbuf->st_mode)) {
|
477 |
qidp->type |= P9_QID_TYPE_SYMLINK; |
478 |
} |
479 |
} |
480 |
|
481 |
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp) |
482 |
{ |
483 |
struct stat stbuf;
|
484 |
int err;
|
485 |
|
486 |
err = v9fs_do_lstat(s, &fidp->path, &stbuf); |
487 |
if (err) {
|
488 |
return err;
|
489 |
} |
490 |
|
491 |
stat_to_qid(&stbuf, qidp); |
492 |
return 0; |
493 |
} |
494 |
|
495 |
static V9fsPDU *alloc_pdu(V9fsState *s)
|
496 |
{ |
497 |
V9fsPDU *pdu = NULL;
|
498 |
|
499 |
if (!QLIST_EMPTY(&s->free_list)) {
|
500 |
pdu = QLIST_FIRST(&s->free_list); |
501 |
QLIST_REMOVE(pdu, next); |
502 |
} |
503 |
return pdu;
|
504 |
} |
505 |
|
506 |
static void free_pdu(V9fsState *s, V9fsPDU *pdu) |
507 |
{ |
508 |
if (pdu) {
|
509 |
QLIST_INSERT_HEAD(&s->free_list, pdu, next); |
510 |
} |
511 |
} |
512 |
|
513 |
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count, |
514 |
size_t offset, size_t size, int pack)
|
515 |
{ |
516 |
int i = 0; |
517 |
size_t copied = 0;
|
518 |
|
519 |
for (i = 0; size && i < sg_count; i++) { |
520 |
size_t len; |
521 |
if (offset >= sg[i].iov_len) {
|
522 |
/* skip this sg */
|
523 |
offset -= sg[i].iov_len; |
524 |
continue;
|
525 |
} else {
|
526 |
len = MIN(sg[i].iov_len - offset, size); |
527 |
if (pack) {
|
528 |
memcpy(sg[i].iov_base + offset, addr, len); |
529 |
} else {
|
530 |
memcpy(addr, sg[i].iov_base + offset, len); |
531 |
} |
532 |
size -= len; |
533 |
copied += len; |
534 |
addr += len; |
535 |
if (size) {
|
536 |
offset = 0;
|
537 |
continue;
|
538 |
} |
539 |
} |
540 |
} |
541 |
|
542 |
return copied;
|
543 |
} |
544 |
|
545 |
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size) |
546 |
{ |
547 |
return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
|
548 |
offset, size, 0);
|
549 |
} |
550 |
|
551 |
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src, |
552 |
size_t size) |
553 |
{ |
554 |
return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num, |
555 |
offset, size, 1);
|
556 |
} |
557 |
|
558 |
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg) |
559 |
{ |
560 |
size_t pos = 0;
|
561 |
int i, j;
|
562 |
struct iovec *src_sg;
|
563 |
unsigned int num; |
564 |
|
565 |
if (rx) {
|
566 |
src_sg = pdu->elem.in_sg; |
567 |
num = pdu->elem.in_num; |
568 |
} else {
|
569 |
src_sg = pdu->elem.out_sg; |
570 |
num = pdu->elem.out_num; |
571 |
} |
572 |
|
573 |
j = 0;
|
574 |
for (i = 0; i < num; i++) { |
575 |
if (offset <= pos) {
|
576 |
sg[j].iov_base = src_sg[i].iov_base; |
577 |
sg[j].iov_len = src_sg[i].iov_len; |
578 |
j++; |
579 |
} else if (offset < (src_sg[i].iov_len + pos)) { |
580 |
sg[j].iov_base = src_sg[i].iov_base; |
581 |
sg[j].iov_len = src_sg[i].iov_len; |
582 |
sg[j].iov_base += (offset - pos); |
583 |
sg[j].iov_len -= (offset - pos); |
584 |
j++; |
585 |
} |
586 |
pos += src_sg[i].iov_len; |
587 |
} |
588 |
|
589 |
return j;
|
590 |
} |
591 |
|
592 |
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) |
593 |
{ |
594 |
size_t old_offset = offset; |
595 |
va_list ap; |
596 |
int i;
|
597 |
|
598 |
va_start(ap, fmt); |
599 |
for (i = 0; fmt[i]; i++) { |
600 |
switch (fmt[i]) {
|
601 |
case 'b': { |
602 |
uint8_t *valp = va_arg(ap, uint8_t *); |
603 |
offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
|
604 |
break;
|
605 |
} |
606 |
case 'w': { |
607 |
uint16_t val, *valp; |
608 |
valp = va_arg(ap, uint16_t *); |
609 |
val = le16_to_cpupu(valp); |
610 |
offset += pdu_unpack(&val, pdu, offset, sizeof(val));
|
611 |
*valp = val; |
612 |
break;
|
613 |
} |
614 |
case 'd': { |
615 |
uint32_t val, *valp; |
616 |
valp = va_arg(ap, uint32_t *); |
617 |
val = le32_to_cpupu(valp); |
618 |
offset += pdu_unpack(&val, pdu, offset, sizeof(val));
|
619 |
*valp = val; |
620 |
break;
|
621 |
} |
622 |
case 'q': { |
623 |
uint64_t val, *valp; |
624 |
valp = va_arg(ap, uint64_t *); |
625 |
val = le64_to_cpup(valp); |
626 |
offset += pdu_unpack(&val, pdu, offset, sizeof(val));
|
627 |
*valp = val; |
628 |
break;
|
629 |
} |
630 |
case 'v': { |
631 |
struct iovec *iov = va_arg(ap, struct iovec *); |
632 |
int *iovcnt = va_arg(ap, int *); |
633 |
*iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
|
634 |
break;
|
635 |
} |
636 |
case 's': { |
637 |
V9fsString *str = va_arg(ap, V9fsString *); |
638 |
offset += pdu_unmarshal(pdu, offset, "w", &str->size);
|
639 |
/* FIXME: sanity check str->size */
|
640 |
str->data = qemu_malloc(str->size + 1);
|
641 |
offset += pdu_unpack(str->data, pdu, offset, str->size); |
642 |
str->data[str->size] = 0;
|
643 |
break;
|
644 |
} |
645 |
case 'Q': { |
646 |
V9fsQID *qidp = va_arg(ap, V9fsQID *); |
647 |
offset += pdu_unmarshal(pdu, offset, "bdq",
|
648 |
&qidp->type, &qidp->version, &qidp->path); |
649 |
break;
|
650 |
} |
651 |
case 'S': { |
652 |
V9fsStat *statp = va_arg(ap, V9fsStat *); |
653 |
offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
|
654 |
&statp->size, &statp->type, &statp->dev, |
655 |
&statp->qid, &statp->mode, &statp->atime, |
656 |
&statp->mtime, &statp->length, |
657 |
&statp->name, &statp->uid, &statp->gid, |
658 |
&statp->muid, &statp->extension, |
659 |
&statp->n_uid, &statp->n_gid, |
660 |
&statp->n_muid); |
661 |
break;
|
662 |
} |
663 |
default:
|
664 |
break;
|
665 |
} |
666 |
} |
667 |
|
668 |
va_end(ap); |
669 |
|
670 |
return offset - old_offset;
|
671 |
} |
672 |
|
673 |
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) |
674 |
{ |
675 |
size_t old_offset = offset; |
676 |
va_list ap; |
677 |
int i;
|
678 |
|
679 |
va_start(ap, fmt); |
680 |
for (i = 0; fmt[i]; i++) { |
681 |
switch (fmt[i]) {
|
682 |
case 'b': { |
683 |
uint8_t val = va_arg(ap, int);
|
684 |
offset += pdu_pack(pdu, offset, &val, sizeof(val));
|
685 |
break;
|
686 |
} |
687 |
case 'w': { |
688 |
uint16_t val; |
689 |
cpu_to_le16w(&val, va_arg(ap, int));
|
690 |
offset += pdu_pack(pdu, offset, &val, sizeof(val));
|
691 |
break;
|
692 |
} |
693 |
case 'd': { |
694 |
uint32_t val; |
695 |
cpu_to_le32w(&val, va_arg(ap, uint32_t)); |
696 |
offset += pdu_pack(pdu, offset, &val, sizeof(val));
|
697 |
break;
|
698 |
} |
699 |
case 'q': { |
700 |
uint64_t val; |
701 |
cpu_to_le64w(&val, va_arg(ap, uint64_t)); |
702 |
offset += pdu_pack(pdu, offset, &val, sizeof(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, 1, iov);
|
709 |
break;
|
710 |
} |
711 |
case 's': { |
712 |
V9fsString *str = va_arg(ap, V9fsString *); |
713 |
offset += pdu_marshal(pdu, offset, "w", str->size);
|
714 |
offset += pdu_pack(pdu, offset, str->data, str->size); |
715 |
break;
|
716 |
} |
717 |
case 'Q': { |
718 |
V9fsQID *qidp = va_arg(ap, V9fsQID *); |
719 |
offset += pdu_marshal(pdu, offset, "bdq",
|
720 |
qidp->type, qidp->version, qidp->path); |
721 |
break;
|
722 |
} |
723 |
case 'S': { |
724 |
V9fsStat *statp = va_arg(ap, V9fsStat *); |
725 |
offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
|
726 |
statp->size, statp->type, statp->dev, |
727 |
&statp->qid, statp->mode, statp->atime, |
728 |
statp->mtime, statp->length, &statp->name, |
729 |
&statp->uid, &statp->gid, &statp->muid, |
730 |
&statp->extension, statp->n_uid, |
731 |
statp->n_gid, statp->n_muid); |
732 |
break;
|
733 |
} |
734 |
default:
|
735 |
break;
|
736 |
} |
737 |
} |
738 |
va_end(ap); |
739 |
|
740 |
return offset - old_offset;
|
741 |
} |
742 |
|
743 |
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len) |
744 |
{ |
745 |
int8_t id = pdu->id + 1; /* Response */ |
746 |
|
747 |
if (len < 0) { |
748 |
V9fsString str; |
749 |
int err = -len;
|
750 |
|
751 |
str.data = strerror(err); |
752 |
str.size = strlen(str.data); |
753 |
|
754 |
len = 7;
|
755 |
len += pdu_marshal(pdu, len, "s", &str);
|
756 |
if (dotu) {
|
757 |
len += pdu_marshal(pdu, len, "d", err);
|
758 |
} |
759 |
|
760 |
id = P9_RERROR; |
761 |
} |
762 |
|
763 |
/* fill out the header */
|
764 |
pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag); |
765 |
|
766 |
/* keep these in sync */
|
767 |
pdu->size = len; |
768 |
pdu->id = id; |
769 |
|
770 |
/* push onto queue and notify */
|
771 |
virtqueue_push(s->vq, &pdu->elem, len); |
772 |
|
773 |
/* FIXME: we should batch these completions */
|
774 |
virtio_notify(&s->vdev, s->vq); |
775 |
|
776 |
free_pdu(s, pdu); |
777 |
} |
778 |
|
779 |
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
|
780 |
{ |
781 |
mode_t ret; |
782 |
|
783 |
ret = mode & 0777;
|
784 |
if (mode & P9_STAT_MODE_DIR) {
|
785 |
ret |= S_IFDIR; |
786 |
} |
787 |
|
788 |
if (dotu) {
|
789 |
if (mode & P9_STAT_MODE_SYMLINK) {
|
790 |
ret |= S_IFLNK; |
791 |
} |
792 |
if (mode & P9_STAT_MODE_SOCKET) {
|
793 |
ret |= S_IFSOCK; |
794 |
} |
795 |
if (mode & P9_STAT_MODE_NAMED_PIPE) {
|
796 |
ret |= S_IFIFO; |
797 |
} |
798 |
if (mode & P9_STAT_MODE_DEVICE) {
|
799 |
if (extension && extension->data[0] == 'c') { |
800 |
ret |= S_IFCHR; |
801 |
} else {
|
802 |
ret |= S_IFBLK; |
803 |
} |
804 |
} |
805 |
} |
806 |
|
807 |
if (!(ret&~0777)) { |
808 |
ret |= S_IFREG; |
809 |
} |
810 |
|
811 |
if (mode & P9_STAT_MODE_SETUID) {
|
812 |
ret |= S_ISUID; |
813 |
} |
814 |
if (mode & P9_STAT_MODE_SETGID) {
|
815 |
ret |= S_ISGID; |
816 |
} |
817 |
if (mode & P9_STAT_MODE_SETVTX) {
|
818 |
ret |= S_ISVTX; |
819 |
} |
820 |
|
821 |
return ret;
|
822 |
} |
823 |
|
824 |
static int donttouch_stat(V9fsStat *stat) |
825 |
{ |
826 |
if (stat->type == -1 && |
827 |
stat->dev == -1 &&
|
828 |
stat->qid.type == -1 &&
|
829 |
stat->qid.version == -1 &&
|
830 |
stat->qid.path == -1 &&
|
831 |
stat->mode == -1 &&
|
832 |
stat->atime == -1 &&
|
833 |
stat->mtime == -1 &&
|
834 |
stat->length == -1 &&
|
835 |
!stat->name.size && |
836 |
!stat->uid.size && |
837 |
!stat->gid.size && |
838 |
!stat->muid.size && |
839 |
stat->n_uid == -1 &&
|
840 |
stat->n_gid == -1 &&
|
841 |
stat->n_muid == -1) {
|
842 |
return 1; |
843 |
} |
844 |
|
845 |
return 0; |
846 |
} |
847 |
|
848 |
static void v9fs_stat_free(V9fsStat *stat) |
849 |
{ |
850 |
v9fs_string_free(&stat->name); |
851 |
v9fs_string_free(&stat->uid); |
852 |
v9fs_string_free(&stat->gid); |
853 |
v9fs_string_free(&stat->muid); |
854 |
v9fs_string_free(&stat->extension); |
855 |
} |
856 |
|
857 |
static uint32_t stat_to_v9mode(const struct stat *stbuf) |
858 |
{ |
859 |
uint32_t mode; |
860 |
|
861 |
mode = stbuf->st_mode & 0777;
|
862 |
if (S_ISDIR(stbuf->st_mode)) {
|
863 |
mode |= P9_STAT_MODE_DIR; |
864 |
} |
865 |
|
866 |
if (dotu) {
|
867 |
if (S_ISLNK(stbuf->st_mode)) {
|
868 |
mode |= P9_STAT_MODE_SYMLINK; |
869 |
} |
870 |
|
871 |
if (S_ISSOCK(stbuf->st_mode)) {
|
872 |
mode |= P9_STAT_MODE_SOCKET; |
873 |
} |
874 |
|
875 |
if (S_ISFIFO(stbuf->st_mode)) {
|
876 |
mode |= P9_STAT_MODE_NAMED_PIPE; |
877 |
} |
878 |
|
879 |
if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
|
880 |
mode |= P9_STAT_MODE_DEVICE; |
881 |
} |
882 |
|
883 |
if (stbuf->st_mode & S_ISUID) {
|
884 |
mode |= P9_STAT_MODE_SETUID; |
885 |
} |
886 |
|
887 |
if (stbuf->st_mode & S_ISGID) {
|
888 |
mode |= P9_STAT_MODE_SETGID; |
889 |
} |
890 |
|
891 |
if (stbuf->st_mode & S_ISVTX) {
|
892 |
mode |= P9_STAT_MODE_SETVTX; |
893 |
} |
894 |
} |
895 |
|
896 |
return mode;
|
897 |
} |
898 |
|
899 |
static int stat_to_v9stat(V9fsState *s, V9fsString *name, |
900 |
const struct stat *stbuf, |
901 |
V9fsStat *v9stat) |
902 |
{ |
903 |
int err;
|
904 |
const char *str; |
905 |
|
906 |
memset(v9stat, 0, sizeof(*v9stat)); |
907 |
|
908 |
stat_to_qid(stbuf, &v9stat->qid); |
909 |
v9stat->mode = stat_to_v9mode(stbuf); |
910 |
v9stat->atime = stbuf->st_atime; |
911 |
v9stat->mtime = stbuf->st_mtime; |
912 |
v9stat->length = stbuf->st_size; |
913 |
|
914 |
v9fs_string_null(&v9stat->uid); |
915 |
v9fs_string_null(&v9stat->gid); |
916 |
v9fs_string_null(&v9stat->muid); |
917 |
|
918 |
if (dotu) {
|
919 |
v9stat->n_uid = stbuf->st_uid; |
920 |
v9stat->n_gid = stbuf->st_gid; |
921 |
v9stat->n_muid = 0;
|
922 |
|
923 |
v9fs_string_null(&v9stat->extension); |
924 |
|
925 |
if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
|
926 |
err = v9fs_do_readlink(s, name, &v9stat->extension); |
927 |
if (err == -1) { |
928 |
err = -errno; |
929 |
return err;
|
930 |
} |
931 |
v9stat->extension.data[err] = 0;
|
932 |
v9stat->extension.size = err; |
933 |
} else if (v9stat->mode & P9_STAT_MODE_DEVICE) { |
934 |
v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
|
935 |
S_ISCHR(stbuf->st_mode) ? 'c' : 'b', |
936 |
major(stbuf->st_rdev), minor(stbuf->st_rdev)); |
937 |
} else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { |
938 |
v9fs_string_sprintf(&v9stat->extension, "%s %u",
|
939 |
"HARDLINKCOUNT", stbuf->st_nlink);
|
940 |
} |
941 |
} |
942 |
|
943 |
str = strrchr(name->data, '/');
|
944 |
if (str) {
|
945 |
str += 1;
|
946 |
} else {
|
947 |
str = name->data; |
948 |
} |
949 |
|
950 |
v9fs_string_sprintf(&v9stat->name, "%s", str);
|
951 |
|
952 |
v9stat->size = 61 +
|
953 |
v9fs_string_size(&v9stat->name) + |
954 |
v9fs_string_size(&v9stat->uid) + |
955 |
v9fs_string_size(&v9stat->gid) + |
956 |
v9fs_string_size(&v9stat->muid) + |
957 |
v9fs_string_size(&v9stat->extension); |
958 |
return 0; |
959 |
} |
960 |
|
961 |
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt) |
962 |
{ |
963 |
while (len && *iovcnt) {
|
964 |
if (len < sg->iov_len) {
|
965 |
sg->iov_len -= len; |
966 |
sg->iov_base += len; |
967 |
len = 0;
|
968 |
} else {
|
969 |
len -= sg->iov_len; |
970 |
sg++; |
971 |
*iovcnt -= 1;
|
972 |
} |
973 |
} |
974 |
|
975 |
return sg;
|
976 |
} |
977 |
|
978 |
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt) |
979 |
{ |
980 |
int i;
|
981 |
int total = 0; |
982 |
|
983 |
for (i = 0; i < *cnt; i++) { |
984 |
if ((total + sg[i].iov_len) > cap) {
|
985 |
sg[i].iov_len -= ((total + sg[i].iov_len) - cap); |
986 |
i++; |
987 |
break;
|
988 |
} |
989 |
total += sg[i].iov_len; |
990 |
} |
991 |
|
992 |
*cnt = i; |
993 |
|
994 |
return sg;
|
995 |
} |
996 |
|
997 |
static void print_sg(struct iovec *sg, int cnt) |
998 |
{ |
999 |
int i;
|
1000 |
|
1001 |
printf("sg[%d]: {", cnt);
|
1002 |
for (i = 0; i < cnt; i++) { |
1003 |
if (i) {
|
1004 |
printf(", ");
|
1005 |
} |
1006 |
printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
|
1007 |
} |
1008 |
printf("}\n");
|
1009 |
} |
1010 |
|
1011 |
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len) |
1012 |
{ |
1013 |
V9fsString str; |
1014 |
v9fs_string_init(&str); |
1015 |
v9fs_string_copy(&str, dst); |
1016 |
v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
|
1017 |
v9fs_string_free(&str); |
1018 |
} |
1019 |
|
1020 |
static void v9fs_version(V9fsState *s, V9fsPDU *pdu) |
1021 |
{ |
1022 |
int32_t msize; |
1023 |
V9fsString version; |
1024 |
size_t offset = 7;
|
1025 |
|
1026 |
pdu_unmarshal(pdu, offset, "ds", &msize, &version);
|
1027 |
|
1028 |
if (!strcmp(version.data, "9P2000.u")) { |
1029 |
s->proto_version = V9FS_PROTO_2000U; |
1030 |
} else if (!strcmp(version.data, "9P2000.L")) { |
1031 |
s->proto_version = V9FS_PROTO_2000L; |
1032 |
} else {
|
1033 |
v9fs_string_sprintf(&version, "unknown");
|
1034 |
} |
1035 |
|
1036 |
offset += pdu_marshal(pdu, offset, "ds", msize, &version);
|
1037 |
complete_pdu(s, pdu, offset); |
1038 |
|
1039 |
v9fs_string_free(&version); |
1040 |
} |
1041 |
|
1042 |
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu) |
1043 |
{ |
1044 |
int32_t fid, afid, n_uname; |
1045 |
V9fsString uname, aname; |
1046 |
V9fsFidState *fidp; |
1047 |
V9fsQID qid; |
1048 |
size_t offset = 7;
|
1049 |
ssize_t err; |
1050 |
|
1051 |
pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
|
1052 |
|
1053 |
fidp = alloc_fid(s, fid); |
1054 |
if (fidp == NULL) { |
1055 |
err = -EINVAL; |
1056 |
goto out;
|
1057 |
} |
1058 |
|
1059 |
fidp->uid = n_uname; |
1060 |
|
1061 |
v9fs_string_sprintf(&fidp->path, "%s", "/"); |
1062 |
err = fid_to_qid(s, fidp, &qid); |
1063 |
if (err) {
|
1064 |
err = -EINVAL; |
1065 |
free_fid(s, fid); |
1066 |
goto out;
|
1067 |
} |
1068 |
|
1069 |
offset += pdu_marshal(pdu, offset, "Q", &qid);
|
1070 |
|
1071 |
err = offset; |
1072 |
out:
|
1073 |
complete_pdu(s, pdu, err); |
1074 |
v9fs_string_free(&uname); |
1075 |
v9fs_string_free(&aname); |
1076 |
} |
1077 |
|
1078 |
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err) |
1079 |
{ |
1080 |
if (err == -1) { |
1081 |
err = -errno; |
1082 |
goto out;
|
1083 |
} |
1084 |
|
1085 |
err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat); |
1086 |
if (err) {
|
1087 |
goto out;
|
1088 |
} |
1089 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat); |
1090 |
err = vs->offset; |
1091 |
|
1092 |
out:
|
1093 |
complete_pdu(s, vs->pdu, err); |
1094 |
v9fs_stat_free(&vs->v9stat); |
1095 |
qemu_free(vs); |
1096 |
} |
1097 |
|
1098 |
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu) |
1099 |
{ |
1100 |
int32_t fid; |
1101 |
V9fsStatState *vs; |
1102 |
ssize_t err = 0;
|
1103 |
|
1104 |
vs = qemu_malloc(sizeof(*vs));
|
1105 |
vs->pdu = pdu; |
1106 |
vs->offset = 7;
|
1107 |
|
1108 |
memset(&vs->v9stat, 0, sizeof(vs->v9stat)); |
1109 |
|
1110 |
pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
|
1111 |
|
1112 |
vs->fidp = lookup_fid(s, fid); |
1113 |
if (vs->fidp == NULL) { |
1114 |
err = -ENOENT; |
1115 |
goto out;
|
1116 |
} |
1117 |
|
1118 |
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); |
1119 |
v9fs_stat_post_lstat(s, vs, err); |
1120 |
return;
|
1121 |
|
1122 |
out:
|
1123 |
complete_pdu(s, vs->pdu, err); |
1124 |
v9fs_stat_free(&vs->v9stat); |
1125 |
qemu_free(vs); |
1126 |
} |
1127 |
|
1128 |
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err) |
1129 |
{ |
1130 |
complete_pdu(s, vs->pdu, err); |
1131 |
|
1132 |
if (vs->nwnames) {
|
1133 |
for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) { |
1134 |
v9fs_string_free(&vs->wnames[vs->name_idx]); |
1135 |
} |
1136 |
|
1137 |
qemu_free(vs->wnames); |
1138 |
qemu_free(vs->qids); |
1139 |
} |
1140 |
} |
1141 |
|
1142 |
static void v9fs_walk_marshal(V9fsWalkState *vs) |
1143 |
{ |
1144 |
int i;
|
1145 |
vs->offset = 7;
|
1146 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
|
1147 |
|
1148 |
for (i = 0; i < vs->nwnames; i++) { |
1149 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
|
1150 |
} |
1151 |
} |
1152 |
|
1153 |
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs, |
1154 |
int err)
|
1155 |
{ |
1156 |
if (err == -1) { |
1157 |
free_fid(s, vs->newfidp->fid); |
1158 |
v9fs_string_free(&vs->path); |
1159 |
err = -ENOENT; |
1160 |
goto out;
|
1161 |
} |
1162 |
|
1163 |
stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]); |
1164 |
|
1165 |
vs->name_idx++; |
1166 |
if (vs->name_idx < vs->nwnames) {
|
1167 |
v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
|
1168 |
vs->wnames[vs->name_idx].data); |
1169 |
v9fs_string_copy(&vs->newfidp->path, &vs->path); |
1170 |
|
1171 |
err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf); |
1172 |
v9fs_walk_post_newfid_lstat(s, vs, err); |
1173 |
return;
|
1174 |
} |
1175 |
|
1176 |
v9fs_string_free(&vs->path); |
1177 |
v9fs_walk_marshal(vs); |
1178 |
err = vs->offset; |
1179 |
out:
|
1180 |
v9fs_walk_complete(s, vs, err); |
1181 |
} |
1182 |
|
1183 |
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs, |
1184 |
int err)
|
1185 |
{ |
1186 |
if (err == -1) { |
1187 |
v9fs_string_free(&vs->path); |
1188 |
err = -ENOENT; |
1189 |
goto out;
|
1190 |
} |
1191 |
|
1192 |
stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]); |
1193 |
vs->name_idx++; |
1194 |
if (vs->name_idx < vs->nwnames) {
|
1195 |
|
1196 |
v9fs_string_sprintf(&vs->path, "%s/%s",
|
1197 |
vs->fidp->path.data, vs->wnames[vs->name_idx].data); |
1198 |
v9fs_string_copy(&vs->fidp->path, &vs->path); |
1199 |
|
1200 |
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); |
1201 |
v9fs_walk_post_oldfid_lstat(s, vs, err); |
1202 |
return;
|
1203 |
} |
1204 |
|
1205 |
v9fs_string_free(&vs->path); |
1206 |
v9fs_walk_marshal(vs); |
1207 |
err = vs->offset; |
1208 |
out:
|
1209 |
v9fs_walk_complete(s, vs, err); |
1210 |
} |
1211 |
|
1212 |
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) |
1213 |
{ |
1214 |
int32_t fid, newfid; |
1215 |
V9fsWalkState *vs; |
1216 |
int err = 0; |
1217 |
int i;
|
1218 |
|
1219 |
vs = qemu_malloc(sizeof(*vs));
|
1220 |
vs->pdu = pdu; |
1221 |
vs->wnames = NULL;
|
1222 |
vs->qids = NULL;
|
1223 |
vs->offset = 7;
|
1224 |
|
1225 |
vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
|
1226 |
&newfid, &vs->nwnames); |
1227 |
|
1228 |
if (vs->nwnames) {
|
1229 |
vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames); |
1230 |
|
1231 |
vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames); |
1232 |
|
1233 |
for (i = 0; i < vs->nwnames; i++) { |
1234 |
vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
|
1235 |
&vs->wnames[i]); |
1236 |
} |
1237 |
} |
1238 |
|
1239 |
vs->fidp = lookup_fid(s, fid); |
1240 |
if (vs->fidp == NULL) { |
1241 |
err = -ENOENT; |
1242 |
goto out;
|
1243 |
} |
1244 |
|
1245 |
/* FIXME: is this really valid? */
|
1246 |
if (fid == newfid) {
|
1247 |
|
1248 |
BUG_ON(vs->fidp->fd != -1);
|
1249 |
BUG_ON(vs->fidp->dir); |
1250 |
v9fs_string_init(&vs->path); |
1251 |
vs->name_idx = 0;
|
1252 |
|
1253 |
if (vs->name_idx < vs->nwnames) {
|
1254 |
v9fs_string_sprintf(&vs->path, "%s/%s",
|
1255 |
vs->fidp->path.data, vs->wnames[vs->name_idx].data); |
1256 |
v9fs_string_copy(&vs->fidp->path, &vs->path); |
1257 |
|
1258 |
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); |
1259 |
v9fs_walk_post_oldfid_lstat(s, vs, err); |
1260 |
return;
|
1261 |
} |
1262 |
} else {
|
1263 |
vs->newfidp = alloc_fid(s, newfid); |
1264 |
if (vs->newfidp == NULL) { |
1265 |
err = -EINVAL; |
1266 |
goto out;
|
1267 |
} |
1268 |
|
1269 |
vs->newfidp->uid = vs->fidp->uid; |
1270 |
v9fs_string_init(&vs->path); |
1271 |
vs->name_idx = 0;
|
1272 |
v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path); |
1273 |
|
1274 |
if (vs->name_idx < vs->nwnames) {
|
1275 |
v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
|
1276 |
vs->wnames[vs->name_idx].data); |
1277 |
v9fs_string_copy(&vs->newfidp->path, &vs->path); |
1278 |
|
1279 |
err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf); |
1280 |
v9fs_walk_post_newfid_lstat(s, vs, err); |
1281 |
return;
|
1282 |
} |
1283 |
} |
1284 |
|
1285 |
v9fs_walk_marshal(vs); |
1286 |
err = vs->offset; |
1287 |
out:
|
1288 |
v9fs_walk_complete(s, vs, err); |
1289 |
} |
1290 |
|
1291 |
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err) |
1292 |
{ |
1293 |
if (vs->fidp->dir == NULL) { |
1294 |
err = -errno; |
1295 |
goto out;
|
1296 |
} |
1297 |
|
1298 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0); |
1299 |
err = vs->offset; |
1300 |
out:
|
1301 |
complete_pdu(s, vs->pdu, err); |
1302 |
qemu_free(vs); |
1303 |
|
1304 |
} |
1305 |
|
1306 |
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err) |
1307 |
{ |
1308 |
if (vs->fidp->fd == -1) { |
1309 |
err = -errno; |
1310 |
goto out;
|
1311 |
} |
1312 |
|
1313 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0); |
1314 |
err = vs->offset; |
1315 |
out:
|
1316 |
complete_pdu(s, vs->pdu, err); |
1317 |
qemu_free(vs); |
1318 |
} |
1319 |
|
1320 |
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err) |
1321 |
{ |
1322 |
if (err) {
|
1323 |
err = -errno; |
1324 |
goto out;
|
1325 |
} |
1326 |
|
1327 |
stat_to_qid(&vs->stbuf, &vs->qid); |
1328 |
|
1329 |
if (S_ISDIR(vs->stbuf.st_mode)) {
|
1330 |
vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path); |
1331 |
v9fs_open_post_opendir(s, vs, err); |
1332 |
} else {
|
1333 |
vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path, |
1334 |
omode_to_uflags(vs->mode)); |
1335 |
v9fs_open_post_open(s, vs, err); |
1336 |
} |
1337 |
return;
|
1338 |
out:
|
1339 |
complete_pdu(s, vs->pdu, err); |
1340 |
qemu_free(vs); |
1341 |
} |
1342 |
|
1343 |
static void v9fs_open(V9fsState *s, V9fsPDU *pdu) |
1344 |
{ |
1345 |
int32_t fid; |
1346 |
V9fsOpenState *vs; |
1347 |
ssize_t err = 0;
|
1348 |
|
1349 |
|
1350 |
vs = qemu_malloc(sizeof(*vs));
|
1351 |
vs->pdu = pdu; |
1352 |
vs->offset = 7;
|
1353 |
|
1354 |
pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
|
1355 |
|
1356 |
vs->fidp = lookup_fid(s, fid); |
1357 |
if (vs->fidp == NULL) { |
1358 |
err = -ENOENT; |
1359 |
goto out;
|
1360 |
} |
1361 |
|
1362 |
BUG_ON(vs->fidp->fd != -1);
|
1363 |
BUG_ON(vs->fidp->dir); |
1364 |
|
1365 |
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); |
1366 |
|
1367 |
v9fs_open_post_lstat(s, vs, err); |
1368 |
return;
|
1369 |
out:
|
1370 |
complete_pdu(s, pdu, err); |
1371 |
qemu_free(vs); |
1372 |
} |
1373 |
|
1374 |
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu) |
1375 |
{ |
1376 |
int32_t fid; |
1377 |
size_t offset = 7;
|
1378 |
int err;
|
1379 |
|
1380 |
pdu_unmarshal(pdu, offset, "d", &fid);
|
1381 |
|
1382 |
err = free_fid(s, fid); |
1383 |
if (err < 0) { |
1384 |
goto out;
|
1385 |
} |
1386 |
|
1387 |
offset = 7;
|
1388 |
err = offset; |
1389 |
out:
|
1390 |
complete_pdu(s, pdu, err); |
1391 |
} |
1392 |
|
1393 |
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t); |
1394 |
|
1395 |
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err) |
1396 |
{ |
1397 |
if (err) {
|
1398 |
goto out;
|
1399 |
} |
1400 |
v9fs_stat_free(&vs->v9stat); |
1401 |
v9fs_string_free(&vs->name); |
1402 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
|
1403 |
vs->offset += vs->count; |
1404 |
err = vs->offset; |
1405 |
out:
|
1406 |
complete_pdu(s, vs->pdu, err); |
1407 |
qemu_free(vs); |
1408 |
return;
|
1409 |
} |
1410 |
|
1411 |
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs, |
1412 |
ssize_t err) |
1413 |
{ |
1414 |
if (err) {
|
1415 |
err = -errno; |
1416 |
goto out;
|
1417 |
} |
1418 |
err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat); |
1419 |
if (err) {
|
1420 |
goto out;
|
1421 |
} |
1422 |
|
1423 |
vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S", |
1424 |
&vs->v9stat); |
1425 |
if ((vs->len != (vs->v9stat.size + 2)) || |
1426 |
((vs->count + vs->len) > vs->max_count)) { |
1427 |
v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos); |
1428 |
v9fs_read_post_seekdir(s, vs, err); |
1429 |
return;
|
1430 |
} |
1431 |
vs->count += vs->len; |
1432 |
v9fs_stat_free(&vs->v9stat); |
1433 |
v9fs_string_free(&vs->name); |
1434 |
vs->dir_pos = vs->dent->d_off; |
1435 |
vs->dent = v9fs_do_readdir(s, vs->fidp->dir); |
1436 |
v9fs_read_post_readdir(s, vs, err); |
1437 |
return;
|
1438 |
out:
|
1439 |
v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos); |
1440 |
v9fs_read_post_seekdir(s, vs, err); |
1441 |
return;
|
1442 |
|
1443 |
} |
1444 |
|
1445 |
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err) |
1446 |
{ |
1447 |
if (vs->dent) {
|
1448 |
memset(&vs->v9stat, 0, sizeof(vs->v9stat)); |
1449 |
v9fs_string_init(&vs->name); |
1450 |
v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
|
1451 |
vs->dent->d_name); |
1452 |
err = v9fs_do_lstat(s, &vs->name, &vs->stbuf); |
1453 |
v9fs_read_post_dir_lstat(s, vs, err); |
1454 |
return;
|
1455 |
} |
1456 |
|
1457 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
|
1458 |
vs->offset += vs->count; |
1459 |
err = vs->offset; |
1460 |
complete_pdu(s, vs->pdu, err); |
1461 |
qemu_free(vs); |
1462 |
return;
|
1463 |
} |
1464 |
|
1465 |
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err) |
1466 |
{ |
1467 |
vs->dent = v9fs_do_readdir(s, vs->fidp->dir); |
1468 |
v9fs_read_post_readdir(s, vs, err); |
1469 |
return;
|
1470 |
} |
1471 |
|
1472 |
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs, |
1473 |
ssize_t err) |
1474 |
{ |
1475 |
vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir); |
1476 |
v9fs_read_post_telldir(s, vs, err); |
1477 |
return;
|
1478 |
} |
1479 |
|
1480 |
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err) |
1481 |
{ |
1482 |
if (err < 0) { |
1483 |
/* IO error return the error */
|
1484 |
err = -errno; |
1485 |
goto out;
|
1486 |
} |
1487 |
vs->total += vs->len; |
1488 |
vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt); |
1489 |
if (vs->total < vs->count && vs->len > 0) { |
1490 |
do {
|
1491 |
if (0) { |
1492 |
print_sg(vs->sg, vs->cnt); |
1493 |
} |
1494 |
vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt); |
1495 |
} while (vs->len == -1 && errno == EINTR); |
1496 |
if (vs->len == -1) { |
1497 |
err = -errno; |
1498 |
} |
1499 |
v9fs_read_post_readv(s, vs, err); |
1500 |
return;
|
1501 |
} |
1502 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
|
1503 |
vs->offset += vs->count; |
1504 |
err = vs->offset; |
1505 |
|
1506 |
out:
|
1507 |
complete_pdu(s, vs->pdu, err); |
1508 |
qemu_free(vs); |
1509 |
} |
1510 |
|
1511 |
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err) |
1512 |
{ |
1513 |
if (err == -1) { |
1514 |
err = -errno; |
1515 |
goto out;
|
1516 |
} |
1517 |
vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt); |
1518 |
|
1519 |
if (vs->total < vs->count) {
|
1520 |
do {
|
1521 |
if (0) { |
1522 |
print_sg(vs->sg, vs->cnt); |
1523 |
} |
1524 |
vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt); |
1525 |
} while (vs->len == -1 && errno == EINTR); |
1526 |
if (vs->len == -1) { |
1527 |
err = -errno; |
1528 |
} |
1529 |
v9fs_read_post_readv(s, vs, err); |
1530 |
return;
|
1531 |
} |
1532 |
out:
|
1533 |
complete_pdu(s, vs->pdu, err); |
1534 |
qemu_free(vs); |
1535 |
} |
1536 |
|
1537 |
static void v9fs_read(V9fsState *s, V9fsPDU *pdu) |
1538 |
{ |
1539 |
int32_t fid; |
1540 |
V9fsReadState *vs; |
1541 |
ssize_t err = 0;
|
1542 |
|
1543 |
vs = qemu_malloc(sizeof(*vs));
|
1544 |
vs->pdu = pdu; |
1545 |
vs->offset = 7;
|
1546 |
vs->total = 0;
|
1547 |
vs->len = 0;
|
1548 |
vs->count = 0;
|
1549 |
|
1550 |
pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
|
1551 |
|
1552 |
vs->fidp = lookup_fid(s, fid); |
1553 |
if (vs->fidp == NULL) { |
1554 |
err = -EINVAL; |
1555 |
goto out;
|
1556 |
} |
1557 |
|
1558 |
if (vs->fidp->dir) {
|
1559 |
vs->max_count = vs->count; |
1560 |
vs->count = 0;
|
1561 |
if (vs->off == 0) { |
1562 |
v9fs_do_rewinddir(s, vs->fidp->dir); |
1563 |
} |
1564 |
v9fs_read_post_rewinddir(s, vs, err); |
1565 |
return;
|
1566 |
} else if (vs->fidp->fd != -1) { |
1567 |
vs->sg = vs->iov; |
1568 |
pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt); |
1569 |
err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET); |
1570 |
v9fs_read_post_lseek(s, vs, err); |
1571 |
return;
|
1572 |
} else {
|
1573 |
err = -EINVAL; |
1574 |
} |
1575 |
out:
|
1576 |
complete_pdu(s, pdu, err); |
1577 |
qemu_free(vs); |
1578 |
} |
1579 |
|
1580 |
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs, |
1581 |
ssize_t err) |
1582 |
{ |
1583 |
if (err < 0) { |
1584 |
/* IO error return the error */
|
1585 |
err = -errno; |
1586 |
goto out;
|
1587 |
} |
1588 |
vs->total += vs->len; |
1589 |
vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt); |
1590 |
if (vs->total < vs->count && vs->len > 0) { |
1591 |
do {
|
1592 |
if (0) { |
1593 |
print_sg(vs->sg, vs->cnt); |
1594 |
} |
1595 |
vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt); |
1596 |
} while (vs->len == -1 && errno == EINTR); |
1597 |
if (vs->len == -1) { |
1598 |
err = -errno; |
1599 |
} |
1600 |
v9fs_write_post_writev(s, vs, err); |
1601 |
return;
|
1602 |
} |
1603 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
|
1604 |
|
1605 |
err = vs->offset; |
1606 |
out:
|
1607 |
complete_pdu(s, vs->pdu, err); |
1608 |
qemu_free(vs); |
1609 |
} |
1610 |
|
1611 |
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err) |
1612 |
{ |
1613 |
if (err == -1) { |
1614 |
err = -errno; |
1615 |
goto out;
|
1616 |
} |
1617 |
vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt); |
1618 |
|
1619 |
if (vs->total < vs->count) {
|
1620 |
do {
|
1621 |
if (0) { |
1622 |
print_sg(vs->sg, vs->cnt); |
1623 |
} |
1624 |
vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt); |
1625 |
} while (vs->len == -1 && errno == EINTR); |
1626 |
if (vs->len == -1) { |
1627 |
err = -errno; |
1628 |
} |
1629 |
v9fs_write_post_writev(s, vs, err); |
1630 |
return;
|
1631 |
} |
1632 |
|
1633 |
out:
|
1634 |
complete_pdu(s, vs->pdu, err); |
1635 |
qemu_free(vs); |
1636 |
} |
1637 |
|
1638 |
static void v9fs_write(V9fsState *s, V9fsPDU *pdu) |
1639 |
{ |
1640 |
int32_t fid; |
1641 |
V9fsWriteState *vs; |
1642 |
ssize_t err; |
1643 |
|
1644 |
vs = qemu_malloc(sizeof(*vs));
|
1645 |
|
1646 |
vs->pdu = pdu; |
1647 |
vs->offset = 7;
|
1648 |
vs->sg = vs->iov; |
1649 |
vs->total = 0;
|
1650 |
vs->len = 0;
|
1651 |
|
1652 |
pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
|
1653 |
vs->sg, &vs->cnt); |
1654 |
|
1655 |
vs->fidp = lookup_fid(s, fid); |
1656 |
if (vs->fidp == NULL) { |
1657 |
err = -EINVAL; |
1658 |
goto out;
|
1659 |
} |
1660 |
|
1661 |
if (vs->fidp->fd == -1) { |
1662 |
err = -EINVAL; |
1663 |
goto out;
|
1664 |
} |
1665 |
|
1666 |
err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET); |
1667 |
|
1668 |
v9fs_write_post_lseek(s, vs, err); |
1669 |
return;
|
1670 |
|
1671 |
out:
|
1672 |
complete_pdu(s, vs->pdu, err); |
1673 |
qemu_free(vs); |
1674 |
} |
1675 |
|
1676 |
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err) |
1677 |
{ |
1678 |
if (err == 0) { |
1679 |
v9fs_string_copy(&vs->fidp->path, &vs->fullname); |
1680 |
stat_to_qid(&vs->stbuf, &vs->qid); |
1681 |
|
1682 |
vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0); |
1683 |
|
1684 |
err = vs->offset; |
1685 |
} |
1686 |
|
1687 |
complete_pdu(s, vs->pdu, err); |
1688 |
v9fs_string_free(&vs->name); |
1689 |
v9fs_string_free(&vs->extension); |
1690 |
v9fs_string_free(&vs->fullname); |
1691 |
qemu_free(vs); |
1692 |
} |
1693 |
|
1694 |
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err) |
1695 |
{ |
1696 |
if (err) {
|
1697 |
err = -errno; |
1698 |
} |
1699 |
v9fs_post_create(s, vs, err); |
1700 |
} |
1701 |
|
1702 |
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs, |
1703 |
int err)
|
1704 |
{ |
1705 |
if (!vs->fidp->dir) {
|
1706 |
err = -errno; |
1707 |
} |
1708 |
v9fs_post_create(s, vs, err); |
1709 |
} |
1710 |
|
1711 |
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs, |
1712 |
int err)
|
1713 |
{ |
1714 |
if (err) {
|
1715 |
err = -errno; |
1716 |
goto out;
|
1717 |
} |
1718 |
|
1719 |
vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname); |
1720 |
v9fs_create_post_opendir(s, vs, err); |
1721 |
return;
|
1722 |
|
1723 |
out:
|
1724 |
v9fs_post_create(s, vs, err); |
1725 |
} |
1726 |
|
1727 |
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err) |
1728 |
{ |
1729 |
if (err) {
|
1730 |
err = -errno; |
1731 |
goto out;
|
1732 |
} |
1733 |
|
1734 |
err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); |
1735 |
v9fs_create_post_dir_lstat(s, vs, err); |
1736 |
return;
|
1737 |
|
1738 |
out:
|
1739 |
v9fs_post_create(s, vs, err); |
1740 |
} |
1741 |
|
1742 |
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err) |
1743 |
{ |
1744 |
if (err) {
|
1745 |
vs->fidp->fd = -1;
|
1746 |
err = -errno; |
1747 |
} |
1748 |
|
1749 |
v9fs_post_create(s, vs, err); |
1750 |
return;
|
1751 |
} |
1752 |
|
1753 |
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err) |
1754 |
{ |
1755 |
if (vs->fidp->fd == -1) { |
1756 |
err = -errno; |
1757 |
goto out;
|
1758 |
} |
1759 |
|
1760 |
err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf); |
1761 |
v9fs_create_post_fstat(s, vs, err); |
1762 |
|
1763 |
return;
|
1764 |
|
1765 |
out:
|
1766 |
v9fs_post_create(s, vs, err); |
1767 |
|
1768 |
} |
1769 |
|
1770 |
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err) |
1771 |
{ |
1772 |
|
1773 |
if (err == 0 || errno != ENOENT) { |
1774 |
err = -errno; |
1775 |
goto out;
|
1776 |
} |
1777 |
|
1778 |
if (vs->perm & P9_STAT_MODE_DIR) {
|
1779 |
err = v9fs_do_mkdir(s, vs); |
1780 |
v9fs_create_post_mkdir(s, vs, err); |
1781 |
} else if (vs->perm & P9_STAT_MODE_SYMLINK) { |
1782 |
err = v9fs_do_symlink(s, vs); |
1783 |
v9fs_create_post_perms(s, vs, err); |
1784 |
} else if (vs->perm & P9_STAT_MODE_LINK) { |
1785 |
int32_t nfid = atoi(vs->extension.data); |
1786 |
V9fsFidState *nfidp = lookup_fid(s, nfid); |
1787 |
if (nfidp == NULL) { |
1788 |
err = -errno; |
1789 |
v9fs_post_create(s, vs, err); |
1790 |
} |
1791 |
err = v9fs_do_link(s, &nfidp->path, &vs->fullname); |
1792 |
v9fs_create_post_perms(s, vs, err); |
1793 |
} else if (vs->perm & P9_STAT_MODE_DEVICE) { |
1794 |
char ctype;
|
1795 |
uint32_t major, minor; |
1796 |
mode_t nmode = 0;
|
1797 |
|
1798 |
if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major, |
1799 |
&minor) != 3) {
|
1800 |
err = -errno; |
1801 |
v9fs_post_create(s, vs, err); |
1802 |
} |
1803 |
|
1804 |
switch (ctype) {
|
1805 |
case 'c': |
1806 |
nmode = S_IFCHR; |
1807 |
break;
|
1808 |
case 'b': |
1809 |
nmode = S_IFBLK; |
1810 |
break;
|
1811 |
default:
|
1812 |
err = -EIO; |
1813 |
v9fs_post_create(s, vs, err); |
1814 |
} |
1815 |
|
1816 |
nmode |= vs->perm & 0777;
|
1817 |
err = v9fs_do_mknod(s, vs, nmode, makedev(major, minor)); |
1818 |
v9fs_create_post_perms(s, vs, err); |
1819 |
} else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) { |
1820 |
err = v9fs_do_mknod(s, vs, S_IFIFO | (vs->perm & 0777), 0); |
1821 |
v9fs_post_create(s, vs, err); |
1822 |
} else if (vs->perm & P9_STAT_MODE_SOCKET) { |
1823 |
err = v9fs_do_mknod(s, vs, S_IFSOCK | (vs->perm & 0777), 0); |
1824 |
v9fs_post_create(s, vs, err); |
1825 |
} else {
|
1826 |
vs->fidp->fd = v9fs_do_open2(s, vs); |
1827 |
v9fs_create_post_open2(s, vs, err); |
1828 |
} |
1829 |
|
1830 |
return;
|
1831 |
|
1832 |
out:
|
1833 |
v9fs_post_create(s, vs, err); |
1834 |
} |
1835 |
|
1836 |
static void v9fs_create(V9fsState *s, V9fsPDU *pdu) |
1837 |
{ |
1838 |
int32_t fid; |
1839 |
V9fsCreateState *vs; |
1840 |
int err = 0; |
1841 |
|
1842 |
vs = qemu_malloc(sizeof(*vs));
|
1843 |
vs->pdu = pdu; |
1844 |
vs->offset = 7;
|
1845 |
|
1846 |
v9fs_string_init(&vs->fullname); |
1847 |
|
1848 |
pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
|
1849 |
&vs->perm, &vs->mode, &vs->extension); |
1850 |
|
1851 |
vs->fidp = lookup_fid(s, fid); |
1852 |
if (vs->fidp == NULL) { |
1853 |
err = -EINVAL; |
1854 |
goto out;
|
1855 |
} |
1856 |
|
1857 |
v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
|
1858 |
vs->name.data); |
1859 |
|
1860 |
err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf); |
1861 |
v9fs_create_post_lstat(s, vs, err); |
1862 |
return;
|
1863 |
|
1864 |
out:
|
1865 |
complete_pdu(s, vs->pdu, err); |
1866 |
v9fs_string_free(&vs->name); |
1867 |
v9fs_string_free(&vs->extension); |
1868 |
qemu_free(vs); |
1869 |
} |
1870 |
|
1871 |
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu) |
1872 |
{ |
1873 |
/* A nop call with no return */
|
1874 |
complete_pdu(s, pdu, 7);
|
1875 |
} |
1876 |
|
1877 |
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs, |
1878 |
int err)
|
1879 |
{ |
1880 |
/* For TREMOVE we need to clunk the fid even on failed remove */
|
1881 |
err = free_fid(s, vs->fidp->fid); |
1882 |
if (err < 0) { |
1883 |
goto out;
|
1884 |
} |
1885 |
|
1886 |
err = vs->offset; |
1887 |
out:
|
1888 |
complete_pdu(s, vs->pdu, err); |
1889 |
qemu_free(vs); |
1890 |
} |
1891 |
|
1892 |
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu) |
1893 |
{ |
1894 |
int32_t fid; |
1895 |
V9fsRemoveState *vs; |
1896 |
int err = 0; |
1897 |
|
1898 |
vs = qemu_malloc(sizeof(*vs));
|
1899 |
vs->pdu = pdu; |
1900 |
vs->offset = 7;
|
1901 |
|
1902 |
pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
|
1903 |
|
1904 |
vs->fidp = lookup_fid(s, fid); |
1905 |
if (vs->fidp == NULL) { |
1906 |
err = -EINVAL; |
1907 |
goto out;
|
1908 |
} |
1909 |
|
1910 |
err = v9fs_do_remove(s, &vs->fidp->path); |
1911 |
v9fs_remove_post_remove(s, vs, err); |
1912 |
return;
|
1913 |
|
1914 |
out:
|
1915 |
complete_pdu(s, pdu, err); |
1916 |
qemu_free(vs); |
1917 |
} |
1918 |
|
1919 |
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err) |
1920 |
{ |
1921 |
if (err < 0) { |
1922 |
goto out;
|
1923 |
} |
1924 |
|
1925 |
err = vs->offset; |
1926 |
|
1927 |
out:
|
1928 |
v9fs_stat_free(&vs->v9stat); |
1929 |
complete_pdu(s, vs->pdu, err); |
1930 |
qemu_free(vs); |
1931 |
} |
1932 |
|
1933 |
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err) |
1934 |
{ |
1935 |
if (err < 0) { |
1936 |
goto out;
|
1937 |
} |
1938 |
|
1939 |
if (vs->v9stat.name.size != 0) { |
1940 |
v9fs_string_free(&vs->nname); |
1941 |
} |
1942 |
|
1943 |
if (vs->v9stat.length != -1) { |
1944 |
if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) { |
1945 |
err = -errno; |
1946 |
} |
1947 |
} |
1948 |
v9fs_wstat_post_truncate(s, vs, err); |
1949 |
return;
|
1950 |
|
1951 |
out:
|
1952 |
v9fs_stat_free(&vs->v9stat); |
1953 |
complete_pdu(s, vs->pdu, err); |
1954 |
qemu_free(vs); |
1955 |
} |
1956 |
|
1957 |
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err) |
1958 |
{ |
1959 |
V9fsFidState *fidp; |
1960 |
if (err < 0) { |
1961 |
goto out;
|
1962 |
} |
1963 |
|
1964 |
if (vs->v9stat.name.size != 0) { |
1965 |
char *old_name, *new_name;
|
1966 |
char *end;
|
1967 |
|
1968 |
old_name = vs->fidp->path.data; |
1969 |
end = strrchr(old_name, '/');
|
1970 |
if (end) {
|
1971 |
end++; |
1972 |
} else {
|
1973 |
end = old_name; |
1974 |
} |
1975 |
|
1976 |
new_name = qemu_mallocz(end - old_name + vs->v9stat.name.size + 1);
|
1977 |
|
1978 |
memcpy(new_name, old_name, end - old_name); |
1979 |
memcpy(new_name + (end - old_name), vs->v9stat.name.data, |
1980 |
vs->v9stat.name.size); |
1981 |
vs->nname.data = new_name; |
1982 |
vs->nname.size = strlen(new_name); |
1983 |
|
1984 |
if (strcmp(new_name, vs->fidp->path.data) != 0) { |
1985 |
if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
|
1986 |
err = -errno; |
1987 |
} else {
|
1988 |
/*
|
1989 |
* Fixup fid's pointing to the old name to
|
1990 |
* start pointing to the new name
|
1991 |
*/
|
1992 |
for (fidp = s->fid_list; fidp; fidp = fidp->next) {
|
1993 |
|
1994 |
if (vs->fidp == fidp) {
|
1995 |
/*
|
1996 |
* we replace name of this fid towards the end
|
1997 |
* so that our below strcmp will work
|
1998 |
*/
|
1999 |
continue;
|
2000 |
} |
2001 |
if (!strncmp(vs->fidp->path.data, fidp->path.data,
|
2002 |
strlen(vs->fidp->path.data))) { |
2003 |
/* replace the name */
|
2004 |
v9fs_fix_path(&fidp->path, &vs->nname, |
2005 |
strlen(vs->fidp->path.data)); |
2006 |
} |
2007 |
} |
2008 |
v9fs_string_copy(&vs->fidp->path, &vs->nname); |
2009 |
} |
2010 |
} |
2011 |
} |
2012 |
v9fs_wstat_post_rename(s, vs, err); |
2013 |
return;
|
2014 |
|
2015 |
out:
|
2016 |
v9fs_stat_free(&vs->v9stat); |
2017 |
complete_pdu(s, vs->pdu, err); |
2018 |
qemu_free(vs); |
2019 |
} |
2020 |
|
2021 |
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err) |
2022 |
{ |
2023 |
if (err < 0) { |
2024 |
goto out;
|
2025 |
} |
2026 |
|
2027 |
if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) { |
2028 |
if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
|
2029 |
vs->v9stat.n_gid)) { |
2030 |
err = -errno; |
2031 |
} |
2032 |
} |
2033 |
v9fs_wstat_post_chown(s, vs, err); |
2034 |
return;
|
2035 |
|
2036 |
out:
|
2037 |
v9fs_stat_free(&vs->v9stat); |
2038 |
complete_pdu(s, vs->pdu, err); |
2039 |
qemu_free(vs); |
2040 |
} |
2041 |
|
2042 |
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err) |
2043 |
{ |
2044 |
if (err < 0) { |
2045 |
goto out;
|
2046 |
} |
2047 |
|
2048 |
if (vs->v9stat.mtime != -1) { |
2049 |
struct utimbuf tb;
|
2050 |
tb.actime = 0;
|
2051 |
tb.modtime = vs->v9stat.mtime; |
2052 |
if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
|
2053 |
err = -errno; |
2054 |
} |
2055 |
} |
2056 |
|
2057 |
v9fs_wstat_post_utime(s, vs, err); |
2058 |
return;
|
2059 |
|
2060 |
out:
|
2061 |
v9fs_stat_free(&vs->v9stat); |
2062 |
complete_pdu(s, vs->pdu, err); |
2063 |
qemu_free(vs); |
2064 |
} |
2065 |
|
2066 |
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err) |
2067 |
{ |
2068 |
if (err == -1) { |
2069 |
err = -errno; |
2070 |
} |
2071 |
v9fs_stat_free(&vs->v9stat); |
2072 |
complete_pdu(s, vs->pdu, err); |
2073 |
qemu_free(vs); |
2074 |
} |
2075 |
|
2076 |
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err) |
2077 |
{ |
2078 |
uint32_t v9_mode; |
2079 |
|
2080 |
if (err == -1) { |
2081 |
err = -errno; |
2082 |
goto out;
|
2083 |
} |
2084 |
|
2085 |
v9_mode = stat_to_v9mode(&vs->stbuf); |
2086 |
|
2087 |
if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
|
2088 |
(v9_mode & P9_STAT_MODE_TYPE_BITS)) { |
2089 |
/* Attempting to change the type */
|
2090 |
err = -EIO; |
2091 |
goto out;
|
2092 |
} |
2093 |
|
2094 |
if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
|
2095 |
&vs->v9stat.extension))) { |
2096 |
err = -errno; |
2097 |
} |
2098 |
v9fs_wstat_post_chmod(s, vs, err); |
2099 |
return;
|
2100 |
|
2101 |
out:
|
2102 |
v9fs_stat_free(&vs->v9stat); |
2103 |
complete_pdu(s, vs->pdu, err); |
2104 |
qemu_free(vs); |
2105 |
} |
2106 |
|
2107 |
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu) |
2108 |
{ |
2109 |
int32_t fid; |
2110 |
V9fsWstatState *vs; |
2111 |
int err = 0; |
2112 |
|
2113 |
vs = qemu_malloc(sizeof(*vs));
|
2114 |
vs->pdu = pdu; |
2115 |
vs->offset = 7;
|
2116 |
|
2117 |
pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
|
2118 |
|
2119 |
vs->fidp = lookup_fid(s, fid); |
2120 |
if (vs->fidp == NULL) { |
2121 |
err = -EINVAL; |
2122 |
goto out;
|
2123 |
} |
2124 |
|
2125 |
/* do we need to sync the file? */
|
2126 |
if (donttouch_stat(&vs->v9stat)) {
|
2127 |
err = v9fs_do_fsync(s, vs->fidp->fd); |
2128 |
v9fs_wstat_post_fsync(s, vs, err); |
2129 |
return;
|
2130 |
} |
2131 |
|
2132 |
if (vs->v9stat.mode != -1) { |
2133 |
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf); |
2134 |
v9fs_wstat_post_lstat(s, vs, err); |
2135 |
return;
|
2136 |
} |
2137 |
|
2138 |
v9fs_wstat_post_chmod(s, vs, err); |
2139 |
return;
|
2140 |
|
2141 |
out:
|
2142 |
v9fs_stat_free(&vs->v9stat); |
2143 |
complete_pdu(s, vs->pdu, err); |
2144 |
qemu_free(vs); |
2145 |
} |
2146 |
|
2147 |
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu); |
2148 |
|
2149 |
static pdu_handler_t *pdu_handlers[] = {
|
2150 |
[P9_TVERSION] = v9fs_version, |
2151 |
[P9_TATTACH] = v9fs_attach, |
2152 |
[P9_TSTAT] = v9fs_stat, |
2153 |
[P9_TWALK] = v9fs_walk, |
2154 |
[P9_TCLUNK] = v9fs_clunk, |
2155 |
[P9_TOPEN] = v9fs_open, |
2156 |
[P9_TREAD] = v9fs_read, |
2157 |
#if 0
|
2158 |
[P9_TAUTH] = v9fs_auth,
|
2159 |
#endif
|
2160 |
[P9_TFLUSH] = v9fs_flush, |
2161 |
[P9_TCREATE] = v9fs_create, |
2162 |
[P9_TWRITE] = v9fs_write, |
2163 |
[P9_TWSTAT] = v9fs_wstat, |
2164 |
[P9_TREMOVE] = v9fs_remove, |
2165 |
}; |
2166 |
|
2167 |
static void submit_pdu(V9fsState *s, V9fsPDU *pdu) |
2168 |
{ |
2169 |
pdu_handler_t *handler; |
2170 |
|
2171 |
if (debug_9p_pdu) {
|
2172 |
pprint_pdu(pdu); |
2173 |
} |
2174 |
|
2175 |
BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers)); |
2176 |
|
2177 |
handler = pdu_handlers[pdu->id]; |
2178 |
BUG_ON(handler == NULL);
|
2179 |
|
2180 |
handler(s, pdu); |
2181 |
} |
2182 |
|
2183 |
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) |
2184 |
{ |
2185 |
V9fsState *s = (V9fsState *)vdev; |
2186 |
V9fsPDU *pdu; |
2187 |
ssize_t len; |
2188 |
|
2189 |
while ((pdu = alloc_pdu(s)) &&
|
2190 |
(len = virtqueue_pop(vq, &pdu->elem)) != 0) {
|
2191 |
uint8_t *ptr; |
2192 |
|
2193 |
BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0); |
2194 |
BUG_ON(pdu->elem.out_sg[0].iov_len < 7); |
2195 |
|
2196 |
ptr = pdu->elem.out_sg[0].iov_base;
|
2197 |
|
2198 |
memcpy(&pdu->size, ptr, 4);
|
2199 |
pdu->id = ptr[4];
|
2200 |
memcpy(&pdu->tag, ptr + 5, 2); |
2201 |
|
2202 |
submit_pdu(s, pdu); |
2203 |
} |
2204 |
|
2205 |
free_pdu(s, pdu); |
2206 |
} |
2207 |
|
2208 |
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
|
2209 |
{ |
2210 |
features |= 1 << VIRTIO_9P_MOUNT_TAG;
|
2211 |
return features;
|
2212 |
} |
2213 |
|
2214 |
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
|
2215 |
{ |
2216 |
return (V9fsState *)vdev;
|
2217 |
} |
2218 |
|
2219 |
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) |
2220 |
{ |
2221 |
struct virtio_9p_config *cfg;
|
2222 |
V9fsState *s = to_virtio_9p(vdev); |
2223 |
|
2224 |
cfg = qemu_mallocz(sizeof(struct virtio_9p_config) + |
2225 |
s->tag_len); |
2226 |
stw_raw(&cfg->tag_len, s->tag_len); |
2227 |
memcpy(cfg->tag, s->tag, s->tag_len); |
2228 |
memcpy(config, cfg, s->config_size); |
2229 |
qemu_free(cfg); |
2230 |
} |
2231 |
|
2232 |
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) |
2233 |
{ |
2234 |
V9fsState *s; |
2235 |
int i, len;
|
2236 |
struct stat stat;
|
2237 |
FsTypeEntry *fse; |
2238 |
|
2239 |
|
2240 |
s = (V9fsState *)virtio_common_init("virtio-9p",
|
2241 |
VIRTIO_ID_9P, |
2242 |
sizeof(struct virtio_9p_config)+ |
2243 |
MAX_TAG_LEN, |
2244 |
sizeof(V9fsState));
|
2245 |
|
2246 |
/* initialize pdu allocator */
|
2247 |
QLIST_INIT(&s->free_list); |
2248 |
for (i = 0; i < (MAX_REQ - 1); i++) { |
2249 |
QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); |
2250 |
} |
2251 |
|
2252 |
s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); |
2253 |
|
2254 |
fse = get_fsdev_fsentry(conf->fsdev_id); |
2255 |
|
2256 |
if (!fse) {
|
2257 |
/* We don't have a fsdev identified by fsdev_id */
|
2258 |
fprintf(stderr, "Virtio-9p device couldn't find fsdev "
|
2259 |
"with the id %s\n", conf->fsdev_id);
|
2260 |
exit(1);
|
2261 |
} |
2262 |
|
2263 |
if (!fse->path || !conf->tag) {
|
2264 |
/* we haven't specified a mount_tag or the path */
|
2265 |
fprintf(stderr, "fsdev with id %s needs path "
|
2266 |
"and Virtio-9p device needs mount_tag arguments\n",
|
2267 |
conf->fsdev_id); |
2268 |
exit(1);
|
2269 |
} |
2270 |
|
2271 |
if (!strcmp(fse->security_model, "passthrough")) { |
2272 |
/* Files on the Fileserver set to client user credentials */
|
2273 |
s->ctx.fs_sm = SM_PASSTHROUGH; |
2274 |
} else if (!strcmp(fse->security_model, "mapped")) { |
2275 |
/* Files on the fileserver are set to QEMU credentials.
|
2276 |
* Client user credentials are saved in extended attributes.
|
2277 |
*/
|
2278 |
s->ctx.fs_sm = SM_MAPPED; |
2279 |
} else {
|
2280 |
/* user haven't specified a correct security option */
|
2281 |
fprintf(stderr, "one of the following must be specified as the"
|
2282 |
"security option:\n\t security_model=passthrough \n\t "
|
2283 |
"security_model=mapped\n");
|
2284 |
return NULL; |
2285 |
} |
2286 |
|
2287 |
if (lstat(fse->path, &stat)) {
|
2288 |
fprintf(stderr, "share path %s does not exist\n", fse->path);
|
2289 |
exit(1);
|
2290 |
} else if (!S_ISDIR(stat.st_mode)) { |
2291 |
fprintf(stderr, "share path %s is not a directory \n", fse->path);
|
2292 |
exit(1);
|
2293 |
} |
2294 |
|
2295 |
s->ctx.fs_root = qemu_strdup(fse->path); |
2296 |
len = strlen(conf->tag); |
2297 |
if (len > MAX_TAG_LEN) {
|
2298 |
len = MAX_TAG_LEN; |
2299 |
} |
2300 |
/* s->tag is non-NULL terminated string */
|
2301 |
s->tag = qemu_malloc(len); |
2302 |
memcpy(s->tag, conf->tag, len); |
2303 |
s->tag_len = len; |
2304 |
s->ctx.uid = -1;
|
2305 |
|
2306 |
s->ops = fse->ops; |
2307 |
s->vdev.get_features = virtio_9p_get_features; |
2308 |
s->config_size = sizeof(struct virtio_9p_config) + |
2309 |
s->tag_len; |
2310 |
s->vdev.get_config = virtio_9p_get_config; |
2311 |
|
2312 |
return &s->vdev;
|
2313 |
} |