Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.h revision 1.18
      1 /*	$NetBSD: psshfs.h,v 1.18 2007/05/20 17:47:12 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006, 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  * 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 #ifndef PSSHFS_H_
     32 #define PSSHFS_H_
     33 
     34 #include <sys/queue.h>
     35 
     36 #include <puffs.h>
     37 
     38 /*
     39  * Later protocol versions would have some advantages (such as link count
     40  * supported directly as a part of stat), but since proto version 3 seems
     41  * to be the only widely available version, let's not try to jump through
     42  * too many hoops to be compatible with all versions.
     43  */
     44 #define SFTP_PROTOVERSION 3
     45 
     46 /*
     47  * Refresh directories every n seconds as indicated by the following macro.
     48  * Note that local changes will still be visible immediately.
     49  */
     50 #define PSSHFS_REFRESHIVAL 30
     51 
     52 /* warm getattr cache in readdir */
     53 #define SUPERREADDIR
     54 
     55 PUFFSOP_PROTOS(psshfs);
     56 
     57 #define NEXTREQ(pctx) ((pctx->nextreq)++)
     58 #define PSSHFSAUTOVAR(pcc)						\
     59 	struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);		\
     60 	uint32_t reqid = NEXTREQ(pctx);					\
     61 	struct puffs_framebuf *pb = psbuf_makeout();			\
     62 	int rv = 0
     63 
     64 #define PSSHFSRETURN(rv)						\
     65 	puffs_framebuf_destroy(pb);					\
     66 	return (rv)
     67 
     68 #define GETRESPONSE(pb)							\
     69 do {									\
     70 	if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb) == -1) {	\
     71 		rv = errno;						\
     72 		goto out;						\
     73 	}								\
     74 } while (/*CONSTCOND*/0)
     75 
     76 #define JUSTSEND(pb)							\
     77 do {									\
     78 	if (puffs_framev_enqueue_justsend(pu,pctx->sshfd,pb,1) == -1) {	\
     79 		rv = errno;						\
     80 		goto out;						\
     81 	}								\
     82 } while (/*CONSTCOND*/0)
     83 
     84 #define SENDCB(pb, f, a)						\
     85 do {									\
     86 	if (puffs_framev_enqueue_cb(pu, pctx->sshfd, pb, f,a) == -1) {	\
     87 		rv = errno;						\
     88 		goto out;						\
     89 	}								\
     90 } while (/*CONSTCOND*/0)
     91 
     92 struct psshfs_dir {
     93 	int valid;
     94 	struct puffs_node *entry;
     95 
     96 	char *entryname;
     97 	struct vattr va;
     98 	time_t attrread;
     99 };
    100 
    101 struct psshfs_fid {
    102 	time_t mounttime;
    103 	struct puffs_node *node;
    104 };
    105 
    106 struct psshfs_node {
    107 	struct puffs_node *parent;
    108 
    109 	struct psshfs_dir *dir;	/* only valid if we're of type VDIR */
    110 #ifdef SUPERREADDIR
    111 	struct delayattr {
    112 		struct puffs_framebuf *pufbuf;
    113 		struct readdirattr *rda;
    114 	} *da;
    115 	size_t nextda;
    116 #endif /* SUPERREADDIR */
    117 
    118 	size_t denttot;
    119 	size_t dentnext;
    120 	time_t dentread;
    121 	int childcount;
    122 
    123 	int stat;
    124 
    125 	time_t attrread;
    126 
    127 	char *fhand_r;
    128 	char *fhand_w;
    129 	uint32_t fhand_r_len;
    130 	uint32_t fhand_w_len;
    131 };
    132 #define PSN_RECLAIMED	0x01
    133 #define PSN_HASFH	0x02
    134 
    135 struct psshfs_ctx {
    136 	int sshfd;
    137 	pid_t sshpid;
    138 	const char *mountpath;
    139 
    140 	int protover;
    141 	uint32_t nextreq;
    142 
    143 	struct puffs_framebuf *curpb;
    144 
    145 	struct psshfs_node psn_root;
    146 	ino_t nextino;
    147 
    148 	int canexport;
    149 	time_t mounttime;
    150 };
    151 
    152 int	psshfs_domount(struct puffs_usermount *);
    153 
    154 int	psbuf_read(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
    155 int	psbuf_write(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
    156 int	psbuf_cmp(struct puffs_usermount *,
    157 		  struct puffs_framebuf *, struct puffs_framebuf *);
    158 
    159 struct puffs_framebuf	*psbuf_makeout(void);
    160 void			psbuf_recycleout(struct puffs_framebuf *);
    161 
    162 void	psbuf_put_1(struct puffs_framebuf *, uint8_t);
    163 void	psbuf_put_2(struct puffs_framebuf *, uint16_t);
    164 void	psbuf_put_4(struct puffs_framebuf *, uint32_t);
    165 void	psbuf_put_8(struct puffs_framebuf *, uint64_t);
    166 void	psbuf_put_str(struct puffs_framebuf *, const char *);
    167 void	psbuf_put_data(struct puffs_framebuf *, const void *, uint32_t);
    168 void	psbuf_put_vattr(struct puffs_framebuf *, const struct vattr *);
    169 
    170 uint8_t		psbuf_get_type(struct puffs_framebuf *);
    171 uint32_t	psbuf_get_len(struct puffs_framebuf *);
    172 uint32_t	psbuf_get_reqid(struct puffs_framebuf *);
    173 
    174 int	psbuf_get_1(struct puffs_framebuf *, uint8_t *);
    175 int	psbuf_get_2(struct puffs_framebuf *, uint16_t *);
    176 int	psbuf_get_4(struct puffs_framebuf *, uint32_t *);
    177 int	psbuf_get_8(struct puffs_framebuf *, uint64_t *);
    178 int	psbuf_get_str(struct puffs_framebuf *, char **, uint32_t *);
    179 int	psbuf_get_vattr(struct puffs_framebuf *, struct vattr *);
    180 
    181 int	psbuf_expect_status(struct puffs_framebuf *);
    182 int	psbuf_expect_handle(struct puffs_framebuf *, char **, uint32_t *);
    183 int	psbuf_expect_name(struct puffs_framebuf *, uint32_t *);
    184 int	psbuf_expect_attrs(struct puffs_framebuf *, struct vattr *);
    185 
    186 int	psbuf_do_data(struct puffs_framebuf *, uint8_t *, uint32_t *);
    187 
    188 void	psbuf_req_data(struct puffs_framebuf *, int, uint32_t,
    189 		       const void *, uint32_t);
    190 void	psbuf_req_str(struct puffs_framebuf *, int, uint32_t, const char *);
    191 
    192 int	sftp_readdir(struct puffs_cc *, struct psshfs_ctx *,
    193 		     struct puffs_node *);
    194 
    195 struct psshfs_dir *lookup(struct psshfs_dir *, size_t, const char *);
    196 struct puffs_node *makenode(struct puffs_usermount *, struct puffs_node *,
    197 			    struct psshfs_dir *, const struct vattr *);
    198 struct puffs_node *allocnode(struct puffs_usermount *, struct puffs_node *,
    199 			    const char *, const struct vattr *);
    200 struct psshfs_dir *direnter(struct puffs_node *, const char *);
    201 void nukenode(struct puffs_node *, const char *, int);
    202 void doreclaim(struct puffs_node *);
    203 int getpathattr(struct puffs_cc *, const char *, struct vattr *);
    204 int getnodeattr(struct puffs_cc *, struct puffs_node *);
    205 
    206 #endif /* PSSHFS_H_ */
    207