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