1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #ifndef _XFS_PLATFORM_H
7 #define _XFS_PLATFORM_H
8
9 #include <linux/types.h>
10 #include <linux/uuid.h>
11 #include <linux/semaphore.h>
12 #include <linux/mm.h>
13 #include <linux/sched/mm.h>
14 #include <linux/kernel.h>
15 #include <linux/blkdev.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/crc32c.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/file.h>
22 #include <linux/filelock.h>
23 #include <linux/swap.h>
24 #include <linux/errno.h>
25 #include <linux/sched/signal.h>
26 #include <linux/bitops.h>
27 #include <linux/major.h>
28 #include <linux/pagemap.h>
29 #include <linux/vfs.h>
30 #include <linux/seq_file.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/proc_fs.h>
34 #include <linux/sort.h>
35 #include <linux/cpu.h>
36 #include <linux/notifier.h>
37 #include <linux/delay.h>
38 #include <linux/log2.h>
39 #include <linux/rwsem.h>
40 #include <linux/spinlock.h>
41 #include <linux/random.h>
42 #include <linux/ctype.h>
43 #include <linux/writeback.h>
44 #include <linux/capability.h>
45 #include <linux/kthread.h>
46 #include <linux/freezer.h>
47 #include <linux/list_sort.h>
48 #include <linux/ratelimit.h>
49 #include <linux/rhashtable.h>
50 #include <linux/xattr.h>
51 #include <linux/mnt_idmapping.h>
52 #include <linux/debugfs.h>
53 #include <asm/page.h>
54 #include <asm/div64.h>
55 #include <asm/param.h>
56 #include <linux/uaccess.h>
57 #include <asm/byteorder.h>
58 #include <linux/unaligned.h>
59
60 #ifdef CONFIG_XFS_DEBUG
61 #define DEBUG 1
62 #endif
63
64 #ifdef CONFIG_XFS_DEBUG_EXPENSIVE
65 #define DEBUG_EXPENSIVE 1
66 #endif
67
68 #ifdef CONFIG_XFS_ASSERT_FATAL
69 #define XFS_ASSERT_FATAL 1
70 #endif
71
72 #ifdef CONFIG_XFS_WARN
73 #define XFS_WARN 1
74 #endif
75
76 /*
77 * Kernel specific type declarations for XFS
78 */
79 typedef __s64 xfs_off_t; /* <file offset> type */
80 typedef unsigned long long xfs_ino_t; /* <inode> type */
81 typedef __s64 xfs_daddr_t; /* <disk address> type */
82 typedef __u32 xfs_dev_t;
83 typedef __u32 xfs_nlink_t;
84
85 #include "xfs_types.h"
86 #include "xfs_fs.h"
87 #include "xfs_stats.h"
88 #include "xfs_sysctl.h"
89 #include "xfs_iops.h"
90 #include "xfs_aops.h"
91 #include "xfs_super.h"
92 #include "xfs_cksum.h"
93 #include "xfs_buf.h"
94 #include "xfs_message.h"
95 #include "xfs_drain.h"
96 #include "xfs_hooks.h"
97
98 #ifdef __BIG_ENDIAN
99 #define XFS_NATIVE_HOST 1
100 #else
101 #undef XFS_NATIVE_HOST
102 #endif
103
104 #define xfs_panic_mask xfs_params.panic_mask.val
105 #define xfs_error_level xfs_params.error_level.val
106 #define xfs_syncd_centisecs xfs_params.syncd_timer.val
107 #define xfs_stats_clear xfs_params.stats_clear.val
108 #define xfs_inherit_sync xfs_params.inherit_sync.val
109 #define xfs_inherit_nodump xfs_params.inherit_nodump.val
110 #define xfs_inherit_noatime xfs_params.inherit_noatim.val
111 #define xfs_inherit_nosymlinks xfs_params.inherit_nosym.val
112 #define xfs_rotorstep xfs_params.rotorstep.val
113 #define xfs_inherit_nodefrag xfs_params.inherit_nodfrg.val
114 #define xfs_fstrm_centisecs xfs_params.fstrm_timer.val
115 #define xfs_blockgc_secs xfs_params.blockgc_timer.val
116
117 #define current_cpu() (raw_smp_processor_id())
118 #define current_set_flags_nested(sp, f) \
119 (*(sp) = current->flags, current->flags |= (f))
120 #define current_restore_flags_nested(sp, f) \
121 (current->flags = ((current->flags & ~(f)) | (*(sp) & (f))))
122
123 #define NBBY 8 /* number of bits per byte */
124
125 /*
126 * Size of block device i/o is parameterized here.
127 * Currently the system supports page-sized i/o.
128 */
129 #define BLKDEV_IOSHIFT PAGE_SHIFT
130 #define BLKDEV_IOSIZE (1<<BLKDEV_IOSHIFT)
131 /* number of BB's per block device block */
132 #define BLKDEV_BB BTOBB(BLKDEV_IOSIZE)
133
134 #define ENOATTR ENODATA /* Attribute not found */
135 #define EWRONGFS EINVAL /* Mount with wrong filesystem type */
136
137 #define __return_address __builtin_return_address(0)
138
139 /*
140 * Return the address of a label. Use barrier() so that the optimizer
141 * won't reorder code to refactor the error jumpouts into a single
142 * return, which throws off the reported address.
143 */
144 #define __this_address ({ __label__ __here; __here: barrier(); &&__here; })
145
146 #define howmany(x, y) (((x)+((y)-1))/(y))
147
delay(long ticks)148 static inline void delay(long ticks)
149 {
150 schedule_timeout_uninterruptible(ticks);
151 }
152
153 /*
154 * XFS wrapper structure for sysfs support. It depends on external data
155 * structures and is embedded in various internal data structures to implement
156 * the XFS sysfs object heirarchy. Define it here for broad access throughout
157 * the codebase.
158 */
159 struct xfs_kobj {
160 struct kobject kobject;
161 struct completion complete;
162 };
163
164 struct xstats {
165 struct xfsstats __percpu *xs_stats;
166 struct xfs_kobj xs_kobj;
167 };
168
169 extern struct xstats xfsstats;
170
xfs_to_linux_dev_t(xfs_dev_t dev)171 static inline dev_t xfs_to_linux_dev_t(xfs_dev_t dev)
172 {
173 return MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
174 }
175
linux_to_xfs_dev_t(dev_t dev)176 static inline xfs_dev_t linux_to_xfs_dev_t(dev_t dev)
177 {
178 return sysv_encode_dev(dev);
179 }
180
181 /*
182 * Various platform dependent calls that don't fit anywhere else
183 */
184 #define xfs_sort(a,n,s,fn) sort(a,n,s,fn,NULL)
185 #define xfs_stack_trace() dump_stack()
186
rounddown_64(uint64_t x,uint32_t y)187 static inline uint64_t rounddown_64(uint64_t x, uint32_t y)
188 {
189 do_div(x, y);
190 return x * y;
191 }
192
roundup_64(uint64_t x,uint32_t y)193 static inline uint64_t roundup_64(uint64_t x, uint32_t y)
194 {
195 x += y - 1;
196 do_div(x, y);
197 return x * y;
198 }
199
howmany_64(uint64_t x,uint32_t y)200 static inline uint64_t howmany_64(uint64_t x, uint32_t y)
201 {
202 x += y - 1;
203 do_div(x, y);
204 return x;
205 }
206
isaligned_64(uint64_t x,uint32_t y)207 static inline bool isaligned_64(uint64_t x, uint32_t y)
208 {
209 return do_div(x, y) == 0;
210 }
211
212 /* If @b is a power of 2, return log2(b). Else return -1. */
log2_if_power2(unsigned long b)213 static inline int8_t log2_if_power2(unsigned long b)
214 {
215 return is_power_of_2(b) ? ilog2(b) : -1;
216 }
217
218 /* If @b is a power of 2, return a mask of the lower bits, else return zero. */
mask64_if_power2(unsigned long b)219 static inline unsigned long long mask64_if_power2(unsigned long b)
220 {
221 return is_power_of_2(b) ? b - 1 : 0;
222 }
223
224 int xfs_rw_bdev(struct block_device *bdev, sector_t sector, unsigned int count,
225 char *data, enum req_op op);
226
227 #define ASSERT_ALWAYS(expr) \
228 (likely(expr) ? (void)0 : assfail(NULL, #expr, __FILE__, __LINE__))
229
230 #ifdef DEBUG
231 #define ASSERT(expr) \
232 (likely(expr) ? (void)0 : assfail(NULL, #expr, __FILE__, __LINE__))
233
234 #else /* !DEBUG */
235
236 #ifdef XFS_WARN
237
238 /*
239 * Please note that this ASSERT doesn't kill the kernel. It will if the kernel
240 * has panic_on_warn set.
241 */
242 #define ASSERT(expr) \
243 (likely(expr) ? (void)0 : asswarn(NULL, #expr, __FILE__, __LINE__))
244
245 #else /* !DEBUG && !XFS_WARN */
246
247 #define ASSERT(expr) ((void)0)
248
249 #endif /* XFS_WARN */
250 #endif /* DEBUG */
251
252 /*
253 * Use this to catch metadata corruptions that are not caught by block or
254 * structure verifiers. The reason is that the verifiers check corruptions only
255 * within the scope of the object being verified.
256 */
257 #define XFS_IS_CORRUPT(mp, expr) \
258 (unlikely(expr) ? xfs_corruption_error(#expr, XFS_ERRLEVEL_LOW, (mp), \
259 NULL, 0, __FILE__, __LINE__, \
260 __this_address), \
261 true : false)
262
263 #define STATIC static noinline
264
265 #ifdef CONFIG_XFS_RT
266
267 /*
268 * make sure we ignore the inode flag if the filesystem doesn't have a
269 * configured realtime device.
270 */
271 #define XFS_IS_REALTIME_INODE(ip) \
272 (((ip)->i_diflags & XFS_DIFLAG_REALTIME) && \
273 (ip)->i_mount->m_rtdev_targp)
274 #define XFS_IS_REALTIME_MOUNT(mp) ((mp)->m_rtdev_targp ? 1 : 0)
275 #else
276 #define XFS_IS_REALTIME_INODE(ip) (0)
277 #define XFS_IS_REALTIME_MOUNT(mp) (0)
278 #endif
279
280 /*
281 * Starting in Linux 4.15, the %p (raw pointer value) printk modifier
282 * prints a hashed version of the pointer to avoid leaking kernel
283 * pointers into dmesg. If we're trying to debug the kernel we want the
284 * raw values, so override this behavior as best we can.
285 */
286 #ifdef DEBUG
287 # define PTR_FMT "%px"
288 #else
289 # define PTR_FMT "%p"
290 #endif
291
292 /*
293 * Helper for IO routines to grab backing pages from allocated kernel memory.
294 */
295 static inline struct page *
kmem_to_page(void * addr)296 kmem_to_page(void *addr)
297 {
298 if (is_vmalloc_addr(addr))
299 return vmalloc_to_page(addr);
300 return virt_to_page(addr);
301 }
302
303 #endif /* _XFS_PLATFORM_H */
304