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