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