Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.c revision 1.26
      1  1.26  pooka /*	$NetBSD: psshfs.c,v 1.26 2007/05/15 13:46:48 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  * 3. The name of the company nor the name of the author may be used to
     15   1.1  pooka  *    endorse or promote products derived from this software without specific
     16   1.1  pooka  *    prior written permission.
     17   1.1  pooka  *
     18   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1  pooka  * SUCH DAMAGE.
     29   1.1  pooka  */
     30   1.1  pooka 
     31   1.1  pooka /*
     32   1.1  pooka  * psshfs: puffs sshfs
     33   1.1  pooka  *
     34   1.1  pooka  * psshfs implements sshfs functionality on top of puffs making it
     35   1.1  pooka  * possible to mount a filesystme through the sftp service.
     36   1.1  pooka  *
     37   1.1  pooka  * psshfs can execute multiple operations in "parallel" by using the
     38   1.1  pooka  * puffs_cc framework for continuations.
     39   1.1  pooka  *
     40   1.1  pooka  * Concurrency control is handled currently by vnode locking (this
     41   1.1  pooka  * will change in the future).  Context switch locations are easy to
     42  1.17  pooka  * find by grepping for puffs_framebuf_enqueue_cc().
     43   1.1  pooka  */
     44   1.1  pooka 
     45   1.1  pooka #include <sys/cdefs.h>
     46   1.1  pooka #ifndef lint
     47  1.26  pooka __RCSID("$NetBSD: psshfs.c,v 1.26 2007/05/15 13:46:48 pooka Exp $");
     48   1.1  pooka #endif /* !lint */
     49   1.1  pooka 
     50   1.1  pooka #include <sys/types.h>
     51   1.1  pooka 
     52   1.1  pooka #include <assert.h>
     53   1.1  pooka #include <err.h>
     54   1.1  pooka #include <errno.h>
     55   1.2  pooka #include <mntopts.h>
     56   1.1  pooka #include <poll.h>
     57   1.1  pooka #include <puffs.h>
     58   1.1  pooka #include <signal.h>
     59   1.1  pooka #include <stdlib.h>
     60   1.1  pooka #include <util.h>
     61   1.1  pooka #include <unistd.h>
     62   1.1  pooka 
     63   1.1  pooka #include "psshfs.h"
     64   1.1  pooka 
     65   1.1  pooka static void	pssh_connect(struct psshfs_ctx *, char **);
     66   1.2  pooka static void	usage(void);
     67  1.21    tnn static void	add_ssharg(char ***, int *, char *);
     68   1.1  pooka 
     69   1.1  pooka #define SSH_PATH "/usr/bin/ssh"
     70   1.1  pooka 
     71   1.2  pooka static void
     72  1.21    tnn add_ssharg(char ***sshargs, int *nargs, char *arg)
     73  1.21    tnn {
     74  1.21    tnn 
     75  1.21    tnn 	*sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
     76  1.21    tnn 	if (!*sshargs)
     77  1.21    tnn 		err(1, "realloc");
     78  1.21    tnn 	(*sshargs)[(*nargs)++] = arg;
     79  1.21    tnn 	(*sshargs)[*nargs] = NULL;
     80  1.21    tnn }
     81  1.21    tnn 
     82  1.21    tnn static void
     83   1.2  pooka usage()
     84   1.2  pooka {
     85   1.2  pooka 
     86  1.23  pooka 	fprintf(stderr, "usage: %s "
     87  1.23  pooka 	    "[-es] [-O sshopt=value] [-o opts] user@host:path mountpath\n",
     88   1.4  pooka 	    getprogname());
     89  1.23  pooka 	exit(1);
     90   1.2  pooka }
     91   1.2  pooka 
     92   1.1  pooka int
     93   1.1  pooka main(int argc, char *argv[])
     94   1.1  pooka {
     95   1.1  pooka 	struct psshfs_ctx pctx;
     96   1.1  pooka 	struct puffs_usermount *pu;
     97   1.1  pooka 	struct puffs_ops *pops;
     98   1.2  pooka 	mntoptparse_t mp;
     99  1.21    tnn 	char **sshargs;
    100   1.1  pooka 	char *userhost;
    101   1.1  pooka 	char *hostpath;
    102  1.25  pooka 	int mntflags, pflags, lflags, ch;
    103  1.25  pooka 	int exportfs;
    104  1.21    tnn 	int nargs, x;
    105   1.1  pooka 
    106   1.1  pooka 	setprogname(argv[0]);
    107   1.1  pooka 
    108   1.2  pooka 	if (argc < 3)
    109   1.2  pooka 		usage();
    110   1.2  pooka 
    111  1.25  pooka 	mntflags = pflags = lflags = exportfs = nargs = 0;
    112  1.21    tnn 	sshargs = NULL;
    113  1.21    tnn 	add_ssharg(&sshargs, &nargs, SSH_PATH);
    114  1.21    tnn 	add_ssharg(&sshargs, &nargs, "-axs");
    115  1.21    tnn 	add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
    116  1.21    tnn 
    117  1.21    tnn 	while ((ch = getopt(argc, argv, "eo:O:s")) != -1) {
    118   1.2  pooka 		switch (ch) {
    119  1.11  pooka 		case 'e':
    120  1.13  pooka 			exportfs = 1;
    121  1.11  pooka 			break;
    122  1.21    tnn 		case 'O':
    123  1.22    tnn 			add_ssharg(&sshargs, &nargs, "-o");
    124  1.22    tnn 			add_ssharg(&sshargs, &nargs, optarg);
    125  1.21    tnn 			break;
    126   1.2  pooka 		case 'o':
    127   1.2  pooka 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    128   1.2  pooka 			if (mp == NULL)
    129   1.2  pooka 				err(1, "getmntopts");
    130   1.2  pooka 			freemntopts(mp);
    131   1.2  pooka 			break;
    132   1.4  pooka 		case 's':
    133  1.25  pooka 			lflags |= PUFFSLOOP_NODAEMON;
    134   1.4  pooka 			break;
    135   1.2  pooka 		default:
    136   1.2  pooka 			usage();
    137   1.2  pooka 			/*NOTREACHED*/
    138   1.2  pooka 		}
    139   1.2  pooka 	}
    140   1.2  pooka 	argc -= optind;
    141   1.2  pooka 	argv += optind;
    142   1.2  pooka 
    143   1.7  pooka #if 0
    144   1.7  pooka 	/* XXX: noatime is mandatory for now */
    145   1.7  pooka 	mntflags |= MNT_NOATIME;
    146   1.7  pooka #endif
    147   1.7  pooka 
    148   1.4  pooka 	if (pflags & PUFFS_FLAG_OPDUMP)
    149  1.25  pooka 		lflags |= PUFFSLOOP_NODAEMON;
    150  1.20  pooka 	pflags |= PUFFS_FLAG_BUILDPATH;
    151  1.20  pooka 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    152   1.4  pooka 
    153   1.2  pooka 	if (argc != 2)
    154   1.2  pooka 		usage();
    155   1.1  pooka 
    156   1.1  pooka 	PUFFSOP_INIT(pops);
    157   1.1  pooka 
    158   1.1  pooka 	PUFFSOP_SET(pops, psshfs, fs, unmount);
    159   1.1  pooka 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
    160   1.1  pooka 	PUFFSOP_SETFSNOP(pops, statvfs);
    161  1.11  pooka 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
    162  1.11  pooka 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
    163   1.1  pooka 
    164   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, lookup);
    165   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, create);
    166  1.19  pooka 	PUFFSOP_SET(pops, psshfs, node, open);
    167  1.19  pooka 	PUFFSOP_SET(pops, psshfs, node, inactive);
    168   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, readdir);
    169   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, getattr);
    170   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, setattr);
    171   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, mkdir);
    172   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, remove);
    173   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, readlink);
    174   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, rmdir);
    175   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, symlink);
    176   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, rename);
    177   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, read);
    178   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, write);
    179   1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, reclaim);
    180   1.1  pooka 
    181   1.1  pooka 	memset(&pctx, 0, sizeof(pctx));
    182  1.11  pooka 	pctx.mounttime = time(NULL);
    183   1.1  pooka 
    184   1.2  pooka 	userhost = argv[0];
    185   1.1  pooka 	hostpath = strchr(userhost, ':');
    186   1.1  pooka 	if (hostpath) {
    187   1.1  pooka 		*hostpath++ = '\0';
    188   1.1  pooka 		pctx.mountpath = hostpath;
    189   1.1  pooka 	} else
    190   1.1  pooka 		pctx.mountpath = ".";
    191   1.1  pooka 
    192  1.21    tnn 	add_ssharg(&sshargs, &nargs, argv[0]);
    193  1.21    tnn 	add_ssharg(&sshargs, &nargs, "sftp");
    194   1.1  pooka 
    195  1.14  pooka 	pu = puffs_init(pops, "ppshfs", &pctx, pflags);
    196  1.13  pooka 	if (pu == NULL)
    197  1.13  pooka 		err(1, "puffs_init");
    198   1.1  pooka 
    199   1.4  pooka 	pssh_connect(&pctx, sshargs);
    200   1.4  pooka 
    201   1.1  pooka 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    202   1.1  pooka 		err(1, "setblockingmode");
    203  1.13  pooka 	if (exportfs)
    204  1.13  pooka 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
    205  1.13  pooka 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
    206  1.13  pooka 
    207  1.13  pooka 	if (puffs_domount(pu, argv[1], mntflags) == -1)
    208  1.13  pooka 		err(1, "puffs_domount");
    209  1.13  pooka 
    210   1.2  pooka 	if (psshfs_domount(pu) != 0)
    211  1.13  pooka 		errx(1, "psshfs_domount");
    212   1.1  pooka 
    213   1.1  pooka 	x = 1;
    214  1.17  pooka 	if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
    215   1.1  pooka 		err(1, "nonblocking descriptor");
    216   1.1  pooka 
    217  1.26  pooka 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp,
    218  1.26  pooka 	    puffs_framev_unmountonclose);
    219  1.26  pooka 	if (puffs_framev_addfd(pu, pctx.sshfd) == -1)
    220  1.25  pooka 		err(1, "framebuf addfd");
    221   1.1  pooka 
    222  1.25  pooka 	return puffs_mainloop(pu, lflags);
    223   1.1  pooka }
    224   1.1  pooka 
    225   1.1  pooka static void
    226   1.1  pooka pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
    227   1.1  pooka {
    228   1.1  pooka 	int fds[2];
    229   1.1  pooka 	pid_t pid;
    230   1.1  pooka 
    231   1.1  pooka 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
    232   1.1  pooka 		err(1, "socketpair");
    233   1.1  pooka 
    234   1.1  pooka 	pid = fork();
    235   1.1  pooka 	switch (pid) {
    236   1.1  pooka 	case -1:
    237   1.1  pooka 		err(1, "fork");
    238   1.1  pooka 		/*NOTREACHED*/
    239   1.1  pooka 	case 0: /* child */
    240   1.1  pooka 		if (dup2(fds[0], STDIN_FILENO) == -1)
    241   1.1  pooka 			err(1, "child dup2");
    242   1.1  pooka 		if (dup2(fds[0], STDOUT_FILENO) == -1)
    243   1.1  pooka 			err(1, "child dup2");
    244   1.1  pooka 		close(fds[0]);
    245   1.1  pooka 		close(fds[1]);
    246   1.1  pooka 		execvp(sshargs[0], sshargs);
    247   1.1  pooka 		break;
    248   1.1  pooka 	default:
    249   1.1  pooka 		pctx->sshpid = pid;
    250   1.1  pooka 		pctx->sshfd = fds[1];
    251   1.1  pooka 		close(fds[0]);
    252   1.1  pooka 		break;
    253   1.1  pooka 	}
    254   1.1  pooka }
    255