xref: /netbsd-src/usr.sbin/puffs/mount_9p/fs.c (revision 63a7cdaa84548cb085681f0166c05d68b6d0cbc3)
1 /*	$NetBSD: fs.c,v 1.11 2020/05/28 14:00:05 uwe 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.11 2020/05/28 14:00:05 uwe 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 *
p9p_ver2str(int version)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 *
p9p_handshake(struct puffs_usermount * pu,const char * username,const char * path)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 	/* build attach message */
113 	p9pbuf_recycleout(pb);
114 	tagid = NEXTTAG(p9p);
115 	p9pbuf_put_1(pb, P9PROTO_T_ATTACH);
116 	p9pbuf_put_2(pb, tagid);
117 	p9pbuf_put_4(pb, P9P_ROOTFID);
118 	p9pbuf_put_4(pb, P9PROTO_NOFID);
119 	p9pbuf_put_str(pb, username);
120 	p9pbuf_put_str(pb, "");
121 	if (p9p->protover == P9PROTO_VERSION_U)
122 		p9pbuf_put_4(pb, P9PROTO_NUNAME_UNSPECIFIED); /* n_uname[4] */
123 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
124 
125 	puffs_framebuf_recycle(pb);
126 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
127 
128 	if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_ATTACH)
129 		errx(1, "Rattach not received, got %d", type);
130 	if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
131 		errx(1, "server invalid tag: %d vs. %d", tagid, rtagid);
132 
133 	/* just walk away rootfid, you won't see me follow you back home */
134 
135 #define EATSLASH(p) while (*(p) == '/') p++
136 	assert(*path == '/');
137 	p = path;
138 	EATSLASH(p);
139 	for (ncomp = 0; p && *p; ncomp++) {
140 		EATSLASH(p);
141 		if (!*p)
142 			break;
143 		p = strchr(p, '/');
144 	}
145 
146 	if (ncomp == 0) {
147 		curfid = P9P_ROOTFID;
148 	} else {
149 		uint16_t walked;
150 
151 		p9pbuf_recycleout(pb);
152 		tagid = NEXTTAG(p9p);
153 		curfid = NEXTFID(p9p);
154 		p9pbuf_put_1(pb, P9PROTO_T_WALK);
155 		p9pbuf_put_2(pb, tagid);
156 		p9pbuf_put_4(pb, P9P_ROOTFID);
157 		p9pbuf_put_4(pb, curfid);
158 		p9pbuf_put_2(pb, ncomp);
159 
160 		p = path;
161 		while (p && *p) {
162 			char *p2;
163 
164 			EATSLASH(p);
165 			if (!*p)
166 				break;
167 			if ((p2 = strchr(p, '/')) == NULL)
168 				p2 = strchr(p, '\0');
169 			p9pbuf_put_data(pb, p, p2-p);
170 			p = p2;
171 		}
172 
173 		DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
174 
175 		puffs_framebuf_recycle(pb);
176 		DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
177 
178 		if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_WALK)
179 			errx(1, "Rwalk not received for rnode, got %d", type);
180 		if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
181 			errx(1, "server invalid tag: %d vs. %d",
182 			    tagid, rtagid);
183 		if (p9pbuf_get_2(pb, &walked) == -1)
184 			errx(1, "can't get number of walked qids");
185 		if (walked != ncomp)
186 			errx(1, "can't locate rootpath %s, only %d/%d "
187 			    "components found", path, walked, ncomp);
188 
189 		/* curfid is alive, clunk P9P_ROOTFID */
190 		p9pbuf_recycleout(pb);
191 		tagid = NEXTTAG(p9p);
192 		p9pbuf_put_1(pb, P9PROTO_T_CLUNK);
193 		p9pbuf_put_2(pb, tagid);
194 		p9pbuf_put_4(pb, P9P_ROOTFID);
195 
196 		DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
197 		puffs_framebuf_recycle(pb);
198 		DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
199 		/* wedontcare */
200 	}
201 
202 	/* finally, stat the node */
203 	p9pbuf_recycleout(pb);
204 	tagid = NEXTTAG(p9p);
205 	p9pbuf_put_1(pb, P9PROTO_T_STAT);
206 	p9pbuf_put_2(pb, tagid);
207 	p9pbuf_put_4(pb, curfid);
208 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
209 
210 	puffs_framebuf_recycle(pb);
211 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
212 
213 	if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_STAT)
214 		errx(1, "Rstat not received, got %d", type);
215 	if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
216 		errx(1, "server invalid tag: %d vs. %d", tagid, rtagid);
217 	if (p9pbuf_get_2(pb, &dummy))
218 		errx(1, "couldn't get stat len parameter");
219 	if (proto_getstat(pu, pb, &rootva, NULL, NULL))
220 		errx(1, "could not parse root attributes");
221 	puffs_framebuf_destroy(pb);
222 
223 	rootva.va_nlink = 0156; /* guess, will be fixed with first readdir */
224 	pn = newp9pnode_va(pu, &rootva, curfid);
225 
226 	if (ioctl(p9p->servsock, FIONBIO, &x) == -1)
227 		err(1, "cannot set socket in nonblocking mode");
228 
229 	return pn;
230 }
231 
232 int
puffs9p_fs_unmount(struct puffs_usermount * pu,int flags)233 puffs9p_fs_unmount(struct puffs_usermount *pu, int flags)
234 {
235 	struct puffs9p *p9p = puffs_getspecific(pu);
236 
237 	close(p9p->servsock);
238 	return 0;
239 }
240