xfs_inode.c 50.1 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17
 */
18 19
#include <linux/log2.h>

L
Linus Torvalds 已提交
20
#include "xfs.h"
21
#include "xfs_fs.h"
22
#include "xfs_format.h"
L
Linus Torvalds 已提交
23
#include "xfs_log.h"
24
#include "xfs_inum.h"
L
Linus Torvalds 已提交
25 26 27 28 29 30
#include "xfs_trans.h"
#include "xfs_trans_priv.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
31
#include "xfs_alloc_btree.h"
L
Linus Torvalds 已提交
32
#include "xfs_ialloc_btree.h"
33
#include "xfs_attr_sf.h"
L
Linus Torvalds 已提交
34 35 36
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_buf_item.h"
37 38 39 40 41
#include "xfs_inode_item.h"
#include "xfs_btree.h"
#include "xfs_alloc.h"
#include "xfs_ialloc.h"
#include "xfs_bmap.h"
L
Linus Torvalds 已提交
42 43 44
#include "xfs_error.h"
#include "xfs_utils.h"
#include "xfs_quota.h"
45
#include "xfs_filestream.h"
46
#include "xfs_vnodeops.h"
47
#include "xfs_cksum.h"
C
Christoph Hellwig 已提交
48
#include "xfs_trace.h"
D
Dave Chinner 已提交
49
#include "xfs_icache.h"
L
Linus Torvalds 已提交
50 51 52 53

kmem_zone_t *xfs_inode_zone;

/*
54
 * Used in xfs_itruncate_extents().  This is the maximum number of extents
L
Linus Torvalds 已提交
55 56 57 58 59 60
 * freed from a file in a single transaction.
 */
#define	XFS_ITRUNC_MAX_EXTENTS	2

STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);

61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
 * helper function to extract extent size hint from inode
 */
xfs_extlen_t
xfs_get_extsz_hint(
	struct xfs_inode	*ip)
{
	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
		return ip->i_d.di_extsize;
	if (XFS_IS_REALTIME_INODE(ip))
		return ip->i_mount->m_sb.sb_rextsize;
	return 0;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
 * This is a wrapper routine around the xfs_ilock() routine used to centralize
 * some grungy code.  It is used in places that wish to lock the inode solely
 * for reading the extents.  The reason these places can't just call
 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
 * extents from disk for a file in b-tree format.  If the inode is in b-tree
 * format, then we need to lock the inode exclusively until the extents are read
 * in.  Locking it exclusively all the time would limit our parallelism
 * unnecessarily, though.  What we do instead is check to see if the extents
 * have been read in yet, and only lock the inode exclusively if they have not.
 *
 * The function returns a value which should be given to the corresponding
 * xfs_iunlock_map_shared().  This value is the mode in which the lock was
 * actually taken.
 */
uint
xfs_ilock_map_shared(
	xfs_inode_t	*ip)
{
	uint	lock_mode;

	if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
	    ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
		lock_mode = XFS_ILOCK_EXCL;
	} else {
		lock_mode = XFS_ILOCK_SHARED;
	}

	xfs_ilock(ip, lock_mode);

	return lock_mode;
}

/*
 * This is simply the unlock routine to go with xfs_ilock_map_shared().
 * All it does is call xfs_iunlock() with the given lock_mode.
 */
void
xfs_iunlock_map_shared(
	xfs_inode_t	*ip,
	unsigned int	lock_mode)
{
	xfs_iunlock(ip, lock_mode);
}

/*
 * The xfs inode contains 2 locks: a multi-reader lock called the
 * i_iolock and a multi-reader lock called the i_lock.  This routine
 * allows either or both of the locks to be obtained.
 *
 * The 2 locks should always be ordered so that the IO lock is
 * obtained first in order to prevent deadlock.
 *
 * ip -- the inode being locked
 * lock_flags -- this parameter indicates the inode's locks
 *       to be locked.  It can be:
 *		XFS_IOLOCK_SHARED,
 *		XFS_IOLOCK_EXCL,
 *		XFS_ILOCK_SHARED,
 *		XFS_ILOCK_EXCL,
 *		XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
 *		XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
 *		XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
 *		XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
 */
void
xfs_ilock(
	xfs_inode_t		*ip,
	uint			lock_flags)
{
	trace_xfs_ilock(ip, lock_flags, _RET_IP_);

	/*
	 * You can't set both SHARED and EXCL for the same lock,
	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
	 */
	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);

	if (lock_flags & XFS_IOLOCK_EXCL)
		mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
	else if (lock_flags & XFS_IOLOCK_SHARED)
		mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));

	if (lock_flags & XFS_ILOCK_EXCL)
		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
	else if (lock_flags & XFS_ILOCK_SHARED)
		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
}

/*
 * This is just like xfs_ilock(), except that the caller
 * is guaranteed not to sleep.  It returns 1 if it gets
 * the requested locks and 0 otherwise.  If the IO lock is
 * obtained but the inode lock cannot be, then the IO lock
 * is dropped before returning.
 *
 * ip -- the inode being locked
 * lock_flags -- this parameter indicates the inode's locks to be
 *       to be locked.  See the comment for xfs_ilock() for a list
 *	 of valid values.
 */
int
xfs_ilock_nowait(
	xfs_inode_t		*ip,
	uint			lock_flags)
{
	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);

	/*
	 * You can't set both SHARED and EXCL for the same lock,
	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
	 */
	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);

	if (lock_flags & XFS_IOLOCK_EXCL) {
		if (!mrtryupdate(&ip->i_iolock))
			goto out;
	} else if (lock_flags & XFS_IOLOCK_SHARED) {
		if (!mrtryaccess(&ip->i_iolock))
			goto out;
	}
	if (lock_flags & XFS_ILOCK_EXCL) {
		if (!mrtryupdate(&ip->i_lock))
			goto out_undo_iolock;
	} else if (lock_flags & XFS_ILOCK_SHARED) {
		if (!mrtryaccess(&ip->i_lock))
			goto out_undo_iolock;
	}
	return 1;

 out_undo_iolock:
	if (lock_flags & XFS_IOLOCK_EXCL)
		mrunlock_excl(&ip->i_iolock);
	else if (lock_flags & XFS_IOLOCK_SHARED)
		mrunlock_shared(&ip->i_iolock);
 out:
	return 0;
}

/*
 * xfs_iunlock() is used to drop the inode locks acquired with
 * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
 * that we know which locks to drop.
 *
 * ip -- the inode being unlocked
 * lock_flags -- this parameter indicates the inode's locks to be
 *       to be unlocked.  See the comment for xfs_ilock() for a list
 *	 of valid values for this parameter.
 *
 */
void
xfs_iunlock(
	xfs_inode_t		*ip,
	uint			lock_flags)
{
	/*
	 * You can't set both SHARED and EXCL for the same lock,
	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
	 */
	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
	ASSERT(lock_flags != 0);

	if (lock_flags & XFS_IOLOCK_EXCL)
		mrunlock_excl(&ip->i_iolock);
	else if (lock_flags & XFS_IOLOCK_SHARED)
		mrunlock_shared(&ip->i_iolock);

	if (lock_flags & XFS_ILOCK_EXCL)
		mrunlock_excl(&ip->i_lock);
	else if (lock_flags & XFS_ILOCK_SHARED)
		mrunlock_shared(&ip->i_lock);

	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
}

/*
 * give up write locks.  the i/o lock cannot be held nested
 * if it is being demoted.
 */
void
xfs_ilock_demote(
	xfs_inode_t		*ip,
	uint			lock_flags)
{
	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
	ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);

	if (lock_flags & XFS_ILOCK_EXCL)
		mrdemote(&ip->i_lock);
	if (lock_flags & XFS_IOLOCK_EXCL)
		mrdemote(&ip->i_iolock);

	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
}

