1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25/*
26 * Copyright (c) 2018 by Delphix. All rights reserved.
27 */
28
29#ifndef	_SYS_FS_ZFS_RLOCK_H
30#define	_SYS_FS_ZFS_RLOCK_H
31
32#ifdef	__cplusplus
33extern "C" {
34#endif
35
36#ifdef __FreeBSD__
37#define	rangelock_init		zfs_rangelock_init
38#define	rangelock_fini		zfs_rangelock_fini
39#endif
40
41typedef enum {
42	RL_READER,
43	RL_WRITER,
44	RL_APPEND
45} rangelock_type_t;
46
47struct locked_range;
48
49typedef void (rangelock_cb_t)(struct locked_range *, void *);
50
51#ifdef __FreeBSD__
52typedef struct zfs_rangelock {
53#else
54typedef struct rangelock {
55#endif
56	avl_tree_t rl_tree; /* contains locked_range_t */
57	kmutex_t rl_lock;
58	rangelock_cb_t *rl_cb;
59	void *rl_arg;
60} rangelock_t;
61
62typedef struct locked_range {
63	rangelock_t *lr_rangelock; /* rangelock that this lock applies to */
64	avl_node_t lr_node;	/* avl node link */
65	uint64_t lr_offset;	/* file range offset */
66	uint64_t lr_length;	/* file range length */
67	uint_t lr_count;	/* range reference count in tree */
68	rangelock_type_t lr_type; /* range type */
69	kcondvar_t lr_write_cv;	/* cv for waiting writers */
70	kcondvar_t lr_read_cv;	/* cv for waiting readers */
71	uint8_t lr_proxy;	/* acting for original range */
72	uint8_t lr_write_wanted; /* writer wants to lock this range */
73	uint8_t lr_read_wanted;	/* reader wants to lock this range */
74} locked_range_t;
75
76void rangelock_init(rangelock_t *, rangelock_cb_t *, void *);
77void rangelock_fini(rangelock_t *);
78
79locked_range_t *rangelock_enter(rangelock_t *,
80    uint64_t, uint64_t, rangelock_type_t);
81void rangelock_exit(locked_range_t *);
82void rangelock_reduce(locked_range_t *, uint64_t, uint64_t);
83
84#ifdef	__cplusplus
85}
86#endif
87
88#endif	/* _SYS_FS_ZFS_RLOCK_H */
89