blob: 4dc0d2bed1c13acd56d3a0beff4c9ad0efa4a548 [file] [log] [blame]
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +05301/*
Wei Liu3b9ca042015-11-18 18:03:14 +00002 * 9p handle callback
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +05303 *
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 Maydellfbc04122016-01-26 18:17:10 +000014#include "qemu/osdep.h"
Wei Liuebe74f82016-01-07 18:18:02 +000015#include "9p.h"
Wei Liu267ae092015-11-18 18:31:52 +000016#include "9p-xattr.h"
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053017#include <arpa/inet.h>
18#include <pwd.h>
19#include <grp.h>
20#include <sys/socket.h>
21#include <sys/un.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/xattr.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020023#include "qemu/cutils.h"
Greg Kurz63325b12016-01-22 15:12:17 +010024#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010025#include "qemu/option.h"
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +053026#include <linux/fs.h>
27#ifdef CONFIG_LINUX_MAGIC_H
28#include <linux/magic.h>
29#endif
30#include <sys/ioctl.h>
31
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
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053044
Greg Kurzc4ce2c02018-01-08 11:18:22 +010045typedef struct HandleData {
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053046 int mountfd;
47 int handle_bytes;
Greg Kurzc4ce2c02018-01-08 11:18:22 +010048} HandleData;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053049
Aneesh Kumar K.Vd2042372011-10-12 19:11:24 +053050static inline int name_to_handle(int dirfd, const char *name,
51 struct file_handle *fh, int *mnt_id, int flags)
52{
53 return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
54}
55
56static inline int open_by_handle(int mountfd, const char *fh, int flags)
57{
58 return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
59}
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053060
61static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
62{
63 int fd, ret;
Dong Xu Wang3a931132011-11-29 16:52:38 +080064 fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053065 if (fd < 0) {
66 return fd;
67 }
M. Mohan Kumar2d405642012-01-19 12:21:12 +053068 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053069 if (ret < 0) {
70 goto err_out;
71 }
M. Mohan Kumar2d405642012-01-19 12:21:12 +053072 ret = fchmod(fd, credp->fc_mode & 07777);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053073err_out:
74 close(fd);
75 return ret;
76}
77
78
79static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
80 struct stat *stbuf)
81{
82 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +010083 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053084
85 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
86 if (fd < 0) {
87 return fd;
88 }
89 ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
90 close(fd);
91 return ret;
92}
93
94static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
95 char *buf, size_t bufsz)
96{
97 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +010098 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +053099
100 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
101 if (fd < 0) {
102 return fd;
103 }
104 ret = readlinkat(fd, "", buf, bufsz);
105 close(fd);
106 return ret;
107}
108
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530109static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530110{
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530111 return close(fs->fd);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530112}
113
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530114static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530115{
Greg Kurzf314ea42016-06-06 11:52:34 +0200116 return closedir(fs->dir.stream);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530117}
118
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530119static int handle_open(FsContext *ctx, V9fsPath *fs_path,
120 int flags, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530121{
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100122 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530123
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530124 fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
125 return fs->fd;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530126}
127
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530128static int handle_opendir(FsContext *ctx,
129 V9fsPath *fs_path, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530130{
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530131 int ret;
132 ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
133 if (ret < 0) {
134 return -1;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530135 }
Greg Kurzf314ea42016-06-06 11:52:34 +0200136 fs->dir.stream = fdopendir(ret);
137 if (!fs->dir.stream) {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530138 return -1;
139 }
140 return 0;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530141}
142
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530143static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530144{
Greg Kurzf314ea42016-06-06 11:52:34 +0200145 rewinddir(fs->dir.stream);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530146}
147
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530148static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530149{
Greg Kurzf314ea42016-06-06 11:52:34 +0200150 return telldir(fs->dir.stream);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530151}
152
Greg Kurz635324e2016-06-06 11:52:34 +0200153static struct dirent *handle_readdir(FsContext *ctx, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530154{
Greg Kurz635324e2016-06-06 11:52:34 +0200155 return readdir(fs->dir.stream);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530156}
157
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530158static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530159{
Greg Kurzf314ea42016-06-06 11:52:34 +0200160 seekdir(fs->dir.stream, off);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530161}
162
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530163static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
164 const struct iovec *iov,
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530165 int iovcnt, off_t offset)
166{
167#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530168 return preadv(fs->fd, iov, iovcnt, offset);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530169#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530170 int err = lseek(fs->fd, offset, SEEK_SET);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530171 if (err == -1) {
172 return err;
173 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530174 return readv(fs->fd, iov, iovcnt);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530175 }
176#endif
177}
178
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530179static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
180 const struct iovec *iov,
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530181 int iovcnt, off_t offset)
182{
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530183 ssize_t ret;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530184#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530185 ret = pwritev(fs->fd, iov, iovcnt, offset);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530186#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530187 int err = lseek(fs->fd, offset, SEEK_SET);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530188 if (err == -1) {
189 return err;
190 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530191 ret = writev(fs->fd, iov, iovcnt);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530192 }
193#endif
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530194#ifdef CONFIG_SYNC_FILE_RANGE
195 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
196 /*
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.
200 */
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530201 sync_file_range(fs->fd, offset, ret,
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530202 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
203 }
204#endif
205 return ret;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530206}
207
208static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
209{
210 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100211 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530212
213 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
214 if (fd < 0) {
215 return fd;
216 }
217 ret = fchmod(fd, credp->fc_mode);
218 close(fd);
219 return ret;
220}
221
222static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
223 const char *name, FsCred *credp)
224{
225 int dirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100226 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530227
228 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
229 if (dirfd < 0) {
230 return dirfd;
231 }
232 ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
233 if (!ret) {
234 ret = handle_update_file_cred(dirfd, name, credp);
235 }
236 close(dirfd);
237 return ret;
238}
239
240static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
241 const char *name, FsCred *credp)
242{
243 int dirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100244 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530245
246 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
247 if (dirfd < 0) {
248 return dirfd;
249 }
250 ret = mkdirat(dirfd, name, credp->fc_mode);
251 if (!ret) {
252 ret = handle_update_file_cred(dirfd, name, credp);
253 }
254 close(dirfd);
255 return ret;
256}
257
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530258static int handle_fstat(FsContext *fs_ctx, int fid_type,
259 V9fsFidOpenState *fs, struct stat *stbuf)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530260{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530261 int fd;
262
263 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200264 fd = dirfd(fs->dir.stream);
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530265 } else {
266 fd = fs->fd;
267 }
268 return fstat(fd, stbuf);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530269}
270
271static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530272 int flags, FsCred *credp, V9fsFidOpenState *fs)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530273{
274 int ret;
275 int dirfd, fd;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100276 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530277
278 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
279 if (dirfd < 0) {
280 return dirfd;
281 }
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;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530288 } else {
289 fs->fd = fd;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530290 }
291 }
292 close(dirfd);
293 return fd;
294}
295
296
297static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
298 V9fsPath *dir_path, const char *name, FsCred *credp)
299{
300 int fd, dirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100301 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530302
303 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
304 if (dirfd < 0) {
305 return dirfd;
306 }
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;
313 }
314 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
315 close(fd);
316 }
317err_out:
318 close(dirfd);
319 return ret;
320}
321
322static int handle_link(FsContext *ctx, V9fsPath *oldpath,
323 V9fsPath *dirpath, const char *name)
324{
325 int oldfd, newdirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100326 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530327
328 oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
329 if (oldfd < 0) {
330 return oldfd;
331 }
332 newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
333 if (newdirfd < 0) {
334 close(oldfd);
335 return newdirfd;
336 }
337 ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
338 close(newdirfd);
339 close(oldfd);
340 return ret;
341}
342
343static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
344{
345 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100346 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530347
348 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
349 if (fd < 0) {
350 return fd;
351 }
352 ret = ftruncate(fd, size);
353 close(fd);
354 return ret;
355}
356
357static int handle_rename(FsContext *ctx, const char *oldpath,
358 const char *newpath)
359{
360 errno = EOPNOTSUPP;
361 return -1;
362}
363
364static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
365{
366 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100367 HandleData *data = (HandleData *) fs_ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530368
369 fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
370 if (fd < 0) {
371 return fd;
372 }
373 ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
374 close(fd);
375 return ret;
376}
377
378static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
379 const struct timespec *buf)
380{
Aneesh Kumar K.Vd2042372011-10-12 19:11:24 +0530381 int ret;
Aneesh Kumar K.Vd2042372011-10-12 19:11:24 +0530382 int fd;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100383 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530384
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);
391 return ret;
392}
393
394static int handle_remove(FsContext *ctx, const char *path)
395{
396 errno = EOPNOTSUPP;
397 return -1;
398}
399
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530400static int handle_fsync(FsContext *ctx, int fid_type,
401 V9fsFidOpenState *fs, int datasync)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530402{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530403 int fd;
404
405 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200406 fd = dirfd(fs->dir.stream);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530407 } else {
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530408 fd = fs->fd;
409 }
410
411 if (datasync) {
412 return qemu_fdatasync(fd);
413 } else {
414 return fsync(fd);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530415 }
416}
417
418static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
419 struct statfs *stbuf)
420{
421 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100422 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530423
424 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
425 if (fd < 0) {
426 return fd;
427 }
428 ret = fstatfs(fd, stbuf);
429 close(fd);
430 return ret;
431}
432
433static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
434 const char *name, void *value, size_t size)
435{
436 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100437 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530438
439 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
440 if (fd < 0) {
441 return fd;
442 }
443 ret = fgetxattr(fd, name, value, size);
444 close(fd);
445 return ret;
446}
447
448static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
449 void *value, size_t size)
450{
451 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100452 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530453
454 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
455 if (fd < 0) {
456 return fd;
457 }
458 ret = flistxattr(fd, value, size);
459 close(fd);
460 return ret;
461}
462
463static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
464 void *value, size_t size, int flags)
465{
466 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100467 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530468
469 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
470 if (fd < 0) {
471 return fd;
472 }
473 ret = fsetxattr(fd, name, value, size, flags);
474 close(fd);
475 return ret;
476}
477
478static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
479 const char *name)
480{
481 int fd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100482 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530483
484 fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
485 if (fd < 0) {
486 return fd;
487 }
488 ret = fremovexattr(fd, name);
489 close(fd);
490 return ret;
491}
492
493static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
494 const char *name, V9fsPath *target)
495{
Chen Gang4fa4ce72014-03-02 01:36:19 +0800496 char *buffer;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530497 struct file_handle *fh;
498 int dirfd, ret, mnt_id;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100499 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530500
501 /* "." and ".." are not allowed */
502 if (!strcmp(name, ".") || !strcmp(name, "..")) {
503 errno = EINVAL;
504 return -1;
505
506 }
507 if (dir_path) {
508 dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
509 } else {
510 /* relative to export root */
Chen Gang4fa4ce72014-03-02 01:36:19 +0800511 buffer = rpath(ctx, ".");
512 dirfd = open(buffer, O_DIRECTORY);
513 g_free(buffer);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530514 }
515 if (dirfd < 0) {
516 return dirfd;
517 }
518 fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
519 fh->handle_bytes = data->handle_bytes;
Dong Xu Wang66a0a2c2011-11-29 16:52:39 +0800520 /* add a "./" at the beginning of the path */
Chen Gang4fa4ce72014-03-02 01:36:19 +0800521 buffer = g_strdup_printf("./%s", name);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530522 /* 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);
529 }
530 close(dirfd);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800531 g_free(buffer);
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530532 return ret;
533}
534
535static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
536 const char *old_name, V9fsPath *newdir,
537 const char *new_name)
538{
539 int olddirfd, newdirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100540 HandleData *data = (HandleData *) ctx->private;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530541
542 olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
543 if (olddirfd < 0) {
544 return olddirfd;
545 }
546 newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
547 if (newdirfd < 0) {
548 close(olddirfd);
549 return newdirfd;
550 }
551 ret = renameat(olddirfd, old_name, newdirfd, new_name);
552 close(newdirfd);
553 close(olddirfd);
554 return ret;
555}
556
557static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
558 const char *name, int flags)
559{
560 int dirfd, ret;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100561 HandleData *data = (HandleData *) ctx->private;
Paolo Bonzini930b5882011-11-18 17:35:38 +0100562 int rflags;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530563
564 dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
565 if (dirfd < 0) {
566 return dirfd;
567 }
568
Paolo Bonzini930b5882011-11-18 17:35:38 +0100569 rflags = 0;
570 if (flags & P9_DOTL_AT_REMOVEDIR) {
571 rflags |= AT_REMOVEDIR;
572 }
573
574 ret = unlinkat(dirfd, name, rflags);
575
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530576 close(dirfd);
577 return ret;
578}
579
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530580static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
581 mode_t st_mode, uint64_t *st_gen)
582{
Kirill A. Shutemovb9317662014-01-28 17:08:25 +0200583#ifdef FS_IOC_GETVERSION
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530584 int err;
585 V9fsFidOpenState fid_open;
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530586
587 /*
588 * Do not try to open special files like device nodes, fifos etc
589 * We can get fd for regular files and directories only
590 */
591 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
Kirill A. Shutemov1a9978a2014-01-28 17:08:26 +0200592 errno = ENOTTY;
593 return -1;
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530594 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530595 err = handle_open(ctx, path, O_RDONLY, &fid_open);
596 if (err < 0) {
597 return err;
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530598 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530599 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
600 handle_close(ctx, &fid_open);
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530601 return err;
Kirill A. Shutemovb9317662014-01-28 17:08:25 +0200602#else
603 errno = ENOTTY;
604 return -1;
605#endif
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530606}
607
Greg Kurz65603a82018-01-08 11:18:23 +0100608static int handle_init(FsContext *ctx, Error **errp)
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530609{
610 int ret, mnt_id;
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530611 struct statfs stbuf;
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530612 struct file_handle fh;
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100613 HandleData *data = g_malloc(sizeof(HandleData));
Aneesh Kumar K.Vd2042372011-10-12 19:11:24 +0530614
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530615 data->mountfd = open(ctx->fs_root, O_DIRECTORY);
616 if (data->mountfd < 0) {
617 ret = data->mountfd;
618 goto err_out;
619 }
Harsh Prateek Boraedb9eb72011-10-12 19:11:25 +0530620 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;
629 }
630 }
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530631 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;
638 }
639 /* we got 0 byte handle ? */
640 ret = -1;
641 close(data->mountfd);
642err_out:
643 g_free(data);
644out:
645 return ret;
646}
647
Li Qiang971f4062016-11-23 13:53:34 +0100648static void handle_cleanup(FsContext *ctx)
649{
Greg Kurzc4ce2c02018-01-08 11:18:22 +0100650 HandleData *data = ctx->private;
Li Qiang971f4062016-11-23 13:53:34 +0100651
652 close(data->mountfd);
653 g_free(data);
654}
655
Greg Kurz91cda4e2018-01-08 11:18:23 +0100656static int handle_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530657{
658 const char *sec_model = qemu_opt_get(opts, "security_model");
659 const char *path = qemu_opt_get(opts, "path");
660
Greg Kurzdb3b3c72018-01-08 11:18:23 +0100661 warn_report("handle backend is deprecated");
662
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530663 if (sec_model) {
Greg Kurz63325b12016-01-22 15:12:17 +0100664 error_report("Invalid argument security_model specified with handle fsdriver");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530665 return -1;
666 }
667
668 if (!path) {
Greg Kurz63325b12016-01-22 15:12:17 +0100669 error_report("fsdev: No path specified");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530670 return -1;
671 }
672 fse->path = g_strdup(path);
673 return 0;
674
675}
676
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530677FileOperations handle_ops = {
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530678 .parse_opts = handle_parse_opts,
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530679 .init = handle_init,
Li Qiang971f4062016-11-23 13:53:34 +0100680 .cleanup = handle_cleanup,
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530681 .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,
Greg Kurz635324e2016-06-06 11:52:34 +0200689 .readdir = handle_readdir,
Aneesh Kumar K.V5f542222011-08-02 11:35:54 +0530690 .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,
714};