D
Dave Chinner 已提交
286
#if defined(DEBUG) || defined(XFS_WARN)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
int
xfs_isilocked(
	xfs_inode_t		*ip,
	uint			lock_flags)
{
	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
		if (!(lock_flags & XFS_ILOCK_SHARED))
			return !!ip->i_lock.mr_writer;
		return rwsem_is_locked(&ip->i_lock.mr_lock);
	}

	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
		if (!(lock_flags & XFS_IOLOCK_SHARED))
			return !!ip->i_iolock.mr_writer;
		return rwsem_is_locked(&ip->i_iolock.mr_lock);
	}

	ASSERT(0);
	return 0;
}
#endif

void
__xfs_iflock(
	struct xfs_inode	*ip)
{
	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);

	do {
		prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
		if (xfs_isiflocked(ip))
			io_schedule();
	} while (!xfs_iflock_nowait(ip));

	finish_wait(wq, &wait.wait);
}

L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
STATIC uint
_xfs_dic2xflags(
	__uint16_t		di_flags)
{
	uint			flags = 0;

	if (di_flags & XFS_DIFLAG_ANY) {
		if (di_flags & XFS_DIFLAG_REALTIME)
			flags |= XFS_XFLAG_REALTIME;
		if (di_flags & XFS_DIFLAG_PREALLOC)
			flags |= XFS_XFLAG_PREALLOC;
		if (di_flags & XFS_DIFLAG_IMMUTABLE)
			flags |= XFS_XFLAG_IMMUTABLE;
		if (di_flags & XFS_DIFLAG_APPEND)
			flags |= XFS_XFLAG_APPEND;
		if (di_flags & XFS_DIFLAG_SYNC)
			flags |= XFS_XFLAG_SYNC;
		if (di_flags & XFS_DIFLAG_NOATIME)
			flags |= XFS_XFLAG_NOATIME;
		if (di_flags & XFS_DIFLAG_NODUMP)
			flags |= XFS_XFLAG_NODUMP;
		if (di_flags & XFS_DIFLAG_RTINHERIT)
			flags |= XFS_XFLAG_RTINHERIT;
		if (di_flags & XFS_DIFLAG_PROJINHERIT)
			flags |= XFS_XFLAG_PROJINHERIT;
		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
			flags |= XFS_XFLAG_NOSYMLINKS;
352 353 354 355
		if (di_flags & XFS_DIFLAG_EXTSIZE)
			flags |= XFS_XFLAG_EXTSIZE;
		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
			flags |= XFS_XFLAG_EXTSZINHERIT;
356 357
		if (di_flags & XFS_DIFLAG_NODEFRAG)
			flags |= XFS_XFLAG_NODEFRAG;
358 359
		if (di_flags & XFS_DIFLAG_FILESTREAM)
			flags |= XFS_XFLAG_FILESTREAM;
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368
	}

	return flags;
}

uint
xfs_ip2xflags(
	xfs_inode_t		*ip)
{
369
	xfs_icdinode_t		*dic = &ip->i_d;
L
Linus Torvalds 已提交
370

371
	return _xfs_dic2xflags(dic->di_flags) |
372
				(XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
L
Linus Torvalds 已提交
373 374 375 376
}

uint
xfs_dic2xflags(
377
	xfs_dinode_t		*dip)
L
Linus Torvalds 已提交
378
{
C
Christoph Hellwig 已提交
379
	return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
380
				(XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389
}

/*
 * Allocate an inode on disk and return a copy of its in-core version.
 * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
 * appropriately within the inode.  The uid and gid for the inode are
 * set according to the contents of the given cred structure.
 *
 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
C
Carlos Maiolino 已提交
390 391 392 393
 * has a free inode available, call xfs_iget() to obtain the in-core
 * version of the allocated inode.  Finally, fill in the inode and
 * log its initial contents.  In this case, ialloc_context would be
 * set to NULL.
L
Linus Torvalds 已提交
394
 *
C
Carlos Maiolino 已提交
395 396 397 398 399
 * If xfs_dialloc() does not have an available inode, it will replenish
 * its supply by doing an allocation. Since we can only do one
 * allocation within a transaction without deadlocks, we must commit
 * the current transaction before returning the inode itself.
 * In this case, therefore, we will set ialloc_context and return.
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407
 * The caller should then commit the current transaction, start a new
 * transaction, and call xfs_ialloc() again to actually get the inode.
 *
 * To ensure that some other process does not grab the inode that
 * was allocated during the first call to xfs_ialloc(), this routine
 * also returns the [locked] bp pointing to the head of the freelist
 * as ialloc_context.  The caller should hold this buffer across
 * the commit and pass it back into this routine on the second call.
D
David Chinner 已提交
408 409 410 411 412
 *
 * If we are allocating quota inodes, we do not have a parent inode
 * to attach to or associate with (i.e. pip == NULL) because they
 * are not linked into the directory structure - they are attached
 * directly to the superblock - and so have no parent.
L
Linus Torvalds 已提交
413 414 415 416 417
 */
int
xfs_ialloc(
	xfs_trans_t	*tp,
	xfs_inode_t	*pip,
A
Al Viro 已提交
418
	umode_t		mode,
419
	xfs_nlink_t	nlink,
L
Linus Torvalds 已提交
420
	xfs_dev_t	rdev,
421
	prid_t		prid,
L
Linus Torvalds 已提交
422 423 424 425
	int		okalloc,
	xfs_buf_t	**ialloc_context,
	xfs_inode_t	**ipp)
{
426
	struct xfs_mount *mp = tp->t_mountp;
L
Linus Torvalds 已提交
427 428 429 430
	xfs_ino_t	ino;
	xfs_inode_t	*ip;
	uint		flags;
	int		error;
431
	timespec_t	tv;
432
	int		filestreams = 0;
L
Linus Torvalds 已提交
433 434 435 436 437

	/*
	 * Call the space management code to pick
	 * the on-disk inode to be allocated.
	 */
D
David Chinner 已提交
438
	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
439
			    ialloc_context, &ino);
440
	if (error)
L
Linus Torvalds 已提交
441
		return error;
442
	if (*ialloc_context || ino == NULLFSINO) {
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452
		*ipp = NULL;
		return 0;
	}
	ASSERT(*ialloc_context == NULL);

	/*
	 * Get the in-core inode with the lock held exclusively.
	 * This is because we're setting fields here we need
	 * to prevent others from looking at until we're done.
	 */
453
	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
454
			 XFS_ILOCK_EXCL, &ip);
455
	if (error)
L
Linus Torvalds 已提交
456 457 458
		return error;
	ASSERT(ip != NULL);

A
Al Viro 已提交
459
	ip->i_d.di_mode = mode;
L
Linus Torvalds 已提交
460 461 462
	ip->i_d.di_onlink = 0;
	ip->i_d.di_nlink = nlink;
	ASSERT(ip->i_d.di_nlink == nlink);
463 464
	ip->i_d.di_uid = current_fsuid();
	ip->i_d.di_gid = current_fsgid();
465
	xfs_set_projid(ip, prid);
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473
	memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));

	/*
	 * If the superblock version is up to where we support new format
	 * inodes and this is currently an old format inode, then change
	 * the inode version number now.  This way we only do the conversion
	 * here rather than here and in the flush/logging code.
	 */
474
	if (xfs_sb_version_hasnlink(&mp->m_sb) &&
475 476
	    ip->i_d.di_version == 1) {
		ip->i_d.di_version = 2;
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485
		/*
		 * We've already zeroed the old link count, the projid field,
		 * and the pad field.
		 */
	}

	/*
	 * Project ids won't be stored on disk if we are using a version 1 inode.
	 */
486
	if ((prid != 0) && (ip->i_d.di_version == 1))
L
Linus Torvalds 已提交
487 488
		xfs_bump_ino_vers2(tp, ip);

