Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.21
      1 /*      $NetBSD: subr.c,v 1.21 2007/05/20 17:47:12 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.21 2007/05/20 17:47:12 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 #ifdef SUPERREADDIR
     70 	psn->da = erealloc(psn->da, psn->denttot * sizeof(struct delayattr));
     71 #endif
     72 }
     73 
     74 struct psshfs_dir *
     75 lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
     76 {
     77 	struct psshfs_dir *test;
     78 	int i;
     79 
     80 	for (i = 0; i < ndir; i++) {
     81 		test = &bdir[i];
     82 		if (test->valid != 1)
     83 			continue;
     84 		if (strcmp(test->entryname, name) == 0)
     85 			return test;
     86 	}
     87 
     88 	return NULL;
     89 }
     90 
     91 static struct psshfs_dir *
     92 lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
     93 {
     94 	struct psshfs_dir *test;
     95 	int i;
     96 
     97 	for (i = 0; i < ndir; i++) {
     98 		test = &bdir[i];
     99 		if (test->valid != 1)
    100 			continue;
    101 		if (test->entry == entry)
    102 			return test;
    103 	}
    104 
    105 	return NULL;
    106 }
    107 
    108 #ifdef SUPERREADDIR
    109 struct readdirattr {
    110 	struct psshfs_node *psn;
    111 	int idx;
    112 	char entryname[MAXPATHLEN+1];
    113 };
    114 
    115 static void
    116 readdir_getattr_resp(struct puffs_usermount *pu,
    117 	struct puffs_framebuf *pb, void *arg, int error)
    118 {
    119 	struct readdirattr *rda = arg;
    120 	struct psshfs_node *psn = rda->psn;
    121 	struct psshfs_dir *pdir;
    122 	struct vattr va;
    123 
    124 	if (error)
    125 		goto out;
    126 
    127 	pdir = lookup(psn->dir, psn->denttot, rda->entryname);
    128 	if (!pdir)
    129 		goto out;
    130 
    131 	if (psbuf_expect_attrs(pb, &va))
    132 		goto out;
    133 
    134 	if (pdir->entry) {
    135 		struct psshfs_node *psn_targ;
    136 		psn_targ = pdir->entry->pn_data;
    137 
    138 		puffs_setvattr(&pdir->entry->pn_va, &va);
    139 		psn_targ->attrread = time(NULL);
    140 	} else {
    141 		puffs_setvattr(&pdir->va, &va);
    142 		pdir->attrread = time(NULL);
    143 	}
    144 
    145  out:
    146 	free(rda);
    147 	puffs_framebuf_destroy(pb);
    148 }
    149 
    150 static void
    151 readdir_getattr(struct puffs_usermount *pu, struct psshfs_node *psn,
    152 	const char *basepath, int idx)
    153 {
    154 	char path[MAXPATHLEN+1];
    155 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    156 	struct psshfs_dir *pdir = psn->dir;
    157 	struct puffs_framebuf *pb;
    158 	struct readdirattr *rda;
    159 	const char *entryname = pdir[idx].entryname;
    160 	uint32_t reqid = NEXTREQ(pctx);
    161 
    162 	rda = emalloc(sizeof(struct readdirattr));
    163 	rda->psn = psn;
    164 	rda->idx = idx;
    165 	strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
    166 
    167 	strcpy(path, basepath);
    168 	strcat(path, "/");
    169 	strlcat(path, entryname, sizeof(path));
    170 
    171 	pb = psbuf_makeout();
    172 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    173 	psn->da[psn->nextda].pufbuf = pb;
    174 	psn->da[psn->nextda].rda = rda;
    175 	psn->nextda++;
    176 }
    177 
    178 static void
    179 readdir_getattr_send(struct puffs_usermount *pu, struct psshfs_node *psn)
    180 {
    181 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    182 	size_t i;
    183 	int rv = 0;
    184 
    185 	for (i = 0; i < psn->nextda; i++)
    186 		SENDCB(psn->da[i].pufbuf, readdir_getattr_resp, psn->da[i].rda);
    187 
    188  out:
    189 	return;
    190 }
    191 #endif
    192 
    193 int
    194 getpathattr(struct puffs_cc *pcc, const char *path, struct vattr *vap)
    195 {
    196 	PSSHFSAUTOVAR(pcc);
    197 
    198 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    199 	GETRESPONSE(pb);
    200 
    201 	rv = psbuf_expect_attrs(pb, vap);
    202 
    203  out:
    204 	PSSHFSRETURN(rv);
    205 }
    206 
    207 int
    208 getnodeattr(struct puffs_cc *pcc, struct puffs_node *pn)
    209 {
    210 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    211 	struct psshfs_node *psn = pn->pn_data;
    212 	struct vattr va;
    213 	int rv;
    214 
    215 	if ((time(NULL) - psn->attrread) >= PSSHFS_REFRESHIVAL) {
    216 		rv = getpathattr(pcc, PNPATH(pn), &va);
    217 		if (rv)
    218 			return rv;
    219 
    220 		/*
    221 		 * Check if the file was modified from below us.  If
    222 		 * so, invalidate page cache.  This is the only sensible
    223 		 * place we can do this in.
    224 		 */
    225 		if (psn->attrread)
    226 			if (pn->pn_va.va_mtime.tv_sec != va.va_mtime.tv_sec)
    227 				puffs_inval_pagecache_node(pu, pn);
    228 
    229 		puffs_setvattr(&pn->pn_va, &va);
    230 		psn->attrread = time(NULL);
    231 	}
    232 
    233 	return 0;
    234 }
    235 
    236 int
    237 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
    238 	struct puffs_node *pn)
    239 {
    240 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    241 	struct psshfs_node *psn = pn->pn_data;
    242 	struct psshfs_dir *olddir, *testd;
    243 	struct puffs_framebuf *pb;
    244 	uint32_t reqid = NEXTREQ(pctx);
    245 	uint32_t count, dhandlen;
    246 	char *dhand = NULL;
    247 	size_t nent;
    248 	char *longname;
    249 	int idx, rv;
    250 
    251 	assert(pn->pn_va.va_type == VDIR);
    252 	idx = 0;
    253 	olddir = psn->dir;
    254 	nent = psn->dentnext;
    255 
    256 	if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
    257 		return 0;
    258 
    259 	puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
    260 
    261 	pb = psbuf_makeout();
    262 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
    263 	GETRESPONSE(pb);
    264 
    265 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    266 	if (rv)
    267 		goto wayout;
    268 
    269 	/*
    270 	 * Well, the following is O(n^2), so feel free to improve if it
    271 	 * gets too taxing on your system.
    272 	 */
    273 
    274 	/*
    275 	 * note: for the "getattr in batch" to work, this must be before
    276 	 * the attribute-getting.  Otherwise times for first entries in
    277 	 * large directories might expire before the directory itself and
    278 	 * result in one-by-one attribute fetching.
    279 	 */
    280 	psn->dentread = time(NULL);
    281 
    282 	psn->dentnext = 0;
    283 	psn->denttot = 0;
    284 	psn->dir = NULL;
    285 #ifdef SUPERREADDIR
    286 	psn->nextda = 0;
    287 #endif /* SUPERREADDIR */
    288 
    289 	for (;;) {
    290 		reqid = NEXTREQ(pctx);
    291 		psbuf_recycleout(pb);
    292 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    293 		GETRESPONSE(pb);
    294 
    295 		/* check for EOF */
    296 		if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
    297 			rv = psbuf_expect_status(pb);
    298 			goto out;
    299 		}
    300 		rv = psbuf_expect_name(pb, &count);
    301 		if (rv)
    302 			goto out;
    303 
    304 		for (; count--; idx++) {
    305 			if (idx == psn->denttot)
    306 				allocdirs(psn);
    307 			if ((rv = psbuf_get_str(pb,
    308 			    &psn->dir[idx].entryname, NULL)))
    309 				goto out;
    310 			if ((rv = psbuf_get_str(pb, &longname, NULL)))
    311 				goto out;
    312 			if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va))) {
    313 				free(longname);
    314 				goto out;
    315 			}
    316 			if (sscanf(longname, "%*s%d",
    317 			    &psn->dir[idx].va.va_nlink) != 1) {
    318 				rv = EPROTO;
    319 				goto out;
    320 			}
    321 			free(longname);
    322 
    323 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    324 			if (testd) {
    325 				psn->dir[idx].entry = testd->entry;
    326 				psn->dir[idx].va = testd->va;
    327 			} else {
    328 				psn->dir[idx].entry = NULL;
    329 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    330 			}
    331 #ifdef SUPERREADDIR
    332 			/*
    333 			 * XXX: there's a dangling pointer race here if
    334 			 * the server responds to our queries out-of-order.
    335 			 * fixxxme some day
    336 			 */
    337 			readdir_getattr(puffs_cc_getusermount(pcc),
    338 			    psn, PNPATH(pn), idx);
    339 #endif
    340 			psn->dir[idx].valid = 1;
    341 		}
    342 	}
    343 
    344  out:
    345 	/* fire off getattr requests */
    346 	readdir_getattr_send(pu, psn);
    347 
    348 	/* XXX: rv */
    349 	psn->dentnext = idx;
    350 	freedircache(olddir, nent);
    351 
    352 	reqid = NEXTREQ(pctx);
    353 	psbuf_recycleout(pb);
    354 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    355 	JUSTSEND(pb);
    356 	free(dhand);
    357 
    358 	return rv;
    359 
    360  wayout:
    361 	free(dhand);
    362 	PSSHFSRETURN(rv);
    363 }
    364 
    365 struct puffs_node *
    366 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    367 	struct psshfs_dir *pd, const struct vattr *vap)
    368 {
    369 	struct psshfs_node *psn_parent = parent->pn_data;
    370 	struct psshfs_node *psn;
    371 	struct puffs_node *pn;
    372 
    373 	psn = emalloc(sizeof(struct psshfs_node));
    374 	memset(psn, 0, sizeof(struct psshfs_node));
    375 
    376 	pn = puffs_pn_new(pu, psn);
    377 	if (!pn) {
    378 		free(psn);
    379 		return NULL;
    380 	}
    381 	puffs_setvattr(&pn->pn_va, &pd->va);
    382 	psn->attrread = pd->attrread;
    383 	puffs_setvattr(&pn->pn_va, vap);
    384 
    385 	pd->entry = pn;
    386 	psn->parent = parent;
    387 	psn_parent->childcount++;
    388 
    389 	return pn;
    390 }
    391 
    392 struct puffs_node *
    393 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    394 	const char *entryname, const struct vattr *vap)
    395 {
    396 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    397 	struct psshfs_dir *pd;
    398 	struct puffs_node *pn;
    399 
    400 	pd = direnter(parent, entryname);
    401 
    402 	pd->va.va_fileid = pctx->nextino++;
    403 	if (vap->va_type == VDIR) {
    404 		pd->va.va_nlink = 2;
    405 		parent->pn_va.va_nlink++;
    406 	} else {
    407 		pd->va.va_nlink = 1;
    408 	}
    409 
    410 	pn = makenode(pu, parent, pd, vap);
    411 	if (pn)
    412 		pd->va.va_fileid = pn->pn_va.va_fileid;
    413 
    414 	return pn;
    415 }
    416 
    417 struct psshfs_dir *
    418 direnter(struct puffs_node *parent, const char *entryname)
    419 {
    420 	struct psshfs_node *psn_parent = parent->pn_data;
    421 	struct psshfs_dir *pd;
    422 	int i;
    423 
    424 	/* create directory entry */
    425 	if (psn_parent->denttot == psn_parent->dentnext)
    426 		allocdirs(psn_parent);
    427 
    428 	i = psn_parent->dentnext;
    429 	pd = &psn_parent->dir[i];
    430 	pd->entryname = estrdup(entryname);
    431 	pd->valid = 1;
    432 	pd->attrread = 0;
    433 	puffs_vattr_null(&pd->va);
    434 	psn_parent->dentnext++;
    435 
    436 	return pd;
    437 }
    438 
    439 void
    440 doreclaim(struct puffs_node *pn)
    441 {
    442 	struct psshfs_node *psn = pn->pn_data;
    443 	struct psshfs_node *psn_parent;
    444 	struct psshfs_dir *dent;
    445 
    446 	psn_parent = psn->parent->pn_data;
    447 	psn_parent->childcount--;
    448 
    449 	/*
    450 	 * Null out entry from directory.  Do not treat a missing entry
    451 	 * as an invariant error, since the node might be removed from
    452 	 * under us, and we might do a readdir before the reclaim resulting
    453 	 * in no directory entry in the parent directory.
    454 	 */
    455 	dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
    456 	if (dent)
    457 		dent->entry = NULL;
    458 
    459 	if (pn->pn_va.va_type == VDIR) {
    460 		freedircache(psn->dir, psn->dentnext);
    461 #ifdef SUPERREADDIR
    462 		free(psn->da);
    463 #endif
    464 	}
    465 
    466 	puffs_pn_put(pn);
    467 }
    468 
    469 void
    470 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
    471 {
    472 	struct psshfs_node *psn, *psn_parent;
    473 	struct psshfs_dir *pd;
    474 
    475 	psn = node->pn_data;
    476 	psn_parent = psn->parent->pn_data;
    477 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    478 	assert(pd != NULL);
    479 	pd->valid = 0;
    480 	free(pd->entryname);
    481 	pd->entryname = NULL;
    482 
    483 	if (node->pn_va.va_type == VDIR)
    484 		psn->parent->pn_va.va_nlink--;
    485 
    486 	if (reclaim)
    487 		doreclaim(node);
    488 }
    489