puffs_node.c revision 1.4.6.3 1 1.4.6.3 ad /* $NetBSD: puffs_node.c,v 1.4.6.3 2007/10/09 15:22:16 ad Exp $ */
2 1.4.6.2 ad
3 1.4.6.2 ad /*
4 1.4.6.2 ad * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 1.4.6.2 ad *
6 1.4.6.2 ad * Development of this software was supported by the
7 1.4.6.2 ad * Google Summer of Code program, the Ulla Tuominen Foundation
8 1.4.6.2 ad * and the Finnish Cultural Foundation.
9 1.4.6.2 ad *
10 1.4.6.2 ad * Redistribution and use in source and binary forms, with or without
11 1.4.6.2 ad * modification, are permitted provided that the following conditions
12 1.4.6.2 ad * are met:
13 1.4.6.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.4.6.2 ad * notice, this list of conditions and the following disclaimer.
15 1.4.6.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.6.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.4.6.2 ad * documentation and/or other materials provided with the distribution.
18 1.4.6.2 ad *
19 1.4.6.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.4.6.2 ad * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.4.6.2 ad * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.4.6.2 ad * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.4.6.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.4.6.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.4.6.2 ad * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.4.6.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.4.6.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.4.6.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.4.6.2 ad * SUCH DAMAGE.
30 1.4.6.2 ad */
31 1.4.6.2 ad
32 1.4.6.2 ad #include <sys/cdefs.h>
33 1.4.6.3 ad __KERNEL_RCSID(0, "$NetBSD: puffs_node.c,v 1.4.6.3 2007/10/09 15:22:16 ad Exp $");
34 1.4.6.2 ad
35 1.4.6.2 ad #include <sys/param.h>
36 1.4.6.2 ad #include <sys/hash.h>
37 1.4.6.2 ad #include <sys/kmem.h>
38 1.4.6.2 ad #include <sys/malloc.h>
39 1.4.6.2 ad #include <sys/mount.h>
40 1.4.6.2 ad #include <sys/namei.h>
41 1.4.6.2 ad #include <sys/vnode.h>
42 1.4.6.2 ad
43 1.4.6.2 ad #include <fs/puffs/puffs_msgif.h>
44 1.4.6.2 ad #include <fs/puffs/puffs_sys.h>
45 1.4.6.2 ad
46 1.4.6.2 ad #include <miscfs/genfs/genfs_node.h>
47 1.4.6.2 ad #include <miscfs/specfs/specdev.h>
48 1.4.6.2 ad
49 1.4.6.2 ad static const struct genfs_ops puffs_genfsops = {
50 1.4.6.2 ad .gop_size = puffs_gop_size,
51 1.4.6.2 ad .gop_write = genfs_gop_write,
52 1.4.6.2 ad .gop_markupdate = puffs_gop_markupdate,
53 1.4.6.2 ad #if 0
54 1.4.6.2 ad .gop_alloc, should ask userspace
55 1.4.6.2 ad #endif
56 1.4.6.2 ad };
57 1.4.6.2 ad
58 1.4.6.2 ad static __inline struct puffs_node_hashlist
59 1.4.6.2 ad *puffs_cookie2hashlist(struct puffs_mount *, void *);
60 1.4.6.2 ad static struct puffs_node *puffs_cookie2pnode(struct puffs_mount *, void *);
61 1.4.6.2 ad
62 1.4.6.2 ad struct pool puffs_pnpool;
63 1.4.6.2 ad
64 1.4.6.2 ad /*
65 1.4.6.2 ad * Grab a vnode, intialize all the puffs-dependant stuff.
66 1.4.6.2 ad */
67 1.4.6.2 ad int
68 1.4.6.2 ad puffs_getvnode(struct mount *mp, void *cookie, enum vtype type,
69 1.4.6.2 ad voff_t vsize, dev_t rdev, struct vnode **vpp)
70 1.4.6.2 ad {
71 1.4.6.2 ad struct puffs_mount *pmp;
72 1.4.6.2 ad struct puffs_newcookie *pnc;
73 1.4.6.2 ad struct vnode *vp, *nvp;
74 1.4.6.2 ad struct puffs_node *pnode;
75 1.4.6.2 ad struct puffs_node_hashlist *plist;
76 1.4.6.2 ad int error;
77 1.4.6.2 ad
78 1.4.6.2 ad pmp = MPTOPUFFSMP(mp);
79 1.4.6.2 ad
80 1.4.6.2 ad error = EPROTO;
81 1.4.6.2 ad if (type <= VNON || type >= VBAD) {
82 1.4.6.2 ad puffs_errnotify(pmp, PUFFS_ERR_MAKENODE, EINVAL,
83 1.4.6.2 ad "bad node type", cookie);
84 1.4.6.2 ad goto bad;
85 1.4.6.2 ad }
86 1.4.6.2 ad if (vsize == VSIZENOTSET) {
87 1.4.6.2 ad puffs_errnotify(pmp, PUFFS_ERR_MAKENODE, EINVAL,
88 1.4.6.2 ad "VSIZENOTSET is not a valid size", cookie);
89 1.4.6.2 ad goto bad;
90 1.4.6.2 ad }
91 1.4.6.2 ad
92 1.4.6.2 ad /*
93 1.4.6.2 ad * XXX: there is a deadlock condition between vfs_busy() and
94 1.4.6.2 ad * vnode locks. For an unmounting file system the mountpoint
95 1.4.6.2 ad * is frozen, but in unmount(FORCE) vflush() wants to access all
96 1.4.6.2 ad * of the vnodes. If we are here waiting for the mountpoint
97 1.4.6.2 ad * lock while holding on to a vnode lock, well, we ain't
98 1.4.6.2 ad * just pining for the fjords anymore. If we release the
99 1.4.6.2 ad * vnode lock, we will be in the situation "mount point
100 1.4.6.2 ad * is dying" and panic() will ensue in insmntque. So as a
101 1.4.6.2 ad * temporary workaround, get a vnode without putting it on
102 1.4.6.2 ad * the mount point list, check if mount point is still alive
103 1.4.6.2 ad * and kicking and only then add the vnode to the list.
104 1.4.6.2 ad */
105 1.4.6.2 ad error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
106 1.4.6.2 ad if (error)
107 1.4.6.2 ad goto bad;
108 1.4.6.2 ad vp->v_vnlock = NULL;
109 1.4.6.2 ad vp->v_type = type;
110 1.4.6.2 ad
111 1.4.6.2 ad /*
112 1.4.6.2 ad * Check what mount point isn't going away. This will work
113 1.4.6.2 ad * until we decide to remove biglock or make the kernel
114 1.4.6.2 ad * preemptive. But hopefully the real problem will be fixed
115 1.4.6.2 ad * by then.
116 1.4.6.2 ad *
117 1.4.6.2 ad * XXX: yes, should call vfs_busy(), but thar be rabbits with
118 1.4.6.2 ad * vicious streaks a mile wide ...
119 1.4.6.2 ad *
120 1.4.6.2 ad * XXX: there is a transient failure here: if someone is unmounting
121 1.4.6.2 ad * the file system but doesn't succeed (due to it being busy),
122 1.4.6.2 ad * we incorrectly fail new vnode allocation. This is *very*
123 1.4.6.2 ad * hard to fix with the current structure of file system unmounting.
124 1.4.6.2 ad */
125 1.4.6.2 ad if (mp->mnt_iflag & IMNT_UNMOUNT) {
126 1.4.6.2 ad DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
127 1.4.6.2 ad "vnode for cookie %p\n", mp, cookie));
128 1.4.6.2 ad ungetnewvnode(vp);
129 1.4.6.2 ad error = ENXIO;
130 1.4.6.2 ad goto bad;
131 1.4.6.2 ad }
132 1.4.6.2 ad
133 1.4.6.2 ad /*
134 1.4.6.2 ad * Creation should not fail after this point. Or if it does,
135 1.4.6.2 ad * care must be taken so that VOP_INACTIVE() isn't called.
136 1.4.6.2 ad */
137 1.4.6.2 ad
138 1.4.6.2 ad /* So mp is not dead yet.. good.. inform new vnode of its master */
139 1.4.6.3 ad mutex_enter(&mntvnode_lock);
140 1.4.6.2 ad TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
141 1.4.6.3 ad mutex_exit(&mntvnode_lock);
142 1.4.6.2 ad vp->v_mount = mp;
143 1.4.6.2 ad
144 1.4.6.2 ad /*
145 1.4.6.2 ad * clerical tasks & footwork
146 1.4.6.2 ad */
147 1.4.6.2 ad
148 1.4.6.2 ad /* default size */
149 1.4.6.2 ad uvm_vnp_setsize(vp, 0);
150 1.4.6.2 ad
151 1.4.6.2 ad /* dances based on vnode type. almost ufs_vinit(), but not quite */
152 1.4.6.2 ad switch (type) {
153 1.4.6.2 ad case VCHR:
154 1.4.6.2 ad case VBLK:
155 1.4.6.2 ad /*
156 1.4.6.2 ad * replace vnode operation vector with the specops vector.
157 1.4.6.2 ad * our user server has very little control over the node
158 1.4.6.2 ad * if it decides its a character or block special file
159 1.4.6.2 ad */
160 1.4.6.2 ad vp->v_op = puffs_specop_p;
161 1.4.6.2 ad
162 1.4.6.2 ad /* do the standard checkalias-dance */
163 1.4.6.2 ad if ((nvp = checkalias(vp, rdev, mp)) != NULL) {
164 1.4.6.2 ad /*
165 1.4.6.2 ad * found: release & unallocate aliased
166 1.4.6.2 ad * old (well, actually, new) node
167 1.4.6.2 ad */
168 1.4.6.2 ad vp->v_op = spec_vnodeop_p;
169 1.4.6.3 ad vp->v_iflag &= ~VI_LOCKSWORK;
170 1.4.6.2 ad vgone(vp); /* cya */
171 1.4.6.2 ad
172 1.4.6.2 ad /* init "new" vnode */
173 1.4.6.2 ad vp = nvp;
174 1.4.6.2 ad vp->v_vnlock = NULL;
175 1.4.6.2 ad vp->v_mount = mp;
176 1.4.6.2 ad }
177 1.4.6.2 ad break;
178 1.4.6.2 ad
179 1.4.6.2 ad case VFIFO:
180 1.4.6.2 ad vp->v_op = puffs_fifoop_p;
181 1.4.6.2 ad break;
182 1.4.6.2 ad
183 1.4.6.2 ad case VREG:
184 1.4.6.2 ad uvm_vnp_setsize(vp, vsize);
185 1.4.6.2 ad break;
186 1.4.6.2 ad
187 1.4.6.2 ad case VDIR:
188 1.4.6.2 ad case VLNK:
189 1.4.6.2 ad case VSOCK:
190 1.4.6.2 ad break;
191 1.4.6.2 ad default:
192 1.4.6.2 ad panic("puffs_getvnode: invalid vtype %d", type);
193 1.4.6.2 ad }
194 1.4.6.2 ad
195 1.4.6.2 ad pnode = pool_get(&puffs_pnpool, PR_WAITOK);
196 1.4.6.2 ad memset(pnode, 0, sizeof(struct puffs_node));
197 1.4.6.2 ad
198 1.4.6.2 ad pnode->pn_cookie = cookie;
199 1.4.6.2 ad pnode->pn_refcount = 1;
200 1.4.6.2 ad
201 1.4.6.2 ad /* insert cookie on list, take off of interlock list */
202 1.4.6.2 ad mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
203 1.4.6.2 ad SLIST_INIT(&pnode->pn_sel.sel_klist);
204 1.4.6.2 ad plist = puffs_cookie2hashlist(pmp, cookie);
205 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
206 1.4.6.2 ad LIST_INSERT_HEAD(plist, pnode, pn_hashent);
207 1.4.6.2 ad if (cookie != pmp->pmp_root_cookie) {
208 1.4.6.2 ad LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
209 1.4.6.2 ad if (pnc->pnc_cookie == cookie) {
210 1.4.6.2 ad LIST_REMOVE(pnc, pnc_entries);
211 1.4.6.2 ad kmem_free(pnc, sizeof(struct puffs_newcookie));
212 1.4.6.2 ad break;
213 1.4.6.2 ad }
214 1.4.6.2 ad }
215 1.4.6.2 ad KASSERT(pnc != NULL);
216 1.4.6.2 ad }
217 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
218 1.4.6.2 ad
219 1.4.6.2 ad vp->v_data = pnode;
220 1.4.6.2 ad vp->v_type = type;
221 1.4.6.2 ad pnode->pn_vp = vp;
222 1.4.6.2 ad pnode->pn_serversize = vsize;
223 1.4.6.2 ad
224 1.4.6.2 ad genfs_node_init(vp, &puffs_genfsops);
225 1.4.6.2 ad *vpp = vp;
226 1.4.6.2 ad
227 1.4.6.2 ad DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
228 1.4.6.2 ad pnode, pnode->pn_cookie));
229 1.4.6.2 ad
230 1.4.6.2 ad return 0;
231 1.4.6.2 ad
232 1.4.6.2 ad bad:
233 1.4.6.2 ad /* remove staging cookie from list */
234 1.4.6.2 ad if (cookie != pmp->pmp_root_cookie) {
235 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
236 1.4.6.2 ad LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
237 1.4.6.2 ad if (pnc->pnc_cookie == cookie) {
238 1.4.6.2 ad LIST_REMOVE(pnc, pnc_entries);
239 1.4.6.2 ad kmem_free(pnc, sizeof(struct puffs_newcookie));
240 1.4.6.2 ad break;
241 1.4.6.2 ad }
242 1.4.6.2 ad }
243 1.4.6.2 ad KASSERT(pnc != NULL);
244 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
245 1.4.6.2 ad }
246 1.4.6.2 ad
247 1.4.6.2 ad return error;
248 1.4.6.2 ad }
249 1.4.6.2 ad
250 1.4.6.2 ad /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
251 1.4.6.2 ad int
252 1.4.6.2 ad puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
253 1.4.6.2 ad void *cookie, struct componentname *cnp, enum vtype type, dev_t rdev)
254 1.4.6.2 ad {
255 1.4.6.2 ad struct puffs_mount *pmp = MPTOPUFFSMP(mp);
256 1.4.6.2 ad struct puffs_newcookie *pnc;
257 1.4.6.2 ad struct vnode *vp;
258 1.4.6.2 ad int error;
259 1.4.6.2 ad
260 1.4.6.2 ad /* userspace probably has this as a NULL op */
261 1.4.6.2 ad if (cookie == NULL) {
262 1.4.6.2 ad error = EOPNOTSUPP;
263 1.4.6.2 ad return error;
264 1.4.6.2 ad }
265 1.4.6.2 ad
266 1.4.6.2 ad /*
267 1.4.6.2 ad * Check for previous node with the same designation.
268 1.4.6.2 ad * Explicitly check the root node cookie, since it might be
269 1.4.6.2 ad * reclaimed from the kernel when this check is made.
270 1.4.6.2 ad */
271 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
272 1.4.6.2 ad if (cookie == pmp->pmp_root_cookie
273 1.4.6.2 ad || puffs_cookie2pnode(pmp, cookie) != NULL) {
274 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
275 1.4.6.2 ad puffs_errnotify(pmp, PUFFS_ERR_MAKENODE, EEXIST,
276 1.4.6.2 ad "cookie exists", cookie);
277 1.4.6.2 ad return EPROTO;
278 1.4.6.2 ad }
279 1.4.6.2 ad
280 1.4.6.2 ad LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
281 1.4.6.2 ad if (pnc->pnc_cookie == cookie) {
282 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
283 1.4.6.2 ad puffs_errnotify(pmp, PUFFS_ERR_MAKENODE, EEXIST,
284 1.4.6.2 ad "cookie exists", cookie);
285 1.4.6.2 ad return EPROTO;
286 1.4.6.2 ad }
287 1.4.6.2 ad }
288 1.4.6.2 ad pnc = kmem_alloc(sizeof(struct puffs_newcookie), KM_SLEEP);
289 1.4.6.2 ad pnc->pnc_cookie = cookie;
290 1.4.6.2 ad LIST_INSERT_HEAD(&pmp->pmp_newcookie, pnc, pnc_entries);
291 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
292 1.4.6.2 ad
293 1.4.6.2 ad error = puffs_getvnode(dvp->v_mount, cookie, type, 0, rdev, &vp);
294 1.4.6.2 ad if (error)
295 1.4.6.2 ad return error;
296 1.4.6.2 ad
297 1.4.6.2 ad vp->v_type = type;
298 1.4.6.2 ad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
299 1.4.6.2 ad *vpp = vp;
300 1.4.6.2 ad
301 1.4.6.2 ad if ((cnp->cn_flags & MAKEENTRY) && PUFFS_USE_NAMECACHE(pmp))
302 1.4.6.2 ad cache_enter(dvp, vp, cnp);
303 1.4.6.2 ad
304 1.4.6.2 ad return 0;
305 1.4.6.2 ad }
306 1.4.6.2 ad
307 1.4.6.2 ad void
308 1.4.6.2 ad puffs_putvnode(struct vnode *vp)
309 1.4.6.2 ad {
310 1.4.6.2 ad struct puffs_mount *pmp;
311 1.4.6.2 ad struct puffs_node *pnode;
312 1.4.6.2 ad
313 1.4.6.2 ad pmp = VPTOPUFFSMP(vp);
314 1.4.6.2 ad pnode = VPTOPP(vp);
315 1.4.6.2 ad
316 1.4.6.2 ad #ifdef DIAGNOSTIC
317 1.4.6.2 ad if (vp->v_tag != VT_PUFFS)
318 1.4.6.2 ad panic("puffs_putvnode: %p not a puffs vnode", vp);
319 1.4.6.2 ad #endif
320 1.4.6.2 ad
321 1.4.6.2 ad LIST_REMOVE(pnode, pn_hashent);
322 1.4.6.2 ad genfs_node_destroy(vp);
323 1.4.6.2 ad puffs_releasenode(pnode);
324 1.4.6.2 ad vp->v_data = NULL;
325 1.4.6.2 ad
326 1.4.6.2 ad return;
327 1.4.6.2 ad }
328 1.4.6.2 ad
329 1.4.6.2 ad static __inline struct puffs_node_hashlist *
330 1.4.6.2 ad puffs_cookie2hashlist(struct puffs_mount *pmp, void *cookie)
331 1.4.6.2 ad {
332 1.4.6.2 ad uint32_t hash;
333 1.4.6.2 ad
334 1.4.6.2 ad hash = hash32_buf(&cookie, sizeof(void *), HASH32_BUF_INIT);
335 1.4.6.2 ad return &pmp->pmp_pnodehash[hash % pmp->pmp_npnodehash];
336 1.4.6.2 ad }
337 1.4.6.2 ad
338 1.4.6.2 ad /*
339 1.4.6.2 ad * Translate cookie to puffs_node. Caller must hold pmp_lock
340 1.4.6.2 ad * and it will be held upon return.
341 1.4.6.2 ad */
342 1.4.6.2 ad static struct puffs_node *
343 1.4.6.2 ad puffs_cookie2pnode(struct puffs_mount *pmp, void *cookie)
344 1.4.6.2 ad {
345 1.4.6.2 ad struct puffs_node_hashlist *plist;
346 1.4.6.2 ad struct puffs_node *pnode;
347 1.4.6.2 ad
348 1.4.6.2 ad plist = puffs_cookie2hashlist(pmp, cookie);
349 1.4.6.2 ad LIST_FOREACH(pnode, plist, pn_hashent) {
350 1.4.6.2 ad if (pnode->pn_cookie == cookie)
351 1.4.6.2 ad break;
352 1.4.6.2 ad }
353 1.4.6.2 ad
354 1.4.6.2 ad return pnode;
355 1.4.6.2 ad }
356 1.4.6.2 ad
357 1.4.6.2 ad /*
358 1.4.6.2 ad * Make sure root vnode exists and reference it. Does NOT lock.
359 1.4.6.2 ad */
360 1.4.6.2 ad static int
361 1.4.6.2 ad puffs_makeroot(struct puffs_mount *pmp)
362 1.4.6.2 ad {
363 1.4.6.2 ad struct vnode *vp;
364 1.4.6.2 ad int rv;
365 1.4.6.2 ad
366 1.4.6.2 ad /*
367 1.4.6.2 ad * pmp_lock must be held if vref()'ing or vrele()'ing the
368 1.4.6.2 ad * root vnode. the latter is controlled by puffs_inactive().
369 1.4.6.2 ad *
370 1.4.6.2 ad * pmp_root is set here and cleared in puffs_reclaim().
371 1.4.6.2 ad */
372 1.4.6.2 ad retry:
373 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
374 1.4.6.2 ad vp = pmp->pmp_root;
375 1.4.6.2 ad if (vp) {
376 1.4.6.3 ad mutex_enter(&vp->v_interlock);
377 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
378 1.4.6.2 ad if (vget(vp, LK_INTERLOCK) == 0)
379 1.4.6.2 ad return 0;
380 1.4.6.2 ad } else
381 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
382 1.4.6.2 ad
383 1.4.6.2 ad /*
384 1.4.6.2 ad * So, didn't have the magic root vnode available.
385 1.4.6.2 ad * No matter, grab another and stuff it with the cookie.
386 1.4.6.2 ad */
387 1.4.6.2 ad if ((rv = puffs_getvnode(pmp->pmp_mp, pmp->pmp_root_cookie,
388 1.4.6.2 ad pmp->pmp_root_vtype, pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp)))
389 1.4.6.2 ad return rv;
390 1.4.6.2 ad
391 1.4.6.2 ad /*
392 1.4.6.2 ad * Someone magically managed to race us into puffs_getvnode?
393 1.4.6.2 ad * Put our previous new vnode back and retry.
394 1.4.6.2 ad */
395 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
396 1.4.6.2 ad if (pmp->pmp_root) {
397 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
398 1.4.6.2 ad puffs_putvnode(vp);
399 1.4.6.2 ad goto retry;
400 1.4.6.2 ad }
401 1.4.6.2 ad
402 1.4.6.2 ad /* store cache */
403 1.4.6.3 ad vp->v_vflag |= VV_ROOT;
404 1.4.6.2 ad pmp->pmp_root = vp;
405 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
406 1.4.6.2 ad
407 1.4.6.2 ad return 0;
408 1.4.6.2 ad }
409 1.4.6.2 ad
410 1.4.6.2 ad /*
411 1.4.6.2 ad * Locate the in-kernel vnode based on the cookie received given
412 1.4.6.2 ad * from userspace. Returns a vnode, if found, NULL otherwise.
413 1.4.6.2 ad * The parameter "lock" control whether to lock the possible or
414 1.4.6.2 ad * not. Locking always might cause us to lock against ourselves
415 1.4.6.2 ad * in situations where we want the vnode but don't care for the
416 1.4.6.2 ad * vnode lock, e.g. file server issued putpages.
417 1.4.6.2 ad */
418 1.4.6.2 ad int
419 1.4.6.2 ad puffs_cookie2vnode(struct puffs_mount *pmp, void *cookie, int lock,
420 1.4.6.2 ad int willcreate, struct vnode **vpp)
421 1.4.6.2 ad {
422 1.4.6.2 ad struct puffs_node *pnode;
423 1.4.6.2 ad struct puffs_newcookie *pnc;
424 1.4.6.2 ad struct vnode *vp;
425 1.4.6.2 ad int vgetflags, rv;
426 1.4.6.2 ad
427 1.4.6.2 ad /*
428 1.4.6.2 ad * Handle root in a special manner, since we want to make sure
429 1.4.6.2 ad * pmp_root is properly set.
430 1.4.6.2 ad */
431 1.4.6.2 ad if (cookie == pmp->pmp_root_cookie) {
432 1.4.6.2 ad if ((rv = puffs_makeroot(pmp)))
433 1.4.6.2 ad return rv;
434 1.4.6.2 ad if (lock)
435 1.4.6.2 ad vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
436 1.4.6.2 ad
437 1.4.6.2 ad *vpp = pmp->pmp_root;
438 1.4.6.2 ad return 0;
439 1.4.6.2 ad }
440 1.4.6.2 ad
441 1.4.6.2 ad mutex_enter(&pmp->pmp_lock);
442 1.4.6.2 ad pnode = puffs_cookie2pnode(pmp, cookie);
443 1.4.6.2 ad if (pnode == NULL) {
444 1.4.6.2 ad if (willcreate) {
445 1.4.6.2 ad pnc = kmem_alloc(sizeof(struct puffs_newcookie),
446 1.4.6.2 ad KM_SLEEP);
447 1.4.6.2 ad pnc->pnc_cookie = cookie;
448 1.4.6.2 ad LIST_INSERT_HEAD(&pmp->pmp_newcookie, pnc, pnc_entries);
449 1.4.6.2 ad }
450 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
451 1.4.6.2 ad return PUFFS_NOSUCHCOOKIE;
452 1.4.6.2 ad }
453 1.4.6.2 ad vp = pnode->pn_vp;
454 1.4.6.3 ad mutex_enter(&vp->v_interlock);
455 1.4.6.2 ad mutex_exit(&pmp->pmp_lock);
456 1.4.6.2 ad
457 1.4.6.2 ad vgetflags = LK_INTERLOCK;
458 1.4.6.2 ad if (lock)
459 1.4.6.2 ad vgetflags |= LK_EXCLUSIVE | LK_RETRY;
460 1.4.6.2 ad if ((rv = vget(vp, vgetflags)))
461 1.4.6.2 ad return rv;
462 1.4.6.2 ad
463 1.4.6.2 ad *vpp = vp;
464 1.4.6.2 ad return 0;
465 1.4.6.2 ad }
466 1.4.6.2 ad
467 1.4.6.2 ad void
468 1.4.6.2 ad puffs_updatenode(struct vnode *vp, int flags)
469 1.4.6.2 ad {
470 1.4.6.2 ad struct puffs_node *pn;
471 1.4.6.2 ad struct timespec ts;
472 1.4.6.2 ad
473 1.4.6.2 ad if (flags == 0)
474 1.4.6.2 ad return;
475 1.4.6.2 ad
476 1.4.6.2 ad pn = VPTOPP(vp);
477 1.4.6.2 ad nanotime(&ts);
478 1.4.6.2 ad
479 1.4.6.2 ad if (flags & PUFFS_UPDATEATIME) {
480 1.4.6.2 ad pn->pn_mc_atime = ts;
481 1.4.6.2 ad pn->pn_stat |= PNODE_METACACHE_ATIME;
482 1.4.6.2 ad }
483 1.4.6.2 ad if (flags & PUFFS_UPDATECTIME) {
484 1.4.6.2 ad pn->pn_mc_ctime = ts;
485 1.4.6.2 ad pn->pn_stat |= PNODE_METACACHE_CTIME;
486 1.4.6.2 ad }
487 1.4.6.2 ad if (flags & PUFFS_UPDATEMTIME) {
488 1.4.6.2 ad pn->pn_mc_mtime = ts;
489 1.4.6.2 ad pn->pn_stat |= PNODE_METACACHE_MTIME;
490 1.4.6.2 ad }
491 1.4.6.2 ad if (flags & PUFFS_UPDATESIZE) {
492 1.4.6.2 ad pn->pn_mc_size = vp->v_size;
493 1.4.6.2 ad pn->pn_stat |= PNODE_METACACHE_SIZE;
494 1.4.6.2 ad }
495 1.4.6.2 ad }
496 1.4.6.2 ad
497 1.4.6.2 ad /*
498 1.4.6.2 ad * Add reference to node.
499 1.4.6.2 ad * mutex held on entry and return
500 1.4.6.2 ad */
501 1.4.6.2 ad void
502 1.4.6.2 ad puffs_referencenode(struct puffs_node *pn)
503 1.4.6.2 ad {
504 1.4.6.2 ad
505 1.4.6.2 ad KASSERT(mutex_owned(&pn->pn_mtx));
506 1.4.6.2 ad pn->pn_refcount++;
507 1.4.6.2 ad }
508 1.4.6.2 ad
509 1.4.6.2 ad /*
510 1.4.6.2 ad * Release pnode structure which dealing with references to the
511 1.4.6.2 ad * puffs_node instead of the vnode. Can't use vref()/vrele() on
512 1.4.6.2 ad * the vnode there, since that causes the lovely VOP_INACTIVE(),
513 1.4.6.2 ad * which in turn causes the lovely deadlock when called by the one
514 1.4.6.2 ad * who is supposed to handle it.
515 1.4.6.2 ad */
516 1.4.6.2 ad void
517 1.4.6.2 ad puffs_releasenode(struct puffs_node *pn)
518 1.4.6.2 ad {
519 1.4.6.2 ad
520 1.4.6.2 ad mutex_enter(&pn->pn_mtx);
521 1.4.6.2 ad if (--pn->pn_refcount == 0) {
522 1.4.6.2 ad mutex_exit(&pn->pn_mtx);
523 1.4.6.2 ad mutex_destroy(&pn->pn_mtx);
524 1.4.6.2 ad pool_put(&puffs_pnpool, pn);
525 1.4.6.2 ad } else {
526 1.4.6.2 ad mutex_exit(&pn->pn_mtx);
527 1.4.6.2 ad }
528 1.4.6.2 ad }
529