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