chfs_vnode_cache.c revision 1.1.6.3 1 1.1.6.3 yamt /* $NetBSD: chfs_vnode_cache.c,v 1.1.6.3 2012/10/30 17:22:58 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.3 yamt /* vnode cache is a hashtable for vnodes */
39 1.1.6.3 yamt
40 1.1.6.3 yamt /* chfs_vnocache_hash_init - initializing the hashtable */
41 1.1.6.2 yamt struct chfs_vnode_cache **
42 1.1.6.2 yamt chfs_vnocache_hash_init(void)
43 1.1.6.2 yamt {
44 1.1.6.2 yamt return kmem_zalloc(VNODECACHE_SIZE *
45 1.1.6.2 yamt sizeof(struct chfs_vnode_cache *), KM_SLEEP);
46 1.1.6.2 yamt }
47 1.1.6.2 yamt
48 1.1.6.3 yamt /*
49 1.1.6.3 yamt * chfs_vnode_cache_get - get a vnode_cache from the hashtable
50 1.1.6.2 yamt * Returns the vnode_cache.
51 1.1.6.2 yamt */
52 1.1.6.2 yamt struct chfs_vnode_cache *
53 1.1.6.2 yamt chfs_vnode_cache_get(struct chfs_mount *chmp, ino_t vno)
54 1.1.6.2 yamt {
55 1.1.6.2 yamt struct chfs_vnode_cache* ret;
56 1.1.6.2 yamt
57 1.1.6.2 yamt KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
58 1.1.6.2 yamt
59 1.1.6.2 yamt ret = chmp->chm_vnocache_hash[vno % VNODECACHE_SIZE];
60 1.1.6.2 yamt
61 1.1.6.2 yamt if (ret == NULL) {
62 1.1.6.2 yamt return NULL;
63 1.1.6.2 yamt }
64 1.1.6.2 yamt
65 1.1.6.2 yamt while (ret && ret->vno < vno) {
66 1.1.6.2 yamt ret = ret->next;
67 1.1.6.2 yamt }
68 1.1.6.2 yamt
69 1.1.6.2 yamt if (ret && ret->vno != vno) {
70 1.1.6.2 yamt ret = NULL;
71 1.1.6.2 yamt }
72 1.1.6.2 yamt
73 1.1.6.2 yamt return ret;
74 1.1.6.2 yamt }
75 1.1.6.2 yamt
76 1.1.6.3 yamt /* chfs_vnode_cache_add - add a vnode_cache to the hashtable */
77 1.1.6.2 yamt void
78 1.1.6.2 yamt chfs_vnode_cache_add(struct chfs_mount *chmp,
79 1.1.6.2 yamt struct chfs_vnode_cache* new)
80 1.1.6.2 yamt {
81 1.1.6.2 yamt struct chfs_vnode_cache** prev;
82 1.1.6.2 yamt
83 1.1.6.2 yamt KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
84 1.1.6.2 yamt
85 1.1.6.2 yamt if (!new->vno) {
86 1.1.6.2 yamt new->vno = ++chmp->chm_max_vno;
87 1.1.6.2 yamt }
88 1.1.6.2 yamt
89 1.1.6.2 yamt prev = &chmp->chm_vnocache_hash[new->vno % VNODECACHE_SIZE];
90 1.1.6.2 yamt
91 1.1.6.2 yamt while ((*prev) && (*prev)->vno < new->vno) {
92 1.1.6.2 yamt prev = &((*prev)->next);
93 1.1.6.2 yamt }
94 1.1.6.2 yamt new->next = *prev;
95 1.1.6.2 yamt *prev = new;
96 1.1.6.2 yamt }
97 1.1.6.2 yamt
98 1.1.6.3 yamt /* chfs_vnode_cache_remove - removes a vnode_cache from the hashtable */
99 1.1.6.2 yamt void
100 1.1.6.2 yamt chfs_vnode_cache_remove(struct chfs_mount *chmp,
101 1.1.6.2 yamt struct chfs_vnode_cache* old)
102 1.1.6.2 yamt {
103 1.1.6.2 yamt struct chfs_vnode_cache** prev;
104 1.1.6.2 yamt
105 1.1.6.2 yamt KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
106 1.1.6.2 yamt
107 1.1.6.2 yamt prev = &chmp->chm_vnocache_hash[old->vno % VNODECACHE_SIZE];
108 1.1.6.2 yamt while ((*prev) && (*prev)->vno < old->vno) {
109 1.1.6.2 yamt prev = &(*prev)->next;
110 1.1.6.2 yamt }
111 1.1.6.2 yamt
112 1.1.6.2 yamt if ((*prev) == old) {
113 1.1.6.2 yamt *prev = old->next;
114 1.1.6.2 yamt }
115 1.1.6.2 yamt
116 1.1.6.2 yamt if (old->state != VNO_STATE_READING &&
117 1.1.6.2 yamt old->state != VNO_STATE_CLEARING) {
118 1.1.6.2 yamt chfs_vnode_cache_free(old);
119 1.1.6.2 yamt }
120 1.1.6.2 yamt }
121 1.1.6.2 yamt
122 1.1.6.3 yamt /* chfs_vnocache_hash_destroy - destroying the vnode cache */
123 1.1.6.2 yamt void
124 1.1.6.2 yamt chfs_vnocache_hash_destroy(struct chfs_vnode_cache **hash)
125 1.1.6.2 yamt {
126 1.1.6.2 yamt struct chfs_vnode_cache *this, *next;
127 1.1.6.2 yamt int i;
128 1.1.6.2 yamt
129 1.1.6.3 yamt /* free every row */
130 1.1.6.2 yamt for (i = 0; i < VNODECACHE_SIZE; i++) {
131 1.1.6.2 yamt this = hash[i];
132 1.1.6.2 yamt while (this) {
133 1.1.6.2 yamt next = this->next;
134 1.1.6.2 yamt chfs_vnode_cache_free(this);
135 1.1.6.2 yamt this = next;
136 1.1.6.2 yamt }
137 1.1.6.2 yamt hash[i] = NULL;
138 1.1.6.2 yamt }
139 1.1.6.2 yamt }
140 1.1.6.2 yamt
141