cofs.c 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

/*
 * Virtio 9p backend
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include "fsdev/qemu-fsdev.h"
#include "qemu-thread.h"
#include "qemu-coroutine.h"
#include "virtio-9p-coth.h"

20
int v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
21 22 23
{
    int err;
    ssize_t len;
24
    V9fsState *s = pdu->s;
25

26 27 28
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
29
    buf->data = g_malloc(PATH_MAX);
30
    v9fs_path_read_lock(s);
31 32
    v9fs_co_run_in_worker(
        {
33
            len = s->ops->readlink(&s->ctx, path,
34 35 36 37 38 39 40 41 42
                                   buf->data, PATH_MAX - 1);
            if (len > -1) {
                buf->size = len;
                buf->data[len] = 0;
                err = 0;
            } else {
                err = -errno;
            }
        });
43
    v9fs_path_unlock(s);
44
    if (err) {
45
        g_free(buf->data);
46 47 48 49 50
        buf->data = NULL;
        buf->size = 0;
    }
    return err;
}
51

52
int v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path, struct statfs *stbuf)
53 54
{
    int err;
55
    V9fsState *s = pdu->s;
56

57 58 59
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
60
    v9fs_path_read_lock(s);
61 62
    v9fs_co_run_in_worker(
        {
63
            err = s->ops->statfs(&s->ctx, path, stbuf);
64 65 66 67
            if (err < 0) {
                err = -errno;
            }
        });
68
    v9fs_path_unlock(s);
69 70
    return err;
}
71

72
int v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
73 74 75
{
    int err;
    FsCred cred;
76
    V9fsState *s = pdu->s;
77

78 79 80
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
81 82
    cred_init(&cred);
    cred.fc_mode = mode;
83
    v9fs_path_read_lock(s);
84 85
    v9fs_co_run_in_worker(
        {
86
            err = s->ops->chmod(&s->ctx, path, &cred);
87 88 89 90
            if (err < 0) {
                err = -errno;
            }
        });
91
    v9fs_path_unlock(s);
92 93 94
    return err;
}

95
int v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
96 97 98
                      struct timespec times[2])
{
    int err;
99
    V9fsState *s = pdu->s;
100

101 102 103
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
104
    v9fs_path_read_lock(s);
105 106
    v9fs_co_run_in_worker(
        {
107
            err = s->ops->utimensat(&s->ctx, path, times);
108 109 110 111
            if (err < 0) {
                err = -errno;
            }
        });
112
    v9fs_path_unlock(s);
113 114 115
    return err;
}

116
int v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid, gid_t gid)
117 118 119
{
    int err;
    FsCred cred;
120
    V9fsState *s = pdu->s;
121

122 123 124
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
125 126 127
    cred_init(&cred);
    cred.fc_uid = uid;
    cred.fc_gid = gid;
128
    v9fs_path_read_lock(s);
129 130
    v9fs_co_run_in_worker(
        {
131
            err = s->ops->chown(&s->ctx, path, &cred);
132 133 134 135
            if (err < 0) {
                err = -errno;
            }
        });
136
    v9fs_path_unlock(s);
137 138 139
    return err;
}

140
int v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
141 142
{
    int err;
143
    V9fsState *s = pdu->s;
144

145 146 147
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
148
    v9fs_path_read_lock(s);
149 150
    v9fs_co_run_in_worker(
        {
151
            err = s->ops->truncate(&s->ctx, path, size);
152 153 154 155
            if (err < 0) {
                err = -errno;
            }
        });
156
    v9fs_path_unlock(s);
157 158
    return err;
}
159

160
int v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, uid_t uid,
161
                  gid_t gid, dev_t dev, mode_t mode, struct stat *stbuf)
162 163
{
    int err;
164
    V9fsPath path;
165
    FsCred cred;
166
    V9fsState *s = pdu->s;
167

168 169 170
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
171 172 173 174 175
    cred_init(&cred);
    cred.fc_uid  = uid;
    cred.fc_gid  = gid;
    cred.fc_mode = mode;
    cred.fc_rdev = dev;
176
    v9fs_path_read_lock(s);
177 178
    v9fs_co_run_in_worker(
        {
179
            err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
180 181
            if (err < 0) {
                err = -errno;
182
            } else {
183 184 185 186 187 188 189
                v9fs_path_init(&path);
                err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
                if (!err) {
                    err = s->ops->lstat(&s->ctx, &path, stbuf);
                    if (err < 0) {
                        err = -errno;
                    }
190
                }
191
                v9fs_path_free(&path);
192 193
            }
        });
194
    v9fs_path_unlock(s);
195 196
    return err;
}
197

198
/* Only works with path name based fid */
199
int v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
200 201
{
    int err;
202
    V9fsState *s = pdu->s;
203

204 205 206
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
207
    v9fs_path_read_lock(s);
208 209 210 211 212 213 214
    v9fs_co_run_in_worker(
        {
            err = s->ops->remove(&s->ctx, path->data);
            if (err < 0) {
                err = -errno;
            }
        });
215
    v9fs_path_unlock(s);
216 217
    return err;
}
218

