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