2007-11-18 00:55:29 +03:00
|
|
|
/* $NetBSD: puffs_subr.c,v 1.60 2007/11/17 21:55:29 pooka Exp $ */
|
2006-10-23 02:43:23 +04:00
|
|
|
|
|
|
|
/*
|
2007-09-28 03:21:07 +04:00
|
|
|
* Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
|
2006-10-23 02:43:23 +04:00
|
|
|
*
|
|
|
|
* Development of this software was supported by the
|
2007-09-28 03:21:07 +04:00
|
|
|
* Ulla Tuominen Foundation and the Finnish Cultural Foundation.
|
2006-10-23 02:43:23 +04:00
|
|
|
*
|
|
|
|
* 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 <sys/cdefs.h>
|
2007-11-18 00:55:29 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.60 2007/11/17 21:55:29 pooka Exp $");
|
2006-10-23 02:43:23 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mount.h>
|
2007-05-18 17:53:08 +04:00
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/poll.h>
|
2007-10-11 00:42:20 +04:00
|
|
|
#include <sys/proc.h>
|
2006-10-23 02:43:23 +04:00
|
|
|
|
|
|
|
#include <fs/puffs/puffs_msgif.h>
|
|
|
|
#include <fs/puffs/puffs_sys.h>
|
|
|
|
|
2007-02-15 15:14:34 +03:00
|
|
|
#ifdef PUFFSDEBUG
|
2006-12-06 02:03:28 +03:00
|
|
|
int puffsdebug;
|
|
|
|
#endif
|
|
|
|
|
2006-10-23 02:43:23 +04:00
|
|
|
void
|
2007-07-01 19:30:15 +04:00
|
|
|
puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr,
|
2007-07-02 02:54:16 +04:00
|
|
|
struct puffs_kcid *pkcid, const struct componentname *cn, int full)
|
2006-10-23 02:43:23 +04:00
|
|
|
{
|
|
|
|
|
2006-12-29 18:38:37 +03:00
|
|
|
pkcn->pkcn_nameiop = cn->cn_nameiop;
|
|
|
|
pkcn->pkcn_flags = cn->cn_flags;
|
2007-07-01 21:22:13 +04:00
|
|
|
puffs_cidcvt(pkcid, cn->cn_lwp);
|
2006-10-23 02:43:23 +04:00
|
|
|
|
2007-07-02 02:54:16 +04:00
|
|
|
if (full) {
|
|
|
|
(void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
|
|
|
|
} else {
|
|
|
|
(void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
|
|
|
|
pkcn->pkcn_name[cn->cn_namelen] = '\0';
|
|
|
|
}
|
2006-12-29 18:38:37 +03:00
|
|
|
pkcn->pkcn_namelen = cn->cn_namelen;
|
2007-07-02 02:54:16 +04:00
|
|
|
pkcn->pkcn_consume = 0;
|
2007-07-01 19:30:15 +04:00
|
|
|
|
|
|
|
puffs_credcvt(pkcr, cn->cn_cred);
|
2006-10-23 02:43:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-07-01 19:30:15 +04:00
|
|
|
* Convert given credentials to struct puffs_kcred for userspace.
|
2006-10-23 02:43:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2007-07-01 19:30:15 +04:00
|
|
|
puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
|
2006-10-23 02:43:23 +04:00
|
|
|
{
|
|
|
|
|
2007-07-01 19:30:15 +04:00
|
|
|
memset(pkcr, 0, sizeof(struct puffs_kcred));
|
2006-10-23 02:43:23 +04:00
|
|
|
|
|
|
|
if (cred == NOCRED || cred == FSCRED) {
|
2007-07-01 19:30:15 +04:00
|
|
|
pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
|
2006-10-23 02:43:23 +04:00
|
|
|
if (cred == NOCRED)
|
2007-07-01 19:30:15 +04:00
|
|
|
pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
|
2006-10-23 02:43:23 +04:00
|
|
|
if (cred == FSCRED)
|
2007-07-01 19:30:15 +04:00
|
|
|
pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
|
2006-10-23 02:43:23 +04:00
|
|
|
} else {
|
2007-07-01 19:30:15 +04:00
|
|
|
pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
|
|
|
|
kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
|
2006-10-23 02:43:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-01 21:22:13 +04:00
|
|
|
void
|
|
|
|
puffs_cidcvt(struct puffs_kcid *pkcid, const struct lwp *l)
|
2006-10-23 02:43:23 +04:00
|
|
|
{
|
|
|
|
|
2007-07-01 21:22:13 +04:00
|
|
|
if (l) {
|
|
|
|
pkcid->pkcid_type = PUFFCID_TYPE_REAL;
|
|
|
|
pkcid->pkcid_pid = l->l_proc->p_pid;
|
|
|
|
pkcid->pkcid_lwpid = l->l_lid;
|
|
|
|
} else {
|
|
|
|
pkcid->pkcid_type = PUFFCID_TYPE_FAKE;
|
|
|
|
pkcid->pkcid_pid = 0;
|
|
|
|
pkcid->pkcid_lwpid = 0;
|
|
|
|
}
|
2006-10-23 02:43:23 +04:00
|
|
|
}
|
2006-11-08 01:10:18 +03:00
|
|
|
|
2007-03-30 21:48:57 +04:00
|
|
|
void
|
2007-10-02 01:09:07 +04:00
|
|
|
puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
|
|
|
|
struct puffs_req *preq, void *arg)
|
2007-03-30 21:48:57 +04:00
|
|
|
{
|
2007-10-11 23:41:13 +04:00
|
|
|
struct puffs_vnmsg_read *read_msg = (void *)preq;
|
2007-03-30 21:48:57 +04:00
|
|
|
struct buf *bp = arg;
|
|
|
|
size_t moved;
|
|
|
|
|
2007-11-17 21:09:04 +03:00
|
|
|
DPRINTF(("%s\n", __func__));
|
|
|
|
|
2007-10-02 01:09:07 +04:00
|
|
|
bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
|
2007-03-30 21:48:57 +04:00
|
|
|
if (bp->b_error == 0) {
|
2007-11-17 21:09:04 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2007-03-30 21:48:57 +04:00
|
|
|
|
2007-11-17 21:09:04 +03:00
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2007-03-30 21:48:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
biodone(bp);
|
|
|
|
}
|
2007-05-01 16:18:40 +04:00
|
|
|
|
2007-08-01 18:20:45 +04:00
|
|
|
/* XXX: userspace can leak kernel resources */
|
2007-05-18 17:53:08 +04:00
|
|
|
void
|
2007-10-02 01:09:07 +04:00
|
|
|
puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
|
2007-05-18 17:53:08 +04:00
|
|
|
{
|
2007-10-11 23:41:13 +04:00
|
|
|
struct puffs_vnmsg_poll *poll_msg = (void *)preq;
|
2007-05-18 17:53:08 +04:00
|
|
|
struct puffs_node *pn = arg;
|
2007-10-02 01:09:07 +04:00
|
|
|
int revents, error;
|
2007-05-18 17:53:08 +04:00
|
|
|
|
2007-10-02 01:09:07 +04:00
|
|
|
error = checkerr(pmp, preq->preq_rv, __func__);
|
|
|
|
if (error)
|
2007-10-11 23:41:13 +04:00
|
|
|
revents = poll_msg->pvnr_events;
|
2007-05-18 17:53:08 +04:00
|
|
|
else
|
|
|
|
revents = POLLERR;
|
|
|
|
|
|
|
|
mutex_enter(&pn->pn_mtx);
|
|
|
|
pn->pn_revents |= revents;
|
|
|
|
mutex_exit(&pn->pn_mtx);
|
|
|
|
|
|
|
|
selnotify(&pn->pn_sel, 0);
|
|
|
|
|
|
|
|
puffs_releasenode(pn);
|
|
|
|
}
|
|
|
|
|
2007-05-01 16:18:40 +04:00
|
|
|
void
|
|
|
|
puffs_mp_reference(struct puffs_mount *pmp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&pmp->pmp_lock));
|
|
|
|
pmp->pmp_refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_mp_release(struct puffs_mount *pmp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&pmp->pmp_lock));
|
|
|
|
if (--pmp->pmp_refcount == 0)
|
|
|
|
cv_broadcast(&pmp->pmp_refcount_cv);
|
|
|
|
}
|
2007-09-28 03:21:07 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
|
|
|
|
*eobp = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_gop_markupdate(struct vnode *vp, int flags)
|
|
|
|
{
|
|
|
|
int uflags = 0;
|
|
|
|
|
|
|
|
if (flags & GOP_UPDATE_ACCESSED)
|
|
|
|
uflags |= PUFFS_UPDATEATIME;
|
|
|
|
if (flags & GOP_UPDATE_MODIFIED)
|
|
|
|
uflags |= PUFFS_UPDATEMTIME;
|
|
|
|
|
2007-11-18 00:55:29 +03:00
|
|
|
puffs_updatenode(VPTOPP(vp), uflags, 0);
|
2007-09-28 03:21:07 +04:00
|
|
|
}
|
2007-11-16 23:32:17 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
puffs_senderr(struct puffs_mount *pmp, int type, int error,
|
|
|
|
const char *str, void *cookie)
|
|
|
|
{
|
|
|
|
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, cookie);
|
|
|
|
|
|
|
|
perr->perr_error = error;
|
|
|
|
strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
|
|
|
|
|
|
|
|
puffs_msg_enqueue(pmp, park);
|
|
|
|
puffs_msgmem_release(park);
|
|
|
|
}
|