ppc440_pcix: Change some error_report to qemu_log_mask(LOG_UNIMP, ...)
[qemu.git] / hw / 9pfs / 9p-handle.c
blob4dc0d2bed1c13acd56d3a0beff4c9ad0efa4a548
1 /*
2 * 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 "qemu/osdep.h"
15 #include "9p.h"
16 #include "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 "qemu/cutils.h"
24 #include "qemu/error-report.h"
25 #include "qemu/option.h"
26 #include <linux/fs.h>
27 #ifdef CONFIG_LINUX_MAGIC_H
28 #include <linux/magic.h>
29 #endif
30 #include <sys/ioctl.h>
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
34 #endif
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
37 #endif
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
40 #endif
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
43 #endif
45 typedef struct HandleData {
46 int mountfd;
47 int handle_bytes;
48 } HandleData;
50 static inline int name_to_handle(int dirfd, const char *name,
51 struct file_handle *fh, int *mnt_id, int flags)
53 return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
56 static inline int open_by_handle(int mountfd, const char *fh, int flags)
58 return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
61 static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
63 int fd, ret;
64 fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);
65 if (fd < 0) {
66 return fd;
68 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
69 if (ret < 0) {
70 goto err_out;
72 ret = fchmod(fd, credp->fc_mode & 07777);
73 err_out:
74 close(fd);
75 return ret;
79 static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
80 struct stat *stbuf)
82 int fd, ret;
83 HandleData *data = (HandleData *) fs_ctx->private;
85 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
86 if (fd < 0) {
87 return fd;
89 ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
90 close(fd);
91 return ret;
94 static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
95 char *buf, size_t bufsz)
97 int fd, ret;
98 HandleData *data = (HandleData *) fs_ctx->private;
100 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
101 if (fd < 0) {
102 return fd;
104 ret = readlinkat(fd, "", buf, bufsz);
105 close(fd);
106 return ret;
109 static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
111 return close(fs->fd);
114 static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
116 return closedir(fs->dir.stream);
119 static int handle_open(FsContext *ctx, V9fsPath *fs_path,
120 int flags, V9fsFidOpenState *fs)
122 HandleData *data = (HandleData *) ctx->private;
124 fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
125 return fs->fd;
128 static int handle_opendir(FsContext *ctx,
129 V9fsPath *fs_path, V9fsFidOpenState *fs)
131 int ret;
132 ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
133 if (ret < 0) {
134 return -1;
136 fs->dir.stream = fdopendir(ret);
137 if (!fs->dir.stream) {
138 return -1;
140 return 0;
143 static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
145 rewinddir(fs->dir.stream);
148 static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
150 return telldir(fs->dir.stream);
153 static struct dirent *handle_readdir(FsContext *ctx, V9fsFidOpenState *fs)
155 return readdir(fs->dir.stream);
158 static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
160 seekdir(fs->dir.stream, 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 HandleData *data = (HandleData *) 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 HandleData *data = (HandleData *) 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 HandleData *data = (HandleData *) 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, int fid_type,
259 V9fsFidOpenState *fs, struct stat *stbuf)
261 int fd;
263 if (fid_type == P9_FID_DIR) {
264 fd = dirfd(fs->dir.stream);
265 } else {
266 fd = fs->fd;
268 return fstat(fd, stbuf);
271 static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
272 int flags, FsCred *credp, V9fsFidOpenState *fs)
274 int ret;
275 int dirfd, fd;
276 HandleData *data = (HandleData *) fs_ctx->private;
278 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
279 if (dirfd < 0) {
280 return dirfd;
282 fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
283 if (fd >= 0) {
284 ret = handle_update_file_cred(dirfd, name, credp);
285 if (ret < 0) {
286 close(fd);
287 fd = ret;
288 } else {
289 fs->fd = fd;
292 close(dirfd);
293 return fd;
297 static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
298 V9fsPath *dir_path, const char *name, FsCred *credp)
300 int fd, dirfd, ret;
301 HandleData *data = (HandleData *) fs_ctx->private;
303 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
304 if (dirfd < 0) {
305 return dirfd;
307 ret = symlinkat(oldpath, dirfd, name);
308 if (!ret) {
309 fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
310 if (fd < 0) {
311 ret = fd;
312 goto err_out;
314 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
315 close(fd);
317 err_out:
318 close(dirfd);
319 return ret;
322 static int handle_link(FsContext *ctx, V9fsPath *oldpath,
323 V9fsPath *dirpath, const char *name)
325 int oldfd, newdirfd, ret;
326 HandleData *data = (HandleData *) ctx->private;
328 oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
329 if (oldfd < 0) {
330 return oldfd;
332 newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
333 if (newdirfd < 0) {
334 close(oldfd);
335 return newdirfd;
337 ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
338 close(newdirfd);
339 close(oldfd);
340 return ret;
343 static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
345 int fd, ret;
346 HandleData *data = (HandleData *) ctx->private;
348 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
349 if (fd < 0) {
350 return fd;
352 ret = ftruncate(fd, size);
353 close(fd);
354 return ret;
357 static int handle_rename(FsContext *ctx, const char *oldpath,
358 const char *newpath)
360 errno = EOPNOTSUPP;
361 return -1;
364 static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
366 int fd, ret;
367 HandleData *data = (HandleData *) fs_ctx->private;
369 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
370 if (fd < 0) {
371 return fd;
373 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
374 close(fd);
375 return ret;
378 static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
379 const struct timespec *buf)
381 int ret;
382 int fd;
383 HandleData *data = (HandleData *) ctx->private;
385 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
386 if (fd < 0) {
387 return fd;
389 ret = futimens(fd, buf);
390 close(fd);
391 return ret;
394 static int handle_remove(FsContext *ctx, const char *path)
396 errno = EOPNOTSUPP;
397 return -1;
400 static int handle_fsync(FsContext *ctx, int fid_type,
401 V9fsFidOpenState *fs, int datasync)
403 int fd;
405 if (fid_type == P9_FID_DIR) {
406 fd = dirfd(fs->dir.stream);
407 } else {
408 fd = fs->fd;
411 if (datasync) {
412 return qemu_fdatasync(fd);
413 } else {
414 return fsync(fd);
418 static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
419 struct statfs *stbuf)
421 int fd, ret;
422 HandleData *data = (HandleData *) ctx->private;
424 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
425 if (fd < 0) {
426 return fd;
428 ret = fstatfs(fd, stbuf);
429 close(fd);
430 return ret;
433 static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
434 const char *name, void *value, size_t size)
436 int fd, ret;
437 HandleData *data = (HandleData *) ctx->private;
439 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
440 if (fd < 0) {
441 return fd;
443 ret = fgetxattr(fd, name, value, size);
444 close(fd);
445 return ret;
448 static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
449 void *value, size_t size)
451 int fd, ret;
452 HandleData *data = (HandleData *) ctx->private;
454 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
455 if (fd < 0) {
456 return fd;
458 ret = flistxattr(fd, value, size);
459 close(fd);
460 return ret;
463 static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
464 void *value, size_t size, int flags)
466 int fd, ret;
467 HandleData *data = (HandleData *) ctx->private;
469 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
470 if (fd < 0) {
471 return fd;
473 ret = fsetxattr(fd, name, value, size, flags);
474 close(fd);
475 return ret;
478 static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
479 const char *name)
481 int fd, ret;
482 HandleData *data = (HandleData *) ctx->private;
484 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
485 if (fd < 0) {
486 return fd;
488 ret = fremovexattr(fd, name);
489 close(fd);
490 return ret;
493 static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
494 const char *name, V9fsPath *target)
496 char *buffer;
497 struct file_handle *fh;
498 int dirfd, ret, mnt_id;
499 HandleData *data = (HandleData *) ctx->private;
501 /* "." and ".." are not allowed */
502 if (!strcmp(name, ".") || !strcmp(name, "..")) {
503 errno = EINVAL;
504 return -1;
507 if (dir_path) {
508 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
509 } else {
510 /* relative to export root */
511 buffer = rpath(ctx, ".");
512 dirfd = open(buffer, O_DIRECTORY);
513 g_free(buffer);
515 if (dirfd < 0) {
516 return dirfd;
518 fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
519 fh->handle_bytes = data->handle_bytes;
520 /* add a "./" at the beginning of the path */
521 buffer = g_strdup_printf("./%s", name);
522 /* flag = 0 imply don't follow symlink */
523 ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
524 if (!ret) {
525 target->data = (char *)fh;
526 target->size = sizeof(struct file_handle) + data->handle_bytes;
527 } else {
528 g_free(fh);
530 close(dirfd);
531 g_free(buffer);
532 return ret;
535 static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
536 const char *old_name, V9fsPath *newdir,
537 const char *new_name)
539 int olddirfd, newdirfd, ret;
540 HandleData *data = (HandleData *) ctx->private;
542 olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
543 if (olddirfd < 0) {
544 return olddirfd;
546 newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
547 if (newdirfd < 0) {
548 close(olddirfd);
549 return newdirfd;
551 ret = renameat(olddirfd, old_name, newdirfd, new_name);
552 close(newdirfd);
553 close(olddirfd);
554 return ret;
557 static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
558 const char *name, int flags)
560 int dirfd, ret;
561 HandleData *data = (HandleData *) ctx->private;
562 int rflags;
564 dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
565 if (dirfd < 0) {
566 return dirfd;
569 rflags = 0;
570 if (flags & P9_DOTL_AT_REMOVEDIR) {
571 rflags |= AT_REMOVEDIR;
574 ret = unlinkat(dirfd, name, rflags);
576 close(dirfd);
577 return ret;
580 static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
581 mode_t st_mode, uint64_t *st_gen)
583 #ifdef FS_IOC_GETVERSION
584 int err;
585 V9fsFidOpenState fid_open;
588 * Do not try to open special files like device nodes, fifos etc
589 * We can get fd for regular files and directories only
591 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
592 errno = ENOTTY;
593 return -1;
595 err = handle_open(ctx, path, O_RDONLY, &fid_open);
596 if (err < 0) {
597 return err;
599 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
600 handle_close(ctx, &fid_open);
601 return err;
602 #else
603 errno = ENOTTY;
604 return -1;
605 #endif
608 static int handle_init(FsContext *ctx, Error **errp)
610 int ret, mnt_id;
611 struct statfs stbuf;
612 struct file_handle fh;
613 HandleData *data = g_malloc(sizeof(HandleData));
615 data->mountfd = open(ctx->fs_root, O_DIRECTORY);
616 if (data->mountfd < 0) {
617 ret = data->mountfd;
618 goto err_out;
620 ret = statfs(ctx->fs_root, &stbuf);
621 if (!ret) {
622 switch (stbuf.f_type) {
623 case EXT2_SUPER_MAGIC:
624 case BTRFS_SUPER_MAGIC:
625 case REISERFS_SUPER_MAGIC:
626 case XFS_SUPER_MAGIC:
627 ctx->exops.get_st_gen = handle_ioc_getversion;
628 break;
631 memset(&fh, 0, sizeof(struct file_handle));
632 ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
633 if (ret && errno == EOVERFLOW) {
634 data->handle_bytes = fh.handle_bytes;
635 ctx->private = data;
636 ret = 0;
637 goto out;
639 /* we got 0 byte handle ? */
640 ret = -1;
641 close(data->mountfd);
642 err_out:
643 g_free(data);
644 out:
645 return ret;
648 static void handle_cleanup(FsContext *ctx)
650 HandleData *data = ctx->private;
652 close(data->mountfd);
653 g_free(data);
656 static int handle_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
658 const char *sec_model = qemu_opt_get(opts, "security_model");
659 const char *path = qemu_opt_get(opts, "path");
661 warn_report("handle backend is deprecated");
663 if (sec_model) {
664 error_report("Invalid argument security_model specified with handle fsdriver");
665 return -1;
668 if (!path) {
669 error_report("fsdev: No path specified");
670 return -1;
672 fse->path = g_strdup(path);
673 return 0;
677 FileOperations handle_ops = {
678 .parse_opts = handle_parse_opts,
679 .init = handle_init,
680 .cleanup = handle_cleanup,
681 .lstat = handle_lstat,
682 .readlink = handle_readlink,
683 .close = handle_close,
684 .closedir = handle_closedir,
685 .open = handle_open,
686 .opendir = handle_opendir,
687 .rewinddir = handle_rewinddir,
688 .telldir = handle_telldir,
689 .readdir = handle_readdir,
690 .seekdir = handle_seekdir,
691 .preadv = handle_preadv,
692 .pwritev = handle_pwritev,
693 .chmod = handle_chmod,
694 .mknod = handle_mknod,
695 .mkdir = handle_mkdir,
696 .fstat = handle_fstat,
697 .open2 = handle_open2,
698 .symlink = handle_symlink,
699 .link = handle_link,
700 .truncate = handle_truncate,
701 .rename = handle_rename,
702 .chown = handle_chown,
703 .utimensat = handle_utimensat,
704 .remove = handle_remove,
705 .fsync = handle_fsync,
706 .statfs = handle_statfs,
707 .lgetxattr = handle_lgetxattr,
708 .llistxattr = handle_llistxattr,
709 .lsetxattr = handle_lsetxattr,
710 .lremovexattr = handle_lremovexattr,
711 .name_to_path = handle_name_to_path,
712 .renameat = handle_renameat,
713 .unlinkat = handle_unlinkat,