Home | History | Annotate | Line # | Download | only in puffs
puffs_subr.c revision 1.13.2.7
      1  1.13.2.7  yamt /*	$NetBSD: puffs_subr.c,v 1.13.2.7 2008/01/21 09:45:51 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.7  yamt __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.13.2.7 2008/01/21 09:45:51 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.7  yamt 	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.2  yamt 
     56  1.13.2.4  yamt 	if (full) {
     57  1.13.2.4  yamt 		(void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
     58  1.13.2.4  yamt 	} else {
     59  1.13.2.4  yamt 		(void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
     60  1.13.2.4  yamt 		pkcn->pkcn_name[cn->cn_namelen] = '\0';
     61  1.13.2.4  yamt 	}
     62  1.13.2.2  yamt 	pkcn->pkcn_namelen = cn->cn_namelen;
     63  1.13.2.4  yamt 	pkcn->pkcn_consume = 0;
     64  1.13.2.4  yamt 
     65  1.13.2.4  yamt 	puffs_credcvt(pkcr, cn->cn_cred);
     66  1.13.2.2  yamt }
     67  1.13.2.2  yamt 
     68  1.13.2.2  yamt /*
     69  1.13.2.4  yamt  * Convert given credentials to struct puffs_kcred for userspace.
     70  1.13.2.2  yamt  */
     71  1.13.2.2  yamt void
     72  1.13.2.4  yamt puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
     73  1.13.2.2  yamt {
     74  1.13.2.2  yamt 
     75  1.13.2.4  yamt 	memset(pkcr, 0, sizeof(struct puffs_kcred));
     76  1.13.2.2  yamt 
     77  1.13.2.2  yamt 	if (cred == NOCRED || cred == FSCRED) {
     78  1.13.2.4  yamt 		pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
     79  1.13.2.2  yamt 		if (cred == NOCRED)
     80  1.13.2.4  yamt 			pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
     81  1.13.2.2  yamt 		if (cred == FSCRED)
     82  1.13.2.4  yamt 			pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
     83  1.13.2.2  yamt  	} else {
     84  1.13.2.4  yamt 		pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
     85  1.13.2.4  yamt 		kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
     86  1.13.2.2  yamt 	}
     87  1.13.2.2  yamt }
     88  1.13.2.2  yamt 
     89  1.13.2.4  yamt void
     90  1.13.2.5  yamt puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
     91  1.13.2.5  yamt 	struct puffs_req *preq, void *arg)
     92  1.13.2.2  yamt {
     93  1.13.2.5  yamt 	struct puffs_vnmsg_read *read_msg = (void *)preq;
     94  1.13.2.4  yamt 	struct buf *bp = arg;
     95  1.13.2.4  yamt 	size_t moved;
     96  1.13.2.4  yamt 
     97  1.13.2.6  yamt 	DPRINTF(("%s\n", __func__));
     98  1.13.2.6  yamt 
     99  1.13.2.5  yamt 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    100  1.13.2.4  yamt 	if (bp->b_error == 0) {
    101  1.13.2.6  yamt 		if (read_msg->pvnr_resid > bp->b_bcount) {
    102  1.13.2.6  yamt 			puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG,
    103  1.13.2.6  yamt 			    "resid grew", preq->preq_cookie);
    104  1.13.2.6  yamt 			bp->b_error = E2BIG;
    105  1.13.2.6  yamt 		} else {
    106  1.13.2.6  yamt 			moved = bp->b_bcount - read_msg->pvnr_resid;
    107  1.13.2.6  yamt 			bp->b_resid = read_msg->pvnr_resid;
    108  1.13.2.2  yamt 
    109  1.13.2.6  yamt 			memcpy(bp->b_data, read_msg->pvnr_data, moved);
    110  1.13.2.6  yamt 		}
    111  1.13.2.6  yamt 	}
    112  1.13.2.6  yamt 
    113  1.13.2.6  yamt 	biodone(bp);
    114  1.13.2.6  yamt }
    115  1.13.2.6  yamt 
    116  1.13.2.6  yamt void
    117  1.13.2.6  yamt puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp,
    118  1.13.2.6  yamt 	struct puffs_req *preq, void *arg)
    119  1.13.2.6  yamt {
    120  1.13.2.6  yamt 	struct puffs_vnmsg_write *write_msg = (void *)preq;
    121  1.13.2.6  yamt 	struct buf *bp = arg;
    122  1.13.2.6  yamt 
    123  1.13.2.6  yamt 	DPRINTF(("%s\n", __func__));
    124  1.13.2.6  yamt 
    125  1.13.2.6  yamt 	bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
    126  1.13.2.6  yamt 	if (bp->b_error == 0) {
    127  1.13.2.6  yamt 		if (write_msg->pvnr_resid > bp->b_bcount) {
    128  1.13.2.6  yamt 			puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG,
    129  1.13.2.6  yamt 			    "resid grew", preq->preq_cookie);
    130  1.13.2.6  yamt 			bp->b_error = E2BIG;
    131  1.13.2.6  yamt 		} else {
    132  1.13.2.6  yamt 			bp->b_resid = write_msg->pvnr_resid;
    133  1.13.2.6  yamt 		}
    134  1.13.2.2  yamt 	}
    135  1.13.2.2  yamt 
    136  1.13.2.4  yamt 	biodone(bp);
    137  1.13.2.4  yamt }
    138  1.13.2.4  yamt 
    139  1.13.2.4  yamt /* XXX: userspace can leak kernel resources */
    140  1.13.2.4  yamt void
    141  1.13.2.5  yamt puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
    142  1.13.2.4  yamt {
    143  1.13.2.5  yamt 	struct puffs_vnmsg_poll *poll_msg = (void *)preq;
    144  1.13.2.4  yamt 	struct puffs_node *pn = arg;
    145  1.13.2.5  yamt 	int revents, error;
    146  1.13.2.4  yamt 
    147  1.13.2.5  yamt 	error = checkerr(pmp, preq->preq_rv, __func__);
    148  1.13.2.5  yamt 	if (error)
    149  1.13.2.5  yamt 		revents = poll_msg->pvnr_events;
    150  1.13.2.4  yamt 	else
    151  1.13.2.4  yamt 		revents = POLLERR;
    152  1.13.2.4  yamt 
    153  1.13.2.4  yamt 	mutex_enter(&pn->pn_mtx);
    154  1.13.2.4  yamt 	pn->pn_revents |= revents;
    155  1.13.2.4  yamt 	mutex_exit(&pn->pn_mtx);
    156  1.13.2.4  yamt 
    157  1.13.2.4  yamt 	selnotify(&pn->pn_sel, 0);
    158  1.13.2.4  yamt 
    159  1.13.2.4  yamt 	puffs_releasenode(pn);
    160  1.13.2.4  yamt }
    161  1.13.2.4  yamt 
    162  1.13.2.4  yamt void
    163  1.13.2.4  yamt puffs_mp_reference(struct puffs_mount *pmp)
    164  1.13.2.4  yamt {
    165  1.13.2.4  yamt 
    166  1.13.2.4  yamt 	KASSERT(mutex_owned(&pmp->pmp_lock));
    167  1.13.2.4  yamt 	pmp->pmp_refcount++;
    168  1.13.2.4  yamt }
    169  1.13.2.4  yamt 
    170  1.13.2.4  yamt void
    171  1.13.2.4  yamt puffs_mp_release(struct puffs_mount *pmp)
    172  1.13.2.4  yamt {
    173  1.13.2.4  yamt 
    174  1.13.2.4  yamt 	KASSERT(mutex_owned(&pmp->pmp_lock));
    175  1.13.2.4  yamt 	if (--pmp->pmp_refcount == 0)
    176  1.13.2.4  yamt 		cv_broadcast(&pmp->pmp_refcount_cv);
    177  1.13.2.2  yamt }
    178  1.13.2.5  yamt 
    179  1.13.2.5  yamt void
    180  1.13.2.5  yamt puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
    181  1.13.2.5  yamt 	int flags)
    182  1.13.2.5  yamt {
    183  1.13.2.5  yamt 
    184  1.13.2.5  yamt 	*eobp = size;
    185  1.13.2.5  yamt }
    186  1.13.2.5  yamt 
    187  1.13.2.5  yamt void
    188  1.13.2.5  yamt puffs_gop_markupdate(struct vnode *vp, int flags)
    189  1.13.2.5  yamt {
    190  1.13.2.5  yamt 	int uflags = 0;
    191  1.13.2.5  yamt 
    192  1.13.2.5  yamt 	if (flags & GOP_UPDATE_ACCESSED)
    193  1.13.2.5  yamt 		uflags |= PUFFS_UPDATEATIME;
    194  1.13.2.5  yamt 	if (flags & GOP_UPDATE_MODIFIED)
    195  1.13.2.5  yamt 		uflags |= PUFFS_UPDATEMTIME;
    196  1.13.2.5  yamt 
    197  1.13.2.6  yamt 	puffs_updatenode(VPTOPP(vp), uflags, 0);
    198  1.13.2.6  yamt }
    199  1.13.2.6  yamt 
    200  1.13.2.6  yamt void
    201  1.13.2.6  yamt puffs_senderr(struct puffs_mount *pmp, int type, int error,
    202  1.13.2.6  yamt 	const char *str, void *cookie)
    203  1.13.2.6  yamt {
    204  1.13.2.6  yamt 	struct puffs_msgpark *park;
    205  1.13.2.6  yamt 	struct puffs_error *perr;
    206  1.13.2.6  yamt 
    207  1.13.2.7  yamt 	puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void *)&perr, 1);
    208  1.13.2.6  yamt 	puffs_msg_setfaf(park);
    209  1.13.2.6  yamt 	puffs_msg_setinfo(park, PUFFSOP_ERROR, type, cookie);
    210  1.13.2.6  yamt 
    211  1.13.2.6  yamt 	perr->perr_error = error;
    212  1.13.2.6  yamt 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    213  1.13.2.6  yamt 
    214  1.13.2.6  yamt 	puffs_msg_enqueue(pmp, park);
    215  1.13.2.6  yamt 	puffs_msgmem_release(park);
    216  1.13.2.5  yamt }
    217