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