fs.c revision 1.9.42.1 1 /* $NetBSD: fs.c,v 1.9.42.1 2019/06/10 22:10:35 christos Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: fs.c,v 1.9.42.1 2019/06/10 22:10:35 christos Exp $");
31 #endif /* !lint */
32
33 #include <assert.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <puffs.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "ninepuffs.h"
43 #include "nineproto.h"
44
45 #define DO_IO(fname, a1, a2, a3, a4, rv) \
46 puffs_framebuf_seekset(a2, 0); \
47 *(a4) = 0; \
48 rv = fname(a1, a2, a3, a4); \
49 if (rv) errx(1, "p9p_handshake io failed %d, %d", rv, *a4)
50
51 static const char *
52 p9p_ver2str(int version)
53 {
54
55 switch (version) {
56 case P9PROTO_VERSION: return P9PROTO_VERSTR;
57 case P9PROTO_VERSION_U: return P9PROTO_VERSTR_U;
58 }
59 return NULL;
60 }
61
62 struct puffs_node *
63 p9p_handshake(struct puffs_usermount *pu,
64 const char *username, const char *path)
65 {
66 struct puffs9p *p9p = puffs_getspecific(pu);
67 struct puffs_framebuf *pb;
68 struct puffs_node *pn;
69 struct vattr rootva;
70 uint32_t maxreq;
71 uint16_t dummy;
72 p9ptag_t tagid, rtagid;
73 p9pfid_t curfid;
74 const char *p;
75 uint8_t type;
76 int rv, done, x = 1, ncomp;
77 uint16_t strsize;
78 char *str;
79
80 /* send initial handshake */
81 pb = p9pbuf_makeout();
82 p9pbuf_put_1(pb, P9PROTO_T_VERSION);
83 p9pbuf_put_2(pb, P9PROTO_NOTAG);
84 p9pbuf_put_4(pb, p9p->maxreq);
85 p9pbuf_put_str(pb, p9p_ver2str(p9p->protover));
86 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
87
88 puffs_framebuf_recycle(pb);
89 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
90
91 if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_VERSION)
92 errx(1, "server invalid response to Tversion: %d", type);
93 if ((rtagid = p9pbuf_get_tag(pb)) != P9PROTO_NOTAG) {
94 errx(1, "server invalid tag: %d vs. %d",
95 P9PROTO_NOTAG, rtagid);
96 return NULL;
97 }
98 if (p9pbuf_get_4(pb, &maxreq))
99 errx(1, "server invalid response: no request length");
100 if (maxreq < P9P_MINREQLEN)
101 errx(1, "server request length below minimum accepted: "
102 "%d vs. %d", P9P_MINREQLEN, maxreq);
103 p9p->maxreq = maxreq;
104
105 if (p9pbuf_get_str(pb, &str, &strsize))
106 errx(1, "server invalid response: no version");
107 if (strncmp(str, p9p_ver2str(p9p->protover), P9PROTO_VERSTR_MAXLEN) != 0) {
108 errx(1, "server doesn't support %s", p9p_ver2str(p9p->protover));
109 /* Should downgrade from 9P2000.u to 9P2000 if the server request? */
110 }
111
112 /* tell the server we don't support authentication */
113 p9pbuf_recycleout(pb);
114 tagid = NEXTTAG(p9p);
115 p9pbuf_put_1(pb, P9PROTO_T_AUTH);
116 p9pbuf_put_2(pb, tagid);
117 p9pbuf_put_4(pb, P9PROTO_NOFID);
118 p9pbuf_put_str(pb, username);
119 p9pbuf_put_str(pb, "");
120 if (p9p->protover == P9PROTO_VERSION_U)
121 p9pbuf_put_4(pb, P9PROTO_NUNAME_UNSPECIFIED); /* n_uname[4] */
122 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
123
124 puffs_framebuf_recycle(pb);
125 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
126
127 /* assume all Rerror is "no auth" */
128 if (p9pbuf_get_type(pb) != P9PROTO_R_ERROR)
129 errx(1, "mount_9p supports only NO auth");
130 if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
131 errx(1, "server invalid tag: %d vs. %d", tagid, rtagid);
132
133 /* build attach message */
134 p9pbuf_recycleout(pb);
135 tagid = NEXTTAG(p9p);
136 p9pbuf_put_1(pb, P9PROTO_T_ATTACH);
137 p9pbuf_put_2(pb, tagid);
138 p9pbuf_put_4(pb, P9P_ROOTFID);
139 p9pbuf_put_4(pb, P9PROTO_NOFID);
140 p9pbuf_put_str(pb, username);
141 p9pbuf_put_str(pb, "");
142 if (p9p->protover == P9PROTO_VERSION_U)
143 p9pbuf_put_4(pb, P9PROTO_NUNAME_UNSPECIFIED); /* n_uname[4] */
144 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
145
146 puffs_framebuf_recycle(pb);
147 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
148
149 if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_ATTACH)
150 errx(1, "Rattach not received, got %d", type);
151 if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
152 errx(1, "server invalid tag: %d vs. %d", tagid, rtagid);
153
154 /* just walk away rootfid, you won't see me follow you back home */
155
156 #define EATSLASH(p) while (*(p) == '/') p++
157 assert(*path == '/');
158 p = path;
159 EATSLASH(p);
160 for (ncomp = 0; p && *p; ncomp++) {
161 EATSLASH(p);
162 if (!*p)
163 break;
164 p = strchr(p, '/');
165 }
166
167 if (ncomp == 0) {
168 curfid = P9P_ROOTFID;
169 } else {
170 uint16_t walked;
171
172 p9pbuf_recycleout(pb);
173 tagid = NEXTTAG(p9p);
174 curfid = NEXTFID(p9p);
175 p9pbuf_put_1(pb, P9PROTO_T_WALK);
176 p9pbuf_put_2(pb, tagid);
177 p9pbuf_put_4(pb, P9P_ROOTFID);
178 p9pbuf_put_4(pb, curfid);
179 p9pbuf_put_2(pb, ncomp);
180
181 p = path;
182 while (p && *p) {
183 char *p2;
184
185 EATSLASH(p);
186 if (!*p)
187 break;
188 if ((p2 = strchr(p, '/')) == NULL)
189 p2 = strchr(p, '\0');
190 p9pbuf_put_data(pb, p, p2-p);
191 p = p2;
192 }
193
194 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
195
196 puffs_framebuf_recycle(pb);
197 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
198
199 if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_WALK)
200 errx(1, "Rwalk not received for rnode, got %d", type);
201 if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
202 errx(1, "server invalid tag: %d vs. %d",
203 tagid, rtagid);
204 if (p9pbuf_get_2(pb, &walked) == -1)
205 errx(1, "can't get number of walked qids");
206 if (walked != ncomp)
207 errx(1, "can't locate rootpath %s, only %d/%d "
208 "components found", path, walked, ncomp);
209
210 /* curfid is alive, clunk P9P_ROOTFID */
211 p9pbuf_recycleout(pb);
212 tagid = NEXTTAG(p9p);
213 p9pbuf_put_1(pb, P9PROTO_T_CLUNK);
214 p9pbuf_put_2(pb, tagid);
215 p9pbuf_put_4(pb, P9P_ROOTFID);
216
217 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
218 puffs_framebuf_recycle(pb);
219 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
220 /* wedontcare */
221 }
222
223 /* finally, stat the node */
224 p9pbuf_recycleout(pb);
225 tagid = NEXTTAG(p9p);
226 p9pbuf_put_1(pb, P9PROTO_T_STAT);
227 p9pbuf_put_2(pb, tagid);
228 p9pbuf_put_4(pb, curfid);
229 DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
230
231 puffs_framebuf_recycle(pb);
232 DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
233
234 if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_STAT)
235 errx(1, "Rstat not received, got %d", type);
236 if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
237 errx(1, "server invalid tag: %d vs. %d", tagid, rtagid);
238 if (p9pbuf_get_2(pb, &dummy))
239 errx(1, "couldn't get stat len parameter");
240 if (proto_getstat(pu, pb, &rootva, NULL, NULL))
241 errx(1, "could not parse root attributes");
242 puffs_framebuf_destroy(pb);
243
244 rootva.va_nlink = 0156; /* guess, will be fixed with first readdir */
245 pn = newp9pnode_va(pu, &rootva, curfid);
246
247 if (ioctl(p9p->servsock, FIONBIO, &x) == -1)
248 err(1, "cannot set socket in nonblocking mode");
249
250 return pn;
251 }
252
253 int
254 puffs9p_fs_unmount(struct puffs_usermount *pu, int flags)
255 {
256 struct puffs9p *p9p = puffs_getspecific(pu);
257
258 close(p9p->servsock);
259 return 0;
260 }
261