1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/* $NetBSD: vnode.c,v 1.16 2020/04/03 19:36:33 joerg Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Konrad E. Schroder <perseant@hhhh.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/queue.h>
#define VU_DIROP 0x01000000 /* XXX XXX from sys/vnode.h */
#define vnode uvnode
#include <ufs/lfs/lfs.h>
#include <ufs/lfs/lfs_inode.h>
#undef vnode
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "bufcache.h"
#include "extern.h"
#include "kernelops.h"
struct uvnodelst vnodelist;
struct uvnodelst getvnodelist[VNODE_HASH_MAX];
struct vgrlst vgrlist;
int nvnodes;
/* Convert between inode pointers and vnode pointers. */
#ifndef VTOI
#define VTOI(vp) ((struct inode *)(vp)->v_data)
#endif
/*
* Raw device uvnode ops
*/
int
raw_vop_strategy(struct ubuf * bp)
{
if (bp->b_flags & B_READ) {
return kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
bp->b_blkno * dev_bsize);
} else {
return kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
bp->b_blkno * dev_bsize);
}
}
int
raw_vop_bwrite(struct ubuf * bp)
{
bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR);
raw_vop_strategy(bp);
brelse(bp, 0);
return 0;
}
int
raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
{
*daddrp = lbn;
return 0;
}
/* Register a fs-specific vget function */
void
register_vget(void *fs, struct uvnode *func(void *, ino_t))
{
struct vget_reg *vgr;
vgr = emalloc(sizeof(*vgr));
vgr->vgr_fs = fs;
vgr->vgr_func = func;
LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list);
}
static struct uvnode *
VFS_VGET(void *fs, ino_t ino)
{
struct vget_reg *vgr;
LIST_FOREACH(vgr, &vgrlist, vgr_list) {
if (vgr->vgr_fs == fs)
return vgr->vgr_func(fs, ino);
}
return NULL;
}
void
vnode_destroy(struct uvnode *tossvp)
{
struct ubuf *bp;
--nvnodes;
LIST_REMOVE(tossvp, v_getvnodes);
LIST_REMOVE(tossvp, v_mntvnodes);
while ((bp = LIST_FIRST(&tossvp->v_dirtyblkhd)) != NULL) {
LIST_REMOVE(bp, b_vnbufs);
bremfree(bp);
buf_destroy(bp);
}
while ((bp = LIST_FIRST(&tossvp->v_cleanblkhd)) != NULL) {
LIST_REMOVE(bp, b_vnbufs);
bremfree(bp);
buf_destroy(bp);
}
free(VTOI(tossvp)->inode_ext.lfs);
free(VTOI(tossvp)->i_din);
memset(VTOI(tossvp), 0, sizeof(struct inode));
free(tossvp->v_data);
memset(tossvp, 0, sizeof(*tossvp));
free(tossvp);
}
int hits, misses;
/*
* Find a vnode in the cache; if not present, get it from the
* filesystem-specific vget routine.
*/
struct uvnode *
vget(void *fs, ino_t ino)
{
struct uvnode *vp, *tossvp;
int hash;
/* Look in the uvnode cache */
tossvp = NULL;
hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1);
LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) {
if (vp->v_fs != fs)
continue;
if (VTOI(vp)->i_number == ino) {
/* Move to the front of the list */
LIST_REMOVE(vp, v_getvnodes);
LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
++hits;
break;
}
if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
vp->v_usecount == 0 &&
!(vp->v_uflag & VU_DIROP))
tossvp = vp;
}
/* Don't let vnode list grow arbitrarily */
if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
vnode_destroy(tossvp);
}
if (vp)
return vp;
++misses;
return VFS_VGET(fs, ino);
}
void
vfs_init(void)
{
int i;
nvnodes = 0;
LIST_INIT(&vnodelist);
for (i = 0; i < VNODE_HASH_MAX; i++)
LIST_INIT(&getvnodelist[i]);
LIST_INIT(&vgrlist);
}
|