Statistics
| Branch: | Revision:

root / hw / virtio-9p-local.c @ e95ead32

History | View | Annotate | Download (6.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
#include "virtio.h"
14
#include "virtio-9p.h"
15
#include <arpa/inet.h>
16
#include <pwd.h>
17
#include <grp.h>
18
#include <sys/socket.h>
19
#include <sys/un.h>
20
#include <attr/xattr.h>
21

    
22
static const char *rpath(FsContext *ctx, const char *path)
23
{
24
    /* FIXME: so wrong... */
25
    static char buffer[4096];
26
    snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path);
27
    return buffer;
28
}
29

    
30
static int local_lstat(FsContext *ctx, const char *path, struct stat *stbuf)
31
{
32
    return lstat(rpath(ctx, path), stbuf);
33
}
34

    
35
static int local_set_xattr(const char *path, FsCred *credp)
36
{
37
    int err;
38
    if (credp->fc_uid != -1) {
39
        err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
40
                0);
41
        if (err) {
42
            return err;
43
        }
44
    }
45
    if (credp->fc_gid != -1) {
46
        err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
47
                0);
48
        if (err) {
49
            return err;
50
        }
51
    }
52
    if (credp->fc_mode != -1) {
53
        err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
54
                sizeof(mode_t), 0);
55
        if (err) {
56
            return err;
57
        }
58
    }
59
    if (credp->fc_rdev != -1) {
60
        err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
61
                sizeof(dev_t), 0);
62
        if (err) {
63
            return err;
64
        }
65
    }
66
    return 0;
67
}
68

    
69
static ssize_t local_readlink(FsContext *ctx, const char *path,
70
                                char *buf, size_t bufsz)
71
{
72
    return readlink(rpath(ctx, path), buf, bufsz);
73
}
74

    
75
static int local_close(FsContext *ctx, int fd)
76
{
77
    return close(fd);
78
}
79

    
80
static int local_closedir(FsContext *ctx, DIR *dir)
81
{
82
    return closedir(dir);
83
}
84

    
85
static int local_open(FsContext *ctx, const char *path, int flags)
86
{
87
    return open(rpath(ctx, path), flags);
88
}
89

    
90
static DIR *local_opendir(FsContext *ctx, const char *path)
91
{
92
    return opendir(rpath(ctx, path));
93
}
94

    
95
static void local_rewinddir(FsContext *ctx, DIR *dir)
96
{
97
    return rewinddir(dir);
98
}
99

    
100
static off_t local_telldir(FsContext *ctx, DIR *dir)
101
{
102
    return telldir(dir);
103
}
104

    
105
static struct dirent *local_readdir(FsContext *ctx, DIR *dir)
106
{
107
    return readdir(dir);
108
}
109

    
110
static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
111
{
112
    return seekdir(dir, off);
113
}
114

    
115
static ssize_t local_readv(FsContext *ctx, int fd, const struct iovec *iov,
116
                            int iovcnt)
117
{
118
    return readv(fd, iov, iovcnt);
119
}
120

    
121
static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence)
122
{
123
    return lseek(fd, offset, whence);
124
}
125

    
126
static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov,
127
                            int iovcnt)
128
{
129
    return writev(fd, iov, iovcnt);
130
}
131

    
132
static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
133
{
134
    if (fs_ctx->fs_sm == SM_MAPPED) {
135
        return local_set_xattr(rpath(fs_ctx, path), credp);
136
    } else if (fs_ctx->fs_sm == SM_PASSTHROUGH) {
137
        return chmod(rpath(fs_ctx, path), credp->fc_mode);
138
    }
139
    return -1;
140
}
141

    
142
static int local_mknod(FsContext *ctx, const char *path, mode_t mode, dev_t dev)
143
{
144
    return mknod(rpath(ctx, path), mode, dev);
145
}
146

    
147
static int local_mksock(FsContext *ctx2, const char *path)
148
{
149
    struct sockaddr_un addr;
150
    int s;
151

    
152
    addr.sun_family = AF_UNIX;
153
    snprintf(addr.sun_path, 108, "%s", rpath(ctx2, path));
154

    
155
    s = socket(PF_UNIX, SOCK_STREAM, 0);
156
    if (s == -1) {
157
        return -1;
158
    }
159

    
160
    if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
161
        close(s);
162
        return -1;
163
    }
