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