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