| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 |  * Copyright (c) 2000-2006 Silicon Graphics, Inc. | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 |  * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  */ | 
| Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" | 
 | 26 | #include "xfs_sb.h" | 
 | 27 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dir2.h" | 
 | 29 | #include "xfs_dmapi.h" | 
 | 30 | #include "xfs_mount.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_da_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_bmap_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_alloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_ialloc_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_dir2_sf.h" | 
 | 36 | #include "xfs_attr_sf.h" | 
 | 37 | #include "xfs_dinode.h" | 
 | 38 | #include "xfs_inode.h" | 
 | 39 | #include "xfs_inode_item.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" | 
 | 41 | #include "xfs_btree.h" | 
 | 42 | #include "xfs_ialloc.h" | 
 | 43 | #include "xfs_alloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_bmap.h" | 
| Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 45 | #include "xfs_acl.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_attr.h" | 
 | 47 | #include "xfs_rw.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_error.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include "xfs_quota.h" | 
 | 50 | #include "xfs_utils.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 51 | #include "xfs_rtalloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include "xfs_trans_space.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include "xfs_log_priv.h" | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 54 | #include "xfs_filestream.h" | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 55 | #include "xfs_vnodeops.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 57 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | xfs_setattr( | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 59 | 	struct xfs_inode	*ip, | 
 | 60 | 	struct iattr		*iattr, | 
| Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 61 | 	int			flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 63 | 	xfs_mount_t		*mp = ip->i_mount; | 
| David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 64 | 	struct inode		*inode = VFS_I(ip); | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 65 | 	int			mask = iattr->ia_valid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | 	xfs_trans_t		*tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	int			code; | 
 | 68 | 	uint			lock_flags; | 
 | 69 | 	uint			commit_flags=0; | 
 | 70 | 	uid_t			uid=0, iuid=0; | 
 | 71 | 	gid_t			gid=0, igid=0; | 
 | 72 | 	int			timeflags = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | 	struct xfs_dquot	*udqp, *gdqp, *olddquot1, *olddquot2; | 
| Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 74 | 	int			need_iolock = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 76 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |  | 
| Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 78 | 	if (mp->m_flags & XFS_MOUNT_RDONLY) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | 		return XFS_ERROR(EROFS); | 
 | 80 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 82 | 		return XFS_ERROR(EIO); | 
 | 83 |  | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 84 | 	code = -inode_change_ok(inode, iattr); | 
 | 85 | 	if (code) | 
 | 86 | 		return code; | 
 | 87 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 	olddquot1 = olddquot2 = NULL; | 
 | 89 | 	udqp = gdqp = NULL; | 
 | 90 |  | 
 | 91 | 	/* | 
 | 92 | 	 * If disk quotas is on, we make sure that the dquots do exist on disk, | 
 | 93 | 	 * before we start any other transactions. Trying to do this later | 
 | 94 | 	 * is messy. We don't care to take a readlock to look at the ids | 
 | 95 | 	 * in inode here, because we can't hold it across the trans_reserve. | 
 | 96 | 	 * If the IDs do change before we take the ilock, we're covered | 
 | 97 | 	 * because the i_*dquot fields will get updated anyway. | 
 | 98 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 99 | 	if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | 		uint	qflags = 0; | 
 | 101 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 102 | 		if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { | 
 | 103 | 			uid = iattr->ia_uid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | 			qflags |= XFS_QMOPT_UQUOTA; | 
 | 105 | 		} else { | 
 | 106 | 			uid = ip->i_d.di_uid; | 
 | 107 | 		} | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 108 | 		if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { | 
 | 109 | 			gid = iattr->ia_gid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | 			qflags |= XFS_QMOPT_GQUOTA; | 
 | 111 | 		}  else { | 
 | 112 | 			gid = ip->i_d.di_gid; | 
 | 113 | 		} | 
| Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 114 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | 		/* | 
 | 116 | 		 * We take a reference when we initialize udqp and gdqp, | 
 | 117 | 		 * so it is important that we never blindly double trip on | 
 | 118 | 		 * the same variable. See xfs_create() for an example. | 
 | 119 | 		 */ | 
 | 120 | 		ASSERT(udqp == NULL); | 
 | 121 | 		ASSERT(gdqp == NULL); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 122 | 		code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid, | 
| Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 123 | 					 qflags, &udqp, &gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 		if (code) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 125 | 			return code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 	} | 
 | 127 |  | 
 | 128 | 	/* | 
 | 129 | 	 * For the other attributes, we acquire the inode lock and | 
 | 130 | 	 * first do an error checking pass. | 
 | 131 | 	 */ | 
 | 132 | 	tp = NULL; | 
 | 133 | 	lock_flags = XFS_ILOCK_EXCL; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 134 | 	if (flags & XFS_ATTR_NOLOCK) | 
| Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 135 | 		need_iolock = 0; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 136 | 	if (!(mask & ATTR_SIZE)) { | 
 | 137 | 		if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 		    (mp->m_flags & XFS_MOUNT_WSYNC)) { | 
 | 139 | 			tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); | 
 | 140 | 			commit_flags = 0; | 
 | 141 | 			if ((code = xfs_trans_reserve(tp, 0, | 
 | 142 | 						     XFS_ICHANGE_LOG_RES(mp), 0, | 
 | 143 | 						     0, 0))) { | 
 | 144 | 				lock_flags = 0; | 
 | 145 | 				goto error_return; | 
 | 146 | 			} | 
 | 147 | 		} | 
 | 148 | 	} else { | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 149 | 		if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) && | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 150 | 		    !(flags & XFS_ATTR_DMI)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 			int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR; | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 152 | 			code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip, | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 153 | 				iattr->ia_size, 0, dmflags, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | 			if (code) { | 
 | 155 | 				lock_flags = 0; | 
 | 156 | 				goto error_return; | 
 | 157 | 			} | 
 | 158 | 		} | 
 | 159 | 		if (need_iolock) | 
 | 160 | 			lock_flags |= XFS_IOLOCK_EXCL; | 
 | 161 | 	} | 
 | 162 |  | 
 | 163 | 	xfs_ilock(ip, lock_flags); | 
 | 164 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 	/* | 
 | 166 | 	 * Change file ownership.  Must be the owner or privileged. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 168 | 	if (mask & (ATTR_UID|ATTR_GID)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | 		/* | 
 | 170 | 		 * These IDs could have changed since we last looked at them. | 
 | 171 | 		 * But, we're assured that if the ownership did change | 
 | 172 | 		 * while we didn't have the inode locked, inode's dquot(s) | 
 | 173 | 		 * would have changed also. | 
 | 174 | 		 */ | 
 | 175 | 		iuid = ip->i_d.di_uid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 		igid = ip->i_d.di_gid; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 177 | 		gid = (mask & ATTR_GID) ? iattr->ia_gid : igid; | 
 | 178 | 		uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 |  | 
 | 180 | 		/* | 
| Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 181 | 		 * Do a quota reservation only if uid/gid is actually | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | 		 * going to change. | 
 | 183 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 184 | 		if (XFS_IS_QUOTA_RUNNING(mp) && | 
 | 185 | 		    ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) || | 
 | 186 | 		     (XFS_IS_GQUOTA_ON(mp) && igid != gid))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | 			ASSERT(tp); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 188 | 			code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | 						capable(CAP_FOWNER) ? | 
 | 190 | 						XFS_QMOPT_FORCE_RES : 0); | 
 | 191 | 			if (code)	/* out of quota */ | 
 | 192 | 				goto error_return; | 
 | 193 | 		} | 
 | 194 | 	} | 
 | 195 |  | 
 | 196 | 	/* | 
 | 197 | 	 * Truncate file.  Must have write permission and not be a directory. | 
 | 198 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 199 | 	if (mask & ATTR_SIZE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | 		/* Short circuit the truncate case for zero length files */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 201 | 		if (iattr->ia_size == 0 && | 
 | 202 | 		    ip->i_size == 0 && ip->i_d.di_nextents == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 			xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 204 | 			lock_flags &= ~XFS_ILOCK_EXCL; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 205 | 			if (mask & ATTR_CTIME) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 				xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
 | 207 | 			code = 0; | 
 | 208 | 			goto error_return; | 
 | 209 | 		} | 
 | 210 |  | 
| Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 211 | 		if (S_ISDIR(ip->i_d.di_mode)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | 			code = XFS_ERROR(EISDIR); | 
 | 213 | 			goto error_return; | 
| Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 214 | 		} else if (!S_ISREG(ip->i_d.di_mode)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | 			code = XFS_ERROR(EINVAL); | 
 | 216 | 			goto error_return; | 
 | 217 | 		} | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 218 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | 		/* | 
 | 220 | 		 * Make sure that the dquots are attached to the inode. | 
 | 221 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 222 | 		code = xfs_qm_dqattach_locked(ip, 0); | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 223 | 		if (code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | 			goto error_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 |  | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 226 | 		/* | 
 | 227 | 		 * Now we can make the changes.  Before we join the inode | 
 | 228 | 		 * to the transaction, if ATTR_SIZE is set then take care of | 
 | 229 | 		 * the part of the truncation that must be done without the | 
 | 230 | 		 * inode lock.  This needs to be done before joining the inode | 
 | 231 | 		 * to the transaction, because the inode cannot be unlocked | 
 | 232 | 		 * once it is a part of the transaction. | 
 | 233 | 		 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 234 | 		if (iattr->ia_size > ip->i_size) { | 
| Christoph Hellwig | 61436fe | 2008-05-20 11:30:46 +1000 | [diff] [blame] | 235 | 			/* | 
 | 236 | 			 * Do the first part of growing a file: zero any data | 
 | 237 | 			 * in the last block that is beyond the old EOF.  We | 
 | 238 | 			 * need to do this before the inode is joined to the | 
 | 239 | 			 * transaction to modify the i_size. | 
 | 240 | 			 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 241 | 			code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size); | 
| Eric Sandeen | 374e2ac | 2005-11-02 15:07:34 +1100 | [diff] [blame] | 242 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 		xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
| David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 244 |  | 
 | 245 | 		/* | 
 | 246 | 		 * We are going to log the inode size change in this | 
 | 247 | 		 * transaction so any previous writes that are beyond the on | 
 | 248 | 		 * disk EOF and the new EOF that have not been written out need | 
 | 249 | 		 * to be written here. If we do not write the data out, we | 
 | 250 | 		 * expose ourselves to the null files problem. | 
 | 251 | 		 * | 
 | 252 | 		 * Only flush from the on disk size to the smaller of the in | 
 | 253 | 		 * memory file size or the new size as that's the range we | 
 | 254 | 		 * really care about here and prevents waiting for other data | 
 | 255 | 		 * not within the range we care about here. | 
 | 256 | 		 */ | 
 | 257 | 		if (!code && | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 258 | 		    ip->i_size != ip->i_d.di_size && | 
 | 259 | 		    iattr->ia_size > ip->i_d.di_size) { | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 260 | 			code = xfs_flush_pages(ip, | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 261 | 					ip->i_d.di_size, iattr->ia_size, | 
| David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 262 | 					XFS_B_ASYNC, FI_NONE); | 
 | 263 | 		} | 
 | 264 |  | 
 | 265 | 		/* wait for all I/O to complete */ | 
| Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 266 | 		xfs_ioend_wait(ip); | 
| David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 267 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 		if (!code) | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 269 | 			code = xfs_itruncate_data(ip, iattr->ia_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | 		if (code) { | 
 | 271 | 			ASSERT(tp == NULL); | 
 | 272 | 			lock_flags &= ~XFS_ILOCK_EXCL; | 
 | 273 | 			ASSERT(lock_flags == XFS_IOLOCK_EXCL); | 
 | 274 | 			goto error_return; | 
 | 275 | 		} | 
 | 276 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE); | 
 | 277 | 		if ((code = xfs_trans_reserve(tp, 0, | 
 | 278 | 					     XFS_ITRUNCATE_LOG_RES(mp), 0, | 
 | 279 | 					     XFS_TRANS_PERM_LOG_RES, | 
 | 280 | 					     XFS_ITRUNCATE_LOG_COUNT))) { | 
 | 281 | 			xfs_trans_cancel(tp, 0); | 
 | 282 | 			if (need_iolock) | 
 | 283 | 				xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
 | 284 | 			return code; | 
 | 285 | 		} | 
 | 286 | 		commit_flags = XFS_TRANS_RELEASE_LOG_RES; | 
 | 287 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | 		xfs_trans_ijoin(tp, ip, lock_flags); | 
 | 290 | 		xfs_trans_ihold(tp, ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
| David Chinner | 44d814c | 2008-03-06 13:45:29 +1100 | [diff] [blame] | 292 | 		/* | 
 | 293 | 		 * Only change the c/mtime if we are changing the size | 
 | 294 | 		 * or we are explicitly asked to change it. This handles | 
 | 295 | 		 * the semantic difference between truncate() and ftruncate() | 
 | 296 | 		 * as implemented in the VFS. | 
 | 297 | 		 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 298 | 		if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME)) | 
| David Chinner | 44d814c | 2008-03-06 13:45:29 +1100 | [diff] [blame] | 299 | 			timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | 
 | 300 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 301 | 		if (iattr->ia_size > ip->i_size) { | 
 | 302 | 			ip->i_d.di_size = iattr->ia_size; | 
 | 303 | 			ip->i_size = iattr->ia_size; | 
 | 304 | 			if (!(flags & XFS_ATTR_DMI)) | 
| Christoph Hellwig | 61436fe | 2008-05-20 11:30:46 +1000 | [diff] [blame] | 305 | 				xfs_ichgtime(ip, XFS_ICHGTIME_CHG); | 
 | 306 | 			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 307 | 		} else if (iattr->ia_size <= ip->i_size || | 
 | 308 | 			   (iattr->ia_size == 0 && ip->i_d.di_nextents)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | 			/* | 
 | 310 | 			 * signal a sync transaction unless | 
 | 311 | 			 * we're truncating an already unlinked | 
 | 312 | 			 * file on a wsync filesystem | 
 | 313 | 			 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 314 | 			code = xfs_itruncate_finish(&tp, ip, iattr->ia_size, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | 					    XFS_DATA_FORK, | 
 | 316 | 					    ((ip->i_d.di_nlink != 0 || | 
 | 317 | 					      !(mp->m_flags & XFS_MOUNT_WSYNC)) | 
 | 318 | 					     ? 1 : 0)); | 
| Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 319 | 			if (code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | 				goto abort_return; | 
| Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 321 | 			/* | 
 | 322 | 			 * Truncated "down", so we're removing references | 
 | 323 | 			 * to old data here - if we now delay flushing for | 
 | 324 | 			 * a long time, we expose ourselves unduly to the | 
 | 325 | 			 * notorious NULL files problem.  So, we mark this | 
 | 326 | 			 * vnode and flush it when the file is closed, and | 
 | 327 | 			 * do not wait the usual (long) time for writeout. | 
 | 328 | 			 */ | 
| Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 329 | 			xfs_iflags_set(ip, XFS_ITRUNCATED); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 		} | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 331 | 	} else if (tp) { | 
 | 332 | 		xfs_trans_ijoin(tp, ip, lock_flags); | 
 | 333 | 		xfs_trans_ihold(tp, ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | 	} | 
 | 335 |  | 
 | 336 | 	/* | 
 | 337 | 	 * Change file ownership.  Must be the owner or privileged. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 339 | 	if (mask & (ATTR_UID|ATTR_GID)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | 		/* | 
 | 341 | 		 * CAP_FSETID overrides the following restrictions: | 
 | 342 | 		 * | 
 | 343 | 		 * The set-user-ID and set-group-ID bits of a file will be | 
 | 344 | 		 * cleared upon successful return from chown() | 
 | 345 | 		 */ | 
 | 346 | 		if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && | 
 | 347 | 		    !capable(CAP_FSETID)) { | 
 | 348 | 			ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); | 
 | 349 | 		} | 
 | 350 |  | 
 | 351 | 		/* | 
 | 352 | 		 * Change the ownerships and register quota modifications | 
 | 353 | 		 * in the transaction. | 
 | 354 | 		 */ | 
 | 355 | 		if (iuid != uid) { | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 356 | 			if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) { | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 357 | 				ASSERT(mask & ATTR_UID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | 				ASSERT(udqp); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 359 | 				olddquot1 = xfs_qm_vop_chown(tp, ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | 							&ip->i_udquot, udqp); | 
 | 361 | 			} | 
 | 362 | 			ip->i_d.di_uid = uid; | 
| Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 363 | 			inode->i_uid = uid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | 		} | 
 | 365 | 		if (igid != gid) { | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 366 | 			if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) { | 
| Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 367 | 				ASSERT(!XFS_IS_PQUOTA_ON(mp)); | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 368 | 				ASSERT(mask & ATTR_GID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | 				ASSERT(gdqp); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 370 | 				olddquot2 = xfs_qm_vop_chown(tp, ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 							&ip->i_gdquot, gdqp); | 
 | 372 | 			} | 
 | 373 | 			ip->i_d.di_gid = gid; | 
| Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 374 | 			inode->i_gid = gid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 |  | 
 | 377 | 		xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); | 
 | 378 | 		timeflags |= XFS_ICHGTIME_CHG; | 
 | 379 | 	} | 
 | 380 |  | 
