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