]> git.proxmox.com Git - mirror_qemu.git/blob - hw/9pfs/virtio-9p-local.c
hw/9pfs: Add new virtfs option writeout=immediate skip host page cache
[mirror_qemu.git] / hw / 9pfs / virtio-9p-local.c
1 /*
2 * Virtio 9p Posix callback
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14 #include "hw/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <attr/xattr.h>
23
24 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
25 {
26 int err;
27 char buffer[PATH_MAX];
28 char *path = fs_path->data;
29
30 err = lstat(rpath(fs_ctx, path, buffer), stbuf);
31 if (err) {
32 return err;
33 }
34 if (fs_ctx->fs_sm == SM_MAPPED) {
35 /* Actual credentials are part of extended attrs */
36 uid_t tmp_uid;
37 gid_t tmp_gid;
38 mode_t tmp_mode;
39 dev_t tmp_dev;
40 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid,
41 sizeof(uid_t)) > 0) {
42 stbuf->st_uid = tmp_uid;
43 }
44 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid,
45 sizeof(gid_t)) > 0) {
46 stbuf->st_gid = tmp_gid;
47 }
48 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode",
49 &tmp_mode, sizeof(mode_t)) > 0) {
50 stbuf->st_mode = tmp_mode;
51 }
52 if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev,
53 sizeof(dev_t)) > 0) {
54 stbuf->st_rdev = tmp_dev;
55 }
56 }
57 return err;
58 }
59
60 static int local_set_xattr(const char *path, FsCred *credp)
61 {
62 int err;
63
64 if (credp->fc_uid != -1) {
65 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
66 0);
67 if (err) {
68 return err;
69 }
70 }
71 if (credp->fc_gid != -1) {
72 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
73 0);
74 if (err) {
75 return err;
76 }
77 }
78 if (credp->fc_mode != -1) {
79 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
80 sizeof(mode_t), 0);
81 if (err) {
82 return err;
83 }
84 }
85 if (credp->fc_rdev != -1) {
86 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
87 sizeof(dev_t), 0);
88 if (err) {
89 return err;
90 }
91 }
92 return 0;
93 }
94
95 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
96 FsCred *credp)
97 {
98 char buffer[PATH_MAX];
99
100 if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
101 return -1;
102 }
103 if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
104 credp->fc_gid) < 0) {
105 /*
106 * If we fail to change ownership and if we are
107 * using security model none. Ignore the error
108 */
109 if (fs_ctx->fs_sm != SM_NONE) {
110 return -1;
111 }
112 }
113 return 0;
114 }
115
116 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
117 char *buf, size_t bufsz)
118 {
119 ssize_t tsize = -1;
120 char buffer[PATH_MAX];
121 char *path = fs_path->data;
122
123 if (fs_ctx->fs_sm == SM_MAPPED) {
124 int fd;
125 fd = open(rpath(fs_ctx, path, buffer), O_RDONLY);
126 if (fd == -1) {
127 return -1;
128 }
129 do {
130 tsize = read(fd, (void *)buf, bufsz);
131 } while (tsize == -1 && errno == EINTR);
132 close(fd);
133 return tsize;
134 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
135 (fs_ctx->fs_sm == SM_NONE)) {
136 tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
137 }
138 return tsize;
139 }
140
141 static int local_close(FsContext *ctx, int fd)
142 {
143 return close(fd);
144 }
145
146 static int local_closedir(FsContext *ctx, DIR *dir)
147 {
148 return closedir(dir);
149 }
150
151 static int local_open(FsContext *ctx, V9fsPath *fs_path, int flags)
152 {
153 char buffer[PATH_MAX];
154 char *path = fs_path->data;
155
156 return open(rpath(ctx, path, buffer), flags);
157 }
158
159 static DIR *local_opendir(FsContext *ctx, V9fsPath *fs_path)
160 {
161 char buffer[PATH_MAX];
162 char *path = fs_path->data;
163
164 return opendir(rpath(ctx, path, buffer));
165 }
166
167 static void local_rewinddir(FsContext *ctx, DIR *dir)
168 {
169 return rewinddir(dir);
170 }
171
172 static off_t local_telldir(FsContext *ctx, DIR *dir)
173 {
174 return telldir(dir);
175 }
176
177 static int local_readdir_r(FsContext *ctx, DIR *dir, struct dirent *entry,
178 struct dirent **result)
179 {
180 return readdir_r(dir, entry, result);
181 }
182
183 static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
184 {
185 return seekdir(dir, off);
186 }
187
188 static ssize_t local_preadv(FsContext *ctx, int fd, const struct iovec *iov,
189 int iovcnt, off_t offset)
190 {
191 #ifdef CONFIG_PREADV
192 return preadv(fd, iov, iovcnt, offset);
193 #else
194 int err = lseek(fd, offset, SEEK_SET);
195 if (err == -1) {
196 return err;
197 } else {
198 return readv(fd, iov, iovcnt);
199 }
200 #endif
201 }
202
203 static ssize_t local_pwritev(FsContext *ctx, int fd, const struct iovec *iov,
204 int iovcnt, off_t offset)
205 {
206 ssize_t ret
207 ;
208 #ifdef CONFIG_PREADV
209 ret = pwritev(fd, iov, iovcnt, offset);
210 #else
211 int err = lseek(fd, offset, SEEK_SET);
212 if (err == -1) {
213 return err;
214 } else {
215 ret = writev(fd, iov, iovcnt);
216 }
217 #endif
218 #ifdef CONFIG_SYNC_FILE_RANGE
219 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
220 /*
221 * Initiate a writeback. This is not a data integrity sync.
222 * We want to ensure that we don't leave dirty pages in the cache
223 * after write when writeout=immediate is sepcified.
224 */
225 sync_file_range(fd, offset, ret,
226 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
227 }
228 #endif
229 return ret;
230 }
231
232 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
233 {
234 char buffer[PATH_MAX];
235 char *path = fs_path->data;
236
237 if (fs_ctx->fs_sm == SM_MAPPED) {
238 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
239 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
240 (fs_ctx->fs_sm == SM_NONE)) {
241 return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
242 }
243 return -1;
244 }
245
246 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
247 const char *name, FsCred *credp)
248 {
249 char *path;
250 int err = -1;
251 int serrno = 0;
252 V9fsString fullname;
253 char buffer[PATH_MAX];
254
255 v9fs_string_init(&fullname);
256 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
257 path = fullname.data;
258
259 /* Determine the security model */
260 if (fs_ctx->fs_sm == SM_MAPPED) {
261 err = mknod(rpath(fs_ctx, path, buffer),
262 SM_LOCAL_MODE_BITS|S_IFREG, 0);
263 if (err == -1) {
264 goto out;
265 }
266 local_set_xattr(rpath(fs_ctx, path, buffer), credp);
267 if (err == -1) {
268 serrno = errno;
269 goto err_end;
270 }
271 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
272 (fs_ctx->fs_sm == SM_NONE)) {
273 err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
274 credp->fc_rdev);
275 if (err == -1) {
276 goto out;
277 }
278 err = local_post_create_passthrough(fs_ctx, path, credp);
279 if (err == -1) {
280 serrno = errno;
281 goto err_end;
282 }
283 }
284 goto out;
285
286 err_end:
287 remove(rpath(fs_ctx, path, buffer));
288 errno = serrno;
289 out:
290 v9fs_string_free(&fullname);
291 return err;
292 }
293
294 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
295 const char *name, FsCred *credp)
296 {
297 char *path;
298 int err = -1;
299 int serrno = 0;
300 V9fsString fullname;
301 char buffer[PATH_MAX];
302
303 v9fs_string_init(&fullname);
304 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
305 path = fullname.data;
306
307 /* Determine the security model */
308 if (fs_ctx->fs_sm == SM_MAPPED) {
309 err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
310 if (err == -1) {
311 goto out;
312 }
313 credp->fc_mode = credp->fc_mode|S_IFDIR;
314 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
315 if (err == -1) {
316 serrno = errno;
317 goto err_end;
318 }
319 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
320 (fs_ctx->fs_sm == SM_NONE)) {
321 err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
322 if (err == -1) {
323 goto out;
324 }
325 err = local_post_create_passthrough(fs_ctx, path, credp);
326 if (err == -1) {
327 serrno = errno;
328 goto err_end;
329 }
330 }
331 goto out;
332
333 err_end:
334 remove(rpath(fs_ctx, path, buffer));
335 errno = serrno;
336 out:
337 v9fs_string_free(&fullname);
338 return err;
339 }
340
341 static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf)
342 {
343 int err;
344 err = fstat(fd, stbuf);
345 if (err) {
346 return err;
347 }
348 if (fs_ctx->fs_sm == SM_MAPPED) {
349 /* Actual credentials are part of extended attrs */
350 uid_t tmp_uid;
351 gid_t tmp_gid;
352 mode_t tmp_mode;
353 dev_t tmp_dev;
354
355 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
356 stbuf->st_uid = tmp_uid;
357 }
358 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
359 stbuf->st_gid = tmp_gid;
360 }
361 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
362 stbuf->st_mode = tmp_mode;
363 }
364 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
365 stbuf->st_rdev = tmp_dev;
366 }
367 }
368 return err;
369 }
370
371 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
372 int flags, FsCred *credp)
373 {
374 char *path;
375 int fd = -1;
376 int err = -1;
377 int serrno = 0;
378 V9fsString fullname;
379 char buffer[PATH_MAX];
380
381 v9fs_string_init(&fullname);
382 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
383 path = fullname.data;
384
385 /* Determine the security model */
386 if (fs_ctx->fs_sm == SM_MAPPED) {
387 fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
388 if (fd == -1) {
389 err = fd;
390 goto out;
391 }
392 credp->fc_mode = credp->fc_mode|S_IFREG;
393 /* Set cleint credentials in xattr */
394 err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
395 if (err == -1) {
396 serrno = errno;
397 goto err_end;
398 }
399 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
400 (fs_ctx->fs_sm == SM_NONE)) {
401 fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
402 if (fd == -1) {
403 err = fd;
404 goto out;
405 }
406 err = local_post_create_passthrough(fs_ctx, path, credp);
407 if (err == -1) {
408 serrno = errno;
409 goto err_end;
410 }
411 }
412 err = fd;
413 goto out;
414
415 err_end:
416 close(fd);
417 remove(rpath(fs_ctx, path, buffer));
418 errno = serrno;
419 out:
420 v9fs_string_free(&fullname);
421 return err;
422 }
423
424
425 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
426 V9fsPath *dir_path, const char *name, FsCred *credp)
427 {
428 int err = -1;
429 int serrno = 0;
430 char *newpath;
431 V9fsString fullname;
432 char buffer[PATH_MAX];
433
434 v9fs_string_init(&fullname);
435 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
436 newpath = fullname.data;
437
438 /* Determine the security model */
439 if (fs_ctx->fs_sm == SM_MAPPED) {
440 int fd;
441 ssize_t oldpath_size, write_size;
442 fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR,
443 SM_LOCAL_MODE_BITS);
444 if (fd == -1) {
445 err = fd;
446 goto out;
447 }
448 /* Write the oldpath (target) to the file. */
449 oldpath_size = strlen(oldpath);
450 do {
451 write_size = write(fd, (void *)oldpath, oldpath_size);
452 } while (write_size == -1 && errno == EINTR);
453
454 if (write_size != oldpath_size) {
455 serrno = errno;
456 close(fd);
457 err = -1;
458 goto err_end;
459 }
460 close(fd);
461 /* Set cleint credentials in symlink's xattr */
462 credp->fc_mode = credp->fc_mode|S_IFLNK;
463 err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp);
464 if (err == -1) {
465 serrno = errno;
466 goto err_end;
467 }
468 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
469 (fs_ctx->fs_sm == SM_NONE)) {
470 err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
471 if (err) {
472 goto out;
473 }
474 err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
475 credp->fc_gid);
476 if (err == -1) {
477 /*
478 * If we fail to change ownership and if we are
479 * using security model none. Ignore the error
480 */
481 if (fs_ctx->fs_sm != SM_NONE) {
482 serrno = errno;
483 goto err_end;
484 } else
485 err = 0;
486 }
487 }
488 goto out;
489
490 err_end:
491 remove(rpath(fs_ctx, newpath, buffer));
492 errno = serrno;
493 out:
494 v9fs_string_free(&fullname);
495 return err;
496 }
497
498 static int local_link(FsContext *ctx, V9fsPath *oldpath,
499 V9fsPath *dirpath, const char *name)
500 {
501 int ret;
502 V9fsString newpath;
503 char buffer[PATH_MAX], buffer1[PATH_MAX];
504
505 v9fs_string_init(&newpath);
506 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
507
508 ret = link(rpath(ctx, oldpath->data, buffer),
509 rpath(ctx, newpath.data, buffer1));
510 v9fs_string_free(&newpath);
511 return ret;
512 }
513
514 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
515 {
516 char buffer[PATH_MAX];
517 char *path = fs_path->data;
518
519 return truncate(rpath(ctx, path, buffer), size);
520 }
521
522 static int local_rename(FsContext *ctx, const char *oldpath,
523 const char *newpath)
524 {
525 char buffer[PATH_MAX], buffer1[PATH_MAX];
526
527 return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
528 }
529
530 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
531 {
532 char buffer[PATH_MAX];
533 char *path = fs_path->data;
534
535 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
536 (fs_ctx->fs_sm == SM_PASSTHROUGH)) {
537 return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
538 credp->fc_gid);
539 } else if (fs_ctx->fs_sm == SM_MAPPED) {
540 return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
541 } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
542 (fs_ctx->fs_sm == SM_NONE)) {
543 return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
544 credp->fc_gid);
545 }
546 return -1;
547 }
548
549 static int local_utimensat(FsContext *s, V9fsPath *fs_path,
550 const struct timespec *buf)
551 {
552 char buffer[PATH_MAX];
553 char *path = fs_path->data;
554
555 return qemu_utimensat(AT_FDCWD, rpath(s, path, buffer), buf,
556 AT_SYMLINK_NOFOLLOW);
557 }
558
559 static int local_remove(FsContext *ctx, const char *path)
560 {
561 char buffer[PATH_MAX];
562 return remove(rpath(ctx, path, buffer));
563 }
564
565 static int local_fsync(FsContext *ctx, int fd, int datasync)
566 {
567 if (datasync) {
568 return qemu_fdatasync(fd);
569 } else {
570 return fsync(fd);
571 }
572 }
573
574 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
575 {
576 char buffer[PATH_MAX];
577 char *path = fs_path->data;
578
579 return statfs(rpath(s, path, buffer), stbuf);
580 }
581
582 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
583 const char *name, void *value, size_t size)
584 {
585 char *path = fs_path->data;
586
587 return v9fs_get_xattr(ctx, path, name, value, size);
588 }
589
590 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
591 void *value, size_t size)
592 {
593 char *path = fs_path->data;
594
595 return v9fs_list_xattr(ctx, path, value, size);
596 }
597
598 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
599 void *value, size_t size, int flags)
600 {
601 char *path = fs_path->data;
602
603 return v9fs_set_xattr(ctx, path, name, value, size, flags);
604 }
605
606 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
607 const char *name)
608 {
609 char *path = fs_path->data;
610
611 return v9fs_remove_xattr(ctx, path, name);
612 }
613
614 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
615 const char *name, V9fsPath *target)
616 {
617 if (dir_path) {
618 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
619 dir_path->data, name);
620 } else {
621 v9fs_string_sprintf((V9fsString *)target, "%s", name);
622 }
623 /* Bump the size for including terminating NULL */
624 target->size++;
625 return 0;
626 }
627
628 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
629 const char *old_name, V9fsPath *newdir,
630 const char *new_name)
631 {
632 int ret;
633 V9fsString old_full_name, new_full_name;
634
635 v9fs_string_init(&old_full_name);
636 v9fs_string_init(&new_full_name);
637
638 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
639 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
640
641 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
642 v9fs_string_free(&old_full_name);
643 v9fs_string_free(&new_full_name);
644 return ret;
645 }
646
647 static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
648 const char *name, int flags)
649 {
650 int ret;
651 V9fsString fullname;
652 char buffer[PATH_MAX];
653 v9fs_string_init(&fullname);
654
655 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
656 ret = remove(rpath(ctx, fullname.data, buffer));
657 v9fs_string_free(&fullname);
658
659 return ret;
660 }
661
662 static int local_init(FsContext *ctx)
663 {
664 ctx->flags |= PATHNAME_FSCONTEXT;
665 return 0;
666 }
667
668 FileOperations local_ops = {
669 .init = local_init,
670 .lstat = local_lstat,
671 .readlink = local_readlink,
672 .close = local_close,
673 .closedir = local_closedir,
674 .open = local_open,
675 .opendir = local_opendir,
676 .rewinddir = local_rewinddir,
677 .telldir = local_telldir,
678 .readdir_r = local_readdir_r,
679 .seekdir = local_seekdir,
680 .preadv = local_preadv,
681 .pwritev = local_pwritev,
682 .chmod = local_chmod,
683 .mknod = local_mknod,
684 .mkdir = local_mkdir,
685 .fstat = local_fstat,
686 .open2 = local_open2,
687 .symlink = local_symlink,
688 .link = local_link,
689 .truncate = local_truncate,
690 .rename = local_rename,
691 .chown = local_chown,
692 .utimensat = local_utimensat,
693 .remove = local_remove,
694 .fsync = local_fsync,
695 .statfs = local_statfs,
696 .lgetxattr = local_lgetxattr,
697 .llistxattr = local_llistxattr,
698 .lsetxattr = local_lsetxattr,
699 .lremovexattr = local_lremovexattr,
700 .name_to_path = local_name_to_path,
701 .renameat = local_renameat,
702 .unlinkat = local_unlinkat,
703 };