1 /* $NetBSD: vnode.c,v 1.9 2008/04/28 20:23:08 martin Exp $ */
3 * Copyright (c) 2003 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Konrad E. Schroder <perseant@hhhh.org>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
32 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/queue.h>
38 #include <ufs/ufs/inode.h>
39 #include <ufs/ufs/ufsmount.h>
41 #include <ufs/lfs/lfs.h>
56 #include "kernelops.h"
58 struct uvnodelst vnodelist
;
59 struct uvnodelst getvnodelist
[VNODE_HASH_MAX
];
60 struct vgrlst vgrlist
;
64 /* Convert between inode pointers and vnode pointers. */
66 #define VTOI(vp) ((struct inode *)(vp)->v_data)
70 * Raw device uvnode ops
74 raw_vop_strategy(struct ubuf
* bp
)
76 if (bp
->b_flags
& B_READ
) {
77 return kops
.ko_pread(bp
->b_vp
->v_fd
, bp
->b_data
, bp
->b_bcount
,
80 return kops
.ko_pwrite(bp
->b_vp
->v_fd
, bp
->b_data
, bp
->b_bcount
,
86 raw_vop_bwrite(struct ubuf
* bp
)
88 bp
->b_flags
&= ~(B_READ
| B_DELWRI
| B_DONE
| B_ERROR
);
95 raw_vop_bmap(struct uvnode
* vp
, daddr_t lbn
, daddr_t
* daddrp
)
101 /* Register a fs-specific vget function */
103 register_vget(void *fs
, struct uvnode
*func(void *, ino_t
))
105 struct vget_reg
*vgr
;
107 vgr
= emalloc(sizeof(*vgr
));
109 vgr
->vgr_func
= func
;
110 LIST_INSERT_HEAD(&vgrlist
, vgr
, vgr_list
);
113 static struct uvnode
*
114 VFS_VGET(void *fs
, ino_t ino
)
116 struct vget_reg
*vgr
;
118 LIST_FOREACH(vgr
, &vgrlist
, vgr_list
) {
119 if (vgr
->vgr_fs
== fs
)
120 return vgr
->vgr_func(fs
, ino
);
126 vnode_destroy(struct uvnode
*tossvp
)
131 LIST_REMOVE(tossvp
, v_getvnodes
);
132 LIST_REMOVE(tossvp
, v_mntvnodes
);
133 while ((bp
= LIST_FIRST(&tossvp
->v_dirtyblkhd
)) != NULL
) {
134 LIST_REMOVE(bp
, b_vnbufs
);
138 while ((bp
= LIST_FIRST(&tossvp
->v_cleanblkhd
)) != NULL
) {
139 LIST_REMOVE(bp
, b_vnbufs
);
143 free(VTOI(tossvp
)->inode_ext
.lfs
);
144 free(VTOI(tossvp
)->i_din
.ffs1_din
);
145 memset(VTOI(tossvp
), 0, sizeof(struct inode
));
146 free(tossvp
->v_data
);
147 memset(tossvp
, 0, sizeof(*tossvp
));
154 * Find a vnode in the cache; if not present, get it from the
155 * filesystem-specific vget routine.
158 vget(void *fs
, ino_t ino
)
160 struct uvnode
*vp
, *tossvp
;
163 /* Look in the uvnode cache */
165 hash
= ((unsigned long)fs
+ ino
) & (VNODE_HASH_MAX
- 1);
166 LIST_FOREACH(vp
, &getvnodelist
[hash
], v_getvnodes
) {
169 if (VTOI(vp
)->i_number
== ino
) {
170 /* Move to the front of the list */
171 LIST_REMOVE(vp
, v_getvnodes
);
172 LIST_INSERT_HEAD(&getvnodelist
[hash
], vp
, v_getvnodes
);
176 if (LIST_EMPTY(&vp
->v_dirtyblkhd
) &&
177 vp
->v_usecount
== 0 &&
178 !(vp
->v_uflag
& VU_DIROP
))
181 /* Don't let vnode list grow arbitrarily */
182 if (nvnodes
> VNODE_CACHE_SIZE
&& tossvp
) {
183 vnode_destroy(tossvp
);
189 return VFS_VGET(fs
, ino
);
198 LIST_INIT(&vnodelist
);
199 for (i
= 0; i
< VNODE_HASH_MAX
; i
++)
200 LIST_INIT(&getvnodelist
[i
]);