]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-local.c
hw/9pfs: Use little-endian format for xattr values
[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
AK
47{
48 char *dir_name;
d3f8e138 49 char *tmp_path = g_strdup(path);
2c30dd74 50 char *base_name = basename(tmp_path);
4fa4ce71 51 char *buffer;
2c30dd74
AK
52
53 /* NULL terminate the directory */
54 dir_name = tmp_path;
55 *(base_name - 1) = '\0';
56
4fa4ce71 57 buffer = g_strdup_printf("%s/%s/%s/%s",
2c30dd74 58 ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
d3f8e138 59 g_free(tmp_path);
2c30dd74
AK
60 return buffer;
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);
335 return tsize;
b97400ca
AK
336 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
337 (fs_ctx->export_flags & V9FS_SM_NONE)) {
4fa4ce71
CG
338 buffer = rpath(fs_ctx, path);
339 tsize = readlink(buffer, buf, bufsz);
340 g_free(buffer);
879c2813
VJ
341 }
342 return tsize;
131dcb25
AL
343}
344
cc720ddb 345static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 346{
cc720ddb 347 return close(fs->fd);
131dcb25
AL
348}
349
cc720ddb 350static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
131dcb25 351{
cc720ddb 352 return closedir(fs->dir);
131dcb25 353}
9f107513 354
cc720ddb
AK
355static int local_open(FsContext *ctx, V9fsPath *fs_path,
356 int flags, V9fsFidOpenState *fs)
a6568fe2 357{
4fa4ce71 358 char *buffer;
2289be19
AK
359 char *path = fs_path->data;
360
4fa4ce71
CG
361 buffer = rpath(ctx, path);
362 fs->fd = open(buffer, flags | O_NOFOLLOW);
363 g_free(buffer);
cc720ddb 364 return fs->fd;
a6568fe2
AL
365}
366
cc720ddb
AK
367static int local_opendir(FsContext *ctx,
368 V9fsPath *fs_path, V9fsFidOpenState *fs)
a6568fe2 369{
4fa4ce71 370 char *buffer;
2289be19
AK
371 char *path = fs_path->data;
372
4fa4ce71
CG
373 buffer = rpath(ctx, path);
374 fs->dir = opendir(buffer);
375 g_free(buffer);
cc720ddb
AK
376 if (!fs->dir) {
377 return -1;
378 }
379 return 0;
a6568fe2
AL
380}
381
cc720ddb 382static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 383{
cc720ddb 384 return rewinddir(fs->dir);
a9231555
AL
385}
386
cc720ddb 387static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
a9231555 388{
cc720ddb 389 return telldir(fs->dir);
a9231555
AL
390}
391
cc720ddb
AK
392static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
393 struct dirent *entry,
2289be19 394 struct dirent **result)
a9231555 395{
2c30dd74
AK
396 int ret;
397
398again:
399 ret = readdir_r(fs->dir, entry, result);
400 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
401 if (!ret && *result != NULL &&
402 !strcmp(entry->d_name, VIRTFS_META_DIR)) {
403 /* skp the meta data directory */
404 goto again;
405 }
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};