xref: /netbsd-src/usr.sbin/puffs/mount_psshfs/psshfs.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: psshfs.h,v 1.33 2007/12/07 14:59:22 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 #define DEFAULTREFRESH 30
46 #define REFRESHTIMEOUT(pctx, t) \
47   (!(pctx)->refreshival || ((pctx->refreshival!=-1) && ((t)>pctx->refreshival)))
48 
49 PUFFSOP_PROTOS(psshfs);
50 
51 #define NEXTREQ(pctx) ((pctx->nextreq)++)
52 #define PSSHFSAUTOVAR(pu)						\
53 	struct puffs_cc *pcc = puffs_cc_getcc(pu);			\
54 	struct psshfs_ctx *pctx = puffs_getspecific(pu);		\
55 	uint32_t reqid = NEXTREQ(pctx);					\
56 	struct puffs_framebuf *pb = psbuf_makeout();			\
57 	int rv = 0
58 
59 #define PSSHFSRETURN(rv)						\
60 	puffs_framebuf_destroy(pb);					\
61 	return (rv)
62 
63 #define GETRESPONSE(pb)							\
64 do {									\
65 	if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb, 0) == -1) {	\
66 		rv = errno;						\
67 		goto out;						\
68 	}								\
69 } while (/*CONSTCOND*/0)
70 
71 #define JUSTSEND(pb)							\
72 do {									\
73 	if (puffs_framev_enqueue_justsend(pu,pctx->sshfd,pb,1,0) == -1){\
74 		rv = errno;						\
75 		goto out;						\
76 	}								\
77 } while (/*CONSTCOND*/0)
78 
79 #define SENDCB(pb, f, a)						\
80 do {									\
81 	if (puffs_framev_enqueue_cb(pu, pctx->sshfd, pb,f,a,0) == -1) {	\
82 		rv = errno;						\
83 		goto out;						\
84 	}								\
85 } while (/*CONSTCOND*/0)
86 
87 struct psshfs_dir {
88 	int valid;
89 	struct puffs_node *entry;
90 
91 	char *entryname;
92 	struct vattr va;
93 	time_t attrread;
94 };
95 
96 struct psshfs_fid {
97 	time_t mounttime;
98 	struct puffs_node *node;
99 };
100 
101 struct psshfs_wait {
102 	struct puffs_cc *pw_cc;
103 	int pw_type;
104 	TAILQ_ENTRY(psshfs_wait) pw_entries;
105 };
106 #define PWTYPE_READDIR	1
107 #define PWTYPE_READ1	2
108 #define PWTYPE_READ2	3
109 #define PWTYPE_WRITE	4
110 
111 struct psshfs_node {
112 	struct puffs_node *parent;
113 
114 	struct psshfs_dir *dir;	/* only valid if we're of type VDIR */
115 
116 	size_t denttot;
117 	size_t dentnext;
118 	time_t dentread;
119 	int childcount;
120 
121 	int stat;
122 	int readcount;
123 
124 	time_t attrread;
125 	char *symlink;
126 	time_t slread;
127 
128 	char *fhand_r;
129 	char *fhand_w;
130 	uint32_t fhand_r_len;
131 	uint32_t fhand_w_len;
132 	struct puffs_framebuf *lazyopen_r;
133 	struct puffs_framebuf *lazyopen_w;
134 	int lazyopen_err_r, lazyopen_err_w;
135 
136 	TAILQ_HEAD(, psshfs_wait) pw;
137 };
138 #define PSN_RECLAIMED	0x01
139 #define PSN_HASFH	0x02
140 #define PSN_READDIR	0x04
141 #define PSN_DOLAZY_R	0x08
142 #define PSN_DOLAZY_W	0x10
143 #define PSN_LAZYWAIT_R	0x20
144 #define PSN_LAZYWAIT_W	0x40
145 #define PSN_HANDLECLOSE	0x80
146 
147 #define HANDLE_READ	0x1
148 #define HANDLE_WRITE	0x2
149 
150 struct psshfs_ctx {
151 	int sshfd;
152 	pid_t sshpid;
153 	const char *mountpath;
154 
155 	int protover;
156 	uint32_t nextreq;
157 
158 	struct puffs_framebuf *curpb;
159 
160 	struct psshfs_node psn_root;
161 	ino_t nextino;
162 
163 	int canexport;
164 	time_t mounttime;
165 
166 	int refreshival;
167 };
168 
169 int	psshfs_domount(struct puffs_usermount *);
170 
171 int	psbuf_read(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
172 int	psbuf_write(struct puffs_usermount *, struct puffs_framebuf *,int,int*);
173 int	psbuf_cmp(struct puffs_usermount *,
174 		  struct puffs_framebuf *, struct puffs_framebuf *, int *);
175 
176 struct puffs_framebuf	*psbuf_makeout(void);
177 void			psbuf_recycleout(struct puffs_framebuf *);
178 
179 void	psbuf_put_1(struct puffs_framebuf *, uint8_t);
180 void	psbuf_put_2(struct puffs_framebuf *, uint16_t);
181 void	psbuf_put_4(struct puffs_framebuf *, uint32_t);
182 void	psbuf_put_8(struct puffs_framebuf *, uint64_t);
183 void	psbuf_put_str(struct puffs_framebuf *, const char *);
184 void	psbuf_put_data(struct puffs_framebuf *, const void *, uint32_t);
185 void	psbuf_put_vattr(struct puffs_framebuf *, const struct vattr *);
186 
187 uint8_t		psbuf_get_type(struct puffs_framebuf *);
188 uint32_t	psbuf_get_len(struct puffs_framebuf *);
189 uint32_t	psbuf_get_reqid(struct puffs_framebuf *);
190 
191 int	psbuf_get_1(struct puffs_framebuf *, uint8_t *);
192 int	psbuf_get_2(struct puffs_framebuf *, uint16_t *);
193 int	psbuf_get_4(struct puffs_framebuf *, uint32_t *);
194 int	psbuf_get_8(struct puffs_framebuf *, uint64_t *);
195 int	psbuf_get_str(struct puffs_framebuf *, char **, uint32_t *);
196 int	psbuf_get_vattr(struct puffs_framebuf *, struct vattr *);
197 
198 int	psbuf_expect_status(struct puffs_framebuf *);
199 int	psbuf_expect_handle(struct puffs_framebuf *, char **, uint32_t *);
200 int	psbuf_expect_name(struct puffs_framebuf *, uint32_t *);
201 int	psbuf_expect_attrs(struct puffs_framebuf *, struct vattr *);
202 
203 int	psbuf_do_data(struct puffs_framebuf *, uint8_t *, uint32_t *);
204 
205 void	psbuf_req_data(struct puffs_framebuf *, int, uint32_t,
206 		       const void *, uint32_t);
207 void	psbuf_req_str(struct puffs_framebuf *, int, uint32_t, const char *);
208 
209 int	sftp_readdir(struct puffs_usermount *, 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_usermount *, const char *, struct vattr *);
221 int getnodeattr(struct puffs_usermount *, struct puffs_node *);
222 
223 void closehandles(struct puffs_usermount *, struct psshfs_node *, int);
224 void lazyopen_rresp(struct puffs_usermount *, struct puffs_framebuf *,
225 		    void *, int);
226 void lazyopen_wresp(struct puffs_usermount *, struct puffs_framebuf *,
227 		    void *, int);
228 
229 #endif /* PSSHFS_H_ */
230