xref: /freebsd-src/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompat.h (revision 2eb4d8dc723da3cf7d735a3226ae49da4c8c5dbc)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef	_SYS_CCOMPAT_H
29 #define	_SYS_CCOMPAT_H
30 
31 #if  __FreeBSD_version < 1300051
32 #define	vm_page_valid(m) (m)->valid = VM_PAGE_BITS_ALL
33 #define	vm_page_do_sunbusy(m)
34 #define	vm_page_none_valid(m) ((m)->valid == 0)
35 #else
36 #define	vm_page_do_sunbusy(m) vm_page_sunbusy(m)
37 #endif
38 
39 #if  __FreeBSD_version < 1300074
40 #define	VOP_UNLOCK1(x)	VOP_UNLOCK(x, 0)
41 #else
42 #define	VOP_UNLOCK1(x)	VOP_UNLOCK(x)
43 #endif
44 
45 #if  __FreeBSD_version < 1300064
46 #define	VN_IS_DOOMED(vp)	((vp)->v_iflag & VI_DOOMED)
47 #endif
48 
49 #if  __FreeBSD_version < 1300068
50 #define	VFS_VOP_VECTOR_REGISTER(x)
51 #endif
52 
53 #if  __FreeBSD_version >= 1300076
54 #define	getnewvnode_reserve_()	getnewvnode_reserve()
55 #else
56 #define	getnewvnode_reserve_()	getnewvnode_reserve(1)
57 #endif
58 
59 #if  __FreeBSD_version < 1300102
60 #define	ASSERT_VOP_IN_SEQC(zp)
61 #define	MNTK_FPLOOKUP 0
62 #define	vn_seqc_write_begin(vp)
63 #define	vn_seqc_write_end(vp)
64 
65 #ifndef VFS_SMR_DECLARE
66 #define	VFS_SMR_DECLARE
67 #endif
68 #ifndef VFS_SMR_ZONE_SET
69 #define	VFS_SMR_ZONE_SET(zone)
70 #endif
71 #endif
72 
73 struct hlist_node {
74 	struct hlist_node *next, **pprev;
75 };
76 
77 struct hlist_head {
78 	struct hlist_node *first;
79 };
80 
81 typedef struct {
82 	volatile int counter;
83 } atomic_t;
84 
85 	/* BEGIN CSTYLED */
86 #define	hlist_for_each(p, head)                                      \
87 	for (p = (head)->first; p; p = (p)->next)
88 
89 #define	hlist_entry(ptr, type, field)   container_of(ptr, type, field)
90 
91 #define	container_of(ptr, type, member)                         \
92 ({                                                              \
93         const __typeof(((type *)0)->member) *__p = (ptr);       \
94         (type *)((uintptr_t)__p - offsetof(type, member));      \
95 })
96 	/* END CSTYLED */
97 
98 static inline void
99 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
100 {
101 	n->next = h->first;
102 	if (h->first != NULL)
103 		h->first->pprev = &n->next;
104 	WRITE_ONCE(h->first, n);
105 	n->pprev = &h->first;
106 }
107 
108 static inline void
109 hlist_del(struct hlist_node *n)
110 {
111 	WRITE_ONCE(*(n->pprev), n->next);
112 	if (n->next != NULL)
113 		n->next->pprev = n->pprev;
114 }
115 	/* BEGIN CSTYLED */
116 #define	READ_ONCE(x) ({			\
117 	__typeof(x) __var = ({		\
118 		barrier();		\
119 		ACCESS_ONCE(x);		\
120 	});				\
121 	barrier();			\
122 	__var;				\
123 })
124 
125 #define	HLIST_HEAD_INIT { }
126 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
127 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
128 
129 #define	INIT_HLIST_NODE(node)					\
130 	do {																\
131 		(node)->next = NULL;											\
132 		(node)->pprev = NULL;											\
133 	} while (0)
134 
135 /* END CSTYLED */
136 static inline int
137 atomic_read(const atomic_t *v)
138 {
139 	return (READ_ONCE(v->counter));
140 }
141 
142 static inline int
143 atomic_inc(atomic_t *v)
144 {
145 	return (atomic_fetchadd_int(&v->counter, 1) + 1);
146 }
147 
148 static inline int
149 atomic_dec(atomic_t *v)
150 {
151 	return (atomic_fetchadd_int(&v->counter, -1) - 1);
152 }
153 #endif
154