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