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