PageRenderTime 7ms CodeModel.GetById 22ms app.highlight 70ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_vnodeops.c

https://bitbucket.org/digetx/picasso-kernel
C | 2348 lines | 1519 code | 282 blank | 547 comment | 338 complexity | 94f8a60308c5d2a920684c3c4903862e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
   1/*
   2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it would be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write the Free Software Foundation,
  16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17 */
  18
  19#include "xfs.h"
  20#include "xfs_fs.h"
  21#include "xfs_types.h"
  22#include "xfs_bit.h"
  23#include "xfs_log.h"
  24#include "xfs_trans.h"
  25#include "xfs_sb.h"
  26#include "xfs_ag.h"
  27#include "xfs_dir2.h"
  28#include "xfs_mount.h"
  29#include "xfs_da_btree.h"
  30#include "xfs_bmap_btree.h"
  31#include "xfs_ialloc_btree.h"
  32#include "xfs_dinode.h"
  33#include "xfs_inode.h"
  34#include "xfs_inode_item.h"
  35#include "xfs_itable.h"
  36#include "xfs_ialloc.h"
  37#include "xfs_alloc.h"
  38#include "xfs_bmap.h"
  39#include "xfs_acl.h"
  40#include "xfs_attr.h"
  41#include "xfs_error.h"
  42#include "xfs_quota.h"
  43#include "xfs_utils.h"
  44#include "xfs_rtalloc.h"
  45#include "xfs_trans_space.h"
  46#include "xfs_log_priv.h"
  47#include "xfs_filestream.h"
  48#include "xfs_vnodeops.h"
  49#include "xfs_trace.h"
  50#include "xfs_icache.h"
  51
  52/*
  53 * The maximum pathlen is 1024 bytes. Since the minimum file system
  54 * blocksize is 512 bytes, we can get a max of 2 extents back from
  55 * bmapi.
  56 */
  57#define SYMLINK_MAPS 2
  58
  59STATIC int
  60xfs_readlink_bmap(
  61	xfs_inode_t	*ip,
  62	char		*link)
  63{
  64	xfs_mount_t	*mp = ip->i_mount;
  65	int		pathlen = ip->i_d.di_size;
  66	int             nmaps = SYMLINK_MAPS;
  67	xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  68	xfs_daddr_t	d;
  69	int		byte_cnt;
  70	int		n;
  71	xfs_buf_t	*bp;
  72	int		error = 0;
  73
  74	error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
  75			       0);
  76	if (error)
  77		goto out;
  78
  79	for (n = 0; n < nmaps; n++) {
  80		d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  81		byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  82
  83		bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0, NULL);
  84		if (!bp)
  85			return XFS_ERROR(ENOMEM);
  86		error = bp->b_error;
  87		if (error) {
  88			xfs_buf_ioerror_alert(bp, __func__);
  89			xfs_buf_relse(bp);
  90			goto out;
  91		}
  92		if (pathlen < byte_cnt)
  93			byte_cnt = pathlen;
  94		pathlen -= byte_cnt;
  95
  96		memcpy(link, bp->b_addr, byte_cnt);
  97		xfs_buf_relse(bp);
  98	}
  99
 100	link[ip->i_d.di_size] = '\0';
 101	error = 0;
 102
 103 out:
 104	return error;
 105}
 106
 107int
 108xfs_readlink(
 109	xfs_inode_t     *ip,
 110	char		*link)
 111{
 112	xfs_mount_t	*mp = ip->i_mount;
 113	xfs_fsize_t	pathlen;
 114	int		error = 0;
 115
 116	trace_xfs_readlink(ip);
 117
 118	if (XFS_FORCED_SHUTDOWN(mp))
 119		return XFS_ERROR(EIO);
 120
 121	xfs_ilock(ip, XFS_ILOCK_SHARED);
 122
 123	pathlen = ip->i_d.di_size;
 124	if (!pathlen)
 125		goto out;
 126
 127	if (pathlen < 0 || pathlen > MAXPATHLEN) {
 128		xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
 129			 __func__, (unsigned long long) ip->i_ino,
 130			 (long long) pathlen);
 131		ASSERT(0);
 132		error = XFS_ERROR(EFSCORRUPTED);
 133		goto out;
 134	}
 135
 136
 137	if (ip->i_df.if_flags & XFS_IFINLINE) {
 138		memcpy(link, ip->i_df.if_u1.if_data, pathlen);
 139		link[pathlen] = '\0';
 140	} else {
 141		error = xfs_readlink_bmap(ip, link);
 142	}
 143
 144 out:
 145	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 146	return error;
 147}
 148
 149/*
 150 * This is called by xfs_inactive to free any blocks beyond eof
 151 * when the link count isn't zero and by xfs_dm_punch_hole() when
 152 * punching a hole to EOF.
 153 */
 154int
 155xfs_free_eofblocks(
 156	xfs_mount_t	*mp,
 157	xfs_inode_t	*ip,
 158	bool		need_iolock)
 159{
 160	xfs_trans_t	*tp;
 161	int		error;
 162	xfs_fileoff_t	end_fsb;
 163	xfs_fileoff_t	last_fsb;
 164	xfs_filblks_t	map_len;
 165	int		nimaps;
 166	xfs_bmbt_irec_t	imap;
 167
 168	/*
 169	 * Figure out if there are any blocks beyond the end
 170	 * of the file.  If not, then there is nothing to do.
 171	 */
 172	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
 173	last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
 174	if (last_fsb <= end_fsb)
 175		return 0;
 176	map_len = last_fsb - end_fsb;
 177
 178	nimaps = 1;
 179	xfs_ilock(ip, XFS_ILOCK_SHARED);
 180	error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
 181	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 182
 183	if (!error && (nimaps != 0) &&
 184	    (imap.br_startblock != HOLESTARTBLOCK ||
 185	     ip->i_delayed_blks)) {
 186		/*
 187		 * Attach the dquots to the inode up front.
 188		 */
 189		error = xfs_qm_dqattach(ip, 0);
 190		if (error)
 191			return error;
 192
 193		/*
 194		 * There are blocks after the end of file.
 195		 * Free them up now by truncating the file to
 196		 * its current size.
 197		 */
 198		tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
 199
 200		if (need_iolock) {
 201			if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
 202				xfs_trans_cancel(tp, 0);
 203				return EAGAIN;
 204			}
 205		}
 206
 207		error = xfs_trans_reserve(tp, 0,
 208					  XFS_ITRUNCATE_LOG_RES(mp),
 209					  0, XFS_TRANS_PERM_LOG_RES,
 210					  XFS_ITRUNCATE_LOG_COUNT);
 211		if (error) {
 212			ASSERT(XFS_FORCED_SHUTDOWN(mp));
 213			xfs_trans_cancel(tp, 0);
 214			if (need_iolock)
 215				xfs_iunlock(ip, XFS_IOLOCK_EXCL);
 216			return error;
 217		}
 218
 219		xfs_ilock(ip, XFS_ILOCK_EXCL);
 220		xfs_trans_ijoin(tp, ip, 0);
 221
 222		/*
 223		 * Do not update the on-disk file size.  If we update the
 224		 * on-disk file size and then the system crashes before the
 225		 * contents of the file are flushed to disk then the files
 226		 * may be full of holes (ie NULL files bug).
 227		 */
 228		error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
 229					      XFS_ISIZE(ip));
 230		if (error) {
 231			/*
 232			 * If we get an error at this point we simply don't
 233			 * bother truncating the file.
 234			 */
 235			xfs_trans_cancel(tp,
 236					 (XFS_TRANS_RELEASE_LOG_RES |
 237					  XFS_TRANS_ABORT));
 238		} else {
 239			error = xfs_trans_commit(tp,
 240						XFS_TRANS_RELEASE_LOG_RES);
 241			if (!error)
 242				xfs_inode_clear_eofblocks_tag(ip);
 243		}
 244
 245		xfs_iunlock(ip, XFS_ILOCK_EXCL);
 246		if (need_iolock)
 247			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
 248	}
 249	return error;
 250}
 251
 252/*
 253 * Free a symlink that has blocks associated with it.
 254 */
 255STATIC int
 256xfs_inactive_symlink_rmt(
 257	xfs_inode_t	*ip,
 258	xfs_trans_t	**tpp)
 259{
 260	xfs_buf_t	*bp;
 261	int		committed;
 262	int		done;
 263	int		error;
 264	xfs_fsblock_t	first_block;
 265	xfs_bmap_free_t	free_list;
 266	int		i;
 267	xfs_mount_t	*mp;
 268	xfs_bmbt_irec_t	mval[SYMLINK_MAPS];
 269	int		nmaps;
 270	xfs_trans_t	*ntp;
 271	int		size;
 272	xfs_trans_t	*tp;
 273
 274	tp = *tpp;
 275	mp = ip->i_mount;
 276	ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
 277	/*
 278	 * We're freeing a symlink that has some
 279	 * blocks allocated to it.  Free the
 280	 * blocks here.  We know that we've got
 281	 * either 1 or 2 extents and that we can
 282	 * free them all in one bunmapi call.
 283	 */
 284	ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
 285
 286	/*
 287	 * Lock the inode, fix the size, and join it to the transaction.
 288	 * Hold it so in the normal path, we still have it locked for
 289	 * the second transaction.  In the error paths we need it
 290	 * held so the cancel won't rele it, see below.
 291	 */
 292	size = (int)ip->i_d.di_size;
 293	ip->i_d.di_size = 0;
 294	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
 295	/*
 296	 * Find the block(s) so we can inval and unmap them.
 297	 */
 298	done = 0;
 299	xfs_bmap_init(&free_list, &first_block);
 300	nmaps = ARRAY_SIZE(mval);
 301	error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, size),
 302				mval, &nmaps, 0);
 303	if (error)
 304		goto error0;
 305	/*
 306	 * Invalidate the block(s).
 307	 */
 308	for (i = 0; i < nmaps; i++) {
 309		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
 310			XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
 311			XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
 312		if (!bp) {
 313			error = ENOMEM;
 314			goto error1;
 315		}
 316		xfs_trans_binval(tp, bp);
 317	}
 318	/*
 319	 * Unmap the dead block(s) to the free_list.
 320	 */
 321	if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
 322			&first_block, &free_list, &done)))
 323		goto error1;
 324	ASSERT(done);
 325	/*
 326	 * Commit the first transaction.  This logs the EFI and the inode.
 327	 */
 328	if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
 329		goto error1;
 330	/*
 331	 * The transaction must have been committed, since there were
 332	 * actually extents freed by xfs_bunmapi.  See xfs_bmap_finish.
 333	 * The new tp has the extent freeing and EFDs.
 334	 */
 335	ASSERT(committed);
 336	/*
 337	 * The first xact was committed, so add the inode to the new one.
 338	 * Mark it dirty so it will be logged and moved forward in the log as
 339	 * part of every commit.
 340	 */
 341	xfs_trans_ijoin(tp, ip, 0);
 342	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
 343	/*
 344	 * Get a new, empty transaction to return to our caller.
 345	 */
 346	ntp = xfs_trans_dup(tp);
 347	/*
 348	 * Commit the transaction containing extent freeing and EFDs.
 349	 * If we get an error on the commit here or on the reserve below,
 350	 * we need to unlock the inode since the new transaction doesn't
 351	 * have the inode attached.
 352	 */
 353	error = xfs_trans_commit(tp, 0);
 354	tp = ntp;
 355	if (error) {
 356		ASSERT(XFS_FORCED_SHUTDOWN(mp));
 357		goto error0;
 358	}
 359	/*
 360	 * transaction commit worked ok so we can drop the extra ticket
 361	 * reference that we gained in xfs_trans_dup()
 362	 */
 363	xfs_log_ticket_put(tp->t_ticket);
 364
 365	/*
 366	 * Remove the memory for extent descriptions (just bookkeeping).
 367	 */
 368	if (ip->i_df.if_bytes)
 369		xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
 370	ASSERT(ip->i_df.if_bytes == 0);
 371	/*
 372	 * Put an itruncate log reservation in the new transaction
 373	 * for our caller.
 374	 */
 375	if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
 376			XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
 377		ASSERT(XFS_FORCED_SHUTDOWN(mp));
 378		goto error0;
 379	}
 380
 381	xfs_trans_ijoin(tp, ip, 0);
 382	*tpp = tp;
 383	return 0;
 384
 385 error1:
 386	xfs_bmap_cancel(&free_list);
 387 error0:
 388	return error;
 389}
 390
 391int
 392xfs_release(
 393	xfs_inode_t	*ip)
 394{
 395	xfs_mount_t	*mp = ip->i_mount;
 396	int		error;
 397
 398	if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
 399		return 0;
 400
 401	/* If this is a read-only mount, don't do this (would generate I/O) */
 402	if (mp->m_flags & XFS_MOUNT_RDONLY)
 403		return 0;
 404
 405	if (!XFS_FORCED_SHUTDOWN(mp)) {
 406		int truncated;
 407
 408		/*
 409		 * If we are using filestreams, and we have an unlinked
 410		 * file that we are processing the last close on, then nothing
 411		 * will be able to reopen and write to this file. Purge this
 412		 * inode from the filestreams cache so that it doesn't delay
 413		 * teardown of the inode.
 414		 */
 415		if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
 416			xfs_filestream_deassociate(ip);
 417
 418		/*
 419		 * If we previously truncated this file and removed old data
 420		 * in the process, we want to initiate "early" writeout on
 421		 * the last close.  This is an attempt to combat the notorious
 422		 * NULL files problem which is particularly noticeable from a
 423		 * truncate down, buffered (re-)write (delalloc), followed by
 424		 * a crash.  What we are effectively doing here is
 425		 * significantly reducing the time window where we'd otherwise
 426		 * be exposed to that problem.
 427		 */
 428		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
 429		if (truncated) {
 430			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
 431			if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
 432				error = -filemap_flush(VFS_I(ip)->i_mapping);
 433				if (error)
 434					return error;
 435			}
 436		}
 437	}
 438
 439	if (ip->i_d.di_nlink == 0)
 440		return 0;
 441
 442	if (xfs_can_free_eofblocks(ip, false)) {
 443
 444		/*
 445		 * If we can't get the iolock just skip truncating the blocks
 446		 * past EOF because we could deadlock with the mmap_sem
 447		 * otherwise.  We'll get another chance to drop them once the
 448		 * last reference to the inode is dropped, so we'll never leak
 449		 * blocks permanently.
 450		 *
 451		 * Further, check if the inode is being opened, written and
 452		 * closed frequently and we have delayed allocation blocks
 453		 * outstanding (e.g. streaming writes from the NFS server),
 454		 * truncating the blocks past EOF will cause fragmentation to
 455		 * occur.
 456		 *
 457		 * In this case don't do the truncation, either, but we have to
 458		 * be careful how we detect this case. Blocks beyond EOF show
 459		 * up as i_delayed_blks even when the inode is clean, so we
 460		 * need to truncate them away first before checking for a dirty
 461		 * release. Hence on the first dirty close we will still remove
 462		 * the speculative allocation, but after that we will leave it
 463		 * in place.
 464		 */
 465		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
 466			return 0;
 467
 468		error = xfs_free_eofblocks(mp, ip, true);
 469		if (error && error != EAGAIN)
 470			return error;
 471
 472		/* delalloc blocks after truncation means it really is dirty */
 473		if (ip->i_delayed_blks)
 474			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
 475	}
 476	return 0;
 477}
 478
 479/*
 480 * xfs_inactive
 481 *
 482 * This is called when the vnode reference count for the vnode
 483 * goes to zero.  If the file has been unlinked, then it must
 484 * now be truncated.  Also, we clear all of the read-ahead state
 485 * kept for the inode here since the file is now closed.
 486 */
 487int
 488xfs_inactive(
 489	xfs_inode_t	*ip)
 490{
 491	xfs_bmap_free_t	free_list;
 492	xfs_fsblock_t	first_block;
 493	int		committed;
 494	xfs_trans_t	*tp;
 495	xfs_mount_t	*mp;
 496	int		error;
 497	int		truncate = 0;
 498
 499	/*
 500	 * If the inode is already free, then there can be nothing
 501	 * to clean up here.
 502	 */
 503	if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
 504		ASSERT(ip->i_df.if_real_bytes == 0);
 505		ASSERT(ip->i_df.if_broot_bytes == 0);
 506		return VN_INACTIVE_CACHE;
 507	}
 508
 509	mp = ip->i_mount;
 510
 511	error = 0;
 512
 513	/* If this is a read-only mount, don't do this (would generate I/O) */
 514	if (mp->m_flags & XFS_MOUNT_RDONLY)
 515		goto out;
 516
 517	if (ip->i_d.di_nlink != 0) {
 518		/*
 519		 * force is true because we are evicting an inode from the
 520		 * cache. Post-eof blocks must be freed, lest we end up with
 521		 * broken free space accounting.
 522		 */
 523		if (xfs_can_free_eofblocks(ip, true)) {
 524			error = xfs_free_eofblocks(mp, ip, false);
 525			if (error)
 526				return VN_INACTIVE_CACHE;
 527		}
 528		goto out;
 529	}
 530
 531	if (S_ISREG(ip->i_d.di_mode) &&
 532	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
 533	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
 534		truncate = 1;
 535
 536	error = xfs_qm_dqattach(ip, 0);
 537	if (error)
 538		return VN_INACTIVE_CACHE;
 539
 540	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
 541	error = xfs_trans_reserve(tp, 0,
 542			(truncate || S_ISLNK(ip->i_d.di_mode)) ?
 543				XFS_ITRUNCATE_LOG_RES(mp) :
 544				XFS_IFREE_LOG_RES(mp),
 545			0,
 546			XFS_TRANS_PERM_LOG_RES,
 547			XFS_ITRUNCATE_LOG_COUNT);
 548	if (error) {
 549		ASSERT(XFS_FORCED_SHUTDOWN(mp));
 550		xfs_trans_cancel(tp, 0);
 551		return VN_INACTIVE_CACHE;
 552	}
 553
 554	xfs_ilock(ip, XFS_ILOCK_EXCL);
 555	xfs_trans_ijoin(tp, ip, 0);
 556
 557	if (S_ISLNK(ip->i_d.di_mode)) {
 558		/*
 559		 * Zero length symlinks _can_ exist.
 560		 */
 561		if (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) {
 562			error = xfs_inactive_symlink_rmt(ip, &tp);
 563			if (error)
 564				goto out_cancel;
 565		} else if (ip->i_df.if_bytes > 0) {
 566			xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
 567					  XFS_DATA_FORK);
 568			ASSERT(ip->i_df.if_bytes == 0);
 569		}
 570	} else if (truncate) {
 571		ip->i_d.di_size = 0;
 572		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
 573
 574		error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
 575		if (error)
 576			goto out_cancel;
 577
 578		ASSERT(ip->i_d.di_nextents == 0);
 579	}
 580
 581	/*
 582	 * If there are attributes associated with the file then blow them away
 583	 * now.  The code calls a routine that recursively deconstructs the
 584	 * attribute fork.  We need to just commit the current transaction
 585	 * because we can't use it for xfs_attr_inactive().
 586	 */
 587	if (ip->i_d.di_anextents > 0) {
 588		ASSERT(ip->i_d.di_forkoff != 0);
 589
 590		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
 591		if (error)
 592			goto out_unlock;
 593
 594		xfs_iunlock(ip, XFS_ILOCK_EXCL);
 595
 596		error = xfs_attr_inactive(ip);
 597		if (error)
 598			goto out;
 599
 600		tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
 601		error = xfs_trans_reserve(tp, 0,
 602					  XFS_IFREE_LOG_RES(mp),
 603					  0, XFS_TRANS_PERM_LOG_RES,
 604					  XFS_INACTIVE_LOG_COUNT);
 605		if (error) {
 606			xfs_trans_cancel(tp, 0);
 607			goto out;
 608		}
 609
 610		xfs_ilock(ip, XFS_ILOCK_EXCL);
 611		xfs_trans_ijoin(tp, ip, 0);
 612	}
 613
 614	if (ip->i_afp)
 615		xfs_idestroy_fork(ip, XFS_ATTR_FORK);
 616
 617	ASSERT(ip->i_d.di_anextents == 0);
 618
 619	/*
 620	 * Free the inode.
 621	 */
 622	xfs_bmap_init(&free_list, &first_block);
 623	error = xfs_ifree(tp, ip, &free_list);
 624	if (error) {
 625		/*
 626		 * If we fail to free the inode, shut down.  The cancel
 627		 * might do that, we need to make sure.  Otherwise the
 628		 * inode might be lost for a long time or forever.
 629		 */
 630		if (!XFS_FORCED_SHUTDOWN(mp)) {
 631			xfs_notice(mp, "%s: xfs_ifree returned error %d",
 632				__func__, error);
 633			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
 634		}
 635		xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
 636	} else {
 637		/*
 638		 * Credit the quota account(s). The inode is gone.
 639		 */
 640		xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
 641
 642		/*
 643		 * Just ignore errors at this point.  There is nothing we can
 644		 * do except to try to keep going. Make sure it's not a silent
 645		 * error.
 646		 */
 647		error = xfs_bmap_finish(&tp,  &free_list, &committed);
 648		if (error)
 649			xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
 650				__func__, error);
 651		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
 652		if (error)
 653			xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
 654				__func__, error);
 655	}
 656
 657	/*
 658	 * Release the dquots held by inode, if any.
 659	 */
 660	xfs_qm_dqdetach(ip);
 661out_unlock:
 662	xfs_iunlock(ip, XFS_ILOCK_EXCL);
 663out:
 664	return VN_INACTIVE_CACHE;
 665out_cancel:
 666	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
 667	goto out_unlock;
 668}
 669
 670/*
 671 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
 672 * is allowed, otherwise it has to be an exact match. If a CI match is found,
 673 * ci_name->name will point to a the actual name (caller must free) or
 674 * will be set to NULL if an exact match is found.
 675 */
 676int
 677xfs_lookup(
 678	xfs_inode_t		*dp,
 679	struct xfs_name		*name,
 680	xfs_inode_t		**ipp,
 681	struct xfs_name		*ci_name)
 682{
 683	xfs_ino_t		inum;
 684	int			error;
 685	uint			lock_mode;
 686
 687	trace_xfs_lookup(dp, name);
 688
 689	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
 690		return XFS_ERROR(EIO);
 691
 692	lock_mode = xfs_ilock_map_shared(dp);
 693	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
 694	xfs_iunlock_map_shared(dp, lock_mode);
 695
 696	if (error)
 697		goto out;
 698
 699	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
 700	if (error)
 701		goto out_free_name;
 702
 703	return 0;
 704
 705out_free_name:
 706	if (ci_name)
 707		kmem_free(ci_name->name);
 708out:
 709	*ipp = NULL;
 710	return error;
 711}
 712
 713int
 714xfs_create(
 715	xfs_inode_t		*dp,
 716	struct xfs_name		*name,
 717	umode_t			mode,
 718	xfs_dev_t		rdev,
 719	xfs_inode_t		**ipp)
 720{
 721	int			is_dir = S_ISDIR(mode);
 722	struct xfs_mount	*mp = dp->i_mount;
 723	struct xfs_inode	*ip = NULL;
 724	struct xfs_trans	*tp = NULL;
 725	int			error;
 726	xfs_bmap_free_t		free_list;
 727	xfs_fsblock_t		first_block;
 728	boolean_t		unlock_dp_on_error = B_FALSE;
 729	uint			cancel_flags;
 730	int			committed;
 731	prid_t			prid;
 732	struct xfs_dquot	*udqp = NULL;
 733	struct xfs_dquot	*gdqp = NULL;
 734	uint			resblks;
 735	uint			log_res;
 736	uint			log_count;
 737
 738	trace_xfs_create(dp, name);
 739
 740	if (XFS_FORCED_SHUTDOWN(mp))
 741		return XFS_ERROR(EIO);
 742
 743	if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
 744		prid = xfs_get_projid(dp);
 745	else
 746		prid = XFS_PROJID_DEFAULT;
 747
 748	/*
 749	 * Make sure that we have allocated dquot(s) on disk.
 750	 */
 751	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
 752			XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
 753	if (error)
 754		return error;
 755
 756	if (is_dir) {
 757		rdev = 0;
 758		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
 759		log_res = XFS_MKDIR_LOG_RES(mp);
 760		log_count = XFS_MKDIR_LOG_COUNT;
 761		tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
 762	} else {
 763		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
 764		log_res = XFS_CREATE_LOG_RES(mp);
 765		log_count = XFS_CREATE_LOG_COUNT;
 766		tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
 767	}
 768
 769	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
 770
 771	/*
 772	 * Initially assume that the file does not exist and
 773	 * reserve the resources for that case.  If that is not
 774	 * the case we'll drop the one we have and get a more
 775	 * appropriate transaction later.
 776	 */
 777	error = xfs_trans_reserve(tp, resblks, log_res, 0,
 778			XFS_TRANS_PERM_LOG_RES, log_count);
 779	if (error == ENOSPC) {
 780		/* flush outstanding delalloc blocks and retry */
 781		xfs_flush_inodes(mp);
 782		error = xfs_trans_reserve(tp, resblks, log_res, 0,
 783				XFS_TRANS_PERM_LOG_RES, log_count);
 784	}
 785	if (error == ENOSPC) {
 786		/* No space at all so try a "no-allocation" reservation */
 787		resblks = 0;
 788		error = xfs_trans_reserve(tp, 0, log_res, 0,
 789				XFS_TRANS_PERM_LOG_RES, log_count);
 790	}
 791	if (error) {
 792		cancel_flags = 0;
 793		goto out_trans_cancel;
 794	}
 795
 796	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
 797	unlock_dp_on_error = B_TRUE;
 798
 799	xfs_bmap_init(&free_list, &first_block);
 800
 801	/*
 802	 * Reserve disk quota and the inode.
 803	 */
 804	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
 805	if (error)
 806		goto out_trans_cancel;
 807
 808	error = xfs_dir_canenter(tp, dp, name, resblks);
 809	if (error)
 810		goto out_trans_cancel;
 811
 812	/*
 813	 * A newly created regular or special file just has one directory
 814	 * entry pointing to them, but a directory also the "." entry
 815	 * pointing to itself.
 816	 */
 817	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
 818			       prid, resblks > 0, &ip, &committed);
 819	if (error) {
 820		if (error == ENOSPC)
 821			goto out_trans_cancel;
 822		goto out_trans_abort;
 823	}
 824
 825	/*
 826	 * Now we join the directory inode to the transaction.  We do not do it
 827	 * earlier because xfs_dir_ialloc might commit the previous transaction
 828	 * (and release all the locks).  An error from here on will result in
 829	 * the transaction cancel unlocking dp so don't do it explicitly in the
 830	 * error path.
 831	 */
 832	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
 833	unlock_dp_on_error = B_FALSE;
 834
 835	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
 836					&first_block, &free_list, resblks ?
 837					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
 838	if (error) {
 839		ASSERT(error != ENOSPC);
 840		goto out_trans_abort;
 841	}
 842	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 843	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
 844
 845	if (is_dir) {
 846		error = xfs_dir_init(tp, ip, dp);
 847		if (error)
 848			goto out_bmap_cancel;
 849
 850		error = xfs_bumplink(tp, dp);
 851		if (error)
 852			goto out_bmap_cancel;
 853	}
 854
 855	/*
 856	 * If this is a synchronous mount, make sure that the
 857	 * create transaction goes to disk before returning to
 858	 * the user.
 859	 */
 860	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
 861		xfs_trans_set_sync(tp);
 862
 863	/*
 864	 * Attach the dquot(s) to the inodes and modify them incore.
 865	 * These ids of the inode couldn't have changed since the new
 866	 * inode has been locked ever since it was created.
 867	 */
 868	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
 869
 870	error = xfs_bmap_finish(&tp, &free_list, &committed);
 871	if (error)
 872		goto out_bmap_cancel;
 873
 874	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
 875	if (error)
 876		goto out_release_inode;
 877
 878	xfs_qm_dqrele(udqp);
 879	xfs_qm_dqrele(gdqp);
 880
 881	*ipp = ip;
 882	return 0;
 883
 884 out_bmap_cancel:
 885	xfs_bmap_cancel(&free_list);
 886 out_trans_abort:
 887	cancel_flags |= XFS_TRANS_ABORT;
 888 out_trans_cancel:
 889	xfs_trans_cancel(tp, cancel_flags);
 890 out_release_inode:
 891	/*
 892	 * Wait until after the current transaction is aborted to
 893	 * release the inode.  This prevents recursive transactions
 894	 * and deadlocks from xfs_inactive.
 895	 */
 896	if (ip)
 897		IRELE(ip);
 898
 899	xfs_qm_dqrele(udqp);
 900	xfs_qm_dqrele(gdqp);
 901
 902	if (unlock_dp_on_error)
 903		xfs_iunlock(dp, XFS_ILOCK_EXCL);
 904	return error;
 905}
 906
 907#ifdef DEBUG
 908int xfs_locked_n;
 909int xfs_small_retries;
 910int xfs_middle_retries;
 911int xfs_lots_retries;
 912int xfs_lock_delays;
 913#endif
 914
 915/*
 916 * Bump the subclass so xfs_lock_inodes() acquires each lock with
 917 * a different value
 918 */
 919static inline int
 920xfs_lock_inumorder(int lock_mode, int subclass)
 921{
 922	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
 923		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
 924	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
 925		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
 926
 927	return lock_mode;
 928}
 929
 930/*
 931 * The following routine will lock n inodes in exclusive mode.
 932 * We assume the caller calls us with the inodes in i_ino order.
 933 *
 934 * We need to detect deadlock where an inode that we lock
 935 * is in the AIL and we start waiting for another inode that is locked
 936 * by a thread in a long running transaction (such as truncate). This can
 937 * result in deadlock since the long running trans might need to wait
 938 * for the inode we just locked in order to push the tail and free space
 939 * in the log.
 940 */
 941void
 942xfs_lock_inodes(
 943	xfs_inode_t	**ips,
 944	int		inodes,
 945	uint		lock_mode)
 946{
 947	int		attempts = 0, i, j, try_lock;
 948	xfs_log_item_t	*lp;
 949
 950	ASSERT(ips && (inodes >= 2)); /* we need at least two */
 951
 952	try_lock = 0;
 953	i = 0;
 954
 955again:
 956	for (; i < inodes; i++) {
 957		ASSERT(ips[i]);
 958
 959		if (i && (ips[i] == ips[i-1]))	/* Already locked */
 960			continue;
 961
 962		/*
 963		 * If try_lock is not set yet, make sure all locked inodes
 964		 * are not in the AIL.
 965		 * If any are, set try_lock to be used later.
 966		 */
 967
 968		if (!try_lock) {
 969			for (j = (i - 1); j >= 0 && !try_lock; j--) {
 970				lp = (xfs_log_item_t *)ips[j]->i_itemp;
 971				if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
 972					try_lock++;
 973				}
 974			}
 975		}
 976
 977		/*
 978		 * If any of the previous locks we have locked is in the AIL,
 979		 * we must TRY to get the second and subsequent locks. If
 980		 * we can't get any, we must release all we have
 981		 * and try again.
 982		 */
 983
 984		if (try_lock) {
 985			/* try_lock must be 0 if i is 0. */
 986			/*
 987			 * try_lock means we have an inode locked
 988			 * that is in the AIL.
 989			 */
 990			ASSERT(i != 0);
 991			if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
 992				attempts++;
 993
 994				/*
 995				 * Unlock all previous guys and try again.
 996				 * xfs_iunlock will try to push the tail
 997				 * if the inode is in the AIL.
 998				 */
 999
1000				for(j = i - 1; j >= 0; j--) {
1001
1002					/*
1003					 * Check to see if we've already
1004					 * unlocked this one.
1005					 * Not the first one going back,
1006					 * and the inode ptr is the same.
1007					 */
1008					if ((j != (i - 1)) && ips[j] ==
1009								ips[j+1])
1010						continue;
1011
1012					xfs_iunlock(ips[j], lock_mode);
1013				}
1014
1015				if ((attempts % 5) == 0) {
1016					delay(1); /* Don't just spin the CPU */
1017#ifdef DEBUG
1018					xfs_lock_delays++;
1019#endif
1020				}
1021				i = 0;
1022				try_lock = 0;
1023				goto again;
1024			}
1025		} else {
1026			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1027		}
1028	}
1029
1030#ifdef DEBUG
1031	if (attempts) {
1032		if (attempts < 5) xfs_small_retries++;
1033		else if (attempts < 100) xfs_middle_retries++;
1034		else xfs_lots_retries++;
1035	} else {
1036		xfs_locked_n++;
1037	}
1038#endif
1039}
1040
1041/*
1042 * xfs_lock_two_inodes() can only be used to lock one type of lock
1043 * at a time - the iolock or the ilock, but not both at once. If
1044 * we lock both at once, lockdep will report false positives saying
1045 * we have violated locking orders.
1046 */
1047void
1048xfs_lock_two_inodes(
1049	xfs_inode_t		*ip0,
1050	xfs_inode_t		*ip1,
1051	uint			lock_mode)
1052{
1053	xfs_inode_t		*temp;
1054	int			attempts = 0;
1055	xfs_log_item_t		*lp;
1056
1057	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1058		ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1059	ASSERT(ip0->i_ino != ip1->i_ino);
1060
1061	if (ip0->i_ino > ip1->i_ino) {
1062		temp = ip0;
1063		ip0 = ip1;
1064		ip1 = temp;
1065	}
1066
1067 again:
1068	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1069
1070	/*
1071	 * If the first lock we have locked is in the AIL, we must TRY to get
1072	 * the second lock. If we can't get it, we must release the first one
1073	 * and try again.
1074	 */
1075	lp = (xfs_log_item_t *)ip0->i_itemp;
1076	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1077		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1078			xfs_iunlock(ip0, lock_mode);
1079			if ((++attempts % 5) == 0)
1080				delay(1); /* Don't just spin the CPU */
1081			goto again;
1082		}
1083	} else {
1084		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1085	}
1086}
1087
1088int
1089xfs_remove(
1090	xfs_inode_t             *dp,
1091	struct xfs_name		*name,
1092	xfs_inode_t		*ip)
1093{
1094	xfs_mount_t		*mp = dp->i_mount;
1095	xfs_trans_t             *tp = NULL;
1096	int			is_dir = S_ISDIR(ip->i_d.di_mode);
1097	int                     error = 0;
1098	xfs_bmap_free_t         free_list;
1099	xfs_fsblock_t           first_block;
1100	int			cancel_flags;
1101	int			committed;
1102	int			link_zero;
1103	uint			resblks;
1104	uint			log_count;
1105
1106	trace_xfs_remove(dp, name);
1107
1108	if (XFS_FORCED_SHUTDOWN(mp))
1109		return XFS_ERROR(EIO);
1110
1111	error = xfs_qm_dqattach(dp, 0);
1112	if (error)
1113		goto std_return;
1114
1115	error = xfs_qm_dqattach(ip, 0);
1116	if (error)
1117		goto std_return;
1118
1119	if (is_dir) {
1120		tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1121		log_count = XFS_DEFAULT_LOG_COUNT;
1122	} else {
1123		tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1124		log_count = XFS_REMOVE_LOG_COUNT;
1125	}
1126	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1127
1128	/*
1129	 * We try to get the real space reservation first,
1130	 * allowing for directory btree deletion(s) implying
1131	 * possible bmap insert(s).  If we can't get the space
1132	 * reservation then we use 0 instead, and avoid the bmap
1133	 * btree insert(s) in the directory code by, if the bmap
1134	 * insert tries to happen, instead trimming the LAST
1135	 * block from the directory.
1136	 */
1137	resblks = XFS_REMOVE_SPACE_RES(mp);
1138	error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1139				  XFS_TRANS_PERM_LOG_RES, log_count);
1140	if (error == ENOSPC) {
1141		resblks = 0;
1142		error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1143					  XFS_TRANS_PERM_LOG_RES, log_count);
1144	}
1145	if (error) {
1146		ASSERT(error != ENOSPC);
1147		cancel_flags = 0;
1148		goto out_trans_cancel;
1149	}
1150
1151	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1152
1153	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1154	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1155
1156	/*
1157	 * If we're removing a directory perform some additional validation.
1158	 */
1159	if (is_dir) {
1160		ASSERT(ip->i_d.di_nlink >= 2);
1161		if (ip->i_d.di_nlink != 2) {
1162			error = XFS_ERROR(ENOTEMPTY);
1163			goto out_trans_cancel;
1164		}
1165		if (!xfs_dir_isempty(ip)) {
1166			error = XFS_ERROR(ENOTEMPTY);
1167			goto out_trans_cancel;
1168		}
1169	}
1170
1171	xfs_bmap_init(&free_list, &first_block);
1172	error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1173					&first_block, &free_list, resblks);
1174	if (error) {
1175		ASSERT(error != ENOENT);
1176		goto out_bmap_cancel;
1177	}
1178	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1179
1180	if (is_dir) {
1181		/*
1182		 * Drop the link from ip's "..".
1183		 */
1184		error = xfs_droplink(tp, dp);
1185		if (error)
1186			goto out_bmap_cancel;
1187
1188		/*
1189		 * Drop the "." link from ip to self.
1190		 */
1191		error = xfs_droplink(tp, ip);
1192		if (error)
1193			goto out_bmap_cancel;
1194	} else {
1195		/*
1196		 * When removing a non-directory we need to log the parent
1197		 * inode here.  For a directory this is done implicitly
1198		 * by the xfs_droplink call for the ".." entry.
1199		 */
1200		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1201	}
1202
1203	/*
1204	 * Drop the link from dp to ip.
1205	 */
1206	error = xfs_droplink(tp, ip);
1207	if (error)
1208		goto out_bmap_cancel;
1209
1210	/*
1211	 * Determine if this is the last link while
1212	 * we are in the transaction.
1213	 */
1214	link_zero = (ip->i_d.di_nlink == 0);
1215
1216	/*
1217	 * If this is a synchronous mount, make sure that the
1218	 * remove transaction goes to disk before returning to
1219	 * the user.
1220	 */
1221	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1222		xfs_trans_set_sync(tp);
1223
1224	error = xfs_bmap_finish(&tp, &free_list, &committed);
1225	if (error)
1226		goto out_bmap_cancel;
1227
1228	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1229	if (error)
1230		goto std_return;
1231
1232	/*
1233	 * If we are using filestreams, kill the stream association.
1234	 * If the file is still open it may get a new one but that
1235	 * will get killed on last close in xfs_close() so we don't
1236	 * have to worry about that.
1237	 */
1238	if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1239		xfs_filestream_deassociate(ip);
1240
1241	return 0;
1242
1243 out_bmap_cancel:
1244	xfs_bmap_cancel(&free_list);
1245	cancel_flags |= XFS_TRANS_ABORT;
1246 out_trans_cancel:
1247	xfs_trans_cancel(tp, cancel_flags);
1248 std_return:
1249	return error;
1250}
1251
1252int
1253xfs_link(
1254	xfs_inode_t		*tdp,
1255	xfs_inode_t		*sip,
1256	struct xfs_name		*target_name)
1257{
1258	xfs_mount_t		*mp = tdp->i_mount;
1259	xfs_trans_t		*tp;
1260	int			error;
1261	xfs_bmap_free_t         free_list;
1262	xfs_fsblock_t           first_block;
1263	int			cancel_flags;
1264	int			committed;
1265	int			resblks;
1266
1267	trace_xfs_link(tdp, target_name);
1268
1269	ASSERT(!S_ISDIR(sip->i_d.di_mode));
1270
1271	if (XFS_FORCED_SHUTDOWN(mp))
1272		return XFS_ERROR(EIO);
1273
1274	error = xfs_qm_dqattach(sip, 0);
1275	if (error)
1276		goto std_return;
1277
1278	error = xfs_qm_dqattach(tdp, 0);
1279	if (error)
1280		goto std_return;
1281
1282	tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1283	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1284	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1285	error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1286			XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1287	if (error == ENOSPC) {
1288		resblks = 0;
1289		error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1290				XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1291	}
1292	if (error) {
1293		cancel_flags = 0;
1294		goto error_return;
1295	}
1296
1297	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1298
1299	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1300	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1301
1302	/*
1303	 * If we are using project inheritance, we only allow hard link
1304	 * creation in our tree when the project IDs are the same; else
1305	 * the tree quota mechanism could be circumvented.
1306	 */
1307	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1308		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1309		error = XFS_ERROR(EXDEV);
1310		goto error_return;
1311	}
1312
1313	error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1314	if (error)
1315		goto error_return;
1316
1317	xfs_bmap_init(&free_list, &first_block);
1318
1319	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1320					&first_block, &free_list, resblks);
1321	if (error)
1322		goto abort_return;
1323	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1324	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1325
1326	error = xfs_bumplink(tp, sip);
1327	if (error)
1328		goto abort_return;
1329
1330	/*
1331	 * If this is a synchronous mount, make sure that the
1332	 * link transaction goes to disk before returning to
1333	 * the user.
1334	 */
1335	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1336		xfs_trans_set_sync(tp);
1337	}
1338
1339	error = xfs_bmap_finish (&tp, &free_list, &committed);
1340	if (error) {
1341		xfs_bmap_cancel(&free_list);
1342		goto abort_return;
1343	}
1344
1345	return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1346
1347 abort_return:
1348	cancel_flags |= XFS_TRANS_ABORT;
1349 error_return:
1350	xfs_trans_cancel(tp, cancel_flags);
1351 std_return:
1352	return error;
1353}
1354
1355int
1356xfs_symlink(
1357	xfs_inode_t		*dp,
1358	struct xfs_name		*link_name,
1359	const char		*target_path,
1360	umode_t			mode,
1361	xfs_inode_t		**ipp)
1362{
1363	xfs_mount_t		*mp = dp->i_mount;
1364	xfs_trans_t		*tp;
1365	xfs_inode_t		*ip;
1366	int			error;
1367	int			pathlen;
1368	xfs_bmap_free_t		free_list;
1369	xfs_fsblock_t		first_block;
1370	boolean_t		unlock_dp_on_error = B_FALSE;
1371	uint			cancel_flags;
1372	int			committed;
1373	xfs_fileoff_t		first_fsb;
1374	xfs_filblks_t		fs_blocks;
1375	int			nmaps;
1376	xfs_bmbt_irec_t		mval[SYMLINK_MAPS];
1377	xfs_daddr_t		d;
1378	const char		*cur_chunk;
1379	int			byte_cnt;
1380	int			n;
1381	xfs_buf_t		*bp;
1382	prid_t			prid;
1383	struct xfs_dquot	*udqp, *gdqp;
1384	uint			resblks;
1385
1386	*ipp = NULL;
1387	error = 0;
1388	ip = NULL;
1389	tp = NULL;
1390
1391	trace_xfs_symlink(dp, link_name);
1392
1393	if (XFS_FORCED_SHUTDOWN(mp))
1394		return XFS_ERROR(EIO);
1395
1396	/*
1397	 * Check component lengths of the target path name.
1398	 */
1399	pathlen = strlen(target_path);
1400	if (pathlen >= MAXPATHLEN)      /* total string too long */
1401		return XFS_ERROR(ENAMETOOLONG);
1402
1403	udqp = gdqp = NULL;
1404	if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1405		prid = xfs_get_projid(dp);
1406	else
1407		prid = XFS_PROJID_DEFAULT;
1408
1409	/*
1410	 * Make sure that we have allocated dquot(s) on disk.
1411	 */
1412	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1413			XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1414	if (error)
1415		goto std_return;
1416
1417	tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1418	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1419	/*
1420	 * The symlink will fit into the inode data fork?
1421	 * There can't be any attributes so we get the whole variable part.
1422	 */
1423	if (pathlen <= XFS_LITINO(mp))
1424		fs_blocks = 0;
1425	else
1426		fs_blocks = XFS_B_TO_FSB(mp, pathlen);
1427	resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
1428	error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1429			XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1430	if (error == ENOSPC && fs_blocks == 0) {
1431		resblks = 0;
1432		error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1433				XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1434	}
1435	if (error) {
1436		cancel_flags = 0;
1437		goto error_return;
1438	}
1439
1440	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1441	unlock_dp_on_error = B_TRUE;
1442
1443	/*
1444	 * Check whether the directory allows new symlinks or not.
1445	 */
1446	if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1447		error = XFS_ERROR(EPERM);
1448		goto error_return;
1449	}
1450
1451	/*
1452	 * Reserve disk quota : blocks and inode.
1453	 */
1454	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1455	if (error)
1456		goto error_return;
1457
1458	/*
1459	 * Check for ability to enter directory entry, if no space reserved.
1460	 */
1461	error = xfs_dir_canenter(tp, dp, link_name, resblks);
1462	if (error)
1463		goto error_return;
1464	/*
1465	 * Initialize the bmap freelist prior to calling either
1466	 * bmapi or the directory create code.
1467	 */
1468	xfs_bmap_init(&free_list, &first_block);
1469
1470	/*
1471	 * Allocate an inode for the symlink.
1472	 */
1473	error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1474			       prid, resblks > 0, &ip, NULL);
1475	if (error) {
1476		if (error == ENOSPC)
1477			goto error_return;
1478		goto error1;
1479	}
1480
1481	/*
1482	 * An error after we've joined dp to the transaction will result in the
1483	 * transaction cancel unlocking dp so don't do it explicitly in the
1484	 * error path.
1485	 */
1486	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1487	unlock_dp_on_error = B_FALSE;
1488
1489	/*
1490	 * Also attach the dquot(s) to it, if applicable.
1491	 */
1492	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1493
1494	if (resblks)
1495		resblks -= XFS_IALLOC_SPACE_RES(mp);
1496	/*
1497	 * If the symlink will fit into the inode, write it inline.
1498	 */
1499	if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1500		xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1501		memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1502		ip->i_d.di_size = pathlen;
1503
1504		/*
1505		 * The inode was initially created in extent format.
1506		 */
1507		ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1508		ip->i_df.if_flags |= XFS_IFINLINE;
1509
1510		ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1511		xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1512
1513	} else {
1514		first_fsb = 0;
1515		nmaps = SYMLINK_MAPS;
1516
1517		error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
1518				  XFS_BMAPI_METADATA, &first_block, resblks,
1519				  mval, &nmaps, &free_list);
1520		if (error)
1521			goto error2;
1522
1523		if (resblks)
1524			resblks -= fs_blocks;
1525		ip->i_d.di_size = pathlen;
1526		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1527
1528		cur_chunk = target_path;
1529		for (n = 0; n < nmaps; n++) {
1530			d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1531			byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1532			bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1533					       BTOBB(byte_cnt), 0);
1534			if (!bp) {
1535				error = ENOMEM;
1536				goto error2;
1537			}
1538			if (pathlen < byte_cnt) {
1539				byte_cnt = pathlen;
1540			}
1541			pathlen -= byte_cnt;
1542
1543			memcpy(bp->b_addr, cur_chunk, byte_cnt);
1544			cur_chunk += byte_cnt;
1545
1546			xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1547		}
1548	}
1549
1550	/*
1551	 * Create the directory entry for the symlink.
1552	 */
1553	error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1554					&first_block, &free_list, resblks);
1555	if (error)
1556		goto error2;
1557	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1558	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1559
1560	/*
1561	 * If this is a synchronous mount, make sure that the
1562	 * symlink transaction goes to disk before returning to
1563	 * the user.
1564	 */
1565	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1566		xfs_trans_set_sync(tp);
1567	}
1568
1569	error = xfs_bmap_finish(&tp, &free_list, &committed);
1570	if (error) {
1571		goto error2;
1572	}
1573	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1574	xfs_qm_dqrele(udqp);
1575	xfs_qm_dqrele(gdqp);
1576
1577	*ipp = ip;
1578	return 0;
1579
1580 error2:
1581	IRELE(ip);
1582 error1:
1583	xfs_bmap_cancel(&free_list);
1584	cancel_flags |= XFS_TRANS_ABORT;
1585 error_return:
1586	xfs_trans_cancel(tp, cancel_flags);
1587	xfs_qm_dqrele(udqp);
1588	xfs_qm_dqrele(gdqp);
1589
1590	if (unlock_dp_on_error)
1591		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1592 std_return:
1593	return error;
1594}
1595
1596int
1597xfs_set_dmattrs(
1598	xfs_inode_t     *ip,
1599	u_int		evmask,
1600	u_int16_t	state)
1601{
1602	xfs_mount_t	*mp = ip->i_mount;
1603	xfs_trans_t	*tp;
1604	int		error;
1605
1606	if (!capable(CAP_SYS_ADMIN))
1607		return XFS_ERROR(EPERM);
1608
1609	if (XFS_FORCED_SHUTDOWN(mp))
1610		return XFS_ERROR(EIO);
1611
1612	tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1613	error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1614	if (error) {
1615		xfs_trans_cancel(tp, 0);
1616		return error;
1617	}
1618	xfs_ilock(ip, XFS_ILOCK_EXCL);
1619	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1620
1621	ip->i_d.di_dmevmask = evmask;
1622	ip->i_d.di_dmstate  = state;
1623
1624	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1625	error = xfs_trans_commit(tp, 0);
1626
1627	return error;
1628}
1629
1630/*
1631 * xfs_alloc_file_space()
1632 *      This routine allocates disk space for the given file.
1633 *
1634 *	If alloc_type == 0, this request is for an ALLOCSP type
1635 *	request which will change the file size.  In this case, no
1636 *	DMAPI event will be generated by the call.  A TRUNCATE event
1637 *	will be generated later by xfs_setattr.
1638 *
1639 *	If alloc_type != 0, this request is for a RESVSP type
1640 *	request, and a DMAPI DM_EVENT_WRITE will be generated if the
1641 *	lower block boundary byte address is less than the file's
1642 *	length.
1643 *
1644 * RETURNS:
1645 *       0 on success
1646 *      errno on error
1647 *
1648 */
1649STATIC int
1650xfs_alloc_file_space(
1651	xfs_inode_t		*ip,
1652	xfs_off_t		offset,
1653	xfs_off_t		len,
1654	int			alloc_type,
1655	int			attr_flags)
1656{
1657	xfs_mount_t		*mp = ip->i_mount;
1658	xfs_off_t		count;
1659	xfs_filblks_t		allocated_fsb;
1660	xfs_filblks_t		allocatesize_fsb;
1661	xfs_extlen_t		extsz, temp;
1662	xfs_fileoff_t		startoffset_fsb;
1663	xfs_fsblock_t		firstfsb;
1664	int			nimaps;
1665	int			quota_flag;
1666	int			rt;
1667	xfs_trans_t		*tp;
1668	xfs_bmbt_irec_t		imaps[1], *imapp;
1669	xfs_bmap_free_t		free_list;
1670	uint			qblocks, resblks, resrtextents;
1671	int			committed;
1672	int			error;
1673
1674	trace_xfs_alloc_file_space(ip);
1675
1676	if (XFS_FORCED_SHUTDOWN(mp))
1677		return XFS_ERROR(EIO);
1678
1679	error = xfs_qm_dqattach(ip, 0);
1680	if (error)
1681		return error;
1682
1683	if (len <= 0)
1684		return XFS_ERROR(EINVAL);
1685
1686	rt = XFS_IS_REALTIME_INODE(ip);
1687	extsz = xfs_get_extsz_hint(ip);
1688
1689	count = len;
1690	imapp = &imaps[0];
1691	nimaps = 1;
1692	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
1693	allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1694
1695	/*
1696	 * Allocate file space until done or until there is an error
1697	 */
1698	while (allocatesize_fsb && !error) {
1699		xfs_fileoff_t	s, e;
1700
1701		/*
1702		 * Determine space reservations for data/realtime.
1703		 */
1704		if (unlikely(extsz)) {
1705			s = startoffset_fsb;
1706			do_div(s, extsz);
1707			s *= extsz;
1708			e = startoffset_fsb + allocatesize_fsb;
1709			if ((temp = do_mod(startoffset_fsb, extsz)))
1710				e += temp;
1711			if ((temp = do_mod(e, extsz)))
1712				e += extsz - temp;
1713		} else {
1714			s = 0;
1715			e = allocatesize_fsb;
1716		}
1717
1718		/*
1719		 * The transaction reservation is limited to a 32-bit block
1720		 * count, hence we need to limit the number of blocks we are
1721		 * trying to reserve to avoid an overflow. We can't allocate
1722		 * more than @nimaps extents, and an extent is limited on disk
1723		 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1724		 */
1725		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
1726		if (unlikely(rt)) {
1727			resrtextents = qblocks = resblks;
1728			resrtextents /= mp->m_sb.sb_rextsize;
1729			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1730			quota_flag = XFS_QMOPT_RES_RTBLKS;
1731		} else {
1732			resrtextents = 0;
1733			resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
1734			quota_flag = XFS_QMOPT_RES_REGBLKS;
1735		}
1736
1737		/*
1738		 * Allocate and setup the transaction.
1739		 */
1740		tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1741		error = xfs_trans_reserve(tp, resblks,
1742					  XFS_WRITE_LOG_RES(mp), resrtextents,
1743					  XFS_TRANS_PERM_LOG_RES,
1744					  XFS_WRITE_LOG_COUNT);
1745		/*
1746		 * Check for running out of space
1747		 */
1748		if (error) {
1749			/*
1750			 * Free the transaction structure.
1751			 */
1752			ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1753			xfs_trans_cancel(tp, 0);
1754			break;
1755		}
1756		xfs_ilock(ip, XFS_ILOCK_EXCL);
1757		error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1758						      0, quota_flag);
1759		if (error)
1760			goto error1;
1761
1762		xfs_trans_ijoin(tp, ip, 0);
1763
1764		xfs_bmap_init(&free_list, &firstfsb);
1765		error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1766					allocatesize_fsb, alloc_type, &firstfsb,
1767					0, imapp, &nimaps, &free_list);
1768		if (error) {
1769			goto error0;
1770		}
1771
1772		/*
1773		 * Complete the transaction
1774		 */
1775		error = xfs_bmap_finish(&tp, &free_list, &committed);
1776		if (error) {
1777			goto error0;
1778		}
1779
1780		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1781		xfs_iunlock(ip, XFS_ILOCK_EXCL);
1782		if (error) {
1783			break;
1784		}
1785
1786		allocated_fsb = imapp->br_blockcount;
1787
1788		if (nimaps == 0) {
1789			error = XFS_ERROR(ENOSPC);
1790			break;
1791		}
1792
1793		startoffset_fsb += allocated_fsb;
1794		allocatesize_fsb -= allocated_fsb;
1795	}
1796
1797	return error;
1798
1799error0:	/* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1800	xfs_bmap_cancel(&free_list);
1801	xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1802
1803error1:	/* Just cancel transaction */
1804	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1805	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1806	return error;
1807}
1808
1809/*
1810 * Zero file bytes between startoff and endoff inclusive.
1811 * The iolock is held exclusive and no blocks are buffered.
1812 *
1813 * This function is used by xfs_free_file_space() to zero
1814 * partial blocks when the range to free is not block aligned.
1815 * When unreserving space with boundaries that are not block
1816 * aligned we round up the start and round down the end
1817 * boundaries and then use this function to zero the parts of
1818 * the blocks that got dropped during the rounding.
1819 */
1820STATIC int
1821xfs_zero_remaining_bytes(
1822	xfs_inode_t		*ip,
1823	xfs_off_t		startoff,
1824	xfs_off_t		endoff)
1825{
1826	xfs_bmbt_irec_t		imap;
1827	xfs_fileoff_t		offset_fsb;
1828	xfs_off_t		lastoffset;
1829	xfs_off_t		offset;
1830	xfs_buf_t		*bp;
1831	xfs_mount_t		*mp = ip->i_mount;
1832	int			nimap;
1833	int			error = 0;
1834
1835	/*
1836	 * Avoid doing I/O beyond eof - it's not necessary
1837	 * since nothing can read beyond eof.  The space will
1838	 * be zeroed when the file is extended anyway.
1839	 */
1840	if (startoff >= XFS_ISIZE(ip))
1841		return 0;
1842
1843	if (endoff > XFS_ISIZE(ip))
1844		endoff = XFS_ISIZE(ip);
1845
1846	bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1847					mp->m_rtdev_targp : mp->m_ddev_targp,
1848				  BTOBB(mp->m_sb.sb_blocksize), 0);
1849	if (!bp)
1850		return XFS_ERROR(ENOMEM);
1851
1852	xfs_buf_unlock(bp);
1853
1854	for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1855		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1856		nimap = 1;
1857		error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
1858		if (error || nimap < 1)
1859			break;
1860		ASSERT(imap.br_blockcount >= 1);
1861		ASSERT(imap.br_startoff == offset_fsb);
1862		lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1863		if (lastoffset > endoff)
1864			lastoffset = endoff;
1865		if (imap.br_startblock == HOLESTARTBLOCK)
1866			continue;
1867		ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1868		if (imap.br_state == XFS_EXT_UNWRITTEN)
1869			continue;
1870		XFS_BUF_UNDONE(bp);
1871		XFS_BUF_UNWRITE(bp);
1872		XFS_BUF_READ(bp);
1873		XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
1874		xfsbdstrat(mp, bp);
1875		error = xfs_buf_iowait(bp);
1876		if (error) {
1877			xfs_buf_ioerror_alert(bp,
1878					"xfs_zero_remaining_bytes(read)");
1879			break;
1880		}
1881		memset(bp->b_addr +
1882			(offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
1883		      0, lastoffset - offset + 1);
1884		XFS_BUF_UNDONE(bp);
1885		XFS_BUF_UNREAD(bp);
1886		XFS_BUF_WRITE(bp);
1887		xfsbdstrat(mp, bp);
1888		error = xfs_buf_iowait(bp);
1889		if (error) {
1890			xfs_buf_ioerror_alert(bp,
1891					"xfs_zero_remaining_bytes(write)");
1892			break;
1893		}
1894	}
1895	xfs_buf_free(bp);
1896	return error;
1897}
1898
1899/*
1900 * xfs_free_file_space()
1901 *      This routine frees disk space for the given file.
1902 *
1903 *	This routine is only called by xfs_change_file_space
1904 *	for an UNRESVSP type call.
1905 *
1906 * RETURNS:
1907 *       0 on success
1908 *      errno on error
1909 *
1910 */
1911STATIC int
1912xfs_free_file_space(
1913	xfs_inode_t		*ip,
1914	xfs_off_t		offset,
1915	xfs_off_t		len,
1916	int			attr_flags)
1917{
1918	int			committed;
1919	int			done;
1920	xfs_fileoff_t		endoffset_fsb;
1921	int			error;
1922	xfs_fsblock_t		firstfsb;
1923	xfs_bmap_free_t		free_list;
1924	xfs_bmbt_irec_t		imap;
1925	xfs_off_t		ioffset;
1926	xfs_extlen_t		mod=0;
1927	xfs_mount_t		*mp;
1928	int			nimap;
1929	uint			resblks;
1930	uint			rounding;
1931	int			rt;
1932	xfs_fileoff_t		startoffset_fsb;
1933	xfs_trans_t		*tp;
1934	int			need_iolock = 1;
1935
1936	mp = ip->i_mount;
1937
1938	trace_xfs_free_file_space(ip);
1939
1940	error = xfs_qm_dqattach(ip, 0);
1941	if (error)
1942		return error;
1943
1944	error = 0;
1945	if (len <= 0)	/* if nothing being freed */
1946		return error;
1947	rt = XFS_IS_REALTIME_INODE(ip);
1948	startoffset_fsb	= XFS_B_TO_FSB(mp, offset);
1949	endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1950
1951	if (attr_flags & XFS_ATTR_NOLOCK)
1952		need_iolock = 0;
1953	if (need_iolock) {
1954		xfs_ilock(ip, XFS_IOLOCK_EXCL);
1955		/* wait for the completion of any pending DIOs */
1956		inode_dio_wait(VFS_I(ip));
1957	}
1958
1959	rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1960	ioffset = offset & ~(rounding - 1);
1961	error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1962					      ioffset, -1);
1963	if (error)
1964		goto out_unlock_iolock;
1965	truncate_pagecache_range(VFS_I(ip), ioffset, -1);
1966
1967	/*
1968	 * Need to zero the stuff we're not freeing, on disk.
1969	 * If it's a realtime file & can't use unwritten extents then we
1970	 * actually need to zero the extent edges.  Otherwise xfs_bunmapi
1971	 * will take care of it for us.
1972	 */
1973	if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1974		nimap = 1;
1975		error = xfs_bmapi_read(ip, startoffset_fsb, 1,
1976					&imap, &nimap, 0);
1977		if (error)
1978			goto out_unlock_iolock;
1979		ASSERT(nimap == 0 || nimap == 1);
1980		if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1981			xfs_daddr_t	block;
1982
1983			ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1984			block = imap.br_startblock;
1985			mod = do_div(block, mp->m_sb.sb_rextsize);
1986			if (mod)
1987				startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1988		}
1989		nimap = 1;
1990		error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
1991					&imap, &nimap, 0);
1992		if (error)
1993			goto out_unlock_iolock;
1994		ASSERT(nimap == 0 || nimap == 1);
1995		if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1996			ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1997			mod++;
1998			if (mod && (mod != mp->m_sb.sb_rextsize))
1999				endoffset_fsb -= mod;
2000		}
2001	}
2002	if ((done = (endoffset_fsb <= startoffset_fsb)))
2003		/*
2004		 * One contiguous piece to clear
2005		 */
2006		error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2007	else {
2008		/*
2009		 * Some full blocks, possibly two pieces to clear
2010		 */
2011		if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2012			error = xfs_zero_remaining_bytes(ip, offset,
2013				XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2014		if (!error &&
2015		    XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2016			error = xfs_zero_remaining_bytes(ip,
2017				XFS_FSB_TO_B(mp, endoffset_fsb),
2018				offset + len - 1);
2019	}
2020
2021	/*
2022	 * free file space until done or until there is an error
2023	 */
2024	resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2025	while (!error && !done) {
2026
2027		/*
2028		 * allocate and setup the transaction. Allow this
2029		 * transaction to dip into the reserve blocks to ensure
2030		 * the freeing of the space succeeds at ENOSPC.
2031		 */
2032		tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2033		tp->t_flags |= XFS_TRANS_RESERVE;
2034		error = xfs_trans_reserve(tp,
2035					  resblks,
2036					  XFS_WRITE_LOG_RES(mp),
2037					  0,
2038					  XFS_TRANS_PERM_LOG_RES,
2039					  XFS_WRITE_LOG_COUNT);
2040
2041		/*
2042		 * check for running out of space
2043		 */
2044		if (error) {
2045			/*
2046			 * Free the transaction structure.
2047			 */
2048			ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2049			xfs_trans_cancel(tp, 0);
2050			break;
2051		}
2052		xfs_ilock(ip, XFS_ILOCK_EXCL);
2053		error = xfs_trans_reserve_quota(tp, mp,
2054				ip->i_udquot, ip->i_gdquot,
2055				resblks, 0, XFS_QMOPT_RES_REGBLKS);
2056		if (error)
2057			goto error1;
2058
2059		xfs_trans_ijoin(tp, ip, 0);
2060
2061		/*
2062		 * issue the bunmapi() call to free the blocks
2063		 */
2064		xfs_bmap_init(&free_list, &firstfsb);
2065		error = xfs_bunmapi(tp, ip, startoffset_fsb,
2066				  endoffset_fsb - startoffset_fsb,
2067				  0, 2, &firstfsb, &free_list, &done);
2068		if (error) {
2069			goto error0;
2070		}
2071
2072		/*
2073		 * complete the transaction
2074		 */
2075		error = xfs_bmap_finish(&tp, &free_list, &committed);
2076		if (error) {
2077			goto error0;
2078		}
2079
2080		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2081		xfs_iunlock(ip, XFS_ILOCK_EXCL);
2082	}
2083
2084 out_unlock_iolock:
2085	if (need_iolock)
2086		xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2087	return error;
2088
2089 error0:
2090	xfs_bmap_cancel(&free_list);
2091 error1:
2092	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2093	xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2094		    XFS_ILOCK_EXCL);
2095	return error;
2096}
2097
2098
2099STATIC int
2100xfs_zero_file_space(
2101	struct xfs_inode	*ip,
2102	xfs_off_t		offset,
2103	xfs_off_t		len,
2104	int			attr_flags)
2105{
2106	struct xfs_mount	*mp = ip->i_mount;
2107	uint			granularity;
2108	xfs_off_t		start_boundary;
2109	xfs_off_t		end_boundary;
2110	int			error;
2111
2112	granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2113
2114	/*
2115	 * Round the range of extents we are going to convert inwards.  If the
2116	 * offset is aligned, then it doesn't get changed so we zero from the
2117	 * start of the block offset points to.
2118	 */
2119	start_boundary = round_up(offset, granularity);
2120	end_boundary = round_down(offset + len, granularity);
2121
2122	ASSERT(start_boundary >= offset);
2123	ASSERT(end_boundary <= offset + len);
2124
2125	if (!(attr_flags & XFS_ATTR_NOLOCK))
2126		xfs_ilock(ip, XFS_IOLOCK_EXCL);
2127
2128	if (start_boundary < end_boundary - 1) {
2129		/* punch out the page cache over the conversion range */
2130		truncate_pagecache_range(VFS_I(ip), start_boundary,
2131					 end_boundary - 1);
2132		/* convert the blocks */
2133		error = xfs_alloc_file_space(ip, start_boundary,
2134					end_boundary - start_boundary - 1,
2135					XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
2136					attr_flags);
2137		if (error)
2138			goto out_unlock;
2139
2140		/* We've handled the interior of the range, now for the edges */
2141		if (start_boundary != offset)
2142			error = xfs_iozero(ip, offset, start_boundary - offset);
2143		if (error)
2144			goto out_unlock;
2145
2146		if (end_boundary != offset + len)
2147			error = xfs_iozero(ip, end_boundary,
2148					   offset + len - end_boundary);
2149
2150	} else {
2151		/*
2152		 * It's either a sub-granularity range or the range spanned lies
2153		 * partially across two adjacent blocks.
2154		 */
2155		error = xfs_iozero(ip, offset, len);
2156	}
2157
2158out_unlock:
2159	if (!(attr_flags & XFS_ATTR_NOLOCK))
2160		xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2161	return error;
2162
2163}
2164
2165/*
2166 * xfs_change_file_space()
2167 *      This routine allocates or frees disk space for the given file.
2168 *      The user specified parameters are checked for alignment and size
2169 *      limitations.
2170 *
2171 * RETURNS:
2172 *       0 on success
2173 *      errno on error
2174 *
2175 */
2176int
2177xfs_change_file_space(
2178	xfs_inode_t	*ip,
2179	int		cmd,
2180	xfs_flock64_t	*bf,
2181	xfs_off_t	offset,
2182	int		attr_flags)
2183{
2184	xfs_mount_t	*mp = ip->i_mount;
2185	int		clrprealloc;
2186	int		error;
2187	xfs_fsize_t	fsize;
2188	int		setprealloc;
2189	xfs_off_t	startoffset;
2190	xfs_trans_t	*tp;
2191	struct iattr	iattr;
2192
2193	if (!S_ISREG(ip->i_d.di_mode))
2194		return XFS_ERROR(EINVAL);
2195
2196	switch (bf->l_whence) {
2197	case 0: /*SEEK_SET*/
2198		break;
2199	case 1: /*SEEK_CUR*/
2200		bf->l_start += offset;
2201		break;
2202	case 2: /*SEEK_END*/
2203		bf->l_start += XFS_ISIZE(ip);
2204		break;
2205	default:
2206		return XFS_ERROR(EINVAL);
2207	}
2208
2209	/*
2210	 * length of <= 0 for resv/unresv/zero is invalid.  length for
2211	 * alloc/free is ignored completely and we have no idea what userspace
2212	 * might have set it to, so set it to zero to allow range
2213	 * checks to pass.
2214	 */
2215	switch (cmd) {
2216	case XFS_IOC_ZERO_RANGE:
2217	case XFS_IOC_RESVSP:
2218	case XFS_IOC_RESVSP64:
2219	case XFS_IOC_UNRESVSP:
2220	case XFS_IOC_UNRESVSP64:
2221		if (bf->l_len <= 0)
2222			return XFS_ERROR(EINVAL);
2223		break;
2224	default:
2225		bf->l_len = 0;
2226		break;
2227	}
2228
2229	if (bf->l_start < 0 ||
2230	    bf->l_start > mp->m_super->s_maxbytes ||
2231	    bf->l_start + bf->l_len < 0 ||
2232	    bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
2233		return XFS_ERROR(EINVAL);
2234
2235	bf->l_whence = 0;
2236
2237	startoffset = bf->l_start;
2238	fsize = XFS_ISIZE(ip);
2239
2240	setprealloc = clrprealloc = 0;
2241	switch (cmd) {
2242	case XFS_IOC_ZERO_RANGE:
2243		error = xfs_zero_file_space(ip, startoffset, bf->l_len,
2244						attr_flags);
2245		if (error)
2246			return error;
2247		setprealloc = 1;
2248		break;
2249
2250	case XFS_IOC_RESVSP:
2251	case XFS_IOC_RESVSP64:
2252		error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
2253						XFS_BMAPI_PREALLOC, attr_flags);
2254		if (error)
2255			return error;
2256		setprealloc = 1;
2257		break;
2258
2259	case XFS_IOC_UNRESVSP:
2260	case XFS_IOC_UNRESVSP64:
2261		if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2262								attr_flags)))
2263			return error;
2264		break;
2265
2266	case XFS_IOC_ALLOCSP:
2267	case XFS_IOC_ALLOCSP64:
2268	case XFS_IOC_FREESP:
2269	case XFS_IOC_FREESP64:
2270		/*
2271		 * These operations actually do IO when extending the file, but
2272		 * the allocation is done seperately to the zeroing that is
2273		 * done. This set of operations need to be serialised against
2274		 * other IO operations, such as truncate and buffered IO. We
2275		 * need to take the IOLOCK here to serialise the allocation and
2276		 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
2277		 * truncate, direct IO) from racing against the transient
2278		 * allocated but not written state we can have here.
2279		 */
2280		xfs_ilock(ip, XFS_IOLOCK_EXCL);
2281		if (startoffset > fsize) {
2282			error = xfs_alloc_file_space(ip, fsize,
2283					startoffset - fsize, 0,
2284					attr_flags | XFS_ATTR_NOLOCK);
2285			if (error) {
2286				xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2287				break;
2288			}
2289		}
2290
2291		iattr.ia_valid = ATTR_SIZE;
2292		iattr.ia_size = startoffset;
2293
2294		error = xfs_setattr_size(ip, &iattr,
2295					 attr_flags | XFS_ATTR_NOLOCK);
2296		xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2297
2298		if (error)
2299			return error;
2300
2301		clrprealloc = 1;
2302		break;
2303
2304	default:
2305		ASSERT(0);
2306		return XFS_ERROR(EINVAL);
2307	}
2308
2309	/*
2310	 * update the inode timestamp, mode, and prealloc flag bits
2311	 */
2312	tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2313
2314	if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2315				      0, 0, 0))) {
2316		/* ASSERT(0); */
2317		xfs_trans_cancel(tp, 0);
2318		return error;
2319	}
2320
2321	xfs_ilock(ip, XFS_ILOCK_EXCL);
2322	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2323
2324	if ((attr_flags & XFS_ATTR_DMI) == 0) {
2325		ip->i_d.di_mode &= ~S_ISUID;
2326
2327		/*
2328		 * Note that we don't have to worry about mandatory
2329		 * file locking being disabled here because we only
2330		 * clear the S_ISGID bit if the Group execute bit is
2331		 * on, but if it was on then mandatory locking wouldn't
2332		 * have been enabled.
2333		 */
2334		if (ip->i_d.di_mode & S_IXGRP)
2335			ip->i_d.di_mode &= ~S_ISGID;
2336
2337		xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2338	}
2339	if (setprealloc)
2340		ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2341	else if (clrprealloc)
2342		ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2343
2344	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2345	if (attr_flags & XFS_ATTR_SYNC)
2346		xfs_trans_set_sync(tp);
2347	return xfs_trans_commit(tp, 0);
2348}