]>
Commit | Line | Data |
---|---|---|
9f107513 | 1 | /* |
f00d4f59 | 2 | * 9p Posix callback |
9f107513 AL |
3 | * |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
873c3213 | 13 | |
fbc04127 | 14 | #include "qemu/osdep.h" |
ebe74f8b | 15 | #include "9p.h" |
996a0d76 | 16 | #include "9p-local.h" |
267ae092 | 17 | #include "9p-xattr.h" |
0e35a378 | 18 | #include "9p-util.h" |
69b15212 | 19 | #include "fsdev/qemu-fsdev.h" /* local_ops */ |
c494dd6f | 20 | #include <arpa/inet.h> |
131dcb25 AL |
21 | #include <pwd.h> |
22 | #include <grp.h> | |
c494dd6f AL |
23 | #include <sys/socket.h> |
24 | #include <sys/un.h> | |
1de7afc9 | 25 | #include "qemu/xattr.h" |
f348b6d1 | 26 | #include "qemu/cutils.h" |
63325b18 | 27 | #include "qemu/error-report.h" |
2c30dd74 | 28 | #include <libgen.h> |
e06a765e HPB |
29 | #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 | |
131dcb25 | 47 | |
0e35a378 GK |
48 | typedef struct { |
49 | int mountfd; | |
50 | } LocalData; | |
2c30dd74 | 51 | |
996a0d76 GK |
52 | int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, |
53 | mode_t mode) | |
2c30dd74 | 54 | { |
996a0d76 GK |
55 | LocalData *data = fs_ctx->private; |
56 | ||
57 | /* All paths are relative to the path data->mountfd points to */ | |
58 | while (*path == '/') { | |
59 | path++; | |
1b6f85e2 | 60 | } |
996a0d76 GK |
61 | |
62 | return relative_openat_nofollow(data->mountfd, path, flags, mode); | |
63 | } | |
64 | ||
65 | int 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 | ||
99f2cf4b GK |
70 | static 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; | |
2c30dd74 AK |
76 | } |
77 | ||
ad0b46e6 GK |
78 | static 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 | ||
2c30dd74 AK |
85 | #define VIRTFS_META_DIR ".virtfs_metadata" |
86 | ||
f9aef99b | 87 | static FILE *local_fopenat(int dirfd, const char *name, const char *mode) |
0ceb092e AK |
88 | { |
89 | int fd, o_mode = 0; | |
90 | FILE *fp; | |
f9aef99b | 91 | int flags; |
0ceb092e AK |
92 | /* |
93 | * only supports two modes | |
94 | */ | |
95 | if (mode[0] == 'r') { | |
f9aef99b | 96 | flags = O_RDONLY; |
0ceb092e | 97 | } else if (mode[0] == 'w') { |
f9aef99b | 98 | flags = O_WRONLY | O_TRUNC | O_CREAT; |
0ceb092e AK |
99 | o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
100 | } else { | |
101 | return NULL; | |
102 | } | |
f9aef99b | 103 | fd = openat_file(dirfd, name, flags, o_mode); |
0ceb092e AK |
104 | if (fd == -1) { |
105 | return NULL; | |
106 | } | |
107 | fp = fdopen(fd, mode); | |
108 | if (!fp) { | |
109 | close(fd); | |
110 | } | |
111 | return fp; | |
112 | } | |
113 | ||
2c30dd74 | 114 | #define ATTR_MAX 100 |
f9aef99b | 115 | static void local_mapped_file_attr(int dirfd, const char *name, |
2c30dd74 AK |
116 | struct stat *stbuf) |
117 | { | |
118 | FILE *fp; | |
119 | char buf[ATTR_MAX]; | |
f9aef99b | 120 | int map_dirfd; |
2c30dd74 | 121 | |
99f2cf4b | 122 | map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); |
f9aef99b GK |
123 | if (map_dirfd == -1) { |
124 | return; | |
125 | } | |
2c30dd74 | 126 | |
f9aef99b GK |
127 | fp = local_fopenat(map_dirfd, name, "r"); |
128 | close_preserve_errno(map_dirfd); | |
2c30dd74 AK |
129 | 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 | ||
2289be19 | 148 | static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) |
131dcb25 | 149 | { |
f9aef99b GK |
150 | 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; | |
154 | ||
155 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); | |
156 | if (dirfd == -1) { | |
157 | goto out; | |
158 | } | |
2289be19 | 159 | |
f9aef99b | 160 | err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); |
1237ad76 | 161 | if (err) { |
4fa4ce71 | 162 | goto err_out; |
1237ad76 | 163 | } |
b97400ca | 164 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
1237ad76 VJ |
165 | /* 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; | |
f9aef99b GK |
170 | |
171 | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, | |
172 | sizeof(uid_t)) > 0) { | |
f8ad4a89 | 173 | stbuf->st_uid = le32_to_cpu(tmp_uid); |
1237ad76 | 174 | } |
f9aef99b GK |
175 | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, |
176 | sizeof(gid_t)) > 0) { | |
f8ad4a89 | 177 | stbuf->st_gid = le32_to_cpu(tmp_gid); |
1237ad76 | 178 | } |
f9aef99b GK |
179 | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, |
180 | sizeof(mode_t)) > 0) { | |
f8ad4a89 | 181 | stbuf->st_mode = le32_to_cpu(tmp_mode); |
1237ad76 | 182 | } |
f9aef99b GK |
183 | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, |
184 | sizeof(dev_t)) > 0) { | |
f8ad4a89 | 185 | stbuf->st_rdev = le64_to_cpu(tmp_dev); |
1237ad76 | 186 | } |
2c30dd74 | 187 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
f9aef99b | 188 | local_mapped_file_attr(dirfd, name, stbuf); |
2c30dd74 | 189 | } |
4fa4ce71 CG |
190 | |
191 | err_out: | |
f9aef99b GK |
192 | close_preserve_errno(dirfd); |
193 | out: | |
194 | g_free(name); | |
195 | g_free(dirpath); | |
1237ad76 | 196 | return err; |
131dcb25 AL |
197 | } |
198 | ||
e3187a45 GK |
199 | static int local_set_mapped_file_attrat(int dirfd, const char *name, |
200 | FsCred *credp) | |
2c30dd74 AK |
201 | { |
202 | FILE *fp; | |
e3187a45 | 203 | int ret; |
2c30dd74 | 204 | char buf[ATTR_MAX]; |
2c30dd74 | 205 | int uid = -1, gid = -1, mode = -1, rdev = -1; |
e3187a45 GK |
206 | int map_dirfd; |
207 | ||
208 | ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700); | |
209 | if (ret < 0 && errno != EEXIST) { | |
210 | return -1; | |
211 | } | |
2c30dd74 | 212 | |
e3187a45 GK |
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"); | |
2c30dd74 | 219 | if (!fp) { |
e3187a45 GK |
220 | if (errno == ENOENT) { |
221 | goto update_map_file; | |
222 | } else { | |
223 | close_preserve_errno(map_dirfd); | |
224 | return -1; | |
225 | } | |
2c30dd74 AK |
226 | } |
227 | memset(buf, 0, ATTR_MAX); | |
228 | while (fgets(buf, ATTR_MAX, fp)) { | |
229 | if (!strncmp(buf, "virtfs.uid", 10)) { | |
e3187a45 | 230 | uid = atoi(buf + 11); |
2c30dd74 | 231 | } else if (!strncmp(buf, "virtfs.gid", 10)) { |
e3187a45 | 232 | gid = atoi(buf + 11); |
2c30dd74 | 233 | } else if (!strncmp(buf, "virtfs.mode", 11)) { |
e3187a45 | 234 | mode = atoi(buf + 12); |
2c30dd74 | 235 | } else if (!strncmp(buf, "virtfs.rdev", 11)) { |
e3187a45 | 236 | rdev = atoi(buf + 12); |
2c30dd74 AK |
237 | } |
238 | memset(buf, 0, ATTR_MAX); | |
239 | } | |
240 | fclose(fp); | |
2c30dd74 AK |
241 | |
242 | update_map_file: | |
e3187a45 GK |
243 | fp = local_fopenat(map_dirfd, name, "w"); |
244 | close_preserve_errno(map_dirfd); | |
2c30dd74 | 245 | if (!fp) { |
e3187a45 | 246 | return -1; |
2c30dd74 AK |
247 | } |
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 | ||
2c30dd74 AK |
262 | 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 | ||
e3187a45 GK |
276 | return 0; |
277 | } | |
278 | ||
279 | static 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); | |
2c30dd74 AK |
306 | return ret; |
307 | } | |
308 | ||
e3187a45 | 309 | static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) |
131dcb25 | 310 | { |
758e8e38 | 311 | int err; |
2289be19 | 312 | |
758e8e38 | 313 | if (credp->fc_uid != -1) { |
f8ad4a89 | 314 | uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); |
e3187a45 GK |
315 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, |
316 | sizeof(uid_t), 0); | |
758e8e38 VJ |
317 | if (err) { |
318 | return err; | |
319 | } | |
131dcb25 | 320 | } |
758e8e38 | 321 | if (credp->fc_gid != -1) { |
f8ad4a89 | 322 | uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); |
e3187a45 GK |
323 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, |
324 | sizeof(gid_t), 0); | |
758e8e38 VJ |
325 | if (err) { |
326 | return err; | |
327 | } | |
131dcb25 | 328 | } |
758e8e38 | 329 | if (credp->fc_mode != -1) { |
f8ad4a89 | 330 | uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); |
e3187a45 GK |
331 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, |
332 | sizeof(mode_t), 0); | |
758e8e38 VJ |
333 | if (err) { |
334 | return err; | |
335 | } | |
131dcb25 | 336 | } |
758e8e38 | 337 | if (credp->fc_rdev != -1) { |
f8ad4a89 | 338 | uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); |
e3187a45 GK |
339 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, |
340 | sizeof(dev_t), 0); | |
758e8e38 VJ |
341 | if (err) { |
342 | return err; | |
343 | } | |
131dcb25 | 344 | } |
131dcb25 AL |
345 | return 0; |
346 | } | |
347 | ||
d815e721 GK |
348 | static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, |
349 | const char *name, FsCred *credp) | |
4750a96f | 350 | { |
d815e721 | 351 | if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
b314f6a0 | 352 | AT_SYMLINK_NOFOLLOW) < 0) { |
12848bfc AK |
353 | /* |
354 | * If we fail to change ownership and if we are | |
355 | * using security model none. Ignore the error | |
356 | */ | |
b97400ca | 357 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
d815e721 | 358 | return -1; |
12848bfc | 359 | } |
4750a96f | 360 | } |
2d40564a | 361 | |
d815e721 | 362 | return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); |
4750a96f VJ |
363 | } |
364 | ||
2289be19 AK |
365 | static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, |
366 | char *buf, size_t bufsz) | |
131dcb25 | 367 | { |
879c2813 | 368 | ssize_t tsize = -1; |
2289be19 | 369 | |
2c30dd74 AK |
370 | if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || |
371 | (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { | |
879c2813 | 372 | int fd; |
bec1e954 GK |
373 | |
374 | fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); | |
879c2813 VJ |
375 | if (fd == -1) { |
376 | return -1; | |
377 | } | |
378 | do { | |
379 | tsize = read(fd, (void *)buf, bufsz); | |
380 | } while (tsize == -1 && errno == EINTR); | |
bec1e954 | 381 | close_preserve_errno(fd); |
b97400ca AK |
382 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
383 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
bec1e954 GK |
384 | 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); | |
879c2813 VJ |
398 | } |
399 | return tsize; | |
131dcb25 AL |
400 | } |
401 | ||
cc720ddb | 402 | static int local_close(FsContext *ctx, V9fsFidOpenState *fs) |
131dcb25 | 403 | { |
cc720ddb | 404 | return close(fs->fd); |
131dcb25 AL |
405 | } |
406 | ||
cc720ddb | 407 | static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) |
131dcb25 | 408 | { |
f314ea4e | 409 | return closedir(fs->dir.stream); |
131dcb25 | 410 | } |
9f107513 | 411 | |
cc720ddb AK |
412 | static int local_open(FsContext *ctx, V9fsPath *fs_path, |
413 | int flags, V9fsFidOpenState *fs) | |
a6568fe2 | 414 | { |
21328e1e | 415 | int fd; |
2289be19 | 416 | |
996a0d76 | 417 | fd = local_open_nofollow(ctx, fs_path->data, flags, 0); |
21328e1e GK |
418 | if (fd == -1) { |
419 | return -1; | |
420 | } | |
421 | fs->fd = fd; | |
cc720ddb | 422 | return fs->fd; |
a6568fe2 AL |
423 | } |
424 | ||
cc720ddb AK |
425 | static int local_opendir(FsContext *ctx, |
426 | V9fsPath *fs_path, V9fsFidOpenState *fs) | |
a6568fe2 | 427 | { |
996a0d76 | 428 | int dirfd; |
21328e1e | 429 | DIR *stream; |
2289be19 | 430 | |
996a0d76 GK |
431 | dirfd = local_opendir_nofollow(ctx, fs_path->data); |
432 | if (dirfd == -1) { | |
433 | return -1; | |
434 | } | |
2289be19 | 435 | |
996a0d76 | 436 | stream = fdopendir(dirfd); |
21328e1e | 437 | if (!stream) { |
faab207f | 438 | close(dirfd); |
cc720ddb AK |
439 | return -1; |
440 | } | |
21328e1e | 441 | fs->dir.stream = stream; |
cc720ddb | 442 | return 0; |
a6568fe2 AL |
443 | } |
444 | ||
cc720ddb | 445 | static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) |
a9231555 | 446 | { |
f314ea4e | 447 | rewinddir(fs->dir.stream); |
a9231555 AL |
448 | } |
449 | ||
cc720ddb | 450 | static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) |
a9231555 | 451 | { |
f314ea4e | 452 | return telldir(fs->dir.stream); |
a9231555 AL |
453 | } |
454 | ||
635324e8 | 455 | static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) |
a9231555 | 456 | { |
635324e8 | 457 | struct dirent *entry; |
2c30dd74 AK |
458 | |
459 | again: | |
635324e8 GK |
460 | entry = readdir(fs->dir.stream); |
461 | if (!entry) { | |
462 | return NULL; | |
463 | } | |
464 | ||
840a1bf2 BB |
465 | if (ctx->export_flags & V9FS_SM_MAPPED) { |
466 | entry->d_type = DT_UNKNOWN; | |
467 | } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
635324e8 | 468 | if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { |
2c30dd74 AK |
469 | /* skp the meta data directory */ |
470 | goto again; | |
471 | } | |
840a1bf2 | 472 | entry->d_type = DT_UNKNOWN; |
2c30dd74 | 473 | } |
635324e8 GK |
474 | |
475 | return entry; | |
a9231555 AL |
476 | } |
477 | ||
cc720ddb | 478 | static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) |
a9231555 | 479 | { |
f314ea4e | 480 | seekdir(fs->dir.stream, off); |
a9231555 AL |
481 | } |
482 | ||
cc720ddb AK |
483 | static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, |
484 | const struct iovec *iov, | |
56d15a53 | 485 | int iovcnt, off_t offset) |
a9231555 | 486 | { |
56d15a53 | 487 | #ifdef CONFIG_PREADV |
cc720ddb | 488 | return preadv(fs->fd, iov, iovcnt, offset); |
56d15a53 | 489 | #else |
cc720ddb | 490 | int err = lseek(fs->fd, offset, SEEK_SET); |
56d15a53 SG |
491 | if (err == -1) { |
492 | return err; | |
493 | } else { | |
cc720ddb | 494 | return readv(fs->fd, iov, iovcnt); |
56d15a53 SG |
495 | } |
496 | #endif | |
a9231555 AL |
497 | } |
498 | ||
cc720ddb AK |
499 | static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, |
500 | const struct iovec *iov, | |
2289be19 | 501 | int iovcnt, off_t offset) |
8449360c | 502 | { |
6fe76acc | 503 | ssize_t ret; |
56d15a53 | 504 | #ifdef CONFIG_PREADV |
cc720ddb | 505 | ret = pwritev(fs->fd, iov, iovcnt, offset); |
56d15a53 | 506 | #else |
cc720ddb | 507 | int err = lseek(fs->fd, offset, SEEK_SET); |
56d15a53 SG |
508 | if (err == -1) { |
509 | return err; | |
510 | } else { | |
cc720ddb | 511 | ret = writev(fs->fd, iov, iovcnt); |
56d15a53 SG |
512 | } |
513 | #endif | |
d3ab98e6 AK |
514 | #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 | */ | |
cc720ddb | 521 | sync_file_range(fs->fd, offset, ret, |
d3ab98e6 AK |
522 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); |
523 | } | |
524 | #endif | |
525 | return ret; | |
8449360c AL |
526 | } |
527 | ||
2289be19 | 528 | static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
c494dd6f | 529 | { |
e3187a45 GK |
530 | char *dirpath = g_path_get_dirname(fs_path->data); |
531 | char *name = g_path_get_basename(fs_path->data); | |
4fa4ce71 | 532 | int ret = -1; |
e3187a45 GK |
533 | int dirfd; |
534 | ||
535 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); | |
536 | if (dirfd == -1) { | |
537 | goto out; | |
538 | } | |
2289be19 | 539 | |
b97400ca | 540 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
e3187a45 | 541 | ret = local_set_xattrat(dirfd, name, credp); |
2c30dd74 | 542 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
e3187a45 GK |
543 | 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); | |
e95ead32 | 547 | } |
e3187a45 GK |
548 | close_preserve_errno(dirfd); |
549 | ||
550 | out: | |
551 | g_free(dirpath); | |
552 | g_free(name); | |
4fa4ce71 | 553 | return ret; |
c494dd6f AL |
554 | } |
555 | ||
2289be19 AK |
556 | static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, |
557 | const char *name, FsCred *credp) | |
c494dd6f | 558 | { |
1c293312 | 559 | int err = -1; |
d815e721 | 560 | int dirfd; |
1c293312 | 561 | |
d815e721 GK |
562 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
563 | if (dirfd == -1) { | |
564 | return -1; | |
565 | } | |
2289be19 | 566 | |
d815e721 GK |
567 | 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); | |
1c293312 | 570 | if (err == -1) { |
2289be19 | 571 | goto out; |
1c293312 | 572 | } |
2c30dd74 | 573 | |
d815e721 GK |
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); | |
2c30dd74 | 578 | } |
2c30dd74 | 579 | if (err == -1) { |
2c30dd74 AK |
580 | goto err_end; |
581 | } | |
d815e721 GK |
582 | } 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); | |
1c293312 | 585 | if (err == -1) { |
2289be19 | 586 | goto out; |
1c293312 | 587 | } |
d815e721 | 588 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
1c293312 | 589 | if (err == -1) { |
1c293312 VJ |
590 | goto err_end; |
591 | } | |
592 | } | |
2289be19 | 593 | goto out; |
1c293312 VJ |
594 | |
595 | err_end: | |
d815e721 | 596 | unlinkat_preserve_errno(dirfd, name, 0); |
2289be19 | 597 | out: |
d815e721 | 598 | close_preserve_errno(dirfd); |
1c293312 | 599 | return err; |
c494dd6f AL |
600 | } |
601 | ||
2289be19 AK |
602 | static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, |
603 | const char *name, FsCred *credp) | |
c494dd6f | 604 | { |
00ec5c37 | 605 | int err = -1; |
3f3a1699 | 606 | int dirfd; |
00ec5c37 | 607 | |
3f3a1699 GK |
608 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
609 | if (dirfd == -1) { | |
610 | return -1; | |
611 | } | |
2289be19 | 612 | |
3f3a1699 GK |
613 | 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); | |
00ec5c37 | 616 | if (err == -1) { |
2289be19 | 617 | goto out; |
00ec5c37 | 618 | } |
3f3a1699 GK |
619 | 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); | |
2c30dd74 | 625 | } |
2c30dd74 | 626 | if (err == -1) { |
2c30dd74 AK |
627 | goto err_end; |
628 | } | |
3f3a1699 GK |
629 | } 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); | |
00ec5c37 | 632 | if (err == -1) { |
2289be19 | 633 | goto out; |
00ec5c37 | 634 | } |
3f3a1699 | 635 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
00ec5c37 | 636 | if (err == -1) { |
00ec5c37 VJ |
637 | goto err_end; |
638 | } | |
639 | } | |
2289be19 | 640 | goto out; |
00ec5c37 VJ |
641 | |
642 | err_end: | |
3f3a1699 | 643 | unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); |
2289be19 | 644 | out: |
3f3a1699 | 645 | close_preserve_errno(dirfd); |
00ec5c37 | 646 | return err; |
c494dd6f AL |
647 | } |
648 | ||
8b888272 | 649 | static int local_fstat(FsContext *fs_ctx, int fid_type, |
cc720ddb | 650 | V9fsFidOpenState *fs, struct stat *stbuf) |
c494dd6f | 651 | { |
8b888272 AK |
652 | int err, fd; |
653 | ||
654 | if (fid_type == P9_FID_DIR) { | |
f314ea4e | 655 | fd = dirfd(fs->dir.stream); |
8b888272 AK |
656 | } else { |
657 | fd = fs->fd; | |
658 | } | |
659 | ||
660 | err = fstat(fd, stbuf); | |
1237ad76 VJ |
661 | if (err) { |
662 | return err; | |
663 | } | |
b97400ca | 664 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
1237ad76 VJ |
665 | /* 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 | ||
f8ad4a89 AK |
671 | if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { |
672 | stbuf->st_uid = le32_to_cpu(tmp_uid); | |
1237ad76 | 673 | } |
f8ad4a89 AK |
674 | if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { |
675 | stbuf->st_gid = le32_to_cpu(tmp_gid); | |
1237ad76 | 676 | } |
f8ad4a89 AK |
677 | if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { |
678 | stbuf->st_mode = le32_to_cpu(tmp_mode); | |
1237ad76 | 679 | } |
f8ad4a89 AK |
680 | if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { |
681 | stbuf->st_rdev = le64_to_cpu(tmp_dev); | |
1237ad76 | 682 | } |
2c30dd74 AK |
683 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
684 | errno = EOPNOTSUPP; | |
685 | return -1; | |
1237ad76 VJ |
686 | } |
687 | return err; | |
c494dd6f AL |
688 | } |
689 | ||
2289be19 | 690 | static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, |
cc720ddb | 691 | int flags, FsCred *credp, V9fsFidOpenState *fs) |
c494dd6f | 692 | { |
4750a96f VJ |
693 | int fd = -1; |
694 | int err = -1; | |
a565fea5 | 695 | int dirfd; |
4750a96f | 696 | |
0ceb092e AK |
697 | /* |
698 | * Mark all the open to not follow symlinks | |
699 | */ | |
700 | flags |= O_NOFOLLOW; | |
701 | ||
a565fea5 GK |
702 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
703 | if (dirfd == -1) { | |
704 | return -1; | |
705 | } | |
2289be19 | 706 | |
4750a96f | 707 | /* Determine the security model */ |
a565fea5 GK |
708 | 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); | |
4750a96f | 711 | if (fd == -1) { |
2289be19 | 712 | goto out; |
4750a96f VJ |
713 | } |
714 | credp->fc_mode = credp->fc_mode|S_IFREG; | |
a565fea5 GK |
715 | 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); | |
2c30dd74 | 720 | } |
2c30dd74 | 721 | if (err == -1) { |
2c30dd74 AK |
722 | goto err_end; |
723 | } | |
b97400ca AK |
724 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
725 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
a565fea5 | 726 | fd = openat_file(dirfd, name, flags, credp->fc_mode); |
4750a96f | 727 | if (fd == -1) { |
2289be19 | 728 | goto out; |
4750a96f | 729 | } |
a565fea5 | 730 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
4750a96f | 731 | if (err == -1) { |
4750a96f VJ |
732 | goto err_end; |
733 | } | |
734 | } | |
2289be19 | 735 | err = fd; |
cc720ddb | 736 | fs->fd = fd; |
2289be19 | 737 | goto out; |
4750a96f VJ |
738 | |
739 | err_end: | |
a565fea5 GK |
740 | unlinkat_preserve_errno(dirfd, name, |
741 | flags & O_DIRECTORY ? AT_REMOVEDIR : 0); | |
742 | close_preserve_errno(fd); | |
2289be19 | 743 | out: |
a565fea5 | 744 | close_preserve_errno(dirfd); |
4750a96f | 745 | return err; |
c494dd6f AL |
746 | } |
747 | ||
758e8e38 | 748 | |
879c2813 | 749 | static int local_symlink(FsContext *fs_ctx, const char *oldpath, |
2289be19 | 750 | V9fsPath *dir_path, const char *name, FsCred *credp) |
c494dd6f | 751 | { |
879c2813 | 752 | int err = -1; |
38771613 | 753 | int dirfd; |
879c2813 | 754 | |
38771613 GK |
755 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
756 | if (dirfd == -1) { | |
757 | return -1; | |
758 | } | |
2289be19 | 759 | |
879c2813 | 760 | /* Determine the security model */ |
38771613 GK |
761 | if (fs_ctx->export_flags & V9FS_SM_MAPPED || |
762 | fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
879c2813 VJ |
763 | int fd; |
764 | ssize_t oldpath_size, write_size; | |
38771613 GK |
765 | |
766 | fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, | |
767 | SM_LOCAL_MODE_BITS); | |
879c2813 | 768 | if (fd == -1) { |
2289be19 | 769 | goto out; |
879c2813 VJ |
770 | } |
771 | /* Write the oldpath (target) to the file. */ | |
f35bde2f | 772 | oldpath_size = strlen(oldpath); |
879c2813 VJ |
773 | do { |
774 | write_size = write(fd, (void *)oldpath, oldpath_size); | |
775 | } while (write_size == -1 && errno == EINTR); | |
38771613 | 776 | close_preserve_errno(fd); |
879c2813 VJ |
777 | |
778 | if (write_size != oldpath_size) { | |
879c2813 VJ |
779 | goto err_end; |
780 | } | |
879c2813 | 781 | /* Set cleint credentials in symlink's xattr */ |
38771613 | 782 | credp->fc_mode = credp->fc_mode | S_IFLNK; |
2c30dd74 | 783 | |
38771613 GK |
784 | 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); | |
2c30dd74 | 788 | } |
2c30dd74 | 789 | if (err == -1) { |
2c30dd74 AK |
790 | goto err_end; |
791 | } | |
38771613 GK |
792 | } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || |
793 | fs_ctx->export_flags & V9FS_SM_NONE) { | |
794 | err = symlinkat(oldpath, dirfd, name); | |
879c2813 | 795 | if (err) { |
2289be19 | 796 | goto out; |
879c2813 | 797 | } |
38771613 GK |
798 | err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
799 | AT_SYMLINK_NOFOLLOW); | |
879c2813 | 800 | if (err == -1) { |
12848bfc AK |
801 | /* |
802 | * If we fail to change ownership and if we are | |
803 | * using security model none. Ignore the error | |
804 | */ | |
b97400ca | 805 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
12848bfc | 806 | goto err_end; |
38771613 | 807 | } else { |
12848bfc | 808 | err = 0; |
38771613 | 809 | } |
879c2813 VJ |
810 | } |
811 | } | |
2289be19 | 812 | goto out; |
879c2813 VJ |
813 | |
814 | err_end: | |
38771613 | 815 | unlinkat_preserve_errno(dirfd, name, 0); |
2289be19 | 816 | out: |
38771613 | 817 | close_preserve_errno(dirfd); |
879c2813 | 818 | return err; |
c494dd6f AL |
819 | } |
820 | ||
2289be19 AK |
821 | static int local_link(FsContext *ctx, V9fsPath *oldpath, |
822 | V9fsPath *dirpath, const char *name) | |
c494dd6f | 823 | { |
ad0b46e6 GK |
824 | 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; | |
828 | ||
829 | odirfd = local_opendir_nofollow(ctx, odirpath); | |
830 | if (odirfd == -1) { | |
831 | goto out; | |
832 | } | |
c494dd6f | 833 | |
ad0b46e6 GK |
834 | ndirfd = local_opendir_nofollow(ctx, dirpath->data); |
835 | if (ndirfd == -1) { | |
836 | close_preserve_errno(odirfd); | |
837 | goto out; | |
838 | } | |
2289be19 | 839 | |
ad0b46e6 | 840 | ret = linkat(odirfd, oname, ndirfd, name, 0); |
6dd4b1f1 | 841 | if (ret < 0) { |
ad0b46e6 | 842 | goto out_close; |
6dd4b1f1 | 843 | } |
2c30dd74 AK |
844 | |
845 | /* now link the virtfs_metadata files */ | |
6dd4b1f1 | 846 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
ad0b46e6 GK |
847 | int omap_dirfd, nmap_dirfd; |
848 | ||
849 | ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); | |
850 | if (ret < 0 && errno != EEXIST) { | |
851 | goto err_undo_link; | |
2c30dd74 | 852 | } |
ad0b46e6 GK |
853 | |
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); | |
2c30dd74 | 868 | if (ret < 0 && errno != ENOENT) { |
ad0b46e6 | 869 | goto err_undo_link; |
2c30dd74 | 870 | } |
c494dd6f | 871 | |
ad0b46e6 | 872 | ret = 0; |
2c30dd74 | 873 | } |
ad0b46e6 | 874 | goto out_close; |
2289be19 | 875 | |
ad0b46e6 GK |
876 | err: |
877 | ret = -1; | |
878 | err_undo_link: | |
879 | unlinkat_preserve_errno(ndirfd, name, 0); | |
880 | out_close: | |
881 | close_preserve_errno(ndirfd); | |
882 | close_preserve_errno(odirfd); | |
6dd4b1f1 | 883 | out: |
ad0b46e6 GK |
884 | g_free(oname); |
885 | g_free(odirpath); | |
4fa4ce71 | 886 | return ret; |
8cf89e00 AL |
887 | } |
888 | ||
2289be19 | 889 | static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) |
8cf89e00 | 890 | { |
ac125d99 | 891 | int fd, ret; |
8cf89e00 | 892 | |
ac125d99 GK |
893 | fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); |
894 | if (fd == -1) { | |
895 | return -1; | |
2c30dd74 | 896 | } |
ac125d99 GK |
897 | ret = ftruncate(fd, size); |
898 | close_preserve_errno(fd); | |
4fa4ce71 | 899 | return ret; |
8cf89e00 AL |
900 | } |
901 | ||
2289be19 | 902 | static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
8cf89e00 | 903 | { |
d369f207 GK |
904 | char *dirpath = g_path_get_dirname(fs_path->data); |
905 | char *name = g_path_get_basename(fs_path->data); | |
4fa4ce71 | 906 | int ret = -1; |
d369f207 GK |
907 | int dirfd; |
908 | ||
909 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); | |
910 | if (dirfd == -1) { | |
911 | goto out; | |
912 | } | |
2289be19 | 913 | |
c79ce737 | 914 | if ((credp->fc_uid == -1 && credp->fc_gid == -1) || |
17b1971f AK |
915 | (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
916 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
d369f207 GK |
917 | ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
918 | AT_SYMLINK_NOFOLLOW); | |
b97400ca | 919 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
d369f207 | 920 | ret = local_set_xattrat(dirfd, name, credp); |
2c30dd74 | 921 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
d369f207 | 922 | ret = local_set_mapped_file_attrat(dirfd, name, credp); |
f7613bee | 923 | } |
d369f207 GK |
924 | |
925 | close_preserve_errno(dirfd); | |
926 | out: | |
927 | g_free(name); | |
928 | g_free(dirpath); | |
4fa4ce71 | 929 | return ret; |
8cf89e00 AL |
930 | } |
931 | ||
2289be19 | 932 | static int local_utimensat(FsContext *s, V9fsPath *fs_path, |
38671423 | 933 | const struct timespec *buf) |
8cf89e00 | 934 | { |
a33eda0d GK |
935 | char *dirpath = g_path_get_dirname(fs_path->data); |
936 | char *name = g_path_get_basename(fs_path->data); | |
937 | int dirfd, ret = -1; | |
2289be19 | 938 | |
a33eda0d GK |
939 | dirfd = local_opendir_nofollow(s, dirpath); |
940 | if (dirfd == -1) { | |
941 | goto out; | |
942 | } | |
2289be19 | 943 | |
a33eda0d GK |
944 | ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); |
945 | close_preserve_errno(dirfd); | |
946 | out: | |
947 | g_free(dirpath); | |
948 | g_free(name); | |
4fa4ce71 | 949 | return ret; |
8cf89e00 AL |
950 | } |
951 | ||
df4938a6 GK |
952 | static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, |
953 | int flags) | |
5bae1900 | 954 | { |
df4938a6 | 955 | int ret = -1; |
2c30dd74 AK |
956 | |
957 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
df4938a6 GK |
958 | int map_dirfd; |
959 | ||
960 | if (flags == AT_REMOVEDIR) { | |
961 | int fd; | |
962 | ||
b003fc0d | 963 | fd = openat_dir(dirfd, name); |
df4938a6 GK |
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) { | |
2c30dd74 AK |
974 | /* |
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 | |
df4938a6 | 983 | * .virtfs_metadata directory. |
2c30dd74 | 984 | */ |
df4938a6 GK |
985 | 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) { | |
2c30dd74 AK |
989 | /* |
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 | } | |
4fa4ce71 | 996 | |
df4938a6 GK |
997 | ret = unlinkat(dirfd, name, flags); |
998 | err_out: | |
999 | return ret; | |
1000 | } | |
1001 | ||
5bae1900 AL |
1002 | static int local_remove(FsContext *ctx, const char *path) |
1003 | { | |
2c30dd74 | 1004 | struct stat stbuf; |
a0e640a8 GK |
1005 | 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; | |
2c30dd74 | 1010 | |
a0e640a8 | 1011 | dirfd = local_opendir_nofollow(ctx, dirpath); |
b7361d46 | 1012 | if (dirfd == -1) { |
a0e640a8 | 1013 | goto out; |
2c30dd74 | 1014 | } |
4fa4ce71 | 1015 | |
a0e640a8 GK |
1016 | 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); | |
2c30dd74 | 1025 | err_out: |
a0e640a8 GK |
1026 | close_preserve_errno(dirfd); |
1027 | out: | |
1028 | g_free(name); | |
1029 | g_free(dirpath); | |
2c30dd74 | 1030 | return err; |
5bae1900 AL |
1031 | } |
1032 | ||
8b888272 AK |
1033 | static int local_fsync(FsContext *ctx, int fid_type, |
1034 | V9fsFidOpenState *fs, int datasync) | |
8cf89e00 | 1035 | { |
8b888272 AK |
1036 | int fd; |
1037 | ||
1038 | if (fid_type == P9_FID_DIR) { | |
f314ea4e | 1039 | fd = dirfd(fs->dir.stream); |
8b888272 AK |
1040 | } else { |
1041 | fd = fs->fd; | |
1042 | } | |
1043 | ||
49594973 | 1044 | if (datasync) { |
8b888272 | 1045 | return qemu_fdatasync(fd); |
49594973 | 1046 | } else { |
8b888272 | 1047 | return fsync(fd); |
49594973 | 1048 | } |
8cf89e00 AL |
1049 | } |
1050 | ||
2289be19 | 1051 | static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) |
be940c87 | 1052 | { |
31e51d1c | 1053 | int fd, ret; |
2289be19 | 1054 | |
31e51d1c | 1055 | fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); |
23da0145 GK |
1056 | if (fd == -1) { |
1057 | return -1; | |
1058 | } | |
31e51d1c GK |
1059 | ret = fstatfs(fd, stbuf); |
1060 | close_preserve_errno(fd); | |
4fa4ce71 | 1061 | return ret; |
be940c87 MK |
1062 | } |
1063 | ||
2289be19 | 1064 | static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, |
fa32ef88 AK |
1065 | const char *name, void *value, size_t size) |
1066 | { | |
2289be19 AK |
1067 | char *path = fs_path->data; |
1068 | ||
fc22118d | 1069 | return v9fs_get_xattr(ctx, path, name, value, size); |
fa32ef88 AK |
1070 | } |
1071 | ||
2289be19 | 1072 | static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, |
fa32ef88 AK |
1073 | void *value, size_t size) |
1074 | { | |
2289be19 AK |
1075 | char *path = fs_path->data; |
1076 | ||
fc22118d | 1077 | return v9fs_list_xattr(ctx, path, value, size); |
fa32ef88 AK |
1078 | } |
1079 | ||
2289be19 | 1080 | static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, |
10b468bd AK |
1081 | void *value, size_t size, int flags) |
1082 | { | |
2289be19 AK |
1083 | char *path = fs_path->data; |
1084 | ||
fc22118d | 1085 | return v9fs_set_xattr(ctx, path, name, value, size, flags); |
10b468bd AK |
1086 | } |
1087 | ||
2289be19 AK |
1088 | static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, |
1089 | const char *name) | |
9ed3ef26 | 1090 | { |
2289be19 AK |
1091 | char *path = fs_path->data; |
1092 | ||
fc22118d | 1093 | return v9fs_remove_xattr(ctx, path, name); |
9ed3ef26 AK |
1094 | } |
1095 | ||
2289be19 AK |
1096 | static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, |
1097 | const char *name, V9fsPath *target) | |
1098 | { | |
1099 | if (dir_path) { | |
e3e83f2e | 1100 | v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); |
2289be19 | 1101 | } else { |
e3e83f2e | 1102 | v9fs_path_sprintf(target, "%s", name); |
2289be19 | 1103 | } |
2289be19 AK |
1104 | return 0; |
1105 | } | |
1106 | ||
1107 | static int local_renameat(FsContext *ctx, V9fsPath *olddir, | |
1108 | const char *old_name, V9fsPath *newdir, | |
1109 | const char *new_name) | |
1110 | { | |
1111 | int ret; | |
99f2cf4b | 1112 | int odirfd, ndirfd; |
2289be19 | 1113 | |
99f2cf4b GK |
1114 | odirfd = local_opendir_nofollow(ctx, olddir->data); |
1115 | if (odirfd == -1) { | |
1116 | return -1; | |
1117 | } | |
2289be19 | 1118 | |
99f2cf4b GK |
1119 | ndirfd = local_opendir_nofollow(ctx, newdir->data); |
1120 | if (ndirfd == -1) { | |
1121 | close_preserve_errno(odirfd); | |
1122 | return -1; | |
1123 | } | |
2289be19 | 1124 | |
99f2cf4b GK |
1125 | ret = renameat(odirfd, old_name, ndirfd, new_name); |
1126 | if (ret < 0) { | |
1127 | goto out; | |
1128 | } | |
2289be19 | 1129 | |
99f2cf4b GK |
1130 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
1131 | int omap_dirfd, nmap_dirfd; | |
2289be19 | 1132 | |
99f2cf4b GK |
1133 | ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); |
1134 | if (ret < 0 && errno != EEXIST) { | |
1135 | goto err_undo_rename; | |
1136 | } | |
2289be19 | 1137 | |
6dd4b1f1 | 1138 | omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); |
99f2cf4b GK |
1139 | if (omap_dirfd == -1) { |
1140 | goto err; | |
1141 | } | |
1142 | ||
6dd4b1f1 | 1143 | nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); |
99f2cf4b GK |
1144 | if (nmap_dirfd == -1) { |
1145 | close_preserve_errno(omap_dirfd); | |
1146 | goto err; | |
1147 | } | |
1148 | ||
1149 | /* rename the .virtfs_metadata files */ | |
1150 | ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); | |
1151 | close_preserve_errno(nmap_dirfd); | |
1152 | close_preserve_errno(omap_dirfd); | |
1153 | if (ret < 0 && errno != ENOENT) { | |
1154 | goto err_undo_rename; | |
1155 | } | |
1156 | ||
1157 | ret = 0; | |
1158 | } | |
1159 | goto out; | |
1160 | ||
1161 | err: | |
1162 | ret = -1; | |
1163 | err_undo_rename: | |
1164 | renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); | |
1165 | out: | |
1166 | close_preserve_errno(ndirfd); | |
1167 | close_preserve_errno(odirfd); | |
2289be19 AK |
1168 | return ret; |
1169 | } | |
1170 | ||
d2767ede GK |
1171 | static void v9fs_path_init_dirname(V9fsPath *path, const char *str) |
1172 | { | |
1173 | path->data = g_path_get_dirname(str); | |
1174 | path->size = strlen(path->data) + 1; | |
1175 | } | |
1176 | ||
1177 | static int local_rename(FsContext *ctx, const char *oldpath, | |
1178 | const char *newpath) | |
1179 | { | |
1180 | int err; | |
1181 | char *oname = g_path_get_basename(oldpath); | |
1182 | char *nname = g_path_get_basename(newpath); | |
1183 | V9fsPath olddir, newdir; | |
1184 | ||
1185 | v9fs_path_init_dirname(&olddir, oldpath); | |
1186 | v9fs_path_init_dirname(&newdir, newpath); | |
1187 | ||
1188 | err = local_renameat(ctx, &olddir, oname, &newdir, nname); | |
1189 | ||
1190 | v9fs_path_free(&newdir); | |
1191 | v9fs_path_free(&olddir); | |
1192 | g_free(nname); | |
1193 | g_free(oname); | |
1194 | ||
1195 | return err; | |
1196 | } | |
1197 | ||
2289be19 AK |
1198 | static int local_unlinkat(FsContext *ctx, V9fsPath *dir, |
1199 | const char *name, int flags) | |
1200 | { | |
1201 | int ret; | |
df4938a6 | 1202 | int dirfd; |
2289be19 | 1203 | |
df4938a6 GK |
1204 | dirfd = local_opendir_nofollow(ctx, dir->data); |
1205 | if (dirfd == -1) { | |
1206 | return -1; | |
2c30dd74 | 1207 | } |
2289be19 | 1208 | |
df4938a6 GK |
1209 | ret = local_unlinkat_common(ctx, dirfd, name, flags); |
1210 | close_preserve_errno(dirfd); | |
2289be19 AK |
1211 | return ret; |
1212 | } | |
9ed3ef26 | 1213 | |
e06a765e HPB |
1214 | static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, |
1215 | mode_t st_mode, uint64_t *st_gen) | |
1216 | { | |
ae0f940e | 1217 | #ifdef FS_IOC_GETVERSION |
0e5fc994 | 1218 | int err; |
cc720ddb AK |
1219 | V9fsFidOpenState fid_open; |
1220 | ||
e06a765e HPB |
1221 | /* |
1222 | * Do not try to open special files like device nodes, fifos etc | |
1223 | * We can get fd for regular files and directories only | |
1224 | */ | |
1225 | if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { | |
1a9978a5 KS |
1226 | errno = ENOTTY; |
1227 | return -1; | |
e06a765e | 1228 | } |
cc720ddb AK |
1229 | err = local_open(ctx, path, O_RDONLY, &fid_open); |
1230 | if (err < 0) { | |
1231 | return err; | |
e06a765e | 1232 | } |
cc720ddb AK |
1233 | err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); |
1234 | local_close(ctx, &fid_open); | |
0e5fc994 | 1235 | return err; |
ae0f940e | 1236 | #else |
0e5fc994 KS |
1237 | errno = ENOTTY; |
1238 | return -1; | |
ae0f940e | 1239 | #endif |
e06a765e HPB |
1240 | } |
1241 | ||
0174fe73 AK |
1242 | static int local_init(FsContext *ctx) |
1243 | { | |
e06a765e | 1244 | struct statfs stbuf; |
0e35a378 GK |
1245 | LocalData *data = g_malloc(sizeof(*data)); |
1246 | ||
1247 | data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); | |
1248 | if (data->mountfd == -1) { | |
1249 | goto err; | |
1250 | } | |
e06a765e | 1251 | |
00c90bd1 GK |
1252 | #ifdef FS_IOC_GETVERSION |
1253 | /* | |
1254 | * use ioc_getversion only if the ioctl is definied | |
1255 | */ | |
0e35a378 GK |
1256 | if (fstatfs(data->mountfd, &stbuf) < 0) { |
1257 | close_preserve_errno(data->mountfd); | |
1258 | goto err; | |
00c90bd1 GK |
1259 | } |
1260 | switch (stbuf.f_type) { | |
1261 | case EXT2_SUPER_MAGIC: | |
1262 | case BTRFS_SUPER_MAGIC: | |
1263 | case REISERFS_SUPER_MAGIC: | |
1264 | case XFS_SUPER_MAGIC: | |
1265 | ctx->exops.get_st_gen = local_ioc_getversion; | |
1266 | break; | |
1267 | } | |
1268 | #endif | |
e06a765e | 1269 | |
2c30dd74 AK |
1270 | if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { |
1271 | ctx->xops = passthrough_xattr_ops; | |
1272 | } else if (ctx->export_flags & V9FS_SM_MAPPED) { | |
1273 | ctx->xops = mapped_xattr_ops; | |
1274 | } else if (ctx->export_flags & V9FS_SM_NONE) { | |
1275 | ctx->xops = none_xattr_ops; | |
1276 | } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
1277 | /* | |
1278 | * xattr operation for mapped-file and passthrough | |
1279 | * remain same. | |
1280 | */ | |
1281 | ctx->xops = passthrough_xattr_ops; | |
1282 | } | |
c98f1d4a | 1283 | ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; |
00c90bd1 | 1284 | |
0e35a378 | 1285 | ctx->private = data; |
00c90bd1 | 1286 | return 0; |
0e35a378 GK |
1287 | |
1288 | err: | |
1289 | g_free(data); | |
1290 | return -1; | |
1291 | } | |
1292 | ||
1293 | static void local_cleanup(FsContext *ctx) | |
1294 | { | |
1295 | LocalData *data = ctx->private; | |
1296 | ||
1297 | close(data->mountfd); | |
1298 | g_free(data); | |
0174fe73 AK |
1299 | } |
1300 | ||
99519f0a AK |
1301 | static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) |
1302 | { | |
1303 | const char *sec_model = qemu_opt_get(opts, "security_model"); | |
1304 | const char *path = qemu_opt_get(opts, "path"); | |
b8bbdb88 | 1305 | Error *err = NULL; |
99519f0a AK |
1306 | |
1307 | if (!sec_model) { | |
63325b18 GK |
1308 | error_report("Security model not specified, local fs needs security model"); |
1309 | error_printf("valid options are:" | |
1310 | "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); | |
99519f0a AK |
1311 | return -1; |
1312 | } | |
1313 | ||
1314 | if (!strcmp(sec_model, "passthrough")) { | |
1315 | fse->export_flags |= V9FS_SM_PASSTHROUGH; | |
2c30dd74 AK |
1316 | } else if (!strcmp(sec_model, "mapped") || |
1317 | !strcmp(sec_model, "mapped-xattr")) { | |
99519f0a AK |
1318 | fse->export_flags |= V9FS_SM_MAPPED; |
1319 | } else if (!strcmp(sec_model, "none")) { | |
1320 | fse->export_flags |= V9FS_SM_NONE; | |
2c30dd74 AK |
1321 | } else if (!strcmp(sec_model, "mapped-file")) { |
1322 | fse->export_flags |= V9FS_SM_MAPPED_FILE; | |
99519f0a | 1323 | } else { |
63325b18 GK |
1324 | error_report("Invalid security model %s specified", sec_model); |
1325 | error_printf("valid options are:" | |
1326 | "\t[passthrough|mapped-xattr|mapped-file|none]\n"); | |
99519f0a AK |
1327 | return -1; |
1328 | } | |
1329 | ||
1330 | if (!path) { | |
63325b18 | 1331 | error_report("fsdev: No path specified"); |
99519f0a AK |
1332 | return -1; |
1333 | } | |
b8bbdb88 PJ |
1334 | |
1335 | fsdev_throttle_parse_opts(opts, &fse->fst, &err); | |
1336 | if (err) { | |
1337 | error_reportf_err(err, "Throttle configuration is not valid: "); | |
1338 | return -1; | |
1339 | } | |
1340 | ||
99519f0a AK |
1341 | fse->path = g_strdup(path); |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
9f107513 | 1346 | FileOperations local_ops = { |
99519f0a | 1347 | .parse_opts = local_parse_opts, |
0174fe73 | 1348 | .init = local_init, |
0e35a378 | 1349 | .cleanup = local_cleanup, |
131dcb25 | 1350 | .lstat = local_lstat, |
131dcb25 AL |
1351 | .readlink = local_readlink, |
1352 | .close = local_close, | |
1353 | .closedir = local_closedir, | |
a6568fe2 AL |
1354 | .open = local_open, |
1355 | .opendir = local_opendir, | |
a9231555 AL |
1356 | .rewinddir = local_rewinddir, |
1357 | .telldir = local_telldir, | |
635324e8 | 1358 | .readdir = local_readdir, |
a9231555 | 1359 | .seekdir = local_seekdir, |
56d15a53 SG |
1360 | .preadv = local_preadv, |
1361 | .pwritev = local_pwritev, | |
c494dd6f AL |
1362 | .chmod = local_chmod, |
1363 | .mknod = local_mknod, | |
c494dd6f AL |
1364 | .mkdir = local_mkdir, |
1365 | .fstat = local_fstat, | |
1366 | .open2 = local_open2, | |
1367 | .symlink = local_symlink, | |
1368 | .link = local_link, | |
8cf89e00 AL |
1369 | .truncate = local_truncate, |
1370 | .rename = local_rename, | |
1371 | .chown = local_chown, | |
74bc02b2 | 1372 | .utimensat = local_utimensat, |
5bae1900 | 1373 | .remove = local_remove, |
8cf89e00 | 1374 | .fsync = local_fsync, |
be940c87 | 1375 | .statfs = local_statfs, |
fa32ef88 AK |
1376 | .lgetxattr = local_lgetxattr, |
1377 | .llistxattr = local_llistxattr, | |
10b468bd | 1378 | .lsetxattr = local_lsetxattr, |
9ed3ef26 | 1379 | .lremovexattr = local_lremovexattr, |
2289be19 AK |
1380 | .name_to_path = local_name_to_path, |
1381 | .renameat = local_renameat, | |
1382 | .unlinkat = local_unlinkat, | |
9f107513 | 1383 | }; |