Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.h revision 1.2
      1  1.2  pooka /*	$NetBSD: psshfs.h,v 1.2 2007/01/06 18:25:19 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  * 3. The name of the company nor the name of the author may be used to
     15  1.1  pooka  *    endorse or promote products derived from this software without specific
     16  1.1  pooka  *    prior written permission.
     17  1.1  pooka  *
     18  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  pooka  * SUCH DAMAGE.
     29  1.1  pooka  */
     30  1.1  pooka 
     31  1.1  pooka #ifndef PSSHFS_H_
     32  1.1  pooka #define PSSHFS_H_
     33  1.1  pooka 
     34  1.1  pooka #include <sys/queue.h>
     35  1.1  pooka 
     36  1.1  pooka #include <puffs.h>
     37  1.1  pooka 
     38  1.1  pooka /*
     39  1.1  pooka  * Later protocol versions would have some advantages (such as link count
     40  1.1  pooka  * supported directly as a part of stat), but since proto version 3 seems
     41  1.1  pooka  * to be the only widely available version, let's not try to jump through
     42  1.1  pooka  * too many hoops to be compatible with all versions.
     43  1.1  pooka  */
     44  1.1  pooka #define SFTP_PROTOVERSION 3
     45  1.1  pooka 
     46  1.1  pooka /*
     47  1.1  pooka  * Refresh directories every n seconds as indicated by the following macro.
     48  1.1  pooka  * Note that local changes will still be visible immediately.
     49  1.1  pooka  */
     50  1.1  pooka #define PSSHFS_REFRESHIVAL 10
     51  1.1  pooka 
     52  1.1  pooka PUFFSOP_PROTOS(psshfs);
     53  1.1  pooka 
     54  1.1  pooka #define NEXTREQ(pctx) ((pctx->nextreq)++)
     55  1.1  pooka #define PSSHFSAUTOVAR(pcc)						\
     56  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);	\
     57  1.1  pooka 	struct psshfs_ctx *pctx = pu->pu_privdata;			\
     58  1.1  pooka 	uint32_t reqid = NEXTREQ(pctx);					\
     59  1.1  pooka 	struct psbuf *pb = psbuf_make(PSB_OUT);				\
     60  1.1  pooka 	int rv = 0
     61  1.1  pooka 
     62  1.1  pooka #define PSSHFSRETURN(rv)						\
     63  1.1  pooka 	psbuf_destroy(pb);						\
     64  1.1  pooka 	return (rv)
     65  1.1  pooka 
     66  1.1  pooka struct psshfs_dir {
     67  1.1  pooka 	int valid;
     68  1.1  pooka 	struct puffs_node *entry;
     69  1.1  pooka 
     70  1.1  pooka 	char *entryname;
     71  1.1  pooka 	struct vattr va;
     72  1.1  pooka };
     73  1.1  pooka 
     74  1.1  pooka struct psshfs_node {
     75  1.1  pooka 	struct puffs_node *parent;
     76  1.1  pooka 
     77  1.1  pooka 	struct psshfs_dir *dir;	/* only valid if we're of type VDIR */
     78  1.1  pooka 	size_t denttot;
     79  1.1  pooka 	size_t dentnext;
     80  1.1  pooka 	time_t dentread;
     81  1.1  pooka 	int hasvattr;
     82  1.1  pooka 
     83  1.1  pooka 	int childcount;
     84  1.1  pooka };
     85  1.1  pooka 
     86  1.1  pooka /*
     87  1.1  pooka  * XXX: urgh
     88  1.1  pooka  *
     89  1.1  pooka  * This is slightly messy / abusatory structure.  It is used for multiple
     90  1.1  pooka  * things.  Typical life cycle: create output buffer, append to output
     91  1.1  pooka  * queue (pcc included) .  Once the buffer has been sent, the buffer is
     92  1.1  pooka  * freed and the structure is appended to reqqueue as psreq.  It is kept
     93  1.1  pooka  * there until matching network data is read.  Once this happens, the
     94  1.1  pooka  * data from the received buffer is copied to buffer stored on the queue.
     95  1.1  pooka  * This should be rewritten, clearly.
     96  1.1  pooka  */
     97  1.1  pooka #define PSDEFALLOC 0x2000
     98  1.1  pooka #define PSBUFMAX 0x40000
     99  1.1  pooka #define PSB_OUT 0
    100  1.1  pooka #define PSB_IN 1
    101  1.1  pooka struct psbuf {
    102  1.1  pooka 	struct psreq {
    103  1.1  pooka 		struct puffs_cc *pcc;
    104  1.1  pooka 		uint32_t reqid;
    105  1.1  pooka 		TAILQ_ENTRY(psbuf) entries;
    106  1.1  pooka 	} psr;
    107  1.1  pooka 
    108  1.1  pooka 	/* in / out */
    109  1.1  pooka 	uint32_t len;
    110  1.1  pooka 	uint32_t remain;
    111  1.1  pooka 	uint32_t offset;
    112  1.1  pooka 	uint8_t *buf;
    113  1.1  pooka 
    114  1.1  pooka 	int state;
    115  1.1  pooka 
    116  1.1  pooka 	/* helpers for in */
    117  1.1  pooka 	uint8_t type;		/* buf[0] */
    118  1.1  pooka 	uint32_t reqid;		/* buf[1-4] */
    119  1.1  pooka };
    120  1.1  pooka #define PSBUF_PUT 0
    121  1.1  pooka #define PSBUF_PUTDONE 1
    122  1.1  pooka #define PSBUF_GETLEN 2
    123  1.1  pooka #define PSBUF_GETDATA 3
    124  1.1  pooka #define PSBUF_GETREADY 4
    125  1.1  pooka 
    126  1.1  pooka struct psshfs_ctx {
    127  1.1  pooka 	int sshfd;
    128  1.1  pooka 	pid_t sshpid;
    129  1.1  pooka 	const char *mountpath;
    130  1.1  pooka 
    131  1.1  pooka 	int protover;
    132  1.1  pooka 	uint32_t nextreq;
    133  1.1  pooka 
    134  1.1  pooka 	struct psbuf *curpb;
    135  1.1  pooka 
    136  1.1  pooka 	struct psshfs_node psn_root;
    137  1.1  pooka 	ino_t nextino;
    138  1.1  pooka 
    139  1.1  pooka 	TAILQ_HEAD(, psbuf) outbufq;
    140  1.1  pooka 	TAILQ_HEAD(, psbuf) req_queue;
    141  1.1  pooka };
    142  1.1  pooka 
    143  1.2  pooka int	psshfs_domount(struct puffs_usermount *);
    144  1.2  pooka 
    145  1.1  pooka struct psbuf 	*psbuf_make(int);
    146  1.1  pooka void		psbuf_destroy(struct psbuf *);
    147  1.1  pooka void		psbuf_recycle(struct psbuf *, int);
    148  1.1  pooka 
    149  1.1  pooka int		psbuf_read(struct psshfs_ctx *, struct psbuf *);
    150  1.1  pooka int		psbuf_write(struct psshfs_ctx *, struct psbuf *);
    151  1.1  pooka 
    152  1.1  pooka void		pssh_outbuf_enqueue(struct psshfs_ctx *, struct psbuf *,
    153  1.1  pooka 				    struct puffs_cc *, uint32_t);
    154  1.1  pooka void		psshreq_put(struct psshfs_ctx *, struct psbuf *);
    155  1.1  pooka struct psbuf	*psshreq_get(struct psshfs_ctx *, uint32_t);
    156  1.1  pooka 
    157  1.1  pooka 
    158  1.1  pooka int	psbuf_put_1(struct psbuf *, uint8_t);
    159  1.1  pooka int	psbuf_put_2(struct psbuf *, uint16_t);
    160  1.1  pooka int	psbuf_put_4(struct psbuf *, uint32_t);
    161  1.1  pooka int	psbuf_put_8(struct psbuf *, uint64_t);
    162  1.1  pooka int	psbuf_put_str(struct psbuf *, const char *);
    163  1.1  pooka int	psbuf_put_data(struct psbuf *, const void *, uint32_t);
    164  1.1  pooka int	psbuf_put_vattr(struct psbuf *, const struct vattr *);
    165  1.1  pooka 
    166  1.1  pooka int	psbuf_get_1(struct psbuf *, uint8_t *);
    167  1.1  pooka int	psbuf_get_2(struct psbuf *, uint16_t *);
    168  1.1  pooka int	psbuf_get_4(struct psbuf *, uint32_t *);
    169  1.1  pooka int	psbuf_get_8(struct psbuf *, uint64_t *);
    170  1.1  pooka int	psbuf_get_str(struct psbuf *, char **, uint32_t *);
    171  1.1  pooka int	psbuf_get_vattr(struct psbuf *, struct vattr *);
    172  1.1  pooka 
    173  1.1  pooka int	psbuf_expect_status(struct psbuf *);
    174  1.1  pooka int	psbuf_expect_handle(struct psbuf *, char **, size_t *);
    175  1.1  pooka int	psbuf_expect_name(struct psbuf *, uint32_t *);
    176  1.1  pooka int	psbuf_expect_attrs(struct psbuf *, struct vattr *);
    177  1.1  pooka 
    178  1.1  pooka int	psbuf_do_data(struct psbuf *, uint8_t *, size_t *);
    179  1.1  pooka 
    180  1.1  pooka int	psbuf_req_data(struct psbuf *, int, uint32_t, const void *, size_t);
    181  1.1  pooka int	psbuf_req_str(struct psbuf *, int, uint32_t, const char *);
    182  1.1  pooka 
    183  1.1  pooka 
    184  1.1  pooka int	sftp_readdir(struct puffs_cc *, struct psshfs_ctx *,
    185  1.1  pooka 		     struct puffs_node *);
    186  1.1  pooka 
    187  1.1  pooka struct psshfs_dir *lookup(struct psshfs_dir *, size_t, const char *);
    188  1.1  pooka struct puffs_node *makenode(struct puffs_usermount *, struct puffs_node *,
    189  1.1  pooka 			    struct psshfs_dir *, const struct vattr *);
    190  1.1  pooka struct puffs_node *allocnode(struct puffs_usermount *, struct puffs_node *,
    191  1.1  pooka 			    const char *, const struct vattr *);
    192  1.1  pooka struct psshfs_dir *direnter(struct puffs_node *, const char *);
    193  1.1  pooka void nukenode(struct puffs_node *, const char *);
    194  1.1  pooka 
    195  1.1  pooka #endif /* PSSHFS_H_ */
    196