hw/9pfs: Improve portability to older systems
[qemu.git] / hw / 9pfs / virtio-9p-handle.c
bloba62f690eed661a6fa1a264f6535f9fc38c797ae3
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 "qemu-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 static inline int name_to_handle(int dirfd, const char *name,
49 struct file_handle *fh, int *mnt_id, int flags)
51 return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
54 static inline int open_by_handle(int mountfd, const char *fh, int flags)
56 return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
59 static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
61 int fd, ret;
62 fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);;
63 if (fd < 0) {
64 return fd;
66 ret = fchmod(fd, credp->fc_mode & 07777);
67 if (ret < 0) {
68 goto err_out;
70 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
71 err_out:
72 close(fd);
73 return ret;
77 static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
78 struct stat *stbuf)
80 int fd, ret;
81 struct handle_data *data = (struct handle_data *)fs_ctx->private;
83 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
84 if (fd < 0) {
85 return fd;
87 ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
88 close(fd);
89 return ret;
92 static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
93 char *buf, size_t bufsz)
95 int fd, ret;
96 struct handle_data *data = (struct handle_data *)fs_ctx->private;
98 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
99 if (fd < 0) {
100 return fd;
102 ret = readlinkat(fd, "", buf, bufsz);
103 close(fd);
104 return ret;
107 static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
109 return close(fs->fd);
112 static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
114 return closedir(fs->dir);
117 static int handle_open(FsContext *ctx, V9fsPath *fs_path,
118 int flags, V9fsFidOpenState *fs)
120 struct handle_data *data = (struct handle_data *)ctx->private;
122 fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
123 return fs->fd;
126 static int handle_opendir(FsContext *ctx,
127 V9fsPath *fs_path, V9fsFidOpenState *fs)
129 int ret;
130 ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
131 if (ret < 0) {
132 return -1;
134 fs->dir = fdopendir(ret);
135 if (!fs->dir) {
136 return -1;
138 return 0;
141 static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
143 return rewinddir(fs->dir);
146 static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
148 return telldir(fs->dir);
151 static int handle_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
152 struct dirent *entry,
153 struct dirent **result)
155 return readdir_r(fs->dir, entry, result);
158 static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
160 return seekdir(fs->dir, off);
163 static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
164 const struct iovec *iov,
165 int iovcnt, off_t offset)
167 #ifdef CONFIG_PREADV
168 return preadv(fs->fd, iov, iovcnt, offset);
169 #else
170 int err = lseek(fs->fd, offset, SEEK_SET);
171 if (err == -1) {
172 return err;
173 } else {
174 return readv(fs->fd, iov, iovcnt);
176 #endif
179 static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
180 const struct iovec *iov,
181 int iovcnt, off_t offset)
183 ssize_t ret;
184 #ifdef CONFIG_PREADV
185 ret = pwritev(fs->fd, iov, iovcnt, offset);
186 #else
187 int err = lseek(fs->fd, offset, SEEK_SET);
188 if (err == -1) {
189 return err;
190 } else {
191 ret = writev(fs->fd, iov, iovcnt);
193 #endif
194 #ifdef CONFIG_SYNC_FILE_RANGE
195 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
197 * Initiate a writeback. This is not a data integrity sync.
198 * We want to ensure that we don't leave dirty pages in the cache
199 * after write when writeout=immediate is sepcified.
201 sync_file_range(fs->fd, offset, ret,
202 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
204 #endif
205 return ret;
208 static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
210 int fd, ret;
211 struct handle_data *data = (struct handle_data *)fs_ctx->private;
213 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
214 if (fd < 0) {
215 return fd;
217 ret = fchmod(fd, credp->fc_mode);
218 close(fd);
219 return ret;
222 static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
223 const char *name, FsCred *credp)
225 int dirfd, ret;
226 struct handle_data *data = (struct handle_data *)fs_ctx->private;
228 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
229 if (dirfd < 0) {
230 return dirfd;
232 ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
233 if (!ret) {
234 ret = handle_update_file_cred(dirfd, name, credp);
236 close(dirfd);
237 return ret;
240 static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
241 const char *name, FsCred *credp)
243 int dirfd, ret;
244 struct handle_data *data = (struct handle_data *)fs_ctx->private;
246 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
247 if (dirfd < 0) {
248 return dirfd;
250 ret = mkdirat(dirfd, name, credp->fc_mode);
251 if (!ret) {
252 ret = handle_update_file_cred(dirfd, name, credp);
254 close(dirfd);
255 return ret;
258 static int handle_fstat(FsContext *fs_ctx, V9fsFidOpenState *fs,
259 struct stat *stbuf)
261 return fstat(fs->fd, stbuf);
264 static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
265 int flags, FsCred *credp, V9fsFidOpenState *fs)
267 int ret;
268 int dirfd, fd;
269 struct handle_data *data = (struct handle_data *)fs_ctx->private;
271 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
272 if (dirfd < 0) {
273 return dirfd;
275 fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
276 if (fd >= 0) {
277 ret = handle_update_file_cred(dirfd, name, credp);
278 if (ret < 0) {
279 close(fd);
280 fd = ret;
281 } else {
282 fs->fd = fd;
285 close(dirfd);
286 return fd;
290 static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
291 V9fsPath *dir_path, const char *name, FsCred *credp)
293 int fd, dirfd, ret;
294 struct handle_data *data = (struct handle_data *)fs_ctx->private;
296 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
297 if (dirfd < 0) {
298 return dirfd;
300 ret = symlinkat(oldpath, dirfd, name);
301 if (!ret) {
302 fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
303 if (fd < 0) {
304 ret = fd;
305 goto err_out;
307 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
308 close(fd);
310 err_out:
311 close(dirfd);
312 return ret;
315 static int handle_link(FsContext *ctx, V9fsPath *oldpath,
316 V9fsPath *dirpath, const char *name)
318 int oldfd, newdirfd, ret;
319 struct handle_data *data = (struct handle_data *)ctx->private;
321 oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
322 if (oldfd < 0) {
323 return oldfd;
325 newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
326 if (newdirfd < 0) {
327 close(oldfd);
328 return newdirfd;
330 ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
331 close(newdirfd);
332 close(oldfd);
333 return ret;
336 static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
338 int fd, ret;
339 struct handle_data *data = (struct handle_data *)ctx->private;
341 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
342 if (fd < 0) {
343 return fd;
345 ret = ftruncate(fd, size);
346 close(fd);
347 return ret;
350 static int handle_rename(FsContext *ctx, const char *oldpath,
351 const char *newpath)
353 errno = EOPNOTSUPP;
354 return -1;
357 static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
359 int fd, ret;
360 struct handle_data *data = (struct handle_data *)fs_ctx->private;
362 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
363 if (fd < 0) {
364 return fd;
366 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
367 close(fd);
368 return ret;
371 static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
372 const struct timespec *buf)
374 int ret;
375 #ifdef CONFIG_UTIMENSAT
376 int fd;
377 struct handle_data *data = (struct handle_data *)ctx->private;
379 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
380 if (fd < 0) {
381 return fd;
383 ret = futimens(fd, buf);
384 close(fd);
385 #else
386 ret = -1;
387 errno = ENOSYS;
388 #endif
389 return ret;
392 static int handle_remove(FsContext *ctx, const char *path)
394 errno = EOPNOTSUPP;
395 return -1;
398 static int handle_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
400 if (datasync) {
401 return qemu_fdatasync(fs->fd);
402 } else {
403 return fsync(fs->fd);
407 static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
408 struct statfs *stbuf)
410 int fd, ret;
411 struct handle_data *data = (struct handle_data *)ctx->private;
413 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
414 if (fd < 0) {
415 return fd;
417 ret = fstatfs(fd, stbuf);
418 close(fd);
419 return ret;
422 static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
423 const char *name, void *value, size_t size)
425 int fd, ret;
426 struct handle_data *data = (struct handle_data *)ctx->private;
428 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
429 if (fd < 0) {
430 return fd;
432 ret = fgetxattr(fd, name, value, size);
433 close(fd);
434 return ret;
437 static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
438 void *value, size_t size)
440 int fd, ret;
441 struct handle_data *data = (struct handle_data *)ctx->private;
443 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
444 if (fd < 0) {
445 return fd;
447 ret = flistxattr(fd, value, size);
448 close(fd);
449 return ret;
452 static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
453 void *value, size_t size, int flags)
455 int fd, ret;
456 struct handle_data *data = (struct handle_data *)ctx->private;
458 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
459 if (fd < 0) {
460 return fd;
462 ret = fsetxattr(fd, name, value, size, flags);
463 close(fd);
464 return ret;
467 static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
468 const char *name)
470 int fd, ret;
471 struct handle_data *data = (struct handle_data *)ctx->private;
473 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
474 if (fd < 0) {
475 return fd;
477 ret = fremovexattr(fd, name);
478 close(fd);
479 return ret;
482 static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
483 const char *name, V9fsPath *target)
485 char buffer[PATH_MAX];
486 struct file_handle *fh;
487 int dirfd, ret, mnt_id;
488 struct handle_data *data = (struct handle_data *)ctx->private;
490 /* "." and ".." are not allowed */
491 if (!strcmp(name, ".") || !strcmp(name, "..")) {
492 errno = EINVAL;
493 return -1;
496 if (dir_path) {
497 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
498 } else {
499 /* relative to export root */
500 dirfd = open(rpath(ctx, ".", buffer), O_DIRECTORY);
502 if (dirfd < 0) {
503 return dirfd;
505 fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
506 fh->handle_bytes = data->handle_bytes;
507 /* add a "./" at the begining of the path */
508 snprintf(buffer, PATH_MAX, "./%s", name);
509 /* flag = 0 imply don't follow symlink */
510 ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
511 if (!ret) {
512 target->data = (char *)fh;
513 target->size = sizeof(struct file_handle) + data->handle_bytes;
514 } else {
515 g_free(fh);
517 close(dirfd);
518 return ret;
521 static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
522 const char *old_name, V9fsPath *newdir,
523 const char *new_name)
525 int olddirfd, newdirfd, ret;
526 struct handle_data *data = (struct handle_data *)ctx->private;
528 olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
529 if (olddirfd < 0) {
530 return olddirfd;
532 newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
533 if (newdirfd < 0) {
534 close(olddirfd);
535 return newdirfd;
537 ret = renameat(olddirfd, old_name, newdirfd, new_name);
538 close(newdirfd);
539 close(olddirfd);
540 return ret;
543 static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
544 const char *name, int flags)
546 int dirfd, ret;
547 struct handle_data *data = (struct handle_data *)ctx->private;
548 int rflags;
550 dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
551 if (dirfd < 0) {
552 return dirfd;
555 rflags = 0;
556 if (flags & P9_DOTL_AT_REMOVEDIR) {
557 rflags |= AT_REMOVEDIR;
560 ret = unlinkat(dirfd, name, rflags);
562 close(dirfd);
563 return ret;
566 static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
567 mode_t st_mode, uint64_t *st_gen)
569 int err;
570 V9fsFidOpenState fid_open;
573 * Do not try to open special files like device nodes, fifos etc
574 * We can get fd for regular files and directories only
576 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
577 return 0;
579 err = handle_open(ctx, path, O_RDONLY, &fid_open);
580 if (err < 0) {
581 return err;
583 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
584 handle_close(ctx, &fid_open);
585 return err;
588 static int handle_init(FsContext *ctx)
590 int ret, mnt_id;
591 struct statfs stbuf;
592 struct file_handle fh;
593 struct handle_data *data = g_malloc(sizeof(struct handle_data));
595 data->mountfd = open(ctx->fs_root, O_DIRECTORY);
596 if (data->mountfd < 0) {
597 ret = data->mountfd;
598 goto err_out;
600 ret = statfs(ctx->fs_root, &stbuf);
601 if (!ret) {
602 switch (stbuf.f_type) {
603 case EXT2_SUPER_MAGIC:
604 case BTRFS_SUPER_MAGIC:
605 case REISERFS_SUPER_MAGIC:
606 case XFS_SUPER_MAGIC:
607 ctx->exops.get_st_gen = handle_ioc_getversion;
608 break;
611 memset(&fh, 0, sizeof(struct file_handle));
612 ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
613 if (ret && errno == EOVERFLOW) {
614 data->handle_bytes = fh.handle_bytes;
615 ctx->private = data;
616 ret = 0;
617 goto out;
619 /* we got 0 byte handle ? */
620 ret = -1;
621 close(data->mountfd);
622 err_out:
623 g_free(data);
624 out:
625 return ret;
628 FileOperations handle_ops = {
629 .init = handle_init,
630 .lstat = handle_lstat,
631 .readlink = handle_readlink,
632 .close = handle_close,
633 .closedir = handle_closedir,
634 .open = handle_open,
635 .opendir = handle_opendir,
636 .rewinddir = handle_rewinddir,
637 .telldir = handle_telldir,
638 .readdir_r = handle_readdir_r,
639 .seekdir = handle_seekdir,
640 .preadv = handle_preadv,
641 .pwritev = handle_pwritev,
642 .chmod = handle_chmod,
643 .mknod = handle_mknod,
644 .mkdir = handle_mkdir,
645 .fstat = handle_fstat,
646 .open2 = handle_open2,
647 .symlink = handle_symlink,
648 .link = handle_link,
649 .truncate = handle_truncate,
650 .rename = handle_rename,
651 .chown = handle_chown,
652 .utimensat = handle_utimensat,
653 .remove = handle_remove,
654 .fsync = handle_fsync,
655 .statfs = handle_statfs,
656 .lgetxattr = handle_lgetxattr,
657 .llistxattr = handle_llistxattr,
658 .lsetxattr = handle_lsetxattr,
659 .lremovexattr = handle_lremovexattr,
660 .name_to_path = handle_name_to_path,
661 .renameat = handle_renameat,
662 .unlinkat = handle_unlinkat,