xref: /netbsd-src/usr.sbin/puffs/mount_9p/subr.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: subr.c,v 1.6 2007/11/30 19:02:39 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 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: subr.c,v 1.6 2007/11/30 19:02:39 pooka Exp $");
31 #endif /* !lint */
32 
33 #include <sys/types.h>
34 
35 #include <errno.h>
36 #include <puffs.h>
37 #include <stdlib.h>
38 #include <util.h>
39 
40 #include "ninepuffs.h"
41 #include "nineproto.h"
42 
43 void
44 qid2vattr(struct vattr *vap, const struct qid9p *qid)
45 {
46 
47 	vap->va_fileid = qid->qidpath;
48 	vap->va_gen = qid->qidvers;
49 	if (qid->qidtype & P9PROTO_QID_TYPE_DIR)
50 		vap->va_type = VDIR;
51 	else
52 		vap->va_type = VREG;
53 }
54 
55 static struct puffs_node *
56 makep9pnode(struct puffs_usermount *pu, p9pfid_t fid)
57 {
58 	struct p9pnode *p9n;
59 	struct puffs_node *pn;
60 
61 	p9n = emalloc(sizeof(struct p9pnode));
62 	memset(p9n, 0, sizeof(struct p9pnode));
63 	p9n->fid_base = fid;
64 	LIST_INIT(&p9n->dir_openlist);
65 
66 	pn = puffs_pn_new(pu, p9n);
67 	if (pn == NULL)
68 		abort();
69 
70 	return pn;
71 }
72 
73 struct puffs_node *
74 newp9pnode_va(struct puffs_usermount *pu, const struct vattr *va, p9pfid_t fid)
75 {
76 	struct puffs_node *pn;
77 
78 	pn = makep9pnode(pu, fid);
79 	pn->pn_va = *va;
80 
81 	return pn;
82 }
83 
84 struct puffs_node *
85 newp9pnode_qid(struct puffs_usermount *pu, const struct qid9p *qid,
86 	p9pfid_t fid)
87 {
88 	struct puffs_node *pn;
89 
90 	pn = makep9pnode(pu, fid);
91 	puffs_vattr_null(&pn->pn_va);
92 	qid2vattr(&pn->pn_va, qid);
93 
94 	return pn;
95 }
96 
97 /*
98  * search list of fids, or if none is found, walk a fid for a new one
99  * and issue dummy readdirs until we get the result we want
100  */
101 int
102 getdfwithoffset(struct puffs_usermount *pu, struct p9pnode *p9n, off_t wantoff,
103 	struct dirfid **rfid)
104 {
105 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
106 	struct puffs9p *p9p = puffs_getspecific(pu);
107 	struct puffs_framebuf *pb;
108 	struct dirfid *dfp = NULL;
109 	p9ptag_t tag = NEXTTAG(p9p);
110 	off_t curoff, advance;
111 	uint32_t count;
112 	int rv;
113 
114 	LIST_FOREACH(dfp, &p9n->dir_openlist, entries) {
115 		if (dfp->seekoff == wantoff) {
116 			LIST_REMOVE(dfp, entries);
117 			*rfid = dfp;
118 			return 0;
119 		}
120 	}
121 
122 	/* didn't get off easy?  damn, do manual labour */
123 	pb = p9pbuf_makeout();
124 	dfp = ecalloc(1, sizeof(struct dirfid));
125 	dfp->fid = NEXTFID(p9p);
126 	rv = proto_cc_open(pu, p9n->fid_base, dfp->fid, P9PROTO_OMODE_READ);
127 	if (rv)
128 		goto out;
129 
130 	for (curoff = 0;;) {
131 		advance = wantoff - curoff;
132 
133 		tag = NEXTTAG(p9p);
134 		p9pbuf_put_1(pb, P9PROTO_T_READ);
135 		p9pbuf_put_2(pb, tag);
136 		p9pbuf_put_4(pb, dfp->fid);
137 		p9pbuf_put_8(pb, 0);
138 		p9pbuf_put_4(pb, advance);
139 		GETRESPONSE(pb);
140 
141 		if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
142 			rv = EPROTO;
143 			goto out;
144 		}
145 
146 		/*
147 		 * Check how many bytes we got.  If we got the amount we
148 		 * wanted, we are at the correct position.  If we got
149 		 * zero bytes, either the directory doesn't "support" the
150 		 * seek offset we want (someone has probably inserted an
151 		 * entry meantime) or we at the end of directory.  Either
152 		 * way, let the upper layer deal with it.
153 		 */
154 		p9pbuf_get_4(pb, &count);
155 		curoff += count;
156 		if (count == advance || count == 0)
157 			break;
158 
159 		p9pbuf_recycleout(pb);
160 	}
161 	puffs_framebuf_destroy(pb);
162 
163 	dfp->seekoff = curoff;
164 	*rfid = dfp;
165 	return 0;
166 
167  out:
168 	puffs_framebuf_destroy(pb);
169 	free(dfp);
170 	return rv;
171 }
172 
173 void
174 releasedf(struct puffs_usermount *pu, struct dirfid *dfp)
175 {
176 
177 	proto_cc_clunkfid(pu, dfp->fid, 0);
178 	free(dfp);
179 }
180 
181 void
182 storedf(struct p9pnode *p9n, struct dirfid *dfp)
183 {
184 
185 	LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries);
186 }
187 
188 void
189 nukealldf(struct puffs_usermount *pu, struct p9pnode *p9n)
190 {
191 	struct dirfid *dfp;
192 
193 	while ((dfp = LIST_FIRST(&p9n->dir_openlist)) != NULL) {
194 		LIST_REMOVE(dfp, entries);
195 		releasedf(pu, dfp);
196 	}
197 }
198