blob: d10dc8f397c970e7dacc6ec404bf932c36d0434c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_LINUX__
19#define __XFS_LINUX__
20
21#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/*
Dave Chinner4f3d71f2013-08-12 20:49:54 +100024 * 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
Alex Elder06f8e2d2011-08-12 13:57:55 -050055#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Alex Elder06f8e2d2011-08-12 13:57:55 -050057#include "kmem.h"
58#include "mrlock.h"
59#include "time.h"
60#include "uuid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David Chinnerab4a9b02008-08-13 16:42:10 +100062#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
64#include <linux/kernel.h>
65#include <linux/blkdev.h>
66#include <linux/slab.h>
Christoph Hellwigbc02e862012-11-16 09:20:37 +110067#include <linux/crc32c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/module.h>
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +020069#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/sort.h>
David Chinnere8234a62006-03-14 13:23:52 +110083#include <linux/cpu.h>
84#include <linux/notifier.h>
David Chinner01e1b692006-03-14 13:29:16 +110085#include <linux/delay.h>
Eric Sandeenaf3a2e82007-08-16 16:25:23 +100086#include <linux/log2.h>
Eric Sandeen007c61c2007-10-11 17:43:56 +100087#include <linux/spinlock.h>
David Chinner64275ea2008-04-30 17:11:16 +100088#include <linux/random.h>
Barry Naujok189f4bf2008-05-21 16:58:55 +100089#include <linux/ctype.h>
David Chinner6bfb3d02008-10-30 18:32:43 +110090#include <linux/writeback.h>
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +000091#include <linux/capability.h>
Christoph Hellwig00308072011-10-11 11:14:10 -040092#include <linux/kthread.h>
93#include <linux/freezer.h>
Christoph Hellwig8a072a42011-04-24 19:06:17 +000094#include <linux/list_sort.h>
Rich Johnston3d6e0362013-03-27 09:26:49 -050095#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
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
Dave Chinnerb92cc59f62014-08-04 13:28:20 +1000104#include "xfs_fs.h"
Alex Elder06f8e2d2011-08-12 13:57:55 -0500105#include "xfs_stats.h"
106#include "xfs_sysctl.h"
107#include "xfs_iops.h"
108#include "xfs_aops.h"
109#include "xfs_super.h"
Eric Sandeen51582172014-02-27 15:17:27 +1100110#include "xfs_cksum.h"
Alex Elder06f8e2d2011-08-12 13:57:55 -0500111#include "xfs_buf.h"
112#include "xfs_message.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Christoph Hellwig218106a2011-07-08 14:35:58 +0200114#ifdef __BIG_ENDIAN
115#define XFS_NATIVE_HOST 1
116#else
117#undef XFS_NATIVE_HOST
118#endif
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * Feature macros (disable/enable)
122 */
Nathan Scottd2c32ed2006-03-14 13:20:13 +1100123#ifdef CONFIG_SMP
David Chinner8d280b92006-03-14 13:13:09 +1100124#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define xfs_inherit_nosymlinks xfs_params.inherit_nosym.val
139#define xfs_rotorstep xfs_params.rotorstep.val
Barry Naujokd3446ea2006-06-09 14:54:19 +1000140#define xfs_inherit_nodefrag xfs_params.inherit_nodfrg.val
David Chinner2a82b8b2007-07-11 11:09:12 +1000141#define xfs_fstrm_centisecs xfs_params.fstrm_timer.val
Brian Foster579b62f2012-11-06 09:50:47 -0500142#define xfs_eofb_secs xfs_params.eofb_timer.val
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Nathan Scott59c1b082006-06-09 14:59:13 +1000144#define current_cpu() (raw_smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define current_pid() (current->pid)
Nathan Scott59c1b082006-06-09 14:59:13 +1000146#define current_test_flags(f) (current->flags & (f))
Nathan Scott59c1b082006-06-09 14:59:13 +1000147#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))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Sandeen007c61c2007-10-11 17:43:56 +1000154#define spinlock_destroy(lock)
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define NBBY 8 /* number of bits per byte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/*
159 * Size of block device i/o is parameterized here.
160 * Currently the system supports page-sized i/o.
161 */
Tim Shimmine6a4b372007-11-23 16:30:42 +1100162#define BLKDEV_IOSHIFT PAGE_CACHE_SHIFT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define BLKDEV_IOSIZE (1<<BLKDEV_IOSHIFT)
164/* number of BB's per block device block */
165#define BLKDEV_BB BTOBB(BLKDEV_IOSIZE)
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define ENOATTR ENODATA /* Attribute not found */
Nathan Scottda2f4d62006-06-20 13:01:38 +1000168#define EWRONGFS EINVAL /* Mount with wrong filesystem type */
169#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
Eric Sandeenca23f8f2014-02-27 15:21:07 +1100170#define EFSBADCRC EBADMSG /* Bad CRC detected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172#define SYNCHRONIZE() barrier()
173#define __return_address __builtin_return_address(0)
174
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000175#define XFS_PROJID_DEFAULT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Brian Fostera31b1d32014-07-15 08:07:01 +1000182/*
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 */
188struct xfs_kobj {
189 struct kobject kobject;
190 struct completion complete;
191};
192
Dwight Engenc5eeb7e2013-08-15 14:07:58 -0400193/* 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 */
199static inline __uint32_t xfs_kuid_to_uid(kuid_t uid)
200{
201 return from_kuid(&init_user_ns, uid);
202}
203
204static inline kuid_t xfs_uid_to_kuid(__uint32_t uid)
205{
206 return make_kuid(&init_user_ns, uid);
207}
208
209static inline __uint32_t xfs_kgid_to_gid(kgid_t gid)
210{
211 return from_kgid(&init_user_ns, gid);
212}
213
214static inline kgid_t xfs_gid_to_kgid(__uint32_t gid)
215{
216 return make_kgid(&init_user_ns, gid);
217}
218
Nathan Scottcde410a2005-09-05 11:47:01 +1000219/*
220 * Various platform dependent calls that don't fit anywhere else
221 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100222#define xfs_sort(a,n,s,fn) sort(a,n,s,fn,NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define xfs_stack_trace() dump_stack()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
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 */
233static 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 */
264static 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
289static 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 */
308static 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
329static inline __uint64_t roundup_64(__uint64_t x, __uint32_t y)
330{
331 x += y - 1;
332 do_div(x, y);
Eric Sandeend99831f2014-06-22 15:03:54 +1000333 return x * y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Nathan Scott68c32712006-09-28 11:03:53 +1000336static 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
Eric Sandeenae23a5e2008-06-23 13:23:32 +1000343/* 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
Dave Chinner91300902011-03-07 10:09:35 +1100350#define ASSERT_ALWAYS(expr) \
351 (unlikely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
352
Dave Chinner742ae1e2013-04-30 21:39:34 +1000353#ifdef DEBUG
Dave Chinner91300902011-03-07 10:09:35 +1100354#define ASSERT(expr) \
355 (unlikely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
356
357#ifndef STATIC
358# define STATIC noinline
359#endif
360
Dave Chinner742ae1e2013-04-30 21:39:34 +1000361#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 */
Dave Chinner91300902011-03-07 10:09:35 +1100381#endif /* DEBUG */
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif /* __XFS_LINUX__ */