Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.c revision 1.16
      1 /*	$NetBSD: psshfs.c,v 1.16 2007/05/02 18:50:30 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006  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  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * psshfs: puffs sshfs
     33  *
     34  * psshfs implements sshfs functionality on top of puffs making it
     35  * possible to mount a filesystme through the sftp service.
     36  *
     37  * psshfs can execute multiple operations in "parallel" by using the
     38  * puffs_cc framework for continuations.
     39  *
     40  * Concurrency control is handled currently by vnode locking (this
     41  * will change in the future).  Context switch locations are easy to
     42  * find by grepping for puffs_cc_yield().
     43  *
     44  * The operation revolves around an event loop.  Incoming requests from
     45  * the kernel are dispatched off to the libpuffs event handler, which
     46  * eventually calls the callbacks.  The callbacks do whatever they need
     47  * to do, queue output and call puffs_cc_yield() to put them to sleep.
     48  * Meanwhile execution continues.  When an answer to the callback's
     49  * query arrives, the blocker is located by the protocol's request
     50  * id and awakened by calling puffs_docc().  All changes made to the
     51  * buffer (psbuf->buf) will be seen by the callback.  Here the buffer
     52  * contains the response of the sftp server.  The callback will then
     53  * proceed to parse the buffer.
     54  *
     55  * XXX: struct psbuf is a mess.  it will be fixed sooner or later
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 #ifndef lint
     60 __RCSID("$NetBSD: psshfs.c,v 1.16 2007/05/02 18:50:30 pooka Exp $");
     61 #endif /* !lint */
     62 
     63 #include <sys/types.h>
     64 
     65 #include <assert.h>
     66 #include <err.h>
     67 #include <errno.h>
     68 #include <mntopts.h>
     69 #include <poll.h>
     70 #include <puffs.h>
     71 #include <signal.h>
     72 #include <stdlib.h>
     73 #include <util.h>
     74 #include <unistd.h>
     75 
     76 #include "psshfs.h"
     77 
     78 static void	psshfs_eventloop(struct puffs_usermount *, struct psshfs_ctx *);
     79 static void	pssh_connect(struct psshfs_ctx *, char **);
     80 static void	usage(void);
     81 
     82 #define SSH_PATH "/usr/bin/ssh"
     83 
     84 static void
     85 usage()
     86 {
     87 
     88 	errx(1, "usage: %s [-es][-p port] [-o opts]user@host:path mountpath",
     89 	    getprogname());
     90 }
     91 
     92 int
     93 main(int argc, char *argv[])
     94 {
     95 	struct psshfs_ctx pctx;
     96 	struct puffs_usermount *pu;
     97 	struct puffs_ops *pops;
     98 	mntoptparse_t mp;
     99 	char *sshargs[16];
    100 	char *userhost;
    101 	char *hostpath;
    102 	char *sshport = NULL;
    103 	int mntflags, pflags, ch;
    104 	int detach, exportfs;
    105 	int argi;
    106 
    107 	setprogname(argv[0]);
    108 
    109 	if (argc < 3)
    110 		usage();
    111 
    112 	mntflags = pflags = exportfs = 0;
    113 	detach = 1;
    114 	while ((ch = getopt(argc, argv, "eo:p:s")) != -1) {
    115 		switch (ch) {
    116 		case 'e':
    117 			exportfs = 1;
    118 			break;
    119 		case 'o':
    120 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    121 			if (mp == NULL)
    122 				err(1, "getmntopts");
    123 			freemntopts(mp);
    124 			break;
    125 		case 'p':
    126 			sshport = optarg;
    127 			break;
    128 		case 's':
    129 			detach = 0;
    130 			break;
    131 		default:
    132 			usage();
    133 			/*NOTREACHED*/
    134 		}
    135 	}
    136 	argc -= optind;
    137 	argv += optind;
    138 
    139 #if 0
    140 	/* XXX: noatime is mandatory for now */
    141 	mntflags |= MNT_NOATIME;
    142 #endif
    143 
    144 	if (pflags & PUFFS_FLAG_OPDUMP)
    145 		detach = 0;
    146 	pflags |= PUFFS_FLAG_BUILDPATH | PUFFS_KFLAG_WTCACHE;
    147 
    148 	if (argc != 2)
    149 		usage();
    150 
    151 	PUFFSOP_INIT(pops);
    152 
    153 	PUFFSOP_SET(pops, psshfs, fs, unmount);
    154 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
    155 	PUFFSOP_SETFSNOP(pops, statvfs);
    156 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
    157 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
    158 
    159 	PUFFSOP_SET(pops, psshfs, node, lookup);
    160 	PUFFSOP_SET(pops, psshfs, node, create);
    161 	PUFFSOP_SET(pops, psshfs, node, readdir);
    162 	PUFFSOP_SET(pops, psshfs, node, getattr);
    163 	PUFFSOP_SET(pops, psshfs, node, setattr);
    164 	PUFFSOP_SET(pops, psshfs, node, mkdir);
    165 	PUFFSOP_SET(pops, psshfs, node, remove);
    166 	PUFFSOP_SET(pops, psshfs, node, readlink);
    167 	PUFFSOP_SET(pops, psshfs, node, rmdir);
    168 	PUFFSOP_SET(pops, psshfs, node, symlink);
    169 	PUFFSOP_SET(pops, psshfs, node, rename);
    170 	PUFFSOP_SET(pops, psshfs, node, read);
    171 	PUFFSOP_SET(pops, psshfs, node, write);
    172 	PUFFSOP_SET(pops, psshfs, node, reclaim);
    173 
    174 	memset(&pctx, 0, sizeof(pctx));
    175 	TAILQ_INIT(&pctx.outbufq);
    176 	TAILQ_INIT(&pctx.req_queue);
    177 	pctx.mounttime = time(NULL);
    178 
    179 	userhost = argv[0];
    180 	hostpath = strchr(userhost, ':');
    181 	if (hostpath) {
    182 		*hostpath++ = '\0';
    183 		pctx.mountpath = hostpath;
    184 	} else
    185 		pctx.mountpath = ".";
    186 
    187 	/* xblah */
    188 	argi = 0;
    189 	sshargs[argi++] = SSH_PATH;
    190 	sshargs[argi++] = argv[0];
    191 	sshargs[argi++] = "-oClearAllForwardings=yes";
    192 	sshargs[argi++] = "-a";
    193 	sshargs[argi++] = "-x";
    194 	sshargs[argi++] = "-s";
    195 	if (sshport) {
    196 		sshargs[argi++] = "-p";
    197 		sshargs[argi++] = sshport;
    198 	}
    199 	sshargs[argi++] = "sftp";
    200 	sshargs[argi] = 0;
    201 
    202 	pu = puffs_init(pops, "ppshfs", &pctx, pflags);
    203 	if (pu == NULL)
    204 		err(1, "puffs_init");
    205 
    206 	pssh_connect(&pctx, sshargs);
    207 
    208 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    209 		err(1, "setblockingmode");
    210 	if (exportfs)
    211 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
    212 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
    213 
    214 	if (puffs_domount(pu, argv[1], mntflags) == -1)
    215 		err(1, "puffs_domount");
    216 
    217 	if (psshfs_domount(pu) != 0)
    218 		errx(1, "psshfs_domount");
    219 
    220 	if (detach)
    221 		daemon(1, 0);
    222 
    223 	psshfs_eventloop(pu, &pctx);
    224 	return 0;
    225 }
    226 
    227 /*
    228  * enqueue buffer to be handled with cc
    229  */
    230 void
    231 pssh_outbuf_enqueue(struct psshfs_ctx *pctx, struct psbuf *pb,
    232 	struct puffs_cc *pcc, uint32_t reqid)
    233 {
    234 
    235 	pb->psr.reqid = reqid;
    236 	pb->psr.pcc = pcc;
    237 	pb->psr.func = NULL;
    238 	pb->psr.arg = NULL;
    239 	TAILQ_INSERT_TAIL(&pctx->outbufq, pb, psr.entries);
    240 }
    241 
    242 /*
    243  * enqueue buffer to be handled with "f".  "f" must not block.
    244  * gives up struct psbuf ownership.
    245  */
    246 void
    247 pssh_outbuf_enqueue_nocc(struct psshfs_ctx *pctx, struct psbuf *pb,
    248 	void (*f)(struct psshfs_ctx *, struct psbuf *, void *), void *arg,
    249 	uint32_t reqid)
    250 {
    251 
    252 	pb->psr.reqid = reqid;
    253 	pb->psr.pcc = NULL;
    254 	pb->psr.func = f;
    255 	pb->psr.arg = arg;
    256 	TAILQ_INSERT_TAIL(&pctx->outbufq, pb, psr.entries);
    257 }
    258 
    259 struct psbuf *
    260 psshreq_get(struct psshfs_ctx *pctx, uint32_t reqid)
    261 {
    262 	struct psbuf *pb;
    263 
    264 	TAILQ_FOREACH(pb, &pctx->req_queue, psr.entries)
    265 		if (pb->psr.reqid == reqid)
    266 			break;
    267 
    268 	if (!pb)
    269 		return NULL;
    270 
    271 	TAILQ_REMOVE(&pctx->req_queue, pb, psr.entries);
    272 
    273 	return pb;
    274 }
    275 
    276 static void
    277 handlebuf(struct psshfs_ctx *pctx, struct psbuf *datapb,
    278 	struct puffs_putreq *ppr)
    279 {
    280 	struct psreq psrtmp;
    281 	struct psbuf *pb;
    282 
    283 	/* is this something we are expecting? */
    284 	pb = psshreq_get(pctx, datapb->reqid);
    285 
    286 	if (pb == NULL) {
    287 		printf("invalid server request response %d\n", datapb->reqid);
    288 		psbuf_destroy(datapb);
    289 		return;
    290 	}
    291 
    292 	/* keep psreq clean, xxx uknow */
    293 	psrtmp = pb->psr;
    294 	*pb = *datapb;
    295 	pb->psr = psrtmp;
    296 	free(datapb);
    297 
    298 	/* don't allow both cc and handler func, but allow neither */
    299 	assert((pb->psr.pcc && pb->psr.func) == 0);
    300 	if (pb->psr.pcc) {
    301 		puffs_docc(pb->psr.pcc, ppr);
    302 	} else if (pb->psr.func) {
    303 		pb->psr.func(pctx, pb, pb->psr.arg);
    304 	} else {
    305 		assert(pb->psr.arg == NULL);
    306 		psbuf_destroy(pb);
    307 	}
    308 }
    309 
    310 static int
    311 psshinput(struct psshfs_ctx *pctx, struct puffs_putreq *ppr)
    312 {
    313 	struct psbuf *cb;
    314 	int rv;
    315 
    316 	for (;;) {
    317 		if ((cb = pctx->curpb) == NULL) {
    318 			cb = psbuf_make(PSB_IN);
    319 			if (cb == NULL)
    320 				return -1;
    321 			pctx->curpb = cb;
    322 		}
    323 
    324 		rv = psbuf_read(pctx, cb);
    325 		if (rv == -1)
    326 			err(1, "psbuf read");
    327 		if (rv == 0)
    328 			break;
    329 
    330 		handlebuf(pctx, cb, ppr);
    331 		pctx->curpb = NULL;
    332 	}
    333 
    334 	return rv;
    335 }
    336 
    337 static int
    338 psshoutput(struct psshfs_ctx *pctx)
    339 {
    340 	struct psbuf *pb;
    341 	int rv;
    342 
    343 	TAILQ_FOREACH(pb, &pctx->outbufq, psr.entries) {
    344 		rv = psbuf_write(pctx, pb);
    345 		if (rv == -1)
    346 			return -1;
    347 		if (rv == 0)
    348 			return 0;
    349 
    350 		/* sent everything, move to cookiequeue */
    351 		TAILQ_REMOVE(&pctx->outbufq, pb, psr.entries);
    352 		free(pb->buf);
    353 		TAILQ_INSERT_TAIL(&pctx->req_queue, pb, psr.entries);
    354 	}
    355 
    356 	return 1;
    357 }
    358 
    359 volatile int timetodie;
    360 
    361 #define PFD_SSH 0
    362 #define PFD_PUFFS 1
    363 static void
    364 psshfs_eventloop(struct puffs_usermount *pu, struct psshfs_ctx *pctx)
    365 {
    366 	struct puffs_getreq *pgr;
    367 	struct puffs_putreq *ppr;
    368 	struct pollfd pfds[2];
    369 	int x;
    370 
    371 	pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
    372 	if (!pgr)
    373 		err(1, "makegetreq");
    374 	ppr = puffs_req_makeput(pu);
    375 	if (!ppr)
    376 		err(1, "makeputreq");
    377 
    378 	x = 1;
    379 	if (ioctl(pctx->sshfd, FIONBIO, &x) == -1)
    380 		err(1, "nonblocking descriptor");
    381 
    382 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    383 		if (timetodie) {
    384 			kill(pctx->sshpid, SIGTERM);
    385 			break;
    386 		}
    387 
    388 		memset(pfds, 0, sizeof(pfds));
    389 		pfds[PFD_SSH].events = POLLIN;
    390 		if (!TAILQ_EMPTY(&pctx->outbufq))
    391 			pfds[PFD_SSH].events |= POLLOUT;
    392 		pfds[PFD_SSH].fd = pctx->sshfd;
    393 		pfds[PFD_PUFFS].fd = puffs_getselectable(pu);
    394 		pfds[PFD_PUFFS].events = POLLIN;
    395 
    396 		if (poll(pfds, 2, INFTIM) == -1)
    397 			err(1, "poll");
    398 
    399 		if (pfds[PFD_SSH].revents & POLLOUT)
    400 			if (psshoutput(pctx) == -1)
    401 				err(1, "psshoutput");
    402 
    403 		/* get & possibly dispatch events from kernel */
    404 		if (pfds[PFD_PUFFS].revents & POLLIN)
    405 			if (puffs_req_handle(pu, pgr, ppr, 0) == -1)
    406 				err(1, "puffs_handlereqs");
    407 
    408 		/* get input from sftpd, possibly build more responses */
    409 		if (pfds[PFD_SSH].revents & POLLIN)
    410 			if (psshinput(pctx, ppr) == -1)
    411 				errx(1, "psshinput");
    412 
    413 		/* it's likely we got outputtables, poke the ice with a stick */
    414 		if (psshoutput(pctx) == -1)
    415 			err(1, "psshoutput");
    416 
    417 		/* stuff all replies from both of the above into kernel */
    418 		if (puffs_req_putput(ppr) == -1)
    419 			err(1, "putputreq");
    420 		puffs_req_resetput(ppr);
    421 	}
    422 
    423 	puffs_req_destroyget(pgr);
    424 	puffs_req_destroyput(ppr);
    425 }
    426 
    427 static void
    428 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
    429 {
    430 	int fds[2];
    431 	pid_t pid;
    432 
    433 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
    434 		err(1, "socketpair");
    435 
    436 	pid = fork();
    437 	switch (pid) {
    438 	case -1:
    439 		err(1, "fork");
    440 		/*NOTREACHED*/
    441 	case 0: /* child */
    442 		if (dup2(fds[0], STDIN_FILENO) == -1)
    443 			err(1, "child dup2");
    444 		if (dup2(fds[0], STDOUT_FILENO) == -1)
    445 			err(1, "child dup2");
    446 		close(fds[0]);
    447 		close(fds[1]);
    448 		execvp(sshargs[0], sshargs);
    449 		break;
    450 	default:
    451 		pctx->sshpid = pid;
    452 		pctx->sshfd = fds[1];
    453 		close(fds[0]);
    454 		break;
    455 	}
    456 }
    457