Home | History | Annotate | Line # | Download | only in mount_psshfs
      1  1.40  pooka /*	$NetBSD: psshfs.h,v 1.40 2010/04/01 02:34:09 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4  1.37  pooka  * Copyright (c) 2006-2009  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  *
     15   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  pooka  * SUCH DAMAGE.
     26   1.1  pooka  */
     27   1.1  pooka 
     28   1.1  pooka #ifndef PSSHFS_H_
     29   1.1  pooka #define PSSHFS_H_
     30   1.1  pooka 
     31   1.1  pooka #include <sys/queue.h>
     32   1.1  pooka 
     33   1.1  pooka #include <puffs.h>
     34   1.1  pooka 
     35  1.25  pooka extern unsigned int max_reads;
     36  1.25  pooka 
     37   1.1  pooka /*
     38   1.1  pooka  * Later protocol versions would have some advantages (such as link count
     39   1.1  pooka  * supported directly as a part of stat), but since proto version 3 seems
     40   1.1  pooka  * to be the only widely available version, let's not try to jump through
     41   1.1  pooka  * too many hoops to be compatible with all versions.
     42   1.1  pooka  */
     43   1.1  pooka #define SFTP_PROTOVERSION 3
     44   1.1  pooka 
     45  1.35  pooka /* extensions, held in psshfs_ctx extensions */
     46  1.35  pooka #define SFTP_EXT_POSIX_RENAME	0x01
     47  1.35  pooka #define SFTP_EXT_STATVFS	0x02
     48  1.35  pooka #define SFTP_EXT_FSTATVFS	0x04
     49  1.35  pooka 
     50  1.26  pooka #define DEFAULTREFRESH 30
     51  1.26  pooka #define REFRESHTIMEOUT(pctx, t) \
     52  1.26  pooka   (!(pctx)->refreshival || ((pctx->refreshival!=-1) && ((t)>pctx->refreshival)))
     53   1.4  pooka 
     54   1.1  pooka PUFFSOP_PROTOS(psshfs);
     55   1.1  pooka 
     56   1.1  pooka #define NEXTREQ(pctx) ((pctx->nextreq)++)
     57  1.31  pooka #define PSSHFSAUTOVAR(pu)						\
     58  1.31  pooka 	struct puffs_cc *pcc = puffs_cc_getcc(pu);			\
     59  1.31  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);		\
     60   1.1  pooka 	uint32_t reqid = NEXTREQ(pctx);					\
     61  1.11  pooka 	struct puffs_framebuf *pb = psbuf_makeout();			\
     62   1.1  pooka 	int rv = 0
     63   1.1  pooka 
     64   1.1  pooka #define PSSHFSRETURN(rv)						\
     65  1.11  pooka 	puffs_framebuf_destroy(pb);					\
     66   1.1  pooka 	return (rv)
     67   1.1  pooka 
     68  1.36  pooka #define GETRESPONSE(pb, fd)						\
     69  1.15  pooka do {									\
     70  1.36  pooka 	if (puffs_framev_enqueue_cc(pcc, fd, pb, 0) == -1) 	{	\
     71  1.15  pooka 		rv = errno;						\
     72  1.15  pooka 		goto out;						\
     73  1.15  pooka 	}								\
     74  1.15  pooka } while (/*CONSTCOND*/0)
     75  1.15  pooka 
     76  1.36  pooka #define JUSTSEND(pb,fd)							\
     77  1.15  pooka do {									\
     78  1.36  pooka 	if (puffs_framev_enqueue_justsend(pu, fd, pb, 1, 0) == -1) {	\
     79  1.15  pooka 		rv = errno;						\
     80  1.15  pooka 		goto out;						\
     81  1.15  pooka 	}								\
     82  1.15  pooka } while (/*CONSTCOND*/0)
     83  1.19  pooka 
     84  1.36  pooka #define SENDCB(pb, fd, f, a)						\
     85  1.15  pooka do {									\
     86  1.36  pooka 	if (puffs_framev_enqueue_cb(pu, fd, pb, f, a, 0) == -1) {	\
     87  1.15  pooka 		rv = errno;						\
     88  1.15  pooka 		goto out;						\
     89  1.15  pooka 	}								\
     90  1.15  pooka } while (/*CONSTCOND*/0)
     91  1.13  pooka 
     92   1.1  pooka struct psshfs_dir {
     93   1.1  pooka 	int valid;
     94   1.1  pooka 	struct puffs_node *entry;
     95   1.1  pooka 
     96   1.1  pooka 	char *entryname;
     97   1.1  pooka 	struct vattr va;
     98   1.4  pooka 	time_t attrread;
     99   1.1  pooka };
    100   1.1  pooka 
    101   1.9  pooka struct psshfs_fid {
    102   1.9  pooka 	time_t mounttime;
    103   1.9  pooka 	struct puffs_node *node;
    104   1.9  pooka };
    105   1.9  pooka 
    106  1.25  pooka struct psshfs_wait {
    107  1.25  pooka 	struct puffs_cc *pw_cc;
    108  1.30  pooka 	int pw_type;
    109  1.25  pooka 	TAILQ_ENTRY(psshfs_wait) pw_entries;
    110  1.23  pooka };
    111  1.30  pooka #define PWTYPE_READDIR	1
    112  1.30  pooka #define PWTYPE_READ1	2
    113  1.30  pooka #define PWTYPE_READ2	3
    114  1.30  pooka #define PWTYPE_WRITE	4
    115  1.23  pooka 
    116   1.1  pooka struct psshfs_node {
    117   1.1  pooka 	struct puffs_node *parent;
    118   1.1  pooka 
    119   1.1  pooka 	struct psshfs_dir *dir;	/* only valid if we're of type VDIR */
    120  1.18  pooka 
    121   1.1  pooka 	size_t denttot;
    122   1.1  pooka 	size_t dentnext;
    123   1.1  pooka 	time_t dentread;
    124   1.4  pooka 	int childcount;
    125  1.16  pooka 
    126  1.16  pooka 	int stat;
    127  1.38  pooka 	unsigned readcount;
    128   1.1  pooka 
    129   1.4  pooka 	time_t attrread;
    130  1.27  pooka 	char *symlink;
    131  1.28  pooka 	time_t slread;
    132  1.12  pooka 
    133  1.12  pooka 	char *fhand_r;
    134  1.12  pooka 	char *fhand_w;
    135  1.14  pooka 	uint32_t fhand_r_len;
    136  1.14  pooka 	uint32_t fhand_w_len;
    137  1.30  pooka 	struct puffs_framebuf *lazyopen_r;
    138  1.30  pooka 	struct puffs_framebuf *lazyopen_w;
    139  1.30  pooka 	int lazyopen_err_r, lazyopen_err_w;
    140  1.23  pooka 
    141  1.25  pooka 	TAILQ_HEAD(, psshfs_wait) pw;
    142   1.1  pooka };
    143  1.16  pooka #define PSN_RECLAIMED	0x01
    144  1.16  pooka #define PSN_HASFH	0x02
    145  1.30  pooka #define PSN_READDIR	0x04
    146  1.30  pooka #define PSN_DOLAZY_R	0x08
    147  1.30  pooka #define PSN_DOLAZY_W	0x10
    148  1.30  pooka #define PSN_LAZYWAIT_R	0x20
    149  1.30  pooka #define PSN_LAZYWAIT_W	0x40
    150  1.30  pooka #define PSN_HANDLECLOSE	0x80
    151  1.30  pooka 
    152  1.30  pooka #define HANDLE_READ	0x1
    153  1.30  pooka #define HANDLE_WRITE	0x2
    154   1.1  pooka 
    155   1.1  pooka struct psshfs_ctx {
    156  1.36  pooka 	int numconnections;
    157   1.1  pooka 	int sshfd;
    158  1.36  pooka 	int sshfd_data;
    159   1.1  pooka 	pid_t sshpid;
    160  1.36  pooka 	pid_t sshpid_data;
    161  1.36  pooka 
    162   1.1  pooka 	const char *mountpath;
    163  1.34  pooka 	char **sshargs;
    164   1.1  pooka 
    165   1.1  pooka 	int protover;
    166  1.35  pooka 	int extensions;
    167  1.35  pooka 
    168   1.1  pooka 	uint32_t nextreq;
    169   1.1  pooka 
    170  1.11  pooka 	struct puffs_framebuf *curpb;
    171   1.1  pooka 
    172   1.1  pooka 	struct psshfs_node psn_root;
    173   1.1  pooka 	ino_t nextino;
    174   1.1  pooka 
    175   1.9  pooka 	int canexport;
    176   1.9  pooka 	time_t mounttime;
    177  1.26  pooka 
    178  1.26  pooka 	int refreshival;
    179  1.39  pooka 
    180  1.39  pooka 	int domangleuid, domanglegid;
    181  1.39  pooka 	uid_t mangleuid, myuid;
    182  1.39  pooka 	gid_t manglegid, mygid;
    183   1.1  pooka };
    184  1.36  pooka #define PSSHFD_META 0
    185  1.36  pooka #define PSSHFD_DATA 1
    186   1.1  pooka 
    187  1.36  pooka int	psshfs_handshake(struct puffs_usermount *, int);
    188   1.2  pooka 
    189  1.11  pooka int	psbuf_read(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
    190  1.11  pooka int	psbuf_write(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
    191  1.11  pooka int	psbuf_cmp(struct puffs_usermount *,
    192  1.24  pooka 		  struct puffs_framebuf *, struct puffs_framebuf *, int *);
    193  1.11  pooka 
    194  1.11  pooka struct puffs_framebuf	*psbuf_makeout(void);
    195  1.11  pooka void			psbuf_recycleout(struct puffs_framebuf *);
    196  1.11  pooka 
    197  1.11  pooka void	psbuf_put_1(struct puffs_framebuf *, uint8_t);
    198  1.11  pooka void	psbuf_put_2(struct puffs_framebuf *, uint16_t);
    199  1.11  pooka void	psbuf_put_4(struct puffs_framebuf *, uint32_t);
    200  1.11  pooka void	psbuf_put_8(struct puffs_framebuf *, uint64_t);
    201  1.11  pooka void	psbuf_put_str(struct puffs_framebuf *, const char *);
    202  1.11  pooka void	psbuf_put_data(struct puffs_framebuf *, const void *, uint32_t);
    203  1.39  pooka void	psbuf_put_vattr(struct puffs_framebuf *, const struct vattr *,
    204  1.39  pooka 			const struct psshfs_ctx *);
    205  1.11  pooka 
    206  1.11  pooka uint8_t		psbuf_get_type(struct puffs_framebuf *);
    207  1.11  pooka uint32_t	psbuf_get_len(struct puffs_framebuf *);
    208  1.11  pooka uint32_t	psbuf_get_reqid(struct puffs_framebuf *);
    209  1.11  pooka 
    210  1.11  pooka int	psbuf_get_1(struct puffs_framebuf *, uint8_t *);
    211  1.11  pooka int	psbuf_get_2(struct puffs_framebuf *, uint16_t *);
    212  1.11  pooka int	psbuf_get_4(struct puffs_framebuf *, uint32_t *);
    213  1.11  pooka int	psbuf_get_8(struct puffs_framebuf *, uint64_t *);
    214  1.11  pooka int	psbuf_get_str(struct puffs_framebuf *, char **, uint32_t *);
    215  1.11  pooka int	psbuf_get_vattr(struct puffs_framebuf *, struct vattr *);
    216  1.11  pooka 
    217  1.11  pooka int	psbuf_expect_status(struct puffs_framebuf *);
    218  1.11  pooka int	psbuf_expect_handle(struct puffs_framebuf *, char **, uint32_t *);
    219  1.11  pooka int	psbuf_expect_name(struct puffs_framebuf *, uint32_t *);
    220  1.11  pooka int	psbuf_expect_attrs(struct puffs_framebuf *, struct vattr *);
    221  1.11  pooka 
    222  1.11  pooka int	psbuf_do_data(struct puffs_framebuf *, uint8_t *, uint32_t *);
    223  1.11  pooka 
    224  1.11  pooka void	psbuf_req_data(struct puffs_framebuf *, int, uint32_t,
    225  1.11  pooka 		       const void *, uint32_t);
    226  1.11  pooka void	psbuf_req_str(struct puffs_framebuf *, int, uint32_t, const char *);
    227   1.1  pooka 
    228  1.33  pooka int	sftp_readdir(struct puffs_usermount *, struct psshfs_ctx *,
    229   1.1  pooka 		     struct puffs_node *);
    230   1.1  pooka 
    231   1.1  pooka struct psshfs_dir *lookup(struct psshfs_dir *, size_t, const char *);
    232   1.1  pooka struct puffs_node *makenode(struct puffs_usermount *, struct puffs_node *,
    233  1.40  pooka 			    const struct psshfs_dir *, const struct vattr *);
    234   1.1  pooka struct puffs_node *allocnode(struct puffs_usermount *, struct puffs_node *,
    235   1.1  pooka 			    const char *, const struct vattr *);
    236   1.1  pooka struct psshfs_dir *direnter(struct puffs_node *, const char *);
    237   1.3  pooka void nukenode(struct puffs_node *, const char *, int);
    238   1.5  pooka void doreclaim(struct puffs_node *);
    239  1.31  pooka int getpathattr(struct puffs_usermount *, const char *, struct vattr *);
    240  1.40  pooka int getnodeattr(struct puffs_usermount *, struct puffs_node *, const char *);
    241   1.1  pooka 
    242  1.30  pooka void closehandles(struct puffs_usermount *, struct psshfs_node *, int);
    243  1.30  pooka void lazyopen_rresp(struct puffs_usermount *, struct puffs_framebuf *,
    244  1.30  pooka 		    void *, int);
    245  1.30  pooka void lazyopen_wresp(struct puffs_usermount *, struct puffs_framebuf *,
    246  1.30  pooka 		    void *, int);
    247  1.30  pooka 
    248   1.1  pooka #endif /* PSSHFS_H_ */
    249