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