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