fs.c revision 1.1 1 /* $NetBSD: fs.c,v 1.1 2007/04/21 14:21:43 pooka 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.1 2007/04/21 14:21:43 pooka Exp $");
31 #endif /* !lint */
32
33 #include <err.h>
34 #include <errno.h>
35 #include <puffs.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "ninepuffs.h"
42 #include "nineproto.h"
43
44 struct puffs_node *
45 p9p_handshake(struct puffs_usermount *pu, const char *username)
46 {
47 struct puffs9p *p9p = puffs_getspecific(pu);
48 struct p9pbuf *pb;
49 struct puffs_node *pn;
50 struct vattr rootva;
51 uint32_t maxreq;
52 uint16_t dummy;
53 p9ptag_t tagid;
54 int rv, x = 1;
55
56 /* send initial handshake */
57 pb = p9pbuf_make(p9p->maxreq, P9PB_OUT);
58 p9pbuf_put_1(pb, P9PROTO_T_VERSION);
59 p9pbuf_put_2(pb, P9PROTO_NOTAG);
60 p9pbuf_put_4(pb, p9p->maxreq);
61 p9pbuf_put_str(pb, P9PROTO_VERSION);
62 while ((rv = p9pbuf_write(p9p, pb)) != 1) {
63 if (rv == -1) {
64 warn("Tversion send failed");
65 return NULL;
66 }
67 }
68
69 p9pbuf_recycle(pb, P9PB_IN);
70 while ((rv = p9pbuf_read(p9p, pb)) != 1) {
71 if (rv == -1) {
72 warn("Rversion receive failed");
73 return NULL;
74 }
75 }
76
77 if (pb->type != P9PROTO_R_VERSION) {
78 warnx("server invalid response to Tversion: %d", pb->type);
79 return NULL;
80 }
81 if (pb->tagid != P9PROTO_NOTAG) {
82 warnx("server invalid tag: %d vs. %d\n",
83 P9PROTO_NOTAG, pb->tagid);
84 return NULL;
85 }
86 if (!p9pbuf_get_4(pb, &maxreq)) {
87 warnx("server invalid response: no request length");
88 return NULL;
89 }
90 if (maxreq < P9P_MINREQLEN) {
91 warnx("server request length below minimum accepted: %d vs. %d",
92 P9P_MINREQLEN, maxreq);
93 return NULL;
94 }
95 p9p->maxreq = maxreq;
96
97 /* tell the server we don't support authentication */
98 tagid = NEXTTAG(p9p);
99 p9pbuf_recycle(pb, P9PB_OUT);
100 p9pbuf_put_1(pb, P9PROTO_T_AUTH);
101 p9pbuf_put_2(pb, tagid);
102 p9pbuf_put_4(pb, P9PROTO_NOFID);
103 p9pbuf_put_str(pb, username);
104 p9pbuf_put_str(pb, "");
105 while ((rv = p9pbuf_write(p9p, pb)) != 1) {
106 if (rv == -1) {
107 warn("Tauth send failed");
108 return NULL;
109 }
110 }
111
112 p9pbuf_recycle(pb, P9PB_IN);
113 while ((rv = p9pbuf_read(p9p, pb)) != 1) {
114 if (rv == -1) {
115 warn("Rauth receive failed");
116 return NULL;
117 }
118 }
119
120 /* assume all Rerror is "no auth" */
121 if (pb->type != P9PROTO_R_ERROR) {
122 warnx("mount_9p supports only NO auth");
123 return NULL;
124 }
125 if (pb->tagid != tagid) {
126 warnx("server invalid tag: %d vs. %d\n", tagid, pb->tagid);
127 return NULL;
128 }
129
130 /* build attach message */
131 tagid = NEXTTAG(p9p);
132 p9pbuf_recycle(pb, P9PB_OUT);
133 p9pbuf_put_1(pb, P9PROTO_T_ATTACH);
134 p9pbuf_put_2(pb, tagid);
135 p9pbuf_put_4(pb, P9P_ROOTFID);
136 p9pbuf_put_4(pb, P9PROTO_NOFID);
137 p9pbuf_put_str(pb, username);
138 p9pbuf_put_str(pb, "");
139 while ((rv = p9pbuf_write(p9p, pb)) != 1) {
140 if (rv == -1) {
141 warn("Tattach send failed");
142 return NULL;
143 }
144 }
145
146 p9pbuf_recycle(pb, P9PB_IN);
147 while ((rv = p9pbuf_read(p9p, pb)) != 1) {
148 if (rv == -1) {
149 warn("Rattach receive failed");
150 return NULL;
151 }
152 }
153
154 if (pb->type != P9PROTO_R_ATTACH) {
155 warnx("Rattach not received, got %d", pb->type);
156 return NULL;
157 }
158 if (pb->tagid != tagid) {
159 warnx("server invalid tag: %d vs. %d\n", tagid, pb->tagid);
160 return NULL;
161 }
162 #if 0
163 if (!proto_getqid(pb, rqid)) {
164 warnx("cannot read root node qid");
165 return NULL;
166 }
167 #endif
168
169 /* finally, stat the rootnode */
170 tagid = NEXTTAG(p9p);
171 p9pbuf_recycle(pb, P9PB_OUT);
172 p9pbuf_put_1(pb, P9PROTO_T_STAT);
173 p9pbuf_put_2(pb, tagid);
174 p9pbuf_put_4(pb, P9P_ROOTFID);
175 while ((rv = p9pbuf_write(p9p, pb)) != 1) {
176 if (rv == -1) {
177 warn("Tstat send failed");
178 return NULL;
179 }
180 }
181
182 p9pbuf_recycle(pb, P9PB_IN);
183 while ((rv = p9pbuf_read(p9p, pb)) != 1) {
184 if (rv == -1) {
185 warn("Rstat receive failed");
186 return NULL;
187 }
188 }
189
190 if (pb->type != P9PROTO_R_STAT) {
191 warnx("Rstat not received, got %d", pb->type);
192 return NULL;
193 }
194 if (pb->tagid != tagid) {
195 warnx("server invalid tag: %d vs. %d\n", tagid, pb->tagid);
196 return NULL;
197 }
198 if (!p9pbuf_get_2(pb, &dummy)) {
199 warnx("couldn't get stat len parameter");
200 return NULL;
201 }
202 if (!proto_getstat(pb, &rootva, NULL, NULL)) {
203 warnx("could not parse root attributes");
204 return NULL;
205 }
206 p9pbuf_destroy(pb);
207
208 rootva.va_nlink = 0156; /* guess, will be fixed with first readdir */
209 pn = newp9pnode_va(pu, &rootva, P9P_ROOTFID);
210
211 if (ioctl(p9p->servsock, FIONBIO, &x) == -1) {
212 warnx("cannot set socket in nonblocking mode");
213 return NULL;
214 }
215
216 return pn;
217 }
218
219 int
220 puffs9p_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
221 {
222 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
223 struct puffs9p *p9p = puffs_getspecific(pu);
224
225 close(p9p->servsock);
226 return 0;
227 }
228