puffs_subr.c revision 1.61 1 /* $NetBSD: puffs_subr.c,v 1.61 2007/12/08 19:29:44 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Ulla Tuominen Foundation and the Finnish Cultural Foundation.
8 *
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.
17 *
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.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.61 2007/12/08 19:29:44 pooka Exp $");
33
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
38 #include <sys/poll.h>
39 #include <sys/proc.h>
40
41 #include <fs/puffs/puffs_msgif.h>
42 #include <fs/puffs/puffs_sys.h>
43
44 #ifdef PUFFSDEBUG
45 int puffsdebug;
46 #endif
47
48 void
49 puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr,
50 struct puffs_kcid *pkcid, const struct componentname *cn, int full)
51 {
52
53 pkcn->pkcn_nameiop = cn->cn_nameiop;
54 pkcn->pkcn_flags = cn->cn_flags;
55
56 if (full) {
57 (void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
58 } else {
59 (void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
60 pkcn->pkcn_name[cn->cn_namelen] = '\0';
61 }
62 pkcn->pkcn_namelen = cn->cn_namelen;
63 pkcn->pkcn_consume = 0;
64
65 puffs_credcvt(pkcr, cn->cn_cred);
66 }
67
68 /*
69 * Convert given credentials to struct puffs_kcred for userspace.
70 */
71 void
72 puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
73 {
74
75 memset(pkcr, 0, sizeof(struct puffs_kcred));
76
77 if (cred == NOCRED || cred == FSCRED) {
78 pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
79 if (cred == NOCRED)
80 pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
81 if (cred == FSCRED)
82 pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
83 } else {
84 pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
85 kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
86 }
87 }
88
89 void
90 puffs_cidcvt(struct puffs_kcid *pkcid, const struct lwp *l)
91 {
92
93 if (l) {
94 pkcid->pkcid_type = PUFFCID_TYPE_REAL;
95 pkcid->pkcid_pid = l->l_proc->p_pid;
96 pkcid->pkcid_lwpid = l->l_lid;
97 } else {
98 pkcid->pkcid_type = PUFFCID_TYPE_FAKE;
99 pkcid->pkcid_pid = 0;
100 pkcid->pkcid_lwpid = 0;
101 }
102 }
103
104 void
105 puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
106 struct puffs_req *preq, void *arg)
107 {
108 struct puffs_vnmsg_read *read_msg = (void *)preq;
109 struct buf *bp = arg;
110 size_t moved;
111
112 DPRINTF(("%s\n", __func__));
113
114 bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
115 if (bp->b_error == 0) {
116 if (read_msg->pvnr_resid > bp->b_bcount) {
117 puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG,
118 "resid grew", preq->preq_cookie);
119 bp->b_error = E2BIG;
120 } else {
121 moved = bp->b_bcount - read_msg->pvnr_resid;
122 bp->b_resid = read_msg->pvnr_resid;
123
124 memcpy(bp->b_data, read_msg->pvnr_data, moved);
125 }
126 }
127
128 biodone(bp);
129 }
130
131 void
132 puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp,
133 struct puffs_req *preq, void *arg)
134 {
135 struct puffs_vnmsg_write *write_msg = (void *)preq;
136 struct buf *bp = arg;
137
138 DPRINTF(("%s\n", __func__));
139
140 bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
141 if (bp->b_error == 0) {
142 if (write_msg->pvnr_resid > bp->b_bcount) {
143 puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG,
144 "resid grew", preq->preq_cookie);
145 bp->b_error = E2BIG;
146 } else {
147 bp->b_resid = write_msg->pvnr_resid;
148 }
149 }
150
151 biodone(bp);
152 }
153
154 /* XXX: userspace can leak kernel resources */
155 void
156 puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
157 {
158 struct puffs_vnmsg_poll *poll_msg = (void *)preq;
159 struct puffs_node *pn = arg;
160 int revents, error;
161
162 error = checkerr(pmp, preq->preq_rv, __func__);
163 if (error)
164 revents = poll_msg->pvnr_events;
165 else
166 revents = POLLERR;
167
168 mutex_enter(&pn->pn_mtx);
169 pn->pn_revents |= revents;
170 mutex_exit(&pn->pn_mtx);
171
172 selnotify(&pn->pn_sel, 0);
173
174 puffs_releasenode(pn);
175 }
176
177 void
178 puffs_mp_reference(struct puffs_mount *pmp)
179 {
180
181 KASSERT(mutex_owned(&pmp->pmp_lock));
182 pmp->pmp_refcount++;
183 }
184
185 void
186 puffs_mp_release(struct puffs_mount *pmp)
187 {
188
189 KASSERT(mutex_owned(&pmp->pmp_lock));
190 if (--pmp->pmp_refcount == 0)
191 cv_broadcast(&pmp->pmp_refcount_cv);
192 }
193
194 void
195 puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
196 int flags)
197 {
198
199 *eobp = size;
200 }
201
202 void
203 puffs_gop_markupdate(struct vnode *vp, int flags)
204 {
205 int uflags = 0;
206
207 if (flags & GOP_UPDATE_ACCESSED)
208 uflags |= PUFFS_UPDATEATIME;
209 if (flags & GOP_UPDATE_MODIFIED)
210 uflags |= PUFFS_UPDATEMTIME;
211
212 puffs_updatenode(VPTOPP(vp), uflags, 0);
213 }
214
215 void
216 puffs_senderr(struct puffs_mount *pmp, int type, int error,
217 const char *str, void *cookie)
218 {
219 struct puffs_msgpark *park;
220 struct puffs_error *perr;
221
222 puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void**)&perr, 1);
223 puffs_msg_setfaf(park);
224 puffs_msg_setinfo(park, PUFFSOP_ERROR, type, cookie);
225
226 perr->perr_error = error;
227 strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
228
229 puffs_msg_enqueue(pmp, park);
230 puffs_msgmem_release(park);
231 }
232