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