Home | History | Annotate | Line # | Download | only in puffs
puffs_subr.c revision 1.44.4.3
      1  1.44.4.3   matt /*	puffs_subr.c,v 1.44.4.2 2008/01/09 01:55:48 matt Exp	*/
      2       1.1  pooka 
      3       1.1  pooka /*
      4  1.44.4.1   matt  * 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.44.4.1   matt  * 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.44.4.3   matt __KERNEL_RCSID(0, "puffs_subr.c,v 1.44.4.2 2008/01/09 01:55:48 matt 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.40     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.44.4.2   matt 	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.44.4.2   matt puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
     91  1.44.4.2   matt 	struct puffs_req *preq, void *arg)
     92       1.1  pooka {
     93  1.44.4.2   matt 	struct puffs_vnmsg_read *read_msg = (void *)preq;
     94  1.44.4.2   matt 	struct buf *bp = arg;
     95  1.44.4.2   matt 	size_t moved;
     96       1.1  pooka 
     97  1.44.4.2   matt 	DPRINTF(("%s\n", __func__));
     98  1.44.4.2   matt 
     99  1.44.4.2   matt 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    100  1.44.4.2   matt 	if (bp->b_error == 0) {
    101  1.44.4.2   matt 		if (read_msg->pvnr_resid > bp->b_bcount) {
    102  1.44.4.2   matt 			puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG,
    103  1.44.4.2   matt 			    "resid grew", preq->preq_cookie);
    104  1.44.4.2   matt 			bp->b_error = E2BIG;
    105  1.44.4.2   matt 		} else {
    106  1.44.4.2   matt 			moved = bp->b_bcount - read_msg->pvnr_resid;
    107  1.44.4.2   matt 			bp->b_resid = read_msg->pvnr_resid;
    108  1.44.4.2   matt 
    109  1.44.4.2   matt 			memcpy(bp->b_data, read_msg->pvnr_data, moved);
    110  1.44.4.2   matt 		}
    111      1.37  pooka 	}
    112  1.44.4.2   matt 
    113  1.44.4.2   matt 	biodone(bp);
    114       1.1  pooka }
    115       1.7  pooka 
    116       1.7  pooka void
    117  1.44.4.2   matt puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp,
    118  1.44.4.1   matt 	struct puffs_req *preq, void *arg)
    119       1.7  pooka {
    120  1.44.4.2   matt 	struct puffs_vnmsg_write *write_msg = (void *)preq;
    121      1.27  pooka 	struct buf *bp = arg;
    122  1.44.4.2   matt 
    123  1.44.4.2   matt 	DPRINTF(("%s\n", __func__));
    124      1.27  pooka 
    125  1.44.4.1   matt 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    126      1.27  pooka 	if (bp->b_error == 0) {
    127  1.44.4.2   matt 		if (write_msg->pvnr_resid > bp->b_bcount) {
    128  1.44.4.2   matt 			puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG,
    129  1.44.4.2   matt 			    "resid grew", preq->preq_cookie);
    130  1.44.4.2   matt 			bp->b_error = E2BIG;
    131  1.44.4.2   matt 		} else {
    132  1.44.4.2   matt 			bp->b_resid = write_msg->pvnr_resid;
    133  1.44.4.2   matt 		}
    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.44.4.1   matt puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
    142      1.31  pooka {
    143  1.44.4.1   matt 	struct puffs_vnmsg_poll *poll_msg = (void *)preq;
    144      1.31  pooka 	struct puffs_node *pn = arg;
    145  1.44.4.1   matt 	int revents, error;
    146      1.31  pooka 
    147  1.44.4.1   matt 	error = checkerr(pmp, preq->preq_rv, __func__);
    148  1.44.4.1   matt 	if (error)
    149  1.44.4.1   matt 		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.44.4.3   matt 	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.44.4.1   matt 
    179  1.44.4.1   matt void
    180  1.44.4.1   matt puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
    181  1.44.4.1   matt 	int flags)
    182  1.44.4.1   matt {
    183  1.44.4.1   matt 
    184  1.44.4.1   matt 	*eobp = size;
    185  1.44.4.1   matt }
    186  1.44.4.1   matt 
    187  1.44.4.1   matt void
    188  1.44.4.1   matt puffs_gop_markupdate(struct vnode *vp, int flags)
    189  1.44.4.1   matt {
    190  1.44.4.1   matt 	int uflags = 0;
    191  1.44.4.1   matt 
    192  1.44.4.1   matt 	if (flags & GOP_UPDATE_ACCESSED)
    193  1.44.4.1   matt 		uflags |= PUFFS_UPDATEATIME;
    194  1.44.4.1   matt 	if (flags & GOP_UPDATE_MODIFIED)
    195  1.44.4.1   matt 		uflags |= PUFFS_UPDATEMTIME;
    196  1.44.4.1   matt 
    197  1.44.4.2   matt 	puffs_updatenode(VPTOPP(vp), uflags, 0);
    198  1.44.4.2   matt }
    199  1.44.4.2   matt 
    200  1.44.4.2   matt void
    201  1.44.4.2   matt puffs_senderr(struct puffs_mount *pmp, int type, int error,
    202  1.44.4.3   matt 	const char *str, puffs_cookie_t ck)
    203  1.44.4.2   matt {
    204  1.44.4.2   matt 	struct puffs_msgpark *park;
    205  1.44.4.2   matt 	struct puffs_error *perr;
    206  1.44.4.2   matt 
    207  1.44.4.2   matt 	puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void *)&perr, 1);
    208  1.44.4.2   matt 	puffs_msg_setfaf(park);
    209  1.44.4.3   matt 	puffs_msg_setinfo(park, PUFFSOP_ERROR, type, ck);
    210  1.44.4.2   matt 
    211  1.44.4.2   matt 	perr->perr_error = error;
    212  1.44.4.2   matt 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    213  1.44.4.2   matt 
    214  1.44.4.2   matt 	puffs_msg_enqueue(pmp, park);
    215  1.44.4.2   matt 	puffs_msgmem_release(park);
    216  1.44.4.1   matt }
    217