Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/9pfs/cofs.c
Line
Count
Source
1
/*
2
 * 9p backend
3
 *
4
 * Copyright IBM, Corp. 2011
5
 *
6
 * Authors:
7
 *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.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
/*
15
 * Not so fast! You might want to read the 9p developer docs first:
16
 * https://wiki.qemu.org/Documentation/9p
17
 */
18
19
#include "qemu/osdep.h"
20
#include "fsdev/qemu-fsdev.h"
21
#include "qemu/thread.h"
22
#include "qemu/main-loop.h"
23
#include "coth.h"
24
25
static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
26
0
{
27
0
    ssize_t len, maxlen = PATH_MAX;
28
29
0
    buf->data = g_malloc(PATH_MAX);
30
0
    for (;;) {
31
0
        len = s->ops->readlink(&s->ctx, path, buf->data, maxlen);
32
0
        if (len < 0) {
33
0
            g_free(buf->data);
34
0
            buf->data = NULL;
35
0
            buf->size = 0;
36
0
            break;
37
0
        } else if (len == maxlen) {
38
            /*
39
             * We dodn't have space to put the NULL or we have more
40
             * to read. Increase the size and try again
41
             */
42
0
            maxlen *= 2;
43
0
            g_free(buf->data);
44
0
            buf->data = g_malloc(maxlen);
45
0
            continue;
46
0
        }
47
        /*
48
         * Null terminate the readlink output
49
         */
50
0
        buf->data[len] = '\0';
51
0
        buf->size = len;
52
0
        break;
53
0
    }
54
0
    return len;
55
0
}
56
57
int coroutine_fn v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
58
0
{
59
0
    int err;
60
0
    V9fsState *s = pdu->s;
61
62
0
    if (v9fs_request_cancelled(pdu)) {
63
0
        return -EINTR;
64
0
    }
65
0
    v9fs_path_read_lock(s);
66
0
    v9fs_co_run_in_worker(
67
0
        {
68
0
            err = __readlink(s, path, buf);
69
0
            if (err < 0) {
70
0
                err = -errno;
71
0
            }
72
0
        });
73
0
    v9fs_path_unlock(s);
74
0
    return err;
75
0
}
76
77
int coroutine_fn v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path,
78
                                struct statfs *stbuf)
79
0
{
80
0
    int err;
81
0
    V9fsState *s = pdu->s;
82
83
0
    if (v9fs_request_cancelled(pdu)) {
84
0
        return -EINTR;
85
0
    }
86
0
    v9fs_path_read_lock(s);
87
0
    v9fs_co_run_in_worker(
88
0
        {
89
0
            err = s->ops->statfs(&s->ctx, path, stbuf);
90
0
            if (err < 0) {
91
0
                err = -errno;
92
0
            }
93
0
        });
94
0
    v9fs_path_unlock(s);
95
0
    return err;
96
0
}
97
98
int coroutine_fn v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
99
0
{
100
0
    int err;
101
0
    FsCred cred;
102
0
    V9fsState *s = pdu->s;
103
104
0
    if (v9fs_request_cancelled(pdu)) {
105
0
        return -EINTR;
106
0
    }
107
0
    cred_init(&cred);
108
0
    cred.fc_mode = mode;
109
0
    v9fs_path_read_lock(s);
110
0
    v9fs_co_run_in_worker(
111
0
        {
112
0
            err = s->ops->chmod(&s->ctx, path, &cred);
113
0
            if (err < 0) {
114
0
                err = -errno;
115
0
            }
116
0
        });
117
0
    v9fs_path_unlock(s);
118
0
    return err;
119
0
}
120
121
int coroutine_fn v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
122
                                   struct timespec times[2])
123
13.3k
{
124
13.3k
    int err;
125
13.3k
    V9fsState *s = pdu->s;
126
127
13.3k
    if (v9fs_request_cancelled(pdu)) {
128
0
        return -EINTR;
129
0
    }
130
13.3k
    v9fs_path_read_lock(s);
131
13.3k
    v9fs_co_run_in_worker(
132
13.3k
        {
133
13.3k
            err = s->ops->utimensat(&s->ctx, path, times);
134
13.3k
            if (err < 0) {
135
13.3k
                err = -errno;
136
13.3k
            }
137
13.3k
        });
138
13.3k
    v9fs_path_unlock(s);
139
13.3k
    return err;
140
13.3k
}
141
142
int coroutine_fn v9fs_co_futimens(V9fsPDU *pdu, V9fsFidState *fidp,
143
                                  struct timespec times[2])
