#include "qemu/osdep.h"
#include "hw/virtio/virtio.h"
-#include "hw/i386/pc.h"
+#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/iov.h"
#include "qemu/sockets.h"
} while (err == -EINTR && !pdu->cancelled);
}
} else if (f->fid_type == P9_FID_DIR) {
- if (f->fs.dir == NULL) {
+ if (f->fs.dir.stream == NULL) {
do {
err = v9fs_co_opendir(pdu, f);
} while (err == -EINTR && !pdu->cancelled);
f->next = s->fid_list;
s->fid_list = f;
+ v9fs_readdir_init(&f->fs.dir);
+ v9fs_readdir_init(&f->fs_reclaim.dir);
+
return f;
}
retval = v9fs_co_close(pdu, &fidp->fs);
}
} else if (fidp->fid_type == P9_FID_DIR) {
- if (fidp->fs.dir != NULL) {
+ if (fidp->fs.dir.stream != NULL) {
retval = v9fs_co_closedir(pdu, &fidp->fs);
}
} else if (fidp->fid_type == P9_FID_XATTR) {
reclaim_count++;
}
} else if (f->fid_type == P9_FID_DIR) {
- if (f->fs.dir != NULL) {
+ if (f->fs.dir.stream != NULL) {
/*
* Up the reference count so that
* a clunk request won't free this fid
f->ref++;
f->rclm_lst = reclaim_list;
reclaim_list = f;
- f->fs_reclaim.dir = f->fs.dir;
- f->fs.dir = NULL;
+ f->fs_reclaim.dir.stream = f->fs.dir.stream;
+ f->fs.dir.stream = NULL;
reclaim_count++;
}
}
goto out;
}
err += offset;
+ memcpy(&s->root_qid, &qid, sizeof(qid));
trace_v9fs_attach_return(pdu->tag, pdu->id,
qid.type, qid.version, qid.path);
/*
return offset;
}
+static bool name_is_illegal(const char *name)
+{
+ return !*name || strchr(name, '/') != NULL;
+}
+
+static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
+{
+ return
+ qid1->type != qid2->type ||
+ qid1->version != qid2->version ||
+ qid1->path != qid2->path;
+}
+
static void v9fs_walk(void *opaque)
{
int name_idx;
V9fsFidState *newfidp = NULL;
V9fsPDU *pdu = opaque;
V9fsState *s = pdu->s;
+ V9fsQID qid;
err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
if (err < 0) {
if (err < 0) {
goto out_nofid;
}
+ if (name_is_illegal(wnames[i].data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
offset += err;
}
} else if (nwnames > P9_MAXWELEM) {
err = -ENOENT;
goto out_nofid;
}
+
+ err = fid_to_qid(pdu, fidp, &qid);
+ if (err < 0) {
+ goto out;
+ }
+
v9fs_path_init(&dpath);
v9fs_path_init(&path);
/*
v9fs_path_copy(&dpath, &fidp->path);
v9fs_path_copy(&path, &fidp->path);
for (name_idx = 0; name_idx < nwnames; name_idx++) {
- err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
- if (err < 0) {
- goto out;
- }
- err = v9fs_co_lstat(pdu, &path, &stbuf);
- if (err < 0) {
- goto out;
+ if (not_same_qid(&pdu->s->root_qid, &qid) ||
+ strcmp("..", wnames[name_idx].data)) {
+ err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
+ &path);
+ if (err < 0) {
+ goto out;
+ }
+
+ err = v9fs_co_lstat(pdu, &path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qid);
+ v9fs_path_copy(&dpath, &path);
}
- stat_to_qid(&stbuf, &qids[name_idx]);
- v9fs_path_copy(&dpath, &path);
+ memcpy(&qids[name_idx], &qid, sizeof(qid));
}
if (fid == newfid) {
BUG_ON(fidp->fid_type != P9_FID_NONE);
}
trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
fidp = get_fid(pdu, dfid);
if (fidp == NULL) {
err = -ENOENT;
int read_count;
int64_t xattr_len;
V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
- VirtQueueElement *elem = &v->elems[pdu->idx];
+ VirtQueueElement *elem = v->elems[pdu->idx];
xattr_len = fidp->fs.xattr.len;
read_count = xattr_len - off;
int32_t count = 0;
struct stat stbuf;
off_t saved_dir_pos;
- struct dirent *dent, *result;
+ struct dirent *dent;
/* save the directory position */
saved_dir_pos = v9fs_co_telldir(pdu, fidp);
return saved_dir_pos;
}
- dent = g_malloc(sizeof(struct dirent));
-
while (1) {
v9fs_path_init(&path);
- err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
- if (err || !result) {
+
+ v9fs_readdir_lock(&fidp->fs.dir);
+
+ err = v9fs_co_readdir(pdu, fidp, &dent);
+ if (err || !dent) {
break;
}
err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
if (err < 0) {
- goto out;
+ break;
}
err = v9fs_co_lstat(pdu, &path, &stbuf);
if (err < 0) {
- goto out;
+ break;
}
err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
if (err < 0) {
- goto out;
+ break;
}
/* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
+
+ v9fs_readdir_unlock(&fidp->fs.dir);
+
if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
/* Ran out of buffer. Set dir back to old position and return */
v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
v9fs_stat_free(&v9stat);
v9fs_path_free(&path);
- g_free(dent);
return count;
}
count += len;
v9fs_path_free(&path);
saved_dir_pos = dent->d_off;
}
-out:
- g_free(dent);
+
+ v9fs_readdir_unlock(&fidp->fs.dir);
+
v9fs_path_free(&path);
if (err < 0) {
return err;
int len, err = 0;
int32_t count = 0;
off_t saved_dir_pos;
- struct dirent *dent, *result;
+ struct dirent *dent;
/* save the directory position */
saved_dir_pos = v9fs_co_telldir(pdu, fidp);
return saved_dir_pos;
}
- dent = g_malloc(sizeof(struct dirent));
-
while (1) {
- err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
- if (err || !result) {
+ v9fs_readdir_lock(&fidp->fs.dir);
+
+ err = v9fs_co_readdir(pdu, fidp, &dent);
+ if (err || !dent) {
break;
}
v9fs_string_init(&name);
v9fs_string_sprintf(&name, "%s", dent->d_name);
if ((count + v9fs_readdir_data_size(&name)) > max_count) {
+ v9fs_readdir_unlock(&fidp->fs.dir);
+
/* Ran out of buffer. Set dir back to old position and return */
v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
v9fs_string_free(&name);
- g_free(dent);
return count;
}
/*
len = pdu_marshal(pdu, 11 + count, "Qqbs",
&qid, dent->d_off,
dent->d_type, &name);
+
+ v9fs_readdir_unlock(&fidp->fs.dir);
+
if (len < 0) {
v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
v9fs_string_free(&name);
- g_free(dent);
return len;
}
count += len;
v9fs_string_free(&name);
saved_dir_pos = dent->d_off;
}
- g_free(dent);
+
+ v9fs_readdir_unlock(&fidp->fs.dir);
+
if (err < 0) {
return err;
}
retval = -EINVAL;
goto out_nofid;
}
- if (!fidp->fs.dir) {
+ if (!fidp->fs.dir.stream) {
retval = -EINVAL;
goto out;
}
}
trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
fidp = get_fid(pdu, fid);
if (fidp == NULL) {
err = -EINVAL;
}
trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
dfidp = get_fid(pdu, dfid);
if (dfidp == NULL) {
err = -EINVAL;
}
trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
dfidp = get_fid(pdu, dfid);
if (dfidp == NULL) {
err = -ENOENT;
if (err < 0) {
goto out_nofid;
}
+
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data)) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+
+ if (!strcmp("..", name.data)) {
+ err = -ENOTEMPTY;
+ goto out_nofid;
+ }
+
dfidp = get_fid(pdu, dfid);
if (dfidp == NULL) {
err = -EINVAL;
if (err < 0) {
goto out_nofid;
}
+
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EISDIR;
+ goto out_nofid;
+ }
+
fidp = get_fid(pdu, fid);
if (fidp == NULL) {
err = -ENOENT;
goto out_err;
}
+ if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
+ err = -ENOENT;
+ goto out_err;
+ }
+
+ if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
+ !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
+ err = -EISDIR;
+ goto out_err;
+ }
+
v9fs_path_write_lock(s);
err = v9fs_complete_renameat(pdu, olddirfid,
&old_name, newdirfid, &new_name);
}
trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
fidp = get_fid(pdu, fid);
if (fidp == NULL) {
err = -ENOENT;
}
trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
+ if (name_is_illegal(name.data)) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
+ err = -EEXIST;
+ goto out_nofid;
+ }
+
fidp = get_fid(pdu, fid);
if (fidp == NULL) {
err = -ENOENT;
if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
handler = v9fs_fs_ro;
}
- co = qemu_coroutine_create(handler);
- qemu_coroutine_enter(co, pdu);
+ co = qemu_coroutine_create(handler, pdu);
+ qemu_coroutine_enter(co);
}
/* Returns 0 on success, 1 on failure. */