ninepuffs.c revision 1.1 1 /* $NetBSD: ninepuffs.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 /*
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.1 2007/04/21 14:21:43 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
151 PUFFSOP_INIT(pops);
152
153 PUFFSOP_SET(pops, puffs9p, fs, unmount);
154 PUFFSOP_SETFSNOP(pops, sync);
155 PUFFSOP_SETFSNOP(pops, statvfs);
156
157 PUFFSOP_SET(pops, puffs9p, node, lookup);
158 PUFFSOP_SET(pops, puffs9p, node, readdir);
159 PUFFSOP_SET(pops, puffs9p, node, getattr);
160 PUFFSOP_SET(pops, puffs9p, node, setattr);
161 PUFFSOP_SET(pops, puffs9p, node, create);
162 PUFFSOP_SET(pops, puffs9p, node, open);
163 PUFFSOP_SET(pops, puffs9p, node, close);
164 PUFFSOP_SET(pops, puffs9p, node, mkdir);
165 PUFFSOP_SET(pops, puffs9p, node, remove);
166 PUFFSOP_SET(pops, puffs9p, node, rmdir);
167 PUFFSOP_SET(pops, puffs9p, node, read);
168 PUFFSOP_SET(pops, puffs9p, node, write);
169 PUFFSOP_SET(pops, puffs9p, node, rename);
170 #if 0
171 PUFFSOP_SET(pops, puffs9p, node, reclaim);
172 PUFFSOP_SET(pops, puffs9p, node, mknod);
173 #endif
174
175 memset(&p9p, 0, sizeof(p9p));
176 p9p.maxreq = P9P_DEFREQLEN;
177 p9p.nextfid = 1;
178 TAILQ_INIT(&p9p.outbufq);
179 TAILQ_INIT(&p9p.req_queue);
180
181 p9p.servsock = serverconnect(srvhost, port);
182 pu = puffs_init(pops, "9P", &p9p, pflags);
183 if (pu == NULL)
184 err(1, "puffs_init");
185
186 if ((pn_root = p9p_handshake(pu, user)) == NULL) {
187 puffs_exit(pu, 1);
188 exit(1);
189 }
190
191 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
192 err(1, "setblockingmode");
193
194 if (puffs_domount(pu, argv[2], mntflags) == -1)
195 err(1, "puffs_domount");
196
197 puffs_zerostatvfs(&svfsb);
198 if (puffs_start(pu, pn_root, &svfsb) == -1)
199 err(1, "puffs_start");
200
201 if (detach)
202 daemon(1, 0);
203
204 puffs9p_eventloop(pu, &p9p);
205
206 return 0;
207 }
208
209 /*
210 * enqueue buffer to be handled with cc
211 */
212 void
213 outbuf_enqueue(struct puffs9p *p9p, struct p9pbuf *pb,
214 struct puffs_cc *pcc, uint16_t tagid)
215 {
216
217 pb->p9pr.tagid = tagid;
218 pb->p9pr.pcc = pcc;
219 pb->p9pr.func = NULL;
220 pb->p9pr.arg = NULL;
221 TAILQ_INSERT_TAIL(&p9p->outbufq, pb, p9pr.entries);
222 }
223
224 /*
225 * enqueue buffer to be handled with "f". "f" must not block.
226 * gives up struct p9pbuf ownership.
227 */
228 void
229 outbuf_enqueue_nocc(struct puffs9p *p9p, struct p9pbuf *pb,
230 void (*f)(struct puffs9p *, struct p9pbuf *, void *), void *arg,
231 uint16_t tagid)
232 {
233
234 pb->p9pr.tagid = tagid;
235 pb->p9pr.pcc = NULL;
236 pb->p9pr.func = f;
237 pb->p9pr.arg = arg;
238 TAILQ_INSERT_TAIL(&p9p->outbufq, pb, p9pr.entries);
239 }
240
241 struct p9pbuf *
242 req_get(struct puffs9p *p9p, uint16_t tagid)
243 {
244 struct p9pbuf *pb;
245
246 TAILQ_FOREACH(pb, &p9p->req_queue, p9pr.entries)
247 if (pb->p9pr.tagid == tagid)
248 break;
249
250 if (!pb)
251 return NULL;
252
253 TAILQ_REMOVE(&p9p->req_queue, pb, p9pr.entries);
254
255 return pb;
256 }
257
258 static void
259 handlebuf(struct puffs9p *p9p, struct p9pbuf *datapb,
260 struct puffs_putreq *ppr)
261 {
262 struct p9preq p9prtmp;
263 struct p9pbuf *pb;
264
265 /* is this something we are expecting? */
266 pb = req_get(p9p, datapb->tagid);
267
268 if (pb == NULL) {
269 printf("invalid server request response %d\n", datapb->tagid);
270 p9pbuf_destroy(datapb);
271 return;
272 }
273
274 /* keep p9preq clean, xxx uknow */
275 p9prtmp = pb->p9pr;
276 *pb = *datapb;
277 pb->p9pr = p9prtmp;
278 free(datapb);
279
280 /* don't allow both cc and handler func, but allow neither */
281 assert((pb->p9pr.pcc && pb->p9pr.func) == 0);
282 if (pb->p9pr.pcc) {
283 puffs_docc(pb->p9pr.pcc, ppr);
284 } else if (pb->p9pr.func) {
285 pb->p9pr.func(p9p, pb, pb->p9pr.arg);
286 } else {
287 assert(pb->p9pr.arg == NULL);
288 p9pbuf_destroy(pb);
289 }
290 }
291
292 static int
293 psshinput(struct puffs9p *p9p, struct puffs_putreq *ppr)
294 {
295 struct p9pbuf *cb;
296 int rv;
297
298 for (;;) {
299 if ((cb = p9p->curpb) == NULL) {
300 cb = p9pbuf_make(p9p->maxreq, P9PB_IN);
301 if (cb == NULL)
302 return -1;
303 p9p->curpb = cb;
304 }
305
306 rv = p9pbuf_read(p9p, cb);
307 if (rv == -1)
308 err(1, "p9pbuf read");
309 if (rv == 0)
310 break;
311
312 handlebuf(p9p, cb, ppr);
313 p9p->curpb = NULL;
314 }
315
316 return rv;
317 }
318
319 static int
320 psshoutput(struct puffs9p *p9p)
321 {
322 struct p9pbuf *pb;
323 int rv;
324
325 TAILQ_FOREACH(pb, &p9p->outbufq, p9pr.entries) {
326 rv = p9pbuf_write(p9p, pb);
327 if (rv == -1)
328 return -1;
329 if (rv == 0)
330 return 0;
331
332 /* sent everything, move to cookiequeue */
333 TAILQ_REMOVE(&p9p->outbufq, pb, p9pr.entries);
334 free(pb->buf);
335 TAILQ_INSERT_TAIL(&p9p->req_queue, pb, p9pr.entries);
336 }
337
338 return 1;
339 }
340
341 #define PFD_SOCK 0
342 #define PFD_PUFFS 1
343 static void
344 puffs9p_eventloop(struct puffs_usermount *pu, struct puffs9p *p9p)
345 {
346 struct puffs_getreq *pgr;
347 struct puffs_putreq *ppr;
348 struct pollfd pfds[2];
349
350 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
351 if (!pgr)
352 err(1, "makegetreq");
353 ppr = puffs_req_makeput(pu);
354 if (!ppr)
355 err(1, "makeputreq");
356
357 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
358 memset(pfds, 0, sizeof(pfds));
359 pfds[PFD_SOCK].events = POLLIN;
360 if (!TAILQ_EMPTY(&p9p->outbufq))
361 pfds[PFD_SOCK].events |= POLLOUT;
362 pfds[PFD_SOCK].fd = p9p->servsock;
363 pfds[PFD_PUFFS].fd = puffs_getselectable(pu);
364 pfds[PFD_PUFFS].events = POLLIN;
365
366 if (poll(pfds, 2, INFTIM) == -1)
367 err(1, "poll");
368
369 if (pfds[PFD_SOCK].revents & POLLOUT)
370 if (psshoutput(p9p) == -1)
371 err(1, "psshoutput");
372
373 /* get & possibly dispatch events from kernel */
374 if (pfds[PFD_PUFFS].revents & POLLIN)
375 if (puffs_req_handle(pu, pgr, ppr, 0) == -1)
376 err(1, "puffs_handlereqs");
377
378 /* get input from sftpd, possibly build more responses */
379 if (pfds[PFD_SOCK].revents & POLLIN)
380 if (psshinput(p9p, ppr) == -1)
381 errx(1, "psshinput");
382
383 /* it's likely we got outputtables, poke the ice with a stick */
384 if (psshoutput(p9p) == -1)
385 err(1, "psshoutput");
386
387 /* stuff all replies from both of the above into kernel */
388 if (puffs_req_putput(ppr) == -1)
389 err(1, "putputreq");
390 puffs_req_resetput(ppr);
391 }
392
393 puffs_req_destroyget(pgr);
394 }
395