Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 1 | /* |
Wei Liu | 3b9ca04 | 2015-11-18 18:03:14 +0000 | [diff] [blame] | 2 | * 9p handle callback |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.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 | |
Peter Maydell | fbc0412 | 2016-01-26 18:17:10 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Wei Liu | ebe74f8 | 2016-01-07 18:18:02 +0000 | [diff] [blame] | 15 | #include "9p.h" |
Wei Liu | 267ae09 | 2015-11-18 18:31:52 +0000 | [diff] [blame] | 16 | #include "9p-xattr.h" |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <pwd.h> |
| 19 | #include <grp.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 22 | #include "qemu/xattr.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 23 | #include "qemu/cutils.h" |
Greg Kurz | 63325b1 | 2016-01-22 15:12:17 +0100 | [diff] [blame] | 24 | #include "qemu/error-report.h" |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 25 | #include <linux/fs.h> |
| 26 | #ifdef CONFIG_LINUX_MAGIC_H |
| 27 | #include <linux/magic.h> |
| 28 | #endif |
| 29 | #include <sys/ioctl.h> |
| 30 | |
| 31 | #ifndef XFS_SUPER_MAGIC |
| 32 | #define XFS_SUPER_MAGIC 0x58465342 |
| 33 | #endif |
| 34 | #ifndef EXT2_SUPER_MAGIC |
| 35 | #define EXT2_SUPER_MAGIC 0xEF53 |
| 36 | #endif |
| 37 | #ifndef REISERFS_SUPER_MAGIC |
| 38 | #define REISERFS_SUPER_MAGIC 0x52654973 |
| 39 | #endif |
| 40 | #ifndef BTRFS_SUPER_MAGIC |
| 41 | #define BTRFS_SUPER_MAGIC 0x9123683E |
| 42 | #endif |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 43 | |
| 44 | struct handle_data { |
| 45 | int mountfd; |
| 46 | int handle_bytes; |
| 47 | }; |
| 48 | |
Aneesh Kumar K.V | d204237 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 49 | static inline int name_to_handle(int dirfd, const char *name, |
| 50 | struct file_handle *fh, int *mnt_id, int flags) |
| 51 | { |
| 52 | return name_to_handle_at(dirfd, name, fh, mnt_id, flags); |
| 53 | } |
| 54 | |
| 55 | static inline int open_by_handle(int mountfd, const char *fh, int flags) |
| 56 | { |
| 57 | return open_by_handle_at(mountfd, (struct file_handle *)fh, flags); |
| 58 | } |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 59 | |
| 60 | static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp) |
| 61 | { |
| 62 | int fd, ret; |
Dong Xu Wang | 3a93113 | 2011-11-29 16:52:38 +0800 | [diff] [blame] | 63 | fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 64 | if (fd < 0) { |
| 65 | return fd; |
| 66 | } |
M. Mohan Kumar | 2d40564 | 2012-01-19 12:21:12 +0530 | [diff] [blame] | 67 | ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 68 | if (ret < 0) { |
| 69 | goto err_out; |
| 70 | } |
M. Mohan Kumar | 2d40564 | 2012-01-19 12:21:12 +0530 | [diff] [blame] | 71 | ret = fchmod(fd, credp->fc_mode & 07777); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 72 | err_out: |
| 73 | close(fd); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path, |
| 79 | struct stat *stbuf) |
| 80 | { |
| 81 | int fd, ret; |
| 82 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 83 | |
| 84 | fd = open_by_handle(data->mountfd, fs_path->data, O_PATH); |
| 85 | if (fd < 0) { |
| 86 | return fd; |
| 87 | } |
| 88 | ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH); |
| 89 | close(fd); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path, |
| 94 | char *buf, size_t bufsz) |
| 95 | { |
| 96 | int fd, ret; |
| 97 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 98 | |
| 99 | fd = open_by_handle(data->mountfd, fs_path->data, O_PATH); |
| 100 | if (fd < 0) { |
| 101 | return fd; |
| 102 | } |
| 103 | ret = readlinkat(fd, "", buf, bufsz); |
| 104 | close(fd); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 108 | static int handle_close(FsContext *ctx, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 109 | { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 110 | return close(fs->fd); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 111 | } |
| 112 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 113 | static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 114 | { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 115 | return closedir(fs->dir.stream); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 116 | } |
| 117 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 118 | static int handle_open(FsContext *ctx, V9fsPath *fs_path, |
| 119 | int flags, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 120 | { |
| 121 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 122 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 123 | fs->fd = open_by_handle(data->mountfd, fs_path->data, flags); |
| 124 | return fs->fd; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 125 | } |
| 126 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 127 | static int handle_opendir(FsContext *ctx, |
| 128 | V9fsPath *fs_path, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 129 | { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 130 | int ret; |
| 131 | ret = handle_open(ctx, fs_path, O_DIRECTORY, fs); |
| 132 | if (ret < 0) { |
| 133 | return -1; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 134 | } |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 135 | fs->dir.stream = fdopendir(ret); |
| 136 | if (!fs->dir.stream) { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | return 0; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 140 | } |
| 141 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 142 | static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 143 | { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 144 | rewinddir(fs->dir.stream); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 147 | static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 148 | { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 149 | return telldir(fs->dir.stream); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 152 | static struct dirent *handle_readdir(FsContext *ctx, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 153 | { |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 154 | return readdir(fs->dir.stream); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 157 | static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 158 | { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 159 | seekdir(fs->dir.stream, off); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 160 | } |
| 161 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 162 | static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs, |
| 163 | const struct iovec *iov, |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 164 | int iovcnt, off_t offset) |
| 165 | { |
| 166 | #ifdef CONFIG_PREADV |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 167 | return preadv(fs->fd, iov, iovcnt, offset); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 168 | #else |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 169 | int err = lseek(fs->fd, offset, SEEK_SET); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 170 | if (err == -1) { |
| 171 | return err; |
| 172 | } else { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 173 | return readv(fs->fd, iov, iovcnt); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 174 | } |
| 175 | #endif |
| 176 | } |
| 177 | |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 178 | static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs, |
| 179 | const struct iovec *iov, |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 180 | int iovcnt, off_t offset) |
| 181 | { |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 182 | ssize_t ret; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 183 | #ifdef CONFIG_PREADV |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 184 | ret = pwritev(fs->fd, iov, iovcnt, offset); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 185 | #else |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 186 | int err = lseek(fs->fd, offset, SEEK_SET); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 187 | if (err == -1) { |
| 188 | return err; |
| 189 | } else { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 190 | ret = writev(fs->fd, iov, iovcnt); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 191 | } |
| 192 | #endif |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 193 | #ifdef CONFIG_SYNC_FILE_RANGE |
| 194 | if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { |
| 195 | /* |
| 196 | * Initiate a writeback. This is not a data integrity sync. |
| 197 | * We want to ensure that we don't leave dirty pages in the cache |
| 198 | * after write when writeout=immediate is sepcified. |
| 199 | */ |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 200 | sync_file_range(fs->fd, offset, ret, |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 201 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); |
| 202 | } |
| 203 | #endif |
| 204 | return ret; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
| 208 | { |
| 209 | int fd, ret; |
| 210 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 211 | |
| 212 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 213 | if (fd < 0) { |
| 214 | return fd; |
| 215 | } |
| 216 | ret = fchmod(fd, credp->fc_mode); |
| 217 | close(fd); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path, |
| 222 | const char *name, FsCred *credp) |
| 223 | { |
| 224 | int dirfd, ret; |
| 225 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 226 | |
| 227 | dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH); |
| 228 | if (dirfd < 0) { |
| 229 | return dirfd; |
| 230 | } |
| 231 | ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); |
| 232 | if (!ret) { |
| 233 | ret = handle_update_file_cred(dirfd, name, credp); |
| 234 | } |
| 235 | close(dirfd); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, |
| 240 | const char *name, FsCred *credp) |
| 241 | { |
| 242 | int dirfd, ret; |
| 243 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 244 | |
| 245 | dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH); |
| 246 | if (dirfd < 0) { |
| 247 | return dirfd; |
| 248 | } |
| 249 | ret = mkdirat(dirfd, name, credp->fc_mode); |
| 250 | if (!ret) { |
| 251 | ret = handle_update_file_cred(dirfd, name, credp); |
| 252 | } |
| 253 | close(dirfd); |
| 254 | return ret; |
| 255 | } |
| 256 | |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 257 | static int handle_fstat(FsContext *fs_ctx, int fid_type, |
| 258 | V9fsFidOpenState *fs, struct stat *stbuf) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 259 | { |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 260 | int fd; |
| 261 | |
| 262 | if (fid_type == P9_FID_DIR) { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 263 | fd = dirfd(fs->dir.stream); |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 264 | } else { |
| 265 | fd = fs->fd; |
| 266 | } |
| 267 | return fstat(fd, stbuf); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 271 | int flags, FsCred *credp, V9fsFidOpenState *fs) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 272 | { |
| 273 | int ret; |
| 274 | int dirfd, fd; |
| 275 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 276 | |
| 277 | dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH); |
| 278 | if (dirfd < 0) { |
| 279 | return dirfd; |
| 280 | } |
| 281 | fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode); |
| 282 | if (fd >= 0) { |
| 283 | ret = handle_update_file_cred(dirfd, name, credp); |
| 284 | if (ret < 0) { |
| 285 | close(fd); |
| 286 | fd = ret; |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 287 | } else { |
| 288 | fs->fd = fd; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | close(dirfd); |
| 292 | return fd; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | static int handle_symlink(FsContext *fs_ctx, const char *oldpath, |
| 297 | V9fsPath *dir_path, const char *name, FsCred *credp) |
| 298 | { |
| 299 | int fd, dirfd, ret; |
| 300 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 301 | |
| 302 | dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH); |
| 303 | if (dirfd < 0) { |
| 304 | return dirfd; |
| 305 | } |
| 306 | ret = symlinkat(oldpath, dirfd, name); |
| 307 | if (!ret) { |
| 308 | fd = openat(dirfd, name, O_PATH | O_NOFOLLOW); |
| 309 | if (fd < 0) { |
| 310 | ret = fd; |
| 311 | goto err_out; |
| 312 | } |
| 313 | ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH); |
| 314 | close(fd); |
| 315 | } |
| 316 | err_out: |
| 317 | close(dirfd); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int handle_link(FsContext *ctx, V9fsPath *oldpath, |
| 322 | V9fsPath *dirpath, const char *name) |
| 323 | { |
| 324 | int oldfd, newdirfd, ret; |
| 325 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 326 | |
| 327 | oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH); |
| 328 | if (oldfd < 0) { |
| 329 | return oldfd; |
| 330 | } |
| 331 | newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH); |
| 332 | if (newdirfd < 0) { |
| 333 | close(oldfd); |
| 334 | return newdirfd; |
| 335 | } |
| 336 | ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH); |
| 337 | close(newdirfd); |
| 338 | close(oldfd); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) |
| 343 | { |
| 344 | int fd, ret; |
| 345 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 346 | |
| 347 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY); |
| 348 | if (fd < 0) { |
| 349 | return fd; |
| 350 | } |
| 351 | ret = ftruncate(fd, size); |
| 352 | close(fd); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int handle_rename(FsContext *ctx, const char *oldpath, |
| 357 | const char *newpath) |
| 358 | { |
| 359 | errno = EOPNOTSUPP; |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
| 364 | { |
| 365 | int fd, ret; |
| 366 | struct handle_data *data = (struct handle_data *)fs_ctx->private; |
| 367 | |
| 368 | fd = open_by_handle(data->mountfd, fs_path->data, O_PATH); |
| 369 | if (fd < 0) { |
| 370 | return fd; |
| 371 | } |
| 372 | ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH); |
| 373 | close(fd); |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path, |
| 378 | const struct timespec *buf) |
| 379 | { |
Aneesh Kumar K.V | d204237 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 380 | int ret; |
| 381 | #ifdef CONFIG_UTIMENSAT |
| 382 | int fd; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 383 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 384 | |
| 385 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 386 | if (fd < 0) { |
| 387 | return fd; |
| 388 | } |
| 389 | ret = futimens(fd, buf); |
| 390 | close(fd); |
Aneesh Kumar K.V | d204237 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 391 | #else |
| 392 | ret = -1; |
| 393 | errno = ENOSYS; |
| 394 | #endif |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static int handle_remove(FsContext *ctx, const char *path) |
| 399 | { |
| 400 | errno = EOPNOTSUPP; |
| 401 | return -1; |
| 402 | } |
| 403 | |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 404 | static int handle_fsync(FsContext *ctx, int fid_type, |
| 405 | V9fsFidOpenState *fs, int datasync) |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 406 | { |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 407 | int fd; |
| 408 | |
| 409 | if (fid_type == P9_FID_DIR) { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 410 | fd = dirfd(fs->dir.stream); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 411 | } else { |
Aneesh Kumar K.V | 8b88827 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 412 | fd = fs->fd; |
| 413 | } |
| 414 | |
| 415 | if (datasync) { |
| 416 | return qemu_fdatasync(fd); |
| 417 | } else { |
| 418 | return fsync(fd); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | static int handle_statfs(FsContext *ctx, V9fsPath *fs_path, |
| 423 | struct statfs *stbuf) |
| 424 | { |
| 425 | int fd, ret; |
| 426 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 427 | |
| 428 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 429 | if (fd < 0) { |
| 430 | return fd; |
| 431 | } |
| 432 | ret = fstatfs(fd, stbuf); |
| 433 | close(fd); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path, |
| 438 | const char *name, void *value, size_t size) |
| 439 | { |
| 440 | int fd, ret; |
| 441 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 442 | |
| 443 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 444 | if (fd < 0) { |
| 445 | return fd; |
| 446 | } |
| 447 | ret = fgetxattr(fd, name, value, size); |
| 448 | close(fd); |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path, |
| 453 | void *value, size_t size) |
| 454 | { |
| 455 | int fd, ret; |
| 456 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 457 | |
| 458 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 459 | if (fd < 0) { |
| 460 | return fd; |
| 461 | } |
| 462 | ret = flistxattr(fd, value, size); |
| 463 | close(fd); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, |
| 468 | void *value, size_t size, int flags) |
| 469 | { |
| 470 | int fd, ret; |
| 471 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 472 | |
| 473 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 474 | if (fd < 0) { |
| 475 | return fd; |
| 476 | } |
| 477 | ret = fsetxattr(fd, name, value, size, flags); |
| 478 | close(fd); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path, |
| 483 | const char *name) |
| 484 | { |
| 485 | int fd, ret; |
| 486 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 487 | |
| 488 | fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK); |
| 489 | if (fd < 0) { |
| 490 | return fd; |
| 491 | } |
| 492 | ret = fremovexattr(fd, name); |
| 493 | close(fd); |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path, |
| 498 | const char *name, V9fsPath *target) |
| 499 | { |
Chen Gang | 4fa4ce7 | 2014-03-02 01:36:19 +0800 | [diff] [blame] | 500 | char *buffer; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 501 | struct file_handle *fh; |
| 502 | int dirfd, ret, mnt_id; |
| 503 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 504 | |
| 505 | /* "." and ".." are not allowed */ |
| 506 | if (!strcmp(name, ".") || !strcmp(name, "..")) { |
| 507 | errno = EINVAL; |
| 508 | return -1; |
| 509 | |
| 510 | } |
| 511 | if (dir_path) { |
| 512 | dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH); |
| 513 | } else { |
| 514 | /* relative to export root */ |
Chen Gang | 4fa4ce7 | 2014-03-02 01:36:19 +0800 | [diff] [blame] | 515 | buffer = rpath(ctx, "."); |
| 516 | dirfd = open(buffer, O_DIRECTORY); |
| 517 | g_free(buffer); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 518 | } |
| 519 | if (dirfd < 0) { |
| 520 | return dirfd; |
| 521 | } |
| 522 | fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes); |
| 523 | fh->handle_bytes = data->handle_bytes; |
Dong Xu Wang | 66a0a2c | 2011-11-29 16:52:39 +0800 | [diff] [blame] | 524 | /* add a "./" at the beginning of the path */ |
Chen Gang | 4fa4ce7 | 2014-03-02 01:36:19 +0800 | [diff] [blame] | 525 | buffer = g_strdup_printf("./%s", name); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 526 | /* flag = 0 imply don't follow symlink */ |
| 527 | ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0); |
| 528 | if (!ret) { |
| 529 | target->data = (char *)fh; |
| 530 | target->size = sizeof(struct file_handle) + data->handle_bytes; |
| 531 | } else { |
| 532 | g_free(fh); |
| 533 | } |
| 534 | close(dirfd); |
Chen Gang | 4fa4ce7 | 2014-03-02 01:36:19 +0800 | [diff] [blame] | 535 | g_free(buffer); |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static int handle_renameat(FsContext *ctx, V9fsPath *olddir, |
| 540 | const char *old_name, V9fsPath *newdir, |
| 541 | const char *new_name) |
| 542 | { |
| 543 | int olddirfd, newdirfd, ret; |
| 544 | struct handle_data *data = (struct handle_data *)ctx->private; |
| 545 | |
| 546 | olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH); |
| 547 | if (olddirfd < 0) { |
| 548 | return olddirfd; |
| 549 | } |
| 550 | newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH); |
| 551 | if (newdirfd < 0) { |
| 552 | close(olddirfd); |
| 553 | return newdirfd; |
| 554 | } |
| 555 | ret = renameat(olddirfd, old_name, newdirfd, new_name); |
| 556 | close(newdirfd); |
| 557 | close(olddirfd); |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | static int handle_unlinkat(FsContext *ctx, V9fsPath *dir, |
| 562 | const char *name, int flags) |
| 563 | { |
| 564 | int dirfd, ret; |
| 565 | struct handle_data *data = (struct handle_data *)ctx->private; |
Paolo Bonzini | 930b588 | 2011-11-18 17:35:38 +0100 | [diff] [blame] | 566 | int rflags; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 567 | |
| 568 | dirfd = open_by_handle(data->mountfd, dir->data, O_PATH); |
| 569 | if (dirfd < 0) { |
| 570 | return dirfd; |
| 571 | } |
| 572 | |
Paolo Bonzini | 930b588 | 2011-11-18 17:35:38 +0100 | [diff] [blame] | 573 | rflags = 0; |
| 574 | if (flags & P9_DOTL_AT_REMOVEDIR) { |
| 575 | rflags |= AT_REMOVEDIR; |
| 576 | } |
| 577 | |
| 578 | ret = unlinkat(dirfd, name, rflags); |
| 579 | |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 580 | close(dirfd); |
| 581 | return ret; |
| 582 | } |
| 583 | |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 584 | static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path, |
| 585 | mode_t st_mode, uint64_t *st_gen) |
| 586 | { |
Kirill A. Shutemov | b931766 | 2014-01-28 17:08:25 +0200 | [diff] [blame] | 587 | #ifdef FS_IOC_GETVERSION |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 588 | int err; |
| 589 | V9fsFidOpenState fid_open; |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * Do not try to open special files like device nodes, fifos etc |
| 593 | * We can get fd for regular files and directories only |
| 594 | */ |
| 595 | if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { |
Kirill A. Shutemov | 1a9978a | 2014-01-28 17:08:26 +0200 | [diff] [blame] | 596 | errno = ENOTTY; |
| 597 | return -1; |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 598 | } |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 599 | err = handle_open(ctx, path, O_RDONLY, &fid_open); |
| 600 | if (err < 0) { |
| 601 | return err; |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 602 | } |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 603 | err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); |
| 604 | handle_close(ctx, &fid_open); |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 605 | return err; |
Kirill A. Shutemov | b931766 | 2014-01-28 17:08:25 +0200 | [diff] [blame] | 606 | #else |
| 607 | errno = ENOTTY; |
| 608 | return -1; |
| 609 | #endif |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 610 | } |
| 611 | |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 612 | static int handle_init(FsContext *ctx) |
| 613 | { |
| 614 | int ret, mnt_id; |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 615 | struct statfs stbuf; |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 616 | struct file_handle fh; |
| 617 | struct handle_data *data = g_malloc(sizeof(struct handle_data)); |
Aneesh Kumar K.V | d204237 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 618 | |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 619 | data->mountfd = open(ctx->fs_root, O_DIRECTORY); |
| 620 | if (data->mountfd < 0) { |
| 621 | ret = data->mountfd; |
| 622 | goto err_out; |
| 623 | } |
Harsh Prateek Bora | edb9eb7 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 624 | ret = statfs(ctx->fs_root, &stbuf); |
| 625 | if (!ret) { |
| 626 | switch (stbuf.f_type) { |
| 627 | case EXT2_SUPER_MAGIC: |
| 628 | case BTRFS_SUPER_MAGIC: |
| 629 | case REISERFS_SUPER_MAGIC: |
| 630 | case XFS_SUPER_MAGIC: |
| 631 | ctx->exops.get_st_gen = handle_ioc_getversion; |
| 632 | break; |
| 633 | } |
| 634 | } |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 635 | memset(&fh, 0, sizeof(struct file_handle)); |
| 636 | ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0); |
| 637 | if (ret && errno == EOVERFLOW) { |
| 638 | data->handle_bytes = fh.handle_bytes; |
| 639 | ctx->private = data; |
| 640 | ret = 0; |
| 641 | goto out; |
| 642 | } |
| 643 | /* we got 0 byte handle ? */ |
| 644 | ret = -1; |
| 645 | close(data->mountfd); |
| 646 | err_out: |
| 647 | g_free(data); |
| 648 | out: |
| 649 | return ret; |
| 650 | } |
| 651 | |
Aneesh Kumar K.V | 99519f0 | 2011-12-14 13:48:59 +0530 | [diff] [blame] | 652 | static int handle_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) |
| 653 | { |
| 654 | const char *sec_model = qemu_opt_get(opts, "security_model"); |
| 655 | const char *path = qemu_opt_get(opts, "path"); |
| 656 | |
| 657 | if (sec_model) { |
Greg Kurz | 63325b1 | 2016-01-22 15:12:17 +0100 | [diff] [blame] | 658 | error_report("Invalid argument security_model specified with handle fsdriver"); |
Aneesh Kumar K.V | 99519f0 | 2011-12-14 13:48:59 +0530 | [diff] [blame] | 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | if (!path) { |
Greg Kurz | 63325b1 | 2016-01-22 15:12:17 +0100 | [diff] [blame] | 663 | error_report("fsdev: No path specified"); |
Aneesh Kumar K.V | 99519f0 | 2011-12-14 13:48:59 +0530 | [diff] [blame] | 664 | return -1; |
| 665 | } |
| 666 | fse->path = g_strdup(path); |
| 667 | return 0; |
| 668 | |
| 669 | } |
| 670 | |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 671 | FileOperations handle_ops = { |
Aneesh Kumar K.V | 99519f0 | 2011-12-14 13:48:59 +0530 | [diff] [blame] | 672 | .parse_opts = handle_parse_opts, |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 673 | .init = handle_init, |
| 674 | .lstat = handle_lstat, |
| 675 | .readlink = handle_readlink, |
| 676 | .close = handle_close, |
| 677 | .closedir = handle_closedir, |
| 678 | .open = handle_open, |
| 679 | .opendir = handle_opendir, |
| 680 | .rewinddir = handle_rewinddir, |
| 681 | .telldir = handle_telldir, |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 682 | .readdir = handle_readdir, |
Aneesh Kumar K.V | 5f54222 | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 683 | .seekdir = handle_seekdir, |
| 684 | .preadv = handle_preadv, |
| 685 | .pwritev = handle_pwritev, |
| 686 | .chmod = handle_chmod, |
| 687 | .mknod = handle_mknod, |
| 688 | .mkdir = handle_mkdir, |
| 689 | .fstat = handle_fstat, |
| 690 | .open2 = handle_open2, |
| 691 | .symlink = handle_symlink, |
| 692 | .link = handle_link, |
| 693 | .truncate = handle_truncate, |
| 694 | .rename = handle_rename, |
| 695 | .chown = handle_chown, |
| 696 | .utimensat = handle_utimensat, |
| 697 | .remove = handle_remove, |
| 698 | .fsync = handle_fsync, |
| 699 | .statfs = handle_statfs, |
| 700 | .lgetxattr = handle_lgetxattr, |
| 701 | .llistxattr = handle_llistxattr, |
| 702 | .lsetxattr = handle_lsetxattr, |
| 703 | .lremovexattr = handle_lremovexattr, |
| 704 | .name_to_path = handle_name_to_path, |
| 705 | .renameat = handle_renameat, |
| 706 | .unlinkat = handle_unlinkat, |
| 707 | }; |