]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/include/sys/zfs_rlock.h
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.8-0ubuntu1, zfs to 0.6.5.8-0ubuntu1
[mirror_ubuntu-zesty-kernel.git] / zfs / include / sys / zfs_rlock.h
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 #ifndef _SYS_FS_ZFS_RLOCK_H
27 #define _SYS_FS_ZFS_RLOCK_H
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #ifdef _KERNEL
34
35 #include <sys/list.h>
36 #include <sys/avl.h>
37 #include <sys/condvar.h>
38
39 typedef enum {
40 RL_READER,
41 RL_WRITER,
42 RL_APPEND
43 } rl_type_t;
44
45 typedef struct zfs_rlock {
46 kmutex_t zr_mutex; /* protects changes to zr_avl */
47 avl_tree_t zr_avl; /* avl tree of range locks */
48 uint64_t *zr_size; /* points to znode->z_size */
49 uint_t *zr_blksz; /* points to znode->z_blksz */
50 uint64_t *zr_max_blksz; /* points to zsb->z_max_blksz */
51 } zfs_rlock_t;
52
53 typedef struct rl {
54 zfs_rlock_t *r_zrl;
55 avl_node_t r_node; /* avl node link */
56 uint64_t r_off; /* file range offset */
57 uint64_t r_len; /* file range length */
58 uint_t r_cnt; /* range reference count in tree */
59 rl_type_t r_type; /* range type */
60 kcondvar_t r_wr_cv; /* cv for waiting writers */
61 kcondvar_t r_rd_cv; /* cv for waiting readers */
62 uint8_t r_proxy; /* acting for original range */
63 uint8_t r_write_wanted; /* writer wants to lock this range */
64 uint8_t r_read_wanted; /* reader wants to lock this range */
65 list_node_t rl_node; /* used for deferred release */
66 } rl_t;
67
68 /*
69 * Lock a range (offset, length) as either shared (RL_READER)
70 * or exclusive (RL_WRITER or RL_APPEND). RL_APPEND is a special type that
71 * is converted to RL_WRITER that specified to lock from the start of the
72 * end of file. Returns the range lock structure.
73 */
74 rl_t *zfs_range_lock(zfs_rlock_t *zrl, uint64_t off, uint64_t len,
75 rl_type_t type);
76
77 /* Unlock range and destroy range lock structure. */
78 void zfs_range_unlock(rl_t *rl);
79
80 /*
81 * Reduce range locked as RW_WRITER from whole file to specified range.
82 * Asserts the whole file was previously locked.
83 */
84 void zfs_range_reduce(rl_t *rl, uint64_t off, uint64_t len);
85
86 /*
87 * AVL comparison function used to order range locks
88 * Locks are ordered on the start offset of the range.
89 */
90 int zfs_range_compare(const void *arg1, const void *arg2);
91
92 static inline void
93 zfs_rlock_init(zfs_rlock_t *zrl)
94 {
95 mutex_init(&zrl->zr_mutex, NULL, MUTEX_DEFAULT, NULL);
96 avl_create(&zrl->zr_avl, zfs_range_compare,
97 sizeof (rl_t), offsetof(rl_t, r_node));
98 zrl->zr_size = NULL;
99 zrl->zr_blksz = NULL;
100 zrl->zr_max_blksz = NULL;
101 }
102
103 static inline void
104 zfs_rlock_destroy(zfs_rlock_t *zrl)
105 {
106 avl_destroy(&zrl->zr_avl);
107 mutex_destroy(&zrl->zr_mutex);
108 }
109 #endif /* _KERNEL */
110
111 #ifdef __cplusplus
112 }
113 #endif
114
115 #endif /* _SYS_FS_ZFS_RLOCK_H */