Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.h revision 1.10
      1 /*	$NetBSD: psshfs.h,v 1.10 2007/04/19 20:31:09 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 #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 psbuf *pb = psbuf_make(PSB_OUT);				\
     62 	int rv = 0
     63 
     64 #define PSSHFSRETURN(rv)						\
     65 	psbuf_destroy(pb);						\
     66 	return (rv)
     67 
     68 struct psshfs_dir {
     69 	int valid;
     70 	struct puffs_node *entry;
     71 
     72 	char *entryname;
     73 	struct vattr va;
     74 	time_t attrread;
     75 };
     76 
     77 struct psshfs_fid {
     78 	time_t mounttime;
     79 	struct puffs_node *node;
     80 };
     81 
     82 struct psshfs_node {
     83 	struct puffs_node *parent;
     84 
     85 	struct psshfs_dir *dir;	/* only valid if we're of type VDIR */
     86 	size_t denttot;
     87 	size_t dentnext;
     88 	time_t dentread;
     89 	int childcount;
     90 	int reclaimed;
     91 	int hasfh;
     92 
     93 	time_t attrread;
     94 };
     95 
     96 struct psshfs_ctx;
     97 /*
     98  * XXX: urgh
     99  *
    100  * This is slightly messy / abusatory structure.  It is used for multiple
    101  * things.  Typical life cycle: create output buffer, append to output
    102  * queue (pcc included) .  Once the buffer has been sent, the buffer is
    103  * freed and the structure is appended to reqqueue as psreq.  It is kept
    104  * there until matching network data is read.  Once this happens, the
    105  * data from the received buffer is copied to buffer stored on the queue.
    106  * This should be rewritten, clearly.
    107  */
    108 #define PSDEFALLOC 0x2000
    109 #define PSBUFMAX 0x40000
    110 #define PSB_OUT 0
    111 #define PSB_IN 1
    112 struct psbuf {
    113 	struct psreq {
    114 		uint32_t reqid;
    115 
    116 		/* either ... */
    117 		struct puffs_cc *pcc;
    118 
    119 		/* ... or */
    120 		void (*func)(struct psshfs_ctx *, struct psbuf *, void *);
    121 		void *arg;
    122 		/* no union, we'd need a "which" flag */
    123 
    124 		TAILQ_ENTRY(psbuf) entries;
    125 	} psr;
    126 
    127 	/* in / out */
    128 	uint32_t len;
    129 	uint32_t remain;
    130 	uint32_t offset;
    131 	uint8_t *buf;
    132 
    133 	int state;
    134 
    135 	/* helpers for in */
    136 	uint8_t type;		/* buf[0] */
    137 	uint32_t reqid;		/* buf[1-4] */
    138 };
    139 #define PSBUF_PUT 0
    140 #define PSBUF_PUTDONE 1
    141 #define PSBUF_GETLEN 2
    142 #define PSBUF_GETDATA 3
    143 #define PSBUF_GETREADY 4
    144 
    145 struct psshfs_ctx {
    146 	int sshfd;
    147 	pid_t sshpid;
    148 	const char *mountpath;
    149 
    150 	int protover;
    151 	uint32_t nextreq;
    152 
    153 	struct psbuf *curpb;
    154 
    155 	struct psshfs_node psn_root;
    156 	ino_t nextino;
    157 
    158 	int canexport;
    159 	time_t mounttime;
    160 
    161 	TAILQ_HEAD(, psbuf) outbufq;
    162 	TAILQ_HEAD(, psbuf) req_queue;
    163 };
    164 
    165 int	psshfs_domount(struct puffs_usermount *);
    166 
    167 struct psbuf 	*psbuf_make(int);
    168 void		psbuf_destroy(struct psbuf *);
    169 void		psbuf_recycle(struct psbuf *, int);
    170 
    171 int		psbuf_read(struct psshfs_ctx *, struct psbuf *);
    172 int		psbuf_write(struct psshfs_ctx *, struct psbuf *);
    173 
    174 void		pssh_outbuf_enqueue(struct psshfs_ctx *, struct psbuf *,
    175 				    struct puffs_cc *, uint32_t);
    176 void		pssh_outbuf_enqueue_nocc(struct psshfs_ctx *, struct psbuf *,
    177 				    void (*f)(struct psshfs_ctx *,
    178 					      struct psbuf *, void *),
    179 				    void *, uint32_t);
    180 struct psbuf	*psshreq_get(struct psshfs_ctx *, uint32_t);
    181 
    182 
    183 int	psbuf_put_1(struct psbuf *, uint8_t);
    184 int	psbuf_put_2(struct psbuf *, uint16_t);
    185 int	psbuf_put_4(struct psbuf *, uint32_t);
    186 int	psbuf_put_8(struct psbuf *, uint64_t);
    187 int	psbuf_put_str(struct psbuf *, const char *);
    188 int	psbuf_put_data(struct psbuf *, const void *, uint32_t);
    189 int	psbuf_put_vattr(struct psbuf *, const struct vattr *);
    190 
    191 int	psbuf_get_1(struct psbuf *, uint8_t *);
    192 int	psbuf_get_2(struct psbuf *, uint16_t *);
    193 int	psbuf_get_4(struct psbuf *, uint32_t *);
    194 int	psbuf_get_8(struct psbuf *, uint64_t *);
    195 int	psbuf_get_str(struct psbuf *, char **, uint32_t *);
    196 int	psbuf_get_vattr(struct psbuf *, struct vattr *);
    197 
    198 int	psbuf_expect_status(struct psbuf *);
    199 int	psbuf_expect_handle(struct psbuf *, char **, uint32_t *);
    200 int	psbuf_expect_name(struct psbuf *, uint32_t *);
    201 int	psbuf_expect_attrs(struct psbuf *, struct vattr *);
    202 
    203 int	psbuf_do_data(struct psbuf *, uint8_t *, uint32_t *);
    204 
    205 int	psbuf_req_data(struct psbuf *, int, uint32_t, const void *, uint32_t);
    206 int	psbuf_req_str(struct psbuf *, int, uint32_t, const char *);
    207 
    208 
    209 int	sftp_readdir(struct puffs_cc *, struct psshfs_ctx *,
    210 		     struct puffs_node *);
    211 
    212 struct psshfs_dir *lookup(struct psshfs_dir *, size_t, const char *);
    213 struct puffs_node *makenode(struct puffs_usermount *, struct puffs_node *,
    214 			    struct psshfs_dir *, const struct vattr *);
    215 struct puffs_node *allocnode(struct puffs_usermount *, struct puffs_node *,
    216 			    const char *, const struct vattr *);
    217 struct psshfs_dir *direnter(struct puffs_node *, const char *);
    218 void nukenode(struct puffs_node *, const char *, int);
    219 void doreclaim(struct puffs_node *);
    220 int getpathattr(struct puffs_cc *, const char *, struct vattr *);
    221 int getnodeattr(struct puffs_cc *, struct puffs_node *);
    222 
    223 #endif /* PSSHFS_H_ */
    224