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