| Christoph Hellwig | c4cd747 | 2008-12-09 04:47:34 -0500 | [diff] [blame] | 381 | 	/* | 
 | 382 | 	 * Change file access modes. | 
 | 383 | 	 */ | 
 | 384 | 	if (mask & ATTR_MODE) { | 
 | 385 | 		umode_t mode = iattr->ia_mode; | 
 | 386 |  | 
 | 387 | 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | 
 | 388 | 			mode &= ~S_ISGID; | 
 | 389 |  | 
 | 390 | 		ip->i_d.di_mode &= S_IFMT; | 
 | 391 | 		ip->i_d.di_mode |= mode & ~S_IFMT; | 
 | 392 |  | 
 | 393 | 		inode->i_mode &= S_IFMT; | 
 | 394 | 		inode->i_mode |= mode & ~S_IFMT; | 
 | 395 |  | 
 | 396 | 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 397 | 		timeflags |= XFS_ICHGTIME_CHG; | 
 | 398 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 |  | 
 | 400 | 	/* | 
 | 401 | 	 * Change file access or modified times. | 
 | 402 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 403 | 	if (mask & (ATTR_ATIME|ATTR_MTIME)) { | 
 | 404 | 		if (mask & ATTR_ATIME) { | 
| Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 405 | 			inode->i_atime = iattr->ia_atime; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 406 | 			ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec; | 
 | 407 | 			ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | 			ip->i_update_core = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | 		} | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 410 | 		if (mask & ATTR_MTIME) { | 
| Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 411 | 			inode->i_mtime = iattr->ia_mtime; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 412 | 			ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec; | 
 | 413 | 			ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | 			timeflags &= ~XFS_ICHGTIME_MOD; | 
 | 415 | 			timeflags |= XFS_ICHGTIME_CHG; | 
 | 416 | 		} | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 417 | 		if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | 			xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); | 
 | 419 | 	} | 
 | 420 |  | 
 | 421 | 	/* | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 422 | 	 * Change file inode change time only if ATTR_CTIME set | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | 	 * AND we have been called by a DMI function. | 
 | 424 | 	 */ | 
 | 425 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 426 | 	if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) { | 
| Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 427 | 		inode->i_ctime = iattr->ia_ctime; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 428 | 		ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec; | 
 | 429 | 		ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | 		ip->i_update_core = 1; | 
 | 431 | 		timeflags &= ~XFS_ICHGTIME_CHG; | 
 | 432 | 	} | 
 | 433 |  | 
 | 434 | 	/* | 
 | 435 | 	 * Send out timestamp changes that need to be set to the | 
 | 436 | 	 * current time.  Not done when called by a DMI function. | 
 | 437 | 	 */ | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 438 | 	if (timeflags && !(flags & XFS_ATTR_DMI)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | 		xfs_ichgtime(ip, timeflags); | 
 | 440 |  | 
 | 441 | 	XFS_STATS_INC(xs_ig_attrchg); | 
 | 442 |  | 
 | 443 | 	/* | 
 | 444 | 	 * If this is a synchronous mount, make sure that the | 
 | 445 | 	 * transaction goes to disk before returning to the user. | 
 | 446 | 	 * This is slightly sub-optimal in that truncates require | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 447 | 	 * two sync transactions instead of one for wsync filesystems. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | 	 * One for the truncate and one for the timestamps since we | 
 | 449 | 	 * don't want to change the timestamps unless we're sure the | 
 | 450 | 	 * truncate worked.  Truncates are less than 1% of the laddis | 
 | 451 | 	 * mix so this probably isn't worth the trouble to optimize. | 
 | 452 | 	 */ | 
 | 453 | 	code = 0; | 
 | 454 | 	if (tp) { | 
 | 455 | 		if (mp->m_flags & XFS_MOUNT_WSYNC) | 
 | 456 | 			xfs_trans_set_sync(tp); | 
 | 457 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 458 | 		code = xfs_trans_commit(tp, commit_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | 	} | 
 | 460 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | 	xfs_iunlock(ip, lock_flags); | 
 | 462 |  | 
 | 463 | 	/* | 
 | 464 | 	 * Release any dquot(s) the inode had kept before chown. | 
 | 465 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 466 | 	xfs_qm_dqrele(olddquot1); | 
 | 467 | 	xfs_qm_dqrele(olddquot2); | 
 | 468 | 	xfs_qm_dqrele(udqp); | 
 | 469 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 |  | 
| Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 471 | 	if (code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | 		return code; | 
| Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 473 |  | 
 | 474 | 	/* | 
 | 475 | 	 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode | 
 | 476 | 	 * 	     update.  We could avoid this with linked transactions | 
 | 477 | 	 * 	     and passing down the transaction pointer all the way | 
 | 478 | 	 *	     to attr_set.  No previous user of the generic | 
 | 479 | 	 * 	     Posix ACL code seems to care about this issue either. | 
 | 480 | 	 */ | 
 | 481 | 	if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) { | 
 | 482 | 		code = -xfs_acl_chmod(inode); | 
 | 483 | 		if (code) | 
 | 484 | 			return XFS_ERROR(code); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | 	} | 
 | 486 |  | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 487 | 	if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) && | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 488 | 	    !(flags & XFS_ATTR_DMI)) { | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 489 | 		(void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 					NULL, DM_RIGHT_NULL, NULL, NULL, | 
 | 491 | 					0, 0, AT_DELAY_FLAG(flags)); | 
 | 492 | 	} | 
 | 493 | 	return 0; | 
 | 494 |  | 
 | 495 |  abort_return: | 
 | 496 | 	commit_flags |= XFS_TRANS_ABORT; | 
 | 497 | 	/* FALLTHROUGH */ | 
 | 498 |  error_return: | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 499 | 	xfs_qm_dqrele(udqp); | 
 | 500 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | 	if (tp) { | 
 | 502 | 		xfs_trans_cancel(tp, commit_flags); | 
 | 503 | 	} | 
 | 504 | 	if (lock_flags != 0) { | 
 | 505 | 		xfs_iunlock(ip, lock_flags); | 
 | 506 | 	} | 
 | 507 | 	return code; | 
 | 508 | } | 
 | 509 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | /* | 
| Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 511 |  * The maximum pathlen is 1024 bytes. Since the minimum file system | 
 | 512 |  * blocksize is 512 bytes, we can get a max of 2 extents back from | 
 | 513 |  * bmapi. | 
 | 514 |  */ | 
 | 515 | #define SYMLINK_MAPS 2 | 
 | 516 |  | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 517 | STATIC int | 
 | 518 | xfs_readlink_bmap( | 
 | 519 | 	xfs_inode_t	*ip, | 
 | 520 | 	char		*link) | 
 | 521 | { | 
 | 522 | 	xfs_mount_t	*mp = ip->i_mount; | 
 | 523 | 	int		pathlen = ip->i_d.di_size; | 
 | 524 | 	int             nmaps = SYMLINK_MAPS; | 
 | 525 | 	xfs_bmbt_irec_t mval[SYMLINK_MAPS]; | 
 | 526 | 	xfs_daddr_t	d; | 
 | 527 | 	int		byte_cnt; | 
 | 528 | 	int		n; | 
 | 529 | 	xfs_buf_t	*bp; | 
 | 530 | 	int		error = 0; | 
 | 531 |  | 
 | 532 | 	error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0, | 
 | 533 | 			mval, &nmaps, NULL, NULL); | 
 | 534 | 	if (error) | 
 | 535 | 		goto out; | 
 | 536 |  | 
 | 537 | 	for (n = 0; n < nmaps; n++) { | 
 | 538 | 		d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); | 
 | 539 | 		byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); | 
 | 540 |  | 
 | 541 | 		bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0); | 
 | 542 | 		error = XFS_BUF_GETERROR(bp); | 
 | 543 | 		if (error) { | 
 | 544 | 			xfs_ioerror_alert("xfs_readlink", | 
 | 545 | 				  ip->i_mount, bp, XFS_BUF_ADDR(bp)); | 
 | 546 | 			xfs_buf_relse(bp); | 
 | 547 | 			goto out; | 
 | 548 | 		} | 
 | 549 | 		if (pathlen < byte_cnt) | 
 | 550 | 			byte_cnt = pathlen; | 
 | 551 | 		pathlen -= byte_cnt; | 
 | 552 |  | 
 | 553 | 		memcpy(link, XFS_BUF_PTR(bp), byte_cnt); | 
 | 554 | 		xfs_buf_relse(bp); | 
 | 555 | 	} | 
 | 556 |  | 
 | 557 | 	link[ip->i_d.di_size] = '\0'; | 
 | 558 | 	error = 0; | 
 | 559 |  | 
 | 560 |  out: | 
 | 561 | 	return error; | 
 | 562 | } | 
 | 563 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 564 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | xfs_readlink( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 566 | 	xfs_inode_t     *ip, | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 567 | 	char		*link) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 569 | 	xfs_mount_t	*mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | 	int		pathlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | 	int		error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 573 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 |  | 
 | 575 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 576 | 		return XFS_ERROR(EIO); | 
 | 577 |  | 
 | 578 | 	xfs_ilock(ip, XFS_ILOCK_SHARED); | 
 | 579 |  | 
 | 580 | 	ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK); | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 581 | 	ASSERT(ip->i_d.di_size <= MAXPATHLEN); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 |  | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 583 | 	pathlen = ip->i_d.di_size; | 
 | 584 | 	if (!pathlen) | 
 | 585 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 |  | 
 | 587 | 	if (ip->i_df.if_flags & XFS_IFINLINE) { | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 588 | 		memcpy(link, ip->i_df.if_u1.if_data, pathlen); | 
 | 589 | 		link[pathlen] = '\0'; | 
 | 590 | 	} else { | 
 | 591 | 		error = xfs_readlink_bmap(ip, link); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | 	} | 
 | 593 |  | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 594 |  out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | 	xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | 	return error; | 
 | 597 | } | 
 | 598 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | /* | 
 | 600 |  * xfs_fsync | 
 | 601 |  * | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 602 |  * This is called to sync the inode and its data out to disk.  We need to hold | 
 | 603 |  * the I/O lock while flushing the data, and the inode lock while flushing the | 
 | 604 |  * inode.  The inode lock CANNOT be held while flushing the data, so acquire | 
 | 605 |  * after we're done with that. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 |  */ | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 607 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | xfs_fsync( | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 609 | 	xfs_inode_t	*ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | 	xfs_trans_t	*tp; | 
 | 612 | 	int		error; | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 613 | 	int		log_flushed = 0, changed = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 615 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
 | 618 | 		return XFS_ERROR(EIO); | 
 | 619 |  | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 620 | 	/* capture size updates in I/O completion before writing the inode. */ | 
| Dave Chinner | 2e65609 | 2008-11-28 14:23:33 +1100 | [diff] [blame] | 621 | 	error = xfs_wait_on_pages(ip, 0, -1); | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 622 | 	if (error) | 
 | 623 | 		return XFS_ERROR(error); | 
| Lachlan McIlroy | 776a75fa | 2007-09-14 15:22:50 +1000 | [diff] [blame] | 624 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | 	/* | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 626 | 	 * We always need to make sure that the required inode state is safe on | 
 | 627 | 	 * disk.  The vnode might be clean but we still might need to force the | 
 | 628 | 	 * log because of committed transactions that haven't hit the disk yet. | 
 | 629 | 	 * Likewise, there could be unflushed non-transactional changes to the | 
 | 630 | 	 * inode core that have to go to disk and this requires us to issue | 
 | 631 | 	 * a synchronous transaction to capture these changes correctly. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | 	 * | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 633 | 	 * This code relies on the assumption that if the update_* fields | 
 | 634 | 	 * of the inode are clear and the inode is unpinned then it is clean | 
 | 635 | 	 * and no action is required. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | 	 */ | 
 | 637 | 	xfs_ilock(ip, XFS_ILOCK_SHARED); | 
 | 638 |  | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 639 | 	if (!(ip->i_update_size || ip->i_update_core)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | 		/* | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 641 | 		 * Timestamps/size haven't changed since last inode flush or | 
 | 642 | 		 * inode transaction commit.  That means either nothing got | 
 | 643 | 		 * written or a transaction committed which caught the updates. | 
 | 644 | 		 * If the latter happened and the transaction hasn't hit the | 
 | 645 | 		 * disk yet, the inode will be still be pinned.  If it is, | 
 | 646 | 		 * force the log. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 		 */ | 
 | 648 |  | 
 | 649 | 		xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
 | 650 |  | 
 | 651 | 		if (xfs_ipincount(ip)) { | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 652 | 			error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0, | 
 | 653 | 				      XFS_LOG_FORCE | XFS_LOG_SYNC, | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 654 | 				      &log_flushed); | 
 | 655 | 		} else { | 
 | 656 | 			/* | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 657 | 			 * If the inode is not pinned and nothing has changed | 
 | 658 | 			 * we don't need to flush the cache. | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 659 | 			 */ | 
 | 660 | 			changed = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | 	} else	{ | 
 | 663 | 		/* | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 664 | 		 * Kick off a transaction to log the inode core to get the | 
 | 665 | 		 * updates.  The sync transaction will also force the log. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | 		 */ | 
 | 667 | 		xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
 | 668 | 		tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS); | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 669 | 		error = xfs_trans_reserve(tp, 0, | 
 | 670 | 				XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0); | 
 | 671 | 		if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | 			xfs_trans_cancel(tp, 0); | 
 | 673 | 			return error; | 
 | 674 | 		} | 
 | 675 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 676 |  | 
 | 677 | 		/* | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 678 | 		 * Note - it's possible that we might have pushed ourselves out | 
 | 679 | 		 * of the way during trans_reserve which would flush the inode. | 
 | 680 | 		 * But there's no guarantee that the inode buffer has actually | 
 | 681 | 		 * gone out yet (it's delwri).	Plus the buffer could be pinned | 
 | 682 | 		 * anyway if it's part of an inode in another recent | 
 | 683 | 		 * transaction.	 So we play it safe and fire off the | 
 | 684 | 		 * transaction anyway. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | 		 */ | 
 | 686 | 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
 | 687 | 		xfs_trans_ihold(tp, ip); | 
 | 688 | 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
| David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 689 | 		xfs_trans_set_sync(tp); | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 690 | 		error = _xfs_trans_commit(tp, 0, &log_flushed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 |  | 
 | 692 | 		xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 693 | 	} | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 694 |  | 
 | 695 | 	if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) { | 
 | 696 | 		/* | 
 | 697 | 		 * If the log write didn't issue an ordered tag we need | 
 | 698 | 		 * to flush the disk cache for the data device now. | 
 | 699 | 		 */ | 
 | 700 | 		if (!log_flushed) | 
 | 701 | 			xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp); | 
 | 702 |  | 
 | 703 | 		/* | 
 | 704 | 		 * If this inode is on the RT dev we need to flush that | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 705 | 		 * cache as well. | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 706 | 		 */ | 
| Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 707 | 		if (XFS_IS_REALTIME_INODE(ip)) | 
| Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 708 | 			xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp); | 
 | 709 | 	} | 
 | 710 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | 	return error; | 
 | 712 | } | 
 | 713 |  | 
 | 714 | /* | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 715 |  * This is called by xfs_inactive to free any blocks beyond eof | 
 | 716 |  * when the link count isn't zero and by xfs_dm_punch_hole() when | 
 | 717 |  * punching a hole to EOF. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 |  */ | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 719 | int | 
 | 720 | xfs_free_eofblocks( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | 	xfs_mount_t	*mp, | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 722 | 	xfs_inode_t	*ip, | 
 | 723 | 	int		flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { | 
 | 725 | 	xfs_trans_t	*tp; | 
 | 726 | 	int		error; | 
 | 727 | 	xfs_fileoff_t	end_fsb; | 
 | 728 | 	xfs_fileoff_t	last_fsb; | 
 | 729 | 	xfs_filblks_t	map_len; | 
 | 730 | 	int		nimaps; | 
 | 731 | 	xfs_bmbt_irec_t	imap; | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 732 | 	int		use_iolock = (flags & XFS_FREE_EOF_LOCK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 |  | 
 | 734 | 	/* | 
 | 735 | 	 * Figure out if there are any blocks beyond the end | 
 | 736 | 	 * of the file.  If not, then there is nothing to do. | 
 | 737 | 	 */ | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 738 | 	end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | 	last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); | 
 | 740 | 	map_len = last_fsb - end_fsb; | 
 | 741 | 	if (map_len <= 0) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 742 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 |  | 
 | 744 | 	nimaps = 1; | 
 | 745 | 	xfs_ilock(ip, XFS_ILOCK_SHARED); | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 746 | 	error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 747 | 			  NULL, 0, &imap, &nimaps, NULL, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | 	xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
 | 749 |  | 
 | 750 | 	if (!error && (nimaps != 0) && | 
| Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 751 | 	    (imap.br_startblock != HOLESTARTBLOCK || | 
 | 752 | 	     ip->i_delayed_blks)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | 		/* | 
 | 754 | 		 * Attach the dquots to the inode up front. | 
 | 755 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 756 | 		error = xfs_qm_dqattach(ip, 0); | 
 | 757 | 		if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 758 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 |  | 
 | 760 | 		/* | 
 | 761 | 		 * There are blocks after the end of file. | 
 | 762 | 		 * Free them up now by truncating the file to | 
 | 763 | 		 * its current size. | 
 | 764 | 		 */ | 
 | 765 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); | 
 | 766 |  | 
 | 767 | 		/* | 
 | 768 | 		 * Do the xfs_itruncate_start() call before | 
 | 769 | 		 * reserving any log space because | 
 | 770 | 		 * itruncate_start will call into the buffer | 
 | 771 | 		 * cache and we can't | 
 | 772 | 		 * do that within a transaction. | 
 | 773 | 		 */ | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 774 | 		if (use_iolock) | 
 | 775 | 			xfs_ilock(ip, XFS_IOLOCK_EXCL); | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 776 | 		error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 777 | 				    ip->i_size); | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 778 | 		if (error) { | 
| Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 779 | 			xfs_trans_cancel(tp, 0); | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 780 | 			if (use_iolock) | 
 | 781 | 				xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 782 | 			return error; | 
 | 783 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 |  | 
 | 785 | 		error = xfs_trans_reserve(tp, 0, | 
 | 786 | 					  XFS_ITRUNCATE_LOG_RES(mp), | 
 | 787 | 					  0, XFS_TRANS_PERM_LOG_RES, | 
 | 788 | 					  XFS_ITRUNCATE_LOG_COUNT); | 
 | 789 | 		if (error) { | 
 | 790 | 			ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 791 | 			xfs_trans_cancel(tp, 0); | 
 | 792 | 			xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 793 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | 		} | 
 | 795 |  | 
 | 796 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 797 | 		xfs_trans_ijoin(tp, ip, | 
 | 798 | 				XFS_IOLOCK_EXCL | | 
 | 799 | 				XFS_ILOCK_EXCL); | 
 | 800 | 		xfs_trans_ihold(tp, ip); | 
 | 801 |  | 
 | 802 | 		error = xfs_itruncate_finish(&tp, ip, | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 803 | 					     ip->i_size, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | 					     XFS_DATA_FORK, | 
 | 805 | 					     0); | 
 | 806 | 		/* | 
 | 807 | 		 * If we get an error at this point we | 
 | 808 | 		 * simply don't bother truncating the file. | 
 | 809 | 		 */ | 
 | 810 | 		if (error) { | 
 | 811 | 			xfs_trans_cancel(tp, | 
 | 812 | 					 (XFS_TRANS_RELEASE_LOG_RES | | 
 | 813 | 					  XFS_TRANS_ABORT)); | 
 | 814 | 		} else { | 
 | 815 | 			error = xfs_trans_commit(tp, | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 816 | 						XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | 		} | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 818 | 		xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL) | 
 | 819 | 					    : XFS_ILOCK_EXCL)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | 	} | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 821 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } | 
 | 823 |  | 
 | 824 | /* | 
 | 825 |  * Free a symlink that has blocks associated with it. | 
 | 826 |  */ | 
 | 827 | STATIC int | 
 | 828 | xfs_inactive_symlink_rmt( | 
 | 829 | 	xfs_inode_t	*ip, | 
 | 830 | 	xfs_trans_t	**tpp) | 
 | 831 | { | 
 | 832 | 	xfs_buf_t	*bp; | 
 | 833 | 	int		committed; | 
 | 834 | 	int		done; | 
 | 835 | 	int		error; | 
 | 836 | 	xfs_fsblock_t	first_block; | 
 | 837 | 	xfs_bmap_free_t	free_list; | 
 | 838 | 	int		i; | 
 | 839 | 	xfs_mount_t	*mp; | 
 | 840 | 	xfs_bmbt_irec_t	mval[SYMLINK_MAPS]; | 
 | 841 | 	int		nmaps; | 
 | 842 | 	xfs_trans_t	*ntp; | 
 | 843 | 	int		size; | 
 | 844 | 	xfs_trans_t	*tp; | 
 | 845 |  | 
 | 846 | 	tp = *tpp; | 
 | 847 | 	mp = ip->i_mount; | 
 | 848 | 	ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip)); | 
 | 849 | 	/* | 
 | 850 | 	 * We're freeing a symlink that has some | 
 | 851 | 	 * blocks allocated to it.  Free the | 
 | 852 | 	 * blocks here.  We know that we've got | 
 | 853 | 	 * either 1 or 2 extents and that we can | 
 | 854 | 	 * free them all in one bunmapi call. | 
 | 855 | 	 */ | 
 | 856 | 	ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); | 
 | 857 | 	if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, | 
 | 858 | 			XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { | 
 | 859 | 		ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 860 | 		xfs_trans_cancel(tp, 0); | 
 | 861 | 		*tpp = NULL; | 
 | 862 | 		return error; | 
 | 863 | 	} | 
 | 864 | 	/* | 
 | 865 | 	 * Lock the inode, fix the size, and join it to the transaction. | 
 | 866 | 	 * Hold it so in the normal path, we still have it locked for | 
 | 867 | 	 * the second transaction.  In the error paths we need it | 
 | 868 | 	 * held so the cancel won't rele it, see below. | 
 | 869 | 	 */ | 
 | 870 | 	xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 871 | 	size = (int)ip->i_d.di_size; | 
 | 872 | 	ip->i_d.di_size = 0; | 
 | 873 | 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 874 | 	xfs_trans_ihold(tp, ip); | 
 | 875 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 876 | 	/* | 
 | 877 | 	 * Find the block(s) so we can inval and unmap them. | 
 | 878 | 	 */ | 
 | 879 | 	done = 0; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 880 | 	xfs_bmap_init(&free_list, &first_block); | 
| Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 881 | 	nmaps = ARRAY_SIZE(mval); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | 	if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size), | 
 | 883 | 			XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 884 | 			&free_list, NULL))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | 		goto error0; | 
 | 886 | 	/* | 
 | 887 | 	 * Invalidate the block(s). | 
 | 888 | 	 */ | 
 | 889 | 	for (i = 0; i < nmaps; i++) { | 
 | 890 | 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, | 
 | 891 | 			XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), | 
 | 892 | 			XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0); | 
 | 893 | 		xfs_trans_binval(tp, bp); | 
 | 894 | 	} | 
 | 895 | 	/* | 
 | 896 | 	 * Unmap the dead block(s) to the free_list. | 
 | 897 | 	 */ | 
 | 898 | 	if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 899 | 			&first_block, &free_list, NULL, &done))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | 		goto error1; | 
 | 901 | 	ASSERT(done); | 
 | 902 | 	/* | 
 | 903 | 	 * Commit the first transaction.  This logs the EFI and the inode. | 
 | 904 | 	 */ | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 905 | 	if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | 		goto error1; | 
 | 907 | 	/* | 
 | 908 | 	 * The transaction must have been committed, since there were | 
 | 909 | 	 * actually extents freed by xfs_bunmapi.  See xfs_bmap_finish. | 
 | 910 | 	 * The new tp has the extent freeing and EFDs. | 
 | 911 | 	 */ | 
 | 912 | 	ASSERT(committed); | 
 | 913 | 	/* | 
 | 914 | 	 * The first xact was committed, so add the inode to the new one. | 
 | 915 | 	 * Mark it dirty so it will be logged and moved forward in the log as | 
 | 916 | 	 * part of every commit. | 
 | 917 | 	 */ | 
 | 918 | 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 919 | 	xfs_trans_ihold(tp, ip); | 
 | 920 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 921 | 	/* | 
 | 922 | 	 * Get a new, empty transaction to return to our caller. | 
 | 923 | 	 */ | 
 | 924 | 	ntp = xfs_trans_dup(tp); | 
 | 925 | 	/* | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 926 | 	 * Commit the transaction containing extent freeing and EFDs. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | 	 * If we get an error on the commit here or on the reserve below, | 
 | 928 | 	 * we need to unlock the inode since the new transaction doesn't | 
 | 929 | 	 * have the inode attached. | 
 | 930 | 	 */ | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 931 | 	error = xfs_trans_commit(tp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | 	tp = ntp; | 
 | 933 | 	if (error) { | 
 | 934 | 		ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 935 | 		goto error0; | 
 | 936 | 	} | 
 | 937 | 	/* | 
| Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 938 | 	 * transaction commit worked ok so we can drop the extra ticket | 
 | 939 | 	 * reference that we gained in xfs_trans_dup() | 
 | 940 | 	 */ | 
 | 941 | 	xfs_log_ticket_put(tp->t_ticket); | 
 | 942 |  | 
 | 943 | 	/* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | 	 * Remove the memory for extent descriptions (just bookkeeping). | 
 | 945 | 	 */ | 
 | 946 | 	if (ip->i_df.if_bytes) | 
 | 947 | 		xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); | 
 | 948 | 	ASSERT(ip->i_df.if_bytes == 0); | 
 | 949 | 	/* | 
 | 950 | 	 * Put an itruncate log reservation in the new transaction | 
 | 951 | 	 * for our caller. | 
 | 952 | 	 */ | 
 | 953 | 	if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, | 
 | 954 | 			XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { | 
 | 955 | 		ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 956 | 		goto error0; | 
 | 957 | 	} | 
 | 958 | 	/* | 
 | 959 | 	 * Return with the inode locked but not joined to the transaction. | 
 | 960 | 	 */ | 
 | 961 | 	*tpp = tp; | 
 | 962 | 	return 0; | 
 | 963 |  | 
 | 964 |  error1: | 
 | 965 | 	xfs_bmap_cancel(&free_list); | 
 | 966 |  error0: | 
 | 967 | 	/* | 
 | 968 | 	 * Have to come here with the inode locked and either | 
 | 969 | 	 * (held and in the transaction) or (not in the transaction). | 
 | 970 | 	 * If the inode isn't held then cancel would iput it, but | 
 | 971 | 	 * that's wrong since this is inactive and the vnode ref | 
 | 972 | 	 * count is 0 already. | 
 | 973 | 	 * Cancel won't do anything to the inode if held, but it still | 
 | 974 | 	 * needs to be locked until the cancel is done, if it was | 
 | 975 | 	 * joined to the transaction. | 
 | 976 | 	 */ | 
 | 977 | 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | 
 | 978 | 	xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 979 | 	*tpp = NULL; | 
 | 980 | 	return error; | 
 | 981 |  | 
 | 982 | } | 
 | 983 |  | 
 | 984 | STATIC int | 
 | 985 | xfs_inactive_symlink_local( | 
 | 986 | 	xfs_inode_t	*ip, | 
 | 987 | 	xfs_trans_t	**tpp) | 
 | 988 | { | 
 | 989 | 	int		error; | 
 | 990 |  | 
 | 991 | 	ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip)); | 
 | 992 | 	/* | 
 | 993 | 	 * We're freeing a symlink which fit into | 
 | 994 | 	 * the inode.  Just free the memory used | 
 | 995 | 	 * to hold the old symlink. | 
 | 996 | 	 */ | 
 | 997 | 	error = xfs_trans_reserve(*tpp, 0, | 
 | 998 | 				  XFS_ITRUNCATE_LOG_RES(ip->i_mount), | 
 | 999 | 				  0, XFS_TRANS_PERM_LOG_RES, | 
 | 1000 | 				  XFS_ITRUNCATE_LOG_COUNT); | 
 | 1001 |  | 
 | 1002 | 	if (error) { | 
 | 1003 | 		xfs_trans_cancel(*tpp, 0); | 
 | 1004 | 		*tpp = NULL; | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1005 | 		return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | 	} | 
 | 1007 | 	xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 1008 |  | 
 | 1009 | 	/* | 
 | 1010 | 	 * Zero length symlinks _can_ exist. | 
 | 1011 | 	 */ | 
 | 1012 | 	if (ip->i_df.if_bytes > 0) { | 
 | 1013 | 		xfs_idata_realloc(ip, | 
 | 1014 | 				  -(ip->i_df.if_bytes), | 
 | 1015 | 				  XFS_DATA_FORK); | 
 | 1016 | 		ASSERT(ip->i_df.if_bytes == 0); | 
 | 1017 | 	} | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1018 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } | 
 | 1020 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | STATIC int | 
 | 1022 | xfs_inactive_attrs( | 
 | 1023 | 	xfs_inode_t	*ip, | 
 | 1024 | 	xfs_trans_t	**tpp) | 
 | 1025 | { | 
 | 1026 | 	xfs_trans_t	*tp; | 
 | 1027 | 	int		error; | 
 | 1028 | 	xfs_mount_t	*mp; | 
 | 1029 |  | 
| Christoph Hellwig | 579aa9ca | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1030 | 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | 	tp = *tpp; | 
 | 1032 | 	mp = ip->i_mount; | 
 | 1033 | 	ASSERT(ip->i_d.di_forkoff != 0); | 
| David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1034 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
| David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1036 | 	if (error) | 
 | 1037 | 		goto error_unlock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 |  | 
 | 1039 | 	error = xfs_attr_inactive(ip); | 
| David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1040 | 	if (error) | 
 | 1041 | 		goto error_unlock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 |  | 
 | 1043 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); | 
 | 1044 | 	error = xfs_trans_reserve(tp, 0, | 
 | 1045 | 				  XFS_IFREE_LOG_RES(mp), | 
 | 1046 | 				  0, XFS_TRANS_PERM_LOG_RES, | 
 | 1047 | 				  XFS_INACTIVE_LOG_COUNT); | 
| David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1048 | 	if (error) | 
 | 1049 | 		goto error_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 |  | 
 | 1051 | 	xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 1052 | 	xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 1053 | 	xfs_trans_ihold(tp, ip); | 
 | 1054 | 	xfs_idestroy_fork(ip, XFS_ATTR_FORK); | 
 | 1055 |  | 
 | 1056 | 	ASSERT(ip->i_d.di_anextents == 0); | 
 | 1057 |  | 
 | 1058 | 	*tpp = tp; | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1059 | 	return 0; | 
