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