/* $NetBSD: puffs_subr.c,v 1.66 2008/11/16 19:34:30 pooka Exp $ */ /* * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved. * * Development of this software was supported by the * Ulla Tuominen Foundation and the Finnish Cultural Foundation. * * 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. */ #include #include #include #include #include #include #include #include #include #include MALLOC_DEFINE(M_PUFFS, "puffs", "PUFFS"); #ifdef PUFFSDEBUG int puffsdebug = 1; #endif void puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr, const struct namecache *ncp, struct ucred *cred) { KKASSERT(ncp->nc_nlen < sizeof(pkcn->pkcn_name)); pkcn->pkcn_namelen = ncp->nc_nlen; strlcpy(pkcn->pkcn_name, ncp->nc_name, sizeof(pkcn->pkcn_name)); puffs_credcvt(pkcr, cred); } /* * Convert given credentials to struct puffs_kcred for userspace. */ void puffs_credcvt(struct puffs_kcred *pkcr, struct ucred *cred) { memset(pkcr, 0, sizeof(struct puffs_kcred)); if (cred == NOCRED || cred == FSCRED) { pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL; if (cred == NOCRED) pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED; if (cred == FSCRED) pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED; } else { pkcr->pkcr_type = PUFFCRED_TYPE_UUC; cru2x(cred, &pkcr->pkcr_uuc); } } void puffs_parkdone_asyncbioread(struct puffs_mount *pmp, struct puffs_req *preq, void *arg) { struct puffs_vnmsg_read *read_msg = (void *)preq; struct buf *bp = arg; size_t moved; DPRINTF(("%s\n", __func__)); bp->b_error = checkerr(pmp, preq->preq_rv, __func__); if (bp->b_error == 0) { if (read_msg->pvnr_resid > bp->b_bcount) { puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG, "resid grew", preq->preq_cookie); bp->b_error = E2BIG; } else { moved = bp->b_bcount - read_msg->pvnr_resid; bp->b_resid = read_msg->pvnr_resid; memcpy(bp->b_data, read_msg->pvnr_data, moved); } } bpdone(bp, 0); } void puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp, struct puffs_req *preq, void *arg) { struct puffs_vnmsg_write *write_msg = (void *)preq; struct buf *bp = arg; DPRINTF(("%s\n", __func__)); bp->b_error = checkerr(pmp, preq->preq_rv, __func__); if (bp->b_error == 0) { if (write_msg->pvnr_resid > bp->b_bcount) { puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG, "resid grew", preq->preq_cookie); bp->b_error = E2BIG; } else { bp->b_resid = write_msg->pvnr_resid; } } bpdone(bp, 0); } /* XXX: userspace can leak kernel resources */ void puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg) { struct puffs_vnmsg_poll *poll_msg = (void *)preq; struct puffs_node *pn = arg; int revents, error; error = checkerr(pmp, preq->preq_rv, __func__); if (error) revents = poll_msg->pvnr_events; else revents = POLLERR; lockmgr(&pn->pn_mtx, LK_EXCLUSIVE); pn->pn_revents |= revents; lockmgr(&pn->pn_mtx, LK_RELEASE); #ifdef XXXDF selnotify(&pn->pn_sel, revents, 0); #endif puffs_releasenode(pn); } void puffs_mp_reference(struct puffs_mount *pmp) { KKASSERT(lockstatus(&pmp->pmp_lock, curthread) == LK_EXCLUSIVE); pmp->pmp_refcount++; } void puffs_mp_release(struct puffs_mount *pmp) { KKASSERT(lockstatus(&pmp->pmp_lock, curthread) == LK_EXCLUSIVE); if (--pmp->pmp_refcount == 0) cv_broadcast(&pmp->pmp_refcount_cv); } void puffs_senderr(struct puffs_mount *pmp, int type, int error, const char *str, puffs_cookie_t ck) { struct puffs_msgpark *park; struct puffs_error *perr; puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void *)&perr, 1); puffs_msg_setfaf(park); puffs_msg_setinfo(park, PUFFSOP_ERROR, type, ck); perr->perr_error = error; strlcpy(perr->perr_str, str, sizeof(perr->perr_str)); puffs_msg_enqueue(pmp, park); puffs_msgmem_release(park); }