Home | History | Annotate | Line # | Download | only in mount_psshfs
node.c revision 1.39
      1 /*	$NetBSD: node.c,v 1.39 2007/08/24 13:33:51 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006  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.39 2007/08/24 13:33:51 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <assert.h>
     34 #include <errno.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 
     38 #include "psshfs.h"
     39 #include "sftp_proto.h"
     40 
     41 int
     42 psshfs_node_lookup(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
     43 	const struct puffs_cn *pcn)
     44 {
     45         struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
     46 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     47 	struct puffs_node *pn_dir = opc;
     48 	struct psshfs_node *psn, *psn_dir = pn_dir->pn_data;
     49 	struct puffs_node *pn;
     50 	struct psshfs_dir *pd;
     51 	struct vattr va;
     52 	int rv;
     53 
     54 	if (PCNISDOTDOT(pcn)) {
     55 		psn = psn_dir->parent->pn_data;
     56 		psn->stat &= ~PSN_RECLAIMED;
     57 
     58 		puffs_newinfo_setcookie(pni, psn_dir->parent);
     59 		puffs_newinfo_setvtype(pni, VDIR);
     60 		return 0;
     61 	}
     62 
     63 	rv = sftp_readdir(pcc, pctx, pn_dir);
     64 	if (rv) {
     65 		if (rv != EPERM)
     66 			return rv;
     67 
     68 		/*
     69 		 * Can't read the directory.  We still might be
     70 		 * able to find the node with getattr in -r+x dirs
     71 		 */
     72 		rv = getpathattr(pcc, PCNPATH(pcn), &va);
     73 		if (rv)
     74 			return rv;
     75 
     76 		/* guess */
     77 		if (va.va_type == VDIR)
     78 			va.va_nlink = 2;
     79 		else
     80 			va.va_nlink = 1;
     81 
     82 		pn = allocnode(pu, pn_dir, pcn->pcn_name, &va);
     83 		psn = pn->pn_data;
     84 		psn->attrread = time(NULL);
     85 	} else {
     86 		pd = lookup(psn_dir->dir, psn_dir->dentnext, pcn->pcn_name);
     87 		if (!pd) {
     88 			return ENOENT;
     89 		}
     90 
     91 		if (pd->entry)
     92 			pn = pd->entry;
     93 		else
     94 			pn = makenode(pu, pn_dir, pd, &pd->va);
     95 		psn = pn->pn_data;
     96 	}
     97 
     98 	psn->stat &= ~PSN_RECLAIMED;
     99 
    100 	puffs_newinfo_setcookie(pni, pn);
    101 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    102 	puffs_newinfo_setsize(pni, pn->pn_va.va_size);
    103 
    104 	return 0;
    105 }
    106 
    107 int
    108 psshfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
    109 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    110 {
    111 	struct puffs_node *pn = opc;
    112 	int rv;
    113 
    114 	rv = getnodeattr(pcc, pn);
    115 	if (rv)
    116 		return rv;
    117 
    118 	memcpy(vap, &pn->pn_va, sizeof(struct vattr));
    119 
    120 	return 0;
    121 }
    122 
    123 int
    124 psshfs_node_setattr(struct puffs_cc *pcc, void *opc,
    125 	const struct vattr *va, const struct puffs_cred *pcr,
    126 	const struct puffs_cid *pcid)
    127 {
    128 	PSSHFSAUTOVAR(pcc);
    129 	struct vattr kludgeva;
    130 	struct puffs_node *pn = opc;
    131 
    132 	psbuf_req_str(pb, SSH_FXP_SETSTAT, reqid, PNPATH(pn));
    133 
    134 	memcpy(&kludgeva, va, sizeof(struct vattr));
    135 
    136 	/* XXX: kludge due to openssh server implementation */
    137 	if (va->va_atime.tv_sec != PUFFS_VNOVAL
    138 	    && va->va_mtime.tv_sec == PUFFS_VNOVAL) {
    139 		if (pn->pn_va.va_mtime.tv_sec != PUFFS_VNOVAL)
    140 			kludgeva.va_mtime.tv_sec = pn->pn_va.va_mtime.tv_sec;
    141 		else
    142 			kludgeva.va_mtime.tv_sec = va->va_atime.tv_sec;
    143 	}
    144 	if (va->va_mtime.tv_sec != PUFFS_VNOVAL
    145 	    && va->va_atime.tv_sec == PUFFS_VNOVAL) {
    146 		if (pn->pn_va.va_atime.tv_sec != PUFFS_VNOVAL)
    147 			kludgeva.va_atime.tv_sec = pn->pn_va.va_atime.tv_sec;
    148 		else
    149 			kludgeva.va_atime.tv_sec = va->va_mtime.tv_sec;
    150 	}
    151 
    152 	psbuf_put_vattr(pb, &kludgeva);
    153 	GETRESPONSE(pb);
    154 
    155 	rv = psbuf_expect_status(pb);
    156 	if (rv == 0)
    157 		puffs_setvattr(&pn->pn_va, &kludgeva);
    158 
    159  out:
    160 	PSSHFSRETURN(rv);
    161 }
    162 
    163 int
    164 psshfs_node_create(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
    165 	const struct puffs_cn *pcn, const struct vattr *va)
    166 {
    167 	PSSHFSAUTOVAR(pcc);
    168 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    169 	struct puffs_node *pn = opc;
    170 	struct puffs_node *pn_new;
    171 	char *fhand = NULL;
    172 	uint32_t fhandlen;
    173 
    174 	pn_new = allocnode(pu, pn, pcn->pcn_name, va);
    175 	if (!pn_new) {
    176 		rv = ENOMEM;
    177 		goto out;
    178 	}
    179 
    180 	psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PCNPATH(pcn));
    181 	psbuf_put_4(pb, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
    182 	psbuf_put_vattr(pb, va);
    183 	GETRESPONSE(pb);
    184 
    185 	rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
    186 	if (rv == 0)
    187 		puffs_newinfo_setcookie(pni, pn_new);
    188 	else
    189 		goto out;
    190 
    191 	reqid = NEXTREQ(pctx);
    192 	psbuf_recycleout(pb);
    193 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
    194 	JUSTSEND(pb);
    195 	free(fhand);
    196 	return 0;
    197 
    198  out:
    199 	free(fhand);
    200 	PSSHFSRETURN(rv);
    201 }
    202 
    203 int
    204 psshfs_node_mmap(struct puffs_cc* pcc, void *opc, vm_prot_t prot,
    205 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    206 {
    207 	struct puffs_node *pn = opc;
    208 	struct psshfs_node *psn = pn->pn_data;
    209 
    210 	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE))
    211 		psn->stat |= PSN_READMAP;
    212 	if (prot & VM_PROT_WRITE)
    213 		psn->stat |= PSN_WRITEMAP;
    214 
    215 	return 0;
    216 }
    217 
    218 int
    219 psshfs_node_open(struct puffs_cc *pcc, void *opc, int mode,
    220 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    221 {
    222 	PSSHFSAUTOVAR(pcc);
    223 	struct vattr va;
    224 	struct puffs_node *pn = opc;
    225 	struct psshfs_node *psn = pn->pn_data;
    226 
    227 	if (pn->pn_va.va_type == VDIR)
    228 		goto out;
    229 
    230 	puffs_setback(pcc, PUFFS_SETBACK_INACT_N1);
    231 	puffs_vattr_null(&va);
    232 	if (mode & FREAD && psn->fhand_r == NULL) {
    233 		psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
    234 		psbuf_put_4(pb, SSH_FXF_READ);
    235 		psbuf_put_vattr(pb, &va);
    236 		GETRESPONSE(pb);
    237 
    238 		rv = psbuf_expect_handle(pb, &psn->fhand_r, &psn->fhand_r_len);
    239 		if (rv)
    240 			goto out;
    241 		psbuf_recycleout(pb);
    242 	}
    243 	if (mode & FWRITE && psn->fhand_w == NULL) {
    244 		psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
    245 		psbuf_put_4(pb, SSH_FXF_WRITE);
    246 		psbuf_put_vattr(pb, &va);
    247 		GETRESPONSE(pb);
    248 
    249 		rv = psbuf_expect_handle(pb, &psn->fhand_w, &psn->fhand_w_len);
    250 		if (rv)
    251 			goto out;
    252 	}
    253 
    254  out:
    255 	if (rv == 0)
    256 		psn->opencount++;
    257 
    258 	PSSHFSRETURN(rv);
    259 }
    260 
    261 static void
    262 closehandles(struct puffs_cc *pcc, struct psshfs_node *psn)
    263 {
    264 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    265 	struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
    266 	struct puffs_framebuf *pb1, *pb2;
    267 	uint32_t reqid = NEXTREQ(pctx);
    268 	int rv; /* macro magic */
    269 
    270 	if ((psn->stat & PSN_READMAP) == 0 && psn->fhand_r) {
    271 		pb1 = psbuf_makeout();
    272 		psbuf_req_data(pb1, SSH_FXP_CLOSE, reqid,
    273 		    psn->fhand_r, psn->fhand_r_len);
    274 		JUSTSEND(pb1);
    275 		free(psn->fhand_r);
    276 		psn->fhand_r = NULL;
    277 	}
    278 	if ((psn->stat & PSN_WRITEMAP) == 0 && psn->fhand_w) {
    279 		pb2 = psbuf_makeout();
    280 		psbuf_req_data(pb2, SSH_FXP_CLOSE, reqid,
    281 		    psn->fhand_w, psn->fhand_w_len);
    282 		JUSTSEND(pb2);
    283 		free(psn->fhand_w);
    284 		psn->fhand_w = NULL;
    285 	}
    286 
    287  out:
    288 	return;
    289 }
    290 
    291 int
    292 psshfs_node_close(struct puffs_cc *pcc, void *opc, int flags,
    293 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    294 {
    295 	struct puffs_node *pn = opc;
    296 	struct psshfs_node *psn = pn->pn_data;
    297 
    298 	if (psn->opencount > 0)
    299 		psn->opencount--;
    300 
    301 	if (psn->opencount)
    302 		closehandles(pcc, psn);
    303 
    304 	return 0;
    305 }
    306 
    307 int
    308 psshfs_node_inactive(struct puffs_cc *pcc, void *opc,
    309 	const struct puffs_cid *pcid)
    310 {
    311 	struct puffs_node *pn = opc;
    312 	struct psshfs_node *psn = pn->pn_data;
    313 
    314 	assert(psn->opencount == 0);
    315 	psn->stat &= ~(PSN_READMAP | PSN_WRITEMAP);
    316 	closehandles(pcc, psn);
    317 
    318 	return 0;
    319 }
    320 
    321 /*
    322  * XXXX: I'm not really sure I'll export this symbol eventually,
    323  * so fix this a bit in the future.
    324  */
    325 void puffs_goto(struct puffs_cc *);
    326 
    327 int
    328 psshfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    329 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    330 	int *eofflag, off_t *cookies, size_t *ncookies)
    331 {
    332 	struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
    333 	struct puffs_node *pn = opc;
    334 	struct psshfs_node *psn = pn->pn_data;
    335 	struct psshfs_dir *pd;
    336 	int i, rv, set_readdir;
    337 
    338 	if (psn->stat & PSN_READDIR) {
    339 		struct psshfs_dirwait dw;
    340 
    341 		set_readdir = 0;
    342 		dw.dw_cc = pcc;
    343 		LIST_INSERT_HEAD(&psn->dw, &dw, dw_entries);
    344 		puffs_cc_yield(pcc);
    345 		LIST_REMOVE(&dw, dw_entries);
    346 	} else {
    347 		psn->stat |= PSN_READDIR;
    348 		set_readdir = 1;
    349 	}
    350 
    351 	*ncookies = 0;
    352 	rv = sftp_readdir(pcc, pctx, pn);
    353 	if (rv)
    354 		goto out;
    355 
    356 	/* find next dirent */
    357 	for (i = *readoff;;i++) {
    358 		if (i >= psn->dentnext)
    359 			goto out;
    360 		pd = &psn->dir[i];
    361 		if (pd->valid)
    362 			break;
    363 	}
    364 
    365 	for (;;) {
    366 		*readoff = i;
    367 		if (!puffs_nextdent(&dent, pd->entryname,
    368 		    pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen)) {
    369 			rv = 0;
    370 			goto out;
    371 		}
    372 
    373 		/* find next entry, store possible nfs key */
    374 		do {
    375 			if (++i >= psn->dentnext)
    376 				goto out;
    377 			pd = &psn->dir[i];
    378 		} while (pd->valid == 0);
    379 		PUFFS_STORE_DCOOKIE(cookies, ncookies, (off_t)i);
    380 	}
    381 
    382  out:
    383 	if (rv == 0) {
    384 		if (i >= psn->dentnext)
    385 			*eofflag = 1;
    386 
    387 		*readoff = i;
    388 	}
    389 
    390 	if (set_readdir) {
    391 		struct psshfs_dirwait *dw;
    392 
    393 		while ((dw = LIST_FIRST(&psn->dw)) != NULL)
    394 			puffs_goto(dw->dw_cc);
    395 
    396 		psn->stat &= ~PSN_READDIR;
    397 	}
    398 
    399 	return rv;
    400 }
    401 
    402 int
    403 psshfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    404 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    405 	int ioflag)
    406 {
    407 	PSSHFSAUTOVAR(pcc);
    408 	struct puffs_node *pn = opc;
    409 	struct psshfs_node *psn = pn->pn_data;
    410 	uint32_t readlen;
    411 
    412 	if (pn->pn_va.va_type == VDIR) {
    413 		rv = EISDIR;
    414 		goto out;
    415 	}
    416 
    417 	readlen = *resid;
    418 	psbuf_req_data(pb, SSH_FXP_READ, reqid, psn->fhand_r, psn->fhand_r_len);
    419 	psbuf_put_8(pb, offset);
    420 	psbuf_put_4(pb, readlen);
    421 	GETRESPONSE(pb);
    422 
    423 	rv = psbuf_do_data(pb, buf, &readlen);
    424 	if (rv == 0)
    425 		*resid -= readlen;
    426 
    427  out:
    428 	PSSHFSRETURN(rv);
    429 }
    430 
    431 /* XXX: we should getattr for size */
    432 int
    433 psshfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    434 	off_t offset, size_t *resid, const struct puffs_cred *cred,
    435 	int ioflag)
    436 {
    437 	PSSHFSAUTOVAR(pcc);
    438 	struct puffs_node *pn = opc;
    439 	struct psshfs_node *psn = pn->pn_data;
    440 	uint32_t writelen;
    441 
    442 	if (pn->pn_va.va_type == VDIR) {
    443 		rv = EISDIR;
    444 		goto out;
    445 	}
    446 
    447 	writelen = *resid;
    448 	psbuf_req_data(pb, SSH_FXP_WRITE, reqid, psn->fhand_w,psn->fhand_w_len);
    449 	psbuf_put_8(pb, offset);
    450 	psbuf_put_data(pb, buf, writelen);
    451 	GETRESPONSE(pb);
    452 
    453 	rv = psbuf_expect_status(pb);
    454 	if (rv == 0)
    455 		*resid = 0;
    456 
    457 	if (pn->pn_va.va_size < offset + writelen)
    458 		pn->pn_va.va_size = offset + writelen;
    459 
    460  out:
    461 	PSSHFSRETURN(rv);
    462 }
    463 
    464 int
    465 psshfs_node_readlink(struct puffs_cc *pcc, void *opc,
    466 	const struct puffs_cred *cred, char *linkvalue, size_t *linklen)
    467 {
    468 	PSSHFSAUTOVAR(pcc);
    469 	struct puffs_node *pn = opc;
    470 	char *linktmp = NULL;
    471 	uint32_t count;
    472 
    473 	if (pctx->protover < 3) {
    474 		rv = EOPNOTSUPP;
    475 		goto out;
    476 	}
    477 
    478 	psbuf_req_str(pb, SSH_FXP_READLINK, reqid, PNPATH(pn));
    479 	GETRESPONSE(pb);
    480 
    481 	rv = psbuf_expect_name(pb, &count);
    482 	if (rv)
    483 		goto out;
    484 	if (count != 1) {
    485 		rv = EPROTO;
    486 		goto out;
    487 	}
    488 
    489 	rv = psbuf_get_str(pb, &linktmp, (uint32_t *)linklen);
    490 	if (rv)
    491 		goto out;
    492 	(void) memcpy(linkvalue, linktmp, *linklen);
    493 
    494  out:
    495 	free(linktmp);
    496 	PSSHFSRETURN(rv);
    497 }
    498 
    499 static int
    500 doremove(struct puffs_cc *pcc, struct puffs_node *pn_dir,
    501 	struct puffs_node *pn, const char *name)
    502 {
    503 	PSSHFSAUTOVAR(pcc);
    504 	int op;
    505 
    506 	if (pn->pn_va.va_type == VDIR)
    507 		op = SSH_FXP_RMDIR;
    508 	else
    509 		op = SSH_FXP_REMOVE;
    510 
    511 	psbuf_req_str(pb, op, reqid, PNPATH(pn));
    512 	GETRESPONSE(pb);
    513 
    514 	rv = psbuf_expect_status(pb);
    515 	if (rv == 0)
    516 		nukenode(pn, name, 0);
    517 
    518  out:
    519 	PSSHFSRETURN(rv);
    520 }
    521 
    522 int
    523 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    524 	const struct puffs_cn *pcn)
    525 {
    526 	struct puffs_node *pn_targ = targ;
    527 	int rv;
    528 
    529 	assert(pn_targ->pn_va.va_type != VDIR);
    530 
    531 	rv = doremove(pcc, opc, targ, pcn->pcn_name);
    532 	if (rv == 0)
    533 		puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
    534 
    535 	return rv;
    536 }
    537 
    538 int
    539 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    540 	const struct puffs_cn *pcn)
    541 {
    542 	struct puffs_node *pn_targ = targ;
    543 	int rv;
    544 
    545 	assert(pn_targ->pn_va.va_type == VDIR);
    546 
    547 	rv = doremove(pcc, opc, targ, pcn->pcn_name);
    548 	if (rv == 0)
    549 		puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
    550 
    551 	return rv;
    552 }
    553 
    554 int
    555 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc,  struct puffs_newinfo *pni,
    556 	const struct puffs_cn *pcn, const struct vattr *va)
    557 {
    558 	PSSHFSAUTOVAR(pcc);
    559 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    560 	struct puffs_node *pn = opc;
    561 	struct puffs_node *pn_new;
    562 
    563 	pn_new = allocnode(pu, pn, pcn->pcn_name, va);
    564 	if (!pn_new) {
    565 		rv = ENOMEM;
    566 		goto out;
    567 	}
    568 
    569 	psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
    570 	psbuf_put_vattr(pb, va);
    571 	GETRESPONSE(pb);
    572 
    573 	rv = psbuf_expect_status(pb);
    574 
    575 	if (rv == 0)
    576 		puffs_newinfo_setcookie(pni, pn_new);
    577 	else
    578 		nukenode(pn_new, pcn->pcn_name, 1);
    579 
    580  out:
    581 	PSSHFSRETURN(rv);
    582 }
    583 
    584 int
    585 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
    586 	const struct puffs_cn *pcn, const struct vattr *va,
    587 	const char *link_target)
    588 {
    589 	PSSHFSAUTOVAR(pcc);
    590 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    591 	struct puffs_node *pn = opc;
    592 	struct puffs_node *pn_new;
    593 
    594 	if (pctx->protover < 3) {
    595 		rv = EOPNOTSUPP;
    596 		goto out;
    597 	}
    598 
    599 	pn_new = allocnode(pu, pn, pcn->pcn_name, va);
    600 	if (!pn_new) {
    601 		rv = ENOMEM;
    602 		goto out;
    603 	}
    604 
    605 	/*
    606 	 * XXX: ietf says: source, target.  openssh says: ietf who?
    607 	 * Let's go with openssh and build quirk tables later if we care
    608 	 */
    609 	psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
    610 	psbuf_put_str(pb, PCNPATH(pcn));
    611 	GETRESPONSE(pb);
    612 
    613 	rv = psbuf_expect_status(pb);
    614 	if (rv == 0)
    615 		puffs_newinfo_setcookie(pni, pn_new);
    616 	else
    617 		nukenode(pn_new, pcn->pcn_name, 1);
    618 
    619  out:
    620 	PSSHFSRETURN(rv);
    621 }
    622 
    623 int
    624 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    625 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    626 	const struct puffs_cn *pcn_targ)
    627 {
    628 	PSSHFSAUTOVAR(pcc);
    629 	struct puffs_node *pn_sf = src;
    630 	struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
    631 	struct psshfs_node *psn_targdir = pn_td->pn_data;
    632 
    633 	if (pctx->protover < 2) {
    634 		rv = EOPNOTSUPP;
    635 		goto out;
    636 	}
    637 
    638 	if (pn_tf) {
    639 		rv = doremove(pcc, targ_dir, pn_tf, pcn_targ->pcn_name);
    640 		if (rv)
    641 			goto out;
    642 	}
    643 
    644 	psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
    645 	psbuf_put_str(pb, PCNPATH(pcn_targ));
    646 	GETRESPONSE(pb);
    647 
    648 	rv = psbuf_expect_status(pb);
    649 	if (rv == 0) {
    650 		struct psshfs_dir *pd;
    651 
    652 		/*
    653 		 * XXX: interfaces didn't quite work with rename..
    654 		 * the song remains the same.  go figure .. ;)
    655 		 */
    656 		nukenode(pn_sf, pcn_src->pcn_name, 0);
    657 		pd = direnter(pn_td, pcn_targ->pcn_name);
    658 		pd->entry = pn_sf;
    659 		puffs_setvattr(&pd->va, &pn_sf->pn_va);
    660 
    661 		if (opc != targ_dir) {
    662 			psn_targdir->childcount++;
    663 			if (pn_sf->pn_va.va_type == VDIR)
    664 				pn_td->pn_va.va_nlink++;
    665 		}
    666 	}
    667 
    668  out:
    669 	PSSHFSRETURN(rv);
    670 }
    671 
    672 /*
    673  * So this file system happened to be written in such a way that
    674  * lookup for ".." is hard if we lose the in-memory node.  We'd
    675  * need to recreate the entire directory structure from the root
    676  * node up to the ".." node we're looking up.
    677  *
    678  * And since our entire fs structure is purely fictional (i.e. it's
    679  * only in-memory, not fetchable from the server), the easiest way
    680  * to deal with it is to not allow nodes with children to be
    681  * reclaimed.
    682  *
    683  * If a node with children is being attempted to be reclaimed, we
    684  * just mark it "reclaimed" but leave it as is until all its children
    685  * have been reclaimed.  If a lookup for that node is done meanwhile,
    686  * it will be found by lookup() and we just remove the "reclaimed"
    687  * bit.
    688  */
    689 int
    690 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc,
    691 	const struct puffs_cid *pcid)
    692 {
    693 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    694 	struct puffs_node *pn = opc, *pn_next, *pn_root;
    695 	struct psshfs_node *psn = pn->pn_data;
    696 
    697 	/*
    698 	 * don't reclaim if we have file handle issued, otherwise
    699 	 * we can't do fhtonode
    700 	 */
    701 	if (psn->stat & PSN_HASFH)
    702 		return 0;
    703 
    704 	psn->stat |= PSN_RECLAIMED;
    705 	pn_root = puffs_getroot(pu);
    706 	for (; pn != pn_root; pn = pn_next) {
    707 		psn = pn->pn_data;
    708 		if ((psn->stat & PSN_RECLAIMED) == 0 || psn->childcount != 0)
    709 			break;
    710 
    711 		pn_next = psn->parent;
    712 		doreclaim(pn);
    713 	}
    714 
    715 	return 0;
    716 }
    717