Merge remote-tracking branch 'spice/spice.v46' into staging
[qemu.git] / hw / 9pfs / virtio-9p-handle.c
blobc38e0e7863d12dc9a9c0c57c86ac36bbfc4836c7
1 /*
2 * Virtio 9p handle callback
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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 #include <unistd.h>
24 #include <linux/fs.h>
25 #ifdef CONFIG_LINUX_MAGIC_H
26 #include <linux/magic.h>
27 #endif
28 #include <sys/ioctl.h>
30 #ifndef XFS_SUPER_MAGIC
31 #define XFS_SUPER_MAGIC 0x58465342
32 #endif
33 #ifndef EXT2_SUPER_MAGIC
34 #define EXT2_SUPER_MAGIC 0xEF53
35 #endif
36 #ifndef REISERFS_SUPER_MAGIC
37 #define REISERFS_SUPER_MAGIC 0x52654973
38 #endif
39 #ifndef BTRFS_SUPER_MAGIC
40 #define BTRFS_SUPER_MAGIC 0x9123683E
41 #endif
43 struct handle_data {
44 int mountfd;
45 int handle_bytes;
48 #ifdef CONFIG_OPEN_BY_HANDLE
49 static inline int name_to_handle(int dirfd, const char *name,
50 struct file_handle *fh, int *mnt_id, int flags)
52 return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
55 static inline int open_by_handle(int mountfd, const char *fh, int flags)
57 return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
59 #else
61 struct file_handle {
62 unsigned int handle_bytes;
63 int handle_type;
64 unsigned char handle[0];
67 #ifndef AT_EMPTY_PATH
68 #define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname */
69 #endif
70 #ifndef O_PATH
71 #define O_PATH 010000000
72 #endif
74 static inline int name_to_handle(int dirfd, const char *name,
75 struct file_handle *fh, int *mnt_id, int flags)
77 errno = ENOSYS;
78 return -1;
81 static inline int open_by_handle(int mountfd, const char *fh, int flags)
83 errno = ENOSYS;
84 return -1;
86 #endif
88 static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
90 int fd, ret;
91 fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);;
92 if (fd < 0) {
93 return fd;
95 ret = fchmod(fd, credp->fc_mode & 07777);
96 if (ret < 0) {
97 goto err_out;
99 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
100 err_out:
101 close(fd);
102 return ret;
106 static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
107 struct stat *stbuf)
109 int fd, ret;
110 struct handle_data *data = (struct handle_data *)fs_ctx->private;
112 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
113 if (fd < 0) {
114 return fd;
116 ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
117 close(fd);
118 return ret;
121 static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
122 char *buf, size_t bufsz)
124 int fd, ret;
125 struct handle_data *data = (struct handle_data *)fs_ctx->private;
127 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
128 if (fd < 0) {
129 return fd;
131 ret = readlinkat(fd, "", buf, bufsz);
132 close(fd);
133 return ret;
136 static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
138 return close(fs->fd);
141 static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
143 return closedir(fs->dir);
146 static int handle_open(FsContext *ctx, V9fsPath *fs_path,
147 int flags, V9fsFidOpenState *fs)
149 struct handle_data *data = (struct handle_data *)ctx->private;
151 fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
152 return fs->fd;
155 static int handle_opendir(FsContext *ctx,
156 V9fsPath *fs_path, V9fsFidOpenState *fs)
158 int ret;
159 ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
160 if (ret < 0) {
161 return -1;
163 fs->dir = fdopendir(ret);
164 if (!fs->dir) {
165 return -1;
167 return 0;
170 static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
172 return rewinddir(fs->dir);
175 static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
177 return telldir(fs->dir);
180 static int handle_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
181 struct dirent *entry,
182 struct dirent **result)
184 return readdir_r(fs->dir, entry, result);
187 static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
189 return seekdir(fs->dir, off);
192 static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
193 const struct iovec *iov,
194 int iovcnt, off_t offset)
196 #ifdef CONFIG_PREADV
197 return preadv(fs->fd, iov, iovcnt, offset);
198 #else
199 int err = lseek(fs->fd, offset, SEEK_SET);
200 if (err == -1) {
201 return err;
202 } else {
203 return readv(fs->fd, iov, iovcnt);
205 #endif
208 static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
209 const struct iovec *iov,
210 int iovcnt, off_t offset)
212 ssize_t ret;
213 #ifdef CONFIG_PREADV
214 ret = pwritev(fs->fd, iov, iovcnt, offset);
215 #else
216 int err = lseek(fs->fd, offset, SEEK_SET);
217 if (err == -1) {
218 return err;
219 } else {
220 ret = writev(fs->fd, iov, iovcnt);
222 #endif
223 #ifdef CONFIG_SYNC_FILE_RANGE
224 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
226 * Initiate a writeback. This is not a data integrity sync.
227 * We want to ensure that we don't leave dirty pages in the cache
228 * after write when writeout=immediate is sepcified.
230 sync_file_range(fs->fd, offset, ret,
231 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
233 #endif
234 return ret;
237 static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
239 int fd, ret;
240 struct handle_data *data = (struct handle_data *)fs_ctx->private;
242 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
243 if (fd < 0) {
244 return fd;
246 ret = fchmod(fd, credp->fc_mode);
247 close(fd);
248 return ret;
251 static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
252 const char *name, FsCred *credp)
254 int dirfd, ret;
255 struct handle_data *data = (struct handle_data *)fs_ctx->private;
257 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
258 if (dirfd < 0) {
259 return dirfd;
261 ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
262 if (!ret) {
263 ret = handle_update_file_cred(dirfd, name, credp);
265 close(dirfd);
266 return ret;
269 static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
270 const char *name, FsCred *credp)
272 int dirfd, ret;
273 struct handle_data *data = (struct handle_data *)fs_ctx->private;
275 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
276 if (dirfd < 0) {
277 return dirfd;
279 ret = mkdirat(dirfd, name, credp->fc_mode);
280 if (!ret) {
281 ret = handle_update_file_cred(dirfd, name, credp);
283 close(dirfd);
284 return ret;
287 static int handle_fstat(FsContext *fs_ctx, V9fsFidOpenState *fs,
288 struct stat *stbuf)
290 return fstat(fs->fd, stbuf);
293 static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
294 int flags, FsCred *credp, V9fsFidOpenState *fs)
296 int ret;
297 int dirfd, fd;
298 struct handle_data *data = (struct handle_data *)fs_ctx->private;
300 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
301 if (dirfd < 0) {
302 return dirfd;
304 fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
305 if (fd >= 0) {
306 ret = handle_update_file_cred(dirfd, name, credp);
307 if (ret < 0) {
308 close(fd);
309 fd = ret;
310 } else {
311 fs->fd = fd;
314 close(dirfd);
315 return fd;
319 static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
320 V9fsPath *dir_path, const char *name, FsCred *credp)
322 int fd, dirfd, ret;
323 struct handle_data *data = (struct handle_data *)fs_ctx->private;
325 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
326 if (dirfd < 0) {
327 return dirfd;
329 ret = symlinkat(oldpath, dirfd, name);
330 if (!ret) {
331 fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
332 if (fd < 0) {
333 ret = fd;
334 goto err_out;
336 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
337 close(fd);
339 err_out:
340 close(dirfd);
341 return ret;
344 static int handle_link(FsContext *ctx, V9fsPath *oldpath,
345 V9fsPath *dirpath, const char *name)
347 int oldfd, newdirfd, ret;
348 struct handle_data *data = (struct handle_data *)ctx->private;
350 oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
351 if (oldfd < 0) {
352 return oldfd;
354 newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
355 if (newdirfd < 0) {
356 close(oldfd);
357 return newdirfd;
359 ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
360 close(newdirfd);
361 close(oldfd);
362 return ret;
365 static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
367 int fd, ret;
368 struct handle_data *data = (struct handle_data *)ctx->private;
370 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
371 if (fd < 0) {
372 return fd;
374 ret = ftruncate(fd, size);
375 close(fd);
376 return ret;
379 static int handle_rename(FsContext *ctx, const char *oldpath,
380 const char *newpath)
382 errno = EOPNOTSUPP;
383 return -1;
386 static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
388 int fd, ret;
389 struct handle_data *data = (struct handle_data *)fs_ctx->private;
391 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
392 if (fd < 0) {
393 return fd;
395 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
396 close(fd);
397 return ret;
400 static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
401 const struct timespec *buf)
403 int ret;
404 #ifdef CONFIG_UTIMENSAT
405 int fd;
406 struct handle_data *data = (struct handle_data *)ctx->private;
408 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
409 if (fd < 0) {
410 return fd;
412 ret = futimens(fd, buf);
413 close(fd);
414 #else
415 ret = -1;
416 errno = ENOSYS;
417 #endif
418 return ret;
421 static int handle_remove(FsContext *ctx, const char *path)
423 errno = EOPNOTSUPP;
424 return -1;
427 static int handle_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
429 if (datasync) {
430 return qemu_fdatasync(fs->fd);
431 } else {
432 return fsync(fs->fd);
436 static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
437 struct statfs *stbuf)
439 int fd, ret;
440 struct handle_data *data = (struct handle_data *)ctx->private;
442 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
443 if (fd < 0) {
444 return fd;
446 ret = fstatfs(fd, stbuf);
447 close(fd);
448 return ret;
451 static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
452 const char *name, void *value, size_t size)
454 int fd, ret;
455 struct handle_data *data = (struct handle_data *)ctx->private;
457 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
458 if (fd < 0) {
459 return fd;
461 ret = fgetxattr(fd, name, value, size);
462 close(fd);
463 return ret;
466 static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
467 void *value, size_t size)
469 int fd, ret;
470 struct handle_data *data = (struct handle_data *)ctx->private;
472 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
473 if (fd < 0) {
474 return fd;
476 ret = flistxattr(fd, value, size);
477 close(fd);
478 return ret;
481 static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
482 void *value, size_t size, int flags)
484 int fd, ret;
485 struct handle_data *data = (struct handle_data *)ctx->private;
487 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
488 if (fd < 0) {
489 return fd;
491 ret = fsetxattr(fd, name, value, size, flags);
492 close(fd);
493 return ret;
496 static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
497 const char *name)
499 int fd, ret;
500 struct handle_data *data = (struct handle_data *)ctx->private;
502 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
503 if (fd < 0) {
504 return fd;
506 ret = fremovexattr(fd, name);
507 close(fd);
508 return ret;
511 static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
512 const char *name, V9fsPath *target)
514 char buffer[PATH_MAX];
515 struct file_handle *fh;
516 int dirfd, ret, mnt_id;
517 struct handle_data *data = (struct handle_data *)ctx->private;
519 /* "." and ".." are not allowed */
520 if (!strcmp(name, ".") || !strcmp(name, "..")) {
521 errno = EINVAL;
522 return -1;
525 if (dir_path) {
526 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
527 } else {
528 /* relative to export root */
529 dirfd = open(rpath(ctx, ".", buffer), O_DIRECTORY);
531 if (dirfd < 0) {
532 return dirfd;
534 fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
535 fh->handle_bytes = data->handle_bytes;
536 /* add a "./" at the begining of the path */
537 snprintf(buffer, PATH_MAX, "./%s", name);
538 /* flag = 0 imply don't follow symlink */
539 ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
540 if (!ret) {
541 target->data = (char *)fh;
542 target->size = sizeof(struct file_handle) + data->handle_bytes;
543 } else {
544 g_free(fh);
546 close(dirfd);
547 return ret;
550 static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
551 const char *old_name, V9fsPath *newdir,
552 const char *new_name)
554 int olddirfd, newdirfd, ret;
555 struct handle_data *data = (struct handle_data *)ctx->private;
557 olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
558 if (olddirfd < 0) {
559 return olddirfd;
561 newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
562 if (newdirfd < 0) {
563 close(olddirfd);
564 return newdirfd;
566 ret = renameat(olddirfd, old_name, newdirfd, new_name);
567 close(newdirfd);
568 close(olddirfd);
569 return ret;
572 static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
573 const char *name, int flags)
575 int dirfd, ret;
576 struct handle_data *data = (struct handle_data *)ctx->private;
578 dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
579 if (dirfd < 0) {
580 return dirfd;
583 ret = unlinkat(dirfd, name, flags);
584 close(dirfd);
585 return ret;
588 static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
589 mode_t st_mode, uint64_t *st_gen)
591 int err;
592 V9fsFidOpenState fid_open;
595 * Do not try to open special files like device nodes, fifos etc
596 * We can get fd for regular files and directories only
598 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
599 return 0;
601 err = handle_open(ctx, path, O_RDONLY, &fid_open);
602 if (err < 0) {
603 return err;
605 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
606 handle_close(ctx, &fid_open);
607 return err;
610 static int handle_init(FsContext *ctx)
612 int ret, mnt_id;
613 struct statfs stbuf;
614 struct file_handle fh;
615 struct handle_data *data = g_malloc(sizeof(struct handle_data));
617 data->mountfd = open(ctx->fs_root, O_DIRECTORY);
618 if (data->mountfd < 0) {
619 ret = data->mountfd;
620 goto err_out;
622 ret = statfs(ctx->fs_root, &stbuf);
623 if (!ret) {
624 switch (stbuf.f_type) {
625 case EXT2_SUPER_MAGIC:
626 case BTRFS_SUPER_MAGIC:
627 case REISERFS_SUPER_MAGIC:
628 case XFS_SUPER_MAGIC:
629 ctx->exops.get_st_gen = handle_ioc_getversion;
630 break;
633 memset(&fh, 0, sizeof(struct file_handle));
634 ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
635 if (ret && errno == EOVERFLOW) {
636 data->handle_bytes = fh.handle_bytes;
637 ctx->private = data;
638 ret = 0;
639 goto out;
641 /* we got 0 byte handle ? */
642 ret = -1;
643 close(data->mountfd);
644 err_out:
645 g_free(data);
646 out:
647 return ret;
650 FileOperations handle_ops = {
651 .init = handle_init,
652 .lstat = handle_lstat,
653 .readlink = handle_readlink,
654 .close = handle_close,
655 .closedir = handle_closedir,
656 .open = handle_open,
657 .opendir = handle_opendir,
658 .rewinddir = handle_rewinddir,
659 .telldir = handle_telldir,
660 .readdir_r = handle_readdir_r,
661 .seekdir = handle_seekdir,
662 .preadv = handle_preadv,
663 .pwritev = handle_pwritev,
664 .chmod = handle_chmod,
665 .mknod = handle_mknod,
666 .mkdir = handle_mkdir,
667 .fstat = handle_fstat,
668 .open2 = handle_open2,
669 .symlink = handle_symlink,
670 .link = handle_link,
671 .truncate = handle_truncate,
672 .rename = handle_rename,
673 .chown = handle_chown,
674 .utimensat = handle_utimensat,
675 .remove = handle_remove,
676 .fsync = handle_fsync,
677 .statfs = handle_statfs,
678 .lgetxattr = handle_lgetxattr,
679 .llistxattr = handle_llistxattr,
680 .lsetxattr = handle_lsetxattr,
681 .lremovexattr = handle_lremovexattr,
682 .name_to_path = handle_name_to_path,
683 .renameat = handle_renameat,
684 .unlinkat = handle_unlinkat,