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