Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / puffs / mount_9p / subr.c
blob87002f74d5aac154f951fabcc3bacf4b968e69f0
1 /* $NetBSD: subr.c,v 1.5 2007/05/16 09:57:21 pooka Exp $ */
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
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.
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.
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: subr.c,v 1.5 2007/05/16 09:57:21 pooka Exp $");
31 #endif /* !lint */
33 #include <sys/types.h>
35 #include <errno.h>
36 #include <puffs.h>
37 #include <stdlib.h>
38 #include <util.h>
40 #include "ninepuffs.h"
41 #include "nineproto.h"
43 void
44 qid2vattr(struct vattr *vap, const struct qid9p *qid)
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;
55 static struct puffs_node *
56 makep9pnode(struct puffs_usermount *pu, p9pfid_t fid)
58 struct p9pnode *p9n;
59 struct puffs_node *pn;
61 p9n = emalloc(sizeof(struct p9pnode));
62 memset(p9n, 0, sizeof(struct p9pnode));
63 p9n->fid_base = fid;
64 LIST_INIT(&p9n->dir_openlist);
66 pn = puffs_pn_new(pu, p9n);
67 if (pn == NULL)
68 abort();
70 return pn;
73 struct puffs_node *
74 newp9pnode_va(struct puffs_usermount *pu, const struct vattr *va, p9pfid_t fid)
76 struct puffs_node *pn;
78 pn = makep9pnode(pu, fid);
79 pn->pn_va = *va;
81 return pn;
84 struct puffs_node *
85 newp9pnode_qid(struct puffs_usermount *pu, const struct qid9p *qid,
86 p9pfid_t fid)
88 struct puffs_node *pn;
90 pn = makep9pnode(pu, fid);
91 puffs_vattr_null(&pn->pn_va);
92 qid2vattr(&pn->pn_va, qid);
94 return pn;
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
102 getdfwithoffset(struct puffs_usermount *pu, struct p9pnode *p9n, off_t wantoff,
103 struct dirfid **rfid)
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;
114 LIST_FOREACH(dfp, &p9n->dir_openlist, entries) {
115 if (dfp->seekoff == wantoff) {
116 LIST_REMOVE(dfp, entries);
117 *rfid = dfp;
118 return 0;
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;
130 for (curoff = 0;;) {
131 advance = wantoff - curoff;
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);
141 if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
142 rv = EPROTO;
143 goto out;
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.
154 p9pbuf_get_4(pb, &count);
155 curoff += count;
156 if (count == advance || count == 0)
157 break;
159 p9pbuf_recycleout(pb);
161 puffs_framebuf_destroy(pb);
163 dfp->seekoff = curoff;
164 *rfid = dfp;
165 return 0;
167 out:
168 puffs_framebuf_destroy(pb);
169 free(dfp);
170 return rv;
173 void
174 releasedf(struct puffs_usermount *pu, struct dirfid *dfp)
177 proto_cc_clunkfid(pu, dfp->fid, 0);
178 free(dfp);
181 void
182 storedf(struct p9pnode *p9n, struct dirfid *dfp)
185 LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries);
188 void
189 nukealldf(struct puffs_usermount *pu, struct p9pnode *p9n)
191 struct dirfid *dfp;
193 while ((dfp = LIST_FIRST(&p9n->dir_openlist)) != NULL) {
194 LIST_REMOVE(dfp, entries);
195 releasedf(pu, dfp);