Home | History | Annotate | Line # | Download | only in mount_psshfs
subr.c revision 1.27
      1 /*      $NetBSD: subr.c,v 1.27 2007/08/25 09:01:08 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  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #ifndef lint
     30 __RCSID("$NetBSD: subr.c,v 1.27 2007/08/25 09:01:08 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <assert.h>
     34 #include <err.h>
     35 #include <errno.h>
     36 #include <puffs.h>
     37 #include <stdlib.h>
     38 #include <util.h>
     39 
     40 #include "psshfs.h"
     41 #include "sftp_proto.h"
     42 
     43 static void
     44 freedircache(struct psshfs_dir *base, size_t count)
     45 {
     46 	int i;
     47 
     48 	for (i = 0; i < count; i++) {
     49 		free(base[i].entryname);
     50 		base[i].entryname = NULL;
     51 	}
     52 
     53 	free(base);
     54 }
     55 
     56 #define ENTRYCHUNK 16
     57 static void
     58 allocdirs(struct psshfs_node *psn)
     59 {
     60 	size_t oldtot = psn->denttot;
     61 
     62 	psn->denttot += ENTRYCHUNK;
     63 	psn->dir = erealloc(psn->dir,
     64 	    psn->denttot * sizeof(struct psshfs_dir));
     65 	memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
     66 
     67 	psn->da = erealloc(psn->da, psn->denttot * sizeof(struct delayattr));
     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 struct readdirattr {
    105 	struct psshfs_node *psn;
    106 	int idx;
    107 	char entryname[MAXPATHLEN+1];
    108 };
    109 
    110 static void
    111 readdir_getattr_resp(struct puffs_usermount *pu,
    112 	struct puffs_framebuf *pb, void *arg, int error)
    113 {
    114 	struct readdirattr *rda = arg;
    115 	struct psshfs_node *psn = rda->psn;
    116 	struct psshfs_node *psn_targ = NULL;
    117 	struct psshfs_dir *pdir = NULL;
    118 	struct vattr va;
    119 
    120 	/* XXX: this is not enough */
    121 	if (psn->stat & PSN_RECLAIMED)
    122 		goto out;
    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 		psn_targ = pdir->entry->pn_data;
    136 
    137 		puffs_setvattr(&pdir->entry->pn_va, &va);
    138 		psn_targ->attrread = time(NULL);
    139 	} else {
    140 		puffs_setvattr(&pdir->va, &va);
    141 		pdir->attrread = time(NULL);
    142 	}
    143 
    144  out:
    145 	if (psn_targ) {
    146 		psn_targ->getattr_pb = NULL;
    147 		assert(pdir->getattr_pb == NULL);
    148 	} else if (pdir) {
    149 		pdir->getattr_pb = NULL;
    150 	}
    151 
    152 	free(rda);
    153 	puffs_framebuf_destroy(pb);
    154 }
    155 
    156 static void
    157 readdir_getattr(struct puffs_usermount *pu, struct psshfs_node *psn,
    158 	const char *basepath, int idx)
    159 {
    160 	char path[MAXPATHLEN+1];
    161 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    162 	struct psshfs_dir *pdir = psn->dir;
    163 	struct puffs_framebuf *pb;
    164 	struct readdirattr *rda;
    165 	const char *entryname = pdir[idx].entryname;
    166 	uint32_t reqid = NEXTREQ(pctx);
    167 
    168 	rda = emalloc(sizeof(struct readdirattr));
    169 	rda->psn = psn;
    170 	rda->idx = idx;
    171 	strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
    172 
    173 	strcpy(path, basepath);
    174 	strcat(path, "/");
    175 	strlcat(path, entryname, sizeof(path));
    176 
    177 	pb = psbuf_makeout();
    178 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    179 	psn->da[psn->nextda].pufbuf = pb;
    180 	psn->da[psn->nextda].rda = rda;
    181 	psn->nextda++;
    182 }
    183 
    184 static void
    185 readdir_getattr_send(struct puffs_usermount *pu, struct psshfs_node *psn)
    186 {
    187 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    188 	struct psshfs_dir *pdir = psn->dir;
    189 	struct psshfs_node *psn_targ;
    190 	struct readdirattr *rda;
    191 	size_t i;
    192 	int rv = 0;
    193 
    194 	for (i = 0; i < psn->nextda; i++) {
    195 		rda = psn->da[i].rda;
    196 		if (pdir[rda->idx].entry) {
    197 			psn_targ = pdir[rda->idx].entry->pn_data;
    198 			psn_targ->getattr_pb = psn->da[i].pufbuf;
    199 		} else {
    200 			pdir[rda->idx].getattr_pb = psn->da[i].pufbuf;
    201 		}
    202 		SENDCB(psn->da[i].pufbuf, readdir_getattr_resp, rda);
    203 	}
    204 
    205  out:
    206 	return;
    207 }
    208 
    209 int
    210 getpathattr(struct puffs_cc *pcc, const char *path, struct vattr *vap)
    211 {
    212 	PSSHFSAUTOVAR(pcc);
    213 
    214 	psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
    215 	GETRESPONSE(pb);
    216 
    217 	rv = psbuf_expect_attrs(pb, vap);
    218 
    219  out:
    220 	PSSHFSRETURN(rv);
    221 }
    222 
    223 int
    224 getnodeattr(struct puffs_cc *pcc, struct puffs_node *pn)
    225 {
    226 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    227 	struct psshfs_node *psn = pn->pn_data;
    228 	struct vattr va;
    229 	int rv, dohardway;
    230 
    231 	if ((time(NULL) - psn->attrread) >= PSSHFS_REFRESHIVAL) {
    232 		dohardway = 1;
    233 		if (psn->getattr_pb) {
    234 			rv=puffs_framev_framebuf_ccpromote(psn->getattr_pb,pcc);
    235 			if (rv == 0) {
    236 				rv = psbuf_expect_attrs(psn->getattr_pb, &va);
    237 				puffs_framebuf_destroy(psn->getattr_pb);
    238 				psn->getattr_pb = NULL;
    239 				if (rv == 0)
    240 					dohardway = 0;
    241 			}
    242 		}
    243 
    244 		if (dohardway) {
    245 			rv = getpathattr(pcc, PNPATH(pn), &va);
    246 			if (rv)
    247 				return rv;
    248 		}
    249 
    250 		/*
    251 		 * Check if the file was modified from below us.
    252 		 * If so, invalidate page cache.  This is the only
    253 		 * sensible place we can do this in.
    254 		 */
    255 		if (psn->attrread)
    256 			if (pn->pn_va.va_mtime.tv_sec != va.va_mtime.tv_sec)
    257 				puffs_inval_pagecache_node(pu, pn);
    258 
    259 		puffs_setvattr(&pn->pn_va, &va);
    260 		psn->attrread = time(NULL);
    261 	}
    262 
    263 	return 0;
    264 }
    265 
    266 int
    267 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
    268 	struct puffs_node *pn)
    269 {
    270 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    271 	struct psshfs_node *psn = pn->pn_data;
    272 	struct psshfs_dir *olddir, *testd;
    273 	struct puffs_framebuf *pb;
    274 	uint32_t reqid = NEXTREQ(pctx);
    275 	uint32_t count, dhandlen;
    276 	char *dhand = NULL;
    277 	size_t nent;
    278 	char *longname;
    279 	int idx, rv;
    280 
    281 	assert(pn->pn_va.va_type == VDIR);
    282 	idx = 0;
    283 	olddir = psn->dir;
    284 	nent = psn->dentnext;
    285 
    286 	if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
    287 		return 0;
    288 
    289 	if ((rv = puffs_inval_namecache_dir(pu, pn)))
    290 		warn("readdir: dcache inval fail %p", pn);
    291 
    292 	pb = psbuf_makeout();
    293 	psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
    294 	if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb, 0) == -1) {
    295 		rv = errno;
    296 		goto wayout;
    297 	}
    298 	rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
    299 	if (rv)
    300 		goto wayout;
    301 
    302 	/*
    303 	 * Well, the following is O(n^2), so feel free to improve if it
    304 	 * gets too taxing on your system.
    305 	 */
    306 
    307 	/*
    308 	 * note: for the "getattr in batch" to work, this must be before
    309 	 * the attribute-getting.  Otherwise times for first entries in
    310 	 * large directories might expire before the directory itself and
    311 	 * result in one-by-one attribute fetching.
    312 	 */
    313 	psn->dentread = time(NULL);
    314 
    315 	psn->dentnext = 0;
    316 	psn->denttot = 0;
    317 	psn->dir = NULL;
    318 	psn->nextda = 0;
    319 
    320 	for (;;) {
    321 		reqid = NEXTREQ(pctx);
    322 		psbuf_recycleout(pb);
    323 		psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
    324 		GETRESPONSE(pb);
    325 
    326 		/* check for EOF */
    327 		if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
    328 			rv = psbuf_expect_status(pb);
    329 			goto out;
    330 		}
    331 		rv = psbuf_expect_name(pb, &count);
    332 		if (rv)
    333 			goto out;
    334 
    335 		for (; count--; idx++) {
    336 			if (idx == psn->denttot)
    337 				allocdirs(psn);
    338 			if ((rv = psbuf_get_str(pb,
    339 			    &psn->dir[idx].entryname, NULL)))
    340 				goto out;
    341 			if ((rv = psbuf_get_str(pb, &longname, NULL)))
    342 				goto out;
    343 			if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va))) {
    344 				free(longname);
    345 				goto out;
    346 			}
    347 			if (sscanf(longname, "%*s%d",
    348 			    &psn->dir[idx].va.va_nlink) != 1) {
    349 				rv = EPROTO;
    350 				goto out;
    351 			}
    352 			free(longname);
    353 
    354 			testd = lookup(olddir, nent, psn->dir[idx].entryname);
    355 			if (testd) {
    356 				psn->dir[idx].entry = testd->entry;
    357 				psn->dir[idx].va = testd->va;
    358 			} else {
    359 				psn->dir[idx].entry = NULL;
    360 				psn->dir[idx].va.va_fileid = pctx->nextino++;
    361 			}
    362 			/*
    363 			 * XXX: there's a dangling pointer race here if
    364 			 * the server responds to our queries out-of-order.
    365 			 * fixxxme some day
    366 			 */
    367 			readdir_getattr(puffs_cc_getusermount(pcc),
    368 			    psn, PNPATH(pn), idx);
    369 
    370 			psn->dir[idx].valid = 1;
    371 		}
    372 	}
    373 
    374  out:
    375 	/* fire off getattr requests */
    376 	readdir_getattr_send(pu, psn);
    377 
    378 	/* XXX: rv */
    379 	psn->dentnext = idx;
    380 	freedircache(olddir, nent);
    381 
    382 	reqid = NEXTREQ(pctx);
    383 	psbuf_recycleout(pb);
    384 	psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
    385 	JUSTSEND(pb);
    386 	free(dhand);
    387 
    388 	return rv;
    389 
    390  wayout:
    391 	free(dhand);
    392 	PSSHFSRETURN(rv);
    393 }
    394 
    395 struct puffs_node *
    396 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
    397 	struct psshfs_dir *pd, const struct vattr *vap)
    398 {
    399 	struct psshfs_node *psn_parent = parent->pn_data;
    400 	struct psshfs_node *psn;
    401 	struct puffs_node *pn;
    402 
    403 	psn = emalloc(sizeof(struct psshfs_node));
    404 	memset(psn, 0, sizeof(struct psshfs_node));
    405 
    406 	pn = puffs_pn_new(pu, psn);
    407 	if (!pn) {
    408 		free(psn);
    409 		return NULL;
    410 	}
    411 	puffs_setvattr(&pn->pn_va, &pd->va);
    412 	psn->attrread = pd->attrread;
    413 	puffs_setvattr(&pn->pn_va, vap);
    414 
    415 	pd->entry = pn;
    416 	psn->parent = parent;
    417 	psn_parent->childcount++;
    418 
    419 	LIST_INIT(&psn->dw);
    420 
    421 	if (pd->getattr_pb) {
    422 		psn->getattr_pb = pd->getattr_pb;
    423 		pd->getattr_pb = NULL;
    424 	}
    425 
    426 	return pn;
    427 }
    428 
    429 struct puffs_node *
    430 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
    431 	const char *entryname, const struct vattr *vap)
    432 {
    433 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    434 	struct psshfs_dir *pd;
    435 	struct puffs_node *pn;
    436 
    437 	pd = direnter(parent, entryname);
    438 
    439 	pd->va.va_fileid = pctx->nextino++;
    440 	if (vap->va_type == VDIR) {
    441 		pd->va.va_nlink = 2;
    442 		parent->pn_va.va_nlink++;
    443 	} else {
    444 		pd->va.va_nlink = 1;
    445 	}
    446 
    447 	pn = makenode(pu, parent, pd, vap);
    448 	if (pn)
    449 		pd->va.va_fileid = pn->pn_va.va_fileid;
    450 
    451 	return pn;
    452 }
    453 
    454 struct psshfs_dir *
    455 direnter(struct puffs_node *parent, const char *entryname)
    456 {
    457 	struct psshfs_node *psn_parent = parent->pn_data;
    458 	struct psshfs_dir *pd;
    459 	int i;
    460 
    461 	/* create directory entry */
    462 	if (psn_parent->denttot == psn_parent->dentnext)
    463 		allocdirs(psn_parent);
    464 
    465 	i = psn_parent->dentnext;
    466 	pd = &psn_parent->dir[i];
    467 	pd->entryname = estrdup(entryname);
    468 	pd->valid = 1;
    469 	pd->attrread = 0;
    470 	puffs_vattr_null(&pd->va);
    471 	psn_parent->dentnext++;
    472 
    473 	return pd;
    474 }
    475 
    476 void
    477 doreclaim(struct puffs_node *pn)
    478 {
    479 	struct psshfs_node *psn = pn->pn_data;
    480 	struct psshfs_node *psn_parent;
    481 	struct psshfs_dir *dent;
    482 
    483 	psn_parent = psn->parent->pn_data;
    484 	psn_parent->childcount--;
    485 
    486 	/*
    487 	 * Null out entry from directory.  Do not treat a missing entry
    488 	 * as an invariant error, since the node might be removed from
    489 	 * under us, and we might do a readdir before the reclaim resulting
    490 	 * in no directory entry in the parent directory.
    491 	 */
    492 	dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
    493 	if (dent)
    494 		dent->entry = NULL;
    495 
    496 	if (pn->pn_va.va_type == VDIR) {
    497 		freedircache(psn->dir, psn->dentnext);
    498 		psn->denttot = psn->dentnext = 0;
    499 		free(psn->da);
    500 	}
    501 
    502 	puffs_pn_put(pn);
    503 }
    504 
    505 void
    506 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
    507 {
    508 	struct psshfs_node *psn, *psn_parent;
    509 	struct psshfs_dir *pd;
    510 
    511 	psn = node->pn_data;
    512 	psn_parent = psn->parent->pn_data;
    513 	pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
    514 	assert(pd != NULL);
    515 	pd->valid = 0;
    516 	free(pd->entryname);
    517 	pd->entryname = NULL;
    518 
    519 	if (node->pn_va.va_type == VDIR)
    520 		psn->parent->pn_va.va_nlink--;
    521 
    522 	if (reclaim)
    523 		doreclaim(node);
    524 }
    525