xref: /openbmc/linux/fs/xfs/xfs_linux.h (revision 588b48ca)
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #ifndef __XFS_LINUX__
19 #define __XFS_LINUX__
20 
21 #include <linux/types.h>
22 
23 /*
24  * Kernel specific type declarations for XFS
25  */
26 typedef signed char		__int8_t;
27 typedef unsigned char		__uint8_t;
28 typedef signed short int	__int16_t;
29 typedef unsigned short int	__uint16_t;
30 typedef signed int		__int32_t;
31 typedef unsigned int		__uint32_t;
32 typedef signed long long int	__int64_t;
33 typedef unsigned long long int	__uint64_t;
34 
35 typedef __uint32_t		inst_t;		/* an instruction */
36 
37 typedef __s64			xfs_off_t;	/* <file offset> type */
38 typedef unsigned long long	xfs_ino_t;	/* <inode> type */
39 typedef __s64			xfs_daddr_t;	/* <disk address> type */
40 typedef char *			xfs_caddr_t;	/* <core address> type */
41 typedef __u32			xfs_dev_t;
42 typedef __u32			xfs_nlink_t;
43 
44 /* __psint_t is the same size as a pointer */
45 #if (BITS_PER_LONG == 32)
46 typedef __int32_t __psint_t;
47 typedef __uint32_t __psunsigned_t;
48 #elif (BITS_PER_LONG == 64)
49 typedef __int64_t __psint_t;
50 typedef __uint64_t __psunsigned_t;
51 #else
52 #error BITS_PER_LONG must be 32 or 64
53 #endif
54 
55 #include "xfs_types.h"
56 
57 #include "kmem.h"
58 #include "mrlock.h"
59 #include "time.h"
60 #include "uuid.h"
61 
62 #include <linux/semaphore.h>
63 #include <linux/mm.h>
64 #include <linux/kernel.h>
65 #include <linux/blkdev.h>
66 #include <linux/slab.h>
67 #include <linux/crc32c.h>
68 #include <linux/module.h>
69 #include <linux/mutex.h>
70 #include <linux/file.h>
71 #include <linux/swap.h>
72 #include <linux/errno.h>
73 #include <linux/sched.h>
74 #include <linux/bitops.h>
75 #include <linux/major.h>
76 #include <linux/pagemap.h>
77 #include <linux/vfs.h>
78 #include <linux/seq_file.h>
79 #include <linux/init.h>
80 #include <linux/list.h>
81 #include <linux/proc_fs.h>
82 #include <linux/sort.h>
83 #include <linux/cpu.h>
84 #include <linux/notifier.h>
85 #include <linux/delay.h>
86 #include <linux/log2.h>
87 #include <linux/spinlock.h>
88 #include <linux/random.h>
89 #include <linux/ctype.h>
90 #include <linux/writeback.h>
91 #include <linux/capability.h>
92 #include <linux/kthread.h>
93 #include <linux/freezer.h>
94 #include <linux/list_sort.h>
95 #include <linux/ratelimit.h>
96 
97 #include <asm/page.h>
98 #include <asm/div64.h>
99 #include <asm/param.h>
100 #include <asm/uaccess.h>
101 #include <asm/byteorder.h>
102 #include <asm/unaligned.h>
103 
104 #include "xfs_fs.h"
105 #include "xfs_stats.h"
106 #include "xfs_sysctl.h"
107 #include "xfs_iops.h"
108 #include "xfs_aops.h"
109 #include "xfs_super.h"
110 #include "xfs_cksum.h"
111 #include "xfs_buf.h"
112 #include "xfs_message.h"
113 
114 #ifdef __BIG_ENDIAN
115 #define XFS_NATIVE_HOST 1
116 #else
117 #undef XFS_NATIVE_HOST
118 #endif
119 
120 /*
121  * Feature macros (disable/enable)
122  */
123 #ifdef CONFIG_SMP
124 #define HAVE_PERCPU_SB	/* per cpu superblock counters are a 2.6 feature */
125 #else
126 #undef  HAVE_PERCPU_SB	/* per cpu superblock counters are a 2.6 feature */
127 #endif
128 
129 #define irix_sgid_inherit	xfs_params.sgid_inherit.val
130 #define irix_symlink_mode	xfs_params.symlink_mode.val
131 #define xfs_panic_mask		xfs_params.panic_mask.val
132 #define xfs_error_level		xfs_params.error_level.val
133 #define xfs_syncd_centisecs	xfs_params.syncd_timer.val
134 #define xfs_stats_clear		xfs_params.stats_clear.val
135 #define xfs_inherit_sync	xfs_params.inherit_sync.val
136 #define xfs_inherit_nodump	xfs_params.inherit_nodump.val
137 #define xfs_inherit_noatime	xfs_params.inherit_noatim.val
138 #define xfs_inherit_nosymlinks	xfs_params.inherit_nosym.val
139 #define xfs_rotorstep		xfs_params.rotorstep.val
140 #define xfs_inherit_nodefrag	xfs_params.inherit_nodfrg.val
141 #define xfs_fstrm_centisecs	xfs_params.fstrm_timer.val
142 #define xfs_eofb_secs		xfs_params.eofb_timer.val
143 
144 #define current_cpu()		(raw_smp_processor_id())
145 #define current_pid()		(current->pid)
146 #define current_test_flags(f)	(current->flags & (f))
147 #define current_set_flags_nested(sp, f)		\
148 		(*(sp) = current->flags, current->flags |= (f))
149 #define current_clear_flags_nested(sp, f)	\
150 		(*(sp) = current->flags, current->flags &= ~(f))
151 #define current_restore_flags_nested(sp, f)	\
152 		(current->flags = ((current->flags & ~(f)) | (*(sp) & (f))))
153 
154 #define spinlock_destroy(lock)
155 
156 #define NBBY		8		/* number of bits per byte */
157 
158 /*
159  * Size of block device i/o is parameterized here.
160  * Currently the system supports page-sized i/o.
161  */
162 #define	BLKDEV_IOSHIFT		PAGE_CACHE_SHIFT
163 #define	BLKDEV_IOSIZE		(1<<BLKDEV_IOSHIFT)
164 /* number of BB's per block device block */
165 #define	BLKDEV_BB		BTOBB(BLKDEV_IOSIZE)
166 
167 #define ENOATTR		ENODATA		/* Attribute not found */
168 #define EWRONGFS	EINVAL		/* Mount with wrong filesystem type */
169 #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
170 #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
171 
172 #define SYNCHRONIZE()	barrier()
173 #define __return_address __builtin_return_address(0)
174 
175 #define XFS_PROJID_DEFAULT	0
176 #define MAXPATHLEN	1024
177 
178 #define MIN(a,b)	(min(a,b))
179 #define MAX(a,b)	(max(a,b))
180 #define howmany(x, y)	(((x)+((y)-1))/(y))
181 
182 /*
183  * XFS wrapper structure for sysfs support. It depends on external data
184  * structures and is embedded in various internal data structures to implement
185  * the XFS sysfs object heirarchy. Define it here for broad access throughout
186  * the codebase.
187  */
188 struct xfs_kobj {
189 	struct kobject		kobject;
190 	struct completion	complete;
191 };
192 
193 /* Kernel uid/gid conversion. These are used to convert to/from the on disk
194  * uid_t/gid_t types to the kuid_t/kgid_t types that the kernel uses internally.
195  * The conversion here is type only, the value will remain the same since we
196  * are converting to the init_user_ns. The uid is later mapped to a particular
197  * user namespace value when crossing the kernel/user boundary.
198  */
199 static inline __uint32_t xfs_kuid_to_uid(kuid_t uid)
200 {
201 	return from_kuid(&init_user_ns, uid);
202 }
203 
204 static inline kuid_t xfs_uid_to_kuid(__uint32_t uid)
205 {
206 	return make_kuid(&init_user_ns, uid);
207 }
208 
209 static inline __uint32_t xfs_kgid_to_gid(kgid_t gid)
210 {
211 	return from_kgid(&init_user_ns, gid);
212 }
213 
214 static inline kgid_t xfs_gid_to_kgid(__uint32_t gid)
215 {
216 	return make_kgid(&init_user_ns, gid);
217 }
218 
219 /*
220  * Various platform dependent calls that don't fit anywhere else
221  */
222 #define xfs_sort(a,n,s,fn)	sort(a,n,s,fn,NULL)
223 #define xfs_stack_trace()	dump_stack()
224 
225 
226 /* Move the kernel do_div definition off to one side */
227 
228 #if defined __i386__
229 /* For ia32 we need to pull some tricks to get past various versions
230  * of the compiler which do not like us using do_div in the middle
231  * of large functions.
232  */
233 static inline __u32 xfs_do_div(void *a, __u32 b, int n)
234 {
235 	__u32	mod;
236 
237 	switch (n) {
238 		case 4:
239 			mod = *(__u32 *)a % b;
240 			*(__u32 *)a = *(__u32 *)a / b;
241 			return mod;
242 		case 8:
243 			{
244 			unsigned long __upper, __low, __high, __mod;
245 			__u64	c = *(__u64 *)a;
246 			__upper = __high = c >> 32;
247 			__low = c;
248 			if (__high) {
249 				__upper = __high % (b);
250 				__high = __high / (b);
251 			}
252 			asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (b), "0" (__low), "1" (__upper));
253 			asm("":"=A" (c):"a" (__low),"d" (__high));
254 			*(__u64 *)a = c;
255 			return __mod;
256 			}
257 	}
258 
259 	/* NOTREACHED */
260 	return 0;
261 }
262 
263 /* Side effect free 64 bit mod operation */
264 static inline __u32 xfs_do_mod(void *a, __u32 b, int n)
265 {
266 	switch (n) {
267 		case 4:
268 			return *(__u32 *)a % b;
269 		case 8:
270 			{
271 			unsigned long __upper, __low, __high, __mod;
272 			__u64	c = *(__u64 *)a;
273 			__upper = __high = c >> 32;
274 			__low = c;
275 			if (__high) {
276 				__upper = __high % (b);
277 				__high = __high / (b);
278 			}
279 			asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (b), "0" (__low), "1" (__upper));
280 			asm("":"=A" (c):"a" (__low),"d" (__high));
281 			return __mod;
282 			}
283 	}
284 
285 	/* NOTREACHED */
286 	return 0;
287 }
288 #else
289 static inline __u32 xfs_do_div(void *a, __u32 b, int n)
290 {
291 	__u32	mod;
292 
293 	switch (n) {
294 		case 4:
295 			mod = *(__u32 *)a % b;
296 			*(__u32 *)a = *(__u32 *)a / b;
297 			return mod;
298 		case 8:
299 			mod = do_div(*(__u64 *)a, b);
300 			return mod;
301 	}
302 
303 	/* NOTREACHED */
304 	return 0;
305 }
306 
307 /* Side effect free 64 bit mod operation */
308 static inline __u32 xfs_do_mod(void *a, __u32 b, int n)
309 {
310 	switch (n) {
311 		case 4:
312 			return *(__u32 *)a % b;
313 		case 8:
314 			{
315 			__u64	c = *(__u64 *)a;
316 			return do_div(c, b);
317 			}
318 	}
319 
320 	/* NOTREACHED */
321 	return 0;
322 }
323 #endif
324 
325 #undef do_div
326 #define do_div(a, b)	xfs_do_div(&(a), (b), sizeof(a))
327 #define do_mod(a, b)	xfs_do_mod(&(a), (b), sizeof(a))
328 
329 static inline __uint64_t roundup_64(__uint64_t x, __uint32_t y)
330 {
331 	x += y - 1;
332 	do_div(x, y);
333 	return x * y;
334 }
335 
336 static inline __uint64_t howmany_64(__uint64_t x, __uint32_t y)
337 {
338 	x += y - 1;
339 	do_div(x, y);
340 	return x;
341 }
342 
343 /* ARM old ABI has some weird alignment/padding */
344 #if defined(__arm__) && !defined(__ARM_EABI__)
345 #define __arch_pack __attribute__((packed))
346 #else
347 #define __arch_pack
348 #endif
349 
350 #define ASSERT_ALWAYS(expr)	\
351 	(unlikely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
352 
353 #ifdef DEBUG
354 #define ASSERT(expr)	\
355 	(unlikely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
356 
357 #ifndef STATIC
358 # define STATIC noinline
359 #endif
360 
361 #else	/* !DEBUG */
362 
363 #ifdef XFS_WARN
364 
365 #define ASSERT(expr)	\
366 	(unlikely(expr) ? (void)0 : asswarn(#expr, __FILE__, __LINE__))
367 
368 #ifndef STATIC
369 # define STATIC static noinline
370 #endif
371 
372 #else	/* !DEBUG && !XFS_WARN */
373 
374 #define ASSERT(expr)	((void)0)
375 
376 #ifndef STATIC
377 # define STATIC static noinline
378 #endif
379 
380 #endif /* XFS_WARN */
381 #endif /* DEBUG */
382 
383 #endif /* __XFS_LINUX__ */
384