2
* Virtio 9p handle callback
4
* Copyright IBM, Corp. 2011
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>
20
#include <sys/socket.h>
22
#include "qemu-xattr.h"
25
#ifdef CONFIG_LINUX_MAGIC_H
26
#include <linux/magic.h>
28
#include <sys/ioctl.h>
30
#ifndef XFS_SUPER_MAGIC
31
#define XFS_SUPER_MAGIC 0x58465342
33
#ifndef EXT2_SUPER_MAGIC
34
#define EXT2_SUPER_MAGIC 0xEF53
36
#ifndef REISERFS_SUPER_MAGIC
37
#define REISERFS_SUPER_MAGIC 0x52654973
39
#ifndef BTRFS_SUPER_MAGIC
40
#define BTRFS_SUPER_MAGIC 0x9123683E
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);
61
struct rpl_file_handle {
62
unsigned int handle_bytes;
64
unsigned char handle[0];
66
#define file_handle rpl_file_handle
69
#define AT_REMOVEDIR 0x200
72
#define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname */
75
#define O_PATH 010000000
78
static inline int name_to_handle(int dirfd, const char *name,
79
struct file_handle *fh, int *mnt_id, int flags)
85
static inline int open_by_handle(int mountfd, const char *fh, int flags)
92
static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
95
fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);;
99
ret = fchmod(fd, credp->fc_mode & 07777);
103
ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
110
static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
114
struct handle_data *data = (struct handle_data *)fs_ctx->private;
116
fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
120
ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
125
static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
126
char *buf, size_t bufsz)
129
struct handle_data *data = (struct handle_data *)fs_ctx->private;
131
fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
135
ret = readlinkat(fd, "", buf, bufsz);
140
static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
142
return close(fs->fd);
145
static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
147
return closedir(fs->dir);
150
static int handle_open(FsContext *ctx, V9fsPath *fs_path,
151
int flags, V9fsFidOpenState *fs)
153
struct handle_data *data = (struct handle_data *)ctx->private;
155
fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
159
static int handle_opendir(FsContext *ctx,
160
V9fsPath *fs_path, V9fsFidOpenState *fs)
163
ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
167
fs->dir = fdopendir(ret);
174
static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
176
return rewinddir(fs->dir);
179
static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
181
return telldir(fs->dir);
184
static int handle_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
185
struct dirent *entry,
186
struct dirent **result)
188
return readdir_r(fs->dir, entry, result);
191
static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
193
return seekdir(fs->dir, off);
196
static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
197
const struct iovec *iov,
198
int iovcnt, off_t offset)
201
return preadv(fs->fd, iov, iovcnt, offset);
203
int err = lseek(fs->fd, offset, SEEK_SET);
207
return readv(fs->fd, iov, iovcnt);
212
static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
213
const struct iovec *iov,
214
int iovcnt, off_t offset)
218
ret = pwritev(fs->fd, iov, iovcnt, offset);
220
int err = lseek(fs->fd, offset, SEEK_SET);
224
ret = writev(fs->fd, iov, iovcnt);
227
#ifdef CONFIG_SYNC_FILE_RANGE
228
if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
230
* Initiate a writeback. This is not a data integrity sync.
231
* We want to ensure that we don't leave dirty pages in the cache
232
* after write when writeout=immediate is sepcified.
234
sync_file_range(fs->fd, offset, ret,
235
SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
241
static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
244
struct handle_data *data = (struct handle_data *)fs_ctx->private;
246
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
250
ret = fchmod(fd, credp->fc_mode);
255
static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
256
const char *name, FsCred *credp)
259
struct handle_data *data = (struct handle_data *)fs_ctx->private;
261
dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
265
ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
267
ret = handle_update_file_cred(dirfd, name, credp);
273
static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
274
const char *name, FsCred *credp)
277
struct handle_data *data = (struct handle_data *)fs_ctx->private;
279
dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
283
ret = mkdirat(dirfd, name, credp->fc_mode);
285
ret = handle_update_file_cred(dirfd, name, credp);
291
static int handle_fstat(FsContext *fs_ctx, V9fsFidOpenState *fs,
294
return fstat(fs->fd, stbuf);
297
static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
298
int flags, FsCred *credp, V9fsFidOpenState *fs)
302
struct handle_data *data = (struct handle_data *)fs_ctx->private;
304
dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
308
fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
310
ret = handle_update_file_cred(dirfd, name, credp);
323
static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
324
V9fsPath *dir_path, const char *name, FsCred *credp)
327
struct handle_data *data = (struct handle_data *)fs_ctx->private;
329
dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
333
ret = symlinkat(oldpath, dirfd, name);
335
fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
340
ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
348
static int handle_link(FsContext *ctx, V9fsPath *oldpath,
349
V9fsPath *dirpath, const char *name)
351
int oldfd, newdirfd, ret;
352
struct handle_data *data = (struct handle_data *)ctx->private;
354
oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
358
newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
363
ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
369
static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
372
struct handle_data *data = (struct handle_data *)ctx->private;
374
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
378
ret = ftruncate(fd, size);
383
static int handle_rename(FsContext *ctx, const char *oldpath,
390
static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
393
struct handle_data *data = (struct handle_data *)fs_ctx->private;
395
fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
399
ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
404
static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
405
const struct timespec *buf)
408
#ifdef CONFIG_UTIMENSAT
410
struct handle_data *data = (struct handle_data *)ctx->private;
412
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
416
ret = futimens(fd, buf);
425
static int handle_remove(FsContext *ctx, const char *path)
431
static int handle_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
434
return qemu_fdatasync(fs->fd);
436
return fsync(fs->fd);
440
static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
441
struct statfs *stbuf)
444
struct handle_data *data = (struct handle_data *)ctx->private;
446
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
450
ret = fstatfs(fd, stbuf);
455
static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
456
const char *name, void *value, size_t size)
459
struct handle_data *data = (struct handle_data *)ctx->private;
461
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
465
ret = fgetxattr(fd, name, value, size);
470
static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
471
void *value, size_t size)
474
struct handle_data *data = (struct handle_data *)ctx->private;
476
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
480
ret = flistxattr(fd, value, size);
485
static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
486
void *value, size_t size, int flags)
489
struct handle_data *data = (struct handle_data *)ctx->private;
491
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
495
ret = fsetxattr(fd, name, value, size, flags);
500
static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
504
struct handle_data *data = (struct handle_data *)ctx->private;
506
fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
510
ret = fremovexattr(fd, name);
515
static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
516
const char *name, V9fsPath *target)
518
char buffer[PATH_MAX];
519
struct file_handle *fh;
520
int dirfd, ret, mnt_id;
521
struct handle_data *data = (struct handle_data *)ctx->private;
523
/* "." and ".." are not allowed */
524
if (!strcmp(name, ".") || !strcmp(name, "..")) {
530
dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
532
/* relative to export root */
533
dirfd = open(rpath(ctx, ".", buffer), O_DIRECTORY);
538
fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
539
fh->handle_bytes = data->handle_bytes;
540
/* add a "./" at the begining of the path */
541
snprintf(buffer, PATH_MAX, "./%s", name);
542
/* flag = 0 imply don't follow symlink */
543
ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
545
target->data = (char *)fh;
546
target->size = sizeof(struct file_handle) + data->handle_bytes;
554
static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
555
const char *old_name, V9fsPath *newdir,
556
const char *new_name)
558
int olddirfd, newdirfd, ret;
559
struct handle_data *data = (struct handle_data *)ctx->private;
561
olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
565
newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
570
ret = renameat(olddirfd, old_name, newdirfd, new_name);
576
static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
577
const char *name, int flags)
580
struct handle_data *data = (struct handle_data *)ctx->private;
583
dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
589
if (flags & P9_DOTL_AT_REMOVEDIR) {
590
rflags |= AT_REMOVEDIR;
593
ret = unlinkat(dirfd, name, rflags);
599
static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
600
mode_t st_mode, uint64_t *st_gen)
603
V9fsFidOpenState fid_open;
606
* Do not try to open special files like device nodes, fifos etc
607
* We can get fd for regular files and directories only
609
if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
612
err = handle_open(ctx, path, O_RDONLY, &fid_open);
616
err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
617
handle_close(ctx, &fid_open);
621
static int handle_init(FsContext *ctx)
625
struct file_handle fh;
626
struct handle_data *data = g_malloc(sizeof(struct handle_data));
628
data->mountfd = open(ctx->fs_root, O_DIRECTORY);
629
if (data->mountfd < 0) {
633
ret = statfs(ctx->fs_root, &stbuf);
635
switch (stbuf.f_type) {
636
case EXT2_SUPER_MAGIC:
637
case BTRFS_SUPER_MAGIC:
638
case REISERFS_SUPER_MAGIC:
639
case XFS_SUPER_MAGIC:
640
ctx->exops.get_st_gen = handle_ioc_getversion;
644
memset(&fh, 0, sizeof(struct file_handle));
645
ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
646
if (ret && errno == EOVERFLOW) {
647
data->handle_bytes = fh.handle_bytes;
652
/* we got 0 byte handle ? */
654
close(data->mountfd);
661
FileOperations handle_ops = {
663
.lstat = handle_lstat,
664
.readlink = handle_readlink,
665
.close = handle_close,
666
.closedir = handle_closedir,
668
.opendir = handle_opendir,
669
.rewinddir = handle_rewinddir,
670
.telldir = handle_telldir,
671
.readdir_r = handle_readdir_r,
672
.seekdir = handle_seekdir,
673
.preadv = handle_preadv,
674
.pwritev = handle_pwritev,
675
.chmod = handle_chmod,
676
.mknod = handle_mknod,
677
.mkdir = handle_mkdir,
678
.fstat = handle_fstat,
679
.open2 = handle_open2,
680
.symlink = handle_symlink,
682
.truncate = handle_truncate,
683
.rename = handle_rename,
684
.chown = handle_chown,
685
.utimensat = handle_utimensat,
686
.remove = handle_remove,
687
.fsync = handle_fsync,
688
.statfs = handle_statfs,
689
.lgetxattr = handle_lgetxattr,
690
.llistxattr = handle_llistxattr,
691
.lsetxattr = handle_lsetxattr,
692
.lremovexattr = handle_lremovexattr,
693
.name_to_path = handle_name_to_path,
694
.renameat = handle_renameat,
695
.unlinkat = handle_unlinkat,