489
	if (pip && XFS_INHERIT_GID(pip)) {
L
Linus Torvalds 已提交
490
		ip->i_d.di_gid = pip->i_d.di_gid;
491
		if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
			ip->i_d.di_mode |= S_ISGID;
		}
	}

	/*
	 * If the group ID of the new file does not match the effective group
	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
	 * (and only if the irix_sgid_inherit compatibility variable is set).
	 */
	if ((irix_sgid_inherit) &&
	    (ip->i_d.di_mode & S_ISGID) &&
	    (!in_group_p((gid_t)ip->i_d.di_gid))) {
		ip->i_d.di_mode &= ~S_ISGID;
	}

	ip->i_d.di_size = 0;
	ip->i_d.di_nextents = 0;
	ASSERT(ip->i_d.di_nblocks == 0);
510 511 512 513 514 515 516

	nanotime(&tv);
	ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
	ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
	ip->i_d.di_atime = ip->i_d.di_mtime;
	ip->i_d.di_ctime = ip->i_d.di_mtime;

L
Linus Torvalds 已提交
517 518 519 520 521 522 523
	/*
	 * di_gen will have been taken care of in xfs_iread.
	 */
	ip->i_d.di_extsize = 0;
	ip->i_d.di_dmevmask = 0;
	ip->i_d.di_dmstate = 0;
	ip->i_d.di_flags = 0;
524 525 526 527 528 529 530 531 532 533 534 535 536

	if (ip->i_d.di_version == 3) {
		ASSERT(ip->i_d.di_ino == ino);
		ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
		ip->i_d.di_crc = 0;
		ip->i_d.di_changecount = 1;
		ip->i_d.di_lsn = 0;
		ip->i_d.di_flags2 = 0;
		memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
		ip->i_d.di_crtime = ip->i_d.di_mtime;
	}


L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544 545 546 547 548
	flags = XFS_ILOG_CORE;
	switch (mode & S_IFMT) {
	case S_IFIFO:
	case S_IFCHR:
	case S_IFBLK:
	case S_IFSOCK:
		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
		ip->i_df.if_u2.if_rdev = rdev;
		ip->i_df.if_flags = 0;
		flags |= XFS_ILOG_DEV;
		break;
	case S_IFREG:
549 550 551 552 553 554
		/*
		 * we can't set up filestreams until after the VFS inode
		 * is set up properly.
		 */
		if (pip && xfs_inode_is_filestream(pip))
			filestreams = 1;
555
		/* fall through */
L
Linus Torvalds 已提交
556
	case S_IFDIR:
D
David Chinner 已提交
557
		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
558 559
			uint	di_flags = 0;

560
			if (S_ISDIR(mode)) {
561 562
				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
					di_flags |= XFS_DIFLAG_RTINHERIT;
563 564 565 566
				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
					ip->i_d.di_extsize = pip->i_d.di_extsize;
				}
567
			} else if (S_ISREG(mode)) {
C
Christoph Hellwig 已提交
568
				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
569
					di_flags |= XFS_DIFLAG_REALTIME;
570 571 572 573
				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
					di_flags |= XFS_DIFLAG_EXTSIZE;
					ip->i_d.di_extsize = pip->i_d.di_extsize;
				}
L
Linus Torvalds 已提交
574 575 576
			}
			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
			    xfs_inherit_noatime)
577
				di_flags |= XFS_DIFLAG_NOATIME;
L
Linus Torvalds 已提交
578 579
			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
			    xfs_inherit_nodump)
580
				di_flags |= XFS_DIFLAG_NODUMP;
L
Linus Torvalds 已提交
581 582
			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
			    xfs_inherit_sync)
583
				di_flags |= XFS_DIFLAG_SYNC;
L
Linus Torvalds 已提交
584 585
			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
			    xfs_inherit_nosymlinks)
586 587 588
				di_flags |= XFS_DIFLAG_NOSYMLINKS;
			if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
				di_flags |= XFS_DIFLAG_PROJINHERIT;
589 590 591
			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
			    xfs_inherit_nodefrag)
				di_flags |= XFS_DIFLAG_NODEFRAG;
592 593
			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
				di_flags |= XFS_DIFLAG_FILESTREAM;
594
			ip->i_d.di_flags |= di_flags;
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
		}
		/* FALLTHROUGH */
	case S_IFLNK:
		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
		ip->i_df.if_flags = XFS_IFEXTENTS;
		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
		ip->i_df.if_u1.if_extents = NULL;
		break;
	default:
		ASSERT(0);
	}
	/*
	 * Attribute fork settings for new inode.
	 */
	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
	ip->i_d.di_anextents = 0;

	/*
	 * Log the new values stuffed into the inode.
	 */
615
	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
616 617
	xfs_trans_log_inode(tp, ip, flags);

618
	/* now that we have an i_mode we can setup inode ops and unlock */
619
	xfs_setup_inode(ip);
L
Linus Torvalds 已提交
620

621 622 623 624 625 626 627 628 629
	/* now we have set up the vfs inode we can associate the filestream */
	if (filestreams) {
		error = xfs_filestream_associate(pip, ip);
		if (error < 0)
			return -error;
		if (!error)
			xfs_iflags_set(ip, XFS_IFILESTREAM);
	}

L
Linus Torvalds 已提交
630 631 632 633 634
	*ipp = ip;
	return 0;
}

/*
635 636 637
 * Free up the underlying blocks past new_size.  The new size must be smaller
 * than the current size.  This routine can be used both for the attribute and
 * data fork, and does not modify the inode size, which is left to the caller.
L
Linus Torvalds 已提交
638
 *
639 640 641 642 643 644 645 646 647
 * The transaction passed to this routine must have made a permanent log
 * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
 * given transaction and start new ones, so make sure everything involved in
 * the transaction is tidy before calling here.  Some transaction will be
 * returned to the caller to be committed.  The incoming transaction must
 * already include the inode, and both inode locks must be held exclusively.
 * The inode must also be "held" within the transaction.  On return the inode
 * will be "held" within the returned transaction.  This routine does NOT
 * require any disk space to be reserved for it within the transaction.
L
Linus Torvalds 已提交
648
 *
649 650 651 652 653
 * If we get an error, we must return with the inode locked and linked into the
 * current transaction. This keeps things simple for the higher level code,
 * because it always knows that the inode is locked and held in the transaction
 * that returns to it whether errors occur or not.  We don't mark the inode
 * dirty on error so that transactions can be easily aborted if possible.
L
Linus Torvalds 已提交
654 655
 */
int
656 657 658 659 660
xfs_itruncate_extents(
	struct xfs_trans	**tpp,
	struct xfs_inode	*ip,
	int			whichfork,
	xfs_fsize_t		new_size)
