]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/9p-local.c
Move include qemu/option.h from qemu-common.h to actual users
[mirror_qemu.git] / hw / 9pfs / 9p-local.c
CommitLineData
9f107513 1/*
f00d4f59 2 * 9p Posix callback
9f107513
AL
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.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.
9f107513 11 */
873c3213 12
fbc04127 13#include "qemu/osdep.h"
ebe74f8b 14#include "9p.h"
996a0d76 15#include "9p-local.h"
267ae092 16#include "9p-xattr.h"
0e35a378 17#include "9p-util.h"
69b15212 18#include "fsdev/qemu-fsdev.h" /* local_ops */
c494dd6f 19#include <arpa/inet.h>
131dcb25
AL
20#include <pwd.h>
21#include <grp.h>
c494dd6f
AL
22#include <sys/socket.h>
23#include <sys/un.h>
1de7afc9 24#include "qemu/xattr.h"
e688df6b 25#include "qapi/error.h"
f348b6d1 26#include "qemu/cutils.h"
63325b18 27#include "qemu/error-report.h"
922a01a0 28#include "qemu/option.h"
2c30dd74 29#include <libgen.h>
e06a765e
HPB
30#include <linux/fs.h>
31#ifdef CONFIG_LINUX_MAGIC_H
32#include <linux/magic.h>
33#endif
34#include <sys/ioctl.h>
35
36#ifndef XFS_SUPER_MAGIC
37#define XFS_SUPER_MAGIC 0x58465342
38#endif
39#ifndef EXT2_SUPER_MAGIC
40#define EXT2_SUPER_MAGIC 0xEF53
41#endif
42#ifndef REISERFS_SUPER_MAGIC
43#define REISERFS_SUPER_MAGIC 0x52654973
44#endif
45#ifndef BTRFS_SUPER_MAGIC
46#define BTRFS_SUPER_MAGIC 0x9123683E
47#endif
131dcb25 48
0e35a378
GK
49typedef struct {
50 int mountfd;
51} LocalData;
2c30dd74 52
996a0d76
GK
53int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
54 mode_t mode)
2c30dd74 55{
996a0d76 56 LocalData *data = fs_ctx->private;
3dbcf273 57 int fd = data->mountfd;
996a0d76 58
3dbcf273
GK
59 while (*path && fd != -1) {
60 const char *c;
61 int next_fd;
62 char *head;
63
64 /* Only relative paths without consecutive slashes */
65 assert(*path != '/');
66
67 head = g_strdup(path);
68 c = strchrnul(path, '/');
69 if (*c) {
70 /* Intermediate path element */
71 head[c - path] = 0;
72 path = c + 1;
73 next_fd = openat_dir(fd, head);
74 } else {
75 /* Rightmost path element */
76 next_fd = openat_file(fd, head, flags, mode);
77 path = c;
78 }
79 g_free(head);
80 if (fd != data->mountfd) {
81 close_preserve_errno(fd);
82 }
83 fd = next_fd;
1b6f85e2 84 }
996a0d76 85
3dbcf273
GK
86 assert(fd != data->mountfd);
87 return fd;
996a0d76
GK
88}
89
90int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
91{
92 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
93}
94
99f2cf4b
GK
95static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
96 const char *npath)
97{
98 int serrno = errno;
99 renameat(odirfd, opath, ndirfd, npath);
100 errno = serrno;
2c30dd74
AK
101}
102
ad0b46e6
GK
103static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
104{
105 int serrno = errno;
106 unlinkat(dirfd, path, flags);
107 errno = serrno;
108}
109
2c30dd74 110#define VIRTFS_META_DIR ".virtfs_metadata"
81ffbf5a 111#define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
2c30dd74 112
f9aef99b 113static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
0ceb092e
AK
114{
115 int fd, o_mode = 0;
116 FILE *fp;
f9aef99b 117 int flags;
0ceb092e
AK
118 /*
119 * only supports two modes
120 */
121 if (mode[0] == 'r') {
f9aef99b 122 flags = O_RDONLY;
0ceb092e 123 } else if (mode[0] == 'w') {
f9aef99b 124 flags = O_WRONLY | O_TRUNC | O_CREAT;
0ceb092e
AK
125 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
126 } else {
127 return NULL;
128 }
f9aef99b 129 fd = openat_file(dirfd, name, flags, o_mode);
0ceb092e
AK
130 if (fd == -1) {
131 return NULL;
132 }
133 fp = fdopen(fd, mode);
134 if (!fp) {
135 close(fd);
136 }
137 return fp;
138}
139
2c30dd74 140#define ATTR_MAX 100
f9aef99b 141static void local_mapped_file_attr(int dirfd, const char *name,
2c30dd74
AK
142 struct stat *stbuf)
143{
144 FILE *fp;
145 char buf[ATTR_MAX];
f9aef99b 146 int map_dirfd;
2c30dd74 147
81ffbf5a
GK
148 if (strcmp(name, ".")) {
149 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
150 if (map_dirfd == -1) {
151 return;
152 }
2c30dd74 153
81ffbf5a
GK
154 fp = local_fopenat(map_dirfd, name, "r");
155 close_preserve_errno(map_dirfd);
156 } else {
157 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
158 }
2c30dd74
AK
159 if (!fp) {
160 return;
161 }
162 memset(buf, 0, ATTR_MAX);
163 while (fgets(buf, ATTR_MAX, fp)) {
164 if (!strncmp(buf, "virtfs.uid", 10)) {
165 stbuf->st_uid = atoi(buf+11);
166 } else if (!strncmp(buf, "virtfs.gid", 10)) {
167 stbuf->st_gid = atoi(buf+11);
168 } else if (!strncmp(buf, "virtfs.mode", 11)) {
169 stbuf->st_mode = atoi(buf+12);
170 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
171 stbuf->st_rdev = atoi(buf+12);
172 }
173 memset(buf, 0, ATTR_MAX);
174 }
175 fclose(fp);
176}
177
2289be19 178static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
131dcb25 179{
f9aef99b
GK
180 int err = -1;
181 char *dirpath = g_path_get_dirname(fs_path->data);
182 char *name = g_path_get_basename(fs_path->data);
183 int dirfd;
184
185 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
186 if (dirfd == -1) {
187 goto out;
188 }
2289be19 189
f9aef99b 190 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
1237ad76 191 if (err) {
4fa4ce71 192 goto err_out;
1237ad76 193 }
b97400ca 194 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1237ad76
VJ
195 /* Actual credentials are part of extended attrs */
196 uid_t tmp_uid;
197 gid_t tmp_gid;
198 mode_t tmp_mode;
199 dev_t tmp_dev;
f9aef99b
GK
200
201 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
202 sizeof(uid_t)) > 0) {
f8ad4a89 203 stbuf->st_uid = le32_to_cpu(tmp_uid);
1237ad76 204 }
f9aef99b
GK
205 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
206 sizeof(gid_t)) > 0) {
f8ad4a89 207 stbuf->st_gid = le32_to_cpu(tmp_gid);
1237ad76 208 }
f9aef99b
GK
209 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
210 sizeof(mode_t)) > 0) {
f8ad4a89 211 stbuf->st_mode = le32_to_cpu(tmp_mode);
1237ad76 212 }
f9aef99b
GK
213 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
214 sizeof(dev_t)) > 0) {
f8ad4a89 215 stbuf->st_rdev = le64_to_cpu(tmp_dev);
1237ad76 216 }
2c30dd74 217 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
f9aef99b 218 local_mapped_file_attr(dirfd, name, stbuf);
2c30dd74 219 }
4fa4ce71
CG
220
221err_out:
f9aef99b
GK
222 close_preserve_errno(dirfd);
223out:
224 g_free(name);
225 g_free(dirpath);
1237ad76 226 return err;
131dcb25
AL
227}
228
e3187a45
GK
229static int local_set_mapped_file_attrat(int dirfd, const char *name,
230 FsCred *credp)
2c30dd74
AK
231{
232 FILE *fp;
e3187a45 233 int ret;
2c30dd74 234 char buf[ATTR_MAX];
2c30dd74 235 int uid = -1, gid = -1, mode = -1, rdev = -1;
81ffbf5a
GK
236 int map_dirfd = -1, map_fd;
237 bool is_root = !strcmp(name, ".");
238
239 if (is_root) {
240 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
241 if (!fp) {
242 if (errno == ENOENT) {
243 goto update_map_file;
244 } else {
245 return -1;
246 }
247 }
248 } else {
249 ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
250 if (ret < 0 && errno != EEXIST) {
251 return -1;
252 }
e3187a45 253
81ffbf5a
GK
254 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
255 if (map_dirfd == -1) {
e3187a45
GK
256 return -1;
257 }
81ffbf5a
GK
258
259 fp = local_fopenat(map_dirfd, name, "r");
260 if (!fp) {
261 if (errno == ENOENT) {
262 goto update_map_file;
263 } else {
264 close_preserve_errno(map_dirfd);
265 return -1;
266 }
267 }
2c30dd74
AK
268 }
269 memset(buf, 0, ATTR_MAX);
270 while (fgets(buf, ATTR_MAX, fp)) {
271 if (!strncmp(buf, "virtfs.uid", 10)) {
e3187a45 272 uid = atoi(buf + 11);
2c30dd74 273 } else if (!strncmp(buf, "virtfs.gid", 10)) {
e3187a45 274 gid = atoi(buf + 11);
2c30dd74 275 } else if (!strncmp(buf, "virtfs.mode", 11)) {
e3187a45 276 mode = atoi(buf + 12);
2c30dd74 277 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
e3187a45 278 rdev = atoi(buf + 12);
2c30dd74
AK
279 }
280 memset(buf, 0, ATTR_MAX);
281 }
282 fclose(fp);
2c30dd74
AK
283
284update_map_file:
81ffbf5a
GK
285 if (is_root) {
286 fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
287 } else {
288 fp = local_fopenat(map_dirfd, name, "w");
289 /* We can't go this far with map_dirfd not being a valid file descriptor
290 * but some versions of gcc aren't smart enough to see it.
291 */
292 if (map_dirfd != -1) {
293 close_preserve_errno(map_dirfd);
294 }
295 }
2c30dd74 296 if (!fp) {
e3187a45 297 return -1;
2c30dd74
AK
298 }
299
81ffbf5a
GK
300 map_fd = fileno(fp);
301 assert(map_fd != -1);
302 ret = fchmod(map_fd, 0600);
303 assert(ret == 0);
304
2c30dd74
AK
305 if (credp->fc_uid != -1) {
306 uid = credp->fc_uid;
307 }
308 if (credp->fc_gid != -1) {
309 gid = credp->fc_gid;
310 }
311 if (credp->fc_mode != -1) {
312 mode = credp->fc_mode;
313 }
314 if (credp->fc_rdev != -1) {
315 rdev = credp->fc_rdev;
316 }
317
2c30dd74
AK
318 if (uid != -1) {
319 fprintf(fp, "virtfs.uid=%d\n", uid);
320 }
321 if (gid != -1) {
322 fprintf(fp, "virtfs.gid=%d\n", gid);
323 }
324 if (mode != -1) {
325 fprintf(fp, "virtfs.mode=%d\n", mode);
326 }
327 if (rdev != -1) {
328 fprintf(fp, "virtfs.rdev=%d\n", rdev);
329 }
330 fclose(fp);
331
e3187a45
GK
332 return 0;
333}
334
335static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
336{
4751fd53 337 struct stat stbuf;
e3187a45
GK
338 int fd, ret;
339
340 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
4751fd53 341 * Unfortunately, the linux kernel doesn't implement it yet.
e3187a45 342 */
4751fd53
GK
343
344 /* First, we clear non-racing symlinks out of the way. */
345 if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) {
346 return -1;
347 }
348 if (S_ISLNK(stbuf.st_mode)) {
349 errno = ELOOP;
350 return -1;
351 }
352
aa5e85a1 353 fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0);
4751fd53 354#if O_PATH_9P_UTIL == 0
aa5e85a1
GK
355 /* Fallback for systems that don't support O_PATH: we depend on the file
356 * being readable or writable.
357 */
e3187a45
GK
358 if (fd == -1) {
359 /* In case the file is writable-only and isn't a directory. */
360 if (errno == EACCES) {
361 fd = openat_file(dirfd, name, O_WRONLY, 0);
362 }
363 if (fd == -1 && errno == EISDIR) {
364 errno = EACCES;
365 }
366 }
367 if (fd == -1) {
368 return -1;
369 }
370 ret = fchmod(fd, mode);
4751fd53 371#else
aa5e85a1
GK
372 /* Access modes are ignored when O_PATH is supported. If name is a symbolic
373 * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor
374 * referring to the symbolic link.
375 */
4751fd53
GK
376 if (fd == -1) {
377 return -1;
378 }
379
380 /* Now we handle racing symlinks. */
381 ret = fstat(fd, &stbuf);
382 if (!ret) {
383 if (S_ISLNK(stbuf.st_mode)) {
384 errno = ELOOP;
385 ret = -1;
386 } else {
387 char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd);
388 ret = chmod(proc_path, mode);
389 g_free(proc_path);
390 }
391 }
392#endif
e3187a45 393 close_preserve_errno(fd);
2c30dd74
AK
394 return ret;
395}
396
e3187a45 397static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
131dcb25 398{
758e8e38 399 int err;
2289be19 400
758e8e38 401 if (credp->fc_uid != -1) {
f8ad4a89 402 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
e3187a45
GK
403 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
404 sizeof(uid_t), 0);
758e8e38
VJ
405 if (err) {
406 return err;
407 }
131dcb25 408 }
758e8e38 409 if (credp->fc_gid != -1) {
f8ad4a89 410 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
e3187a45
GK
411 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
412 sizeof(gid_t), 0);
758e8e38
VJ
413 if (err) {
414 return err;
415 }
131dcb25 416 }
758e8e38 417 if (credp->fc_mode != -1) {
f8ad4a89 418 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
e3187a45
GK
419 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
420 sizeof(mode_t), 0);
758e8e38
VJ
421 if (err) {
422 return err;
423 }
131dcb25 424 }
758e8e38 425 if (credp->fc_rdev != -1) {
f8ad4a89 426 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
e3187a45
GK
427 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
428 sizeof(dev_t), 0);
758e8e38
VJ
429 if (err) {
430 return err;
431 }
131dcb25 432 }
131dcb25
AL
433 return 0;
434}
435
d815e721
GK
436static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
437 const char *name, FsCred *credp)
4750a96f 438{
d815e721 439 if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
b314f6a0 440 AT_SYMLINK_NOFOLLOW) < 0) {
12848bfc
AK
441 /*
442 * If we fail to change ownership and if we are
443 * using security model none. Ignore the error
444 */
b97400ca 445 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
d815e721 446 return -1;
12848bfc 447 }
4750a96f 448 }
2d40564a 449
d815e721 450 return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
4750a96f
VJ
451}
452
2289be19
AK
453static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
454 char *buf, size_t bufsz)
131dcb25 455{
879c2813 456 ssize_t tsize = -1;
2289be19 457
2c30dd74
AK
458 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
459 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
879c2813 460 int fd;
bec1e954
GK
461
462 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
879c2813
VJ
463 if (fd == -1) {
464 return -1;
465 }
466 do {
467 tsize = read(fd, (void *)buf, bufsz);
468 } while (tsize == -1 && errno == EINTR);
bec1e954 469 close_preserve_errno(fd);
b97400ca
AK
470 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
471 (fs_ctx->export_flags & V9FS_SM_NONE)) {
bec1e954
GK
472 char *dirpath = g_path_get_dirname(fs_path->data);
473 char *name = g_path_get_basename(fs_path->data);
474 int dirfd;
475
476 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
477 if (dirfd == -1) {
478 goto out;
479 }
480
481 tsize = readlinkat(dirfd, name, buf, bufsz);
482 close_preserve_errno(dirfd);
483 out:
484 g_free(name);
485 g_free(dirpath);
879c2813
VJ
486 }
487 return tsize;
131dcb25
AL
488}
489
cc720ddb 490static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 491{
cc720ddb 492 return close(fs->fd);
131dcb25
AL
493}
494
cc720ddb 495static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 496{
f314ea4e 497 return closedir(fs->dir.stream);
131dcb25 498}
9f107513 499
cc720ddb
AK
500static int local_open(FsContext *ctx, V9fsPath *fs_path,
501 int flags, V9fsFidOpenState *fs)
a6568fe2 502{
21328e1e 503 int fd;
2289be19 504
996a0d76 505 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
21328e1e
GK
506 if (fd == -1) {
507 return -1;
508 }
509 fs->fd = fd;
cc720ddb 510 return fs->fd;
a6568fe2
AL
511}
512
cc720ddb
AK
513static int local_opendir(FsContext *ctx,
514 V9fsPath *fs_path, V9fsFidOpenState *fs)
a6568fe2 515{
996a0d76 516 int dirfd;
21328e1e 517 DIR *stream;
2289be19 518
996a0d76
GK
519 dirfd = local_opendir_nofollow(ctx, fs_path->data);
520 if (dirfd == -1) {
521 return -1;
522 }
2289be19 523
996a0d76 524 stream = fdopendir(dirfd);
21328e1e 525 if (!stream) {
faab207f 526 close(dirfd);
cc720ddb
AK
527 return -1;
528 }
21328e1e 529 fs->dir.stream = stream;
cc720ddb 530 return 0;
a6568fe2
AL
531}
532
cc720ddb 533static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 534{
f314ea4e 535 rewinddir(fs->dir.stream);
a9231555
AL
536}
537
cc720ddb 538static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 539{
f314ea4e 540 return telldir(fs->dir.stream);
a9231555
AL
541}
542
7a95434e
GK
543static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
544{
81ffbf5a
GK
545 return
546 !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
7a95434e
GK
547}
548
635324e8 549static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 550{
635324e8 551 struct dirent *entry;
2c30dd74
AK
552
553again:
635324e8
GK
554 entry = readdir(fs->dir.stream);
555 if (!entry) {
556 return NULL;
557 }
558
840a1bf2
BB
559 if (ctx->export_flags & V9FS_SM_MAPPED) {
560 entry->d_type = DT_UNKNOWN;
561 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
7a95434e 562 if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
81ffbf5a 563 /* skip the meta data */
2c30dd74
AK
564 goto again;
565 }
840a1bf2 566 entry->d_type = DT_UNKNOWN;
2c30dd74 567 }
635324e8
GK
568
569 return entry;
a9231555
AL
570}
571
cc720ddb 572static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
a9231555 573{
f314ea4e 574 seekdir(fs->dir.stream, off);
a9231555
AL
575}
576
cc720ddb
AK
577static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
578 const struct iovec *iov,
56d15a53 579 int iovcnt, off_t offset)
a9231555 580{
56d15a53 581#ifdef CONFIG_PREADV
cc720ddb 582 return preadv(fs->fd, iov, iovcnt, offset);
56d15a53 583#else
cc720ddb 584 int err = lseek(fs->fd, offset, SEEK_SET);
56d15a53
SG
585 if (err == -1) {
586 return err;
587 } else {
cc720ddb 588 return readv(fs->fd, iov, iovcnt);
56d15a53
SG
589 }
590#endif
a9231555
AL
591}
592
cc720ddb
AK
593static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
594 const struct iovec *iov,
2289be19 595 int iovcnt, off_t offset)
8449360c 596{
6fe76acc 597 ssize_t ret;
56d15a53 598#ifdef CONFIG_PREADV
cc720ddb 599 ret = pwritev(fs->fd, iov, iovcnt, offset);
56d15a53 600#else
cc720ddb 601 int err = lseek(fs->fd, offset, SEEK_SET);
56d15a53
SG
602 if (err == -1) {
603 return err;
604 } else {
cc720ddb 605 ret = writev(fs->fd, iov, iovcnt);
56d15a53
SG
606 }
607#endif
d3ab98e6
AK
608#ifdef CONFIG_SYNC_FILE_RANGE
609 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
610 /*
611 * Initiate a writeback. This is not a data integrity sync.
612 * We want to ensure that we don't leave dirty pages in the cache
613 * after write when writeout=immediate is sepcified.
614 */
cc720ddb 615 sync_file_range(fs->fd, offset, ret,
d3ab98e6
AK
616 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
617 }
618#endif
619 return ret;
8449360c
AL
620}
621
2289be19 622static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
c494dd6f 623{
e3187a45
GK
624 char *dirpath = g_path_get_dirname(fs_path->data);
625 char *name = g_path_get_basename(fs_path->data);
4fa4ce71 626 int ret = -1;
e3187a45
GK
627 int dirfd;
628
629 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
630 if (dirfd == -1) {
631 goto out;
632 }
2289be19 633
b97400ca 634 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
e3187a45 635 ret = local_set_xattrat(dirfd, name, credp);
2c30dd74 636 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
e3187a45
GK
637 ret = local_set_mapped_file_attrat(dirfd, name, credp);
638 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
639 fs_ctx->export_flags & V9FS_SM_NONE) {
640 ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
e95ead32 641 }
e3187a45
GK
642 close_preserve_errno(dirfd);
643
644out:
645 g_free(dirpath);
646 g_free(name);
4fa4ce71 647 return ret;
c494dd6f
AL
648}
649
2289be19
AK
650static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
651 const char *name, FsCred *credp)
c494dd6f 652{
1c293312 653 int err = -1;
d815e721 654 int dirfd;
1c293312 655
7a95434e
GK
656 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
657 local_is_mapped_file_metadata(fs_ctx, name)) {
658 errno = EINVAL;
659 return -1;
660 }
661
d815e721
GK
662 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
663 if (dirfd == -1) {
664 return -1;
665 }
2289be19 666
d815e721
GK
667 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
668 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
b96feb2c 669 err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
1c293312 670 if (err == -1) {
2289be19 671 goto out;
1c293312 672 }
2c30dd74 673
d815e721
GK
674 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
675 err = local_set_xattrat(dirfd, name, credp);
676 } else {
677 err = local_set_mapped_file_attrat(dirfd, name, credp);
2c30dd74 678 }
2c30dd74 679 if (err == -1) {
2c30dd74
AK
680 goto err_end;
681 }
d815e721
GK
682 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
683 fs_ctx->export_flags & V9FS_SM_NONE) {
684 err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
1c293312 685 if (err == -1) {
2289be19 686 goto out;
1c293312 687 }
d815e721 688 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
1c293312 689 if (err == -1) {
1c293312
VJ
690 goto err_end;
691 }
692 }
2289be19 693 goto out;
1c293312
VJ
694
695err_end:
d815e721 696 unlinkat_preserve_errno(dirfd, name, 0);
2289be19 697out:
d815e721 698 close_preserve_errno(dirfd);
1c293312 699 return err;
c494dd6f
AL
700}
701
2289be19
AK
702static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
703 const char *name, FsCred *credp)
c494dd6f 704{
00ec5c37 705 int err = -1;
3f3a1699 706 int dirfd;
00ec5c37 707
7a95434e
GK
708 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
709 local_is_mapped_file_metadata(fs_ctx, name)) {
710 errno = EINVAL;
711 return -1;
712 }
713
3f3a1699
GK
714 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
715 if (dirfd == -1) {
716 return -1;
717 }
2289be19 718
3f3a1699
GK
719 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
720 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
b96feb2c 721 err = mkdirat(dirfd, name, fs_ctx->dmode);
00ec5c37 722 if (err == -1) {
2289be19 723 goto out;
00ec5c37 724 }
3f3a1699
GK
725 credp->fc_mode = credp->fc_mode | S_IFDIR;
726
727 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
728 err = local_set_xattrat(dirfd, name, credp);
729 } else {
730 err = local_set_mapped_file_attrat(dirfd, name, credp);
2c30dd74 731 }
2c30dd74 732 if (err == -1) {
2c30dd74
AK
733 goto err_end;
734 }
3f3a1699
GK
735 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
736 fs_ctx->export_flags & V9FS_SM_NONE) {
737 err = mkdirat(dirfd, name, credp->fc_mode);
00ec5c37 738 if (err == -1) {
2289be19 739 goto out;
00ec5c37 740 }
3f3a1699 741 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
00ec5c37 742 if (err == -1) {
00ec5c37
VJ
743 goto err_end;
744 }
745 }
2289be19 746 goto out;
00ec5c37
VJ
747
748err_end:
3f3a1699 749 unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
2289be19 750out:
3f3a1699 751 close_preserve_errno(dirfd);
00ec5c37 752 return err;
c494dd6f
AL
753}
754
8b888272 755static int local_fstat(FsContext *fs_ctx, int fid_type,
cc720ddb 756 V9fsFidOpenState *fs, struct stat *stbuf)
c494dd6f 757{
8b888272
AK
758 int err, fd;
759
760 if (fid_type == P9_FID_DIR) {
f314ea4e 761 fd = dirfd(fs->dir.stream);
8b888272
AK
762 } else {
763 fd = fs->fd;
764 }
765
766 err = fstat(fd, stbuf);
1237ad76
VJ
767 if (err) {
768 return err;
769 }
b97400ca 770 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1237ad76
VJ
771 /* Actual credentials are part of extended attrs */
772 uid_t tmp_uid;
773 gid_t tmp_gid;
774 mode_t tmp_mode;
775 dev_t tmp_dev;
776
f8ad4a89
AK
777 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
778 stbuf->st_uid = le32_to_cpu(tmp_uid);
1237ad76 779 }
f8ad4a89
AK
780 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
781 stbuf->st_gid = le32_to_cpu(tmp_gid);
1237ad76 782 }
f8ad4a89
AK
783 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
784 stbuf->st_mode = le32_to_cpu(tmp_mode);
1237ad76 785 }
f8ad4a89
AK
786 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
787 stbuf->st_rdev = le64_to_cpu(tmp_dev);
1237ad76 788 }
2c30dd74
AK
789 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
790 errno = EOPNOTSUPP;
791 return -1;
1237ad76
VJ
792 }
793 return err;
c494dd6f
AL
794}
795
2289be19 796static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
cc720ddb 797 int flags, FsCred *credp, V9fsFidOpenState *fs)
c494dd6f 798{
4750a96f
VJ
799 int fd = -1;
800 int err = -1;
a565fea5 801 int dirfd;
4750a96f 802
7a95434e
GK
803 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
804 local_is_mapped_file_metadata(fs_ctx, name)) {
805 errno = EINVAL;
806 return -1;
807 }
808
0ceb092e
AK
809 /*
810 * Mark all the open to not follow symlinks
811 */
812 flags |= O_NOFOLLOW;
813
a565fea5
GK
814 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
815 if (dirfd == -1) {
816 return -1;
817 }
2289be19 818
4750a96f 819 /* Determine the security model */
a565fea5
GK
820 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
821 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
b96feb2c 822 fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
4750a96f 823 if (fd == -1) {
2289be19 824 goto out;
4750a96f
VJ
825 }
826 credp->fc_mode = credp->fc_mode|S_IFREG;
a565fea5
GK
827 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
828 /* Set cleint credentials in xattr */
829 err = local_set_xattrat(dirfd, name, credp);
830 } else {
831 err = local_set_mapped_file_attrat(dirfd, name, credp);
2c30dd74 832 }
2c30dd74 833 if (err == -1) {
2c30dd74
AK
834 goto err_end;
835 }
b97400ca
AK
836 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
837 (fs_ctx->export_flags & V9FS_SM_NONE)) {
a565fea5 838 fd = openat_file(dirfd, name, flags, credp->fc_mode);
4750a96f 839 if (fd == -1) {
2289be19 840 goto out;
4750a96f 841 }
a565fea5 842 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
4750a96f 843 if (err == -1) {
4750a96f
VJ
844 goto err_end;
845 }
846 }
2289be19 847 err = fd;
cc720ddb 848 fs->fd = fd;
2289be19 849 goto out;
4750a96f
VJ
850
851err_end:
a565fea5
GK
852 unlinkat_preserve_errno(dirfd, name,
853 flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
854 close_preserve_errno(fd);
2289be19 855out:
a565fea5 856 close_preserve_errno(dirfd);
4750a96f 857 return err;
c494dd6f
AL
858}
859
758e8e38 860
879c2813 861static int local_symlink(FsContext *fs_ctx, const char *oldpath,
2289be19 862 V9fsPath *dir_path, const char *name, FsCred *credp)
c494dd6f 863{
879c2813 864 int err = -1;
38771613 865 int dirfd;
879c2813 866
7a95434e
GK
867 if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
868 local_is_mapped_file_metadata(fs_ctx, name)) {
869 errno = EINVAL;
870 return -1;
871 }
872
38771613
GK
873 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
874 if (dirfd == -1) {
875 return -1;
876 }
2289be19 877
879c2813 878 /* Determine the security model */
38771613
GK
879 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
880 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
879c2813
VJ
881 int fd;
882 ssize_t oldpath_size, write_size;
38771613
GK
883
884 fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
b96feb2c 885 fs_ctx->fmode);
879c2813 886 if (fd == -1) {
2289be19 887 goto out;
879c2813
VJ
888 }
889 /* Write the oldpath (target) to the file. */
f35bde2f 890 oldpath_size = strlen(oldpath);
879c2813
VJ
891 do {
892 write_size = write(fd, (void *)oldpath, oldpath_size);
893 } while (write_size == -1 && errno == EINTR);
38771613 894 close_preserve_errno(fd);
879c2813
VJ
895
896 if (write_size != oldpath_size) {
879c2813
VJ
897 goto err_end;
898 }
879c2813 899 /* Set cleint credentials in symlink's xattr */
38771613 900 credp->fc_mode = credp->fc_mode | S_IFLNK;
2c30dd74 901
38771613
GK
902 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
903 err = local_set_xattrat(dirfd, name, credp);
904 } else {
905 err = local_set_mapped_file_attrat(dirfd, name, credp);
2c30dd74 906 }
2c30dd74 907 if (err == -1) {
2c30dd74
AK
908 goto err_end;
909 }
38771613
GK
910 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
911 fs_ctx->export_flags & V9FS_SM_NONE) {
912 err = symlinkat(oldpath, dirfd, name);
879c2813 913 if (err) {
2289be19 914 goto out;
879c2813 915 }
38771613
GK
916 err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
917 AT_SYMLINK_NOFOLLOW);
879c2813 918 if (err == -1) {
12848bfc
AK
919 /*
920 * If we fail to change ownership and if we are
921 * using security model none. Ignore the error
922 */
b97400ca 923 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
12848bfc 924 goto err_end;
38771613 925 } else {
12848bfc 926 err = 0;
38771613 927 }
879c2813
VJ
928 }
929 }
2289be19 930 goto out;
879c2813
VJ
931
932err_end:
38771613 933 unlinkat_preserve_errno(dirfd, name, 0);
2289be19 934out:
38771613 935 close_preserve_errno(dirfd);
879c2813 936 return err;
c494dd6f
AL
937}
938
2289be19
AK
939static int local_link(FsContext *ctx, V9fsPath *oldpath,
940 V9fsPath *dirpath, const char *name)
c494dd6f 941{
ad0b46e6
GK
942 char *odirpath = g_path_get_dirname(oldpath->data);
943 char *oname = g_path_get_basename(oldpath->data);
944 int ret = -1;
945 int odirfd, ndirfd;
946
7a95434e
GK
947 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
948 local_is_mapped_file_metadata(ctx, name)) {
949 errno = EINVAL;
950 return -1;
951 }
952
ad0b46e6
GK
953 odirfd = local_opendir_nofollow(ctx, odirpath);
954 if (odirfd == -1) {
955 goto out;
956 }
c494dd6f 957
ad0b46e6
GK
958 ndirfd = local_opendir_nofollow(ctx, dirpath->data);
959 if (ndirfd == -1) {
960 close_preserve_errno(odirfd);
961 goto out;
962 }
2289be19 963
ad0b46e6 964 ret = linkat(odirfd, oname, ndirfd, name, 0);
6dd4b1f1 965 if (ret < 0) {
ad0b46e6 966 goto out_close;
6dd4b1f1 967 }
2c30dd74
AK
968
969 /* now link the virtfs_metadata files */
6dd4b1f1 970 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
ad0b46e6
GK
971 int omap_dirfd, nmap_dirfd;
972
973 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
974 if (ret < 0 && errno != EEXIST) {
975 goto err_undo_link;
2c30dd74 976 }
ad0b46e6
GK
977
978 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
979 if (omap_dirfd == -1) {
980 goto err;
981 }
982
983 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
984 if (nmap_dirfd == -1) {
985 close_preserve_errno(omap_dirfd);
986 goto err;
987 }
988
989 ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
990 close_preserve_errno(nmap_dirfd);
991 close_preserve_errno(omap_dirfd);
2c30dd74 992 if (ret < 0 && errno != ENOENT) {
ad0b46e6 993 goto err_undo_link;
2c30dd74 994 }
c494dd6f 995
ad0b46e6 996 ret = 0;
2c30dd74 997 }
ad0b46e6 998 goto out_close;
2289be19 999
ad0b46e6
GK
1000err:
1001 ret = -1;
1002err_undo_link:
1003 unlinkat_preserve_errno(ndirfd, name, 0);
1004out_close:
1005 close_preserve_errno(ndirfd);
1006 close_preserve_errno(odirfd);
6dd4b1f1 1007out:
ad0b46e6
GK
1008 g_free(oname);
1009 g_free(odirpath);
4fa4ce71 1010 return ret;
8cf89e00
AL
1011}
1012
2289be19 1013static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
8cf89e00 1014{
ac125d99 1015 int fd, ret;
8cf89e00 1016
ac125d99
GK
1017 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
1018 if (fd == -1) {
1019 return -1;
2c30dd74 1020 }
ac125d99
GK
1021 ret = ftruncate(fd, size);
1022 close_preserve_errno(fd);
4fa4ce71 1023 return ret;
8cf89e00
AL
1024}
1025
2289be19 1026static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
8cf89e00 1027{
d369f207
GK
1028 char *dirpath = g_path_get_dirname(fs_path->data);
1029 char *name = g_path_get_basename(fs_path->data);
4fa4ce71 1030 int ret = -1;
d369f207
GK
1031 int dirfd;
1032
1033 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
1034 if (dirfd == -1) {
1035 goto out;
1036 }
2289be19 1037
c79ce737 1038 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
17b1971f
AK
1039 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
1040 (fs_ctx->export_flags & V9FS_SM_NONE)) {
d369f207
GK
1041 ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
1042 AT_SYMLINK_NOFOLLOW);
b97400ca 1043 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
d369f207 1044 ret = local_set_xattrat(dirfd, name, credp);
2c30dd74 1045 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
d369f207 1046 ret = local_set_mapped_file_attrat(dirfd, name, credp);
f7613bee 1047 }
d369f207
GK
1048
1049 close_preserve_errno(dirfd);
1050out:
1051 g_free(name);
1052 g_free(dirpath);
4fa4ce71 1053 return ret;
8cf89e00
AL
1054}
1055
2289be19 1056static int local_utimensat(FsContext *s, V9fsPath *fs_path,
38671423 1057 const struct timespec *buf)
8cf89e00 1058{
a33eda0d
GK
1059 char *dirpath = g_path_get_dirname(fs_path->data);
1060 char *name = g_path_get_basename(fs_path->data);
1061 int dirfd, ret = -1;
2289be19 1062
a33eda0d
GK
1063 dirfd = local_opendir_nofollow(s, dirpath);
1064 if (dirfd == -1) {
1065 goto out;
1066 }
2289be19 1067
a33eda0d
GK
1068 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1069 close_preserve_errno(dirfd);
1070out:
1071 g_free(dirpath);
1072 g_free(name);
4fa4ce71 1073 return ret;
8cf89e00
AL
1074}
1075
df4938a6
GK
1076static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1077 int flags)
5bae1900 1078{
df4938a6 1079 int ret = -1;
2c30dd74
AK
1080
1081 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
df4938a6
GK
1082 int map_dirfd;
1083
6a87e792
GK
1084 /* We need to remove the metadata as well:
1085 * - the metadata directory if we're removing a directory
1086 * - the metadata file in the parent's metadata directory
1087 *
1088 * If any of these are missing (ie, ENOENT) then we're probably
1089 * trying to remove something that wasn't created in mapped-file
1090 * mode. We just ignore the error.
1091 */
df4938a6
GK
1092 if (flags == AT_REMOVEDIR) {
1093 int fd;
1094
b003fc0d 1095 fd = openat_dir(dirfd, name);
df4938a6
GK
1096 if (fd == -1) {
1097 goto err_out;
1098 }
df4938a6
GK
1099 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1100 close_preserve_errno(fd);
1101 if (ret < 0 && errno != ENOENT) {
2c30dd74
AK
1102 goto err_out;
1103 }
1104 }
df4938a6 1105 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
6a87e792
GK
1106 if (map_dirfd != -1) {
1107 ret = unlinkat(map_dirfd, name, 0);
1108 close_preserve_errno(map_dirfd);
1109 if (ret < 0 && errno != ENOENT) {
1110 goto err_out;
1111 }
1112 } else if (errno != ENOENT) {
2c30dd74
AK
1113 goto err_out;
1114 }
1115 }
4fa4ce71 1116
df4938a6
GK
1117 ret = unlinkat(dirfd, name, flags);
1118err_out:
1119 return ret;
1120}
1121
5bae1900
AL
1122static int local_remove(FsContext *ctx, const char *path)
1123{
2c30dd74 1124 struct stat stbuf;
a0e640a8
GK
1125 char *dirpath = g_path_get_dirname(path);
1126 char *name = g_path_get_basename(path);
1127 int flags = 0;
1128 int dirfd;
1129 int err = -1;
2c30dd74 1130
a0e640a8 1131 dirfd = local_opendir_nofollow(ctx, dirpath);
b7361d46 1132 if (dirfd == -1) {
a0e640a8 1133 goto out;
2c30dd74 1134 }
4fa4ce71 1135
790db7ef 1136 if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
a0e640a8
GK
1137 goto err_out;
1138 }
1139
1140 if (S_ISDIR(stbuf.st_mode)) {
1141 flags |= AT_REMOVEDIR;
1142 }
1143
1144 err = local_unlinkat_common(ctx, dirfd, name, flags);
2c30dd74 1145err_out:
a0e640a8
GK
1146 close_preserve_errno(dirfd);
1147out:
1148 g_free(name);
1149 g_free(dirpath);
2c30dd74 1150 return err;
5bae1900
AL
1151}
1152
8b888272
AK
1153static int local_fsync(FsContext *ctx, int fid_type,
1154 V9fsFidOpenState *fs, int datasync)
8cf89e00 1155{
8b888272
AK
1156 int fd;
1157
1158 if (fid_type == P9_FID_DIR) {
f314ea4e 1159 fd = dirfd(fs->dir.stream);
8b888272
AK
1160 } else {
1161 fd = fs->fd;
1162 }
1163
49594973 1164 if (datasync) {
8b888272 1165 return qemu_fdatasync(fd);
49594973 1166 } else {
8b888272 1167 return fsync(fd);
49594973 1168 }
8cf89e00
AL
1169}
1170
2289be19 1171static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
be940c87 1172{
31e51d1c 1173 int fd, ret;
2289be19 1174
31e51d1c 1175 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
23da0145
GK
1176 if (fd == -1) {
1177 return -1;
1178 }
31e51d1c
GK
1179 ret = fstatfs(fd, stbuf);
1180 close_preserve_errno(fd);
4fa4ce71 1181 return ret;
be940c87
MK
1182}
1183
2289be19 1184static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1185 const char *name, void *value, size_t size)
1186{
2289be19
AK
1187 char *path = fs_path->data;
1188
fc22118d 1189 return v9fs_get_xattr(ctx, path, name, value, size);
fa32ef88
AK
1190}
1191
2289be19 1192static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1193 void *value, size_t size)
1194{
2289be19
AK
1195 char *path = fs_path->data;
1196
fc22118d 1197 return v9fs_list_xattr(ctx, path, value, size);
fa32ef88
AK
1198}
1199
2289be19 1200static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
10b468bd
AK
1201 void *value, size_t size, int flags)
1202{
2289be19
AK
1203 char *path = fs_path->data;
1204
fc22118d 1205 return v9fs_set_xattr(ctx, path, name, value, size, flags);
10b468bd
AK
1206}
1207
2289be19
AK
1208static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1209 const char *name)
9ed3ef26 1210{
2289be19
AK
1211 char *path = fs_path->data;
1212
fc22118d 1213 return v9fs_remove_xattr(ctx, path, name);
9ed3ef26
AK
1214}
1215
2289be19
AK
1216static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1217 const char *name, V9fsPath *target)
1218{
7a95434e
GK
1219 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1220 local_is_mapped_file_metadata(ctx, name)) {
1221 errno = EINVAL;
1222 return -1;
1223 }
1224
2289be19 1225 if (dir_path) {
f57f5878
GK
1226 if (!strcmp(name, ".")) {
1227 /* "." relative to "foo/bar" is "foo/bar" */
1228 v9fs_path_copy(target, dir_path);
1229 } else if (!strcmp(name, "..")) {
1230 if (!strcmp(dir_path->data, ".")) {
1231 /* ".." relative to the root is "." */
1232 v9fs_path_sprintf(target, ".");
1233 } else {
1234 char *tmp = g_path_get_dirname(dir_path->data);
1235 /* Symbolic links are resolved by the client. We can assume
1236 * that ".." relative to "foo/bar" is equivalent to "foo"
1237 */
1238 v9fs_path_sprintf(target, "%s", tmp);
1239 g_free(tmp);
1240 }
1241 } else {
1242 assert(!strchr(name, '/'));
1243 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1244 }
1245 } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
1246 !strcmp(name, "..")) {
1247 /* This is the root fid */
1248 v9fs_path_sprintf(target, ".");
9c6b899f 1249 } else {
f57f5878
GK
1250 assert(!strchr(name, '/'));
1251 v9fs_path_sprintf(target, "./%s", name);
2289be19 1252 }
2289be19
AK
1253 return 0;
1254}
1255
1256static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1257 const char *old_name, V9fsPath *newdir,
1258 const char *new_name)
1259{
1260 int ret;
99f2cf4b 1261 int odirfd, ndirfd;
2289be19 1262
7a95434e
GK
1263 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1264 (local_is_mapped_file_metadata(ctx, old_name) ||
1265 local_is_mapped_file_metadata(ctx, new_name))) {
1266 errno = EINVAL;
1267 return -1;
1268 }
1269
99f2cf4b
GK
1270 odirfd = local_opendir_nofollow(ctx, olddir->data);
1271 if (odirfd == -1) {
1272 return -1;
1273 }
2289be19 1274
99f2cf4b
GK
1275 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1276 if (ndirfd == -1) {
1277 close_preserve_errno(odirfd);
1278 return -1;
1279 }
2289be19 1280
99f2cf4b
GK
1281 ret = renameat(odirfd, old_name, ndirfd, new_name);
1282 if (ret < 0) {
1283 goto out;
1284 }
2289be19 1285
99f2cf4b
GK
1286 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1287 int omap_dirfd, nmap_dirfd;
2289be19 1288
99f2cf4b
GK
1289 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1290 if (ret < 0 && errno != EEXIST) {
1291 goto err_undo_rename;
1292 }
2289be19 1293
6dd4b1f1 1294 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
99f2cf4b
GK
1295 if (omap_dirfd == -1) {
1296 goto err;
1297 }
1298
6dd4b1f1 1299 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
99f2cf4b
GK
1300 if (nmap_dirfd == -1) {
1301 close_preserve_errno(omap_dirfd);
1302 goto err;
1303 }
1304
1305 /* rename the .virtfs_metadata files */
1306 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1307 close_preserve_errno(nmap_dirfd);
1308 close_preserve_errno(omap_dirfd);
1309 if (ret < 0 && errno != ENOENT) {
1310 goto err_undo_rename;
1311 }
1312
1313 ret = 0;
1314 }
1315 goto out;
1316
1317err:
1318 ret = -1;
1319err_undo_rename:
1320 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1321out:
1322 close_preserve_errno(ndirfd);
1323 close_preserve_errno(odirfd);
2289be19
AK
1324 return ret;
1325}
1326
d2767ede
GK
1327static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1328{
1329 path->data = g_path_get_dirname(str);
1330 path->size = strlen(path->data) + 1;
1331}
1332
1333static int local_rename(FsContext *ctx, const char *oldpath,
1334 const char *newpath)
1335{
1336 int err;
1337 char *oname = g_path_get_basename(oldpath);
1338 char *nname = g_path_get_basename(newpath);
1339 V9fsPath olddir, newdir;
1340
1341 v9fs_path_init_dirname(&olddir, oldpath);
1342 v9fs_path_init_dirname(&newdir, newpath);
1343
1344 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1345
1346 v9fs_path_free(&newdir);
1347 v9fs_path_free(&olddir);
1348 g_free(nname);
1349 g_free(oname);
1350
1351 return err;
1352}
1353
2289be19
AK
1354static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1355 const char *name, int flags)
1356{
1357 int ret;
df4938a6 1358 int dirfd;
2289be19 1359
7a95434e
GK
1360 if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
1361 local_is_mapped_file_metadata(ctx, name)) {
1362 errno = EINVAL;
1363 return -1;
1364 }
1365
df4938a6
GK
1366 dirfd = local_opendir_nofollow(ctx, dir->data);
1367 if (dirfd == -1) {
1368 return -1;
2c30dd74 1369 }
2289be19 1370
df4938a6
GK
1371 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1372 close_preserve_errno(dirfd);
2289be19
AK
1373 return ret;
1374}
9ed3ef26 1375
e06a765e
HPB
1376static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1377 mode_t st_mode, uint64_t *st_gen)
1378{
ae0f940e 1379#ifdef FS_IOC_GETVERSION
0e5fc994 1380 int err;
cc720ddb
AK
1381 V9fsFidOpenState fid_open;
1382
e06a765e
HPB
1383 /*
1384 * Do not try to open special files like device nodes, fifos etc
1385 * We can get fd for regular files and directories only
1386 */
1387 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1a9978a5
KS
1388 errno = ENOTTY;
1389 return -1;
e06a765e 1390 }
cc720ddb
AK
1391 err = local_open(ctx, path, O_RDONLY, &fid_open);
1392 if (err < 0) {
1393 return err;
e06a765e 1394 }
cc720ddb
AK
1395 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1396 local_close(ctx, &fid_open);
0e5fc994 1397 return err;
ae0f940e 1398#else
0e5fc994
KS
1399 errno = ENOTTY;
1400 return -1;
ae0f940e 1401#endif
e06a765e
HPB
1402}
1403
65603a80 1404static int local_init(FsContext *ctx, Error **errp)
0174fe73 1405{
e06a765e 1406 struct statfs stbuf;
0e35a378
GK
1407 LocalData *data = g_malloc(sizeof(*data));
1408
1409 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1410 if (data->mountfd == -1) {
65603a80 1411 error_setg_errno(errp, errno, "failed to open '%s'", ctx->fs_root);
0e35a378
GK
1412 goto err;
1413 }
e06a765e 1414
00c90bd1
GK
1415#ifdef FS_IOC_GETVERSION
1416 /*
1417 * use ioc_getversion only if the ioctl is definied
1418 */
0e35a378
GK
1419 if (fstatfs(data->mountfd, &stbuf) < 0) {
1420 close_preserve_errno(data->mountfd);
1421 goto err;
00c90bd1
GK
1422 }
1423 switch (stbuf.f_type) {
1424 case EXT2_SUPER_MAGIC:
1425 case BTRFS_SUPER_MAGIC:
1426 case REISERFS_SUPER_MAGIC:
1427 case XFS_SUPER_MAGIC:
1428 ctx->exops.get_st_gen = local_ioc_getversion;
1429 break;
1430 }
1431#endif
e06a765e 1432
2c30dd74
AK
1433 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1434 ctx->xops = passthrough_xattr_ops;
1435 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1436 ctx->xops = mapped_xattr_ops;
1437 } else if (ctx->export_flags & V9FS_SM_NONE) {
1438 ctx->xops = none_xattr_ops;
1439 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1440 /*
1441 * xattr operation for mapped-file and passthrough
1442 * remain same.
1443 */
1444 ctx->xops = passthrough_xattr_ops;
1445 }
c98f1d4a 1446 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
00c90bd1 1447
0e35a378 1448 ctx->private = data;
00c90bd1 1449 return 0;
0e35a378
GK
1450
1451err:
1452 g_free(data);
1453 return -1;
1454}
1455
1456static void local_cleanup(FsContext *ctx)
1457{
1458 LocalData *data = ctx->private;
1459
1460 close(data->mountfd);
1461 g_free(data);
0174fe73
AK
1462}
1463
91cda4e8
GK
1464static void error_append_security_model_hint(Error **errp)
1465{
1466 error_append_hint(errp, "Valid options are: security_model="
1467 "[passthrough|mapped-xattr|mapped-file|none]\n");
1468}
1469
1470static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
99519f0a
AK
1471{
1472 const char *sec_model = qemu_opt_get(opts, "security_model");
1473 const char *path = qemu_opt_get(opts, "path");
91cda4e8 1474 Error *local_err = NULL;
99519f0a
AK
1475
1476 if (!sec_model) {
91cda4e8
GK
1477 error_setg(errp, "security_model property not set");
1478 error_append_security_model_hint(errp);
99519f0a
AK
1479 return -1;
1480 }
1481
1482 if (!strcmp(sec_model, "passthrough")) {
1483 fse->export_flags |= V9FS_SM_PASSTHROUGH;
2c30dd74
AK
1484 } else if (!strcmp(sec_model, "mapped") ||
1485 !strcmp(sec_model, "mapped-xattr")) {
99519f0a
AK
1486 fse->export_flags |= V9FS_SM_MAPPED;
1487 } else if (!strcmp(sec_model, "none")) {
1488 fse->export_flags |= V9FS_SM_NONE;
2c30dd74
AK
1489 } else if (!strcmp(sec_model, "mapped-file")) {
1490 fse->export_flags |= V9FS_SM_MAPPED_FILE;
99519f0a 1491 } else {
91cda4e8
GK
1492 error_setg(errp, "invalid security_model property '%s'", sec_model);
1493 error_append_security_model_hint(errp);
99519f0a
AK
1494 return -1;
1495 }
1496
1497 if (!path) {
91cda4e8 1498 error_setg(errp, "path property not set");
99519f0a
AK
1499 return -1;
1500 }
b8bbdb88 1501
91cda4e8
GK
1502 fsdev_throttle_parse_opts(opts, &fse->fst, &local_err);
1503 if (local_err) {
1504 error_propagate(errp, local_err);
1505 error_prepend(errp, "invalid throttle configuration: ");
b8bbdb88
PJ
1506 return -1;
1507 }
1508
b96feb2c
TS
1509 if (fse->export_flags & V9FS_SM_MAPPED ||
1510 fse->export_flags & V9FS_SM_MAPPED_FILE) {
1511 fse->fmode =
1512 qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
1513 fse->dmode =
1514 qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
1515 } else {
1516 if (qemu_opt_find(opts, "fmode")) {
91cda4e8 1517 error_setg(errp, "fmode is only valid for mapped security modes");
b96feb2c
TS
1518 return -1;
1519 }
1520 if (qemu_opt_find(opts, "dmode")) {
91cda4e8 1521 error_setg(errp, "dmode is only valid for mapped security modes");
b96feb2c
TS
1522 return -1;
1523 }
1524 }
1525
99519f0a
AK
1526 fse->path = g_strdup(path);
1527
1528 return 0;
1529}
1530
9f107513 1531FileOperations local_ops = {
99519f0a 1532 .parse_opts = local_parse_opts,
0174fe73 1533 .init = local_init,
0e35a378 1534 .cleanup = local_cleanup,
131dcb25 1535 .lstat = local_lstat,
131dcb25
AL
1536 .readlink = local_readlink,
1537 .close = local_close,
1538 .closedir = local_closedir,
a6568fe2
AL
1539 .open = local_open,
1540 .opendir = local_opendir,
a9231555
AL
1541 .rewinddir = local_rewinddir,
1542 .telldir = local_telldir,
635324e8 1543 .readdir = local_readdir,
a9231555 1544 .seekdir = local_seekdir,
56d15a53
SG
1545 .preadv = local_preadv,
1546 .pwritev = local_pwritev,
c494dd6f
AL
1547 .chmod = local_chmod,
1548 .mknod = local_mknod,
c494dd6f
AL
1549 .mkdir = local_mkdir,
1550 .fstat = local_fstat,
1551 .open2 = local_open2,
1552 .symlink = local_symlink,
1553 .link = local_link,
8cf89e00
AL
1554 .truncate = local_truncate,
1555 .rename = local_rename,
1556 .chown = local_chown,
74bc02b2 1557 .utimensat = local_utimensat,
5bae1900 1558 .remove = local_remove,
8cf89e00 1559 .fsync = local_fsync,
be940c87 1560 .statfs = local_statfs,
fa32ef88
AK
1561 .lgetxattr = local_lgetxattr,
1562 .llistxattr = local_llistxattr,
10b468bd 1563 .lsetxattr = local_lsetxattr,
9ed3ef26 1564 .lremovexattr = local_lremovexattr,
2289be19
AK
1565 .name_to_path = local_name_to_path,
1566 .renameat = local_renameat,
1567 .unlinkat = local_unlinkat,
9f107513 1568};