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