ninepuffs.c revision 1.2 1 /* $NetBSD: ninepuffs.c,v 1.2 2007/04/22 18:10:48 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 /*
29 * 9puffs: access a 9P file server as a vfs via puffs
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: ninepuffs.c,v 1.2 2007/04/22 18:10:48 pooka Exp $");
35 #endif /* !lint */
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/poll.h>
40
41 #include <netinet/in.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <puffs.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #include "ninepuffs.h"
52
53 #define DEFPORT_9P 564
54
55 static void puffs9p_eventloop(struct puffs_usermount *, struct puffs9p *);
56
57 static void
58 usage(void)
59 {
60
61 errx(1, "usage: %s user server mountpoint", getprogname());
62 }
63
64 /*
65 * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
66 * Return connected socket or exit with error.
67 */
68 static int
69 serverconnect(const char *addr, unsigned short port)
70 {
71 struct sockaddr_in mysin;
72 struct hostent *he;
73 int s;
74
75 he = gethostbyname2(addr, AF_INET);
76 if (he == NULL) {
77 herror("gethostbyname");
78 exit(1);
79 }
80
81 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
82 if (s == -1)
83 err(1, "socket");
84
85 memset(&mysin, 0, sizeof(struct sockaddr_in));
86 mysin.sin_family = AF_INET;
87 mysin.sin_port = htons(port);
88 memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
89
90 if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
91 err(1, "connect");
92
93 return s;
94 }
95
96 int
97 main(int argc, char *argv[])
98 {
99 struct puffs9p p9p;
100 struct puffs_usermount *pu;
101 struct puffs_ops *pops;
102 struct puffs_node *pn_root;
103 struct statvfs svfsb;
104 mntoptparse_t mp;
105 char *srvhost;
106 char *user;
107 unsigned short port;
108 int mntflags, pflags, ch;
109 int detach;
110
111 setprogname(argv[0]);
112
113 if (argc < 3)
114 usage();
115
116 mntflags = pflags = 0;
117 detach = 1;
118 port = DEFPORT_9P;
119
120 while ((ch = getopt(argc, argv, "o:p:s")) != -1) {
121 switch (ch) {
122 case 'o':
123 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
124 if (mp == NULL)
125 err(1, "getmntopts");
126 freemntopts(mp);
127 break;
128 case 'p':
129 port = atoi(optarg);
130 break;
131 case 's':
132 detach = 0;
133 break;
134 default:
135 usage();
136 /*NOTREACHED*/
137 }
138 }
139 argc -= optind;
140 argv += optind;
141
142 if (argc != 3)
143 usage();
144
145 user = argv[0];
146 srvhost = argv[1];
147
148 if (pflags & PUFFS_FLAG_OPDUMP)
149 detach = 0;
150 pflags |= PUFFS_KFLAG_WTCACHE;
151
152 PUFFSOP_INIT(pops);
153
154 PUFFSOP_SET(pops, puffs9p, fs, unmount);
155 PUFFSOP_SETFSNOP(pops, sync);
156 PUFFSOP_SETFSNOP(pops, statvfs);
157
158 PUFFSOP_SET(pops, puffs9p, node, lookup);
159 PUFFSOP_SET(pops, puffs9p, node, readdir);
160 PUFFSOP_SET(pops, puffs9p, node, getattr);
161 PUFFSOP_SET(pops, puffs9p, node, setattr);
162 PUFFSOP_SET(pops, puffs9p, node, create);
163 PUFFSOP_SET(pops, puffs9p, node, open);
164 PUFFSOP_SET(pops, puffs9p, node, close);
165 PUFFSOP_SET(pops, puffs9p, node, mkdir);
166 PUFFSOP_SET(pops, puffs9p, node, remove);
167 PUFFSOP_SET(pops, puffs9p, node, rmdir);
168 PUFFSOP_SET(pops, puffs9p, node, read);
169 PUFFSOP_SET(pops, puffs9p, node, write);
170 PUFFSOP_SET(pops, puffs9p, node, rename);
171 #if 0
172 PUFFSOP_SET(pops, puffs9p, node, reclaim);
173 PUFFSOP_SET(pops, puffs9p, node, mknod);
174 #endif
175
176 memset(&p9p, 0, sizeof(p9p));
177 p9p.maxreq = P9P_DEFREQLEN;
178 p9p.nextfid = 1;
179 TAILQ_INIT(&p9p.outbufq);
180 TAILQ_INIT(&p9p.req_queue);
181
182 p9p.servsock = serverconnect(srvhost, port);
183 pu = puffs_init(pops, "9P", &p9p, pflags);
184 if (pu == NULL)
185 err(1, "puffs_init");
186
187 if ((pn_root = p9p_handshake(pu, user)) == NULL) {
188 puffs_exit(pu, 1);
189 exit(1);
190 }
191
192 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
193 err(1, "setblockingmode");
194
195 if (puffs_domount(pu, argv[2], mntflags) == -1)
196 err(1, "puffs_domount");
197
198 puffs_zerostatvfs(&svfsb);
199 if (puffs_start(pu, pn_root, &svfsb) == -1)
200 err(1, "puffs_start");
201
202 if (detach)
203 daemon(1, 0);
204
205 puffs9p_eventloop(pu, &p9p);
206
207 return 0;
208 }
209
210 /*
211 * enqueue buffer to be handled with cc
212 */
213 void
214 outbuf_enqueue(struct puffs9p *p9p, struct p9pbuf *pb,
215 struct puffs_cc *pcc, uint16_t tagid)
216 {
217
218 pb->p9pr.tagid = tagid;
219 pb->p9pr.pcc = pcc;
220 pb->p9pr.func = NULL;
221 pb->p9pr.arg = NULL;
222 TAILQ_INSERT_TAIL(&p9p->outbufq, pb, p9pr.entries);
223 }
224
225 /*
226 * enqueue buffer to be handled with "f". "f" must not block.
227 * gives up struct p9pbuf ownership.
228 */
229 void
230 outbuf_enqueue_nocc(struct puffs9p *p9p, struct p9pbuf *pb,
231 void (*f)(struct puffs9p *, struct p9pbuf *, void *), void *arg,
232 uint16_t tagid)
233 {
234
235 pb->p9pr.tagid = tagid;
236 pb->p9pr.pcc = NULL;
237 pb->p9pr.func = f;
238 pb->p9pr.arg = arg;
239 TAILQ_INSERT_TAIL(&p9p->outbufq, pb, p9pr.entries);
240 }
241
242 struct p9pbuf *
243 req_get(struct puffs9p *p9p, uint16_t tagid)
244 {
245 struct p9pbuf *pb;
246
247 TAILQ_FOREACH(pb, &p9p->req_queue, p9pr.entries)
248 if (pb->p9pr.tagid == tagid)
249 break;
250
251 if (!pb)
252 return NULL;
253
254 TAILQ_REMOVE(&p9p->req_queue, pb, p9pr.entries);
255
256 return pb;
257 }
258
259 static void
260 handlebuf(struct puffs9p *p9p, struct p9pbuf *datapb,
261 struct puffs_putreq *ppr)
262 {
263 struct p9preq p9prtmp;
264 struct p9pbuf *pb;
265
266 /* is this something we are expecting? */
267 pb = req_get(p9p, datapb->tagid);
268
269 if (pb == NULL) {
270 printf("invalid server request response %d\n", datapb->tagid);
271 p9pbuf_destroy(datapb);
272 return;
273 }
274
275 /* keep p9preq clean, xxx uknow */
276 p9prtmp = pb->p9pr;
277 *pb = *datapb;
278 pb->p9pr = p9prtmp;
279 free(datapb);
280
281 /* don't allow both cc and handler func, but allow neither */
282 assert((pb->p9pr.pcc && pb->p9pr.func) == 0);
283 if (pb->p9pr.pcc) {
284 puffs_docc(pb->p9pr.pcc, ppr);
285 } else if (pb->p9pr.func) {
286 pb->p9pr.func(p9p, pb, pb->p9pr.arg);
287 } else {
288 assert(pb->p9pr.arg == NULL);
289 p9pbuf_destroy(pb);
290 }
291 }
292
293 static int
294 psshinput(struct puffs9p *p9p, struct puffs_putreq *ppr)
295 {
296 struct p9pbuf *cb;
297 int rv;
298
299 for (;;) {
300 if ((cb = p9p->curpb) == NULL) {
301 cb = p9pbuf_make(p9p->maxreq, P9PB_IN);
302 if (cb == NULL)
303 return -1;
304 p9p->curpb = cb;
305 }
306
307 rv = p9pbuf_read(p9p, cb);
308 if (rv == -1)
309 err(1, "p9pbuf read");
310 if (rv == 0)
311 break;
312
313 handlebuf(p9p, cb, ppr);
314 p9p->curpb = NULL;
315 }
316
317 return rv;
318 }
319
320 static int
321 psshoutput(struct puffs9p *p9p)
322 {
323 struct p9pbuf *pb;
324 int rv;
325
326 TAILQ_FOREACH(pb, &p9p->outbufq, p9pr.entries) {
327 rv = p9pbuf_write(p9p, pb);
328 if (rv == -1)
329 return -1;
330 if (rv == 0)
331 return 0;
332
333 /* sent everything, move to cookiequeue */
334 TAILQ_REMOVE(&p9p->outbufq, pb, p9pr.entries);
335 free(pb->buf);
336 TAILQ_INSERT_TAIL(&p9p->req_queue, pb, p9pr.entries);
337 }
338
339 return 1;
340 }
341
342 #define PFD_SOCK 0
343 #define PFD_PUFFS 1
344 static void
345 puffs9p_eventloop(struct puffs_usermount *pu, struct puffs9p *p9p)
346 {
347 struct puffs_getreq *pgr;
348 struct puffs_putreq *ppr;
349 struct pollfd pfds[2];
350
351 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
352 if (!pgr)
353 err(1, "makegetreq");
354 ppr = puffs_req_makeput(pu);
355 if (!ppr)
356 err(1, "makeputreq");
357
358 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
359 memset(pfds, 0, sizeof(pfds));
360 pfds[PFD_SOCK].events = POLLIN;
361 if (!TAILQ_EMPTY(&p9p->outbufq))
362 pfds[PFD_SOCK].events |= POLLOUT;
363 pfds[PFD_SOCK].fd = p9p->servsock;
364 pfds[PFD_PUFFS].fd = puffs_getselectable(pu);
365 pfds[PFD_PUFFS].events = POLLIN;
366
367 if (poll(pfds, 2, INFTIM) == -1)
368 err(1, "poll");
369
370 if (pfds[PFD_SOCK].revents & POLLOUT)
371 if (psshoutput(p9p) == -1)
372 err(1, "psshoutput");
373
374 /* get & possibly dispatch events from kernel */
375 if (pfds[PFD_PUFFS].revents & POLLIN)
376 if (puffs_req_handle(pu, pgr, ppr, 0) == -1)
377 err(1, "puffs_handlereqs");
378
379 /* get input from sftpd, possibly build more responses */
380 if (pfds[PFD_SOCK].revents & POLLIN)
381 if (psshinput(p9p, ppr) == -1)
382 errx(1, "psshinput");
383
384 /* it's likely we got outputtables, poke the ice with a stick */
385 if (psshoutput(p9p) == -1)
386 err(1, "psshoutput");
387
388 /* stuff all replies from both of the above into kernel */
389 if (puffs_req_putput(ppr) == -1)
390 err(1, "putputreq");
391 puffs_req_resetput(ppr);
392 }
393
394 puffs_req_destroyget(pgr);
395 }
396