puffs_node.c revision 1.32 1 /* $NetBSD: puffs_node.c,v 1.32 2014/08/28 08:29:50 hannken Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Google Summer of Code program, the Ulla Tuominen Foundation
8 * and the Finnish Cultural Foundation.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_node.c,v 1.32 2014/08/28 08:29:50 hannken Exp $");
34
35 #include <sys/param.h>
36 #include <sys/hash.h>
37 #include <sys/kmem.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42
43 #include <uvm/uvm.h>
44
45 #include <fs/puffs/puffs_msgif.h>
46 #include <fs/puffs/puffs_sys.h>
47
48 #include <miscfs/genfs/genfs_node.h>
49 #include <miscfs/specfs/specdev.h>
50
51 struct pool puffs_pnpool;
52 struct pool puffs_vapool;
53
54 /*
55 * Grab a vnode, intialize all the puffs-dependent stuff.
56 */
57 static int
58 puffs_getvnode1(struct mount *mp, puffs_cookie_t ck, enum vtype type,
59 voff_t vsize, dev_t rdev, bool may_exist, struct vnode **vpp)
60 {
61 struct puffs_mount *pmp;
62 struct vnode *vp;
63 struct puffs_node *pnode;
64 int error;
65
66 pmp = MPTOPUFFSMP(mp);
67
68 if (type <= VNON || type >= VBAD) {
69 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
70 "bad node type", ck);
71 return EPROTO;
72 }
73 if (vsize == VSIZENOTSET) {
74 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
75 "VSIZENOTSET is not a valid size", ck);
76 return EPROTO;
77 }
78
79 for (;;) {
80 error = vcache_get(mp, &ck, sizeof(ck), &vp);
81 if (error)
82 return error;
83 mutex_enter(vp->v_interlock);
84 pnode = VPTOPP(vp);
85 if (pnode != NULL)
86 break;
87 mutex_exit(vp->v_interlock);
88 vrele(vp);
89 }
90 mutex_enter(&pnode->pn_mtx);
91 mutex_exit(vp->v_interlock);
92
93 /*
94 * Release and error out if caller wants a fresh vnode.
95 */
96 if (vp->v_type != VNON && ! may_exist) {
97 mutex_exit(&pnode->pn_mtx);
98 vrele(vp);
99 return EEXIST;
100 }
101
102 *vpp = vp;
103
104 /*
105 * If fully initialized were done.
106 */
107 if (vp->v_type != VNON) {
108 mutex_exit(&pnode->pn_mtx);
109 return 0;
110 }
111
112 /*
113 * Set type and finalize the initialisation.
114 */
115 vp->v_type = type;
116 if (type == VCHR || type == VBLK) {
117 vp->v_op = puffs_specop_p;
118 spec_node_init(vp, rdev);
119 } else if (type == VFIFO) {
120 vp->v_op = puffs_fifoop_p;
121 } else if (vp->v_type == VREG) {
122 uvm_vnp_setsize(vp, vsize);
123 }
124
125 pnode->pn_serversize = vsize;
126
127 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
128 pnode, pnode->pn_cookie));
129
130 mutex_exit(&pnode->pn_mtx);
131
132 return 0;
133 }
134
135 int
136 puffs_getvnode(struct mount *mp, puffs_cookie_t ck, enum vtype type,
137 voff_t vsize, dev_t rdev, struct vnode **vpp)
138 {
139
140 return puffs_getvnode1(mp, ck, type, vsize, rdev, true, vpp);
141 }
142
143 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
144 int
145 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
146 puffs_cookie_t ck, struct componentname *cnp,
147 enum vtype type, dev_t rdev)
148 {
149 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
150 int error;
151
152 /* userspace probably has this as a NULL op */
153 if (ck == NULL)
154 return EOPNOTSUPP;
155
156 /*
157 * Check for previous node with the same designation.
158 * Explicitly check the root node cookie, since it might be
159 * reclaimed from the kernel when this check is made.
160 */
161 if (ck == pmp->pmp_root_cookie) {
162 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EEXIST,
163 "cookie exists", ck);
164 return EPROTO;
165 }
166
167 KASSERT(curlwp != uvm.pagedaemon_lwp);
168
169 error = puffs_getvnode1(dvp->v_mount, ck, type, 0, rdev, false, vpp);
170 if (error) {
171 if (error == EEXIST) {
172 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EEXIST,
173 "cookie exists", ck);
174 error = EPROTO;
175 }
176 return error;
177 }
178
179 if (PUFFS_USE_NAMECACHE(pmp))
180 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
181 cnp->cn_flags);
182
183 return 0;
184 }
185
186 void
187 puffs_putvnode(struct vnode *vp)
188 {
189 struct puffs_node *pnode;
190
191 pnode = VPTOPP(vp);
192
193 KASSERT(vp->v_tag == VT_PUFFS);
194
195 vcache_remove(vp->v_mount, &pnode->pn_cookie, sizeof(pnode->pn_cookie));
196 genfs_node_destroy(vp);
197
198 /*
199 * To interlock with puffs_getvnode1().
200 */
201 mutex_enter(vp->v_interlock);
202 vp->v_data = NULL;
203 mutex_exit(vp->v_interlock);
204 puffs_releasenode(pnode);
205 }
206
207 /*
208 * Make sure root vnode exists and reference it. Does NOT lock.
209 */
210 static int
211 puffs_makeroot(struct puffs_mount *pmp)
212 {
213 struct vnode *vp;
214 int rv;
215
216 /*
217 * pmp_lock must be held if vref()'ing or vrele()'ing the
218 * root vnode. the latter is controlled by puffs_inactive().
219 *
220 * pmp_root is set here and cleared in puffs_reclaim().
221 */
222
223 rv = puffs_getvnode(pmp->pmp_mp, pmp->pmp_root_cookie,
224 pmp->pmp_root_vtype, pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp);
225 if (rv != 0)
226 return rv;
227
228 mutex_enter(&pmp->pmp_lock);
229 if (pmp->pmp_root == NULL)
230 pmp->pmp_root = vp;
231 mutex_exit(&pmp->pmp_lock);
232
233 return 0;
234 }
235
236 /*
237 * Locate the in-kernel vnode based on the cookie received given
238 * from userspace.
239 *
240 * returns 0 on success. otherwise returns an errno or PUFFS_NOSUCHCOOKIE.
241 *
242 * returns PUFFS_NOSUCHCOOKIE if no vnode for the cookie is found.
243 */
244 int
245 puffs_cookie2vnode(struct puffs_mount *pmp, puffs_cookie_t ck,
246 struct vnode **vpp)
247 {
248 int rv;
249
250 /*
251 * Handle root in a special manner, since we want to make sure
252 * pmp_root is properly set.
253 */
254 if (ck == pmp->pmp_root_cookie) {
255 if ((rv = puffs_makeroot(pmp)))
256 return rv;
257 *vpp = pmp->pmp_root;
258 return 0;
259 }
260
261 rv = vcache_get(PMPTOMP(pmp), ck, sizeof(ck), vpp);
262 if (rv != 0)
263 return rv;
264 mutex_enter((*vpp)->v_interlock);
265 if ((*vpp)->v_type == VNON) {
266 mutex_exit((*vpp)->v_interlock);
267 vrele(*vpp);
268 *vpp = NULL;
269 return PUFFS_NOSUCHCOOKIE;
270 }
271
272 return 0;
273 }
274
275 void
276 puffs_updatenode(struct puffs_node *pn, int flags, voff_t size)
277 {
278 struct timespec ts;
279
280 if (flags == 0)
281 return;
282
283 nanotime(&ts);
284
285 if (flags & PUFFS_UPDATEATIME) {
286 pn->pn_mc_atime = ts;
287 pn->pn_stat |= PNODE_METACACHE_ATIME;
288 }
289 if (flags & PUFFS_UPDATECTIME) {
290 pn->pn_mc_ctime = ts;
291 pn->pn_stat |= PNODE_METACACHE_CTIME;
292 }
293 if (flags & PUFFS_UPDATEMTIME) {
294 pn->pn_mc_mtime = ts;
295 pn->pn_stat |= PNODE_METACACHE_MTIME;
296 }
297 if (flags & PUFFS_UPDATESIZE) {
298 pn->pn_mc_size = size;
299 pn->pn_stat |= PNODE_METACACHE_SIZE;
300 }
301 }
302
303 /*
304 * Add reference to node.
305 * mutex held on entry and return
306 */
307 void
308 puffs_referencenode(struct puffs_node *pn)
309 {
310
311 KASSERT(mutex_owned(&pn->pn_mtx));
312 pn->pn_refcount++;
313 }
314
315 /*
316 * Release pnode structure which dealing with references to the
317 * puffs_node instead of the vnode. Can't use vref()/vrele() on
318 * the vnode there, since that causes the lovely VOP_INACTIVE(),
319 * which in turn causes the lovely deadlock when called by the one
320 * who is supposed to handle it.
321 */
322 void
323 puffs_releasenode(struct puffs_node *pn)
324 {
325
326 mutex_enter(&pn->pn_mtx);
327 if (--pn->pn_refcount == 0) {
328 mutex_exit(&pn->pn_mtx);
329 mutex_destroy(&pn->pn_mtx);
330 mutex_destroy(&pn->pn_sizemtx);
331 seldestroy(&pn->pn_sel);
332 if (pn->pn_va_cache != NULL)
333 pool_put(&puffs_vapool, pn->pn_va_cache);
334 pool_put(&puffs_pnpool, pn);
335 } else {
336 mutex_exit(&pn->pn_mtx);
337 }
338 }
339