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