/src/qemu/hw/9pfs/9p-local.c
Line | Count | Source |
1 | | /* |
2 | | * 9p Posix callback |
3 | | * |
4 | | * Copyright IBM, Corp. 2010 |
5 | | * |
6 | | * Authors: |
7 | | * Anthony Liguori <aliguori@us.ibm.com> |
8 | | * |
9 | | * This work is licensed under the terms of the GNU GPL, version 2. See |
10 | | * the COPYING file in the top-level directory. |
11 | | */ |
12 | | |
13 | | /* |
14 | | * Not so fast! You might want to read the 9p developer docs first: |
15 | | * https://wiki.qemu.org/Documentation/9p |
16 | | */ |
17 | | |
18 | | #include "qemu/osdep.h" |
19 | | #include "9p.h" |
20 | | #include "9p-local.h" |
21 | | #include "9p-xattr.h" |
22 | | #include "9p-util.h" |
23 | | #include "fsdev/qemu-fsdev.h" /* local_ops */ |
24 | | #include <arpa/inet.h> |
25 | | #include <pwd.h> |
26 | | #include <grp.h> |
27 | | #include <sys/socket.h> |
28 | | #include <sys/un.h> |
29 | | #include "qemu/xattr.h" |
30 | | #include "qapi/error.h" |
31 | | #include "qemu/bswap.h" |
32 | | #include "qemu/cutils.h" |
33 | | #include "qemu/error-report.h" |
34 | | #include "qemu/option.h" |
35 | | #include <libgen.h> |
36 | | #ifdef CONFIG_LINUX |
37 | | #include <linux/fs.h> |
38 | | #ifdef CONFIG_LINUX_MAGIC_H |
39 | | #include <linux/magic.h> |
40 | | #endif |
41 | | #endif |
42 | | #include <sys/ioctl.h> |
43 | | |
44 | | #ifndef XFS_SUPER_MAGIC |
45 | | #define XFS_SUPER_MAGIC 0x58465342 |
46 | | #endif |
47 | | #ifndef EXT2_SUPER_MAGIC |
48 | | #define EXT2_SUPER_MAGIC 0xEF53 |
49 | | #endif |
50 | | #ifndef REISERFS_SUPER_MAGIC |
51 | | #define REISERFS_SUPER_MAGIC 0x52654973 |
52 | | #endif |
53 | | #ifndef BTRFS_SUPER_MAGIC |
54 | | #define BTRFS_SUPER_MAGIC 0x9123683E |
55 | | #endif |
56 | | |
57 | | typedef struct { |
58 | | int mountfd; |
59 | | } LocalData; |
60 | | |
61 | | int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, |
62 | | mode_t mode) |
63 | 9.42k | { |
64 | 9.42k | LocalData *data = fs_ctx->private; |
65 | 9.42k | int fd = data->mountfd; |
66 | | |
67 | 18.8k | while (*path && fd != -1) { |
68 | 9.42k | const char *c; |
69 | 9.42k | int next_fd; |
70 | 9.42k | char *head; |
71 | | |
72 | | /* Only relative paths without consecutive slashes */ |
73 | 9.42k | assert(*path != '/'); |
74 | | |
75 | 9.42k | head = g_strdup(path); |
76 | 9.42k | c = qemu_strchrnul(path, '/'); |
77 | 9.42k | if (*c) { |
78 | | /* Intermediate path element */ |
79 | 0 | head[c - path] = 0; |
80 | 0 | path = c + 1; |
81 | 0 | next_fd = openat_dir(fd, head); |
82 | 9.42k | } else { |
83 | | /* Rightmost path element */ |
84 | 9.42k | next_fd = openat_file(fd, head, flags, mode); |
85 | 9.42k | path = c; |
86 | 9.42k | } |
87 | 9.42k | g_free(head); |
88 | 9.42k | if (fd != data->mountfd) { |
89 | 0 | close_preserve_errno(fd); |
90 | 0 | } |
91 | 9.42k | fd = next_fd; |
92 | 9.42k | } |
93 | | |
94 | 9.42k | assert(fd != data->mountfd); |
95 | 9.42k | return fd; |
96 | 9.42k | } |
97 | | |
98 | | int local_opendir_nofollow(FsContext *fs_ctx, const char *path) |
99 | 9.42k | { |
100 | 9.42k | return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0); |
101 | 9.42k | } |
102 | | |
103 | | static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, |
104 | | const char *npath) |
105 | 0 | { |
106 | 0 | int serrno = errno; |
107 | 0 | qemu_renameat(odirfd, opath, ndirfd, npath); |
108 | 0 | errno = serrno; |
109 | 0 | } |
110 | | |
111 | | static void unlinkat_preserve_errno(int dirfd, const char *path, int flags) |
112 | 0 | { |
113 | 0 | int serrno = errno; |
114 | 0 | qemu_unlinkat(dirfd, path, flags); |
115 | 0 | errno = serrno; |
116 | 0 | } |
117 | | |
118 | 0 | #define VIRTFS_META_DIR ".virtfs_metadata" |
119 | 0 | #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root" |
120 | | |
121 | | static FILE *local_fopenat(int dirfd, const char *name, const char *mode) |
122 | 0 | { |
123 | 0 | int fd, o_mode = 0; |
124 | 0 | FILE *fp; |
125 | 0 | int flags; |
126 | | /* |
127 | | * only supports two modes |
128 | | */ |
129 | 0 | if (mode[0] == 'r') { |
130 | 0 | flags = O_RDONLY; |
131 | 0 | } else if (mode[0] == 'w') { |
132 | 0 | flags = O_WRONLY | O_TRUNC | O_CREAT; |
133 | 0 | o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
134 | 0 | } else { |
135 | 0 | return NULL; |
136 | 0 | } |
137 | 0 | fd = openat_file(dirfd, name, flags, o_mode); |
138 | 0 | if (fd == -1) { |
139 | 0 | return NULL; |
140 | 0 | } |
141 | 0 | fp = fdopen(fd, mode); |
142 | 0 | if (!fp) { |
143 | 0 | close(fd); |
144 | 0 | } |
145 | 0 | return fp; |
146 | 0 | } |
147 | | |
148 | 0 | #define ATTR_MAX 100 |
149 | | static void local_mapped_file_attr(int dirfd, const char *name, |
150 | | struct stat *stbuf) |
151 | 0 | { |
152 | 0 | FILE *fp; |
153 | 0 | char buf[ATTR_MAX]; |
154 | 0 | int map_dirfd; |
155 | |
|
156 | 0 | if (strcmp(name, ".")) { |
157 | 0 | map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); |
158 | 0 | if (map_dirfd == -1) { |
159 | 0 | return; |
160 | 0 | } |
161 | | |
162 | 0 | fp = local_fopenat(map_dirfd, name, "r"); |
163 | 0 | close_preserve_errno(map_dirfd); |
164 | 0 | } else { |
165 | 0 | fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); |
166 | 0 | } |
167 | 0 | if (!fp) { |
168 | 0 | return; |
169 | 0 | } |
170 | 0 | memset(buf, 0, ATTR_MAX); |
171 | 0 | while (fgets(buf, ATTR_MAX, fp)) { |
172 | 0 | if (!strncmp(buf, "virtfs.uid", 10)) { |
173 | 0 | stbuf->st_uid = atoi(buf + 11); |
174 | 0 | } else if (!strncmp(buf, "virtfs.gid", 10)) { |
175 | 0 | stbuf->st_gid = atoi(buf + 11); |
176 | 0 | } else if (!strncmp(buf, "virtfs.mode", 11)) { |
177 | 0 | stbuf->st_mode = atoi(buf + 12); |
178 | 0 | } else if (!strncmp(buf, "virtfs.rdev", 11)) { |
179 | 0 | stbuf->st_rdev = atoi(buf + 12); |
180 | 0 | } |
181 | 0 | memset(buf, 0, ATTR_MAX); |
182 | 0 | } |
183 | 0 | fclose(fp); |
184 | 0 | } |
185 | | |
186 | | static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) |
187 | 9.42k | { |
188 | 9.42k | int err = -1; |
189 | 9.42k | char *dirpath = g_path_get_dirname(fs_path->data); |
190 | 9.42k | char *name = g_path_get_basename(fs_path->data); |
191 | 9.42k | int dirfd; |
192 | | |
193 | 9.42k | dirfd = local_opendir_nofollow(fs_ctx, dirpath); |
194 | 9.42k | if (dirfd == -1) { |
195 | 0 | goto out; |
196 | 0 | } |
197 | | |
198 | 9.42k | err = qemu_fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); |
199 | 9.42k | if (err) { |
200 | 0 | goto err_out; |
201 | 0 | } |
202 | 9.42k | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
203 | | /* Actual credentials are part of extended attrs */ |
204 | 9.42k | uid_t tmp_uid; |
205 | 9.42k | gid_t tmp_gid; |
206 | 9.42k | mode_t tmp_mode; |
207 | 9.42k | dev_t tmp_dev; |
208 | | |
209 | 9.42k | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, |
210 | 9.42k | sizeof(uid_t)) > 0) { |
211 | 0 | stbuf->st_uid = le32_to_cpu(tmp_uid); |
212 | 0 | } |
213 | 9.42k | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, |
214 | 9.42k | sizeof(gid_t)) > 0) { |
215 | 0 | stbuf->st_gid = le32_to_cpu(tmp_gid); |
216 | 0 | } |
217 | 9.42k | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, |
218 | 9.42k | sizeof(mode_t)) > 0) { |
219 | 0 | stbuf->st_mode = le32_to_cpu(tmp_mode); |
220 | 0 | } |
221 | 9.42k | if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, |
222 | 9.42k | sizeof(dev_t)) > 0) { |
223 | 0 | stbuf->st_rdev = le64_to_cpu(tmp_dev); |
224 | 0 | } |
225 | 9.42k | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
226 | 0 | local_mapped_file_attr(dirfd, name, stbuf); |
227 | 0 | } |
228 | | |
229 | 9.42k | err_out: |
230 | 9.42k | close_preserve_errno(dirfd); |
231 | 9.42k | out: |
232 | 9.42k | g_free(name); |
233 | 9.42k | g_free(dirpath); |
234 | 9.42k | return err; |
235 | 9.42k | } |
236 | | |
237 | | static int local_set_mapped_file_attrat(int dirfd, const char *name, |
238 | | FsCred *credp) |
239 | 0 | { |
240 | 0 | FILE *fp; |
241 | 0 | int ret; |
242 | 0 | char buf[ATTR_MAX]; |
243 | 0 | int uid = -1, gid = -1, mode = -1, rdev = -1; |
244 | 0 | int map_dirfd = -1, map_fd; |
245 | 0 | bool is_root = !strcmp(name, "."); |
246 | |
|
247 | 0 | if (is_root) { |
248 | 0 | fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r"); |
249 | 0 | if (!fp) { |
250 | 0 | if (errno == ENOENT) { |
251 | 0 | goto update_map_file; |
252 | 0 | } else { |
253 | 0 | return -1; |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } else { |
257 | 0 | ret = qemu_mkdirat(dirfd, VIRTFS_META_DIR, 0700); |
258 | 0 | if (ret < 0 && errno != EEXIST) { |
259 | 0 | return -1; |
260 | 0 | } |
261 | | |
262 | 0 | map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); |
263 | 0 | if (map_dirfd == -1) { |
264 | 0 | return -1; |
265 | 0 | } |
266 | | |
267 | 0 | fp = local_fopenat(map_dirfd, name, "r"); |
268 | 0 | if (!fp) { |
269 | 0 | if (errno == ENOENT) { |
270 | 0 | goto update_map_file; |
271 | 0 | } else { |
272 | 0 | close_preserve_errno(map_dirfd); |
273 | 0 | return -1; |
274 | 0 | } |
275 | 0 | } |
276 | 0 | } |
277 | 0 | memset(buf, 0, ATTR_MAX); |
278 | 0 | while (fgets(buf, ATTR_MAX, fp)) { |
279 | 0 | if (!strncmp(buf, "virtfs.uid", 10)) { |
280 | 0 | uid = atoi(buf + 11); |
281 | 0 | } else if (!strncmp(buf, "virtfs.gid", 10)) { |
282 | 0 | gid = atoi(buf + 11); |
283 | 0 | } else if (!strncmp(buf, "virtfs.mode", 11)) { |
284 | 0 | mode = atoi(buf + 12); |
285 | 0 | } else if (!strncmp(buf, "virtfs.rdev", 11)) { |
286 | 0 | rdev = atoi(buf + 12); |
287 | 0 | } |
288 | 0 | memset(buf, 0, ATTR_MAX); |
289 | 0 | } |
290 | 0 | fclose(fp); |
291 | |
|
292 | 0 | update_map_file: |
293 | 0 | if (is_root) { |
294 | 0 | fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w"); |
295 | 0 | } else { |
296 | 0 | fp = local_fopenat(map_dirfd, name, "w"); |
297 | | /* We can't go this far with map_dirfd not being a valid file descriptor |
298 | | * but some versions of gcc aren't smart enough to see it. |
299 | | */ |
300 | 0 | if (map_dirfd != -1) { |
301 | 0 | close_preserve_errno(map_dirfd); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | if (!fp) { |
305 | 0 | return -1; |
306 | 0 | } |
307 | | |
308 | 0 | map_fd = fileno(fp); |
309 | 0 | assert(map_fd != -1); |
310 | 0 | ret = fchmod(map_fd, 0600); |
311 | 0 | assert(ret == 0); |
312 | |
|
313 | 0 | if (credp->fc_uid != -1) { |
314 | 0 | uid = credp->fc_uid; |
315 | 0 | } |
316 | 0 | if (credp->fc_gid != -1) { |
317 | 0 | gid = credp->fc_gid; |
318 | 0 | } |
319 | 0 | if (credp->fc_mode != (mode_t)-1) { |
320 | 0 | mode = credp->fc_mode; |
321 | 0 | } |
322 | 0 | if (credp->fc_rdev != -1) { |
323 | 0 | rdev = credp->fc_rdev; |
324 | 0 | } |
325 | |
|
326 | 0 | if (uid != -1) { |
327 | 0 | fprintf(fp, "virtfs.uid=%d\n", uid); |
328 | 0 | } |
329 | 0 | if (gid != -1) { |
330 | 0 | fprintf(fp, "virtfs.gid=%d\n", gid); |
331 | 0 | } |
332 | 0 | if (mode != -1) { |
333 | 0 | fprintf(fp, "virtfs.mode=%d\n", mode); |
334 | 0 | } |
335 | 0 | if (rdev != -1) { |
336 | 0 | fprintf(fp, "virtfs.rdev=%d\n", rdev); |
337 | 0 | } |
338 | 0 | fclose(fp); |
339 | |
|
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | | static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode) |
344 | 0 | { |
345 | 0 | struct stat stbuf; |
346 | 0 | int fd, ret; |
347 | | |
348 | | /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW). |
349 | | * Unfortunately, the linux kernel doesn't implement it yet. |
350 | | */ |
351 | | |
352 | | /* First, we clear non-racing symlinks out of the way. */ |
353 | 0 | if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) { |
354 | 0 | return -1; |
355 | 0 | } |
356 | 0 | if (S_ISLNK(stbuf.st_mode)) { |
357 | 0 | errno = ELOOP; |
358 | 0 | return -1; |
359 | 0 | } |
360 | | |
361 | 0 | fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0); |
362 | | #if O_PATH_9P_UTIL == 0 |
363 | | /* Fallback for systems that don't support O_PATH: we depend on the file |
364 | | * being readable or writable. |
365 | | */ |
366 | | if (fd == -1) { |
367 | | /* In case the file is writable-only and isn't a directory. */ |
368 | | if (errno == EACCES) { |
369 | | fd = openat_file(dirfd, name, O_WRONLY, 0); |
370 | | } |
371 | | if (fd == -1 && errno == EISDIR) { |
372 | | errno = EACCES; |
373 | | } |
374 | | } |
375 | | if (fd == -1) { |
376 | | return -1; |
377 | | } |
378 | | ret = fchmod(fd, mode); |
379 | | #else |
380 | | /* Access modes are ignored when O_PATH is supported. If name is a symbolic |
381 | | * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor |
382 | | * referring to the symbolic link. |
383 | | */ |
384 | 0 | if (fd == -1) { |
385 | 0 | return -1; |
386 | 0 | } |
387 | | |
388 | | /* Now we handle racing symlinks. */ |
389 | 0 | ret = fstat(fd, &stbuf); |
390 | 0 | if (!ret) { |
391 | 0 | if (S_ISLNK(stbuf.st_mode)) { |
392 | 0 | errno = ELOOP; |
393 | 0 | ret = -1; |
394 | 0 | } else { |
395 | 0 | char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd); |
396 | 0 | ret = chmod(proc_path, mode); |
397 | 0 | g_free(proc_path); |
398 | 0 | } |
399 | 0 | } |
400 | 0 | #endif |
401 | 0 | close_preserve_errno(fd); |
402 | 0 | return ret; |
403 | 0 | } |
404 | | |
405 | | static int local_set_xattrat(int dirfd, const char *path, FsCred *credp) |
406 | 0 | { |
407 | 0 | int err; |
408 | |
|
409 | 0 | if (credp->fc_uid != -1) { |
410 | 0 | uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); |
411 | 0 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid, |
412 | 0 | sizeof(uid_t), 0); |
413 | 0 | if (err) { |
414 | 0 | return err; |
415 | 0 | } |
416 | 0 | } |
417 | 0 | if (credp->fc_gid != -1) { |
418 | 0 | uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); |
419 | 0 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid, |
420 | 0 | sizeof(gid_t), 0); |
421 | 0 | if (err) { |
422 | 0 | return err; |
423 | 0 | } |
424 | 0 | } |
425 | 0 | if (credp->fc_mode != (mode_t)-1) { |
426 | 0 | uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); |
427 | 0 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode, |
428 | 0 | sizeof(mode_t), 0); |
429 | 0 | if (err) { |
430 | 0 | return err; |
431 | 0 | } |
432 | 0 | } |
433 | 0 | if (credp->fc_rdev != -1) { |
434 | 0 | uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); |
435 | 0 | err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev, |
436 | 0 | sizeof(dev_t), 0); |
437 | 0 | if (err) { |
438 | 0 | return err; |
439 | 0 | } |
440 | 0 | } |
441 | 0 | return 0; |
442 | 0 | } |
443 | | |
444 | | static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd, |
445 | | const char *name, FsCred *credp) |
446 | 0 | { |
447 | 0 | if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
448 | 0 | AT_SYMLINK_NOFOLLOW) < 0) { |
449 | | /* |
450 | | * If we fail to change ownership and if we are |
451 | | * using security model none. Ignore the error |
452 | | */ |
453 | 0 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
454 | 0 | return -1; |
455 | 0 | } |
456 | 0 | } |
457 | | |
458 | 0 | return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777); |
459 | 0 | } |
460 | | |
461 | | static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, |
462 | | char *buf, size_t bufsz) |
463 | 0 | { |
464 | 0 | ssize_t tsize = -1; |
465 | |
|
466 | 0 | if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || |
467 | 0 | (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { |
468 | 0 | int fd; |
469 | |
|
470 | 0 | fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); |
471 | 0 | if (fd == -1) { |
472 | 0 | if (errno == ELOOP) { |
473 | 0 | goto native_symlink; |
474 | 0 | } |
475 | 0 | return -1; |
476 | 0 | } |
477 | 0 | tsize = RETRY_ON_EINTR(read(fd, (void *)buf, bufsz)); |
478 | 0 | close_preserve_errno(fd); |
479 | 0 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
480 | 0 | (fs_ctx->export_flags & V9FS_SM_NONE)) { |
481 | 0 | native_symlink:; |
482 | 0 | char *dirpath = g_path_get_dirname(fs_path->data); |
483 | 0 | char *name = g_path_get_basename(fs_path->data); |
484 | 0 | int dirfd; |
485 | |
|
486 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); |
487 | 0 | if (dirfd == -1) { |
488 | 0 | goto out; |
489 | 0 | } |
490 | | |
491 | 0 | tsize = readlinkat(dirfd, name, buf, bufsz); |
492 | 0 | close_preserve_errno(dirfd); |
493 | 0 | out: |
494 | 0 | g_free(name); |
495 | 0 | g_free(dirpath); |
496 | 0 | } |
497 | 0 | return tsize; |
498 | 0 | } |
499 | | |
500 | | static int local_close(FsContext *ctx, V9fsFidOpenState *fs) |
501 | 0 | { |
502 | 0 | return close(fs->fd); |
503 | 0 | } |
504 | | |
505 | | static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) |
506 | 0 | { |
507 | 0 | return closedir(fs->dir.stream); |
508 | 0 | } |
509 | | |
510 | | static int local_open(FsContext *ctx, V9fsPath *fs_path, |
511 | | int flags, V9fsFidOpenState *fs) |
512 | 0 | { |
513 | 0 | int fd; |
514 | |
|
515 | 0 | fd = local_open_nofollow(ctx, fs_path->data, flags, 0); |
516 | 0 | if (fd == -1) { |
517 | 0 | return -1; |
518 | 0 | } |
519 | 0 | fs->fd = fd; |
520 | 0 | return fs->fd; |
521 | 0 | } |
522 | | |
523 | | static int local_opendir(FsContext *ctx, |
524 | | V9fsPath *fs_path, V9fsFidOpenState *fs) |
525 | 0 | { |
526 | 0 | int dirfd; |
527 | 0 | DIR *stream; |
528 | |
|
529 | 0 | dirfd = local_opendir_nofollow(ctx, fs_path->data); |
530 | 0 | if (dirfd == -1) { |
531 | 0 | return -1; |
532 | 0 | } |
533 | | |
534 | 0 | stream = fdopendir(dirfd); |
535 | 0 | if (!stream) { |
536 | 0 | close(dirfd); |
537 | 0 | return -1; |
538 | 0 | } |
539 | 0 | fs->dir.stream = stream; |
540 | 0 | return 0; |
541 | 0 | } |
542 | | |
543 | | static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) |
544 | 0 | { |
545 | 0 | rewinddir(fs->dir.stream); |
546 | 0 | } |
547 | | |
548 | | static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) |
549 | 0 | { |
550 | 0 | return telldir(fs->dir.stream); |
551 | 0 | } |
552 | | |
553 | | static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name) |
554 | 0 | { |
555 | 0 | return |
556 | 0 | !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE); |
557 | 0 | } |
558 | | |
559 | | static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) |
560 | 0 | { |
561 | 0 | struct dirent *entry; |
562 | |
|
563 | 0 | again: |
564 | 0 | entry = readdir(fs->dir.stream); |
565 | 0 | if (!entry) { |
566 | 0 | return NULL; |
567 | 0 | } |
568 | | #ifdef CONFIG_DARWIN |
569 | | int off; |
570 | | off = telldir(fs->dir.stream); |
571 | | /* If telldir fails, fail the entire readdir call */ |
572 | | if (off < 0) { |
573 | | return NULL; |
574 | | } |
575 | | entry->d_seekoff = off; |
576 | | #endif |
577 | | |
578 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED) { |
579 | 0 | entry->d_type = DT_UNKNOWN; |
580 | 0 | } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
581 | 0 | if (local_is_mapped_file_metadata(ctx, entry->d_name)) { |
582 | | /* skip the meta data */ |
583 | 0 | goto again; |
584 | 0 | } |
585 | 0 | entry->d_type = DT_UNKNOWN; |
586 | 0 | } |
587 | | |
588 | 0 | return entry; |
589 | 0 | } |
590 | | |
591 | | static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) |
592 | 0 | { |
593 | 0 | seekdir(fs->dir.stream, off); |
594 | 0 | } |
595 | | |
596 | | static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, |
597 | | const struct iovec *iov, |
598 | | int iovcnt, off_t offset) |
599 | 0 | { |
600 | 0 | #ifdef CONFIG_PREADV |
601 | 0 | return preadv(fs->fd, iov, iovcnt, offset); |
602 | | #else |
603 | | int err = lseek(fs->fd, offset, SEEK_SET); |
604 | | if (err == -1) { |
605 | | return err; |
606 | | } else { |
607 | | return readv(fs->fd, iov, iovcnt); |
608 | | } |
609 | | #endif |
610 | 0 | } |
611 | | |
612 | | static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, |
613 | | const struct iovec *iov, |
614 | | int iovcnt, off_t offset) |
615 | 0 | { |
616 | 0 | ssize_t ret; |
617 | 0 | #ifdef CONFIG_PREADV |
618 | 0 | ret = pwritev(fs->fd, iov, iovcnt, offset); |
619 | | #else |
620 | | int err = lseek(fs->fd, offset, SEEK_SET); |
621 | | if (err == -1) { |
622 | | return err; |
623 | | } else { |
624 | | ret = writev(fs->fd, iov, iovcnt); |
625 | | } |
626 | | #endif |
627 | 0 | #ifdef CONFIG_SYNC_FILE_RANGE |
628 | 0 | if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { |
629 | | /* |
630 | | * Initiate a writeback. This is not a data integrity sync. |
631 | | * We want to ensure that we don't leave dirty pages in the cache |
632 | | * after write when writeout=immediate is specified. |
633 | | */ |
634 | 0 | sync_file_range(fs->fd, offset, ret, |
635 | 0 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); |
636 | 0 | } |
637 | 0 | #endif |
638 | 0 | return ret; |
639 | 0 | } |
640 | | |
641 | | static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
642 | 0 | { |
643 | 0 | char *dirpath = g_path_get_dirname(fs_path->data); |
644 | 0 | char *name = g_path_get_basename(fs_path->data); |
645 | 0 | int ret = -1; |
646 | 0 | int dirfd; |
647 | |
|
648 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); |
649 | 0 | if (dirfd == -1) { |
650 | 0 | goto out; |
651 | 0 | } |
652 | | |
653 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
654 | 0 | ret = local_set_xattrat(dirfd, name, credp); |
655 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
656 | 0 | ret = local_set_mapped_file_attrat(dirfd, name, credp); |
657 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || |
658 | 0 | fs_ctx->export_flags & V9FS_SM_NONE) { |
659 | 0 | ret = fchmodat_nofollow(dirfd, name, credp->fc_mode); |
660 | 0 | } |
661 | 0 | close_preserve_errno(dirfd); |
662 | |
|
663 | 0 | out: |
664 | 0 | g_free(dirpath); |
665 | 0 | g_free(name); |
666 | 0 | return ret; |
667 | 0 | } |
668 | | |
669 | | static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, |
670 | | const char *name, FsCred *credp) |
671 | 0 | { |
672 | 0 | int err = -1; |
673 | 0 | int dirfd; |
674 | |
|
675 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && |
676 | 0 | local_is_mapped_file_metadata(fs_ctx, name)) { |
677 | 0 | errno = EINVAL; |
678 | 0 | return -1; |
679 | 0 | } |
680 | | |
681 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
682 | 0 | if (dirfd == -1) { |
683 | 0 | return -1; |
684 | 0 | } |
685 | | |
686 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED || |
687 | 0 | fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
688 | 0 | err = qemu_mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0); |
689 | 0 | if (err == -1) { |
690 | 0 | goto out; |
691 | 0 | } |
692 | | |
693 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
694 | 0 | err = local_set_xattrat(dirfd, name, credp); |
695 | 0 | } else { |
696 | 0 | err = local_set_mapped_file_attrat(dirfd, name, credp); |
697 | 0 | } |
698 | 0 | if (err == -1) { |
699 | 0 | goto err_end; |
700 | 0 | } |
701 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || |
702 | 0 | fs_ctx->export_flags & V9FS_SM_NONE) { |
703 | 0 | err = qemu_mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev); |
704 | 0 | if (err == -1) { |
705 | 0 | goto out; |
706 | 0 | } |
707 | 0 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
708 | 0 | if (err == -1) { |
709 | 0 | goto err_end; |
710 | 0 | } |
711 | 0 | } |
712 | 0 | goto out; |
713 | | |
714 | 0 | err_end: |
715 | 0 | unlinkat_preserve_errno(dirfd, name, 0); |
716 | 0 | out: |
717 | 0 | close_preserve_errno(dirfd); |
718 | 0 | return err; |
719 | 0 | } |
720 | | |
721 | | static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, |
722 | | const char *name, FsCred *credp) |
723 | 0 | { |
724 | 0 | int err = -1; |
725 | 0 | int dirfd; |
726 | |
|
727 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && |
728 | 0 | local_is_mapped_file_metadata(fs_ctx, name)) { |
729 | 0 | errno = EINVAL; |
730 | 0 | return -1; |
731 | 0 | } |
732 | | |
733 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
734 | 0 | if (dirfd == -1) { |
735 | 0 | return -1; |
736 | 0 | } |
737 | | |
738 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED || |
739 | 0 | fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
740 | 0 | err = qemu_mkdirat(dirfd, name, fs_ctx->dmode); |
741 | 0 | if (err == -1) { |
742 | 0 | goto out; |
743 | 0 | } |
744 | 0 | credp->fc_mode = credp->fc_mode | S_IFDIR; |
745 | |
|
746 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
747 | 0 | err = local_set_xattrat(dirfd, name, credp); |
748 | 0 | } else { |
749 | 0 | err = local_set_mapped_file_attrat(dirfd, name, credp); |
750 | 0 | } |
751 | 0 | if (err == -1) { |
752 | 0 | goto err_end; |
753 | 0 | } |
754 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || |
755 | 0 | fs_ctx->export_flags & V9FS_SM_NONE) { |
756 | 0 | err = qemu_mkdirat(dirfd, name, credp->fc_mode); |
757 | 0 | if (err == -1) { |
758 | 0 | goto out; |
759 | 0 | } |
760 | 0 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
761 | 0 | if (err == -1) { |
762 | 0 | goto err_end; |
763 | 0 | } |
764 | 0 | } |
765 | 0 | goto out; |
766 | | |
767 | 0 | err_end: |
768 | 0 | unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR); |
769 | 0 | out: |
770 | 0 | close_preserve_errno(dirfd); |
771 | 0 | return err; |
772 | 0 | } |
773 | | |
774 | | static int local_fid_fd(int fid_type, V9fsFidOpenState *fs) |
775 | 0 | { |
776 | 0 | if (fid_type == P9_FID_DIR) { |
777 | 0 | return dirfd(fs->dir.stream); |
778 | 0 | } else if (fid_type == P9_FID_FILE) { |
779 | 0 | return fs->fd; |
780 | 0 | } else { |
781 | 0 | errno = EBADF; |
782 | 0 | return -1; |
783 | 0 | } |
784 | 0 | } |
785 | | |
786 | | static int local_fstat(FsContext *fs_ctx, int fid_type, |
787 | | V9fsFidOpenState *fs, struct stat *stbuf) |
788 | 0 | { |
789 | 0 | int err, fd = local_fid_fd(fid_type, fs); |
790 | |
|
791 | 0 | err = fstat(fd, stbuf); |
792 | 0 | if (err) { |
793 | 0 | return err; |
794 | 0 | } |
795 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
796 | | /* Actual credentials are part of extended attrs */ |
797 | 0 | uid_t tmp_uid; |
798 | 0 | gid_t tmp_gid; |
799 | 0 | mode_t tmp_mode; |
800 | 0 | dev_t tmp_dev; |
801 | |
|
802 | 0 | if (qemu_fgetxattr(fd, "user.virtfs.uid", |
803 | 0 | &tmp_uid, sizeof(uid_t)) > 0) { |
804 | 0 | stbuf->st_uid = le32_to_cpu(tmp_uid); |
805 | 0 | } |
806 | 0 | if (qemu_fgetxattr(fd, "user.virtfs.gid", |
807 | 0 | &tmp_gid, sizeof(gid_t)) > 0) { |
808 | 0 | stbuf->st_gid = le32_to_cpu(tmp_gid); |
809 | 0 | } |
810 | 0 | if (qemu_fgetxattr(fd, "user.virtfs.mode", |
811 | 0 | &tmp_mode, sizeof(mode_t)) > 0) { |
812 | 0 | stbuf->st_mode = le32_to_cpu(tmp_mode); |
813 | 0 | } |
814 | 0 | if (qemu_fgetxattr(fd, "user.virtfs.rdev", |
815 | 0 | &tmp_dev, sizeof(dev_t)) > 0) { |
816 | 0 | stbuf->st_rdev = le64_to_cpu(tmp_dev); |
817 | 0 | } |
818 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
819 | 0 | errno = EOPNOTSUPP; |
820 | 0 | return -1; |
821 | 0 | } |
822 | 0 | return err; |
823 | 0 | } |
824 | | |
825 | | static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, |
826 | | int flags, FsCred *credp, V9fsFidOpenState *fs) |
827 | 0 | { |
828 | 0 | int fd = -1; |
829 | 0 | int err = -1; |
830 | 0 | int dirfd; |
831 | |
|
832 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && |
833 | 0 | local_is_mapped_file_metadata(fs_ctx, name)) { |
834 | 0 | errno = EINVAL; |
835 | 0 | return -1; |
836 | 0 | } |
837 | | |
838 | | /* |
839 | | * Mark all the open to not follow symlinks |
840 | | */ |
841 | 0 | flags |= O_NOFOLLOW; |
842 | |
|
843 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
844 | 0 | if (dirfd == -1) { |
845 | 0 | return -1; |
846 | 0 | } |
847 | | |
848 | | /* Determine the security model */ |
849 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED || |
850 | 0 | fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
851 | 0 | fd = openat_file(dirfd, name, flags, fs_ctx->fmode); |
852 | 0 | if (fd == -1) { |
853 | 0 | goto out; |
854 | 0 | } |
855 | 0 | credp->fc_mode = credp->fc_mode | S_IFREG; |
856 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
857 | | /* Set client credentials in xattr */ |
858 | 0 | err = local_set_xattrat(dirfd, name, credp); |
859 | 0 | } else { |
860 | 0 | err = local_set_mapped_file_attrat(dirfd, name, credp); |
861 | 0 | } |
862 | 0 | if (err == -1) { |
863 | 0 | goto err_end; |
864 | 0 | } |
865 | 0 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
866 | 0 | (fs_ctx->export_flags & V9FS_SM_NONE)) { |
867 | 0 | fd = openat_file(dirfd, name, flags, credp->fc_mode); |
868 | 0 | if (fd == -1) { |
869 | 0 | goto out; |
870 | 0 | } |
871 | 0 | err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp); |
872 | 0 | if (err == -1) { |
873 | 0 | goto err_end; |
874 | 0 | } |
875 | 0 | } |
876 | 0 | err = fd; |
877 | 0 | fs->fd = fd; |
878 | 0 | goto out; |
879 | | |
880 | 0 | err_end: |
881 | 0 | unlinkat_preserve_errno(dirfd, name, |
882 | 0 | flags & O_DIRECTORY ? AT_REMOVEDIR : 0); |
883 | 0 | close_preserve_errno(fd); |
884 | 0 | out: |
885 | 0 | close_preserve_errno(dirfd); |
886 | 0 | return err; |
887 | 0 | } |
888 | | |
889 | | |
890 | | static int local_symlink(FsContext *fs_ctx, const char *oldpath, |
891 | | V9fsPath *dir_path, const char *name, FsCred *credp) |
892 | 0 | { |
893 | 0 | int err = -1; |
894 | 0 | int dirfd; |
895 | |
|
896 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE && |
897 | 0 | local_is_mapped_file_metadata(fs_ctx, name)) { |
898 | 0 | errno = EINVAL; |
899 | 0 | return -1; |
900 | 0 | } |
901 | | |
902 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dir_path->data); |
903 | 0 | if (dirfd == -1) { |
904 | 0 | return -1; |
905 | 0 | } |
906 | | |
907 | | /* Determine the security model */ |
908 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED || |
909 | 0 | fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
910 | 0 | int fd; |
911 | 0 | ssize_t oldpath_size, write_size; |
912 | |
|
913 | 0 | fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR, |
914 | 0 | fs_ctx->fmode); |
915 | 0 | if (fd == -1) { |
916 | 0 | goto out; |
917 | 0 | } |
918 | | /* Write the oldpath (target) to the file. */ |
919 | 0 | oldpath_size = strlen(oldpath); |
920 | 0 | write_size = RETRY_ON_EINTR(write(fd, (void *)oldpath, oldpath_size)); |
921 | 0 | close_preserve_errno(fd); |
922 | |
|
923 | 0 | if (write_size != oldpath_size) { |
924 | 0 | goto err_end; |
925 | 0 | } |
926 | | /* Set client credentials in symlink's xattr */ |
927 | 0 | credp->fc_mode = credp->fc_mode | S_IFLNK; |
928 | |
|
929 | 0 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
930 | 0 | err = local_set_xattrat(dirfd, name, credp); |
931 | 0 | } else { |
932 | 0 | err = local_set_mapped_file_attrat(dirfd, name, credp); |
933 | 0 | } |
934 | 0 | if (err == -1) { |
935 | 0 | goto err_end; |
936 | 0 | } |
937 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH || |
938 | 0 | fs_ctx->export_flags & V9FS_SM_NONE) { |
939 | 0 | err = symlinkat(oldpath, dirfd, name); |
940 | 0 | if (err) { |
941 | 0 | goto out; |
942 | 0 | } |
943 | 0 | err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
944 | 0 | AT_SYMLINK_NOFOLLOW); |
945 | 0 | if (err == -1) { |
946 | | /* |
947 | | * If we fail to change ownership and if we are |
948 | | * using security model none. Ignore the error |
949 | | */ |
950 | 0 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
951 | 0 | goto err_end; |
952 | 0 | } else { |
953 | 0 | err = 0; |
954 | 0 | } |
955 | 0 | } |
956 | 0 | } |
957 | 0 | goto out; |
958 | | |
959 | 0 | err_end: |
960 | 0 | unlinkat_preserve_errno(dirfd, name, 0); |
961 | 0 | out: |
962 | 0 | close_preserve_errno(dirfd); |
963 | 0 | return err; |
964 | 0 | } |
965 | | |
966 | | static int local_link(FsContext *ctx, V9fsPath *oldpath, |
967 | | V9fsPath *dirpath, const char *name) |
968 | 0 | { |
969 | 0 | char *odirpath = g_path_get_dirname(oldpath->data); |
970 | 0 | char *oname = g_path_get_basename(oldpath->data); |
971 | 0 | int ret = -1; |
972 | 0 | int odirfd, ndirfd; |
973 | |
|
974 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE && |
975 | 0 | local_is_mapped_file_metadata(ctx, name)) { |
976 | 0 | errno = EINVAL; |
977 | 0 | goto out; |
978 | 0 | } |
979 | | |
980 | 0 | odirfd = local_opendir_nofollow(ctx, odirpath); |
981 | 0 | if (odirfd == -1) { |
982 | 0 | goto out; |
983 | 0 | } |
984 | | |
985 | 0 | ndirfd = local_opendir_nofollow(ctx, dirpath->data); |
986 | 0 | if (ndirfd == -1) { |
987 | 0 | close_preserve_errno(odirfd); |
988 | 0 | goto out; |
989 | 0 | } |
990 | | |
991 | 0 | ret = linkat(odirfd, oname, ndirfd, name, 0); |
992 | 0 | if (ret < 0) { |
993 | 0 | goto out_close; |
994 | 0 | } |
995 | | |
996 | | /* now link the virtfs_metadata files */ |
997 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
998 | 0 | int omap_dirfd, nmap_dirfd; |
999 | |
|
1000 | 0 | ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700); |
1001 | 0 | if (ret < 0 && errno != EEXIST) { |
1002 | 0 | goto err_undo_link; |
1003 | 0 | } |
1004 | | |
1005 | 0 | omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); |
1006 | 0 | if (omap_dirfd == -1) { |
1007 | 0 | goto err; |
1008 | 0 | } |
1009 | | |
1010 | 0 | nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); |
1011 | 0 | if (nmap_dirfd == -1) { |
1012 | 0 | close_preserve_errno(omap_dirfd); |
1013 | 0 | goto err; |
1014 | 0 | } |
1015 | | |
1016 | 0 | ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0); |
1017 | 0 | close_preserve_errno(nmap_dirfd); |
1018 | 0 | close_preserve_errno(omap_dirfd); |
1019 | 0 | if (ret < 0 && errno != ENOENT) { |
1020 | 0 | goto err_undo_link; |
1021 | 0 | } |
1022 | | |
1023 | 0 | ret = 0; |
1024 | 0 | } |
1025 | 0 | goto out_close; |
1026 | | |
1027 | 0 | err: |
1028 | 0 | ret = -1; |
1029 | 0 | err_undo_link: |
1030 | 0 | unlinkat_preserve_errno(ndirfd, name, 0); |
1031 | 0 | out_close: |
1032 | 0 | close_preserve_errno(ndirfd); |
1033 | 0 | close_preserve_errno(odirfd); |
1034 | 0 | out: |
1035 | 0 | g_free(oname); |
1036 | 0 | g_free(odirpath); |
1037 | 0 | return ret; |
1038 | 0 | } |
1039 | | |
1040 | | static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) |
1041 | 0 | { |
1042 | 0 | int fd, ret; |
1043 | |
|
1044 | 0 | fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); |
1045 | 0 | if (fd == -1) { |
1046 | 0 | return -1; |
1047 | 0 | } |
1048 | 0 | ret = ftruncate(fd, size); |
1049 | 0 | close_preserve_errno(fd); |
1050 | 0 | return ret; |
1051 | 0 | } |
1052 | | |
1053 | | static int local_ftruncate(FsContext *ctx, int fid_type, V9fsFidOpenState *fs, |
1054 | | off_t size) |
1055 | 0 | { |
1056 | 0 | int fd = local_fid_fd(fid_type, fs); |
1057 | |
|
1058 | 0 | return ftruncate(fd, size); |
1059 | 0 | } |
1060 | | |
1061 | | static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
1062 | 0 | { |
1063 | 0 | char *dirpath = g_path_get_dirname(fs_path->data); |
1064 | 0 | char *name = g_path_get_basename(fs_path->data); |
1065 | 0 | int ret = -1; |
1066 | 0 | int dirfd; |
1067 | |
|
1068 | 0 | dirfd = local_opendir_nofollow(fs_ctx, dirpath); |
1069 | 0 | if (dirfd == -1) { |
1070 | 0 | goto out; |
1071 | 0 | } |
1072 | | |
1073 | 0 | if ((credp->fc_uid == -1 && credp->fc_gid == -1) || |
1074 | 0 | (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
1075 | 0 | (fs_ctx->export_flags & V9FS_SM_NONE)) { |
1076 | 0 | ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid, |
1077 | 0 | AT_SYMLINK_NOFOLLOW); |
1078 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
1079 | 0 | ret = local_set_xattrat(dirfd, name, credp); |
1080 | 0 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
1081 | 0 | ret = local_set_mapped_file_attrat(dirfd, name, credp); |
1082 | 0 | } |
1083 | |
|
1084 | 0 | close_preserve_errno(dirfd); |
1085 | 0 | out: |
1086 | 0 | g_free(name); |
1087 | 0 | g_free(dirpath); |
1088 | 0 | return ret; |
1089 | 0 | } |
1090 | | |
1091 | | static int local_utimensat(FsContext *s, V9fsPath *fs_path, |
1092 | | const struct timespec *buf) |
1093 | 0 | { |
1094 | 0 | char *dirpath = g_path_get_dirname(fs_path->data); |
1095 | 0 | char *name = g_path_get_basename(fs_path->data); |
1096 | 0 | int dirfd, ret = -1; |
1097 | |
|
1098 | 0 | dirfd = local_opendir_nofollow(s, dirpath); |
1099 | 0 | if (dirfd == -1) { |
1100 | 0 | goto out; |
1101 | 0 | } |
1102 | | |
1103 | 0 | ret = qemu_utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); |
1104 | 0 | close_preserve_errno(dirfd); |
1105 | 0 | out: |
1106 | 0 | g_free(dirpath); |
1107 | 0 | g_free(name); |
1108 | 0 | return ret; |
1109 | 0 | } |
1110 | | |
1111 | | static int local_futimens(FsContext *s, int fid_type, V9fsFidOpenState *fs, |
1112 | | const struct timespec *times) |
1113 | 0 | { |
1114 | 0 | int fd = local_fid_fd(fid_type, fs); |
1115 | |
|
1116 | 0 | return qemu_futimens(fd, times); |
1117 | 0 | } |
1118 | | |
1119 | | static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, |
1120 | | int flags) |
1121 | 0 | { |
1122 | 0 | int ret; |
1123 | |
|
1124 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
1125 | 0 | int map_dirfd; |
1126 | | |
1127 | | /* We need to remove the metadata as well: |
1128 | | * - the metadata directory if we're removing a directory |
1129 | | * - the metadata file in the parent's metadata directory |
1130 | | * |
1131 | | * If any of these are missing (ie, ENOENT) then we're probably |
1132 | | * trying to remove something that wasn't created in mapped-file |
1133 | | * mode. We just ignore the error. |
1134 | | */ |
1135 | 0 | if (flags == AT_REMOVEDIR) { |
1136 | 0 | int fd; |
1137 | |
|
1138 | 0 | fd = openat_dir(dirfd, name); |
1139 | 0 | if (fd == -1) { |
1140 | 0 | return -1; |
1141 | 0 | } |
1142 | 0 | ret = qemu_unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); |
1143 | 0 | close_preserve_errno(fd); |
1144 | 0 | if (ret < 0 && errno != ENOENT) { |
1145 | 0 | return -1; |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); |
1149 | 0 | if (map_dirfd != -1) { |
1150 | 0 | ret = qemu_unlinkat(map_dirfd, name, 0); |
1151 | 0 | close_preserve_errno(map_dirfd); |
1152 | 0 | if (ret < 0 && errno != ENOENT) { |
1153 | 0 | return -1; |
1154 | 0 | } |
1155 | 0 | } else if (errno != ENOENT) { |
1156 | 0 | return -1; |
1157 | 0 | } |
1158 | 0 | } |
1159 | | |
1160 | 0 | return qemu_unlinkat(dirfd, name, flags); |
1161 | 0 | } |
1162 | | |
1163 | | static int local_remove(FsContext *ctx, const char *path) |
1164 | 0 | { |
1165 | 0 | struct stat stbuf; |
1166 | 0 | char *dirpath = g_path_get_dirname(path); |
1167 | 0 | char *name = g_path_get_basename(path); |
1168 | 0 | int flags = 0; |
1169 | 0 | int dirfd; |
1170 | 0 | int err = -1; |
1171 | |
|
1172 | 0 | dirfd = local_opendir_nofollow(ctx, dirpath); |
1173 | 0 | if (dirfd == -1) { |
1174 | 0 | goto out; |
1175 | 0 | } |
1176 | | |
1177 | 0 | if (qemu_fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { |
1178 | 0 | goto err_out; |
1179 | 0 | } |
1180 | | |
1181 | 0 | if (S_ISDIR(stbuf.st_mode)) { |
1182 | 0 | flags |= AT_REMOVEDIR; |
1183 | 0 | } |
1184 | |
|
1185 | 0 | err = local_unlinkat_common(ctx, dirfd, name, flags); |
1186 | 0 | err_out: |
1187 | 0 | close_preserve_errno(dirfd); |
1188 | 0 | out: |
1189 | 0 | g_free(name); |
1190 | 0 | g_free(dirpath); |
1191 | 0 | return err; |
1192 | 0 | } |
1193 | | |
1194 | | static int local_fsync(FsContext *ctx, int fid_type, |
1195 | | V9fsFidOpenState *fs, int datasync) |
1196 | 0 | { |
1197 | 0 | int fd = local_fid_fd(fid_type, fs); |
1198 | |
|
1199 | 0 | if (datasync) { |
1200 | 0 | return qemu_fdatasync(fd); |
1201 | 0 | } else { |
1202 | 0 | return fsync(fd); |
1203 | 0 | } |
1204 | 0 | } |
1205 | | |
1206 | | static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) |
1207 | 0 | { |
1208 | 0 | int fd, ret; |
1209 | |
|
1210 | 0 | fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); |
1211 | 0 | if (fd == -1) { |
1212 | 0 | return -1; |
1213 | 0 | } |
1214 | 0 | ret = fstatfs(fd, stbuf); |
1215 | 0 | close_preserve_errno(fd); |
1216 | 0 | return ret; |
1217 | 0 | } |
1218 | | |
1219 | | static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, |
1220 | | const char *name, void *value, size_t size) |
1221 | 0 | { |
1222 | 0 | char *path = fs_path->data; |
1223 | |
|
1224 | 0 | return v9fs_get_xattr(ctx, path, name, value, size); |
1225 | 0 | } |
1226 | | |
1227 | | static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, |
1228 | | void *value, size_t size) |
1229 | 0 | { |
1230 | 0 | char *path = fs_path->data; |
1231 | |
|
1232 | 0 | return v9fs_list_xattr(ctx, path, value, size); |
1233 | 0 | } |
1234 | | |
1235 | | static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, |
1236 | | void *value, size_t size, int flags) |
1237 | 0 | { |
1238 | 0 | char *path = fs_path->data; |
1239 | |
|
1240 | 0 | return v9fs_set_xattr(ctx, path, name, value, size, flags); |
1241 | 0 | } |
1242 | | |
1243 | | static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, |
1244 | | const char *name) |
1245 | 0 | { |
1246 | 0 | char *path = fs_path->data; |
1247 | |
|
1248 | 0 | return v9fs_remove_xattr(ctx, path, name); |
1249 | 0 | } |
1250 | | |
1251 | | static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, |
1252 | | const char *name, V9fsPath *target) |
1253 | 9.42k | { |
1254 | 9.42k | if (ctx->export_flags & V9FS_SM_MAPPED_FILE && |
1255 | 0 | local_is_mapped_file_metadata(ctx, name)) { |
1256 | 0 | errno = EINVAL; |
1257 | 0 | return -1; |
1258 | 0 | } |
1259 | | |
1260 | 9.42k | if (dir_path) { |
1261 | 0 | if (!strcmp(name, ".")) { |
1262 | | /* "." relative to "foo/bar" is "foo/bar" */ |
1263 | 0 | v9fs_path_copy(target, dir_path); |
1264 | 0 | } else if (!strcmp(name, "..")) { |
1265 | 0 | if (!strcmp(dir_path->data, ".")) { |
1266 | | /* ".." relative to the root is "." */ |
1267 | 0 | if (v9fs_path_sprintf(target, ".") < 0) { |
1268 | 0 | return -1; |
1269 | 0 | } |
1270 | 0 | } else { |
1271 | 0 | g_autofree char *tmp = g_path_get_dirname(dir_path->data); |
1272 | | /* Symbolic links are resolved by the client. We can assume |
1273 | | * that ".." relative to "foo/bar" is equivalent to "foo" |
1274 | | */ |
1275 | 0 | if (v9fs_path_sprintf(target, "%s", tmp) < 0) { |
1276 | 0 | return -1; |
1277 | 0 | } |
1278 | 0 | } |
1279 | 0 | } else { |
1280 | 0 | assert(!strchr(name, '/')); |
1281 | 0 | if (v9fs_path_sprintf(target, "%s/%s", dir_path->data, name) < 0) { |
1282 | 0 | return -1; |
1283 | 0 | } |
1284 | 0 | } |
1285 | 9.42k | } else if (!strcmp(name, "/") || !strcmp(name, ".") || |
1286 | 9.42k | !strcmp(name, "..")) { |
1287 | | /* This is the root fid */ |
1288 | 9.42k | if (v9fs_path_sprintf(target, ".") < 0) { |
1289 | 0 | return -1; |
1290 | 0 | } |
1291 | 9.42k | } else { |
1292 | 0 | assert(!strchr(name, '/')); |
1293 | 0 | if (v9fs_path_sprintf(target, "./%s", name) < 0) { |
1294 | 0 | return -1; |
1295 | 0 | } |
1296 | 0 | } |
1297 | 9.42k | return 0; |
1298 | 9.42k | } |
1299 | | |
1300 | | static int local_renameat(FsContext *ctx, V9fsPath *olddir, |
1301 | | const char *old_name, V9fsPath *newdir, |
1302 | | const char *new_name) |
1303 | 0 | { |
1304 | 0 | int ret; |
1305 | 0 | int odirfd, ndirfd; |
1306 | |
|
1307 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE && |
1308 | 0 | (local_is_mapped_file_metadata(ctx, old_name) || |
1309 | 0 | local_is_mapped_file_metadata(ctx, new_name))) { |
1310 | 0 | errno = EINVAL; |
1311 | 0 | return -1; |
1312 | 0 | } |
1313 | | |
1314 | 0 | odirfd = local_opendir_nofollow(ctx, olddir->data); |
1315 | 0 | if (odirfd == -1) { |
1316 | 0 | return -1; |
1317 | 0 | } |
1318 | | |
1319 | 0 | ndirfd = local_opendir_nofollow(ctx, newdir->data); |
1320 | 0 | if (ndirfd == -1) { |
1321 | 0 | close_preserve_errno(odirfd); |
1322 | 0 | return -1; |
1323 | 0 | } |
1324 | | |
1325 | 0 | ret = qemu_renameat(odirfd, old_name, ndirfd, new_name); |
1326 | 0 | if (ret < 0) { |
1327 | 0 | goto out; |
1328 | 0 | } |
1329 | | |
1330 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
1331 | 0 | int omap_dirfd, nmap_dirfd; |
1332 | |
|
1333 | 0 | ret = qemu_mkdirat(ndirfd, VIRTFS_META_DIR, 0700); |
1334 | 0 | if (ret < 0 && errno != EEXIST) { |
1335 | 0 | goto err_undo_rename; |
1336 | 0 | } |
1337 | | |
1338 | 0 | omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR); |
1339 | 0 | if (omap_dirfd == -1) { |
1340 | 0 | goto err; |
1341 | 0 | } |
1342 | | |
1343 | 0 | nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR); |
1344 | 0 | if (nmap_dirfd == -1) { |
1345 | 0 | close_preserve_errno(omap_dirfd); |
1346 | 0 | goto err; |
1347 | 0 | } |
1348 | | |
1349 | | /* rename the .virtfs_metadata files */ |
1350 | 0 | ret = qemu_renameat(omap_dirfd, old_name, nmap_dirfd, new_name); |
1351 | 0 | close_preserve_errno(nmap_dirfd); |
1352 | 0 | close_preserve_errno(omap_dirfd); |
1353 | 0 | if (ret < 0 && errno != ENOENT) { |
1354 | 0 | goto err_undo_rename; |
1355 | 0 | } |
1356 | | |
1357 | 0 | ret = 0; |
1358 | 0 | } |
1359 | 0 | goto out; |
1360 | | |
1361 | 0 | err: |
1362 | 0 | ret = -1; |
1363 | 0 | err_undo_rename: |
1364 | 0 | renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); |
1365 | 0 | out: |
1366 | 0 | close_preserve_errno(ndirfd); |
1367 | 0 | close_preserve_errno(odirfd); |
1368 | 0 | return ret; |
1369 | 0 | } |
1370 | | |
1371 | | static void v9fs_path_init_dirname(V9fsPath *path, const char *str) |
1372 | 0 | { |
1373 | 0 | path->data = g_path_get_dirname(str); |
1374 | 0 | path->size = strlen(path->data) + 1; |
1375 | 0 | } |
1376 | | |
1377 | | static int local_rename(FsContext *ctx, const char *oldpath, |
1378 | | const char *newpath) |
1379 | 0 | { |
1380 | 0 | int err; |
1381 | 0 | char *oname = g_path_get_basename(oldpath); |
1382 | 0 | char *nname = g_path_get_basename(newpath); |
1383 | 0 | V9fsPath olddir, newdir; |
1384 | |
|
1385 | 0 | v9fs_path_init_dirname(&olddir, oldpath); |
1386 | 0 | v9fs_path_init_dirname(&newdir, newpath); |
1387 | |
|
1388 | 0 | err = local_renameat(ctx, &olddir, oname, &newdir, nname); |
1389 | |
|
1390 | 0 | v9fs_path_free(&newdir); |
1391 | 0 | v9fs_path_free(&olddir); |
1392 | 0 | g_free(nname); |
1393 | 0 | g_free(oname); |
1394 | |
|
1395 | 0 | return err; |
1396 | 0 | } |
1397 | | |
1398 | | static int local_unlinkat(FsContext *ctx, V9fsPath *dir, |
1399 | | const char *name, int flags) |
1400 | 0 | { |
1401 | 0 | int ret; |
1402 | 0 | int dirfd; |
1403 | |
|
1404 | 0 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE && |
1405 | 0 | local_is_mapped_file_metadata(ctx, name)) { |
1406 | 0 | errno = EINVAL; |
1407 | 0 | return -1; |
1408 | 0 | } |
1409 | | |
1410 | 0 | dirfd = local_opendir_nofollow(ctx, dir->data); |
1411 | 0 | if (dirfd == -1) { |
1412 | 0 | return -1; |
1413 | 0 | } |
1414 | | |
1415 | 0 | ret = local_unlinkat_common(ctx, dirfd, name, flags); |
1416 | 0 | close_preserve_errno(dirfd); |
1417 | 0 | return ret; |
1418 | 0 | } |
1419 | | |
1420 | | #ifdef FS_IOC_GETVERSION |
1421 | | static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, |
1422 | | mode_t st_mode, uint64_t *st_gen) |
1423 | 0 | { |
1424 | 0 | int err; |
1425 | 0 | V9fsFidOpenState fid_open; |
1426 | | |
1427 | | /* |
1428 | | * Do not try to open special files like device nodes, fifos etc |
1429 | | * We can get fd for regular files and directories only |
1430 | | */ |
1431 | 0 | if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { |
1432 | 0 | errno = ENOTTY; |
1433 | 0 | return -1; |
1434 | 0 | } |
1435 | 0 | err = local_open(ctx, path, O_RDONLY, &fid_open); |
1436 | 0 | if (err < 0) { |
1437 | 0 | return err; |
1438 | 0 | } |
1439 | 0 | err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); |
1440 | 0 | local_close(ctx, &fid_open); |
1441 | 0 | return err; |
1442 | 0 | } |
1443 | | #endif |
1444 | | |
1445 | | static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp) |
1446 | 2 | { |
1447 | 2 | #ifdef FS_IOC_GETVERSION |
1448 | 2 | struct statfs stbuf; |
1449 | | |
1450 | | /* |
1451 | | * use ioc_getversion only if the ioctl is defined |
1452 | | */ |
1453 | 2 | if (fstatfs(data->mountfd, &stbuf) < 0) { |
1454 | 0 | error_setg_errno(errp, errno, |
1455 | 0 | "failed to stat file system at '%s'", ctx->fs_root); |
1456 | 0 | return -1; |
1457 | 0 | } |
1458 | 2 | switch (stbuf.f_type) { |
1459 | 0 | case EXT2_SUPER_MAGIC: |
1460 | 0 | case BTRFS_SUPER_MAGIC: |
1461 | 0 | case REISERFS_SUPER_MAGIC: |
1462 | 0 | case XFS_SUPER_MAGIC: |
1463 | 0 | ctx->exops.get_st_gen = local_ioc_getversion; |
1464 | 0 | break; |
1465 | 2 | } |
1466 | 2 | #endif |
1467 | 2 | return 0; |
1468 | 2 | } |
1469 | | |
1470 | | static int local_init(FsContext *ctx, Error **errp) |
1471 | 2 | { |
1472 | 2 | LocalData *data = g_malloc(sizeof(*data)); |
1473 | | |
1474 | 2 | data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); |
1475 | 2 | if (data->mountfd == -1) { |
1476 | 0 | error_setg_file_open(errp, errno, ctx->fs_root); |
1477 | 0 | goto err; |
1478 | 0 | } |
1479 | | |
1480 | 2 | if (local_ioc_getversion_init(ctx, data, errp) < 0) { |
1481 | 0 | close(data->mountfd); |
1482 | 0 | goto err; |
1483 | 0 | } |
1484 | | |
1485 | 2 | if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { |
1486 | 0 | ctx->xops = passthrough_xattr_ops; |
1487 | 2 | } else if (ctx->export_flags & V9FS_SM_MAPPED) { |
1488 | 2 | ctx->xops = mapped_xattr_ops; |
1489 | 2 | } else if (ctx->export_flags & V9FS_SM_NONE) { |
1490 | 0 | ctx->xops = none_xattr_ops; |
1491 | 0 | } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
1492 | | /* |
1493 | | * xattr operation for mapped-file and passthrough |
1494 | | * remain same. |
1495 | | */ |
1496 | 0 | ctx->xops = passthrough_xattr_ops; |
1497 | 0 | } |
1498 | 2 | ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; |
1499 | | |
1500 | 2 | ctx->private = data; |
1501 | 2 | return 0; |
1502 | | |
1503 | 0 | err: |
1504 | 0 | g_free(data); |
1505 | 0 | return -1; |
1506 | 2 | } |
1507 | | |
1508 | | static void local_cleanup(FsContext *ctx) |
1509 | 0 | { |
1510 | 0 | LocalData *data = ctx->private; |
1511 | |
|
1512 | 0 | if (!data) { |
1513 | 0 | return; |
1514 | 0 | } |
1515 | | |
1516 | 0 | close(data->mountfd); |
1517 | 0 | g_free(data); |
1518 | 0 | } |
1519 | | |
1520 | | static void error_append_security_model_hint(Error *const *errp) |
1521 | 0 | { |
1522 | 0 | error_append_hint(errp, "Valid options are: security_model=" |
1523 | 0 | "[passthrough|mapped-xattr|mapped-file|none]\n"); |
1524 | 0 | } |
1525 | | |
1526 | | static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp) |
1527 | 2 | { |
1528 | 2 | ERRP_GUARD(); |
1529 | 2 | const char *sec_model = qemu_opt_get(opts, "security_model"); |
1530 | 2 | const char *path = qemu_opt_get(opts, "path"); |
1531 | 2 | const char *multidevs = qemu_opt_get(opts, "multidevs"); |
1532 | | |
1533 | 2 | uint64_t val = qemu_opt_get_number(opts, "max_xattr", |
1534 | 2 | V9FS_MAX_XATTR_DEFAULT); |
1535 | 2 | if (val > UINT32_MAX) { |
1536 | 0 | error_setg(errp, "max_xattr value '%s' too large", |
1537 | 0 | qemu_opt_get(opts, "max_xattr")); |
1538 | 0 | return -1; |
1539 | 0 | } |
1540 | 2 | fse->max_xattr = val; |
1541 | | |
1542 | 2 | if (!sec_model) { |
1543 | 0 | error_setg(errp, "security_model property not set"); |
1544 | 0 | error_append_security_model_hint(errp); |
1545 | 0 | return -1; |
1546 | 0 | } |
1547 | | |
1548 | 2 | if (!strcmp(sec_model, "passthrough")) { |
1549 | 0 | fse->export_flags |= V9FS_SM_PASSTHROUGH; |
1550 | 2 | } else if (!strcmp(sec_model, "mapped") || |
1551 | 2 | !strcmp(sec_model, "mapped-xattr")) { |
1552 | 2 | fse->export_flags |= V9FS_SM_MAPPED; |
1553 | 2 | } else if (!strcmp(sec_model, "none")) { |
1554 | 0 | fse->export_flags |= V9FS_SM_NONE; |
1555 | 0 | } else if (!strcmp(sec_model, "mapped-file")) { |
1556 | 0 | fse->export_flags |= V9FS_SM_MAPPED_FILE; |
1557 | 0 | } else { |
1558 | 0 | error_setg(errp, "invalid security_model property '%s'", sec_model); |
1559 | 0 | error_append_security_model_hint(errp); |
1560 | 0 | return -1; |
1561 | 0 | } |
1562 | | |
1563 | 2 | if (multidevs) { |
1564 | 0 | if (!strcmp(multidevs, "remap")) { |
1565 | 0 | fse->export_flags &= ~V9FS_FORBID_MULTIDEVS; |
1566 | 0 | fse->export_flags |= V9FS_REMAP_INODES; |
1567 | 0 | } else if (!strcmp(multidevs, "forbid")) { |
1568 | 0 | fse->export_flags &= ~V9FS_REMAP_INODES; |
1569 | 0 | fse->export_flags |= V9FS_FORBID_MULTIDEVS; |
1570 | 0 | } else if (!strcmp(multidevs, "warn")) { |
1571 | 0 | fse->export_flags &= ~V9FS_FORBID_MULTIDEVS; |
1572 | 0 | fse->export_flags &= ~V9FS_REMAP_INODES; |
1573 | 0 | } else { |
1574 | 0 | error_setg(errp, "invalid multidevs property '%s'", |
1575 | 0 | multidevs); |
1576 | 0 | error_append_hint(errp, "Valid options are: multidevs=" |
1577 | 0 | "[remap|forbid|warn]\n"); |
1578 | 0 | return -1; |
1579 | 0 | } |
1580 | 2 | } else { |
1581 | 2 | fse->export_flags &= ~V9FS_FORBID_MULTIDEVS; |
1582 | 2 | fse->export_flags |= V9FS_REMAP_INODES; |
1583 | 2 | } |
1584 | | |
1585 | 2 | if (!path) { |
1586 | 0 | error_setg(errp, "path property not set"); |
1587 | 0 | return -1; |
1588 | 0 | } |
1589 | | |
1590 | 2 | if (fsdev_throttle_parse_opts(opts, &fse->fst, errp)) { |
1591 | 0 | error_prepend(errp, "invalid throttle configuration: "); |
1592 | 0 | return -1; |
1593 | 0 | } |
1594 | | |
1595 | 2 | if (fse->export_flags & V9FS_SM_MAPPED || |
1596 | 2 | fse->export_flags & V9FS_SM_MAPPED_FILE) { |
1597 | 2 | fse->fmode = |
1598 | 2 | qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777; |
1599 | 2 | fse->dmode = |
1600 | 2 | qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777; |
1601 | 2 | } else { |
1602 | 0 | if (qemu_opt_find(opts, "fmode")) { |
1603 | 0 | error_setg(errp, "fmode is only valid for mapped security modes"); |
1604 | 0 | return -1; |
1605 | 0 | } |
1606 | 0 | if (qemu_opt_find(opts, "dmode")) { |
1607 | 0 | error_setg(errp, "dmode is only valid for mapped security modes"); |
1608 | 0 | return -1; |
1609 | 0 | } |
1610 | 0 | } |
1611 | | |
1612 | 2 | fse->path = g_strdup(path); |
1613 | | |
1614 | 2 | return 0; |
1615 | 2 | } |
1616 | | |
1617 | | static bool local_has_valid_file_handle(int fid_type, V9fsFidOpenState *fs) |
1618 | 0 | { |
1619 | 0 | return |
1620 | 0 | (fid_type == P9_FID_FILE && fs->fd != -1) || |
1621 | 0 | (fid_type == P9_FID_DIR && fs->dir.stream != NULL); |
1622 | 0 | } |
1623 | | |
1624 | | FileOperations local_ops = { |
1625 | | .parse_opts = local_parse_opts, |
1626 | | .init = local_init, |
1627 | | .cleanup = local_cleanup, |
1628 | | .lstat = local_lstat, |
1629 | | .readlink = local_readlink, |
1630 | | .close = local_close, |
1631 | | .closedir = local_closedir, |
1632 | | .open = local_open, |
1633 | | .opendir = local_opendir, |
1634 | | .rewinddir = local_rewinddir, |
1635 | | .telldir = local_telldir, |
1636 | | .readdir = local_readdir, |
1637 | | .seekdir = local_seekdir, |
1638 | | .preadv = local_preadv, |
1639 | | .pwritev = local_pwritev, |
1640 | | .chmod = local_chmod, |
1641 | | .mknod = local_mknod, |
1642 | | .mkdir = local_mkdir, |
1643 | | .fstat = local_fstat, |
1644 | | .open2 = local_open2, |
1645 | | .symlink = local_symlink, |
1646 | | .link = local_link, |
1647 | | .truncate = local_truncate, |
1648 | | .rename = local_rename, |
1649 | | .chown = local_chown, |
1650 | | .utimensat = local_utimensat, |
1651 | | .remove = local_remove, |
1652 | | .fsync = local_fsync, |
1653 | | .statfs = local_statfs, |
1654 | | .lgetxattr = local_lgetxattr, |
1655 | | .llistxattr = local_llistxattr, |
1656 | | .lsetxattr = local_lsetxattr, |
1657 | | .lremovexattr = local_lremovexattr, |
1658 | | .name_to_path = local_name_to_path, |
1659 | | .renameat = local_renameat, |
1660 | | .unlinkat = local_unlinkat, |
1661 | | .has_valid_file_handle = local_has_valid_file_handle, |
1662 | | .ftruncate = local_ftruncate, |
1663 | | .futimens = local_futimens, |
1664 | | }; |