Home | History | Annotate | Line # | Download | only in mount_9p
node.c revision 1.19.8.1
      1  1.19.8.1  wrstuden /*	$NetBSD: node.c,v 1.19.8.1 2008/09/18 04:30:10 wrstuden Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4       1.1     pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1     pooka  * modification, are permitted provided that the following conditions
      8       1.1     pooka  * are met:
      9       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1     pooka  *
     15       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1     pooka  * SUCH DAMAGE.
     26       1.1     pooka  */
     27       1.1     pooka 
     28       1.1     pooka #include <sys/cdefs.h>
     29       1.1     pooka #ifndef lint
     30  1.19.8.1  wrstuden __RCSID("$NetBSD: node.c,v 1.19.8.1 2008/09/18 04:30:10 wrstuden Exp $");
     31       1.1     pooka #endif /* !lint */
     32       1.1     pooka 
     33       1.1     pooka #include <assert.h>
     34       1.1     pooka #include <errno.h>
     35       1.1     pooka #include <puffs.h>
     36       1.1     pooka #include <stdio.h>
     37       1.9     pooka #include <stdlib.h>
     38       1.1     pooka 
     39       1.1     pooka #include "ninepuffs.h"
     40       1.1     pooka #include "nineproto.h"
     41       1.1     pooka 
     42       1.1     pooka static void *
     43       1.1     pooka nodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
     44       1.1     pooka {
     45       1.1     pooka 	struct vattr *vap = &pn->pn_va;
     46       1.1     pooka 	struct qid9p *qid = arg;
     47       1.1     pooka 
     48  1.19.8.1  wrstuden 	if (vap->va_fileid == qid->qidpath && vap->va_gen == qid->qidvers)
     49       1.1     pooka 		return pn;
     50       1.1     pooka 
     51       1.1     pooka 	return NULL;
     52       1.1     pooka }
     53       1.1     pooka 
     54      1.17     pooka static int
     55      1.19     pooka do_getattr(struct puffs_usermount *pu, struct puffs_node *pn, struct vattr *vap)
     56      1.17     pooka {
     57      1.19     pooka 	AUTOVAR(pu);
     58      1.17     pooka 	struct p9pnode *p9n = pn->pn_data;
     59      1.17     pooka 
     60      1.17     pooka 	p9pbuf_put_1(pb, P9PROTO_T_STAT);
     61      1.17     pooka 	p9pbuf_put_2(pb, tag);
     62      1.17     pooka 	p9pbuf_put_4(pb, p9n->fid_base);
     63      1.17     pooka 	GETRESPONSE(pb);
     64      1.17     pooka 
     65      1.17     pooka 	rv = proto_expect_stat(pb, vap);
     66      1.17     pooka 
     67      1.17     pooka  out:
     68      1.17     pooka 	RETURN(rv);
     69      1.17     pooka }
     70      1.17     pooka 
     71      1.17     pooka int
     72      1.19     pooka puffs9p_node_getattr(struct puffs_usermount *pu, void *opc, struct vattr *vap,
     73      1.18     pooka 	const struct puffs_cred *pcr)
     74      1.17     pooka {
     75      1.17     pooka 	struct puffs_node *pn = opc;
     76      1.17     pooka 	int rv;
     77      1.17     pooka 
     78      1.19     pooka 	rv = do_getattr(pu, pn, &pn->pn_va);
     79      1.17     pooka 	if (rv == 0)
     80      1.17     pooka 		memcpy(vap, &pn->pn_va, sizeof(struct vattr));
     81      1.17     pooka 	return rv;
     82      1.17     pooka }
     83      1.17     pooka 
     84       1.1     pooka int
     85      1.19     pooka puffs9p_node_lookup(struct puffs_usermount *pu, void *opc, struct puffs_newinfo *pni,
     86       1.1     pooka 	const struct puffs_cn *pcn)
     87       1.1     pooka {
     88      1.19     pooka 	AUTOVAR(pu);
     89  1.19.8.1  wrstuden 	struct vattr va;
     90       1.1     pooka 	struct puffs_node *pn, *pn_dir = opc;
     91       1.1     pooka 	struct p9pnode *p9n_dir = pn_dir->pn_data;
     92       1.1     pooka 	p9ptag_t tfid = NEXTFID(p9p);
     93       1.1     pooka 	struct qid9p newqid;
     94       1.1     pooka 	uint16_t nqid;
     95       1.1     pooka 
     96       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_WALK);
     97       1.1     pooka 	p9pbuf_put_2(pb, tag);
     98       1.1     pooka 	p9pbuf_put_4(pb, p9n_dir->fid_base);
     99       1.1     pooka 	p9pbuf_put_4(pb, tfid);
    100       1.1     pooka 	p9pbuf_put_2(pb, 1);
    101       1.1     pooka 	p9pbuf_put_str(pb, pcn->pcn_name);
    102       1.8     pooka 	GETRESPONSE(pb);
    103       1.1     pooka 
    104       1.1     pooka 	rv = proto_expect_walk_nqids(pb, &nqid);
    105       1.1     pooka 	if (rv) {
    106       1.1     pooka 		rv = ENOENT;
    107       1.1     pooka 		goto out;
    108       1.1     pooka 	}
    109       1.4     pooka 	if (nqid != 1) {
    110       1.1     pooka 		rv = EPROTO;
    111       1.1     pooka 		goto out;
    112       1.1     pooka 	}
    113       1.4     pooka 	if ((rv = proto_getqid(pb, &newqid)))
    114       1.4     pooka 		goto out;
    115       1.1     pooka 
    116  1.19.8.1  wrstuden 	/* we get the parent vers in walk(?)  compensate */
    117  1.19.8.1  wrstuden 	p9pbuf_recycleout(pb);
    118  1.19.8.1  wrstuden 	tag = NEXTTAG(p9p);
    119  1.19.8.1  wrstuden 	p9pbuf_put_1(pb, P9PROTO_T_STAT);
    120  1.19.8.1  wrstuden 	p9pbuf_put_2(pb, tag);
    121  1.19.8.1  wrstuden 	p9pbuf_put_4(pb, tfid);
    122  1.19.8.1  wrstuden 	GETRESPONSE(pb);
    123  1.19.8.1  wrstuden 	if ((rv = proto_expect_stat(pb, &va)) != 0) {
    124  1.19.8.1  wrstuden 		proto_cc_clunkfid(pu, tfid, 0);
    125  1.19.8.1  wrstuden 		rv = ENOENT;
    126  1.19.8.1  wrstuden 		goto out;
    127  1.19.8.1  wrstuden 	}
    128  1.19.8.1  wrstuden 	if (newqid.qidpath != va.va_fileid) {
    129  1.19.8.1  wrstuden 		proto_cc_clunkfid(pu, tfid, 0);
    130  1.19.8.1  wrstuden 		rv = EPROTO;
    131  1.19.8.1  wrstuden 		goto out;
    132  1.19.8.1  wrstuden 	}
    133  1.19.8.1  wrstuden 	newqid.qidvers = va.va_gen;
    134  1.19.8.1  wrstuden 
    135       1.1     pooka 	pn = puffs_pn_nodewalk(pu, nodecmp, &newqid);
    136       1.1     pooka 	if (pn == NULL)
    137       1.1     pooka 		pn = newp9pnode_qid(pu, &newqid, tfid);
    138       1.1     pooka 	else
    139      1.19     pooka 		proto_cc_clunkfid(pu, tfid, 0);
    140  1.19.8.1  wrstuden 	/* assert pn */
    141  1.19.8.1  wrstuden 	memcpy(&pn->pn_va, &va, sizeof(va));
    142      1.17     pooka 
    143      1.15     pooka 	puffs_newinfo_setcookie(pni, pn);
    144      1.15     pooka 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    145      1.15     pooka 	puffs_newinfo_setsize(pni, pn->pn_va.va_size);
    146      1.15     pooka 	puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
    147       1.1     pooka 
    148       1.1     pooka  out:
    149       1.1     pooka 	RETURN(rv);
    150       1.1     pooka }
    151       1.1     pooka 
    152       1.1     pooka /*
    153       1.1     pooka  * Problem is that 9P doesn't allow seeking into a directory.  So we
    154       1.1     pooka  * maintain a list of active fids for any given directory.  They
    155       1.1     pooka  * start living at the first read and exist either until the directory
    156       1.1     pooka  * is closed or until they reach the end.
    157       1.1     pooka  */
    158       1.1     pooka int
    159      1.19     pooka puffs9p_node_readdir(struct puffs_usermount *pu, void *opc, struct dirent *dent,
    160       1.1     pooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    161       1.1     pooka 	int *eofflag, off_t *cookies, size_t *ncookies)
    162       1.1     pooka {
    163      1.19     pooka 	AUTOVAR(pu);
    164       1.1     pooka 	struct puffs_node *pn = opc;
    165       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    166       1.1     pooka 	struct vattr va;
    167       1.1     pooka 	struct dirfid *dfp;
    168       1.1     pooka 	char *name;
    169       1.1     pooka 	uint32_t count;
    170       1.1     pooka 	uint16_t statsize;
    171       1.1     pooka 
    172      1.19     pooka 	rv = getdfwithoffset(pu, p9n, *readoff, &dfp);
    173       1.1     pooka 	if (rv)
    174       1.1     pooka 		goto out;
    175       1.1     pooka 
    176       1.1     pooka 	tag = NEXTTAG(p9p);
    177       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_READ);
    178       1.1     pooka 	p9pbuf_put_2(pb, tag);
    179       1.1     pooka 	p9pbuf_put_4(pb, dfp->fid);
    180       1.1     pooka 	p9pbuf_put_8(pb, *readoff);
    181       1.1     pooka 	p9pbuf_put_4(pb, *reslen); /* XXX */
    182       1.8     pooka 	GETRESPONSE(pb);
    183       1.1     pooka 
    184       1.1     pooka 	p9pbuf_get_4(pb, &count);
    185       1.1     pooka 
    186       1.1     pooka 	/*
    187       1.1     pooka 	 * if count is 0, assume we at end-of-dir.  dfp is no longer
    188       1.1     pooka 	 * useful, so nuke it
    189       1.1     pooka 	 */
    190       1.1     pooka 	if (count == 0) {
    191       1.1     pooka 		*eofflag = 1;
    192      1.19     pooka 		releasedf(pu, dfp);
    193       1.1     pooka 		goto out;
    194       1.1     pooka 	}
    195       1.1     pooka 
    196       1.1     pooka 	while (count > 0) {
    197      1.10     pooka 		if ((rv = proto_getstat(pb, &va, &name, &statsize))) {
    198      1.10     pooka 			/*
    199      1.10     pooka 			 * If there was an error, it's unlikely we'll be
    200      1.10     pooka 			 * coming back, so just nuke the dfp.  If we do
    201      1.10     pooka 			 * come back for some strange reason, we'll just
    202      1.10     pooka 			 * regen it.
    203      1.10     pooka 			 */
    204      1.19     pooka 			releasedf(pu, dfp);
    205       1.1     pooka 			goto out;
    206      1.10     pooka 		}
    207       1.1     pooka 
    208       1.1     pooka 		puffs_nextdent(&dent, name, va.va_fileid,
    209       1.1     pooka 		    puffs_vtype2dt(va.va_type), reslen);
    210       1.1     pooka 
    211       1.1     pooka 		count -= statsize;
    212       1.1     pooka 		*readoff += statsize;
    213       1.1     pooka 		dfp->seekoff += statsize;
    214       1.9     pooka 		free(name);
    215       1.1     pooka 	}
    216       1.1     pooka 
    217       1.1     pooka 	storedf(p9n, dfp);
    218       1.1     pooka 
    219       1.1     pooka  out:
    220       1.1     pooka 	RETURN(rv);
    221       1.1     pooka }
    222       1.1     pooka 
    223       1.1     pooka int
    224      1.19     pooka puffs9p_node_setattr(struct puffs_usermount *pu, void *opc,
    225      1.18     pooka 	const struct vattr *va, const struct puffs_cred *pcr)
    226       1.1     pooka {
    227      1.19     pooka 	AUTOVAR(pu);
    228       1.1     pooka 	struct puffs_node *pn = opc;
    229       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    230       1.1     pooka 
    231       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
    232       1.1     pooka 	p9pbuf_put_2(pb, tag);
    233       1.1     pooka 	p9pbuf_put_4(pb, p9n->fid_base);
    234       1.6     pooka 	proto_make_stat(pb, va, NULL, pn->pn_va.va_type);
    235       1.8     pooka 	GETRESPONSE(pb);
    236       1.1     pooka 
    237       1.4     pooka 	if (p9pbuf_get_type(pb) != P9PROTO_R_WSTAT)
    238       1.1     pooka 		rv = EPROTO;
    239       1.1     pooka 
    240      1.11     pooka  out:
    241       1.1     pooka 	RETURN(rv);
    242       1.1     pooka }
    243       1.1     pooka 
    244       1.1     pooka /*
    245       1.1     pooka  * Ok, time to get clever.  There are two possible cases: we are
    246       1.1     pooka  * opening a file or we are opening a directory.
    247       1.1     pooka  *
    248       1.1     pooka  * If it's a directory, don't bother opening it here, but rather
    249       1.1     pooka  * wait until readdir, since it's probable we need to be able to
    250       1.1     pooka  * open a directory there in any case.
    251       1.1     pooka  *
    252       1.2     pooka  * If it's a regular file, open it here with whatever credentials
    253       1.2     pooka  * we happen to have.   Let the upper layers of the kernel worry
    254       1.2     pooka  * about permission control.
    255       1.1     pooka  */
    256       1.1     pooka int
    257      1.19     pooka puffs9p_node_open(struct puffs_usermount *pu, void *opc, int mode,
    258      1.18     pooka 	const struct puffs_cred *pcr)
    259       1.1     pooka {
    260      1.19     pooka 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
    261      1.19     pooka 	struct puffs9p *p9p = puffs_getspecific(pu);
    262       1.1     pooka 	struct puffs_node *pn = opc;
    263       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    264       1.1     pooka 	p9pfid_t nfid;
    265       1.1     pooka 	int error = 0;
    266       1.1     pooka 
    267       1.7     pooka 	puffs_setback(pcc, PUFFS_SETBACK_INACT_N1);
    268       1.2     pooka 	if (pn->pn_va.va_type != VDIR) {
    269       1.2     pooka 		if (mode & FREAD && p9n->fid_read == P9P_INVALFID) {
    270       1.2     pooka 			nfid = NEXTFID(p9p);
    271      1.19     pooka 			error = proto_cc_open(pu, p9n->fid_base, nfid,
    272       1.2     pooka 			    P9PROTO_OMODE_READ);
    273       1.2     pooka 			if (error)
    274       1.2     pooka 				return error;
    275       1.2     pooka 			p9n->fid_read = nfid;
    276       1.2     pooka 		}
    277       1.2     pooka 		if (mode & FWRITE && p9n->fid_write == P9P_INVALFID) {
    278       1.2     pooka 			nfid = NEXTFID(p9p);
    279      1.19     pooka 			error = proto_cc_open(pu, p9n->fid_base, nfid,
    280       1.2     pooka 			    P9PROTO_OMODE_WRITE);
    281       1.2     pooka 			if (error)
    282       1.2     pooka 				return error;
    283       1.2     pooka 			p9n->fid_write = nfid;
    284       1.2     pooka 		}
    285       1.1     pooka 	}
    286       1.1     pooka 
    287       1.1     pooka 	return 0;
    288       1.1     pooka }
    289       1.1     pooka 
    290       1.1     pooka int
    291      1.19     pooka puffs9p_node_inactive(struct puffs_usermount *pu, void *opc)
    292       1.1     pooka {
    293       1.1     pooka 	struct puffs_node *pn = opc;
    294       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    295       1.1     pooka 
    296       1.5     pooka 	if (pn->pn_va.va_type == VDIR) {
    297      1.19     pooka 		nukealldf(pu, p9n);
    298       1.5     pooka 	} else  {
    299       1.5     pooka 		if (p9n->fid_read != P9P_INVALFID) {
    300      1.19     pooka 			proto_cc_clunkfid(pu, p9n->fid_read, 0);
    301       1.5     pooka 			p9n->fid_read = P9P_INVALFID;
    302       1.5     pooka 		}
    303       1.5     pooka 		if (p9n->fid_write != P9P_INVALFID) {
    304      1.19     pooka 			proto_cc_clunkfid(pu, p9n->fid_write, 0);
    305       1.5     pooka 			p9n->fid_write = P9P_INVALFID;
    306       1.2     pooka 		}
    307       1.2     pooka 	}
    308       1.1     pooka 
    309       1.1     pooka 	return 0;
    310       1.1     pooka }
    311       1.1     pooka 
    312       1.1     pooka int
    313      1.19     pooka puffs9p_node_read(struct puffs_usermount *pu, void *opc, uint8_t *buf,
    314       1.1     pooka 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    315       1.1     pooka 	int ioflag)
    316       1.1     pooka {
    317      1.19     pooka 	AUTOVAR(pu);
    318       1.1     pooka 	struct puffs_node *pn = opc;
    319       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    320       1.1     pooka 	uint32_t count;
    321       1.1     pooka 	size_t nread;
    322       1.1     pooka 
    323       1.1     pooka 	nread = 0;
    324  1.19.8.1  wrstuden 	while (*resid > 0 && offset+nread < pn->pn_va.va_size) {
    325       1.1     pooka 		p9pbuf_put_1(pb, P9PROTO_T_READ);
    326       1.1     pooka 		p9pbuf_put_2(pb, tag);
    327       1.2     pooka 		p9pbuf_put_4(pb, p9n->fid_read);
    328       1.1     pooka 		p9pbuf_put_8(pb, offset+nread);
    329       1.1     pooka 		p9pbuf_put_4(pb, MIN((uint32_t)*resid,p9p->maxreq-24));
    330       1.8     pooka 		GETRESPONSE(pb);
    331       1.1     pooka 
    332       1.4     pooka 		if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
    333       1.1     pooka 			rv = EPROTO;
    334       1.1     pooka 			break;
    335       1.1     pooka 		}
    336       1.1     pooka 
    337       1.1     pooka 		p9pbuf_get_4(pb, &count);
    338       1.4     pooka 		if ((rv = p9pbuf_read_data(pb, buf + nread, count)))
    339       1.1     pooka 			break;
    340       1.1     pooka 
    341  1.19.8.1  wrstuden 		if (count == 0)
    342  1.19.8.1  wrstuden 			break;
    343  1.19.8.1  wrstuden 
    344       1.1     pooka 		*resid -= count;
    345       1.1     pooka 		nread += count;
    346       1.1     pooka 
    347       1.4     pooka 		p9pbuf_recycleout(pb);
    348       1.1     pooka 	}
    349       1.1     pooka 
    350      1.11     pooka  out:
    351       1.1     pooka 	RETURN(rv);
    352       1.1     pooka }
    353       1.1     pooka 
    354       1.1     pooka int
    355      1.19     pooka puffs9p_node_write(struct puffs_usermount *pu, void *opc, uint8_t *buf,
    356       1.1     pooka 	off_t offset, size_t *resid, const struct puffs_cred *cred,
    357       1.1     pooka 	int ioflag)
    358       1.1     pooka {
    359      1.19     pooka 	AUTOVAR(pu);
    360       1.1     pooka 	struct puffs_node *pn = opc;
    361       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    362       1.1     pooka 	uint32_t chunk, count;
    363       1.1     pooka 	size_t nwrite;
    364       1.1     pooka 
    365       1.1     pooka 	if (ioflag & PUFFS_IO_APPEND)
    366       1.1     pooka 		offset = pn->pn_va.va_size;
    367       1.1     pooka 
    368       1.1     pooka 	nwrite = 0;
    369       1.1     pooka 	while (*resid > 0) {
    370       1.1     pooka 		chunk = MIN(*resid, p9p->maxreq-32);
    371       1.1     pooka 
    372       1.1     pooka 		p9pbuf_put_1(pb, P9PROTO_T_WRITE);
    373       1.1     pooka 		p9pbuf_put_2(pb, tag);
    374       1.2     pooka 		p9pbuf_put_4(pb, p9n->fid_write);
    375       1.1     pooka 		p9pbuf_put_8(pb, offset+nwrite);
    376       1.1     pooka 		p9pbuf_put_4(pb, chunk);
    377       1.1     pooka 		p9pbuf_write_data(pb, buf+nwrite, chunk);
    378       1.8     pooka 		GETRESPONSE(pb);
    379       1.1     pooka 
    380       1.4     pooka 		if (p9pbuf_get_type(pb) != P9PROTO_R_WRITE) {
    381       1.1     pooka 			rv = EPROTO;
    382       1.1     pooka 			break;
    383       1.1     pooka 		}
    384       1.1     pooka 
    385       1.1     pooka 		p9pbuf_get_4(pb, &count);
    386       1.1     pooka 		*resid -= count;
    387       1.1     pooka 		nwrite += count;
    388       1.1     pooka 
    389       1.1     pooka 		if (count != chunk) {
    390       1.1     pooka 			rv = EPROTO;
    391       1.1     pooka 			break;
    392       1.1     pooka 		}
    393       1.1     pooka 
    394       1.4     pooka 		p9pbuf_recycleout(pb);
    395       1.1     pooka 	}
    396       1.1     pooka 
    397      1.11     pooka  out:
    398       1.1     pooka 	RETURN(rv);
    399       1.1     pooka }
    400       1.1     pooka 
    401       1.1     pooka static int
    402      1.19     pooka nodecreate(struct puffs_usermount *pu, struct puffs_node *pn,
    403      1.15     pooka 	struct puffs_newinfo *pni, const char *name,
    404      1.15     pooka 	const struct vattr *vap, uint32_t dirbit)
    405       1.1     pooka {
    406      1.19     pooka 	AUTOVAR(pu);
    407       1.1     pooka 	struct puffs_node *pn_new;
    408       1.1     pooka 	struct p9pnode *p9n = pn->pn_data;
    409       1.1     pooka 	p9pfid_t nfid = NEXTFID(p9p);
    410       1.1     pooka 	struct qid9p nqid;
    411       1.1     pooka 	int tries = 0;
    412       1.1     pooka 
    413       1.1     pooka  again:
    414       1.1     pooka 	if (++tries > 5) {
    415       1.1     pooka 		rv = EPROTO;
    416       1.1     pooka 		goto out;
    417       1.1     pooka 	}
    418       1.1     pooka 
    419      1.19     pooka 	rv = proto_cc_dupfid(pu, p9n->fid_base, nfid);
    420       1.1     pooka 	if (rv)
    421       1.1     pooka 		goto out;
    422       1.1     pooka 
    423       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_CREATE);
    424       1.1     pooka 	p9pbuf_put_2(pb, tag);
    425       1.1     pooka 	p9pbuf_put_4(pb, nfid);
    426       1.1     pooka 	p9pbuf_put_str(pb, name);
    427       1.1     pooka 	p9pbuf_put_4(pb, dirbit | (vap->va_mode & 0777));
    428       1.1     pooka 	p9pbuf_put_1(pb, 0);
    429       1.8     pooka 	GETRESPONSE(pb);
    430       1.1     pooka 
    431       1.1     pooka 	rv = proto_expect_qid(pb, P9PROTO_R_CREATE, &nqid);
    432       1.1     pooka 	if (rv)
    433       1.1     pooka 		goto out;
    434       1.1     pooka 
    435       1.1     pooka 	/*
    436       1.1     pooka 	 * Now, little problem here: create returns an *open* fid.
    437       1.1     pooka 	 * So, clunk it and walk the parent directory to get a fid
    438       1.1     pooka 	 * which is not open for I/O yet.
    439       1.1     pooka 	 */
    440      1.19     pooka 	proto_cc_clunkfid(pu, nfid, 0);
    441       1.1     pooka 	nfid = NEXTFID(p9p);
    442       1.1     pooka 
    443       1.4     pooka 	p9pbuf_recycleout(pb);
    444       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_WALK);
    445       1.1     pooka 	p9pbuf_put_2(pb, tag);
    446       1.1     pooka 	p9pbuf_put_4(pb, p9n->fid_base);
    447       1.1     pooka 	p9pbuf_put_4(pb, nfid);
    448       1.1     pooka 	p9pbuf_put_2(pb, 1);
    449       1.1     pooka 	p9pbuf_put_str(pb, name);
    450       1.8     pooka 	GETRESPONSE(pb);
    451       1.1     pooka 
    452       1.1     pooka 	/*
    453       1.1     pooka 	 * someone removed it already? try again
    454       1.1     pooka 	 * note: this is kind of lose/lose
    455       1.1     pooka 	 */
    456       1.4     pooka 	if (p9pbuf_get_type(pb) != P9PROTO_R_WALK)
    457       1.1     pooka 		goto again;
    458       1.1     pooka 
    459       1.1     pooka 	pn_new = newp9pnode_va(pu, vap, nfid);
    460       1.1     pooka 	qid2vattr(&pn_new->pn_va, &nqid);
    461      1.15     pooka 	puffs_newinfo_setcookie(pni, pn_new);
    462       1.1     pooka 
    463       1.1     pooka  out:
    464       1.1     pooka 	RETURN(rv);
    465       1.1     pooka }
    466       1.1     pooka 
    467       1.1     pooka int
    468      1.19     pooka puffs9p_node_create(struct puffs_usermount *pu, void *opc, struct puffs_newinfo *pni,
    469       1.1     pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    470       1.1     pooka {
    471       1.1     pooka 
    472      1.19     pooka 	return nodecreate(pu, opc, pni, pcn->pcn_name, va, 0);
    473       1.1     pooka }
    474       1.1     pooka 
    475       1.1     pooka int
    476      1.19     pooka puffs9p_node_mkdir(struct puffs_usermount *pu, void *opc, struct puffs_newinfo *pni,
    477       1.1     pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    478       1.1     pooka {
    479       1.1     pooka 
    480      1.19     pooka 	return nodecreate(pu, opc, pni, pcn->pcn_name,
    481       1.1     pooka 	    va, P9PROTO_CPERM_DIR);
    482       1.1     pooka }
    483       1.1     pooka 
    484       1.1     pooka /*
    485       1.1     pooka  * Need to be a bit clever again: the fid is clunked no matter if
    486       1.1     pooka  * the remove succeeds or not.  Re-getting a fid would be way too
    487       1.1     pooka  * difficult in case the remove failed for a valid reason (directory
    488       1.1     pooka  * not empty etcetc.).  So walk ourselves another fid to prod the
    489       1.1     pooka  * ice with.
    490       1.1     pooka  */
    491       1.1     pooka static int
    492      1.19     pooka noderemove(struct puffs_usermount *pu, struct puffs_node *pn)
    493       1.1     pooka {
    494      1.19     pooka 	AUTOVAR(pu);
    495      1.13     pooka 	struct p9pnode *p9n = pn->pn_data;
    496       1.1     pooka 	p9pfid_t testfid = NEXTFID(p9p);
    497       1.1     pooka 
    498      1.19     pooka 	rv = proto_cc_dupfid(pu, p9n->fid_base, testfid);
    499      1.11     pooka 	if (rv)
    500      1.11     pooka 		goto out;
    501       1.1     pooka 
    502       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_REMOVE);
    503       1.1     pooka 	p9pbuf_put_2(pb, tag);
    504       1.1     pooka 	p9pbuf_put_4(pb, testfid);
    505      1.11     pooka 
    506      1.11     pooka 	/*
    507      1.11     pooka 	 * XXX: error handling isn't very robust, but doom is impending
    508      1.11     pooka 	 * anyway, so just accept we're going belly up and play dead
    509      1.11     pooka 	 */
    510       1.8     pooka 	GETRESPONSE(pb);
    511       1.1     pooka 
    512       1.4     pooka 	if (p9pbuf_get_type(pb) != P9PROTO_R_REMOVE) {
    513       1.1     pooka 		rv = EPROTO;
    514       1.1     pooka 	} else {
    515      1.19     pooka 		proto_cc_clunkfid(pu, p9n->fid_base, 0);
    516       1.1     pooka 		p9n->fid_base = P9P_INVALFID;
    517      1.13     pooka 		puffs_pn_remove(pn);
    518       1.1     pooka 	}
    519       1.1     pooka 
    520      1.11     pooka  out:
    521      1.12     pooka 	if (rv == 0)
    522      1.12     pooka 		puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
    523      1.12     pooka 
    524       1.1     pooka 	RETURN(rv);
    525       1.1     pooka }
    526       1.1     pooka 
    527       1.1     pooka int
    528      1.19     pooka puffs9p_node_remove(struct puffs_usermount *pu, void *opc, void *targ,
    529       1.1     pooka 	const struct puffs_cn *pcn)
    530       1.1     pooka {
    531       1.1     pooka 	struct puffs_node *pn = targ;
    532       1.1     pooka 
    533       1.1     pooka 	if (pn->pn_va.va_type == VDIR)
    534       1.1     pooka 		return EISDIR;
    535       1.1     pooka 
    536      1.19     pooka 	return noderemove(pu, pn);
    537       1.1     pooka }
    538       1.1     pooka 
    539       1.1     pooka int
    540      1.19     pooka puffs9p_node_rmdir(struct puffs_usermount *pu, void *opc, void *targ,
    541       1.1     pooka 	const struct puffs_cn *pcn)
    542       1.1     pooka {
    543       1.1     pooka 	struct puffs_node *pn = targ;
    544       1.1     pooka 
    545       1.1     pooka 	if (pn->pn_va.va_type != VDIR)
    546       1.1     pooka 		return ENOTDIR;
    547       1.1     pooka 
    548      1.19     pooka 	return noderemove(pu, pn);
    549       1.1     pooka }
    550       1.1     pooka 
    551       1.1     pooka /*
    552       1.6     pooka  * 9P supports renames only for files within a directory
    553       1.1     pooka  * from what I could tell.  So just support in-directory renames
    554       1.1     pooka  * for now.
    555       1.1     pooka  */
    556       1.1     pooka int
    557      1.19     pooka puffs9p_node_rename(struct puffs_usermount *pu, void *opc, void *src,
    558       1.1     pooka 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    559       1.1     pooka 	const struct puffs_cn *pcn_targ)
    560       1.1     pooka {
    561      1.19     pooka 	AUTOVAR(pu);
    562       1.1     pooka 	struct puffs_node *pn_src = src;
    563       1.1     pooka 	struct p9pnode *p9n_src = pn_src->pn_data;
    564       1.1     pooka 
    565       1.1     pooka 	if (opc != targ_dir) {
    566       1.1     pooka 		rv = EOPNOTSUPP;
    567       1.1     pooka 		goto out;
    568       1.1     pooka 	}
    569       1.1     pooka 
    570       1.1     pooka 	/* 9P doesn't allow to overwrite in rename */
    571       1.1     pooka 	if (targ) {
    572       1.1     pooka 		struct puffs_node *pn_targ = targ;
    573       1.1     pooka 
    574      1.19     pooka 		rv = noderemove(pu, pn_targ->pn_data);
    575       1.1     pooka 		if (rv)
    576       1.3     pooka 			goto out;
    577       1.1     pooka 	}
    578       1.1     pooka 
    579       1.1     pooka 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
    580       1.1     pooka 	p9pbuf_put_2(pb, tag);
    581       1.1     pooka 	p9pbuf_put_4(pb, p9n_src->fid_base);
    582       1.6     pooka 	proto_make_stat(pb, NULL, pcn_targ->pcn_name, pn_src->pn_va.va_type);
    583       1.8     pooka 	GETRESPONSE(pb);
    584       1.1     pooka 
    585       1.4     pooka 	if (p9pbuf_get_type(pb) != P9PROTO_R_WSTAT)
    586       1.1     pooka 		rv = EPROTO;
    587       1.1     pooka 
    588       1.1     pooka  out:
    589       1.1     pooka 	RETURN(rv);
    590       1.1     pooka }
    591       1.1     pooka 
    592       1.1     pooka /*
    593       1.1     pooka  * - "here's one"
    594       1.1     pooka  * - "9P"
    595       1.1     pooka  * ~ "i'm not dead"
    596       1.1     pooka  * - "you're not fooling anyone you know, you'll be stone dead in a minute
    597       1.1     pooka  * - "he says he's not quite dead"
    598       1.1     pooka  * - "isn't there anything you could do?"
    599       1.1     pooka  * - *clunk*!
    600       1.1     pooka  * - "thanks"
    601       1.1     pooka  */
    602       1.1     pooka int
    603      1.19     pooka puffs9p_node_reclaim(struct puffs_usermount *pu, void *opc)
    604       1.1     pooka {
    605      1.12     pooka 	struct puffs_node *pn = opc;
    606      1.12     pooka 	struct p9pnode *p9n = pn->pn_data;
    607      1.12     pooka 
    608      1.12     pooka 	assert(LIST_EMPTY(&p9n->dir_openlist));
    609      1.12     pooka 	assert(p9n->fid_read == P9P_INVALFID && p9n->fid_write == P9P_INVALFID);
    610      1.12     pooka 
    611      1.19     pooka 	proto_cc_clunkfid(pu, p9n->fid_base, 0);
    612      1.12     pooka 	free(p9n);
    613      1.12     pooka 	puffs_pn_put(pn);
    614       1.1     pooka 
    615       1.1     pooka 	return 0;
    616       1.1     pooka }
    617