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