Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / fs / puffs / puffs_node.c
blobecce314a650e87bed7420462759b19f58f9414ec
1 /* $NetBSD: puffs_node.c,v 1.14 2009/09/30 18:19:17 pooka Exp $ */
3 /*
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
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.
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.
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>
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>
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,
53 #if 0
54 .gop_alloc, should ask userspace
55 #endif
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 *,
61 puffs_cookie_t);
63 struct pool puffs_pnpool;
66 * Grab a vnode, intialize all the puffs-dependant stuff.
68 int
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;
74 struct vnode *vp;
75 struct puffs_node *pnode;
76 struct puffs_node_hashlist *plist;
77 int error;
79 pmp = MPTOPUFFSMP(mp);
81 error = EPROTO;
82 if (type <= VNON || type >= VBAD) {
83 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
84 "bad node type", ck);
85 goto bad;
87 if (vsize == VSIZENOTSET) {
88 puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
89 "VSIZENOTSET is not a valid size", ck);
90 goto bad;
93 error = getnewvnode(VT_PUFFS, mp, puffs_vnodeop_p, &vp);
94 if (error)
95 goto bad;
96 vp->v_type = type;
99 * Creation should not fail after this point. Or if it does,
100 * care must be taken so that VOP_INACTIVE() isn't called.
103 /* default size */
104 uvm_vnp_setsize(vp, 0);
106 /* dances based on vnode type. almost ufs_vinit(), but not quite */
107 switch (type) {
108 case VCHR:
109 case VBLK:
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);
117 break;
119 case VFIFO:
120 vp->v_op = puffs_fifoop_p;
121 break;
123 case VREG:
124 uvm_vnp_setsize(vp, vsize);
125 break;
127 case VDIR:
128 case VLNK:
129 case VSOCK:
130 break;
131 default:
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));
152 break;
155 KASSERT(pnc != NULL);
157 mutex_exit(&pmp->pmp_lock);
159 vp->v_data = pnode;
160 vp->v_type = type;
161 pnode->pn_vp = vp;
162 pnode->pn_serversize = vsize;
164 genfs_node_init(vp, &puffs_genfsops);
165 *vpp = vp;
167 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
168 pnode, pnode->pn_cookie));
170 return 0;
172 bad:
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));
180 break;
183 KASSERT(pnc != NULL);
184 mutex_exit(&pmp->pmp_lock);
187 return error;
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;
198 struct vnode *vp;
199 int error;
201 /* userspace probably has this as a NULL op */
202 if (ck == NULL) {
203 error = EOPNOTSUPP;
204 return error;
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);
218 return EPROTO;
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);
226 return EPROTO;
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);
235 if (error)
236 return error;
238 vp->v_type = type;
239 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
240 *vpp = vp;
242 if ((cnp->cn_flags & MAKEENTRY) && PUFFS_USE_NAMECACHE(pmp))
243 cache_enter(dvp, vp, cnp);
245 return 0;
248 void
249 puffs_putvnode(struct vnode *vp)
251 struct puffs_mount *pmp;
252 struct puffs_node *pnode;
254 pmp = VPTOPUFFSMP(vp);
255 pnode = VPTOPP(vp);
257 #ifdef DIAGNOSTIC
258 if (vp->v_tag != VT_PUFFS)
259 panic("puffs_putvnode: %p not a puffs vnode", vp);
260 #endif
262 genfs_node_destroy(vp);
263 puffs_releasenode(pnode);
264 vp->v_data = NULL;
266 return;
269 static __inline struct puffs_node_hashlist *
270 puffs_cookie2hashlist(struct puffs_mount *pmp, puffs_cookie_t ck)
272 uint32_t hash;
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)
291 break;
294 return pnode;
298 * Make sure root vnode exists and reference it. Does NOT lock.
300 static int
301 puffs_makeroot(struct puffs_mount *pmp)
303 struct vnode *vp;
304 int rv;
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().
312 retry:
313 mutex_enter(&pmp->pmp_lock);
314 vp = pmp->pmp_root;
315 if (vp) {
316 mutex_enter(&vp->v_interlock);
317 mutex_exit(&pmp->pmp_lock);
318 if (vget(vp, LK_INTERLOCK) == 0)
319 return 0;
320 } else
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)))
329 return rv;
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);
336 if (pmp->pmp_root) {
337 struct puffs_node *pnode = vp->v_data;
339 LIST_REMOVE(pnode, pn_hashent);
340 mutex_exit(&pmp->pmp_lock);
341 puffs_putvnode(vp);
342 goto retry;
345 /* store cache */
346 vp->v_vflag |= VV_ROOT;
347 pmp->pmp_root = vp;
348 mutex_exit(&pmp->pmp_lock);
350 return 0;
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;
367 struct vnode *vp;
368 int vgetflags, rv;
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)))
376 return rv;
377 if (lock)
378 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
380 *vpp = pmp->pmp_root;
381 return 0;
384 mutex_enter(&pmp->pmp_lock);
385 pnode = puffs_cookie2pnode(pmp, ck);
386 if (pnode == NULL) {
387 if (willcreate) {
388 pnc = kmem_alloc(sizeof(struct puffs_newcookie),
389 KM_SLEEP);
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;
396 vp = pnode->pn_vp;
397 mutex_enter(&vp->v_interlock);
398 mutex_exit(&pmp->pmp_lock);
400 vgetflags = LK_INTERLOCK;
401 if (lock)
402 vgetflags |= LK_EXCLUSIVE | LK_RETRY;
403 if ((rv = vget(vp, vgetflags)))
404 return rv;
406 *vpp = vp;
407 return 0;
410 void
411 puffs_updatenode(struct puffs_node *pn, int flags, voff_t size)
413 struct timespec ts;
415 if (flags == 0)
416 return;
418 nanotime(&ts);
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
442 void
443 puffs_referencenode(struct puffs_node *pn)
446 KASSERT(mutex_owned(&pn->pn_mtx));
447 pn->pn_refcount++;
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.
457 void
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);
467 } else {
468 mutex_exit(&pn->pn_mtx);