ninepuffs.h revision 1.2 1 1.2 pooka /* $NetBSD: ninepuffs.h,v 1.2 2007/04/22 18:10:48 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 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 PUFFS9P_H_
29 1.1 pooka #define PUFFS9P_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.1 pooka PUFFSOP_PROTOS(puffs9p);
36 1.1 pooka
37 1.1 pooka /* Qid structure. optimized for in-mem. different order on-wire */
38 1.1 pooka struct qid9p {
39 1.1 pooka uint64_t qidpath;
40 1.1 pooka uint32_t qidvers;
41 1.1 pooka uint8_t qidtype;
42 1.1 pooka };
43 1.1 pooka
44 1.1 pooka typedef uint16_t p9ptag_t;
45 1.1 pooka typedef uint32_t p9pfid_t;
46 1.1 pooka
47 1.1 pooka /*
48 1.1 pooka * refuse (no, not *that* refuse) to play if the server doesn't
49 1.1 pooka * support requests of at least the following size. It would only
50 1.1 pooka * make life difficult
51 1.1 pooka */
52 1.1 pooka #define P9P_MINREQLEN 512
53 1.1 pooka
54 1.1 pooka #define P9P_DEFREQLEN (16*1024)
55 1.1 pooka #define P9P_INVALFID 0
56 1.1 pooka #define P9P_ROOTFID 1
57 1.1 pooka
58 1.1 pooka #define NEXTTAG(p9p) \
59 1.1 pooka ((++(p9p->nexttag)) == P9PROTO_NOTAG ? p9p->nexttag = 0 : p9p->nexttag)
60 1.1 pooka
61 1.1 pooka #define NEXTFID(p9p) \
62 1.1 pooka ((++(p9p->nextfid)) == P9P_INVALFID ? p9p->nextfid = 2 : p9p->nextfid)
63 1.1 pooka
64 1.1 pooka #define AUTOVAR(pcc) \
65 1.1 pooka struct puffs9p *p9p = puffs_cc_getspecific(pcc); \
66 1.1 pooka uint16_t tag = NEXTTAG(p9p); \
67 1.1 pooka struct p9pbuf *pb = p9pbuf_make(p9p->maxreq, P9PB_OUT); \
68 1.1 pooka int rv = 0
69 1.1 pooka
70 1.1 pooka #define RETURN(rv) \
71 1.1 pooka p9pbuf_destroy(pb); \
72 1.1 pooka return (rv)
73 1.1 pooka
74 1.1 pooka struct puffs9p;
75 1.1 pooka /*
76 1.1 pooka * XXX: urgh
77 1.1 pooka *
78 1.1 pooka * This is slightly messy / abusatory structure. It is used for multiple
79 1.1 pooka * things. Typical life cycle: create output buffer, append to output
80 1.1 pooka * queue (pcc included) . Once the buffer has been sent, the buffer is
81 1.1 pooka * freed and the structure is appended to reqqueue as psreq. It is kept
82 1.1 pooka * there until matching network data is read. Once this happens, the
83 1.1 pooka * data from the received buffer is copied to buffer stored on the queue.
84 1.1 pooka * This should be rewritten, clearly.
85 1.1 pooka *
86 1.1 pooka * Also, this should not be copypasted from psshfs. But that's
87 1.1 pooka * another matter ;)
88 1.1 pooka */
89 1.1 pooka #define P9PB_OUT 0
90 1.1 pooka #define P9PB_IN 1
91 1.1 pooka struct p9pbuf {
92 1.1 pooka struct p9preq {
93 1.1 pooka p9ptag_t tagid;
94 1.1 pooka
95 1.1 pooka /* either ... */
96 1.1 pooka struct puffs_cc *pcc;
97 1.1 pooka
98 1.1 pooka /* ... or */
99 1.1 pooka void (*func)(struct puffs9p *, struct p9pbuf *, void *);
100 1.1 pooka void *arg;
101 1.1 pooka /* no union, we'd need a "which" flag */
102 1.1 pooka
103 1.1 pooka TAILQ_ENTRY(p9pbuf) entries;
104 1.1 pooka } p9pr;
105 1.1 pooka
106 1.1 pooka /* in / out */
107 1.1 pooka uint32_t len;
108 1.1 pooka uint32_t offset;
109 1.1 pooka uint8_t *buf;
110 1.1 pooka
111 1.1 pooka /* helpers for in */
112 1.1 pooka uint32_t remain;
113 1.1 pooka uint8_t type;
114 1.1 pooka p9ptag_t tagid;
115 1.1 pooka
116 1.1 pooka int state;
117 1.1 pooka };
118 1.1 pooka #define P9PBUF_PUT 0
119 1.1 pooka #define P9PBUF_PUTDONE 1
120 1.1 pooka #define P9PBUF_GETLEN 2
121 1.1 pooka #define P9PBUF_GETDATA 3
122 1.1 pooka #define P9PBUF_GETREADY 4
123 1.1 pooka
124 1.1 pooka #define P9PB_CHECK(pb, space) if (pb->remain < (space)) return ENOMEM
125 1.1 pooka
126 1.1 pooka struct puffs9p {
127 1.1 pooka int servsock;
128 1.1 pooka
129 1.1 pooka p9ptag_t nexttag;
130 1.1 pooka p9pfid_t nextfid;
131 1.1 pooka
132 1.1 pooka size_t maxreq; /* negotiated with server */
133 1.1 pooka struct p9pbuf *curpb;
134 1.1 pooka
135 1.1 pooka TAILQ_HEAD(, p9pbuf) outbufq;
136 1.1 pooka TAILQ_HEAD(, p9pbuf) req_queue;
137 1.1 pooka };
138 1.1 pooka
139 1.1 pooka struct dirfid {
140 1.1 pooka p9pfid_t fid;
141 1.1 pooka off_t seekoff;
142 1.1 pooka LIST_ENTRY(dirfid) entries;
143 1.1 pooka };
144 1.1 pooka
145 1.1 pooka struct p9pnode {
146 1.1 pooka p9pfid_t fid_base;
147 1.2 pooka p9pfid_t fid_read;
148 1.2 pooka p9pfid_t fid_write;
149 1.1 pooka int opencount;
150 1.1 pooka
151 1.1 pooka LIST_HEAD(,dirfid) dir_openlist;
152 1.1 pooka };
153 1.1 pooka
154 1.1 pooka struct p9pbuf *p9pbuf_make(size_t, int);
155 1.1 pooka void p9pbuf_destroy(struct p9pbuf *);
156 1.1 pooka void p9pbuf_recycle(struct p9pbuf *, int);
157 1.1 pooka
158 1.1 pooka int p9pbuf_read(struct puffs9p *, struct p9pbuf *);
159 1.1 pooka int p9pbuf_write(struct puffs9p *, struct p9pbuf *);
160 1.1 pooka
161 1.1 pooka void outbuf_enqueue(struct puffs9p *, struct p9pbuf *,
162 1.1 pooka struct puffs_cc *, uint16_t);
163 1.1 pooka void outbuf_enqueue_nocc(struct puffs9p *, struct p9pbuf *,
164 1.1 pooka void (*f)(struct puffs9p *,
165 1.1 pooka struct p9pbuf *, void *),
166 1.1 pooka void *, uint16_t);
167 1.1 pooka struct p9pbuf *req_get(struct puffs9p *, uint16_t);
168 1.1 pooka
169 1.1 pooka
170 1.1 pooka int p9pbuf_put_1(struct p9pbuf *, uint8_t);
171 1.1 pooka int p9pbuf_put_2(struct p9pbuf *, uint16_t);
172 1.1 pooka int p9pbuf_put_4(struct p9pbuf *, uint32_t);
173 1.1 pooka int p9pbuf_put_8(struct p9pbuf *, uint64_t);
174 1.1 pooka int p9pbuf_put_str(struct p9pbuf *, const char *);
175 1.1 pooka int p9pbuf_put_data(struct p9pbuf *, const void *, uint16_t);
176 1.1 pooka int p9pbuf_write_data(struct p9pbuf *, uint8_t *, uint32_t);
177 1.1 pooka
178 1.1 pooka int p9pbuf_get_1(struct p9pbuf *, uint8_t *);
179 1.1 pooka int p9pbuf_get_2(struct p9pbuf *, uint16_t *);
180 1.1 pooka int p9pbuf_get_4(struct p9pbuf *, uint32_t *);
181 1.1 pooka int p9pbuf_get_8(struct p9pbuf *, uint64_t *);
182 1.1 pooka int p9pbuf_get_str(struct p9pbuf *, char **, uint16_t *);
183 1.1 pooka int p9pbuf_get_data(struct p9pbuf *, uint8_t **, uint16_t *);
184 1.1 pooka int p9pbuf_read_data(struct p9pbuf *, uint8_t *, uint32_t);
185 1.1 pooka
186 1.1 pooka int p9pbuf_remaining(struct p9pbuf *);
187 1.1 pooka int p9pbuf_tell(struct p9pbuf *);
188 1.1 pooka void p9pbuf_seekset(struct p9pbuf *, int);
189 1.1 pooka
190 1.1 pooka int proto_getqid(struct p9pbuf *, struct qid9p *);
191 1.1 pooka int proto_getstat(struct p9pbuf *, struct vattr *, char **, uint16_t *);
192 1.1 pooka int proto_expect_walk_nqids(struct p9pbuf *, uint16_t *);
193 1.1 pooka int proto_expect_stat(struct p9pbuf *, struct vattr *);
194 1.1 pooka int proto_expect_qid(struct p9pbuf *, uint8_t, struct qid9p *);
195 1.1 pooka
196 1.1 pooka int proto_cc_dupfid(struct puffs_cc *, p9pfid_t, p9pfid_t);
197 1.1 pooka int proto_cc_clunkfid(struct puffs_cc *, p9pfid_t, int);
198 1.1 pooka int proto_cc_open(struct puffs_cc *, p9pfid_t, p9pfid_t, int);
199 1.1 pooka
200 1.1 pooka void proto_make_stat(struct p9pbuf *, const struct vattr *, const char *);
201 1.1 pooka
202 1.1 pooka struct puffs_node *p9p_handshake(struct puffs_usermount *, const char *);
203 1.1 pooka
204 1.1 pooka void qid2vattr(struct vattr *, const struct qid9p *);
205 1.1 pooka struct puffs_node *newp9pnode_va(struct puffs_usermount *,
206 1.1 pooka const struct vattr *, p9pfid_t);
207 1.1 pooka struct puffs_node *newp9pnode_qid(struct puffs_usermount *,
208 1.1 pooka const struct qid9p *, p9pfid_t);
209 1.1 pooka
210 1.1 pooka int getdfwithoffset(struct puffs_cc *, struct p9pnode *, off_t,
211 1.1 pooka struct dirfid **);
212 1.1 pooka void storedf(struct p9pnode *, struct dirfid *);
213 1.1 pooka void releasedf(struct puffs_cc *, struct dirfid *);
214 1.1 pooka void nukealldf(struct puffs_cc *, struct p9pnode *);
215 1.1 pooka
216 1.1 pooka #endif /* PUFFS9P_H_ */
217