Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.37
      1  1.37  pooka /*      $NetBSD: subr.c,v 1.37 2007/11/30 16:24:04 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.37  pooka __RCSID("$NetBSD: subr.c,v 1.37 2007/11/30 16:24:04 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.21  pooka 
     67  1.21  pooka 	psn->da = erealloc(psn->da, psn->denttot * sizeof(struct delayattr));
     68   1.1  pooka }
     69   1.1  pooka 
     70  1.36  pooka static void
     71  1.36  pooka setpnva(struct puffs_usermount *pu, struct puffs_node *pn,
     72  1.36  pooka 	const struct vattr *vap)
     73  1.36  pooka {
     74  1.36  pooka 	struct psshfs_node *psn = pn->pn_data;
     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.36  pooka 	puffs_setvattr(&pn->pn_va, vap);
     87  1.36  pooka 	psn->attrread = time(NULL);
     88  1.36  pooka }
     89  1.36  pooka 
     90   1.1  pooka struct psshfs_dir *
     91   1.9  pooka lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
     92   1.1  pooka {
     93   1.9  pooka 	struct psshfs_dir *test;
     94   1.1  pooka 	int i;
     95   1.1  pooka 
     96   1.1  pooka 	for (i = 0; i < ndir; i++) {
     97   1.9  pooka 		test = &bdir[i];
     98   1.9  pooka 		if (test->valid != 1)
     99   1.1  pooka 			continue;
    100   1.9  pooka 		if (strcmp(test->entryname, name) == 0)
    101   1.9  pooka 			return test;
    102   1.9  pooka 	}
    103   1.9  pooka 
    104   1.9  pooka 	return NULL;
    105   1.9  pooka }
    106   1.9  pooka 
    107   1.9  pooka static struct psshfs_dir *
    108   1.9  pooka lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
    109   1.9  pooka {
    110   1.9  pooka 	struct psshfs_dir *test;
    111   1.9  pooka 	int i;
    112   1.9  pooka 
    113   1.9  pooka 	for (i = 0; i < ndir; i++) {
    114   1.9  pooka 		test = &bdir[i];
    115   1.9  pooka 		if (test->valid != 1)
    116   1.9  pooka 			continue;
    117   1.9  pooka 		if (test->entry == entry)
    118   1.9  pooka 			return test;
    119   1.1  pooka 	}
    120   1.1  pooka 
    121   1.1  pooka 	return NULL;
    122   1.1  pooka }
    123   1.1  pooka 
    124  1.37  pooka 
    125  1.37  pooka void
    126  1.37  pooka closehandles(struct puffs_usermount *pu, struct psshfs_node *psn, int which)
    127  1.37  pooka {
    128  1.37  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    129  1.37  pooka 	struct puffs_framebuf *pb1, *pb2;
    130  1.37  pooka 	uint32_t reqid;
    131  1.37  pooka 
    132  1.37  pooka 	if (psn->fhand_r && (which & HANDLE_READ)) {
    133  1.37  pooka 		assert(psn->lazyopen_r == NULL);
    134  1.37  pooka 
    135  1.37  pooka 		pb1 = psbuf_makeout();
    136  1.37  pooka 		reqid = NEXTREQ(pctx);
    137  1.37  pooka 		psbuf_req_data(pb1, SSH_FXP_CLOSE, reqid,
    138  1.37  pooka 		    psn->fhand_r, psn->fhand_r_len);
    139  1.37  pooka 		puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb1, 1, 0);
    140  1.37  pooka 		free(psn->fhand_r);
    141  1.37  pooka 		psn->fhand_r = NULL;
    142  1.37  pooka 	}
    143  1.37  pooka 
    144  1.37  pooka 	if (psn->fhand_w && (which & HANDLE_WRITE)) {
    145  1.37  pooka 		assert(psn->lazyopen_w == NULL);
    146  1.37  pooka 
    147  1.37  pooka 		pb2 = psbuf_makeout();
    148  1.37  pooka 		reqid = NEXTREQ(pctx);
    149  1.37  pooka 		psbuf_req_data(pb2, SSH_FXP_CLOSE, reqid,
    150  1.37  pooka 		    psn->fhand_w, psn->fhand_w_len);
    151  1.37  pooka 		puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb2, 1, 0);
    152  1.37  pooka 		free(psn->fhand_w);
    153  1.37  pooka 		psn->fhand_w = NULL;
    154  1.37  pooka 	}
    155  1.37  pooka 
    156  1.37  pooka 	psn->stat |= PSN_HANDLECLOSE;
    157  1.37  pooka }
    158  1.37  pooka 
    159  1.37  pooka void
    160  1.37  pooka lazyopen_rresp(struct puffs_usermount *pu, struct puffs_framebuf *pb,
    161  1.37  pooka 	void *arg, int error)
    162  1.37  pooka {
    163  1.37  pooka 	struct psshfs_node *psn = arg;
    164  1.37  pooka 
    165  1.37  pooka 	/* XXX: this is not enough */
    166  1.37  pooka 	if (psn->stat & PSN_RECLAIMED) {
    167  1.37  pooka 		error = ENOENT;
    168  1.37  pooka 		goto moreout;
    169  1.37  pooka 	}
    170  1.37  pooka 	if (error)
    171  1.37  pooka 		goto out;
    172  1.37  pooka 
    173  1.37  pooka 	error = psbuf_expect_handle(pb, &psn->fhand_r, &psn->fhand_r_len);
    174  1.37  pooka 
    175  1.37  pooka  out:
    176  1.37  pooka 	psn->lazyopen_err_r = error;
    177  1.37  pooka 	psn->lazyopen_r = NULL;
    178  1.37  pooka 	if (error)
    179  1.37  pooka 		psn->stat &= ~PSN_DOLAZY_R;
    180  1.37  pooka 	if (psn->stat & PSN_HANDLECLOSE && (psn->stat & PSN_LAZYWAIT_R) == 0)
    181  1.37  pooka 		closehandles(pu, psn, HANDLE_READ);
    182  1.37  pooka  moreout:
    183  1.37  pooka 	puffs_framebuf_destroy(pb);
    184  1.37  pooka }
    185  1.37  pooka 
    186  1.37  pooka void
    187  1.37  pooka lazyopen_wresp(struct puffs_usermount *pu, struct puffs_framebuf *pb,
    188  1.37  pooka 	void *arg, int error)
    189  1.37  pooka {
    190  1.37  pooka 	struct psshfs_node *psn = arg;
    191  1.37  pooka 
    192  1.37  pooka 	/* XXX: this is not enough */
    193  1.37  pooka 	if (psn->stat & PSN_RECLAIMED) {
    194  1.37  pooka 		error = ENOENT;
    195  1.37  pooka 		goto moreout;
    196  1.37  pooka 	}
    197  1.37  pooka 	if (error)
    198  1.37  pooka 		goto out;
    199  1.37  pooka 
    200  1.37  pooka 	error = psbuf_expect_handle(pb, &psn->fhand_w, &psn->fhand_w_len);
    201  1.37  pooka 
    202  1.37  pooka  out:
    203  1.37  pooka 	psn->lazyopen_err_w = error;
    204  1.37  pooka 	psn->lazyopen_w = NULL;
    205  1.37  pooka 	if (error)
    206  1.37  pooka 		psn->stat &= ~PSN_DOLAZY_W;
    207  1.37  pooka 	if (psn->stat & PSN_HANDLECLOSE && (psn->stat & PSN_LAZYWAIT_W) == 0)
    208  1.37  pooka 		closehandles(pu, psn, HANDLE_WRITE);
    209  1.37  pooka  moreout:
    210  1.37  pooka 	puffs_framebuf_destroy(pb);
    211  1.37  pooka }
    212  1.37  pooka 
    213   1.8  pooka struct readdirattr {
    214   1.8  pooka 	struct psshfs_node *psn;
    215   1.8  pooka 	int idx;
    216   1.8  pooka 	char entryname[MAXPATHLEN+1];
    217   1.8  pooka };
    218   1.8  pooka 
    219   1.8  pooka static void
    220  1.14  pooka readdir_getattr_resp(struct puffs_usermount *pu,
    221  1.18  pooka 	struct puffs_framebuf *pb, void *arg, int error)
    222   1.8  pooka {
    223   1.8  pooka 	struct readdirattr *rda = arg;
    224   1.8  pooka 	struct psshfs_node *psn = rda->psn;
    225  1.23  pooka 	struct psshfs_node *psn_targ = NULL;
    226  1.23  pooka 	struct psshfs_dir *pdir = NULL;
    227   1.8  pooka 	struct vattr va;
    228   1.8  pooka 
    229  1.23  pooka 	/* XXX: this is not enough */
    230  1.23  pooka 	if (psn->stat & PSN_RECLAIMED)
    231  1.23  pooka 		goto out;
    232  1.23  pooka 
    233  1.18  pooka 	if (error)
    234  1.18  pooka 		goto out;
    235  1.18  pooka 
    236   1.8  pooka 	pdir = lookup(psn->dir, psn->denttot, rda->entryname);
    237   1.8  pooka 	if (!pdir)
    238   1.8  pooka 		goto out;
    239   1.8  pooka 
    240   1.8  pooka 	if (psbuf_expect_attrs(pb, &va))
    241   1.8  pooka 		goto out;
    242   1.8  pooka 
    243   1.8  pooka 	if (pdir->entry) {
    244   1.8  pooka 		psn_targ = pdir->entry->pn_data;
    245   1.8  pooka 
    246  1.36  pooka 		setpnva(pu, pdir->entry, &va);
    247   1.8  pooka 	} else {
    248   1.8  pooka 		puffs_setvattr(&pdir->va, &va);
    249   1.8  pooka 		pdir->attrread = time(NULL);
    250   1.8  pooka 	}
    251   1.8  pooka 
    252   1.8  pooka  out:
    253  1.22  pooka 	if (psn_targ) {
    254  1.22  pooka 		psn_targ->getattr_pb = NULL;
    255  1.22  pooka 		assert(pdir->getattr_pb == NULL);
    256  1.23  pooka 	} else if (pdir) {
    257  1.22  pooka 		pdir->getattr_pb = NULL;
    258  1.22  pooka 	}
    259  1.35   jmmv 
    260   1.8  pooka 	free(rda);
    261  1.14  pooka 	puffs_framebuf_destroy(pb);
    262   1.8  pooka }
    263   1.8  pooka 
    264  1.21  pooka static void
    265  1.14  pooka readdir_getattr(struct puffs_usermount *pu, struct psshfs_node *psn,
    266   1.8  pooka 	const char *basepath, int idx)
    267   1.8  pooka {
    268   1.8  pooka 	char path[MAXPATHLEN+1];
    269  1.14  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    270   1.8  pooka 	struct psshfs_dir *pdir = psn->dir;
    271  1.14  pooka 	struct puffs_framebuf *pb;
    272   1.8  pooka 	struct readdirattr *rda;
    273   1.8  pooka 	const char *entryname = pdir[idx].entryname;
    274   1.8  pooka 	uint32_t reqid = NEXTREQ(pctx);
    275   1.8  pooka 
    276   1.8  pooka 	rda = emalloc(sizeof(struct readdirattr));
    277   1.8  pooka 	rda->psn = psn;
    278   1.8  pooka 	rda->idx = idx;
    279   1.8  pooka 	strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
    280   1.8  pooka 
    281   1.8  pooka 	strcpy(path, basepath);
    282   1.8  pooka 	strcat(path, "/");
    283   1.8  pooka 	strlcat(path, entryname, sizeof(path));
    284   1.8  pooka 
    285  1.14  pooka 	pb = psbuf_makeout();
    286   1.8  pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    287  1.21  pooka 	psn->da[psn->nextda].pufbuf = pb;
    288  1.21  pooka 	psn->da[psn->nextda].rda = rda;
    289  1.21  pooka 	psn->nextda++;
    290  1.21  pooka }
    291  1.21  pooka 
    292  1.21  pooka static void
    293  1.21  pooka readdir_getattr_send(struct puffs_usermount *pu, struct psshfs_node *psn)
    294  1.21  pooka {
    295  1.21  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    296  1.22  pooka 	struct psshfs_dir *pdir = psn->dir;
    297  1.22  pooka 	struct psshfs_node *psn_targ;
    298  1.22  pooka 	struct readdirattr *rda;
    299  1.21  pooka 	size_t i;
    300  1.21  pooka 	int rv = 0;
    301  1.21  pooka 
    302  1.22  pooka 	for (i = 0; i < psn->nextda; i++) {
    303  1.22  pooka 		rda = psn->da[i].rda;
    304  1.22  pooka 		if (pdir[rda->idx].entry) {
    305  1.22  pooka 			psn_targ = pdir[rda->idx].entry->pn_data;
    306  1.22  pooka 			psn_targ->getattr_pb = psn->da[i].pufbuf;
    307  1.22  pooka 		} else {
    308  1.22  pooka 			pdir[rda->idx].getattr_pb = psn->da[i].pufbuf;
    309  1.22  pooka 		}
    310  1.22  pooka 		SENDCB(psn->da[i].pufbuf, readdir_getattr_resp, rda);
    311  1.22  pooka 	}
    312  1.16  pooka 
    313  1.16  pooka  out:
    314  1.21  pooka 	return;
    315   1.8  pooka }
    316   1.8  pooka 
    317   1.1  pooka int
    318  1.12  pooka getpathattr(struct puffs_cc *pcc, const char *path, struct vattr *vap)
    319  1.12  pooka {
    320  1.12  pooka 	PSSHFSAUTOVAR(pcc);
    321  1.12  pooka 
    322  1.12  pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    323  1.15  pooka 	GETRESPONSE(pb);
    324  1.12  pooka 
    325  1.12  pooka 	rv = psbuf_expect_attrs(pb, vap);
    326  1.12  pooka 
    327  1.16  pooka  out:
    328  1.12  pooka 	PSSHFSRETURN(rv);
    329  1.12  pooka }
    330  1.12  pooka 
    331  1.12  pooka int
    332  1.12  pooka getnodeattr(struct puffs_cc *pcc, struct puffs_node *pn)
    333  1.12  pooka {
    334  1.12  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    335  1.31  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    336  1.12  pooka 	struct psshfs_node *psn = pn->pn_data;
    337  1.12  pooka 	struct vattr va;
    338  1.23  pooka 	int rv, dohardway;
    339  1.12  pooka 
    340  1.31  pooka 	if (!psn->attrread || REFRESHTIMEOUT(pctx, time(NULL)-psn->attrread)) {
    341  1.23  pooka 		dohardway = 1;
    342  1.22  pooka 		if (psn->getattr_pb) {
    343  1.22  pooka 			rv=puffs_framev_framebuf_ccpromote(psn->getattr_pb,pcc);
    344  1.23  pooka 			if (rv == 0) {
    345  1.23  pooka 				rv = psbuf_expect_attrs(psn->getattr_pb, &va);
    346  1.23  pooka 				puffs_framebuf_destroy(psn->getattr_pb);
    347  1.23  pooka 				psn->getattr_pb = NULL;
    348  1.23  pooka 				if (rv == 0)
    349  1.23  pooka 					dohardway = 0;
    350  1.23  pooka 			}
    351  1.23  pooka 		}
    352  1.22  pooka 
    353  1.23  pooka 		if (dohardway) {
    354  1.22  pooka 			rv = getpathattr(pcc, PNPATH(pn), &va);
    355  1.22  pooka 			if (rv)
    356  1.22  pooka 				return rv;
    357  1.22  pooka 		}
    358  1.12  pooka 
    359  1.36  pooka 		setpnva(pu, pn, &va);
    360  1.12  pooka 	}
    361  1.12  pooka 
    362  1.12  pooka 	return 0;
    363  1.12  pooka }
    364  1.12  pooka 
    365  1.12  pooka int
    366   1.1  pooka sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
    367   1.1  pooka 	struct puffs_node *pn)
    368   1.1  pooka {
    369  1.15  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    370   1.1  pooka 	struct psshfs_node *psn = pn->pn_data;
    371   1.1  pooka 	struct psshfs_dir *olddir, *testd;
    372  1.14  pooka 	struct puffs_framebuf *pb;
    373   1.1  pooka 	uint32_t reqid = NEXTREQ(pctx);
    374  1.10  pooka 	uint32_t count, dhandlen;
    375   1.1  pooka 	char *dhand = NULL;
    376  1.10  pooka 	size_t nent;
    377  1.29  pooka 	char *longname = NULL;
    378   1.1  pooka 	int idx, rv;
    379   1.1  pooka 
    380   1.1  pooka 	assert(pn->pn_va.va_type == VDIR);
    381  1.16  pooka 	idx = 0;
    382  1.16  pooka 	olddir = psn->dir;
    383  1.16  pooka 	nent = psn->dentnext;
    384   1.1  pooka 
    385  1.32  pooka 	if (psn->dir && psn->dentread
    386  1.32  pooka 	    && !REFRESHTIMEOUT(pctx, time(NULL) - psn->dentread))
    387   1.1  pooka 		return 0;
    388   1.1  pooka 
    389  1.25  pooka 	if ((rv = puffs_inval_namecache_dir(pu, pn)))
    390  1.25  pooka 		warn("readdir: dcache inval fail %p", pn);
    391   1.5  pooka 
    392  1.14  pooka 	pb = psbuf_makeout();
    393   1.7  pooka 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
    394  1.27  pooka 	if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb, 0) == -1) {
    395  1.27  pooka 		rv = errno;
    396  1.27  pooka 		goto wayout;
    397  1.27  pooka 	}
    398   1.1  pooka 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    399   1.1  pooka 	if (rv)
    400   1.1  pooka 		goto wayout;
    401   1.1  pooka 
    402   1.1  pooka 	/*
    403   1.1  pooka 	 * Well, the following is O(n^2), so feel free to improve if it
    404   1.1  pooka 	 * gets too taxing on your system.
    405   1.1  pooka 	 */
    406   1.1  pooka 
    407   1.8  pooka 	/*
    408   1.8  pooka 	 * note: for the "getattr in batch" to work, this must be before
    409   1.8  pooka 	 * the attribute-getting.  Otherwise times for first entries in
    410   1.8  pooka 	 * large directories might expire before the directory itself and
    411   1.8  pooka 	 * result in one-by-one attribute fetching.
    412   1.8  pooka 	 */
    413   1.8  pooka 	psn->dentread = time(NULL);
    414   1.8  pooka 
    415   1.1  pooka 	psn->dentnext = 0;
    416   1.1  pooka 	psn->denttot = 0;
    417   1.1  pooka 	psn->dir = NULL;
    418  1.21  pooka 	psn->nextda = 0;
    419   1.1  pooka 
    420   1.1  pooka 	for (;;) {
    421   1.1  pooka 		reqid = NEXTREQ(pctx);
    422  1.14  pooka 		psbuf_recycleout(pb);
    423   1.1  pooka 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    424  1.15  pooka 		GETRESPONSE(pb);
    425   1.1  pooka 
    426   1.1  pooka 		/* check for EOF */
    427  1.14  pooka 		if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
    428   1.1  pooka 			rv = psbuf_expect_status(pb);
    429   1.1  pooka 			goto out;
    430   1.1  pooka 		}
    431   1.1  pooka 		rv = psbuf_expect_name(pb, &count);
    432   1.1  pooka 		if (rv)
    433   1.1  pooka 			goto out;
    434   1.1  pooka 
    435   1.1  pooka 		for (; count--; idx++) {
    436   1.1  pooka 			if (idx == psn->denttot)
    437   1.1  pooka 				allocdirs(psn);
    438  1.14  pooka 			if ((rv = psbuf_get_str(pb,
    439  1.14  pooka 			    &psn->dir[idx].entryname, NULL)))
    440   1.1  pooka 				goto out;
    441  1.29  pooka 			if ((rv = psbuf_get_str(pb, &longname, NULL)) != 0)
    442   1.1  pooka 				goto out;
    443  1.29  pooka 			if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va)) != 0)
    444   1.1  pooka 				goto out;
    445   1.1  pooka 			if (sscanf(longname, "%*s%d",
    446   1.1  pooka 			    &psn->dir[idx].va.va_nlink) != 1) {
    447   1.1  pooka 				rv = EPROTO;
    448   1.1  pooka 				goto out;
    449   1.1  pooka 			}
    450   1.1  pooka 			free(longname);
    451  1.29  pooka 			longname = NULL;
    452   1.1  pooka 
    453   1.1  pooka 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    454   1.1  pooka 			if (testd) {
    455   1.1  pooka 				psn->dir[idx].entry = testd->entry;
    456   1.8  pooka 				psn->dir[idx].va = testd->va;
    457   1.1  pooka 			} else {
    458   1.1  pooka 				psn->dir[idx].entry = NULL;
    459   1.1  pooka 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    460   1.1  pooka 			}
    461   1.8  pooka 			/*
    462   1.8  pooka 			 * XXX: there's a dangling pointer race here if
    463   1.8  pooka 			 * the server responds to our queries out-of-order.
    464   1.8  pooka 			 * fixxxme some day
    465   1.8  pooka 			 */
    466  1.21  pooka 			readdir_getattr(puffs_cc_getusermount(pcc),
    467  1.14  pooka 			    psn, PNPATH(pn), idx);
    468  1.22  pooka 
    469   1.1  pooka 			psn->dir[idx].valid = 1;
    470   1.1  pooka 		}
    471   1.1  pooka 	}
    472   1.1  pooka 
    473   1.1  pooka  out:
    474  1.21  pooka 	/* fire off getattr requests */
    475  1.21  pooka 	readdir_getattr_send(pu, psn);
    476  1.21  pooka 
    477   1.1  pooka 	/* XXX: rv */
    478   1.1  pooka 	psn->dentnext = idx;
    479   1.1  pooka 	freedircache(olddir, nent);
    480   1.1  pooka 
    481   1.1  pooka 	reqid = NEXTREQ(pctx);
    482  1.14  pooka 	psbuf_recycleout(pb);
    483   1.1  pooka 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    484  1.28  pooka 	puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb, 1, 0);
    485  1.11  pooka 	free(dhand);
    486  1.29  pooka 	free(longname);
    487  1.21  pooka 
    488  1.12  pooka 	return rv;
    489   1.1  pooka 
    490   1.1  pooka  wayout:
    491   1.1  pooka 	free(dhand);
    492  1.11  pooka 	PSSHFSRETURN(rv);
    493   1.1  pooka }
    494   1.1  pooka 
    495   1.1  pooka struct puffs_node *
    496   1.1  pooka makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    497   1.1  pooka 	struct psshfs_dir *pd, const struct vattr *vap)
    498   1.1  pooka {
    499   1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    500   1.1  pooka 	struct psshfs_node *psn;
    501   1.1  pooka 	struct puffs_node *pn;
    502   1.1  pooka 
    503   1.1  pooka 	psn = emalloc(sizeof(struct psshfs_node));
    504   1.1  pooka 	memset(psn, 0, sizeof(struct psshfs_node));
    505   1.1  pooka 
    506   1.1  pooka 	pn = puffs_pn_new(pu, psn);
    507   1.1  pooka 	if (!pn) {
    508   1.1  pooka 		free(psn);
    509   1.1  pooka 		return NULL;
    510   1.1  pooka 	}
    511  1.36  pooka 	setpnva(pu, pn, &pd->va);
    512  1.36  pooka 	setpnva(pu, pn, vap);
    513   1.1  pooka 
    514   1.1  pooka 	pd->entry = pn;
    515   1.1  pooka 	psn->parent = parent;
    516   1.1  pooka 	psn_parent->childcount++;
    517   1.1  pooka 
    518  1.30  pooka 	TAILQ_INIT(&psn->pw);
    519  1.26  pooka 
    520  1.22  pooka 	if (pd->getattr_pb) {
    521  1.22  pooka 		psn->getattr_pb = pd->getattr_pb;
    522  1.22  pooka 		pd->getattr_pb = NULL;
    523  1.22  pooka 	}
    524  1.22  pooka 
    525   1.1  pooka 	return pn;
    526   1.1  pooka }
    527   1.1  pooka 
    528   1.1  pooka struct puffs_node *
    529   1.1  pooka allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    530   1.1  pooka 	const char *entryname, const struct vattr *vap)
    531   1.1  pooka {
    532  1.13  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    533   1.1  pooka 	struct psshfs_dir *pd;
    534   1.3  pooka 	struct puffs_node *pn;
    535   1.1  pooka 
    536   1.1  pooka 	pd = direnter(parent, entryname);
    537   1.1  pooka 
    538   1.1  pooka 	pd->va.va_fileid = pctx->nextino++;
    539   1.2  pooka 	if (vap->va_type == VDIR) {
    540   1.1  pooka 		pd->va.va_nlink = 2;
    541   1.2  pooka 		parent->pn_va.va_nlink++;
    542   1.2  pooka 	} else {
    543   1.1  pooka 		pd->va.va_nlink = 1;
    544   1.2  pooka 	}
    545   1.1  pooka 
    546   1.3  pooka 	pn = makenode(pu, parent, pd, vap);
    547   1.3  pooka 	if (pn)
    548   1.3  pooka 		pd->va.va_fileid = pn->pn_va.va_fileid;
    549   1.3  pooka 
    550   1.3  pooka 	return pn;
    551   1.1  pooka }
    552   1.1  pooka 
    553   1.1  pooka struct psshfs_dir *
    554   1.1  pooka direnter(struct puffs_node *parent, const char *entryname)
    555   1.1  pooka {
    556   1.1  pooka 	struct psshfs_node *psn_parent = parent->pn_data;
    557   1.1  pooka 	struct psshfs_dir *pd;
    558   1.1  pooka 	int i;
    559   1.1  pooka 
    560   1.1  pooka 	/* create directory entry */
    561   1.1  pooka 	if (psn_parent->denttot == psn_parent->dentnext)
    562   1.1  pooka 		allocdirs(psn_parent);
    563   1.1  pooka 
    564   1.1  pooka 	i = psn_parent->dentnext;
    565   1.1  pooka 	pd = &psn_parent->dir[i];
    566   1.1  pooka 	pd->entryname = estrdup(entryname);
    567   1.1  pooka 	pd->valid = 1;
    568   1.8  pooka 	pd->attrread = 0;
    569   1.4  pooka 	puffs_vattr_null(&pd->va);
    570   1.1  pooka 	psn_parent->dentnext++;
    571   1.1  pooka 
    572   1.1  pooka 	return pd;
    573   1.1  pooka }
    574   1.1  pooka 
    575   1.1  pooka void
    576   1.9  pooka doreclaim(struct puffs_node *pn)
    577   1.9  pooka {
    578   1.9  pooka 	struct psshfs_node *psn = pn->pn_data;
    579   1.9  pooka 	struct psshfs_node *psn_parent;
    580   1.9  pooka 	struct psshfs_dir *dent;
    581   1.9  pooka 
    582   1.9  pooka 	psn_parent = psn->parent->pn_data;
    583   1.9  pooka 	psn_parent->childcount--;
    584   1.9  pooka 
    585  1.11  pooka 	/*
    586  1.11  pooka 	 * Null out entry from directory.  Do not treat a missing entry
    587  1.11  pooka 	 * as an invariant error, since the node might be removed from
    588  1.11  pooka 	 * under us, and we might do a readdir before the reclaim resulting
    589  1.11  pooka 	 * in no directory entry in the parent directory.
    590  1.11  pooka 	 */
    591   1.9  pooka 	dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
    592  1.11  pooka 	if (dent)
    593  1.11  pooka 		dent->entry = NULL;
    594   1.9  pooka 
    595  1.21  pooka 	if (pn->pn_va.va_type == VDIR) {
    596   1.9  pooka 		freedircache(psn->dir, psn->dentnext);
    597  1.23  pooka 		psn->denttot = psn->dentnext = 0;
    598  1.21  pooka 		free(psn->da);
    599  1.21  pooka 	}
    600  1.33  pooka 	if (psn->symlink)
    601  1.33  pooka 		free(psn->symlink);
    602   1.9  pooka 
    603   1.9  pooka 	puffs_pn_put(pn);
    604   1.9  pooka }
    605   1.9  pooka 
    606   1.9  pooka void
    607   1.9  pooka nukenode(struct puffs_node *node, const char *entryname, int reclaim)
    608   1.1  pooka {
    609   1.1  pooka 	struct psshfs_node *psn, *psn_parent;
    610   1.1  pooka 	struct psshfs_dir *pd;
    611   1.1  pooka 
    612   1.1  pooka 	psn = node->pn_data;
    613   1.1  pooka 	psn_parent = psn->parent->pn_data;
    614   1.1  pooka 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    615   1.1  pooka 	assert(pd != NULL);
    616   1.1  pooka 	pd->valid = 0;
    617   1.1  pooka 	free(pd->entryname);
    618   1.1  pooka 	pd->entryname = NULL;
    619   1.1  pooka 
    620   1.9  pooka 	if (node->pn_va.va_type == VDIR)
    621   1.1  pooka 		psn->parent->pn_va.va_nlink--;
    622   1.6  pooka 
    623   1.9  pooka 	if (reclaim)
    624   1.9  pooka 		doreclaim(node);
    625   1.1  pooka }
    626