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