L
Linus Torvalds 已提交
661
{
662 663 664 665 666 667 668 669 670 671 672
	struct xfs_mount	*mp = ip->i_mount;
	struct xfs_trans	*tp = *tpp;
	struct xfs_trans	*ntp;
	xfs_bmap_free_t		free_list;
	xfs_fsblock_t		first_block;
	xfs_fileoff_t		first_unmap_block;
	xfs_fileoff_t		last_block;
	xfs_filblks_t		unmap_len;
	int			committed;
	int			error = 0;
	int			done = 0;
L
Linus Torvalds 已提交
673

674 675 676
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
677
	ASSERT(new_size <= XFS_ISIZE(ip));
678
	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
L
Linus Torvalds 已提交
679
	ASSERT(ip->i_itemp != NULL);
680
	ASSERT(ip->i_itemp->ili_lock_flags == 0);
681
	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
L
Linus Torvalds 已提交
682

683 684
	trace_xfs_itruncate_extents_start(ip, new_size);

L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693
	/*
	 * Since it is possible for space to become allocated beyond
	 * the end of the file (in a crash where the space is allocated
	 * but the inode size is not yet updated), simply remove any
	 * blocks which show up between the new EOF and the maximum
	 * possible file size.  If the first block to be removed is
	 * beyond the maximum file size (ie it is the same as last_block),
	 * then there is nothing to do.
	 */
694
	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
695
	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
696 697 698 699 700
	if (first_unmap_block == last_block)
		return 0;

	ASSERT(first_unmap_block < last_block);
	unmap_len = last_block - first_unmap_block + 1;
L
Linus Torvalds 已提交
701
	while (!done) {
702
		xfs_bmap_init(&free_list, &first_block);
703
		error = xfs_bunmapi(tp, ip,
704
				    first_unmap_block, unmap_len,
705
				    xfs_bmapi_aflag(whichfork),
L
Linus Torvalds 已提交
706
				    XFS_ITRUNC_MAX_EXTENTS,
707
				    &first_block, &free_list,
708
				    &done);
709 710
		if (error)
			goto out_bmap_cancel;
L
Linus Torvalds 已提交
711 712 713 714 715

		/*
		 * Duplicate the transaction that has the permanent
		 * reservation and commit the old transaction.
		 */
716
		error = xfs_bmap_finish(&tp, &free_list, &committed);
717
		if (committed)
718
			xfs_trans_ijoin(tp, ip, 0);
719 720
		if (error)
			goto out_bmap_cancel;
L
Linus Torvalds 已提交
721 722 723

		if (committed) {
			/*
724
			 * Mark the inode dirty so it will be logged and
725
			 * moved forward in the log as part of every commit.
L
Linus Torvalds 已提交
726
			 */
727
			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
L
Linus Torvalds 已提交
728
		}
729

730 731 732
		ntp = xfs_trans_dup(tp);
		error = xfs_trans_commit(tp, 0);
		tp = ntp;
733

734
		xfs_trans_ijoin(tp, ip, 0);
735

736
		if (error)
737 738
			goto out;

739
		/*
740
		 * Transaction commit worked ok so we can drop the extra ticket
741 742
		 * reference that we gained in xfs_trans_dup()
		 */
743 744
		xfs_log_ticket_put(tp->t_ticket);
		error = xfs_trans_reserve(tp, 0,
745 746 747 748
					XFS_ITRUNCATE_LOG_RES(mp), 0,
					XFS_TRANS_PERM_LOG_RES,
					XFS_ITRUNCATE_LOG_COUNT);
		if (error)
749
			goto out;
L
Linus Torvalds 已提交
750
	}
751

752 753 754 755 756 757 758 759
	/*
	 * Always re-log the inode so that our permanent transaction can keep
	 * on rolling it forward in the log.
	 */
	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);

	trace_xfs_itruncate_extents_end(ip, new_size);

760 761 762 763
out:
	*tpp = tp;
	return error;
out_bmap_cancel:
L
Linus Torvalds 已提交
764
	/*
765 766 767
	 * If the bunmapi call encounters an error, return to the caller where
	 * the transaction can be properly aborted.  We just need to make sure
	 * we're not holding any resources that we were not when we came in.
L
Linus Torvalds 已提交
768
	 */
769 770 771 772
	xfs_bmap_cancel(&free_list);
	goto out;
}

L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
/*
 * This is called when the inode's link count goes to 0.
 * We place the on-disk inode on a list in the AGI.  It
 * will be pulled from this list when the inode is freed.
 */
int
xfs_iunlink(
	xfs_trans_t	*tp,
	xfs_inode_t	*ip)
{
	xfs_mount_t	*mp;
	xfs_agi_t	*agi;
	xfs_dinode_t	*dip;
	xfs_buf_t	*agibp;
	xfs_buf_t	*ibp;
	xfs_agino_t	agino;
	short		bucket_index;
	int		offset;
	int		error;

	ASSERT(ip->i_d.di_nlink == 0);
	ASSERT(ip->i_d.di_mode != 0);

	mp = tp->t_mountp;

	/*
	 * Get the agi buffer first.  It ensures lock ordering
	 * on the list.
	 */
802
	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
803
	if (error)
L
Linus Torvalds 已提交
804 805
		return error;
	agi = XFS_BUF_TO_AGI(agibp);
806

L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814
	/*
	 * Get the index into the agi hash table for the
	 * list this inode will go on.
	 */
	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
	ASSERT(agino != 0);
	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
	ASSERT(agi->agi_unlinked[bucket_index]);
815
	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
L
Linus Torvalds 已提交
816

817
	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
L
Linus Torvalds 已提交
818 819 820 821 822 823
		/*
		 * There is already another inode in the bucket we need
		 * to add ourselves to.  Add us at the front of the list.
		 * Here we put the head pointer into our next pointer,
		 * and then we fall through to point the head at us.
		 */
824 825
		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
				       0, 0);
826 827 828
		if (error)
			return error;

829
		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
L
Linus Torvalds 已提交
830
		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
831
		offset = ip->i_imap.im_boffset +
L
Linus Torvalds 已提交
832
			offsetof(xfs_dinode_t, di_next_unlinked);
833 834 835 836

		/* need to recalc the inode CRC if appropriate */
		xfs_dinode_calc_crc(mp, dip);

L
Linus Torvalds 已提交
837 838 839 840 841 842 843 844 845 846
		xfs_trans_inode_buf(tp, ibp);
		xfs_trans_log_buf(tp, ibp, offset,
				  (offset + sizeof(xfs_agino_t) - 1));
		xfs_inobp_check(mp, ibp);
	}

	/*
	 * Point the bucket head pointer at the inode being inserted.
	 */
	ASSERT(agino != 0);
847
	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	offset = offsetof(xfs_agi_t, agi_unlinked) +
		(sizeof(xfs_agino_t) * bucket_index);
	xfs_trans_log_buf(tp, agibp, offset,
			  (offset + sizeof(xfs_agino_t) - 1));
	return 0;
}

/*
 * Pull the on-disk inode from the AGI unlinked list.
 */
STATIC int
xfs_iunlink_remove(
	xfs_trans_t	*tp,
	xfs_inode_t	*ip)
{
	xfs_ino_t	next_ino;
	xfs_mount_t	*mp;
	xfs_agi_t	*agi;
	xfs_dinode_t	*dip;
	xfs_buf_t	*agibp;
	xfs_buf_t	*ibp;
	xfs_agnumber_t	agno;
	xfs_agino_t	agino;
	xfs_agino_t	next_agino;
	xfs_buf_t	*last_ibp;
873
	xfs_dinode_t	*last_dip = NULL;
L
Linus Torvalds 已提交
874
	short		bucket_index;
875
	int		offset, last_offset = 0;
L
Linus Torvalds 已提交
876 877 878 879 880 881 882 883 884
	int		error;

	mp = tp->t_mountp;
	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);

	/*
	 * Get the agi buffer first.  It ensures lock ordering
	 * on the list.
	 */
885 886
	error = xfs_read_agi(mp, tp, agno, &agibp);
	if (error)
L
Linus Torvalds 已提交
887
		return error;
888

L
Linus Torvalds 已提交
889
	agi = XFS_BUF_TO_AGI(agibp);
890

L
Linus Torvalds 已提交
891 892 893 894 895 896 897
	/*
	 * Get the index into the agi hash table for the
	 * list this inode will go on.
	 */
	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
	ASSERT(agino != 0);
	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
898
	ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
L
Linus Torvalds 已提交
899 900
	ASSERT(agi->agi_unlinked[bucket_index]);

901
	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
L
Linus Torvalds 已提交
902
		/*
903 904 905 906 907
		 * We're at the head of the list.  Get the inode's on-disk
		 * buffer to see if there is anyone after us on the list.
		 * Only modify our next pointer if it is not already NULLAGINO.
		 * This saves us the overhead of dealing with the buffer when
		 * there is no need to change it.
L
Linus Torvalds 已提交
908
		 */
909 910
		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
				       0, 0);
L
Linus Torvalds 已提交
911
		if (error) {
912
			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
913
				__func__, error);
L
Linus Torvalds 已提交
914 915
			return error;
		}
916
		next_agino = be32_to_cpu(dip->di_next_unlinked);