144
0
{
145
0
    int err;
146
0
    V9fsState *s = pdu->s;
147
148
0
    if (v9fs_request_cancelled(pdu)) {
149
0
        return -EINTR;
150
0
    }
151
0
    v9fs_co_run_in_worker(
152
0
        {
153
0
            err = s->ops->futimens(&s->ctx, fidp->fid_type, &fidp->fs, times);
154
0
            if (err < 0) {
155
0
                err = -errno;
156
0
            }
157
0
        });
158
0
    return err;
159
0
}
160
161
int coroutine_fn v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid,
162
                               gid_t gid)
163
13.5k
{
164
13.5k
    int err;
165
13.5k
    FsCred cred;
166
13.5k
    V9fsState *s = pdu->s;
167
168
13.5k
    if (v9fs_request_cancelled(pdu)) {
169
0
        return -EINTR;
170
0
    }
171
13.5k
    cred_init(&cred);
172
13.5k
    cred.fc_uid = uid;
173
13.5k
    cred.fc_gid = gid;
174
13.5k
    v9fs_path_read_lock(s);
175
13.5k
    v9fs_co_run_in_worker(
176
13.5k
        {
177
13.5k
            err = s->ops->chown(&s->ctx, path, &cred);
178
13.5k
            if (err < 0) {
179
13.5k
                err = -errno;
180
13.5k
            }
181
13.5k
        });
182
13.5k
    v9fs_path_unlock(s);
183
13.5k
    return err;
184
13.5k
}
185
186
int coroutine_fn v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
187
87
{
188
87
    int err;
189
87
    V9fsState *s = pdu->s;
190
191
87
    if (v9fs_request_cancelled(pdu)) {
192
0
        return -EINTR;
193
0
    }
194
87
    v9fs_path_read_lock(s);
195
87
    v9fs_co_run_in_worker(
196
87
        {
197
87
            err = s->ops->truncate(&s->ctx, path, size);
198
87
            if (err < 0) {
199
87
                err = -errno;
200
87
            }
201
87
        });
202
87
    v9fs_path_unlock(s);
203
87
    return err;
204
87
}
205
206
int coroutine_fn v9fs_co_ftruncate(V9fsPDU *pdu, V9fsFidState *fidp, off_t size)
207
0
{
208
0
    int err;
209
0
    V9fsState *s = pdu->s;
210
211
0
    if (v9fs_request_cancelled(pdu)) {
212
0
        return -EINTR;
213
0
    }
214
0
    v9fs_co_run_in_worker(
215
0
        {
216
0
            err = s->ops->ftruncate(&s->ctx, fidp->fid_type, &fidp->fs, size);
217
0
            if (err < 0) {
218
0
                err = -errno;
219
0
            }
220
0
        });
221
0
    return err;
222
0
}
223
224
int coroutine_fn v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp,
225
                               V9fsString *name, uid_t uid, gid_t gid,
226
                               dev_t dev, mode_t mode, struct stat *stbuf)
227
6
{
228
6
    int err;
229
6
    V9fsPath path;
230
6
    FsCred cred;
231
6
    V9fsState *s = pdu->s;
232
233
6
    if (v9fs_request_cancelled(pdu)) {
234
0
        return -EINTR;
235
0
    }
236
6
    cred_init(&cred);
237
6
    cred.fc_uid  = uid;
238
6
    cred.fc_gid  = gid;
239
6
    cred.fc_mode = mode;
240
6
    cred.fc_rdev = dev;
241
6
    v9fs_path_read_lock(s);
242
6
    v9fs_co_run_in_worker(
243
6
        {
244
6
            err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
245
6
            if (err < 0) {
246
6
                err = -errno;
247
6
            } else {
248
6
                v9fs_path_init(&path);
249
6
                err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
250
6
                if (!err) {
251
6
                    err = s->ops->lstat(&s->ctx, &path, stbuf);
252
6
                    if (err < 0) {
253
6
                        err = -errno;
254
6
                    }
255
6
                }
256
6
                v9fs_path_free(&path);
257
6
            }
258
6
        });
259
6
    v9fs_path_unlock(s);
260
6
    return err;
261
6
}
262
263
/* Only works with path name based fid */
264
int coroutine_fn v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
265
0
{
266
0
    int err;
267
0
    V9fsState *s = pdu->s;
268
269
0
    if (v9fs_request_cancelled(pdu)) {
270
0
        return -EINTR;
271
0
    }
272
0
    v9fs_path_read_lock(s);
273
0
    v9fs_co_run_in_worker(
274
0
        {
275
0
            err = s->ops->remove(&s->ctx, path->data);
276
0
            if (err < 0) {
277
0
                err = -errno;
278
0
            }
279
0
        });
280
0
    v9fs_path_unlock(s);
281
0
    return err;
282
0
}
283
284
int coroutine_fn v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path,
285
                                  V9fsString *name, int flags)
