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