Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.8
      1 /*	$NetBSD: fs.c,v 1.8 2007/05/05 15:49:51 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 #include <sys/cdefs.h>
     32 #ifndef lint
     33 __RCSID("$NetBSD: fs.c,v 1.8 2007/05/05 15:49:51 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <puffs.h>
     39 #include <signal.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <unistd.h>
     43 
     44 #include "psshfs.h"
     45 #include "sftp_proto.h"
     46 
     47 #define DO_IO(fname, a1, a2, a3, a4, rv)				\
     48 	puffs_framebuf_seekset(a2, 0);					\
     49 	*(a4) = 0;							\
     50 	rv = fname(a1, a2, a3, a4);					\
     51 	if (rv || a4 == 0) errx(1, "psshfs_domount io failed %d, %d", rv, *a4)
     52 
     53 int
     54 psshfs_domount(struct puffs_usermount *pu)
     55 {
     56 	struct statvfs sb;
     57 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     58 	struct psshfs_node *root = &pctx->psn_root;
     59 	struct puffs_pathobj *po_root;
     60 	struct puffs_node *pn_root;
     61 	struct vattr va;
     62 	struct vattr *rva;
     63 	struct puffs_framebuf *pb;
     64 	char *rootpath;
     65 	uint32_t count;
     66 	int rv, done;
     67 
     68 	pb = psbuf_makeout();
     69 	psbuf_put_1(pb, SSH_FXP_INIT);
     70 	psbuf_put_4(pb, SFTP_PROTOVERSION);
     71 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     72 
     73 	puffs_framebuf_recycle(pb);
     74 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     75 	if (psbuf_get_type(pb) != SSH_FXP_VERSION)
     76 		errx(1, "invalid server response: %d", psbuf_get_type(pb));
     77 	pctx->protover = psbuf_get_reqid(pb);
     78 	/* might contain some other stuff, but we're not interested */
     79 
     80 	/* scope out our rootpath */
     81 	psbuf_recycleout(pb);
     82 	psbuf_put_1(pb, SSH_FXP_REALPATH);
     83 	psbuf_put_4(pb, NEXTREQ(pctx));
     84 	psbuf_put_str(pb, pctx->mountpath);
     85 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     86 
     87 	puffs_framebuf_recycle(pb);
     88 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     89 	if (psbuf_get_type(pb) != SSH_FXP_NAME)
     90 		errx(1, "invalid server realpath response for \"%s\"",
     91 		    pctx->mountpath);
     92 	if (psbuf_get_4(pb, &count) == -1)
     93 		errx(1, "invalid realpath response: count");
     94 	if (psbuf_get_str(pb, &rootpath, NULL) == -1)
     95 		errx(1, "invalid realpath response: rootpath");
     96 
     97 	/* stat the rootdir so that we know it's a dir */
     98 	psbuf_recycleout(pb);
     99 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
    100 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
    101 
    102 	puffs_framebuf_recycle(pb);
    103 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    104 
    105 	rv = psbuf_expect_attrs(pb, &va);
    106 	if (rv)
    107 		errx(1, "couldn't stat rootpath");
    108 	puffs_framebuf_destroy(pb);
    109 
    110 	if (puffs_mode2vt(va.va_mode) != VDIR)
    111 		errx(1, "remote path (%s) not a directory", rootpath);
    112 
    113 	pctx->nextino = 2;
    114 
    115 	memset(root, 0, sizeof(struct psshfs_node));
    116 	pn_root = puffs_pn_new(pu, root);
    117 	if (pn_root == NULL)
    118 		return errno;
    119 	puffs_setroot(pu, pn_root);
    120 
    121 	po_root = puffs_getrootpathobj(pu);
    122 	if (po_root == NULL)
    123 		err(1, "getrootpathobj");
    124 	po_root->po_path = rootpath;
    125 	po_root->po_len = strlen(rootpath);
    126 
    127 	rva = &pn_root->pn_va;
    128 	puffs_setvattr(rva, &va);
    129 	rva->va_fileid = pctx->nextino++;
    130 	rva->va_nlink = 0156; /* XXX */
    131 
    132 	puffs_zerostatvfs(&sb);
    133 	if (puffs_start(pu, pn_root, &sb) != 0)
    134 		return errno;
    135 
    136 	return 0;
    137 }
    138 
    139 int
    140 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    141 {
    142 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    143 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    144 
    145 	kill(pctx->sshpid, SIGTERM);
    146 	close(pctx->sshfd);
    147 	return 0;
    148 }
    149 
    150 int
    151 psshfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
    152 	void *fid, size_t *fidsize)
    153 {
    154 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    155 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    156 	struct puffs_node *pn = cookie;
    157 	struct psshfs_node *psn = pn->pn_data;
    158 	struct psshfs_fid *pf = fid;
    159 
    160 	pf->mounttime = pctx->mounttime;
    161 	pf->node = pn;
    162 
    163 	psn->hasfh = 1;
    164 
    165 	return 0;
    166 }
    167 
    168 int
    169 psshfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
    170 	void **fcookie, enum vtype *ftype, voff_t *fsize, dev_t *fdev)
    171 {
    172 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    173 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    174 	struct psshfs_fid *pf = fid;
    175 	struct puffs_node *pn = pf->node;
    176 	struct psshfs_node *psn;
    177 	int rv;
    178 
    179 	if (pf->mounttime != pctx->mounttime)
    180 		return EINVAL;
    181 	if (pn == 0)
    182 		return EINVAL;
    183 	psn = pn->pn_data;
    184 	if (psn->hasfh == 0)
    185 		return EINVAL;
    186 
    187 	/* update node attributes */
    188 	rv = getnodeattr(pcc, pn);
    189 	if (rv)
    190 		return EINVAL;
    191 
    192 	*fcookie = pn;
    193 	*ftype = pn->pn_va.va_type;
    194 	*fsize = pn->pn_va.va_size;
    195 
    196 	return 0;
    197 }
    198