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