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