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