Home | History | Annotate | Line # | Download | only in puffs
puffs_subr.c revision 1.64.6.1
      1  1.64.6.1    mjf /*	$NetBSD: puffs_subr.c,v 1.64.6.1 2008/04/03 12:43:00 mjf Exp $	*/
      2       1.1  pooka 
      3       1.1  pooka /*
      4      1.53  pooka  * Copyright (c) 2006, 2007  Antti Kantee.  All Rights Reserved.
      5       1.1  pooka  *
      6       1.1  pooka  * Development of this software was supported by the
      7      1.53  pooka  * Ulla Tuominen Foundation and the Finnish Cultural Foundation.
      8       1.1  pooka  *
      9       1.1  pooka  * Redistribution and use in source and binary forms, with or without
     10       1.1  pooka  * modification, are permitted provided that the following conditions
     11       1.1  pooka  * are met:
     12       1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     13       1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     14       1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  pooka  *    documentation and/or other materials provided with the distribution.
     17       1.1  pooka  *
     18       1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19       1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20       1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21       1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22       1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23       1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24       1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25       1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26       1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1  pooka  * SUCH DAMAGE.
     29       1.1  pooka  */
     30       1.1  pooka 
     31       1.1  pooka #include <sys/cdefs.h>
     32  1.64.6.1    mjf __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.64.6.1 2008/04/03 12:43:00 mjf Exp $");
     33       1.1  pooka 
     34       1.1  pooka #include <sys/param.h>
     35       1.1  pooka #include <sys/malloc.h>
     36       1.1  pooka #include <sys/mount.h>
     37      1.31  pooka #include <sys/namei.h>
     38      1.31  pooka #include <sys/poll.h>
     39      1.56     ad #include <sys/proc.h>
     40       1.1  pooka 
     41       1.1  pooka #include <fs/puffs/puffs_msgif.h>
     42       1.1  pooka #include <fs/puffs/puffs_sys.h>
     43       1.1  pooka 
     44      1.19  pooka #ifdef PUFFSDEBUG
     45      1.10  pooka int puffsdebug;
     46      1.10  pooka #endif
     47      1.10  pooka 
     48       1.1  pooka void
     49      1.36  pooka puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr,
     50      1.62  pooka 	const struct componentname *cn, int full)
     51       1.1  pooka {
     52       1.1  pooka 
     53      1.12  pooka 	pkcn->pkcn_nameiop = cn->cn_nameiop;
     54      1.12  pooka 	pkcn->pkcn_flags = cn->cn_flags;
     55       1.1  pooka 
     56      1.38  pooka 	if (full) {
     57      1.38  pooka 		(void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
     58      1.38  pooka 	} else {
     59      1.38  pooka 		(void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
     60      1.38  pooka 		pkcn->pkcn_name[cn->cn_namelen] = '\0';
     61      1.38  pooka 	}
     62      1.12  pooka 	pkcn->pkcn_namelen = cn->cn_namelen;
     63      1.38  pooka 	pkcn->pkcn_consume = 0;
     64      1.36  pooka 
     65      1.36  pooka 	puffs_credcvt(pkcr, cn->cn_cred);
     66       1.1  pooka }
     67       1.1  pooka 
     68       1.1  pooka /*
     69      1.36  pooka  * Convert given credentials to struct puffs_kcred for userspace.
     70       1.1  pooka  */
     71       1.1  pooka void
     72      1.36  pooka puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
     73       1.1  pooka {
     74       1.1  pooka 
     75      1.36  pooka 	memset(pkcr, 0, sizeof(struct puffs_kcred));
     76       1.1  pooka 
     77       1.1  pooka 	if (cred == NOCRED || cred == FSCRED) {
     78      1.36  pooka 		pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
     79       1.1  pooka 		if (cred == NOCRED)
     80      1.36  pooka 			pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
     81       1.1  pooka 		if (cred == FSCRED)
     82      1.36  pooka 			pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
     83       1.1  pooka  	} else {
     84      1.36  pooka 		pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
     85      1.36  pooka 		kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
     86       1.1  pooka 	}
     87       1.1  pooka }
     88       1.1  pooka 
     89      1.37  pooka void
     90      1.55  pooka puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
     91      1.55  pooka 	struct puffs_req *preq, void *arg)
     92      1.27  pooka {
     93      1.57  pooka 	struct puffs_vnmsg_read *read_msg = (void *)preq;
     94      1.27  pooka 	struct buf *bp = arg;
     95      1.27  pooka 	size_t moved;
     96      1.27  pooka 
     97      1.59  pooka 	DPRINTF(("%s\n", __func__));
     98      1.59  pooka 
     99      1.55  pooka 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    100      1.27  pooka 	if (bp->b_error == 0) {
    101      1.59  pooka 		if (read_msg->pvnr_resid > bp->b_bcount) {
    102      1.59  pooka 			puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG,
    103      1.59  pooka 			    "resid grew", preq->preq_cookie);
    104      1.59  pooka 			bp->b_error = E2BIG;
    105      1.59  pooka 		} else {
    106      1.59  pooka 			moved = bp->b_bcount - read_msg->pvnr_resid;
    107      1.59  pooka 			bp->b_resid = read_msg->pvnr_resid;
    108      1.59  pooka 
    109      1.59  pooka 			memcpy(bp->b_data, read_msg->pvnr_data, moved);
    110      1.59  pooka 		}
    111      1.59  pooka 	}
    112      1.59  pooka 
    113      1.59  pooka 	biodone(bp);
    114      1.59  pooka }
    115      1.59  pooka 
    116      1.59  pooka void
    117      1.59  pooka puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp,
    118      1.59  pooka 	struct puffs_req *preq, void *arg)
    119      1.59  pooka {
    120      1.59  pooka 	struct puffs_vnmsg_write *write_msg = (void *)preq;
    121      1.59  pooka 	struct buf *bp = arg;
    122      1.27  pooka 
    123      1.59  pooka 	DPRINTF(("%s\n", __func__));
    124      1.59  pooka 
    125      1.59  pooka 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    126      1.59  pooka 	if (bp->b_error == 0) {
    127      1.59  pooka 		if (write_msg->pvnr_resid > bp->b_bcount) {
    128      1.59  pooka 			puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG,
    129      1.59  pooka 			    "resid grew", preq->preq_cookie);
    130      1.59  pooka 			bp->b_error = E2BIG;
    131      1.59  pooka 		} else {
    132      1.59  pooka 			bp->b_resid = write_msg->pvnr_resid;
    133      1.59  pooka 		}
    134      1.27  pooka 	}
    135      1.27  pooka 
    136      1.27  pooka 	biodone(bp);
    137      1.27  pooka }
    138      1.28  pooka 
    139      1.44  pooka /* XXX: userspace can leak kernel resources */
    140      1.28  pooka void
    141      1.55  pooka puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
    142      1.31  pooka {
    143      1.57  pooka 	struct puffs_vnmsg_poll *poll_msg = (void *)preq;
    144      1.31  pooka 	struct puffs_node *pn = arg;
    145      1.55  pooka 	int revents, error;
    146      1.31  pooka 
    147      1.55  pooka 	error = checkerr(pmp, preq->preq_rv, __func__);
    148      1.55  pooka 	if (error)
    149      1.57  pooka 		revents = poll_msg->pvnr_events;
    150      1.31  pooka 	else
    151      1.31  pooka 		revents = POLLERR;
    152      1.31  pooka 
    153      1.31  pooka 	mutex_enter(&pn->pn_mtx);
    154      1.31  pooka 	pn->pn_revents |= revents;
    155      1.31  pooka 	mutex_exit(&pn->pn_mtx);
    156      1.31  pooka 
    157  1.64.6.1    mjf 	selnotify(&pn->pn_sel, revents, 0);
    158      1.31  pooka 
    159      1.31  pooka 	puffs_releasenode(pn);
    160      1.31  pooka }
    161      1.31  pooka 
    162      1.31  pooka void
    163      1.28  pooka puffs_mp_reference(struct puffs_mount *pmp)
    164      1.28  pooka {
    165      1.28  pooka 
    166      1.28  pooka 	KASSERT(mutex_owned(&pmp->pmp_lock));
    167      1.28  pooka 	pmp->pmp_refcount++;
    168      1.28  pooka }
    169      1.28  pooka 
    170      1.28  pooka void
    171      1.28  pooka puffs_mp_release(struct puffs_mount *pmp)
    172      1.28  pooka {
    173      1.28  pooka 
    174      1.28  pooka 	KASSERT(mutex_owned(&pmp->pmp_lock));
    175      1.28  pooka 	if (--pmp->pmp_refcount == 0)
    176      1.28  pooka 		cv_broadcast(&pmp->pmp_refcount_cv);
    177      1.28  pooka }
    178      1.53  pooka 
    179      1.53  pooka void
    180      1.53  pooka puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
    181      1.53  pooka 	int flags)
    182      1.53  pooka {
    183      1.53  pooka 
    184      1.53  pooka 	*eobp = size;
    185      1.53  pooka }
    186      1.53  pooka 
    187      1.53  pooka void
    188      1.53  pooka puffs_gop_markupdate(struct vnode *vp, int flags)
    189      1.53  pooka {
    190      1.53  pooka 	int uflags = 0;
    191      1.53  pooka 
    192      1.53  pooka 	if (flags & GOP_UPDATE_ACCESSED)
    193      1.53  pooka 		uflags |= PUFFS_UPDATEATIME;
    194      1.53  pooka 	if (flags & GOP_UPDATE_MODIFIED)
    195      1.53  pooka 		uflags |= PUFFS_UPDATEMTIME;
    196      1.53  pooka 
    197      1.60  pooka 	puffs_updatenode(VPTOPP(vp), uflags, 0);
    198      1.53  pooka }
    199      1.58  pooka 
    200      1.58  pooka void
    201      1.58  pooka puffs_senderr(struct puffs_mount *pmp, int type, int error,
    202      1.64  pooka 	const char *str, puffs_cookie_t ck)
    203      1.58  pooka {
    204      1.58  pooka 	struct puffs_msgpark *park;
    205      1.58  pooka 	struct puffs_error *perr;
    206      1.58  pooka 
    207      1.63  pooka 	puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void *)&perr, 1);
    208      1.58  pooka 	puffs_msg_setfaf(park);
    209      1.64  pooka 	puffs_msg_setinfo(park, PUFFSOP_ERROR, type, ck);
    210      1.58  pooka 
    211      1.58  pooka 	perr->perr_error = error;
    212      1.58  pooka 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    213      1.58  pooka 
    214      1.58  pooka 	puffs_msg_enqueue(pmp, park);
    215      1.58  pooka 	puffs_msgmem_release(park);
    216      1.58  pooka }
    217