| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved. | 
 | 3 |  * | 
 | 4 |  * This program is free software; you can redistribute it and/or modify it | 
 | 5 |  * under the terms of version 2 of the GNU General Public License as | 
 | 6 |  * published by the Free Software Foundation. | 
 | 7 |  * | 
 | 8 |  * This program is distributed in the hope that it would be useful, but | 
 | 9 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 10 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
 | 11 |  * | 
 | 12 |  * Further, this software is distributed without any warranty that it is | 
 | 13 |  * free of the rightful claim of any third person regarding infringement | 
 | 14 |  * or the like.  Any license provided herein, whether implied or | 
 | 15 |  * otherwise, applies only to this software file.  Patent licenses, if | 
 | 16 |  * any, provided herein do not apply to combinations of this program with | 
 | 17 |  * other software, or any other product whatsoever. | 
 | 18 |  * | 
 | 19 |  * You should have received a copy of the GNU General Public License along | 
 | 20 |  * with this program; if not, write the Free Software Foundation, Inc., 59 | 
 | 21 |  * Temple Place - Suite 330, Boston MA 02111-1307, USA. | 
 | 22 |  * | 
 | 23 |  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | 
 | 24 |  * Mountain View, CA  94043, or: | 
 | 25 |  * | 
 | 26 |  * http://www.sgi.com | 
 | 27 |  * | 
 | 28 |  * For further information regarding this notice, see: | 
 | 29 |  * | 
 | 30 |  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ | 
 | 31 |  */ | 
 | 32 | #ifndef __XFS_LINUX__ | 
 | 33 | #define __XFS_LINUX__ | 
 | 34 |  | 
 | 35 | #include <linux/types.h> | 
 | 36 | #include <linux/config.h> | 
 | 37 |  | 
 | 38 | /* | 
 | 39 |  * Some types are conditional depending on the target system. | 
 | 40 |  * XFS_BIG_BLKNOS needs block layer disk addresses to be 64 bits. | 
 | 41 |  * XFS_BIG_INUMS needs the VFS inode number to be 64 bits, as well | 
 | 42 |  * as requiring XFS_BIG_BLKNOS to be set. | 
 | 43 |  */ | 
 | 44 | #if defined(CONFIG_LBD) || (BITS_PER_LONG == 64) | 
 | 45 | # define XFS_BIG_BLKNOS	1 | 
 | 46 | # if BITS_PER_LONG == 64 | 
 | 47 | #  define XFS_BIG_INUMS	1 | 
 | 48 | # else | 
 | 49 | #  define XFS_BIG_INUMS	0 | 
 | 50 | # endif | 
 | 51 | #else | 
 | 52 | # define XFS_BIG_BLKNOS	0 | 
 | 53 | # define XFS_BIG_INUMS	0 | 
 | 54 | #endif | 
 | 55 |  | 
 | 56 | #include <xfs_types.h> | 
 | 57 | #include <xfs_arch.h> | 
 | 58 |  | 
 | 59 | #include <kmem.h> | 
 | 60 | #include <mrlock.h> | 
 | 61 | #include <spin.h> | 
 | 62 | #include <sv.h> | 
 | 63 | #include <mutex.h> | 
 | 64 | #include <sema.h> | 
 | 65 | #include <time.h> | 
 | 66 |  | 
 | 67 | #include <support/qsort.h> | 
 | 68 | #include <support/ktrace.h> | 
 | 69 | #include <support/debug.h> | 
 | 70 | #include <support/move.h> | 
 | 71 | #include <support/uuid.h> | 
 | 72 |  | 
 | 73 | #include <linux/mm.h> | 
 | 74 | #include <linux/kernel.h> | 
 | 75 | #include <linux/blkdev.h> | 
 | 76 | #include <linux/slab.h> | 
 | 77 | #include <linux/module.h> | 
 | 78 | #include <linux/file.h> | 
 | 79 | #include <linux/swap.h> | 
 | 80 | #include <linux/errno.h> | 
 | 81 | #include <linux/sched.h> | 
 | 82 | #include <linux/bitops.h> | 
 | 83 | #include <linux/major.h> | 
 | 84 | #include <linux/pagemap.h> | 
 | 85 | #include <linux/vfs.h> | 
 | 86 | #include <linux/seq_file.h> | 
 | 87 | #include <linux/init.h> | 
 | 88 | #include <linux/list.h> | 
 | 89 | #include <linux/proc_fs.h> | 
 | 90 | #include <linux/version.h> | 
 | 91 | #include <linux/sort.h> | 
 | 92 |  | 
 | 93 | #include <asm/page.h> | 
 | 94 | #include <asm/div64.h> | 
 | 95 | #include <asm/param.h> | 
 | 96 | #include <asm/uaccess.h> | 
 | 97 | #include <asm/byteorder.h> | 
 | 98 | #include <asm/unaligned.h> | 
 | 99 |  | 
 | 100 | #include <xfs_behavior.h> | 
 | 101 | #include <xfs_vfs.h> | 
 | 102 | #include <xfs_cred.h> | 
 | 103 | #include <xfs_vnode.h> | 
 | 104 | #include <xfs_stats.h> | 
 | 105 | #include <xfs_sysctl.h> | 
 | 106 | #include <xfs_iops.h> | 
 | 107 | #include <xfs_super.h> | 
 | 108 | #include <xfs_globals.h> | 
 | 109 | #include <xfs_fs_subr.h> | 
 | 110 | #include <xfs_lrw.h> | 
 | 111 | #include <xfs_buf.h> | 
 | 112 |  | 
 | 113 | /* | 
 | 114 |  * Feature macros (disable/enable) | 
 | 115 |  */ | 
 | 116 | #undef  HAVE_REFCACHE	/* reference cache not needed for NFS in 2.6 */ | 
 | 117 | #define HAVE_SENDFILE	/* sendfile(2) exists in 2.6, but not in 2.4 */ | 
 | 118 |  | 
 | 119 | /* | 
 | 120 |  * State flag for unwritten extent buffers. | 
 | 121 |  * | 
 | 122 |  * We need to be able to distinguish between these and delayed | 
 | 123 |  * allocate buffers within XFS.  The generic IO path code does | 
 | 124 |  * not need to distinguish - we use the BH_Delay flag for both | 
 | 125 |  * delalloc and these ondisk-uninitialised buffers. | 
 | 126 |  */ | 
 | 127 | BUFFER_FNS(PrivateStart, unwritten); | 
 | 128 | static inline void set_buffer_unwritten_io(struct buffer_head *bh) | 
 | 129 | { | 
 | 130 | 	bh->b_end_io = linvfs_unwritten_done; | 
 | 131 | } | 
 | 132 |  | 
 | 133 | #define restricted_chown	xfs_params.restrict_chown.val | 
 | 134 | #define irix_sgid_inherit	xfs_params.sgid_inherit.val | 
 | 135 | #define irix_symlink_mode	xfs_params.symlink_mode.val | 
 | 136 | #define xfs_panic_mask		xfs_params.panic_mask.val | 
 | 137 | #define xfs_error_level		xfs_params.error_level.val | 
 | 138 | #define xfs_syncd_centisecs	xfs_params.syncd_timer.val | 
 | 139 | #define xfs_stats_clear		xfs_params.stats_clear.val | 
 | 140 | #define xfs_inherit_sync	xfs_params.inherit_sync.val | 
 | 141 | #define xfs_inherit_nodump	xfs_params.inherit_nodump.val | 
 | 142 | #define xfs_inherit_noatime	xfs_params.inherit_noatim.val | 
 | 143 | #define xfs_buf_timer_centisecs	xfs_params.xfs_buf_timer.val | 
 | 144 | #define xfs_buf_age_centisecs	xfs_params.xfs_buf_age.val | 
 | 145 | #define xfs_inherit_nosymlinks	xfs_params.inherit_nosym.val | 
 | 146 | #define xfs_rotorstep		xfs_params.rotorstep.val | 
 | 147 |  | 
| Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 148 | #ifndef raw_smp_processor_id | 
 | 149 | #define raw_smp_processor_id()	smp_processor_id() | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #endif | 
| Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 151 | #define current_cpu()		raw_smp_processor_id() | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #define current_pid()		(current->pid) | 
 | 153 | #define current_fsuid(cred)	(current->fsuid) | 
 | 154 | #define current_fsgid(cred)	(current->fsgid) | 
 | 155 |  | 
 | 156 | #define NBPP		PAGE_SIZE | 
 | 157 | #define DPPSHFT		(PAGE_SHIFT - 9) | 
 | 158 | #define NDPP		(1 << (PAGE_SHIFT - 9)) | 
 | 159 | #define dtop(DD)	(((DD) + NDPP - 1) >> DPPSHFT) | 
 | 160 | #define dtopt(DD)	((DD) >> DPPSHFT) | 
 | 161 | #define dpoff(DD)	((DD) & (NDPP-1)) | 
 | 162 |  | 
 | 163 | #define NBBY		8		/* number of bits per byte */ | 
 | 164 | #define	NBPC		PAGE_SIZE	/* Number of bytes per click */ | 
 | 165 | #define	BPCSHIFT	PAGE_SHIFT	/* LOG2(NBPC) if exact */ | 
 | 166 |  | 
 | 167 | /* | 
 | 168 |  * Size of block device i/o is parameterized here. | 
 | 169 |  * Currently the system supports page-sized i/o. | 
 | 170 |  */ | 
 | 171 | #define	BLKDEV_IOSHIFT		BPCSHIFT | 
 | 172 | #define	BLKDEV_IOSIZE		(1<<BLKDEV_IOSHIFT) | 
 | 173 | /* number of BB's per block device block */ | 
 | 174 | #define	BLKDEV_BB		BTOBB(BLKDEV_IOSIZE) | 
 | 175 |  | 
 | 176 | /* bytes to clicks */ | 
 | 177 | #define	btoc(x)		(((__psunsigned_t)(x)+(NBPC-1))>>BPCSHIFT) | 
 | 178 | #define	btoct(x)	((__psunsigned_t)(x)>>BPCSHIFT) | 
 | 179 | #define	btoc64(x)	(((__uint64_t)(x)+(NBPC-1))>>BPCSHIFT) | 
 | 180 | #define	btoct64(x)	((__uint64_t)(x)>>BPCSHIFT) | 
 | 181 | #define	io_btoc(x)	(((__psunsigned_t)(x)+(IO_NBPC-1))>>IO_BPCSHIFT) | 
 | 182 | #define	io_btoct(x)	((__psunsigned_t)(x)>>IO_BPCSHIFT) | 
 | 183 |  | 
 | 184 | /* off_t bytes to clicks */ | 
 | 185 | #define offtoc(x)       (((__uint64_t)(x)+(NBPC-1))>>BPCSHIFT) | 
 | 186 | #define offtoct(x)      ((xfs_off_t)(x)>>BPCSHIFT) | 
 | 187 |  | 
 | 188 | /* clicks to off_t bytes */ | 
 | 189 | #define	ctooff(x)	((xfs_off_t)(x)<<BPCSHIFT) | 
 | 190 |  | 
 | 191 | /* clicks to bytes */ | 
 | 192 | #define	ctob(x)		((__psunsigned_t)(x)<<BPCSHIFT) | 
 | 193 | #define btoct(x)        ((__psunsigned_t)(x)>>BPCSHIFT) | 
 | 194 | #define	ctob64(x)	((__uint64_t)(x)<<BPCSHIFT) | 
 | 195 | #define	io_ctob(x)	((__psunsigned_t)(x)<<IO_BPCSHIFT) | 
 | 196 |  | 
 | 197 | /* bytes to clicks */ | 
 | 198 | #define btoc(x)         (((__psunsigned_t)(x)+(NBPC-1))>>BPCSHIFT) | 
 | 199 |  | 
 | 200 | #ifndef CELL_CAPABLE | 
 | 201 | #define FSC_NOTIFY_NAME_CHANGED(vp) | 
 | 202 | #endif | 
 | 203 |  | 
 | 204 | #ifndef ENOATTR | 
 | 205 | #define ENOATTR		ENODATA		/* Attribute not found */ | 
 | 206 | #endif | 
 | 207 |  | 
 | 208 | /* Note: EWRONGFS never visible outside the kernel */ | 
 | 209 | #define	EWRONGFS	EINVAL		/* Mount with wrong filesystem type */ | 
 | 210 |  | 
 | 211 | /* | 
 | 212 |  * XXX EFSCORRUPTED needs a real value in errno.h. asm-i386/errno.h won't | 
 | 213 |  *     return codes out of its known range in errno. | 
 | 214 |  * XXX Also note: needs to be < 1000 and fairly unique on Linux (mustn't | 
 | 215 |  *     conflict with any code we use already or any code a driver may use) | 
 | 216 |  * XXX Some options (currently we do #2): | 
 | 217 |  *	1/ New error code ["Filesystem is corrupted", _after_ glibc updated] | 
 | 218 |  *	2/ 990 ["Unknown error 990"] | 
 | 219 |  *	3/ EUCLEAN ["Structure needs cleaning"] | 
 | 220 |  *	4/ Convert EFSCORRUPTED to EIO [just prior to return into userspace] | 
 | 221 |  */ | 
 | 222 | #define EFSCORRUPTED    990		/* Filesystem is corrupted */ | 
 | 223 |  | 
 | 224 | #define SYNCHRONIZE()	barrier() | 
 | 225 | #define __return_address __builtin_return_address(0) | 
 | 226 |  | 
 | 227 | /* | 
 | 228 |  * IRIX (BSD) quotactl makes use of separate commands for user/group, | 
 | 229 |  * whereas on Linux the syscall encodes this information into the cmd | 
 | 230 |  * field (see the QCMD macro in quota.h).  These macros help keep the | 
 | 231 |  * code portable - they are not visible from the syscall interface. | 
 | 232 |  */ | 
| Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 233 | #define Q_XSETGQLIM	XQM_CMD(8)	/* set groups disk limits */ | 
 | 234 | #define Q_XGETGQUOTA	XQM_CMD(9)	/* get groups disk limits */ | 
 | 235 | #define Q_XSETPQLIM	XQM_CMD(10)	/* set projects disk limits */ | 
 | 236 | #define Q_XGETPQUOTA	XQM_CMD(11)	/* get projects disk limits */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 |  | 
 | 238 | /* IRIX uses a dynamic sizing algorithm (ndquot = 200 + numprocs*2) */ | 
 | 239 | /* we may well need to fine-tune this if it ever becomes an issue.  */ | 
 | 240 | #define DQUOT_MAX_HEURISTIC	1024	/* NR_DQUOTS */ | 
 | 241 | #define ndquot			DQUOT_MAX_HEURISTIC | 
 | 242 |  | 
 | 243 | /* IRIX uses the current size of the name cache to guess a good value */ | 
 | 244 | /* - this isn't the same but is a good enough starting point for now. */ | 
 | 245 | #define DQUOT_HASH_HEURISTIC	files_stat.nr_files | 
 | 246 |  | 
 | 247 | /* IRIX inodes maintain the project ID also, zero this field on Linux */ | 
 | 248 | #define DEFAULT_PROJID	0 | 
 | 249 | #define dfltprid	DEFAULT_PROJID | 
 | 250 |  | 
 | 251 | #define MAXPATHLEN	1024 | 
 | 252 |  | 
 | 253 | #define MIN(a,b)	(min(a,b)) | 
 | 254 | #define MAX(a,b)	(max(a,b)) | 
 | 255 | #define howmany(x, y)	(((x)+((y)-1))/(y)) | 
 | 256 | #define roundup(x, y)	((((x)+((y)-1))/(y))*(y)) | 
 | 257 |  | 
 | 258 | #define xfs_stack_trace()	dump_stack() | 
 | 259 |  | 
 | 260 | #define xfs_itruncate_data(ip, off)	\ | 
 | 261 | 	(-vmtruncate(LINVFS_GET_IP(XFS_ITOV(ip)), (off))) | 
 | 262 |  | 
 | 263 |  | 
 | 264 | /* Move the kernel do_div definition off to one side */ | 
 | 265 |  | 
 | 266 | #if defined __i386__ | 
 | 267 | /* For ia32 we need to pull some tricks to get past various versions | 
 | 268 |  * of the compiler which do not like us using do_div in the middle | 
 | 269 |  * of large functions. | 
 | 270 |  */ | 
 | 271 | static inline __u32 xfs_do_div(void *a, __u32 b, int n) | 
 | 272 | { | 
 | 273 | 	__u32	mod; | 
 | 274 |  | 
 | 275 | 	switch (n) { | 
 | 276 | 		case 4: | 
 | 277 | 			mod = *(__u32 *)a % b; | 
 | 278 | 			*(__u32 *)a = *(__u32 *)a / b; | 
 | 279 | 			return mod; | 
 | 280 | 		case 8: | 
 | 281 | 			{ | 
 | 282 | 			unsigned long __upper, __low, __high, __mod; | 
 | 283 | 			__u64	c = *(__u64 *)a; | 
 | 284 | 			__upper = __high = c >> 32; | 
 | 285 | 			__low = c; | 
 | 286 | 			if (__high) { | 
 | 287 | 				__upper = __high % (b); | 
 | 288 | 				__high = __high / (b); | 
 | 289 | 			} | 
 | 290 | 			asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (b), "0" (__low), "1" (__upper)); | 
 | 291 | 			asm("":"=A" (c):"a" (__low),"d" (__high)); | 
 | 292 | 			*(__u64 *)a = c; | 
 | 293 | 			return __mod; | 
 | 294 | 			} | 
 | 295 | 	} | 
 | 296 |  | 
 | 297 | 	/* NOTREACHED */ | 
 | 298 | 	return 0; | 
 | 299 | } | 
 | 300 |  | 
 | 301 | /* Side effect free 64 bit mod operation */ | 
 | 302 | static inline __u32 xfs_do_mod(void *a, __u32 b, int n) | 
 | 303 | { | 
 | 304 | 	switch (n) { | 
 | 305 | 		case 4: | 
 | 306 | 			return *(__u32 *)a % b; | 
 | 307 | 		case 8: | 
 | 308 | 			{ | 
 | 309 | 			unsigned long __upper, __low, __high, __mod; | 
 | 310 | 			__u64	c = *(__u64 *)a; | 
 | 311 | 			__upper = __high = c >> 32; | 
 | 312 | 			__low = c; | 
 | 313 | 			if (__high) { | 
 | 314 | 				__upper = __high % (b); | 
 | 315 | 				__high = __high / (b); | 
 | 316 | 			} | 
 | 317 | 			asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (b), "0" (__low), "1" (__upper)); | 
 | 318 | 			asm("":"=A" (c):"a" (__low),"d" (__high)); | 
 | 319 | 			return __mod; | 
 | 320 | 			} | 
 | 321 | 	} | 
 | 322 |  | 
 | 323 | 	/* NOTREACHED */ | 
 | 324 | 	return 0; | 
 | 325 | } | 
 | 326 | #else | 
 | 327 | static inline __u32 xfs_do_div(void *a, __u32 b, int n) | 
 | 328 | { | 
 | 329 | 	__u32	mod; | 
 | 330 |  | 
 | 331 | 	switch (n) { | 
 | 332 | 		case 4: | 
 | 333 | 			mod = *(__u32 *)a % b; | 
 | 334 | 			*(__u32 *)a = *(__u32 *)a / b; | 
 | 335 | 			return mod; | 
 | 336 | 		case 8: | 
 | 337 | 			mod = do_div(*(__u64 *)a, b); | 
 | 338 | 			return mod; | 
 | 339 | 	} | 
 | 340 |  | 
 | 341 | 	/* NOTREACHED */ | 
 | 342 | 	return 0; | 
 | 343 | } | 
 | 344 |  | 
 | 345 | /* Side effect free 64 bit mod operation */ | 
 | 346 | static inline __u32 xfs_do_mod(void *a, __u32 b, int n) | 
 | 347 | { | 
 | 348 | 	switch (n) { | 
 | 349 | 		case 4: | 
 | 350 | 			return *(__u32 *)a % b; | 
 | 351 | 		case 8: | 
 | 352 | 			{ | 
 | 353 | 			__u64	c = *(__u64 *)a; | 
 | 354 | 			return do_div(c, b); | 
 | 355 | 			} | 
 | 356 | 	} | 
 | 357 |  | 
 | 358 | 	/* NOTREACHED */ | 
 | 359 | 	return 0; | 
 | 360 | } | 
 | 361 | #endif | 
 | 362 |  | 
 | 363 | #undef do_div | 
 | 364 | #define do_div(a, b)	xfs_do_div(&(a), (b), sizeof(a)) | 
 | 365 | #define do_mod(a, b)	xfs_do_mod(&(a), (b), sizeof(a)) | 
 | 366 |  | 
 | 367 | static inline __uint64_t roundup_64(__uint64_t x, __uint32_t y) | 
 | 368 | { | 
 | 369 | 	x += y - 1; | 
 | 370 | 	do_div(x, y); | 
 | 371 | 	return(x * y); | 
 | 372 | } | 
 | 373 |  | 
 | 374 | #define qsort(a, n, s, cmp) sort(a, n, s, cmp, NULL) | 
 | 375 |  | 
 | 376 | #endif /* __XFS_LINUX__ */ |