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