L
Linus Torvalds 已提交
917 918
		ASSERT(next_agino != 0);
		if (next_agino != NULLAGINO) {
919
			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
920
			offset = ip->i_imap.im_boffset +
L
Linus Torvalds 已提交
921
				offsetof(xfs_dinode_t, di_next_unlinked);
922 923 924 925

			/* need to recalc the inode CRC if appropriate */
			xfs_dinode_calc_crc(mp, dip);

L
Linus Torvalds 已提交
926 927 928 929 930 931 932 933 934 935 936 937
			xfs_trans_inode_buf(tp, ibp);
			xfs_trans_log_buf(tp, ibp, offset,
					  (offset + sizeof(xfs_agino_t) - 1));
			xfs_inobp_check(mp, ibp);
		} else {
			xfs_trans_brelse(tp, ibp);
		}
		/*
		 * Point the bucket head pointer at the next inode.
		 */
		ASSERT(next_agino != 0);
		ASSERT(next_agino != agino);
938
		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946
		offset = offsetof(xfs_agi_t, agi_unlinked) +
			(sizeof(xfs_agino_t) * bucket_index);
		xfs_trans_log_buf(tp, agibp, offset,
				  (offset + sizeof(xfs_agino_t) - 1));
	} else {
		/*
		 * We need to search the list for the inode being freed.
		 */
947
		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
L
Linus Torvalds 已提交
948 949
		last_ibp = NULL;
		while (next_agino != agino) {
C
Christoph Hellwig 已提交
950 951 952
			struct xfs_imap	imap;

			if (last_ibp)
L
Linus Torvalds 已提交
953
				xfs_trans_brelse(tp, last_ibp);
C
Christoph Hellwig 已提交
954 955

			imap.im_blkno = 0;
L
Linus Torvalds 已提交
956
			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
C
Christoph Hellwig 已提交
957 958 959 960 961 962 963 964 965 966 967

			error = xfs_imap(mp, tp, next_ino, &imap, 0);
			if (error) {
				xfs_warn(mp,
	"%s: xfs_imap returned error %d.",
					 __func__, error);
				return error;
			}

			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
					       &last_ibp, 0, 0);
L
Linus Torvalds 已提交
968
			if (error) {
969
				xfs_warn(mp,
C
Christoph Hellwig 已提交
970
	"%s: xfs_imap_to_bp returned error %d.",
971
					__func__, error);
L
Linus Torvalds 已提交
972 973
				return error;
			}
C
Christoph Hellwig 已提交
974 975

			last_offset = imap.im_boffset;
976
			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
L
Linus Torvalds 已提交
977 978 979
			ASSERT(next_agino != NULLAGINO);
			ASSERT(next_agino != 0);
		}
980

L
Linus Torvalds 已提交
981
		/*
982 983
		 * Now last_ibp points to the buffer previous to us on the
		 * unlinked list.  Pull us from the list.
L
Linus Torvalds 已提交
984
		 */
985 986
		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
				       0, 0);
L
Linus Torvalds 已提交
987
		if (error) {
988
			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
989
				__func__, error);
L
Linus Torvalds 已提交
990 991
			return error;
		}
992
		next_agino = be32_to_cpu(dip->di_next_unlinked);
L
Linus Torvalds 已提交
993 994 995
		ASSERT(next_agino != 0);
		ASSERT(next_agino != agino);
		if (next_agino != NULLAGINO) {
996
			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
997
			offset = ip->i_imap.im_boffset +
L
Linus Torvalds 已提交
998
				offsetof(xfs_dinode_t, di_next_unlinked);
999 1000 1001 1002

			/* need to recalc the inode CRC if appropriate */
			xfs_dinode_calc_crc(mp, dip);

L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
			xfs_trans_inode_buf(tp, ibp);
			xfs_trans_log_buf(tp, ibp, offset,
					  (offset + sizeof(xfs_agino_t) - 1));
			xfs_inobp_check(mp, ibp);
		} else {
			xfs_trans_brelse(tp, ibp);
		}
		/*
		 * Point the previous inode on the list to the next inode.
		 */
1013
		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
L
Linus Torvalds 已提交
1014 1015
		ASSERT(next_agino != 0);
		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1016 1017 1018 1019

		/* need to recalc the inode CRC if appropriate */
		xfs_dinode_calc_crc(mp, last_dip);

L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027
		xfs_trans_inode_buf(tp, last_ibp);
		xfs_trans_log_buf(tp, last_ibp, offset,
				  (offset + sizeof(xfs_agino_t) - 1));
		xfs_inobp_check(mp, last_ibp);
	}
	return 0;
}

1028 1029 1030 1031 1032
/*
 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
 * inodes that are in memory - they all must be marked stale and attached to
 * the cluster buffer.
 */
1033
STATIC int
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042
xfs_ifree_cluster(
	xfs_inode_t	*free_ip,
	xfs_trans_t	*tp,
	xfs_ino_t	inum)
{
	xfs_mount_t		*mp = free_ip->i_mount;
	int			blks_per_cluster;
	int			nbufs;
	int			ninodes;
1043
	int			i, j;
L
Linus Torvalds 已提交
1044 1045
	xfs_daddr_t		blkno;
	xfs_buf_t		*bp;
1046
	xfs_inode_t		*ip;
L
Linus Torvalds 已提交
1047 1048
	xfs_inode_log_item_t	*iip;
	xfs_log_item_t		*lip;
D
Dave Chinner 已提交
1049
	struct xfs_perag	*pag;
L
Linus Torvalds 已提交
1050

D
Dave Chinner 已提交
1051
	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
		blks_per_cluster = 1;
		ninodes = mp->m_sb.sb_inopblock;
		nbufs = XFS_IALLOC_BLOCKS(mp);
	} else {
		blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
					mp->m_sb.sb_blocksize;
		ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
		nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
	}

	for (j = 0; j < nbufs; j++, inum += ninodes) {
		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
					 XFS_INO_TO_AGBNO(mp, inum));

1067 1068 1069 1070 1071 1072 1073 1074 1075
		/*
		 * We obtain and lock the backing buffer first in the process
		 * here, as we have to ensure that any dirty inode that we
		 * can't get the flush lock on is attached to the buffer.
		 * If we scan the in-memory inodes first, then buffer IO can
		 * complete before we get a lock on it, and hence we may fail
		 * to mark all the active inodes on the buffer stale.
		 */
		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
1076 1077
					mp->m_bsize * blks_per_cluster,
					XBF_UNMAPPED);
1078

1079 1080
		if (!bp)
			return ENOMEM;
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

		/*
		 * This buffer may not have been correctly initialised as we
		 * didn't read it from disk. That's not important because we are
		 * only using to mark the buffer as stale in the log, and to
		 * attach stale cached inodes on it. That means it will never be
		 * dispatched for IO. If it is, we want to know about it, and we
		 * want it to fail. We can acheive this by adding a write
		 * verifier to the buffer.
		 */
1091
		 bp->b_ops = &xfs_inode_buf_ops;
1092

1093 1094 1095
		/*
		 * Walk the inodes already attached to the buffer and mark them
		 * stale. These will all have the flush locks held, so an
1096 1097 1098
		 * in-memory inode walk can't lock them. By marking them all
		 * stale first, we will not attempt to lock them in the loop
		 * below as the XFS_ISTALE flag will be set.
1099
		 */
1100
		lip = bp->b_fspriv;
1101 1102 1103 1104
		while (lip) {
			if (lip->li_type == XFS_LI_INODE) {
				iip = (xfs_inode_log_item_t *)lip;
				ASSERT(iip->ili_logged == 1);
1105
				lip->li_cb = xfs_istale_done;
1106 1107 1108 1109 1110 1111 1112
				xfs_trans_ail_copy_lsn(mp->m_ail,
							&iip->ili_flush_lsn,
							&iip->ili_item.li_lsn);
				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
			}
			lip = lip->li_bio_list;
		}
L
Linus Torvalds 已提交
1113

1114

