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