| David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1060 |  | 
 | 1061 | error_cancel: | 
 | 1062 | 	ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 1063 | 	xfs_trans_cancel(tp, 0); | 
 | 1064 | error_unlock: | 
 | 1065 | 	*tpp = NULL; | 
 | 1066 | 	xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
 | 1067 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } | 
 | 1069 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1070 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | xfs_release( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1072 | 	xfs_inode_t	*ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1074 | 	xfs_mount_t	*mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | 	int		error; | 
 | 1076 |  | 
| Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 1077 | 	if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 |  | 
 | 1080 | 	/* If this is a read-only mount, don't do this (would generate I/O) */ | 
| Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1081 | 	if (mp->m_flags & XFS_MOUNT_RDONLY) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | 		return 0; | 
 | 1083 |  | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1084 | 	if (!XFS_FORCED_SHUTDOWN(mp)) { | 
| Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1085 | 		int truncated; | 
 | 1086 |  | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1087 | 		/* | 
 | 1088 | 		 * If we are using filestreams, and we have an unlinked | 
 | 1089 | 		 * file that we are processing the last close on, then nothing | 
 | 1090 | 		 * will be able to reopen and write to this file. Purge this | 
 | 1091 | 		 * inode from the filestreams cache so that it doesn't delay | 
 | 1092 | 		 * teardown of the inode. | 
 | 1093 | 		 */ | 
 | 1094 | 		if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) | 
 | 1095 | 			xfs_filestream_deassociate(ip); | 
 | 1096 |  | 
| Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1097 | 		/* | 
 | 1098 | 		 * If we previously truncated this file and removed old data | 
 | 1099 | 		 * in the process, we want to initiate "early" writeout on | 
 | 1100 | 		 * the last close.  This is an attempt to combat the notorious | 
 | 1101 | 		 * NULL files problem which is particularly noticable from a | 
 | 1102 | 		 * truncate down, buffered (re-)write (delalloc), followed by | 
 | 1103 | 		 * a crash.  What we are effectively doing here is | 
 | 1104 | 		 * significantly reducing the time window where we'd otherwise | 
 | 1105 | 		 * be exposed to that problem. | 
 | 1106 | 		 */ | 
| Christoph Hellwig | 09262b4 | 2007-08-29 11:44:50 +1000 | [diff] [blame] | 1107 | 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); | 
| Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 1108 | 		if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1109 | 			xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE); | 
| Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1110 | 	} | 
 | 1111 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | 	if (ip->i_d.di_nlink != 0) { | 
 | 1113 | 		if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && | 
| Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 1114 | 		     ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 || | 
| Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1115 | 		       ip->i_delayed_blks > 0)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | 		     (ip->i_df.if_flags & XFS_IFEXTENTS))  && | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1117 | 		    (!(ip->i_d.di_flags & | 
 | 1118 | 				(XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) { | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1119 | 			error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); | 
 | 1120 | 			if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1121 | 				return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | 		} | 
 | 1123 | 	} | 
 | 1124 |  | 
 | 1125 | 	return 0; | 
 | 1126 | } | 
 | 1127 |  | 
 | 1128 | /* | 
 | 1129 |  * xfs_inactive | 
 | 1130 |  * | 
 | 1131 |  * This is called when the vnode reference count for the vnode | 
 | 1132 |  * goes to zero.  If the file has been unlinked, then it must | 
 | 1133 |  * now be truncated.  Also, we clear all of the read-ahead state | 
 | 1134 |  * kept for the inode here since the file is now closed. | 
 | 1135 |  */ | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1136 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | xfs_inactive( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1138 | 	xfs_inode_t	*ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1140 | 	xfs_bmap_free_t	free_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | 	xfs_fsblock_t	first_block; | 
 | 1142 | 	int		committed; | 
 | 1143 | 	xfs_trans_t	*tp; | 
 | 1144 | 	xfs_mount_t	*mp; | 
 | 1145 | 	int		error; | 
 | 1146 | 	int		truncate; | 
 | 1147 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1148 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | 	/* | 
 | 1151 | 	 * If the inode is already free, then there can be nothing | 
 | 1152 | 	 * to clean up here. | 
 | 1153 | 	 */ | 
| Christoph Hellwig | cb4c8cc | 2009-03-16 08:25:25 +0100 | [diff] [blame] | 1154 | 	if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | 		ASSERT(ip->i_df.if_real_bytes == 0); | 
 | 1156 | 		ASSERT(ip->i_df.if_broot_bytes == 0); | 
 | 1157 | 		return VN_INACTIVE_CACHE; | 
 | 1158 | 	} | 
 | 1159 |  | 
 | 1160 | 	/* | 
 | 1161 | 	 * Only do a truncate if it's a regular file with | 
 | 1162 | 	 * some actual space in it.  It's OK to look at the | 
 | 1163 | 	 * inode's fields without the lock because we're the | 
 | 1164 | 	 * only one with a reference to the inode. | 
 | 1165 | 	 */ | 
 | 1166 | 	truncate = ((ip->i_d.di_nlink == 0) && | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1167 | 	    ((ip->i_d.di_size != 0) || (ip->i_size != 0) || | 
 | 1168 | 	     (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | 	    ((ip->i_d.di_mode & S_IFMT) == S_IFREG)); | 
 | 1170 |  | 
 | 1171 | 	mp = ip->i_mount; | 
 | 1172 |  | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1173 | 	if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) | 
 | 1174 | 		XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 |  | 
 | 1176 | 	error = 0; | 
 | 1177 |  | 
 | 1178 | 	/* If this is a read-only mount, don't do this (would generate I/O) */ | 
| Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1179 | 	if (mp->m_flags & XFS_MOUNT_RDONLY) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | 		goto out; | 
 | 1181 |  | 
 | 1182 | 	if (ip->i_d.di_nlink != 0) { | 
 | 1183 | 		if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && | 
| Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 1184 |                      ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 || | 
| Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1185 |                        ip->i_delayed_blks > 0)) && | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1186 | 		      (ip->i_df.if_flags & XFS_IFEXTENTS) && | 
 | 1187 | 		     (!(ip->i_d.di_flags & | 
 | 1188 | 				(XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) || | 
 | 1189 | 		      (ip->i_delayed_blks != 0)))) { | 
| David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1190 | 			error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); | 
 | 1191 | 			if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1192 | 				return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | 		} | 
 | 1194 | 		goto out; | 
 | 1195 | 	} | 
 | 1196 |  | 
 | 1197 | 	ASSERT(ip->i_d.di_nlink == 0); | 
 | 1198 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1199 | 	error = xfs_qm_dqattach(ip, 0); | 
 | 1200 | 	if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1201 | 		return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 |  | 
 | 1203 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); | 
 | 1204 | 	if (truncate) { | 
 | 1205 | 		/* | 
 | 1206 | 		 * Do the xfs_itruncate_start() call before | 
 | 1207 | 		 * reserving any log space because itruncate_start | 
 | 1208 | 		 * will call into the buffer cache and we can't | 
 | 1209 | 		 * do that within a transaction. | 
 | 1210 | 		 */ | 
 | 1211 | 		xfs_ilock(ip, XFS_IOLOCK_EXCL); | 
 | 1212 |  | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1213 | 		error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); | 
 | 1214 | 		if (error) { | 
| Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 1215 | 			xfs_trans_cancel(tp, 0); | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1216 | 			xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
 | 1217 | 			return VN_INACTIVE_CACHE; | 
 | 1218 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 |  | 
 | 1220 | 		error = xfs_trans_reserve(tp, 0, | 
 | 1221 | 					  XFS_ITRUNCATE_LOG_RES(mp), | 
 | 1222 | 					  0, XFS_TRANS_PERM_LOG_RES, | 
 | 1223 | 					  XFS_ITRUNCATE_LOG_COUNT); | 
 | 1224 | 		if (error) { | 
 | 1225 | 			/* Don't call itruncate_cleanup */ | 
 | 1226 | 			ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 1227 | 			xfs_trans_cancel(tp, 0); | 
 | 1228 | 			xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1229 | 			return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | 		} | 
 | 1231 |  | 
 | 1232 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 1233 | 		xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 1234 | 		xfs_trans_ihold(tp, ip); | 
 | 1235 |  | 
 | 1236 | 		/* | 
 | 1237 | 		 * normally, we have to run xfs_itruncate_finish sync. | 
 | 1238 | 		 * But if filesystem is wsync and we're in the inactive | 
 | 1239 | 		 * path, then we know that nlink == 0, and that the | 
 | 1240 | 		 * xaction that made nlink == 0 is permanently committed | 
 | 1241 | 		 * since xfs_remove runs as a synchronous transaction. | 
 | 1242 | 		 */ | 
 | 1243 | 		error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK, | 
 | 1244 | 				(!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0)); | 
 | 1245 |  | 
 | 1246 | 		if (error) { | 
 | 1247 | 			xfs_trans_cancel(tp, | 
 | 1248 | 				XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | 
 | 1249 | 			xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1250 | 			return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | 		} | 
 | 1252 | 	} else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) { | 
 | 1253 |  | 
 | 1254 | 		/* | 
 | 1255 | 		 * If we get an error while cleaning up a | 
 | 1256 | 		 * symlink we bail out. | 
 | 1257 | 		 */ | 
 | 1258 | 		error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ? | 
 | 1259 | 			xfs_inactive_symlink_rmt(ip, &tp) : | 
 | 1260 | 			xfs_inactive_symlink_local(ip, &tp); | 
 | 1261 |  | 
 | 1262 | 		if (error) { | 
 | 1263 | 			ASSERT(tp == NULL); | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1264 | 			return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | 		} | 
 | 1266 |  | 
 | 1267 | 		xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 1268 | 		xfs_trans_ihold(tp, ip); | 
 | 1269 | 	} else { | 
 | 1270 | 		error = xfs_trans_reserve(tp, 0, | 
 | 1271 | 					  XFS_IFREE_LOG_RES(mp), | 
 | 1272 | 					  0, XFS_TRANS_PERM_LOG_RES, | 
 | 1273 | 					  XFS_INACTIVE_LOG_COUNT); | 
 | 1274 | 		if (error) { | 
 | 1275 | 			ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
 | 1276 | 			xfs_trans_cancel(tp, 0); | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1277 | 			return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | 		} | 
 | 1279 |  | 
 | 1280 | 		xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 1281 | 		xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 1282 | 		xfs_trans_ihold(tp, ip); | 
 | 1283 | 	} | 
 | 1284 |  | 
 | 1285 | 	/* | 
 | 1286 | 	 * If there are attributes associated with the file | 
 | 1287 | 	 * then blow them away now.  The code calls a routine | 
 | 1288 | 	 * that recursively deconstructs the attribute fork. | 
 | 1289 | 	 * We need to just commit the current transaction | 
 | 1290 | 	 * because we can't use it for xfs_attr_inactive(). | 
 | 1291 | 	 */ | 
 | 1292 | 	if (ip->i_d.di_anextents > 0) { | 
 | 1293 | 		error = xfs_inactive_attrs(ip, &tp); | 
 | 1294 | 		/* | 
 | 1295 | 		 * If we got an error, the transaction is already | 
 | 1296 | 		 * cancelled, and the inode is unlocked. Just get out. | 
 | 1297 | 		 */ | 
 | 1298 | 		 if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1299 | 			 return VN_INACTIVE_CACHE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | 	} else if (ip->i_afp) { | 
 | 1301 | 		xfs_idestroy_fork(ip, XFS_ATTR_FORK); | 
 | 1302 | 	} | 
 | 1303 |  | 
 | 1304 | 	/* | 
 | 1305 | 	 * Free the inode. | 
 | 1306 | 	 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1307 | 	xfs_bmap_init(&free_list, &first_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | 	error = xfs_ifree(tp, ip, &free_list); | 
 | 1309 | 	if (error) { | 
 | 1310 | 		/* | 
 | 1311 | 		 * If we fail to free the inode, shut down.  The cancel | 
 | 1312 | 		 * might do that, we need to make sure.  Otherwise the | 
 | 1313 | 		 * inode might be lost for a long time or forever. | 
 | 1314 | 		 */ | 
 | 1315 | 		if (!XFS_FORCED_SHUTDOWN(mp)) { | 
 | 1316 | 			cmn_err(CE_NOTE, | 
 | 1317 | 		"xfs_inactive:	xfs_ifree() returned an error = %d on %s", | 
 | 1318 | 				error, mp->m_fsname); | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1319 | 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | 		} | 
 | 1321 | 		xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); | 
 | 1322 | 	} else { | 
 | 1323 | 		/* | 
 | 1324 | 		 * Credit the quota account(s). The inode is gone. | 
 | 1325 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1326 | 		xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 |  | 
 | 1328 | 		/* | 
| David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1329 | 		 * Just ignore errors at this point.  There is nothing we can | 
 | 1330 | 		 * do except to try to keep going. Make sure it's not a silent | 
 | 1331 | 		 * error. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | 		 */ | 
| David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1333 | 		error = xfs_bmap_finish(&tp,  &free_list, &committed); | 
 | 1334 | 		if (error) | 
 | 1335 | 			xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " | 
 | 1336 | 				"xfs_bmap_finish() returned error %d", error); | 
 | 1337 | 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
 | 1338 | 		if (error) | 
 | 1339 | 			xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " | 
 | 1340 | 				"xfs_trans_commit() returned error %d", error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | 	} | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1342 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | 	/* | 
 | 1344 | 	 * Release the dquots held by inode, if any. | 
 | 1345 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1346 | 	xfs_qm_dqdetach(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | 	xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); | 
 | 1348 |  | 
 | 1349 |  out: | 
 | 1350 | 	return VN_INACTIVE_CACHE; | 
 | 1351 | } | 
 | 1352 |  | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1353 | /* | 
 | 1354 |  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match | 
 | 1355 |  * is allowed, otherwise it has to be an exact match. If a CI match is found, | 
 | 1356 |  * ci_name->name will point to a the actual name (caller must free) or | 
 | 1357 |  * will be set to NULL if an exact match is found. | 
 | 1358 |  */ | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1359 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | xfs_lookup( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1361 | 	xfs_inode_t		*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1362 | 	struct xfs_name		*name, | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1363 | 	xfs_inode_t		**ipp, | 
 | 1364 | 	struct xfs_name		*ci_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | { | 
| Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1366 | 	xfs_ino_t		inum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | 	int			error; | 
 | 1368 | 	uint			lock_mode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1370 | 	xfs_itrace_entry(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 |  | 
 | 1372 | 	if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | 
 | 1373 | 		return XFS_ERROR(EIO); | 
 | 1374 |  | 
 | 1375 | 	lock_mode = xfs_ilock_map_shared(dp); | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1376 | 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | 	xfs_iunlock_map_shared(dp, lock_mode); | 
| Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1378 |  | 
 | 1379 | 	if (error) | 
 | 1380 | 		goto out; | 
 | 1381 |  | 
 | 1382 | 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0); | 
 | 1383 | 	if (error) | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1384 | 		goto out_free_name; | 
| Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1385 |  | 
 | 1386 | 	xfs_itrace_ref(*ipp); | 
 | 1387 | 	return 0; | 
 | 1388 |  | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1389 | out_free_name: | 
 | 1390 | 	if (ci_name) | 
 | 1391 | 		kmem_free(ci_name->name); | 
 | 1392 | out: | 
| Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1393 | 	*ipp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | 	return error; | 
 | 1395 | } | 
 | 1396 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1397 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | xfs_create( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1399 | 	xfs_inode_t		*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1400 | 	struct xfs_name		*name, | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1401 | 	mode_t			mode, | 
 | 1402 | 	xfs_dev_t		rdev, | 
| Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1403 | 	xfs_inode_t		**ipp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | 	cred_t			*credp) | 
 | 1405 | { | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1406 | 	int			is_dir = S_ISDIR(mode); | 
 | 1407 | 	struct xfs_mount	*mp = dp->i_mount; | 
 | 1408 | 	struct xfs_inode	*ip = NULL; | 
 | 1409 | 	struct xfs_trans	*tp = NULL; | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1410 | 	int			error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | 	xfs_bmap_free_t		free_list; | 
 | 1412 | 	xfs_fsblock_t		first_block; | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1413 | 	boolean_t		unlock_dp_on_error = B_FALSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | 	uint			cancel_flags; | 
 | 1415 | 	int			committed; | 
 | 1416 | 	xfs_prid_t		prid; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1417 | 	struct xfs_dquot	*udqp = NULL; | 
 | 1418 | 	struct xfs_dquot	*gdqp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | 	uint			resblks; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1420 | 	uint			log_res; | 
 | 1421 | 	uint			log_count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1423 | 	xfs_itrace_entry(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1425 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 1426 | 		return XFS_ERROR(EIO); | 
 | 1427 |  | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 1428 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | 		error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE, | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1430 | 				dp, DM_RIGHT_NULL, NULL, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1431 | 				DM_RIGHT_NULL, name->name, NULL, | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1432 | 				mode, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 |  | 
 | 1434 | 		if (error) | 
 | 1435 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | 	} | 
 | 1437 |  | 
| Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1438 | 	if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | 
 | 1439 | 		prid = dp->i_d.di_projid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | 	else | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1441 | 		prid = dfltprid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 |  | 
 | 1443 | 	/* | 
 | 1444 | 	 * Make sure that we have allocated dquot(s) on disk. | 
 | 1445 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1446 | 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1447 | 			XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | 	if (error) | 
 | 1449 | 		goto std_return; | 
 | 1450 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1451 | 	if (is_dir) { | 
 | 1452 | 		rdev = 0; | 
 | 1453 | 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len); | 
 | 1454 | 		log_res = XFS_MKDIR_LOG_RES(mp); | 
 | 1455 | 		log_count = XFS_MKDIR_LOG_COUNT; | 
 | 1456 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); | 
 | 1457 | 	} else { | 
 | 1458 | 		resblks = XFS_CREATE_SPACE_RES(mp, name->len); | 
 | 1459 | 		log_res = XFS_CREATE_LOG_RES(mp); | 
 | 1460 | 		log_count = XFS_CREATE_LOG_COUNT; | 
 | 1461 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); | 
 | 1462 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1465 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | 	/* | 
 | 1467 | 	 * Initially assume that the file does not exist and | 
 | 1468 | 	 * reserve the resources for that case.  If that is not | 
 | 1469 | 	 * the case we'll drop the one we have and get a more | 
 | 1470 | 	 * appropriate transaction later. | 
 | 1471 | 	 */ | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1472 | 	error = xfs_trans_reserve(tp, resblks, log_res, 0, | 
 | 1473 | 			XFS_TRANS_PERM_LOG_RES, log_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | 	if (error == ENOSPC) { | 
| Dave Chinner | 153fec4 | 2009-04-06 18:48:30 +0200 | [diff] [blame] | 1475 | 		/* flush outstanding delalloc blocks and retry */ | 
 | 1476 | 		xfs_flush_inodes(dp); | 
 | 1477 | 		error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0, | 
 | 1478 | 			XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT); | 
 | 1479 | 	} | 
 | 1480 | 	if (error == ENOSPC) { | 
 | 1481 | 		/* No space at all so try a "no-allocation" reservation */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | 		resblks = 0; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1483 | 		error = xfs_trans_reserve(tp, 0, log_res, 0, | 
 | 1484 | 				XFS_TRANS_PERM_LOG_RES, log_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | 	} | 
 | 1486 | 	if (error) { | 
 | 1487 | 		cancel_flags = 0; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1488 | 		goto out_trans_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | 	} | 
 | 1490 |  | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1491 | 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1492 | 	unlock_dp_on_error = B_TRUE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1494 | 	/* | 
 | 1495 | 	 * Check for directory link count overflow. | 
 | 1496 | 	 */ | 
 | 1497 | 	if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) { | 
 | 1498 | 		error = XFS_ERROR(EMLINK); | 
 | 1499 | 		goto out_trans_cancel; | 
 | 1500 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1502 | 	xfs_bmap_init(&free_list, &first_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 |  | 
 | 1504 | 	/* | 
 | 1505 | 	 * Reserve disk quota and the inode. | 
 | 1506 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1507 | 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | 	if (error) | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1509 | 		goto out_trans_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1511 | 	error = xfs_dir_canenter(tp, dp, name, resblks); | 
 | 1512 | 	if (error) | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1513 | 		goto out_trans_cancel; | 
 | 1514 |  | 
 | 1515 | 	/* | 
 | 1516 | 	 * A newly created regular or special file just has one directory | 
 | 1517 | 	 * entry pointing to them, but a directory also the "." entry | 
 | 1518 | 	 * pointing to itself. | 
 | 1519 | 	 */ | 
 | 1520 | 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp, | 
 | 1521 | 			       prid, resblks > 0, &ip, &committed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | 	if (error) { | 
 | 1523 | 		if (error == ENOSPC) | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1524 | 			goto out_trans_cancel; | 
 | 1525 | 		goto out_trans_abort; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 |  | 
 | 1528 | 	/* | 
 | 1529 | 	 * At this point, we've gotten a newly allocated inode. | 
 | 1530 | 	 * It is locked (and joined to the transaction). | 
 | 1531 | 	 */ | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1532 | 	xfs_itrace_ref(ip); | 
| Christoph Hellwig | 579aa9ca | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1533 | 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 |  | 
 | 1535 | 	/* | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1536 | 	 * Now we join the directory inode to the transaction.  We do not do it | 
 | 1537 | 	 * earlier because xfs_dir_ialloc might commit the previous transaction | 
 | 1538 | 	 * (and release all the locks).  An error from here on will result in | 
 | 1539 | 	 * the transaction cancel unlocking dp so don't do it explicitly in the | 
 | 1540 | 	 * error path. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | 	 */ | 
| Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1542 | 	IHOLD(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1544 | 	unlock_dp_on_error = B_FALSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1546 | 	error = xfs_dir_createname(tp, dp, name, ip->i_ino, | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 1547 | 					&first_block, &free_list, resblks ? | 
 | 1548 | 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | 	if (error) { | 
 | 1550 | 		ASSERT(error != ENOSPC); | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1551 | 		goto out_trans_abort; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | 	} | 
 | 1553 | 	xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
 | 1554 | 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
 | 1555 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1556 | 	if (is_dir) { | 
 | 1557 | 		error = xfs_dir_init(tp, ip, dp); | 
 | 1558 | 		if (error) | 
 | 1559 | 			goto out_bmap_cancel; | 
 | 1560 |  | 
 | 1561 | 		error = xfs_bumplink(tp, dp); | 
 | 1562 | 		if (error) | 
 | 1563 | 			goto out_bmap_cancel; | 
 | 1564 | 	} | 
 | 1565 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | 	/* | 
 | 1567 | 	 * If this is a synchronous mount, make sure that the | 
 | 1568 | 	 * create transaction goes to disk before returning to | 
 | 1569 | 	 * the user. | 
 | 1570 | 	 */ | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1571 | 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | 		xfs_trans_set_sync(tp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | 	/* | 
 | 1575 | 	 * Attach the dquot(s) to the inodes and modify them incore. | 
 | 1576 | 	 * These ids of the inode couldn't have changed since the new | 
 | 1577 | 	 * inode has been locked ever since it was created. | 
 | 1578 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1579 | 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 |  | 
 | 1581 | 	/* | 
 | 1582 | 	 * xfs_trans_commit normally decrements the vnode ref count | 
 | 1583 | 	 * when it unlocks the inode. Since we want to return the | 
 | 1584 | 	 * vnode to the caller, we bump the vnode ref count now. | 
 | 1585 | 	 */ | 
 | 1586 | 	IHOLD(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 |  | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1588 | 	error = xfs_bmap_finish(&tp, &free_list, &committed); | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1589 | 	if (error) | 
 | 1590 | 		goto out_abort_rele; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1592 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | 	if (error) { | 
 | 1594 | 		IRELE(ip); | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1595 | 		goto out_dqrele; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | 	} | 
 | 1597 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1598 | 	xfs_qm_dqrele(udqp); | 
 | 1599 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 |  | 
| Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1601 | 	*ipp = ip; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 |  | 
 | 1603 | 	/* Fallthrough to std_return with error = 0  */ | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1604 |  std_return: | 
 | 1605 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) { | 
 | 1606 | 		XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL, | 
 | 1607 | 				ip, DM_RIGHT_NULL, name->name, NULL, mode, | 
 | 1608 | 				error, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | 	} | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1610 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | 	return error; | 
 | 1612 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1613 |  out_bmap_cancel: | 
 | 1614 | 	xfs_bmap_cancel(&free_list); | 
 | 1615 |  out_trans_abort: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | 	cancel_flags |= XFS_TRANS_ABORT; | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1617 |  out_trans_cancel: | 
 | 1618 | 	xfs_trans_cancel(tp, cancel_flags); | 
 | 1619 |  out_dqrele: | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1620 | 	xfs_qm_dqrele(udqp); | 
 | 1621 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1623 | 	if (unlock_dp_on_error) | 
 | 1624 | 		xfs_iunlock(dp, XFS_ILOCK_EXCL); | 
 | 1625 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | 	goto std_return; | 
 | 1627 |  | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1628 |  out_abort_rele: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | 	/* | 
 | 1630 | 	 * Wait until after the current transaction is aborted to | 
 | 1631 | 	 * release the inode.  This prevents recursive transactions | 
 | 1632 | 	 * and deadlocks from xfs_inactive. | 
 | 1633 | 	 */ | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1634 | 	xfs_bmap_cancel(&free_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | 	cancel_flags |= XFS_TRANS_ABORT; | 
 | 1636 | 	xfs_trans_cancel(tp, cancel_flags); | 
 | 1637 | 	IRELE(ip); | 
| Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 1638 | 	unlock_dp_on_error = B_FALSE; | 
 | 1639 | 	goto out_dqrele; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | } | 
 | 1641 |  | 
 | 1642 | #ifdef DEBUG | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | int xfs_locked_n; | 
 | 1644 | int xfs_small_retries; | 
 | 1645 | int xfs_middle_retries; | 
 | 1646 | int xfs_lots_retries; | 
 | 1647 | int xfs_lock_delays; | 
 | 1648 | #endif | 
 | 1649 |  | 
 | 1650 | /* | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1651 |  * Bump the subclass so xfs_lock_inodes() acquires each lock with | 
 | 1652 |  * a different value | 
 | 1653 |  */ | 
 | 1654 | static inline int | 
 | 1655 | xfs_lock_inumorder(int lock_mode, int subclass) | 
 | 1656 | { | 
 | 1657 | 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) | 
| David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 1658 | 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1659 | 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) | 
| David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 1660 | 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1661 |  | 
 | 1662 | 	return lock_mode; | 
 | 1663 | } | 
 | 1664 |  | 
 | 1665 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 |  * The following routine will lock n inodes in exclusive mode. | 
 | 1667 |  * We assume the caller calls us with the inodes in i_ino order. | 
 | 1668 |  * | 
 | 1669 |  * We need to detect deadlock where an inode that we lock | 
 | 1670 |  * is in the AIL and we start waiting for another inode that is locked | 
 | 1671 |  * by a thread in a long running transaction (such as truncate). This can | 
 | 1672 |  * result in deadlock since the long running trans might need to wait | 
 | 1673 |  * for the inode we just locked in order to push the tail and free space | 
 | 1674 |  * in the log. | 
 | 1675 |  */ | 
 | 1676 | void | 
 | 1677 | xfs_lock_inodes( | 
 | 1678 | 	xfs_inode_t	**ips, | 
 | 1679 | 	int		inodes, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | 	uint		lock_mode) | 
 | 1681 | { | 
 | 1682 | 	int		attempts = 0, i, j, try_lock; | 
 | 1683 | 	xfs_log_item_t	*lp; | 
 | 1684 |  | 
 | 1685 | 	ASSERT(ips && (inodes >= 2)); /* we need at least two */ | 
 | 1686 |  | 
| Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 1687 | 	try_lock = 0; | 
 | 1688 | 	i = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 |  | 
 | 1690 | again: | 
 | 1691 | 	for (; i < inodes; i++) { | 
 | 1692 | 		ASSERT(ips[i]); | 
 | 1693 |  | 
 | 1694 | 		if (i && (ips[i] == ips[i-1]))	/* Already locked */ | 
 | 1695 | 			continue; | 
 | 1696 |  | 
 | 1697 | 		/* | 
 | 1698 | 		 * If try_lock is not set yet, make sure all locked inodes | 
 | 1699 | 		 * are not in the AIL. | 
 | 1700 | 		 * If any are, set try_lock to be used later. | 
 | 1701 | 		 */ | 
 | 1702 |  | 
 | 1703 | 		if (!try_lock) { | 
 | 1704 | 			for (j = (i - 1); j >= 0 && !try_lock; j--) { | 
 | 1705 | 				lp = (xfs_log_item_t *)ips[j]->i_itemp; | 
 | 1706 | 				if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { | 
 | 1707 | 					try_lock++; | 
 | 1708 | 				} | 
 | 1709 | 			} | 
 | 1710 | 		} | 
 | 1711 |  | 
 | 1712 | 		/* | 
 | 1713 | 		 * If any of the previous locks we have locked is in the AIL, | 
 | 1714 | 		 * we must TRY to get the second and subsequent locks. If | 
 | 1715 | 		 * we can't get any, we must release all we have | 
 | 1716 | 		 * and try again. | 
 | 1717 | 		 */ | 
 | 1718 |  | 
 | 1719 | 		if (try_lock) { | 
 | 1720 | 			/* try_lock must be 0 if i is 0. */ | 
 | 1721 | 			/* | 
 | 1722 | 			 * try_lock means we have an inode locked | 
 | 1723 | 			 * that is in the AIL. | 
 | 1724 | 			 */ | 
 | 1725 | 			ASSERT(i != 0); | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1726 | 			if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | 				attempts++; | 
 | 1728 |  | 
 | 1729 | 				/* | 
 | 1730 | 				 * Unlock all previous guys and try again. | 
 | 1731 | 				 * xfs_iunlock will try to push the tail | 
 | 1732 | 				 * if the inode is in the AIL. | 
 | 1733 | 				 */ | 
 | 1734 |  | 
 | 1735 | 				for(j = i - 1; j >= 0; j--) { | 
 | 1736 |  | 
 | 1737 | 					/* | 
 | 1738 | 					 * Check to see if we've already | 
 | 1739 | 					 * unlocked this one. | 
 | 1740 | 					 * Not the first one going back, | 
 | 1741 | 					 * and the inode ptr is the same. | 
 | 1742 | 					 */ | 
 | 1743 | 					if ((j != (i - 1)) && ips[j] == | 
 | 1744 | 								ips[j+1]) | 
 | 1745 | 						continue; | 
 | 1746 |  | 
 | 1747 | 					xfs_iunlock(ips[j], lock_mode); | 
 | 1748 | 				} | 
 | 1749 |  | 
 | 1750 | 				if ((attempts % 5) == 0) { | 
 | 1751 | 					delay(1); /* Don't just spin the CPU */ | 
 | 1752 | #ifdef DEBUG | 
 | 1753 | 					xfs_lock_delays++; | 
 | 1754 | #endif | 
 | 1755 | 				} | 
 | 1756 | 				i = 0; | 
 | 1757 | 				try_lock = 0; | 
 | 1758 | 				goto again; | 
 | 1759 | 			} | 
 | 1760 | 		} else { | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1761 | 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | 		} | 
 | 1763 | 	} | 
 | 1764 |  | 
 | 1765 | #ifdef DEBUG | 
 | 1766 | 	if (attempts) { | 
 | 1767 | 		if (attempts < 5) xfs_small_retries++; | 
 | 1768 | 		else if (attempts < 100) xfs_middle_retries++; | 
 | 1769 | 		else xfs_lots_retries++; | 
 | 1770 | 	} else { | 
 | 1771 | 		xfs_locked_n++; | 
 | 1772 | 	} | 
 | 1773 | #endif | 
 | 1774 | } | 
 | 1775 |  | 
| David Chinner | f9114eb | 2008-09-17 16:51:21 +1000 | [diff] [blame] | 1776 | /* | 
 | 1777 |  * xfs_lock_two_inodes() can only be used to lock one type of lock | 
 | 1778 |  * at a time - the iolock or the ilock, but not both at once. If | 
 | 1779 |  * we lock both at once, lockdep will report false positives saying | 
 | 1780 |  * we have violated locking orders. | 
 | 1781 |  */ | 
| Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 1782 | void | 
 | 1783 | xfs_lock_two_inodes( | 
 | 1784 | 	xfs_inode_t		*ip0, | 
 | 1785 | 	xfs_inode_t		*ip1, | 
 | 1786 | 	uint			lock_mode) | 
 | 1787 | { | 
 | 1788 | 	xfs_inode_t		*temp; | 
 | 1789 | 	int			attempts = 0; | 
 | 1790 | 	xfs_log_item_t		*lp; | 
 | 1791 |  | 
| David Chinner | f9114eb | 2008-09-17 16:51:21 +1000 | [diff] [blame] | 1792 | 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) | 
 | 1793 | 		ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); | 
| Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 1794 | 	ASSERT(ip0->i_ino != ip1->i_ino); | 
 | 1795 |  | 
 | 1796 | 	if (ip0->i_ino > ip1->i_ino) { | 
 | 1797 | 		temp = ip0; | 
 | 1798 | 		ip0 = ip1; | 
 | 1799 | 		ip1 = temp; | 
 | 1800 | 	} | 
 | 1801 |  | 
 | 1802 |  again: | 
 | 1803 | 	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); | 
 | 1804 |  | 
 | 1805 | 	/* | 
 | 1806 | 	 * If the first lock we have locked is in the AIL, we must TRY to get | 
 | 1807 | 	 * the second lock. If we can't get it, we must release the first one | 
 | 1808 | 	 * and try again. | 
 | 1809 | 	 */ | 
 | 1810 | 	lp = (xfs_log_item_t *)ip0->i_itemp; | 
 | 1811 | 	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { | 
 | 1812 | 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { | 
 | 1813 | 			xfs_iunlock(ip0, lock_mode); | 
 | 1814 | 			if ((++attempts % 5) == 0) | 
 | 1815 | 				delay(1); /* Don't just spin the CPU */ | 
 | 1816 | 			goto again; | 
 | 1817 | 		} | 
 | 1818 | 	} else { | 
 | 1819 | 		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); | 
 | 1820 | 	} | 
 | 1821 | } | 
 | 1822 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1823 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | xfs_remove( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1825 | 	xfs_inode_t             *dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1826 | 	struct xfs_name		*name, | 
 | 1827 | 	xfs_inode_t		*ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1829 | 	xfs_mount_t		*mp = dp->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | 	xfs_trans_t             *tp = NULL; | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1831 | 	int			is_dir = S_ISDIR(ip->i_d.di_mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | 	int                     error = 0; | 
 | 1833 | 	xfs_bmap_free_t         free_list; | 
 | 1834 | 	xfs_fsblock_t           first_block; | 
 | 1835 | 	int			cancel_flags; | 
 | 1836 | 	int			committed; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | 	int			link_zero; | 
 | 1838 | 	uint			resblks; | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1839 | 	uint			log_count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1841 | 	xfs_itrace_entry(dp); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1842 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 1845 | 		return XFS_ERROR(EIO); | 
 | 1846 |  | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 1847 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) { | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1848 | 		error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL, | 
 | 1849 | 					NULL, DM_RIGHT_NULL, name->name, NULL, | 
 | 1850 | 					ip->i_d.di_mode, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | 		if (error) | 
 | 1852 | 			return error; | 
 | 1853 | 	} | 
 | 1854 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1855 | 	error = xfs_qm_dqattach(dp, 0); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1856 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | 		goto std_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1859 | 	error = xfs_qm_dqattach(ip, 0); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1860 | 	if (error) | 
 | 1861 | 		goto std_return; | 
 | 1862 |  | 
 | 1863 | 	if (is_dir) { | 
 | 1864 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); | 
 | 1865 | 		log_count = XFS_DEFAULT_LOG_COUNT; | 
 | 1866 | 	} else { | 
 | 1867 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); | 
 | 1868 | 		log_count = XFS_REMOVE_LOG_COUNT; | 
 | 1869 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1871 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | 	/* | 
 | 1873 | 	 * We try to get the real space reservation first, | 
 | 1874 | 	 * allowing for directory btree deletion(s) implying | 
 | 1875 | 	 * possible bmap insert(s).  If we can't get the space | 
 | 1876 | 	 * reservation then we use 0 instead, and avoid the bmap | 
 | 1877 | 	 * btree insert(s) in the directory code by, if the bmap | 
 | 1878 | 	 * insert tries to happen, instead trimming the LAST | 
 | 1879 | 	 * block from the directory. | 
 | 1880 | 	 */ | 
 | 1881 | 	resblks = XFS_REMOVE_SPACE_RES(mp); | 
 | 1882 | 	error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1883 | 				  XFS_TRANS_PERM_LOG_RES, log_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | 	if (error == ENOSPC) { | 
 | 1885 | 		resblks = 0; | 
 | 1886 | 		error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1887 | 					  XFS_TRANS_PERM_LOG_RES, log_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | 	} | 
 | 1889 | 	if (error) { | 
 | 1890 | 		ASSERT(error != ENOSPC); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1891 | 		cancel_flags = 0; | 
 | 1892 | 		goto out_trans_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | 	} | 
 | 1894 |  | 
| Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 1895 | 	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 |  | 
 | 1897 | 	/* | 
 | 1898 | 	 * At this point, we've gotten both the directory and the entry | 
 | 1899 | 	 * inodes locked. | 
 | 1900 | 	 */ | 
| Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 1901 | 	IHOLD(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 82dab94 | 2008-04-22 17:34:18 +1000 | [diff] [blame] | 1903 |  | 
 | 1904 | 	IHOLD(dp); | 
 | 1905 | 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 |  | 
 | 1907 | 	/* | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1908 | 	 * If we're removing a directory perform some additional validation. | 
 | 1909 | 	 */ | 
 | 1910 | 	if (is_dir) { | 
 | 1911 | 		ASSERT(ip->i_d.di_nlink >= 2); | 
 | 1912 | 		if (ip->i_d.di_nlink != 2) { | 
 | 1913 | 			error = XFS_ERROR(ENOTEMPTY); | 
 | 1914 | 			goto out_trans_cancel; | 
 | 1915 | 		} | 
 | 1916 | 		if (!xfs_dir_isempty(ip)) { | 
 | 1917 | 			error = XFS_ERROR(ENOTEMPTY); | 
 | 1918 | 			goto out_trans_cancel; | 
 | 1919 | 		} | 
 | 1920 | 	} | 
 | 1921 |  | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1922 | 	xfs_bmap_init(&free_list, &first_block); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1923 | 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, | 
| Christoph Hellwig | d4377d8 | 2008-04-22 17:33:46 +1000 | [diff] [blame] | 1924 | 					&first_block, &free_list, resblks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | 	if (error) { | 
 | 1926 | 		ASSERT(error != ENOENT); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1927 | 		goto out_bmap_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | 	} | 
 | 1929 | 	xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
 | 1930 |  | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1931 | 	if (is_dir) { | 
 | 1932 | 		/* | 
 | 1933 | 		 * Drop the link from ip's "..". | 
 | 1934 | 		 */ | 
 | 1935 | 		error = xfs_droplink(tp, dp); | 
 | 1936 | 		if (error) | 
 | 1937 | 			goto out_bmap_cancel; | 
 | 1938 |  | 
 | 1939 | 		/* | 
| Christoph Hellwig | 2b7035f | 2008-10-30 17:55:18 +1100 | [diff] [blame] | 1940 | 		 * Drop the "." link from ip to self. | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1941 | 		 */ | 
 | 1942 | 		error = xfs_droplink(tp, ip); | 
 | 1943 | 		if (error) | 
 | 1944 | 			goto out_bmap_cancel; | 
 | 1945 | 	} else { | 
 | 1946 | 		/* | 
 | 1947 | 		 * When removing a non-directory we need to log the parent | 
| Dave Chinner | 26c5295 | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1948 | 		 * inode here.  For a directory this is done implicitly | 
 | 1949 | 		 * by the xfs_droplink call for the ".." entry. | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1950 | 		 */ | 
 | 1951 | 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | 	} | 
 | 1953 |  | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1954 | 	/* | 
| Christoph Hellwig | 2b7035f | 2008-10-30 17:55:18 +1100 | [diff] [blame] | 1955 | 	 * Drop the link from dp to ip. | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1956 | 	 */ | 
 | 1957 | 	error = xfs_droplink(tp, ip); | 
 | 1958 | 	if (error) | 
 | 1959 | 		goto out_bmap_cancel; | 
 | 1960 |  | 
 | 1961 | 	/* | 
 | 1962 | 	 * Determine if this is the last link while | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | 	 * we are in the transaction. | 
 | 1964 | 	 */ | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1965 | 	link_zero = (ip->i_d.di_nlink == 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 |  | 
 | 1967 | 	/* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | 	 * If this is a synchronous mount, make sure that the | 
 | 1969 | 	 * remove transaction goes to disk before returning to | 
 | 1970 | 	 * the user. | 
 | 1971 | 	 */ | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1972 | 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | 		xfs_trans_set_sync(tp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 |  | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1975 | 	error = xfs_bmap_finish(&tp, &free_list, &committed); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1976 | 	if (error) | 
 | 1977 | 		goto out_bmap_cancel; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1979 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 1980 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | 		goto std_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 |  | 
 | 1983 | 	/* | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1984 | 	 * If we are using filestreams, kill the stream association. | 
 | 1985 | 	 * If the file is still open it may get a new one but that | 
 | 1986 | 	 * will get killed on last close in xfs_close() so we don't | 
 | 1987 | 	 * have to worry about that. | 
 | 1988 | 	 */ | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1989 | 	if (!is_dir && link_zero && xfs_inode_is_filestream(ip)) | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1990 | 		xfs_filestream_deassociate(ip); | 
 | 1991 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1992 | 	xfs_itrace_exit(ip); | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1993 | 	xfs_itrace_exit(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 |  std_return: | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 1996 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) { | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1997 | 		XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL, | 
 | 1998 | 				NULL, DM_RIGHT_NULL, name->name, NULL, | 
 | 1999 | 				ip->i_d.di_mode, error, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | 	} | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 2001 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | 	return error; | 
 | 2003 |  | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 2004 |  out_bmap_cancel: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | 	xfs_bmap_cancel(&free_list); | 
 | 2006 | 	cancel_flags |= XFS_TRANS_ABORT; | 
| Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 2007 |  out_trans_cancel: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | 	xfs_trans_cancel(tp, cancel_flags); | 
 | 2009 | 	goto std_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } | 
 | 2011 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2012 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | xfs_link( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2014 | 	xfs_inode_t		*tdp, | 
| Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2015 | 	xfs_inode_t		*sip, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2016 | 	struct xfs_name		*target_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2018 | 	xfs_mount_t		*mp = tdp->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | 	xfs_trans_t		*tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | 	int			error; | 
 | 2021 | 	xfs_bmap_free_t         free_list; | 
 | 2022 | 	xfs_fsblock_t           first_block; | 
 | 2023 | 	int			cancel_flags; | 
 | 2024 | 	int			committed; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | 	int			resblks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2027 | 	xfs_itrace_entry(tdp); | 
| Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2028 | 	xfs_itrace_entry(sip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 |  | 
| Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2030 | 	ASSERT(!S_ISDIR(sip->i_d.di_mode)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 2033 | 		return XFS_ERROR(EIO); | 
 | 2034 |  | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2035 | 	if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | 		error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK, | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2037 | 					tdp, DM_RIGHT_NULL, | 
 | 2038 | 					sip, DM_RIGHT_NULL, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2039 | 					target_name->name, NULL, 0, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | 		if (error) | 
 | 2041 | 			return error; | 
 | 2042 | 	} | 
 | 2043 |  | 
 | 2044 | 	/* Return through std_return after this point. */ | 
 | 2045 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2046 | 	error = xfs_qm_dqattach(sip, 0); | 
| Christoph Hellwig | cb3f35b | 2009-02-04 09:34:20 +0100 | [diff] [blame] | 2047 | 	if (error) | 
 | 2048 | 		goto std_return; | 
 | 2049 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2050 | 	error = xfs_qm_dqattach(tdp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | 	if (error) | 
 | 2052 | 		goto std_return; | 
 | 2053 |  | 
 | 2054 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); | 
 | 2055 | 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2056 | 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | 	error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0, | 
 | 2058 | 			XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); | 
 | 2059 | 	if (error == ENOSPC) { | 
 | 2060 | 		resblks = 0; | 
 | 2061 | 		error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0, | 
 | 2062 | 				XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); | 
 | 2063 | 	} | 
 | 2064 | 	if (error) { | 
 | 2065 | 		cancel_flags = 0; | 
 | 2066 | 		goto error_return; | 
 | 2067 | 	} | 
 | 2068 |  | 
| Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 2069 | 	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 |  | 
 | 2071 | 	/* | 
 | 2072 | 	 * Increment vnode ref counts since xfs_trans_commit & | 
 | 2073 | 	 * xfs_trans_cancel will both unlock the inodes and | 
 | 2074 | 	 * decrement the associated ref counts. | 
 | 2075 | 	 */ | 
| Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2076 | 	IHOLD(sip); | 
 | 2077 | 	IHOLD(tdp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); | 
 | 2079 | 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); | 
 | 2080 |  | 
 | 2081 | 	/* | 
 | 2082 | 	 * If the source has too many links, we can't make any more to it. | 
 | 2083 | 	 */ | 
 | 2084 | 	if (sip->i_d.di_nlink >= XFS_MAXLINK) { | 
 | 2085 | 		error = XFS_ERROR(EMLINK); | 
 | 2086 | 		goto error_return; | 
 | 2087 | 	} | 
 | 2088 |  | 
| Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2089 | 	/* | 
 | 2090 | 	 * If we are using project inheritance, we only allow hard link | 
 | 2091 | 	 * creation in our tree when the project IDs are the same; else | 
 | 2092 | 	 * the tree quota mechanism could be circumvented. | 
 | 2093 | 	 */ | 
 | 2094 | 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | 
 | 2095 | 		     (tdp->i_d.di_projid != sip->i_d.di_projid))) { | 
| Nathan Scott | b1ecdda | 2006-05-08 19:51:42 +1000 | [diff] [blame] | 2096 | 		error = XFS_ERROR(EXDEV); | 
| Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2097 | 		goto error_return; | 
 | 2098 | 	} | 
 | 2099 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2100 | 	error = xfs_dir_canenter(tp, tdp, target_name, resblks); | 
 | 2101 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | 		goto error_return; | 
 | 2103 |  | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2104 | 	xfs_bmap_init(&free_list, &first_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2106 | 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, | 
 | 2107 | 					&first_block, &free_list, resblks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | 	if (error) | 
 | 2109 | 		goto abort_return; | 
 | 2110 | 	xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); | 
 | 2112 |  | 
 | 2113 | 	error = xfs_bumplink(tp, sip); | 
| Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2114 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | 		goto abort_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 |  | 
 | 2117 | 	/* | 
 | 2118 | 	 * If this is a synchronous mount, make sure that the | 
 | 2119 | 	 * link transaction goes to disk before returning to | 
 | 2120 | 	 * the user. | 
 | 2121 | 	 */ | 
 | 2122 | 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | 
 | 2123 | 		xfs_trans_set_sync(tp); | 
 | 2124 | 	} | 
 | 2125 |  | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2126 | 	error = xfs_bmap_finish (&tp, &free_list, &committed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | 	if (error) { | 
 | 2128 | 		xfs_bmap_cancel(&free_list); | 
 | 2129 | 		goto abort_return; | 
 | 2130 | 	} | 
 | 2131 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2132 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2133 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | 		goto std_return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 |  | 
 | 2136 | 	/* Fall through to std_return with error = 0. */ | 
 | 2137 | std_return: | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2138 | 	if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | 		(void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK, | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2140 | 				tdp, DM_RIGHT_NULL, | 
 | 2141 | 				sip, DM_RIGHT_NULL, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2142 | 				target_name->name, NULL, 0, error, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | 	} | 
 | 2144 | 	return error; | 
 | 2145 |  | 
 | 2146 |  abort_return: | 
 | 2147 | 	cancel_flags |= XFS_TRANS_ABORT; | 
 | 2148 | 	/* FALLTHROUGH */ | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 |  error_return: | 
 | 2151 | 	xfs_trans_cancel(tp, cancel_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | 	goto std_return; | 
 | 2153 | } | 
| Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2154 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2155 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | xfs_symlink( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2157 | 	xfs_inode_t		*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2158 | 	struct xfs_name		*link_name, | 
 | 2159 | 	const char		*target_path, | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2160 | 	mode_t			mode, | 
| Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2161 | 	xfs_inode_t		**ipp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | 	cred_t			*credp) | 
 | 2163 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2164 | 	xfs_mount_t		*mp = dp->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | 	xfs_trans_t		*tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | 	xfs_inode_t		*ip; | 
 | 2167 | 	int			error; | 
 | 2168 | 	int			pathlen; | 
 | 2169 | 	xfs_bmap_free_t		free_list; | 
 | 2170 | 	xfs_fsblock_t		first_block; | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2171 | 	boolean_t		unlock_dp_on_error = B_FALSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | 	uint			cancel_flags; | 
 | 2173 | 	int			committed; | 
 | 2174 | 	xfs_fileoff_t		first_fsb; | 
 | 2175 | 	xfs_filblks_t		fs_blocks; | 
 | 2176 | 	int			nmaps; | 
 | 2177 | 	xfs_bmbt_irec_t		mval[SYMLINK_MAPS]; | 
 | 2178 | 	xfs_daddr_t		d; | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2179 | 	const char		*cur_chunk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | 	int			byte_cnt; | 
 | 2181 | 	int			n; | 
 | 2182 | 	xfs_buf_t		*bp; | 
 | 2183 | 	xfs_prid_t		prid; | 
 | 2184 | 	struct xfs_dquot	*udqp, *gdqp; | 
 | 2185 | 	uint			resblks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 |  | 
| Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2187 | 	*ipp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | 	error = 0; | 
 | 2189 | 	ip = NULL; | 
 | 2190 | 	tp = NULL; | 
 | 2191 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2192 | 	xfs_itrace_entry(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 |  | 
 | 2194 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 2195 | 		return XFS_ERROR(EIO); | 
 | 2196 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | 	/* | 
 | 2198 | 	 * Check component lengths of the target path name. | 
 | 2199 | 	 */ | 
 | 2200 | 	pathlen = strlen(target_path); | 
 | 2201 | 	if (pathlen >= MAXPATHLEN)      /* total string too long */ | 
 | 2202 | 		return XFS_ERROR(ENAMETOOLONG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 |  | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2204 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) { | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2205 | 		error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | 					DM_RIGHT_NULL, NULL, DM_RIGHT_NULL, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2207 | 					link_name->name, target_path, 0, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | 		if (error) | 
 | 2209 | 			return error; | 
 | 2210 | 	} | 
 | 2211 |  | 
 | 2212 | 	/* Return through std_return after this point. */ | 
 | 2213 |  | 
 | 2214 | 	udqp = gdqp = NULL; | 
| Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2215 | 	if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | 
 | 2216 | 		prid = dp->i_d.di_projid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | 	else | 
 | 2218 | 		prid = (xfs_prid_t)dfltprid; | 
 | 2219 |  | 
 | 2220 | 	/* | 
 | 2221 | 	 * Make sure that we have allocated dquot(s) on disk. | 
 | 2222 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2223 | 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | 			XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); | 
 | 2225 | 	if (error) | 
 | 2226 | 		goto std_return; | 
 | 2227 |  | 
 | 2228 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); | 
 | 2229 | 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | 
 | 2230 | 	/* | 
 | 2231 | 	 * The symlink will fit into the inode data fork? | 
 | 2232 | 	 * There can't be any attributes so we get the whole variable part. | 
 | 2233 | 	 */ | 
 | 2234 | 	if (pathlen <= XFS_LITINO(mp)) | 
 | 2235 | 		fs_blocks = 0; | 
 | 2236 | 	else | 
 | 2237 | 		fs_blocks = XFS_B_TO_FSB(mp, pathlen); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2238 | 	resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | 	error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0, | 
 | 2240 | 			XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); | 
 | 2241 | 	if (error == ENOSPC && fs_blocks == 0) { | 
 | 2242 | 		resblks = 0; | 
 | 2243 | 		error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, | 
 | 2244 | 				XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); | 
 | 2245 | 	} | 
 | 2246 | 	if (error) { | 
 | 2247 | 		cancel_flags = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | 		goto error_return; | 
 | 2249 | 	} | 
 | 2250 |  | 
| Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2251 | 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2252 | 	unlock_dp_on_error = B_TRUE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 |  | 
 | 2254 | 	/* | 
 | 2255 | 	 * Check whether the directory allows new symlinks or not. | 
 | 2256 | 	 */ | 
 | 2257 | 	if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { | 
 | 2258 | 		error = XFS_ERROR(EPERM); | 
 | 2259 | 		goto error_return; | 
 | 2260 | 	} | 
 | 2261 |  | 
 | 2262 | 	/* | 
 | 2263 | 	 * Reserve disk quota : blocks and inode. | 
 | 2264 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2265 | 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | 	if (error) | 
 | 2267 | 		goto error_return; | 
 | 2268 |  | 
 | 2269 | 	/* | 
 | 2270 | 	 * Check for ability to enter directory entry, if no space reserved. | 
 | 2271 | 	 */ | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2272 | 	error = xfs_dir_canenter(tp, dp, link_name, resblks); | 
 | 2273 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | 		goto error_return; | 
 | 2275 | 	/* | 
 | 2276 | 	 * Initialize the bmap freelist prior to calling either | 
 | 2277 | 	 * bmapi or the directory create code. | 
 | 2278 | 	 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2279 | 	xfs_bmap_init(&free_list, &first_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 |  | 
 | 2281 | 	/* | 
 | 2282 | 	 * Allocate an inode for the symlink. | 
 | 2283 | 	 */ | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2284 | 	error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | 			       1, 0, credp, prid, resblks > 0, &ip, NULL); | 
 | 2286 | 	if (error) { | 
 | 2287 | 		if (error == ENOSPC) | 
 | 2288 | 			goto error_return; | 
 | 2289 | 		goto error1; | 
 | 2290 | 	} | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2291 | 	xfs_itrace_ref(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2293 | 	/* | 
 | 2294 | 	 * An error after we've joined dp to the transaction will result in the | 
 | 2295 | 	 * transaction cancel unlocking dp so don't do it explicitly in the | 
 | 2296 | 	 * error path. | 
 | 2297 | 	 */ | 
| Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2298 | 	IHOLD(dp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2300 | 	unlock_dp_on_error = B_FALSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 |  | 
 | 2302 | 	/* | 
 | 2303 | 	 * Also attach the dquot(s) to it, if applicable. | 
 | 2304 | 	 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2305 | 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 |  | 
 | 2307 | 	if (resblks) | 
 | 2308 | 		resblks -= XFS_IALLOC_SPACE_RES(mp); | 
 | 2309 | 	/* | 
 | 2310 | 	 * If the symlink will fit into the inode, write it inline. | 
 | 2311 | 	 */ | 
 | 2312 | 	if (pathlen <= XFS_IFORK_DSIZE(ip)) { | 
 | 2313 | 		xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK); | 
 | 2314 | 		memcpy(ip->i_df.if_u1.if_data, target_path, pathlen); | 
 | 2315 | 		ip->i_d.di_size = pathlen; | 
 | 2316 |  | 
 | 2317 | 		/* | 
 | 2318 | 		 * The inode was initially created in extent format. | 
 | 2319 | 		 */ | 
 | 2320 | 		ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); | 
 | 2321 | 		ip->i_df.if_flags |= XFS_IFINLINE; | 
 | 2322 |  | 
 | 2323 | 		ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; | 
 | 2324 | 		xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); | 
 | 2325 |  | 
 | 2326 | 	} else { | 
 | 2327 | 		first_fsb = 0; | 
 | 2328 | 		nmaps = SYMLINK_MAPS; | 
 | 2329 |  | 
 | 2330 | 		error = xfs_bmapi(tp, ip, first_fsb, fs_blocks, | 
 | 2331 | 				  XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, | 
 | 2332 | 				  &first_block, resblks, mval, &nmaps, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2333 | 				  &free_list, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | 		if (error) { | 
 | 2335 | 			goto error1; | 
 | 2336 | 		} | 
 | 2337 |  | 
 | 2338 | 		if (resblks) | 
 | 2339 | 			resblks -= fs_blocks; | 
 | 2340 | 		ip->i_d.di_size = pathlen; | 
 | 2341 | 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 2342 |  | 
 | 2343 | 		cur_chunk = target_path; | 
 | 2344 | 		for (n = 0; n < nmaps; n++) { | 
 | 2345 | 			d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); | 
 | 2346 | 			byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); | 
 | 2347 | 			bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, | 
 | 2348 | 					       BTOBB(byte_cnt), 0); | 
 | 2349 | 			ASSERT(bp && !XFS_BUF_GETERROR(bp)); | 
 | 2350 | 			if (pathlen < byte_cnt) { | 
 | 2351 | 				byte_cnt = pathlen; | 
 | 2352 | 			} | 
 | 2353 | 			pathlen -= byte_cnt; | 
 | 2354 |  | 
 | 2355 | 			memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt); | 
 | 2356 | 			cur_chunk += byte_cnt; | 
 | 2357 |  | 
 | 2358 | 			xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1); | 
 | 2359 | 		} | 
 | 2360 | 	} | 
 | 2361 |  | 
 | 2362 | 	/* | 
 | 2363 | 	 * Create the directory entry for the symlink. | 
 | 2364 | 	 */ | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2365 | 	error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, | 
 | 2366 | 					&first_block, &free_list, resblks); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2367 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | 		goto error1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | 	xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
 | 2370 | 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
 | 2371 |  | 
 | 2372 | 	/* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | 	 * If this is a synchronous mount, make sure that the | 
 | 2374 | 	 * symlink transaction goes to disk before returning to | 
 | 2375 | 	 * the user. | 
 | 2376 | 	 */ | 
 | 2377 | 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | 
 | 2378 | 		xfs_trans_set_sync(tp); | 
 | 2379 | 	} | 
 | 2380 |  | 
 | 2381 | 	/* | 
 | 2382 | 	 * xfs_trans_commit normally decrements the vnode ref count | 
 | 2383 | 	 * when it unlocks the inode. Since we want to return the | 
 | 2384 | 	 * vnode to the caller, we bump the vnode ref count now. | 
 | 2385 | 	 */ | 
 | 2386 | 	IHOLD(ip); | 
 | 2387 |  | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2388 | 	error = xfs_bmap_finish(&tp, &free_list, &committed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | 	if (error) { | 
 | 2390 | 		goto error2; | 
 | 2391 | 	} | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2392 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2393 | 	xfs_qm_dqrele(udqp); | 
 | 2394 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 |  | 
 | 2396 | 	/* Fall through to std_return with error = 0 or errno from | 
 | 2397 | 	 * xfs_trans_commit	*/ | 
 | 2398 | std_return: | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2399 | 	if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | 		(void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK, | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2401 | 					dp, DM_RIGHT_NULL, | 
 | 2402 | 					error ? NULL : ip, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2403 | 					DM_RIGHT_NULL, link_name->name, | 
 | 2404 | 					target_path, 0, error, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | 	} | 
 | 2406 |  | 
| Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2407 | 	if (!error) | 
 | 2408 | 		*ipp = ip; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | 	return error; | 
 | 2410 |  | 
 | 2411 |  error2: | 
 | 2412 | 	IRELE(ip); | 
 | 2413 |  error1: | 
 | 2414 | 	xfs_bmap_cancel(&free_list); | 
 | 2415 | 	cancel_flags |= XFS_TRANS_ABORT; | 
 | 2416 |  error_return: | 
 | 2417 | 	xfs_trans_cancel(tp, cancel_flags); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2418 | 	xfs_qm_dqrele(udqp); | 
 | 2419 | 	xfs_qm_dqrele(gdqp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2421 | 	if (unlock_dp_on_error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | 		xfs_iunlock(dp, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 |  | 
 | 2424 | 	goto std_return; | 
 | 2425 | } | 
 | 2426 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | int | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2428 | xfs_set_dmattrs( | 
 | 2429 | 	xfs_inode_t     *ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | 	u_int		evmask, | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2431 | 	u_int16_t	state) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2433 | 	xfs_mount_t	*mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | 	xfs_trans_t	*tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | 	int		error; | 
 | 2436 |  | 
 | 2437 | 	if (!capable(CAP_SYS_ADMIN)) | 
 | 2438 | 		return XFS_ERROR(EPERM); | 
 | 2439 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 2441 | 		return XFS_ERROR(EIO); | 
 | 2442 |  | 
 | 2443 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS); | 
 | 2444 | 	error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0); | 
 | 2445 | 	if (error) { | 
 | 2446 | 		xfs_trans_cancel(tp, 0); | 
 | 2447 | 		return error; | 
 | 2448 | 	} | 
 | 2449 | 	xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 2450 | 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
 | 2451 |  | 
| Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 2452 | 	ip->i_d.di_dmevmask = evmask; | 
 | 2453 | 	ip->i_d.di_dmstate  = state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 |  | 
 | 2455 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 2456 | 	IHOLD(ip); | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2457 | 	error = xfs_trans_commit(tp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 |  | 
 | 2459 | 	return error; | 
 | 2460 | } | 
 | 2461 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2462 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | xfs_reclaim( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2464 | 	xfs_inode_t	*ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2467 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 |  | 
| Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 2469 | 	ASSERT(!VN_MAPPED(VFS_I(ip))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 |  | 
 | 2471 | 	/* bad inode, get out here ASAP */ | 
| Christoph Hellwig | cb4c8cc | 2009-03-16 08:25:25 +0100 | [diff] [blame] | 2472 | 	if (is_bad_inode(VFS_I(ip))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | 		xfs_ireclaim(ip); | 
 | 2474 | 		return 0; | 
 | 2475 | 	} | 
 | 2476 |  | 
| Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 2477 | 	xfs_ioend_wait(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 |  | 
| Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame] | 2479 | 	ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 |  | 
| Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 2481 | 	/* | 
 | 2482 | 	 * Make sure the atime in the XFS inode is correct before freeing the | 
 | 2483 | 	 * Linux inode. | 
 | 2484 | 	 */ | 
 | 2485 | 	xfs_synchronize_atime(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 |  | 
| David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 2487 | 	/* | 
 | 2488 | 	 * If we have nothing to flush with this inode then complete the | 
 | 2489 | 	 * teardown now, otherwise break the link between the xfs inode and the | 
 | 2490 | 	 * linux inode and clean up the xfs inode later. This avoids flushing | 
 | 2491 | 	 * the inode to disk during the delete operation itself. | 
 | 2492 | 	 * | 
 | 2493 | 	 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag | 
 | 2494 | 	 * first to ensure that xfs_iunpin() will never see an xfs inode | 
 | 2495 | 	 * that has a linux inode being reclaimed. Synchronisation is provided | 
 | 2496 | 	 * by the i_flags_lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | 	 */ | 
 | 2498 | 	if (!ip->i_update_core && (ip->i_itemp == NULL)) { | 
 | 2499 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 2500 | 		xfs_iflock(ip); | 
| David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 2501 | 		xfs_iflags_set(ip, XFS_IRECLAIMABLE); | 
| David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 2502 | 		return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | 	} | 
| David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 2504 | 	xfs_inode_set_reclaim_tag(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | 	return 0; | 
 | 2506 | } | 
 | 2507 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | /* | 
 | 2509 |  * xfs_alloc_file_space() | 
 | 2510 |  *      This routine allocates disk space for the given file. | 
 | 2511 |  * | 
 | 2512 |  *	If alloc_type == 0, this request is for an ALLOCSP type | 
 | 2513 |  *	request which will change the file size.  In this case, no | 
 | 2514 |  *	DMAPI event will be generated by the call.  A TRUNCATE event | 
 | 2515 |  *	will be generated later by xfs_setattr. | 
 | 2516 |  * | 
 | 2517 |  *	If alloc_type != 0, this request is for a RESVSP type | 
 | 2518 |  *	request, and a DMAPI DM_EVENT_WRITE will be generated if the | 
 | 2519 |  *	lower block boundary byte address is less than the file's | 
 | 2520 |  *	length. | 
 | 2521 |  * | 
 | 2522 |  * RETURNS: | 
 | 2523 |  *       0 on success | 
 | 2524 |  *      errno on error | 
 | 2525 |  * | 
 | 2526 |  */ | 
| Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2527 | STATIC int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | xfs_alloc_file_space( | 
 | 2529 | 	xfs_inode_t		*ip, | 
 | 2530 | 	xfs_off_t		offset, | 
 | 2531 | 	xfs_off_t		len, | 
 | 2532 | 	int			alloc_type, | 
 | 2533 | 	int			attr_flags) | 
 | 2534 | { | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2535 | 	xfs_mount_t		*mp = ip->i_mount; | 
 | 2536 | 	xfs_off_t		count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | 	xfs_filblks_t		allocated_fsb; | 
 | 2538 | 	xfs_filblks_t		allocatesize_fsb; | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2539 | 	xfs_extlen_t		extsz, temp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | 	xfs_fileoff_t		startoffset_fsb; | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2541 | 	xfs_fsblock_t		firstfsb; | 
 | 2542 | 	int			nimaps; | 
 | 2543 | 	int			bmapi_flag; | 
 | 2544 | 	int			quota_flag; | 
 | 2545 | 	int			rt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | 	xfs_trans_t		*tp; | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2547 | 	xfs_bmbt_irec_t		imaps[1], *imapp; | 
 | 2548 | 	xfs_bmap_free_t		free_list; | 
 | 2549 | 	uint			qblocks, resblks, resrtextents; | 
 | 2550 | 	int			committed; | 
 | 2551 | 	int			error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2553 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 |  | 
 | 2555 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 2556 | 		return XFS_ERROR(EIO); | 
 | 2557 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2558 | 	error = xfs_qm_dqattach(ip, 0); | 
 | 2559 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | 		return error; | 
 | 2561 |  | 
 | 2562 | 	if (len <= 0) | 
 | 2563 | 		return XFS_ERROR(EINVAL); | 
 | 2564 |  | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2565 | 	rt = XFS_IS_REALTIME_INODE(ip); | 
 | 2566 | 	extsz = xfs_get_extsz_hint(ip); | 
 | 2567 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | 	count = len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | 	imapp = &imaps[0]; | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2570 | 	nimaps = 1; | 
 | 2571 | 	bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | 	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset); | 
 | 2573 | 	allocatesize_fsb = XFS_B_TO_FSB(mp, count); | 
 | 2574 |  | 
 | 2575 | 	/*	Generate a DMAPI event if needed.	*/ | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 2576 | 	if (alloc_type != 0 && offset < ip->i_size && | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 2577 | 			(attr_flags & XFS_ATTR_DMI) == 0  && | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2578 | 			DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | 		xfs_off_t           end_dmi_offset; | 
 | 2580 |  | 
 | 2581 | 		end_dmi_offset = offset+len; | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 2582 | 		if (end_dmi_offset > ip->i_size) | 
 | 2583 | 			end_dmi_offset = ip->i_size; | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2584 | 		error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset, | 
 | 2585 | 				      end_dmi_offset - offset, 0, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | 		if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2587 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | 	} | 
 | 2589 |  | 
 | 2590 | 	/* | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2591 | 	 * Allocate file space until done or until there is an error | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | 	 */ | 
 | 2593 | retry: | 
 | 2594 | 	while (allocatesize_fsb && !error) { | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2595 | 		xfs_fileoff_t	s, e; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 |  | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2597 | 		/* | 
| Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 2598 | 		 * Determine space reservations for data/realtime. | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2599 | 		 */ | 
 | 2600 | 		if (unlikely(extsz)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | 			s = startoffset_fsb; | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2602 | 			do_div(s, extsz); | 
 | 2603 | 			s *= extsz; | 
 | 2604 | 			e = startoffset_fsb + allocatesize_fsb; | 
 | 2605 | 			if ((temp = do_mod(startoffset_fsb, extsz))) | 
 | 2606 | 				e += temp; | 
 | 2607 | 			if ((temp = do_mod(e, extsz))) | 
 | 2608 | 				e += extsz - temp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | 		} else { | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2610 | 			s = 0; | 
 | 2611 | 			e = allocatesize_fsb; | 
 | 2612 | 		} | 
 | 2613 |  | 
 | 2614 | 		if (unlikely(rt)) { | 
 | 2615 | 			resrtextents = qblocks = (uint)(e - s); | 
 | 2616 | 			resrtextents /= mp->m_sb.sb_rextsize; | 
 | 2617 | 			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | 
 | 2618 | 			quota_flag = XFS_QMOPT_RES_RTBLKS; | 
 | 2619 | 		} else { | 
 | 2620 | 			resrtextents = 0; | 
 | 2621 | 			resblks = qblocks = \ | 
 | 2622 | 				XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s)); | 
 | 2623 | 			quota_flag = XFS_QMOPT_RES_REGBLKS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | 		} | 
 | 2625 |  | 
 | 2626 | 		/* | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2627 | 		 * Allocate and setup the transaction. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | 		 */ | 
 | 2629 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2630 | 		error = xfs_trans_reserve(tp, resblks, | 
 | 2631 | 					  XFS_WRITE_LOG_RES(mp), resrtextents, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | 					  XFS_TRANS_PERM_LOG_RES, | 
 | 2633 | 					  XFS_WRITE_LOG_COUNT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | 		/* | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2635 | 		 * Check for running out of space | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | 		 */ | 
 | 2637 | 		if (error) { | 
 | 2638 | 			/* | 
 | 2639 | 			 * Free the transaction structure. | 
 | 2640 | 			 */ | 
 | 2641 | 			ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); | 
 | 2642 | 			xfs_trans_cancel(tp, 0); | 
 | 2643 | 			break; | 
 | 2644 | 		} | 
 | 2645 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2646 | 		error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, | 
 | 2647 | 						      0, quota_flag); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | 		if (error) | 
 | 2649 | 			goto error1; | 
 | 2650 |  | 
 | 2651 | 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
 | 2652 | 		xfs_trans_ihold(tp, ip); | 
 | 2653 |  | 
 | 2654 | 		/* | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2655 | 		 * Issue the xfs_bmapi() call to allocate the blocks | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | 		 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2657 | 		xfs_bmap_init(&free_list, &firstfsb); | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 2658 | 		error = xfs_bmapi(tp, ip, startoffset_fsb, | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2659 | 				  allocatesize_fsb, bmapi_flag, | 
 | 2660 | 				  &firstfsb, 0, imapp, &nimaps, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2661 | 				  &free_list, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | 		if (error) { | 
 | 2663 | 			goto error0; | 
 | 2664 | 		} | 
 | 2665 |  | 
 | 2666 | 		/* | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2667 | 		 * Complete the transaction | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | 		 */ | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2669 | 		error = xfs_bmap_finish(&tp, &free_list, &committed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | 		if (error) { | 
 | 2671 | 			goto error0; | 
 | 2672 | 		} | 
 | 2673 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2674 | 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | 		xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 2676 | 		if (error) { | 
 | 2677 | 			break; | 
 | 2678 | 		} | 
 | 2679 |  | 
 | 2680 | 		allocated_fsb = imapp->br_blockcount; | 
 | 2681 |  | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2682 | 		if (nimaps == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | 			error = XFS_ERROR(ENOSPC); | 
 | 2684 | 			break; | 
 | 2685 | 		} | 
 | 2686 |  | 
 | 2687 | 		startoffset_fsb += allocated_fsb; | 
 | 2688 | 		allocatesize_fsb -= allocated_fsb; | 
 | 2689 | 	} | 
 | 2690 | dmapi_enospc_check: | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 2691 | 	if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 && | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2692 | 	    DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | 		error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE, | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2694 | 				ip, DM_RIGHT_NULL, | 
 | 2695 | 				ip, DM_RIGHT_NULL, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | 				NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */ | 
 | 2697 | 		if (error == 0) | 
 | 2698 | 			goto retry;	/* Maybe DMAPI app. has made space */ | 
 | 2699 | 		/* else fall through with error from XFS_SEND_DATA */ | 
 | 2700 | 	} | 
 | 2701 |  | 
 | 2702 | 	return error; | 
 | 2703 |  | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2704 | error0:	/* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | 	xfs_bmap_cancel(&free_list); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2706 | 	xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2707 |  | 
 | 2708 | error1:	/* Just cancel transaction */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | 
 | 2710 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 2711 | 	goto dmapi_enospc_check; | 
 | 2712 | } | 
 | 2713 |  | 
 | 2714 | /* | 
 | 2715 |  * Zero file bytes between startoff and endoff inclusive. | 
 | 2716 |  * The iolock is held exclusive and no blocks are buffered. | 
| Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 2717 |  * | 
 | 2718 |  * This function is used by xfs_free_file_space() to zero | 
 | 2719 |  * partial blocks when the range to free is not block aligned. | 
 | 2720 |  * When unreserving space with boundaries that are not block | 
 | 2721 |  * aligned we round up the start and round down the end | 
 | 2722 |  * boundaries and then use this function to zero the parts of | 
 | 2723 |  * the blocks that got dropped during the rounding. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 |  */ | 
 | 2725 | STATIC int | 
 | 2726 | xfs_zero_remaining_bytes( | 
 | 2727 | 	xfs_inode_t		*ip, | 
 | 2728 | 	xfs_off_t		startoff, | 
 | 2729 | 	xfs_off_t		endoff) | 
 | 2730 | { | 
 | 2731 | 	xfs_bmbt_irec_t		imap; | 
 | 2732 | 	xfs_fileoff_t		offset_fsb; | 
 | 2733 | 	xfs_off_t		lastoffset; | 
 | 2734 | 	xfs_off_t		offset; | 
 | 2735 | 	xfs_buf_t		*bp; | 
 | 2736 | 	xfs_mount_t		*mp = ip->i_mount; | 
 | 2737 | 	int			nimap; | 
 | 2738 | 	int			error = 0; | 
 | 2739 |  | 
| Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 2740 | 	/* | 
 | 2741 | 	 * Avoid doing I/O beyond eof - it's not necessary | 
 | 2742 | 	 * since nothing can read beyond eof.  The space will | 
 | 2743 | 	 * be zeroed when the file is extended anyway. | 
 | 2744 | 	 */ | 
 | 2745 | 	if (startoff >= ip->i_size) | 
 | 2746 | 		return 0; | 
 | 2747 |  | 
 | 2748 | 	if (endoff > ip->i_size) | 
 | 2749 | 		endoff = ip->i_size; | 
 | 2750 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | 	bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize, | 
| Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2752 | 				XFS_IS_REALTIME_INODE(ip) ? | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | 				mp->m_rtdev_targp : mp->m_ddev_targp); | 
| Lachlan McIlroy | c642261 | 2008-12-05 13:16:15 +1100 | [diff] [blame] | 2754 | 	if (!bp) | 
 | 2755 | 		return XFS_ERROR(ENOMEM); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 |  | 
 | 2757 | 	for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { | 
 | 2758 | 		offset_fsb = XFS_B_TO_FSBT(mp, offset); | 
 | 2759 | 		nimap = 1; | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 2760 | 		error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2761 | 			NULL, 0, &imap, &nimap, NULL, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | 		if (error || nimap < 1) | 
 | 2763 | 			break; | 
 | 2764 | 		ASSERT(imap.br_blockcount >= 1); | 
 | 2765 | 		ASSERT(imap.br_startoff == offset_fsb); | 
 | 2766 | 		lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; | 
 | 2767 | 		if (lastoffset > endoff) | 
 | 2768 | 			lastoffset = endoff; | 
 | 2769 | 		if (imap.br_startblock == HOLESTARTBLOCK) | 
 | 2770 | 			continue; | 
 | 2771 | 		ASSERT(imap.br_startblock != DELAYSTARTBLOCK); | 
 | 2772 | 		if (imap.br_state == XFS_EXT_UNWRITTEN) | 
 | 2773 | 			continue; | 
 | 2774 | 		XFS_BUF_UNDONE(bp); | 
 | 2775 | 		XFS_BUF_UNWRITE(bp); | 
 | 2776 | 		XFS_BUF_READ(bp); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2777 | 		XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | 		xfsbdstrat(mp, bp); | 
| David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 2779 | 		error = xfs_iowait(bp); | 
 | 2780 | 		if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | 			xfs_ioerror_alert("xfs_zero_remaining_bytes(read)", | 
 | 2782 | 					  mp, bp, XFS_BUF_ADDR(bp)); | 
 | 2783 | 			break; | 
 | 2784 | 		} | 
 | 2785 | 		memset(XFS_BUF_PTR(bp) + | 
 | 2786 | 			(offset - XFS_FSB_TO_B(mp, imap.br_startoff)), | 
 | 2787 | 		      0, lastoffset - offset + 1); | 
 | 2788 | 		XFS_BUF_UNDONE(bp); | 
 | 2789 | 		XFS_BUF_UNREAD(bp); | 
 | 2790 | 		XFS_BUF_WRITE(bp); | 
 | 2791 | 		xfsbdstrat(mp, bp); | 
