| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 |  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. | 
 | 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 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
 | 25 | #include "xfs_sb.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_mount.h" | 
 | 29 | #include "xfs_bmap_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_dinode.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_inode_item.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_bmap.h" | 
 | 34 | #include "xfs_error.h" | 
 | 35 | #include "xfs_quota.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_itable.h" | 
 | 37 | #include "xfs_utils.h" | 
 | 38 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* | 
 | 41 |  * Allocates a new inode from disk and return a pointer to the | 
 | 42 |  * incore copy. This routine will internally commit the current | 
 | 43 |  * transaction and allocate a new one if the Space Manager needed | 
 | 44 |  * to do an allocation to replenish the inode free-list. | 
 | 45 |  * | 
 | 46 |  * This routine is designed to be called from xfs_create and | 
 | 47 |  * xfs_create_dir. | 
 | 48 |  * | 
 | 49 |  */ | 
 | 50 | int | 
 | 51 | xfs_dir_ialloc( | 
 | 52 | 	xfs_trans_t	**tpp,		/* input: current transaction; | 
 | 53 | 					   output: may be a new transaction. */ | 
 | 54 | 	xfs_inode_t	*dp,		/* directory within whose allocate | 
 | 55 | 					   the inode. */ | 
 | 56 | 	mode_t		mode, | 
| Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 57 | 	xfs_nlink_t	nlink, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 	xfs_dev_t	rdev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 	prid_t		prid,		/* project id */ | 
 | 60 | 	int		okalloc,	/* ok to allocate new space */ | 
 | 61 | 	xfs_inode_t	**ipp,		/* pointer to inode; it will be | 
 | 62 | 					   locked. */ | 
 | 63 | 	int		*committed) | 
 | 64 |  | 
 | 65 | { | 
 | 66 | 	xfs_trans_t	*tp; | 
 | 67 | 	xfs_trans_t	*ntp; | 
 | 68 | 	xfs_inode_t	*ip; | 
 | 69 | 	xfs_buf_t	*ialloc_context = NULL; | 
 | 70 | 	boolean_t	call_again = B_FALSE; | 
 | 71 | 	int		code; | 
 | 72 | 	uint		log_res; | 
 | 73 | 	uint		log_count; | 
 | 74 | 	void		*dqinfo; | 
 | 75 | 	uint		tflags; | 
 | 76 |  | 
 | 77 | 	tp = *tpp; | 
 | 78 | 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); | 
 | 79 |  | 
 | 80 | 	/* | 
 | 81 | 	 * xfs_ialloc will return a pointer to an incore inode if | 
 | 82 | 	 * the Space Manager has an available inode on the free | 
 | 83 | 	 * list. Otherwise, it will do an allocation and replenish | 
 | 84 | 	 * the freelist.  Since we can only do one allocation per | 
 | 85 | 	 * transaction without deadlocks, we will need to commit the | 
 | 86 | 	 * current transaction and start a new one.  We will then | 
 | 87 | 	 * need to call xfs_ialloc again to get the inode. | 
 | 88 | 	 * | 
 | 89 | 	 * If xfs_ialloc did an allocation to replenish the freelist, | 
 | 90 | 	 * it returns the bp containing the head of the freelist as | 
 | 91 | 	 * ialloc_context. We will hold a lock on it across the | 
 | 92 | 	 * transaction commit so that no other process can steal | 
 | 93 | 	 * the inode(s) that we've just allocated. | 
 | 94 | 	 */ | 
| Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 95 | 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | 			  &ialloc_context, &call_again, &ip); | 
 | 97 |  | 
 | 98 | 	/* | 
 | 99 | 	 * Return an error if we were unable to allocate a new inode. | 
 | 100 | 	 * This should only happen if we run out of space on disk or | 
 | 101 | 	 * encounter a disk error. | 
 | 102 | 	 */ | 
 | 103 | 	if (code) { | 
 | 104 | 		*ipp = NULL; | 
 | 105 | 		return code; | 
 | 106 | 	} | 
 | 107 | 	if (!call_again && (ip == NULL)) { | 
 | 108 | 		*ipp = NULL; | 
 | 109 | 		return XFS_ERROR(ENOSPC); | 
 | 110 | 	} | 
 | 111 |  | 
 | 112 | 	/* | 
 | 113 | 	 * If call_again is set, then we were unable to get an | 
 | 114 | 	 * inode in one operation.  We need to commit the current | 
 | 115 | 	 * transaction and call xfs_ialloc() again.  It is guaranteed | 
 | 116 | 	 * to succeed the second time. | 
 | 117 | 	 */ | 
 | 118 | 	if (call_again) { | 
 | 119 |  | 
 | 120 | 		/* | 
 | 121 | 		 * Normally, xfs_trans_commit releases all the locks. | 
 | 122 | 		 * We call bhold to hang on to the ialloc_context across | 
 | 123 | 		 * the commit.  Holding this buffer prevents any other | 
 | 124 | 		 * processes from doing any allocations in this | 
 | 125 | 		 * allocation group. | 
 | 126 | 		 */ | 
 | 127 | 		xfs_trans_bhold(tp, ialloc_context); | 
 | 128 | 		/* | 
 | 129 | 		 * Save the log reservation so we can use | 
 | 130 | 		 * them in the next transaction. | 
 | 131 | 		 */ | 
 | 132 | 		log_res = xfs_trans_get_log_res(tp); | 
 | 133 | 		log_count = xfs_trans_get_log_count(tp); | 
 | 134 |  | 
 | 135 | 		/* | 
 | 136 | 		 * We want the quota changes to be associated with the next | 
 | 137 | 		 * transaction, NOT this one. So, detach the dqinfo from this | 
 | 138 | 		 * and attach it to the next transaction. | 
 | 139 | 		 */ | 
 | 140 | 		dqinfo = NULL; | 
 | 141 | 		tflags = 0; | 
 | 142 | 		if (tp->t_dqinfo) { | 
 | 143 | 			dqinfo = (void *)tp->t_dqinfo; | 
 | 144 | 			tp->t_dqinfo = NULL; | 
 | 145 | 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; | 
 | 146 | 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); | 
 | 147 | 		} | 
 | 148 |  | 
 | 149 | 		ntp = xfs_trans_dup(tp); | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 150 | 		code = xfs_trans_commit(tp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 		tp = ntp; | 
 | 152 | 		if (committed != NULL) { | 
 | 153 | 			*committed = 1; | 
 | 154 | 		} | 
 | 155 | 		/* | 
 | 156 | 		 * If we get an error during the commit processing, | 
 | 157 | 		 * release the buffer that is still held and return | 
 | 158 | 		 * to the caller. | 
 | 159 | 		 */ | 
 | 160 | 		if (code) { | 
 | 161 | 			xfs_buf_relse(ialloc_context); | 
 | 162 | 			if (dqinfo) { | 
 | 163 | 				tp->t_dqinfo = dqinfo; | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 164 | 				xfs_trans_free_dqinfo(tp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 			} | 
 | 166 | 			*tpp = ntp; | 
 | 167 | 			*ipp = NULL; | 
 | 168 | 			return code; | 
 | 169 | 		} | 
| Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 170 |  | 
 | 171 | 		/* | 
 | 172 | 		 * transaction commit worked ok so we can drop the extra ticket | 
 | 173 | 		 * reference that we gained in xfs_trans_dup() | 
 | 174 | 		 */ | 
 | 175 | 		xfs_log_ticket_put(tp->t_ticket); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 		code = xfs_trans_reserve(tp, 0, log_res, 0, | 
 | 177 | 					 XFS_TRANS_PERM_LOG_RES, log_count); | 
 | 178 | 		/* | 
 | 179 | 		 * Re-attach the quota info that we detached from prev trx. | 
 | 180 | 		 */ | 
 | 181 | 		if (dqinfo) { | 
 | 182 | 			tp->t_dqinfo = dqinfo; | 
 | 183 | 			tp->t_flags |= tflags; | 
 | 184 | 		} | 
 | 185 |  | 
 | 186 | 		if (code) { | 
 | 187 | 			xfs_buf_relse(ialloc_context); | 
 | 188 | 			*tpp = ntp; | 
 | 189 | 			*ipp = NULL; | 
 | 190 | 			return code; | 
 | 191 | 		} | 
 | 192 | 		xfs_trans_bjoin(tp, ialloc_context); | 
 | 193 |  | 
 | 194 | 		/* | 
 | 195 | 		 * Call ialloc again. Since we've locked out all | 
 | 196 | 		 * other allocations in this allocation group, | 
 | 197 | 		 * this call should always succeed. | 
 | 198 | 		 */ | 
| Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 199 | 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | 				  okalloc, &ialloc_context, &call_again, &ip); | 
 | 201 |  | 
 | 202 | 		/* | 
 | 203 | 		 * If we get an error at this point, return to the caller | 
 | 204 | 		 * so that the current transaction can be aborted. | 
 | 205 | 		 */ | 
 | 206 | 		if (code) { | 
 | 207 | 			*tpp = tp; | 
 | 208 | 			*ipp = NULL; | 
 | 209 | 			return code; | 
 | 210 | 		} | 
 | 211 | 		ASSERT ((!call_again) && (ip != NULL)); | 
 | 212 |  | 
 | 213 | 	} else { | 
 | 214 | 		if (committed != NULL) { | 
 | 215 | 			*committed = 0; | 
 | 216 | 		} | 
 | 217 | 	} | 
 | 218 |  | 
 | 219 | 	*ipp = ip; | 
 | 220 | 	*tpp = tp; | 
 | 221 |  | 
 | 222 | 	return 0; | 
 | 223 | } | 
 | 224 |  | 
 | 225 | /* | 
 | 226 |  * Decrement the link count on an inode & log the change. | 
 | 227 |  * If this causes the link count to go to zero, initiate the | 
 | 228 |  * logging activity required to truncate a file. | 
 | 229 |  */ | 
 | 230 | int				/* error */ | 
 | 231 | xfs_droplink( | 
 | 232 | 	xfs_trans_t *tp, | 
 | 233 | 	xfs_inode_t *ip) | 
 | 234 | { | 
 | 235 | 	int	error; | 
 | 236 |  | 
| Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 237 | 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 |  | 
 | 239 | 	ASSERT (ip->i_d.di_nlink > 0); | 
 | 240 | 	ip->i_d.di_nlink--; | 
| David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 241 | 	drop_nlink(VFS_I(ip)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 243 |  | 
 | 244 | 	error = 0; | 
 | 245 | 	if (ip->i_d.di_nlink == 0) { | 
 | 246 | 		/* | 
 | 247 | 		 * We're dropping the last link to this file. | 
 | 248 | 		 * Move the on-disk inode to the AGI unlinked list. | 
 | 249 | 		 * From xfs_inactive() we will pull the inode from | 
 | 250 | 		 * the list and free it. | 
 | 251 | 		 */ | 
 | 252 | 		error = xfs_iunlink(tp, ip); | 
 | 253 | 	} | 
 | 254 | 	return error; | 
 | 255 | } | 
 | 256 |  | 
 | 257 | /* | 
 | 258 |  * This gets called when the inode's version needs to be changed from 1 to 2. | 
 | 259 |  * Currently this happens when the nlink field overflows the old 16-bit value | 
 | 260 |  * or when chproj is called to change the project for the first time. | 
 | 261 |  * As a side effect the superblock version will also get rev'd | 
 | 262 |  * to contain the NLINK bit. | 
 | 263 |  */ | 
 | 264 | void | 
 | 265 | xfs_bump_ino_vers2( | 
 | 266 | 	xfs_trans_t	*tp, | 
 | 267 | 	xfs_inode_t	*ip) | 
 | 268 | { | 
 | 269 | 	xfs_mount_t	*mp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 |  | 
| Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 271 | 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | 
| Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 272 | 	ASSERT(ip->i_d.di_version == 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 |  | 
| Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 274 | 	ip->i_d.di_version = 2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | 	ip->i_d.di_onlink = 0; | 
 | 276 | 	memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); | 
 | 277 | 	mp = tp->t_mountp; | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 278 | 	if (!xfs_sb_version_hasnlink(&mp->m_sb)) { | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 279 | 		spin_lock(&mp->m_sb_lock); | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 280 | 		if (!xfs_sb_version_hasnlink(&mp->m_sb)) { | 
 | 281 | 			xfs_sb_version_addnlink(&mp->m_sb); | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 282 | 			spin_unlock(&mp->m_sb_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 			xfs_mod_sb(tp, XFS_SB_VERSIONNUM); | 
 | 284 | 		} else { | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 285 | 			spin_unlock(&mp->m_sb_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 		} | 
 | 287 | 	} | 
 | 288 | 	/* Caller must log the inode */ | 
 | 289 | } | 
 | 290 |  | 
 | 291 | /* | 
 | 292 |  * Increment the link count on an inode & log the change. | 
 | 293 |  */ | 
 | 294 | int | 
 | 295 | xfs_bumplink( | 
 | 296 | 	xfs_trans_t *tp, | 
 | 297 | 	xfs_inode_t *ip) | 
 | 298 | { | 
 | 299 | 	if (ip->i_d.di_nlink >= XFS_MAXLINK) | 
 | 300 | 		return XFS_ERROR(EMLINK); | 
| Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 301 | 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  | 
 | 303 | 	ASSERT(ip->i_d.di_nlink > 0); | 
 | 304 | 	ip->i_d.di_nlink++; | 
| David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 305 | 	inc_nlink(VFS_I(ip)); | 
| Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 306 | 	if ((ip->i_d.di_version == 1) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 	    (ip->i_d.di_nlink > XFS_MAXLINK_1)) { | 
 | 308 | 		/* | 
 | 309 | 		 * The inode has increased its number of links beyond | 
 | 310 | 		 * what can fit in an old format inode.  It now needs | 
 | 311 | 		 * to be converted to a version 2 inode with a 32 bit | 
 | 312 | 		 * link count.  If this is the first inode in the file | 
 | 313 | 		 * system to do this, then we need to bump the superblock | 
 | 314 | 		 * version number as well. | 
 | 315 | 		 */ | 
 | 316 | 		xfs_bump_ino_vers2(tp, ip); | 
 | 317 | 	} | 
 | 318 |  | 
 | 319 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
 | 320 | 	return 0; | 
 | 321 | } |