puffs_subr.c revision 1.2 1 /* $NetBSD: puffs_subr.c,v 1.2 2006/10/23 23:32:57 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Google Summer of Code program and the Ulla Tuominen Foundation.
8 * The Google SoC project was mentored by Bill Studenmund.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.2 2006/10/23 23:32:57 pooka Exp $");
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/socketvar.h>
43 #include <sys/vnode.h>
44 #include <sys/kauth.h>
45 #include <sys/namei.h>
46
47 #include <fs/puffs/puffs_msgif.h>
48 #include <fs/puffs/puffs_sys.h>
49
50 POOL_INIT(puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0, "puffspnpl",
51 &pool_allocator_nointr);
52
53 /*
54 * Grab a vnode, intialize all the puffs-dependant stuff.
55 */
56 int
57 puffs_getvnode(struct mount *mp, void *cookie, struct vnode **vpp)
58 {
59 struct puffs_mount *pmp;
60 struct vnode *vp;
61 struct puffs_node *pnode;
62 int error;
63
64 pmp = MPTOPUFFSMP(mp);
65
66 /*
67 * XXX: there is a deadlock condition between vfs_busy() and
68 * vnode locks. For an unmounting file system the mountpoint
69 * is frozen, but in unmount(FORCE) vflush() wants to access all
70 * of the vnodes. If we are here waiting for the mountpoint
71 * lock while holding on to a vnode lock, well, we ain't
72 * just pining for the fjords anymore. If we release the
73 * vnode lock, we will be in the situation "mount point
74 * is dying" and panic() will ensue in insmntque. So as a
75 * temporary workaround, get a vnode without putting it on
76 * the mount point list, check if mount point is still alive
77 * and kicking and only then add the vnode to the list.
78 */
79 error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
80 if (error)
81 return error;
82 vp->v_vnlock = NULL;
83
84 /*
85 * Check what mount point isn't going away. This will work
86 * until we decide to remove biglock or make the kernel
87 * preemptive. But hopefully the real problem will be fixed
88 * by then.
89 *
90 * XXX: yes, should call vfs_busy(), but thar be rabbits with
91 * vicious streaks a mile wide ...
92 */
93 if (mp->mnt_iflag & IMNT_UNMOUNT) {
94 DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
95 "vnode for cookie %p\n", mp, cookie));
96 ungetnewvnode(vp);
97 return ENXIO;
98 }
99
100 /* So it's not dead yet.. good.. inform new vnode of its master */
101 simple_lock(&mntvnode_slock);
102 if (TAILQ_EMPTY(&mp->mnt_vnodelist))
103 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
104 else
105 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
106 simple_unlock(&mntvnode_slock);
107 vp->v_mount = mp;
108
109 /* handle clerical tasks & footwork */
110 pnode = pool_get(&puffs_pnpool, PR_WAITOK);
111 pnode->pn_cookie = cookie;
112 pnode->pn_stat = 0;
113 LIST_INSERT_HEAD(&pmp->pmp_pnodelist, pnode, pn_entries);
114 vp->v_data = pnode;
115 pnode->pn_vp = vp;
116
117 *vpp = vp;
118
119 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
120 pnode, pnode->pn_cookie));
121
122 return 0;
123 }
124
125 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
126 int
127 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
128 void *cookie, struct componentname *cnp, enum vtype type)
129 {
130 struct vnode *vp;
131 int error;
132
133 /* userspace probably has this as a NULL op */
134 if (cookie == NULL) {
135 error = EOPNOTSUPP;
136 goto out;
137 }
138
139 error = puffs_getvnode(dvp->v_mount, cookie, &vp);
140 if (error)
141 goto out;
142
143 vp->v_type = type;
144 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
145 *vpp = vp;
146
147 out:
148 if (error || (cnp->cn_flags & SAVESTART) == 0)
149 PNBUF_PUT(cnp->cn_pnbuf);
150 vput(dvp);
151
152 return error;
153 }
154
155 void
156 puffs_putvnode(struct vnode *vp)
157 {
158 struct puffs_mount *pmp;
159 struct puffs_node *pnode;
160
161 pmp = VPTOPUFFSMP(vp);
162 pnode = VPTOPP(vp);
163
164 #ifdef DIAGNOSTIC
165 if (vp->v_tag != VT_PUFFS)
166 panic("puffs_putvnode: %p not a puffs vnode", vp);
167 #endif
168
169 LIST_REMOVE(pnode, pn_entries);
170 pool_put(&puffs_pnpool, vp->v_data);
171 vp->v_data = NULL;
172
173 return;
174 }
175
176 /*
177 * Locate the in-kernel vnode based on the cookie received given
178 * from userspace. Returns a locked & referenced vnode, if found,
179 * NULL otherwise.
180 *
181 * XXX: lists, although lookup cache mostly shields us from this
182 */
183 struct vnode *
184 puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie)
185 {
186 struct puffs_node *pnode;
187 struct vnode *vp;
188
189 simple_lock(&pmp->pmp_lock);
190 LIST_FOREACH(pnode, &pmp->pmp_pnodelist, pn_entries) {
191 if (pnode->pn_cookie == cookie)
192 break;
193 }
194 simple_unlock(&pmp->pmp_lock);
195 if (!pnode)
196 return NULL;
197 vp = pnode->pn_vp;
198
199 if (pnode->pn_stat & PNODE_INACTIVE) {
200 if (vget(vp, LK_EXCLUSIVE | LK_RETRY))
201 return NULL;
202 } else {
203 vref(vp);
204 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
205 }
206 return vp;
207 }
208
209 void
210 puffs_makecn(struct puffs_cn *pcn, const struct componentname *cn)
211 {
212
213 pcn->pcn_nameiop = cn->cn_nameiop;
214 pcn->pcn_flags = cn->cn_flags;
215 pcn->pcn_pid = cn->cn_lwp->l_proc->p_pid;
216 puffs_credcvt(&pcn->pcn_cred, cn->cn_cred);
217
218 (void)memcpy(&pcn->pcn_name, cn->cn_nameptr, cn->cn_namelen);
219 pcn->pcn_name[cn->cn_namelen] = '\0';
220 pcn->pcn_namelen = cn->cn_namelen;
221 }
222
223 /*
224 * Convert given credentials to struct puffs_cred for userspace.
225 */
226 void
227 puffs_credcvt(struct puffs_cred *pcr, const kauth_cred_t cred)
228 {
229
230 memset(pcr, 0, sizeof(struct puffs_cred));
231
232 if (cred == NOCRED || cred == FSCRED) {
233 pcr->pcr_type = PUFFCRED_TYPE_INTERNAL;
234 if (cred == NOCRED)
235 pcr->pcr_internal = PUFFCRED_CRED_NOCRED;
236 if (cred == FSCRED)
237 pcr->pcr_internal = PUFFCRED_CRED_FSCRED;
238 } else {
239 pcr->pcr_type = PUFFCRED_TYPE_UUC;
240 kauth_cred_to_uucred(&pcr->pcr_uuc, cred);
241 }
242 }
243
244 /*
245 * Return pid. In case the operation is coming from within the
246 * kernel without any process context, borrow the swapper's pid.
247 */
248 pid_t
249 puffs_lwp2pid(struct lwp *l)
250 {
251
252 return l ? l->l_proc->p_pid : 0;
253 }
254