Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.23
      1  1.23  pooka /*      $NetBSD: subr.c,v 1.23 2007/05/20 20:27:04 pooka Exp $        */
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  * 3. The name of the company nor the name of the author may be used to
     15   1.1  pooka  *    endorse or promote products derived from this software without specific
     16   1.1  pooka  *    prior written permission.
     17   1.1  pooka  *
     18   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1  pooka  * SUCH DAMAGE.
     29   1.1  pooka  */
     30   1.1  pooka 
     31   1.1  pooka #include <sys/cdefs.h>
     32   1.1  pooka #ifndef lint
     33  1.23  pooka __RCSID("$NetBSD: subr.c,v 1.23 2007/05/20 20:27:04 pooka Exp $");
     34   1.1  pooka #endif /* !lint */
     35   1.1  pooka 
     36   1.1  pooka #include <assert.h>
     37   1.1  pooka #include <errno.h>
     38   1.1  pooka #include <puffs.h>
     39   1.1  pooka #include <stdlib.h>
     40   1.1  pooka #include <util.h>
     41   1.1  pooka 
     42   1.1  pooka #include "psshfs.h"
     43   1.1  pooka #include "sftp_proto.h"
     44   1.1  pooka 
     45   1.1  pooka static void
     46   1.1  pooka freedircache(struct psshfs_dir *base, size_t count)
     47   1.1  pooka {
     48   1.1  pooka 	int i;
     49   1.1  pooka 
     50   1.1  pooka 	for (i = 0; i < count; i++) {
     51   1.1  pooka 		free(base[i].entryname);
     52   1.1  pooka 		base[i].entryname = NULL;
     53   1.1  pooka 	}
     54   1.1  pooka 
     55   1.1  pooka 	free(base);
     56   1.1  pooka }
     57   1.1  pooka 
     58   1.1  pooka #define ENTRYCHUNK 16
     59   1.1  pooka static void
     60   1.1  pooka allocdirs(struct psshfs_node *psn)
     61   1.1  pooka {
     62   1.1  pooka 	size_t oldtot = psn->denttot;
     63   1.1  pooka 
     64   1.1  pooka 	psn->denttot += ENTRYCHUNK;
     65   1.1  pooka 	psn->dir = erealloc(psn->dir,
     66   1.1  pooka 	    psn->denttot * sizeof(struct psshfs_dir));
     67   1.1  pooka 	memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
     68  1.21  pooka 
     69  1.21  pooka 	psn->da = erealloc(psn->da, psn->denttot * sizeof(struct delayattr));
     70   1.1  pooka }
     71   1.1  pooka 
     72   1.1  pooka struct psshfs_dir *
     73   1.9  pooka lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
     74   1.1  pooka {
     75   1.9  pooka 	struct psshfs_dir *test;
     76   1.1  pooka 	int i;
     77   1.1  pooka 
     78   1.1  pooka 	for (i = 0; i < ndir; i++) {
     79   1.9  pooka 		test = &bdir[i];
     80   1.9  pooka 		if (test->valid != 1)
     81   1.1  pooka 			continue;
     82   1.9  pooka 		if (strcmp(test->entryname, name) == 0)
     83   1.9  pooka 			return test;
     84   1.9  pooka 	}
     85   1.9  pooka 
     86   1.9  pooka 	return NULL;
     87   1.9  pooka }
     88   1.9  pooka 
     89   1.9  pooka static struct psshfs_dir *
     90   1.9  pooka lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
     91   1.9  pooka {
     92   1.9  pooka 	struct psshfs_dir *test;
     93   1.9  pooka 	int i;
     94   1.9  pooka 
     95   1.9  pooka 	for (i = 0; i < ndir; i++) {
     96   1.9  pooka 		test = &bdir[i];
     97   1.9  pooka 		if (test->valid != 1)
     98   1.9  pooka 			continue;
     99   1.9  pooka 		if (test->entry == entry)
    100   1.9  pooka 			return test;
    101   1.1  pooka 	}
    102   1.1  pooka 
    103   1.1  pooka 	return NULL;
    104   1.1  pooka }
    105   1.1  pooka 
    106   1.8  pooka struct readdirattr {
    107   1.8  pooka 	struct psshfs_node *psn;
    108   1.8  pooka 	int idx;
    109   1.8  pooka 	char entryname[MAXPATHLEN+1];
    110   1.8  pooka };
    111   1.8  pooka 
    112   1.8  pooka static void
    113  1.14  pooka readdir_getattr_resp(struct puffs_usermount *pu,
    114  1.18  pooka 	struct puffs_framebuf *pb, void *arg, int error)
    115   1.8  pooka {
    116   1.8  pooka 	struct readdirattr *rda = arg;
    117   1.8  pooka 	struct psshfs_node *psn = rda->psn;
    118  1.23  pooka 	struct psshfs_node *psn_targ = NULL;
    119  1.23  pooka 	struct psshfs_dir *pdir = NULL;
    120   1.8  pooka 	struct vattr va;
    121   1.8  pooka 
    122  1.23  pooka 	/* XXX: this is not enough */
    123  1.23  pooka 	if (psn->stat & PSN_RECLAIMED)
    124  1.23  pooka 		goto out;
    125  1.23  pooka 
    126  1.18  pooka 	if (error)
    127  1.18  pooka 		goto out;
    128  1.18  pooka 
    129   1.8  pooka 	pdir = lookup(psn->dir, psn->denttot, rda->entryname);
    130   1.8  pooka 	if (!pdir)
    131   1.8  pooka 		goto out;
    132   1.8  pooka 
    133   1.8  pooka 	if (psbuf_expect_attrs(pb, &va))
    134   1.8  pooka 		goto out;
    135   1.8  pooka 
    136   1.8  pooka 	if (pdir->entry) {
    137   1.8  pooka 		psn_targ = pdir->entry->pn_data;
    138   1.8  pooka 
    139   1.8  pooka 		puffs_setvattr(&pdir->entry->pn_va, &va);
    140   1.8  pooka 		psn_targ->attrread = time(NULL);
    141   1.8  pooka 	} else {
    142   1.8  pooka 		puffs_setvattr(&pdir->va, &va);
    143   1.8  pooka 		pdir->attrread = time(NULL);
    144   1.8  pooka 	}
    145   1.8  pooka 
    146   1.8  pooka  out:
    147  1.22  pooka 	if (psn_targ) {
    148  1.22  pooka 		psn_targ->getattr_pb = NULL;
    149  1.22  pooka 		assert(pdir->getattr_pb == NULL);
    150  1.23  pooka 	} else if (pdir) {
    151  1.22  pooka 		pdir->getattr_pb = NULL;
    152  1.22  pooka 	}
    153  1.22  pooka 
    154   1.8  pooka 	free(rda);
    155  1.14  pooka 	puffs_framebuf_destroy(pb);
    156   1.8  pooka }
    157   1.8  pooka 
    158  1.21  pooka static void
    159  1.14  pooka readdir_getattr(struct puffs_usermount *pu, struct psshfs_node *psn,
    160   1.8  pooka 	const char *basepath, int idx)
    161   1.8  pooka {
    162   1.8  pooka 	char path[MAXPATHLEN+1];
    163  1.14  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    164   1.8  pooka 	struct psshfs_dir *pdir = psn->dir;
    165  1.14  pooka 	struct puffs_framebuf *pb;
    166   1.8  pooka 	struct readdirattr *rda;
    167   1.8  pooka 	const char *entryname = pdir[idx].entryname;
    168   1.8  pooka 	uint32_t reqid = NEXTREQ(pctx);
    169   1.8  pooka 
    170   1.8  pooka 	rda = emalloc(sizeof(struct readdirattr));
    171   1.8  pooka 	rda->psn = psn;
    172   1.8  pooka 	rda->idx = idx;
    173   1.8  pooka 	strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
    174   1.8  pooka 
    175   1.8  pooka 	strcpy(path, basepath);
    176   1.8  pooka 	strcat(path, "/");
    177   1.8  pooka 	strlcat(path, entryname, sizeof(path));
    178   1.8  pooka 
    179  1.14  pooka 	pb = psbuf_makeout();
    180   1.8  pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    181  1.21  pooka 	psn->da[psn->nextda].pufbuf = pb;
    182  1.21  pooka 	psn->da[psn->nextda].rda = rda;
    183  1.21  pooka 	psn->nextda++;
    184  1.21  pooka }
    185  1.21  pooka 
    186  1.21  pooka static void
    187  1.21  pooka readdir_getattr_send(struct puffs_usermount *pu, struct psshfs_node *psn)
    188  1.21  pooka {
    189  1.21  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    190  1.22  pooka 	struct psshfs_dir *pdir = psn->dir;
    191  1.22  pooka 	struct psshfs_node *psn_targ;
    192  1.22  pooka 	struct readdirattr *rda;
    193  1.21  pooka 	size_t i;
    194  1.21  pooka 	int rv = 0;
    195  1.21  pooka 
    196  1.22  pooka 	for (i = 0; i < psn->nextda; i++) {
    197  1.22  pooka 		rda = psn->da[i].rda;
    198  1.22  pooka 		if (pdir[rda->idx].entry) {
    199  1.22  pooka 			psn_targ = pdir[rda->idx].entry->pn_data;
    200  1.22  pooka 			psn_targ->getattr_pb = psn->da[i].pufbuf;
    201  1.22  pooka 		} else {
    202  1.22  pooka 			pdir[rda->idx].getattr_pb = psn->da[i].pufbuf;
    203  1.22  pooka 		}
    204  1.22  pooka 		SENDCB(psn->da[i].pufbuf, readdir_getattr_resp, rda);
    205  1.22  pooka 	}
    206  1.16  pooka 
    207  1.16  pooka  out:
    208  1.21  pooka 	return;
    209   1.8  pooka }
    210   1.8  pooka 
    211   1.1  pooka int
    212  1.12  pooka getpathattr(struct puffs_cc *pcc, const char *path, struct vattr *vap)
    213  1.12  pooka {
    214  1.12  pooka 	PSSHFSAUTOVAR(pcc);
    215  1.12  pooka 
    216  1.12  pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    217  1.15  pooka 	GETRESPONSE(pb);
    218  1.12  pooka 
    219  1.12  pooka 	rv = psbuf_expect_attrs(pb, vap);
    220  1.12  pooka 
    221  1.16  pooka  out:
    222  1.12  pooka 	PSSHFSRETURN(rv);
    223  1.12  pooka }
    224  1.12  pooka 
    225  1.12  pooka int
    226  1.12  pooka getnodeattr(struct puffs_cc *pcc, struct puffs_node *pn)
    227  1.12  pooka {
    228  1.12  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    229  1.12  pooka 	struct psshfs_node *psn = pn->pn_data;
    230  1.12  pooka 	struct vattr va;
    231  1.23  pooka 	int rv, dohardway;
    232  1.12  pooka 
    233  1.12  pooka 	if ((time(NULL) - psn->attrread) >= PSSHFS_REFRESHIVAL) {
    234  1.23  pooka 		dohardway = 1;
    235  1.22  pooka 		if (psn->getattr_pb) {
    236  1.22  pooka 			rv=puffs_framev_framebuf_ccpromote(psn->getattr_pb,pcc);
    237  1.23  pooka 			if (rv == 0) {
    238  1.23  pooka 				rv = psbuf_expect_attrs(psn->getattr_pb, &va);
    239  1.23  pooka 				puffs_framebuf_destroy(psn->getattr_pb);
    240  1.23  pooka 				psn->getattr_pb = NULL;
    241  1.23  pooka 				if (rv == 0)
    242  1.23  pooka 					dohardway = 0;
    243  1.23  pooka 			}
    244  1.23  pooka 		}
    245  1.22  pooka 
    246  1.23  pooka 		if (dohardway) {
    247  1.22  pooka 			rv = getpathattr(pcc, PNPATH(pn), &va);
    248  1.22  pooka 			if (rv)
    249  1.22  pooka 				return rv;
    250  1.22  pooka 		}
    251  1.12  pooka 
    252  1.12  pooka 		/*
    253  1.22  pooka 		 * Check if the file was modified from below us.
    254  1.22  pooka 		 * If so, invalidate page cache.  This is the only
    255  1.22  pooka 		 * sensible place we can do this in.
    256  1.12  pooka 		 */
    257  1.12  pooka 		if (psn->attrread)
    258  1.12  pooka 			if (pn->pn_va.va_mtime.tv_sec != va.va_mtime.tv_sec)
    259  1.12  pooka 				puffs_inval_pagecache_node(pu, pn);
    260  1.12  pooka 
    261  1.12  pooka 		puffs_setvattr(&pn->pn_va, &va);
    262  1.12  pooka 		psn->attrread = time(NULL);
    263  1.12  pooka 	}
    264  1.12  pooka 
    265  1.12  pooka 	return 0;
    266  1.12  pooka }
    267  1.12  pooka 
    268  1.12  pooka int
    269   1.1  pooka sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
    270   1.1  pooka 	struct puffs_node *pn)
    271   1.1  pooka {
    272  1.15  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    273   1.1  pooka 	struct psshfs_node *psn = pn->pn_data;
    274   1.1  pooka 	struct psshfs_dir *olddir, *testd;
    275  1.14  pooka 	struct puffs_framebuf *pb;
    276   1.1  pooka 	uint32_t reqid = NEXTREQ(pctx);
    277  1.10  pooka 	uint32_t count, dhandlen;
    278   1.1  pooka 	char *dhand = NULL;
    279  1.10  pooka 	size_t nent;
    280   1.1  pooka 	char *longname;
    281   1.1  pooka 	int idx, rv;
    282   1.1  pooka 
    283   1.1  pooka 	assert(pn->pn_va.va_type == VDIR);
    284  1.16  pooka 	idx = 0;
    285  1.16  pooka 	olddir = psn->dir;
    286  1.16  pooka 	nent = psn->dentnext;
    287   1.1  pooka 
    288   1.1  pooka 	if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
    289   1.1  pooka 		return 0;
    290   1.1  pooka 
    291   1.5  pooka 	puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
    292   1.5  pooka 
    293  1.14  pooka 	pb = psbuf_makeout();
    294   1.7  pooka 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
    295  1.15  pooka 	GETRESPONSE(pb);
    296   1.1  pooka 
    297   1.1  pooka 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    298   1.1  pooka 	if (rv)
    299   1.1  pooka 		goto wayout;
    300   1.1  pooka 
    301   1.1  pooka 	/*
    302   1.1  pooka 	 * Well, the following is O(n^2), so feel free to improve if it
    303   1.1  pooka 	 * gets too taxing on your system.
    304   1.1  pooka 	 */
    305   1.1  pooka 
    306   1.8  pooka 	/*
    307   1.8  pooka 	 * note: for the "getattr in batch" to work, this must be before
    308   1.8  pooka 	 * the attribute-getting.  Otherwise times for first entries in
    309   1.8  pooka 	 * large directories might expire before the directory itself and
    310   1.8  pooka 	 * result in one-by-one attribute fetching.
    311   1.8  pooka 	 */
    312   1.8  pooka 	psn->dentread = time(NULL);
    313   1.8  pooka 
    314   1.1  pooka 	psn->dentnext = 0;
    315   1.1  pooka 	psn->denttot = 0;
    316   1.1  pooka 	psn->dir = NULL;
    317  1.21  pooka 	psn->nextda = 0;
    318   1.1  pooka 
    319   1.1  pooka 	for (;;) {
    320   1.1  pooka 		reqid = NEXTREQ(pctx);
    321  1.14  pooka 		psbuf_recycleout(pb);
    322   1.1  pooka 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    323  1.15  pooka 		GETRESPONSE(pb);
    324   1.1  pooka 
    325   1.1  pooka 		/* check for EOF */
    326  1.14  pooka 		if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
    327   1.1  pooka 			rv = psbuf_expect_status(pb);
    328   1.1  pooka 			goto out;
    329   1.1  pooka 		}
    330   1.1  pooka 		rv = psbuf_expect_name(pb, &count);
    331   1.1  pooka 		if (rv)
    332   1.1  pooka 			goto out;
    333   1.1  pooka 
    334   1.1  pooka 		for (; count--; idx++) {
    335   1.1  pooka 			if (idx == psn->denttot)
    336   1.1  pooka 				allocdirs(psn);
    337  1.14  pooka 			if ((rv = psbuf_get_str(pb,
    338  1.14  pooka 			    &psn->dir[idx].entryname, NULL)))
    339   1.1  pooka 				goto out;
    340  1.14  pooka 			if ((rv = psbuf_get_str(pb, &longname, NULL)))
    341   1.1  pooka 				goto out;
    342  1.17  pooka 			if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va))) {
    343  1.17  pooka 				free(longname);
    344   1.1  pooka 				goto out;
    345  1.17  pooka 			}
    346   1.1  pooka 			if (sscanf(longname, "%*s%d",
    347   1.1  pooka 			    &psn->dir[idx].va.va_nlink) != 1) {
    348   1.1  pooka 				rv = EPROTO;
    349   1.1  pooka 				goto out;
    350   1.1  pooka 			}
    351   1.1  pooka 			free(longname);
    352   1.1  pooka 
    353   1.1  pooka 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    354   1.1  pooka 			if (testd) {
    355   1.1  pooka 				psn->dir[idx].entry = testd->entry;
    356   1.8  pooka 				psn->dir[idx].va = testd->va;
    357   1.1  pooka 			} else {
    358   1.1  pooka 				psn->dir[idx].entry = NULL;
    359   1.1  pooka 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    360   1.1  pooka 			}
    361   1.8  pooka 			/*
    362   1.8  pooka 			 * XXX: there's a dangling pointer race here if
    363   1.8  pooka 			 * the server responds to our queries out-of-order.
    364   1.8  pooka 			 * fixxxme some day
    365   1.8  pooka 			 */
    366  1.21  pooka 			readdir_getattr(puffs_cc_getusermount(pcc),
    367  1.14  pooka 			    psn, PNPATH(pn), idx);
    368  1.22  pooka 
    369   1.1  pooka 			psn->dir[idx].valid = 1;
    370   1.1  pooka 		}
    371   1.1  pooka 	}
    372   1.1  pooka 
    373   1.1  pooka  out:
    374  1.21  pooka 	/* fire off getattr requests */
    375  1.21  pooka 	readdir_getattr_send(pu, psn);
    376  1.21  pooka 
    377   1.1  pooka 	/* XXX: rv */
    378   1.1  pooka 	psn->dentnext = idx;
    379   1.1  pooka 	freedircache(olddir, nent);
    380   1.1  pooka 
    381   1.1  pooka 	reqid = NEXTREQ(pctx);
    382  1.14  pooka 	psbuf_recycleout(pb);
    383   1.1  pooka 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    384  1.15  pooka 	JUSTSEND(pb);
    385  1.11  pooka 	free(dhand);
    386  1.21  pooka 
    387  1.12  pooka 	return rv;
    388   1.1  pooka 
    389   1.1  pooka  wayout:
    390   1.1  pooka 	free(dhand);
    391  1.11  pooka 	PSSHFSRETURN(rv);
    392   1.1  pooka }
    393   1.1  pooka 
    394   1.1  pooka struct puffs_node *
    395   1.1  pooka makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    396   1.1  pooka 	struct psshfs_dir *pd, const struct vattr *vap)
    397   1.1  pooka {
    398   1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    399   1.1  pooka 	struct psshfs_node *psn;
    400   1.1  pooka 	struct puffs_node *pn;
    401   1.1  pooka 
    402   1.1  pooka 	psn = emalloc(sizeof(struct psshfs_node));
    403   1.1  pooka 	memset(psn, 0, sizeof(struct psshfs_node));
    404   1.1  pooka 
    405   1.1  pooka 	pn = puffs_pn_new(pu, psn);
    406   1.1  pooka 	if (!pn) {
    407   1.1  pooka 		free(psn);
    408   1.1  pooka 		return NULL;
    409   1.1  pooka 	}
    410   1.1  pooka 	puffs_setvattr(&pn->pn_va, &pd->va);
    411   1.8  pooka 	psn->attrread = pd->attrread;
    412   1.1  pooka 	puffs_setvattr(&pn->pn_va, vap);
    413   1.1  pooka 
    414   1.1  pooka 	pd->entry = pn;
    415   1.1  pooka 	psn->parent = parent;
    416   1.1  pooka 	psn_parent->childcount++;
    417   1.1  pooka 
    418  1.22  pooka 	if (pd->getattr_pb) {
    419  1.22  pooka 		psn->getattr_pb = pd->getattr_pb;
    420  1.22  pooka 		pd->getattr_pb = NULL;
    421  1.22  pooka 	}
    422  1.22  pooka 
    423   1.1  pooka 	return pn;
    424   1.1  pooka }
    425   1.1  pooka 
    426   1.1  pooka struct puffs_node *
    427   1.1  pooka allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    428   1.1  pooka 	const char *entryname, const struct vattr *vap)
    429   1.1  pooka {
    430  1.13  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    431   1.1  pooka 	struct psshfs_dir *pd;
    432   1.3  pooka 	struct puffs_node *pn;
    433   1.1  pooka 
    434   1.1  pooka 	pd = direnter(parent, entryname);
    435   1.1  pooka 
    436   1.1  pooka 	pd->va.va_fileid = pctx->nextino++;
    437   1.2  pooka 	if (vap->va_type == VDIR) {
    438   1.1  pooka 		pd->va.va_nlink = 2;
    439   1.2  pooka 		parent->pn_va.va_nlink++;
    440   1.2  pooka 	} else {
    441   1.1  pooka 		pd->va.va_nlink = 1;
    442   1.2  pooka 	}
    443   1.1  pooka 
    444   1.3  pooka 	pn = makenode(pu, parent, pd, vap);
    445   1.3  pooka 	if (pn)
    446   1.3  pooka 		pd->va.va_fileid = pn->pn_va.va_fileid;
    447   1.3  pooka 
    448   1.3  pooka 	return pn;
    449   1.1  pooka }
    450   1.1  pooka 
    451   1.1  pooka struct psshfs_dir *
    452   1.1  pooka direnter(struct puffs_node *parent, const char *entryname)
    453   1.1  pooka {
    454   1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    455   1.1  pooka 	struct psshfs_dir *pd;
    456   1.1  pooka 	int i;
    457   1.1  pooka 
    458   1.1  pooka 	/* create directory entry */
    459   1.1  pooka 	if (psn_parent->denttot == psn_parent->dentnext)
    460   1.1  pooka 		allocdirs(psn_parent);
    461   1.1  pooka 
    462   1.1  pooka 	i = psn_parent->dentnext;
    463   1.1  pooka 	pd = &psn_parent->dir[i];
    464   1.1  pooka 	pd->entryname = estrdup(entryname);
    465   1.1  pooka 	pd->valid = 1;
    466   1.8  pooka 	pd->attrread = 0;
    467   1.4  pooka 	puffs_vattr_null(&pd->va);
    468   1.1  pooka 	psn_parent->dentnext++;
    469   1.1  pooka 
    470   1.1  pooka 	return pd;
    471   1.1  pooka }
    472   1.1  pooka 
    473   1.1  pooka void
    474   1.9  pooka doreclaim(struct puffs_node *pn)
    475   1.9  pooka {
    476   1.9  pooka 	struct psshfs_node *psn = pn->pn_data;
    477   1.9  pooka 	struct psshfs_node *psn_parent;
    478   1.9  pooka 	struct psshfs_dir *dent;
    479   1.9  pooka 
    480   1.9  pooka 	psn_parent = psn->parent->pn_data;
    481   1.9  pooka 	psn_parent->childcount--;
    482   1.9  pooka 
    483  1.11  pooka 	/*
    484  1.11  pooka 	 * Null out entry from directory.  Do not treat a missing entry
    485  1.11  pooka 	 * as an invariant error, since the node might be removed from
    486  1.11  pooka 	 * under us, and we might do a readdir before the reclaim resulting
    487  1.11  pooka 	 * in no directory entry in the parent directory.
    488  1.11  pooka 	 */
    489   1.9  pooka 	dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
    490  1.11  pooka 	if (dent)
    491  1.11  pooka 		dent->entry = NULL;
    492   1.9  pooka 
    493  1.21  pooka 	if (pn->pn_va.va_type == VDIR) {
    494   1.9  pooka 		freedircache(psn->dir, psn->dentnext);
    495  1.23  pooka 		psn->denttot = psn->dentnext = 0;
    496  1.21  pooka 		free(psn->da);
    497  1.21  pooka 	}
    498   1.9  pooka 
    499   1.9  pooka 	puffs_pn_put(pn);
    500   1.9  pooka }
    501   1.9  pooka 
    502   1.9  pooka void
    503   1.9  pooka nukenode(struct puffs_node *node, const char *entryname, int reclaim)
    504   1.1  pooka {
    505   1.1  pooka 	struct psshfs_node *psn, *psn_parent;
    506   1.1  pooka 	struct psshfs_dir *pd;
    507   1.1  pooka 
    508   1.1  pooka 	psn = node->pn_data;
    509   1.1  pooka 	psn_parent = psn->parent->pn_data;
    510   1.1  pooka 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    511   1.1  pooka 	assert(pd != NULL);
    512   1.1  pooka 	pd->valid = 0;
    513   1.1  pooka 	free(pd->entryname);
    514   1.1  pooka 	pd->entryname = NULL;
    515   1.1  pooka 
    516   1.9  pooka 	if (node->pn_va.va_type == VDIR)
    517   1.1  pooka 		psn->parent->pn_va.va_nlink--;
    518   1.6  pooka 
    519   1.9  pooka 	if (reclaim)
    520   1.9  pooka 		doreclaim(node);
    521   1.1  pooka }
    522