chfs_vnode_cache.c revision 1.1 1 1.1 ahoka /* $NetBSD: chfs_vnode_cache.c,v 1.1 2011/11/24 15:51:32 ahoka Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 1.1 ahoka * All rights reserved.
9 1.1 ahoka *
10 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
11 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
12 1.1 ahoka *
13 1.1 ahoka * Redistribution and use in source and binary forms, with or without
14 1.1 ahoka * modification, are permitted provided that the following conditions
15 1.1 ahoka * are met:
16 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
17 1.1 ahoka * notice, this list of conditions and the following disclaimer.
18 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
20 1.1 ahoka * documentation and/or other materials provided with the distribution.
21 1.1 ahoka *
22 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 ahoka * SUCH DAMAGE.
33 1.1 ahoka */
34 1.1 ahoka
35 1.1 ahoka #include "chfs.h"
36 1.1 ahoka #include <sys/pool.h>
37 1.1 ahoka
38 1.1 ahoka struct chfs_vnode_cache **
39 1.1 ahoka chfs_vnocache_hash_init(void)
40 1.1 ahoka {
41 1.1 ahoka return kmem_zalloc(VNODECACHE_SIZE *
42 1.1 ahoka sizeof(struct chfs_vnode_cache *), KM_SLEEP);
43 1.1 ahoka }
44 1.1 ahoka
45 1.1 ahoka /**
46 1.1 ahoka * chfs_set_vnode_cache_state - set state of a vnode_cache
47 1.1 ahoka * @chmp: fs super block info
48 1.1 ahoka * @vc: vnode_cache
49 1.1 ahoka * @state: new state
50 1.1 ahoka */
51 1.1 ahoka void
52 1.1 ahoka chfs_vnode_cache_set_state(struct chfs_mount *chmp,
53 1.1 ahoka struct chfs_vnode_cache* vc, int state)
54 1.1 ahoka {
55 1.1 ahoka /* XXX do we really need locking here? */
56 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
57 1.1 ahoka vc->state = state;
58 1.1 ahoka }
59 1.1 ahoka
60 1.1 ahoka /**
61 1.1 ahoka * chfs_get_vnode_cache - get a vnode_cache from the vnocache_hash
62 1.1 ahoka * @chmp: fs super block info
63 1.1 ahoka * @ino: inode for search
64 1.1 ahoka * Returns the vnode_cache.
65 1.1 ahoka */
66 1.1 ahoka struct chfs_vnode_cache *
67 1.1 ahoka chfs_vnode_cache_get(struct chfs_mount *chmp, ino_t vno)
68 1.1 ahoka {
69 1.1 ahoka struct chfs_vnode_cache* ret;
70 1.1 ahoka
71 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
72 1.1 ahoka
73 1.1 ahoka ret = chmp->chm_vnocache_hash[vno % VNODECACHE_SIZE];
74 1.1 ahoka
75 1.1 ahoka if (ret == NULL) {
76 1.1 ahoka return NULL;
77 1.1 ahoka }
78 1.1 ahoka
79 1.1 ahoka while (ret && ret->vno < vno) {
80 1.1 ahoka ret = ret->next;
81 1.1 ahoka }
82 1.1 ahoka
83 1.1 ahoka if (ret && ret->vno != vno) {
84 1.1 ahoka ret = NULL;
85 1.1 ahoka }
86 1.1 ahoka
87 1.1 ahoka return ret;
88 1.1 ahoka }
89 1.1 ahoka
90 1.1 ahoka /**
91 1.1 ahoka * chfs_add_vnode_cache - add a vnode_cache to the vnocache_hash
92 1.1 ahoka * @chmp: fs super block info
93 1.1 ahoka * @new: new vnode_cache
94 1.1 ahoka */
95 1.1 ahoka void
96 1.1 ahoka chfs_vnode_cache_add(struct chfs_mount *chmp,
97 1.1 ahoka struct chfs_vnode_cache* new)
98 1.1 ahoka {
99 1.1 ahoka struct chfs_vnode_cache** prev;
100 1.1 ahoka
101 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
102 1.1 ahoka
103 1.1 ahoka if (!new->vno) {
104 1.1 ahoka new->vno = ++chmp->chm_max_vno;
105 1.1 ahoka }
106 1.1 ahoka
107 1.1 ahoka prev = &chmp->chm_vnocache_hash[new->vno % VNODECACHE_SIZE];
108 1.1 ahoka
109 1.1 ahoka while ((*prev) && (*prev)->vno < new->vno) {
110 1.1 ahoka prev = &((*prev)->next);
111 1.1 ahoka }
112 1.1 ahoka new->next = *prev;
113 1.1 ahoka *prev = new;
114 1.1 ahoka }
115 1.1 ahoka
116 1.1 ahoka /**
117 1.1 ahoka * chfs_del_vnode_cache - del a vnode_cache from the vnocache_hash
118 1.1 ahoka * @chmp: fs super block info
119 1.1 ahoka * @old: old vnode_cache
120 1.1 ahoka */
121 1.1 ahoka void
122 1.1 ahoka chfs_vnode_cache_remove(struct chfs_mount *chmp,
123 1.1 ahoka struct chfs_vnode_cache* old)
124 1.1 ahoka {
125 1.1 ahoka struct chfs_vnode_cache** prev;
126 1.1 ahoka
127 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
128 1.1 ahoka
129 1.1 ahoka prev = &chmp->chm_vnocache_hash[old->vno % VNODECACHE_SIZE];
130 1.1 ahoka while ((*prev) && (*prev)->vno < old->vno) {
131 1.1 ahoka prev = &(*prev)->next;
132 1.1 ahoka }
133 1.1 ahoka
134 1.1 ahoka if ((*prev) == old) {
135 1.1 ahoka *prev = old->next;
136 1.1 ahoka }
137 1.1 ahoka
138 1.1 ahoka if (old->state != VNO_STATE_READING &&
139 1.1 ahoka old->state != VNO_STATE_CLEARING) {
140 1.1 ahoka chfs_vnode_cache_free(old);
141 1.1 ahoka }
142 1.1 ahoka }
143 1.1 ahoka
144 1.1 ahoka /**
145 1.1 ahoka * chfs_free_vnode_caches - free the vnocache_hash
146 1.1 ahoka * @chmp: fs super block info
147 1.1 ahoka */
148 1.1 ahoka void
149 1.1 ahoka chfs_vnocache_hash_destroy(struct chfs_vnode_cache **hash)
150 1.1 ahoka {
151 1.1 ahoka struct chfs_vnode_cache *this, *next;
152 1.1 ahoka int i;
153 1.1 ahoka
154 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
155 1.1 ahoka this = hash[i];
156 1.1 ahoka while (this) {
157 1.1 ahoka next = this->next;
158 1.1 ahoka chfs_vnode_cache_free(this);
159 1.1 ahoka this = next;
160 1.1 ahoka }
161 1.1 ahoka hash[i] = NULL;
162 1.1 ahoka }
163 1.1 ahoka }
164 1.1 ahoka
165 1.1 ahoka
166