root / hw / 9pfs / virtio-9p-local.c @ 873c3213
History | View | Annotate | Download (14.4 kB)
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 |
*/
|
13 |
|
14 |
#include "hw/virtio.h" |
15 |
#include "virtio-9p.h" |
16 |
#include "virtio-9p-xattr.h" |
17 |
#include <arpa/inet.h> |
18 |
#include <pwd.h> |
19 |
#include <grp.h> |
20 |
#include <sys/socket.h> |
21 |
#include <sys/un.h> |
22 |
#include <attr/xattr.h> |
23 |
|
24 |
|
25 |
static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf) |
26 |
{ |
27 |
int err;
|
28 |
err = lstat(rpath(fs_ctx, path), stbuf); |
29 |
if (err) {
|
30 |
return err;
|
31 |
} |
32 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
33 |
/* Actual credentials are part of extended attrs */
|
34 |
uid_t tmp_uid; |
35 |
gid_t tmp_gid; |
36 |
mode_t tmp_mode; |
37 |
dev_t tmp_dev; |
38 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.uid", &tmp_uid, |
39 |
sizeof(uid_t)) > 0) { |
40 |
stbuf->st_uid = tmp_uid; |
41 |
} |
42 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.gid", &tmp_gid, |
43 |
sizeof(gid_t)) > 0) { |
44 |
stbuf->st_gid = tmp_gid; |
45 |
} |
46 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.mode", &tmp_mode, |
47 |
sizeof(mode_t)) > 0) { |
48 |
stbuf->st_mode = tmp_mode; |
49 |
} |
50 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.rdev", &tmp_dev, |
51 |
sizeof(dev_t)) > 0) { |
52 |
stbuf->st_rdev = tmp_dev; |
53 |
} |
54 |
} |
55 |
return err;
|
56 |
} |
57 |
|
58 |
static int local_set_xattr(const char *path, FsCred *credp) |
59 |
{ |
60 |
int err;
|
61 |
if (credp->fc_uid != -1) { |
62 |
err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t), |
63 |
0);
|
64 |
if (err) {
|
65 |
return err;
|
66 |
} |
67 |
} |
68 |
if (credp->fc_gid != -1) { |
69 |
err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t), |
70 |
0);
|
71 |
if (err) {
|
72 |
return err;
|
73 |
} |
74 |
} |
75 |
if (credp->fc_mode != -1) { |
76 |
err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
|
77 |
sizeof(mode_t), 0); |
78 |
if (err) {
|
79 |
return err;
|
80 |
} |
81 |
} |
82 |
if (credp->fc_rdev != -1) { |
83 |
err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
|
84 |
sizeof(dev_t), 0); |
85 |
if (err) {
|
86 |
return err;
|
87 |
} |
88 |
} |
89 |
return 0; |
90 |
} |
91 |
|
92 |
static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, |
93 |
FsCred *credp) |
94 |
{ |
95 |
if (chmod(rpath(fs_ctx, path), credp->fc_mode & 07777) < 0) { |
96 |
return -1; |
97 |
} |
98 |
if (lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid) < 0) { |
99 |
/*
|
100 |
* If we fail to change ownership and if we are
|
101 |
* using security model none. Ignore the error
|
102 |
*/
|
103 |
if (fs_ctx->fs_sm != SM_NONE) {
|
104 |
return -1; |
105 |
} |
106 |
} |
107 |
return 0; |
108 |
} |
109 |
|
110 |
static ssize_t local_readlink(FsContext *fs_ctx, const char *path, |
111 |
char *buf, size_t bufsz)
|
112 |
{ |
113 |
ssize_t tsize = -1;
|
114 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
115 |
int fd;
|
116 |
fd = open(rpath(fs_ctx, path), O_RDONLY); |
117 |
if (fd == -1) { |
118 |
return -1; |
119 |
} |
120 |
do {
|
121 |
tsize = read(fd, (void *)buf, bufsz);
|
122 |
} while (tsize == -1 && errno == EINTR); |
123 |
close(fd); |
124 |
return tsize;
|
125 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
126 |
(fs_ctx->fs_sm == SM_NONE)) { |
127 |
tsize = readlink(rpath(fs_ctx, path), buf, bufsz); |
128 |
} |
129 |
return tsize;
|
130 |
} |
131 |
|
132 |
static int local_close(FsContext *ctx, int fd) |
133 |
{ |
134 |
return close(fd);
|
135 |
} |
136 |
|
137 |
static int local_closedir(FsContext *ctx, DIR *dir) |
138 |
{ |
139 |
return closedir(dir);
|
140 |
} |
141 |
|
142 |
static int local_open(FsContext *ctx, const char *path, int flags) |
143 |
{ |
144 |
return open(rpath(ctx, path), flags);
|
145 |
} |
146 |
|
147 |
static DIR *local_opendir(FsContext *ctx, const char *path) |
148 |
{ |
149 |
return opendir(rpath(ctx, path));
|
150 |
} |
151 |
|
152 |
static void local_rewinddir(FsContext *ctx, DIR *dir) |
153 |
{ |
154 |
return rewinddir(dir);
|
155 |
} |
156 |
|
157 |
static off_t local_telldir(FsContext *ctx, DIR *dir)
|
158 |
{ |
159 |
return telldir(dir);
|
160 |
} |
161 |
|
162 |
static struct dirent *local_readdir(FsContext *ctx, DIR *dir) |
163 |
{ |
164 |
return readdir(dir);
|
165 |
} |
166 |
|
167 |
static void local_seekdir(FsContext *ctx, DIR *dir, off_t off) |
168 |
{ |
169 |
return seekdir(dir, off);
|
170 |
} |
171 |
|
172 |
static ssize_t local_preadv(FsContext *ctx, int fd, const struct iovec *iov, |
173 |
int iovcnt, off_t offset)
|
174 |
{ |
175 |
#ifdef CONFIG_PREADV
|
176 |
return preadv(fd, iov, iovcnt, offset);
|
177 |
#else
|
178 |
int err = lseek(fd, offset, SEEK_SET);
|
179 |
if (err == -1) { |
180 |
return err;
|
181 |
} else {
|
182 |
return readv(fd, iov, iovcnt);
|
183 |
} |
184 |
#endif
|
185 |
} |
186 |
|
187 |
static ssize_t local_pwritev(FsContext *ctx, int fd, const struct iovec *iov, |
188 |
int iovcnt, off_t offset)
|
189 |
{ |
190 |
#ifdef CONFIG_PREADV
|
191 |
return pwritev(fd, iov, iovcnt, offset);
|
192 |
#else
|
193 |
int err = lseek(fd, offset, SEEK_SET);
|
194 |
if (err == -1) { |
195 |
return err;
|
196 |
} else {
|
197 |
return writev(fd, iov, iovcnt);
|
198 |
} |
199 |
#endif
|
200 |
} |
201 |
|
202 |
static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp) |
203 |
{ |
204 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
205 |
return local_set_xattr(rpath(fs_ctx, path), credp);
|
206 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
207 |
(fs_ctx->fs_sm == SM_NONE)) { |
208 |
return chmod(rpath(fs_ctx, path), credp->fc_mode);
|
209 |
} |
210 |
return -1; |
211 |
} |
212 |
|
213 |
static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp) |
214 |
{ |
215 |
int err = -1; |
216 |
int serrno = 0; |
217 |
|
218 |
/* Determine the security model */
|
219 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
220 |
err = mknod(rpath(fs_ctx, path), SM_LOCAL_MODE_BITS|S_IFREG, 0);
|
221 |
if (err == -1) { |
222 |
return err;
|
223 |
} |
224 |
local_set_xattr(rpath(fs_ctx, path), credp); |
225 |
if (err == -1) { |
226 |
serrno = errno; |
227 |
goto err_end;
|
228 |
} |
229 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
230 |
(fs_ctx->fs_sm == SM_NONE)) { |
231 |
err = mknod(rpath(fs_ctx, path), credp->fc_mode, credp->fc_rdev); |
232 |
if (err == -1) { |
233 |
return err;
|
234 |
} |
235 |
err = local_post_create_passthrough(fs_ctx, path, credp); |
236 |
if (err == -1) { |
237 |
serrno = errno; |
238 |
goto err_end;
|
239 |
} |
240 |
} |
241 |
return err;
|
242 |
|
243 |
err_end:
|
244 |
remove(rpath(fs_ctx, path)); |
245 |
errno = serrno; |
246 |
return err;
|
247 |
} |
248 |
|
249 |
static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp) |
250 |
{ |
251 |
int err = -1; |
252 |
int serrno = 0; |
253 |
|
254 |
/* Determine the security model */
|
255 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
256 |
err = mkdir(rpath(fs_ctx, path), SM_LOCAL_DIR_MODE_BITS); |
257 |
if (err == -1) { |
258 |
return err;
|
259 |
} |
260 |
credp->fc_mode = credp->fc_mode|S_IFDIR; |
261 |
err = local_set_xattr(rpath(fs_ctx, path), credp); |
262 |
if (err == -1) { |
263 |
serrno = errno; |
264 |
goto err_end;
|
265 |
} |
266 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
267 |
(fs_ctx->fs_sm == SM_NONE)) { |
268 |
err = mkdir(rpath(fs_ctx, path), credp->fc_mode); |
269 |
if (err == -1) { |
270 |
return err;
|
271 |
} |
272 |
err = local_post_create_passthrough(fs_ctx, path, credp); |
273 |
if (err == -1) { |
274 |
serrno = errno; |
275 |
goto err_end;
|
276 |
} |
277 |
} |
278 |
return err;
|
279 |
|
280 |
err_end:
|
281 |
remove(rpath(fs_ctx, path)); |
282 |
errno = serrno; |
283 |
return err;
|
284 |
} |
285 |
|
286 |
static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf) |
287 |
{ |
288 |
int err;
|
289 |
err = fstat(fd, stbuf); |
290 |
if (err) {
|
291 |
return err;
|
292 |
} |
293 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
294 |
/* Actual credentials are part of extended attrs */
|
295 |
uid_t tmp_uid; |
296 |
gid_t tmp_gid; |
297 |
mode_t tmp_mode; |
298 |
dev_t tmp_dev; |
299 |
|
300 |
if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { |
301 |
stbuf->st_uid = tmp_uid; |
302 |
} |
303 |
if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { |
304 |
stbuf->st_gid = tmp_gid; |
305 |
} |
306 |
if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { |
307 |
stbuf->st_mode = tmp_mode; |
308 |
} |
309 |
if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { |
310 |
stbuf->st_rdev = tmp_dev; |
311 |
} |
312 |
} |
313 |
return err;
|
314 |
} |
315 |
|
316 |
static int local_open2(FsContext *fs_ctx, const char *path, int flags, |
317 |
FsCred *credp) |
318 |
{ |
319 |
int fd = -1; |
320 |
int err = -1; |
321 |
int serrno = 0; |
322 |
|
323 |
/* Determine the security model */
|
324 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
325 |
fd = open(rpath(fs_ctx, path), flags, SM_LOCAL_MODE_BITS); |
326 |
if (fd == -1) { |
327 |
return fd;
|
328 |
} |
329 |
credp->fc_mode = credp->fc_mode|S_IFREG; |
330 |
/* Set cleint credentials in xattr */
|
331 |
err = local_set_xattr(rpath(fs_ctx, path), credp); |
332 |
if (err == -1) { |
333 |
serrno = errno; |
334 |
goto err_end;
|
335 |
} |
336 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
337 |
(fs_ctx->fs_sm == SM_NONE)) { |
338 |
fd = open(rpath(fs_ctx, path), flags, credp->fc_mode); |
339 |
if (fd == -1) { |
340 |
return fd;
|
341 |
} |
342 |
err = local_post_create_passthrough(fs_ctx, path, credp); |
343 |
if (err == -1) { |
344 |
serrno = errno; |
345 |
goto err_end;
|
346 |
} |
347 |
} |
348 |
return fd;
|
349 |
|
350 |
err_end:
|
351 |
close(fd); |
352 |
remove(rpath(fs_ctx, path)); |
353 |
errno = serrno; |
354 |
return err;
|
355 |
} |
356 |
|
357 |
|
358 |
static int local_symlink(FsContext *fs_ctx, const char *oldpath, |
359 |
const char *newpath, FsCred *credp) |
360 |
{ |
361 |
int err = -1; |
362 |
int serrno = 0; |
363 |
|
364 |
/* Determine the security model */
|
365 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
366 |
int fd;
|
367 |
ssize_t oldpath_size, write_size; |
368 |
fd = open(rpath(fs_ctx, newpath), O_CREAT|O_EXCL|O_RDWR, |
369 |
SM_LOCAL_MODE_BITS); |
370 |
if (fd == -1) { |
371 |
return fd;
|
372 |
} |
373 |
/* Write the oldpath (target) to the file. */
|
374 |
oldpath_size = strlen(oldpath); |
375 |
do {
|
376 |
write_size = write(fd, (void *)oldpath, oldpath_size);
|
377 |
} while (write_size == -1 && errno == EINTR); |
378 |
|
379 |
if (write_size != oldpath_size) {
|
380 |
serrno = errno; |
381 |
close(fd); |
382 |
err = -1;
|
383 |
goto err_end;
|
384 |
} |
385 |
close(fd); |
386 |
/* Set cleint credentials in symlink's xattr */
|
387 |
credp->fc_mode = credp->fc_mode|S_IFLNK; |
388 |
err = local_set_xattr(rpath(fs_ctx, newpath), credp); |
389 |
if (err == -1) { |
390 |
serrno = errno; |
391 |
goto err_end;
|
392 |
} |
393 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
394 |
(fs_ctx->fs_sm == SM_NONE)) { |
395 |
err = symlink(oldpath, rpath(fs_ctx, newpath)); |
396 |
if (err) {
|
397 |
return err;
|
398 |
} |
399 |
err = lchown(rpath(fs_ctx, newpath), credp->fc_uid, credp->fc_gid); |
400 |
if (err == -1) { |
401 |
/*
|
402 |
* If we fail to change ownership and if we are
|
403 |
* using security model none. Ignore the error
|
404 |
*/
|
405 |
if (fs_ctx->fs_sm != SM_NONE) {
|
406 |
serrno = errno; |
407 |
goto err_end;
|
408 |
} else
|
409 |
err = 0;
|
410 |
} |
411 |
} |
412 |
return err;
|
413 |
|
414 |
err_end:
|
415 |
remove(rpath(fs_ctx, newpath)); |
416 |
errno = serrno; |
417 |
return err;
|
418 |
} |
419 |
|
420 |
static int local_link(FsContext *ctx, const char *oldpath, const char *newpath) |
421 |
{ |
422 |
char *tmp = qemu_strdup(rpath(ctx, oldpath));
|
423 |
int err, serrno = 0; |
424 |
|
425 |
if (tmp == NULL) { |
426 |
return -ENOMEM;
|
427 |
} |
428 |
|
429 |
err = link(tmp, rpath(ctx, newpath)); |
430 |
if (err == -1) { |
431 |
serrno = errno; |
432 |
} |
433 |
|
434 |
qemu_free(tmp); |
435 |
|
436 |
if (err == -1) { |
437 |
errno = serrno; |
438 |
} |
439 |
|
440 |
return err;
|
441 |
} |
442 |
|
443 |
static int local_truncate(FsContext *ctx, const char *path, off_t size) |
444 |
{ |
445 |
return truncate(rpath(ctx, path), size);
|
446 |
} |
447 |
|
448 |
static int local_rename(FsContext *ctx, const char *oldpath, |
449 |
const char *newpath) |
450 |
{ |
451 |
char *tmp;
|
452 |
int err;
|
453 |
|
454 |
tmp = qemu_strdup(rpath(ctx, oldpath)); |
455 |
|
456 |
err = rename(tmp, rpath(ctx, newpath)); |
457 |
if (err == -1) { |
458 |
int serrno = errno;
|
459 |
qemu_free(tmp); |
460 |
errno = serrno; |
461 |
} else {
|
462 |
qemu_free(tmp); |
463 |
} |
464 |
|
465 |
return err;
|
466 |
|
467 |
} |
468 |
|
469 |
static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp) |
470 |
{ |
471 |
if ((credp->fc_uid == -1 && credp->fc_gid == -1) || |
472 |
(fs_ctx->fs_sm == SM_PASSTHROUGH)) { |
473 |
return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
|
474 |
} else if (fs_ctx->fs_sm == SM_MAPPED) { |
475 |
return local_set_xattr(rpath(fs_ctx, path), credp);
|
476 |
} else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) || |
477 |
(fs_ctx->fs_sm == SM_NONE)) { |
478 |
return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
|
479 |
} |
480 |
return -1; |
481 |
} |
482 |
|
483 |
static int local_utimensat(FsContext *s, const char *path, |
484 |
const struct timespec *buf) |
485 |
{ |
486 |
return qemu_utimensat(AT_FDCWD, rpath(s, path), buf, AT_SYMLINK_NOFOLLOW);
|
487 |
} |
488 |
|
489 |
static int local_remove(FsContext *ctx, const char *path) |
490 |
{ |
491 |
return remove(rpath(ctx, path));
|
492 |
} |
493 |
|
494 |
static int local_fsync(FsContext *ctx, int fd, int datasync) |
495 |
{ |
496 |
if (datasync) {
|
497 |
return qemu_fdatasync(fd);
|
498 |
} else {
|
499 |
return fsync(fd);
|
500 |
} |
501 |
} |
502 |
|
503 |
static int local_statfs(FsContext *s, const char *path, struct statfs *stbuf) |
504 |
{ |
505 |
return statfs(rpath(s, path), stbuf);
|
506 |
} |
507 |
|
508 |
static ssize_t local_lgetxattr(FsContext *ctx, const char *path, |
509 |
const char *name, void *value, size_t size) |
510 |
{ |
511 |
return v9fs_get_xattr(ctx, path, name, value, size);
|
512 |
} |
513 |
|
514 |
static ssize_t local_llistxattr(FsContext *ctx, const char *path, |
515 |
void *value, size_t size)
|
516 |
{ |
517 |
return v9fs_list_xattr(ctx, path, value, size);
|
518 |
} |
519 |
|
520 |
static int local_lsetxattr(FsContext *ctx, const char *path, const char *name, |
521 |
void *value, size_t size, int flags) |
522 |
{ |
523 |
return v9fs_set_xattr(ctx, path, name, value, size, flags);
|
524 |
} |
525 |
|
526 |
static int local_lremovexattr(FsContext *ctx, |
527 |
const char *path, const char *name) |
528 |
{ |
529 |
return v9fs_remove_xattr(ctx, path, name);
|
530 |
} |
531 |
|
532 |
|
533 |
FileOperations local_ops = { |
534 |
.lstat = local_lstat, |
535 |
.readlink = local_readlink, |
536 |
.close = local_close, |
537 |
.closedir = local_closedir, |
538 |
.open = local_open, |
539 |
.opendir = local_opendir, |
540 |
.rewinddir = local_rewinddir, |
541 |
.telldir = local_telldir, |
542 |
.readdir = local_readdir, |
543 |
.seekdir = local_seekdir, |
544 |
.preadv = local_preadv, |
545 |
.pwritev = local_pwritev, |
546 |
.chmod = local_chmod, |
547 |
.mknod = local_mknod, |
548 |
.mkdir = local_mkdir, |
549 |
.fstat = local_fstat, |
550 |
.open2 = local_open2, |
551 |
.symlink = local_symlink, |
552 |
.link = local_link, |
553 |
.truncate = local_truncate, |
554 |
.rename = local_rename, |
555 |
.chown = local_chown, |
556 |
.utimensat = local_utimensat, |
557 |
.remove = local_remove, |
558 |
.fsync = local_fsync, |
559 |
.statfs = local_statfs, |
560 |
.lgetxattr = local_lgetxattr, |
561 |
.llistxattr = local_llistxattr, |
562 |
.lsetxattr = local_lsetxattr, |
563 |
.lremovexattr = local_lremovexattr, |
564 |
}; |