164

    
165
    close(s);
166
    return 0;
167
}
168

    
169
static int local_mkdir(FsContext *ctx, const char *path, mode_t mode)
170
{
171
    return mkdir(rpath(ctx, path), mode);
172
}
173

    
174
static int local_fstat(FsContext *ctx, int fd, struct stat *stbuf)
175
{
176
    return fstat(fd, stbuf);
177
}
178

    
179
static int local_open2(FsContext *ctx, const char *path, int flags, mode_t mode)
180
{
181
    return open(rpath(ctx, path), flags, mode);
182
}
183

    
184

    
185
static int local_symlink(FsContext *ctx, const char *oldpath,
186
                            const char *newpath)
187
{
188
    return symlink(oldpath, rpath(ctx, newpath));
189
}
190

    
191
static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
192
{
193
    char *tmp = qemu_strdup(rpath(ctx, oldpath));
194
    int err, serrno = 0;
195

    
196
    if (tmp == NULL) {
197
        return -ENOMEM;
198
    }
199

    
200
    err = link(tmp, rpath(ctx, newpath));
201
    if (err == -1) {
202
        serrno = errno;
203
    }
204

    
205
    qemu_free(tmp);
206

    
207
    if (err == -1) {
208
        errno = serrno;
209
    }
210

    
211
    return err;
212
}
213

    
214
static int local_truncate(FsContext *ctx, const char *path, off_t size)
215
{
216
    return truncate(rpath(ctx, path), size);
217
}
218

    
219
static int local_rename(FsContext *ctx, const char *oldpath,
220
                        const char *newpath)
221
{
222
    char *tmp;
223
    int err;
224

    
225
    tmp = qemu_strdup(rpath(ctx, oldpath));
226
    if (tmp == NULL) {
227
        return -1;
228
    }
229

    
230
    err = rename(tmp, rpath(ctx, newpath));
231
    if (err == -1) {
232
        int serrno = errno;
233
        qemu_free(tmp);
234
        errno = serrno;
235
    } else {
236
        qemu_free(tmp);
237
    }
238

    
239
    return err;
240

    
241
}
242

    
243
static int local_chown(FsContext *ctx, const char *path, uid_t uid, gid_t gid)
244
{
245
    return chown(rpath(ctx, path), uid, gid);
246
}
247

    
248
static int local_utime(FsContext *ctx, const char *path,
249
                        const struct utimbuf *buf)
250
{
251
    return utime(rpath(ctx, path), buf);
252
}
253

    
254
static int local_remove(FsContext *ctx, const char *path)
255
{
256
    return remove(rpath(ctx, path));
257
}
258

    
259
static int local_fsync(FsContext *ctx, int fd)
260
{
261
    return fsync(fd);
262
}
263

    
264
FileOperations local_ops = {
265
    .lstat = local_lstat,
266
    .readlink = local_readlink,
267
    .close = local_close,
268
    .closedir = local_closedir,
269
    .open = local_open,
270
    .opendir = local_opendir,
271
    .rewinddir = local_rewinddir,
272
    .telldir = local_telldir,
273
    .readdir = local_readdir,
274
    .seekdir = local_seekdir,
275
    .readv = local_readv,
276
    .lseek = local_lseek,
277
    .writev = local_writev,
278
    .chmod = local_chmod,
279
    .mknod = local_mknod,
280
    .mksock = local_mksock,
281
    .mkdir = local_mkdir,
282
    .fstat = local_fstat,
283
    .open2 = local_open2,
284
    .symlink = local_symlink,
285
    .link = local_link,
286
    .truncate = local_truncate,
287
    .rename = local_rename,
288
    .chown = local_chown,
289
    .utime = local_utime,
290
    .remove = local_remove,
291
    .fsync = local_fsync,
292
};