xref: /netbsd/usr.sbin/puffs/mount_9p/node.c (revision ff4087e2)
1*ff4087e2Spooka /*	$NetBSD: node.c,v 1.3 2007/05/04 18:17:34 pooka 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*ff4087e2Spooka __RCSID("$NetBSD: node.c,v 1.3 2007/05/04 18:17:34 pooka Exp $");
31e73a712fSpooka #endif /* !lint */
32e73a712fSpooka 
33e73a712fSpooka #include <assert.h>
34e73a712fSpooka #include <errno.h>
35e73a712fSpooka #include <puffs.h>
36e73a712fSpooka #include <stdio.h>
37e73a712fSpooka 
38e73a712fSpooka #include "ninepuffs.h"
39e73a712fSpooka #include "nineproto.h"
40e73a712fSpooka 
41e73a712fSpooka static void *
42e73a712fSpooka nodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
43e73a712fSpooka {
44e73a712fSpooka 	struct vattr *vap = &pn->pn_va;
45e73a712fSpooka 	struct qid9p *qid = arg;
46e73a712fSpooka 
47e73a712fSpooka 	if (vap->va_fileid == qid->qidpath)
48e73a712fSpooka 		return pn;
49e73a712fSpooka 
50e73a712fSpooka 	return NULL;
51e73a712fSpooka }
52e73a712fSpooka 
53e73a712fSpooka int
54e73a712fSpooka puffs9p_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
55e73a712fSpooka 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
56e73a712fSpooka 	const struct puffs_cn *pcn)
57e73a712fSpooka {
58e73a712fSpooka 	AUTOVAR(pcc);
59e73a712fSpooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
60e73a712fSpooka 	struct puffs_node *pn, *pn_dir = opc;
61e73a712fSpooka 	struct p9pnode *p9n_dir = pn_dir->pn_data;
62e73a712fSpooka 	p9ptag_t tfid = NEXTFID(p9p);
63e73a712fSpooka 	struct qid9p newqid;
64e73a712fSpooka 	uint16_t nqid;
65e73a712fSpooka 
66e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_WALK);
67e73a712fSpooka 	p9pbuf_put_2(pb, tag);
68e73a712fSpooka 	p9pbuf_put_4(pb, p9n_dir->fid_base);
69e73a712fSpooka 	p9pbuf_put_4(pb, tfid);
70e73a712fSpooka 	p9pbuf_put_2(pb, 1);
71e73a712fSpooka 	p9pbuf_put_str(pb, pcn->pcn_name);
72e73a712fSpooka 
73e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
74e73a712fSpooka 	puffs_cc_yield(pcc);
75e73a712fSpooka 
76e73a712fSpooka 	rv = proto_expect_walk_nqids(pb, &nqid);
77e73a712fSpooka 	if (rv) {
78e73a712fSpooka 		rv = ENOENT;
79e73a712fSpooka 		goto out;
80e73a712fSpooka 	}
81e73a712fSpooka 	if (nqid != 1 || !proto_getqid(pb, &newqid)) {
82e73a712fSpooka 		rv = EPROTO;
83e73a712fSpooka 		goto out;
84e73a712fSpooka 	}
85e73a712fSpooka 
86e73a712fSpooka 	pn = puffs_pn_nodewalk(pu, nodecmp, &newqid);
87e73a712fSpooka 	if (pn == NULL)
88e73a712fSpooka 		pn = newp9pnode_qid(pu, &newqid, tfid);
89e73a712fSpooka 	else
90e73a712fSpooka 		proto_cc_clunkfid(pcc, tfid, 0);
91e73a712fSpooka 
92e73a712fSpooka 	*newnode = pn;
93e73a712fSpooka 	*newtype = pn->pn_va.va_type;
94e73a712fSpooka 	*newsize = pn->pn_va.va_size;
95e73a712fSpooka 	*newrdev = pn->pn_va.va_rdev;
96e73a712fSpooka 
97e73a712fSpooka  out:
98e73a712fSpooka 	RETURN(rv);
99e73a712fSpooka }
100e73a712fSpooka 
101e73a712fSpooka /*
102e73a712fSpooka  * Problem is that 9P doesn't allow seeking into a directory.  So we
103e73a712fSpooka  * maintain a list of active fids for any given directory.  They
104e73a712fSpooka  * start living at the first read and exist either until the directory
105e73a712fSpooka  * is closed or until they reach the end.
106e73a712fSpooka  */
107e73a712fSpooka int
108e73a712fSpooka puffs9p_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
109e73a712fSpooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
110e73a712fSpooka 	int *eofflag, off_t *cookies, size_t *ncookies)
111e73a712fSpooka {
112e73a712fSpooka 	AUTOVAR(pcc);
113e73a712fSpooka 	struct puffs_node *pn = opc;
114e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
115e73a712fSpooka 	struct vattr va;
116e73a712fSpooka 	struct dirfid *dfp;
117e73a712fSpooka 	char *name;
118e73a712fSpooka 	uint32_t count;
119e73a712fSpooka 	uint16_t statsize;
120e73a712fSpooka 
121e73a712fSpooka 	rv = getdfwithoffset(pcc, p9n, *readoff, &dfp);
122e73a712fSpooka 	if (rv)
123e73a712fSpooka 		goto out;
124e73a712fSpooka 
125e73a712fSpooka 	tag = NEXTTAG(p9p);
126e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_READ);
127e73a712fSpooka 	p9pbuf_put_2(pb, tag);
128e73a712fSpooka 	p9pbuf_put_4(pb, dfp->fid);
129e73a712fSpooka 	p9pbuf_put_8(pb, *readoff);
130e73a712fSpooka 	p9pbuf_put_4(pb, *reslen); /* XXX */
131e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
132e73a712fSpooka 
133e73a712fSpooka 	puffs_cc_yield(pcc);
134e73a712fSpooka 
135e73a712fSpooka 	p9pbuf_get_4(pb, &count);
136e73a712fSpooka 
137e73a712fSpooka 	/*
138e73a712fSpooka 	 * if count is 0, assume we at end-of-dir.  dfp is no longer
139e73a712fSpooka 	 * useful, so nuke it
140e73a712fSpooka 	 */
141e73a712fSpooka 	if (count == 0) {
142e73a712fSpooka 		*eofflag = 1;
143e73a712fSpooka 		releasedf(pcc, dfp);
144e73a712fSpooka 		goto out;
145e73a712fSpooka 	}
146e73a712fSpooka 
147e73a712fSpooka 	while (count > 0) {
148e73a712fSpooka 		if (!proto_getstat(pb, &va, &name, &statsize)) {
149e73a712fSpooka 			rv = EINVAL;
150e73a712fSpooka 			goto out;
151e73a712fSpooka 		}
152e73a712fSpooka 
153e73a712fSpooka 		puffs_nextdent(&dent, name, va.va_fileid,
154e73a712fSpooka 		    puffs_vtype2dt(va.va_type), reslen);
155e73a712fSpooka 
156e73a712fSpooka 		count -= statsize;
157e73a712fSpooka 		*readoff += statsize;
158e73a712fSpooka 		dfp->seekoff += statsize;
159e73a712fSpooka 	}
160e73a712fSpooka 
161e73a712fSpooka 	storedf(p9n, dfp);
162e73a712fSpooka 
163e73a712fSpooka  out:
164e73a712fSpooka 	RETURN(rv);
165e73a712fSpooka }
166e73a712fSpooka 
167e73a712fSpooka int
168e73a712fSpooka puffs9p_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
169e73a712fSpooka 	const struct puffs_cred *pcr, pid_t pid)
170e73a712fSpooka {
171e73a712fSpooka 	AUTOVAR(pcc);
172e73a712fSpooka 	struct puffs_node *pn = opc;
173e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
174e73a712fSpooka 
175e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_STAT);
176e73a712fSpooka 	p9pbuf_put_2(pb, tag);
177e73a712fSpooka 	p9pbuf_put_4(pb, p9n->fid_base);
178e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
179e73a712fSpooka 	puffs_cc_yield(pcc);
180e73a712fSpooka 
181e73a712fSpooka 	rv = proto_expect_stat(pb, &pn->pn_va);
182e73a712fSpooka 	if (rv)
183e73a712fSpooka 		goto out;
184e73a712fSpooka 
185e73a712fSpooka 	memcpy(vap, &pn->pn_va, sizeof(struct vattr));
186e73a712fSpooka 
187e73a712fSpooka  out:
188e73a712fSpooka 	RETURN(rv);
189e73a712fSpooka }
190e73a712fSpooka 
191e73a712fSpooka int
192e73a712fSpooka puffs9p_node_setattr(struct puffs_cc *pcc, void *opc,
193e73a712fSpooka 	const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
194e73a712fSpooka {
195e73a712fSpooka 	AUTOVAR(pcc);
196e73a712fSpooka 	struct puffs_node *pn = opc;
197e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
198e73a712fSpooka 
199e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
200e73a712fSpooka 	p9pbuf_put_2(pb, tag);
201e73a712fSpooka 	p9pbuf_put_4(pb, p9n->fid_base);
202e73a712fSpooka 	proto_make_stat(pb, va, NULL);
203e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
204e73a712fSpooka 	puffs_cc_yield(pcc);
205e73a712fSpooka 
206e73a712fSpooka 	if (pb->type != P9PROTO_R_WSTAT)
207e73a712fSpooka 		rv = EPROTO;
208e73a712fSpooka 
209e73a712fSpooka 	RETURN(rv);
210e73a712fSpooka }
211e73a712fSpooka 
212e73a712fSpooka /*
213e73a712fSpooka  * Ok, time to get clever.  There are two possible cases: we are
214e73a712fSpooka  * opening a file or we are opening a directory.
215e73a712fSpooka  *
216e73a712fSpooka  * If it's a directory, don't bother opening it here, but rather
217e73a712fSpooka  * wait until readdir, since it's probable we need to be able to
218e73a712fSpooka  * open a directory there in any case.
219e73a712fSpooka  *
220436cfd0dSpooka  * If it's a regular file, open it here with whatever credentials
221436cfd0dSpooka  * we happen to have.   Let the upper layers of the kernel worry
222436cfd0dSpooka  * about permission control.
223e73a712fSpooka  *
224436cfd0dSpooka  * XXX: this does not work fully for the mmap case
225e73a712fSpooka  */
226e73a712fSpooka int
227e73a712fSpooka puffs9p_node_open(struct puffs_cc *pcc, void *opc, int mode,
228e73a712fSpooka 	const struct puffs_cred *pcr, pid_t pid)
229e73a712fSpooka {
230e73a712fSpooka 	struct puffs9p *p9p = puffs_cc_getspecific(pcc);
231e73a712fSpooka 	struct puffs_node *pn = opc;
232e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
233e73a712fSpooka 	p9pfid_t nfid;
234e73a712fSpooka 	int error = 0;
235e73a712fSpooka 
236e73a712fSpooka 	p9n->opencount++;
237436cfd0dSpooka 	if (pn->pn_va.va_type != VDIR) {
238436cfd0dSpooka 		if (mode & FREAD && p9n->fid_read == P9P_INVALFID) {
239e73a712fSpooka 			nfid = NEXTFID(p9p);
240e73a712fSpooka 			error = proto_cc_open(pcc, p9n->fid_base, nfid,
241436cfd0dSpooka 			    P9PROTO_OMODE_READ);
242e73a712fSpooka 			if (error)
243e73a712fSpooka 				return error;
244436cfd0dSpooka 			p9n->fid_read = nfid;
245436cfd0dSpooka 		}
246436cfd0dSpooka 		if (mode & FWRITE && p9n->fid_write == P9P_INVALFID) {
247436cfd0dSpooka 			nfid = NEXTFID(p9p);
248436cfd0dSpooka 			error = proto_cc_open(pcc, p9n->fid_base, nfid,
249436cfd0dSpooka 			    P9PROTO_OMODE_WRITE);
250436cfd0dSpooka 			if (error)
251436cfd0dSpooka 				return error;
252436cfd0dSpooka 			p9n->fid_write = nfid;
253436cfd0dSpooka 		}
254e73a712fSpooka 	}
255e73a712fSpooka 
256e73a712fSpooka 	return 0;
257e73a712fSpooka }
258e73a712fSpooka 
259e73a712fSpooka int
260e73a712fSpooka puffs9p_node_close(struct puffs_cc *pcc, void *opc, int flags,
261e73a712fSpooka 	const struct puffs_cred *pcr, pid_t pid)
262e73a712fSpooka {
263e73a712fSpooka 	struct puffs_node *pn = opc;
264e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
265e73a712fSpooka 
266436cfd0dSpooka 	if (--p9n->opencount == 0) {
267436cfd0dSpooka 		if (pn->pn_va.va_type == VDIR) {
268e73a712fSpooka 			nukealldf(pcc, p9n);
269436cfd0dSpooka 		} else  {
270436cfd0dSpooka 			if (p9n->fid_read != P9P_INVALFID) {
271436cfd0dSpooka 				proto_cc_clunkfid(pcc, p9n->fid_read, 0);
272436cfd0dSpooka 				p9n->fid_read = P9P_INVALFID;
273436cfd0dSpooka 			}
274436cfd0dSpooka 			if (p9n->fid_write != P9P_INVALFID) {
275436cfd0dSpooka 				proto_cc_clunkfid(pcc, p9n->fid_write, 0);
276436cfd0dSpooka 				p9n->fid_write = P9P_INVALFID;
277436cfd0dSpooka 			}
278436cfd0dSpooka 		}
279436cfd0dSpooka 	}
280e73a712fSpooka 
281e73a712fSpooka 	return 0;
282e73a712fSpooka }
283e73a712fSpooka 
284e73a712fSpooka int
285e73a712fSpooka puffs9p_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
286e73a712fSpooka 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
287e73a712fSpooka 	int ioflag)
288e73a712fSpooka {
289e73a712fSpooka 	AUTOVAR(pcc);
290e73a712fSpooka 	struct puffs_node *pn = opc;
291e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
292e73a712fSpooka 	uint32_t count;
293e73a712fSpooka 	size_t nread;
294e73a712fSpooka 
295e73a712fSpooka 	nread = 0;
296e73a712fSpooka 	while (*resid > 0) {
297e73a712fSpooka 		p9pbuf_put_1(pb, P9PROTO_T_READ);
298e73a712fSpooka 		p9pbuf_put_2(pb, tag);
299436cfd0dSpooka 		p9pbuf_put_4(pb, p9n->fid_read);
300e73a712fSpooka 		p9pbuf_put_8(pb, offset+nread);
301e73a712fSpooka 		p9pbuf_put_4(pb, MIN((uint32_t)*resid,p9p->maxreq-24));
302e73a712fSpooka 		outbuf_enqueue(p9p, pb, pcc, tag);
303e73a712fSpooka 		puffs_cc_yield(pcc);
304e73a712fSpooka 
305e73a712fSpooka 		if (pb->type != P9PROTO_R_READ) {
306e73a712fSpooka 			rv = EPROTO;
307e73a712fSpooka 			break;
308e73a712fSpooka 		}
309e73a712fSpooka 
310e73a712fSpooka 		p9pbuf_get_4(pb, &count);
311e73a712fSpooka 		if (!p9pbuf_read_data(pb, buf + nread, count)) {
312e73a712fSpooka 			rv = EPROTO;
313e73a712fSpooka 			break;
314e73a712fSpooka 		}
315e73a712fSpooka 
316e73a712fSpooka 		*resid -= count;
317e73a712fSpooka 		nread += count;
318e73a712fSpooka 
319e73a712fSpooka 		p9pbuf_recycle(pb, P9PB_OUT);
320e73a712fSpooka 	}
321e73a712fSpooka 
322e73a712fSpooka 	RETURN(rv);
323e73a712fSpooka }
324e73a712fSpooka 
325e73a712fSpooka int
326e73a712fSpooka puffs9p_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
327e73a712fSpooka 	off_t offset, size_t *resid, const struct puffs_cred *cred,
328e73a712fSpooka 	int ioflag)
329e73a712fSpooka {
330e73a712fSpooka 	AUTOVAR(pcc);
331e73a712fSpooka 	struct puffs_node *pn = opc;
332e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
333e73a712fSpooka 	uint32_t chunk, count;
334e73a712fSpooka 	size_t nwrite;
335e73a712fSpooka 
336e73a712fSpooka 	if (ioflag & PUFFS_IO_APPEND)
337e73a712fSpooka 		offset = pn->pn_va.va_size;
338e73a712fSpooka 
339e73a712fSpooka 	nwrite = 0;
340e73a712fSpooka 	while (*resid > 0) {
341e73a712fSpooka 		chunk = MIN(*resid, p9p->maxreq-32);
342e73a712fSpooka 
343e73a712fSpooka 		p9pbuf_put_1(pb, P9PROTO_T_WRITE);
344e73a712fSpooka 		p9pbuf_put_2(pb, tag);
345436cfd0dSpooka 		p9pbuf_put_4(pb, p9n->fid_write);
346e73a712fSpooka 		p9pbuf_put_8(pb, offset+nwrite);
347e73a712fSpooka 		p9pbuf_put_4(pb, chunk);
348e73a712fSpooka 		p9pbuf_write_data(pb, buf+nwrite, chunk);
349e73a712fSpooka 		outbuf_enqueue(p9p, pb, pcc, tag);
350e73a712fSpooka 		puffs_cc_yield(pcc);
351e73a712fSpooka 
352e73a712fSpooka 		if (pb->type != P9PROTO_R_WRITE) {
353e73a712fSpooka 			rv = EPROTO;
354e73a712fSpooka 			break;
355e73a712fSpooka 		}
356e73a712fSpooka 
357e73a712fSpooka 		p9pbuf_get_4(pb, &count);
358e73a712fSpooka 		*resid -= count;
359e73a712fSpooka 		nwrite += count;
360e73a712fSpooka 
361e73a712fSpooka 		if (count != chunk) {
362e73a712fSpooka 			rv = EPROTO;
363e73a712fSpooka 			break;
364e73a712fSpooka 		}
365e73a712fSpooka 
366e73a712fSpooka 		p9pbuf_recycle(pb, P9PB_OUT);
367e73a712fSpooka 	}
368e73a712fSpooka 
369e73a712fSpooka 	RETURN(rv);
370e73a712fSpooka }
371e73a712fSpooka 
372e73a712fSpooka static int
373e73a712fSpooka nodecreate(struct puffs_cc *pcc, struct puffs_node *pn, void **newnode,
374e73a712fSpooka 	const char *name, const struct vattr *vap, uint32_t dirbit)
375e73a712fSpooka {
376e73a712fSpooka 	AUTOVAR(pcc);
377e73a712fSpooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
378e73a712fSpooka 	struct puffs_node *pn_new;
379e73a712fSpooka 	struct p9pnode *p9n = pn->pn_data;
380e73a712fSpooka 	p9pfid_t nfid = NEXTFID(p9p);
381e73a712fSpooka 	struct qid9p nqid;
382e73a712fSpooka 	int tries = 0;
383e73a712fSpooka 
384e73a712fSpooka  again:
385e73a712fSpooka 	if (++tries > 5) {
386e73a712fSpooka 		rv = EPROTO;
387e73a712fSpooka 		goto out;
388e73a712fSpooka 	}
389e73a712fSpooka 
390e73a712fSpooka 	rv = proto_cc_dupfid(pcc, p9n->fid_base, nfid);
391e73a712fSpooka 	if (rv)
392e73a712fSpooka 		goto out;
393e73a712fSpooka 
394e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_CREATE);
395e73a712fSpooka 	p9pbuf_put_2(pb, tag);
396e73a712fSpooka 	p9pbuf_put_4(pb, nfid);
397e73a712fSpooka 	p9pbuf_put_str(pb, name);
398e73a712fSpooka 	p9pbuf_put_4(pb, dirbit | (vap->va_mode & 0777));
399e73a712fSpooka 	p9pbuf_put_1(pb, 0);
400e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
401e73a712fSpooka 	puffs_cc_yield(pcc);
402e73a712fSpooka 
403e73a712fSpooka 	rv = proto_expect_qid(pb, P9PROTO_R_CREATE, &nqid);
404e73a712fSpooka 	if (rv)
405e73a712fSpooka 		goto out;
406e73a712fSpooka 
407e73a712fSpooka 	/*
408e73a712fSpooka 	 * Now, little problem here: create returns an *open* fid.
409e73a712fSpooka 	 * So, clunk it and walk the parent directory to get a fid
410e73a712fSpooka 	 * which is not open for I/O yet.
411e73a712fSpooka 	 */
412e73a712fSpooka 	proto_cc_clunkfid(pcc, nfid, 0);
413e73a712fSpooka 	nfid = NEXTFID(p9p);
414e73a712fSpooka 
415e73a712fSpooka 	p9pbuf_recycle(pb, P9PB_OUT);
416e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_WALK);
417e73a712fSpooka 	p9pbuf_put_2(pb, tag);
418e73a712fSpooka 	p9pbuf_put_4(pb, p9n->fid_base);
419e73a712fSpooka 	p9pbuf_put_4(pb, nfid);
420e73a712fSpooka 	p9pbuf_put_2(pb, 1);
421e73a712fSpooka 	p9pbuf_put_str(pb, name);
422e73a712fSpooka 
423e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
424e73a712fSpooka 	puffs_cc_yield(pcc);
425e73a712fSpooka 
426e73a712fSpooka 	/*
427e73a712fSpooka 	 * someone removed it already? try again
428e73a712fSpooka 	 * note: this is kind of lose/lose
429e73a712fSpooka 	 */
430e73a712fSpooka 	if (pb->type != P9PROTO_R_WALK)
431e73a712fSpooka 		goto again;
432e73a712fSpooka 
433e73a712fSpooka 	pn_new = newp9pnode_va(pu, vap, nfid);
434e73a712fSpooka 	qid2vattr(&pn_new->pn_va, &nqid);
435e73a712fSpooka 	*newnode = pn_new;
436e73a712fSpooka 
437e73a712fSpooka  out:
438e73a712fSpooka 	RETURN(rv);
439e73a712fSpooka }
440e73a712fSpooka 
441e73a712fSpooka int
442e73a712fSpooka puffs9p_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
443e73a712fSpooka 	const struct puffs_cn *pcn, const struct vattr *va)
444e73a712fSpooka {
445e73a712fSpooka 
446e73a712fSpooka 	return nodecreate(pcc, opc, newnode, pcn->pcn_name, va, 0);
447e73a712fSpooka }
448e73a712fSpooka 
449e73a712fSpooka int
450e73a712fSpooka puffs9p_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
451e73a712fSpooka 	const struct puffs_cn *pcn, const struct vattr *va)
452e73a712fSpooka {
453e73a712fSpooka 
454e73a712fSpooka 	return nodecreate(pcc, opc, newnode, pcn->pcn_name,
455e73a712fSpooka 	    va, P9PROTO_CPERM_DIR);
456e73a712fSpooka }
457e73a712fSpooka 
458e73a712fSpooka /*
459e73a712fSpooka  * Need to be a bit clever again: the fid is clunked no matter if
460e73a712fSpooka  * the remove succeeds or not.  Re-getting a fid would be way too
461e73a712fSpooka  * difficult in case the remove failed for a valid reason (directory
462e73a712fSpooka  * not empty etcetc.).  So walk ourselves another fid to prod the
463e73a712fSpooka  * ice with.
464e73a712fSpooka  */
465e73a712fSpooka static int
466e73a712fSpooka noderemove(struct puffs_cc *pcc, struct p9pnode *p9n)
467e73a712fSpooka {
468e73a712fSpooka 	AUTOVAR(pcc);
469e73a712fSpooka 	p9pfid_t testfid = NEXTFID(p9p);
470e73a712fSpooka 
471e73a712fSpooka 	rv = proto_cc_dupfid(pcc, p9n->fid_base, testfid);
472e73a712fSpooka 
473e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_REMOVE);
474e73a712fSpooka 	p9pbuf_put_2(pb, tag);
475e73a712fSpooka 	p9pbuf_put_4(pb, testfid);
476e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
477e73a712fSpooka 	puffs_cc_yield(pcc);
478e73a712fSpooka 
479e73a712fSpooka 	if (pb->type != P9PROTO_R_REMOVE) {
480e73a712fSpooka 		rv = EPROTO;
481e73a712fSpooka 	} else {
482e73a712fSpooka 		proto_cc_clunkfid(pcc, p9n->fid_base, 0);
483e73a712fSpooka 		p9n->fid_base = P9P_INVALFID;
484e73a712fSpooka 	}
485e73a712fSpooka 
486e73a712fSpooka 	RETURN(rv);
487e73a712fSpooka }
488e73a712fSpooka 
489e73a712fSpooka int
490e73a712fSpooka puffs9p_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
491e73a712fSpooka 	const struct puffs_cn *pcn)
492e73a712fSpooka {
493e73a712fSpooka 	struct puffs_node *pn = targ;
494e73a712fSpooka 
495e73a712fSpooka 	if (pn->pn_va.va_type == VDIR)
496e73a712fSpooka 		return EISDIR;
497e73a712fSpooka 
498e73a712fSpooka 	return noderemove(pcc, pn->pn_data);
499e73a712fSpooka }
500e73a712fSpooka 
501e73a712fSpooka int
502e73a712fSpooka puffs9p_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
503e73a712fSpooka 	const struct puffs_cn *pcn)
504e73a712fSpooka {
505e73a712fSpooka 	struct puffs_node *pn = targ;
506e73a712fSpooka 
507e73a712fSpooka 	if (pn->pn_va.va_type != VDIR)
508e73a712fSpooka 		return ENOTDIR;
509e73a712fSpooka 
510e73a712fSpooka 	return noderemove(pcc, pn->pn_data);
511e73a712fSpooka }
512e73a712fSpooka 
513e73a712fSpooka /*
514e73a712fSpooka  * 9P supports renames only for regular files within a directory
515e73a712fSpooka  * from what I could tell.  So just support in-directory renames
516e73a712fSpooka  * for now.
517e73a712fSpooka  */
518e73a712fSpooka int
519e73a712fSpooka puffs9p_node_rename(struct puffs_cc *pcc, void *opc, void *src,
520e73a712fSpooka 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
521e73a712fSpooka 	const struct puffs_cn *pcn_targ)
522e73a712fSpooka {
523e73a712fSpooka 	AUTOVAR(pcc);
524e73a712fSpooka 	struct puffs_node *pn_src = src;
525e73a712fSpooka 	struct p9pnode *p9n_src = pn_src->pn_data;
526e73a712fSpooka 
527e73a712fSpooka 	if (opc != targ_dir) {
528e73a712fSpooka 		rv = EOPNOTSUPP;
529e73a712fSpooka 		goto out;
530e73a712fSpooka 	}
531e73a712fSpooka 
532e73a712fSpooka 	/* 9P doesn't allow to overwrite in rename */
533e73a712fSpooka 	if (targ) {
534e73a712fSpooka 		struct puffs_node *pn_targ = targ;
535e73a712fSpooka 
536e73a712fSpooka 		rv = noderemove(pcc, pn_targ->pn_data);
537e73a712fSpooka 		if (rv)
538*ff4087e2Spooka 			goto out;
539e73a712fSpooka 	}
540e73a712fSpooka 
541e73a712fSpooka 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
542e73a712fSpooka 	p9pbuf_put_2(pb, tag);
543e73a712fSpooka 	p9pbuf_put_4(pb, p9n_src->fid_base);
544e73a712fSpooka 	proto_make_stat(pb, NULL, pcn_targ->pcn_name);
545e73a712fSpooka 	outbuf_enqueue(p9p, pb, pcc, tag);
546e73a712fSpooka 	puffs_cc_yield(pcc);
547e73a712fSpooka 
548e73a712fSpooka 	if (pb->type != P9PROTO_R_WSTAT)
549e73a712fSpooka 		rv = EPROTO;
550e73a712fSpooka 
551e73a712fSpooka  out:
552e73a712fSpooka 	RETURN(rv);
553e73a712fSpooka }
554e73a712fSpooka 
555e73a712fSpooka /*
556e73a712fSpooka  * - "here's one"
557e73a712fSpooka  * - "9P"
558e73a712fSpooka  * ~ "i'm not dead"
559e73a712fSpooka  * - "you're not fooling anyone you know, you'll be stone dead in a minute
560e73a712fSpooka  * - "he says he's not quite dead"
561e73a712fSpooka  * - "isn't there anything you could do?"
562e73a712fSpooka  * - *clunk*!
563e73a712fSpooka  * - "thanks"
564e73a712fSpooka  */
565e73a712fSpooka int
566e73a712fSpooka puffs9p_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
567e73a712fSpooka {
568e73a712fSpooka #if 0
569e73a712fSpooka 	if (p9n->fid_open != P9P_INVALFID)
570e73a712fSpooka 		proto_cc_clunkfid(pcc, p9n->fid_open, 0);
571e73a712fSpooka #endif
572e73a712fSpooka 
573e73a712fSpooka 	return 0;
574e73a712fSpooka }
575