L
Linus Torvalds 已提交
1115
		/*
1116 1117 1118 1119
		 * For each inode in memory attempt to add it to the inode
		 * buffer and set it up for being staled on buffer IO
		 * completion.  This is safe as we've locked out tail pushing
		 * and flushing by locking the buffer.
L
Linus Torvalds 已提交
1120
		 *
1121 1122 1123
		 * We have already marked every inode that was part of a
		 * transaction stale above, which means there is no point in
		 * even trying to lock them.
L
Linus Torvalds 已提交
1124 1125
		 */
		for (i = 0; i < ninodes; i++) {
1126
retry:
1127
			rcu_read_lock();
1128 1129
			ip = radix_tree_lookup(&pag->pag_ici_root,
					XFS_INO_TO_AGINO(mp, (inum + i)));
L
Linus Torvalds 已提交
1130

1131 1132 1133
			/* Inode not in memory, nothing to do */
			if (!ip) {
				rcu_read_unlock();
L
Linus Torvalds 已提交
1134 1135 1136
				continue;
			}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
			/*
			 * because this is an RCU protected lookup, we could
			 * find a recently freed or even reallocated inode
			 * during the lookup. We need to check under the
			 * i_flags_lock for a valid inode here. Skip it if it
			 * is not valid, the wrong inode or stale.
			 */
			spin_lock(&ip->i_flags_lock);
			if (ip->i_ino != inum + i ||
			    __xfs_iflags_test(ip, XFS_ISTALE)) {
				spin_unlock(&ip->i_flags_lock);
				rcu_read_unlock();
				continue;
			}
			spin_unlock(&ip->i_flags_lock);

1153 1154 1155 1156 1157 1158 1159
			/*
			 * Don't try to lock/unlock the current inode, but we
			 * _cannot_ skip the other inodes that we did not find
			 * in the list attached to the buffer and are not
			 * already marked stale. If we can't lock it, back off
			 * and retry.
			 */
1160 1161
			if (ip != free_ip &&
			    !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1162
				rcu_read_unlock();
1163 1164
				delay(1);
				goto retry;
L
Linus Torvalds 已提交
1165
			}
1166
			rcu_read_unlock();
L
Linus Torvalds 已提交
1167

1168
			xfs_iflock(ip);
1169
			xfs_iflags_set(ip, XFS_ISTALE);
L
Linus Torvalds 已提交
1170

1171 1172 1173 1174
			/*
			 * we don't need to attach clean inodes or those only
			 * with unlogged changes (which we throw away, anyway).
			 */
L
Linus Torvalds 已提交
1175
			iip = ip->i_itemp;
1176
			if (!iip || xfs_inode_clean(ip)) {
1177
				ASSERT(ip != free_ip);
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182
				xfs_ifunlock(ip);
				xfs_iunlock(ip, XFS_ILOCK_EXCL);
				continue;
			}

1183 1184
			iip->ili_last_fields = iip->ili_fields;
			iip->ili_fields = 0;
L
Linus Torvalds 已提交
1185
			iip->ili_logged = 1;
1186 1187
			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
						&iip->ili_item.li_lsn);
L
Linus Torvalds 已提交
1188

1189 1190
			xfs_buf_attach_iodone(bp, xfs_istale_done,
						  &iip->ili_item);
1191 1192

			if (ip != free_ip)
L
Linus Torvalds 已提交
1193 1194 1195
				xfs_iunlock(ip, XFS_ILOCK_EXCL);
		}

1196
		xfs_trans_stale_inode_buf(tp, bp);
L
Linus Torvalds 已提交
1197 1198 1199
		xfs_trans_binval(tp, bp);
	}

D
Dave Chinner 已提交
1200
	xfs_perag_put(pag);
1201
	return 0;
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
}

/*
 * This is called to return an inode to the inode free list.
 * The inode should already be truncated to 0 length and have
 * no pages associated with it.  This routine also assumes that
 * the inode is already a part of the transaction.
 *
 * The on-disk copy of the inode will have been added to the list
 * of unlinked inodes in the AGI. We need to remove the inode from
 * that list atomically with respect to freeing it here.
 */
int
xfs_ifree(
	xfs_trans_t	*tp,
	xfs_inode_t	*ip,
	xfs_bmap_free_t	*flist)
{
	int			error;
	int			delete;
	xfs_ino_t		first_ino;

C
Christoph Hellwig 已提交
1224
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
L
Linus Torvalds 已提交
1225 1226 1227
	ASSERT(ip->i_d.di_nlink == 0);
	ASSERT(ip->i_d.di_nextents == 0);
	ASSERT(ip->i_d.di_anextents == 0);
1228
	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234
	ASSERT(ip->i_d.di_nblocks == 0);

	/*
	 * Pull the on-disk inode from the AGI unlinked list.
	 */
	error = xfs_iunlink_remove(tp, ip);
1235
	if (error)
L
Linus Torvalds 已提交
1236 1237 1238
		return error;

	error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1239
	if (error)
L
Linus Torvalds 已提交
1240
		return error;
1241

L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	ip->i_d.di_mode = 0;		/* mark incore inode as free */
	ip->i_d.di_flags = 0;
	ip->i_d.di_dmevmask = 0;
	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
	/*
	 * Bump the generation count so no one will be confused
	 * by reincarnations of this inode.
	 */
	ip->i_d.di_gen++;
	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);

1255
	if (delete)
1256
		error = xfs_ifree_cluster(ip, tp, first_ino);
L
Linus Torvalds 已提交
1257

1258
	return error;
L
Linus Torvalds 已提交
1259 1260 1261
}

/*
1262 1263 1264
 * This is called to unpin an inode.  The caller must have the inode locked
 * in at least shared mode so that the buffer cannot be subsequently pinned
 * once someone is waiting for it to be unpinned.
L
Linus Torvalds 已提交
1265
 */
1266
static void
1267
xfs_iunpin(
1268
	struct xfs_inode	*ip)
L
Linus Torvalds 已提交
1269
{
C
Christoph Hellwig 已提交
1270
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
L
Linus Torvalds 已提交
1271

1272 1273
	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);

1274
	/* Give the log a push to start the unpinning I/O */
1275
	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
1276

1277
}
L
Linus Torvalds 已提交
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
static void
__xfs_iunpin_wait(
	struct xfs_inode	*ip)
{
	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);

	xfs_iunpin(ip);

	do {
		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
		if (xfs_ipincount(ip))
			io_schedule();
	} while (xfs_ipincount(ip));
	finish_wait(wq, &wait.wait);
}

1296
void
1297
xfs_iunpin_wait(
1298
	struct xfs_inode	*ip)
1299
{
1300 1301
	if (xfs_ipincount(ip))
		__xfs_iunpin_wait(ip);
L
Linus Torvalds 已提交
1302 1303
}

1304 1305 1306 1307
STATIC int
xfs_iflush_cluster(
	xfs_inode_t	*ip,
	xfs_buf_t	*bp)
