]>
Commit | Line | Data |
---|---|---|
9f107513 AL |
1 | /* |
2 | * Virtio 9p Posix callback | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
873c3213 | 13 | |
0d09e41a | 14 | #include "hw/virtio/virtio.h" |
9f107513 | 15 | #include "virtio-9p.h" |
fc22118d | 16 | #include "virtio-9p-xattr.h" |
c494dd6f | 17 | #include <arpa/inet.h> |
131dcb25 AL |
18 | #include <pwd.h> |
19 | #include <grp.h> | |
c494dd6f AL |
20 | #include <sys/socket.h> |
21 | #include <sys/un.h> | |
1de7afc9 | 22 | #include "qemu/xattr.h" |
2c30dd74 | 23 | #include <libgen.h> |
e06a765e HPB |
24 | #include <linux/fs.h> |
25 | #ifdef CONFIG_LINUX_MAGIC_H | |
26 | #include <linux/magic.h> | |
27 | #endif | |
28 | #include <sys/ioctl.h> | |
29 | ||
30 | #ifndef XFS_SUPER_MAGIC | |
31 | #define XFS_SUPER_MAGIC 0x58465342 | |
32 | #endif | |
33 | #ifndef EXT2_SUPER_MAGIC | |
34 | #define EXT2_SUPER_MAGIC 0xEF53 | |
35 | #endif | |
36 | #ifndef REISERFS_SUPER_MAGIC | |
37 | #define REISERFS_SUPER_MAGIC 0x52654973 | |
38 | #endif | |
39 | #ifndef BTRFS_SUPER_MAGIC | |
40 | #define BTRFS_SUPER_MAGIC 0x9123683E | |
41 | #endif | |
131dcb25 | 42 | |
2c30dd74 AK |
43 | #define VIRTFS_META_DIR ".virtfs_metadata" |
44 | ||
45 | static const char *local_mapped_attr_path(FsContext *ctx, | |
46 | const char *path, char *buffer) | |
47 | { | |
48 | char *dir_name; | |
d3f8e138 | 49 | char *tmp_path = g_strdup(path); |
2c30dd74 AK |
50 | char *base_name = basename(tmp_path); |
51 | ||
52 | /* NULL terminate the directory */ | |
53 | dir_name = tmp_path; | |
54 | *(base_name - 1) = '\0'; | |
55 | ||
56 | snprintf(buffer, PATH_MAX, "%s/%s/%s/%s", | |
57 | ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name); | |
d3f8e138 | 58 | g_free(tmp_path); |
2c30dd74 AK |
59 | return buffer; |
60 | } | |
61 | ||
62 | #define ATTR_MAX 100 | |
63 | static void local_mapped_file_attr(FsContext *ctx, const char *path, | |
64 | struct stat *stbuf) | |
65 | { | |
66 | FILE *fp; | |
67 | char buf[ATTR_MAX]; | |
68 | char attr_path[PATH_MAX]; | |
69 | ||
70 | local_mapped_attr_path(ctx, path, attr_path); | |
71 | fp = fopen(attr_path, "r"); | |
72 | if (!fp) { | |
73 | return; | |
74 | } | |
75 | memset(buf, 0, ATTR_MAX); | |
76 | while (fgets(buf, ATTR_MAX, fp)) { | |
77 | if (!strncmp(buf, "virtfs.uid", 10)) { | |
78 | stbuf->st_uid = atoi(buf+11); | |
79 | } else if (!strncmp(buf, "virtfs.gid", 10)) { | |
80 | stbuf->st_gid = atoi(buf+11); | |
81 | } else if (!strncmp(buf, "virtfs.mode", 11)) { | |
82 | stbuf->st_mode = atoi(buf+12); | |
83 | } else if (!strncmp(buf, "virtfs.rdev", 11)) { | |
84 | stbuf->st_rdev = atoi(buf+12); | |
85 | } | |
86 | memset(buf, 0, ATTR_MAX); | |
87 | } | |
88 | fclose(fp); | |
89 | } | |
90 | ||
2289be19 | 91 | static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) |
131dcb25 | 92 | { |
1237ad76 | 93 | int err; |
faa44e3d | 94 | char buffer[PATH_MAX]; |
2289be19 AK |
95 | char *path = fs_path->data; |
96 | ||
faa44e3d | 97 | err = lstat(rpath(fs_ctx, path, buffer), stbuf); |
1237ad76 VJ |
98 | if (err) { |
99 | return err; | |
100 | } | |
b97400ca | 101 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
1237ad76 VJ |
102 | /* Actual credentials are part of extended attrs */ |
103 | uid_t tmp_uid; | |
104 | gid_t tmp_gid; | |
105 | mode_t tmp_mode; | |
106 | dev_t tmp_dev; | |
faa44e3d | 107 | if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid, |
1237ad76 VJ |
108 | sizeof(uid_t)) > 0) { |
109 | stbuf->st_uid = tmp_uid; | |
110 | } | |
faa44e3d | 111 | if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid, |
1237ad76 VJ |
112 | sizeof(gid_t)) > 0) { |
113 | stbuf->st_gid = tmp_gid; | |
114 | } | |
faa44e3d VJ |
115 | if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode", |
116 | &tmp_mode, sizeof(mode_t)) > 0) { | |
1237ad76 VJ |
117 | stbuf->st_mode = tmp_mode; |
118 | } | |
faa44e3d | 119 | if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev, |
1237ad76 VJ |
120 | sizeof(dev_t)) > 0) { |
121 | stbuf->st_rdev = tmp_dev; | |
122 | } | |
2c30dd74 AK |
123 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
124 | local_mapped_file_attr(fs_ctx, path, stbuf); | |
125 | } | |
126 | return err; | |
127 | } | |
128 | ||
129 | static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) | |
130 | { | |
131 | int err; | |
132 | char attr_dir[PATH_MAX]; | |
d3f8e138 | 133 | char *tmp_path = g_strdup(path); |
2c30dd74 AK |
134 | |
135 | snprintf(attr_dir, PATH_MAX, "%s/%s/%s", | |
136 | ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); | |
137 | ||
138 | err = mkdir(attr_dir, 0700); | |
139 | if (err < 0 && errno == EEXIST) { | |
140 | err = 0; | |
1237ad76 | 141 | } |
d3f8e138 | 142 | g_free(tmp_path); |
1237ad76 | 143 | return err; |
131dcb25 AL |
144 | } |
145 | ||
2c30dd74 AK |
146 | static int local_set_mapped_file_attr(FsContext *ctx, |
147 | const char *path, FsCred *credp) | |
148 | { | |
149 | FILE *fp; | |
150 | int ret = 0; | |
151 | char buf[ATTR_MAX]; | |
152 | char attr_path[PATH_MAX]; | |
153 | int uid = -1, gid = -1, mode = -1, rdev = -1; | |
154 | ||
155 | fp = fopen(local_mapped_attr_path(ctx, path, attr_path), "r"); | |
156 | if (!fp) { | |
157 | goto create_map_file; | |
158 | } | |
159 | memset(buf, 0, ATTR_MAX); | |
160 | while (fgets(buf, ATTR_MAX, fp)) { | |
161 | if (!strncmp(buf, "virtfs.uid", 10)) { | |
162 | uid = atoi(buf+11); | |
163 | } else if (!strncmp(buf, "virtfs.gid", 10)) { | |
164 | gid = atoi(buf+11); | |
165 | } else if (!strncmp(buf, "virtfs.mode", 11)) { | |
166 | mode = atoi(buf+12); | |
167 | } else if (!strncmp(buf, "virtfs.rdev", 11)) { | |
168 | rdev = atoi(buf+12); | |
169 | } | |
170 | memset(buf, 0, ATTR_MAX); | |
171 | } | |
172 | fclose(fp); | |
173 | goto update_map_file; | |
174 | ||
175 | create_map_file: | |
176 | ret = local_create_mapped_attr_dir(ctx, path); | |
177 | if (ret < 0) { | |
178 | goto err_out; | |
179 | } | |
180 | ||
181 | update_map_file: | |
182 | fp = fopen(attr_path, "w"); | |
183 | if (!fp) { | |
184 | ret = -1; | |
185 | goto err_out; | |
186 | } | |
187 | ||
188 | if (credp->fc_uid != -1) { | |
189 | uid = credp->fc_uid; | |
190 | } | |
191 | if (credp->fc_gid != -1) { | |
192 | gid = credp->fc_gid; | |
193 | } | |
194 | if (credp->fc_mode != -1) { | |
195 | mode = credp->fc_mode; | |
196 | } | |
197 | if (credp->fc_rdev != -1) { | |
198 | rdev = credp->fc_rdev; | |
199 | } | |
200 | ||
201 | ||
202 | if (uid != -1) { | |
203 | fprintf(fp, "virtfs.uid=%d\n", uid); | |
204 | } | |
205 | if (gid != -1) { | |
206 | fprintf(fp, "virtfs.gid=%d\n", gid); | |
207 | } | |
208 | if (mode != -1) { | |
209 | fprintf(fp, "virtfs.mode=%d\n", mode); | |
210 | } | |
211 | if (rdev != -1) { | |
212 | fprintf(fp, "virtfs.rdev=%d\n", rdev); | |
213 | } | |
214 | fclose(fp); | |
215 | ||
216 | err_out: | |
217 | return ret; | |
218 | } | |
219 | ||
758e8e38 | 220 | static int local_set_xattr(const char *path, FsCred *credp) |
131dcb25 | 221 | { |
758e8e38 | 222 | int err; |
2289be19 | 223 | |
758e8e38 VJ |
224 | if (credp->fc_uid != -1) { |
225 | err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t), | |
226 | 0); | |
227 | if (err) { | |
228 | return err; | |
229 | } | |
131dcb25 | 230 | } |
758e8e38 VJ |
231 | if (credp->fc_gid != -1) { |
232 | err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t), | |
233 | 0); | |
234 | if (err) { | |
235 | return err; | |
236 | } | |
131dcb25 | 237 | } |
758e8e38 VJ |
238 | if (credp->fc_mode != -1) { |
239 | err = setxattr(path, "user.virtfs.mode", &credp->fc_mode, | |
240 | sizeof(mode_t), 0); | |
241 | if (err) { | |
242 | return err; | |
243 | } | |
131dcb25 | 244 | } |
758e8e38 VJ |
245 | if (credp->fc_rdev != -1) { |
246 | err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev, | |
247 | sizeof(dev_t), 0); | |
248 | if (err) { | |
249 | return err; | |
250 | } | |
131dcb25 | 251 | } |
131dcb25 AL |
252 | return 0; |
253 | } | |
254 | ||
4750a96f | 255 | static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, |
2289be19 | 256 | FsCred *credp) |
4750a96f | 257 | { |
faa44e3d | 258 | char buffer[PATH_MAX]; |
2289be19 | 259 | |
faa44e3d VJ |
260 | if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid, |
261 | credp->fc_gid) < 0) { | |
12848bfc AK |
262 | /* |
263 | * If we fail to change ownership and if we are | |
264 | * using security model none. Ignore the error | |
265 | */ | |
b97400ca | 266 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
12848bfc AK |
267 | return -1; |
268 | } | |
4750a96f | 269 | } |
2d40564a MK |
270 | |
271 | if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) { | |
272 | return -1; | |
273 | } | |
4750a96f VJ |
274 | return 0; |
275 | } | |
276 | ||
2289be19 AK |
277 | static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, |
278 | char *buf, size_t bufsz) | |
131dcb25 | 279 | { |
879c2813 | 280 | ssize_t tsize = -1; |
faa44e3d | 281 | char buffer[PATH_MAX]; |
2289be19 AK |
282 | char *path = fs_path->data; |
283 | ||
2c30dd74 AK |
284 | if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || |
285 | (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { | |
879c2813 | 286 | int fd; |
aed858ce | 287 | fd = open(rpath(fs_ctx, path, buffer), O_RDONLY | O_NOFOLLOW); |
879c2813 VJ |
288 | if (fd == -1) { |
289 | return -1; | |
290 | } | |
291 | do { | |
292 | tsize = read(fd, (void *)buf, bufsz); | |
293 | } while (tsize == -1 && errno == EINTR); | |
294 | close(fd); | |
295 | return tsize; | |
b97400ca AK |
296 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
297 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d | 298 | tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz); |
879c2813 VJ |
299 | } |
300 | return tsize; | |
131dcb25 AL |
301 | } |
302 | ||
cc720ddb | 303 | static int local_close(FsContext *ctx, V9fsFidOpenState *fs) |
131dcb25 | 304 | { |
cc720ddb | 305 | return close(fs->fd); |
131dcb25 AL |
306 | } |
307 | ||
cc720ddb | 308 | static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) |
131dcb25 | 309 | { |
cc720ddb | 310 | return closedir(fs->dir); |
131dcb25 | 311 | } |
9f107513 | 312 | |
cc720ddb AK |
313 | static int local_open(FsContext *ctx, V9fsPath *fs_path, |
314 | int flags, V9fsFidOpenState *fs) | |
a6568fe2 | 315 | { |
faa44e3d | 316 | char buffer[PATH_MAX]; |
2289be19 AK |
317 | char *path = fs_path->data; |
318 | ||
cc720ddb AK |
319 | fs->fd = open(rpath(ctx, path, buffer), flags); |
320 | return fs->fd; | |
a6568fe2 AL |
321 | } |
322 | ||
cc720ddb AK |
323 | static int local_opendir(FsContext *ctx, |
324 | V9fsPath *fs_path, V9fsFidOpenState *fs) | |
a6568fe2 | 325 | { |
faa44e3d | 326 | char buffer[PATH_MAX]; |
2289be19 AK |
327 | char *path = fs_path->data; |
328 | ||
cc720ddb AK |
329 | fs->dir = opendir(rpath(ctx, path, buffer)); |
330 | if (!fs->dir) { | |
331 | return -1; | |
332 | } | |
333 | return 0; | |
a6568fe2 AL |
334 | } |
335 | ||
cc720ddb | 336 | static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) |
a9231555 | 337 | { |
cc720ddb | 338 | return rewinddir(fs->dir); |
a9231555 AL |
339 | } |
340 | ||
cc720ddb | 341 | static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) |
a9231555 | 342 | { |
cc720ddb | 343 | return telldir(fs->dir); |
a9231555 AL |
344 | } |
345 | ||
cc720ddb AK |
346 | static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs, |
347 | struct dirent *entry, | |
2289be19 | 348 | struct dirent **result) |
a9231555 | 349 | { |
2c30dd74 AK |
350 | int ret; |
351 | ||
352 | again: | |
353 | ret = readdir_r(fs->dir, entry, result); | |
354 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
355 | if (!ret && *result != NULL && | |
356 | !strcmp(entry->d_name, VIRTFS_META_DIR)) { | |
357 | /* skp the meta data directory */ | |
358 | goto again; | |
359 | } | |
360 | } | |
361 | return ret; | |
a9231555 AL |
362 | } |
363 | ||
cc720ddb | 364 | static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) |
a9231555 | 365 | { |
cc720ddb | 366 | return seekdir(fs->dir, off); |
a9231555 AL |
367 | } |
368 | ||
cc720ddb AK |
369 | static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, |
370 | const struct iovec *iov, | |
56d15a53 | 371 | int iovcnt, off_t offset) |
a9231555 | 372 | { |
56d15a53 | 373 | #ifdef CONFIG_PREADV |
cc720ddb | 374 | return preadv(fs->fd, iov, iovcnt, offset); |
56d15a53 | 375 | #else |
cc720ddb | 376 | int err = lseek(fs->fd, offset, SEEK_SET); |
56d15a53 SG |
377 | if (err == -1) { |
378 | return err; | |
379 | } else { | |
cc720ddb | 380 | return readv(fs->fd, iov, iovcnt); |
56d15a53 SG |
381 | } |
382 | #endif | |
a9231555 AL |
383 | } |
384 | ||
cc720ddb AK |
385 | static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, |
386 | const struct iovec *iov, | |
2289be19 | 387 | int iovcnt, off_t offset) |
8449360c | 388 | { |
d3ab98e6 AK |
389 | ssize_t ret |
390 | ; | |
56d15a53 | 391 | #ifdef CONFIG_PREADV |
cc720ddb | 392 | ret = pwritev(fs->fd, iov, iovcnt, offset); |
56d15a53 | 393 | #else |
cc720ddb | 394 | int err = lseek(fs->fd, offset, SEEK_SET); |
56d15a53 SG |
395 | if (err == -1) { |
396 | return err; | |
397 | } else { | |
cc720ddb | 398 | ret = writev(fs->fd, iov, iovcnt); |
56d15a53 SG |
399 | } |
400 | #endif | |
d3ab98e6 AK |
401 | #ifdef CONFIG_SYNC_FILE_RANGE |
402 | if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { | |
403 | /* | |
404 | * Initiate a writeback. This is not a data integrity sync. | |
405 | * We want to ensure that we don't leave dirty pages in the cache | |
406 | * after write when writeout=immediate is sepcified. | |
407 | */ | |
cc720ddb | 408 | sync_file_range(fs->fd, offset, ret, |
d3ab98e6 AK |
409 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); |
410 | } | |
411 | #endif | |
412 | return ret; | |
8449360c AL |
413 | } |
414 | ||
2289be19 | 415 | static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
c494dd6f | 416 | { |
faa44e3d | 417 | char buffer[PATH_MAX]; |
2289be19 AK |
418 | char *path = fs_path->data; |
419 | ||
b97400ca | 420 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
faa44e3d | 421 | return local_set_xattr(rpath(fs_ctx, path, buffer), credp); |
2c30dd74 AK |
422 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
423 | return local_set_mapped_file_attr(fs_ctx, path, credp); | |
b97400ca AK |
424 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
425 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d | 426 | return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode); |
e95ead32 VJ |
427 | } |
428 | return -1; | |
c494dd6f AL |
429 | } |
430 | ||
2289be19 AK |
431 | static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, |
432 | const char *name, FsCred *credp) | |
c494dd6f | 433 | { |
2289be19 | 434 | char *path; |
1c293312 VJ |
435 | int err = -1; |
436 | int serrno = 0; | |
2289be19 | 437 | V9fsString fullname; |
faa44e3d | 438 | char buffer[PATH_MAX]; |
1c293312 | 439 | |
2289be19 AK |
440 | v9fs_string_init(&fullname); |
441 | v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); | |
442 | path = fullname.data; | |
443 | ||
1c293312 | 444 | /* Determine the security model */ |
b97400ca | 445 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
faa44e3d VJ |
446 | err = mknod(rpath(fs_ctx, path, buffer), |
447 | SM_LOCAL_MODE_BITS|S_IFREG, 0); | |
1c293312 | 448 | if (err == -1) { |
2289be19 | 449 | goto out; |
1c293312 | 450 | } |
17b1971f | 451 | err = local_set_xattr(rpath(fs_ctx, path, buffer), credp); |
1c293312 VJ |
452 | if (err == -1) { |
453 | serrno = errno; | |
454 | goto err_end; | |
455 | } | |
2c30dd74 AK |
456 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
457 | ||
458 | err = mknod(rpath(fs_ctx, path, buffer), | |
459 | SM_LOCAL_MODE_BITS|S_IFREG, 0); | |
460 | if (err == -1) { | |
461 | goto out; | |
462 | } | |
463 | err = local_set_mapped_file_attr(fs_ctx, path, credp); | |
464 | if (err == -1) { | |
465 | serrno = errno; | |
466 | goto err_end; | |
467 | } | |
b97400ca AK |
468 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
469 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d VJ |
470 | err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode, |
471 | credp->fc_rdev); | |
1c293312 | 472 | if (err == -1) { |
2289be19 | 473 | goto out; |
1c293312 VJ |
474 | } |
475 | err = local_post_create_passthrough(fs_ctx, path, credp); | |
476 | if (err == -1) { | |
477 | serrno = errno; | |
478 | goto err_end; | |
479 | } | |
480 | } | |
2289be19 | 481 | goto out; |
1c293312 VJ |
482 | |
483 | err_end: | |
faa44e3d | 484 | remove(rpath(fs_ctx, path, buffer)); |
1c293312 | 485 | errno = serrno; |
2289be19 AK |
486 | out: |
487 | v9fs_string_free(&fullname); | |
1c293312 | 488 | return err; |
c494dd6f AL |
489 | } |
490 | ||
2289be19 AK |
491 | static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, |
492 | const char *name, FsCred *credp) | |
c494dd6f | 493 | { |
2289be19 | 494 | char *path; |
00ec5c37 VJ |
495 | int err = -1; |
496 | int serrno = 0; | |
2289be19 | 497 | V9fsString fullname; |
faa44e3d | 498 | char buffer[PATH_MAX]; |
00ec5c37 | 499 | |
2289be19 AK |
500 | v9fs_string_init(&fullname); |
501 | v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); | |
502 | path = fullname.data; | |
503 | ||
00ec5c37 | 504 | /* Determine the security model */ |
b97400ca | 505 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
faa44e3d | 506 | err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS); |
00ec5c37 | 507 | if (err == -1) { |
2289be19 | 508 | goto out; |
00ec5c37 VJ |
509 | } |
510 | credp->fc_mode = credp->fc_mode|S_IFDIR; | |
faa44e3d | 511 | err = local_set_xattr(rpath(fs_ctx, path, buffer), credp); |
00ec5c37 VJ |
512 | if (err == -1) { |
513 | serrno = errno; | |
514 | goto err_end; | |
515 | } | |
2c30dd74 AK |
516 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
517 | err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS); | |
518 | if (err == -1) { | |
519 | goto out; | |
520 | } | |
521 | credp->fc_mode = credp->fc_mode|S_IFDIR; | |
522 | err = local_set_mapped_file_attr(fs_ctx, path, credp); | |
523 | if (err == -1) { | |
524 | serrno = errno; | |
525 | goto err_end; | |
526 | } | |
b97400ca AK |
527 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
528 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d | 529 | err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode); |
00ec5c37 | 530 | if (err == -1) { |
2289be19 | 531 | goto out; |
00ec5c37 VJ |
532 | } |
533 | err = local_post_create_passthrough(fs_ctx, path, credp); | |
534 | if (err == -1) { | |
535 | serrno = errno; | |
536 | goto err_end; | |
537 | } | |
538 | } | |
2289be19 | 539 | goto out; |
00ec5c37 VJ |
540 | |
541 | err_end: | |
faa44e3d | 542 | remove(rpath(fs_ctx, path, buffer)); |
00ec5c37 | 543 | errno = serrno; |
2289be19 AK |
544 | out: |
545 | v9fs_string_free(&fullname); | |
00ec5c37 | 546 | return err; |
c494dd6f AL |
547 | } |
548 | ||
8b888272 | 549 | static int local_fstat(FsContext *fs_ctx, int fid_type, |
cc720ddb | 550 | V9fsFidOpenState *fs, struct stat *stbuf) |
c494dd6f | 551 | { |
8b888272 AK |
552 | int err, fd; |
553 | ||
554 | if (fid_type == P9_FID_DIR) { | |
555 | fd = dirfd(fs->dir); | |
556 | } else { | |
557 | fd = fs->fd; | |
558 | } | |
559 | ||
560 | err = fstat(fd, stbuf); | |
1237ad76 VJ |
561 | if (err) { |
562 | return err; | |
563 | } | |
b97400ca | 564 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
1237ad76 VJ |
565 | /* Actual credentials are part of extended attrs */ |
566 | uid_t tmp_uid; | |
567 | gid_t tmp_gid; | |
568 | mode_t tmp_mode; | |
569 | dev_t tmp_dev; | |
570 | ||
8b888272 | 571 | if (fgetxattr(fd, "user.virtfs.uid", |
cc720ddb | 572 | &tmp_uid, sizeof(uid_t)) > 0) { |
1237ad76 VJ |
573 | stbuf->st_uid = tmp_uid; |
574 | } | |
8b888272 | 575 | if (fgetxattr(fd, "user.virtfs.gid", |
cc720ddb | 576 | &tmp_gid, sizeof(gid_t)) > 0) { |
1237ad76 VJ |
577 | stbuf->st_gid = tmp_gid; |
578 | } | |
8b888272 | 579 | if (fgetxattr(fd, "user.virtfs.mode", |
cc720ddb | 580 | &tmp_mode, sizeof(mode_t)) > 0) { |
1237ad76 VJ |
581 | stbuf->st_mode = tmp_mode; |
582 | } | |
8b888272 | 583 | if (fgetxattr(fd, "user.virtfs.rdev", |
cc720ddb | 584 | &tmp_dev, sizeof(dev_t)) > 0) { |
1237ad76 VJ |
585 | stbuf->st_rdev = tmp_dev; |
586 | } | |
2c30dd74 AK |
587 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
588 | errno = EOPNOTSUPP; | |
589 | return -1; | |
1237ad76 VJ |
590 | } |
591 | return err; | |
c494dd6f AL |
592 | } |
593 | ||
2289be19 | 594 | static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, |
cc720ddb | 595 | int flags, FsCred *credp, V9fsFidOpenState *fs) |
c494dd6f | 596 | { |
2289be19 | 597 | char *path; |
4750a96f VJ |
598 | int fd = -1; |
599 | int err = -1; | |
600 | int serrno = 0; | |
2289be19 | 601 | V9fsString fullname; |
faa44e3d | 602 | char buffer[PATH_MAX]; |
4750a96f | 603 | |
2289be19 AK |
604 | v9fs_string_init(&fullname); |
605 | v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); | |
606 | path = fullname.data; | |
607 | ||
4750a96f | 608 | /* Determine the security model */ |
b97400ca | 609 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
faa44e3d | 610 | fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS); |
4750a96f | 611 | if (fd == -1) { |
2289be19 AK |
612 | err = fd; |
613 | goto out; | |
4750a96f VJ |
614 | } |
615 | credp->fc_mode = credp->fc_mode|S_IFREG; | |
616 | /* Set cleint credentials in xattr */ | |
faa44e3d | 617 | err = local_set_xattr(rpath(fs_ctx, path, buffer), credp); |
4750a96f VJ |
618 | if (err == -1) { |
619 | serrno = errno; | |
620 | goto err_end; | |
621 | } | |
2c30dd74 AK |
622 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
623 | fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS); | |
624 | if (fd == -1) { | |
625 | err = fd; | |
626 | goto out; | |
627 | } | |
628 | credp->fc_mode = credp->fc_mode|S_IFREG; | |
629 | /* Set client credentials in .virtfs_metadata directory files */ | |
630 | err = local_set_mapped_file_attr(fs_ctx, path, credp); | |
631 | if (err == -1) { | |
632 | serrno = errno; | |
633 | goto err_end; | |
634 | } | |
b97400ca AK |
635 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
636 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d | 637 | fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode); |
4750a96f | 638 | if (fd == -1) { |
2289be19 AK |
639 | err = fd; |
640 | goto out; | |
4750a96f VJ |
641 | } |
642 | err = local_post_create_passthrough(fs_ctx, path, credp); | |
643 | if (err == -1) { | |
644 | serrno = errno; | |
645 | goto err_end; | |
646 | } | |
647 | } | |
2289be19 | 648 | err = fd; |
cc720ddb | 649 | fs->fd = fd; |
2289be19 | 650 | goto out; |
4750a96f VJ |
651 | |
652 | err_end: | |
653 | close(fd); | |
faa44e3d | 654 | remove(rpath(fs_ctx, path, buffer)); |
4750a96f | 655 | errno = serrno; |
2289be19 AK |
656 | out: |
657 | v9fs_string_free(&fullname); | |
4750a96f | 658 | return err; |
c494dd6f AL |
659 | } |
660 | ||
758e8e38 | 661 | |
879c2813 | 662 | static int local_symlink(FsContext *fs_ctx, const char *oldpath, |
2289be19 | 663 | V9fsPath *dir_path, const char *name, FsCred *credp) |
c494dd6f | 664 | { |
879c2813 VJ |
665 | int err = -1; |
666 | int serrno = 0; | |
2289be19 AK |
667 | char *newpath; |
668 | V9fsString fullname; | |
faa44e3d | 669 | char buffer[PATH_MAX]; |
879c2813 | 670 | |
2289be19 AK |
671 | v9fs_string_init(&fullname); |
672 | v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); | |
673 | newpath = fullname.data; | |
674 | ||
879c2813 | 675 | /* Determine the security model */ |
b97400ca | 676 | if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
879c2813 VJ |
677 | int fd; |
678 | ssize_t oldpath_size, write_size; | |
faa44e3d | 679 | fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR, |
879c2813 VJ |
680 | SM_LOCAL_MODE_BITS); |
681 | if (fd == -1) { | |
2289be19 AK |
682 | err = fd; |
683 | goto out; | |
879c2813 VJ |
684 | } |
685 | /* Write the oldpath (target) to the file. */ | |
f35bde2f | 686 | oldpath_size = strlen(oldpath); |
879c2813 VJ |
687 | do { |
688 | write_size = write(fd, (void *)oldpath, oldpath_size); | |
689 | } while (write_size == -1 && errno == EINTR); | |
690 | ||
691 | if (write_size != oldpath_size) { | |
692 | serrno = errno; | |
693 | close(fd); | |
694 | err = -1; | |
695 | goto err_end; | |
696 | } | |
697 | close(fd); | |
698 | /* Set cleint credentials in symlink's xattr */ | |
699 | credp->fc_mode = credp->fc_mode|S_IFLNK; | |
faa44e3d | 700 | err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp); |
879c2813 VJ |
701 | if (err == -1) { |
702 | serrno = errno; | |
703 | goto err_end; | |
704 | } | |
2c30dd74 AK |
705 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
706 | int fd; | |
707 | ssize_t oldpath_size, write_size; | |
708 | fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR, | |
709 | SM_LOCAL_MODE_BITS); | |
710 | if (fd == -1) { | |
711 | err = fd; | |
712 | goto out; | |
713 | } | |
714 | /* Write the oldpath (target) to the file. */ | |
715 | oldpath_size = strlen(oldpath); | |
716 | do { | |
717 | write_size = write(fd, (void *)oldpath, oldpath_size); | |
718 | } while (write_size == -1 && errno == EINTR); | |
719 | ||
720 | if (write_size != oldpath_size) { | |
721 | serrno = errno; | |
722 | close(fd); | |
723 | err = -1; | |
724 | goto err_end; | |
725 | } | |
726 | close(fd); | |
727 | /* Set cleint credentials in symlink's xattr */ | |
728 | credp->fc_mode = credp->fc_mode|S_IFLNK; | |
729 | err = local_set_mapped_file_attr(fs_ctx, newpath, credp); | |
730 | if (err == -1) { | |
731 | serrno = errno; | |
732 | goto err_end; | |
733 | } | |
b97400ca AK |
734 | } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
735 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
faa44e3d | 736 | err = symlink(oldpath, rpath(fs_ctx, newpath, buffer)); |
879c2813 | 737 | if (err) { |
2289be19 | 738 | goto out; |
879c2813 | 739 | } |
faa44e3d | 740 | err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid, |
2289be19 | 741 | credp->fc_gid); |
879c2813 | 742 | if (err == -1) { |
12848bfc AK |
743 | /* |
744 | * If we fail to change ownership and if we are | |
745 | * using security model none. Ignore the error | |
746 | */ | |
b97400ca | 747 | if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { |
12848bfc AK |
748 | serrno = errno; |
749 | goto err_end; | |
750 | } else | |
751 | err = 0; | |
879c2813 VJ |
752 | } |
753 | } | |
2289be19 | 754 | goto out; |
879c2813 VJ |
755 | |
756 | err_end: | |
faa44e3d | 757 | remove(rpath(fs_ctx, newpath, buffer)); |
879c2813 | 758 | errno = serrno; |
2289be19 AK |
759 | out: |
760 | v9fs_string_free(&fullname); | |
879c2813 | 761 | return err; |
c494dd6f AL |
762 | } |
763 | ||
2289be19 AK |
764 | static int local_link(FsContext *ctx, V9fsPath *oldpath, |
765 | V9fsPath *dirpath, const char *name) | |
c494dd6f | 766 | { |
2289be19 AK |
767 | int ret; |
768 | V9fsString newpath; | |
faa44e3d | 769 | char buffer[PATH_MAX], buffer1[PATH_MAX]; |
c494dd6f | 770 | |
2289be19 AK |
771 | v9fs_string_init(&newpath); |
772 | v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name); | |
773 | ||
774 | ret = link(rpath(ctx, oldpath->data, buffer), | |
775 | rpath(ctx, newpath.data, buffer1)); | |
2c30dd74 AK |
776 | |
777 | /* now link the virtfs_metadata files */ | |
778 | if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) { | |
779 | /* Link the .virtfs_metadata files. Create the metada directory */ | |
780 | ret = local_create_mapped_attr_dir(ctx, newpath.data); | |
781 | if (ret < 0) { | |
782 | goto err_out; | |
783 | } | |
784 | ret = link(local_mapped_attr_path(ctx, oldpath->data, buffer), | |
785 | local_mapped_attr_path(ctx, newpath.data, buffer1)); | |
786 | if (ret < 0 && errno != ENOENT) { | |
787 | goto err_out; | |
788 | } | |
789 | } | |
790 | err_out: | |
2289be19 AK |
791 | v9fs_string_free(&newpath); |
792 | return ret; | |
c494dd6f AL |
793 | } |
794 | ||
2289be19 | 795 | static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) |
8cf89e00 | 796 | { |
faa44e3d | 797 | char buffer[PATH_MAX]; |
2289be19 AK |
798 | char *path = fs_path->data; |
799 | ||
faa44e3d | 800 | return truncate(rpath(ctx, path, buffer), size); |
8cf89e00 AL |
801 | } |
802 | ||
803 | static int local_rename(FsContext *ctx, const char *oldpath, | |
804 | const char *newpath) | |
805 | { | |
2c30dd74 | 806 | int err; |
faa44e3d | 807 | char buffer[PATH_MAX], buffer1[PATH_MAX]; |
8cf89e00 | 808 | |
2c30dd74 AK |
809 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
810 | err = local_create_mapped_attr_dir(ctx, newpath); | |
811 | if (err < 0) { | |
812 | return err; | |
813 | } | |
814 | /* rename the .virtfs_metadata files */ | |
815 | err = rename(local_mapped_attr_path(ctx, oldpath, buffer), | |
816 | local_mapped_attr_path(ctx, newpath, buffer1)); | |
817 | if (err < 0 && errno != ENOENT) { | |
818 | return err; | |
819 | } | |
820 | } | |
faa44e3d | 821 | return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1)); |
8cf89e00 AL |
822 | } |
823 | ||
2289be19 | 824 | static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) |
8cf89e00 | 825 | { |
faa44e3d | 826 | char buffer[PATH_MAX]; |
2289be19 AK |
827 | char *path = fs_path->data; |
828 | ||
c79ce737 | 829 | if ((credp->fc_uid == -1 && credp->fc_gid == -1) || |
17b1971f AK |
830 | (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || |
831 | (fs_ctx->export_flags & V9FS_SM_NONE)) { | |
832 | return lchown(rpath(fs_ctx, path, buffer), | |
833 | credp->fc_uid, credp->fc_gid); | |
b97400ca | 834 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { |
faa44e3d | 835 | return local_set_xattr(rpath(fs_ctx, path, buffer), credp); |
2c30dd74 AK |
836 | } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
837 | return local_set_mapped_file_attr(fs_ctx, path, credp); | |
f7613bee VJ |
838 | } |
839 | return -1; | |
8cf89e00 AL |
840 | } |
841 | ||
2289be19 | 842 | static int local_utimensat(FsContext *s, V9fsPath *fs_path, |
38671423 | 843 | const struct timespec *buf) |
8cf89e00 | 844 | { |
faa44e3d | 845 | char buffer[PATH_MAX]; |
2289be19 AK |
846 | char *path = fs_path->data; |
847 | ||
ae0f940e | 848 | return qemu_utimens(rpath(s, path, buffer), buf); |
8cf89e00 AL |
849 | } |
850 | ||
5bae1900 AL |
851 | static int local_remove(FsContext *ctx, const char *path) |
852 | { | |
2c30dd74 AK |
853 | int err; |
854 | struct stat stbuf; | |
faa44e3d | 855 | char buffer[PATH_MAX]; |
2c30dd74 AK |
856 | |
857 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
858 | err = lstat(rpath(ctx, path, buffer), &stbuf); | |
859 | if (err) { | |
860 | goto err_out; | |
861 | } | |
862 | /* | |
863 | * If directory remove .virtfs_metadata contained in the | |
864 | * directory | |
865 | */ | |
866 | if (S_ISDIR(stbuf.st_mode)) { | |
867 | sprintf(buffer, "%s/%s/%s", ctx->fs_root, path, VIRTFS_META_DIR); | |
868 | err = remove(buffer); | |
869 | if (err < 0 && errno != ENOENT) { | |
870 | /* | |
871 | * We didn't had the .virtfs_metadata file. May be file created | |
872 | * in non-mapped mode ?. Ignore ENOENT. | |
873 | */ | |
874 | goto err_out; | |
875 | } | |
876 | } | |
877 | /* | |
878 | * Now remove the name from parent directory | |
879 | * .virtfs_metadata directory | |
880 | */ | |
c7e775e4 | 881 | err = remove(local_mapped_attr_path(ctx, path, buffer)); |
2c30dd74 AK |
882 | if (err < 0 && errno != ENOENT) { |
883 | /* | |
884 | * We didn't had the .virtfs_metadata file. May be file created | |
885 | * in non-mapped mode ?. Ignore ENOENT. | |
886 | */ | |
887 | goto err_out; | |
888 | } | |
889 | } | |
faa44e3d | 890 | return remove(rpath(ctx, path, buffer)); |
2c30dd74 AK |
891 | err_out: |
892 | return err; | |
5bae1900 AL |
893 | } |
894 | ||
8b888272 AK |
895 | static int local_fsync(FsContext *ctx, int fid_type, |
896 | V9fsFidOpenState *fs, int datasync) | |
8cf89e00 | 897 | { |
8b888272 AK |
898 | int fd; |
899 | ||
900 | if (fid_type == P9_FID_DIR) { | |
901 | fd = dirfd(fs->dir); | |
902 | } else { | |
903 | fd = fs->fd; | |
904 | } | |
905 | ||
49594973 | 906 | if (datasync) { |
8b888272 | 907 | return qemu_fdatasync(fd); |
49594973 | 908 | } else { |
8b888272 | 909 | return fsync(fd); |
49594973 | 910 | } |
8cf89e00 AL |
911 | } |
912 | ||
2289be19 | 913 | static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) |
be940c87 | 914 | { |
faa44e3d | 915 | char buffer[PATH_MAX]; |
2289be19 AK |
916 | char *path = fs_path->data; |
917 | ||
918 | return statfs(rpath(s, path, buffer), stbuf); | |
be940c87 MK |
919 | } |
920 | ||
2289be19 | 921 | static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, |
fa32ef88 AK |
922 | const char *name, void *value, size_t size) |
923 | { | |
2289be19 AK |
924 | char *path = fs_path->data; |
925 | ||
fc22118d | 926 | return v9fs_get_xattr(ctx, path, name, value, size); |
fa32ef88 AK |
927 | } |
928 | ||
2289be19 | 929 | static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, |
fa32ef88 AK |
930 | void *value, size_t size) |
931 | { | |
2289be19 AK |
932 | char *path = fs_path->data; |
933 | ||
fc22118d | 934 | return v9fs_list_xattr(ctx, path, value, size); |
fa32ef88 AK |
935 | } |
936 | ||
2289be19 | 937 | static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, |
10b468bd AK |
938 | void *value, size_t size, int flags) |
939 | { | |
2289be19 AK |
940 | char *path = fs_path->data; |
941 | ||
fc22118d | 942 | return v9fs_set_xattr(ctx, path, name, value, size, flags); |
10b468bd AK |
943 | } |
944 | ||
2289be19 AK |
945 | static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, |
946 | const char *name) | |
9ed3ef26 | 947 | { |
2289be19 AK |
948 | char *path = fs_path->data; |
949 | ||
fc22118d | 950 | return v9fs_remove_xattr(ctx, path, name); |
9ed3ef26 AK |
951 | } |
952 | ||
2289be19 AK |
953 | static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, |
954 | const char *name, V9fsPath *target) | |
955 | { | |
956 | if (dir_path) { | |
957 | v9fs_string_sprintf((V9fsString *)target, "%s/%s", | |
958 | dir_path->data, name); | |
959 | } else { | |
960 | v9fs_string_sprintf((V9fsString *)target, "%s", name); | |
961 | } | |
962 | /* Bump the size for including terminating NULL */ | |
963 | target->size++; | |
964 | return 0; | |
965 | } | |
966 | ||
967 | static int local_renameat(FsContext *ctx, V9fsPath *olddir, | |
968 | const char *old_name, V9fsPath *newdir, | |
969 | const char *new_name) | |
970 | { | |
971 | int ret; | |
972 | V9fsString old_full_name, new_full_name; | |
973 | ||
974 | v9fs_string_init(&old_full_name); | |
975 | v9fs_string_init(&new_full_name); | |
976 | ||
977 | v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name); | |
978 | v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name); | |
979 | ||
980 | ret = local_rename(ctx, old_full_name.data, new_full_name.data); | |
981 | v9fs_string_free(&old_full_name); | |
982 | v9fs_string_free(&new_full_name); | |
983 | return ret; | |
984 | } | |
985 | ||
986 | static int local_unlinkat(FsContext *ctx, V9fsPath *dir, | |
987 | const char *name, int flags) | |
988 | { | |
989 | int ret; | |
990 | V9fsString fullname; | |
991 | char buffer[PATH_MAX]; | |
2c30dd74 | 992 | |
2289be19 AK |
993 | v9fs_string_init(&fullname); |
994 | ||
995 | v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name); | |
2c30dd74 AK |
996 | if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { |
997 | if (flags == AT_REMOVEDIR) { | |
998 | /* | |
999 | * If directory remove .virtfs_metadata contained in the | |
1000 | * directory | |
1001 | */ | |
1002 | sprintf(buffer, "%s/%s/%s", ctx->fs_root, | |
1003 | fullname.data, VIRTFS_META_DIR); | |
1004 | ret = remove(buffer); | |
1005 | if (ret < 0 && errno != ENOENT) { | |
1006 | /* | |
1007 | * We didn't had the .virtfs_metadata file. May be file created | |
1008 | * in non-mapped mode ?. Ignore ENOENT. | |
1009 | */ | |
1010 | goto err_out; | |
1011 | } | |
1012 | } | |
1013 | /* | |
1014 | * Now remove the name from parent directory | |
1015 | * .virtfs_metadata directory. | |
1016 | */ | |
1017 | ret = remove(local_mapped_attr_path(ctx, fullname.data, buffer)); | |
1018 | if (ret < 0 && errno != ENOENT) { | |
1019 | /* | |
1020 | * We didn't had the .virtfs_metadata file. May be file created | |
1021 | * in non-mapped mode ?. Ignore ENOENT. | |
1022 | */ | |
1023 | goto err_out; | |
1024 | } | |
1025 | } | |
1026 | /* Remove the name finally */ | |
2289be19 AK |
1027 | ret = remove(rpath(ctx, fullname.data, buffer)); |
1028 | v9fs_string_free(&fullname); | |
1029 | ||
2c30dd74 | 1030 | err_out: |
2289be19 AK |
1031 | return ret; |
1032 | } | |
9ed3ef26 | 1033 | |
e06a765e HPB |
1034 | static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, |
1035 | mode_t st_mode, uint64_t *st_gen) | |
1036 | { | |
cc720ddb | 1037 | int err; |
ae0f940e | 1038 | #ifdef FS_IOC_GETVERSION |
cc720ddb AK |
1039 | V9fsFidOpenState fid_open; |
1040 | ||
e06a765e HPB |
1041 | /* |
1042 | * Do not try to open special files like device nodes, fifos etc | |
1043 | * We can get fd for regular files and directories only | |
1044 | */ | |
1045 | if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { | |
1046 | return 0; | |
1047 | } | |
cc720ddb AK |
1048 | err = local_open(ctx, path, O_RDONLY, &fid_open); |
1049 | if (err < 0) { | |
1050 | return err; | |
e06a765e | 1051 | } |
cc720ddb AK |
1052 | err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); |
1053 | local_close(ctx, &fid_open); | |
ae0f940e PB |
1054 | #else |
1055 | err = -ENOTTY; | |
1056 | #endif | |
e06a765e HPB |
1057 | return err; |
1058 | } | |
1059 | ||
0174fe73 AK |
1060 | static int local_init(FsContext *ctx) |
1061 | { | |
2507718b | 1062 | int err = 0; |
e06a765e HPB |
1063 | struct statfs stbuf; |
1064 | ||
2c30dd74 AK |
1065 | if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { |
1066 | ctx->xops = passthrough_xattr_ops; | |
1067 | } else if (ctx->export_flags & V9FS_SM_MAPPED) { | |
1068 | ctx->xops = mapped_xattr_ops; | |
1069 | } else if (ctx->export_flags & V9FS_SM_NONE) { | |
1070 | ctx->xops = none_xattr_ops; | |
1071 | } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { | |
1072 | /* | |
1073 | * xattr operation for mapped-file and passthrough | |
1074 | * remain same. | |
1075 | */ | |
1076 | ctx->xops = passthrough_xattr_ops; | |
1077 | } | |
c98f1d4a | 1078 | ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; |
2507718b AK |
1079 | #ifdef FS_IOC_GETVERSION |
1080 | /* | |
1081 | * use ioc_getversion only if the iocl is definied | |
1082 | */ | |
e06a765e HPB |
1083 | err = statfs(ctx->fs_root, &stbuf); |
1084 | if (!err) { | |
1085 | switch (stbuf.f_type) { | |
1086 | case EXT2_SUPER_MAGIC: | |
1087 | case BTRFS_SUPER_MAGIC: | |
1088 | case REISERFS_SUPER_MAGIC: | |
1089 | case XFS_SUPER_MAGIC: | |
1090 | ctx->exops.get_st_gen = local_ioc_getversion; | |
1091 | break; | |
1092 | } | |
1093 | } | |
2507718b | 1094 | #endif |
e06a765e | 1095 | return err; |
0174fe73 AK |
1096 | } |
1097 | ||
99519f0a AK |
1098 | static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) |
1099 | { | |
1100 | const char *sec_model = qemu_opt_get(opts, "security_model"); | |
1101 | const char *path = qemu_opt_get(opts, "path"); | |
1102 | ||
1103 | if (!sec_model) { | |
1104 | fprintf(stderr, "security model not specified, " | |
1105 | "local fs needs security model\nvalid options are:" | |
1106 | "\tsecurity_model=[passthrough|mapped|none]\n"); | |
1107 | return -1; | |
1108 | } | |
1109 | ||
1110 | if (!strcmp(sec_model, "passthrough")) { | |
1111 | fse->export_flags |= V9FS_SM_PASSTHROUGH; | |
2c30dd74 AK |
1112 | } else if (!strcmp(sec_model, "mapped") || |
1113 | !strcmp(sec_model, "mapped-xattr")) { | |
99519f0a AK |
1114 | fse->export_flags |= V9FS_SM_MAPPED; |
1115 | } else if (!strcmp(sec_model, "none")) { | |
1116 | fse->export_flags |= V9FS_SM_NONE; | |
2c30dd74 AK |
1117 | } else if (!strcmp(sec_model, "mapped-file")) { |
1118 | fse->export_flags |= V9FS_SM_MAPPED_FILE; | |
99519f0a AK |
1119 | } else { |
1120 | fprintf(stderr, "Invalid security model %s specified, valid options are" | |
2c30dd74 AK |
1121 | "\n\t [passthrough|mapped-xattr|mapped-file|none]\n", |
1122 | sec_model); | |
99519f0a AK |
1123 | return -1; |
1124 | } | |
1125 | ||
1126 | if (!path) { | |
1127 | fprintf(stderr, "fsdev: No path specified.\n"); | |
1128 | return -1; | |
1129 | } | |
1130 | fse->path = g_strdup(path); | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
9f107513 | 1135 | FileOperations local_ops = { |
99519f0a | 1136 | .parse_opts = local_parse_opts, |
0174fe73 | 1137 | .init = local_init, |
131dcb25 | 1138 | .lstat = local_lstat, |
131dcb25 AL |
1139 | .readlink = local_readlink, |
1140 | .close = local_close, | |
1141 | .closedir = local_closedir, | |
a6568fe2 AL |
1142 | .open = local_open, |
1143 | .opendir = local_opendir, | |
a9231555 AL |
1144 | .rewinddir = local_rewinddir, |
1145 | .telldir = local_telldir, | |
5f524c1e | 1146 | .readdir_r = local_readdir_r, |
a9231555 | 1147 | .seekdir = local_seekdir, |
56d15a53 SG |
1148 | .preadv = local_preadv, |
1149 | .pwritev = local_pwritev, | |
c494dd6f AL |
1150 | .chmod = local_chmod, |
1151 | .mknod = local_mknod, | |
c494dd6f AL |
1152 | .mkdir = local_mkdir, |
1153 | .fstat = local_fstat, | |
1154 | .open2 = local_open2, | |
1155 | .symlink = local_symlink, | |
1156 | .link = local_link, | |
8cf89e00 AL |
1157 | .truncate = local_truncate, |
1158 | .rename = local_rename, | |
1159 | .chown = local_chown, | |
74bc02b2 | 1160 | .utimensat = local_utimensat, |
5bae1900 | 1161 | .remove = local_remove, |
8cf89e00 | 1162 | .fsync = local_fsync, |
be940c87 | 1163 | .statfs = local_statfs, |
fa32ef88 AK |
1164 | .lgetxattr = local_lgetxattr, |
1165 | .llistxattr = local_llistxattr, | |
10b468bd | 1166 | .lsetxattr = local_lsetxattr, |
9ed3ef26 | 1167 | .lremovexattr = local_lremovexattr, |
2289be19 AK |
1168 | .name_to_path = local_name_to_path, |
1169 | .renameat = local_renameat, | |
1170 | .unlinkat = local_unlinkat, | |
9f107513 | 1171 | }; |