]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-local.c
9pfs: rename virtio-9p-handle.c to 9p-handle.c
[mirror_qemu.git] / hw / 9pfs / virtio-9p-local.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p Posix callback
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.
11 *
12 */
873c3213 13
0d09e41a 14#include "hw/virtio/virtio.h"
9f107513 15#include "virtio-9p.h"
fc22118d 16#include "virtio-9p-xattr.h"
69b15212 17#include "fsdev/qemu-fsdev.h" /* local_ops */
c494dd6f 18#include <arpa/inet.h>
131dcb25
AL
19#include <pwd.h>
20#include <grp.h>
c494dd6f
AL
21#include <sys/socket.h>
22#include <sys/un.h>
1de7afc9 23#include "qemu/xattr.h"
2c30dd74 24#include <libgen.h>
e06a765e
HPB
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
131dcb25 43
2c30dd74
AK
44#define VIRTFS_META_DIR ".virtfs_metadata"
45
4fa4ce71 46static char *local_mapped_attr_path(FsContext *ctx, const char *path)
2c30dd74 47{
1b6f85e2
MT
48 int dirlen;
49 const char *name = strrchr(path, '/');
50 if (name) {
51 dirlen = name - path;
52 ++name;
53 } else {
54 name = path;
55 dirlen = 0;
56 }
57 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
58 dirlen, path, VIRTFS_META_DIR, name);
2c30dd74
AK
59}
60
0ceb092e
AK
61static FILE *local_fopen(const char *path, const char *mode)
62{
63 int fd, o_mode = 0;
64 FILE *fp;
65 int flags = O_NOFOLLOW;
66 /*
67 * only supports two modes
68 */
69 if (mode[0] == 'r') {
70 flags |= O_RDONLY;
71 } else if (mode[0] == 'w') {
72 flags |= O_WRONLY | O_TRUNC | O_CREAT;
73 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
74 } else {
75 return NULL;
76 }
77 fd = open(path, flags, o_mode);
78 if (fd == -1) {
79 return NULL;
80 }
81 fp = fdopen(fd, mode);
82 if (!fp) {
83 close(fd);
84 }
85 return fp;
86}
87
2c30dd74
AK
88#define ATTR_MAX 100
89static void local_mapped_file_attr(FsContext *ctx, const char *path,
90 struct stat *stbuf)
91{
92 FILE *fp;
93 char buf[ATTR_MAX];
4fa4ce71 94 char *attr_path;
2c30dd74 95
4fa4ce71 96 attr_path = local_mapped_attr_path(ctx, path);
0ceb092e 97 fp = local_fopen(attr_path, "r");
4fa4ce71 98 g_free(attr_path);
2c30dd74
AK
99 if (!fp) {
100 return;
101 }
102 memset(buf, 0, ATTR_MAX);
103 while (fgets(buf, ATTR_MAX, fp)) {
104 if (!strncmp(buf, "virtfs.uid", 10)) {
105 stbuf->st_uid = atoi(buf+11);
106 } else if (!strncmp(buf, "virtfs.gid", 10)) {
107 stbuf->st_gid = atoi(buf+11);
108 } else if (!strncmp(buf, "virtfs.mode", 11)) {
109 stbuf->st_mode = atoi(buf+12);
110 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
111 stbuf->st_rdev = atoi(buf+12);
112 }
113 memset(buf, 0, ATTR_MAX);
114 }
115 fclose(fp);
116}
117
2289be19 118static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
131dcb25 119{
1237ad76 120 int err;
4fa4ce71 121 char *buffer;
2289be19
AK
122 char *path = fs_path->data;
123
4fa4ce71
CG
124 buffer = rpath(fs_ctx, path);
125 err = lstat(buffer, stbuf);
1237ad76 126 if (err) {
4fa4ce71 127 goto err_out;
1237ad76 128 }
b97400ca 129 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1237ad76
VJ
130 /* Actual credentials are part of extended attrs */
131 uid_t tmp_uid;
132 gid_t tmp_gid;
133 mode_t tmp_mode;
134 dev_t tmp_dev;
4fa4ce71 135 if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
f8ad4a89 136 stbuf->st_uid = le32_to_cpu(tmp_uid);
1237ad76 137 }
4fa4ce71 138 if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
f8ad4a89 139 stbuf->st_gid = le32_to_cpu(tmp_gid);
1237ad76 140 }
4fa4ce71 141 if (getxattr(buffer, "user.virtfs.mode",
faa44e3d 142 &tmp_mode, sizeof(mode_t)) > 0) {
f8ad4a89 143 stbuf->st_mode = le32_to_cpu(tmp_mode);
1237ad76 144 }
4fa4ce71 145 if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
f8ad4a89 146 stbuf->st_rdev = le64_to_cpu(tmp_dev);
1237ad76 147 }
2c30dd74
AK
148 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
149 local_mapped_file_attr(fs_ctx, path, stbuf);
150 }
4fa4ce71
CG
151
152err_out:
153 g_free(buffer);
2c30dd74
AK
154 return err;
155}
156
157static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
158{
159 int err;
4fa4ce71 160 char *attr_dir;
d3f8e138 161 char *tmp_path = g_strdup(path);
2c30dd74 162
4fa4ce71 163 attr_dir = g_strdup_printf("%s/%s/%s",
2c30dd74
AK
164 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
165
166 err = mkdir(attr_dir, 0700);
167 if (err < 0 && errno == EEXIST) {
168 err = 0;
1237ad76 169 }
4fa4ce71 170 g_free(attr_dir);
d3f8e138 171 g_free(tmp_path);
1237ad76 172 return err;
131dcb25
AL
173}
174
2c30dd74
AK
175static int local_set_mapped_file_attr(FsContext *ctx,
176 const char *path, FsCred *credp)
177{
178 FILE *fp;
179 int ret = 0;
180 char buf[ATTR_MAX];
4fa4ce71 181 char *attr_path;
2c30dd74
AK
182 int uid = -1, gid = -1, mode = -1, rdev = -1;
183
4fa4ce71
CG
184 attr_path = local_mapped_attr_path(ctx, path);
185 fp = local_fopen(attr_path, "r");
2c30dd74
AK
186 if (!fp) {
187 goto create_map_file;
188 }
189 memset(buf, 0, ATTR_MAX);
190 while (fgets(buf, ATTR_MAX, fp)) {
191 if (!strncmp(buf, "virtfs.uid", 10)) {
192 uid = atoi(buf+11);
193 } else if (!strncmp(buf, "virtfs.gid", 10)) {
194 gid = atoi(buf+11);
195 } else if (!strncmp(buf, "virtfs.mode", 11)) {
196 mode = atoi(buf+12);
197 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
198 rdev = atoi(buf+12);
199 }
200 memset(buf, 0, ATTR_MAX);
201 }
202 fclose(fp);
203 goto update_map_file;
204
205create_map_file:
206 ret = local_create_mapped_attr_dir(ctx, path);
207 if (ret < 0) {
208 goto err_out;
209 }
210
211update_map_file:
0ceb092e 212 fp = local_fopen(attr_path, "w");
2c30dd74
AK
213 if (!fp) {
214 ret = -1;
215 goto err_out;
216 }
217
218 if (credp->fc_uid != -1) {
219 uid = credp->fc_uid;
220 }
221 if (credp->fc_gid != -1) {
222 gid = credp->fc_gid;
223 }
224 if (credp->fc_mode != -1) {
225 mode = credp->fc_mode;
226 }
227 if (credp->fc_rdev != -1) {
228 rdev = credp->fc_rdev;
229 }
230
231
232 if (uid != -1) {
233 fprintf(fp, "virtfs.uid=%d\n", uid);
234 }
235 if (gid != -1) {
236 fprintf(fp, "virtfs.gid=%d\n", gid);
237 }
238 if (mode != -1) {
239 fprintf(fp, "virtfs.mode=%d\n", mode);
240 }
241 if (rdev != -1) {
242 fprintf(fp, "virtfs.rdev=%d\n", rdev);
243 }
244 fclose(fp);
245
246err_out:
4fa4ce71 247 g_free(attr_path);
2c30dd74
AK
248 return ret;
249}
250
758e8e38 251static int local_set_xattr(const char *path, FsCred *credp)
131dcb25 252{
758e8e38 253 int err;
2289be19 254
758e8e38 255 if (credp->fc_uid != -1) {
f8ad4a89
AK
256 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
257 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
758e8e38
VJ
258 if (err) {
259 return err;
260 }
131dcb25 261 }
758e8e38 262 if (credp->fc_gid != -1) {
f8ad4a89
AK
263 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
264 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
758e8e38
VJ
265 if (err) {
266 return err;
267 }
131dcb25 268 }
758e8e38 269 if (credp->fc_mode != -1) {
f8ad4a89
AK
270 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
271 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
758e8e38
VJ
272 if (err) {
273 return err;
274 }
131dcb25 275 }
758e8e38 276 if (credp->fc_rdev != -1) {
f8ad4a89
AK
277 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
278 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
758e8e38
VJ
279 if (err) {
280 return err;
281 }
131dcb25 282 }
131dcb25
AL
283 return 0;
284}
285
4750a96f 286static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
2289be19 287 FsCred *credp)
4750a96f 288{
4fa4ce71 289 char *buffer;
2289be19 290
4fa4ce71
CG
291 buffer = rpath(fs_ctx, path);
292 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
12848bfc
AK
293 /*
294 * If we fail to change ownership and if we are
295 * using security model none. Ignore the error
296 */
b97400ca 297 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
4fa4ce71 298 goto err;
12848bfc 299 }
4750a96f 300 }
2d40564a 301
4fa4ce71
CG
302 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
303 goto err;
2d40564a 304 }
4fa4ce71
CG
305
306 g_free(buffer);
4750a96f 307 return 0;
4fa4ce71
CG
308err:
309 g_free(buffer);
310 return -1;
4750a96f
VJ
311}
312
2289be19
AK
313static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
314 char *buf, size_t bufsz)
131dcb25 315{
879c2813 316 ssize_t tsize = -1;
4fa4ce71 317 char *buffer;
2289be19
AK
318 char *path = fs_path->data;
319
2c30dd74
AK
320 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
321 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
879c2813 322 int fd;
4fa4ce71
CG
323 buffer = rpath(fs_ctx, path);
324 fd = open(buffer, O_RDONLY | O_NOFOLLOW);
325 g_free(buffer);
879c2813
VJ
326 if (fd == -1) {
327 return -1;
328 }
329 do {
330 tsize = read(fd, (void *)buf, bufsz);
331 } while (tsize == -1 && errno == EINTR);
332 close(fd);
b97400ca
AK
333 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
334 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
335 buffer = rpath(fs_ctx, path);
336 tsize = readlink(buffer, buf, bufsz);
337 g_free(buffer);
879c2813
VJ
338 }
339 return tsize;
131dcb25
AL
340}
341
cc720ddb 342static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 343{
cc720ddb 344 return close(fs->fd);
131dcb25
AL
345}
346
cc720ddb 347static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 348{
cc720ddb 349 return closedir(fs->dir);
131dcb25 350}
9f107513 351
cc720ddb
AK
352static int local_open(FsContext *ctx, V9fsPath *fs_path,
353 int flags, V9fsFidOpenState *fs)
a6568fe2 354{
4fa4ce71 355 char *buffer;
2289be19
AK
356 char *path = fs_path->data;
357
4fa4ce71
CG
358 buffer = rpath(ctx, path);
359 fs->fd = open(buffer, flags | O_NOFOLLOW);
360 g_free(buffer);
cc720ddb 361 return fs->fd;
a6568fe2
AL
362}
363
cc720ddb
AK
364static int local_opendir(FsContext *ctx,
365 V9fsPath *fs_path, V9fsFidOpenState *fs)
a6568fe2 366{
4fa4ce71 367 char *buffer;
2289be19
AK
368 char *path = fs_path->data;
369
4fa4ce71
CG
370 buffer = rpath(ctx, path);
371 fs->dir = opendir(buffer);
372 g_free(buffer);
cc720ddb
AK
373 if (!fs->dir) {
374 return -1;
375 }
376 return 0;
a6568fe2
AL
377}
378
cc720ddb 379static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 380{
0289a412 381 rewinddir(fs->dir);
a9231555
AL
382}
383
cc720ddb 384static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 385{
cc720ddb 386 return telldir(fs->dir);
a9231555
AL
387}
388
cc720ddb
AK
389static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
390 struct dirent *entry,
2289be19 391 struct dirent **result)
a9231555 392{
2c30dd74
AK
393 int ret;
394
395again:
396 ret = readdir_r(fs->dir, entry, result);
840a1bf2
BB
397 if (ctx->export_flags & V9FS_SM_MAPPED) {
398 entry->d_type = DT_UNKNOWN;
399 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
2c30dd74
AK
400 if (!ret && *result != NULL &&
401 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
402 /* skp the meta data directory */
403 goto again;
404 }
840a1bf2 405 entry->d_type = DT_UNKNOWN;
2c30dd74
AK
406 }
407 return ret;
a9231555
AL
408}
409
cc720ddb 410static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
a9231555 411{
0289a412 412 seekdir(fs->dir, off);
a9231555
AL
413}
414
cc720ddb
AK
415static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
416 const struct iovec *iov,
56d15a53 417 int iovcnt, off_t offset)
a9231555 418{
56d15a53 419#ifdef CONFIG_PREADV
cc720ddb 420 return preadv(fs->fd, iov, iovcnt, offset);
56d15a53 421#else
cc720ddb 422 int err = lseek(fs->fd, offset, SEEK_SET);
56d15a53
SG
423 if (err == -1) {
424 return err;
425 } else {
cc720ddb 426 return readv(fs->fd, iov, iovcnt);
56d15a53
SG
427 }
428#endif
a9231555
AL
429}
430
cc720ddb
AK
431static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
432 const struct iovec *iov,
2289be19 433 int iovcnt, off_t offset)
8449360c 434{
d3ab98e6
AK
435 ssize_t ret
436;
56d15a53 437#ifdef CONFIG_PREADV
cc720ddb 438 ret = pwritev(fs->fd, iov, iovcnt, offset);
56d15a53 439#else
cc720ddb 440 int err = lseek(fs->fd, offset, SEEK_SET);
56d15a53
SG
441 if (err == -1) {
442 return err;
443 } else {
cc720ddb 444 ret = writev(fs->fd, iov, iovcnt);
56d15a53
SG
445 }
446#endif
d3ab98e6
AK
447#ifdef CONFIG_SYNC_FILE_RANGE
448 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
449 /*
450 * Initiate a writeback. This is not a data integrity sync.
451 * We want to ensure that we don't leave dirty pages in the cache
452 * after write when writeout=immediate is sepcified.
453 */
cc720ddb 454 sync_file_range(fs->fd, offset, ret,
d3ab98e6
AK
455 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
456 }
457#endif
458 return ret;
8449360c
AL
459}
460
2289be19 461static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
c494dd6f 462{
4fa4ce71
CG
463 char *buffer;
464 int ret = -1;
2289be19
AK
465 char *path = fs_path->data;
466
b97400ca 467 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
468 buffer = rpath(fs_ctx, path);
469 ret = local_set_xattr(buffer, credp);
470 g_free(buffer);
2c30dd74
AK
471 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
472 return local_set_mapped_file_attr(fs_ctx, path, credp);
b97400ca
AK
473 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
474 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
475 buffer = rpath(fs_ctx, path);
476 ret = chmod(buffer, credp->fc_mode);
477 g_free(buffer);
e95ead32 478 }
4fa4ce71 479 return ret;
c494dd6f
AL
480}
481
2289be19
AK
482static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
483 const char *name, FsCred *credp)
c494dd6f 484{
2289be19 485 char *path;
1c293312
VJ
486 int err = -1;
487 int serrno = 0;
2289be19 488 V9fsString fullname;
4ed7b2c3 489 char *buffer = NULL;
1c293312 490
2289be19
AK
491 v9fs_string_init(&fullname);
492 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
493 path = fullname.data;
494
1c293312 495 /* Determine the security model */
b97400ca 496 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
497 buffer = rpath(fs_ctx, path);
498 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
1c293312 499 if (err == -1) {
2289be19 500 goto out;
1c293312 501 }
4fa4ce71 502 err = local_set_xattr(buffer, credp);
1c293312
VJ
503 if (err == -1) {
504 serrno = errno;
505 goto err_end;
506 }
2c30dd74
AK
507 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
508
4fa4ce71
CG
509 buffer = rpath(fs_ctx, path);
510 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
2c30dd74
AK
511 if (err == -1) {
512 goto out;
513 }
514 err = local_set_mapped_file_attr(fs_ctx, path, credp);
515 if (err == -1) {
516 serrno = errno;
517 goto err_end;
518 }
b97400ca
AK
519 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
520 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
521 buffer = rpath(fs_ctx, path);
522 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
1c293312 523 if (err == -1) {
2289be19 524 goto out;
1c293312
VJ
525 }
526 err = local_post_create_passthrough(fs_ctx, path, credp);
527 if (err == -1) {
528 serrno = errno;
529 goto err_end;
530 }
531 }
2289be19 532 goto out;
1c293312
VJ
533
534err_end:
4fa4ce71 535 remove(buffer);
1c293312 536 errno = serrno;
2289be19 537out:
4ed7b2c3 538 g_free(buffer);
2289be19 539 v9fs_string_free(&fullname);
1c293312 540 return err;
c494dd6f
AL
541}
542
2289be19
AK
543static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
544 const char *name, FsCred *credp)
c494dd6f 545{
2289be19 546 char *path;
00ec5c37
VJ
547 int err = -1;
548 int serrno = 0;
2289be19 549 V9fsString fullname;
4ed7b2c3 550 char *buffer = NULL;
00ec5c37 551
2289be19
AK
552 v9fs_string_init(&fullname);
553 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
554 path = fullname.data;
555
00ec5c37 556 /* Determine the security model */
b97400ca 557 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
558 buffer = rpath(fs_ctx, path);
559 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
00ec5c37 560 if (err == -1) {
2289be19 561 goto out;
00ec5c37
VJ
562 }
563 credp->fc_mode = credp->fc_mode|S_IFDIR;
4fa4ce71 564 err = local_set_xattr(buffer, credp);
00ec5c37
VJ
565 if (err == -1) {
566 serrno = errno;
567 goto err_end;
568 }
2c30dd74 569 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4fa4ce71
CG
570 buffer = rpath(fs_ctx, path);
571 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
2c30dd74
AK
572 if (err == -1) {
573 goto out;
574 }
575 credp->fc_mode = credp->fc_mode|S_IFDIR;
576 err = local_set_mapped_file_attr(fs_ctx, path, credp);
577 if (err == -1) {
578 serrno = errno;
579 goto err_end;
580 }
b97400ca
AK
581 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
582 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
583 buffer = rpath(fs_ctx, path);
584 err = mkdir(buffer, credp->fc_mode);
00ec5c37 585 if (err == -1) {
2289be19 586 goto out;
00ec5c37
VJ
587 }
588 err = local_post_create_passthrough(fs_ctx, path, credp);
589 if (err == -1) {
590 serrno = errno;
591 goto err_end;
592 }
593 }
2289be19 594 goto out;
00ec5c37
VJ
595
596err_end:
4fa4ce71 597 remove(buffer);
00ec5c37 598 errno = serrno;
2289be19 599out:
4ed7b2c3 600 g_free(buffer);
2289be19 601 v9fs_string_free(&fullname);
00ec5c37 602 return err;
c494dd6f
AL
603}
604
8b888272 605static int local_fstat(FsContext *fs_ctx, int fid_type,
cc720ddb 606 V9fsFidOpenState *fs, struct stat *stbuf)
c494dd6f 607{
8b888272
AK
608 int err, fd;
609
610 if (fid_type == P9_FID_DIR) {
611 fd = dirfd(fs->dir);
612 } else {
613 fd = fs->fd;
614 }
615
616 err = fstat(fd, stbuf);
1237ad76
VJ
617 if (err) {
618 return err;
619 }
b97400ca 620 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1237ad76
VJ
621 /* Actual credentials are part of extended attrs */
622 uid_t tmp_uid;
623 gid_t tmp_gid;
624 mode_t tmp_mode;
625 dev_t tmp_dev;
626
f8ad4a89
AK
627 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
628 stbuf->st_uid = le32_to_cpu(tmp_uid);
1237ad76 629 }
f8ad4a89
AK
630 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
631 stbuf->st_gid = le32_to_cpu(tmp_gid);
1237ad76 632 }
f8ad4a89
AK
633 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
634 stbuf->st_mode = le32_to_cpu(tmp_mode);
1237ad76 635 }
f8ad4a89
AK
636 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
637 stbuf->st_rdev = le64_to_cpu(tmp_dev);
1237ad76 638 }
2c30dd74
AK
639 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
640 errno = EOPNOTSUPP;
641 return -1;
1237ad76
VJ
642 }
643 return err;
c494dd6f
AL
644}
645
2289be19 646static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
cc720ddb 647 int flags, FsCred *credp, V9fsFidOpenState *fs)
c494dd6f 648{
2289be19 649 char *path;
4750a96f
VJ
650 int fd = -1;
651 int err = -1;
652 int serrno = 0;
2289be19 653 V9fsString fullname;
4ed7b2c3 654 char *buffer = NULL;
4750a96f 655
0ceb092e
AK
656 /*
657 * Mark all the open to not follow symlinks
658 */
659 flags |= O_NOFOLLOW;
660
2289be19
AK
661 v9fs_string_init(&fullname);
662 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
663 path = fullname.data;
664
4750a96f 665 /* Determine the security model */
b97400ca 666 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
667 buffer = rpath(fs_ctx, path);
668 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
4750a96f 669 if (fd == -1) {
2289be19
AK
670 err = fd;
671 goto out;
4750a96f
VJ
672 }
673 credp->fc_mode = credp->fc_mode|S_IFREG;
674 /* Set cleint credentials in xattr */
4fa4ce71 675 err = local_set_xattr(buffer, credp);
4750a96f
VJ
676 if (err == -1) {
677 serrno = errno;
678 goto err_end;
679 }
2c30dd74 680 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4fa4ce71
CG
681 buffer = rpath(fs_ctx, path);
682 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
2c30dd74
AK
683 if (fd == -1) {
684 err = fd;
685 goto out;
686 }
687 credp->fc_mode = credp->fc_mode|S_IFREG;
688 /* Set client credentials in .virtfs_metadata directory files */
689 err = local_set_mapped_file_attr(fs_ctx, path, credp);
690 if (err == -1) {
691 serrno = errno;
692 goto err_end;
693 }
b97400ca
AK
694 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
695 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
696 buffer = rpath(fs_ctx, path);
697 fd = open(buffer, flags, credp->fc_mode);
4750a96f 698 if (fd == -1) {
2289be19
AK
699 err = fd;
700 goto out;
4750a96f
VJ
701 }
702 err = local_post_create_passthrough(fs_ctx, path, credp);
703 if (err == -1) {
704 serrno = errno;
705 goto err_end;
706 }
707 }
2289be19 708 err = fd;
cc720ddb 709 fs->fd = fd;
2289be19 710 goto out;
4750a96f
VJ
711
712err_end:
713 close(fd);
4fa4ce71 714 remove(buffer);
4750a96f 715 errno = serrno;
2289be19 716out:
4ed7b2c3 717 g_free(buffer);
2289be19 718 v9fs_string_free(&fullname);
4750a96f 719 return err;
c494dd6f
AL
720}
721
758e8e38 722
879c2813 723static int local_symlink(FsContext *fs_ctx, const char *oldpath,
2289be19 724 V9fsPath *dir_path, const char *name, FsCred *credp)
c494dd6f 725{
879c2813
VJ
726 int err = -1;
727 int serrno = 0;
2289be19
AK
728 char *newpath;
729 V9fsString fullname;
4ed7b2c3 730 char *buffer = NULL;
879c2813 731
2289be19
AK
732 v9fs_string_init(&fullname);
733 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
734 newpath = fullname.data;
735
879c2813 736 /* Determine the security model */
b97400ca 737 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
879c2813
VJ
738 int fd;
739 ssize_t oldpath_size, write_size;
4fa4ce71
CG
740 buffer = rpath(fs_ctx, newpath);
741 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
879c2813 742 if (fd == -1) {
2289be19
AK
743 err = fd;
744 goto out;
879c2813
VJ
745 }
746 /* Write the oldpath (target) to the file. */
f35bde2f 747 oldpath_size = strlen(oldpath);
879c2813
VJ
748 do {
749 write_size = write(fd, (void *)oldpath, oldpath_size);
750 } while (write_size == -1 && errno == EINTR);
751
752 if (write_size != oldpath_size) {
753 serrno = errno;
754 close(fd);
755 err = -1;
756 goto err_end;
757 }
758 close(fd);
759 /* Set cleint credentials in symlink's xattr */
760 credp->fc_mode = credp->fc_mode|S_IFLNK;
4fa4ce71 761 err = local_set_xattr(buffer, credp);
879c2813
VJ
762 if (err == -1) {
763 serrno = errno;
764 goto err_end;
765 }
2c30dd74
AK
766 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
767 int fd;
768 ssize_t oldpath_size, write_size;
4fa4ce71
CG
769 buffer = rpath(fs_ctx, newpath);
770 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
2c30dd74
AK
771 if (fd == -1) {
772 err = fd;
773 goto out;
774 }
775 /* Write the oldpath (target) to the file. */
776 oldpath_size = strlen(oldpath);
777 do {
778 write_size = write(fd, (void *)oldpath, oldpath_size);
779 } while (write_size == -1 && errno == EINTR);
780
781 if (write_size != oldpath_size) {
782 serrno = errno;
783 close(fd);
784 err = -1;
785 goto err_end;
786 }
787 close(fd);
788 /* Set cleint credentials in symlink's xattr */
789 credp->fc_mode = credp->fc_mode|S_IFLNK;
790 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
791 if (err == -1) {
792 serrno = errno;
793 goto err_end;
794 }
b97400ca
AK
795 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
796 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
797 buffer = rpath(fs_ctx, newpath);
798 err = symlink(oldpath, buffer);
879c2813 799 if (err) {
2289be19 800 goto out;
879c2813 801 }
4fa4ce71 802 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
879c2813 803 if (err == -1) {
12848bfc
AK
804 /*
805 * If we fail to change ownership and if we are
806 * using security model none. Ignore the error
807 */
b97400ca 808 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
12848bfc
AK
809 serrno = errno;
810 goto err_end;
811 } else
812 err = 0;
879c2813
VJ
813 }
814 }
2289be19 815 goto out;
879c2813
VJ
816
817err_end:
4fa4ce71 818 remove(buffer);
879c2813 819 errno = serrno;
2289be19 820out:
4ed7b2c3 821 g_free(buffer);
2289be19 822 v9fs_string_free(&fullname);
879c2813 823 return err;
c494dd6f
AL
824}
825
2289be19
AK
826static int local_link(FsContext *ctx, V9fsPath *oldpath,
827 V9fsPath *dirpath, const char *name)
c494dd6f 828{
2289be19
AK
829 int ret;
830 V9fsString newpath;
4fa4ce71 831 char *buffer, *buffer1;
c494dd6f 832
2289be19
AK
833 v9fs_string_init(&newpath);
834 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
835
4fa4ce71
CG
836 buffer = rpath(ctx, oldpath->data);
837 buffer1 = rpath(ctx, newpath.data);
838 ret = link(buffer, buffer1);
839 g_free(buffer);
840 g_free(buffer1);
2c30dd74
AK
841
842 /* now link the virtfs_metadata files */
843 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
844 /* Link the .virtfs_metadata files. Create the metada directory */
845 ret = local_create_mapped_attr_dir(ctx, newpath.data);
846 if (ret < 0) {
847 goto err_out;
848 }
4fa4ce71
CG
849 buffer = local_mapped_attr_path(ctx, oldpath->data);
850 buffer1 = local_mapped_attr_path(ctx, newpath.data);
851 ret = link(buffer, buffer1);
852 g_free(buffer);
853 g_free(buffer1);
2c30dd74
AK
854 if (ret < 0 && errno != ENOENT) {
855 goto err_out;
856 }
857 }
858err_out:
2289be19
AK
859 v9fs_string_free(&newpath);
860 return ret;
c494dd6f
AL
861}
862
2289be19 863static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
8cf89e00 864{
4fa4ce71
CG
865 char *buffer;
866 int ret;
2289be19
AK
867 char *path = fs_path->data;
868
4fa4ce71
CG
869 buffer = rpath(ctx, path);
870 ret = truncate(buffer, size);
871 g_free(buffer);
872 return ret;
8cf89e00
AL
873}
874
875static int local_rename(FsContext *ctx, const char *oldpath,
876 const char *newpath)
877{
2c30dd74 878 int err;
4fa4ce71 879 char *buffer, *buffer1;
8cf89e00 880
2c30dd74
AK
881 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
882 err = local_create_mapped_attr_dir(ctx, newpath);
883 if (err < 0) {
884 return err;
885 }
886 /* rename the .virtfs_metadata files */
4fa4ce71
CG
887 buffer = local_mapped_attr_path(ctx, oldpath);
888 buffer1 = local_mapped_attr_path(ctx, newpath);
889 err = rename(buffer, buffer1);
890 g_free(buffer);
891 g_free(buffer1);
2c30dd74
AK
892 if (err < 0 && errno != ENOENT) {
893 return err;
894 }
895 }
4fa4ce71
CG
896
897 buffer = rpath(ctx, oldpath);
898 buffer1 = rpath(ctx, newpath);
899 err = rename(buffer, buffer1);
900 g_free(buffer);
901 g_free(buffer1);
902 return err;
8cf89e00
AL
903}
904
2289be19 905static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
8cf89e00 906{
4fa4ce71
CG
907 char *buffer;
908 int ret = -1;
2289be19
AK
909 char *path = fs_path->data;
910
c79ce737 911 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
17b1971f
AK
912 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
913 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
914 buffer = rpath(fs_ctx, path);
915 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
916 g_free(buffer);
b97400ca 917 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
918 buffer = rpath(fs_ctx, path);
919 ret = local_set_xattr(buffer, credp);
920 g_free(buffer);
2c30dd74
AK
921 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
922 return local_set_mapped_file_attr(fs_ctx, path, credp);
f7613bee 923 }
4fa4ce71 924 return ret;
8cf89e00
AL
925}
926
2289be19 927static int local_utimensat(FsContext *s, V9fsPath *fs_path,
38671423 928 const struct timespec *buf)
8cf89e00 929{
4fa4ce71
CG
930 char *buffer;
931 int ret;
2289be19
AK
932 char *path = fs_path->data;
933
4fa4ce71
CG
934 buffer = rpath(s, path);
935 ret = qemu_utimens(buffer, buf);
936 g_free(buffer);
937 return ret;
8cf89e00
AL
938}
939
5bae1900
AL
940static int local_remove(FsContext *ctx, const char *path)
941{
2c30dd74
AK
942 int err;
943 struct stat stbuf;
4fa4ce71 944 char *buffer;
2c30dd74
AK
945
946 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4fa4ce71
CG
947 buffer = rpath(ctx, path);
948 err = lstat(buffer, &stbuf);
949 g_free(buffer);
2c30dd74
AK
950 if (err) {
951 goto err_out;
952 }
953 /*
954 * If directory remove .virtfs_metadata contained in the
955 * directory
956 */
957 if (S_ISDIR(stbuf.st_mode)) {
4fa4ce71
CG
958 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
959 path, VIRTFS_META_DIR);
2c30dd74 960 err = remove(buffer);
4fa4ce71 961 g_free(buffer);
2c30dd74
AK
962 if (err < 0 && errno != ENOENT) {
963 /*
964 * We didn't had the .virtfs_metadata file. May be file created
965 * in non-mapped mode ?. Ignore ENOENT.
966 */
967 goto err_out;
968 }
969 }
970 /*
971 * Now remove the name from parent directory
972 * .virtfs_metadata directory
973 */
4fa4ce71
CG
974 buffer = local_mapped_attr_path(ctx, path);
975 err = remove(buffer);
976 g_free(buffer);
2c30dd74
AK
977 if (err < 0 && errno != ENOENT) {
978 /*
979 * We didn't had the .virtfs_metadata file. May be file created
980 * in non-mapped mode ?. Ignore ENOENT.
981 */
982 goto err_out;
983 }
984 }
4fa4ce71
CG
985
986 buffer = rpath(ctx, path);
987 err = remove(buffer);
988 g_free(buffer);
2c30dd74
AK
989err_out:
990 return err;
5bae1900
AL
991}
992
8b888272
AK
993static int local_fsync(FsContext *ctx, int fid_type,
994 V9fsFidOpenState *fs, int datasync)
8cf89e00 995{
8b888272
AK
996 int fd;
997
998 if (fid_type == P9_FID_DIR) {
999 fd = dirfd(fs->dir);
1000 } else {
1001 fd = fs->fd;
1002 }
1003
49594973 1004 if (datasync) {
8b888272 1005 return qemu_fdatasync(fd);
49594973 1006 } else {
8b888272 1007 return fsync(fd);
49594973 1008 }
8cf89e00
AL
1009}
1010
2289be19 1011static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
be940c87 1012{
4fa4ce71
CG
1013 char *buffer;
1014 int ret;
2289be19
AK
1015 char *path = fs_path->data;
1016
4fa4ce71
CG
1017 buffer = rpath(s, path);
1018 ret = statfs(buffer, stbuf);
1019 g_free(buffer);
1020 return ret;
be940c87
MK
1021}
1022
2289be19 1023static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1024 const char *name, void *value, size_t size)
1025{
2289be19
AK
1026 char *path = fs_path->data;
1027
fc22118d 1028 return v9fs_get_xattr(ctx, path, name, value, size);
fa32ef88
AK
1029}
1030
2289be19 1031static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1032 void *value, size_t size)
1033{
2289be19
AK
1034 char *path = fs_path->data;
1035
fc22118d 1036 return v9fs_list_xattr(ctx, path, value, size);
fa32ef88
AK
1037}
1038
2289be19 1039static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
10b468bd
AK
1040 void *value, size_t size, int flags)
1041{
2289be19
AK
1042 char *path = fs_path->data;
1043
fc22118d 1044 return v9fs_set_xattr(ctx, path, name, value, size, flags);
10b468bd
AK
1045}
1046
2289be19
AK
1047static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1048 const char *name)
9ed3ef26 1049{
2289be19
AK
1050 char *path = fs_path->data;
1051
fc22118d 1052 return v9fs_remove_xattr(ctx, path, name);
9ed3ef26
AK
1053}
1054
2289be19
AK
1055static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1056 const char *name, V9fsPath *target)
1057{
1058 if (dir_path) {
1059 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1060 dir_path->data, name);
1061 } else {
1062 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1063 }
1064 /* Bump the size for including terminating NULL */
1065 target->size++;
1066 return 0;
1067}
1068
1069static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1070 const char *old_name, V9fsPath *newdir,
1071 const char *new_name)
1072{
1073 int ret;
1074 V9fsString old_full_name, new_full_name;
1075
1076 v9fs_string_init(&old_full_name);
1077 v9fs_string_init(&new_full_name);
1078
1079 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1080 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1081
1082 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1083 v9fs_string_free(&old_full_name);
1084 v9fs_string_free(&new_full_name);
1085 return ret;
1086}
1087
1088static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1089 const char *name, int flags)
1090{
1091 int ret;
1092 V9fsString fullname;
4fa4ce71 1093 char *buffer;
2c30dd74 1094
2289be19
AK
1095 v9fs_string_init(&fullname);
1096
1097 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
2c30dd74
AK
1098 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1099 if (flags == AT_REMOVEDIR) {
1100 /*
1101 * If directory remove .virtfs_metadata contained in the
1102 * directory
1103 */
4fa4ce71
CG
1104 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1105 fullname.data, VIRTFS_META_DIR);
2c30dd74 1106 ret = remove(buffer);
4fa4ce71 1107 g_free(buffer);
2c30dd74
AK
1108 if (ret < 0 && errno != ENOENT) {
1109 /*
1110 * We didn't had the .virtfs_metadata file. May be file created
1111 * in non-mapped mode ?. Ignore ENOENT.
1112 */
1113 goto err_out;
1114 }
1115 }
1116 /*
1117 * Now remove the name from parent directory
1118 * .virtfs_metadata directory.
1119 */
4fa4ce71
CG
1120 buffer = local_mapped_attr_path(ctx, fullname.data);
1121 ret = remove(buffer);
1122 g_free(buffer);
2c30dd74
AK
1123 if (ret < 0 && errno != ENOENT) {
1124 /*
1125 * We didn't had the .virtfs_metadata file. May be file created
1126 * in non-mapped mode ?. Ignore ENOENT.
1127 */
1128 goto err_out;
1129 }
1130 }
1131 /* Remove the name finally */
4fa4ce71
CG
1132 buffer = rpath(ctx, fullname.data);
1133 ret = remove(buffer);
1134 g_free(buffer);
2289be19 1135
2c30dd74 1136err_out:
75b7931e 1137 v9fs_string_free(&fullname);
2289be19
AK
1138 return ret;
1139}
9ed3ef26 1140
e06a765e
HPB
1141static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1142 mode_t st_mode, uint64_t *st_gen)
1143{
ae0f940e 1144#ifdef FS_IOC_GETVERSION
0e5fc994 1145 int err;
cc720ddb
AK
1146 V9fsFidOpenState fid_open;
1147
e06a765e
HPB
1148 /*
1149 * Do not try to open special files like device nodes, fifos etc
1150 * We can get fd for regular files and directories only
1151 */
1152 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1a9978a5
KS
1153 errno = ENOTTY;
1154 return -1;
e06a765e 1155 }
cc720ddb
AK
1156 err = local_open(ctx, path, O_RDONLY, &fid_open);
1157 if (err < 0) {
1158 return err;
e06a765e 1159 }
cc720ddb
AK
1160 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1161 local_close(ctx, &fid_open);
0e5fc994 1162 return err;
ae0f940e 1163#else
0e5fc994
KS
1164 errno = ENOTTY;
1165 return -1;
ae0f940e 1166#endif
e06a765e
HPB
1167}
1168
0174fe73
AK
1169static int local_init(FsContext *ctx)
1170{
2507718b 1171 int err = 0;
e06a765e
HPB
1172 struct statfs stbuf;
1173
2c30dd74
AK
1174 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1175 ctx->xops = passthrough_xattr_ops;
1176 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1177 ctx->xops = mapped_xattr_ops;
1178 } else if (ctx->export_flags & V9FS_SM_NONE) {
1179 ctx->xops = none_xattr_ops;
1180 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1181 /*
1182 * xattr operation for mapped-file and passthrough
1183 * remain same.
1184 */
1185 ctx->xops = passthrough_xattr_ops;
1186 }
c98f1d4a 1187 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
2507718b
AK
1188#ifdef FS_IOC_GETVERSION
1189 /*
1190 * use ioc_getversion only if the iocl is definied
1191 */
e06a765e
HPB
1192 err = statfs(ctx->fs_root, &stbuf);
1193 if (!err) {
1194 switch (stbuf.f_type) {
1195 case EXT2_SUPER_MAGIC:
1196 case BTRFS_SUPER_MAGIC:
1197 case REISERFS_SUPER_MAGIC:
1198 case XFS_SUPER_MAGIC:
1199 ctx->exops.get_st_gen = local_ioc_getversion;
1200 break;
1201 }
1202 }
2507718b 1203#endif
e06a765e 1204 return err;
0174fe73
AK
1205}
1206
99519f0a
AK
1207static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1208{
1209 const char *sec_model = qemu_opt_get(opts, "security_model");
1210 const char *path = qemu_opt_get(opts, "path");
1211
1212 if (!sec_model) {
1213 fprintf(stderr, "security model not specified, "
1214 "local fs needs security model\nvalid options are:"
1215 "\tsecurity_model=[passthrough|mapped|none]\n");
1216 return -1;
1217 }
1218
1219 if (!strcmp(sec_model, "passthrough")) {
1220 fse->export_flags |= V9FS_SM_PASSTHROUGH;
2c30dd74
AK
1221 } else if (!strcmp(sec_model, "mapped") ||
1222 !strcmp(sec_model, "mapped-xattr")) {
99519f0a
AK
1223 fse->export_flags |= V9FS_SM_MAPPED;
1224 } else if (!strcmp(sec_model, "none")) {
1225 fse->export_flags |= V9FS_SM_NONE;
2c30dd74
AK
1226 } else if (!strcmp(sec_model, "mapped-file")) {
1227 fse->export_flags |= V9FS_SM_MAPPED_FILE;
99519f0a
AK
1228 } else {
1229 fprintf(stderr, "Invalid security model %s specified, valid options are"
2c30dd74
AK
1230 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1231 sec_model);
99519f0a
AK
1232 return -1;
1233 }
1234
1235 if (!path) {
1236 fprintf(stderr, "fsdev: No path specified.\n");
1237 return -1;
1238 }
1239 fse->path = g_strdup(path);
1240
1241 return 0;
1242}
1243
9f107513 1244FileOperations local_ops = {
99519f0a 1245 .parse_opts = local_parse_opts,
0174fe73 1246 .init = local_init,
131dcb25 1247 .lstat = local_lstat,
131dcb25
AL
1248 .readlink = local_readlink,
1249 .close = local_close,
1250 .closedir = local_closedir,
a6568fe2
AL
1251 .open = local_open,
1252 .opendir = local_opendir,
a9231555
AL
1253 .rewinddir = local_rewinddir,
1254 .telldir = local_telldir,
5f524c1e 1255 .readdir_r = local_readdir_r,
a9231555 1256 .seekdir = local_seekdir,
56d15a53
SG
1257 .preadv = local_preadv,
1258 .pwritev = local_pwritev,
c494dd6f
AL
1259 .chmod = local_chmod,
1260 .mknod = local_mknod,
c494dd6f
AL
1261 .mkdir = local_mkdir,
1262 .fstat = local_fstat,
1263 .open2 = local_open2,
1264 .symlink = local_symlink,
1265 .link = local_link,
8cf89e00
AL
1266 .truncate = local_truncate,
1267 .rename = local_rename,
1268 .chown = local_chown,
74bc02b2 1269 .utimensat = local_utimensat,
5bae1900 1270 .remove = local_remove,
8cf89e00 1271 .fsync = local_fsync,
be940c87 1272 .statfs = local_statfs,
fa32ef88
AK
1273 .lgetxattr = local_lgetxattr,
1274 .llistxattr = local_llistxattr,
10b468bd 1275 .lsetxattr = local_lsetxattr,
9ed3ef26 1276 .lremovexattr = local_lremovexattr,
2289be19
AK
1277 .name_to_path = local_name_to_path,
1278 .renameat = local_renameat,
1279 .unlinkat = local_unlinkat,
9f107513 1280};