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