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