Statistics
| Branch: | Revision:

root / hw / virtio-9p-local.c @ 758e8e38

History | View | Annotate | Download (6.3 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 *ctx, const char *path, mode_t mode)
133
{
134
    return chmod(rpath(ctx, path), mode);
135
}
136

    
137
static int local_mknod(FsContext *ctx, const char *path, mode_t mode, dev_t dev)
138
{
139
    return mknod(rpath(ctx, path), mode, dev);
140
}
141

    
142
static int local_mksock(FsContext *ctx2, const char *path)
143
{
144
    struct sockaddr_un addr;
145
    int s;
146

    
147
    addr.sun_family = AF_UNIX;
148
    snprintf(addr.sun_path, 108, "%s", rpath(ctx2, path));
149

    
150
    s = socket(PF_UNIX, SOCK_STREAM, 0);
151
    if (s == -1) {
152
        return -1;
153
    }
154

    
155
    if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
156
        close(s);
157
        return -1;
158
    }
159

    
160
    close(s);
161
    return 0;
162
}
163

    
164
static int local_mkdir(FsContext *ctx, const char *path, mode_t mode)
165
{
166
    return mkdir(rpath(ctx, path), mode);
167
}
168

    
169
static int local_fstat(FsContext *ctx, int fd, struct stat *stbuf)
170
{
171
    return fstat(fd, stbuf);
172
}
173

    
174
static int local_open2(FsContext *ctx, const char *path, int flags, mode_t mode)
175
{
176
    return open(rpath(ctx, path), flags, mode);
177
}
178

    
179

    
180
static int local_symlink(FsContext *ctx, const char *oldpath,
181
                            const char *newpath)
182
{
183
    return symlink(oldpath, rpath(ctx, newpath));
184
}
185

    
186
static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
187
{
188
    char *tmp = qemu_strdup(rpath(ctx, oldpath));
189
    int err, serrno = 0;
190

    
191
    if (tmp == NULL) {
192
        return -ENOMEM;
193
    }
194

    
195
    err = link(tmp, rpath(ctx, newpath));
196
    if (err == -1) {
197
        serrno = errno;
198
    }
199

    
200
    qemu_free(tmp);
201

    
202
    if (err == -1) {
203
        errno = serrno;
204
    }
205

    
206
    return err;
207
}
208

    
209
static int local_truncate(FsContext *ctx, const char *path, off_t size)
210
{
211
    return truncate(rpath(ctx, path), size);
212
}
213

    
214
static int local_rename(FsContext *ctx, const char *oldpath,
215
                        const char *newpath)
216
{
217
    char *tmp;
218
    int err;
219

    
220
    tmp = qemu_strdup(rpath(ctx, oldpath));
221
    if (tmp == NULL) {
222
        return -1;
223
    }
224

    
225
    err = rename(tmp, rpath(ctx, newpath));
226
    if (err == -1) {
227
        int serrno = errno;
228
        qemu_free(tmp);
229
        errno = serrno;
230
    } else {
231
        qemu_free(tmp);
232
    }
233

    
234
    return err;
235

    
236
}
237

    
238
static int local_chown(FsContext *ctx, const char *path, uid_t uid, gid_t gid)
239
{
240
    return chown(rpath(ctx, path), uid, gid);
241
}
242

    
243
static int local_utime(FsContext *ctx, const char *path,
244
                        const struct utimbuf *buf)
245
{
246
    return utime(rpath(ctx, path), buf);
247
}
248

    
249
static int local_remove(FsContext *ctx, const char *path)
250
{
251
    return remove(rpath(ctx, path));
252
}
253

    
254
static int local_fsync(FsContext *ctx, int fd)
255
{
256
    if (0) /* Just to supress the warning. Will be removed in next patch. */
257
        (void)local_set_xattr(NULL, NULL);
258
    return fsync(fd);
259
}
260

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