1 /* $NetBSD: puffs_node.c,v 1.14 2009/09/30 18:19:17 pooka Exp $ */
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_node.c,v 1.14 2009/09/30 18:19:17 pooka Exp $");
35 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
43 #include <fs/puffs/puffs_msgif.h>
44 #include <fs/puffs/puffs_sys.h>
46 #include <miscfs/genfs/genfs_node.h>
47 #include <miscfs/specfs/specdev.h>
49 static const struct genfs_ops puffs_genfsops
= {
50 .gop_size
= puffs_gop_size
,
51 .gop_write
= genfs_gop_write
,
52 .gop_markupdate
= puffs_gop_markupdate
,
54 .gop_alloc
, should ask userspace
58 static __inline
struct puffs_node_hashlist
59 *puffs_cookie2hashlist(struct puffs_mount
*, puffs_cookie_t
);
60 static struct puffs_node
*puffs_cookie2pnode(struct puffs_mount
*,
63 struct pool puffs_pnpool
;
66 * Grab a vnode, intialize all the puffs-dependant stuff.
69 puffs_getvnode(struct mount
*mp
, puffs_cookie_t ck
, enum vtype type
,
70 voff_t vsize
, dev_t rdev
, struct vnode
**vpp
)
72 struct puffs_mount
*pmp
;
73 struct puffs_newcookie
*pnc
;
75 struct puffs_node
*pnode
;
76 struct puffs_node_hashlist
*plist
;
79 pmp
= MPTOPUFFSMP(mp
);
82 if (type
<= VNON
|| type
>= VBAD
) {
83 puffs_senderr(pmp
, PUFFS_ERR_MAKENODE
, EINVAL
,
87 if (vsize
== VSIZENOTSET
) {
88 puffs_senderr(pmp
, PUFFS_ERR_MAKENODE
, EINVAL
,
89 "VSIZENOTSET is not a valid size", ck
);
93 error
= getnewvnode(VT_PUFFS
, mp
, puffs_vnodeop_p
, &vp
);
99 * Creation should not fail after this point. Or if it does,
100 * care must be taken so that VOP_INACTIVE() isn't called.
104 uvm_vnp_setsize(vp
, 0);
106 /* dances based on vnode type. almost ufs_vinit(), but not quite */
111 * replace vnode operation vector with the specops vector.
112 * our user server has very little control over the node
113 * if it decides its a character or block special file
115 vp
->v_op
= puffs_specop_p
;
116 spec_node_init(vp
, rdev
);
120 vp
->v_op
= puffs_fifoop_p
;
124 uvm_vnp_setsize(vp
, vsize
);
132 panic("puffs_getvnode: invalid vtype %d", type
);
135 pnode
= pool_get(&puffs_pnpool
, PR_WAITOK
);
136 memset(pnode
, 0, sizeof(struct puffs_node
));
138 pnode
->pn_cookie
= ck
;
139 pnode
->pn_refcount
= 1;
141 /* insert cookie on list, take off of interlock list */
142 mutex_init(&pnode
->pn_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
143 selinit(&pnode
->pn_sel
);
144 plist
= puffs_cookie2hashlist(pmp
, ck
);
145 mutex_enter(&pmp
->pmp_lock
);
146 LIST_INSERT_HEAD(plist
, pnode
, pn_hashent
);
147 if (ck
!= pmp
->pmp_root_cookie
) {
148 LIST_FOREACH(pnc
, &pmp
->pmp_newcookie
, pnc_entries
) {
149 if (pnc
->pnc_cookie
== ck
) {
150 LIST_REMOVE(pnc
, pnc_entries
);
151 kmem_free(pnc
, sizeof(struct puffs_newcookie
));
155 KASSERT(pnc
!= NULL
);
157 mutex_exit(&pmp
->pmp_lock
);
162 pnode
->pn_serversize
= vsize
;
164 genfs_node_init(vp
, &puffs_genfsops
);
167 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp
,
168 pnode
, pnode
->pn_cookie
));
173 /* remove staging cookie from list */
174 if (ck
!= pmp
->pmp_root_cookie
) {
175 mutex_enter(&pmp
->pmp_lock
);
176 LIST_FOREACH(pnc
, &pmp
->pmp_newcookie
, pnc_entries
) {
177 if (pnc
->pnc_cookie
== ck
) {
178 LIST_REMOVE(pnc
, pnc_entries
);
179 kmem_free(pnc
, sizeof(struct puffs_newcookie
));
183 KASSERT(pnc
!= NULL
);
184 mutex_exit(&pmp
->pmp_lock
);
190 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
192 puffs_newnode(struct mount
*mp
, struct vnode
*dvp
, struct vnode
**vpp
,
193 puffs_cookie_t ck
, struct componentname
*cnp
,
194 enum vtype type
, dev_t rdev
)
196 struct puffs_mount
*pmp
= MPTOPUFFSMP(mp
);
197 struct puffs_newcookie
*pnc
;
201 /* userspace probably has this as a NULL op */
208 * Check for previous node with the same designation.
209 * Explicitly check the root node cookie, since it might be
210 * reclaimed from the kernel when this check is made.
212 mutex_enter(&pmp
->pmp_lock
);
213 if (ck
== pmp
->pmp_root_cookie
214 || puffs_cookie2pnode(pmp
, ck
) != NULL
) {
215 mutex_exit(&pmp
->pmp_lock
);
216 puffs_senderr(pmp
, PUFFS_ERR_MAKENODE
, EEXIST
,
217 "cookie exists", ck
);
221 LIST_FOREACH(pnc
, &pmp
->pmp_newcookie
, pnc_entries
) {
222 if (pnc
->pnc_cookie
== ck
) {
223 mutex_exit(&pmp
->pmp_lock
);
224 puffs_senderr(pmp
, PUFFS_ERR_MAKENODE
, EEXIST
,
225 "newcookie exists", ck
);
229 pnc
= kmem_alloc(sizeof(struct puffs_newcookie
), KM_SLEEP
);
230 pnc
->pnc_cookie
= ck
;
231 LIST_INSERT_HEAD(&pmp
->pmp_newcookie
, pnc
, pnc_entries
);
232 mutex_exit(&pmp
->pmp_lock
);
234 error
= puffs_getvnode(dvp
->v_mount
, ck
, type
, 0, rdev
, &vp
);
239 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
242 if ((cnp
->cn_flags
& MAKEENTRY
) && PUFFS_USE_NAMECACHE(pmp
))
243 cache_enter(dvp
, vp
, cnp
);
249 puffs_putvnode(struct vnode
*vp
)
251 struct puffs_mount
*pmp
;
252 struct puffs_node
*pnode
;
254 pmp
= VPTOPUFFSMP(vp
);
258 if (vp
->v_tag
!= VT_PUFFS
)
259 panic("puffs_putvnode: %p not a puffs vnode", vp
);
262 genfs_node_destroy(vp
);
263 puffs_releasenode(pnode
);
269 static __inline
struct puffs_node_hashlist
*
270 puffs_cookie2hashlist(struct puffs_mount
*pmp
, puffs_cookie_t ck
)
274 hash
= hash32_buf(&ck
, sizeof(void *), HASH32_BUF_INIT
);
275 return &pmp
->pmp_pnodehash
[hash
% pmp
->pmp_npnodehash
];
279 * Translate cookie to puffs_node. Caller must hold pmp_lock
280 * and it will be held upon return.
282 static struct puffs_node
*
283 puffs_cookie2pnode(struct puffs_mount
*pmp
, puffs_cookie_t ck
)
285 struct puffs_node_hashlist
*plist
;
286 struct puffs_node
*pnode
;
288 plist
= puffs_cookie2hashlist(pmp
, ck
);
289 LIST_FOREACH(pnode
, plist
, pn_hashent
) {
290 if (pnode
->pn_cookie
== ck
)
298 * Make sure root vnode exists and reference it. Does NOT lock.
301 puffs_makeroot(struct puffs_mount
*pmp
)
307 * pmp_lock must be held if vref()'ing or vrele()'ing the
308 * root vnode. the latter is controlled by puffs_inactive().
310 * pmp_root is set here and cleared in puffs_reclaim().
313 mutex_enter(&pmp
->pmp_lock
);
316 mutex_enter(&vp
->v_interlock
);
317 mutex_exit(&pmp
->pmp_lock
);
318 if (vget(vp
, LK_INTERLOCK
) == 0)
321 mutex_exit(&pmp
->pmp_lock
);
324 * So, didn't have the magic root vnode available.
325 * No matter, grab another and stuff it with the cookie.
327 if ((rv
= puffs_getvnode(pmp
->pmp_mp
, pmp
->pmp_root_cookie
,
328 pmp
->pmp_root_vtype
, pmp
->pmp_root_vsize
, pmp
->pmp_root_rdev
, &vp
)))
332 * Someone magically managed to race us into puffs_getvnode?
333 * Put our previous new vnode back and retry.
335 mutex_enter(&pmp
->pmp_lock
);
337 struct puffs_node
*pnode
= vp
->v_data
;
339 LIST_REMOVE(pnode
, pn_hashent
);
340 mutex_exit(&pmp
->pmp_lock
);
346 vp
->v_vflag
|= VV_ROOT
;
348 mutex_exit(&pmp
->pmp_lock
);
354 * Locate the in-kernel vnode based on the cookie received given
355 * from userspace. Returns a vnode, if found, NULL otherwise.
356 * The parameter "lock" control whether to lock the possible or
357 * not. Locking always might cause us to lock against ourselves
358 * in situations where we want the vnode but don't care for the
359 * vnode lock, e.g. file server issued putpages.
362 puffs_cookie2vnode(struct puffs_mount
*pmp
, puffs_cookie_t ck
, int lock
,
363 int willcreate
, struct vnode
**vpp
)
365 struct puffs_node
*pnode
;
366 struct puffs_newcookie
*pnc
;
371 * Handle root in a special manner, since we want to make sure
372 * pmp_root is properly set.
374 if (ck
== pmp
->pmp_root_cookie
) {
375 if ((rv
= puffs_makeroot(pmp
)))
378 vn_lock(pmp
->pmp_root
, LK_EXCLUSIVE
| LK_RETRY
);
380 *vpp
= pmp
->pmp_root
;
384 mutex_enter(&pmp
->pmp_lock
);
385 pnode
= puffs_cookie2pnode(pmp
, ck
);
388 pnc
= kmem_alloc(sizeof(struct puffs_newcookie
),
390 pnc
->pnc_cookie
= ck
;
391 LIST_INSERT_HEAD(&pmp
->pmp_newcookie
, pnc
, pnc_entries
);
393 mutex_exit(&pmp
->pmp_lock
);
394 return PUFFS_NOSUCHCOOKIE
;
397 mutex_enter(&vp
->v_interlock
);
398 mutex_exit(&pmp
->pmp_lock
);
400 vgetflags
= LK_INTERLOCK
;
402 vgetflags
|= LK_EXCLUSIVE
| LK_RETRY
;
403 if ((rv
= vget(vp
, vgetflags
)))
411 puffs_updatenode(struct puffs_node
*pn
, int flags
, voff_t size
)
420 if (flags
& PUFFS_UPDATEATIME
) {
421 pn
->pn_mc_atime
= ts
;
422 pn
->pn_stat
|= PNODE_METACACHE_ATIME
;
424 if (flags
& PUFFS_UPDATECTIME
) {
425 pn
->pn_mc_ctime
= ts
;
426 pn
->pn_stat
|= PNODE_METACACHE_CTIME
;
428 if (flags
& PUFFS_UPDATEMTIME
) {
429 pn
->pn_mc_mtime
= ts
;
430 pn
->pn_stat
|= PNODE_METACACHE_MTIME
;
432 if (flags
& PUFFS_UPDATESIZE
) {
433 pn
->pn_mc_size
= size
;
434 pn
->pn_stat
|= PNODE_METACACHE_SIZE
;
439 * Add reference to node.
440 * mutex held on entry and return
443 puffs_referencenode(struct puffs_node
*pn
)
446 KASSERT(mutex_owned(&pn
->pn_mtx
));
451 * Release pnode structure which dealing with references to the
452 * puffs_node instead of the vnode. Can't use vref()/vrele() on
453 * the vnode there, since that causes the lovely VOP_INACTIVE(),
454 * which in turn causes the lovely deadlock when called by the one
455 * who is supposed to handle it.
458 puffs_releasenode(struct puffs_node
*pn
)
461 mutex_enter(&pn
->pn_mtx
);
462 if (--pn
->pn_refcount
== 0) {
463 mutex_exit(&pn
->pn_mtx
);
464 mutex_destroy(&pn
->pn_mtx
);
465 seldestroy(&pn
->pn_sel
);
466 pool_put(&puffs_pnpool
, pn
);
468 mutex_exit(&pn
->pn_mtx
);