summaryrefslogtreecommitdiff
path: root/lib9p/srv.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/srv.c')
-rw-r--r--lib9p/srv.c1346
1 files changed, 828 insertions, 518 deletions
diff --git a/lib9p/srv.c b/lib9p/srv.c
index dae73ea..3e4de91 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -4,23 +4,16 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
-#include <alloca.h>
-#include <inttypes.h> /* for PRI* */
-#include <limits.h> /* for SSIZE_MAX, not set by newlib */
#include <stddef.h> /* for size_t */
-#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy() */
-#ifndef SSIZE_MAX
-#define SSIZE_MAX (SIZE_MAX >> 1)
-#endif
#include <libcr/coroutine.h>
-#include <libcr_ipc/chan.h>
#include <libcr_ipc/mutex.h>
+#include <libhw/generic/net.h>
+#include <libmisc/alloc.h>
#include <libmisc/assert.h>
#include <libmisc/endian.h>
#include <libmisc/map.h>
-#include <libhw/generic/net.h>
#define LOG_NAME 9P_SRV
#include <libmisc/log.h>
@@ -28,6 +21,8 @@
#define IMPLEMENTATION_FOR_LIB9P_SRV_H YES
#include <lib9p/srv.h>
+#include "srv_errno.h"
+
/* config *********************************************************************/
#include "config.h"
@@ -39,24 +34,35 @@
#error config.h must define CONFIG_9P_SRV_MAX_HOSTMSG_SIZE
#endif
static_assert(CONFIG_9P_SRV_MAX_MSG_SIZE <= CONFIG_9P_SRV_MAX_HOSTMSG_SIZE);
-static_assert(CONFIG_9P_SRV_MAX_HOSTMSG_SIZE <= SSIZE_MAX);
+static_assert(CONFIG_9P_SRV_MAX_HOSTMSG_SIZE <= SIZE_MAX);
/* context ********************************************************************/
bool lib9p_srv_flush_requested(struct lib9p_srv_ctx *ctx) {
assert(ctx);
- return cr_chan_can_send(&ctx->flushch);
+ return cr_chan_can_send(&ctx->flush_ch);
}
-void lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx) {
- assert(ctx);
- assert(cr_chan_can_send(&ctx->flushch));
- lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_ECANCELED, "request canceled by flush");
- cr_chan_send(&ctx->flushch, true);
-}
+#define req_debug(...) \
+ log_debugln( \
+ "cid=", cr_getcid(), ": ", \
+ lib9p_msgtype_str(ctx->basectx.version, ctx->net_bytes[4]), "(tag=", ctx->tag, "): ", \
+ __VA_ARGS__)
/* structs ********************************************************************/
+#ifndef NDEBUG
+void lib9p_srv_stat_assert(const struct lib9p_srv_stat *stat) {
+ assert(stat);
+ assert( ((bool)(stat->mode & LIB9P_DM_DIR )) == ((bool)(stat->qid.type & LIB9P_QT_DIR )) );
+ assert( ((bool)(stat->mode & LIB9P_DM_APPEND)) == ((bool)(stat->qid.type & LIB9P_QT_APPEND)) );
+ assert( ((bool)(stat->mode & LIB9P_DM_EXCL )) == ((bool)(stat->qid.type & LIB9P_QT_EXCL )) );
+ assert( ((bool)(stat->mode & LIB9P_DM_AUTH )) == ((bool)(stat->qid.type & LIB9P_QT_AUTH )) );
+ assert( ((bool)(stat->mode & LIB9P_DM_TMP )) == ((bool)(stat->qid.type & LIB9P_QT_TMP )) );
+ assert( (stat->size == 0) || !(stat->mode & LIB9P_DM_DIR) );
+}
+#endif
+
enum srv_filetype {
SRV_FILETYPE_FILE,
SRV_FILETYPE_DIR,
@@ -86,11 +92,12 @@ struct srv_pathinfo {
#define FIDFLAG_OPEN_R (1<<0)
#define FIDFLAG_OPEN_W (1<<1)
#define FIDFLAG_RCLOSE (1<<2)
+#define FIDFLAG_APPEND (1<<3)
#define FIDFLAG_OPEN (FIDFLAG_OPEN_R|FIDFLAG_OPEN_W)
struct srv_fidinfo {
srv_path_t path;
- struct lib9p_srv_authinfo *authinfo;
+ struct lib9p_srv_userid *user;
uint8_t flags;
enum srv_filetype type;
union {
@@ -101,6 +108,7 @@ struct srv_fidinfo {
lo_interface lib9p_srv_dio io;
size_t idx;
uint64_t off;
+ struct lib9p_srv_dirent buffered_dirent;
} dir;
struct {
struct lib9p_s aname;
@@ -108,6 +116,7 @@ struct srv_fidinfo {
} auth;
};
};
+DECLARE_ERROR_OR_(struct srv_fidinfo *, srv_fidinfop);
/* contexts **************************************
*
@@ -123,7 +132,7 @@ struct srv_conn {
/* immutable */
struct lib9p_srv *parent_srv;
lo_interface net_stream_conn fd;
- cid_t reader; /* the lib9p_srv_read_cr() coroutine */
+ cid_t reader; /* the lib9p_srv_read() coroutine */
/* mutable */
cr_mutex_t writelock;
};
@@ -138,7 +147,6 @@ struct srv_sess {
enum lib9p_version version;
uint32_t max_msg_size;
/* mutable */
- bool initialized;
bool closing;
struct srv_pathmap paths; /* srv_path_t => `lib9p_srv_file` + metadata */
struct srv_fidmap fids; /* lib9p_fid_t => `lib9p_srv_{fio,dio}` + metadata */
@@ -149,7 +157,7 @@ struct srv_sess {
/* utilities for the above types **********************************************/
-static inline enum srv_filetype srv_qid_filetype(struct lib9p_qid qid) {
+static enum srv_filetype srv_qid_filetype(struct lib9p_qid qid) {
if (qid.type & LIB9P_QT_AUTH)
return SRV_FILETYPE_AUTH;
if (qid.type & LIB9P_QT_DIR)
@@ -157,44 +165,66 @@ static inline enum srv_filetype srv_qid_filetype(struct lib9p_qid qid) {
return SRV_FILETYPE_FILE;
}
-static inline bool srv_check_perm(struct srv_req *ctx, struct lib9p_stat *stat, uint8_t action) {
+[[maybe_unused]]
+static bool srv_stat_check_perm(struct srv_req *ctx, struct lib9p_srv_stat *stat, uint8_t action) {
assert(ctx);
assert(stat);
assert(action);
/* TODO actually check user and group instead of just assuming "other". */
- uint8_t mode = (uint8_t)(stat->file_mode & 07);
+ uint8_t mode = (uint8_t)(stat->mode & 07);
return mode & action;
}
-struct lib9p_srv_authinfo *srv_authinfo_new(struct lib9p_s uname, lib9p_nuid_t uid) {
- struct lib9p_srv_authinfo *ret = malloc(sizeof(struct lib9p_srv_authinfo) + uname.len);
- if (!ret)
- return NULL;
- ret->uid = uid;
- ret->uname.len = uname.len;
- ret->uname.utf8 = (void *)&ret[1];
- memcpy(ret->uname.utf8, uname.utf8, uname.len);
+[[maybe_unused]]
+static ok_or_error srv_file_check_perm(struct srv_req *ctx, lo_interface lib9p_srv_file file, uint8_t action) {
+ assert(ctx);
+ assert(!LO_IS_NULL(file));
+ assert(action);
+
+ struct lib9p_srv_stat stat;
+ error err = LO_CALL(file, stat, ctx, &stat);
+ if (!ERROR_IS_NULL(err))
+ return ERROR_NEW_ERR(ok, err);
+ lib9p_srv_stat_assert(&stat);
+ return ERROR_NEW_VAL(ok, srv_stat_check_perm(ctx, &stat, action));
+}
+
+[[maybe_unused]]
+static struct lib9p_srv_userid *srv_userid_new(struct lib9p_s name
+#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
+ , lib9p_nuid_t num
+#endif
+ ) {
+ struct lib9p_srv_userid *ret = (void *)heap_alloc(sizeof(struct lib9p_srv_userid) + name.len, char);
+ void *end_dat = (void *)&ret[1];
+ memcpy(end_dat, name.utf8, name.len);
+#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
+ ret->num = num;
+#endif
+ ret->name.len = name.len;
+ ret->name.utf8 = end_dat;
ret->refcount = 1;
return ret;
}
+#if !(CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L)
+#define srv_userid_new(name, num) srv_userid_new(name)
+#endif
-struct lib9p_srv_authinfo *srv_authinfo_decref(struct lib9p_srv_authinfo *authinfo) {
- assert(authinfo);
- assert(authinfo->refcount);
- authinfo->refcount--;
- if (!authinfo->refcount) {
- free(authinfo);
- return NULL;
- }
- return authinfo;
+static struct lib9p_srv_userid *srv_userid_decref(struct lib9p_srv_userid *userid) {
+ assert(userid);
+ assert(userid->refcount);
+ userid->refcount--;
+ if (!userid->refcount)
+ free(userid);
+ return NULL;
}
-struct lib9p_srv_authinfo *srv_authinfo_incref(struct lib9p_srv_authinfo *authinfo) {
- assert(authinfo);
- authinfo->refcount++;
- return authinfo;
+static struct lib9p_srv_userid *srv_userid_incref(struct lib9p_srv_userid *userid) {
+ assert(userid);
+ userid->refcount++;
+ return userid;
}
/**
@@ -204,9 +234,10 @@ struct lib9p_srv_authinfo *srv_authinfo_incref(struct lib9p_srv_authinfo *authin
*
* Returns a pointer to the stored pathinfo.
*/
-static inline struct srv_pathinfo *srv_path_save(struct srv_req *ctx,
- lo_interface lib9p_srv_file file,
- srv_path_t parent_path) {
+[[maybe_unused]]
+static struct srv_pathinfo *srv_path_save(struct srv_req *ctx,
+ lo_interface lib9p_srv_file file,
+ srv_path_t parent_path) {
assert(ctx);
assert(!LO_IS_NULL(file));
@@ -238,7 +269,7 @@ static inline struct srv_pathinfo *srv_path_save(struct srv_req *ctx,
* Decrement the path's gc_refcount, and trigger garbage collection as
* appropriate.
*/
-static inline void srv_path_decref(struct srv_req *ctx, srv_path_t path) {
+static void srv_path_decref(struct srv_req *ctx, srv_path_t path) {
assert(ctx);
for (;;) {
@@ -256,32 +287,23 @@ static inline void srv_path_decref(struct srv_req *ctx, srv_path_t path) {
}
}
-static inline void srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, bool remove) {
- struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, fid);
+static error srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, struct srv_fidinfo *fidinfo, bool remove) {
+ assert(ctx);
+ assert(!ctx->user);
assert(fidinfo);
+
+ error err = {};
+
if (fidinfo->flags & FIDFLAG_RCLOSE)
remove = true;
+ ctx->user = srv_userid_incref(fidinfo->user);
+
struct srv_pathinfo *pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path);
assert(pathinfo);
- if (remove) {
- if (pathinfo->parent_dir == fidinfo->path) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBUSY, "cannot remove root");
- goto clunk;
- }
- struct srv_pathinfo *parent = map_load(&ctx->parent_sess->paths, pathinfo->parent_dir);
- assert(parent);
- struct lib9p_stat parent_stat = LO_CALL(parent->file, stat, ctx);
- if (!srv_check_perm(ctx, &parent_stat, 0b010)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "you do not have write permission on the parent directory");
- goto clunk;
- }
- LO_CALL(pathinfo->file, remove, ctx);
- }
+ if (remove)
+ err = LO_CALL(pathinfo->file, remove, ctx);
- clunk:
if (fidinfo->flags & FIDFLAG_OPEN) {
switch (fidinfo->type) {
case SRV_FILETYPE_DIR:
@@ -296,9 +318,13 @@ static inline void srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, bool remove
}
pathinfo->io_refcount--;
}
- fidinfo->authinfo = srv_authinfo_decref(fidinfo->authinfo);
+ fidinfo->user = srv_userid_decref(fidinfo->user);
srv_path_decref(ctx, fidinfo->path);
map_del(&ctx->parent_sess->fids, fid);
+
+ ctx->user = srv_userid_decref(ctx->user);
+
+ return err;
}
/**
@@ -306,7 +332,8 @@ static inline void srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, bool remove
* pathinfo->gc_refcount has already been incremented; does *not*
* decrement it on failure.
*/
-static inline struct srv_fidinfo *srv_fid_store(struct srv_req *ctx, lib9p_fid_t fid, struct srv_pathinfo *pathinfo, bool overwrite) {
+[[maybe_unused]]
+static srv_fidinfop_or_error srv_fid_store(struct srv_req *ctx, lib9p_fid_t fid, struct srv_pathinfo *pathinfo, bool overwrite) {
assert(ctx);
assert(fid != LIB9P_FID_NOFID);
assert(pathinfo);
@@ -323,22 +350,20 @@ static inline struct srv_fidinfo *srv_fid_store(struct srv_req *ctx, lib9p_fid_t
assert(old_fidinfo->type == SRV_FILETYPE_DIR);
assert(old_fidinfo->flags == 0);
- old_fidinfo->authinfo = srv_authinfo_decref(old_fidinfo->authinfo);
+ old_fidinfo->user = srv_userid_decref(old_fidinfo->user);
srv_path_decref(ctx, old_fidinfo->path);
map_del(&ctx->parent_sess->fids, fid);
} else {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "FID already in use");
- return NULL;
+ return ERROR_NEW_ERR(srv_fidinfop, error_new(E_POSIX_EBADF, "FID already in use"));
}
}
struct srv_fidinfo *fidinfo = map_store(&ctx->parent_sess->fids, fid, (struct srv_fidinfo){
- .path = qid.path,
- .type = srv_qid_filetype(qid),
- .authinfo = srv_authinfo_incref(ctx->authinfo),
+ .path = qid.path,
+ .type = srv_qid_filetype(qid),
+ .user = srv_userid_incref(ctx->user),
});
assert(fidinfo);
- return fidinfo;
+ return ERROR_NEW_VAL(srv_fidinfop, fidinfo);
}
/* base utilities *************************************************************/
@@ -349,82 +374,81 @@ static void srv_msglog(struct srv_req *req, enum lib9p_msg_type typ, void *hostm
srv->msglog(req, typ, hostmsg);
return;
}
- /* It sucks that %v trips -Wformat and -Wformat-extra-args
- * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47781 */
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat"
-#pragma GCC diagnostic ignored "-Wformat-extra-args"
- infof("%c %v", typ % 2 ? '<' : '>', lo_box_lib9p_msg_as_fmt_formatter(&req->basectx, typ, hostmsg));
-#pragma GCC diagnostic pop
+ log_infoln(typ % 2 ? "< " : "> ", (lib9p_msg, &req->basectx, typ, hostmsg));
}
-static ssize_t srv_write_Rmsg(struct srv_req *req, struct lib9p_Rmsg_send_buf *resp) {
- ssize_t r;
+#define srv_nonrespond_error log_errorln
+
+static error srv_write_Rmsg(struct srv_req *req, enum lib9p_msg_type typ, void *host) {
+ assert(req);
+ assert(typ % 2 == 1);
+ assert(host);
+
+ struct lib9p_Rmsg_send_buf net;
+ error err = lib9p_Rmsg_marshal(&req->basectx,
+ typ, host,
+ &net);
+ if (!ERROR_IS_NULL(err))
+ return err;
+
+ srv_msglog(req, typ, host);
+
cr_mutex_lock(&req->parent_sess->parent_conn->writelock);
- r = io_writev(req->parent_sess->parent_conn->fd, resp->iov, resp->iov_cnt);
+ size_t_and_error r = io_writev(req->parent_sess->parent_conn->fd, net.iov, net.iov_cnt);
cr_mutex_unlock(&req->parent_sess->parent_conn->writelock);
- return r;
+ if (!ERROR_IS_NULL(r.err)) {
+ srv_nonrespond_error("write: (", r.size_t, ", ", (error, r.err), ")");
+ error_cleanup(&r.err);
+ }
+ return ERROR_NULL;
}
-#define srv_nonrespond_errorf errorf
-
-static void srv_respond_error(struct srv_req *req) {
-#if CONFIG_9P_ENABLE_9P2000_u
- assert(req->basectx.err_num);
-#endif
- assert(req->basectx.err_msg[0]);
+static void srv_respond_error(struct srv_req *req, error err) {
+ assert(!ERROR_IS_NULL(err));
- ssize_t r;
struct lib9p_msg_Rerror host = {
.tag = req->tag,
- .errstr = lib9p_strn(req->basectx.err_msg,
- CONFIG_9P_MAX_ERR_SIZE),
+ .errstr = lib9p_str(error_msg(err)),
#if CONFIG_9P_ENABLE_9P2000_u
- .errnum = req->basectx.err_num,
+ .errnum = libmisc_to_linuxgeneric_errno(err.num),
#endif
};
struct srv_sess *sess = req->parent_sess;
- /* XXX: This assumes that a version's min_msg_size is the
- * Rerror overhead. That's true for the current
- * implementation of core_gen, but is a sneaky assumption. */
- uint32_t overhead = lib9p_version_min_msg_size(sess->version);
+ uint32_t overhead = lib9p_version_min_Rerror_size(sess->version);
/* Truncate the error-string if necessary to avoid needing to
- * return LIB9P_ERRNO_L_ERANGE. */
+ * return E_POSIX_ERANGE. */
if (((uint32_t)host.errstr.len) + overhead > sess->max_msg_size)
host.errstr.len = sess->max_msg_size - overhead;
- struct lib9p_Rmsg_send_buf net;
+ error marshal_err = srv_write_Rmsg(req, LIB9P_TYP_Rerror, &host);
+ assert(ERROR_IS_NULL(marshal_err));
- lib9p_Rmsg_marshal(&req->basectx,
- LIB9P_TYP_Rerror, &host,
- &net);
-
- srv_msglog(req, LIB9P_TYP_Rerror, &host);
- r = srv_write_Rmsg(req, &net);
- if (r < 0)
- srv_nonrespond_errorf("write: %s", net_strerror(-r));
+ error_cleanup(&err);
}
/* read coroutine *************************************************************/
-static inline bool srv_read_exactly(lo_interface net_stream_conn fd, uint8_t *buf, size_t goal, size_t *done) {
+/** Return whether to `break`. */
+static bool srv_read_exactly(lo_interface net_stream_conn fd, uint8_t *buf, size_t goal, size_t *done) {
assert(buf);
assert(goal);
assert(done);
while (*done < goal) {
- ssize_t r = io_read(fd, &buf[*done], goal - *done);
- if (r < 0) {
- srv_nonrespond_errorf("read: %s", net_strerror(-r));
- return true;
- } else if (r == 0) {
- if (*done != 0)
- srv_nonrespond_errorf("read: unexpected EOF");
+ size_t_or_error r = io_read(fd, &buf[*done], goal - *done);
+ if (r.is_err) {
+ if (r.err.num == E_EOF) {
+ if (*done != 0)
+ srv_nonrespond_error("read: unexpected EOF");
+ } else {
+ srv_nonrespond_error("read: ", (error, r.err));
+ }
+ error_cleanup(&r.err);
return true;
}
- *done += r;
+ *done += r.size_t;
}
return false;
}
@@ -437,114 +461,126 @@ void lib9p_srv_accept_and_read_loop(struct lib9p_srv *srv, lo_interface net_stre
srv->readers++;
for (;;) {
- lo_interface net_stream_conn conn = LO_CALL(listener, accept);
- if (LO_IS_NULL(conn)) {
- srv_nonrespond_errorf("accept: error");
+ net_stream_conn_or_error r = LO_CALL(listener, accept);
+ if (r.is_err) {
+ srv_nonrespond_error("accept: ", (error, r.err));
+ error_cleanup(&r.err);
srv->readers--;
if (srv->readers == 0)
while (srv->writers > 0)
- cr_rpc_send_req(&srv->_reqch, NULL);
+ cr_rpc_send_req(&srv->reqch, NULL);
return;
}
- lib9p_srv_read(srv, conn);
+ lib9p_srv_read(srv, r.net_stream_conn);
}
}
-static void handle_message(struct srv_req *ctx);
+static void lib9p_srv_worker_Tversion(struct srv_req *ctx);
void lib9p_srv_read(struct lib9p_srv *srv, lo_interface net_stream_conn _conn) {
assert(srv);
assert(srv->rootdir);
assert(!LO_IS_NULL(_conn));
- struct srv_conn conn = {
+ struct srv_conn _conn_ = {
.parent_srv = srv,
.fd = _conn,
.reader = cr_getcid(),
};
- struct srv_sess sess = {
- .parent_conn = &conn,
- .version = LIB9P_VER_unknown,
+ struct srv_conn *conn = &_conn_;
+ struct srv_sess _sess_ = {
+ .parent_conn = conn,
+ .version = LIB9P_VER_uninitialized,
.max_msg_size = CONFIG_9P_SRV_MAX_MSG_SIZE,
- .initialized = false,
};
+ struct srv_sess *sess = &_sess_;
+ struct srv_req _req_;
+ struct srv_req *req = &_req_;
+
for (;;) {
/* Read the message. */
size_t done = 0;
uint8_t buf[7];
- if (srv_read_exactly(conn.fd, buf, 4, &done))
+ if (srv_read_exactly(conn->fd, buf, 4, &done))
break;
size_t goal = uint32le_decode(buf);
if (goal < 7) {
- srv_nonrespond_errorf("T-message is impossibly small");
+ srv_nonrespond_error("T-message is impossibly small");
break;
}
- if (srv_read_exactly(conn.fd, buf, 7, &done))
+ if (srv_read_exactly(conn->fd, buf, 7, &done))
break;
- struct srv_req req = {
+ *req = (struct srv_req){
.basectx = {
- .version = sess.version,
- .max_msg_size = sess.max_msg_size,
+ .version = sess->version,
+ .max_msg_size = sess->max_msg_size,
},
- .parent_sess = &sess,
+ .parent_sess = sess,
.tag = uint16le_decode(&buf[5]),
.net_bytes = buf,
};
- if (goal > sess.max_msg_size) {
- lib9p_errorf(&req.basectx,
- LIB9P_ERRNO_L_EMSGSIZE, "T-message larger than %s limit (%zu > %"PRIu32")",
- sess.initialized ? "negotiated" : "server",
- goal,
- sess.max_msg_size);
- srv_respond_error(&req);
+ if (goal > sess->max_msg_size) {
+ srv_respond_error(req, error_new(E_POSIX_EMSGSIZE,
+ "T-message larger than ", sess->version == LIB9P_VER_uninitialized ? "server" : "negotiated", " limit",
+ " (", goal, " > ", sess->max_msg_size, ")"));
continue;
}
- req.net_bytes = malloc(goal);
- assert(req.net_bytes);
- memcpy(req.net_bytes, buf, done);
- if (srv_read_exactly(conn.fd, req.net_bytes, goal, &done)) {
- free(req.net_bytes);
+ req->net_bytes = heap_alloc(goal, uint8_t);
+ memcpy(req->net_bytes, buf, done);
+ if (srv_read_exactly(conn->fd, req->net_bytes, goal, &done)) {
+ free(req->net_bytes);
break;
}
/* Handle the message... */
- if (req.net_bytes[4] == LIB9P_TYP_Tversion)
+ if (req->net_bytes[4] == LIB9P_TYP_Tversion)
/* ...in this coroutine for Tversion, */
- handle_message(&req);
+ lib9p_srv_worker_Tversion(req);
else
/* ...but usually in another coroutine. */
- cr_rpc_send_req(&srv->_reqch, &req);
+ cr_rpc_send_req(&srv->reqch, req);
}
- if (map_len(&sess.reqs) == 0)
- io_close(conn.fd);
- else {
- io_close_read(conn.fd);
- sess.closing = true;
+
+ /* Cleanup `conn` and `sess->reqs`. */
+ error err = io_close_read(conn->fd);
+ if (!ERROR_IS_NULL(err)) {
+ srv_nonrespond_error("conn close: ", (error, err));
+ error_cleanup(&err);
+ }
+ if (map_len(&sess->reqs)) {
+ sess->closing = true;
cr_pause_and_yield();
- assert(map_len(&sess.reqs) == 0);
- io_close_write(conn.fd);
}
-
- assert(map_len(&sess.reqs) == 0);
- map_free(&sess.reqs);
-
- MAP_FOREACH(&sess.fids, fid, fidinfo) {
- struct srv_req req = {
- .basectx = {
- .version = sess.version,
- .max_msg_size = sess.max_msg_size,
- },
- .parent_sess = &sess,
- };
- srv_fid_del(&req, fid, false);
- if (lib9p_ctx_has_error(&req.basectx))
- errorf("clunk: %.*s", CONFIG_9P_MAX_ERR_SIZE, req.basectx.err_msg);
+ assert(map_len(&sess->reqs) == 0);
+ err = io_close_write(conn->fd);
+ if (!ERROR_IS_NULL(err)) {
+ srv_nonrespond_error("conn close: ", (error, err));
+ error_cleanup(&err);
+ }
+ map_free(&sess->reqs);
+
+ /* Cleanup `sess->fids`. */
+ *req = (struct srv_req){
+ .basectx = {
+ .version = sess->version,
+ .max_msg_size = sess->max_msg_size,
+ },
+ .parent_sess = sess,
+ };
+ MAP_FOREACH(&sess->fids, fid, fidinfo) {
+ err = srv_fid_del(req, fid, fidinfo, false);
+ if (!ERROR_IS_NULL(err)) {
+ srv_nonrespond_error("clunk: ", (error, err));
+ error_cleanup(&err);
+ }
}
- map_free(&sess.fids);
+ assert(map_len(&sess->fids) == 0);
+ map_free(&sess->fids);
- assert(map_len(&sess.paths) == 0);
- map_free(&sess.paths);
+ /* Cleanup `sess->paths`. */
+ assert(map_len(&sess->paths) == 0);
+ map_free(&sess->paths);
}
/* write coroutine ************************************************************/
@@ -560,7 +596,7 @@ void lib9p_srv_worker_loop(struct lib9p_srv *srv) {
for (;;) {
/* Receive the request from the reader coroutine. ************/
- rpc_handle = cr_rpc_recv_req(&srv->_reqch);
+ rpc_handle = cr_rpc_recv_req(&srv->reqch);
if (!rpc_handle.req) {
srv->writers--;
cr_rpc_send_resp(rpc_handle, 0);
@@ -577,22 +613,15 @@ void lib9p_srv_worker_loop(struct lib9p_srv *srv) {
cr_rpc_send_resp(rpc_handle, 0);
/* Process the request. **************************************/
- handle_message(&req);
-
- /* Release resources. ****************************************/
- while (cr_chan_can_send(&req.flushch))
- cr_chan_send(&req.flushch, false);
- map_del(&req.parent_sess->reqs, req.tag);
- if (req.parent_sess->closing && !map_len(&req.parent_sess->reqs))
- cr_unpause(req.parent_sess->parent_conn->reader);
+ lib9p_srv_worker(&req);
}
}
#define _HANDLER_PROTO(typ) \
static void handle_T##typ(struct srv_req *, \
- struct lib9p_msg_T##typ *, \
- struct lib9p_msg_R##typ *)
+ struct lib9p_msg_T##typ *)
_HANDLER_PROTO(version);
+#if _LIB9P_ENABLE_stat
_HANDLER_PROTO(auth);
_HANDLER_PROTO(attach);
_HANDLER_PROTO(flush);
@@ -605,6 +634,7 @@ _HANDLER_PROTO(clunk);
_HANDLER_PROTO(remove);
_HANDLER_PROTO(stat);
_HANDLER_PROTO(wstat);
+#endif
#if CONFIG_9P_ENABLE_9P2000_p9p
_HANDLER_PROTO(openfd);
#endif
@@ -614,82 +644,139 @@ _HANDLER_PROTO(sread);
_HANDLER_PROTO(swrite);
#endif
-typedef void (*tmessage_handler)(struct srv_req *, void *, void *);
-
-static tmessage_handler tmessage_handlers[0x100] = {
- [LIB9P_TYP_Tversion] = (tmessage_handler)handle_Tversion,
- [LIB9P_TYP_Tauth] = (tmessage_handler)handle_Tauth,
- [LIB9P_TYP_Tattach] = (tmessage_handler)handle_Tattach,
- [LIB9P_TYP_Tflush] = (tmessage_handler)handle_Tflush,
- [LIB9P_TYP_Twalk] = (tmessage_handler)handle_Twalk,
- [LIB9P_TYP_Topen] = (tmessage_handler)handle_Topen,
- [LIB9P_TYP_Tcreate] = (tmessage_handler)handle_Tcreate,
- [LIB9P_TYP_Tread] = (tmessage_handler)handle_Tread,
- [LIB9P_TYP_Twrite] = (tmessage_handler)handle_Twrite,
- [LIB9P_TYP_Tclunk] = (tmessage_handler)handle_Tclunk,
- [LIB9P_TYP_Tremove] = (tmessage_handler)handle_Tremove,
- [LIB9P_TYP_Tstat] = (tmessage_handler)handle_Tstat,
- [LIB9P_TYP_Twstat] = (tmessage_handler)handle_Twstat,
+void lib9p_srv_worker(struct srv_req *ctx) {
+ [[gnu::cleanup(heap_cleanup)]] void *host_req = NULL;
+
+ /* Unmarshal it. *****************************************************/
+ size_t_or_error r = lib9p_Tmsg_validate(&ctx->basectx, ctx->net_bytes);
+ if (r.is_err) {
+ srv_respond_error(ctx, r.err);
+ goto release;
+ }
+ size_t host_size = r.size_t;
+ host_req = heap_alloc(host_size, char);
+ enum lib9p_msg_type typ;
+ lib9p_Tmsg_unmarshal(&ctx->basectx, ctx->net_bytes,
+ &typ, host_req);
+ srv_msglog(ctx, typ, host_req);
+
+ /* Handle it. ********************************************************/
+#define CASE(typ) case LIB9P_TYP_##typ: handle_##typ(ctx, (void *)host_req); break
+ LM_PARTIAL_SWITCH (typ) {
+#if _LIB9P_ENABLE_stat
+ CASE(Tauth);
+ CASE(Tattach);
+ CASE(Tflush);
+ CASE(Twalk);
+ CASE(Topen);
+ CASE(Tcreate);
+ CASE(Tread);
+ CASE(Twrite);
+ CASE(Tclunk);
+ CASE(Tremove);
+ CASE(Tstat);
+ CASE(Twstat);
+#endif
#if CONFIG_9P_ENABLE_9P2000_p9p
- [LIB9P_TYP_Topenfd] = (tmessage_handler)handle_Topenfd,
+ CASE(Topenfd);
#endif
#if CONFIG_9P_ENABLE_9P2000_e
- [LIB9P_TYP_Tsession] = (tmessage_handler)handle_Tsession,
- [LIB9P_TYP_Tsread] = (tmessage_handler)handle_Tsread,
- [LIB9P_TYP_Tswrite] = (tmessage_handler)handle_Tswrite,
+ CASE(Tsession);
+ CASE(Tsread);
+ CASE(Tswrite);
#endif
-};
+#undef CASE
+ default:
+ assert_notreached("lib9p_Tmsg_validate() should have rejected unknown typ");
+ }
+ assert(ctx->responded);
+
+ /* Release resources. ************************************************/
+ release:
+ map_del(&ctx->parent_sess->reqs, ctx->tag);
+ size_t nwaiters;
+ while ((nwaiters = cr_chan_num_waiters(&ctx->flush_ch))) {
+ cr_chan_send(&ctx->flush_ch, (nwaiters == 1)
+ ? _LIB9P_SRV_FLUSH_RFLUSH
+ : _LIB9P_SRV_FLUSH_SILENT);
+ }
+ if (ctx->parent_sess->closing && !map_len(&ctx->parent_sess->reqs))
+ cr_unpause(ctx->parent_sess->parent_conn->reader);
+ free(ctx->net_bytes);
+}
-static void handle_message(struct srv_req *ctx) {
- uint8_t *host_req = NULL;
- uint8_t host_resp[CONFIG_9P_SRV_MAX_HOSTMSG_SIZE];
+static void lib9p_srv_worker_Tversion(struct srv_req *ctx) {
+ [[gnu::cleanup(heap_cleanup)]] void *host_req = NULL;
- /* Unmarshal it. */
- ssize_t host_size = lib9p_Tmsg_validate(&ctx->basectx, ctx->net_bytes);
- if (host_size < 0)
- goto write;
- host_req = calloc(1, host_size);
- assert(host_req);
+ /* Unmarshal it. *****************************************************/
+ size_t_or_error r = lib9p_Tmsg_validate(&ctx->basectx, ctx->net_bytes);
+ if (r.is_err) {
+ srv_respond_error(ctx, r.err);
+ goto release;
+ }
+ size_t host_size = r.size_t;
+ host_req = heap_alloc(host_size, char);
enum lib9p_msg_type typ;
lib9p_Tmsg_unmarshal(&ctx->basectx, ctx->net_bytes,
&typ, host_req);
srv_msglog(ctx, typ, host_req);
- /* Handle it. */
- tmessage_handlers[typ](ctx, (void *)host_req, (void *)host_resp);
-
- write:
- if (lib9p_ctx_has_error(&ctx->basectx))
- srv_respond_error(ctx);
- else {
- struct lib9p_Rmsg_send_buf net_resp;
- if (lib9p_Rmsg_marshal(&ctx->basectx,
- typ+1, host_resp,
- &net_resp))
- goto write;
- srv_msglog(ctx, typ+1, &host_resp);
- srv_write_Rmsg(ctx, &net_resp);
+ /* Handle it. ********************************************************/
+ handle_Tversion(ctx, (void *)host_req);
+ assert(ctx->responded);
+
+ /* Release resources. ************************************************/
+ release:
+ map_del(&ctx->parent_sess->reqs, ctx->tag);
+ size_t nwaiters;
+ while ((nwaiters = cr_chan_num_waiters(&ctx->flush_ch))) {
+ cr_chan_send(&ctx->flush_ch, (nwaiters == 1)
+ ? _LIB9P_SRV_FLUSH_RFLUSH
+ : _LIB9P_SRV_FLUSH_SILENT);
}
- if (host_req)
- free(host_req);
+ if (ctx->parent_sess->closing && !map_len(&ctx->parent_sess->reqs))
+ cr_unpause(ctx->parent_sess->parent_conn->reader);
free(ctx->net_bytes);
}
+static void _srv_respond(struct srv_req *ctx, enum lib9p_msg_type resp_typ, void *host_resp, error err) {
+ assert(!ctx->responded);
+ if (!ERROR_IS_NULL(err)) {
+ if (err.num == E_POSIX_ECANCELED && lib9p_srv_flush_requested(ctx)) {
+ error_cleanup(&err);
+ } else {
+ error:
+ srv_respond_error(ctx, err);
+ }
+ } else {
+ assert(host_resp);
+ err = srv_write_Rmsg(ctx, resp_typ, host_resp);
+ if (!ERROR_IS_NULL(err))
+ goto error;
+ }
+ ctx->responded = true;
+}
+#define srv_respond(CTX, TYP, HOST_RESP, ERR) do { \
+ struct lib9p_msg_R##TYP *_host_resp = HOST_RESP; \
+ _srv_respond(CTX, LIB9P_TYP_R##TYP, _host_resp, ERR); \
+} while (0)
+
/* handle_T* ******************************************************************/
-#define srv_handler_common(ctx, req, resp) do { \
- assert(ctx); \
- assert(req); \
- assert(resp); \
- resp->tag = req->tag; \
- } while (0)
+#define srv_handler_common_no_err(ctx, typ, req) \
+ assert(ctx); \
+ assert(req); \
+ struct lib9p_msg_T##typ *_typecheck_req [[maybe_unused]] = req; \
+ struct lib9p_msg_R##typ resp = { .tag = ctx->tag }
+#define srv_handler_common(ctx, typ, req) \
+ srv_handler_common_no_err(ctx, typ, req); \
+ error err = {}
static void handle_Tversion(struct srv_req *ctx,
- struct lib9p_msg_Tversion *req,
- struct lib9p_msg_Rversion *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tversion *req) {
+ srv_handler_common(ctx, version, req);
- enum lib9p_version version = LIB9P_VER_unknown;
+ enum lib9p_version version = LIB9P_VER_uninitialized;
if (req->version.len >= 6 &&
req->version.utf8[0] == '9' &&
@@ -699,7 +786,9 @@ static void handle_Tversion(struct srv_req *ctx,
'0' <= req->version.utf8[4] && req->version.utf8[4] <= '9' &&
'0' <= req->version.utf8[5] && req->version.utf8[5] <= '9' &&
(req->version.len == 6 || req->version.utf8[6] == '.')) {
+#if CONFIG_9P_ENABLE_9P2000
version = LIB9P_VER_9P2000;
+#endif
#if CONFIG_9P_ENABLE_9P2000_p9p
struct lib9p_srv *srv = ctx->parent_sess->parent_conn->parent_srv;
if (srv->type_assert_unix && !LO_IS_NULL(srv->type_assert_unix(ctx->parent_sess->parent_conn->fd)))
@@ -715,20 +804,23 @@ static void handle_Tversion(struct srv_req *ctx,
#endif
}
- uint32_t min_msg_size = lib9p_version_min_msg_size(version);
+ /* XXX: There are good arguments that min_msg_size should be
+ * something larger than max(rerror.min_size(),
+ * rread.min_size()+1). */
+ uint32_t min_msg_size = _LIB9P_MAX(lib9p_version_min_Rerror_size(ctx->basectx.version),
+ lib9p_version_min_Rread_size(ctx->basectx.version)+1);
if (req->max_msg_size < min_msg_size) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EDOM, "requested max_msg_size is less than minimum for %s (%"PRIu32" < %"PRIu32")",
- lib9p_version_str(version), req->max_msg_size, min_msg_size);
- return;
+ err = error_new(E_POSIX_EDOM, "requested max_msg_size is less than minimum for ", lib9p_version_str(version),
+ " (", req->max_msg_size, " < ", min_msg_size, ")");
+ goto tversion_return;
}
- resp->version = lib9p_str((char *)lib9p_version_str(version)); /* cast to discard "const" qualifier */
+ resp.version = lib9p_str(lib9p_version_str(version));
#if CONFIG_9P_ENABLE_9P2000_p9p
if (version == LIB9P_VER_9P2000_p9p)
- resp->version = lib9p_str("9P2000");
+ resp.version = lib9p_str("9P2000");
#endif
- resp->max_msg_size = (CONFIG_9P_SRV_MAX_MSG_SIZE < req->max_msg_size)
+ resp.max_msg_size = (CONFIG_9P_SRV_MAX_MSG_SIZE < req->max_msg_size)
? CONFIG_9P_SRV_MAX_MSG_SIZE
: req->max_msg_size;
@@ -736,112 +828,110 @@ static void handle_Tversion(struct srv_req *ctx,
if (map_len(&ctx->parent_sess->reqs)) {
/* Flush all in-progress requests, and wait for them
* to finish. */
- struct cr_select_arg *list = alloca(sizeof(struct cr_select_arg) * map_len(&ctx->parent_sess->reqs));
+ struct cr_select_arg *args = heap_alloc(map_len(&ctx->parent_sess->reqs), struct cr_select_arg);
while (map_len(&ctx->parent_sess->reqs)) {
size_t i = 0;
- bool flushed;
MAP_FOREACH(&ctx->parent_sess->reqs, tag, reqpp) {
- list[i] = CR_SELECT_RECV(&((*reqpp)->flushch), &flushed);
+ enum _lib9p_srv_flush_result flushed;
+ args[i++] = CR_SELECT_RECV(&((*reqpp)->flush_ch), &flushed);
}
assert(i == map_len(&ctx->parent_sess->reqs));
- cr_select_v(i, list);
+ cr_select_v(i, args);
}
+ free(args);
}
- if (map_len(&ctx->parent_sess->fids)) {
- /* Close all FIDs. */
- MAP_FOREACH(&ctx->parent_sess->fids, fid, fidinfo) {
- handle_Tclunk(ctx,
- &(struct lib9p_msg_Tclunk){.fid = fid},
- &(struct lib9p_msg_Rclunk){});
+ /* Close all FIDs. */
+ MAP_FOREACH(&ctx->parent_sess->fids, fid, fidinfo) {
+ error fiderr = srv_fid_del(ctx, fid, fidinfo, false);
+ if (!ERROR_IS_NULL(fiderr)) {
+ srv_nonrespond_error("clunk: ", (error, fiderr));
+ error_cleanup(&fiderr);
}
}
/* Replace the old session with the new session. */
ctx->parent_sess->version = version;
- ctx->parent_sess->max_msg_size = resp->max_msg_size;
+ ctx->parent_sess->max_msg_size = resp.max_msg_size;
+
+ tversion_return:
+ srv_respond(ctx, version, &resp, err);
}
+#if _LIB9P_ENABLE_stat
static void handle_Tauth(struct srv_req *ctx,
- struct lib9p_msg_Tauth *req,
- struct lib9p_msg_Rauth *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tauth *req) {
+ srv_handler_common(ctx, auth, req);
struct lib9p_srv *srv = ctx->parent_sess->parent_conn->parent_srv;
if (!srv->auth) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "authentication not required");
- return;
+ err = error_new(E_POSIX_EOPNOTSUPP, "authentication not required");
+ goto tauth_return;
}
- ctx->authinfo = srv_authinfo_new(req->uname, req->n_uid);
+ ctx->user = srv_userid_new(req->uname, req->unum);
- srv->auth(ctx, req->aname);
+ err = srv->auth(ctx, req->aname);
+ if (!ERROR_IS_NULL(err))
+ goto tauth_return;
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "TODO: auth not implemented");
+ err = error_new(E_POSIX_EOPNOTSUPP, "TODO: auth not implemented");
- if (lib9p_ctx_has_error(&ctx->basectx))
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ tauth_return:
+ if (!ERROR_IS_NULL(err) && ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, auth, &resp, err);
}
static void handle_Tattach(struct srv_req *ctx,
- struct lib9p_msg_Tattach *req,
- struct lib9p_msg_Rattach *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tattach *req) {
+ srv_handler_common(ctx, attach, req);
if (req->fid == LIB9P_FID_NOFID) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "cannot assign to NOFID");
- return;
+ err = error_new(E_POSIX_EBADF, "cannot assign to NOFID");
+ goto tattach_return;
}
struct lib9p_srv *srv = ctx->parent_sess->parent_conn->parent_srv;
if (srv->auth) {
struct srv_fidinfo *afid = map_load(&ctx->parent_sess->fids, req->afid);
if (!afid)
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file is not a valid FID");
+ err = error_new(E_POSIX_EACCES, "FID provided as auth-file is not a valid FID");
else if (afid->type != SRV_FILETYPE_AUTH)
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file is not an auth-file");
- else if (!lib9p_str_eq(afid->authinfo->uname, req->uname))
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file is for user=\"%.*s\" and cannot be used for user=\"%.*s\"",
- afid->authinfo->uname.len, afid->authinfo->uname.utf8,
- req->uname.len, req->uname.utf8);
-#if CONFIG_9P_ENABLE_9P2000_u
- else if (afid->authinfo->uid != req->n_uid)
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file is for user=%"PRIu32" and cannot be used for user=%"PRIu32,
- afid->authinfo->uid, req->n_uid);
+ err = error_new(E_POSIX_EACCES, "FID provided as auth-file is not an auth-file");
+ else if (!lib9p_str_eq(afid->user->name, req->uname))
+ err = error_new(E_POSIX_EACCES,
+ "FID provided as auth-file is for user=", (qmem, afid->user->name.utf8, afid->user->name.len),
+ " and cannot be used for user=", (qmem, req->uname.utf8, req->uname.len));
+#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
+ else if (afid->user->num != req->unum)
+ err = error_new(E_POSIX_EACCES,
+ "FID provided as auth-file is for user=", afid->user->num,
+ " and cannot be used for user=", req->unum);
#endif
else if (!lib9p_str_eq(afid->auth.aname, req->aname))
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file is for tree=\"%.*s\" and cannot be used for tree=\"%.*s\"",
- afid->auth.aname.len, afid->auth.aname.utf8,
- req->aname.len, req->aname.utf8);
+ err = error_new(E_POSIX_EACCES,
+ "FID provided as auth-file is for tree=", (qmem, afid->auth.aname.utf8, afid->auth.aname.len),
+ " and cannot be used for tree=", (qmem, req->aname.utf8, req->aname.len));
else if (!afid->auth.completed)
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file has not completed authentication");
- if (lib9p_ctx_has_error(&ctx->basectx))
- return;
- ctx->authinfo = srv_authinfo_incref(afid->authinfo);
+ err = error_new(E_POSIX_EACCES, "FID provided as auth-file has not completed authentication");
+ if (!ERROR_IS_NULL(err))
+ goto tattach_return;
+ ctx->user = srv_userid_incref(afid->user);
} else {
if (req->afid != LIB9P_FID_NOFID) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "FID provided as auth-file, but no auth-file is required");
- return;
+ err = error_new(E_POSIX_EACCES, "FID provided as auth-file, but no auth-file is required");
+ goto tattach_return;
}
- ctx->authinfo = srv_authinfo_new(req->uname, req->n_uid);
+ ctx->user = srv_userid_new(req->uname, req->unum);
}
/* 1. File object */
- lo_interface lib9p_srv_file root_file = srv->rootdir(ctx, req->aname);
- assert(LO_IS_NULL(root_file) == lib9p_ctx_has_error(&ctx->basectx));
- if (lib9p_ctx_has_error(&ctx->basectx)) {
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
- return;
+ lib9p_srv_file_or_error root_file_r = srv->rootdir(ctx, req->aname);
+ if (root_file_r.is_err) {
+ err = root_file_r.err;
+ goto tattach_return;
}
+ lo_interface lib9p_srv_file root_file = root_file_r.lib9p_srv_file;
struct lib9p_qid root_qid = LO_CALL(root_file, qid);
assert(srv_qid_filetype(root_qid) == SRV_FILETYPE_DIR);
@@ -850,134 +940,151 @@ static void handle_Tattach(struct srv_req *ctx,
struct srv_pathinfo *root_pathinfo = srv_path_save(ctx, root_file, root_qid.path);
/* 3. fidinfo */
- if (!srv_fid_store(ctx, req->fid, root_pathinfo, false)) {
+ srv_fidinfop_or_error fidinfo = srv_fid_store(ctx, req->fid, root_pathinfo, false);
+ if (fidinfo.is_err) {
+ err = fidinfo.err;
srv_path_decref(ctx, root_qid.path);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
- return;
+ goto tattach_return;
}
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
- resp->qid = root_qid;
- return;
+ resp.qid = root_qid;
+ tattach_return:
+ if (ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, attach, &resp, err);
}
static void handle_Tflush(struct srv_req *ctx,
- struct lib9p_msg_Tflush *req,
- struct lib9p_msg_Rflush *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tflush *req) {
+ srv_handler_common(ctx, flush, req);
struct srv_req **oldreqp = map_load(&ctx->parent_sess->reqs, req->oldtag);
- if (oldreqp)
- cr_chan_recv(&((*oldreqp)->flushch));
+ if (oldreqp) {
+ struct srv_req *oldreq = *oldreqp;
+ enum _lib9p_srv_flush_result res = _LIB9P_SRV_FLUSH_RFLUSH;
+ switch (cr_select_l(CR_SELECT_RECV(&oldreq->flush_ch, &res),
+ CR_SELECT_SEND(&ctx->flush_ch, &res))) {
+ case 0: /* original request returned */
+ req_debug("original request (tag=", req->oldtag, ") returned");
+ if (res == _LIB9P_SRV_FLUSH_SILENT) {
+ ctx->responded = true;
+ return;
+ }
+ break;
+ case 1: /* flush itself got flushed */
+ ctx->responded = true;
+ return;
+ }
+ }
+ srv_respond(ctx, flush, &resp, err);
}
static void handle_Twalk(struct srv_req *ctx,
- struct lib9p_msg_Twalk *req,
- struct lib9p_msg_Rwalk *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Twalk *req) {
+ srv_handler_common(ctx, walk, req);
if (req->newfid == LIB9P_FID_NOFID) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "cannot assign to NOFID");
- return;
+ err = error_new(E_POSIX_EBADF, "cannot assign to NOFID");
+ goto twalk_return;
}
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
- return;
+ err = error_new(E_POSIX_EBADF, "bad file number ", req->fid);
+ goto twalk_return;
}
if (fidinfo->flags & FIDFLAG_OPEN) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EALREADY, "cannot walk on FID open for I/O");
- return;
+ err = error_new(E_POSIX_EALREADY, "cannot walk on FID open for I/O");
+ goto twalk_return;
}
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
+ ctx->user = srv_userid_incref(fidinfo->user);
struct srv_pathinfo *pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path);
assert(pathinfo);
pathinfo->gc_refcount++;
- resp->wqid = (struct lib9p_qid *)(&resp[1]);
- for (resp->nwqid = 0; resp->nwqid < req->nwname; resp->nwqid++) {
+ resp.wqid = heap_alloc(req->nwname, struct lib9p_qid);
+ for (resp.nwqid = 0; resp.nwqid < req->nwname; resp.nwqid++) {
if (pathinfo->type != SRV_FILETYPE_DIR) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_ENOTDIR, "not a directory");
+ err = error_new(E_POSIX_ENOTDIR, "not a directory");
break;
}
struct srv_pathinfo *new_pathinfo;
- if (lib9p_str_eq(req->wname[resp->nwqid], lib9p_str(".."))) {
+ if (lib9p_str_eq(req->wname[resp.nwqid], lib9p_str(".."))) {
new_pathinfo = map_load(&ctx->parent_sess->paths, pathinfo->parent_dir);
assert(new_pathinfo);
new_pathinfo->gc_refcount++;
} else {
- lo_interface lib9p_srv_file member_file = LO_CALL(pathinfo->file, dwalk, ctx, req->wname[resp->nwqid]);
- assert(LO_IS_NULL(member_file) == lib9p_ctx_has_error(&ctx->basectx));
- if (lib9p_ctx_has_error(&ctx->basectx))
+ lib9p_srv_file_or_error member_file = LO_CALL(pathinfo->file, dwalk, ctx, req->wname[resp.nwqid]);
+ if (member_file.is_err) {
+ err = member_file.err;
break;
- new_pathinfo = srv_path_save(ctx, member_file, LO_CALL(pathinfo->file, qid).path);
+ }
+ new_pathinfo = srv_path_save(ctx, member_file.lib9p_srv_file, LO_CALL(pathinfo->file, qid).path);
assert(new_pathinfo);
}
if (new_pathinfo->type == SRV_FILETYPE_DIR) {
- struct lib9p_stat stat = LO_CALL(new_pathinfo->file, stat, ctx);
- if (lib9p_ctx_has_error(&ctx->basectx))
+ ok_or_error have_perm = srv_file_check_perm(ctx, new_pathinfo->file, 0b001);
+ if (have_perm.is_err) {
+ err = have_perm.err;
break;
- lib9p_stat_assert(stat);
- if (!srv_check_perm(ctx, &stat, 0b001)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "you do not have execute permission on that directory");
+ } else if (!have_perm.ok) {
+ err = error_new(E_POSIX_EACCES, "you do not have execute permission on that directory");
srv_path_decref(ctx, LO_CALL(new_pathinfo->file, qid).path);
break;
}
}
- resp->wqid[resp->nwqid] = LO_CALL(new_pathinfo->file, qid);
+ resp.wqid[resp.nwqid] = LO_CALL(new_pathinfo->file, qid);
srv_path_decref(ctx, LO_CALL(pathinfo->file, qid).path);
pathinfo = new_pathinfo;
}
- if (resp->nwqid == req->nwname) {
- if (!srv_fid_store(ctx, req->newfid, pathinfo, req->newfid == req->fid))
+ if (resp.nwqid == req->nwname) {
+ srv_fidinfop_or_error fidinfo = srv_fid_store(ctx, req->newfid, pathinfo, req->newfid == req->fid);
+ if (fidinfo.is_err) {
+ err = fidinfo.err;
srv_path_decref(ctx, LO_CALL(pathinfo->file, qid).path);
+ }
} else {
- assert(lib9p_ctx_has_error(&ctx->basectx));
+ assert(!ERROR_IS_NULL(err));
srv_path_decref(ctx, LO_CALL(pathinfo->file, qid).path);
- if (resp->nwqid > 0)
- lib9p_ctx_clear_error(&ctx->basectx);
+ if (resp.nwqid > 0)
+ error_cleanup(&err);
}
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ twalk_return:
+ if (ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, walk, &resp, err);
+ if (resp.wqid)
+ free(resp.wqid);
}
static void handle_Topen(struct srv_req *ctx,
- struct lib9p_msg_Topen *req,
- struct lib9p_msg_Ropen *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Topen *req) {
+ srv_handler_common(ctx, open, req);
/* Check that the FID is valid for this. */
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
- return;
+ err = error_new(E_POSIX_EBADF, "bad file number ", req->fid);
+ goto topen_return;
}
if (fidinfo->flags & FIDFLAG_OPEN) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EALREADY, "FID is already open");
- return;
+ err = error_new(E_POSIX_EALREADY, "FID is already open");
+ goto topen_return;
}
if (fidinfo->type == SRV_FILETYPE_DIR) {
if ( ((req->mode & LIB9P_O_MODE_MASK) != LIB9P_O_MODE_READ) ||
(req->mode & LIB9P_O_TRUNC) ||
(req->mode & LIB9P_O_RCLOSE) ) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EISDIR, "directories cannot be written, executed, truncated, or removed-on-close");
- return;
+ err = error_new(E_POSIX_EISDIR, "directories cannot be written, executed, truncated, or removed-on-close");
+ goto topen_return;
}
}
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
+ ctx->user = srv_userid_incref(fidinfo->user);
/* Variables. */
lib9p_o_t reqmode = req->mode;
@@ -989,28 +1096,29 @@ static void handle_Topen(struct srv_req *ctx,
if (reqmode & LIB9P_O_RCLOSE) {
struct srv_pathinfo *parent = map_load(&ctx->parent_sess->paths, pathinfo->parent_dir);
assert(parent);
- struct lib9p_stat parent_stat = LO_CALL(parent->file, stat, ctx);
- if (lib9p_ctx_has_error(&ctx->basectx))
+ ok_or_error have_perm = srv_file_check_perm(ctx, parent->file, 0b010);
+ if (have_perm.is_err) {
+ err = have_perm.err;
goto topen_return;
- lib9p_stat_assert(parent_stat);
- if (!srv_check_perm(ctx, &parent_stat, 0b010)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "permission denied to remove-on-close");
+ } else if (!have_perm.ok) {
+ err = error_new(E_POSIX_EACCES, "permission denied to remove-on-close");
goto topen_return;
}
fidflags |= FIDFLAG_RCLOSE;
}
- struct lib9p_stat stat = LO_CALL(pathinfo->file, stat, ctx);
- if (lib9p_ctx_has_error(&ctx->basectx))
+ struct lib9p_srv_stat stat;
+ err = LO_CALL(pathinfo->file, stat, ctx, &stat);
+ if (!ERROR_IS_NULL(err))
goto topen_return;
- lib9p_stat_assert(stat);
- if ((stat.file_mode & LIB9P_DM_EXCL) && pathinfo->io_refcount) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EEXIST, "exclusive file is already opened");
+ lib9p_srv_stat_assert(&stat);
+ if ((stat.mode & LIB9P_DM_EXCL) && pathinfo->io_refcount) {
+ err = error_new(E_POSIX_EEXIST, "exclusive file is already opened");
goto topen_return;
}
- if (stat.file_mode & LIB9P_DM_APPEND)
+ if (stat.mode & LIB9P_DM_APPEND) {
+ fidflags |= FIDFLAG_APPEND;
reqmode = reqmode & ~LIB9P_O_TRUNC;
+ }
uint8_t perm_bits = 0;
bool rd = false, wr = false;
switch (reqmode & LIB9P_O_MODE_MASK) {
@@ -1031,9 +1139,8 @@ static void handle_Topen(struct srv_req *ctx,
rd = true;
break;
}
- if (!srv_check_perm(ctx, &stat, perm_bits)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EACCES, "permission denied");
+ if (!srv_stat_check_perm(ctx, &stat, perm_bits)) {
+ err = error_new(E_POSIX_EACCES, "permission denied");
goto topen_return;
}
@@ -1042,23 +1149,29 @@ static void handle_Topen(struct srv_req *ctx,
struct lib9p_qid qid;
switch (pathinfo->type) {
case SRV_FILETYPE_DIR:
- fidinfo->dir.io = LO_CALL(pathinfo->file, dopen, ctx);
- assert(LO_IS_NULL(fidinfo->dir.io) == lib9p_ctx_has_error(&ctx->basectx));
- if (lib9p_ctx_has_error(&ctx->basectx))
+ lib9p_srv_dio_or_error dio_r =
+ LO_CALL(pathinfo->file, dopen, ctx);
+ if (dio_r.is_err) {
+ err = dio_r.err;
goto topen_return;
+ }
+ fidinfo->dir.io = dio_r.lib9p_srv_dio;
fidinfo->dir.idx = 0;
fidinfo->dir.off = 0;
- qid = LO_CALL(fidinfo->dir.io, qid);
+ qid = LO_CALL(fidinfo->dir.io, ioqid);
iounit = 0;
break;
case SRV_FILETYPE_FILE:
- fidinfo->file.io = LO_CALL(pathinfo->file, fopen, ctx,
- rd, wr,
- reqmode & LIB9P_O_TRUNC);
- assert(LO_IS_NULL(fidinfo->file.io) == lib9p_ctx_has_error(&ctx->basectx));
- if (lib9p_ctx_has_error(&ctx->basectx))
+ lib9p_srv_fio_or_error fio_r =
+ LO_CALL(pathinfo->file, fopen, ctx,
+ rd, wr,
+ reqmode & LIB9P_O_TRUNC);
+ if (fio_r.is_err) {
+ err = fio_r.err;
goto topen_return;
- qid = LO_CALL(fidinfo->file.io, qid);
+ }
+ fidinfo->file.io = fio_r.lib9p_srv_fio;
+ qid = LO_CALL(fidinfo->file.io, ioqid);
iounit = LO_CALL(fidinfo->file.io, iounit);
break;
case SRV_FILETYPE_AUTH:
@@ -1076,216 +1189,413 @@ static void handle_Topen(struct srv_req *ctx,
fidflags |= FIDFLAG_OPEN_W;
pathinfo->io_refcount++;
fidinfo->flags = fidflags;
- resp->qid = qid;
- resp->iounit = iounit;
+ resp.qid = qid;
+ resp.iounit = iounit;
topen_return:
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ if (ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, open, &resp, err);
}
static void handle_Tcreate(struct srv_req *ctx,
- struct lib9p_msg_Tcreate *req,
- struct lib9p_msg_Rcreate *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tcreate *req) {
+ srv_handler_common(ctx, create, req);
+
+ err = error_new(E_POSIX_EOPNOTSUPP, "create not (yet?) implemented");
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "create not (yet?) implemented");
+ srv_respond(ctx, create, &resp, err);
+}
+
+static void srv_stat_to_net_stat(struct lib9p_stat *out, const struct lib9p_srv_stat *in) {
+ lib9p_srv_stat_assert(in);
+ *out = (struct lib9p_stat){
+ .qid = in->qid,
+ .mode = in->mode,
+ .atime = in->atime_sec,
+ .mtime = in->mtime_sec,
+ .length = in->size,
+ .name = in->name,
+ .owner_uname = in->owner_uid.name,
+ .owner_gname = in->owner_gid.name,
+ .last_modifier_uname = in->last_modifier_uid.name,
+#if CONFIG_9P_ENABLE_9P2000_u
+ .owner_unum = in->owner_uid.num,
+ .owner_gnum = in->owner_gid.num,
+ .last_modifier_unum = in->last_modifier_uid.num,
+ .extension = in->extension,
+#endif
+ };
}
+static void handle_read_file(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count);
+static void handle_read_dir(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count);
+
static void handle_Tread(struct srv_req *ctx,
- struct lib9p_msg_Tread *req,
- struct lib9p_msg_Rread *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tread *req) {
+ assert(ctx);
+ assert(req);
/* TODO: serialize simultaneous reads to the same FID */
+ /* req->count <= CONFIG_9P_SRV_MAX_MSG_SIZE <= CONFIG_9P_SRV_MAX_HOSTMSG_SIZE <= SIZE_MAX */
+ assert(req->count <= SIZE_MAX);
+ /* req->offset is u64, uoff is u64 */
+ static_assert(req->offset <= UOFF_MAX);
+
+ if (req->count > ctx->basectx.max_msg_size - lib9p_version_min_Rread_size(ctx->basectx.version))
+ req->count = ctx->basectx.max_msg_size - lib9p_version_min_Rread_size(ctx->basectx.version);
+
/* Check that the FID is valid for this. */
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
+ srv_respond(ctx, read, NULL, error_new(E_POSIX_EBADF, "bad file number ", req->fid));
return;
}
if (!(fidinfo->flags & FIDFLAG_OPEN_R)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EINVAL, "FID not open for reading");
+ srv_respond(ctx, read, NULL, error_new(E_POSIX_EINVAL, "FID not open for reading"));
return;
}
/* Do it. */
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
+ ctx->user = srv_userid_incref(fidinfo->user);
switch (fidinfo->type) {
case SRV_FILETYPE_DIR:
- /* Translate byte-offset to object-index. */
- size_t idx;
- if (req->offset == 0)
- idx = 0;
- else if (req->offset == fidinfo->dir.off)
- idx = fidinfo->dir.idx;
- else {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EINVAL, "invalid offset (must be 0 or %"PRIu64"): %"PRIu64,
- fidinfo->dir.off, req->offset);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
- return;
- }
- /* Do it. */
- resp->data = (char *)(&resp[1]);
- size_t num = LO_CALL(fidinfo->dir.io, dread, ctx, (uint8_t *)resp->data, req->count, idx);
- /* Translate object-count back to byte-count. */
- uint32_t len = 0;
- for (size_t i = 0; i < num; i++) {
- uint32_t i_len;
- lib9p_stat_validate(&ctx->basectx, req->count, &((uint8_t *)resp->data)[len], &i_len, NULL);
- len += i_len;
- }
- resp->count = len;
- /* Remember. */
- fidinfo->dir.idx = idx+num;
- fidinfo->dir.off = req->offset + len;
+#if _LIB9P_ENABLE_stat
+ handle_read_dir(ctx, fidinfo, req->offset, req->count);
+#else
+ assert_notreached("Tread for directory on protocol version without that");
+#endif
break;
case SRV_FILETYPE_FILE:
- struct iovec iov;
- LO_CALL(fidinfo->file.io, pread, ctx, req->count, req->offset, &iov);
- if (!lib9p_ctx_has_error(&ctx->basectx)) {
- resp->count = iov.iov_len;
- resp->data = iov.iov_base;
- if (resp->count > req->count)
- resp->count = req->count;
- }
+ handle_read_file(ctx, fidinfo, req->offset, req->count);
break;
case SRV_FILETYPE_AUTH:
assert_notreached("TODO: auth not yet implemented");
break;
}
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ ctx->user = srv_userid_decref(ctx->user);
+}
+
+struct rread_writer {
+ struct srv_req *ctx;
+ size_t count;
+ bool written;
+
+};
+LO_IMPLEMENTATION_STATIC(io_writer, struct rread_writer, rread);
+
+static size_t_and_error rread_writev(struct rread_writer *self, const struct wr_iovec *iov, int iovcnt) {
+ assert(self);
+ assert(!self->written);
+ assert(iovcnt == 1);
+ assert(iov);
+ assert(iov->iov_len <= self->count);
+
+ struct lib9p_msg_Rread resp = {
+ .tag = self->ctx->tag,
+ .count = iov->iov_len,
+ .data = iov->iov_write_from,
+ };
+
+ srv_respond(self->ctx, read, &resp, ERROR_NULL);
+
+ self->written = true;
+ return ERROR_AND(size_t, iov->iov_len, ERROR_NULL);
+}
+
+static void handle_read_file(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count) {
+ struct rread_writer _writer = {
+ .ctx = ctx,
+ .count = (size_t) count,
+ .written = false,
+ };
+ lo_interface io_writer writer = LO_BOX(io_writer, &_writer);
+ error err = LO_CALL(fidinfo->file.io, pread, ctx, writer, offset, count);
+ assert(ERROR_IS_NULL(err) == _writer.written);
+ if (!ERROR_IS_NULL(err))
+ srv_respond(ctx, read, NULL, err);
+}
+
+#if _LIB9P_ENABLE_stat
+
+static error stat_and_convert(struct lib9p_stat *out, struct srv_req *ctx, lo_interface lib9p_srv_file file) {
+ struct lib9p_srv_stat hoststat;
+ error err = LO_CALL(file, stat, ctx, &hoststat);
+ if (!ERROR_IS_NULL(err))
+ return err;
+
+ srv_stat_to_net_stat(out, &hoststat);
+ return ERROR_NULL;
}
+static uint32_t_or_error stat_and_encode(struct srv_req *ctx, lo_interface lib9p_srv_file file, void *out, size_t out_len) {
+ struct lib9p_srv_stat hoststat;
+ struct lib9p_stat netstat;
+ error err;
+
+ err = LO_CALL(file, stat, ctx, &hoststat);
+
+ if (!ERROR_IS_NULL(err))
+ return ERROR_NEW_ERR(uint32_t, err);
+ srv_stat_to_net_stat(&netstat, &hoststat);
+ return ERROR_NEW_VAL(uint32_t, lib9p_stat_marshal(&ctx->basectx, out_len, &netstat, (uint8_t *)out));
+}
+
+static void handle_read_dir(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count) {
+ /* Seek. */
+ if (offset == 0) {
+ fidinfo->dir.idx = 0;
+ fidinfo->dir.off = 0;
+ fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){};
+ } else if (offset != fidinfo->dir.off) {
+ srv_respond(ctx, read, NULL, error_new(E_POSIX_EINVAL, "invalid offset (must be 0 or ", fidinfo->dir.off, "): ", offset));
+ return;
+ }
+
+ /* Allocate. */
+ [[gnu::cleanup(heap_cleanup)]] char *heap = NULL;
+ struct lib9p_msg_Rread resp = {
+ .tag = ctx->tag,
+ .data = heap = heap_alloc(count, char),
+ .count = 0,
+ };
+
+ /* Read. */
+ struct srv_pathinfo *dir_pathinfo = NULL;
+ for (;;) {
+ /* 1. Call ->dread() to get `member_dirent`. */
+ struct lib9p_srv_dirent member_dirent;
+ if (fidinfo->dir.buffered_dirent.name.len) {
+ member_dirent = fidinfo->dir.buffered_dirent;
+ } else {
+ lib9p_srv_dirent_or_error r = LO_CALL(fidinfo->dir.io, dread, ctx, fidinfo->dir.idx);
+ if (r.is_err) {
+ if (resp.count) {
+ /* Just do a short-read; discard the error. */
+ error_cleanup(&r.err);
+ break;
+ }
+ srv_respond(ctx, read, NULL, r.err);
+ return;
+ }
+ member_dirent = r.lib9p_srv_dirent;
+ }
+ if (!member_dirent.name.len)
+ /* end-of-directory */
+ break;
+
+ /* 2. Call ->dwalk() to get the `member_file` object to call ->stat() on. */
+ lo_interface lib9p_srv_file member_file;
+ bool free_member_file;
+ {
+ struct srv_pathinfo *member_pathinfo = map_load(&ctx->parent_sess->paths, member_dirent.qid.path);
+ if (member_pathinfo) {
+ member_file = member_pathinfo->file;
+ free_member_file = false;
+ } else {
+ if (!dir_pathinfo)
+ dir_pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path);
+ assert(dir_pathinfo);
+ lib9p_srv_file_or_error r = LO_CALL(dir_pathinfo->file, dwalk, ctx, member_dirent.name);
+ if (r.is_err) {
+ if (resp.count) {
+ /* Just do a short-read; discard the error. */
+ error_cleanup(&r.err);
+ break;
+ }
+ srv_respond(ctx, read, NULL, r.err);
+ return;
+ }
+ member_file = r.lib9p_srv_file;
+ free_member_file = true;
+ }
+ }
+
+ /* 3. Call ->stat() to get `member_stat``. */
+ /* 4. Encode `member_stat` into `resp.data`/`heap` and increment `resp.count`. */
+ uint32_t_or_error r = stat_and_encode(ctx, member_file, &heap[resp.count], count - resp.count);
+ if (free_member_file)
+ LO_CALL(member_file, free);
+ if (r.is_err) {
+ if (resp.count) {
+ /* Just do a short-read; discard the error. */
+ error_cleanup(&r.err);
+ break;
+ }
+ srv_respond(ctx, read, NULL, r.err);
+ return;
+ }
+ uint32_t nbytes = r.uint32_t;
+ if (!nbytes) {
+ if (resp.count) {
+ /* Just do a short-read; discard the error.
+ * But save the member_dirent for next time. */
+ fidinfo->dir.buffered_dirent = member_dirent;
+ break;
+ }
+ srv_respond(ctx, read, NULL,
+ error_new(E_POSIX_ERANGE, "stat object does not fit into negotiated max message size"));
+ return;
+ }
+ resp.count += nbytes;
+ fidinfo->dir.idx++;
+ fidinfo->dir.off += nbytes;
+ fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){};
+ }
+ srv_respond(ctx, read, &resp, ERROR_NULL);
+}
+#endif
+
static void handle_Twrite(struct srv_req *ctx,
- struct lib9p_msg_Twrite *req,
- struct lib9p_msg_Rwrite *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Twrite *req) {
+ srv_handler_common(ctx, write, req);
/* TODO: serialize simultaneous writes to the same FID */
/* Check that the FID is valid for this. */
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
- return;
+ err = error_new(E_POSIX_EBADF, "bad file number ", req->fid);
+ goto twrite_return;
}
if (!(fidinfo->flags & FIDFLAG_OPEN_W)) {
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EINVAL, "FID not open for writing");
- return;
+ err = error_new(E_POSIX_EINVAL, "FID not open for writing");
+ goto twrite_return;
}
+ if (fidinfo->flags & FIDFLAG_APPEND)
+ req->offset = 0;
/* Do it. */
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
- resp->count = LO_CALL(fidinfo->file.io, pwrite, ctx, req->data, req->count, req->offset);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ ctx->user = srv_userid_incref(fidinfo->user);
+ uint32_t_or_error count = LO_CALL(fidinfo->file.io, pwrite, ctx, req->data, req->count, req->offset);
+ if (count.is_err)
+ err = count.err;
+ else
+ resp.count = count.uint32_t;
+ twrite_return:
+ if (ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, write, &resp, err);
}
static void handle_Tclunk(struct srv_req *ctx,
- struct lib9p_msg_Tclunk *req,
- struct lib9p_msg_Rclunk *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tclunk *req) {
+ srv_handler_common_no_err(ctx, clunk, req);
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
+ srv_respond(ctx, clunk, NULL, error_new(E_POSIX_EBADF, "bad file number ", req->fid));
return;
}
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
- srv_fid_del(ctx, req->fid, false);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ srv_respond(ctx, clunk, &resp, ERROR_NULL);
+ /* Yes, don't actually perform the clunk until *after* we send Rclunk. */
+ srv_fid_del(ctx, req->fid, fidinfo, false);
}
static void handle_Tremove(struct srv_req *ctx,
- struct lib9p_msg_Tremove *req,
- struct lib9p_msg_Rremove *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tremove *req) {
+ srv_handler_common(ctx, remove, req);
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
- return;
+ err = error_new(E_POSIX_EBADF, "bad file number ", req->fid);
+ goto tremove_return;
+ }
+
+ bool remove = true;
+ struct srv_pathinfo *pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path);
+ assert(pathinfo);
+ if (pathinfo->parent_dir == fidinfo->path) {
+ err = error_new(E_POSIX_EBUSY, "cannot remove root");
+ remove = false;
+ goto tremove_main;
+ }
+ struct srv_pathinfo *parent = map_load(&ctx->parent_sess->paths, pathinfo->parent_dir);
+ assert(parent);
+ ok_or_error have_perm = srv_file_check_perm(ctx, parent->file, 0b010);
+ if (have_perm.is_err) {
+ err = have_perm.err;
+ remove = false;
+ goto tremove_main;
+ } else if (!have_perm.ok) {
+ err = error_new(E_POSIX_EACCES, "you do not have write permission on the parent directory");
+ remove = false;
+ goto tremove_main;
}
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
- srv_fid_del(ctx, req->fid, true);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ tremove_main:
+ srv_fid_del(ctx, req->fid, fidinfo, remove);
+ tremove_return:
+ srv_respond(ctx, remove, &resp, err);
}
static void handle_Tstat(struct srv_req *ctx,
- struct lib9p_msg_Tstat *req,
- struct lib9p_msg_Rstat *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tstat *req) {
+ srv_handler_common(ctx, stat, req);
struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid);
if (!fidinfo) {
- lib9p_errorf(&ctx->basectx,
- LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid);
- return;
+ err = error_new(E_POSIX_EBADF, "bad file number ", req->fid);
+ goto tstat_return;
}
struct srv_pathinfo *pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path);
assert(pathinfo);
- ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo);
- resp->stat = LO_CALL(pathinfo->file, stat, ctx);
- if (!lib9p_ctx_has_error(&ctx->basectx))
- lib9p_stat_assert(resp->stat);
- ctx->authinfo = srv_authinfo_decref(ctx->authinfo);
+ ctx->user = srv_userid_incref(fidinfo->user);
+ err = stat_and_convert(&resp.stat, ctx, pathinfo->file);
+ tstat_return:
+ if (ctx->user)
+ ctx->user = srv_userid_decref(ctx->user);
+ srv_respond(ctx, stat, &resp, err);
}
static void handle_Twstat(struct srv_req *ctx,
- struct lib9p_msg_Twstat *req,
- struct lib9p_msg_Rwstat *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Twstat *req) {
+ srv_handler_common(ctx, wstat, req);
+
+ err = error_new(E_POSIX_EOPNOTSUPP, "wstat not (yet?) implemented");
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "wstat not (yet?) implemented");
+ srv_respond(ctx, wstat, &resp, err);
}
+#endif
#if CONFIG_9P_ENABLE_9P2000_p9p
static void handle_Topenfd(struct srv_req *ctx,
- struct lib9p_msg_Topenfd *req,
- struct lib9p_msg_Ropenfd *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Topenfd *req) {
+ srv_handler_common(ctx, openfd, req);
+
+ err = error_new(E_POSIX_EOPNOTSUPP, "openfd not (yet?) implemented");
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "openfd not (yet?) implemented");
+ srv_respond(ctx, openfd, &resp, err);
}
#endif
#if CONFIG_9P_ENABLE_9P2000_e
static void handle_Tsession(struct srv_req *ctx,
- struct lib9p_msg_Tsession *req,
- struct lib9p_msg_Rsession *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tsession *req) {
+ srv_handler_common(ctx, session, req);
+
+ err = error_new(E_POSIX_EOPNOTSUPP, "session not (yet?) implemented");
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "session not (yet?) implemented");
+ srv_respond(ctx, session, &resp, err);
}
static void handle_Tsread(struct srv_req *ctx,
- struct lib9p_msg_Tsread *req,
- struct lib9p_msg_Rsread *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tsread *req) {
+ srv_handler_common(ctx, sread, req);
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "sread not (yet?) implemented");
+ err = error_new(E_POSIX_EOPNOTSUPP, "sread not (yet?) implemented");
+
+ srv_respond(ctx, sread, &resp, err);
}
static void handle_Tswrite(struct srv_req *ctx,
- struct lib9p_msg_Tswrite *req,
- struct lib9p_msg_Rswrite *resp) {
- srv_handler_common(ctx, req, resp);
+ struct lib9p_msg_Tswrite *req) {
+ srv_handler_common(ctx, swrite, req);
+
+ err = error_new(E_POSIX_EOPNOTSUPP, "swrite not (yet?) implemented");
- lib9p_error(&ctx->basectx,
- LIB9P_ERRNO_L_EOPNOTSUPP, "swrite not (yet?) implemented");
+ srv_respond(ctx, swrite, &resp, err);
}
#endif