/*
* Copyright (c) 2006-2009 Antti Kantee. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
rv = sftp_readdir(pu, pctx, pn_dir);
if (rv) {
if (rv != EPERM)
return rv;
/*
* Can't read the directory. We still might be
* able to find the node with getattr in -r+x dirs
*/
rv = getpathattr(pu, PCNPATH(pcn), &va);
if (rv)
return rv;
/*
* sure sure we have fresh attributes. most likely we will
* have them cached. we might not if we go through:
* create - reclaim - lookup (this).
*/
rv = getnodeattr(pu, pn, PCNPATH(pcn));
if (rv)
return rv;
/*
* If we cached the remote attributes recently enough, and this
* setattr operation would change nothing that sftp actually
* records, then we can skip the sftp request. So first check
* whether we have the attributes cached, and then compare
* every field that we might send to the sftp server.
*/
if (!psn->attrread || REFRESHTIMEOUT(pctx, time(NULL)-psn->attrread))
goto setattr;
#define CHECK(FIELD, TYPE) do { \
if ((va->FIELD != (TYPE)PUFFS_VNOVAL) && \
(va->FIELD != pn->pn_va.FIELD)) \
goto setattr; \
} while (0)
/* Create node on server first */
psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PCNPATH(pcn));
psbuf_put_4(pb, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
psbuf_put_vattr(pb, va, pctx);
GETRESPONSE(pb, pctx->sshfd);
rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
if (rv)
goto out;
/*
* Do *not* create the local node before getting a response
* from the server. Otherwise we might screw up consistency,
* namely that the node can be looked up before create has
* returned (mind you, the kernel will unlock the directory
* before the create call from userspace returns).
*/
pn_new = allocnode(pu, pn, pcn->pcn_name, va);
if (!pn_new) {
struct puffs_framebuf *pb2 = psbuf_makeout();
reqid = NEXTREQ(pctx);
psbuf_req_str(pb2, SSH_FXP_REMOVE, reqid, PCNPATH(pcn));
JUSTSEND(pb2, pctx->sshfd);
rv = ENOMEM;
}
/*
* Open a file handle. This is used for read and write. We do not
* wait here for the success or failure of this operation. This is
* because otherwise opening and closing file handles would block
* reading potentially cached information. Rather, we defer the wait
* to read/write and therefore allow cached access without a wait.
*
* If we have not yet successfully opened a type of handle, we do wait
* here. Also, if a lazy open fails, we revert back to the same
* state of waiting.
*/
int
psshfs_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
const struct puffs_cred *pcr)
{
struct puffs_cc *pcc = puffs_cc_getcc(pu);
struct psshfs_ctx *pctx = puffs_getspecific(pu);
struct puffs_framebuf *pb, *pb2;
struct vattr va;
struct puffs_node *pn = opc;
struct psshfs_node *psn = pn->pn_data;
uint32_t reqid;
int didread, didwrite;
int rv = 0;
/* find next dirent */
for (i = *readoff;;i++) {
if (i >= psn->dentnext)
goto out;
pd = &psn->dir[i];
if (pd->valid)
break;
}
for (;;) {
*readoff = i;
if (!puffs_nextdent(&dent, pd->entryname,
pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen)) {
rv = 0;
goto out;
}
/* find next entry, store possible nfs key */
do {
if (++i >= psn->dentnext)
goto out;
pd = &psn->dir[i];
} while (pd->valid == 0);
PUFFS_STORE_DCOOKIE(cookies, ncookies, (off_t)i);
}
out:
if (rv == 0) {
if (i >= psn->dentnext)
*eofflag = 1;
*readoff = i;
}
if (set_readdir) {
struct psshfs_wait *pw;
/* all will likely run to completion because of cache */
TAILQ_FOREACH(pw, &psn->pw, pw_entries) {
assert(pw->pw_type == PWTYPE_READDIR);
puffs_cc_schedule(pw->pw_cc);
TAILQ_REMOVE(&psn->pw, pw, pw_entries);
}
/* if lazyopening, wait for the result */
if (psn->lazyopen_r) {
psn->stat |= PSN_LAZYWAIT_R;
rv = puffs_framev_framebuf_ccpromote(psn->lazyopen_r, pcc);
lazyopen_rresp(pu, psn->lazyopen_r, psn, rv);
/*
* Do this *after* accessing the file, the handle might not
* exist after blocking.
*/
if (max_reads && ++psn->readcount > max_reads) {
struct psshfs_wait pw;
farout:
/* check if we need a lazyclose */
if (psn->stat & PSN_HANDLECLOSE && psn->fhand_r) {
TAILQ_FOREACH(pwp, &psn->pw, pw_entries)
if (pwp->pw_type == PWTYPE_READ1)
break;
if (pwp == NULL)
closehandles(pu, psn, HANDLE_READ);
}
PSSHFSRETURN(rv);
}
/* XXX: we should getattr for size */
int
psshfs_node_write(struct puffs_usermount *pu, puffs_cookie_t opc, uint8_t *buf,
off_t offset, size_t *resid, const struct puffs_cred *cred,
int ioflag)
{
PSSHFSAUTOVAR(pu);
struct psshfs_wait *pwp;
struct puffs_node *pn = opc;
struct psshfs_node *psn = pn->pn_data;
uint32_t writelen;
/*
* If lazyopening, wait for the result.
* There can still be more than oen writer at a time in case
* the kernel issues write FAFs.
*/
if (psn->lazyopen_w) {
psn->stat |= PSN_LAZYWAIT_W;
rv = puffs_framev_framebuf_ccpromote(psn->lazyopen_w, pcc);
lazyopen_wresp(pu, psn->lazyopen_w, psn, rv);
out:
/* check if we need a lazyclose */
if (psn->stat & PSN_HANDLECLOSE && psn->fhand_w) {
TAILQ_FOREACH(pwp, &psn->pw, pw_entries)
if (pwp->pw_type == PWTYPE_WRITE)
break;
if (pwp == NULL)
closehandles(pu, psn, HANDLE_WRITE);
}
PSSHFSRETURN(rv);
}
/*
* check if we can use a cached version
*
* XXX: we might end up reading the same link multiple times
* from the server if we get many requests at once, but that's
* quite harmless as this routine is reentrant.
*/
if (psn->symlink && !REFRESHTIMEOUT(pctx, time(NULL) - psn->slread))
goto copy;
/*
* XXX: ietf says: source, target. openssh says: ietf who?
* Let's go with openssh and build quirk tables later if we care
*/
psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
psbuf_put_str(pb, PCNPATH(pcn));
GETRESPONSE(pb, pctx->sshfd);
/*
* XXX: interfaces didn't quite work with rename..
* the song remains the same. go figure .. ;)
*/
nukenode(pn_sf, pcn_src->pcn_name, 0);
pd = direnter(pn_td, pcn_targ->pcn_name);
pd->entry = pn_sf;
puffs_setvattr(&pd->va, &pn_sf->pn_va);
if (opc != targ_dir) {
psn_targdir->childcount++;
psn_src->parent = pn_td;
if (pn_sf->pn_va.va_type == VDIR)
pn_td->pn_va.va_nlink++;
}
}
out:
PSSHFSRETURN(rv);
}
/*
* So this file system happened to be written in such a way that
* lookup for ".." is hard if we lose the in-memory node. We'd
* need to recreate the entire directory structure from the root
* node up to the ".." node we're looking up.
*
* And since our entire fs structure is purely fictional (i.e. it's
* only in-memory, not fetchable from the server), the easiest way
* to deal with it is to not allow nodes with children to be
* reclaimed.
*
* If a node with children is being attempted to be reclaimed, we
* just mark it "reclaimed" but leave it as is until all its children
* have been reclaimed. If a lookup for that node is done meanwhile,
* it will be found by lookup() and we just remove the "reclaimed"
* bit.
*/
int
psshfs_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
{
struct puffs_node *pn = opc, *pn_next, *pn_root;
struct psshfs_node *psn = pn->pn_data;
/*
* don't reclaim if we have file handle issued, otherwise
* we can't do fhtonode
*/
if (psn->stat & PSN_HASFH)
return 0;