| David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 2792 | 		error = xfs_iowait(bp); | 
 | 2793 | 		if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | 			xfs_ioerror_alert("xfs_zero_remaining_bytes(write)", | 
 | 2795 | 					  mp, bp, XFS_BUF_ADDR(bp)); | 
 | 2796 | 			break; | 
 | 2797 | 		} | 
 | 2798 | 	} | 
 | 2799 | 	xfs_buf_free(bp); | 
 | 2800 | 	return error; | 
 | 2801 | } | 
 | 2802 |  | 
 | 2803 | /* | 
 | 2804 |  * xfs_free_file_space() | 
 | 2805 |  *      This routine frees disk space for the given file. | 
 | 2806 |  * | 
 | 2807 |  *	This routine is only called by xfs_change_file_space | 
 | 2808 |  *	for an UNRESVSP type call. | 
 | 2809 |  * | 
 | 2810 |  * RETURNS: | 
 | 2811 |  *       0 on success | 
 | 2812 |  *      errno on error | 
 | 2813 |  * | 
 | 2814 |  */ | 
 | 2815 | STATIC int | 
 | 2816 | xfs_free_file_space( | 
 | 2817 | 	xfs_inode_t		*ip, | 
 | 2818 | 	xfs_off_t		offset, | 
 | 2819 | 	xfs_off_t		len, | 
 | 2820 | 	int			attr_flags) | 
 | 2821 | { | 
 | 2822 | 	int			committed; | 
 | 2823 | 	int			done; | 
 | 2824 | 	xfs_off_t		end_dmi_offset; | 
 | 2825 | 	xfs_fileoff_t		endoffset_fsb; | 
 | 2826 | 	int			error; | 
 | 2827 | 	xfs_fsblock_t		firstfsb; | 
 | 2828 | 	xfs_bmap_free_t		free_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | 	xfs_bmbt_irec_t		imap; | 
 | 2830 | 	xfs_off_t		ioffset; | 
 | 2831 | 	xfs_extlen_t		mod=0; | 
 | 2832 | 	xfs_mount_t		*mp; | 
 | 2833 | 	int			nimap; | 
 | 2834 | 	uint			resblks; | 
| Nathan Scott | 673cdf5 | 2006-09-28 10:56:26 +1000 | [diff] [blame] | 2835 | 	uint			rounding; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | 	int			rt; | 
 | 2837 | 	xfs_fileoff_t		startoffset_fsb; | 
 | 2838 | 	xfs_trans_t		*tp; | 
| Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 2839 | 	int			need_iolock = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | 	mp = ip->i_mount; | 
 | 2842 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2843 | 	xfs_itrace_entry(ip); | 
| Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 2844 |  | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2845 | 	error = xfs_qm_dqattach(ip, 0); | 
 | 2846 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | 		return error; | 
 | 2848 |  | 
 | 2849 | 	error = 0; | 
 | 2850 | 	if (len <= 0)	/* if nothing being freed */ | 
 | 2851 | 		return error; | 
| Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2852 | 	rt = XFS_IS_REALTIME_INODE(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | 	startoffset_fsb	= XFS_B_TO_FSB(mp, offset); | 
 | 2854 | 	end_dmi_offset = offset + len; | 
 | 2855 | 	endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset); | 
 | 2856 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 2857 | 	if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 && | 
| Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2858 | 	    DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 2859 | 		if (end_dmi_offset > ip->i_size) | 
 | 2860 | 			end_dmi_offset = ip->i_size; | 
| Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2861 | 		error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | 				offset, end_dmi_offset - offset, | 
 | 2863 | 				AT_DELAY_FLAG(attr_flags), NULL); | 
 | 2864 | 		if (error) | 
| Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2865 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | 	} | 
 | 2867 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 2868 | 	if (attr_flags & XFS_ATTR_NOLOCK) | 
| Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 2869 | 		need_iolock = 0; | 
| Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 2870 | 	if (need_iolock) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | 		xfs_ilock(ip, XFS_IOLOCK_EXCL); | 
| Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 2872 | 		/* wait for the completion of any pending DIOs */ | 
 | 2873 | 		xfs_ioend_wait(ip); | 
| Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 2874 | 	} | 
| Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 2875 |  | 
| Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2876 | 	rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | 	ioffset = offset & ~(rounding - 1); | 
| Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 2878 |  | 
| Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 2879 | 	if (VN_CACHED(VFS_I(ip)) != 0) { | 
| Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2880 | 		xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1); | 
 | 2881 | 		error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED); | 
| Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 2882 | 		if (error) | 
 | 2883 | 			goto out_unlock_iolock; | 
| Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 2884 | 	} | 
 | 2885 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | 	/* | 
 | 2887 | 	 * Need to zero the stuff we're not freeing, on disk. | 
| Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2888 | 	 * If it's a realtime file & can't use unwritten extents then we | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | 	 * actually need to zero the extent edges.  Otherwise xfs_bunmapi | 
 | 2890 | 	 * will take care of it for us. | 
 | 2891 | 	 */ | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 2892 | 	if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | 		nimap = 1; | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 2894 | 		error = xfs_bmapi(NULL, ip, startoffset_fsb, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2895 | 			1, 0, NULL, 0, &imap, &nimap, NULL, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | 		if (error) | 
 | 2897 | 			goto out_unlock_iolock; | 
 | 2898 | 		ASSERT(nimap == 0 || nimap == 1); | 
 | 2899 | 		if (nimap && imap.br_startblock != HOLESTARTBLOCK) { | 
 | 2900 | 			xfs_daddr_t	block; | 
 | 2901 |  | 
 | 2902 | 			ASSERT(imap.br_startblock != DELAYSTARTBLOCK); | 
 | 2903 | 			block = imap.br_startblock; | 
 | 2904 | 			mod = do_div(block, mp->m_sb.sb_rextsize); | 
 | 2905 | 			if (mod) | 
 | 2906 | 				startoffset_fsb += mp->m_sb.sb_rextsize - mod; | 
 | 2907 | 		} | 
 | 2908 | 		nimap = 1; | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 2909 | 		error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2910 | 			1, 0, NULL, 0, &imap, &nimap, NULL, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | 		if (error) | 
 | 2912 | 			goto out_unlock_iolock; | 
 | 2913 | 		ASSERT(nimap == 0 || nimap == 1); | 
 | 2914 | 		if (nimap && imap.br_startblock != HOLESTARTBLOCK) { | 
 | 2915 | 			ASSERT(imap.br_startblock != DELAYSTARTBLOCK); | 
 | 2916 | 			mod++; | 
 | 2917 | 			if (mod && (mod != mp->m_sb.sb_rextsize)) | 
 | 2918 | 				endoffset_fsb -= mod; | 
 | 2919 | 		} | 
 | 2920 | 	} | 
 | 2921 | 	if ((done = (endoffset_fsb <= startoffset_fsb))) | 
 | 2922 | 		/* | 
 | 2923 | 		 * One contiguous piece to clear | 
 | 2924 | 		 */ | 
 | 2925 | 		error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); | 
 | 2926 | 	else { | 
 | 2927 | 		/* | 
 | 2928 | 		 * Some full blocks, possibly two pieces to clear | 
 | 2929 | 		 */ | 
 | 2930 | 		if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) | 
 | 2931 | 			error = xfs_zero_remaining_bytes(ip, offset, | 
 | 2932 | 				XFS_FSB_TO_B(mp, startoffset_fsb) - 1); | 
 | 2933 | 		if (!error && | 
 | 2934 | 		    XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) | 
 | 2935 | 			error = xfs_zero_remaining_bytes(ip, | 
 | 2936 | 				XFS_FSB_TO_B(mp, endoffset_fsb), | 
 | 2937 | 				offset + len - 1); | 
 | 2938 | 	} | 
 | 2939 |  | 
 | 2940 | 	/* | 
 | 2941 | 	 * free file space until done or until there is an error | 
 | 2942 | 	 */ | 
 | 2943 | 	resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | 
 | 2944 | 	while (!error && !done) { | 
 | 2945 |  | 
 | 2946 | 		/* | 
| David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 2947 | 		 * allocate and setup the transaction. Allow this | 
 | 2948 | 		 * transaction to dip into the reserve blocks to ensure | 
 | 2949 | 		 * the freeing of the space succeeds at ENOSPC. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | 		 */ | 
 | 2951 | 		tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); | 
| David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 2952 | 		tp->t_flags |= XFS_TRANS_RESERVE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | 		error = xfs_trans_reserve(tp, | 
 | 2954 | 					  resblks, | 
 | 2955 | 					  XFS_WRITE_LOG_RES(mp), | 
 | 2956 | 					  0, | 
 | 2957 | 					  XFS_TRANS_PERM_LOG_RES, | 
 | 2958 | 					  XFS_WRITE_LOG_COUNT); | 
 | 2959 |  | 
 | 2960 | 		/* | 
 | 2961 | 		 * check for running out of space | 
 | 2962 | 		 */ | 
 | 2963 | 		if (error) { | 
 | 2964 | 			/* | 
 | 2965 | 			 * Free the transaction structure. | 
 | 2966 | 			 */ | 
 | 2967 | 			ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); | 
 | 2968 | 			xfs_trans_cancel(tp, 0); | 
 | 2969 | 			break; | 
 | 2970 | 		} | 
 | 2971 | 		xfs_ilock(ip, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2972 | 		error = xfs_trans_reserve_quota(tp, mp, | 
 | 2973 | 				ip->i_udquot, ip->i_gdquot, | 
 | 2974 | 				resblks, 0, XFS_QMOPT_RES_REGBLKS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | 		if (error) | 
 | 2976 | 			goto error1; | 
 | 2977 |  | 
 | 2978 | 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
 | 2979 | 		xfs_trans_ihold(tp, ip); | 
 | 2980 |  | 
 | 2981 | 		/* | 
 | 2982 | 		 * issue the bunmapi() call to free the blocks | 
 | 2983 | 		 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2984 | 		xfs_bmap_init(&free_list, &firstfsb); | 
| Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 2985 | 		error = xfs_bunmapi(tp, ip, startoffset_fsb, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | 				  endoffset_fsb - startoffset_fsb, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2987 | 				  0, 2, &firstfsb, &free_list, NULL, &done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | 		if (error) { | 
 | 2989 | 			goto error0; | 
 | 2990 | 		} | 
 | 2991 |  | 
 | 2992 | 		/* | 
 | 2993 | 		 * complete the transaction | 
 | 2994 | 		 */ | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2995 | 		error = xfs_bmap_finish(&tp, &free_list, &committed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | 		if (error) { | 
 | 2997 | 			goto error0; | 
 | 2998 | 		} | 
 | 2999 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3000 | 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | 		xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 3002 | 	} | 
 | 3003 |  | 
 | 3004 |  out_unlock_iolock: | 
 | 3005 | 	if (need_iolock) | 
 | 3006 | 		xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
 | 3007 | 	return error; | 
 | 3008 |  | 
 | 3009 |  error0: | 
 | 3010 | 	xfs_bmap_cancel(&free_list); | 
 | 3011 |  error1: | 
 | 3012 | 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | 
 | 3013 | 	xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) : | 
 | 3014 | 		    XFS_ILOCK_EXCL); | 
 | 3015 | 	return error; | 
 | 3016 | } | 
 | 3017 |  | 
 | 3018 | /* | 
 | 3019 |  * xfs_change_file_space() | 
 | 3020 |  *      This routine allocates or frees disk space for the given file. | 
 | 3021 |  *      The user specified parameters are checked for alignment and size | 
 | 3022 |  *      limitations. | 
 | 3023 |  * | 
 | 3024 |  * RETURNS: | 
 | 3025 |  *       0 on success | 
 | 3026 |  *      errno on error | 
 | 3027 |  * | 
 | 3028 |  */ | 
 | 3029 | int | 
 | 3030 | xfs_change_file_space( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3031 | 	xfs_inode_t	*ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | 	int		cmd, | 
 | 3033 | 	xfs_flock64_t	*bf, | 
 | 3034 | 	xfs_off_t	offset, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | 	int		attr_flags) | 
 | 3036 | { | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3037 | 	xfs_mount_t	*mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | 	int		clrprealloc; | 
 | 3039 | 	int		error; | 
 | 3040 | 	xfs_fsize_t	fsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | 	int		setprealloc; | 
 | 3042 | 	xfs_off_t	startoffset; | 
 | 3043 | 	xfs_off_t	llen; | 
 | 3044 | 	xfs_trans_t	*tp; | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 3045 | 	struct iattr	iattr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 |  | 
| Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3047 | 	xfs_itrace_entry(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 |  | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3049 | 	if (!S_ISREG(ip->i_d.di_mode)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | 		return XFS_ERROR(EINVAL); | 
 | 3051 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | 	switch (bf->l_whence) { | 
 | 3053 | 	case 0: /*SEEK_SET*/ | 
 | 3054 | 		break; | 
 | 3055 | 	case 1: /*SEEK_CUR*/ | 
 | 3056 | 		bf->l_start += offset; | 
 | 3057 | 		break; | 
 | 3058 | 	case 2: /*SEEK_END*/ | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3059 | 		bf->l_start += ip->i_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | 		break; | 
 | 3061 | 	default: | 
 | 3062 | 		return XFS_ERROR(EINVAL); | 
 | 3063 | 	} | 
 | 3064 |  | 
 | 3065 | 	llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len; | 
 | 3066 |  | 
 | 3067 | 	if (   (bf->l_start < 0) | 
 | 3068 | 	    || (bf->l_start > XFS_MAXIOFFSET(mp)) | 
 | 3069 | 	    || (bf->l_start + llen < 0) | 
 | 3070 | 	    || (bf->l_start + llen > XFS_MAXIOFFSET(mp))) | 
 | 3071 | 		return XFS_ERROR(EINVAL); | 
 | 3072 |  | 
 | 3073 | 	bf->l_whence = 0; | 
 | 3074 |  | 
 | 3075 | 	startoffset = bf->l_start; | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3076 | 	fsize = ip->i_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 |  | 
 | 3078 | 	/* | 
 | 3079 | 	 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve | 
 | 3080 | 	 * file space. | 
 | 3081 | 	 * These calls do NOT zero the data space allocated to the file, | 
 | 3082 | 	 * nor do they change the file size. | 
 | 3083 | 	 * | 
 | 3084 | 	 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file | 
 | 3085 | 	 * space. | 
 | 3086 | 	 * These calls cause the new file data to be zeroed and the file | 
 | 3087 | 	 * size to be changed. | 
 | 3088 | 	 */ | 
 | 3089 | 	setprealloc = clrprealloc = 0; | 
 | 3090 |  | 
 | 3091 | 	switch (cmd) { | 
 | 3092 | 	case XFS_IOC_RESVSP: | 
 | 3093 | 	case XFS_IOC_RESVSP64: | 
 | 3094 | 		error = xfs_alloc_file_space(ip, startoffset, bf->l_len, | 
 | 3095 | 								1, attr_flags); | 
 | 3096 | 		if (error) | 
 | 3097 | 			return error; | 
 | 3098 | 		setprealloc = 1; | 
 | 3099 | 		break; | 
 | 3100 |  | 
 | 3101 | 	case XFS_IOC_UNRESVSP: | 
 | 3102 | 	case XFS_IOC_UNRESVSP64: | 
 | 3103 | 		if ((error = xfs_free_file_space(ip, startoffset, bf->l_len, | 
 | 3104 | 								attr_flags))) | 
 | 3105 | 			return error; | 
 | 3106 | 		break; | 
 | 3107 |  | 
 | 3108 | 	case XFS_IOC_ALLOCSP: | 
 | 3109 | 	case XFS_IOC_ALLOCSP64: | 
 | 3110 | 	case XFS_IOC_FREESP: | 
 | 3111 | 	case XFS_IOC_FREESP64: | 
 | 3112 | 		if (startoffset > fsize) { | 
 | 3113 | 			error = xfs_alloc_file_space(ip, fsize, | 
 | 3114 | 					startoffset - fsize, 0, attr_flags); | 
 | 3115 | 			if (error) | 
 | 3116 | 				break; | 
 | 3117 | 		} | 
 | 3118 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 3119 | 		iattr.ia_valid = ATTR_SIZE; | 
 | 3120 | 		iattr.ia_size = startoffset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 |  | 
| Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 3122 | 		error = xfs_setattr(ip, &iattr, attr_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 |  | 
 | 3124 | 		if (error) | 
 | 3125 | 			return error; | 
 | 3126 |  | 
 | 3127 | 		clrprealloc = 1; | 
 | 3128 | 		break; | 
 | 3129 |  | 
 | 3130 | 	default: | 
 | 3131 | 		ASSERT(0); | 
 | 3132 | 		return XFS_ERROR(EINVAL); | 
 | 3133 | 	} | 
 | 3134 |  | 
 | 3135 | 	/* | 
 | 3136 | 	 * update the inode timestamp, mode, and prealloc flag bits | 
 | 3137 | 	 */ | 
 | 3138 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID); | 
 | 3139 |  | 
 | 3140 | 	if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp), | 
 | 3141 | 				      0, 0, 0))) { | 
 | 3142 | 		/* ASSERT(0); */ | 
 | 3143 | 		xfs_trans_cancel(tp, 0); | 
 | 3144 | 		return error; | 
 | 3145 | 	} | 
 | 3146 |  | 
 | 3147 | 	xfs_ilock(ip, XFS_ILOCK_EXCL); | 
 | 3148 |  | 
 | 3149 | 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | 
 | 3150 | 	xfs_trans_ihold(tp, ip); | 
 | 3151 |  | 
| Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 3152 | 	if ((attr_flags & XFS_ATTR_DMI) == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | 		ip->i_d.di_mode &= ~S_ISUID; | 
 | 3154 |  | 
 | 3155 | 		/* | 
 | 3156 | 		 * Note that we don't have to worry about mandatory | 
 | 3157 | 		 * file locking being disabled here because we only | 
 | 3158 | 		 * clear the S_ISGID bit if the Group execute bit is | 
 | 3159 | 		 * on, but if it was on then mandatory locking wouldn't | 
 | 3160 | 		 * have been enabled. | 
 | 3161 | 		 */ | 
 | 3162 | 		if (ip->i_d.di_mode & S_IXGRP) | 
 | 3163 | 			ip->i_d.di_mode &= ~S_ISGID; | 
 | 3164 |  | 
 | 3165 | 		xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | 
 | 3166 | 	} | 
 | 3167 | 	if (setprealloc) | 
 | 3168 | 		ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; | 
 | 3169 | 	else if (clrprealloc) | 
 | 3170 | 		ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; | 
 | 3171 |  | 
 | 3172 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 3173 | 	xfs_trans_set_sync(tp); | 
 | 3174 |  | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3175 | 	error = xfs_trans_commit(tp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 |  | 
 | 3177 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 3178 |  | 
 | 3179 | 	return error; | 
 | 3180 | } |