219
int v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path, V9fsString *name, int flags)
220 221
{
    int err;
222
    V9fsState *s = pdu->s;
223

224 225 226
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
227
    v9fs_path_read_lock(s);
228 229 230 231 232 233 234
    v9fs_co_run_in_worker(
        {
            err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
            if (err < 0) {
                err = -errno;
            }
        });
235
    v9fs_path_unlock(s);
236 237 238 239
    return err;
}

/* Only work with path name based fid */
240
int v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath, V9fsPath *newpath)
241 242
{
    int err;
243
    V9fsState *s = pdu->s;
244

245 246 247
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
248 249 250 251 252 253 254 255 256
    v9fs_co_run_in_worker(
        {
            err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
            if (err < 0) {
                err = -errno;
            }
        });
    return err;
}
257

258
int v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath, V9fsString *oldname,
259 260 261
                     V9fsPath *newdirpath, V9fsString *newname)
{
    int err;
262
    V9fsState *s = pdu->s;
263

264 265 266
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
267 268 269 270 271 272 273 274 275 276 277
    v9fs_co_run_in_worker(
        {
            err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
                                   newdirpath, newname->data);
            if (err < 0) {
                err = -errno;
            }
        });
    return err;
}

278
int v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp, V9fsString *name,
279
                    const char *oldpath, gid_t gid, struct stat *stbuf)
280 281 282
{
    int err;
    FsCred cred;
283
    V9fsPath path;
284
    V9fsState *s = pdu->s;
285

286 287 288
    if (v9fs_request_cancelled(pdu)) {
        return -EINTR;
    }
289
    cred_init(&cred);
290
    cred.fc_uid = dfidp->uid;
291 292
    cred.fc_gid = gid;
    cred.fc_mode = 0777;
293
    v9fs_path_read_lock(s);
294 295
    v9fs_co_run_in_worker(
        {
296 297
            err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
                                  name->data, &cred);
298 299
            if (err < 0) {
                err = -errno;
300
            } else {
301 302 303 304 305 306 307
                v9fs_path_init(&path);
                err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
                if (!err) {
                    err = s->ops->lstat(&s->ctx, &path, stbuf);
                    if (err < 0) {
                        err = -errno;
                    }
308
                }
309
                v9fs_path_free(&path);
310 311
            }
        });
312
    v9fs_path_unlock(s);
313 314 315 316 317 318 319
    return err;
}

/*
 * For path name based fid we don't block. So we can
 * directly call the fs driver ops.
 */
320
int v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
321 322 323
                         const char *name, V9fsPath *path)
{
    int err;
324
    V9fsState *s = pdu->s;
325

326
    if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
327 328 329 330 331
        err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
        if (err < 0) {
            err = -errno;
        }
    } else {
332 333 334
        if (v9fs_request_cancelled(pdu)) {
            return -EINTR;
        }
335 336 337 338 339 340 341
        v9fs_co_run_in_worker(
            {
                err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
                if (err < 0) {
                    err = -errno;
                }
            });
342
    }
343 344
    return err;
}