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