#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/iov.h"
+#include "qemu/main-loop.h"
#include "qemu/sockets.h"
#include "virtio-9p.h"
#include "fsdev/qemu-fsdev.h"
#include "coth.h"
#include "trace.h"
#include "migration/blocker.h"
+#include "sysemu/qtest.h"
int open_fd_hw;
int total_open_fd;
Oappend = 0x80,
};
-ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
+static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
ssize_t ret;
va_list ap;
return ret;
}
-ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
+static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
ssize_t ret;
va_list ap;
return ret;
}
-struct dotl_openflag_map {
+typedef struct DotlOpenflagMap {
int dotl_flag;
int open_flag;
-};
+} DotlOpenflagMap;
static int dotl_to_open_flags(int flags)
{
*/
int oflags = flags & O_ACCMODE;
- struct dotl_openflag_map dotl_oflag_map[] = {
+ DotlOpenflagMap dotl_oflag_map[] = {
{ P9_DOTL_CREATE, O_CREAT },
{ P9_DOTL_EXCL, O_EXCL },
{ P9_DOTL_NOCTTY , O_NOCTTY },
va_end(ap);
}
-void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
+void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
{
- v9fs_path_free(lhs);
- lhs->data = g_malloc(rhs->size);
- memcpy(lhs->data, rhs->data, rhs->size);
- lhs->size = rhs->size;
+ v9fs_path_free(dst);
+ dst->size = src->size;
+ dst->data = g_memdup(src->data, src->size);
}
int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
/* reopen the file/dir if already closed */
err = v9fs_reopen_fid(pdu, fidp);
if (err < 0) {
- return -1;
+ return err;
}
/*
* Go back to head of fid list because
QLIST_INSERT_HEAD(&s->free_list, pdu, next);
}
-/*
- * We don't do error checking for pdu_marshal/unmarshal here
- * because we always expect to have enough space to encode
- * error details
- */
static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
{
int8_t id = pdu->id + 1; /* Response */
V9fsState *s = pdu->s;
+ int ret;
+
+ /*
+ * The 9p spec requires that successfully cancelled pdus receive no reply.
+ * Sending a reply would confuse clients because they would
+ * assume that any EINTR is the actual result of the operation,
+ * rather than a consequence of the cancellation. However, if
+ * the operation completed (succesfully or with an error other
+ * than caused be cancellation), we do send out that reply, both
+ * for efficiency and to avoid confusing the rest of the state machine
+ * that assumes passing a non-error here will mean a successful
+ * transmission of the reply.
+ */
+ bool discard = pdu->cancelled && len == -EINTR;
+ if (discard) {
+ trace_v9fs_rcancel(pdu->tag, pdu->id);
+ pdu->size = 0;
+ goto out_notify;
+ }
if (len < 0) {
int err = -len;
str.data = strerror(err);
str.size = strlen(str.data);
- len += pdu_marshal(pdu, len, "s", &str);
+ ret = pdu_marshal(pdu, len, "s", &str);
+ if (ret < 0) {
+ goto out_notify;
+ }
+ len += ret;
id = P9_RERROR;
}
- len += pdu_marshal(pdu, len, "d", err);
+ ret = pdu_marshal(pdu, len, "d", err);
+ if (ret < 0) {
+ goto out_notify;
+ }
+ len += ret;
if (s->proto_version == V9FS_PROTO_2000L) {
id = P9_RLERROR;
}
/* fill out the header */
- pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
+ if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
+ goto out_notify;
+ }
/* keep these in sync */
pdu->size = len;
pdu->id = id;
+out_notify:
pdu->s->transport->push_and_notify(pdu);
/* Now wakeup anybody waiting in flush for this request */
return mode;
}
-static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
+static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
+ const char *basename,
const struct stat *stbuf,
V9fsStat *v9stat)
{
int err;
- const char *str;
memset(v9stat, 0, sizeof(*v9stat));
v9fs_string_free(&v9stat->extension);
if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
- err = v9fs_co_readlink(pdu, name, &v9stat->extension);
+ err = v9fs_co_readlink(pdu, path, &v9stat->extension);
if (err < 0) {
return err;
}
"HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
}
- str = strrchr(name->data, '/');
- if (str) {
- str += 1;
- } else {
- str = name->data;
- }
-
- v9fs_string_sprintf(&v9stat->name, "%s", str);
+ v9fs_string_sprintf(&v9stat->name, "%s", basename);
v9stat->size = 61 +
v9fs_string_size(&v9stat->name) +
v9fs_string_init(&version);
err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
if (err < 0) {
- offset = err;
goto out;
}
trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
if (err < 0) {
- offset = err;
goto out;
}
- offset += err;
+ err += offset;
trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
out:
- pdu_complete(pdu, offset);
+ pdu_complete(pdu, err);
v9fs_string_free(&version);
}
struct stat stbuf;
V9fsFidState *fidp;
V9fsPDU *pdu = opaque;
+ char *basename;
err = pdu_unmarshal(pdu, offset, "d", &fid);
if (err < 0) {
if (err < 0) {
goto out;
}
- err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
+ basename = g_path_get_basename(fidp->path.data);
+ err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
+ g_free(basename);
if (err < 0) {
goto out;
}
goto out_nofid;
}
+ trace_v9fs_setattr(pdu->tag, pdu->id, fid,
+ v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
+ v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
+
fidp = get_fid(pdu, fid);
if (fidp == NULL) {
err = -EINVAL;
}
}
err = offset;
+ trace_v9fs_setattr_return(pdu->tag, pdu->id);
out:
put_fid(pdu, fidp);
out_nofid:
trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
if (nwnames && nwnames <= P9_MAXWELEM) {
- wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
- qids = g_malloc0(sizeof(qids[0]) * nwnames);
+ wnames = g_new0(V9fsString, nwnames);
+ qids = g_new0(V9fsQID, nwnames);
for (i = 0; i < nwnames; i++) {
err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
if (err < 0) {
err = -EINVAL;
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
} else {
newfidp = alloc_fid(s, newfid);
if (newfidp == NULL) {
unsigned int niov;
if (is_write) {
- pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov);
+ pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
} else {
pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
}
if (err < 0) {
break;
}
- err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
+ err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
if (err < 0) {
break;
}
+ if ((count + v9stat.size + 2) > 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_stat_free(&v9stat);
+ v9fs_path_free(&path);
+ return count;
+ }
+
/* 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 */
+ if (len < 0) {
v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
v9fs_stat_free(&v9stat);
v9fs_path_free(&path);
- return count;
+ return len;
}
count += len;
v9fs_stat_free(&v9stat);
V9fsString extension;
int iounit;
V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
v9fs_path_init(&path);
v9fs_string_init(&name);
if (err < 0) {
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
err = v9fs_co_opendir(pdu, fidp);
if (err < 0) {
goto out;
if (err < 0) {
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
} else if (perm & P9_STAT_MODE_LINK) {
int32_t ofid = atoi(extension.data);
V9fsFidState *ofidp = get_fid(pdu, ofid);
fidp->fid_type = P9_FID_NONE;
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
if (err < 0) {
fidp->fid_type = P9_FID_NONE;
if (err < 0) {
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
} else if (perm & P9_STAT_MODE_NAMED_PIPE) {
err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
0, S_IFIFO | (perm & 0777), &stbuf);
if (err < 0) {
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
} else if (perm & P9_STAT_MODE_SOCKET) {
err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
0, S_IFSOCK | (perm & 0777), &stbuf);
if (err < 0) {
goto out;
}
+ v9fs_path_write_lock(s);
v9fs_path_copy(&fidp->path, &path);
+ v9fs_path_unlock(s);
} else {
err = v9fs_co_open2(pdu, fidp, &name, -1,
omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
trace_v9fs_flush(pdu->tag, pdu->id, tag);
if (pdu->tag == tag) {
- error_report("Warning: the guest sent a self-referencing 9P flush request");
+ warn_report("the guest sent a self-referencing 9P flush request");
} else {
QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
if (cancel_pdu->tag == tag) {
{
int err = 0;
V9fsString name;
- int32_t dfid, flags;
+ int32_t dfid, flags, rflags = 0;
size_t offset = 7;
V9fsPath path;
V9fsFidState *dfidp;
goto out_nofid;
}
+ if (flags & ~P9_DOTL_AT_REMOVEDIR) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+
+ if (flags & P9_DOTL_AT_REMOVEDIR) {
+ rflags |= AT_REMOVEDIR;
+ }
+
dfidp = get_fid(pdu, dfid);
if (dfidp == NULL) {
err = -EINVAL;
if (err < 0) {
goto out_err;
}
- err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
+ err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
if (!err) {
err = offset;
}
int32_t newdirfid,
V9fsString *name)
{
- char *end;
int err = 0;
V9fsPath new_path;
V9fsFidState *tfidp;
V9fsState *s = pdu->s;
V9fsFidState *dirfidp = NULL;
- char *old_name, *new_name;
v9fs_path_init(&new_path);
if (newdirfid != -1) {
goto out;
}
} else {
- old_name = fidp->path.data;
- end = strrchr(old_name, '/');
- if (end) {
- end++;
- } else {
- end = old_name;
- }
- new_name = g_malloc0(end - old_name + name->size + 1);
- strncat(new_name, old_name, end - old_name);
- strncat(new_name + (end - old_name), name->data, name->size);
- err = v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
- g_free(new_name);
+ char *dir_name = g_path_get_dirname(fidp->path.data);
+ V9fsPath dir_path;
+
+ v9fs_path_init(&dir_path);
+ v9fs_path_sprintf(&dir_path, "%s", dir_name);
+ g_free(dir_name);
+
+ err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
+ v9fs_path_free(&dir_path);
if (err < 0) {
goto out;
}
struct stat stbuf;
V9fsFidState *fidp;
V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
v9fs_stat_init(&v9stat);
err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
}
}
if (v9stat.name.size != 0) {
+ v9fs_path_write_lock(s);
err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
+ v9fs_path_unlock(s);
if (err < 0) {
goto out;
}
xattr_fidp->fs.xattr.len = size;
xattr_fidp->fid_type = P9_FID_XATTR;
xattr_fidp->fs.xattr.xattrwalk_fid = true;
+ xattr_fidp->fs.xattr.value = g_malloc0(size);
if (size) {
- xattr_fidp->fs.xattr.value = g_malloc(size);
err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
xattr_fidp->fs.xattr.value,
xattr_fidp->fs.xattr.len);
xattr_fidp->fs.xattr.len = size;
xattr_fidp->fid_type = P9_FID_XATTR;
xattr_fidp->fs.xattr.xattrwalk_fid = true;
+ xattr_fidp->fs.xattr.value = g_malloc0(size);
if (size) {
- xattr_fidp->fs.xattr.value = g_malloc(size);
err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
&name, xattr_fidp->fs.xattr.value,
xattr_fidp->fs.xattr.len);
static void coroutine_fn v9fs_xattrcreate(void *opaque)
{
- int flags;
+ int flags, rflags = 0;
int32_t fid;
uint64_t size;
ssize_t err = 0;
}
trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
+ if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+
+ if (flags & P9_XATTR_CREATE) {
+ rflags |= XATTR_CREATE;
+ }
+
+ if (flags & P9_XATTR_REPLACE) {
+ rflags |= XATTR_REPLACE;
+ }
+
if (size > XATTR_SIZE_MAX) {
err = -E2BIG;
goto out_nofid;
xattr_fidp->fs.xattr.copied_len = 0;
xattr_fidp->fs.xattr.xattrwalk_fid = false;
xattr_fidp->fs.xattr.len = size;
- xattr_fidp->fs.xattr.flags = flags;
+ xattr_fidp->fs.xattr.flags = rflags;
v9fs_string_init(&xattr_fidp->fs.xattr.name);
v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
xattr_fidp->fs.xattr.value = g_malloc0(size);
if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
(pdu_co_handlers[pdu->id] == NULL)) {
handler = v9fs_op_not_supp;
+ } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
+ handler = v9fs_fs_ro;
} else {
handler = pdu_co_handlers[pdu->id];
}
- if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
- handler = v9fs_fs_ro;
- }
-
qemu_co_queue_init(&pdu->complete);
co = qemu_coroutine_create(handler, pdu);
qemu_coroutine_enter(co);
}
/* Returns 0 on success, 1 on failure. */
-int v9fs_device_realize_common(V9fsState *s, Error **errp)
+int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
+ Error **errp)
{
int i, len;
struct stat stat;
V9fsPath path;
int rc = 1;
+ assert(!s->transport);
+ s->transport = t;
+
/* initialize pdu allocator */
QLIST_INIT(&s->free_list);
QLIST_INIT(&s->active_list);
s->ops = fse->ops;
+ s->ctx.fmode = fse->fmode;
+ s->ctx.dmode = fse->dmode;
+
s->fid_list = NULL;
qemu_co_rwlock_init(&s->rename_lock);
- if (s->ops->init(&s->ctx) < 0) {
- error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
- " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
+ if (s->ops->init(&s->ctx, errp) < 0) {
+ error_prepend(errp, "cannot initialize fsdev '%s': ",
+ s->fsconf.fsdev_id);
goto out;
}