Home | History | Annotate | Download | only in zfs
      1   789  ahrens /*
      2   789  ahrens  * CDDL HEADER START
      3   789  ahrens  *
      4   789  ahrens  * The contents of this file are subject to the terms of the
      5  4787  ahrens  * Common Development and Distribution License (the "License").
      6  4787  ahrens  * You may not use this file except in compliance with the License.
      7   789  ahrens  *
      8   789  ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9   789  ahrens  * or http://www.opensolaris.org/os/licensing.
     10   789  ahrens  * See the License for the specific language governing permissions
     11   789  ahrens  * and limitations under the License.
     12   789  ahrens  *
     13   789  ahrens  * When distributing Covered Code, include this CDDL HEADER in each
     14   789  ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15   789  ahrens  * If applicable, add the following below this CDDL HEADER, with the
     16   789  ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
     17   789  ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
     18   789  ahrens  *
     19   789  ahrens  * CDDL HEADER END
     20   789  ahrens  */
     21   789  ahrens /*
     22  4787  ahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     23   789  ahrens  * Use is subject to license terms.
     24   789  ahrens  */
     25   789  ahrens 
     26   789  ahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
     27   789  ahrens 
     28   789  ahrens #include <sys/zfs_context.h>
     29   789  ahrens #include <sys/refcount.h>
     30   789  ahrens 
     31   789  ahrens #if defined(DEBUG) || !defined(_KERNEL)
     32   789  ahrens 
     33   789  ahrens #ifdef _KERNEL
     34   789  ahrens int reference_tracking_enable = FALSE; /* runs out of memory too easily */
     35   789  ahrens #else
     36   789  ahrens int reference_tracking_enable = TRUE;
     37   789  ahrens #endif
     38   789  ahrens int reference_history = 4; /* tunable */
     39   789  ahrens 
     40   789  ahrens static kmem_cache_t *reference_cache;
     41   789  ahrens static kmem_cache_t *reference_history_cache;
     42   789  ahrens 
     43   789  ahrens void
     44   789  ahrens refcount_init(void)
     45   789  ahrens {
     46   789  ahrens 	reference_cache = kmem_cache_create("reference_cache",
     47   789  ahrens 	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
     48   789  ahrens 
     49   789  ahrens 	reference_history_cache = kmem_cache_create("reference_history_cache",
     50   789  ahrens 	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
     51   789  ahrens }
     52   789  ahrens 
     53   789  ahrens void
     54   789  ahrens refcount_fini(void)
     55   789  ahrens {
     56   789  ahrens 	kmem_cache_destroy(reference_cache);
     57   789  ahrens 	kmem_cache_destroy(reference_history_cache);
     58   789  ahrens }
     59   789  ahrens 
     60   789  ahrens void
     61   789  ahrens refcount_create(refcount_t *rc)
     62   789  ahrens {
     63  4787  ahrens 	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
     64   789  ahrens 	list_create(&rc->rc_list, sizeof (reference_t),
     65   789  ahrens 	    offsetof(reference_t, ref_link));
     66   789  ahrens 	list_create(&rc->rc_removed, sizeof (reference_t),
     67   789  ahrens 	    offsetof(reference_t, ref_link));
     68  4787  ahrens 	rc->rc_count = 0;
     69  4787  ahrens 	rc->rc_removed_count = 0;
     70   789  ahrens }
     71   789  ahrens 
     72   789  ahrens void
     73   789  ahrens refcount_destroy_many(refcount_t *rc, uint64_t number)
     74   789  ahrens {
     75   789  ahrens 	reference_t *ref;
     76   789  ahrens 
     77   789  ahrens 	ASSERT(rc->rc_count == number);
     78   789  ahrens 	while (ref = list_head(&rc->rc_list)) {
     79   789  ahrens 		list_remove(&rc->rc_list, ref);
     80   789  ahrens 		kmem_cache_free(reference_cache, ref);
     81   789  ahrens 	}
     82   789  ahrens 	list_destroy(&rc->rc_list);
     83   789  ahrens 
     84   789  ahrens 	while (ref = list_head(&rc->rc_removed)) {
     85   789  ahrens 		list_remove(&rc->rc_removed, ref);
     86   789  ahrens 		kmem_cache_free(reference_history_cache, ref->ref_removed);
     87   789  ahrens 		kmem_cache_free(reference_cache, ref);
     88   789  ahrens 	}
     89   789  ahrens 	list_destroy(&rc->rc_removed);
     90   789  ahrens 	mutex_destroy(&rc->rc_mtx);
     91   789  ahrens }
     92   789  ahrens 
     93   789  ahrens void
     94   789  ahrens refcount_destroy(refcount_t *rc)
     95   789  ahrens {
     96   789  ahrens 	refcount_destroy_many(rc, 0);
     97   789  ahrens }
     98   789  ahrens 
     99   789  ahrens int
    100   789  ahrens refcount_is_zero(refcount_t *rc)
    101   789  ahrens {
    102   789  ahrens 	ASSERT(rc->rc_count >= 0);
    103   789  ahrens 	return (rc->rc_count == 0);
    104   789  ahrens }
    105   789  ahrens 
    106   789  ahrens int64_t
    107   789  ahrens refcount_count(refcount_t *rc)
    108   789  ahrens {
    109   789  ahrens 	ASSERT(rc->rc_count >= 0);
    110   789  ahrens 	return (rc->rc_count);
    111   789  ahrens }
    112   789  ahrens 
    113   789  ahrens int64_t
    114   789  ahrens refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
    115   789  ahrens {
    116   789  ahrens 	reference_t *ref;
    117   789  ahrens 	int64_t count;
    118   789  ahrens 
    119   789  ahrens 	if (reference_tracking_enable) {
    120   789  ahrens 		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
    121   789  ahrens 		ref->ref_holder = holder;
    122   789  ahrens 		ref->ref_number = number;
    123   789  ahrens 	}
    124   789  ahrens 	mutex_enter(&rc->rc_mtx);
    125   789  ahrens 	ASSERT(rc->rc_count >= 0);
    126   789  ahrens 	if (reference_tracking_enable)
    127   789  ahrens 		list_insert_head(&rc->rc_list, ref);
    128   789  ahrens 	rc->rc_count += number;
    129   789  ahrens 	count = rc->rc_count;
    130   789  ahrens 	mutex_exit(&rc->rc_mtx);
    131   789  ahrens 
    132   789  ahrens 	return (count);
    133   789  ahrens }
    134   789  ahrens 
    135   789  ahrens int64_t
    136   789  ahrens refcount_add(refcount_t *rc, void *holder)
    137   789  ahrens {
    138   789  ahrens 	return (refcount_add_many(rc, 1, holder));
    139   789  ahrens }
    140   789  ahrens 
    141   789  ahrens int64_t
    142   789  ahrens refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
    143   789  ahrens {
    144   789  ahrens 	reference_t *ref;
    145   789  ahrens 	int64_t count;
    146   789  ahrens 
    147   789  ahrens 	mutex_enter(&rc->rc_mtx);
    148   789  ahrens 	ASSERT(rc->rc_count >= number);
    149   789  ahrens 
    150   789  ahrens 	if (!reference_tracking_enable) {
    151   789  ahrens 		rc->rc_count -= number;
    152   789  ahrens 		count = rc->rc_count;
    153   789  ahrens 		mutex_exit(&rc->rc_mtx);
    154   789  ahrens 		return (count);
    155   789  ahrens 	}
    156   789  ahrens 
    157   789  ahrens 	for (ref = list_head(&rc->rc_list); ref;
    158   789  ahrens 	    ref = list_next(&rc->rc_list, ref)) {
    159   789  ahrens 		if (ref->ref_holder == holder && ref->ref_number == number) {
    160   789  ahrens 			list_remove(&rc->rc_list, ref);
    161   789  ahrens 			if (reference_history > 0) {
    162   789  ahrens 				ref->ref_removed =
    163   789  ahrens 				    kmem_cache_alloc(reference_history_cache,
    164   789  ahrens 				    KM_SLEEP);
    165   789  ahrens 				list_insert_head(&rc->rc_removed, ref);
    166   789  ahrens 				rc->rc_removed_count++;
    167   789  ahrens 				if (rc->rc_removed_count >= reference_history) {
    168   789  ahrens 					ref = list_tail(&rc->rc_removed);
    169   789  ahrens 					list_remove(&rc->rc_removed, ref);
    170   789  ahrens 					kmem_cache_free(reference_history_cache,
    171   789  ahrens 					    ref->ref_removed);
    172   789  ahrens 					kmem_cache_free(reference_cache, ref);
    173   789  ahrens 					rc->rc_removed_count--;
    174   789  ahrens 				}
    175   789  ahrens 			} else {
    176   789  ahrens 				kmem_cache_free(reference_cache, ref);
    177   789  ahrens 			}
    178   789  ahrens 			rc->rc_count -= number;
    179   789  ahrens 			count = rc->rc_count;
    180   789  ahrens 			mutex_exit(&rc->rc_mtx);
    181   789  ahrens 			return (count);
    182   789  ahrens 		}
    183   789  ahrens 	}
    184   789  ahrens 	panic("No such hold %p on refcount %llx", holder,
    185   789  ahrens 	    (u_longlong_t)(uintptr_t)rc);
    186   789  ahrens 	return (-1);
    187   789  ahrens }
    188   789  ahrens 
    189   789  ahrens int64_t
    190   789  ahrens refcount_remove(refcount_t *rc, void *holder)
    191   789  ahrens {
    192   789  ahrens 	return (refcount_remove_many(rc, 1, holder));
    193   789  ahrens }
    194   789  ahrens 
    195   789  ahrens #endif
    196