xref: /netbsd-src/usr.sbin/puffs/mount_9p/subr.c (revision 795a40f9c27e9386e7d56c2f08bf6b8f74e63dc3)
1*795a40f9Suwe /*	$NetBSD: subr.c,v 1.8 2020/05/26 19:38:14 uwe Exp $	*/
2e73a712fSpooka 
3e73a712fSpooka /*
4e73a712fSpooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
5e73a712fSpooka  *
6e73a712fSpooka  * Redistribution and use in source and binary forms, with or without
7e73a712fSpooka  * modification, are permitted provided that the following conditions
8e73a712fSpooka  * are met:
9e73a712fSpooka  * 1. Redistributions of source code must retain the above copyright
10e73a712fSpooka  *    notice, this list of conditions and the following disclaimer.
11e73a712fSpooka  * 2. Redistributions in binary form must reproduce the above copyright
12e73a712fSpooka  *    notice, this list of conditions and the following disclaimer in the
13e73a712fSpooka  *    documentation and/or other materials provided with the distribution.
14e73a712fSpooka  *
15e73a712fSpooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16e73a712fSpooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17e73a712fSpooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18e73a712fSpooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e73a712fSpooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e73a712fSpooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21e73a712fSpooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e73a712fSpooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e73a712fSpooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e73a712fSpooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e73a712fSpooka  * SUCH DAMAGE.
26e73a712fSpooka  */
27e73a712fSpooka 
28e73a712fSpooka #include <sys/cdefs.h>
29e73a712fSpooka #ifndef lint
30*795a40f9Suwe __RCSID("$NetBSD: subr.c,v 1.8 2020/05/26 19:38:14 uwe Exp $");
31e73a712fSpooka #endif /* !lint */
32e73a712fSpooka 
33e73a712fSpooka #include <sys/types.h>
34e73a712fSpooka 
35e73a712fSpooka #include <errno.h>
36e73a712fSpooka #include <puffs.h>
37e73a712fSpooka #include <stdlib.h>
38e73a712fSpooka #include <util.h>
39e73a712fSpooka 
40e73a712fSpooka #include "ninepuffs.h"
41e73a712fSpooka #include "nineproto.h"
42e73a712fSpooka 
43e73a712fSpooka void
qid2vattr(struct vattr * vap,const struct qid9p * qid)44e73a712fSpooka qid2vattr(struct vattr *vap, const struct qid9p *qid)
45e73a712fSpooka {
46e73a712fSpooka 
47e73a712fSpooka 	vap->va_fileid = qid->qidpath;
48e73a712fSpooka 	vap->va_gen = qid->qidvers;
49e73a712fSpooka 	if (qid->qidtype & P9PROTO_QID_TYPE_DIR)
50e73a712fSpooka 		vap->va_type = VDIR;
51e73a712fSpooka 	else
52e73a712fSpooka 		vap->va_type = VREG;
53e73a712fSpooka }
54e73a712fSpooka 
55e73a712fSpooka static struct puffs_node *
makep9pnode(struct puffs_usermount * pu,p9pfid_t fid)56e73a712fSpooka makep9pnode(struct puffs_usermount *pu, p9pfid_t fid)
57e73a712fSpooka {
58e73a712fSpooka 	struct p9pnode *p9n;
59e73a712fSpooka 	struct puffs_node *pn;
60e73a712fSpooka 
61e73a712fSpooka 	p9n = emalloc(sizeof(struct p9pnode));
62e73a712fSpooka 	memset(p9n, 0, sizeof(struct p9pnode));
63e73a712fSpooka 	p9n->fid_base = fid;
64e73a712fSpooka 	LIST_INIT(&p9n->dir_openlist);
65e73a712fSpooka 
66e73a712fSpooka 	pn = puffs_pn_new(pu, p9n);
67e73a712fSpooka 	if (pn == NULL)
68e73a712fSpooka 		abort();
69e73a712fSpooka 
70e73a712fSpooka 	return pn;
71e73a712fSpooka }
72e73a712fSpooka 
73e73a712fSpooka struct puffs_node *
newp9pnode_va(struct puffs_usermount * pu,const struct vattr * va,p9pfid_t fid)74e73a712fSpooka newp9pnode_va(struct puffs_usermount *pu, const struct vattr *va, p9pfid_t fid)
75e73a712fSpooka {
76e73a712fSpooka 	struct puffs_node *pn;
77e73a712fSpooka 
78e73a712fSpooka 	pn = makep9pnode(pu, fid);
79e73a712fSpooka 	pn->pn_va = *va;
80e73a712fSpooka 
81e73a712fSpooka 	return pn;
82e73a712fSpooka }
83e73a712fSpooka 
84e73a712fSpooka struct puffs_node *
newp9pnode_qid(struct puffs_usermount * pu,const struct qid9p * qid,p9pfid_t fid)85e73a712fSpooka newp9pnode_qid(struct puffs_usermount *pu, const struct qid9p *qid,
86e73a712fSpooka 	p9pfid_t fid)
87e73a712fSpooka {
88e73a712fSpooka 	struct puffs_node *pn;
89e73a712fSpooka 
90e73a712fSpooka 	pn = makep9pnode(pu, fid);
91e73a712fSpooka 	puffs_vattr_null(&pn->pn_va);
92e73a712fSpooka 	qid2vattr(&pn->pn_va, qid);
93e73a712fSpooka 
94e73a712fSpooka 	return pn;
95e73a712fSpooka }
96e73a712fSpooka 
97e73a712fSpooka /*
98e73a712fSpooka  * search list of fids, or if none is found, walk a fid for a new one
99e73a712fSpooka  * and issue dummy readdirs until we get the result we want
100e73a712fSpooka  */
101e73a712fSpooka int
getdfwithoffset(struct puffs_usermount * pu,struct p9pnode * p9n,off_t wantoff,struct dirfid ** rfid)10221913eabSpooka getdfwithoffset(struct puffs_usermount *pu, struct p9pnode *p9n, off_t wantoff,
103e73a712fSpooka 	struct dirfid **rfid)
104e73a712fSpooka {
10521913eabSpooka 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
10621913eabSpooka 	struct puffs9p *p9p = puffs_getspecific(pu);
107e73a712fSpooka 	struct dirfid *dfp = NULL;
1080b0e609bSpooka 	int rv;
109e73a712fSpooka 
110e73a712fSpooka 	LIST_FOREACH(dfp, &p9n->dir_openlist, entries) {
111e73a712fSpooka 		if (dfp->seekoff == wantoff) {
112e73a712fSpooka 			LIST_REMOVE(dfp, entries);
113e73a712fSpooka 			*rfid = dfp;
114e73a712fSpooka 			return 0;
115e73a712fSpooka 		}
116e73a712fSpooka 	}
117e73a712fSpooka 
118e73a712fSpooka 	/* didn't get off easy?  damn, do manual labour */
119e73a712fSpooka 	dfp = ecalloc(1, sizeof(struct dirfid));
120e73a712fSpooka 	dfp->fid = NEXTFID(p9p);
12121913eabSpooka 	rv = proto_cc_open(pu, p9n->fid_base, dfp->fid, P9PROTO_OMODE_READ);
1220b0e609bSpooka 	if (rv)
1230b0e609bSpooka 		goto out;
124e73a712fSpooka 
125*795a40f9Suwe 	off_t curoff = 0;
126*795a40f9Suwe 	if (wantoff != 0) {
127*795a40f9Suwe 		struct puffs_framebuf *pb = p9pbuf_makeout();
128*795a40f9Suwe 		for (;;) {
129*795a40f9Suwe 			off_t advance = wantoff - curoff;
130e73a712fSpooka 
131*795a40f9Suwe 			p9ptag_t tag = NEXTTAG(p9p);
132e73a712fSpooka 			p9pbuf_put_1(pb, P9PROTO_T_READ);
133e73a712fSpooka 			p9pbuf_put_2(pb, tag);
134e73a712fSpooka 			p9pbuf_put_4(pb, dfp->fid);
135*795a40f9Suwe 			p9pbuf_put_8(pb, curoff);
136e73a712fSpooka 			p9pbuf_put_4(pb, advance);
1375069b5dfSpooka 			GETRESPONSE(pb);
138e73a712fSpooka 
1390e7bdfc1Spooka 			if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
1403a48a88cSozaki-r 				rv = proto_handle_rerror(pu, pb);
141*795a40f9Suwe 				puffs_framebuf_destroy(pb);
1420b0e609bSpooka 				goto out;
143e73a712fSpooka 			}
144e73a712fSpooka 
145e73a712fSpooka 			/*
146*795a40f9Suwe 			 * Check how many bytes we got.  If we got the
147*795a40f9Suwe 			 * amount we wanted, we are at the correct position.
148*795a40f9Suwe 			 * If we got zero bytes, either the directory
149*795a40f9Suwe 			 * doesn't "support" the seek offset we want
150*795a40f9Suwe 			 * (someone has probably inserted an entry
151*795a40f9Suwe 			 * meantime) or we at the end of directory.
152*795a40f9Suwe 			 * Either way, let the upper layer deal with it.
153e73a712fSpooka 			 */
154*795a40f9Suwe 			uint32_t count;
155e73a712fSpooka 			p9pbuf_get_4(pb, &count);
156e73a712fSpooka 			curoff += count;
157e73a712fSpooka 			if (count == advance || count == 0)
158e73a712fSpooka 				break;
159e73a712fSpooka 
1600e7bdfc1Spooka 			p9pbuf_recycleout(pb);
161e73a712fSpooka 		}
162ddf4b370Spooka 		puffs_framebuf_destroy(pb);
163*795a40f9Suwe 	}
164e73a712fSpooka 
165e73a712fSpooka 	dfp->seekoff = curoff;
166e73a712fSpooka 	*rfid = dfp;
167e73a712fSpooka 	return 0;
168e73a712fSpooka 
1690b0e609bSpooka  out:
170e73a712fSpooka 	free(dfp);
1710b0e609bSpooka 	return rv;
172e73a712fSpooka }
173e73a712fSpooka 
174e73a712fSpooka void
releasedf(struct puffs_usermount * pu,struct dirfid * dfp)17521913eabSpooka releasedf(struct puffs_usermount *pu, struct dirfid *dfp)
176e73a712fSpooka {
177e73a712fSpooka 
17821913eabSpooka 	proto_cc_clunkfid(pu, dfp->fid, 0);
179e73a712fSpooka 	free(dfp);
180e73a712fSpooka }
181e73a712fSpooka 
182e73a712fSpooka void
storedf(struct p9pnode * p9n,struct dirfid * dfp)183e73a712fSpooka storedf(struct p9pnode *p9n, struct dirfid *dfp)
184e73a712fSpooka {
185e73a712fSpooka 
186e73a712fSpooka 	LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries);
187e73a712fSpooka }
188e73a712fSpooka 
189e73a712fSpooka void
nukealldf(struct puffs_usermount * pu,struct p9pnode * p9n)19021913eabSpooka nukealldf(struct puffs_usermount *pu, struct p9pnode *p9n)
191e73a712fSpooka {
192ddf4b370Spooka 	struct dirfid *dfp;
193e73a712fSpooka 
194ddf4b370Spooka 	while ((dfp = LIST_FIRST(&p9n->dir_openlist)) != NULL) {
195e73a712fSpooka 		LIST_REMOVE(dfp, entries);
19621913eabSpooka 		releasedf(pu, dfp);
197e73a712fSpooka 	}
198e73a712fSpooka }
199