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" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
| 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_bmap.h" |
| 37 | #include "xfs_error.h" |
| 38 | #include "xfs_quota.h" |
| 39 | #include "xfs_rw.h" |
| 40 | #include "xfs_itable.h" |
| 41 | #include "xfs_utils.h" |
| 42 | |
| 43 | /* |
| 44 | * xfs_get_dir_entry is used to get a reference to an inode given |
| 45 | * its parent directory inode and the name of the file. It does |
| 46 | * not lock the child inode, and it unlocks the directory before |
| 47 | * returning. The directory's generation number is returned for |
| 48 | * use by a later call to xfs_lock_dir_and_entry. |
| 49 | */ |
| 50 | int |
| 51 | xfs_get_dir_entry( |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 52 | bhv_vname_t *dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | xfs_inode_t **ipp) |
| 54 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 55 | bhv_vnode_t *vp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | vp = VNAME_TO_VNODE(dentry); |
Christoph Hellwig | 75e17b3 | 2006-01-11 20:58:44 +1100 | [diff] [blame] | 58 | |
| 59 | *ipp = xfs_vtoi(vp); |
| 60 | if (!*ipp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return XFS_ERROR(ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | VN_HOLD(vp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | int |
| 67 | xfs_dir_lookup_int( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 68 | xfs_inode_t *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | uint lock_mode, |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 70 | bhv_vname_t *dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | xfs_ino_t *inum, |
| 72 | xfs_inode_t **ipp) |
| 73 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int error; |
| 75 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 76 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 78 | error = xfs_dir_lookup(NULL, dp, VNAME(dentry), VNAMELEN(dentry), inum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (!error) { |
| 80 | /* |
| 81 | * Unlock the directory. We do this because we can't |
| 82 | * hold the directory lock while doing the vn_get() |
| 83 | * in xfs_iget(). Doing so could cause us to hold |
| 84 | * a lock while waiting for the inode to finish |
| 85 | * being inactive while it's waiting for a log |
| 86 | * reservation in the inactive routine. |
| 87 | */ |
| 88 | xfs_iunlock(dp, lock_mode); |
| 89 | error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0); |
| 90 | xfs_ilock(dp, lock_mode); |
| 91 | |
| 92 | if (error) { |
| 93 | *ipp = NULL; |
| 94 | } else if ((*ipp)->i_d.di_mode == 0) { |
| 95 | /* |
| 96 | * The inode has been freed. Something is |
| 97 | * wrong so just get out of here. |
| 98 | */ |
| 99 | xfs_iunlock(dp, lock_mode); |
| 100 | xfs_iput_new(*ipp, 0); |
| 101 | *ipp = NULL; |
| 102 | xfs_ilock(dp, lock_mode); |
| 103 | error = XFS_ERROR(ENOENT); |
| 104 | } |
| 105 | } |
| 106 | return error; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Allocates a new inode from disk and return a pointer to the |
| 111 | * incore copy. This routine will internally commit the current |
| 112 | * transaction and allocate a new one if the Space Manager needed |
| 113 | * to do an allocation to replenish the inode free-list. |
| 114 | * |
| 115 | * This routine is designed to be called from xfs_create and |
| 116 | * xfs_create_dir. |
| 117 | * |
| 118 | */ |
| 119 | int |
| 120 | xfs_dir_ialloc( |
| 121 | xfs_trans_t **tpp, /* input: current transaction; |
| 122 | output: may be a new transaction. */ |
| 123 | xfs_inode_t *dp, /* directory within whose allocate |
| 124 | the inode. */ |
| 125 | mode_t mode, |
Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 126 | xfs_nlink_t nlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | xfs_dev_t rdev, |
| 128 | cred_t *credp, |
| 129 | prid_t prid, /* project id */ |
| 130 | int okalloc, /* ok to allocate new space */ |
| 131 | xfs_inode_t **ipp, /* pointer to inode; it will be |
| 132 | locked. */ |
| 133 | int *committed) |
| 134 | |
| 135 | { |
| 136 | xfs_trans_t *tp; |
| 137 | xfs_trans_t *ntp; |
| 138 | xfs_inode_t *ip; |
| 139 | xfs_buf_t *ialloc_context = NULL; |
| 140 | boolean_t call_again = B_FALSE; |
| 141 | int code; |
| 142 | uint log_res; |
| 143 | uint log_count; |
| 144 | void *dqinfo; |
| 145 | uint tflags; |
| 146 | |
| 147 | tp = *tpp; |
| 148 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 149 | |
| 150 | /* |
| 151 | * xfs_ialloc will return a pointer to an incore inode if |
| 152 | * the Space Manager has an available inode on the free |
| 153 | * list. Otherwise, it will do an allocation and replenish |
| 154 | * the freelist. Since we can only do one allocation per |
| 155 | * transaction without deadlocks, we will need to commit the |
| 156 | * current transaction and start a new one. We will then |
| 157 | * need to call xfs_ialloc again to get the inode. |
| 158 | * |
| 159 | * If xfs_ialloc did an allocation to replenish the freelist, |
| 160 | * it returns the bp containing the head of the freelist as |
| 161 | * ialloc_context. We will hold a lock on it across the |
| 162 | * transaction commit so that no other process can steal |
| 163 | * the inode(s) that we've just allocated. |
| 164 | */ |
| 165 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc, |
| 166 | &ialloc_context, &call_again, &ip); |
| 167 | |
| 168 | /* |
| 169 | * Return an error if we were unable to allocate a new inode. |
| 170 | * This should only happen if we run out of space on disk or |
| 171 | * encounter a disk error. |
| 172 | */ |
| 173 | if (code) { |
| 174 | *ipp = NULL; |
| 175 | return code; |
| 176 | } |
| 177 | if (!call_again && (ip == NULL)) { |
| 178 | *ipp = NULL; |
| 179 | return XFS_ERROR(ENOSPC); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * If call_again is set, then we were unable to get an |
| 184 | * inode in one operation. We need to commit the current |
| 185 | * transaction and call xfs_ialloc() again. It is guaranteed |
| 186 | * to succeed the second time. |
| 187 | */ |
| 188 | if (call_again) { |
| 189 | |
| 190 | /* |
| 191 | * Normally, xfs_trans_commit releases all the locks. |
| 192 | * We call bhold to hang on to the ialloc_context across |
| 193 | * the commit. Holding this buffer prevents any other |
| 194 | * processes from doing any allocations in this |
| 195 | * allocation group. |
| 196 | */ |
| 197 | xfs_trans_bhold(tp, ialloc_context); |
| 198 | /* |
| 199 | * Save the log reservation so we can use |
| 200 | * them in the next transaction. |
| 201 | */ |
| 202 | log_res = xfs_trans_get_log_res(tp); |
| 203 | log_count = xfs_trans_get_log_count(tp); |
| 204 | |
| 205 | /* |
| 206 | * We want the quota changes to be associated with the next |
| 207 | * transaction, NOT this one. So, detach the dqinfo from this |
| 208 | * and attach it to the next transaction. |
| 209 | */ |
| 210 | dqinfo = NULL; |
| 211 | tflags = 0; |
| 212 | if (tp->t_dqinfo) { |
| 213 | dqinfo = (void *)tp->t_dqinfo; |
| 214 | tp->t_dqinfo = NULL; |
| 215 | tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; |
| 216 | tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); |
| 217 | } |
| 218 | |
| 219 | ntp = xfs_trans_dup(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 220 | code = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | tp = ntp; |
| 222 | if (committed != NULL) { |
| 223 | *committed = 1; |
| 224 | } |
| 225 | /* |
| 226 | * If we get an error during the commit processing, |
| 227 | * release the buffer that is still held and return |
| 228 | * to the caller. |
| 229 | */ |
| 230 | if (code) { |
| 231 | xfs_buf_relse(ialloc_context); |
| 232 | if (dqinfo) { |
| 233 | tp->t_dqinfo = dqinfo; |
| 234 | XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp); |
| 235 | } |
| 236 | *tpp = ntp; |
| 237 | *ipp = NULL; |
| 238 | return code; |
| 239 | } |
| 240 | code = xfs_trans_reserve(tp, 0, log_res, 0, |
| 241 | XFS_TRANS_PERM_LOG_RES, log_count); |
| 242 | /* |
| 243 | * Re-attach the quota info that we detached from prev trx. |
| 244 | */ |
| 245 | if (dqinfo) { |
| 246 | tp->t_dqinfo = dqinfo; |
| 247 | tp->t_flags |= tflags; |
| 248 | } |
| 249 | |
| 250 | if (code) { |
| 251 | xfs_buf_relse(ialloc_context); |
| 252 | *tpp = ntp; |
| 253 | *ipp = NULL; |
| 254 | return code; |
| 255 | } |
| 256 | xfs_trans_bjoin(tp, ialloc_context); |
| 257 | |
| 258 | /* |
| 259 | * Call ialloc again. Since we've locked out all |
| 260 | * other allocations in this allocation group, |
| 261 | * this call should always succeed. |
| 262 | */ |
| 263 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, |
| 264 | okalloc, &ialloc_context, &call_again, &ip); |
| 265 | |
| 266 | /* |
| 267 | * If we get an error at this point, return to the caller |
| 268 | * so that the current transaction can be aborted. |
| 269 | */ |
| 270 | if (code) { |
| 271 | *tpp = tp; |
| 272 | *ipp = NULL; |
| 273 | return code; |
| 274 | } |
| 275 | ASSERT ((!call_again) && (ip != NULL)); |
| 276 | |
| 277 | } else { |
| 278 | if (committed != NULL) { |
| 279 | *committed = 0; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | *ipp = ip; |
| 284 | *tpp = tp; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Decrement the link count on an inode & log the change. |
| 291 | * If this causes the link count to go to zero, initiate the |
| 292 | * logging activity required to truncate a file. |
| 293 | */ |
| 294 | int /* error */ |
| 295 | xfs_droplink( |
| 296 | xfs_trans_t *tp, |
| 297 | xfs_inode_t *ip) |
| 298 | { |
| 299 | int error; |
| 300 | |
| 301 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); |
| 302 | |
| 303 | ASSERT (ip->i_d.di_nlink > 0); |
| 304 | ip->i_d.di_nlink--; |
Christoph Hellwig | 4188c78 | 2008-02-05 12:13:53 +1100 | [diff] [blame] | 305 | drop_nlink(ip->i_vnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 307 | |
| 308 | error = 0; |
| 309 | if (ip->i_d.di_nlink == 0) { |
| 310 | /* |
| 311 | * We're dropping the last link to this file. |
| 312 | * Move the on-disk inode to the AGI unlinked list. |
| 313 | * From xfs_inactive() we will pull the inode from |
| 314 | * the list and free it. |
| 315 | */ |
| 316 | error = xfs_iunlink(tp, ip); |
| 317 | } |
| 318 | return error; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * This gets called when the inode's version needs to be changed from 1 to 2. |
| 323 | * Currently this happens when the nlink field overflows the old 16-bit value |
| 324 | * or when chproj is called to change the project for the first time. |
| 325 | * As a side effect the superblock version will also get rev'd |
| 326 | * to contain the NLINK bit. |
| 327 | */ |
| 328 | void |
| 329 | xfs_bump_ino_vers2( |
| 330 | xfs_trans_t *tp, |
| 331 | xfs_inode_t *ip) |
| 332 | { |
| 333 | xfs_mount_t *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE)); |
| 336 | ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1); |
| 337 | |
| 338 | ip->i_d.di_version = XFS_DINODE_VERSION_2; |
| 339 | ip->i_d.di_onlink = 0; |
| 340 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 341 | mp = tp->t_mountp; |
| 342 | if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 343 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) { |
| 345 | XFS_SB_VERSION_ADDNLINK(&mp->m_sb); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 346 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | xfs_mod_sb(tp, XFS_SB_VERSIONNUM); |
| 348 | } else { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 349 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | /* Caller must log the inode */ |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Increment the link count on an inode & log the change. |
| 357 | */ |
| 358 | int |
| 359 | xfs_bumplink( |
| 360 | xfs_trans_t *tp, |
| 361 | xfs_inode_t *ip) |
| 362 | { |
| 363 | if (ip->i_d.di_nlink >= XFS_MAXLINK) |
| 364 | return XFS_ERROR(EMLINK); |
| 365 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); |
| 366 | |
| 367 | ASSERT(ip->i_d.di_nlink > 0); |
| 368 | ip->i_d.di_nlink++; |
Christoph Hellwig | 4188c78 | 2008-02-05 12:13:53 +1100 | [diff] [blame] | 369 | inc_nlink(ip->i_vnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) && |
| 371 | (ip->i_d.di_nlink > XFS_MAXLINK_1)) { |
| 372 | /* |
| 373 | * The inode has increased its number of links beyond |
| 374 | * what can fit in an old format inode. It now needs |
| 375 | * to be converted to a version 2 inode with a 32 bit |
| 376 | * link count. If this is the first inode in the file |
| 377 | * system to do this, then we need to bump the superblock |
| 378 | * version number as well. |
| 379 | */ |
| 380 | xfs_bump_ino_vers2(tp, ip); |
| 381 | } |
| 382 | |
| 383 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Try to truncate the given file to 0 length. Currently called |
| 389 | * only out of xfs_remove when it has to truncate a file to free |
| 390 | * up space for the remove to proceed. |
| 391 | */ |
| 392 | int |
| 393 | xfs_truncate_file( |
| 394 | xfs_mount_t *mp, |
| 395 | xfs_inode_t *ip) |
| 396 | { |
| 397 | xfs_trans_t *tp; |
| 398 | int error; |
| 399 | |
| 400 | #ifdef QUOTADEBUG |
| 401 | /* |
| 402 | * This is called to truncate the quotainodes too. |
| 403 | */ |
| 404 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 405 | if (ip->i_ino != mp->m_sb.sb_uquotino) |
| 406 | ASSERT(ip->i_udquot); |
| 407 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 408 | if (XFS_IS_OQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (ip->i_ino != mp->m_sb.sb_gquotino) |
| 410 | ASSERT(ip->i_gdquot); |
| 411 | } |
| 412 | #endif |
| 413 | /* |
| 414 | * Make the call to xfs_itruncate_start before starting the |
| 415 | * transaction, because we cannot make the call while we're |
| 416 | * in a transaction. |
| 417 | */ |
| 418 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 419 | error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0); |
| 420 | if (error) { |
| 421 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 422 | return error; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE); |
| 426 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 427 | XFS_TRANS_PERM_LOG_RES, |
| 428 | XFS_ITRUNCATE_LOG_COUNT))) { |
| 429 | xfs_trans_cancel(tp, 0); |
| 430 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 431 | return error; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Follow the normal truncate locking protocol. Since we |
| 436 | * hold the inode in the transaction, we know that it's number |
| 437 | * of references will stay constant. |
| 438 | */ |
| 439 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 440 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 441 | xfs_trans_ihold(tp, ip); |
| 442 | /* |
| 443 | * Signal a sync xaction. The only case where that isn't |
| 444 | * the case is if we're truncating an already unlinked file |
| 445 | * on a wsync fs. In that case, we know the blocks can't |
| 446 | * reappear in the file because the links to file are |
| 447 | * permanently toast. Currently, we're always going to |
| 448 | * want a sync transaction because this code is being |
| 449 | * called from places where nlink is guaranteed to be 1 |
| 450 | * but I'm leaving the tests in to protect against future |
| 451 | * changes -- rcc. |
| 452 | */ |
| 453 | error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0, |
| 454 | XFS_DATA_FORK, |
| 455 | ((ip->i_d.di_nlink != 0 || |
| 456 | !(mp->m_flags & XFS_MOUNT_WSYNC)) |
| 457 | ? 1 : 0)); |
| 458 | if (error) { |
| 459 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | |
| 460 | XFS_TRANS_ABORT); |
| 461 | } else { |
| 462 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 463 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 466 | |
| 467 | return error; |
| 468 | } |