]> Git Repo - qemu.git/blame - hw/9pfs/virtio-9p-local.c
virtfs-proxy: Fix possible overflow
[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 <[email protected]>
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{
cc720ddb 381 return 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{
cc720ddb 412 return 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;
4fa4ce71 489 char *buffer;
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) {
4fa4ce71 500 g_free(buffer);
2289be19 501 goto out;
1c293312 502 }
4fa4ce71 503 err = local_set_xattr(buffer, credp);
1c293312
VJ
504 if (err == -1) {
505 serrno = errno;
506 goto err_end;
507 }
2c30dd74
AK
508 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
509
4fa4ce71
CG
510 buffer = rpath(fs_ctx, path);
511 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
2c30dd74 512 if (err == -1) {
4fa4ce71 513 g_free(buffer);
2c30dd74
AK
514 goto out;
515 }
516 err = local_set_mapped_file_attr(fs_ctx, path, credp);
517 if (err == -1) {
518 serrno = errno;
519 goto err_end;
520 }
b97400ca
AK
521 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
522 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
523 buffer = rpath(fs_ctx, path);
524 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
1c293312 525 if (err == -1) {
4fa4ce71 526 g_free(buffer);
2289be19 527 goto out;
1c293312
VJ
528 }
529 err = local_post_create_passthrough(fs_ctx, path, credp);
530 if (err == -1) {
531 serrno = errno;
532 goto err_end;
533 }
534 }
2289be19 535 goto out;
1c293312
VJ
536
537err_end:
4fa4ce71 538 remove(buffer);
1c293312 539 errno = serrno;
4fa4ce71 540 g_free(buffer);
2289be19
AK
541out:
542 v9fs_string_free(&fullname);
1c293312 543 return err;
c494dd6f
AL
544}
545
2289be19
AK
546static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
547 const char *name, FsCred *credp)
c494dd6f 548{
2289be19 549 char *path;
00ec5c37
VJ
550 int err = -1;
551 int serrno = 0;
2289be19 552 V9fsString fullname;
4fa4ce71 553 char *buffer;
00ec5c37 554
2289be19
AK
555 v9fs_string_init(&fullname);
556 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
557 path = fullname.data;
558
00ec5c37 559 /* Determine the security model */
b97400ca 560 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
561 buffer = rpath(fs_ctx, path);
562 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
00ec5c37 563 if (err == -1) {
4fa4ce71 564 g_free(buffer);
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 576 if (err == -1) {
4fa4ce71 577 g_free(buffer);
2c30dd74
AK
578 goto out;
579 }
580 credp->fc_mode = credp->fc_mode|S_IFDIR;
581 err = local_set_mapped_file_attr(fs_ctx, path, credp);
582 if (err == -1) {
583 serrno = errno;
584 goto err_end;
585 }
b97400ca
AK
586 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
587 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
588 buffer = rpath(fs_ctx, path);
589 err = mkdir(buffer, credp->fc_mode);
00ec5c37 590 if (err == -1) {
4fa4ce71 591 g_free(buffer);
2289be19 592 goto out;
00ec5c37
VJ
593 }
594 err = local_post_create_passthrough(fs_ctx, path, credp);
595 if (err == -1) {
596 serrno = errno;
597 goto err_end;
598 }
599 }
2289be19 600 goto out;
00ec5c37
VJ
601
602err_end:
4fa4ce71 603 remove(buffer);
00ec5c37 604 errno = serrno;
4fa4ce71 605 g_free(buffer);
2289be19
AK
606out:
607 v9fs_string_free(&fullname);
00ec5c37 608 return err;
c494dd6f
AL
609}
610
8b888272 611static int local_fstat(FsContext *fs_ctx, int fid_type,
cc720ddb 612 V9fsFidOpenState *fs, struct stat *stbuf)
c494dd6f 613{
8b888272
AK
614 int err, fd;
615
616 if (fid_type == P9_FID_DIR) {
617 fd = dirfd(fs->dir);
618 } else {
619 fd = fs->fd;
620 }
621
622 err = fstat(fd, stbuf);
1237ad76
VJ
623 if (err) {
624 return err;
625 }
b97400ca 626 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
1237ad76
VJ
627 /* Actual credentials are part of extended attrs */
628 uid_t tmp_uid;
629 gid_t tmp_gid;
630 mode_t tmp_mode;
631 dev_t tmp_dev;
632
f8ad4a89
AK
633 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
634 stbuf->st_uid = le32_to_cpu(tmp_uid);
1237ad76 635 }
f8ad4a89
AK
636 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
637 stbuf->st_gid = le32_to_cpu(tmp_gid);
1237ad76 638 }
f8ad4a89
AK
639 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
640 stbuf->st_mode = le32_to_cpu(tmp_mode);
1237ad76 641 }
f8ad4a89
AK
642 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
643 stbuf->st_rdev = le64_to_cpu(tmp_dev);
1237ad76 644 }
2c30dd74
AK
645 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
646 errno = EOPNOTSUPP;
647 return -1;
1237ad76
VJ
648 }
649 return err;
c494dd6f
AL
650}
651
2289be19 652static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
cc720ddb 653 int flags, FsCred *credp, V9fsFidOpenState *fs)
c494dd6f 654{
2289be19 655 char *path;
4750a96f
VJ
656 int fd = -1;
657 int err = -1;
658 int serrno = 0;
2289be19 659 V9fsString fullname;
4fa4ce71 660 char *buffer;
4750a96f 661
0ceb092e
AK
662 /*
663 * Mark all the open to not follow symlinks
664 */
665 flags |= O_NOFOLLOW;
666
2289be19
AK
667 v9fs_string_init(&fullname);
668 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
669 path = fullname.data;
670
4750a96f 671 /* Determine the security model */
b97400ca 672 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
673 buffer = rpath(fs_ctx, path);
674 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
4750a96f 675 if (fd == -1) {
4fa4ce71 676 g_free(buffer);
2289be19
AK
677 err = fd;
678 goto out;
4750a96f
VJ
679 }
680 credp->fc_mode = credp->fc_mode|S_IFREG;
681 /* Set cleint credentials in xattr */
4fa4ce71 682 err = local_set_xattr(buffer, credp);
4750a96f
VJ
683 if (err == -1) {
684 serrno = errno;
685 goto err_end;
686 }
2c30dd74 687 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4fa4ce71
CG
688 buffer = rpath(fs_ctx, path);
689 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
2c30dd74 690 if (fd == -1) {
4fa4ce71 691 g_free(buffer);
2c30dd74
AK
692 err = fd;
693 goto out;
694 }
695 credp->fc_mode = credp->fc_mode|S_IFREG;
696 /* Set client credentials in .virtfs_metadata directory files */
697 err = local_set_mapped_file_attr(fs_ctx, path, credp);
698 if (err == -1) {
699 serrno = errno;
700 goto err_end;
701 }
b97400ca
AK
702 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
703 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
704 buffer = rpath(fs_ctx, path);
705 fd = open(buffer, flags, credp->fc_mode);
4750a96f 706 if (fd == -1) {
4fa4ce71 707 g_free(buffer);
2289be19
AK
708 err = fd;
709 goto out;
4750a96f
VJ
710 }
711 err = local_post_create_passthrough(fs_ctx, path, credp);
712 if (err == -1) {
713 serrno = errno;
714 goto err_end;
715 }
716 }
2289be19 717 err = fd;
cc720ddb 718 fs->fd = fd;
2289be19 719 goto out;
4750a96f
VJ
720
721err_end:
722 close(fd);
4fa4ce71 723 remove(buffer);
4750a96f 724 errno = serrno;
4fa4ce71 725 g_free(buffer);
2289be19
AK
726out:
727 v9fs_string_free(&fullname);
4750a96f 728 return err;
c494dd6f
AL
729}
730
758e8e38 731
879c2813 732static int local_symlink(FsContext *fs_ctx, const char *oldpath,
2289be19 733 V9fsPath *dir_path, const char *name, FsCred *credp)
c494dd6f 734{
879c2813
VJ
735 int err = -1;
736 int serrno = 0;
2289be19
AK
737 char *newpath;
738 V9fsString fullname;
4fa4ce71 739 char *buffer;
879c2813 740
2289be19
AK
741 v9fs_string_init(&fullname);
742 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
743 newpath = fullname.data;
744
879c2813 745 /* Determine the security model */
b97400ca 746 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
879c2813
VJ
747 int fd;
748 ssize_t oldpath_size, write_size;
4fa4ce71
CG
749 buffer = rpath(fs_ctx, newpath);
750 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
879c2813 751 if (fd == -1) {
4fa4ce71 752 g_free(buffer);
2289be19
AK
753 err = fd;
754 goto out;
879c2813
VJ
755 }
756 /* Write the oldpath (target) to the file. */
f35bde2f 757 oldpath_size = strlen(oldpath);
879c2813
VJ
758 do {
759 write_size = write(fd, (void *)oldpath, oldpath_size);
760 } while (write_size == -1 && errno == EINTR);
761
762 if (write_size != oldpath_size) {
763 serrno = errno;
764 close(fd);
765 err = -1;
766 goto err_end;
767 }
768 close(fd);
769 /* Set cleint credentials in symlink's xattr */
770 credp->fc_mode = credp->fc_mode|S_IFLNK;
4fa4ce71 771 err = local_set_xattr(buffer, credp);
879c2813
VJ
772 if (err == -1) {
773 serrno = errno;
774 goto err_end;
775 }
2c30dd74
AK
776 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
777 int fd;
778 ssize_t oldpath_size, write_size;
4fa4ce71
CG
779 buffer = rpath(fs_ctx, newpath);
780 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
2c30dd74 781 if (fd == -1) {
4fa4ce71 782 g_free(buffer);
2c30dd74
AK
783 err = fd;
784 goto out;
785 }
786 /* Write the oldpath (target) to the file. */
787 oldpath_size = strlen(oldpath);
788 do {
789 write_size = write(fd, (void *)oldpath, oldpath_size);
790 } while (write_size == -1 && errno == EINTR);
791
792 if (write_size != oldpath_size) {
793 serrno = errno;
794 close(fd);
795 err = -1;
796 goto err_end;
797 }
798 close(fd);
799 /* Set cleint credentials in symlink's xattr */
800 credp->fc_mode = credp->fc_mode|S_IFLNK;
801 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
802 if (err == -1) {
803 serrno = errno;
804 goto err_end;
805 }
b97400ca
AK
806 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
807 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
808 buffer = rpath(fs_ctx, newpath);
809 err = symlink(oldpath, buffer);
879c2813 810 if (err) {
4fa4ce71 811 g_free(buffer);
2289be19 812 goto out;
879c2813 813 }
4fa4ce71 814 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
879c2813 815 if (err == -1) {
12848bfc
AK
816 /*
817 * If we fail to change ownership and if we are
818 * using security model none. Ignore the error
819 */
b97400ca 820 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
12848bfc
AK
821 serrno = errno;
822 goto err_end;
823 } else
824 err = 0;
879c2813
VJ
825 }
826 }
2289be19 827 goto out;
879c2813
VJ
828
829err_end:
4fa4ce71 830 remove(buffer);
879c2813 831 errno = serrno;
4fa4ce71 832 g_free(buffer);
2289be19
AK
833out:
834 v9fs_string_free(&fullname);
879c2813 835 return err;
c494dd6f
AL
836}
837
2289be19
AK
838static int local_link(FsContext *ctx, V9fsPath *oldpath,
839 V9fsPath *dirpath, const char *name)
c494dd6f 840{
2289be19
AK
841 int ret;
842 V9fsString newpath;
4fa4ce71 843 char *buffer, *buffer1;
c494dd6f 844
2289be19
AK
845 v9fs_string_init(&newpath);
846 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
847
4fa4ce71
CG
848 buffer = rpath(ctx, oldpath->data);
849 buffer1 = rpath(ctx, newpath.data);
850 ret = link(buffer, buffer1);
851 g_free(buffer);
852 g_free(buffer1);
2c30dd74
AK
853
854 /* now link the virtfs_metadata files */
855 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
856 /* Link the .virtfs_metadata files. Create the metada directory */
857 ret = local_create_mapped_attr_dir(ctx, newpath.data);
858 if (ret < 0) {
859 goto err_out;
860 }
4fa4ce71
CG
861 buffer = local_mapped_attr_path(ctx, oldpath->data);
862 buffer1 = local_mapped_attr_path(ctx, newpath.data);
863 ret = link(buffer, buffer1);
864 g_free(buffer);
865 g_free(buffer1);
2c30dd74
AK
866 if (ret < 0 && errno != ENOENT) {
867 goto err_out;
868 }
869 }
870err_out:
2289be19
AK
871 v9fs_string_free(&newpath);
872 return ret;
c494dd6f
AL
873}
874
2289be19 875static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
8cf89e00 876{
4fa4ce71
CG
877 char *buffer;
878 int ret;
2289be19
AK
879 char *path = fs_path->data;
880
4fa4ce71
CG
881 buffer = rpath(ctx, path);
882 ret = truncate(buffer, size);
883 g_free(buffer);
884 return ret;
8cf89e00
AL
885}
886
887static int local_rename(FsContext *ctx, const char *oldpath,
888 const char *newpath)
889{
2c30dd74 890 int err;
4fa4ce71 891 char *buffer, *buffer1;
8cf89e00 892
2c30dd74
AK
893 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
894 err = local_create_mapped_attr_dir(ctx, newpath);
895 if (err < 0) {
896 return err;
897 }
898 /* rename the .virtfs_metadata files */
4fa4ce71
CG
899 buffer = local_mapped_attr_path(ctx, oldpath);
900 buffer1 = local_mapped_attr_path(ctx, newpath);
901 err = rename(buffer, buffer1);
902 g_free(buffer);
903 g_free(buffer1);
2c30dd74
AK
904 if (err < 0 && errno != ENOENT) {
905 return err;
906 }
907 }
4fa4ce71
CG
908
909 buffer = rpath(ctx, oldpath);
910 buffer1 = rpath(ctx, newpath);
911 err = rename(buffer, buffer1);
912 g_free(buffer);
913 g_free(buffer1);
914 return err;
8cf89e00
AL
915}
916
2289be19 917static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
8cf89e00 918{
4fa4ce71
CG
919 char *buffer;
920 int ret = -1;
2289be19
AK
921 char *path = fs_path->data;
922
c79ce737 923 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
17b1971f
AK
924 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
925 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
926 buffer = rpath(fs_ctx, path);
927 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
928 g_free(buffer);
b97400ca 929 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
4fa4ce71
CG
930 buffer = rpath(fs_ctx, path);
931 ret = local_set_xattr(buffer, credp);
932 g_free(buffer);
2c30dd74
AK
933 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
934 return local_set_mapped_file_attr(fs_ctx, path, credp);
f7613bee 935 }
4fa4ce71 936 return ret;
8cf89e00
AL
937}
938
2289be19 939static int local_utimensat(FsContext *s, V9fsPath *fs_path,
38671423 940 const struct timespec *buf)
8cf89e00 941{
4fa4ce71
CG
942 char *buffer;
943 int ret;
2289be19
AK
944 char *path = fs_path->data;
945
4fa4ce71
CG
946 buffer = rpath(s, path);
947 ret = qemu_utimens(buffer, buf);
948 g_free(buffer);
949 return ret;
8cf89e00
AL
950}
951
5bae1900
AL
952static int local_remove(FsContext *ctx, const char *path)
953{
2c30dd74
AK
954 int err;
955 struct stat stbuf;
4fa4ce71 956 char *buffer;
2c30dd74
AK
957
958 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
4fa4ce71
CG
959 buffer = rpath(ctx, path);
960 err = lstat(buffer, &stbuf);
961 g_free(buffer);
2c30dd74
AK
962 if (err) {
963 goto err_out;
964 }
965 /*
966 * If directory remove .virtfs_metadata contained in the
967 * directory
968 */
969 if (S_ISDIR(stbuf.st_mode)) {
4fa4ce71
CG
970 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
971 path, VIRTFS_META_DIR);
2c30dd74 972 err = remove(buffer);
4fa4ce71 973 g_free(buffer);
2c30dd74
AK
974 if (err < 0 && errno != ENOENT) {
975 /*
976 * We didn't had the .virtfs_metadata file. May be file created
977 * in non-mapped mode ?. Ignore ENOENT.
978 */
979 goto err_out;
980 }
981 }
982 /*
983 * Now remove the name from parent directory
984 * .virtfs_metadata directory
985 */
4fa4ce71
CG
986 buffer = local_mapped_attr_path(ctx, path);
987 err = remove(buffer);
988 g_free(buffer);
2c30dd74
AK
989 if (err < 0 && errno != ENOENT) {
990 /*
991 * We didn't had the .virtfs_metadata file. May be file created
992 * in non-mapped mode ?. Ignore ENOENT.
993 */
994 goto err_out;
995 }
996 }
4fa4ce71
CG
997
998 buffer = rpath(ctx, path);
999 err = remove(buffer);
1000 g_free(buffer);
2c30dd74
AK
1001err_out:
1002 return err;
5bae1900
AL
1003}
1004
8b888272
AK
1005static int local_fsync(FsContext *ctx, int fid_type,
1006 V9fsFidOpenState *fs, int datasync)
8cf89e00 1007{
8b888272
AK
1008 int fd;
1009
1010 if (fid_type == P9_FID_DIR) {
1011 fd = dirfd(fs->dir);
1012 } else {
1013 fd = fs->fd;
1014 }
1015
49594973 1016 if (datasync) {
8b888272 1017 return qemu_fdatasync(fd);
49594973 1018 } else {
8b888272 1019 return fsync(fd);
49594973 1020 }
8cf89e00
AL
1021}
1022
2289be19 1023static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
be940c87 1024{
4fa4ce71
CG
1025 char *buffer;
1026 int ret;
2289be19
AK
1027 char *path = fs_path->data;
1028
4fa4ce71
CG
1029 buffer = rpath(s, path);
1030 ret = statfs(buffer, stbuf);
1031 g_free(buffer);
1032 return ret;
be940c87
MK
1033}
1034
2289be19 1035static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1036 const char *name, void *value, size_t size)
1037{
2289be19
AK
1038 char *path = fs_path->data;
1039
fc22118d 1040 return v9fs_get_xattr(ctx, path, name, value, size);
fa32ef88
AK
1041}
1042
2289be19 1043static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
fa32ef88
AK
1044 void *value, size_t size)
1045{
2289be19
AK
1046 char *path = fs_path->data;
1047
fc22118d 1048 return v9fs_list_xattr(ctx, path, value, size);
fa32ef88
AK
1049}
1050
2289be19 1051static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
10b468bd
AK
1052 void *value, size_t size, int flags)
1053{
2289be19
AK
1054 char *path = fs_path->data;
1055
fc22118d 1056 return v9fs_set_xattr(ctx, path, name, value, size, flags);
10b468bd
AK
1057}
1058
2289be19
AK
1059static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1060 const char *name)
9ed3ef26 1061{
2289be19
AK
1062 char *path = fs_path->data;
1063
fc22118d 1064 return v9fs_remove_xattr(ctx, path, name);
9ed3ef26
AK
1065}
1066
2289be19
AK
1067static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1068 const char *name, V9fsPath *target)
1069{
1070 if (dir_path) {
1071 v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1072 dir_path->data, name);
1073 } else {
1074 v9fs_string_sprintf((V9fsString *)target, "%s", name);
1075 }
1076 /* Bump the size for including terminating NULL */
1077 target->size++;
1078 return 0;
1079}
1080
1081static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1082 const char *old_name, V9fsPath *newdir,
1083 const char *new_name)
1084{
1085 int ret;
1086 V9fsString old_full_name, new_full_name;
1087
1088 v9fs_string_init(&old_full_name);
1089 v9fs_string_init(&new_full_name);
1090
1091 v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1092 v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1093
1094 ret = local_rename(ctx, old_full_name.data, new_full_name.data);
1095 v9fs_string_free(&old_full_name);
1096 v9fs_string_free(&new_full_name);
1097 return ret;
1098}
1099
1100static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1101 const char *name, int flags)
1102{
1103 int ret;
1104 V9fsString fullname;
4fa4ce71 1105 char *buffer;
2c30dd74 1106
2289be19
AK
1107 v9fs_string_init(&fullname);
1108
1109 v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
2c30dd74
AK
1110 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1111 if (flags == AT_REMOVEDIR) {
1112 /*
1113 * If directory remove .virtfs_metadata contained in the
1114 * directory
1115 */
4fa4ce71
CG
1116 buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
1117 fullname.data, VIRTFS_META_DIR);
2c30dd74 1118 ret = remove(buffer);
4fa4ce71 1119 g_free(buffer);
2c30dd74
AK
1120 if (ret < 0 && errno != ENOENT) {
1121 /*
1122 * We didn't had the .virtfs_metadata file. May be file created
1123 * in non-mapped mode ?. Ignore ENOENT.
1124 */
1125 goto err_out;
1126 }
1127 }
1128 /*
1129 * Now remove the name from parent directory
1130 * .virtfs_metadata directory.
1131 */
4fa4ce71
CG
1132 buffer = local_mapped_attr_path(ctx, fullname.data);
1133 ret = remove(buffer);
1134 g_free(buffer);
2c30dd74
AK
1135 if (ret < 0 && errno != ENOENT) {
1136 /*
1137 * We didn't had the .virtfs_metadata file. May be file created
1138 * in non-mapped mode ?. Ignore ENOENT.
1139 */
1140 goto err_out;
1141 }
1142 }
1143 /* Remove the name finally */
4fa4ce71
CG
1144 buffer = rpath(ctx, fullname.data);
1145 ret = remove(buffer);
1146 g_free(buffer);
2289be19 1147
2c30dd74 1148err_out:
75b7931e 1149 v9fs_string_free(&fullname);
2289be19
AK
1150 return ret;
1151}
9ed3ef26 1152
e06a765e
HPB
1153static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1154 mode_t st_mode, uint64_t *st_gen)
1155{
ae0f940e 1156#ifdef FS_IOC_GETVERSION
0e5fc994 1157 int err;
cc720ddb
AK
1158 V9fsFidOpenState fid_open;
1159
e06a765e
HPB
1160 /*
1161 * Do not try to open special files like device nodes, fifos etc
1162 * We can get fd for regular files and directories only
1163 */
1164 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1a9978a5
KS
1165 errno = ENOTTY;
1166 return -1;
e06a765e 1167 }
cc720ddb
AK
1168 err = local_open(ctx, path, O_RDONLY, &fid_open);
1169 if (err < 0) {
1170 return err;
e06a765e 1171 }
cc720ddb
AK
1172 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1173 local_close(ctx, &fid_open);
0e5fc994 1174 return err;
ae0f940e 1175#else
0e5fc994
KS
1176 errno = ENOTTY;
1177 return -1;
ae0f940e 1178#endif
e06a765e
HPB
1179}
1180
0174fe73
AK
1181static int local_init(FsContext *ctx)
1182{
2507718b 1183 int err = 0;
e06a765e
HPB
1184 struct statfs stbuf;
1185
2c30dd74
AK
1186 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1187 ctx->xops = passthrough_xattr_ops;
1188 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1189 ctx->xops = mapped_xattr_ops;
1190 } else if (ctx->export_flags & V9FS_SM_NONE) {
1191 ctx->xops = none_xattr_ops;
1192 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1193 /*
1194 * xattr operation for mapped-file and passthrough
1195 * remain same.
1196 */
1197 ctx->xops = passthrough_xattr_ops;
1198 }
c98f1d4a 1199 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
2507718b
AK
1200#ifdef FS_IOC_GETVERSION
1201 /*
1202 * use ioc_getversion only if the iocl is definied
1203 */
e06a765e
HPB
1204 err = statfs(ctx->fs_root, &stbuf);
1205 if (!err) {
1206 switch (stbuf.f_type) {
1207 case EXT2_SUPER_MAGIC:
1208 case BTRFS_SUPER_MAGIC:
1209 case REISERFS_SUPER_MAGIC:
1210 case XFS_SUPER_MAGIC:
1211 ctx->exops.get_st_gen = local_ioc_getversion;
1212 break;
1213 }
1214 }
2507718b 1215#endif
e06a765e 1216 return err;
0174fe73
AK
1217}
1218
99519f0a
AK
1219static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1220{
1221 const char *sec_model = qemu_opt_get(opts, "security_model");
1222 const char *path = qemu_opt_get(opts, "path");
1223
1224 if (!sec_model) {
1225 fprintf(stderr, "security model not specified, "
1226 "local fs needs security model\nvalid options are:"
1227 "\tsecurity_model=[passthrough|mapped|none]\n");
1228 return -1;
1229 }
1230
1231 if (!strcmp(sec_model, "passthrough")) {
1232 fse->export_flags |= V9FS_SM_PASSTHROUGH;
2c30dd74
AK
1233 } else if (!strcmp(sec_model, "mapped") ||
1234 !strcmp(sec_model, "mapped-xattr")) {
99519f0a
AK
1235 fse->export_flags |= V9FS_SM_MAPPED;
1236 } else if (!strcmp(sec_model, "none")) {
1237 fse->export_flags |= V9FS_SM_NONE;
2c30dd74
AK
1238 } else if (!strcmp(sec_model, "mapped-file")) {
1239 fse->export_flags |= V9FS_SM_MAPPED_FILE;
99519f0a
AK
1240 } else {
1241 fprintf(stderr, "Invalid security model %s specified, valid options are"
2c30dd74
AK
1242 "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
1243 sec_model);
99519f0a
AK
1244 return -1;
1245 }
1246
1247 if (!path) {
1248 fprintf(stderr, "fsdev: No path specified.\n");
1249 return -1;
1250 }
1251 fse->path = g_strdup(path);
1252
1253 return 0;
1254}
1255
9f107513 1256FileOperations local_ops = {
99519f0a 1257 .parse_opts = local_parse_opts,
0174fe73 1258 .init = local_init,
131dcb25 1259 .lstat = local_lstat,
131dcb25
AL
1260 .readlink = local_readlink,
1261 .close = local_close,
1262 .closedir = local_closedir,
a6568fe2
AL
1263 .open = local_open,
1264 .opendir = local_opendir,
a9231555
AL
1265 .rewinddir = local_rewinddir,
1266 .telldir = local_telldir,
5f524c1e 1267 .readdir_r = local_readdir_r,
a9231555 1268 .seekdir = local_seekdir,
56d15a53
SG
1269 .preadv = local_preadv,
1270 .pwritev = local_pwritev,
c494dd6f
AL
1271 .chmod = local_chmod,
1272 .mknod = local_mknod,
c494dd6f
AL
1273 .mkdir = local_mkdir,
1274 .fstat = local_fstat,
1275 .open2 = local_open2,
1276 .symlink = local_symlink,
1277 .link = local_link,
8cf89e00
AL
1278 .truncate = local_truncate,
1279 .rename = local_rename,
1280 .chown = local_chown,
74bc02b2 1281 .utimensat = local_utimensat,
5bae1900 1282 .remove = local_remove,
8cf89e00 1283 .fsync = local_fsync,
be940c87 1284 .statfs = local_statfs,
fa32ef88
AK
1285 .lgetxattr = local_lgetxattr,
1286 .llistxattr = local_llistxattr,
10b468bd 1287 .lsetxattr = local_lsetxattr,
9ed3ef26 1288 .lremovexattr = local_lremovexattr,
2289be19
AK
1289 .name_to_path = local_name_to_path,
1290 .renameat = local_renameat,
1291 .unlinkat = local_unlinkat,
9f107513 1292};
This page took 0.629489 seconds and 4 git commands to generate.