286
0
{
287
0
    int err;
288
0
    V9fsState *s = pdu->s;
289
290
0
    if (v9fs_request_cancelled(pdu)) {
291
0
        return -EINTR;
292
0
    }
293
0
    v9fs_path_read_lock(s);
294
0
    v9fs_co_run_in_worker(
295
0
        {
296
0
            err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
297
0
            if (err < 0) {
298
0
                err = -errno;
299
0
            }
300
0
        });
301
0
    v9fs_path_unlock(s);
302
0
    return err;
303
0
}
304
305
/* Only work with path name based fid */
306
int coroutine_fn v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath,
307
                                V9fsPath *newpath)
308
0
{
309
0
    int err;
310
0
    V9fsState *s = pdu->s;
311
312
0
    if (v9fs_request_cancelled(pdu)) {
313
0
        return -EINTR;
314
0
    }
315
0
    v9fs_co_run_in_worker(
316
0
        {
317
0
            err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
318
0
            if (err < 0) {
319
0
                err = -errno;
320
0
            }
321
0
        });
322
0
    return err;
323
0
}
324
325
int coroutine_fn v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath,
326
                                  V9fsString *oldname, V9fsPath *newdirpath,
327
                                  V9fsString *newname)
328
0
{
329
0
    int err;
330
0
    V9fsState *s = pdu->s;
331
332
0
    if (v9fs_request_cancelled(pdu)) {
333
0
        return -EINTR;
334
0
    }
335
0
    v9fs_co_run_in_worker(
336
0
        {
337
0
            err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
338
0
                                   newdirpath, newname->data);
339
0
            if (err < 0) {
340
0
                err = -errno;
341
0
            }
342
0
        });
343
0
    return err;
344
0
}
345
346
int coroutine_fn v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp,
347
                                 V9fsString *name, const char *oldpath,
348
                                 gid_t gid, struct stat *stbuf)
349
0
{
350
0
    int err;
351
0
    FsCred cred;
352
0
    V9fsPath path;
353
0
    V9fsState *s = pdu->s;
354
355
0
    if (v9fs_request_cancelled(pdu)) {
356
0
        return -EINTR;
357
0
    }
358
0
    cred_init(&cred);
359
0
    cred.fc_uid = dfidp->uid;
360
0
    cred.fc_gid = gid;
361
0
    cred.fc_mode = 0777;
362
0
    v9fs_path_read_lock(s);
363
0
    v9fs_co_run_in_worker(
364
0
        {
365
0
            err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
366
0
                                  name->data, &cred);
367
0
            if (err < 0) {
368
0
                err = -errno;
369
0
            } else {
370
0
                v9fs_path_init(&path);
371
0
                err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
372
0
                if (!err) {
373
0
                    err = s->ops->lstat(&s->ctx, &path, stbuf);
374
0
                    if (err < 0) {
375
0
                        err = -errno;
376
0
                    }
377
0
                }
378
0
                v9fs_path_free(&path);
379
0
            }
380
0
        });
381
0
    v9fs_path_unlock(s);
382
0
    return err;
383
0
}
384
385
/*
386
 * For path name based fid we don't block. So we can
387
 * directly call the fs driver ops.
388
 */
389
int coroutine_fn v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
390
                                      const char *name, V9fsPath *path)
391
32.8k
{
392
32.8k
    int err;
393
32.8k
    V9fsState *s = pdu->s;
394
395
32.8k
    if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
396
9.42k
        err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
397
9.42k
        if (err < 0) {
398
0
            err = -errno;
399
0
        }
400
23.3k
    } else {
401
23.3k
        if (v9fs_request_cancelled(pdu)) {
402
0
            return -EINTR;
403
0
        }
404
23.3k
        v9fs_co_run_in_worker(
405
23.3k
            {
406
23.3k
                err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
407
23.3k
                if (err < 0) {
408
23.3k
                    err = -errno;
409
23.3k
                }
410
23.3k
            });
411
23.3k
    }
412
32.8k
    return err;
413
32.8k
}