usr/src/uts/common/fs/zfs/sys/refcount.h
author eschrock
Tue, 16 May 2006 11:20:11 -0700
changeset 1986 628267397204
parent 1544 938876158511
child 4787 602d3f97842c
permissions -rw-r--r--
6424405 zpool import destroyed_pool can damage existing pool using same devices
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 789
diff changeset
     5
 * Common Development and Distribution License (the "License").
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 789
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 789
diff changeset
    22
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
#ifndef	_SYS_REFCOUNT_H
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#define	_SYS_REFCOUNT_H
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/inttypes.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/list.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#ifdef	__cplusplus
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
extern "C" {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
 * If the reference is held only by the calling function and not any
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
 * particular object, use FTAG (which is a string) for the holder_tag.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
 * Otherwise, use the object that holds the reference.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 789
diff changeset
    44
#define	FTAG ((char *)__func__)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
#if defined(DEBUG) || !defined(_KERNEL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
typedef struct reference {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
	list_node_t ref_link;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
	void *ref_holder;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
	uint64_t ref_number;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
	uint8_t *ref_removed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
} reference_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
typedef struct refcount {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
	kmutex_t rc_mtx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
	list_t rc_list;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
	list_t rc_removed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
	int64_t rc_count;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
	int64_t rc_removed_count;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
} refcount_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
/* Note: refcount_t should be initialized to zero before use. */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
void refcount_create(refcount_t *rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
void refcount_destroy(refcount_t *rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
void refcount_destroy_many(refcount_t *rc, uint64_t number);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
int refcount_is_zero(refcount_t *rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
int64_t refcount_count(refcount_t *rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
int64_t refcount_add(refcount_t *rc, void *holder_tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
int64_t refcount_remove(refcount_t *rc, void *holder_tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
void refcount_init(void);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
void refcount_fini(void);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
#else /* DEBUG */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
typedef struct refcount {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
	uint64_t rc_count;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
} refcount_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
#define	refcount_create(rc) ((rc)->rc_count = 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
#define	refcount_destroy(rc) ((rc)->rc_count = 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
#define	refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
#define	refcount_is_zero(rc) ((rc)->rc_count == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
#define	refcount_count(rc) ((rc)->rc_count)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
#define	refcount_add(rc, holder) atomic_add_64_nv(&(rc)->rc_count, 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
#define	refcount_remove(rc, holder) atomic_add_64_nv(&(rc)->rc_count, -1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
#define	refcount_add_many(rc, number, holder) \
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
	atomic_add_64_nv(&(rc)->rc_count, number)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
#define	refcount_remove_many(rc, number, holder) \
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	atomic_add_64_nv(&(rc)->rc_count, -number)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
#define	refcount_init()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
#define	refcount_fini()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
#endif /* DEBUG */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
#ifdef	__cplusplus
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
#endif /* _SYS_REFCOUNT_H */