Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.9
      1 /*      $NetBSD: subr.c,v 1.9 2007/02/15 13:07:29 pooka Exp $        */
      2 
      3 /*
      4  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #ifndef lint
     33 __RCSID("$NetBSD: subr.c,v 1.9 2007/02/15 13:07:29 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <assert.h>
     37 #include <errno.h>
     38 #include <puffs.h>
     39 #include <stdlib.h>
     40 #include <util.h>
     41 
     42 #include "psshfs.h"
     43 #include "sftp_proto.h"
     44 
     45 static void
     46 freedircache(struct psshfs_dir *base, size_t count)
     47 {
     48 	int i;
     49 
     50 	for (i = 0; i < count; i++) {
     51 		free(base[i].entryname);
     52 		base[i].entryname = NULL;
     53 	}
     54 
     55 	free(base);
     56 }
     57 
     58 #define ENTRYCHUNK 16
     59 static void
     60 allocdirs(struct psshfs_node *psn)
     61 {
     62 	size_t oldtot = psn->denttot;
     63 
     64 	psn->denttot += ENTRYCHUNK;
     65 	psn->dir = erealloc(psn->dir,
     66 	    psn->denttot * sizeof(struct psshfs_dir));
     67 	memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
     68 }
     69 
     70 struct psshfs_dir *
     71 lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
     72 {
     73 	struct psshfs_dir *test;
     74 	int i;
     75 
     76 	for (i = 0; i < ndir; i++) {
     77 		test = &bdir[i];
     78 		if (test->valid != 1)
     79 			continue;
     80 		if (strcmp(test->entryname, name) == 0)
     81 			return test;
     82 	}
     83 
     84 	return NULL;
     85 }
     86 
     87 static struct psshfs_dir *
     88 lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
     89 {
     90 	struct psshfs_dir *test;
     91 	int i;
     92 
     93 	for (i = 0; i < ndir; i++) {
     94 		test = &bdir[i];
     95 		if (test->valid != 1)
     96 			continue;
     97 		if (test->entry == entry)
     98 			return test;
     99 	}
    100 
    101 	return NULL;
    102 }
    103 
    104 #ifdef SUPERREADDIR
    105 struct readdirattr {
    106 	struct psshfs_node *psn;
    107 	int idx;
    108 	char entryname[MAXPATHLEN+1];
    109 };
    110 
    111 static void
    112 readdir_getattr_resp(struct psshfs_ctx *pctx, struct psbuf *pb, void *arg)
    113 {
    114 	struct readdirattr *rda = arg;
    115 	struct psshfs_node *psn = rda->psn;
    116 	struct psshfs_dir *pdir;
    117 	struct vattr va;
    118 
    119 	pdir = lookup(psn->dir, psn->denttot, rda->entryname);
    120 	if (!pdir)
    121 		goto out;
    122 
    123 	if (psbuf_expect_attrs(pb, &va))
    124 		goto out;
    125 
    126 	if (pdir->entry) {
    127 		struct psshfs_node *psn_targ;
    128 		psn_targ = pdir->entry->pn_data;
    129 
    130 		puffs_setvattr(&pdir->entry->pn_va, &va);
    131 		psn_targ->attrread = time(NULL);
    132 	} else {
    133 		puffs_setvattr(&pdir->va, &va);
    134 		pdir->attrread = time(NULL);
    135 	}
    136 
    137  out:
    138 	free(rda);
    139 	psbuf_destroy(pb);
    140 }
    141 
    142 static void
    143 readdir_getattr(struct psshfs_ctx *pctx, struct psshfs_node *psn,
    144 	const char *basepath, int idx)
    145 {
    146 	char path[MAXPATHLEN+1];
    147 	struct psshfs_dir *pdir = psn->dir;
    148 	struct psbuf *pb;
    149 	struct readdirattr *rda;
    150 	const char *entryname = pdir[idx].entryname;
    151 	uint32_t reqid = NEXTREQ(pctx);
    152 
    153 	rda = emalloc(sizeof(struct readdirattr));
    154 	rda->psn = psn;
    155 	rda->idx = idx;
    156 	strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
    157 
    158 	strcpy(path, basepath);
    159 	strcat(path, "/");
    160 	strlcat(path, entryname, sizeof(path));
    161 
    162 	pb = psbuf_make(PSB_OUT);
    163 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    164 	pssh_outbuf_enqueue_nocc(pctx, pb, readdir_getattr_resp, rda, reqid);
    165 }
    166 #endif
    167 
    168 int
    169 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
    170 	struct puffs_node *pn)
    171 {
    172 	struct psshfs_node *psn = pn->pn_data;
    173 	struct psshfs_dir *olddir, *testd;
    174 	struct psbuf *pb;
    175 	uint32_t reqid = NEXTREQ(pctx);
    176 	uint32_t count;
    177 	char *dhand = NULL;
    178 	size_t dhandlen, nent;
    179 	char *longname;
    180 	int idx, rv;
    181 
    182 	assert(pn->pn_va.va_type == VDIR);
    183 
    184 	if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
    185 		return 0;
    186 
    187 	puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
    188 
    189 	pb = psbuf_make(PSB_OUT);
    190 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
    191 	pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    192 
    193 	puffs_cc_yield(pcc);
    194 
    195 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    196 	if (rv)
    197 		goto wayout;
    198 
    199 	/*
    200 	 * Well, the following is O(n^2), so feel free to improve if it
    201 	 * gets too taxing on your system.
    202 	 */
    203 	olddir = psn->dir;
    204 	nent = psn->dentnext;
    205 
    206 	/*
    207 	 * note: for the "getattr in batch" to work, this must be before
    208 	 * the attribute-getting.  Otherwise times for first entries in
    209 	 * large directories might expire before the directory itself and
    210 	 * result in one-by-one attribute fetching.
    211 	 */
    212 	psn->dentread = time(NULL);
    213 
    214 	psn->dentnext = 0;
    215 	psn->denttot = 0;
    216 	psn->dir = NULL;
    217 	idx = 0;
    218 
    219 	for (;;) {
    220 		reqid = NEXTREQ(pctx);
    221 		psbuf_recycle(pb, PSB_OUT);
    222 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    223 		pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    224 
    225 		puffs_cc_yield(pcc);
    226 
    227 		/* check for EOF */
    228 		if (pb->type == SSH_FXP_STATUS) {
    229 			rv = psbuf_expect_status(pb);
    230 			goto out;
    231 		}
    232 		rv = psbuf_expect_name(pb, &count);
    233 		if (rv)
    234 			goto out;
    235 
    236 		for (; count--; idx++) {
    237 			if (idx == psn->denttot)
    238 				allocdirs(psn);
    239 			if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
    240 			    NULL)) {
    241 				rv = EPROTO;
    242 				goto out;
    243 			}
    244 			if (!psbuf_get_str(pb, &longname, NULL)) {
    245 				rv = EPROTO;
    246 				goto out;
    247 			}
    248 			if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
    249 				rv = EPROTO;
    250 				goto out;
    251 			}
    252 			if (sscanf(longname, "%*s%d",
    253 			    &psn->dir[idx].va.va_nlink) != 1) {
    254 				rv = EPROTO;
    255 				goto out;
    256 			}
    257 			free(longname);
    258 
    259 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    260 			if (testd) {
    261 				psn->dir[idx].entry = testd->entry;
    262 				psn->dir[idx].va = testd->va;
    263 			} else {
    264 				psn->dir[idx].entry = NULL;
    265 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    266 			}
    267 #ifdef SUPERREADDIR
    268 			/*
    269 			 * XXX: there's a dangling pointer race here if
    270 			 * the server responds to our queries out-of-order.
    271 			 * fixxxme some day
    272 			 */
    273 			readdir_getattr(pctx, psn, PNPATH(pn), idx);
    274 #endif
    275 			psn->dir[idx].valid = 1;
    276 		}
    277 	}
    278 
    279  out:
    280 	/* XXX: rv */
    281 	psn->dentnext = idx;
    282 	freedircache(olddir, nent);
    283 
    284 	reqid = NEXTREQ(pctx);
    285 	psbuf_recycle(pb, PSB_OUT);
    286 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    287 	pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
    288 
    289 	puffs_cc_yield(pcc);
    290 
    291 	/* EDONTCARE for the response */
    292 
    293  wayout:
    294 	free(dhand);
    295 	psbuf_destroy(pb);
    296 	return rv;
    297 }
    298 
    299 struct puffs_node *
    300 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    301 	struct psshfs_dir *pd, const struct vattr *vap)
    302 {
    303 	struct psshfs_node *psn_parent = parent->pn_data;
    304 	struct psshfs_node *psn;
    305 	struct puffs_node *pn;
    306 
    307 	psn = emalloc(sizeof(struct psshfs_node));
    308 	memset(psn, 0, sizeof(struct psshfs_node));
    309 
    310 	pn = puffs_pn_new(pu, psn);
    311 	if (!pn) {
    312 		free(psn);
    313 		return NULL;
    314 	}
    315 	puffs_setvattr(&pn->pn_va, &pd->va);
    316 	psn->attrread = pd->attrread;
    317 	puffs_setvattr(&pn->pn_va, vap);
    318 
    319 	pd->entry = pn;
    320 	psn->parent = parent;
    321 	psn_parent->childcount++;
    322 
    323 	return pn;
    324 }
    325 
    326 struct puffs_node *
    327 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    328 	const char *entryname, const struct vattr *vap)
    329 {
    330 	struct psshfs_ctx *pctx = pu->pu_privdata;
    331 	struct psshfs_dir *pd;
    332 	struct puffs_node *pn;
    333 
    334 	pd = direnter(parent, entryname);
    335 
    336 	pd->va.va_fileid = pctx->nextino++;
    337 	if (vap->va_type == VDIR) {
    338 		pd->va.va_nlink = 2;
    339 		parent->pn_va.va_nlink++;
    340 	} else {
    341 		pd->va.va_nlink = 1;
    342 	}
    343 
    344 	pn = makenode(pu, parent, pd, vap);
    345 	if (pn)
    346 		pd->va.va_fileid = pn->pn_va.va_fileid;
    347 
    348 	return pn;
    349 }
    350 
    351 struct psshfs_dir *
    352 direnter(struct puffs_node *parent, const char *entryname)
    353 {
    354 	struct psshfs_node *psn_parent = parent->pn_data;
    355 	struct psshfs_dir *pd;
    356 	int i;
    357 
    358 	/* create directory entry */
    359 	if (psn_parent->denttot == psn_parent->dentnext)
    360 		allocdirs(psn_parent);
    361 
    362 	i = psn_parent->dentnext;
    363 	pd = &psn_parent->dir[i];
    364 	pd->entryname = estrdup(entryname);
    365 	pd->valid = 1;
    366 	pd->attrread = 0;
    367 	puffs_vattr_null(&pd->va);
    368 	psn_parent->dentnext++;
    369 
    370 	return pd;
    371 }
    372 
    373 void
    374 doreclaim(struct puffs_node *pn)
    375 {
    376 	struct psshfs_node *psn = pn->pn_data;
    377 	struct psshfs_node *psn_parent;
    378 	struct psshfs_dir *dent;
    379 
    380 	psn_parent = psn->parent->pn_data;
    381 	psn_parent->childcount--;
    382 
    383 	dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
    384 	assert(dent);
    385 	dent->entry = NULL;
    386 
    387 	if (pn->pn_va.va_type == VDIR)
    388 		freedircache(psn->dir, psn->dentnext);
    389 
    390 	puffs_pn_put(pn);
    391 }
    392 
    393 void
    394 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
    395 {
    396 	struct psshfs_node *psn, *psn_parent;
    397 	struct psshfs_dir *pd;
    398 
    399 	psn = node->pn_data;
    400 	psn_parent = psn->parent->pn_data;
    401 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    402 	assert(pd != NULL);
    403 	pd->valid = 0;
    404 	free(pd->entryname);
    405 	pd->entryname = NULL;
    406 
    407 	if (node->pn_va.va_type == VDIR)
    408 		psn->parent->pn_va.va_nlink--;
    409 
    410 	if (reclaim)
    411 		doreclaim(node);
    412 }
    413