L
Linus Torvalds 已提交
1308
{
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	xfs_mount_t		*mp = ip->i_mount;
	struct xfs_perag	*pag;
	unsigned long		first_index, mask;
	unsigned long		inodes_per_cluster;
	int			ilist_size;
	xfs_inode_t		**ilist;
	xfs_inode_t		*iq;
	int			nr_found;
	int			clcount = 0;
	int			bufwasdelwri;
L
Linus Torvalds 已提交
1319 1320
	int			i;

1321
	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
L
Linus Torvalds 已提交
1322

1323 1324 1325 1326 1327
	inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
	ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
	ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
	if (!ilist)
		goto out_put;
L
Linus Torvalds 已提交
1328

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
	rcu_read_lock();
	/* really need a gang lookup range call here */
	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
					first_index, inodes_per_cluster);
	if (nr_found == 0)
		goto out_free;

	for (i = 0; i < nr_found; i++) {
		iq = ilist[i];
		if (iq == ip)
1341
			continue;
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

		/*
		 * because this is an RCU protected lookup, we could find a
		 * recently freed or even reallocated inode during the lookup.
		 * We need to check under the i_flags_lock for a valid inode
		 * here. Skip it if it is not valid or the wrong inode.
		 */
		spin_lock(&ip->i_flags_lock);
		if (!ip->i_ino ||
		    (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
			spin_unlock(&ip->i_flags_lock);
			continue;
		}
		spin_unlock(&ip->i_flags_lock);

1357 1358 1359 1360 1361
		/*
		 * Do an un-protected check to see if the inode is dirty and
		 * is a candidate for flushing.  These checks will be repeated
		 * later after the appropriate locks are acquired.
		 */
1362
		if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
			continue;

		/*
		 * Try to get locks.  If any are unavailable or it is pinned,
		 * then this inode cannot be flushed and is skipped.
		 */

		if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
			continue;
		if (!xfs_iflock_nowait(iq)) {
			xfs_iunlock(iq, XFS_ILOCK_SHARED);
			continue;
		}
		if (xfs_ipincount(iq)) {
			xfs_ifunlock(iq);
			xfs_iunlock(iq, XFS_ILOCK_SHARED);
			continue;
		}

		/*
		 * arriving here means that this inode can be flushed.  First
		 * re-check that it's dirty before flushing.
		 */
1386 1387
		if (!xfs_inode_clean(iq)) {
			int	error;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
			error = xfs_iflush_int(iq, bp);
			if (error) {
				xfs_iunlock(iq, XFS_ILOCK_SHARED);
				goto cluster_corrupt_out;
			}
			clcount++;
		} else {
			xfs_ifunlock(iq);
		}
		xfs_iunlock(iq, XFS_ILOCK_SHARED);
	}

	if (clcount) {
		XFS_STATS_INC(xs_icluster_flushcnt);
		XFS_STATS_ADD(xs_icluster_flushinode, clcount);
	}

out_free:
1406
	rcu_read_unlock();
1407
	kmem_free(ilist);
1408 1409
out_put:
	xfs_perag_put(pag);
1410 1411 1412 1413 1414 1415 1416 1417
	return 0;


cluster_corrupt_out:
	/*
	 * Corruption detected in the clustering loop.  Invalidate the
	 * inode buffer and shut down the filesystem.
	 */
1418
	rcu_read_unlock();
1419
	/*
1420
	 * Clean up the buffer.  If it was delwri, just release it --
1421 1422 1423
	 * brelse can handle it with no problems.  If not, shut down the
	 * filesystem before releasing the buffer.
	 */
1424
	bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	if (bufwasdelwri)
		xfs_buf_relse(bp);

	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);

	if (!bufwasdelwri) {
		/*
		 * Just like incore_relse: if we have b_iodone functions,
		 * mark the buffer as an error and call them.  Otherwise
		 * mark it as stale and brelse.
		 */
1436
		if (bp->b_iodone) {
1437
			XFS_BUF_UNDONE(bp);
1438
			xfs_buf_stale(bp);
1439
			xfs_buf_ioerror(bp, EIO);
C
Christoph Hellwig 已提交
1440
			xfs_buf_ioend(bp, 0);
1441
		} else {
1442
			xfs_buf_stale(bp);
1443 1444 1445 1446 1447 1448 1449
			xfs_buf_relse(bp);
		}
	}

	/*
	 * Unlocks the flush lock
	 */
1450
	xfs_iflush_abort(iq, false);
1451
	kmem_free(ilist);
1452
	xfs_perag_put(pag);
1453 1454 1455
	return XFS_ERROR(EFSCORRUPTED);
}

L
Linus Torvalds 已提交
1456
/*
1457 1458 1459 1460 1461 1462 1463
 * Flush dirty inode metadata into the backing buffer.
 *
 * The caller must have the inode lock and the inode flush lock held.  The
 * inode lock will still be held upon return to the caller, and the inode
 * flush lock will be released after the inode has reached the disk.
 *
 * The caller must write out the buffer returned in *bpp and release it.
L
Linus Torvalds 已提交
1464 1465 1466
 */
int
xfs_iflush(
1467 1468
	struct xfs_inode	*ip,
	struct xfs_buf		**bpp)
L
Linus Torvalds 已提交
1469
{
1470 1471 1472
	struct xfs_mount	*mp = ip->i_mount;
	struct xfs_buf		*bp;
	struct xfs_dinode	*dip;
L
Linus Torvalds 已提交
1473 1474 1475 1476
	int			error;

	XFS_STATS_INC(xs_iflush_count);

C
Christoph Hellwig 已提交
1477
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1478
	ASSERT(xfs_isiflocked(ip));
L
Linus Torvalds 已提交
1479
	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
1480
	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
L
Linus Torvalds 已提交
1481

1482
	*bpp = NULL;
L
Linus Torvalds 已提交
1483 1484 1485

	xfs_iunpin_wait(ip);

1486 1487 1488
	/*
	 * For stale inodes we cannot rely on the backing buffer remaining
	 * stale in cache for the remaining life of the stale inode and so
1489
	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
1490 1491 1492 1493 1494 1495 1496 1497 1498
	 * inodes below. We have to check this after ensuring the inode is
	 * unpinned so that it is safe to reclaim the stale inode after the
	 * flush call.
	 */
	if (xfs_iflags_test(ip, XFS_ISTALE)) {
		xfs_ifunlock(ip);
		return 0;
	}

L
Linus Torvalds 已提交
1499 1500 1501
	/*
	 * This may have been unpinned because the filesystem is shutting
	 * down forcibly. If that's the case we must not write this inode
1502 1503 1504 1505
	 * to disk, because the log record didn't make it to disk.
	 *
	 * We also have to remove the log item from the AIL in this case,
	 * as we wait for an empty AIL as part of the unmount process.
L
Linus Torvalds 已提交
1506 1507
	 */
	if (XFS_FORCED_SHUTDOWN(mp)) {
1508 1509
		error = XFS_ERROR(EIO);
		goto abort_out;
L
Linus Torvalds 已提交
1510 1511
	}

1512 1513 1514
	/*
	 * Get the buffer containing the on-disk inode.
	 */
1515 1516
	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
			       0);
1517 1518 1519 1520 1521
	if (error || !bp) {
		xfs_ifunlock(ip);
		return error;
	}

L
Linus Torvalds 已提交
1522 1523 1524 1525
	/*
	 * First flush out the inode that xfs_iflush was called with.
	 */
	error = xfs_iflush_int(ip, bp);
1526
	if (error)
L
Linus Torvalds 已提交
1527 1528
		goto corrupt_out;

1529 1530 1531 1532
	/*
	 * If the buffer is pinned then push on the log now so we won't
	 * get stuck waiting in the write for too long.
	 */
1533
	if (xfs_buf_ispinned(bp))
1534
		xfs_log_force(mp, 0);
1535

L
Linus Torvalds 已提交
1536 1537 1538 1539
	/*
	 * inode clustering:
	 * see if other inodes can be gathered into this write
	 */
1540 1541 1542
	error = xfs_iflush_cluster(ip, bp);
	if (error)
		goto cluster_corrupt_out;
L
Linus Torvalds 已提交
1543

1544 1545
	*bpp = bp;
	return 0;
L
Linus Torvalds 已提交
1546 1547 1548

corrupt_out:
	xfs_buf_relse(bp);
1549
	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
L
Linus Torvalds 已提交
1550
cluster_corrupt_out:
1551 1552
	error = XFS_ERROR(EFSCORRUPTED);
abort_out:
L
Linus Torvalds 已提交
1553 1554 1555
	/*
	 * Unlocks the flush lock
	 */
1556
	xfs_iflush_abort(ip, false);
1557
	return error;
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562
}


STATIC int
xfs_iflush_int(
1563 1564
	struct xfs_inode	*ip,
	struct xfs_buf		*bp)
L
Linus Torvalds 已提交
1565
{
1566 1567 1568
	struct xfs_inode_log_item *iip = ip->i_itemp;
	struct xfs_dinode	*dip;
	struct xfs_mount	*mp = ip->i_mount;
L
Linus Torvalds 已提交
1569

C
Christoph Hellwig 已提交
1570
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1571
	ASSERT(xfs_isiflocked(ip));
L
Linus Torvalds 已提交
1572
	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
1573
	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
1574
	ASSERT(iip != NULL && iip->ili_fields != 0);
L
Linus Torvalds 已提交
1575 1576

	/* set *dip = inode's place in the buffer */
1577
	dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
L
Linus Torvalds 已提交
1578

1579
	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
L
Linus Torvalds 已提交
1580
			       mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
1581 1582 1583
		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
			"%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
L
Linus Torvalds 已提交
1584 1585 1586 1587
		goto corrupt_out;
	}
	if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
				mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
