Home | History | Annotate | Line # | Download | only in mount_9p
node.c revision 1.13
      1 /*	$NetBSD: node.c,v 1.13 2007/06/24 19:13:39 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.13 2007/06/24 19:13:39 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, pid_t pid)
    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, pid_t pid)
    199 {
    200 	AUTOVAR(pcc);
    201 	struct puffs_node *pn = opc;
    202 	struct p9pnode *p9n = pn->pn_data;
    203 
    204 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
    205 	p9pbuf_put_2(pb, tag);
    206 	p9pbuf_put_4(pb, p9n->fid_base);
    207 	proto_make_stat(pb, va, NULL, pn->pn_va.va_type);
    208 	GETRESPONSE(pb);
    209 
    210 	if (p9pbuf_get_type(pb) != P9PROTO_R_WSTAT)
    211 		rv = EPROTO;
    212 
    213  out:
    214 	RETURN(rv);
    215 }
    216 
    217 /*
    218  * Ok, time to get clever.  There are two possible cases: we are
    219  * opening a file or we are opening a directory.
    220  *
    221  * If it's a directory, don't bother opening it here, but rather
    222  * wait until readdir, since it's probable we need to be able to
    223  * open a directory there in any case.
    224  *
    225  * If it's a regular file, open it here with whatever credentials
    226  * we happen to have.   Let the upper layers of the kernel worry
    227  * about permission control.
    228  */
    229 int
    230 puffs9p_node_open(struct puffs_cc *pcc, void *opc, int mode,
    231 	const struct puffs_cred *pcr, pid_t pid)
    232 {
    233 	struct puffs9p *p9p = puffs_cc_getspecific(pcc);
    234 	struct puffs_node *pn = opc;
    235 	struct p9pnode *p9n = pn->pn_data;
    236 	p9pfid_t nfid;
    237 	int error = 0;
    238 
    239 	puffs_setback(pcc, PUFFS_SETBACK_INACT_N1);
    240 	if (pn->pn_va.va_type != VDIR) {
    241 		if (mode & FREAD && p9n->fid_read == P9P_INVALFID) {
    242 			nfid = NEXTFID(p9p);
    243 			error = proto_cc_open(pcc, p9n->fid_base, nfid,
    244 			    P9PROTO_OMODE_READ);
    245 			if (error)
    246 				return error;
    247 			p9n->fid_read = nfid;
    248 		}
    249 		if (mode & FWRITE && p9n->fid_write == P9P_INVALFID) {
    250 			nfid = NEXTFID(p9p);
    251 			error = proto_cc_open(pcc, p9n->fid_base, nfid,
    252 			    P9PROTO_OMODE_WRITE);
    253 			if (error)
    254 				return error;
    255 			p9n->fid_write = nfid;
    256 		}
    257 	}
    258 
    259 	return 0;
    260 }
    261 
    262 int
    263 puffs9p_node_inactive(struct puffs_cc *pcc, void *opc, pid_t pid,
    264 	int *refcount)
    265 {
    266 	struct puffs_node *pn = opc;
    267 	struct p9pnode *p9n = pn->pn_data;
    268 
    269 	if (pn->pn_va.va_type == VDIR) {
    270 		nukealldf(pcc, p9n);
    271 	} else  {
    272 		if (p9n->fid_read != P9P_INVALFID) {
    273 			proto_cc_clunkfid(pcc, p9n->fid_read, 0);
    274 			p9n->fid_read = P9P_INVALFID;
    275 		}
    276 		if (p9n->fid_write != P9P_INVALFID) {
    277 			proto_cc_clunkfid(pcc, p9n->fid_write, 0);
    278 			p9n->fid_write = P9P_INVALFID;
    279 		}
    280 	}
    281 
    282 	*refcount = 1;
    283 	return 0;
    284 }
    285 
    286 int
    287 puffs9p_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    288 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    289 	int ioflag)
    290 {
    291 	AUTOVAR(pcc);
    292 	struct puffs_node *pn = opc;
    293 	struct p9pnode *p9n = pn->pn_data;
    294 	uint32_t count;
    295 	size_t nread;
    296 
    297 	nread = 0;
    298 	while (*resid > 0) {
    299 		p9pbuf_put_1(pb, P9PROTO_T_READ);
    300 		p9pbuf_put_2(pb, tag);
    301 		p9pbuf_put_4(pb, p9n->fid_read);
    302 		p9pbuf_put_8(pb, offset+nread);
    303 		p9pbuf_put_4(pb, MIN((uint32_t)*resid,p9p->maxreq-24));
    304 		GETRESPONSE(pb);
    305 
    306 		if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
    307 			rv = EPROTO;
    308 			break;
    309 		}
    310 
    311 		p9pbuf_get_4(pb, &count);
    312 		if ((rv = p9pbuf_read_data(pb, buf + nread, count)))
    313 			break;
    314 
    315 		*resid -= count;
    316 		nread += count;
    317 
    318 		p9pbuf_recycleout(pb);
    319 	}
    320 
    321  out:
    322 	RETURN(rv);
    323 }
    324 
    325 int
    326 puffs9p_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    327 	off_t offset, size_t *resid, const struct puffs_cred *cred,
    328 	int ioflag)
    329 {
    330 	AUTOVAR(pcc);
    331 	struct puffs_node *pn = opc;
    332 	struct p9pnode *p9n = pn->pn_data;
    333 	uint32_t chunk, count;
    334 	size_t nwrite;
    335 
    336 	if (ioflag & PUFFS_IO_APPEND)
    337 		offset = pn->pn_va.va_size;
    338 
    339 	nwrite = 0;
    340 	while (*resid > 0) {
    341 		chunk = MIN(*resid, p9p->maxreq-32);
    342 
    343 		p9pbuf_put_1(pb, P9PROTO_T_WRITE);
    344 		p9pbuf_put_2(pb, tag);
    345 		p9pbuf_put_4(pb, p9n->fid_write);
    346 		p9pbuf_put_8(pb, offset+nwrite);
    347 		p9pbuf_put_4(pb, chunk);
    348 		p9pbuf_write_data(pb, buf+nwrite, chunk);
    349 		GETRESPONSE(pb);
    350 
    351 		if (p9pbuf_get_type(pb) != P9PROTO_R_WRITE) {
    352 			rv = EPROTO;
    353 			break;
    354 		}
    355 
    356 		p9pbuf_get_4(pb, &count);
    357 		*resid -= count;
    358 		nwrite += count;
    359 
    360 		if (count != chunk) {
    361 			rv = EPROTO;
    362 			break;
    363 		}
    364 
    365 		p9pbuf_recycleout(pb);
    366 	}
    367 
    368  out:
    369 	RETURN(rv);
    370 }
    371 
    372 static int
    373 nodecreate(struct puffs_cc *pcc, struct puffs_node *pn, void **newnode,
    374 	const char *name, const struct vattr *vap, uint32_t dirbit)
    375 {
    376 	AUTOVAR(pcc);
    377 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    378 	struct puffs_node *pn_new;
    379 	struct p9pnode *p9n = pn->pn_data;
    380 	p9pfid_t nfid = NEXTFID(p9p);
    381 	struct qid9p nqid;
    382 	int tries = 0;
    383 
    384  again:
    385 	if (++tries > 5) {
    386 		rv = EPROTO;
    387 		goto out;
    388 	}
    389 
    390 	rv = proto_cc_dupfid(pcc, p9n->fid_base, nfid);
    391 	if (rv)
    392 		goto out;
    393 
    394 	p9pbuf_put_1(pb, P9PROTO_T_CREATE);
    395 	p9pbuf_put_2(pb, tag);
    396 	p9pbuf_put_4(pb, nfid);
    397 	p9pbuf_put_str(pb, name);
    398 	p9pbuf_put_4(pb, dirbit | (vap->va_mode & 0777));
    399 	p9pbuf_put_1(pb, 0);
    400 	GETRESPONSE(pb);
    401 
    402 	rv = proto_expect_qid(pb, P9PROTO_R_CREATE, &nqid);
    403 	if (rv)
    404 		goto out;
    405 
    406 	/*
    407 	 * Now, little problem here: create returns an *open* fid.
    408 	 * So, clunk it and walk the parent directory to get a fid
    409 	 * which is not open for I/O yet.
    410 	 */
    411 	proto_cc_clunkfid(pcc, nfid, 0);
    412 	nfid = NEXTFID(p9p);
    413 
    414 	p9pbuf_recycleout(pb);
    415 	p9pbuf_put_1(pb, P9PROTO_T_WALK);
    416 	p9pbuf_put_2(pb, tag);
    417 	p9pbuf_put_4(pb, p9n->fid_base);
    418 	p9pbuf_put_4(pb, nfid);
    419 	p9pbuf_put_2(pb, 1);
    420 	p9pbuf_put_str(pb, name);
    421 	GETRESPONSE(pb);
    422 
    423 	/*
    424 	 * someone removed it already? try again
    425 	 * note: this is kind of lose/lose
    426 	 */
    427 	if (p9pbuf_get_type(pb) != P9PROTO_R_WALK)
    428 		goto again;
    429 
    430 	pn_new = newp9pnode_va(pu, vap, nfid);
    431 	qid2vattr(&pn_new->pn_va, &nqid);
    432 	*newnode = pn_new;
    433 
    434  out:
    435 	RETURN(rv);
    436 }
    437 
    438 int
    439 puffs9p_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    440 	const struct puffs_cn *pcn, const struct vattr *va)
    441 {
    442 
    443 	return nodecreate(pcc, opc, newnode, pcn->pcn_name, va, 0);
    444 }
    445 
    446 int
    447 puffs9p_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    448 	const struct puffs_cn *pcn, const struct vattr *va)
    449 {
    450 
    451 	return nodecreate(pcc, opc, newnode, pcn->pcn_name,
    452 	    va, P9PROTO_CPERM_DIR);
    453 }
    454 
    455 /*
    456  * Need to be a bit clever again: the fid is clunked no matter if
    457  * the remove succeeds or not.  Re-getting a fid would be way too
    458  * difficult in case the remove failed for a valid reason (directory
    459  * not empty etcetc.).  So walk ourselves another fid to prod the
    460  * ice with.
    461  */
    462 static int
    463 noderemove(struct puffs_cc *pcc, struct puffs_node *pn)
    464 {
    465 	AUTOVAR(pcc);
    466 	struct p9pnode *p9n = pn->pn_data;
    467 	p9pfid_t testfid = NEXTFID(p9p);
    468 
    469 	rv = proto_cc_dupfid(pcc, p9n->fid_base, testfid);
    470 	if (rv)
    471 		goto out;
    472 
    473 	p9pbuf_put_1(pb, P9PROTO_T_REMOVE);
    474 	p9pbuf_put_2(pb, tag);
    475 	p9pbuf_put_4(pb, testfid);
    476 
    477 	/*
    478 	 * XXX: error handling isn't very robust, but doom is impending
    479 	 * anyway, so just accept we're going belly up and play dead
    480 	 */
    481 	GETRESPONSE(pb);
    482 
    483 	if (p9pbuf_get_type(pb) != P9PROTO_R_REMOVE) {
    484 		rv = EPROTO;
    485 	} else {
    486 		proto_cc_clunkfid(pcc, p9n->fid_base, 0);
    487 		p9n->fid_base = P9P_INVALFID;
    488 		puffs_pn_remove(pn);
    489 	}
    490 
    491  out:
    492 	if (rv == 0)
    493 		puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
    494 
    495 	RETURN(rv);
    496 }
    497 
    498 int
    499 puffs9p_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    500 	const struct puffs_cn *pcn)
    501 {
    502 	struct puffs_node *pn = targ;
    503 
    504 	if (pn->pn_va.va_type == VDIR)
    505 		return EISDIR;
    506 
    507 	return noderemove(pcc, pn);
    508 }
    509 
    510 int
    511 puffs9p_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    512 	const struct puffs_cn *pcn)
    513 {
    514 	struct puffs_node *pn = targ;
    515 
    516 	if (pn->pn_va.va_type != VDIR)
    517 		return ENOTDIR;
    518 
    519 	return noderemove(pcc, pn);
    520 }
    521 
    522 /*
    523  * 9P supports renames only for files within a directory
    524  * from what I could tell.  So just support in-directory renames
    525  * for now.
    526  */
    527 int
    528 puffs9p_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    529 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    530 	const struct puffs_cn *pcn_targ)
    531 {
    532 	AUTOVAR(pcc);
    533 	struct puffs_node *pn_src = src;
    534 	struct p9pnode *p9n_src = pn_src->pn_data;
    535 
    536 	if (opc != targ_dir) {
    537 		rv = EOPNOTSUPP;
    538 		goto out;
    539 	}
    540 
    541 	/* 9P doesn't allow to overwrite in rename */
    542 	if (targ) {
    543 		struct puffs_node *pn_targ = targ;
    544 
    545 		rv = noderemove(pcc, pn_targ->pn_data);
    546 		if (rv)
    547 			goto out;
    548 	}
    549 
    550 	p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
    551 	p9pbuf_put_2(pb, tag);
    552 	p9pbuf_put_4(pb, p9n_src->fid_base);
    553 	proto_make_stat(pb, NULL, pcn_targ->pcn_name, pn_src->pn_va.va_type);
    554 	GETRESPONSE(pb);
    555 
    556 	if (p9pbuf_get_type(pb) != P9PROTO_R_WSTAT)
    557 		rv = EPROTO;
    558 
    559  out:
    560 	RETURN(rv);
    561 }
    562 
    563 /*
    564  * - "here's one"
    565  * - "9P"
    566  * ~ "i'm not dead"
    567  * - "you're not fooling anyone you know, you'll be stone dead in a minute
    568  * - "he says he's not quite dead"
    569  * - "isn't there anything you could do?"
    570  * - *clunk*!
    571  * - "thanks"
    572  */
    573 int
    574 puffs9p_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    575 {
    576 	struct puffs_node *pn = opc;
    577 	struct p9pnode *p9n = pn->pn_data;
    578 
    579 	assert(LIST_EMPTY(&p9n->dir_openlist));
    580 	assert(p9n->fid_read == P9P_INVALFID && p9n->fid_write == P9P_INVALFID);
    581 
    582 	proto_cc_clunkfid(pcc, p9n->fid_base, 0);
    583 	free(p9n);
    584 	puffs_pn_put(pn);
    585 
    586 	return 0;
    587 }
    588