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