1588 1589 1590
		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
			"%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
			__func__, ip->i_ino, ip, ip->i_d.di_magic);
L
Linus Torvalds 已提交
1591 1592
		goto corrupt_out;
	}
1593
	if (S_ISREG(ip->i_d.di_mode)) {
L
Linus Torvalds 已提交
1594 1595 1596 1597
		if (XFS_TEST_ERROR(
		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
		    mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
1598 1599 1600
			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
				"%s: Bad regular inode %Lu, ptr 0x%p",
				__func__, ip->i_ino, ip);
L
Linus Torvalds 已提交
1601 1602
			goto corrupt_out;
		}
1603
	} else if (S_ISDIR(ip->i_d.di_mode)) {
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608
		if (XFS_TEST_ERROR(
		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
		    mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
1609 1610 1611
			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
				"%s: Bad directory inode %Lu, ptr 0x%p",
				__func__, ip->i_ino, ip);
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617
			goto corrupt_out;
		}
	}
	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
				XFS_RANDOM_IFLUSH_5)) {
1618 1619 1620 1621
		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
			"%s: detected corrupt incore inode %Lu, "
			"total extents = %d, nblocks = %Ld, ptr 0x%p",
			__func__, ip->i_ino,
L
Linus Torvalds 已提交
1622
			ip->i_d.di_nextents + ip->i_d.di_anextents,
1623
			ip->i_d.di_nblocks, ip);
L
Linus Torvalds 已提交
1624 1625 1626 1627
		goto corrupt_out;
	}
	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
				mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
1628 1629 1630
		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
			"%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
L
Linus Torvalds 已提交
1631 1632
		goto corrupt_out;
	}
1633

L
Linus Torvalds 已提交
1634
	/*
1635 1636 1637 1638 1639 1640 1641
	 * Inode item log recovery for v1/v2 inodes are dependent on the
	 * di_flushiter count for correct sequencing. We bump the flush
	 * iteration count so we can detect flushes which postdate a log record
	 * during recovery. This is redundant as we now log every change and
	 * hence this can't happen but we need to still do it to ensure
	 * backwards compatibility with old kernels that predate logging all
	 * inode changes.
L
Linus Torvalds 已提交
1642
	 */
1643 1644
	if (ip->i_d.di_version < 3)
		ip->i_d.di_flushiter++;
L
Linus Torvalds 已提交
1645 1646 1647 1648 1649 1650 1651

	/*
	 * Copy the dirty parts of the inode into the on-disk
	 * inode.  We always copy out the core of the inode,
	 * because if the inode is dirty at all the core must
	 * be.
	 */
C
Christoph Hellwig 已提交
1652
	xfs_dinode_to_disk(dip, &ip->i_d);
L
Linus Torvalds 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

	/* Wrap, we never let the log put out DI_MAX_FLUSH */
	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
		ip->i_d.di_flushiter = 0;

	/*
	 * If this is really an old format inode and the superblock version
	 * has not been updated to support only new format inodes, then
	 * convert back to the old inode format.  If the superblock version
	 * has been updated, then make the conversion permanent.
	 */
1664 1665
	ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
	if (ip->i_d.di_version == 1) {
1666
		if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
L
Linus Torvalds 已提交
1667 1668 1669 1670
			/*
			 * Convert it back.
			 */
			ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
C
Christoph Hellwig 已提交
1671
			dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
L
Linus Torvalds 已提交
1672 1673 1674 1675 1676 1677
		} else {
			/*
			 * The superblock version has already been bumped,
			 * so just make the conversion to the new inode
			 * format permanent.
			 */
1678 1679
			ip->i_d.di_version = 2;
			dip->di_version = 2;
L
Linus Torvalds 已提交
1680
			ip->i_d.di_onlink = 0;
C
Christoph Hellwig 已提交
1681
			dip->di_onlink = 0;
L
Linus Torvalds 已提交
1682
			memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
C
Christoph Hellwig 已提交
1683 1684
			memset(&(dip->di_pad[0]), 0,
			      sizeof(dip->di_pad));
1685
			ASSERT(xfs_get_projid(ip) == 0);
L
Linus Torvalds 已提交
1686 1687 1688
		}
	}

1689 1690 1691
	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
	if (XFS_IFORK_Q(ip))
		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
L
Linus Torvalds 已提交
1692 1693 1694
	xfs_inobp_check(mp, bp);

	/*
1695 1696 1697 1698 1699 1700 1701
	 * We've recorded everything logged in the inode, so we'd like to clear
	 * the ili_fields bits so we don't log and flush things unnecessarily.
	 * However, we can't stop logging all this information until the data
	 * we've copied into the disk buffer is written to disk.  If we did we
	 * might overwrite the copy of the inode in the log with all the data
	 * after re-logging only part of it, and in the face of a crash we
	 * wouldn't have all the data we need to recover.
L
Linus Torvalds 已提交
1702
	 *
1703 1704 1705 1706 1707 1708
	 * What we do is move the bits to the ili_last_fields field.  When
	 * logging the inode, these bits are moved back to the ili_fields field.
	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
	 * know that the information those bits represent is permanently on
	 * disk.  As long as the flush completes before the inode is logged
	 * again, then both ili_fields and ili_last_fields will be cleared.
L
Linus Torvalds 已提交
1709
	 *
1710 1711 1712 1713 1714 1715 1716 1717
	 * We can play with the ili_fields bits here, because the inode lock
	 * must be held exclusively in order to set bits there and the flush
	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
	 * done routine can tell whether or not to look in the AIL.  Also, store
	 * the current LSN of the inode so that we can tell whether the item has
	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
	 * need the AIL lock, because it is a 64 bit value that cannot be read
	 * atomically.
L
Linus Torvalds 已提交
1718
	 */
1719 1720 1721
	iip->ili_last_fields = iip->ili_fields;
	iip->ili_fields = 0;
	iip->ili_logged = 1;
L
Linus Torvalds 已提交
1722

1723 1724
	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
				&iip->ili_item.li_lsn);
L
Linus Torvalds 已提交
1725

1726 1727 1728 1729 1730 1731 1732
	/*
	 * Attach the function xfs_iflush_done to the inode's
	 * buffer.  This will remove the inode from the AIL
	 * and unlock the inode's flush lock when the inode is
	 * completely written to disk.
	 */
	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
L
Linus Torvalds 已提交
1733

1734 1735 1736 1737 1738 1739
	/* update the lsn in the on disk inode if required */
	if (ip->i_d.di_version == 3)
		dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);

	/* generate the checksum. */
	xfs_dinode_calc_crc(mp, dip);
L
Linus Torvalds 已提交
1740

1741 1742
	ASSERT(bp->b_fspriv != NULL);
	ASSERT(bp->b_iodone != NULL);
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748
	return 0;

corrupt_out:
	return XFS_ERROR(EFSCORRUPTED);
}

1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
/*
 * Test whether it is appropriate to check an inode for and free post EOF
 * blocks. The 'force' parameter determines whether we should also consider
 * regular files that are marked preallocated or append-only.
 */
bool
xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
{
	/* prealloc/delalloc exists only on regular files */
	if (!S_ISREG(ip->i_d.di_mode))
		return false;

	/*
	 * Zero sized files with no cached pages and delalloc blocks will not
	 * have speculative prealloc/delalloc blocks to remove.
	 */
	if (VFS_I(ip)->i_size == 0 &&
	    VN_CACHED(VFS_I(ip)) == 0 &&
	    ip->i_delayed_blks == 0)
		return false;

	/* If we haven't read in the extent list, then don't do it now. */
	if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
		return false;

	/*
	 * Do not free real preallocated or append-only files unless the file
	 * has delalloc blocks and we are forced to remove them.
	 */
	if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
		if (!force || ip->i_delayed_blks == 0)
			return false;

	return true;
}