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