blob: 09f6a46d61b8eb8f59e095747282c7a7f19fabb4 [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;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053051
Greg Kurz996a0d72017-02-26 23:42:18 +010052int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
53 mode_t mode)
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053054{
Greg Kurz996a0d72017-02-26 23:42:18 +010055 LocalData *data = fs_ctx->private;
56
57 /* All paths are relative to the path data->mountfd points to */
58 while (*path == '/') {
59 path++;
Michael Tokarev1b6f85e2015-03-12 09:52:30 +030060 }
Greg Kurz996a0d72017-02-26 23:42:18 +010061
62 return relative_openat_nofollow(data->mountfd, path, flags, mode);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053063}
64
Greg Kurz996a0d72017-02-26 23:42:18 +010065int 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
Greg Kurzad0b46e2017-02-26 23:44:20 +010078static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
79{
80 int serrno = errno;
81 unlinkat(dirfd, path, flags);
82 errno = serrno;
83}
84
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +053085#define VIRTFS_META_DIR ".virtfs_metadata"
86
Greg Kurzf9aef992017-02-26 23:43:48 +010087static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +053088{
89 int fd, o_mode = 0;
90 FILE *fp;
Greg Kurzf9aef992017-02-26 23:43:48 +010091 int flags;
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +053092 /*
93 * only supports two modes
94 */
95 if (mode[0] == 'r') {
Greg Kurzf9aef992017-02-26 23:43:48 +010096 flags = O_RDONLY;
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +053097 } else if (mode[0] == 'w') {
Greg Kurzf9aef992017-02-26 23:43:48 +010098 flags = O_WRONLY | O_TRUNC | O_CREAT;
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +053099 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
100 } else {
101 return NULL;
102 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100103 fd = openat_file(dirfd, name, flags, o_mode);
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +0530104 if (fd == -1) {
105 return NULL;
106 }
107 fp = fdopen(fd, mode);
108 if (!fp) {
109 close(fd);
110 }
111 return fp;
112}
113
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530114#define ATTR_MAX 100
Greg Kurzf9aef992017-02-26 23:43:48 +0100115static void local_mapped_file_attr(int dirfd, const char *name,
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530116 struct stat *stbuf)
117{
118 FILE *fp;
119 char buf[ATTR_MAX];
Greg Kurzf9aef992017-02-26 23:43:48 +0100120 int map_dirfd;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530121
Greg Kurz99f2cf42017-02-26 23:43:55 +0100122 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
Greg Kurzf9aef992017-02-26 23:43:48 +0100123 if (map_dirfd == -1) {
124 return;
125 }
126
127 fp = local_fopenat(map_dirfd, name, "r");
128 close_preserve_errno(map_dirfd);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530129 if (!fp) {
130 return;
131 }
132 memset(buf, 0, ATTR_MAX);
133 while (fgets(buf, ATTR_MAX, fp)) {
134 if (!strncmp(buf, "virtfs.uid", 10)) {
135 stbuf->st_uid = atoi(buf+11);
136 } else if (!strncmp(buf, "virtfs.gid", 10)) {
137 stbuf->st_gid = atoi(buf+11);
138 } else if (!strncmp(buf, "virtfs.mode", 11)) {
139 stbuf->st_mode = atoi(buf+12);
140 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
141 stbuf->st_rdev = atoi(buf+12);
142 }
143 memset(buf, 0, ATTR_MAX);
144 }
145 fclose(fp);
146}
147
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530148static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530149{
Greg Kurzf9aef992017-02-26 23:43:48 +0100150 int err = -1;
151 char *dirpath = g_path_get_dirname(fs_path->data);
152 char *name = g_path_get_basename(fs_path->data);
153 int dirfd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530154
Greg Kurzf9aef992017-02-26 23:43:48 +0100155 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
156 if (dirfd == -1) {
157 goto out;
158 }
159
160 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700161 if (err) {
Chen Gang4fa4ce72014-03-02 01:36:19 +0800162 goto err_out;
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700163 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530164 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700165 /* Actual credentials are part of extended attrs */
166 uid_t tmp_uid;
167 gid_t tmp_gid;
168 mode_t tmp_mode;
169 dev_t tmp_dev;
Greg Kurzf9aef992017-02-26 23:43:48 +0100170
171 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
172 sizeof(uid_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530173 stbuf->st_uid = le32_to_cpu(tmp_uid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700174 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100175 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
176 sizeof(gid_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530177 stbuf->st_gid = le32_to_cpu(tmp_gid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700178 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100179 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
180 sizeof(mode_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530181 stbuf->st_mode = le32_to_cpu(tmp_mode);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700182 }
Greg Kurzf9aef992017-02-26 23:43:48 +0100183 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
184 sizeof(dev_t)) > 0) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530185 stbuf->st_rdev = le64_to_cpu(tmp_dev);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700186 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530187 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurzf9aef992017-02-26 23:43:48 +0100188 local_mapped_file_attr(dirfd, name, stbuf);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700189 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800190
191err_out:
Greg Kurzf9aef992017-02-26 23:43:48 +0100192 close_preserve_errno(dirfd);
193out:
194 g_free(name);
195 g_free(dirpath);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700196 return err;
Anthony Liguori131dcb22010-04-29 17:44:47 +0530197}
198
Greg Kurze3187a42017-02-26 23:44:28 +0100199static int local_set_mapped_file_attrat(int dirfd, const char *name,
200 FsCred *credp)
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530201{
202 FILE *fp;
Greg Kurze3187a42017-02-26 23:44:28 +0100203 int ret;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530204 char buf[ATTR_MAX];
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530205 int uid = -1, gid = -1, mode = -1, rdev = -1;
Greg Kurze3187a42017-02-26 23:44:28 +0100206 int map_dirfd;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530207
Greg Kurze3187a42017-02-26 23:44:28 +0100208 ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
209 if (ret < 0 && errno != EEXIST) {
210 return -1;
211 }
212
213 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
214 if (map_dirfd == -1) {
215 return -1;
216 }
217
218 fp = local_fopenat(map_dirfd, name, "r");
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530219 if (!fp) {
Greg Kurze3187a42017-02-26 23:44:28 +0100220 if (errno == ENOENT) {
221 goto update_map_file;
222 } else {
223 close_preserve_errno(map_dirfd);
224 return -1;
225 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530226 }
227 memset(buf, 0, ATTR_MAX);
228 while (fgets(buf, ATTR_MAX, fp)) {
229 if (!strncmp(buf, "virtfs.uid", 10)) {
Greg Kurze3187a42017-02-26 23:44:28 +0100230 uid = atoi(buf + 11);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530231 } else if (!strncmp(buf, "virtfs.gid", 10)) {
Greg Kurze3187a42017-02-26 23:44:28 +0100232 gid = atoi(buf + 11);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530233 } else if (!strncmp(buf, "virtfs.mode", 11)) {
Greg Kurze3187a42017-02-26 23:44:28 +0100234 mode = atoi(buf + 12);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530235 } else if (!strncmp(buf, "virtfs.rdev", 11)) {
Greg Kurze3187a42017-02-26 23:44:28 +0100236 rdev = atoi(buf + 12);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530237 }
238 memset(buf, 0, ATTR_MAX);
239 }
240 fclose(fp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530241
242update_map_file:
Greg Kurze3187a42017-02-26 23:44:28 +0100243 fp = local_fopenat(map_dirfd, name, "w");
244 close_preserve_errno(map_dirfd);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530245 if (!fp) {
Greg Kurze3187a42017-02-26 23:44:28 +0100246 return -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530247 }
248
249 if (credp->fc_uid != -1) {
250 uid = credp->fc_uid;
251 }
252 if (credp->fc_gid != -1) {
253 gid = credp->fc_gid;
254 }
255 if (credp->fc_mode != -1) {
256 mode = credp->fc_mode;
257 }
258 if (credp->fc_rdev != -1) {
259 rdev = credp->fc_rdev;
260 }
261
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530262 if (uid != -1) {
263 fprintf(fp, "virtfs.uid=%d\n", uid);
264 }
265 if (gid != -1) {
266 fprintf(fp, "virtfs.gid=%d\n", gid);
267 }
268 if (mode != -1) {
269 fprintf(fp, "virtfs.mode=%d\n", mode);
270 }
271 if (rdev != -1) {
272 fprintf(fp, "virtfs.rdev=%d\n", rdev);
273 }
274 fclose(fp);
275
Greg Kurze3187a42017-02-26 23:44:28 +0100276 return 0;
277}
278
279static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
280{
281 int fd, ret;
282
283 /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
284 * Unfortunately, the linux kernel doesn't implement it yet. As an
285 * alternative, let's open the file and use fchmod() instead. This
286 * may fail depending on the permissions of the file, but it is the
287 * best we can do to avoid TOCTTOU. We first try to open read-only
288 * in case name points to a directory. If that fails, we try write-only
289 * in case name doesn't point to a directory.
290 */
291 fd = openat_file(dirfd, name, O_RDONLY, 0);
292 if (fd == -1) {
293 /* In case the file is writable-only and isn't a directory. */
294 if (errno == EACCES) {
295 fd = openat_file(dirfd, name, O_WRONLY, 0);
296 }
297 if (fd == -1 && errno == EISDIR) {
298 errno = EACCES;
299 }
300 }
301 if (fd == -1) {
302 return -1;
303 }
304 ret = fchmod(fd, mode);
305 close_preserve_errno(fd);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530306 return ret;
307}
308
Greg Kurze3187a42017-02-26 23:44:28 +0100309static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530310{
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700311 int err;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530312
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700313 if (credp->fc_uid != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530314 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
Greg Kurze3187a42017-02-26 23:44:28 +0100315 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
316 sizeof(uid_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700317 if (err) {
318 return err;
319 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530320 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700321 if (credp->fc_gid != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530322 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
Greg Kurze3187a42017-02-26 23:44:28 +0100323 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
324 sizeof(gid_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700325 if (err) {
326 return err;
327 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530328 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700329 if (credp->fc_mode != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530330 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
Greg Kurze3187a42017-02-26 23:44:28 +0100331 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
332 sizeof(mode_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700333 if (err) {
334 return err;
335 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530336 }
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700337 if (credp->fc_rdev != -1) {
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530338 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
Greg Kurze3187a42017-02-26 23:44:28 +0100339 err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
340 sizeof(dev_t), 0);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700341 if (err) {
342 return err;
343 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530344 }
Anthony Liguori131dcb22010-04-29 17:44:47 +0530345 return 0;
346}
347
Greg Kurzd815e722017-02-26 23:44:54 +0100348static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
349 const char *name, FsCred *credp)
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700350{
Greg Kurzd815e722017-02-26 23:44:54 +0100351 if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
352 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) < 0) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530353 /*
354 * If we fail to change ownership and if we are
355 * using security model none. Ignore the error
356 */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530357 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
Greg Kurzd815e722017-02-26 23:44:54 +0100358 return -1;
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530359 }
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700360 }
M. Mohan Kumar2d405642012-01-19 12:21:12 +0530361
Greg Kurzd815e722017-02-26 23:44:54 +0100362 return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700363}
364
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530365static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
366 char *buf, size_t bufsz)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530367{
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700368 ssize_t tsize = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530369
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530370 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
371 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700372 int fd;
Greg Kurzbec1e952017-02-26 23:43:40 +0100373
374 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700375 if (fd == -1) {
376 return -1;
377 }
378 do {
379 tsize = read(fd, (void *)buf, bufsz);
380 } while (tsize == -1 && errno == EINTR);
Greg Kurzbec1e952017-02-26 23:43:40 +0100381 close_preserve_errno(fd);
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530382 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
383 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Greg Kurzbec1e952017-02-26 23:43:40 +0100384 char *dirpath = g_path_get_dirname(fs_path->data);
385 char *name = g_path_get_basename(fs_path->data);
386 int dirfd;
387
388 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
389 if (dirfd == -1) {
390 goto out;
391 }
392
393 tsize = readlinkat(dirfd, name, buf, bufsz);
394 close_preserve_errno(dirfd);
395 out:
396 g_free(name);
397 g_free(dirpath);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700398 }
399 return tsize;
Anthony Liguori131dcb22010-04-29 17:44:47 +0530400}
401
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530402static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530403{
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530404 return close(fs->fd);
Anthony Liguori131dcb22010-04-29 17:44:47 +0530405}
406
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530407static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguori131dcb22010-04-29 17:44:47 +0530408{
Greg Kurzf314ea42016-06-06 11:52:34 +0200409 return closedir(fs->dir.stream);
Anthony Liguori131dcb22010-04-29 17:44:47 +0530410}
Anthony Liguori9f107512010-04-29 17:44:44 +0530411
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530412static int local_open(FsContext *ctx, V9fsPath *fs_path,
413 int flags, V9fsFidOpenState *fs)
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530414{
Greg Kurz21328e12017-02-26 23:41:55 +0100415 int fd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530416
Greg Kurz996a0d72017-02-26 23:42:18 +0100417 fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
Greg Kurz21328e12017-02-26 23:41:55 +0100418 if (fd == -1) {
419 return -1;
420 }
421 fs->fd = fd;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530422 return fs->fd;
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530423}
424
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530425static int local_opendir(FsContext *ctx,
426 V9fsPath *fs_path, V9fsFidOpenState *fs)
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530427{
Greg Kurz996a0d72017-02-26 23:42:18 +0100428 int dirfd;
Greg Kurz21328e12017-02-26 23:41:55 +0100429 DIR *stream;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530430
Greg Kurz996a0d72017-02-26 23:42:18 +0100431 dirfd = local_opendir_nofollow(ctx, fs_path->data);
432 if (dirfd == -1) {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530433 return -1;
434 }
Greg Kurz996a0d72017-02-26 23:42:18 +0100435
436 stream = fdopendir(dirfd);
Greg Kurz21328e12017-02-26 23:41:55 +0100437 if (!stream) {
Greg Kurzfaab2072017-03-06 17:34:01 +0100438 close(dirfd);
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530439 return -1;
440 }
Greg Kurz21328e12017-02-26 23:41:55 +0100441 fs->dir.stream = stream;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530442 return 0;
Anthony Liguoria6568fe2010-04-29 17:44:55 +0530443}
444
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530445static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530446{
Greg Kurzf314ea42016-06-06 11:52:34 +0200447 rewinddir(fs->dir.stream);
Anthony Liguoria9231552010-04-29 17:44:56 +0530448}
449
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530450static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530451{
Greg Kurzf314ea42016-06-06 11:52:34 +0200452 return telldir(fs->dir.stream);
Anthony Liguoria9231552010-04-29 17:44:56 +0530453}
454
Greg Kurz635324e2016-06-06 11:52:34 +0200455static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
Anthony Liguoria9231552010-04-29 17:44:56 +0530456{
Greg Kurz635324e2016-06-06 11:52:34 +0200457 struct dirent *entry;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530458
459again:
Greg Kurz635324e2016-06-06 11:52:34 +0200460 entry = readdir(fs->dir.stream);
461 if (!entry) {
462 return NULL;
463 }
464
Bastian Blank840a1bf2014-08-22 13:22:21 +0400465 if (ctx->export_flags & V9FS_SM_MAPPED) {
466 entry->d_type = DT_UNKNOWN;
467 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurz635324e2016-06-06 11:52:34 +0200468 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530469 /* skp the meta data directory */
470 goto again;
471 }
Bastian Blank840a1bf2014-08-22 13:22:21 +0400472 entry->d_type = DT_UNKNOWN;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530473 }
Greg Kurz635324e2016-06-06 11:52:34 +0200474
475 return entry;
Anthony Liguoria9231552010-04-29 17:44:56 +0530476}
477
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530478static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
Anthony Liguoria9231552010-04-29 17:44:56 +0530479{
Greg Kurzf314ea42016-06-06 11:52:34 +0200480 seekdir(fs->dir.stream, off);
Anthony Liguoria9231552010-04-29 17:44:56 +0530481}
482
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530483static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
484 const struct iovec *iov,
Sanchit Garg56d15a52010-10-08 11:30:16 +0530485 int iovcnt, off_t offset)
Anthony Liguoria9231552010-04-29 17:44:56 +0530486{
Sanchit Garg56d15a52010-10-08 11:30:16 +0530487#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530488 return preadv(fs->fd, iov, iovcnt, offset);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530489#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530490 int err = lseek(fs->fd, offset, SEEK_SET);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530491 if (err == -1) {
492 return err;
493 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530494 return readv(fs->fd, iov, iovcnt);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530495 }
496#endif
Anthony Liguoria9231552010-04-29 17:44:56 +0530497}
498
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530499static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
500 const struct iovec *iov,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530501 int iovcnt, off_t offset)
Anthony Liguoria9231552010-04-29 17:44:56 +0530502{
Greg Kurz6fe76ac2017-01-23 09:46:13 +0100503 ssize_t ret;
Sanchit Garg56d15a52010-10-08 11:30:16 +0530504#ifdef CONFIG_PREADV
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530505 ret = pwritev(fs->fd, iov, iovcnt, offset);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530506#else
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530507 int err = lseek(fs->fd, offset, SEEK_SET);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530508 if (err == -1) {
509 return err;
510 } else {
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530511 ret = writev(fs->fd, iov, iovcnt);
Sanchit Garg56d15a52010-10-08 11:30:16 +0530512 }
513#endif
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530514#ifdef CONFIG_SYNC_FILE_RANGE
515 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
516 /*
517 * Initiate a writeback. This is not a data integrity sync.
518 * We want to ensure that we don't leave dirty pages in the cache
519 * after write when writeout=immediate is sepcified.
520 */
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530521 sync_file_range(fs->fd, offset, ret,
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530522 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
523 }
524#endif
525 return ret;
Anthony Liguori84493602010-04-29 17:44:58 +0530526}
527
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530528static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530529{
Greg Kurze3187a42017-02-26 23:44:28 +0100530 char *dirpath = g_path_get_dirname(fs_path->data);
531 char *name = g_path_get_basename(fs_path->data);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800532 int ret = -1;
Greg Kurze3187a42017-02-26 23:44:28 +0100533 int dirfd;
534
535 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
536 if (dirfd == -1) {
537 goto out;
538 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530539
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530540 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Greg Kurze3187a42017-02-26 23:44:28 +0100541 ret = local_set_xattrat(dirfd, name, credp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530542 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurze3187a42017-02-26 23:44:28 +0100543 ret = local_set_mapped_file_attrat(dirfd, name, credp);
544 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
545 fs_ctx->export_flags & V9FS_SM_NONE) {
546 ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
Venkateswararao Jujjuri (JV)e95ead32010-06-14 13:34:42 -0700547 }
Greg Kurze3187a42017-02-26 23:44:28 +0100548 close_preserve_errno(dirfd);
549
550out:
551 g_free(dirpath);
552 g_free(name);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800553 return ret;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530554}
555
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530556static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
557 const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530558{
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700559 int err = -1;
Greg Kurzd815e722017-02-26 23:44:54 +0100560 int dirfd;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700561
Greg Kurzd815e722017-02-26 23:44:54 +0100562 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
563 if (dirfd == -1) {
564 return -1;
565 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530566
Greg Kurzd815e722017-02-26 23:44:54 +0100567 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
568 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
569 err = mknodat(dirfd, name, SM_LOCAL_MODE_BITS | S_IFREG, 0);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700570 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530571 goto out;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700572 }
Greg Kurzd815e722017-02-26 23:44:54 +0100573
574 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
575 err = local_set_xattrat(dirfd, name, credp);
576 } else {
577 err = local_set_mapped_file_attrat(dirfd, name, credp);
578 }
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700579 if (err == -1) {
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700580 goto err_end;
581 }
Greg Kurzd815e722017-02-26 23:44:54 +0100582 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
583 fs_ctx->export_flags & V9FS_SM_NONE) {
584 err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530585 if (err == -1) {
586 goto out;
587 }
Greg Kurzd815e722017-02-26 23:44:54 +0100588 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530589 if (err == -1) {
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700590 goto err_end;
591 }
592 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530593 goto out;
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700594
595err_end:
Greg Kurzd815e722017-02-26 23:44:54 +0100596 unlinkat_preserve_errno(dirfd, name, 0);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530597out:
Greg Kurzd815e722017-02-26 23:44:54 +0100598 close_preserve_errno(dirfd);
Venkateswararao Jujjuri (JV)1c293312010-06-14 13:34:48 -0700599 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530600}
601
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530602static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
603 const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530604{
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700605 int err = -1;
Greg Kurz3f3a1692017-02-26 23:45:02 +0100606 int dirfd;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700607
Greg Kurz3f3a1692017-02-26 23:45:02 +0100608 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
609 if (dirfd == -1) {
610 return -1;
611 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530612
Greg Kurz3f3a1692017-02-26 23:45:02 +0100613 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
614 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
615 err = mkdirat(dirfd, name, SM_LOCAL_DIR_MODE_BITS);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700616 if (err == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530617 goto out;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700618 }
Greg Kurz3f3a1692017-02-26 23:45:02 +0100619 credp->fc_mode = credp->fc_mode | S_IFDIR;
620
621 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
622 err = local_set_xattrat(dirfd, name, credp);
623 } else {
624 err = local_set_mapped_file_attrat(dirfd, name, credp);
625 }
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700626 if (err == -1) {
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700627 goto err_end;
628 }
Greg Kurz3f3a1692017-02-26 23:45:02 +0100629 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
630 fs_ctx->export_flags & V9FS_SM_NONE) {
631 err = mkdirat(dirfd, name, credp->fc_mode);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530632 if (err == -1) {
633 goto out;
634 }
Greg Kurz3f3a1692017-02-26 23:45:02 +0100635 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530636 if (err == -1) {
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700637 goto err_end;
638 }
639 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530640 goto out;
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700641
642err_end:
Greg Kurz3f3a1692017-02-26 23:45:02 +0100643 unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530644out:
Greg Kurz3f3a1692017-02-26 23:45:02 +0100645 close_preserve_errno(dirfd);
Venkateswararao Jujjuri (JV)00ec5c32010-06-14 13:34:46 -0700646 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530647}
648
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530649static int local_fstat(FsContext *fs_ctx, int fid_type,
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530650 V9fsFidOpenState *fs, struct stat *stbuf)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530651{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530652 int err, fd;
653
654 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +0200655 fd = dirfd(fs->dir.stream);
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +0530656 } else {
657 fd = fs->fd;
658 }
659
660 err = fstat(fd, stbuf);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700661 if (err) {
662 return err;
663 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530664 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700665 /* Actual credentials are part of extended attrs */
666 uid_t tmp_uid;
667 gid_t tmp_gid;
668 mode_t tmp_mode;
669 dev_t tmp_dev;
670
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530671 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
672 stbuf->st_uid = le32_to_cpu(tmp_uid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700673 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530674 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
675 stbuf->st_gid = le32_to_cpu(tmp_gid);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700676 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530677 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
678 stbuf->st_mode = le32_to_cpu(tmp_mode);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700679 }
Aneesh Kumar K.Vf8ad4a82014-08-03 17:02:55 +0530680 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
681 stbuf->st_rdev = le64_to_cpu(tmp_dev);
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700682 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530683 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
684 errno = EOPNOTSUPP;
685 return -1;
Venkateswararao Jujjuri (JV)1237ad72010-06-14 13:34:44 -0700686 }
687 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530688}
689
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530690static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530691 int flags, FsCred *credp, V9fsFidOpenState *fs)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530692{
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700693 int fd = -1;
694 int err = -1;
Greg Kurza565fea2017-02-26 23:45:09 +0100695 int dirfd;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700696
Aneesh Kumar K.V0ceb0922013-05-20 19:43:15 +0530697 /*
698 * Mark all the open to not follow symlinks
699 */
700 flags |= O_NOFOLLOW;
701
Greg Kurza565fea2017-02-26 23:45:09 +0100702 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
703 if (dirfd == -1) {
704 return -1;
705 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530706
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700707 /* Determine the security model */
Greg Kurza565fea2017-02-26 23:45:09 +0100708 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
709 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
710 fd = openat_file(dirfd, name, flags, SM_LOCAL_MODE_BITS);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700711 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530712 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700713 }
714 credp->fc_mode = credp->fc_mode|S_IFREG;
Greg Kurza565fea2017-02-26 23:45:09 +0100715 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
716 /* Set cleint credentials in xattr */
717 err = local_set_xattrat(dirfd, name, credp);
718 } else {
719 err = local_set_mapped_file_attrat(dirfd, name, credp);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700720 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530721 if (err == -1) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530722 goto err_end;
723 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530724 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
725 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Greg Kurza565fea2017-02-26 23:45:09 +0100726 fd = openat_file(dirfd, name, flags, credp->fc_mode);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700727 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530728 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700729 }
Greg Kurza565fea2017-02-26 23:45:09 +0100730 err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700731 if (err == -1) {
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700732 goto err_end;
733 }
734 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530735 err = fd;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +0530736 fs->fd = fd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530737 goto out;
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700738
739err_end:
Greg Kurza565fea2017-02-26 23:45:09 +0100740 unlinkat_preserve_errno(dirfd, name,
741 flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
742 close_preserve_errno(fd);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530743out:
Greg Kurza565fea2017-02-26 23:45:09 +0100744 close_preserve_errno(dirfd);
Venkateswararao Jujjuri (JV)4750a962010-06-14 13:34:45 -0700745 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530746}
747
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700748
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700749static int local_symlink(FsContext *fs_ctx, const char *oldpath,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530750 V9fsPath *dir_path, const char *name, FsCred *credp)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530751{
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700752 int err = -1;
Greg Kurz38771612017-02-26 23:44:46 +0100753 int dirfd;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700754
Greg Kurz38771612017-02-26 23:44:46 +0100755 dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
756 if (dirfd == -1) {
757 return -1;
758 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530759
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700760 /* Determine the security model */
Greg Kurz38771612017-02-26 23:44:46 +0100761 if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
762 fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700763 int fd;
764 ssize_t oldpath_size, write_size;
Greg Kurz38771612017-02-26 23:44:46 +0100765
766 fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
767 SM_LOCAL_MODE_BITS);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700768 if (fd == -1) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530769 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700770 }
771 /* Write the oldpath (target) to the file. */
Harsh Prateek Boraf35bde22011-02-02 10:20:33 +0530772 oldpath_size = strlen(oldpath);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700773 do {
774 write_size = write(fd, (void *)oldpath, oldpath_size);
775 } while (write_size == -1 && errno == EINTR);
Greg Kurz38771612017-02-26 23:44:46 +0100776 close_preserve_errno(fd);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700777
778 if (write_size != oldpath_size) {
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700779 goto err_end;
780 }
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700781 /* Set cleint credentials in symlink's xattr */
Greg Kurz38771612017-02-26 23:44:46 +0100782 credp->fc_mode = credp->fc_mode | S_IFLNK;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530783
Greg Kurz38771612017-02-26 23:44:46 +0100784 if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
785 err = local_set_xattrat(dirfd, name, credp);
786 } else {
787 err = local_set_mapped_file_attrat(dirfd, name, credp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530788 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530789 if (err == -1) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530790 goto err_end;
791 }
Greg Kurz38771612017-02-26 23:44:46 +0100792 } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
793 fs_ctx->export_flags & V9FS_SM_NONE) {
794 err = symlinkat(oldpath, dirfd, name);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700795 if (err) {
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530796 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700797 }
Greg Kurz38771612017-02-26 23:44:46 +0100798 err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
799 AT_SYMLINK_NOFOLLOW);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700800 if (err == -1) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530801 /*
802 * If we fail to change ownership and if we are
803 * using security model none. Ignore the error
804 */
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530805 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530806 goto err_end;
Greg Kurz38771612017-02-26 23:44:46 +0100807 } else {
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +0530808 err = 0;
Greg Kurz38771612017-02-26 23:44:46 +0100809 }
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700810 }
811 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530812 goto out;
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700813
814err_end:
Greg Kurz38771612017-02-26 23:44:46 +0100815 unlinkat_preserve_errno(dirfd, name, 0);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530816out:
Greg Kurz38771612017-02-26 23:44:46 +0100817 close_preserve_errno(dirfd);
Venkateswararao Jujjuri (JV)879c2812010-06-14 13:34:47 -0700818 return err;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530819}
820
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530821static int local_link(FsContext *ctx, V9fsPath *oldpath,
822 V9fsPath *dirpath, const char *name)
Anthony Liguoric494dd62010-04-29 17:44:59 +0530823{
Greg Kurzad0b46e2017-02-26 23:44:20 +0100824 char *odirpath = g_path_get_dirname(oldpath->data);
825 char *oname = g_path_get_basename(oldpath->data);
826 int ret = -1;
827 int odirfd, ndirfd;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530828
Greg Kurzad0b46e2017-02-26 23:44:20 +0100829 odirfd = local_opendir_nofollow(ctx, odirpath);
830 if (odirfd == -1) {
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100831 goto out;
832 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530833
Greg Kurzad0b46e2017-02-26 23:44:20 +0100834 ndirfd = local_opendir_nofollow(ctx, dirpath->data);
835 if (ndirfd == -1) {
836 close_preserve_errno(odirfd);
837 goto out;
838 }
839
840 ret = linkat(odirfd, oname, ndirfd, name, 0);
841 if (ret < 0) {
842 goto out_close;
843 }
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530844
845 /* now link the virtfs_metadata files */
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100846 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurzad0b46e2017-02-26 23:44:20 +0100847 int omap_dirfd, nmap_dirfd;
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100848
Greg Kurzad0b46e2017-02-26 23:44:20 +0100849 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
850 if (ret < 0 && errno != EEXIST) {
851 goto err_undo_link;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530852 }
Greg Kurzad0b46e2017-02-26 23:44:20 +0100853
854 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
855 if (omap_dirfd == -1) {
856 goto err;
857 }
858
859 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
860 if (nmap_dirfd == -1) {
861 close_preserve_errno(omap_dirfd);
862 goto err;
863 }
864
865 ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
866 close_preserve_errno(nmap_dirfd);
867 close_preserve_errno(omap_dirfd);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530868 if (ret < 0 && errno != ENOENT) {
Greg Kurzad0b46e2017-02-26 23:44:20 +0100869 goto err_undo_link;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530870 }
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100871
Greg Kurzad0b46e2017-02-26 23:44:20 +0100872 ret = 0;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530873 }
Greg Kurzad0b46e2017-02-26 23:44:20 +0100874 goto out_close;
875
876err:
877 ret = -1;
878err_undo_link:
879 unlinkat_preserve_errno(ndirfd, name, 0);
880out_close:
881 close_preserve_errno(ndirfd);
882 close_preserve_errno(odirfd);
Greg Kurz6dd4b1f2017-02-26 23:44:11 +0100883out:
Greg Kurzad0b46e2017-02-26 23:44:20 +0100884 g_free(oname);
885 g_free(odirpath);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530886 return ret;
Anthony Liguoric494dd62010-04-29 17:44:59 +0530887}
888
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530889static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530890{
Greg Kurzac125d92017-02-26 23:43:32 +0100891 int fd, ret;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530892
Greg Kurzac125d92017-02-26 23:43:32 +0100893 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
894 if (fd == -1) {
895 return -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530896 }
Greg Kurzac125d92017-02-26 23:43:32 +0100897 ret = ftruncate(fd, size);
898 close_preserve_errno(fd);
Anthony Liguoria9231552010-04-29 17:44:56 +0530899 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530900}
901
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530902static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530903{
Greg Kurzd369f202017-02-26 23:44:37 +0100904 char *dirpath = g_path_get_dirname(fs_path->data);
905 char *name = g_path_get_basename(fs_path->data);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800906 int ret = -1;
Greg Kurzd369f202017-02-26 23:44:37 +0100907 int dirfd;
908
909 dirfd = local_opendir_nofollow(fs_ctx, dirpath);
910 if (dirfd == -1) {
911 goto out;
912 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530913
Sripathi Kodic79ce732010-06-17 18:18:47 +0530914 if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
Aneesh Kumar K.V17b19712011-10-25 12:10:39 +0530915 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
916 (fs_ctx->export_flags & V9FS_SM_NONE)) {
Greg Kurzd369f202017-02-26 23:44:37 +0100917 ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
918 AT_SYMLINK_NOFOLLOW);
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530919 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
Greg Kurzd369f202017-02-26 23:44:37 +0100920 ret = local_set_xattrat(dirfd, name, credp);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530921 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurzd369f202017-02-26 23:44:37 +0100922 ret = local_set_mapped_file_attrat(dirfd, name, credp);
Venkateswararao Jujjuri (JV)f7613be2010-06-14 13:34:43 -0700923 }
Greg Kurzd369f202017-02-26 23:44:37 +0100924
925 close_preserve_errno(dirfd);
926out:
927 g_free(name);
928 g_free(dirpath);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800929 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530930}
931
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530932static int local_utimensat(FsContext *s, V9fsPath *fs_path,
Hidetoshi Seto38671422010-11-24 11:38:10 +0900933 const struct timespec *buf)
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530934{
Greg Kurza33eda02017-02-26 23:43:17 +0100935 char *dirpath = g_path_get_dirname(fs_path->data);
936 char *name = g_path_get_basename(fs_path->data);
937 int dirfd, ret = -1;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +0530938
Greg Kurza33eda02017-02-26 23:43:17 +0100939 dirfd = local_opendir_nofollow(s, dirpath);
940 if (dirfd == -1) {
941 goto out;
942 }
943
944 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
945 close_preserve_errno(dirfd);
946out:
947 g_free(dirpath);
948 g_free(name);
Chen Gang4fa4ce72014-03-02 01:36:19 +0800949 return ret;
Anthony Liguori8cf89e02010-04-29 17:45:00 +0530950}
951
Greg Kurzdf4938a2017-02-26 23:43:00 +0100952static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
953 int flags)
Anthony Liguori5bae1902010-04-29 17:45:01 +0530954{
Greg Kurzdf4938a2017-02-26 23:43:00 +0100955 int ret = -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530956
957 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
Greg Kurzdf4938a2017-02-26 23:43:00 +0100958 int map_dirfd;
959
960 if (flags == AT_REMOVEDIR) {
961 int fd;
962
963 fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH);
964 if (fd == -1) {
965 goto err_out;
966 }
967 /*
968 * If directory remove .virtfs_metadata contained in the
969 * directory
970 */
971 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
972 close_preserve_errno(fd);
973 if (ret < 0 && errno != ENOENT) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530974 /*
975 * We didn't had the .virtfs_metadata file. May be file created
976 * in non-mapped mode ?. Ignore ENOENT.
977 */
978 goto err_out;
979 }
980 }
981 /*
982 * Now remove the name from parent directory
Greg Kurzdf4938a2017-02-26 23:43:00 +0100983 * .virtfs_metadata directory.
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530984 */
Greg Kurzdf4938a2017-02-26 23:43:00 +0100985 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
986 ret = unlinkat(map_dirfd, name, 0);
987 close_preserve_errno(map_dirfd);
988 if (ret < 0 && errno != ENOENT) {
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530989 /*
990 * We didn't had the .virtfs_metadata file. May be file created
991 * in non-mapped mode ?. Ignore ENOENT.
992 */
993 goto err_out;
994 }
995 }
Chen Gang4fa4ce72014-03-02 01:36:19 +0800996
Greg Kurzdf4938a2017-02-26 23:43:00 +0100997 ret = unlinkat(dirfd, name, flags);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +0530998err_out:
Greg Kurzdf4938a2017-02-26 23:43:00 +0100999 return ret;
1000}
1001
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301002static int local_remove(FsContext *ctx, const char *path)
1003{
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301004 struct stat stbuf;
Greg Kurza0e640a2017-02-26 23:43:08 +01001005 char *dirpath = g_path_get_dirname(path);
1006 char *name = g_path_get_basename(path);
1007 int flags = 0;
1008 int dirfd;
1009 int err = -1;
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301010
Greg Kurza0e640a2017-02-26 23:43:08 +01001011 dirfd = local_opendir_nofollow(ctx, dirpath);
Greg Kurzb7361d42017-03-06 17:34:01 +01001012 if (dirfd == -1) {
Greg Kurza0e640a2017-02-26 23:43:08 +01001013 goto out;
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301014 }
Anthony Liguori5bae1902010-04-29 17:45:01 +05301015
Greg Kurza0e640a2017-02-26 23:43:08 +01001016 if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
1017 goto err_out;
1018 }
1019
1020 if (S_ISDIR(stbuf.st_mode)) {
1021 flags |= AT_REMOVEDIR;
1022 }
1023
1024 err = local_unlinkat_common(ctx, dirfd, name, flags);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301025err_out:
Greg Kurza0e640a2017-02-26 23:43:08 +01001026 close_preserve_errno(dirfd);
1027out:
1028 g_free(name);
1029 g_free(dirpath);
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301030 return err;
Anthony Liguori5bae1902010-04-29 17:45:01 +05301031}
1032
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301033static int local_fsync(FsContext *ctx, int fid_type,
1034 V9fsFidOpenState *fs, int datasync)
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301035{
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301036 int fd;
1037
1038 if (fid_type == P9_FID_DIR) {
Greg Kurzf314ea42016-06-06 11:52:34 +02001039 fd = dirfd(fs->dir.stream);
Venkateswararao Jujjuri (JV)49594972010-10-22 10:08:45 -07001040 } else {
Aneesh Kumar K.V8b888272011-12-04 22:35:28 +05301041 fd = fs->fd;
1042 }
1043
1044 if (datasync) {
1045 return qemu_fdatasync(fd);
1046 } else {
1047 return fsync(fd);
Venkateswararao Jujjuri (JV)49594972010-10-22 10:08:45 -07001048 }
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301049}
1050
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301051static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301052{
Greg Kurz31e51d12017-02-26 23:43:25 +01001053 int fd, ret;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301054
Greg Kurz31e51d12017-02-26 23:43:25 +01001055 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
1056 ret = fstatfs(fd, stbuf);
1057 close_preserve_errno(fd);
Chen Gang4fa4ce72014-03-02 01:36:19 +08001058 return ret;
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301059}
1060
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301061static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301062 const char *name, void *value, size_t size)
1063{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301064 char *path = fs_path->data;
1065
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301066 return v9fs_get_xattr(ctx, path, name, value, size);
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301067}
1068
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301069static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301070 void *value, size_t size)
1071{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301072 char *path = fs_path->data;
1073
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301074 return v9fs_list_xattr(ctx, path, value, size);
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301075}
1076
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301077static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301078 void *value, size_t size, int flags)
1079{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301080 char *path = fs_path->data;
1081
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301082 return v9fs_set_xattr(ctx, path, name, value, size, flags);
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301083}
1084
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301085static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1086 const char *name)
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301087{
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301088 char *path = fs_path->data;
1089
Aneesh Kumar K.Vfc221182010-10-18 15:28:16 +05301090 return v9fs_remove_xattr(ctx, path, name);
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301091}
1092
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301093static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1094 const char *name, V9fsPath *target)
1095{
1096 if (dir_path) {
Greg Kurze3e83f22016-09-16 08:56:15 +02001097 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301098 } else {
Greg Kurze3e83f22016-09-16 08:56:15 +02001099 v9fs_path_sprintf(target, "%s", name);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301100 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301101 return 0;
1102}
1103
1104static int local_renameat(FsContext *ctx, V9fsPath *olddir,
1105 const char *old_name, V9fsPath *newdir,
1106 const char *new_name)
1107{
1108 int ret;
Greg Kurz99f2cf42017-02-26 23:43:55 +01001109 int odirfd, ndirfd;
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301110
Greg Kurz99f2cf42017-02-26 23:43:55 +01001111 odirfd = local_opendir_nofollow(ctx, olddir->data);
1112 if (odirfd == -1) {
1113 return -1;
1114 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301115
Greg Kurz99f2cf42017-02-26 23:43:55 +01001116 ndirfd = local_opendir_nofollow(ctx, newdir->data);
1117 if (ndirfd == -1) {
1118 close_preserve_errno(odirfd);
1119 return -1;
1120 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301121
Greg Kurz99f2cf42017-02-26 23:43:55 +01001122 ret = renameat(odirfd, old_name, ndirfd, new_name);
1123 if (ret < 0) {
1124 goto out;
1125 }
1126
1127 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1128 int omap_dirfd, nmap_dirfd;
1129
1130 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
1131 if (ret < 0 && errno != EEXIST) {
1132 goto err_undo_rename;
1133 }
1134
Greg Kurz6dd4b1f2017-02-26 23:44:11 +01001135 omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
Greg Kurz99f2cf42017-02-26 23:43:55 +01001136 if (omap_dirfd == -1) {
1137 goto err;
1138 }
1139
Greg Kurz6dd4b1f2017-02-26 23:44:11 +01001140 nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
Greg Kurz99f2cf42017-02-26 23:43:55 +01001141 if (nmap_dirfd == -1) {
1142 close_preserve_errno(omap_dirfd);
1143 goto err;
1144 }
1145
1146 /* rename the .virtfs_metadata files */
1147 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
1148 close_preserve_errno(nmap_dirfd);
1149 close_preserve_errno(omap_dirfd);
1150 if (ret < 0 && errno != ENOENT) {
1151 goto err_undo_rename;
1152 }
1153
1154 ret = 0;
1155 }
1156 goto out;
1157
1158err:
1159 ret = -1;
1160err_undo_rename:
1161 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
1162out:
1163 close_preserve_errno(ndirfd);
1164 close_preserve_errno(odirfd);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301165 return ret;
1166}
1167
Greg Kurzd2767ed2017-02-26 23:44:03 +01001168static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
1169{
1170 path->data = g_path_get_dirname(str);
1171 path->size = strlen(path->data) + 1;
1172}
1173
1174static int local_rename(FsContext *ctx, const char *oldpath,
1175 const char *newpath)
1176{
1177 int err;
1178 char *oname = g_path_get_basename(oldpath);
1179 char *nname = g_path_get_basename(newpath);
1180 V9fsPath olddir, newdir;
1181
1182 v9fs_path_init_dirname(&olddir, oldpath);
1183 v9fs_path_init_dirname(&newdir, newpath);
1184
1185 err = local_renameat(ctx, &olddir, oname, &newdir, nname);
1186
1187 v9fs_path_free(&newdir);
1188 v9fs_path_free(&olddir);
1189 g_free(nname);
1190 g_free(oname);
1191
1192 return err;
1193}
1194
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301195static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
1196 const char *name, int flags)
1197{
1198 int ret;
Greg Kurzdf4938a2017-02-26 23:43:00 +01001199 int dirfd;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301200
Greg Kurzdf4938a2017-02-26 23:43:00 +01001201 dirfd = local_opendir_nofollow(ctx, dir->data);
1202 if (dirfd == -1) {
1203 return -1;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301204 }
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301205
Greg Kurzdf4938a2017-02-26 23:43:00 +01001206 ret = local_unlinkat_common(ctx, dirfd, name, flags);
1207 close_preserve_errno(dirfd);
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301208 return ret;
1209}
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301210
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301211static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
1212 mode_t st_mode, uint64_t *st_gen)
1213{
Paolo Bonziniae0f9402011-11-21 09:29:11 +01001214#ifdef FS_IOC_GETVERSION
Kirill A. Shutemov0e5fc992014-01-28 17:08:24 +02001215 int err;
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301216 V9fsFidOpenState fid_open;
1217
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301218 /*
1219 * Do not try to open special files like device nodes, fifos etc
1220 * We can get fd for regular files and directories only
1221 */
1222 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
Kirill A. Shutemov1a9978a2014-01-28 17:08:26 +02001223 errno = ENOTTY;
1224 return -1;
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301225 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301226 err = local_open(ctx, path, O_RDONLY, &fid_open);
1227 if (err < 0) {
1228 return err;
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301229 }
Aneesh Kumar K.Vcc720dd2011-10-25 12:10:40 +05301230 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
1231 local_close(ctx, &fid_open);
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301232 return err;
Kirill A. Shutemov0e5fc992014-01-28 17:08:24 +02001233#else
1234 errno = ENOTTY;
1235 return -1;
1236#endif
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301237}
1238
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301239static int local_init(FsContext *ctx)
1240{
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301241 struct statfs stbuf;
Greg Kurz0e35a372017-02-26 23:42:10 +01001242 LocalData *data = g_malloc(sizeof(*data));
1243
1244 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
1245 if (data->mountfd == -1) {
1246 goto err;
1247 }
Harsh Prateek Borae06a7652011-10-12 19:11:25 +05301248
Greg Kurz00c90bd2017-02-26 23:41:48 +01001249#ifdef FS_IOC_GETVERSION
1250 /*
1251 * use ioc_getversion only if the ioctl is definied
1252 */
Greg Kurz0e35a372017-02-26 23:42:10 +01001253 if (fstatfs(data->mountfd, &stbuf) < 0) {
1254 close_preserve_errno(data->mountfd);
1255 goto err;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001256 }
1257 switch (stbuf.f_type) {
1258 case EXT2_SUPER_MAGIC:
1259 case BTRFS_SUPER_MAGIC:
1260 case REISERFS_SUPER_MAGIC:
1261 case XFS_SUPER_MAGIC:
1262 ctx->exops.get_st_gen = local_ioc_getversion;
1263 break;
1264 }
1265#endif
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301266
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301267 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
1268 ctx->xops = passthrough_xattr_ops;
1269 } else if (ctx->export_flags & V9FS_SM_MAPPED) {
1270 ctx->xops = mapped_xattr_ops;
1271 } else if (ctx->export_flags & V9FS_SM_NONE) {
1272 ctx->xops = none_xattr_ops;
1273 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
1274 /*
1275 * xattr operation for mapped-file and passthrough
1276 * remain same.
1277 */
1278 ctx->xops = passthrough_xattr_ops;
1279 }
Aneesh Kumar K.Vc98f1d42011-10-12 20:59:18 +05301280 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001281
Greg Kurz0e35a372017-02-26 23:42:10 +01001282 ctx->private = data;
Greg Kurz00c90bd2017-02-26 23:41:48 +01001283 return 0;
Greg Kurz0e35a372017-02-26 23:42:10 +01001284
1285err:
1286 g_free(data);
1287 return -1;
1288}
1289
1290static void local_cleanup(FsContext *ctx)
1291{
1292 LocalData *data = ctx->private;
1293
1294 close(data->mountfd);
1295 g_free(data);
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301296}
1297
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301298static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
1299{
1300 const char *sec_model = qemu_opt_get(opts, "security_model");
1301 const char *path = qemu_opt_get(opts, "path");
Pradeep Jagadeeshb8bbdb82017-02-28 10:31:46 +01001302 Error *err = NULL;
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301303
1304 if (!sec_model) {
Greg Kurz63325b12016-01-22 15:12:17 +01001305 error_report("Security model not specified, local fs needs security model");
1306 error_printf("valid options are:"
1307 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301308 return -1;
1309 }
1310
1311 if (!strcmp(sec_model, "passthrough")) {
1312 fse->export_flags |= V9FS_SM_PASSTHROUGH;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301313 } else if (!strcmp(sec_model, "mapped") ||
1314 !strcmp(sec_model, "mapped-xattr")) {
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301315 fse->export_flags |= V9FS_SM_MAPPED;
1316 } else if (!strcmp(sec_model, "none")) {
1317 fse->export_flags |= V9FS_SM_NONE;
Aneesh Kumar K.V2c30dd72012-01-19 12:21:11 +05301318 } else if (!strcmp(sec_model, "mapped-file")) {
1319 fse->export_flags |= V9FS_SM_MAPPED_FILE;
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301320 } else {
Greg Kurz63325b12016-01-22 15:12:17 +01001321 error_report("Invalid security model %s specified", sec_model);
1322 error_printf("valid options are:"
1323 "\t[passthrough|mapped-xattr|mapped-file|none]\n");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301324 return -1;
1325 }
1326
1327 if (!path) {
Greg Kurz63325b12016-01-22 15:12:17 +01001328 error_report("fsdev: No path specified");
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301329 return -1;
1330 }
Pradeep Jagadeeshb8bbdb82017-02-28 10:31:46 +01001331
1332 fsdev_throttle_parse_opts(opts, &fse->fst, &err);
1333 if (err) {
1334 error_reportf_err(err, "Throttle configuration is not valid: ");
1335 return -1;
1336 }
1337
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301338 fse->path = g_strdup(path);
1339
1340 return 0;
1341}
1342
Anthony Liguori9f107512010-04-29 17:44:44 +05301343FileOperations local_ops = {
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +05301344 .parse_opts = local_parse_opts,
Aneesh Kumar K.V0174fe72011-08-02 11:35:54 +05301345 .init = local_init,
Greg Kurz0e35a372017-02-26 23:42:10 +01001346 .cleanup = local_cleanup,
Anthony Liguori131dcb22010-04-29 17:44:47 +05301347 .lstat = local_lstat,
Anthony Liguori131dcb22010-04-29 17:44:47 +05301348 .readlink = local_readlink,
1349 .close = local_close,
1350 .closedir = local_closedir,
Anthony Liguoria6568fe2010-04-29 17:44:55 +05301351 .open = local_open,
1352 .opendir = local_opendir,
Anthony Liguoria9231552010-04-29 17:44:56 +05301353 .rewinddir = local_rewinddir,
1354 .telldir = local_telldir,
Greg Kurz635324e2016-06-06 11:52:34 +02001355 .readdir = local_readdir,
Anthony Liguoria9231552010-04-29 17:44:56 +05301356 .seekdir = local_seekdir,
Sanchit Garg56d15a52010-10-08 11:30:16 +05301357 .preadv = local_preadv,
1358 .pwritev = local_pwritev,
Anthony Liguoric494dd62010-04-29 17:44:59 +05301359 .chmod = local_chmod,
1360 .mknod = local_mknod,
Anthony Liguoric494dd62010-04-29 17:44:59 +05301361 .mkdir = local_mkdir,
1362 .fstat = local_fstat,
1363 .open2 = local_open2,
1364 .symlink = local_symlink,
1365 .link = local_link,
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301366 .truncate = local_truncate,
1367 .rename = local_rename,
1368 .chown = local_chown,
M. Mohan Kumar74bc02b2010-06-09 19:14:38 +05301369 .utimensat = local_utimensat,
Anthony Liguori5bae1902010-04-29 17:45:01 +05301370 .remove = local_remove,
Anthony Liguori8cf89e02010-04-29 17:45:00 +05301371 .fsync = local_fsync,
M. Mohan Kumarbe940c82010-05-10 12:11:03 +05301372 .statfs = local_statfs,
Aneesh Kumar K.Vfa32ef82010-09-02 11:09:06 +05301373 .lgetxattr = local_lgetxattr,
1374 .llistxattr = local_llistxattr,
Aneesh Kumar K.V10b468b2010-09-02 11:09:07 +05301375 .lsetxattr = local_lsetxattr,
Aneesh Kumar K.V9ed3ef22010-08-26 11:15:23 +05301376 .lremovexattr = local_lremovexattr,
Aneesh Kumar K.V2289be12011-09-09 15:14:18 +05301377 .name_to_path = local_name_to_path,
1378 .renameat = local_renameat,
1379 .unlinkat = local_unlinkat,
Anthony Liguori9f107512010-04-29 17:44:44 +05301380};