2 * Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
34 #if defined(__FreeBSD__)
37 #include "sbuf/sbuf.h"
40 #include "lib9p_impl.h"
43 #include "hashtable.h"
45 #include "linux_errno.h"
46 #include "backend/backend.h"
47 #include "threadpool.h"
49 #define N(x) (sizeof(x) / sizeof(x[0]))
51 static int l9p_dispatch_tversion(struct l9p_request *req);
52 static int l9p_dispatch_tattach(struct l9p_request *req);
53 static int l9p_dispatch_tclunk(struct l9p_request *req);
54 static int l9p_dispatch_tcreate(struct l9p_request *req);
55 static int l9p_dispatch_topen(struct l9p_request *req);
56 static int l9p_dispatch_tread(struct l9p_request *req);
57 static int l9p_dispatch_tremove(struct l9p_request *req);
58 static int l9p_dispatch_tstat(struct l9p_request *req);
59 static int l9p_dispatch_twalk(struct l9p_request *req);
60 static int l9p_dispatch_twrite(struct l9p_request *req);
61 static int l9p_dispatch_twstat(struct l9p_request *req);
62 static int l9p_dispatch_tstatfs(struct l9p_request *req);
63 static int l9p_dispatch_tlopen(struct l9p_request *req);
64 static int l9p_dispatch_tlcreate(struct l9p_request *req);
65 static int l9p_dispatch_tsymlink(struct l9p_request *req);
66 static int l9p_dispatch_tmknod(struct l9p_request *req);
67 static int l9p_dispatch_trename(struct l9p_request *req);
68 static int l9p_dispatch_treadlink(struct l9p_request *req);
69 static int l9p_dispatch_tgetattr(struct l9p_request *req);
70 static int l9p_dispatch_tsetattr(struct l9p_request *req);
71 static int l9p_dispatch_txattrwalk(struct l9p_request *req);
72 static int l9p_dispatch_txattrcreate(struct l9p_request *req);
73 static int l9p_dispatch_treaddir(struct l9p_request *req);
74 static int l9p_dispatch_tfsync(struct l9p_request *req);
75 static int l9p_dispatch_tlock(struct l9p_request *req);
76 static int l9p_dispatch_tgetlock(struct l9p_request *req);
77 static int l9p_dispatch_tlink(struct l9p_request *req);
78 static int l9p_dispatch_tmkdir(struct l9p_request *req);
79 static int l9p_dispatch_trenameat(struct l9p_request *req);
80 static int l9p_dispatch_tunlinkat(struct l9p_request *req);
83 * Each Txxx handler has a "must run" flag. If it is false,
84 * we check for a flush request before calling the handler.
85 * If a flush is already requested we can instantly fail the
88 * Tclunk and Tremove must run because they make their fids
89 * become invalid. Tversion and Tattach should never get
90 * a flush request applied (it makes no sense as the connection
91 * is not really running yet), so it should be harmless to
92 * set them either way, but for now we have them as must-run.
93 * Flushing a Tflush is not really allowed either so we keep
94 * these as must-run too (although they run without being done
99 int (*handler)(struct l9p_request *);
103 static const struct l9p_handler l9p_handlers_no_version[] = {
104 {L9P_TVERSION, l9p_dispatch_tversion, true},
107 static const struct l9p_handler l9p_handlers_base[] = {
108 {L9P_TVERSION, l9p_dispatch_tversion, true},
109 {L9P_TATTACH, l9p_dispatch_tattach, true},
110 {L9P_TCLUNK, l9p_dispatch_tclunk, true},
111 {L9P_TFLUSH, l9p_threadpool_tflush, true},
112 {L9P_TCREATE, l9p_dispatch_tcreate, false},
113 {L9P_TOPEN, l9p_dispatch_topen, false},
114 {L9P_TREAD, l9p_dispatch_tread, false},
115 {L9P_TWRITE, l9p_dispatch_twrite, false},
116 {L9P_TREMOVE, l9p_dispatch_tremove, true},
117 {L9P_TSTAT, l9p_dispatch_tstat, false},
118 {L9P_TWALK, l9p_dispatch_twalk, false},
119 {L9P_TWSTAT, l9p_dispatch_twstat, false}
121 static const struct l9p_handler l9p_handlers_dotu[] = {
122 {L9P_TVERSION, l9p_dispatch_tversion, true},
123 {L9P_TATTACH, l9p_dispatch_tattach, true},
124 {L9P_TCLUNK, l9p_dispatch_tclunk, true},
125 {L9P_TFLUSH, l9p_threadpool_tflush, true},
126 {L9P_TCREATE, l9p_dispatch_tcreate, false},
127 {L9P_TOPEN, l9p_dispatch_topen, false},
128 {L9P_TREAD, l9p_dispatch_tread, false},
129 {L9P_TWRITE, l9p_dispatch_twrite, false},
130 {L9P_TREMOVE, l9p_dispatch_tremove, true},
131 {L9P_TSTAT, l9p_dispatch_tstat, false},
132 {L9P_TWALK, l9p_dispatch_twalk, false},
133 {L9P_TWSTAT, l9p_dispatch_twstat, false}
135 static const struct l9p_handler l9p_handlers_dotL[] = {
136 {L9P_TVERSION, l9p_dispatch_tversion, true},
137 {L9P_TATTACH, l9p_dispatch_tattach, true},
138 {L9P_TCLUNK, l9p_dispatch_tclunk, true},
139 {L9P_TFLUSH, l9p_threadpool_tflush, true},
140 {L9P_TCREATE, l9p_dispatch_tcreate, false},
141 {L9P_TOPEN, l9p_dispatch_topen, false},
142 {L9P_TREAD, l9p_dispatch_tread, false},
143 {L9P_TWRITE, l9p_dispatch_twrite, false},
144 {L9P_TREMOVE, l9p_dispatch_tremove, true},
145 {L9P_TSTAT, l9p_dispatch_tstat, false},
146 {L9P_TWALK, l9p_dispatch_twalk, false},
147 {L9P_TWSTAT, l9p_dispatch_twstat, false},
148 {L9P_TSTATFS, l9p_dispatch_tstatfs, false},
149 {L9P_TLOPEN, l9p_dispatch_tlopen, false},
150 {L9P_TLCREATE, l9p_dispatch_tlcreate, false},
151 {L9P_TSYMLINK, l9p_dispatch_tsymlink, false},
152 {L9P_TMKNOD, l9p_dispatch_tmknod, false},
153 {L9P_TRENAME, l9p_dispatch_trename, false},
154 {L9P_TREADLINK, l9p_dispatch_treadlink, false},
155 {L9P_TGETATTR, l9p_dispatch_tgetattr, false},
156 {L9P_TSETATTR, l9p_dispatch_tsetattr, false},
157 {L9P_TXATTRWALK, l9p_dispatch_txattrwalk, false},
158 {L9P_TXATTRCREATE, l9p_dispatch_txattrcreate, false},
159 {L9P_TREADDIR, l9p_dispatch_treaddir, false},
160 {L9P_TFSYNC, l9p_dispatch_tfsync, false},
161 {L9P_TLOCK, l9p_dispatch_tlock, true},
162 {L9P_TGETLOCK, l9p_dispatch_tgetlock, true},
163 {L9P_TLINK, l9p_dispatch_tlink, false},
164 {L9P_TMKDIR, l9p_dispatch_tmkdir, false},
165 {L9P_TRENAMEAT, l9p_dispatch_trenameat, false},
166 {L9P_TUNLINKAT, l9p_dispatch_tunlinkat, false},
170 * NB: version index 0 is reserved for new connections, and
171 * is a protocol that handles only L9P_TVERSION. Once we get a
172 * valid version, we start a new session using its dispatch table.
174 static const struct {
176 const struct l9p_handler *handlers;
179 { "<none>", l9p_handlers_no_version, N(l9p_handlers_no_version) },
180 { "9P2000", l9p_handlers_base, N(l9p_handlers_base) },
181 { "9P2000.u", l9p_handlers_dotu, N(l9p_handlers_dotu), },
182 { "9P2000.L", l9p_handlers_dotL, N(l9p_handlers_dotL), },
186 * Run the appropriate handler for this request.
187 * It's our caller's responsibility to respond.
190 l9p_dispatch_request(struct l9p_request *req)
192 struct l9p_connection *conn;
193 #if defined(L9P_DEBUG)
197 const struct l9p_handler *handlers, *hp;
198 bool flush_requested;
201 flush_requested = req->lr_flushstate == L9P_FLUSH_REQUESTED_PRE_START;
203 handlers = l9p_versions[conn->lc_version].handlers;
204 n = (size_t)l9p_versions[conn->lc_version].n_handlers;
205 for (hp = handlers, i = 0; i < n; hp++, i++)
206 if (req->lr_req.hdr.type == hp->type)
211 #if defined(L9P_DEBUG)
212 sb = sbuf_new_auto();
213 if (flush_requested) {
214 sbuf_cat(sb, "FLUSH requested pre-dispatch");
215 if (hp != NULL && hp->must_run)
216 sbuf_cat(sb, ", but must run");
219 l9p_describe_fcall(&req->lr_req, conn->lc_version, sb);
222 L9P_LOG(L9P_DEBUG, "%s", sbuf_data(sb));
227 if (!flush_requested || hp->must_run)
228 return (hp->handler(req));
232 L9P_LOG(L9P_WARNING, "unknown request of type %d",
233 req->lr_req.hdr.type);
238 * Translate BSD errno to 9P2000/9P2000.u errno.
243 static int const table[] = {
246 [ENOSYS] = EPERM, /* ??? */
249 if ((size_t)errnum < N(table) && table[errnum] != 0)
250 return (table[errnum]);
251 if (errnum <= ERANGE)
253 return (EIO); /* ??? */
257 * Translate BSD errno to Linux errno.
262 static int const table[] = {
263 [EDEADLK] = LINUX_EDEADLK,
264 [EAGAIN] = LINUX_EAGAIN,
265 [EINPROGRESS] = LINUX_EINPROGRESS,
266 [EALREADY] = LINUX_EALREADY,
267 [ENOTSOCK] = LINUX_ENOTSOCK,
268 [EDESTADDRREQ] = LINUX_EDESTADDRREQ,
269 [EMSGSIZE] = LINUX_EMSGSIZE,
270 [EPROTOTYPE] = LINUX_EPROTOTYPE,
271 [ENOPROTOOPT] = LINUX_ENOPROTOOPT,
272 [EPROTONOSUPPORT] = LINUX_EPROTONOSUPPORT,
273 [ESOCKTNOSUPPORT] = LINUX_ESOCKTNOSUPPORT,
274 [EOPNOTSUPP] = LINUX_EOPNOTSUPP,
275 [EPFNOSUPPORT] = LINUX_EPFNOSUPPORT,
276 [EAFNOSUPPORT] = LINUX_EAFNOSUPPORT,
277 [EADDRINUSE] = LINUX_EADDRINUSE,
278 [EADDRNOTAVAIL] = LINUX_EADDRNOTAVAIL,
279 [ENETDOWN] = LINUX_ENETDOWN,
280 [ENETUNREACH] = LINUX_ENETUNREACH,
281 [ENETRESET] = LINUX_ENETRESET,
282 [ECONNABORTED] = LINUX_ECONNABORTED,
283 [ECONNRESET] = LINUX_ECONNRESET,
284 [ENOBUFS] = LINUX_ENOBUFS,
285 [EISCONN] = LINUX_EISCONN,
286 [ENOTCONN] = LINUX_ENOTCONN,
287 [ESHUTDOWN] = LINUX_ESHUTDOWN,
288 [ETOOMANYREFS] = LINUX_ETOOMANYREFS,
289 [ETIMEDOUT] = LINUX_ETIMEDOUT,
290 [ECONNREFUSED] = LINUX_ECONNREFUSED,
291 [ELOOP] = LINUX_ELOOP,
292 [ENAMETOOLONG] = LINUX_ENAMETOOLONG,
293 [EHOSTDOWN] = LINUX_EHOSTDOWN,
294 [EHOSTUNREACH] = LINUX_EHOSTUNREACH,
295 [ENOTEMPTY] = LINUX_ENOTEMPTY,
296 [EPROCLIM] = LINUX_EAGAIN,
297 [EUSERS] = LINUX_EUSERS,
298 [EDQUOT] = LINUX_EDQUOT,
299 [ESTALE] = LINUX_ESTALE,
300 [EREMOTE] = LINUX_EREMOTE,
301 /* EBADRPC = unmappable? */
302 /* ERPCMISMATCH = unmappable? */
303 /* EPROGUNAVAIL = unmappable? */
304 /* EPROGMISMATCH = unmappable? */
305 /* EPROCUNAVAIL = unmappable? */
306 [ENOLCK] = LINUX_ENOLCK,
307 [ENOSYS] = LINUX_ENOSYS,
308 /* EFTYPE = unmappable? */
309 /* EAUTH = unmappable? */
310 /* ENEEDAUTH = unmappable? */
311 [EIDRM] = LINUX_EIDRM,
312 [ENOMSG] = LINUX_ENOMSG,
313 [EOVERFLOW] = LINUX_EOVERFLOW,
314 [ECANCELED] = LINUX_ECANCELED,
315 [EILSEQ] = LINUX_EILSEQ,
316 /* EDOOFUS = unmappable? */
317 [EBADMSG] = LINUX_EBADMSG,
318 [EMULTIHOP] = LINUX_EMULTIHOP,
319 [ENOLINK] = LINUX_ENOLINK,
320 [EPROTO] = LINUX_EPROTO,
321 /* ENOTCAPABLE = unmappable? */
325 #ifdef ENOTRECOVERABLE
326 [ENOTRECOVERABLE] = LINUX_ENOTRECOVERABLE,
329 [EOWNERDEAD] = LINUX_EOWNERDEAD,
334 * In case we want to return a raw Linux errno, allow negative
335 * values a la Linux kernel internals.
337 * Values up to ERANGE are shared across systems (see
338 * linux_errno.h), except for EAGAIN.
343 if ((size_t)errnum < N(table) && table[errnum] != 0)
344 return (table[errnum]);
346 if (errnum <= ERANGE)
349 L9P_LOG(L9P_WARNING, "cannot map errno %d to anything reasonable",
352 return (LINUX_ENOTRECOVERABLE); /* ??? */
356 * Send response to request, or possibly just drop request.
357 * We also need to know whether to remove the request from
358 * the tag hash table.
361 l9p_respond(struct l9p_request *req, bool drop, bool rmtag)
363 struct l9p_connection *conn = req->lr_conn;
365 #if defined(L9P_DEBUG)
371 req->lr_resp.hdr.tag = req->lr_req.hdr.tag;
373 error = req->lr_error;
375 req->lr_resp.hdr.type = req->lr_req.hdr.type + 1;
377 if (conn->lc_version == L9P_2000L) {
378 req->lr_resp.hdr.type = L9P_RLERROR;
379 req->lr_resp.error.errnum = (uint32_t)e2linux(error);
381 req->lr_resp.hdr.type = L9P_RERROR;
382 req->lr_resp.error.ename = strerror(error);
383 req->lr_resp.error.errnum = (uint32_t)e29p(error);
387 #if defined(L9P_DEBUG)
388 sb = sbuf_new_auto();
389 l9p_describe_fcall(&req->lr_resp, conn->lc_version, sb);
392 switch (req->lr_flushstate) {
396 case L9P_FLUSH_REQUESTED_PRE_START:
397 ftype = "FLUSH requested pre-dispatch: ";
399 case L9P_FLUSH_REQUESTED_POST_START:
400 ftype = "FLUSH requested while running: ";
402 case L9P_FLUSH_TOOLATE:
403 ftype = "FLUSH requested too late: ";
406 L9P_LOG(L9P_DEBUG, "%s%s%s",
407 drop ? "DROP: " : "", ftype, sbuf_data(sb));
412 l9p_pufcall(&req->lr_resp_msg, &req->lr_resp, conn->lc_version);
414 ht_remove(&conn->lc_requests, req->lr_req.hdr.tag);
416 L9P_LOG(L9P_ERROR, "cannot pack response");
421 conn->lc_lt.lt_drop_response(req,
422 req->lr_resp_msg.lm_iov, req->lr_resp_msg.lm_niov,
425 iosize = req->lr_resp_msg.lm_size;
428 * Include I/O size in calculation for Rread and
429 * Rreaddir responses.
431 if (req->lr_resp.hdr.type == L9P_RREAD ||
432 req->lr_resp.hdr.type == L9P_RREADDIR)
433 iosize += req->lr_resp.io.count;
435 conn->lc_lt.lt_send_response(req,
436 req->lr_resp_msg.lm_iov, req->lr_resp_msg.lm_niov,
437 iosize, conn->lc_lt.lt_aux);
440 l9p_freefcall(&req->lr_req);
441 l9p_freefcall(&req->lr_resp);
447 * This allows a caller to iterate through the data in a
448 * read or write request (creating the data if packing,
449 * scanning through it if unpacking). This is used for
450 * writing readdir entries, so mode should be L9P_PACK
451 * (but we allow L9P_UNPACK so that debug code can also scan
452 * through the data later, if desired).
454 * This relies on the Tread op having positioned the request's
455 * iov to the beginning of the data buffer (note the l9p_seek_iov
456 * in l9p_dispatch_tread).
459 l9p_init_msg(struct l9p_message *msg, struct l9p_request *req,
460 enum l9p_pack_mode mode)
465 msg->lm_cursor_iov = 0;
466 msg->lm_cursor_offset = 0;
467 msg->lm_niov = req->lr_data_niov;
468 memcpy(msg->lm_iov, req->lr_data_iov,
469 sizeof (struct iovec) * req->lr_data_niov);
472 enum fid_lookup_flags {
473 F_REQUIRE_OPEN = 0x01, /* require that the file be marked OPEN */
474 F_REQUIRE_DIR = 0x02, /* require that the file be marked ISDIR */
475 F_REQUIRE_XATTR = 0x04, /* require that the file be marked XATTR */
476 F_REQUIRE_AUTH = 0x08, /* require that the fid be marked AUTH */
477 F_FORBID_OPEN = 0x10, /* forbid that the file be marked OPEN */
478 F_FORBID_DIR = 0x20, /* forbid that the file be marked ISDIR */
479 F_FORBID_XATTR = 0x40, /* forbid that the file be marked XATTR */
480 F_ALLOW_AUTH = 0x80, /* allow that the fid be marked AUTH */
484 * Look up a fid. It must correspond to a valid file, else we return
485 * the given errno (some "not a valid fid" calls must return EIO and
486 * some must return EINVAL and qemu returns ENOENT in other cases and
487 * so on, so we just provide a general "return this error number").
489 * Callers may also set constraints: fid must be (or not be) open,
490 * must be (or not be) a directory, must be (or not be) an xattr.
492 * Only one op has a fid that *must* be an auth fid. Most ops forbid
493 * auth fids So instead of FORBID we have ALLOW here and the default
497 fid_lookup(struct l9p_connection *conn, uint32_t fid, int err, int flags,
498 struct l9p_fid **afile)
500 struct l9p_fid *file;
502 file = ht_find(&conn->lc_files, fid);
507 * As soon as we go multithreaded / async, this
508 * assert has to become "return EINVAL" or "return err".
510 * We may also need a way to mark a fid as
511 * "in async op" (valid for some purposes, but cannot be
512 * used elsewhere until async op is completed or aborted).
514 * For now, this serves for bug-detecting.
516 assert(l9p_fid_isvalid(file));
519 * Note that we're inline expanded and flags is constant,
520 * so unnecessary tests just drop out entirely.
522 if ((flags & F_REQUIRE_OPEN) && !l9p_fid_isopen(file))
524 if ((flags & F_FORBID_OPEN) && l9p_fid_isopen(file))
526 if ((flags & F_REQUIRE_DIR) && !l9p_fid_isdir(file))
528 if ((flags & F_FORBID_DIR) && l9p_fid_isdir(file))
530 if ((flags & F_REQUIRE_XATTR) && !l9p_fid_isxattr(file))
532 if ((flags & F_FORBID_XATTR) && l9p_fid_isxattr(file))
534 if (l9p_fid_isauth(file)) {
535 if ((flags & (F_REQUIRE_AUTH | F_ALLOW_AUTH)) == 0)
537 } else if (flags & F_REQUIRE_AUTH)
544 * Append variable-size stat object and adjust io count.
545 * Returns 0 if the entire stat object was packed, -1 if not.
546 * A fully packed object updates the request's io count.
548 * Caller must use their own private l9p_message object since
549 * a partially packed object will leave the message object in
552 * Frees the stat object.
555 l9p_pack_stat(struct l9p_message *msg, struct l9p_request *req,
558 struct l9p_connection *conn = req->lr_conn;
559 uint16_t size = l9p_sizeof_stat(st, conn->lc_version);
562 assert(msg->lm_mode == L9P_PACK);
564 if (req->lr_resp.io.count + size > req->lr_req.io.count ||
565 l9p_pustat(msg, st, conn->lc_version) < 0)
568 req->lr_resp.io.count += size;
574 l9p_dispatch_tversion(struct l9p_request *req)
576 struct l9p_connection *conn = req->lr_conn;
577 struct l9p_server *server = conn->lc_server;
578 enum l9p_version remote_version = L9P_INVALID_VERSION;
580 const char *remote_version_name;
582 for (i = 0; i < N(l9p_versions); i++) {
583 if (strcmp(req->lr_req.version.version,
584 l9p_versions[i].name) == 0) {
585 remote_version = (enum l9p_version)i;
590 if (remote_version == L9P_INVALID_VERSION) {
591 L9P_LOG(L9P_ERROR, "unsupported remote version: %s",
592 req->lr_req.version.version);
596 remote_version_name = l9p_versions[remote_version].name;
597 L9P_LOG(L9P_INFO, "remote version: %s", remote_version_name);
598 L9P_LOG(L9P_INFO, "local version: %s",
599 l9p_versions[server->ls_max_version].name);
601 conn->lc_version = MIN(remote_version, server->ls_max_version);
602 conn->lc_msize = MIN(req->lr_req.version.msize, conn->lc_msize);
603 conn->lc_max_io_size = conn->lc_msize - 24;
604 req->lr_resp.version.version = strdup(remote_version_name);
605 req->lr_resp.version.msize = conn->lc_msize;
610 l9p_dispatch_tattach(struct l9p_request *req)
612 struct l9p_connection *conn = req->lr_conn;
613 struct l9p_backend *be;
618 * We still don't have Tauth yet, but let's code this part
621 * Look up the auth fid first since if it fails we can just
622 * return immediately.
624 if (req->lr_req.tattach.afid != L9P_NOFID) {
625 error = fid_lookup(conn, req->lr_req.tattach.afid, EINVAL,
626 F_REQUIRE_AUTH, &req->lr_fid2);
632 fid = l9p_connection_alloc_fid(conn, req->lr_req.hdr.fid);
636 be = conn->lc_server->ls_backend;
640 /* For backend convenience, set NONUNAME on 9P2000. */
641 if (conn->lc_version == L9P_2000)
642 req->lr_req.tattach.n_uname = L9P_NONUNAME;
643 error = be->attach(be->softc, req);
646 * On success, fid becomes valid; on failure, disconnect.
647 * It certainly *should* be a directory here...
650 l9p_fid_setvalid(fid);
651 if (req->lr_resp.rattach.qid.type & L9P_QTDIR)
654 l9p_connection_remove_fid(conn, fid);
659 l9p_dispatch_tclunk(struct l9p_request *req)
661 struct l9p_connection *conn = req->lr_conn;
662 struct l9p_backend *be;
666 /* Note that clunk is the only way to dispose of an auth fid. */
667 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
672 be = conn->lc_server->ls_backend;
673 l9p_fid_unsetvalid(fid);
676 * If it's an xattr fid there must, by definition, be an
677 * xattrclunk. The xattrclunk function can only be NULL if
678 * xattrwalk and xattrcreate are NULL or always return error.
680 * Q: do we want to allow async xattrclunk in case of very
681 * large xattr create? This will make things difficult,
684 if (l9p_fid_isxattr(fid))
685 error = be->xattrclunk(be->softc, fid);
687 error = be->clunk(be->softc, fid);
689 /* fid is now gone regardless of any error return */
690 l9p_connection_remove_fid(conn, fid);
695 l9p_dispatch_tcreate(struct l9p_request *req)
697 struct l9p_connection *conn = req->lr_conn;
698 struct l9p_backend *be;
702 /* Incoming fid must represent a directory that has not been opened. */
703 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL,
704 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
708 be = conn->lc_server->ls_backend;
709 dmperm = req->lr_req.tcreate.perm;
710 #define MKDIR_OR_SIMILAR \
711 (L9P_DMDIR | L9P_DMSYMLINK | L9P_DMNAMEDPIPE | L9P_DMSOCKET | L9P_DMDEVICE)
715 * - check new file name
716 * - break out different kinds of create (file vs mkdir etc)
717 * - add async file-create (leaves req->lr_fid in limbo)
719 * A successful file-create changes the fid into an open file.
721 error = be->create(be->softc, req);
722 if (error == 0 && (dmperm & MKDIR_OR_SIMILAR) == 0) {
723 l9p_fid_unsetdir(req->lr_fid);
724 l9p_fid_setopen(req->lr_fid);
731 l9p_dispatch_topen(struct l9p_request *req)
733 struct l9p_connection *conn = req->lr_conn;
734 struct l9p_backend *be;
737 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
738 F_FORBID_OPEN | F_FORBID_XATTR, &req->lr_fid);
742 be = conn->lc_server->ls_backend;
746 * - add async open (leaves req->lr_fid in limbo)
748 error = be->open(be->softc, req);
750 l9p_fid_setopen(req->lr_fid);
755 l9p_dispatch_tread(struct l9p_request *req)
757 struct l9p_connection *conn = req->lr_conn;
758 struct l9p_backend *be;
762 /* Xattr fids are not open, so we need our own tests. */
763 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL, 0, &req->lr_fid);
768 * Adjust so that writing messages (packing data) starts
769 * right after the count field in the response.
771 * size[4] + Rread[1] + tag[2] + count[4] = 11
773 l9p_seek_iov(req->lr_resp_msg.lm_iov, req->lr_resp_msg.lm_niov,
774 req->lr_data_iov, &req->lr_data_niov, 11);
777 * If it's an xattr fid there must, by definition, be an
778 * xattrread. The xattrread function can only be NULL if
779 * xattrwalk and xattrcreate are NULL or always return error.
782 * separate out directory-read
785 be = conn->lc_server->ls_backend;
787 if (l9p_fid_isxattr(fid)) {
788 error = be->xattrread(be->softc, req);
789 } else if (l9p_fid_isopen(fid)) {
790 error = be->read(be->softc, req);
799 l9p_dispatch_tremove(struct l9p_request *req)
801 struct l9p_connection *conn = req->lr_conn;
802 struct l9p_backend *be;
807 * ?? Should we allow Tremove on auth fids? If so, do
808 * we pretend it is just a Tclunk?
810 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL, 0, &fid);
814 be = conn->lc_server->ls_backend;
815 l9p_fid_unsetvalid(fid);
817 error = be->remove(be->softc, fid);
818 /* fid is now gone regardless of any error return */
819 l9p_connection_remove_fid(conn, fid);
824 l9p_dispatch_tstat(struct l9p_request *req)
826 struct l9p_connection *conn = req->lr_conn;
827 struct l9p_backend *be;
831 /* Allow Tstat on auth fid? Seems harmless enough... */
832 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
837 be = conn->lc_server->ls_backend;
839 error = be->stat(be->softc, req);
842 if (l9p_fid_isauth(fid))
843 req->lr_resp.rstat.stat.qid.type |= L9P_QTAUTH;
845 /* should we check req->lr_resp.rstat.qid.type L9P_QTDIR bit? */
846 if (req->lr_resp.rstat.stat.qid.type &= L9P_QTDIR)
849 l9p_fid_unsetdir(fid);
856 l9p_dispatch_twalk(struct l9p_request *req)
858 struct l9p_connection *conn = req->lr_conn;
859 struct l9p_backend *be;
860 struct l9p_fid *fid, *newfid;
864 /* Can forbid XATTR, but cannot require DIR. */
865 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
866 F_FORBID_XATTR, &fid);
870 if (req->lr_req.twalk.hdr.fid != req->lr_req.twalk.newfid) {
871 newfid = l9p_connection_alloc_fid(conn,
872 req->lr_req.twalk.newfid);
878 be = conn->lc_server->ls_backend;
880 req->lr_newfid = newfid;
881 error = be->walk(be->softc, req);
884 * If newfid == fid, then fid itself has (potentially) changed,
885 * but is still valid. Otherwise set newfid valid on
886 * success, and destroy it on error.
890 l9p_fid_setvalid(newfid);
892 l9p_connection_remove_fid(conn, newfid);
896 * If we walked any name elements, the last (n-1'th) qid
897 * has the type (dir vs file) for the new fid. Otherwise
898 * the type of newfid is the same as fid. Of course, if
899 * n==0 and fid==newfid, fid is already set up correctly
900 * as the whole thing was a big no-op, but it's safe to
901 * copy its dir bit to itself.
904 n = req->lr_resp.rwalk.nwqid;
906 if (req->lr_resp.rwalk.wqid[n - 1].type & L9P_QTDIR)
907 l9p_fid_setdir(newfid);
909 if (l9p_fid_isdir(fid))
910 l9p_fid_setdir(newfid);
917 l9p_dispatch_twrite(struct l9p_request *req)
919 struct l9p_connection *conn = req->lr_conn;
920 struct l9p_backend *be;
924 /* Cannot require open due to xattr write, but can forbid dir. */
925 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL,
926 F_FORBID_DIR, &req->lr_fid);
931 * Adjust to point to the data to be written (a la
932 * l9p_dispatch_tread, but we're pointing into the request
933 * buffer rather than the response):
935 * size[4] + Twrite[1] + tag[2] + fid[4] + offset[8] + count[4] = 23
937 l9p_seek_iov(req->lr_req_msg.lm_iov, req->lr_req_msg.lm_niov,
938 req->lr_data_iov, &req->lr_data_niov, 23);
941 * Unlike read, write and xattrwrite are optional (for R/O fs).
946 be = conn->lc_server->ls_backend;
948 if (l9p_fid_isxattr(fid)) {
949 error = be->xattrwrite != NULL ?
950 be->xattrwrite(be->softc, req) : ENOSYS;
951 } else if (l9p_fid_isopen(fid)) {
952 error = be->write != NULL ?
953 be->write(be->softc, req) : ENOSYS;
962 l9p_dispatch_twstat(struct l9p_request *req)
964 struct l9p_connection *conn = req->lr_conn;
965 struct l9p_backend *be;
968 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL,
969 F_FORBID_XATTR, &req->lr_fid);
973 be = conn->lc_server->ls_backend;
974 error = be->wstat != NULL ? be->wstat(be->softc, req) : ENOSYS;
979 l9p_dispatch_tstatfs(struct l9p_request *req)
981 struct l9p_connection *conn = req->lr_conn;
982 struct l9p_backend *be;
985 /* Should we allow statfs on auth fids? */
986 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL, 0, &req->lr_fid);
990 be = conn->lc_server->ls_backend;
991 error = be->statfs(be->softc, req);
996 l9p_dispatch_tlopen(struct l9p_request *req)
998 struct l9p_connection *conn = req->lr_conn;
999 struct l9p_backend *be;
1002 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1003 F_FORBID_OPEN | F_FORBID_XATTR, &req->lr_fid);
1007 be = conn->lc_server->ls_backend;
1011 * - add async open (leaves req->lr_fid in limbo)
1013 error = be->lopen != NULL ? be->lopen(be->softc, req) : ENOSYS;
1015 l9p_fid_setopen(req->lr_fid);
1020 l9p_dispatch_tlcreate(struct l9p_request *req)
1022 struct l9p_connection *conn = req->lr_conn;
1023 struct l9p_backend *be;
1026 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1027 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1031 be = conn->lc_server->ls_backend;
1035 * - check new file name
1036 * - add async create (leaves req->lr_fid in limbo)
1038 error = be->lcreate != NULL ? be->lcreate(be->softc, req) : ENOSYS;
1040 l9p_fid_unsetdir(req->lr_fid);
1041 l9p_fid_setopen(req->lr_fid);
1047 l9p_dispatch_tsymlink(struct l9p_request *req)
1049 struct l9p_connection *conn = req->lr_conn;
1050 struct l9p_backend *be;
1053 /* This doesn't affect the containing dir; maybe allow OPEN? */
1054 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1055 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1059 be = conn->lc_server->ls_backend;
1063 * - check new file name
1065 error = be->symlink != NULL ? be->symlink(be->softc, req) : ENOSYS;
1070 l9p_dispatch_tmknod(struct l9p_request *req)
1072 struct l9p_connection *conn = req->lr_conn;
1073 struct l9p_backend *be;
1076 /* This doesn't affect the containing dir; maybe allow OPEN? */
1077 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1078 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1082 be = conn->lc_server->ls_backend;
1086 * - check new file name
1088 error = be->mknod != NULL ? be->mknod(be->softc, req) : ENOSYS;
1093 l9p_dispatch_trename(struct l9p_request *req)
1095 struct l9p_connection *conn = req->lr_conn;
1096 struct l9p_backend *be;
1099 /* Rename directory or file (including symlink etc). */
1100 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1101 F_FORBID_XATTR, &req->lr_fid);
1105 /* Doesn't affect new dir fid; maybe allow OPEN? */
1106 error = fid_lookup(conn, req->lr_req.trename.dfid, ENOENT,
1107 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid2);
1111 be = conn->lc_server->ls_backend;
1115 * - check new file name (trename.name)
1117 error = be->rename != NULL ? be->rename(be->softc, req) : ENOSYS;
1122 l9p_dispatch_treadlink(struct l9p_request *req)
1124 struct l9p_connection *conn = req->lr_conn;
1125 struct l9p_backend *be;
1129 * The underlying readlink will fail unless it's a symlink,
1130 * and the back end has to check, but we might as well forbid
1131 * directories and open files here since it's cheap.
1133 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1134 F_FORBID_DIR | F_FORBID_OPEN, &req->lr_fid);
1138 be = conn->lc_server->ls_backend;
1140 error = be->readlink != NULL ? be->readlink(be->softc, req) : ENOSYS;
1145 l9p_dispatch_tgetattr(struct l9p_request *req)
1147 struct l9p_connection *conn = req->lr_conn;
1148 struct l9p_backend *be;
1151 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1152 F_FORBID_XATTR, &req->lr_fid);
1156 be = conn->lc_server->ls_backend;
1158 error = be->getattr != NULL ? be->getattr(be->softc, req) : ENOSYS;
1163 l9p_dispatch_tsetattr(struct l9p_request *req)
1165 struct l9p_connection *conn = req->lr_conn;
1166 struct l9p_backend *be;
1169 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1170 F_FORBID_XATTR, &req->lr_fid);
1174 be = conn->lc_server->ls_backend;
1176 error = be->setattr != NULL ? be->setattr(be->softc, req) : ENOSYS;
1181 l9p_dispatch_txattrwalk(struct l9p_request *req)
1183 struct l9p_connection *conn = req->lr_conn;
1184 struct l9p_backend *be;
1185 struct l9p_fid *fid, *newfid;
1189 * Not sure if we care if file-or-dir is open or not.
1190 * However, the fid argument should always be a file or
1191 * dir and the newfid argument must be supplied, must
1192 * be different, and always becomes a new xattr,
1193 * so this is not very much like Twalk.
1195 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1196 F_FORBID_XATTR, &fid);
1200 newfid = l9p_connection_alloc_fid(conn, req->lr_req.txattrwalk.newfid);
1204 be = conn->lc_server->ls_backend;
1207 req->lr_newfid = newfid;
1208 error = be->xattrwalk != NULL ? be->xattrwalk(be->softc, req) : ENOSYS;
1211 * Success/fail is similar to Twalk, except that we need
1212 * to set the xattr type bit in the new fid. It's also
1213 * much simpler since newfid is always a new fid.
1216 l9p_fid_setvalid(newfid);
1217 l9p_fid_setxattr(newfid);
1219 l9p_connection_remove_fid(conn, newfid);
1225 l9p_dispatch_txattrcreate(struct l9p_request *req)
1227 struct l9p_connection *conn = req->lr_conn;
1228 struct l9p_backend *be;
1229 struct l9p_fid *fid;
1233 * Forbid incoming open fid since it's going to become an
1234 * xattr fid instead. If it turns out we need to allow
1235 * it, fs code will need to handle this.
1237 * Curiously, qemu 9pfs uses ENOENT for a bad txattrwalk
1238 * fid, but EINVAL for txattrcreate (so we do too).
1240 error = fid_lookup(conn, req->lr_req.hdr.fid, EINVAL,
1241 F_FORBID_XATTR | F_FORBID_OPEN, &fid);
1245 be = conn->lc_server->ls_backend;
1248 error = be->xattrcreate != NULL ? be->xattrcreate(be->softc, req) :
1252 * On success, fid has changed from a regular (file or dir)
1253 * fid to an xattr fid.
1256 l9p_fid_unsetdir(fid);
1257 l9p_fid_setxattr(fid);
1263 l9p_dispatch_treaddir(struct l9p_request *req)
1265 struct l9p_connection *conn = req->lr_conn;
1266 struct l9p_backend *be;
1269 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1270 F_REQUIRE_DIR | F_REQUIRE_OPEN, &req->lr_fid);
1275 * Adjust so that writing messages (packing data) starts
1276 * right after the count field in the response.
1278 * size[4] + Rreaddir[1] + tag[2] + count[4] = 11
1280 l9p_seek_iov(req->lr_resp_msg.lm_iov, req->lr_resp_msg.lm_niov,
1281 req->lr_data_iov, &req->lr_data_niov, 11);
1283 be = conn->lc_server->ls_backend;
1285 error = be->readdir != NULL ? be->readdir(be->softc, req) : ENOSYS;
1290 l9p_dispatch_tfsync(struct l9p_request *req)
1292 struct l9p_connection *conn = req->lr_conn;
1293 struct l9p_backend *be;
1296 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1297 F_REQUIRE_OPEN, &req->lr_fid);
1301 be = conn->lc_server->ls_backend;
1303 error = be->fsync != NULL ? be->fsync(be->softc, req) : ENOSYS;
1308 l9p_dispatch_tlock(struct l9p_request *req)
1310 struct l9p_connection *conn = req->lr_conn;
1311 struct l9p_backend *be;
1314 /* Forbid directories? */
1315 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1316 F_REQUIRE_OPEN, &req->lr_fid);
1320 be = conn->lc_server->ls_backend;
1323 * TODO: multiple client handling; perhaps async locking.
1325 error = be->lock != NULL ? be->lock(be->softc, req) : ENOSYS;
1330 l9p_dispatch_tgetlock(struct l9p_request *req)
1332 struct l9p_connection *conn = req->lr_conn;
1333 struct l9p_backend *be;
1336 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1337 F_REQUIRE_OPEN, &req->lr_fid);
1341 be = conn->lc_server->ls_backend;
1344 * TODO: multiple client handling; perhaps async locking.
1346 error = be->getlock != NULL ? be->getlock(be->softc, req) : ENOSYS;
1351 l9p_dispatch_tlink(struct l9p_request *req)
1353 struct l9p_connection *conn = req->lr_conn;
1354 struct l9p_backend *be;
1358 * Note, dfid goes into fid2 in current scheme.
1360 * Allow open dir? Target dir fid is not modified...
1362 error = fid_lookup(conn, req->lr_req.tlink.dfid, ENOENT,
1363 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid2);
1367 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1368 F_FORBID_DIR | F_FORBID_XATTR, &req->lr_fid);
1372 be = conn->lc_server->ls_backend;
1374 error = be->link != NULL ? be->link(be->softc, req) : ENOSYS;
1379 l9p_dispatch_tmkdir(struct l9p_request *req)
1381 struct l9p_connection *conn = req->lr_conn;
1382 struct l9p_backend *be;
1385 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1386 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1390 /* Slashes embedded in the name are not allowed */
1391 if (strchr(req->lr_req.tlcreate.name, '/') != NULL)
1394 be = conn->lc_server->ls_backend;
1395 error = be->mkdir != NULL ? be->mkdir(be->softc, req) : ENOSYS;
1400 l9p_dispatch_trenameat(struct l9p_request *req)
1402 struct l9p_connection *conn = req->lr_conn;
1403 struct l9p_backend *be;
1406 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1407 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1411 error = fid_lookup(conn, req->lr_req.trenameat.newdirfid, ENOENT,
1412 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid2);
1416 be = conn->lc_server->ls_backend;
1418 /* TODO: check old and new names */
1419 error = be->renameat != NULL ? be->renameat(be->softc, req) : ENOSYS;
1424 l9p_dispatch_tunlinkat(struct l9p_request *req)
1426 struct l9p_connection *conn = req->lr_conn;
1427 struct l9p_backend *be;
1430 error = fid_lookup(conn, req->lr_req.hdr.fid, ENOENT,
1431 F_REQUIRE_DIR | F_FORBID_OPEN, &req->lr_fid);
1435 be = conn->lc_server->ls_backend;
1437 /* TODO: check dir-or-file name */
1438 error = be->unlinkat != NULL ? be->unlinkat(be->softc, req) : ENOSYS;