blob: 2538bd317d7e76af0739e93efd2c776b9b2f5753 [file] [log] [blame]
Anthony Liguori9f107512010-04-29 17:44:44 +05301/*
Wei Liuf00d4f52015-11-18 18:05:29 +00002 * 9p Posix callback
Anthony Liguori9f107512010-04-29 17:44:44 +05303 *
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 */
Stefan Weil873c3212011-06-01 12:35:14 +053013
Peter Maydellfbc04122016-01-26 18:17:10 +000014#include "qemu/osdep.h"
Wei Liuebe74f82016-01-07 18:18:02 +000015#include "9p.h"
Greg Kurz996a0d72017-02-26 23:42:18 +010016#include "9p-local.h"
Wei Liu267ae092015-11-18 18:31:52 +000017#include "9p-xattr.h"
Greg Kurz0e35a372017-02-26 23:42:10 +010018#include "9p-util.h"
Stefan Weil69b15212014-05-02 22:22:32 +020019#include "fsdev/qemu-fsdev.h" /* local_ops */
Anthony Liguoric494dd62010-04-29 17:44:59 +053020#include <arpa/inet.h>
Anthony Liguori131dcb22010-04-29 17:44:47 +053021#include <pwd.h>
22#include <grp.h>
Anthony Liguoric494dd62010-04-29 17:44:59 +053023#include <sys/socket.h>
24#include <sys/un.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/xattr.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Greg Kurz63325b12016-01-22 15:12:17 +010027#include "qemu/error-report.h"
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053028#include <libgen.h>
Harsh Prateek Borae06a7652011-10-12 19:11:25 +053029#include <linux/fs.h>
30#ifdef CONFIG_LINUX_MAGIC_H
31#include <linux/magic.h>
32#endif
33#include <sys/ioctl.h>
34
35#ifndef XFS_SUPER_MAGIC
36#define XFS_SUPER_MAGIC 0x58465342
37#endif
38#ifndef EXT2_SUPER_MAGIC
39#define EXT2_SUPER_MAGIC 0xEF53
40#endif
41#ifndef REISERFS_SUPER_MAGIC
42#define REISERFS_SUPER_MAGIC 0x52654973
43#endif
44#ifndef BTRFS_SUPER_MAGIC
45#define BTRFS_SUPER_MAGIC 0x9123683E
46#endif
Anthony Liguori131dcb22010-04-29 17:44:47 +053047
Greg Kurz0e35a372017-02-26 23:42:10 +010048typedef struct {
49 int mountfd;
50} LocalData;
51
Greg Kurz996a0d72017-02-26 23:42:18 +010052int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53 mode_t mode)
54{
55 LocalData *data = fs_ctx->private;
56
57 /* All paths are relative to the path data->mountfd points to */
58 while (*path == '/') {
59 path++;
60 }
61
62 return relative_openat_nofollow(data->mountfd, path, flags, mode);
63}
64
65int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
66{
67 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
68}
69
Greg Kurz99f2cf42017-02-26 23:43:55 +010070static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
71 const char *npath)
72{
73 int serrno = errno;
74 renameat(odirfd, opath, ndirfd, npath);
75 errno = serrno;
76}
77
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053078#define VIRTFS_META_DIR ".virtfs_metadata"
79
Chen Gang4fa4ce72014-03-02 01:36:19 +080080static char *local_mapped_attr_path(FsContext *ctx, const char *path)
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053081{
Michael Tokarev1b6f85e2015-03-12 09:52:30 +030082 int dirlen;
83 const char *name = strrchr(path, '/');
84 if (name) {
85 dirlen = name - path;
86 ++name;
87 } else {
88 name = path;
89 dirlen = 0;
90 }
91 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
92 dirlen, path, VIRTFS_META_DIR, name);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053093}
94
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +053095static FILE *local_fopen(const char *path, const char *mode)
96{
97 int fd, o_mode = 0;
98 FILE *fp;
99 int flags = O_NOFOLLOW;
100 /*
101 * only supports two modes
102 */
103 if (mode[0] == 'r') {
104 flags |= O_RDONLY;
105 } else if (mode[0] == 'w') {
106 flags |= O_WRONLY | O_TRUNC | O_CREAT;
107 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
108 } else {
109 return NULL;
110 }
111 fd = open(path, flags, o_mode);
112 if (fd == -1) {
113 return NULL;
114 }
115 fp = fdopen(fd, mode);
116 if (!fp) {
117 close(fd);
118 }
119 return fp;
120}
121
Greg Kurzf9aef992017-02-26 23:43:48 +0100122static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
123{
124 int fd, o_mode = 0;
125 FILE *fp;
126 int flags;
127 /*
128 * only supports two modes
129 */
130 if (mode[0] == 'r') {
131 flags = O_RDONLY;
132 } else if (mode[0] == 'w') {
133 flags = O_WRONLY | O_TRUNC | O_CREAT;
134 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
135 } else {
136 return NULL;
137 }
138 fd = openat_file(dirfd, name, flags, o_mode);
139 if (fd == -1) {
140 return NULL;
141 }
142 fp = fdopen(fd, mode);
143 if (!fp) {
144 close(fd);
145 }
146 return fp;
147}
148
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530149#define ATTR_MAX 100
Greg Kurzf9aef992017-02-26 23:43:48 +0100150static void local_mapped_file_attr(int dirfd, const char *name,
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530151 struct stat *stbuf)
152{
153 FILE *fp;
154 char buf[ATTR_MAX];
Greg Kurzf9aef992017-02-26 23:43:48 +0100155 int map_dirfd;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530156
Greg Kurz99f2cf42017-02-26 23:43:55 +0100157 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
Greg Kurzf9aef992017-02-26 23:43:48 +0100158 if (map_dirfd == -1) {
159 return;
160 }
161
162 fp = local_fopenat(map_dirfd, name, "r");
163 close_preserve_errno(map_dirfd);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530164 if (!fp) {
165 return;
166 }
167 memset(buf, 0, ATTR_MAX);
168 while (fgets(buf, ATTR_MAX, fp)) {
169 if (!strncmp(buf, "virtfs.uid", 10)) {
170 stbuf->st_uid = atoi(buf+11);
171 } else if (!strncmp(buf, "virtfs.gid", 10)) {
172 stbuf->st_gid = atoi(buf+11);
173 } else if (!strncmp(buf, "virtfs.mode", 11)) {
174 stbuf->st_mode = atoi(buf+12);
175 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
176 stbuf->st_rdev = atoi(buf+12);
177 }
178 memset(buf, 0, ATTR_MAX);
179 }
180 fclose(fp);
181}
182
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530183static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530184{
Greg Kurzf9aef992017-02-26 23:43:48 +0100185 int err = -1;
186 char *dirpath = g_path_get_dirname(fs_path->data);
187 char *name = g_path_get_basename(fs_path->data);
188 int dirfd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530189
Greg Kurzf9aef992017-02-26 23:43:48 +0100190 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
191 if (dirfd == -1) {
192 goto out;
193 }
194
195 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700196 if (err) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800197 goto err_out;
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700198 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530199 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700200 /* Actual credentials are part of extended attrs */
201 uid_t tmp_uid;
202 gid_t tmp_gid;
203 mode_t tmp_mode;
204 dev_t tmp_dev;
Greg Kurzf9aef992017-02-26 23:43:48 +0100205
206 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
207 sizeof(uid_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530208 stbuf->st_uid = le32_to_cpu(tmp_uid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700209 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100210 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
211 sizeof(gid_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530212 stbuf->st_gid = le32_to_cpu(tmp_gid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700213 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100214 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
215 sizeof(mode_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530216 stbuf->st_mode = le32_to_cpu(tmp_mode);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700217 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100218 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
219 sizeof(dev_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530220 stbuf->st_rdev = le64_to_cpu(tmp_dev);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700221 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530222 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurzf9aef992017-02-26 23:43:48 +0100223 local_mapped_file_attr(dirfd, name, stbuf);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700224 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800225
226err_out:
Greg Kurzf9aef992017-02-26 23:43:48 +0100227 close_preserve_errno(dirfd);
228out:
229 g_free(name);
230 g_free(dirpath);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700231 return err;
Anthony Liguori131dcb22010-04-29 17:44:47 +0530232}
233
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530234static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
235{
236 int err;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800237 char *attr_dir;
Markus Armbrusterd3f8e132013-01-22 11:07:58 +0100238 char *tmp_path = g_strdup(path);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530239
Chen Gang4fa4ce72014-03-02 01:36:19 +0800240 attr_dir = g_strdup_printf("%s/%s/%s",
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530241 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
242
243 err = mkdir(attr_dir, 0700);
244 if (err < 0 && errno == EEXIST) {
245 err = 0;
246 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800247 g_free(attr_dir);
Markus Armbrusterd3f8e132013-01-22 11:07:58 +0100248 g_free(tmp_path);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530249 return err;
250}
251
252static int local_set_mapped_file_attr(FsContext *ctx,
253 const char *path, FsCred *credp)
254{
255 FILE *fp;
256 int ret = 0;
257 char buf[ATTR_MAX];
Chen Gang4fa4ce72014-03-02 01:36:19 +0800258 char *attr_path;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530259 int uid = -1, gid = -1, mode = -1, rdev = -1;
260
Chen Gang4fa4ce72014-03-02 01:36:19 +0800261 attr_path = local_mapped_attr_path(ctx, path);
262 fp = local_fopen(attr_path, "r");
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530263 if (!fp) {
264 goto create_map_file;
265 }
266 memset(buf, 0, ATTR_MAX);
267 while (fgets(buf, ATTR_MAX, fp)) {
268 if (!strncmp(buf, "virtfs.uid", 10)) {
269 uid = atoi(buf+11);
270 } else if (!strncmp(buf, "virtfs.gid", 10)) {
271 gid = atoi(buf+11);
272 } else if (!strncmp(buf, "virtfs.mode", 11)) {
273 mode = atoi(buf+12);
274 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
275 rdev = atoi(buf+12);
276 }
277 memset(buf, 0, ATTR_MAX);
278 }
279 fclose(fp);
280 goto update_map_file;
281
282create_map_file:
283 ret = local_create_mapped_attr_dir(ctx, path);
284 if (ret < 0) {
285 goto err_out;
286 }
287
288update_map_file:
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +0530289 fp = local_fopen(attr_path, "w");
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530290 if (!fp) {
291 ret = -1;
292 goto err_out;
293 }
294
295 if (credp->fc_uid != -1) {
296 uid = credp->fc_uid;
297 }
298 if (credp->fc_gid != -1) {
299 gid = credp->fc_gid;
300 }
301 if (credp->fc_mode != -1) {
302 mode = credp->fc_mode;
303 }
304 if (credp->fc_rdev != -1) {
305 rdev = credp->fc_rdev;
306 }
307
308
309 if (uid != -1) {
310 fprintf(fp, "virtfs.uid=%d\n", uid);
311 }
312 if (gid != -1) {
313 fprintf(fp, "virtfs.gid=%d\n", gid);
314 }
315 if (mode != -1) {
316 fprintf(fp, "virtfs.mode=%d\n", mode);
317 }
318 if (rdev != -1) {
319 fprintf(fp, "virtfs.rdev=%d\n", rdev);
320 }
321 fclose(fp);
322
323err_out:
Chen Gang4fa4ce72014-03-02 01:36:19 +0800324 g_free(attr_path);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530325 return ret;
326}
327
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700328static int local_set_xattr(const char *path, FsCred *credp)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530329{
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700330 int err;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530331
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700332 if (credp->fc_uid != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530333 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
334 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700335 if (err) {
336 return err;
337 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530338 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700339 if (credp->fc_gid != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530340 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
341 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700342 if (err) {
343 return err;
344 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530345 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700346 if (credp->fc_mode != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530347 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
348 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700349 if (err) {
350 return err;
351 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530352 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700353 if (credp->fc_rdev != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530354 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
355 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700356 if (err) {
357 return err;
358 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530359 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530360 return 0;
361}
362
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700363static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530364 FsCred *credp)
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700365{
Chen Gang4fa4ce72014-03-02 01:36:19 +0800366 char *buffer;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530367
Chen Gang4fa4ce72014-03-02 01:36:19 +0800368 buffer = rpath(fs_ctx, path);
369 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530370 /*
371 * If we fail to change ownership and if we are
372 * using security model none. Ignore the error
373 */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530374 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800375 goto err;
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530376 }
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700377 }
M. Mohan Kumar2d405642012-01-19 12:21:12 +0530378
Chen Gang4fa4ce72014-03-02 01:36:19 +0800379 if (chmod(buffer, credp->fc_mode & 07777) < 0) {
380 goto err;
M. Mohan Kumar2d405642012-01-19 12:21:12 +0530381 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800382
383 g_free(buffer);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700384 return 0;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800385err:
386 g_free(buffer);
387 return -1;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700388}
389
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530390static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
391 char *buf, size_t bufsz)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530392{
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700393 ssize_t tsize = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530394
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530395 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
396 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700397 int fd;
Greg Kurzbec1e952017-02-26 23:43:40 +0100398
399 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700400 if (fd == -1) {
401 return -1;
402 }
403 do {
404 tsize = read(fd, (void *)buf, bufsz);
405 } while (tsize == -1 && errno == EINTR);
Greg Kurzbec1e952017-02-26 23:43:40 +0100406 close_preserve_errno(fd);
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530407 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
408 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Greg Kurzbec1e952017-02-26 23:43:40 +0100409 char *dirpath = g_path_get_dirname(fs_path->data);
410 char *name = g_path_get_basename(fs_path->data);
411 int dirfd;
412
413 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
414 if (dirfd == -1) {
415 goto out;
416 }
417
418 tsize = readlinkat(dirfd, name, buf, bufsz);
419 close_preserve_errno(dirfd);
420 out:
421 g_free(name);
422 g_free(dirpath);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700423 }
424 return tsize;
Anthony Liguori131dcb22010-04-29 17:44:47 +0530425}
426
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530427static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530428{
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530429 return close(fs->fd);
Anthony Liguori131dcb22010-04-29 17:44:47 +0530430}
431
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530432static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530433{
Greg Kurzf314ea42016-06-06 11:52:34 +0200434 return closedir(fs->dir.stream);
Anthony Liguori131dcb22010-04-29 17:44:47 +0530435}
Anthony Liguori9f107512010-04-29 17:44:44 +0530436
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530437static int local_open(FsContext *ctx, V9fsPath *fs_path,
438 int flags, V9fsFidOpenState *fs)
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530439{
Greg Kurz21328e12017-02-26 23:41:55 +0100440 int fd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530441
Greg Kurz996a0d72017-02-26 23:42:18 +0100442 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
Greg Kurz21328e12017-02-26 23:41:55 +0100443 if (fd == -1) {
444 return -1;
445 }
446 fs->fd = fd;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530447 return fs->fd;
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530448}
449
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530450static int local_opendir(FsContext *ctx,
451 V9fsPath *fs_path, V9fsFidOpenState *fs)
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530452{
Greg Kurz996a0d72017-02-26 23:42:18 +0100453 int dirfd;
Greg Kurz21328e12017-02-26 23:41:55 +0100454 DIR *stream;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530455
Greg Kurz996a0d72017-02-26 23:42:18 +0100456 dirfd = local_opendir_nofollow(ctx, fs_path->data);
457 if (dirfd == -1) {
458 return -1;
459 }
460
461 stream = fdopendir(dirfd);
Greg Kurz21328e12017-02-26 23:41:55 +0100462 if (!stream) {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530463 return -1;
464 }
Greg Kurz21328e12017-02-26 23:41:55 +0100465 fs->dir.stream = stream;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530466 return 0;
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530467}
468
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530469static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530470{
Greg Kurzf314ea42016-06-06 11:52:34 +0200471 rewinddir(fs->dir.stream);
Anthony Liguoria9231552010-04-29 17:44:56 +0530472}
473
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530474static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530475{
Greg Kurzf314ea42016-06-06 11:52:34 +0200476 return telldir(fs->dir.stream);
Anthony Liguoria9231552010-04-29 17:44:56 +0530477}
478
Greg Kurz635324e2016-06-06 11:52:34 +0200479static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530480{
Greg Kurz635324e2016-06-06 11:52:34 +0200481 struct dirent *entry;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530482
483again:
Greg Kurz635324e2016-06-06 11:52:34 +0200484 entry = readdir(fs->dir.stream);
485 if (!entry) {
486 return NULL;
487 }
488
Bastian Blank840a1bf2014-08-22 13:22:21 +0400489 if (ctx->export_flags & V9FS_SM_MAPPED) {
490 entry->d_type = DT_UNKNOWN;
491 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurz635324e2016-06-06 11:52:34 +0200492 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530493 /* skp the meta data directory */
494 goto again;
495 }
Bastian Blank840a1bf2014-08-22 13:22:21 +0400496 entry->d_type = DT_UNKNOWN;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530497 }
Greg Kurz635324e2016-06-06 11:52:34 +0200498
499 return entry;
Anthony Liguoria9231552010-04-29 17:44:56 +0530500}
501
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530502static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
Anthony Liguoria9231552010-04-29 17:44:56 +0530503{
Greg Kurzf314ea42016-06-06 11:52:34 +0200504 seekdir(fs->dir.stream, off);
Anthony Liguoria9231552010-04-29 17:44:56 +0530505}
506
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530507static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
508 const struct iovec *iov,
Sanchit Garg56d15a52010-10-08 11:30:16 +0530509 int iovcnt, off_t offset)
Anthony Liguoria9231552010-04-29 17:44:56 +0530510{
Sanchit Garg56d15a52010-10-08 11:30:16 +0530511#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530512 return preadv(fs->fd, iov, iovcnt, offset);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530513#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530514 int err = lseek(fs->fd, offset, SEEK_SET);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530515 if (err == -1) {
516 return err;
517 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530518 return readv(fs->fd, iov, iovcnt);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530519 }
520#endif
Anthony Liguoria9231552010-04-29 17:44:56 +0530521}
522
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530523static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
524 const struct iovec *iov,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530525 int iovcnt, off_t offset)
Anthony Liguoria9231552010-04-29 17:44:56 +0530526{
Greg Kurz6fe76ac2017-01-23 09:46:13 +0100527 ssize_t ret;
Sanchit Garg56d15a52010-10-08 11:30:16 +0530528#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530529 ret = pwritev(fs->fd, iov, iovcnt, offset);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530530#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530531 int err = lseek(fs->fd, offset, SEEK_SET);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530532 if (err == -1) {
533 return err;
534 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530535 ret = writev(fs->fd, iov, iovcnt);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530536 }
537#endif
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530538#ifdef CONFIG_SYNC_FILE_RANGE
539 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
540 /*
541 * Initiate a writeback. This is not a data integrity sync.
542 * We want to ensure that we don't leave dirty pages in the cache
543 * after write when writeout=immediate is sepcified.
544 */
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530545 sync_file_range(fs->fd, offset, ret,
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530546 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
547 }
548#endif
549 return ret;
Anthony Liguori84493602010-04-29 17:44:58 +0530550}
551
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530552static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530553{
Chen Gang4fa4ce72014-03-02 01:36:19 +0800554 char *buffer;
555 int ret = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530556 char *path = fs_path->data;
557
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530558 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800559 buffer = rpath(fs_ctx, path);
560 ret = local_set_xattr(buffer, credp);
561 g_free(buffer);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530562 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
563 return local_set_mapped_file_attr(fs_ctx, path, credp);
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530564 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
565 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800566 buffer = rpath(fs_ctx, path);
567 ret = chmod(buffer, credp->fc_mode);
568 g_free(buffer);
Venkateswararao Jujjuri (JV)e95ead32010-06-14 13:34:42 -0700569 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800570 return ret;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530571}
572
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530573static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
574 const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530575{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530576 char *path;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700577 int err = -1;
578 int serrno = 0;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530579 V9fsString fullname;
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100580 char *buffer = NULL;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700581
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530582 v9fs_string_init(&fullname);
583 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
584 path = fullname.data;
585
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700586 /* Determine the security model */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530587 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800588 buffer = rpath(fs_ctx, path);
589 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700590 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530591 goto out;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700592 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800593 err = local_set_xattr(buffer, credp);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700594 if (err == -1) {
595 serrno = errno;
596 goto err_end;
597 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530598 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
599
Chen Gang4fa4ce72014-03-02 01:36:19 +0800600 buffer = rpath(fs_ctx, path);
601 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530602 if (err == -1) {
603 goto out;
604 }
605 err = local_set_mapped_file_attr(fs_ctx, path, credp);
606 if (err == -1) {
607 serrno = errno;
608 goto err_end;
609 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530610 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
611 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800612 buffer = rpath(fs_ctx, path);
613 err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700614 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530615 goto out;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700616 }
617 err = local_post_create_passthrough(fs_ctx, path, credp);
618 if (err == -1) {
619 serrno = errno;
620 goto err_end;
621 }
622 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530623 goto out;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700624
625err_end:
Chen Gang4fa4ce72014-03-02 01:36:19 +0800626 remove(buffer);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700627 errno = serrno;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530628out:
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100629 g_free(buffer);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530630 v9fs_string_free(&fullname);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700631 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530632}
633
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530634static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
635 const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530636{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530637 char *path;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700638 int err = -1;
639 int serrno = 0;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530640 V9fsString fullname;
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100641 char *buffer = NULL;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700642
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530643 v9fs_string_init(&fullname);
644 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
645 path = fullname.data;
646
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700647 /* Determine the security model */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530648 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800649 buffer = rpath(fs_ctx, path);
650 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700651 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530652 goto out;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700653 }
654 credp->fc_mode = credp->fc_mode|S_IFDIR;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800655 err = local_set_xattr(buffer, credp);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700656 if (err == -1) {
657 serrno = errno;
658 goto err_end;
659 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530660 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800661 buffer = rpath(fs_ctx, path);
662 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530663 if (err == -1) {
664 goto out;
665 }
666 credp->fc_mode = credp->fc_mode|S_IFDIR;
667 err = local_set_mapped_file_attr(fs_ctx, path, credp);
668 if (err == -1) {
669 serrno = errno;
670 goto err_end;
671 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530672 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
673 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800674 buffer = rpath(fs_ctx, path);
675 err = mkdir(buffer, credp->fc_mode);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700676 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530677 goto out;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700678 }
679 err = local_post_create_passthrough(fs_ctx, path, credp);
680 if (err == -1) {
681 serrno = errno;
682 goto err_end;
683 }
684 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530685 goto out;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700686
687err_end:
Chen Gang4fa4ce72014-03-02 01:36:19 +0800688 remove(buffer);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700689 errno = serrno;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530690out:
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100691 g_free(buffer);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530692 v9fs_string_free(&fullname);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700693 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530694}
695
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530696static int local_fstat(FsContext *fs_ctx, int fid_type,
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530697 V9fsFidOpenState *fs, struct stat *stbuf)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530698{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530699 int err, fd;
700
701 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200702 fd = dirfd(fs->dir.stream);
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530703 } else {
704 fd = fs->fd;
705 }
706
707 err = fstat(fd, stbuf);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700708 if (err) {
709 return err;
710 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530711 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700712 /* Actual credentials are part of extended attrs */
713 uid_t tmp_uid;
714 gid_t tmp_gid;
715 mode_t tmp_mode;
716 dev_t tmp_dev;
717
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530718 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
719 stbuf->st_uid = le32_to_cpu(tmp_uid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700720 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530721 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
722 stbuf->st_gid = le32_to_cpu(tmp_gid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700723 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530724 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
725 stbuf->st_mode = le32_to_cpu(tmp_mode);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700726 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530727 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
728 stbuf->st_rdev = le64_to_cpu(tmp_dev);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700729 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530730 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
731 errno = EOPNOTSUPP;
732 return -1;
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700733 }
734 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530735}
736
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530737static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530738 int flags, FsCred *credp, V9fsFidOpenState *fs)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530739{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530740 char *path;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700741 int fd = -1;
742 int err = -1;
743 int serrno = 0;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530744 V9fsString fullname;
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100745 char *buffer = NULL;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700746
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +0530747 /*
748 * Mark all the open to not follow symlinks
749 */
750 flags |= O_NOFOLLOW;
751
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530752 v9fs_string_init(&fullname);
753 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
754 path = fullname.data;
755
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700756 /* Determine the security model */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530757 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800758 buffer = rpath(fs_ctx, path);
759 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700760 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530761 err = fd;
762 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700763 }
764 credp->fc_mode = credp->fc_mode|S_IFREG;
765 /* Set cleint credentials in xattr */
Chen Gang4fa4ce72014-03-02 01:36:19 +0800766 err = local_set_xattr(buffer, credp);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700767 if (err == -1) {
768 serrno = errno;
769 goto err_end;
770 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530771 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800772 buffer = rpath(fs_ctx, path);
773 fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530774 if (fd == -1) {
775 err = fd;
776 goto out;
777 }
778 credp->fc_mode = credp->fc_mode|S_IFREG;
779 /* Set client credentials in .virtfs_metadata directory files */
780 err = local_set_mapped_file_attr(fs_ctx, path, credp);
781 if (err == -1) {
782 serrno = errno;
783 goto err_end;
784 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530785 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
786 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800787 buffer = rpath(fs_ctx, path);
788 fd = open(buffer, flags, credp->fc_mode);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700789 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530790 err = fd;
791 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700792 }
793 err = local_post_create_passthrough(fs_ctx, path, credp);
794 if (err == -1) {
795 serrno = errno;
796 goto err_end;
797 }
798 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530799 err = fd;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530800 fs->fd = fd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530801 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700802
803err_end:
804 close(fd);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800805 remove(buffer);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700806 errno = serrno;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530807out:
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100808 g_free(buffer);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530809 v9fs_string_free(&fullname);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700810 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530811}
812
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700813
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700814static int local_symlink(FsContext *fs_ctx, const char *oldpath,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530815 V9fsPath *dir_path, const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530816{
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700817 int err = -1;
818 int serrno = 0;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530819 char *newpath;
820 V9fsString fullname;
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100821 char *buffer = NULL;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700822
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530823 v9fs_string_init(&fullname);
824 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
825 newpath = fullname.data;
826
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700827 /* Determine the security model */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530828 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700829 int fd;
830 ssize_t oldpath_size, write_size;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800831 buffer = rpath(fs_ctx, newpath);
832 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700833 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530834 err = fd;
835 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700836 }
837 /* Write the oldpath (target) to the file. */
Harsh Prateek Boraf35bde22011-02-02 10:20:33 +0530838 oldpath_size = strlen(oldpath);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700839 do {
840 write_size = write(fd, (void *)oldpath, oldpath_size);
841 } while (write_size == -1 && errno == EINTR);
842
843 if (write_size != oldpath_size) {
844 serrno = errno;
845 close(fd);
846 err = -1;
847 goto err_end;
848 }
849 close(fd);
850 /* Set cleint credentials in symlink's xattr */
851 credp->fc_mode = credp->fc_mode|S_IFLNK;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800852 err = local_set_xattr(buffer, credp);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700853 if (err == -1) {
854 serrno = errno;
855 goto err_end;
856 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530857 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
858 int fd;
859 ssize_t oldpath_size, write_size;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800860 buffer = rpath(fs_ctx, newpath);
861 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530862 if (fd == -1) {
863 err = fd;
864 goto out;
865 }
866 /* Write the oldpath (target) to the file. */
867 oldpath_size = strlen(oldpath);
868 do {
869 write_size = write(fd, (void *)oldpath, oldpath_size);
870 } while (write_size == -1 && errno == EINTR);
871
872 if (write_size != oldpath_size) {
873 serrno = errno;
874 close(fd);
875 err = -1;
876 goto err_end;
877 }
878 close(fd);
879 /* Set cleint credentials in symlink's xattr */
880 credp->fc_mode = credp->fc_mode|S_IFLNK;
881 err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
882 if (err == -1) {
883 serrno = errno;
884 goto err_end;
885 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530886 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
887 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800888 buffer = rpath(fs_ctx, newpath);
889 err = symlink(oldpath, buffer);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700890 if (err) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530891 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700892 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800893 err = lchown(buffer, credp->fc_uid, credp->fc_gid);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700894 if (err == -1) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530895 /*
896 * If we fail to change ownership and if we are
897 * using security model none. Ignore the error
898 */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530899 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530900 serrno = errno;
901 goto err_end;
902 } else
903 err = 0;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700904 }
905 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530906 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700907
908err_end:
Chen Gang4fa4ce72014-03-02 01:36:19 +0800909 remove(buffer);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700910 errno = serrno;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530911out:
Stefan Weil4ed7b2c2015-03-14 11:45:18 +0100912 g_free(buffer);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530913 v9fs_string_free(&fullname);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700914 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530915}
916
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530917static int local_link(FsContext *ctx, V9fsPath *oldpath,
918 V9fsPath *dirpath, const char *name)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530919{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530920 int ret;
921 V9fsString newpath;
Chen Gang4fa4ce72014-03-02 01:36:19 +0800922 char *buffer, *buffer1;
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100923 int serrno;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530924
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530925 v9fs_string_init(&newpath);
926 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
927
Chen Gang4fa4ce72014-03-02 01:36:19 +0800928 buffer = rpath(ctx, oldpath->data);
929 buffer1 = rpath(ctx, newpath.data);
930 ret = link(buffer, buffer1);
931 g_free(buffer);
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100932 if (ret < 0) {
933 goto out;
934 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530935
936 /* now link the virtfs_metadata files */
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100937 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
938 char *vbuffer, *vbuffer1;
939
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530940 /* Link the .virtfs_metadata files. Create the metada directory */
941 ret = local_create_mapped_attr_dir(ctx, newpath.data);
942 if (ret < 0) {
943 goto err_out;
944 }
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100945 vbuffer = local_mapped_attr_path(ctx, oldpath->data);
946 vbuffer1 = local_mapped_attr_path(ctx, newpath.data);
947 ret = link(vbuffer, vbuffer1);
948 g_free(vbuffer);
949 g_free(vbuffer1);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530950 if (ret < 0 && errno != ENOENT) {
951 goto err_out;
952 }
953 }
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100954 goto out;
955
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530956err_out:
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100957 serrno = errno;
958 remove(buffer1);
959 errno = serrno;
960out:
961 g_free(buffer1);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530962 v9fs_string_free(&newpath);
963 return ret;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530964}
965
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530966static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530967{
Greg Kurzac125d92017-02-26 23:43:32 +0100968 int fd, ret;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530969
Greg Kurzac125d92017-02-26 23:43:32 +0100970 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
971 if (fd == -1) {
972 return -1;
973 }
974 ret = ftruncate(fd, size);
975 close_preserve_errno(fd);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800976 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530977}
978
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530979static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530980{
Chen Gang4fa4ce72014-03-02 01:36:19 +0800981 char *buffer;
982 int ret = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530983 char *path = fs_path->data;
984
Sripathi Kodic79ce732010-06-17 18:18:47 +0530985 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
Aneesh Kumar K.V17b19712011-10-25 12:10:39 +0530986 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
987 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800988 buffer = rpath(fs_ctx, path);
989 ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
990 g_free(buffer);
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530991 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800992 buffer = rpath(fs_ctx, path);
993 ret = local_set_xattr(buffer, credp);
994 g_free(buffer);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530995 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
996 return local_set_mapped_file_attr(fs_ctx, path, credp);
Venkateswararao Jujjuri (JV)f7613be2010-06-14 13:34:43 -0700997 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800998 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530999}
1000
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301001static int local_utimensat(FsContext *s, V9fsPath *fs_path,
Hidetoshi Seto38671422010-11-24 11:38:10 +09001002 const struct timespec *buf)
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301003{
Greg Kurza33eda02017-02-26 23:43:17 +01001004 char *dirpath = g_path_get_dirname(fs_path->data);
1005 char *name = g_path_get_basename(fs_path->data);
1006 int dirfd, ret = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301007
Greg Kurza33eda02017-02-26 23:43:17 +01001008 dirfd = local_opendir_nofollow(s, dirpath);
1009 if (dirfd == -1) {
1010 goto out;
1011 }
1012
1013 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
1014 close_preserve_errno(dirfd);
1015out:
1016 g_free(dirpath);
1017 g_free(name);
Chen Gang4fa4ce72014-03-02 01:36:19 +08001018 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301019}
1020
Greg Kurzdf4938a2017-02-26 23:43:00 +01001021static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
1022 int flags)
1023{
1024 int ret = -1;
1025
1026 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1027 int map_dirfd;
1028
1029 if (flags == AT_REMOVEDIR) {
1030 int fd;
1031
1032 fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
1033 if (fd == -1) {
1034 goto err_out;
1035 }
1036 /*
1037 * If directory remove .virtfs_metadata contained in the
1038 * directory
1039 */
1040 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
1041 close_preserve_errno(fd);
1042 if (ret < 0 && errno != ENOENT) {
1043 /*
1044 * We didn't had the .virtfs_metadata file. May be file created
1045 * in non-mapped mode ?. Ignore ENOENT.
1046 */
1047 goto err_out;
1048 }
1049 }
1050 /*
1051 * Now remove the name from parent directory
1052 * .virtfs_metadata directory.
1053 */
1054 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
1055 ret = unlinkat(map_dirfd, name, 0);
1056 close_preserve_errno(map_dirfd);
1057 if (ret < 0 && errno != ENOENT) {
1058 /*
1059 * We didn't had the .virtfs_metadata file. May be file created
1060 * in non-mapped mode ?. Ignore ENOENT.
1061 */
1062 goto err_out;
1063 }
1064 }
1065
1066 ret = unlinkat(dirfd, name, flags);
1067err_out:
1068 return ret;
1069}
1070
Anthony Liguori5bae1902010-04-29 17:45:01 +05301071static int local_remove(FsContext *ctx, const char *path)
1072{
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301073 struct stat stbuf;
Greg Kurza0e640a2017-02-26 23:43:08 +01001074 char *dirpath = g_path_get_dirname(path);
1075 char *name = g_path_get_basename(path);
1076 int flags = 0;
1077 int dirfd;
1078 int err = -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301079
Greg Kurza0e640a2017-02-26 23:43:08 +01001080 dirfd = local_opendir_nofollow(ctx, dirpath);
1081 if (dirfd) {
1082 goto out;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301083 }
Chen Gang4fa4ce72014-03-02 01:36:19 +08001084
Greg Kurza0e640a2017-02-26 23:43:08 +01001085 if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1086 goto err_out;
1087 }
1088
1089 if (S_ISDIR(stbuf.st_mode)) {
1090 flags |= AT_REMOVEDIR;
1091 }
1092
1093 err = local_unlinkat_common(ctx, dirfd, name, flags);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301094err_out:
Greg Kurza0e640a2017-02-26 23:43:08 +01001095 close_preserve_errno(dirfd);
1096out:
1097 g_free(name);
1098 g_free(dirpath);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301099 return err;
Anthony Liguori5bae1902010-04-29 17:45:01 +05301100}
1101
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301102static int local_fsync(FsContext *ctx, int fid_type,
1103 V9fsFidOpenState *fs, int datasync)
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301104{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301105 int fd;
1106
1107 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +02001108 fd = dirfd(fs->dir.stream);
Venkateswararao Jujjuri (JV)49594972010-10-22 10:08:45 -07001109 } else {
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301110 fd = fs->fd;
1111 }
1112
1113 if (datasync) {
1114 return qemu_fdatasync(fd);
1115 } else {
1116 return fsync(fd);
Venkateswararao Jujjuri (JV)49594972010-10-22 10:08:45 -07001117 }
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301118}
1119
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301120static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301121{
Greg Kurz31e51d12017-02-26 23:43:25 +01001122 int fd, ret;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301123
Greg Kurz31e51d12017-02-26 23:43:25 +01001124 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1125 ret = fstatfs(fd, stbuf);
1126 close_preserve_errno(fd);
Chen Gang4fa4ce72014-03-02 01:36:19 +08001127 return ret;
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301128}
1129
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301130static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301131 const char *name, void *value, size_t size)
1132{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301133 char *path = fs_path->data;
1134
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301135 return v9fs_get_xattr(ctx, path, name, value, size);
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301136}
1137
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301138static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301139 void *value, size_t size)
1140{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301141 char *path = fs_path->data;
1142
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301143 return v9fs_list_xattr(ctx, path, value, size);
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301144}
1145
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301146static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301147 void *value, size_t size, int flags)
1148{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301149 char *path = fs_path->data;
1150
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301151 return v9fs_set_xattr(ctx, path, name, value, size, flags);
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301152}
1153
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301154static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1155 const char *name)
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301156{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301157 char *path = fs_path->data;
1158
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301159 return v9fs_remove_xattr(ctx, path, name);
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301160}
1161
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301162static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1163 const char *name, V9fsPath *target)
1164{
1165 if (dir_path) {
Greg Kurze3e83f22016-09-16 08:56:15 +02001166 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301167 } else {
Greg Kurze3e83f22016-09-16 08:56:15 +02001168 v9fs_path_sprintf(target, "%s", name);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301169 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301170 return 0;
1171}
1172
1173static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1174 const char *old_name, V9fsPath *newdir,
1175 const char *new_name)
1176{
1177 int ret;
Greg Kurz99f2cf42017-02-26 23:43:55 +01001178 int odirfd, ndirfd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301179
Greg Kurz99f2cf42017-02-26 23:43:55 +01001180 odirfd = local_opendir_nofollow(ctx, olddir->data);
1181 if (odirfd == -1) {
1182 return -1;
1183 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301184
Greg Kurz99f2cf42017-02-26 23:43:55 +01001185 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1186 if (ndirfd == -1) {
1187 close_preserve_errno(odirfd);
1188 return -1;
1189 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301190
Greg Kurz99f2cf42017-02-26 23:43:55 +01001191 ret = renameat(odirfd, old_name, ndirfd, new_name);
1192 if (ret < 0) {
1193 goto out;
1194 }
1195
1196 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1197 int omap_dirfd, nmap_dirfd;
1198
1199 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1200 if (ret < 0 && errno != EEXIST) {
1201 goto err_undo_rename;
1202 }
1203
Greg Kurz6dd4b1f2017-02-26 23:44:11 +01001204 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
Greg Kurz99f2cf42017-02-26 23:43:55 +01001205 if (omap_dirfd == -1) {
1206 goto err;
1207 }
1208
Greg Kurz6dd4b1f2017-02-26 23:44:11 +01001209 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
Greg Kurz99f2cf42017-02-26 23:43:55 +01001210 if (nmap_dirfd == -1) {
1211 close_preserve_errno(omap_dirfd);
1212 goto err;
1213 }
1214
1215 /* rename the .virtfs_metadata files */
1216 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1217 close_preserve_errno(nmap_dirfd);
1218 close_preserve_errno(omap_dirfd);
1219 if (ret < 0 && errno != ENOENT) {
1220 goto err_undo_rename;
1221 }
1222
1223 ret = 0;
1224 }
1225 goto out;
1226
1227err:
1228 ret = -1;
1229err_undo_rename:
1230 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1231out:
1232 close_preserve_errno(ndirfd);
1233 close_preserve_errno(odirfd);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301234 return ret;
1235}
1236
Greg Kurzd2767ed2017-02-26 23:44:03 +01001237static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1238{
1239 path->data = g_path_get_dirname(str);
1240 path->size = strlen(path->data) + 1;
1241}
1242
1243static int local_rename(FsContext *ctx, const char *oldpath,
1244 const char *newpath)
1245{
1246 int err;
1247 char *oname = g_path_get_basename(oldpath);
1248 char *nname = g_path_get_basename(newpath);
1249 V9fsPath olddir, newdir;
1250
1251 v9fs_path_init_dirname(&olddir, oldpath);
1252 v9fs_path_init_dirname(&newdir, newpath);
1253
1254 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1255
1256 v9fs_path_free(&newdir);
1257 v9fs_path_free(&olddir);
1258 g_free(nname);
1259 g_free(oname);
1260
1261 return err;
1262}
1263
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301264static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1265 const char *name, int flags)
1266{
1267 int ret;
Greg Kurzdf4938a2017-02-26 23:43:00 +01001268 int dirfd;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301269
Greg Kurzdf4938a2017-02-26 23:43:00 +01001270 dirfd = local_opendir_nofollow(ctx, dir->data);
1271 if (dirfd == -1) {
1272 return -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301273 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301274
Greg Kurzdf4938a2017-02-26 23:43:00 +01001275 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1276 close_preserve_errno(dirfd);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301277 return ret;
1278}
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301279
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301280static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1281 mode_t st_mode, uint64_t *st_gen)
1282{
Paolo Bonziniae0f9402011-11-21 09:29:11 +01001283#ifdef FS_IOC_GETVERSION
Kirill A. Shutemov0e5fc992014-01-28 17:08:24 +02001284 int err;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301285 V9fsFidOpenState fid_open;
1286
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301287 /*
1288 * Do not try to open special files like device nodes, fifos etc
1289 * We can get fd for regular files and directories only
1290 */
1291 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
Kirill A. Shutemov1a9978a2014-01-28 17:08:26 +02001292 errno = ENOTTY;
1293 return -1;
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301294 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301295 err = local_open(ctx, path, O_RDONLY, &fid_open);
1296 if (err < 0) {
1297 return err;
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301298 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301299 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1300 local_close(ctx, &fid_open);
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301301 return err;
Kirill A. Shutemov0e5fc992014-01-28 17:08:24 +02001302#else
1303 errno = ENOTTY;
1304 return -1;
1305#endif
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301306}
1307
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301308static int local_init(FsContext *ctx)
1309{
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301310 struct statfs stbuf;
Greg Kurz0e35a372017-02-26 23:42:10 +01001311 LocalData *data = g_malloc(sizeof(*data));
1312
1313 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1314 if (data->mountfd == -1) {
1315 goto err;
1316 }
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301317
Greg Kurz00c90bd2017-02-26 23:41:48 +01001318#ifdef FS_IOC_GETVERSION
1319 /*
1320 * use ioc_getversion only if the ioctl is definied
1321 */
Greg Kurz0e35a372017-02-26 23:42:10 +01001322 if (fstatfs(data->mountfd, &stbuf) < 0) {
1323 close_preserve_errno(data->mountfd);
1324 goto err;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001325 }
1326 switch (stbuf.f_type) {
1327 case EXT2_SUPER_MAGIC:
1328 case BTRFS_SUPER_MAGIC:
1329 case REISERFS_SUPER_MAGIC:
1330 case XFS_SUPER_MAGIC:
1331 ctx->exops.get_st_gen = local_ioc_getversion;
1332 break;
1333 }
1334#endif
1335
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301336 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1337 ctx->xops = passthrough_xattr_ops;
1338 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1339 ctx->xops = mapped_xattr_ops;
1340 } else if (ctx->export_flags & V9FS_SM_NONE) {
1341 ctx->xops = none_xattr_ops;
1342 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1343 /*
1344 * xattr operation for mapped-file and passthrough
1345 * remain same.
1346 */
1347 ctx->xops = passthrough_xattr_ops;
1348 }
Aneesh Kumar K.Vc98f1d42011-10-12 20:59:18 +05301349 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001350
Greg Kurz0e35a372017-02-26 23:42:10 +01001351 ctx->private = data;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001352 return 0;
Greg Kurz0e35a372017-02-26 23:42:10 +01001353
1354err:
1355 g_free(data);
1356 return -1;
1357}
1358
1359static void local_cleanup(FsContext *ctx)
1360{
1361 LocalData *data = ctx->private;
1362
1363 close(data->mountfd);
1364 g_free(data);
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301365}
1366
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301367static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1368{
1369 const char *sec_model = qemu_opt_get(opts, "security_model");
1370 const char *path = qemu_opt_get(opts, "path");
1371
1372 if (!sec_model) {
Greg Kurz63325b12016-01-22 15:12:17 +01001373 error_report("Security model not specified, local fs needs security model");
1374 error_printf("valid options are:"
1375 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301376 return -1;
1377 }
1378
1379 if (!strcmp(sec_model, "passthrough")) {
1380 fse->export_flags |= V9FS_SM_PASSTHROUGH;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301381 } else if (!strcmp(sec_model, "mapped") ||
1382 !strcmp(sec_model, "mapped-xattr")) {
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301383 fse->export_flags |= V9FS_SM_MAPPED;
1384 } else if (!strcmp(sec_model, "none")) {
1385 fse->export_flags |= V9FS_SM_NONE;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301386 } else if (!strcmp(sec_model, "mapped-file")) {
1387 fse->export_flags |= V9FS_SM_MAPPED_FILE;
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301388 } else {
Greg Kurz63325b12016-01-22 15:12:17 +01001389 error_report("Invalid security model %s specified", sec_model);
1390 error_printf("valid options are:"
1391 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301392 return -1;
1393 }
1394
1395 if (!path) {
Greg Kurz63325b12016-01-22 15:12:17 +01001396 error_report("fsdev: No path specified");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301397 return -1;
1398 }
1399 fse->path = g_strdup(path);
1400
1401 return 0;
1402}
1403
Anthony Liguori9f107512010-04-29 17:44:44 +05301404FileOperations local_ops = {
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301405 .parse_opts = local_parse_opts,
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301406 .init = local_init,
Greg Kurz0e35a372017-02-26 23:42:10 +01001407 .cleanup = local_cleanup,
Anthony Liguori131dcb22010-04-29 17:44:47 +05301408 .lstat = local_lstat,
Anthony Liguori131dcb22010-04-29 17:44:47 +05301409 .readlink = local_readlink,
1410 .close = local_close,
1411 .closedir = local_closedir,
Anthony Liguoria6568fe2010-04-29 17:44:55 +05301412 .open = local_open,
1413 .opendir = local_opendir,
Anthony Liguoria9231552010-04-29 17:44:56 +05301414 .rewinddir = local_rewinddir,
1415 .telldir = local_telldir,
Greg Kurz635324e2016-06-06 11:52:34 +02001416 .readdir = local_readdir,
Anthony Liguoria9231552010-04-29 17:44:56 +05301417 .seekdir = local_seekdir,
Sanchit Garg56d15a52010-10-08 11:30:16 +05301418 .preadv = local_preadv,
1419 .pwritev = local_pwritev,
Anthony Liguoric494dd62010-04-29 17:44:59 +05301420 .chmod = local_chmod,
1421 .mknod = local_mknod,
Anthony Liguoric494dd62010-04-29 17:44:59 +05301422 .mkdir = local_mkdir,
1423 .fstat = local_fstat,
1424 .open2 = local_open2,
1425 .symlink = local_symlink,
1426 .link = local_link,
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301427 .truncate = local_truncate,
1428 .rename = local_rename,
1429 .chown = local_chown,
M. Mohan Kumar74bc02b2010-06-09 19:14:38 +05301430 .utimensat = local_utimensat,
Anthony Liguori5bae1902010-04-29 17:45:01 +05301431 .remove = local_remove,
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301432 .fsync = local_fsync,
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301433 .statfs = local_statfs,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301434 .lgetxattr = local_lgetxattr,
1435 .llistxattr = local_llistxattr,
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301436 .lsetxattr = local_lsetxattr,
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301437 .lremovexattr = local_lremovexattr,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301438 .name_to_path = local_name_to_path,
1439 .renameat = local_renameat,
1440 .unlinkat = local_unlinkat,
Anthony Liguori9f107512010-04-29 17:44:44 +05301441};