FreeBSD: Fix ZFS so that snapshots under .zfs/snapshot are NFS visible
[zfs.git] / include / os / freebsd / spl / sys / ccompat.h
blobe34bab7e896d773f6a485a14d3f76bc611604055
1 /*
2 * SPDX-License-Identifier: BSD-2-Clause
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.
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.
25 * $FreeBSD$
28 #ifndef _SYS_CCOMPAT_H
29 #define _SYS_CCOMPAT_H
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
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
45 #if __FreeBSD_version < 1300064
46 #define VN_IS_DOOMED(vp) ((vp)->v_iflag & VI_DOOMED)
47 #endif
49 #if __FreeBSD_version < 1300068
50 #define VFS_VOP_VECTOR_REGISTER(x)
51 #endif
53 #if __FreeBSD_version >= 1300076
54 #define getnewvnode_reserve_() getnewvnode_reserve()
55 #else
56 #define getnewvnode_reserve_() getnewvnode_reserve(1)
57 #endif
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)
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
73 struct hlist_node {
74 struct hlist_node *next, **pprev;
77 struct hlist_head {
78 struct hlist_node *first;
81 typedef struct {
82 volatile int counter;
83 } atomic_t;
85 #define hlist_for_each(p, head) \
86 for (p = (head)->first; p; p = (p)->next)
88 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
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)); \
97 static inline void
98 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
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;
107 static inline void
108 hlist_del(struct hlist_node *n)
110 WRITE_ONCE(*(n->pprev), n->next);
111 if (n->next != NULL)
112 n->next->pprev = n->pprev;
114 /* BEGIN CSTYLED */
115 #define READ_ONCE(x) ({ \
116 __typeof(x) __var = ({ \
117 barrier(); \
118 ACCESS_ONCE(x); \
119 }); \
120 barrier(); \
121 __var; \
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
128 #define INIT_HLIST_NODE(node) \
129 do { \
130 (node)->next = NULL; \
131 (node)->pprev = NULL; \
132 } while (0)
134 /* END CSTYLED */
135 static inline int
136 atomic_read(const atomic_t *v)
138 return (READ_ONCE(v->counter));
141 static inline int
142 atomic_inc(atomic_t *v)
144 return (atomic_fetchadd_int(&v->counter, 1) + 1);
147 static inline int
148 atomic_dec(atomic_t *v)
150 return (atomic_fetchadd_int(&v->counter, -1) - 1);
152 #endif