2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "virtio-9p.h"
15 #include <arpa/inet.h>
18 #include <sys/socket.h>
20 #include <attr/xattr.h>
22 static const char *rpath(FsContext *ctx, const char *path)
24 /* FIXME: so wrong... */
25 static char buffer[4096];
26 snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path);
31 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
34 err = lstat(rpath(fs_ctx, path), stbuf);
38 if (fs_ctx->fs_sm == SM_MAPPED) {
39 /* Actual credentials are part of extended attrs */
44 if (getxattr(rpath(fs_ctx, path), "user.virtfs.uid", &tmp_uid,
46 stbuf->st_uid = tmp_uid;
48 if (getxattr(rpath(fs_ctx, path), "user.virtfs.gid", &tmp_gid,
50 stbuf->st_gid = tmp_gid;
52 if (getxattr(rpath(fs_ctx, path), "user.virtfs.mode", &tmp_mode,
53 sizeof(mode_t)) > 0) {
54 stbuf->st_mode = tmp_mode;
56 if (getxattr(rpath(fs_ctx, path), "user.virtfs.rdev", &tmp_dev,
58 stbuf->st_rdev = tmp_dev;
64 static int local_set_xattr(const char *path, FsCred *credp)
67 if (credp->fc_uid != -1) {
68 err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
74 if (credp->fc_gid != -1) {
75 err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
81 if (credp->fc_mode != -1) {
82 err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
88 if (credp->fc_rdev != -1) {
89 err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
98 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
101 if (chmod(rpath(fs_ctx, path), credp->fc_mode & 07777) < 0) {
104 if (chown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid) < 0) {
110 static ssize_t local_readlink(FsContext *ctx, const char *path,
111 char *buf, size_t bufsz)
113 return readlink(rpath(ctx, path), buf, bufsz);
116 static int local_close(FsContext *ctx, int fd)
121 static int local_closedir(FsContext *ctx, DIR *dir)
123 return closedir(dir);
126 static int local_open(FsContext *ctx, const char *path, int flags)
128 return open(rpath(ctx, path), flags);
131 static DIR *local_opendir(FsContext *ctx, const char *path)
133 return opendir(rpath(ctx, path));
136 static void local_rewinddir(FsContext *ctx, DIR *dir)
138 return rewinddir(dir);
141 static off_t local_telldir(FsContext *ctx, DIR *dir)
146 static struct dirent *local_readdir(FsContext *ctx, DIR *dir)
151 static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
153 return seekdir(dir, off);
156 static ssize_t local_readv(FsContext *ctx, int fd, const struct iovec *iov,
159 return readv(fd, iov, iovcnt);
162 static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence)
164 return lseek(fd, offset, whence);
167 static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov,
170 return writev(fd, iov, iovcnt);
173 static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
175 if (fs_ctx->fs_sm == SM_MAPPED) {
176 return local_set_xattr(rpath(fs_ctx, path), credp);
177 } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
178 return chmod(rpath(fs_ctx, path), credp->fc_mode);
183 static int local_mknod(FsContext *ctx, const char *path, mode_t mode, dev_t dev)
185 return mknod(rpath(ctx, path), mode, dev);
188 static int local_mksock(FsContext *ctx2, const char *path)
190 struct sockaddr_un addr;
193 addr.sun_family = AF_UNIX;
194 snprintf(addr.sun_path, 108, "%s", rpath(ctx2, path));
196 s = socket(PF_UNIX, SOCK_STREAM, 0);
201 if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
210 static int local_mkdir(FsContext *ctx, const char *path, mode_t mode)
212 return mkdir(rpath(ctx, path), mode);
215 static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf)
218 err = fstat(fd, stbuf);
222 if (fs_ctx->fs_sm == SM_MAPPED) {
223 /* Actual credentials are part of extended attrs */
229 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
230 stbuf->st_uid = tmp_uid;
232 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
233 stbuf->st_gid = tmp_gid;
235 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
236 stbuf->st_mode = tmp_mode;
238 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
239 stbuf->st_rdev = tmp_dev;
245 static int local_open2(FsContext *fs_ctx, const char *path, int flags,
252 /* Determine the security model */
253 if (fs_ctx->fs_sm == SM_MAPPED) {
254 fd = open(rpath(fs_ctx, path), flags, SM_LOCAL_MODE_BITS);
258 credp->fc_mode = credp->fc_mode|S_IFREG;
259 /* Set cleint credentials in xattr */
260 err = local_set_xattr(rpath(fs_ctx, path), credp);
265 } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
266 fd = open(rpath(fs_ctx, path), flags, credp->fc_mode);
270 err = local_post_create_passthrough(fs_ctx, path, credp);
280 remove(rpath(fs_ctx, path));
286 static int local_symlink(FsContext *ctx, const char *oldpath,
289 return symlink(oldpath, rpath(ctx, newpath));
292 static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
294 char *tmp = qemu_strdup(rpath(ctx, oldpath));
301 err = link(tmp, rpath(ctx, newpath));
315 static int local_truncate(FsContext *ctx, const char *path, off_t size)
317 return truncate(rpath(ctx, path), size);
320 static int local_rename(FsContext *ctx, const char *oldpath,
326 tmp = qemu_strdup(rpath(ctx, oldpath));
331 err = rename(tmp, rpath(ctx, newpath));
344 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
346 if (fs_ctx->fs_sm == SM_MAPPED) {
347 return local_set_xattr(rpath(fs_ctx, path), credp);
348 } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
349 return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
354 static int local_utime(FsContext *ctx, const char *path,
355 const struct utimbuf *buf)
357 return utime(rpath(ctx, path), buf);
360 static int local_remove(FsContext *ctx, const char *path)
362 return remove(rpath(ctx, path));
365 static int local_fsync(FsContext *ctx, int fd)
370 FileOperations local_ops = {
371 .lstat = local_lstat,
372 .readlink = local_readlink,
373 .close = local_close,
374 .closedir = local_closedir,
376 .opendir = local_opendir,
377 .rewinddir = local_rewinddir,
378 .telldir = local_telldir,
379 .readdir = local_readdir,
380 .seekdir = local_seekdir,
381 .readv = local_readv,
382 .lseek = local_lseek,
383 .writev = local_writev,
384 .chmod = local_chmod,
385 .mknod = local_mknod,
386 .mksock = local_mksock,
387 .mkdir = local_mkdir,
388 .fstat = local_fstat,
389 .open2 = local_open2,
390 .symlink = local_symlink,
392 .truncate = local_truncate,
393 .rename = local_rename,
394 .chown = local_chown,
395 .utime = local_utime,
396 .remove = local_remove,
397 .fsync = local_fsync,