1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
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 #define	hlist_for_each(p, head)                                      \
86 	for (p = (head)->first; p; p = (p)->next)
87 
88 #define	hlist_entry(ptr, type, field)   container_of(ptr, type, field)
89 
90 #define	container_of(ptr, type, member)                         \
91 /* CSTYLED */                                                   \
92 ({                                                              \
93 	const __typeof(((type *)0)->member) *__p = (ptr);       \
94 	(type *)((uintptr_t)__p - offsetof(type, member));      \
95 })
96 
97 static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)98 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
99 {
100 	n->next = h->first;
101 	if (h->first != NULL)
102 		h->first->pprev = &n->next;
103 	WRITE_ONCE(h->first, n);
104 	n->pprev = &h->first;
105 }
106 
107 static inline void
hlist_del(struct hlist_node * n)108 hlist_del(struct hlist_node *n)
109 {
110 	WRITE_ONCE(*(n->pprev), n->next);
111 	if (n->next != NULL)
112 		n->next->pprev = n->pprev;
113 }
114 	/* BEGIN CSTYLED */
115 #define	READ_ONCE(x) ({			\
116 	__typeof(x) __var = ({		\
117 		barrier();		\
118 		ACCESS_ONCE(x);		\
119 	});				\
120 	barrier();			\
121 	__var;				\
122 })
123 
124 #define	HLIST_HEAD_INIT { }
125 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
126 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
127 
128 #define	INIT_HLIST_NODE(node)					\
129 	do {																\
130 		(node)->next = NULL;											\
131 		(node)->pprev = NULL;											\
132 	} while (0)
133 
134 /* END CSTYLED */
135 static inline int
atomic_read(const atomic_t * v)136 atomic_read(const atomic_t *v)
137 {
138 	return (READ_ONCE(v->counter));
139 }
140 
141 static inline int
atomic_inc(atomic_t * v)142 atomic_inc(atomic_t *v)
143 {
144 	return (atomic_fetchadd_int(&v->counter, 1) + 1);
145 }
146 
147 static inline int
atomic_dec(atomic_t * v)148 atomic_dec(atomic_t *v)
149 {
150 	return (atomic_fetchadd_int(&v->counter, -1) - 1);
151 }
152 #endif
153