Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_sb.h" |
| 27 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_dir2_sf.h" |
| 36 | #include "xfs_attr_sf.h" |
| 37 | #include "xfs_dinode.h" |
| 38 | #include "xfs_inode.h" |
| 39 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" |
| 41 | #include "xfs_btree.h" |
| 42 | #include "xfs_ialloc.h" |
| 43 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_attr.h" |
| 46 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_quota.h" |
| 49 | #include "xfs_utils.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 50 | #include "xfs_rtalloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_trans_space.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include "xfs_log_priv.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 53 | #include "xfs_filestream.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 54 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 56 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | xfs_open( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 58 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 63 | return XFS_ERROR(EIO); |
| 64 | |
| 65 | /* |
| 66 | * If it's a directory with any blocks, read-ahead block 0 |
| 67 | * as we're almost certain to have the next operation be a read there. |
| 68 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 69 | if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | mode = xfs_ilock_map_shared(ip); |
| 71 | if (ip->i_d.di_nextents > 0) |
| 72 | (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK); |
| 73 | xfs_iunlock(ip, mode); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * xfs_setattr |
| 80 | */ |
| 81 | int |
| 82 | xfs_setattr( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 83 | xfs_inode_t *ip, |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 84 | bhv_vattr_t *vap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | int flags, |
| 86 | cred_t *credp) |
| 87 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 88 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int mask; |
| 91 | int code; |
| 92 | uint lock_flags; |
| 93 | uint commit_flags=0; |
| 94 | uid_t uid=0, iuid=0; |
| 95 | gid_t gid=0, igid=0; |
| 96 | int timeflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | xfs_prid_t projid=0, iprojid=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2; |
| 99 | int file_owner; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 100 | int need_iolock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 102 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 104 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return XFS_ERROR(EROFS); |
| 106 | |
| 107 | /* |
| 108 | * Cannot set certain attributes. |
| 109 | */ |
| 110 | mask = vap->va_mask; |
| 111 | if (mask & XFS_AT_NOSET) { |
| 112 | return XFS_ERROR(EINVAL); |
| 113 | } |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 116 | return XFS_ERROR(EIO); |
| 117 | |
| 118 | /* |
| 119 | * Timestamps do not need to be logged and hence do not |
| 120 | * need to be done within a transaction. |
| 121 | */ |
| 122 | if (mask & XFS_AT_UPDTIMES) { |
| 123 | ASSERT((mask & ~XFS_AT_UPDTIMES) == 0); |
| 124 | timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) | |
| 125 | ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) | |
| 126 | ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0); |
| 127 | xfs_ichgtime(ip, timeflags); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | olddquot1 = olddquot2 = NULL; |
| 132 | udqp = gdqp = NULL; |
| 133 | |
| 134 | /* |
| 135 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 136 | * before we start any other transactions. Trying to do this later |
| 137 | * is messy. We don't care to take a readlock to look at the ids |
| 138 | * in inode here, because we can't hold it across the trans_reserve. |
| 139 | * If the IDs do change before we take the ilock, we're covered |
| 140 | * because the i_*dquot fields will get updated anyway. |
| 141 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 142 | if (XFS_IS_QUOTA_ON(mp) && |
| 143 | (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | uint qflags = 0; |
| 145 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 146 | if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | uid = vap->va_uid; |
| 148 | qflags |= XFS_QMOPT_UQUOTA; |
| 149 | } else { |
| 150 | uid = ip->i_d.di_uid; |
| 151 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 152 | if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | gid = vap->va_gid; |
| 154 | qflags |= XFS_QMOPT_GQUOTA; |
| 155 | } else { |
| 156 | gid = ip->i_d.di_gid; |
| 157 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 158 | if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) { |
| 159 | projid = vap->va_projid; |
| 160 | qflags |= XFS_QMOPT_PQUOTA; |
| 161 | } else { |
| 162 | projid = ip->i_d.di_projid; |
| 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* |
| 165 | * We take a reference when we initialize udqp and gdqp, |
| 166 | * so it is important that we never blindly double trip on |
| 167 | * the same variable. See xfs_create() for an example. |
| 168 | */ |
| 169 | ASSERT(udqp == NULL); |
| 170 | ASSERT(gdqp == NULL); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 171 | code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags, |
| 172 | &udqp, &gdqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (code) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 174 | return code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * For the other attributes, we acquire the inode lock and |
| 179 | * first do an error checking pass. |
| 180 | */ |
| 181 | tp = NULL; |
| 182 | lock_flags = XFS_ILOCK_EXCL; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 183 | if (flags & ATTR_NOLOCK) |
| 184 | need_iolock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (!(mask & XFS_AT_SIZE)) { |
| 186 | if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) || |
| 187 | (mp->m_flags & XFS_MOUNT_WSYNC)) { |
| 188 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); |
| 189 | commit_flags = 0; |
| 190 | if ((code = xfs_trans_reserve(tp, 0, |
| 191 | XFS_ICHANGE_LOG_RES(mp), 0, |
| 192 | 0, 0))) { |
| 193 | lock_flags = 0; |
| 194 | goto error_return; |
| 195 | } |
| 196 | } |
| 197 | } else { |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 198 | if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | !(flags & ATTR_DMI)) { |
| 200 | int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 201 | code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | vap->va_size, 0, dmflags, NULL); |
| 203 | if (code) { |
| 204 | lock_flags = 0; |
| 205 | goto error_return; |
| 206 | } |
| 207 | } |
| 208 | if (need_iolock) |
| 209 | lock_flags |= XFS_IOLOCK_EXCL; |
| 210 | } |
| 211 | |
| 212 | xfs_ilock(ip, lock_flags); |
| 213 | |
| 214 | /* boolean: are we the file owner? */ |
| 215 | file_owner = (current_fsuid(credp) == ip->i_d.di_uid); |
| 216 | |
| 217 | /* |
| 218 | * Change various properties of a file. |
| 219 | * Only the owner or users with CAP_FOWNER |
| 220 | * capability may do these things. |
| 221 | */ |
| 222 | if (mask & |
| 223 | (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID| |
| 224 | XFS_AT_GID|XFS_AT_PROJID)) { |
| 225 | /* |
| 226 | * CAP_FOWNER overrides the following restrictions: |
| 227 | * |
| 228 | * The user ID of the calling process must be equal |
| 229 | * to the file owner ID, except in cases where the |
| 230 | * CAP_FSETID capability is applicable. |
| 231 | */ |
| 232 | if (!file_owner && !capable(CAP_FOWNER)) { |
| 233 | code = XFS_ERROR(EPERM); |
| 234 | goto error_return; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * CAP_FSETID overrides the following restrictions: |
| 239 | * |
| 240 | * The effective user ID of the calling process shall match |
| 241 | * the file owner when setting the set-user-ID and |
| 242 | * set-group-ID bits on that file. |
| 243 | * |
| 244 | * The effective group ID or one of the supplementary group |
| 245 | * IDs of the calling process shall match the group owner of |
| 246 | * the file when setting the set-group-ID bit on that file |
| 247 | */ |
| 248 | if (mask & XFS_AT_MODE) { |
| 249 | mode_t m = 0; |
| 250 | |
| 251 | if ((vap->va_mode & S_ISUID) && !file_owner) |
| 252 | m |= S_ISUID; |
| 253 | if ((vap->va_mode & S_ISGID) && |
| 254 | !in_group_p((gid_t)ip->i_d.di_gid)) |
| 255 | m |= S_ISGID; |
| 256 | #if 0 |
| 257 | /* Linux allows this, Irix doesn't. */ |
Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 258 | if ((vap->va_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | m |= S_ISVTX; |
| 260 | #endif |
| 261 | if (m && !capable(CAP_FSETID)) |
| 262 | vap->va_mode &= ~m; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Change file ownership. Must be the owner or privileged. |
| 268 | * If the system was configured with the "restricted_chown" |
| 269 | * option, the owner is not permitted to give away the file, |
| 270 | * and can change the group id only to a group of which he |
| 271 | * or she is a member. |
| 272 | */ |
| 273 | if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) { |
| 274 | /* |
| 275 | * These IDs could have changed since we last looked at them. |
| 276 | * But, we're assured that if the ownership did change |
| 277 | * while we didn't have the inode locked, inode's dquot(s) |
| 278 | * would have changed also. |
| 279 | */ |
| 280 | iuid = ip->i_d.di_uid; |
| 281 | iprojid = ip->i_d.di_projid; |
| 282 | igid = ip->i_d.di_gid; |
| 283 | gid = (mask & XFS_AT_GID) ? vap->va_gid : igid; |
| 284 | uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid; |
| 285 | projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid : |
| 286 | iprojid; |
| 287 | |
| 288 | /* |
| 289 | * CAP_CHOWN overrides the following restrictions: |
| 290 | * |
| 291 | * If _POSIX_CHOWN_RESTRICTED is defined, this capability |
| 292 | * shall override the restriction that a process cannot |
| 293 | * change the user ID of a file it owns and the restriction |
| 294 | * that the group ID supplied to the chown() function |
| 295 | * shall be equal to either the group ID or one of the |
| 296 | * supplementary group IDs of the calling process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | */ |
| 298 | if (restricted_chown && |
| 299 | (iuid != uid || (igid != gid && |
| 300 | !in_group_p((gid_t)gid))) && |
| 301 | !capable(CAP_CHOWN)) { |
| 302 | code = XFS_ERROR(EPERM); |
| 303 | goto error_return; |
| 304 | } |
| 305 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 306 | * Do a quota reservation only if uid/projid/gid is actually |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | * going to change. |
| 308 | */ |
| 309 | if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 310 | (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | (XFS_IS_GQUOTA_ON(mp) && igid != gid)) { |
| 312 | ASSERT(tp); |
| 313 | code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp, |
| 314 | capable(CAP_FOWNER) ? |
| 315 | XFS_QMOPT_FORCE_RES : 0); |
| 316 | if (code) /* out of quota */ |
| 317 | goto error_return; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Truncate file. Must have write permission and not be a directory. |
| 323 | */ |
| 324 | if (mask & XFS_AT_SIZE) { |
| 325 | /* Short circuit the truncate case for zero length files */ |
| 326 | if ((vap->va_size == 0) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 327 | (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 329 | lock_flags &= ~XFS_ILOCK_EXCL; |
| 330 | if (mask & XFS_AT_CTIME) |
| 331 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 332 | code = 0; |
| 333 | goto error_return; |
| 334 | } |
| 335 | |
Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 336 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | code = XFS_ERROR(EISDIR); |
| 338 | goto error_return; |
Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 339 | } else if (!S_ISREG(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | code = XFS_ERROR(EINVAL); |
| 341 | goto error_return; |
| 342 | } |
| 343 | /* |
| 344 | * Make sure that the dquots are attached to the inode. |
| 345 | */ |
| 346 | if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED))) |
| 347 | goto error_return; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Change file access or modified times. |
| 352 | */ |
| 353 | if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) { |
| 354 | if (!file_owner) { |
| 355 | if ((flags & ATTR_UTIME) && |
| 356 | !capable(CAP_FOWNER)) { |
| 357 | code = XFS_ERROR(EPERM); |
| 358 | goto error_return; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Change extent size or realtime flag. |
| 365 | */ |
| 366 | if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) { |
| 367 | /* |
| 368 | * Can't change extent size if any extents are allocated. |
| 369 | */ |
Eric Sandeen | e94af02 | 2005-11-02 15:10:41 +1100 | [diff] [blame] | 370 | if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != |
| 372 | vap->va_extsize) ) { |
| 373 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 374 | goto error_return; |
| 375 | } |
| 376 | |
| 377 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | * Can't change realtime flag if any extents are allocated. |
| 379 | */ |
Eric Sandeen | e94af02 | 2005-11-02 15:10:41 +1100 | [diff] [blame] | 380 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
| 381 | (mask & XFS_AT_XFLAGS) && |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 382 | (XFS_IS_REALTIME_INODE(ip)) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | (vap->va_xflags & XFS_XFLAG_REALTIME)) { |
| 384 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 385 | goto error_return; |
| 386 | } |
| 387 | /* |
| 388 | * Extent size must be a multiple of the appropriate block |
| 389 | * size, if set at all. |
| 390 | */ |
| 391 | if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) { |
| 392 | xfs_extlen_t size; |
| 393 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 394 | if (XFS_IS_REALTIME_INODE(ip) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | ((mask & XFS_AT_XFLAGS) && |
| 396 | (vap->va_xflags & XFS_XFLAG_REALTIME))) { |
| 397 | size = mp->m_sb.sb_rextsize << |
| 398 | mp->m_sb.sb_blocklog; |
| 399 | } else { |
| 400 | size = mp->m_sb.sb_blocksize; |
| 401 | } |
| 402 | if (vap->va_extsize % size) { |
| 403 | code = XFS_ERROR(EINVAL); |
| 404 | goto error_return; |
| 405 | } |
| 406 | } |
| 407 | /* |
| 408 | * If realtime flag is set then must have realtime data. |
| 409 | */ |
| 410 | if ((mask & XFS_AT_XFLAGS) && |
| 411 | (vap->va_xflags & XFS_XFLAG_REALTIME)) { |
| 412 | if ((mp->m_sb.sb_rblocks == 0) || |
| 413 | (mp->m_sb.sb_rextsize == 0) || |
| 414 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) { |
| 415 | code = XFS_ERROR(EINVAL); |
| 416 | goto error_return; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Can't modify an immutable/append-only file unless |
| 422 | * we have appropriate permission. |
| 423 | */ |
| 424 | if ((mask & XFS_AT_XFLAGS) && |
| 425 | (ip->i_d.di_flags & |
| 426 | (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) || |
| 427 | (vap->va_xflags & |
| 428 | (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) && |
| 429 | !capable(CAP_LINUX_IMMUTABLE)) { |
| 430 | code = XFS_ERROR(EPERM); |
| 431 | goto error_return; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Now we can make the changes. Before we join the inode |
| 437 | * to the transaction, if XFS_AT_SIZE is set then take care of |
| 438 | * the part of the truncation that must be done without the |
| 439 | * inode lock. This needs to be done before joining the inode |
| 440 | * to the transaction, because the inode cannot be unlocked |
| 441 | * once it is a part of the transaction. |
| 442 | */ |
| 443 | if (mask & XFS_AT_SIZE) { |
| 444 | code = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 445 | if ((vap->va_size > ip->i_size) && |
Eric Sandeen | 374e2ac | 2005-11-02 15:07:34 +1100 | [diff] [blame] | 446 | (flags & ATTR_NOSIZETOK) == 0) { |
Christoph Hellwig | 61436fe | 2008-05-20 11:30:46 +1000 | [diff] [blame] | 447 | /* |
| 448 | * Do the first part of growing a file: zero any data |
| 449 | * in the last block that is beyond the old EOF. We |
| 450 | * need to do this before the inode is joined to the |
| 451 | * transaction to modify the i_size. |
| 452 | */ |
| 453 | code = xfs_zero_eof(ip, vap->va_size, ip->i_size); |
Eric Sandeen | 374e2ac | 2005-11-02 15:07:34 +1100 | [diff] [blame] | 454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * We are going to log the inode size change in this |
| 459 | * transaction so any previous writes that are beyond the on |
| 460 | * disk EOF and the new EOF that have not been written out need |
| 461 | * to be written here. If we do not write the data out, we |
| 462 | * expose ourselves to the null files problem. |
| 463 | * |
| 464 | * Only flush from the on disk size to the smaller of the in |
| 465 | * memory file size or the new size as that's the range we |
| 466 | * really care about here and prevents waiting for other data |
| 467 | * not within the range we care about here. |
| 468 | */ |
| 469 | if (!code && |
| 470 | (ip->i_size != ip->i_d.di_size) && |
| 471 | (vap->va_size > ip->i_d.di_size)) { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 472 | code = xfs_flush_pages(ip, |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 473 | ip->i_d.di_size, vap->va_size, |
| 474 | XFS_B_ASYNC, FI_NONE); |
| 475 | } |
| 476 | |
| 477 | /* wait for all I/O to complete */ |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 478 | vn_iowait(ip); |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (!code) |
| 481 | code = xfs_itruncate_data(ip, vap->va_size); |
| 482 | if (code) { |
| 483 | ASSERT(tp == NULL); |
| 484 | lock_flags &= ~XFS_ILOCK_EXCL; |
| 485 | ASSERT(lock_flags == XFS_IOLOCK_EXCL); |
| 486 | goto error_return; |
| 487 | } |
| 488 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE); |
| 489 | if ((code = xfs_trans_reserve(tp, 0, |
| 490 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 491 | XFS_TRANS_PERM_LOG_RES, |
| 492 | XFS_ITRUNCATE_LOG_COUNT))) { |
| 493 | xfs_trans_cancel(tp, 0); |
| 494 | if (need_iolock) |
| 495 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 496 | return code; |
| 497 | } |
| 498 | commit_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 499 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 500 | } |
| 501 | |
| 502 | if (tp) { |
| 503 | xfs_trans_ijoin(tp, ip, lock_flags); |
| 504 | xfs_trans_ihold(tp, ip); |
| 505 | } |
| 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | /* |
| 508 | * Truncate file. Must have write permission and not be a directory. |
| 509 | */ |
| 510 | if (mask & XFS_AT_SIZE) { |
David Chinner | 44d814c | 2008-03-06 13:45:29 +1100 | [diff] [blame] | 511 | /* |
| 512 | * Only change the c/mtime if we are changing the size |
| 513 | * or we are explicitly asked to change it. This handles |
| 514 | * the semantic difference between truncate() and ftruncate() |
| 515 | * as implemented in the VFS. |
| 516 | */ |
| 517 | if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME)) |
| 518 | timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; |
| 519 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 520 | if (vap->va_size > ip->i_size) { |
Christoph Hellwig | 61436fe | 2008-05-20 11:30:46 +1000 | [diff] [blame] | 521 | ip->i_d.di_size = vap->va_size; |
| 522 | ip->i_size = vap->va_size; |
| 523 | if (!(flags & ATTR_DMI)) |
| 524 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); |
| 525 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 526 | } else if ((vap->va_size <= ip->i_size) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | ((vap->va_size == 0) && ip->i_d.di_nextents)) { |
| 528 | /* |
| 529 | * signal a sync transaction unless |
| 530 | * we're truncating an already unlinked |
| 531 | * file on a wsync filesystem |
| 532 | */ |
| 533 | code = xfs_itruncate_finish(&tp, ip, |
| 534 | (xfs_fsize_t)vap->va_size, |
| 535 | XFS_DATA_FORK, |
| 536 | ((ip->i_d.di_nlink != 0 || |
| 537 | !(mp->m_flags & XFS_MOUNT_WSYNC)) |
| 538 | ? 1 : 0)); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 539 | if (code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | goto abort_return; |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 541 | /* |
| 542 | * Truncated "down", so we're removing references |
| 543 | * to old data here - if we now delay flushing for |
| 544 | * a long time, we expose ourselves unduly to the |
| 545 | * notorious NULL files problem. So, we mark this |
| 546 | * vnode and flush it when the file is closed, and |
| 547 | * do not wait the usual (long) time for writeout. |
| 548 | */ |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 549 | xfs_iflags_set(ip, XFS_ITRUNCATED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Change file access modes. |
| 555 | */ |
| 556 | if (mask & XFS_AT_MODE) { |
| 557 | ip->i_d.di_mode &= S_IFMT; |
| 558 | ip->i_d.di_mode |= vap->va_mode & ~S_IFMT; |
| 559 | |
| 560 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 561 | timeflags |= XFS_ICHGTIME_CHG; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Change file ownership. Must be the owner or privileged. |
| 566 | * If the system was configured with the "restricted_chown" |
| 567 | * option, the owner is not permitted to give away the file, |
| 568 | * and can change the group id only to a group of which he |
| 569 | * or she is a member. |
| 570 | */ |
| 571 | if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) { |
| 572 | /* |
| 573 | * CAP_FSETID overrides the following restrictions: |
| 574 | * |
| 575 | * The set-user-ID and set-group-ID bits of a file will be |
| 576 | * cleared upon successful return from chown() |
| 577 | */ |
| 578 | if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && |
| 579 | !capable(CAP_FSETID)) { |
| 580 | ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Change the ownerships and register quota modifications |
| 585 | * in the transaction. |
| 586 | */ |
| 587 | if (iuid != uid) { |
| 588 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 589 | ASSERT(mask & XFS_AT_UID); |
| 590 | ASSERT(udqp); |
| 591 | olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 592 | &ip->i_udquot, udqp); |
| 593 | } |
| 594 | ip->i_d.di_uid = uid; |
| 595 | } |
| 596 | if (igid != gid) { |
| 597 | if (XFS_IS_GQUOTA_ON(mp)) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 598 | ASSERT(!XFS_IS_PQUOTA_ON(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | ASSERT(mask & XFS_AT_GID); |
| 600 | ASSERT(gdqp); |
| 601 | olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 602 | &ip->i_gdquot, gdqp); |
| 603 | } |
| 604 | ip->i_d.di_gid = gid; |
| 605 | } |
| 606 | if (iprojid != projid) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 607 | if (XFS_IS_PQUOTA_ON(mp)) { |
| 608 | ASSERT(!XFS_IS_GQUOTA_ON(mp)); |
| 609 | ASSERT(mask & XFS_AT_PROJID); |
| 610 | ASSERT(gdqp); |
| 611 | olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 612 | &ip->i_gdquot, gdqp); |
| 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | ip->i_d.di_projid = projid; |
| 615 | /* |
| 616 | * We may have to rev the inode as well as |
| 617 | * the superblock version number since projids didn't |
| 618 | * exist before DINODE_VERSION_2 and SB_VERSION_NLINK. |
| 619 | */ |
| 620 | if (ip->i_d.di_version == XFS_DINODE_VERSION_1) |
| 621 | xfs_bump_ino_vers2(tp, ip); |
| 622 | } |
| 623 | |
| 624 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 625 | timeflags |= XFS_ICHGTIME_CHG; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | /* |
| 630 | * Change file access or modified times. |
| 631 | */ |
| 632 | if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) { |
| 633 | if (mask & XFS_AT_ATIME) { |
| 634 | ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec; |
| 635 | ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec; |
| 636 | ip->i_update_core = 1; |
| 637 | timeflags &= ~XFS_ICHGTIME_ACC; |
| 638 | } |
| 639 | if (mask & XFS_AT_MTIME) { |
| 640 | ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec; |
| 641 | ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec; |
| 642 | timeflags &= ~XFS_ICHGTIME_MOD; |
| 643 | timeflags |= XFS_ICHGTIME_CHG; |
| 644 | } |
| 645 | if (tp && (flags & ATTR_UTIME)) |
| 646 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Change XFS-added attributes. |
| 651 | */ |
| 652 | if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) { |
| 653 | if (mask & XFS_AT_EXTSIZE) { |
| 654 | /* |
| 655 | * Converting bytes to fs blocks. |
| 656 | */ |
| 657 | ip->i_d.di_extsize = vap->va_extsize >> |
| 658 | mp->m_sb.sb_blocklog; |
| 659 | } |
| 660 | if (mask & XFS_AT_XFLAGS) { |
| 661 | uint di_flags; |
| 662 | |
| 663 | /* can't set PREALLOC this way, just preserve it */ |
| 664 | di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 665 | if (vap->va_xflags & XFS_XFLAG_IMMUTABLE) |
| 666 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
| 667 | if (vap->va_xflags & XFS_XFLAG_APPEND) |
| 668 | di_flags |= XFS_DIFLAG_APPEND; |
| 669 | if (vap->va_xflags & XFS_XFLAG_SYNC) |
| 670 | di_flags |= XFS_DIFLAG_SYNC; |
| 671 | if (vap->va_xflags & XFS_XFLAG_NOATIME) |
| 672 | di_flags |= XFS_DIFLAG_NOATIME; |
| 673 | if (vap->va_xflags & XFS_XFLAG_NODUMP) |
| 674 | di_flags |= XFS_DIFLAG_NODUMP; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 675 | if (vap->va_xflags & XFS_XFLAG_PROJINHERIT) |
| 676 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 677 | if (vap->va_xflags & XFS_XFLAG_NODEFRAG) |
| 678 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 679 | if (vap->va_xflags & XFS_XFLAG_FILESTREAM) |
| 680 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 682 | if (vap->va_xflags & XFS_XFLAG_RTINHERIT) |
| 683 | di_flags |= XFS_DIFLAG_RTINHERIT; |
| 684 | if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS) |
| 685 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 686 | if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT) |
| 687 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 688 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 689 | if (vap->va_xflags & XFS_XFLAG_REALTIME) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 691 | if (vap->va_xflags & XFS_XFLAG_EXTSIZE) |
| 692 | di_flags |= XFS_DIFLAG_EXTSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | ip->i_d.di_flags = di_flags; |
| 695 | } |
| 696 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 697 | timeflags |= XFS_ICHGTIME_CHG; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Change file inode change time only if XFS_AT_CTIME set |
| 702 | * AND we have been called by a DMI function. |
| 703 | */ |
| 704 | |
| 705 | if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) { |
| 706 | ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec; |
| 707 | ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec; |
| 708 | ip->i_update_core = 1; |
| 709 | timeflags &= ~XFS_ICHGTIME_CHG; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Send out timestamp changes that need to be set to the |
| 714 | * current time. Not done when called by a DMI function. |
| 715 | */ |
| 716 | if (timeflags && !(flags & ATTR_DMI)) |
| 717 | xfs_ichgtime(ip, timeflags); |
| 718 | |
| 719 | XFS_STATS_INC(xs_ig_attrchg); |
| 720 | |
| 721 | /* |
| 722 | * If this is a synchronous mount, make sure that the |
| 723 | * transaction goes to disk before returning to the user. |
| 724 | * This is slightly sub-optimal in that truncates require |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 725 | * two sync transactions instead of one for wsync filesystems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | * One for the truncate and one for the timestamps since we |
| 727 | * don't want to change the timestamps unless we're sure the |
| 728 | * truncate worked. Truncates are less than 1% of the laddis |
| 729 | * mix so this probably isn't worth the trouble to optimize. |
| 730 | */ |
| 731 | code = 0; |
| 732 | if (tp) { |
| 733 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 734 | xfs_trans_set_sync(tp); |
| 735 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 736 | code = xfs_trans_commit(tp, commit_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | xfs_iunlock(ip, lock_flags); |
| 740 | |
| 741 | /* |
| 742 | * Release any dquot(s) the inode had kept before chown. |
| 743 | */ |
| 744 | XFS_QM_DQRELE(mp, olddquot1); |
| 745 | XFS_QM_DQRELE(mp, olddquot2); |
| 746 | XFS_QM_DQRELE(mp, udqp); |
| 747 | XFS_QM_DQRELE(mp, gdqp); |
| 748 | |
| 749 | if (code) { |
| 750 | return code; |
| 751 | } |
| 752 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 753 | if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | !(flags & ATTR_DMI)) { |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 755 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | NULL, DM_RIGHT_NULL, NULL, NULL, |
| 757 | 0, 0, AT_DELAY_FLAG(flags)); |
| 758 | } |
| 759 | return 0; |
| 760 | |
| 761 | abort_return: |
| 762 | commit_flags |= XFS_TRANS_ABORT; |
| 763 | /* FALLTHROUGH */ |
| 764 | error_return: |
| 765 | XFS_QM_DQRELE(mp, udqp); |
| 766 | XFS_QM_DQRELE(mp, gdqp); |
| 767 | if (tp) { |
| 768 | xfs_trans_cancel(tp, commit_flags); |
| 769 | } |
| 770 | if (lock_flags != 0) { |
| 771 | xfs_iunlock(ip, lock_flags); |
| 772 | } |
| 773 | return code; |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /* |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 777 | * The maximum pathlen is 1024 bytes. Since the minimum file system |
| 778 | * blocksize is 512 bytes, we can get a max of 2 extents back from |
| 779 | * bmapi. |
| 780 | */ |
| 781 | #define SYMLINK_MAPS 2 |
| 782 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 783 | STATIC int |
| 784 | xfs_readlink_bmap( |
| 785 | xfs_inode_t *ip, |
| 786 | char *link) |
| 787 | { |
| 788 | xfs_mount_t *mp = ip->i_mount; |
| 789 | int pathlen = ip->i_d.di_size; |
| 790 | int nmaps = SYMLINK_MAPS; |
| 791 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 792 | xfs_daddr_t d; |
| 793 | int byte_cnt; |
| 794 | int n; |
| 795 | xfs_buf_t *bp; |
| 796 | int error = 0; |
| 797 | |
| 798 | error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0, |
| 799 | mval, &nmaps, NULL, NULL); |
| 800 | if (error) |
| 801 | goto out; |
| 802 | |
| 803 | for (n = 0; n < nmaps; n++) { |
| 804 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 805 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 806 | |
| 807 | bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0); |
| 808 | error = XFS_BUF_GETERROR(bp); |
| 809 | if (error) { |
| 810 | xfs_ioerror_alert("xfs_readlink", |
| 811 | ip->i_mount, bp, XFS_BUF_ADDR(bp)); |
| 812 | xfs_buf_relse(bp); |
| 813 | goto out; |
| 814 | } |
| 815 | if (pathlen < byte_cnt) |
| 816 | byte_cnt = pathlen; |
| 817 | pathlen -= byte_cnt; |
| 818 | |
| 819 | memcpy(link, XFS_BUF_PTR(bp), byte_cnt); |
| 820 | xfs_buf_relse(bp); |
| 821 | } |
| 822 | |
| 823 | link[ip->i_d.di_size] = '\0'; |
| 824 | error = 0; |
| 825 | |
| 826 | out: |
| 827 | return error; |
| 828 | } |
| 829 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 830 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | xfs_readlink( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 832 | xfs_inode_t *ip, |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 833 | char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 835 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | int pathlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 839 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 842 | return XFS_ERROR(EIO); |
| 843 | |
| 844 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 845 | |
| 846 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 847 | ASSERT(ip->i_d.di_size <= MAXPATHLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 849 | pathlen = ip->i_d.di_size; |
| 850 | if (!pathlen) |
| 851 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | if (ip->i_df.if_flags & XFS_IFINLINE) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 854 | memcpy(link, ip->i_df.if_u1.if_data, pathlen); |
| 855 | link[pathlen] = '\0'; |
| 856 | } else { |
| 857 | error = xfs_readlink_bmap(ip, link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 860 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | return error; |
| 863 | } |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | /* |
| 866 | * xfs_fsync |
| 867 | * |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 868 | * This is called to sync the inode and its data out to disk. We need to hold |
| 869 | * the I/O lock while flushing the data, and the inode lock while flushing the |
| 870 | * inode. The inode lock CANNOT be held while flushing the data, so acquire |
| 871 | * after we're done with that. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 873 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | xfs_fsync( |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 875 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | xfs_trans_t *tp; |
| 878 | int error; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 879 | int log_flushed = 0, changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 881 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 884 | return XFS_ERROR(EIO); |
| 885 | |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 886 | /* capture size updates in I/O completion before writing the inode. */ |
| 887 | error = filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping); |
| 888 | if (error) |
| 889 | return XFS_ERROR(error); |
Lachlan McIlroy | 776a75f | 2007-09-14 15:22:50 +1000 | [diff] [blame] | 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | /* |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 892 | * We always need to make sure that the required inode state is safe on |
| 893 | * disk. The vnode might be clean but we still might need to force the |
| 894 | * log because of committed transactions that haven't hit the disk yet. |
| 895 | * Likewise, there could be unflushed non-transactional changes to the |
| 896 | * inode core that have to go to disk and this requires us to issue |
| 897 | * a synchronous transaction to capture these changes correctly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | * |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 899 | * This code relies on the assumption that if the update_* fields |
| 900 | * of the inode are clear and the inode is unpinned then it is clean |
| 901 | * and no action is required. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | */ |
| 903 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 904 | |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 905 | if (!(ip->i_update_size || ip->i_update_core)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 907 | * Timestamps/size haven't changed since last inode flush or |
| 908 | * inode transaction commit. That means either nothing got |
| 909 | * written or a transaction committed which caught the updates. |
| 910 | * If the latter happened and the transaction hasn't hit the |
| 911 | * disk yet, the inode will be still be pinned. If it is, |
| 912 | * force the log. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | */ |
| 914 | |
| 915 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 916 | |
| 917 | if (xfs_ipincount(ip)) { |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 918 | error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0, |
| 919 | XFS_LOG_FORCE | XFS_LOG_SYNC, |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 920 | &log_flushed); |
| 921 | } else { |
| 922 | /* |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 923 | * If the inode is not pinned and nothing has changed |
| 924 | * we don't need to flush the cache. |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 925 | */ |
| 926 | changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } else { |
| 929 | /* |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 930 | * Kick off a transaction to log the inode core to get the |
| 931 | * updates. The sync transaction will also force the log. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | */ |
| 933 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 934 | tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS); |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 935 | error = xfs_trans_reserve(tp, 0, |
| 936 | XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0); |
| 937 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | xfs_trans_cancel(tp, 0); |
| 939 | return error; |
| 940 | } |
| 941 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 942 | |
| 943 | /* |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 944 | * Note - it's possible that we might have pushed ourselves out |
| 945 | * of the way during trans_reserve which would flush the inode. |
| 946 | * But there's no guarantee that the inode buffer has actually |
| 947 | * gone out yet (it's delwri). Plus the buffer could be pinned |
| 948 | * anyway if it's part of an inode in another recent |
| 949 | * transaction. So we play it safe and fire off the |
| 950 | * transaction anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | */ |
| 952 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 953 | xfs_trans_ihold(tp, ip); |
| 954 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
David Chinner | 978b723 | 2008-05-19 16:29:46 +1000 | [diff] [blame] | 955 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 956 | error = _xfs_trans_commit(tp, 0, &log_flushed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 959 | } |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 960 | |
| 961 | if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) { |
| 962 | /* |
| 963 | * If the log write didn't issue an ordered tag we need |
| 964 | * to flush the disk cache for the data device now. |
| 965 | */ |
| 966 | if (!log_flushed) |
| 967 | xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp); |
| 968 | |
| 969 | /* |
| 970 | * If this inode is on the RT dev we need to flush that |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 971 | * cache as well. |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 972 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 973 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 974 | xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp); |
| 975 | } |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return error; |
| 978 | } |
| 979 | |
| 980 | /* |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 981 | * This is called by xfs_inactive to free any blocks beyond eof |
| 982 | * when the link count isn't zero and by xfs_dm_punch_hole() when |
| 983 | * punching a hole to EOF. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | */ |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 985 | int |
| 986 | xfs_free_eofblocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | xfs_mount_t *mp, |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 988 | xfs_inode_t *ip, |
| 989 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
| 991 | xfs_trans_t *tp; |
| 992 | int error; |
| 993 | xfs_fileoff_t end_fsb; |
| 994 | xfs_fileoff_t last_fsb; |
| 995 | xfs_filblks_t map_len; |
| 996 | int nimaps; |
| 997 | xfs_bmbt_irec_t imap; |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 998 | int use_iolock = (flags & XFS_FREE_EOF_LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * Figure out if there are any blocks beyond the end |
| 1002 | * of the file. If not, then there is nothing to do. |
| 1003 | */ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1004 | 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] | 1005 | last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
| 1006 | map_len = last_fsb - end_fsb; |
| 1007 | if (map_len <= 0) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1008 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
| 1010 | nimaps = 1; |
| 1011 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 1012 | error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1013 | NULL, 0, &imap, &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 1015 | |
| 1016 | if (!error && (nimaps != 0) && |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1017 | (imap.br_startblock != HOLESTARTBLOCK || |
| 1018 | ip->i_delayed_blks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | /* |
| 1020 | * Attach the dquots to the inode up front. |
| 1021 | */ |
| 1022 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1023 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * There are blocks after the end of file. |
| 1027 | * Free them up now by truncating the file to |
| 1028 | * its current size. |
| 1029 | */ |
| 1030 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1031 | |
| 1032 | /* |
| 1033 | * Do the xfs_itruncate_start() call before |
| 1034 | * reserving any log space because |
| 1035 | * itruncate_start will call into the buffer |
| 1036 | * cache and we can't |
| 1037 | * do that within a transaction. |
| 1038 | */ |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1039 | if (use_iolock) |
| 1040 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1041 | error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1042 | ip->i_size); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1043 | if (error) { |
Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 1044 | xfs_trans_cancel(tp, 0); |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1045 | if (use_iolock) |
| 1046 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1047 | return error; |
| 1048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
| 1050 | error = xfs_trans_reserve(tp, 0, |
| 1051 | XFS_ITRUNCATE_LOG_RES(mp), |
| 1052 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1053 | XFS_ITRUNCATE_LOG_COUNT); |
| 1054 | if (error) { |
| 1055 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1056 | xfs_trans_cancel(tp, 0); |
| 1057 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1058 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1062 | xfs_trans_ijoin(tp, ip, |
| 1063 | XFS_IOLOCK_EXCL | |
| 1064 | XFS_ILOCK_EXCL); |
| 1065 | xfs_trans_ihold(tp, ip); |
| 1066 | |
| 1067 | error = xfs_itruncate_finish(&tp, ip, |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1068 | ip->i_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | XFS_DATA_FORK, |
| 1070 | 0); |
| 1071 | /* |
| 1072 | * If we get an error at this point we |
| 1073 | * simply don't bother truncating the file. |
| 1074 | */ |
| 1075 | if (error) { |
| 1076 | xfs_trans_cancel(tp, |
| 1077 | (XFS_TRANS_RELEASE_LOG_RES | |
| 1078 | XFS_TRANS_ABORT)); |
| 1079 | } else { |
| 1080 | error = xfs_trans_commit(tp, |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1081 | XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1083 | xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL) |
| 1084 | : XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1086 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Free a symlink that has blocks associated with it. |
| 1091 | */ |
| 1092 | STATIC int |
| 1093 | xfs_inactive_symlink_rmt( |
| 1094 | xfs_inode_t *ip, |
| 1095 | xfs_trans_t **tpp) |
| 1096 | { |
| 1097 | xfs_buf_t *bp; |
| 1098 | int committed; |
| 1099 | int done; |
| 1100 | int error; |
| 1101 | xfs_fsblock_t first_block; |
| 1102 | xfs_bmap_free_t free_list; |
| 1103 | int i; |
| 1104 | xfs_mount_t *mp; |
| 1105 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 1106 | int nmaps; |
| 1107 | xfs_trans_t *ntp; |
| 1108 | int size; |
| 1109 | xfs_trans_t *tp; |
| 1110 | |
| 1111 | tp = *tpp; |
| 1112 | mp = ip->i_mount; |
| 1113 | ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip)); |
| 1114 | /* |
| 1115 | * We're freeing a symlink that has some |
| 1116 | * blocks allocated to it. Free the |
| 1117 | * blocks here. We know that we've got |
| 1118 | * either 1 or 2 extents and that we can |
| 1119 | * free them all in one bunmapi call. |
| 1120 | */ |
| 1121 | ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); |
| 1122 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1123 | XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { |
| 1124 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1125 | xfs_trans_cancel(tp, 0); |
| 1126 | *tpp = NULL; |
| 1127 | return error; |
| 1128 | } |
| 1129 | /* |
| 1130 | * Lock the inode, fix the size, and join it to the transaction. |
| 1131 | * Hold it so in the normal path, we still have it locked for |
| 1132 | * the second transaction. In the error paths we need it |
| 1133 | * held so the cancel won't rele it, see below. |
| 1134 | */ |
| 1135 | xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1136 | size = (int)ip->i_d.di_size; |
| 1137 | ip->i_d.di_size = 0; |
| 1138 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1139 | xfs_trans_ihold(tp, ip); |
| 1140 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1141 | /* |
| 1142 | * Find the block(s) so we can inval and unmap them. |
| 1143 | */ |
| 1144 | done = 0; |
| 1145 | XFS_BMAP_INIT(&free_list, &first_block); |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 1146 | nmaps = ARRAY_SIZE(mval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size), |
| 1148 | XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1149 | &free_list, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | goto error0; |
| 1151 | /* |
| 1152 | * Invalidate the block(s). |
| 1153 | */ |
| 1154 | for (i = 0; i < nmaps; i++) { |
| 1155 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 1156 | XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), |
| 1157 | XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0); |
| 1158 | xfs_trans_binval(tp, bp); |
| 1159 | } |
| 1160 | /* |
| 1161 | * Unmap the dead block(s) to the free_list. |
| 1162 | */ |
| 1163 | if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1164 | &first_block, &free_list, NULL, &done))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | goto error1; |
| 1166 | ASSERT(done); |
| 1167 | /* |
| 1168 | * Commit the first transaction. This logs the EFI and the inode. |
| 1169 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1170 | if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | goto error1; |
| 1172 | /* |
| 1173 | * The transaction must have been committed, since there were |
| 1174 | * actually extents freed by xfs_bunmapi. See xfs_bmap_finish. |
| 1175 | * The new tp has the extent freeing and EFDs. |
| 1176 | */ |
| 1177 | ASSERT(committed); |
| 1178 | /* |
| 1179 | * The first xact was committed, so add the inode to the new one. |
| 1180 | * Mark it dirty so it will be logged and moved forward in the log as |
| 1181 | * part of every commit. |
| 1182 | */ |
| 1183 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1184 | xfs_trans_ihold(tp, ip); |
| 1185 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1186 | /* |
| 1187 | * Get a new, empty transaction to return to our caller. |
| 1188 | */ |
| 1189 | ntp = xfs_trans_dup(tp); |
| 1190 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1191 | * Commit the transaction containing extent freeing and EFDs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | * If we get an error on the commit here or on the reserve below, |
| 1193 | * we need to unlock the inode since the new transaction doesn't |
| 1194 | * have the inode attached. |
| 1195 | */ |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1196 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | tp = ntp; |
| 1198 | if (error) { |
| 1199 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1200 | goto error0; |
| 1201 | } |
| 1202 | /* |
| 1203 | * Remove the memory for extent descriptions (just bookkeeping). |
| 1204 | */ |
| 1205 | if (ip->i_df.if_bytes) |
| 1206 | xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); |
| 1207 | ASSERT(ip->i_df.if_bytes == 0); |
| 1208 | /* |
| 1209 | * Put an itruncate log reservation in the new transaction |
| 1210 | * for our caller. |
| 1211 | */ |
| 1212 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1213 | XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { |
| 1214 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1215 | goto error0; |
| 1216 | } |
| 1217 | /* |
| 1218 | * Return with the inode locked but not joined to the transaction. |
| 1219 | */ |
| 1220 | *tpp = tp; |
| 1221 | return 0; |
| 1222 | |
| 1223 | error1: |
| 1224 | xfs_bmap_cancel(&free_list); |
| 1225 | error0: |
| 1226 | /* |
| 1227 | * Have to come here with the inode locked and either |
| 1228 | * (held and in the transaction) or (not in the transaction). |
| 1229 | * If the inode isn't held then cancel would iput it, but |
| 1230 | * that's wrong since this is inactive and the vnode ref |
| 1231 | * count is 0 already. |
| 1232 | * Cancel won't do anything to the inode if held, but it still |
| 1233 | * needs to be locked until the cancel is done, if it was |
| 1234 | * joined to the transaction. |
| 1235 | */ |
| 1236 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1237 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1238 | *tpp = NULL; |
| 1239 | return error; |
| 1240 | |
| 1241 | } |
| 1242 | |
| 1243 | STATIC int |
| 1244 | xfs_inactive_symlink_local( |
| 1245 | xfs_inode_t *ip, |
| 1246 | xfs_trans_t **tpp) |
| 1247 | { |
| 1248 | int error; |
| 1249 | |
| 1250 | ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip)); |
| 1251 | /* |
| 1252 | * We're freeing a symlink which fit into |
| 1253 | * the inode. Just free the memory used |
| 1254 | * to hold the old symlink. |
| 1255 | */ |
| 1256 | error = xfs_trans_reserve(*tpp, 0, |
| 1257 | XFS_ITRUNCATE_LOG_RES(ip->i_mount), |
| 1258 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1259 | XFS_ITRUNCATE_LOG_COUNT); |
| 1260 | |
| 1261 | if (error) { |
| 1262 | xfs_trans_cancel(*tpp, 0); |
| 1263 | *tpp = NULL; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1264 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1267 | |
| 1268 | /* |
| 1269 | * Zero length symlinks _can_ exist. |
| 1270 | */ |
| 1271 | if (ip->i_df.if_bytes > 0) { |
| 1272 | xfs_idata_realloc(ip, |
| 1273 | -(ip->i_df.if_bytes), |
| 1274 | XFS_DATA_FORK); |
| 1275 | ASSERT(ip->i_df.if_bytes == 0); |
| 1276 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1277 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | STATIC int |
| 1281 | xfs_inactive_attrs( |
| 1282 | xfs_inode_t *ip, |
| 1283 | xfs_trans_t **tpp) |
| 1284 | { |
| 1285 | xfs_trans_t *tp; |
| 1286 | int error; |
| 1287 | xfs_mount_t *mp; |
| 1288 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1289 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | tp = *tpp; |
| 1291 | mp = ip->i_mount; |
| 1292 | ASSERT(ip->i_d.di_forkoff != 0); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1293 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1295 | if (error) |
| 1296 | goto error_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
| 1298 | error = xfs_attr_inactive(ip); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1299 | if (error) |
| 1300 | goto error_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | |
| 1302 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1303 | error = xfs_trans_reserve(tp, 0, |
| 1304 | XFS_IFREE_LOG_RES(mp), |
| 1305 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1306 | XFS_INACTIVE_LOG_COUNT); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1307 | if (error) |
| 1308 | goto error_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1311 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1312 | xfs_trans_ihold(tp, ip); |
| 1313 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 1314 | |
| 1315 | ASSERT(ip->i_d.di_anextents == 0); |
| 1316 | |
| 1317 | *tpp = tp; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1318 | return 0; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1319 | |
| 1320 | error_cancel: |
| 1321 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1322 | xfs_trans_cancel(tp, 0); |
| 1323 | error_unlock: |
| 1324 | *tpp = NULL; |
| 1325 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1326 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1329 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | xfs_release( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1331 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1333 | bhv_vnode_t *vp = XFS_ITOV(ip); |
| 1334 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | int error; |
| 1336 | |
Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 1337 | 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] | 1338 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | /* 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] | 1341 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return 0; |
| 1343 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1344 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1345 | int truncated; |
| 1346 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1347 | /* |
| 1348 | * If we are using filestreams, and we have an unlinked |
| 1349 | * file that we are processing the last close on, then nothing |
| 1350 | * will be able to reopen and write to this file. Purge this |
| 1351 | * inode from the filestreams cache so that it doesn't delay |
| 1352 | * teardown of the inode. |
| 1353 | */ |
| 1354 | if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) |
| 1355 | xfs_filestream_deassociate(ip); |
| 1356 | |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1357 | /* |
| 1358 | * If we previously truncated this file and removed old data |
| 1359 | * in the process, we want to initiate "early" writeout on |
| 1360 | * the last close. This is an attempt to combat the notorious |
| 1361 | * NULL files problem which is particularly noticable from a |
| 1362 | * truncate down, buffered (re-)write (delalloc), followed by |
| 1363 | * a crash. What we are effectively doing here is |
| 1364 | * significantly reducing the time window where we'd otherwise |
| 1365 | * be exposed to that problem. |
| 1366 | */ |
Christoph Hellwig | 09262b4 | 2007-08-29 11:44:50 +1000 | [diff] [blame] | 1367 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1368 | if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0) |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1369 | xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE); |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1370 | } |
| 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | if (ip->i_d.di_nlink != 0) { |
| 1373 | if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1374 | ((ip->i_size > 0) || (VN_CACHED(vp) > 0 || |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1375 | ip->i_delayed_blks > 0)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | (ip->i_df.if_flags & XFS_IFEXTENTS)) && |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1377 | (!(ip->i_d.di_flags & |
| 1378 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) { |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1379 | error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); |
| 1380 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1381 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * xfs_inactive |
| 1390 | * |
| 1391 | * This is called when the vnode reference count for the vnode |
| 1392 | * goes to zero. If the file has been unlinked, then it must |
| 1393 | * now be truncated. Also, we clear all of the read-ahead state |
| 1394 | * kept for the inode here since the file is now closed. |
| 1395 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1396 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | xfs_inactive( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1398 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1400 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1401 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | xfs_fsblock_t first_block; |
| 1403 | int committed; |
| 1404 | xfs_trans_t *tp; |
| 1405 | xfs_mount_t *mp; |
| 1406 | int error; |
| 1407 | int truncate; |
| 1408 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1409 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | * If the inode is already free, then there can be nothing |
| 1413 | * to clean up here. |
| 1414 | */ |
| 1415 | if (ip->i_d.di_mode == 0 || VN_BAD(vp)) { |
| 1416 | ASSERT(ip->i_df.if_real_bytes == 0); |
| 1417 | ASSERT(ip->i_df.if_broot_bytes == 0); |
| 1418 | return VN_INACTIVE_CACHE; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * Only do a truncate if it's a regular file with |
| 1423 | * some actual space in it. It's OK to look at the |
| 1424 | * inode's fields without the lock because we're the |
| 1425 | * only one with a reference to the inode. |
| 1426 | */ |
| 1427 | truncate = ((ip->i_d.di_nlink == 0) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1428 | ((ip->i_d.di_size != 0) || (ip->i_size != 0) || |
| 1429 | (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | ((ip->i_d.di_mode & S_IFMT) == S_IFREG)); |
| 1431 | |
| 1432 | mp = ip->i_mount; |
| 1433 | |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1434 | if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) |
| 1435 | XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
| 1437 | error = 0; |
| 1438 | |
| 1439 | /* 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] | 1440 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | goto out; |
| 1442 | |
| 1443 | if (ip->i_d.di_nlink != 0) { |
| 1444 | if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1445 | ((ip->i_size > 0) || (VN_CACHED(vp) > 0 || |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1446 | ip->i_delayed_blks > 0)) && |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1447 | (ip->i_df.if_flags & XFS_IFEXTENTS) && |
| 1448 | (!(ip->i_d.di_flags & |
| 1449 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) || |
| 1450 | (ip->i_delayed_blks != 0)))) { |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1451 | error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); |
| 1452 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1453 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | goto out; |
| 1456 | } |
| 1457 | |
| 1458 | ASSERT(ip->i_d.di_nlink == 0); |
| 1459 | |
| 1460 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1461 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1464 | if (truncate) { |
| 1465 | /* |
| 1466 | * Do the xfs_itruncate_start() call before |
| 1467 | * reserving any log space because itruncate_start |
| 1468 | * will call into the buffer cache and we can't |
| 1469 | * do that within a transaction. |
| 1470 | */ |
| 1471 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 1472 | |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1473 | error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); |
| 1474 | if (error) { |
Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 1475 | xfs_trans_cancel(tp, 0); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1476 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1477 | return VN_INACTIVE_CACHE; |
| 1478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| 1480 | error = xfs_trans_reserve(tp, 0, |
| 1481 | XFS_ITRUNCATE_LOG_RES(mp), |
| 1482 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1483 | XFS_ITRUNCATE_LOG_COUNT); |
| 1484 | if (error) { |
| 1485 | /* Don't call itruncate_cleanup */ |
| 1486 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1487 | xfs_trans_cancel(tp, 0); |
| 1488 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1489 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1493 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1494 | xfs_trans_ihold(tp, ip); |
| 1495 | |
| 1496 | /* |
| 1497 | * normally, we have to run xfs_itruncate_finish sync. |
| 1498 | * But if filesystem is wsync and we're in the inactive |
| 1499 | * path, then we know that nlink == 0, and that the |
| 1500 | * xaction that made nlink == 0 is permanently committed |
| 1501 | * since xfs_remove runs as a synchronous transaction. |
| 1502 | */ |
| 1503 | error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK, |
| 1504 | (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0)); |
| 1505 | |
| 1506 | if (error) { |
| 1507 | xfs_trans_cancel(tp, |
| 1508 | XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1509 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1510 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) { |
| 1513 | |
| 1514 | /* |
| 1515 | * If we get an error while cleaning up a |
| 1516 | * symlink we bail out. |
| 1517 | */ |
| 1518 | error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ? |
| 1519 | xfs_inactive_symlink_rmt(ip, &tp) : |
| 1520 | xfs_inactive_symlink_local(ip, &tp); |
| 1521 | |
| 1522 | if (error) { |
| 1523 | ASSERT(tp == NULL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1524 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1528 | xfs_trans_ihold(tp, ip); |
| 1529 | } else { |
| 1530 | error = xfs_trans_reserve(tp, 0, |
| 1531 | XFS_IFREE_LOG_RES(mp), |
| 1532 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1533 | XFS_INACTIVE_LOG_COUNT); |
| 1534 | if (error) { |
| 1535 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1536 | xfs_trans_cancel(tp, 0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1537 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1541 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1542 | xfs_trans_ihold(tp, ip); |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * If there are attributes associated with the file |
| 1547 | * then blow them away now. The code calls a routine |
| 1548 | * that recursively deconstructs the attribute fork. |
| 1549 | * We need to just commit the current transaction |
| 1550 | * because we can't use it for xfs_attr_inactive(). |
| 1551 | */ |
| 1552 | if (ip->i_d.di_anextents > 0) { |
| 1553 | error = xfs_inactive_attrs(ip, &tp); |
| 1554 | /* |
| 1555 | * If we got an error, the transaction is already |
| 1556 | * cancelled, and the inode is unlocked. Just get out. |
| 1557 | */ |
| 1558 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1559 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } else if (ip->i_afp) { |
| 1561 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * Free the inode. |
| 1566 | */ |
| 1567 | XFS_BMAP_INIT(&free_list, &first_block); |
| 1568 | error = xfs_ifree(tp, ip, &free_list); |
| 1569 | if (error) { |
| 1570 | /* |
| 1571 | * If we fail to free the inode, shut down. The cancel |
| 1572 | * might do that, we need to make sure. Otherwise the |
| 1573 | * inode might be lost for a long time or forever. |
| 1574 | */ |
| 1575 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
| 1576 | cmn_err(CE_NOTE, |
| 1577 | "xfs_inactive: xfs_ifree() returned an error = %d on %s", |
| 1578 | error, mp->m_fsname); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1579 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | } |
| 1581 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 1582 | } else { |
| 1583 | /* |
| 1584 | * Credit the quota account(s). The inode is gone. |
| 1585 | */ |
| 1586 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1); |
| 1587 | |
| 1588 | /* |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1589 | * Just ignore errors at this point. There is nothing we can |
| 1590 | * do except to try to keep going. Make sure it's not a silent |
| 1591 | * error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | */ |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1593 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 1594 | if (error) |
| 1595 | xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " |
| 1596 | "xfs_bmap_finish() returned error %d", error); |
| 1597 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1598 | if (error) |
| 1599 | xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " |
| 1600 | "xfs_trans_commit() returned error %d", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
| 1602 | /* |
| 1603 | * Release the dquots held by inode, if any. |
| 1604 | */ |
| 1605 | XFS_QM_DQDETACH(mp, ip); |
| 1606 | |
| 1607 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1608 | |
| 1609 | out: |
| 1610 | return VN_INACTIVE_CACHE; |
| 1611 | } |
| 1612 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame^] | 1613 | /* |
| 1614 | * Lookups up an inode from "name". If ci_name is not NULL, then a CI match |
| 1615 | * is allowed, otherwise it has to be an exact match. If a CI match is found, |
| 1616 | * ci_name->name will point to a the actual name (caller must free) or |
| 1617 | * will be set to NULL if an exact match is found. |
| 1618 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1619 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | xfs_lookup( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1621 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1622 | struct xfs_name *name, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame^] | 1623 | xfs_inode_t **ipp, |
| 1624 | struct xfs_name *ci_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | { |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1626 | xfs_ino_t inum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | int error; |
| 1628 | uint lock_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1630 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 1633 | return XFS_ERROR(EIO); |
| 1634 | |
| 1635 | lock_mode = xfs_ilock_map_shared(dp); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame^] | 1636 | error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | xfs_iunlock_map_shared(dp, lock_mode); |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1638 | |
| 1639 | if (error) |
| 1640 | goto out; |
| 1641 | |
| 1642 | error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0); |
| 1643 | if (error) |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame^] | 1644 | goto out_free_name; |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1645 | |
| 1646 | xfs_itrace_ref(*ipp); |
| 1647 | return 0; |
| 1648 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame^] | 1649 | out_free_name: |
| 1650 | if (ci_name) |
| 1651 | kmem_free(ci_name->name); |
| 1652 | out: |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 1653 | *ipp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | return error; |
| 1655 | } |
| 1656 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1657 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | xfs_create( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1659 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1660 | struct xfs_name *name, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1661 | mode_t mode, |
| 1662 | xfs_dev_t rdev, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1663 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | cred_t *credp) |
| 1665 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1666 | xfs_mount_t *mp = dp->i_mount; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1667 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | xfs_trans_t *tp; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1669 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | xfs_bmap_free_t free_list; |
| 1671 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1672 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | int dm_event_sent = 0; |
| 1674 | uint cancel_flags; |
| 1675 | int committed; |
| 1676 | xfs_prid_t prid; |
| 1677 | struct xfs_dquot *udqp, *gdqp; |
| 1678 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1680 | ASSERT(!*ipp); |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1681 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 1683 | if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1685 | dp, DM_RIGHT_NULL, NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1686 | DM_RIGHT_NULL, name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1687 | mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | |
| 1689 | if (error) |
| 1690 | return error; |
| 1691 | dm_event_sent = 1; |
| 1692 | } |
| 1693 | |
| 1694 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1695 | return XFS_ERROR(EIO); |
| 1696 | |
| 1697 | /* Return through std_return after this point. */ |
| 1698 | |
| 1699 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1700 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 1701 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | else |
| 1703 | prid = (xfs_prid_t)dfltprid; |
| 1704 | |
| 1705 | /* |
| 1706 | * Make sure that we have allocated dquot(s) on disk. |
| 1707 | */ |
| 1708 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1709 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 1711 | if (error) |
| 1712 | goto std_return; |
| 1713 | |
| 1714 | ip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
| 1716 | tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); |
| 1717 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1718 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | * Initially assume that the file does not exist and |
| 1721 | * reserve the resources for that case. If that is not |
| 1722 | * the case we'll drop the one we have and get a more |
| 1723 | * appropriate transaction later. |
| 1724 | */ |
| 1725 | error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0, |
| 1726 | XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT); |
| 1727 | if (error == ENOSPC) { |
| 1728 | resblks = 0; |
| 1729 | error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0, |
| 1730 | XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT); |
| 1731 | } |
| 1732 | if (error) { |
| 1733 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | goto error_return; |
| 1735 | } |
| 1736 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1737 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1738 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
| 1740 | XFS_BMAP_INIT(&free_list, &first_block); |
| 1741 | |
| 1742 | ASSERT(ip == NULL); |
| 1743 | |
| 1744 | /* |
| 1745 | * Reserve disk quota and the inode. |
| 1746 | */ |
| 1747 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 1748 | if (error) |
| 1749 | goto error_return; |
| 1750 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1751 | error = xfs_dir_canenter(tp, dp, name, resblks); |
| 1752 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | goto error_return; |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1754 | error = xfs_dir_ialloc(&tp, dp, mode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | rdev, credp, prid, resblks > 0, |
| 1756 | &ip, &committed); |
| 1757 | if (error) { |
| 1758 | if (error == ENOSPC) |
| 1759 | goto error_return; |
| 1760 | goto abort_return; |
| 1761 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1762 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* |
| 1765 | * At this point, we've gotten a newly allocated inode. |
| 1766 | * It is locked (and joined to the transaction). |
| 1767 | */ |
| 1768 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1769 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
| 1771 | /* |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1772 | * Now we join the directory inode to the transaction. We do not do it |
| 1773 | * earlier because xfs_dir_ialloc might commit the previous transaction |
| 1774 | * (and release all the locks). An error from here on will result in |
| 1775 | * the transaction cancel unlocking dp so don't do it explicitly in the |
| 1776 | * error path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | */ |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1778 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1780 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1782 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 1783 | &first_block, &free_list, resblks ? |
| 1784 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | if (error) { |
| 1786 | ASSERT(error != ENOSPC); |
| 1787 | goto abort_return; |
| 1788 | } |
| 1789 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 1790 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 1791 | |
| 1792 | /* |
| 1793 | * If this is a synchronous mount, make sure that the |
| 1794 | * create transaction goes to disk before returning to |
| 1795 | * the user. |
| 1796 | */ |
| 1797 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 1798 | xfs_trans_set_sync(tp); |
| 1799 | } |
| 1800 | |
| 1801 | dp->i_gen++; |
| 1802 | |
| 1803 | /* |
| 1804 | * Attach the dquot(s) to the inodes and modify them incore. |
| 1805 | * These ids of the inode couldn't have changed since the new |
| 1806 | * inode has been locked ever since it was created. |
| 1807 | */ |
| 1808 | XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp); |
| 1809 | |
| 1810 | /* |
| 1811 | * xfs_trans_commit normally decrements the vnode ref count |
| 1812 | * when it unlocks the inode. Since we want to return the |
| 1813 | * vnode to the caller, we bump the vnode ref count now. |
| 1814 | */ |
| 1815 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1817 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | if (error) { |
| 1819 | xfs_bmap_cancel(&free_list); |
| 1820 | goto abort_rele; |
| 1821 | } |
| 1822 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1823 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | if (error) { |
| 1825 | IRELE(ip); |
| 1826 | tp = NULL; |
| 1827 | goto error_return; |
| 1828 | } |
| 1829 | |
| 1830 | XFS_QM_DQRELE(mp, udqp); |
| 1831 | XFS_QM_DQRELE(mp, gdqp); |
| 1832 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1833 | *ipp = ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | |
| 1835 | /* Fallthrough to std_return with error = 0 */ |
| 1836 | |
| 1837 | std_return: |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1838 | if ((*ipp || (error != 0 && dm_event_sent != 0)) && |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1839 | DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1841 | dp, DM_RIGHT_NULL, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1842 | *ipp ? ip : NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1843 | DM_RIGHT_NULL, name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1844 | mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | } |
| 1846 | return error; |
| 1847 | |
| 1848 | abort_return: |
| 1849 | cancel_flags |= XFS_TRANS_ABORT; |
| 1850 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1852 | error_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | if (tp != NULL) |
| 1854 | xfs_trans_cancel(tp, cancel_flags); |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | XFS_QM_DQRELE(mp, udqp); |
| 1857 | XFS_QM_DQRELE(mp, gdqp); |
| 1858 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1859 | if (unlock_dp_on_error) |
| 1860 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | goto std_return; |
| 1863 | |
| 1864 | abort_rele: |
| 1865 | /* |
| 1866 | * Wait until after the current transaction is aborted to |
| 1867 | * release the inode. This prevents recursive transactions |
| 1868 | * and deadlocks from xfs_inactive. |
| 1869 | */ |
| 1870 | cancel_flags |= XFS_TRANS_ABORT; |
| 1871 | xfs_trans_cancel(tp, cancel_flags); |
| 1872 | IRELE(ip); |
| 1873 | |
| 1874 | XFS_QM_DQRELE(mp, udqp); |
| 1875 | XFS_QM_DQRELE(mp, gdqp); |
| 1876 | |
| 1877 | goto std_return; |
| 1878 | } |
| 1879 | |
| 1880 | #ifdef DEBUG |
| 1881 | /* |
| 1882 | * Some counters to see if (and how often) we are hitting some deadlock |
| 1883 | * prevention code paths. |
| 1884 | */ |
| 1885 | |
| 1886 | int xfs_rm_locks; |
| 1887 | int xfs_rm_lock_delays; |
| 1888 | int xfs_rm_attempts; |
| 1889 | #endif |
| 1890 | |
| 1891 | /* |
| 1892 | * The following routine will lock the inodes associated with the |
| 1893 | * directory and the named entry in the directory. The locks are |
| 1894 | * acquired in increasing inode number. |
| 1895 | * |
| 1896 | * If the entry is "..", then only the directory is locked. The |
| 1897 | * vnode ref count will still include that from the .. entry in |
| 1898 | * this case. |
| 1899 | * |
| 1900 | * There is a deadlock we need to worry about. If the locked directory is |
| 1901 | * in the AIL, it might be blocking up the log. The next inode we lock |
| 1902 | * could be already locked by another thread waiting for log space (e.g |
| 1903 | * a permanent log reservation with a long running transaction (see |
| 1904 | * xfs_itruncate_finish)). To solve this, we must check if the directory |
| 1905 | * is in the ail and use lock_nowait. If we can't lock, we need to |
| 1906 | * drop the inode lock on the directory and try again. xfs_iunlock will |
| 1907 | * potentially push the tail if we were holding up the log. |
| 1908 | */ |
| 1909 | STATIC int |
| 1910 | xfs_lock_dir_and_entry( |
| 1911 | xfs_inode_t *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | xfs_inode_t *ip) /* inode of entry 'name' */ |
| 1913 | { |
| 1914 | int attempts; |
| 1915 | xfs_ino_t e_inum; |
| 1916 | xfs_inode_t *ips[2]; |
| 1917 | xfs_log_item_t *lp; |
| 1918 | |
| 1919 | #ifdef DEBUG |
| 1920 | xfs_rm_locks++; |
| 1921 | #endif |
| 1922 | attempts = 0; |
| 1923 | |
| 1924 | again: |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1925 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
| 1927 | e_inum = ip->i_ino; |
| 1928 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1929 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | |
| 1931 | /* |
| 1932 | * We want to lock in increasing inum. Since we've already |
| 1933 | * acquired the lock on the directory, we may need to release |
| 1934 | * if if the inum of the entry turns out to be less. |
| 1935 | */ |
| 1936 | if (e_inum > dp->i_ino) { |
| 1937 | /* |
| 1938 | * We are already in the right order, so just |
| 1939 | * lock on the inode of the entry. |
| 1940 | * We need to use nowait if dp is in the AIL. |
| 1941 | */ |
| 1942 | |
| 1943 | lp = (xfs_log_item_t *)dp->i_itemp; |
| 1944 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 1945 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
| 1946 | attempts++; |
| 1947 | #ifdef DEBUG |
| 1948 | xfs_rm_attempts++; |
| 1949 | #endif |
| 1950 | |
| 1951 | /* |
| 1952 | * Unlock dp and try again. |
| 1953 | * xfs_iunlock will try to push the tail |
| 1954 | * if the inode is in the AIL. |
| 1955 | */ |
| 1956 | |
| 1957 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1958 | |
| 1959 | if ((attempts % 5) == 0) { |
| 1960 | delay(1); /* Don't just spin the CPU */ |
| 1961 | #ifdef DEBUG |
| 1962 | xfs_rm_lock_delays++; |
| 1963 | #endif |
| 1964 | } |
| 1965 | goto again; |
| 1966 | } |
| 1967 | } else { |
| 1968 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1969 | } |
| 1970 | } else if (e_inum < dp->i_ino) { |
| 1971 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1972 | |
| 1973 | ips[0] = ip; |
| 1974 | ips[1] = dp; |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 1975 | xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | } |
| 1977 | /* else e_inum == dp->i_ino */ |
| 1978 | /* This can happen if we're asked to lock /x/.. |
| 1979 | * the entry is "..", which is also the parent directory. |
| 1980 | */ |
| 1981 | |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | #ifdef DEBUG |
| 1986 | int xfs_locked_n; |
| 1987 | int xfs_small_retries; |
| 1988 | int xfs_middle_retries; |
| 1989 | int xfs_lots_retries; |
| 1990 | int xfs_lock_delays; |
| 1991 | #endif |
| 1992 | |
| 1993 | /* |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1994 | * Bump the subclass so xfs_lock_inodes() acquires each lock with |
| 1995 | * a different value |
| 1996 | */ |
| 1997 | static inline int |
| 1998 | xfs_lock_inumorder(int lock_mode, int subclass) |
| 1999 | { |
| 2000 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 2001 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2002 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 2003 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2004 | |
| 2005 | return lock_mode; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | * The following routine will lock n inodes in exclusive mode. |
| 2010 | * We assume the caller calls us with the inodes in i_ino order. |
| 2011 | * |
| 2012 | * We need to detect deadlock where an inode that we lock |
| 2013 | * is in the AIL and we start waiting for another inode that is locked |
| 2014 | * by a thread in a long running transaction (such as truncate). This can |
| 2015 | * result in deadlock since the long running trans might need to wait |
| 2016 | * for the inode we just locked in order to push the tail and free space |
| 2017 | * in the log. |
| 2018 | */ |
| 2019 | void |
| 2020 | xfs_lock_inodes( |
| 2021 | xfs_inode_t **ips, |
| 2022 | int inodes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | uint lock_mode) |
| 2024 | { |
| 2025 | int attempts = 0, i, j, try_lock; |
| 2026 | xfs_log_item_t *lp; |
| 2027 | |
| 2028 | ASSERT(ips && (inodes >= 2)); /* we need at least two */ |
| 2029 | |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 2030 | try_lock = 0; |
| 2031 | i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
| 2033 | again: |
| 2034 | for (; i < inodes; i++) { |
| 2035 | ASSERT(ips[i]); |
| 2036 | |
| 2037 | if (i && (ips[i] == ips[i-1])) /* Already locked */ |
| 2038 | continue; |
| 2039 | |
| 2040 | /* |
| 2041 | * If try_lock is not set yet, make sure all locked inodes |
| 2042 | * are not in the AIL. |
| 2043 | * If any are, set try_lock to be used later. |
| 2044 | */ |
| 2045 | |
| 2046 | if (!try_lock) { |
| 2047 | for (j = (i - 1); j >= 0 && !try_lock; j--) { |
| 2048 | lp = (xfs_log_item_t *)ips[j]->i_itemp; |
| 2049 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 2050 | try_lock++; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * If any of the previous locks we have locked is in the AIL, |
| 2057 | * we must TRY to get the second and subsequent locks. If |
| 2058 | * we can't get any, we must release all we have |
| 2059 | * and try again. |
| 2060 | */ |
| 2061 | |
| 2062 | if (try_lock) { |
| 2063 | /* try_lock must be 0 if i is 0. */ |
| 2064 | /* |
| 2065 | * try_lock means we have an inode locked |
| 2066 | * that is in the AIL. |
| 2067 | */ |
| 2068 | ASSERT(i != 0); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2069 | if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | attempts++; |
| 2071 | |
| 2072 | /* |
| 2073 | * Unlock all previous guys and try again. |
| 2074 | * xfs_iunlock will try to push the tail |
| 2075 | * if the inode is in the AIL. |
| 2076 | */ |
| 2077 | |
| 2078 | for(j = i - 1; j >= 0; j--) { |
| 2079 | |
| 2080 | /* |
| 2081 | * Check to see if we've already |
| 2082 | * unlocked this one. |
| 2083 | * Not the first one going back, |
| 2084 | * and the inode ptr is the same. |
| 2085 | */ |
| 2086 | if ((j != (i - 1)) && ips[j] == |
| 2087 | ips[j+1]) |
| 2088 | continue; |
| 2089 | |
| 2090 | xfs_iunlock(ips[j], lock_mode); |
| 2091 | } |
| 2092 | |
| 2093 | if ((attempts % 5) == 0) { |
| 2094 | delay(1); /* Don't just spin the CPU */ |
| 2095 | #ifdef DEBUG |
| 2096 | xfs_lock_delays++; |
| 2097 | #endif |
| 2098 | } |
| 2099 | i = 0; |
| 2100 | try_lock = 0; |
| 2101 | goto again; |
| 2102 | } |
| 2103 | } else { |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2104 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | #ifdef DEBUG |
| 2109 | if (attempts) { |
| 2110 | if (attempts < 5) xfs_small_retries++; |
| 2111 | else if (attempts < 100) xfs_middle_retries++; |
| 2112 | else xfs_lots_retries++; |
| 2113 | } else { |
| 2114 | xfs_locked_n++; |
| 2115 | } |
| 2116 | #endif |
| 2117 | } |
| 2118 | |
| 2119 | #ifdef DEBUG |
| 2120 | #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);} |
| 2121 | int remove_which_error_return = 0; |
| 2122 | #else /* ! DEBUG */ |
| 2123 | #define REMOVE_DEBUG_TRACE(x) |
| 2124 | #endif /* ! DEBUG */ |
| 2125 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2126 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | xfs_remove( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2128 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2129 | struct xfs_name *name, |
| 2130 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2132 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | xfs_trans_t *tp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | int error = 0; |
| 2135 | xfs_bmap_free_t free_list; |
| 2136 | xfs_fsblock_t first_block; |
| 2137 | int cancel_flags; |
| 2138 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | int link_zero; |
| 2140 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2142 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2145 | return XFS_ERROR(EIO); |
| 2146 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2147 | if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2148 | error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL, |
| 2149 | NULL, DM_RIGHT_NULL, name->name, NULL, |
| 2150 | ip->i_d.di_mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | if (error) |
| 2152 | return error; |
| 2153 | } |
| 2154 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2155 | xfs_itrace_entry(ip); |
| 2156 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
| 2158 | error = XFS_QM_DQATTACH(mp, dp, 0); |
Christoph Hellwig | 82dab94 | 2008-04-22 17:34:18 +1000 | [diff] [blame] | 2159 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | error = XFS_QM_DQATTACH(mp, ip, 0); |
| 2161 | if (error) { |
| 2162 | REMOVE_DEBUG_TRACE(__LINE__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | goto std_return; |
| 2164 | } |
| 2165 | |
| 2166 | tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); |
| 2167 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2168 | /* |
| 2169 | * We try to get the real space reservation first, |
| 2170 | * allowing for directory btree deletion(s) implying |
| 2171 | * possible bmap insert(s). If we can't get the space |
| 2172 | * reservation then we use 0 instead, and avoid the bmap |
| 2173 | * btree insert(s) in the directory code by, if the bmap |
| 2174 | * insert tries to happen, instead trimming the LAST |
| 2175 | * block from the directory. |
| 2176 | */ |
| 2177 | resblks = XFS_REMOVE_SPACE_RES(mp); |
| 2178 | error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, |
| 2179 | XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT); |
| 2180 | if (error == ENOSPC) { |
| 2181 | resblks = 0; |
| 2182 | error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, |
| 2183 | XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT); |
| 2184 | } |
| 2185 | if (error) { |
| 2186 | ASSERT(error != ENOSPC); |
| 2187 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2188 | xfs_trans_cancel(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | return error; |
| 2190 | } |
| 2191 | |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 2192 | error = xfs_lock_dir_and_entry(dp, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | if (error) { |
| 2194 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2195 | xfs_trans_cancel(tp, cancel_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | goto std_return; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * At this point, we've gotten both the directory and the entry |
| 2201 | * inodes locked. |
| 2202 | */ |
Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 2203 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 82dab94 | 2008-04-22 17:34:18 +1000 | [diff] [blame] | 2205 | |
| 2206 | IHOLD(dp); |
| 2207 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
| 2209 | /* |
| 2210 | * Entry must exist since we did a lookup in xfs_lock_dir_and_entry. |
| 2211 | */ |
| 2212 | XFS_BMAP_INIT(&free_list, &first_block); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2213 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, |
Christoph Hellwig | d4377d8 | 2008-04-22 17:33:46 +1000 | [diff] [blame] | 2214 | &first_block, &free_list, resblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | if (error) { |
| 2216 | ASSERT(error != ENOENT); |
| 2217 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2218 | goto error1; |
| 2219 | } |
| 2220 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2221 | |
| 2222 | dp->i_gen++; |
| 2223 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 2224 | |
| 2225 | error = xfs_droplink(tp, ip); |
| 2226 | if (error) { |
| 2227 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2228 | goto error1; |
| 2229 | } |
| 2230 | |
| 2231 | /* Determine if this is the last link while |
| 2232 | * we are in the transaction. |
| 2233 | */ |
| 2234 | link_zero = (ip)->i_d.di_nlink==0; |
| 2235 | |
| 2236 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | * If this is a synchronous mount, make sure that the |
| 2238 | * remove transaction goes to disk before returning to |
| 2239 | * the user. |
| 2240 | */ |
| 2241 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2242 | xfs_trans_set_sync(tp); |
| 2243 | } |
| 2244 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2245 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | if (error) { |
| 2247 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2248 | goto error_rele; |
| 2249 | } |
| 2250 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2251 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 2252 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | goto std_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | |
| 2255 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2256 | * If we are using filestreams, kill the stream association. |
| 2257 | * If the file is still open it may get a new one but that |
| 2258 | * will get killed on last close in xfs_close() so we don't |
| 2259 | * have to worry about that. |
| 2260 | */ |
| 2261 | if (link_zero && xfs_inode_is_filestream(ip)) |
| 2262 | xfs_filestream_deassociate(ip); |
| 2263 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2264 | xfs_itrace_exit(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | |
| 2266 | /* Fall through to std_return with error = 0 */ |
| 2267 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2268 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2270 | dp, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2272 | name->name, NULL, ip->i_d.di_mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | } |
| 2274 | return error; |
| 2275 | |
| 2276 | error1: |
| 2277 | xfs_bmap_cancel(&free_list); |
| 2278 | cancel_flags |= XFS_TRANS_ABORT; |
| 2279 | xfs_trans_cancel(tp, cancel_flags); |
| 2280 | goto std_return; |
| 2281 | |
| 2282 | error_rele: |
| 2283 | /* |
| 2284 | * In this case make sure to not release the inode until after |
| 2285 | * the current transaction is aborted. Releasing it beforehand |
| 2286 | * can cause us to go to xfs_inactive and start a recursive |
| 2287 | * transaction which can easily deadlock with the current one. |
| 2288 | */ |
| 2289 | xfs_bmap_cancel(&free_list); |
| 2290 | cancel_flags |= XFS_TRANS_ABORT; |
| 2291 | xfs_trans_cancel(tp, cancel_flags); |
| 2292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | goto std_return; |
| 2294 | } |
| 2295 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2296 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | xfs_link( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2298 | xfs_inode_t *tdp, |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2299 | xfs_inode_t *sip, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2300 | struct xfs_name *target_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2302 | xfs_mount_t *mp = tdp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | xfs_inode_t *ips[2]; |
| 2305 | int error; |
| 2306 | xfs_bmap_free_t free_list; |
| 2307 | xfs_fsblock_t first_block; |
| 2308 | int cancel_flags; |
| 2309 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | int resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2312 | xfs_itrace_entry(tdp); |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2313 | xfs_itrace_entry(sip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2315 | ASSERT(!S_ISDIR(sip->i_d.di_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2318 | return XFS_ERROR(EIO); |
| 2319 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2320 | if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2322 | tdp, DM_RIGHT_NULL, |
| 2323 | sip, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2324 | target_name->name, NULL, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | if (error) |
| 2326 | return error; |
| 2327 | } |
| 2328 | |
| 2329 | /* Return through std_return after this point. */ |
| 2330 | |
| 2331 | error = XFS_QM_DQATTACH(mp, sip, 0); |
| 2332 | if (!error && sip != tdp) |
| 2333 | error = XFS_QM_DQATTACH(mp, tdp, 0); |
| 2334 | if (error) |
| 2335 | goto std_return; |
| 2336 | |
| 2337 | tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); |
| 2338 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2339 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0, |
| 2341 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 2342 | if (error == ENOSPC) { |
| 2343 | resblks = 0; |
| 2344 | error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0, |
| 2345 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 2346 | } |
| 2347 | if (error) { |
| 2348 | cancel_flags = 0; |
| 2349 | goto error_return; |
| 2350 | } |
| 2351 | |
| 2352 | if (sip->i_ino < tdp->i_ino) { |
| 2353 | ips[0] = sip; |
| 2354 | ips[1] = tdp; |
| 2355 | } else { |
| 2356 | ips[0] = tdp; |
| 2357 | ips[1] = sip; |
| 2358 | } |
| 2359 | |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 2360 | xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* |
| 2363 | * Increment vnode ref counts since xfs_trans_commit & |
| 2364 | * xfs_trans_cancel will both unlock the inodes and |
| 2365 | * decrement the associated ref counts. |
| 2366 | */ |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2367 | IHOLD(sip); |
| 2368 | IHOLD(tdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); |
| 2370 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); |
| 2371 | |
| 2372 | /* |
| 2373 | * If the source has too many links, we can't make any more to it. |
| 2374 | */ |
| 2375 | if (sip->i_d.di_nlink >= XFS_MAXLINK) { |
| 2376 | error = XFS_ERROR(EMLINK); |
| 2377 | goto error_return; |
| 2378 | } |
| 2379 | |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2380 | /* |
| 2381 | * If we are using project inheritance, we only allow hard link |
| 2382 | * creation in our tree when the project IDs are the same; else |
| 2383 | * the tree quota mechanism could be circumvented. |
| 2384 | */ |
| 2385 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && |
| 2386 | (tdp->i_d.di_projid != sip->i_d.di_projid))) { |
Nathan Scott | b1ecdda | 2006-05-08 19:51:42 +1000 | [diff] [blame] | 2387 | error = XFS_ERROR(EXDEV); |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2388 | goto error_return; |
| 2389 | } |
| 2390 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2391 | error = xfs_dir_canenter(tp, tdp, target_name, resblks); |
| 2392 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | goto error_return; |
| 2394 | |
| 2395 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2396 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2397 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, |
| 2398 | &first_block, &free_list, resblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | if (error) |
| 2400 | goto abort_return; |
| 2401 | xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2402 | tdp->i_gen++; |
| 2403 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); |
| 2404 | |
| 2405 | error = xfs_bumplink(tp, sip); |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2406 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | goto abort_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | |
| 2409 | /* |
| 2410 | * If this is a synchronous mount, make sure that the |
| 2411 | * link transaction goes to disk before returning to |
| 2412 | * the user. |
| 2413 | */ |
| 2414 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2415 | xfs_trans_set_sync(tp); |
| 2416 | } |
| 2417 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2418 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | if (error) { |
| 2420 | xfs_bmap_cancel(&free_list); |
| 2421 | goto abort_return; |
| 2422 | } |
| 2423 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2424 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2425 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | goto std_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | |
| 2428 | /* Fall through to std_return with error = 0. */ |
| 2429 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2430 | if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2432 | tdp, DM_RIGHT_NULL, |
| 2433 | sip, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2434 | target_name->name, NULL, 0, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
| 2436 | return error; |
| 2437 | |
| 2438 | abort_return: |
| 2439 | cancel_flags |= XFS_TRANS_ABORT; |
| 2440 | /* FALLTHROUGH */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | error_return: |
| 2443 | xfs_trans_cancel(tp, cancel_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | goto std_return; |
| 2445 | } |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2446 | |
| 2447 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2448 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | xfs_mkdir( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2450 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2451 | struct xfs_name *dir_name, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2452 | mode_t mode, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2453 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | cred_t *credp) |
| 2455 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2456 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | xfs_inode_t *cdp; /* inode of created dir */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | int cancel_flags; |
| 2460 | int error; |
| 2461 | int committed; |
| 2462 | xfs_bmap_free_t free_list; |
| 2463 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2464 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | boolean_t created = B_FALSE; |
| 2466 | int dm_event_sent = 0; |
| 2467 | xfs_prid_t prid; |
| 2468 | struct xfs_dquot *udqp, *gdqp; |
| 2469 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | |
| 2471 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2472 | return XFS_ERROR(EIO); |
| 2473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | tp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2476 | if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2478 | dp, DM_RIGHT_NULL, NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2479 | DM_RIGHT_NULL, dir_name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2480 | mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | if (error) |
| 2482 | return error; |
| 2483 | dm_event_sent = 1; |
| 2484 | } |
| 2485 | |
| 2486 | /* Return through std_return after this point. */ |
| 2487 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2488 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
| 2490 | mp = dp->i_mount; |
| 2491 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2492 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 2493 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | else |
| 2495 | prid = (xfs_prid_t)dfltprid; |
| 2496 | |
| 2497 | /* |
| 2498 | * Make sure that we have allocated dquot(s) on disk. |
| 2499 | */ |
| 2500 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2501 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 2503 | if (error) |
| 2504 | goto std_return; |
| 2505 | |
| 2506 | tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); |
| 2507 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2508 | resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0, |
| 2510 | XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT); |
| 2511 | if (error == ENOSPC) { |
| 2512 | resblks = 0; |
| 2513 | error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0, |
| 2514 | XFS_TRANS_PERM_LOG_RES, |
| 2515 | XFS_MKDIR_LOG_COUNT); |
| 2516 | } |
| 2517 | if (error) { |
| 2518 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | goto error_return; |
| 2520 | } |
| 2521 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2522 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2523 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
| 2525 | /* |
| 2526 | * Check for directory link count overflow. |
| 2527 | */ |
| 2528 | if (dp->i_d.di_nlink >= XFS_MAXLINK) { |
| 2529 | error = XFS_ERROR(EMLINK); |
| 2530 | goto error_return; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * Reserve disk quota and the inode. |
| 2535 | */ |
| 2536 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 2537 | if (error) |
| 2538 | goto error_return; |
| 2539 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2540 | error = xfs_dir_canenter(tp, dp, dir_name, resblks); |
| 2541 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | goto error_return; |
| 2543 | /* |
| 2544 | * create the directory inode. |
| 2545 | */ |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2546 | error = xfs_dir_ialloc(&tp, dp, mode, 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | 0, credp, prid, resblks > 0, |
| 2548 | &cdp, NULL); |
| 2549 | if (error) { |
| 2550 | if (error == ENOSPC) |
| 2551 | goto error_return; |
| 2552 | goto abort_return; |
| 2553 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2554 | xfs_itrace_ref(cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | |
| 2556 | /* |
| 2557 | * Now we add the directory inode to the transaction. |
| 2558 | * We waited until now since xfs_dir_ialloc might start |
| 2559 | * a new transaction. Had we joined the transaction |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2560 | * earlier, the locks might have gotten released. An error |
| 2561 | * from here on will result in the transaction cancel |
| 2562 | * unlocking dp so don't do it explicitly in the error path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | */ |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2564 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2566 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
| 2568 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2569 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2570 | error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino, |
| 2571 | &first_block, &free_list, resblks ? |
| 2572 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | if (error) { |
| 2574 | ASSERT(error != ENOSPC); |
| 2575 | goto error1; |
| 2576 | } |
| 2577 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2578 | |
| 2579 | /* |
| 2580 | * Bump the in memory version number of the parent directory |
| 2581 | * so that other processes accessing it will recognize that |
| 2582 | * the directory has changed. |
| 2583 | */ |
| 2584 | dp->i_gen++; |
| 2585 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2586 | error = xfs_dir_init(tp, cdp, dp); |
| 2587 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | |
| 2590 | cdp->i_gen = 1; |
| 2591 | error = xfs_bumplink(tp, dp); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2592 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | created = B_TRUE; |
| 2596 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2597 | *ipp = cdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | IHOLD(cdp); |
| 2599 | |
| 2600 | /* |
| 2601 | * Attach the dquots to the new inode and modify the icount incore. |
| 2602 | */ |
| 2603 | XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp); |
| 2604 | |
| 2605 | /* |
| 2606 | * If this is a synchronous mount, make sure that the |
| 2607 | * mkdir transaction goes to disk before returning to |
| 2608 | * the user. |
| 2609 | */ |
| 2610 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2611 | xfs_trans_set_sync(tp); |
| 2612 | } |
| 2613 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2614 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | if (error) { |
| 2616 | IRELE(cdp); |
| 2617 | goto error2; |
| 2618 | } |
| 2619 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2620 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | XFS_QM_DQRELE(mp, udqp); |
| 2622 | XFS_QM_DQRELE(mp, gdqp); |
| 2623 | if (error) { |
| 2624 | IRELE(cdp); |
| 2625 | } |
| 2626 | |
| 2627 | /* Fall through to std_return with error = 0 or errno from |
| 2628 | * xfs_trans_commit. */ |
| 2629 | |
| 2630 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2631 | if ((created || (error != 0 && dm_event_sent != 0)) && |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2632 | DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2634 | dp, DM_RIGHT_NULL, |
| 2635 | created ? cdp : NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2637 | dir_name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2638 | mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | } |
| 2640 | return error; |
| 2641 | |
| 2642 | error2: |
| 2643 | error1: |
| 2644 | xfs_bmap_cancel(&free_list); |
| 2645 | abort_return: |
| 2646 | cancel_flags |= XFS_TRANS_ABORT; |
| 2647 | error_return: |
| 2648 | xfs_trans_cancel(tp, cancel_flags); |
| 2649 | XFS_QM_DQRELE(mp, udqp); |
| 2650 | XFS_QM_DQRELE(mp, gdqp); |
| 2651 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2652 | if (unlock_dp_on_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | |
| 2655 | goto std_return; |
| 2656 | } |
| 2657 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2658 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | xfs_rmdir( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2660 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2661 | struct xfs_name *name, |
| 2662 | xfs_inode_t *cdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2664 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | int error; |
| 2667 | xfs_bmap_free_t free_list; |
| 2668 | xfs_fsblock_t first_block; |
| 2669 | int cancel_flags; |
| 2670 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | int last_cdp_link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | uint resblks; |
| 2673 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2674 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2676 | if (XFS_FORCED_SHUTDOWN(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | return XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2679 | if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2681 | dp, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2682 | NULL, DM_RIGHT_NULL, name->name, |
| 2683 | NULL, cdp->i_d.di_mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | if (error) |
| 2685 | return XFS_ERROR(error); |
| 2686 | } |
| 2687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | * Get the dquots for the inodes. |
| 2690 | */ |
| 2691 | error = XFS_QM_DQATTACH(mp, dp, 0); |
Christoph Hellwig | 82dab94 | 2008-04-22 17:34:18 +1000 | [diff] [blame] | 2692 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | error = XFS_QM_DQATTACH(mp, cdp, 0); |
| 2694 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2696 | goto std_return; |
| 2697 | } |
| 2698 | |
| 2699 | tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); |
| 2700 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2701 | /* |
| 2702 | * We try to get the real space reservation first, |
| 2703 | * allowing for directory btree deletion(s) implying |
| 2704 | * possible bmap insert(s). If we can't get the space |
| 2705 | * reservation then we use 0 instead, and avoid the bmap |
| 2706 | * btree insert(s) in the directory code by, if the bmap |
| 2707 | * insert tries to happen, instead trimming the LAST |
| 2708 | * block from the directory. |
| 2709 | */ |
| 2710 | resblks = XFS_REMOVE_SPACE_RES(mp); |
| 2711 | error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, |
| 2712 | XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT); |
| 2713 | if (error == ENOSPC) { |
| 2714 | resblks = 0; |
| 2715 | error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, |
| 2716 | XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT); |
| 2717 | } |
| 2718 | if (error) { |
| 2719 | ASSERT(error != ENOSPC); |
| 2720 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | goto error_return; |
| 2722 | } |
| 2723 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2724 | |
| 2725 | /* |
| 2726 | * Now lock the child directory inode and the parent directory |
| 2727 | * inode in the proper order. This will take care of validating |
| 2728 | * that the directory entry for the child directory inode has |
| 2729 | * not changed while we were obtaining a log reservation. |
| 2730 | */ |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 2731 | error = xfs_lock_dir_and_entry(dp, cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | if (error) { |
| 2733 | xfs_trans_cancel(tp, cancel_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | goto std_return; |
| 2735 | } |
| 2736 | |
Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 2737 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | |
Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 2740 | IHOLD(cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL); |
| 2742 | |
| 2743 | ASSERT(cdp->i_d.di_nlink >= 2); |
| 2744 | if (cdp->i_d.di_nlink != 2) { |
| 2745 | error = XFS_ERROR(ENOTEMPTY); |
| 2746 | goto error_return; |
| 2747 | } |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2748 | if (!xfs_dir_isempty(cdp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | error = XFS_ERROR(ENOTEMPTY); |
| 2750 | goto error_return; |
| 2751 | } |
| 2752 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2753 | error = xfs_dir_removename(tp, dp, name, cdp->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2754 | &first_block, &free_list, resblks); |
| 2755 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | |
| 2758 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2759 | |
| 2760 | /* |
| 2761 | * Bump the in memory generation count on the parent |
| 2762 | * directory so that other can know that it has changed. |
| 2763 | */ |
| 2764 | dp->i_gen++; |
| 2765 | |
| 2766 | /* |
| 2767 | * Drop the link from cdp's "..". |
| 2768 | */ |
| 2769 | error = xfs_droplink(tp, dp); |
| 2770 | if (error) { |
| 2771 | goto error1; |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | * Drop the link from dp to cdp. |
| 2776 | */ |
| 2777 | error = xfs_droplink(tp, cdp); |
| 2778 | if (error) { |
| 2779 | goto error1; |
| 2780 | } |
| 2781 | |
| 2782 | /* |
| 2783 | * Drop the "." link from cdp to self. |
| 2784 | */ |
| 2785 | error = xfs_droplink(tp, cdp); |
| 2786 | if (error) { |
| 2787 | goto error1; |
| 2788 | } |
| 2789 | |
| 2790 | /* Determine these before committing transaction */ |
| 2791 | last_cdp_link = (cdp)->i_d.di_nlink==0; |
| 2792 | |
| 2793 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | * If this is a synchronous mount, make sure that the |
| 2795 | * rmdir transaction goes to disk before returning to |
| 2796 | * the user. |
| 2797 | */ |
| 2798 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2799 | xfs_trans_set_sync(tp); |
| 2800 | } |
| 2801 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2802 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | if (error) { |
| 2804 | xfs_bmap_cancel(&free_list); |
| 2805 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | |
| 2806 | XFS_TRANS_ABORT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | goto std_return; |
| 2808 | } |
| 2809 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2810 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | goto std_return; |
| 2813 | } |
| 2814 | |
| 2815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | /* Fall through to std_return with error = 0 or the errno |
| 2817 | * from xfs_trans_commit. */ |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 2818 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2819 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2821 | dp, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2823 | name->name, NULL, cdp->i_d.di_mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | error, 0); |
| 2825 | } |
| 2826 | return error; |
| 2827 | |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 2828 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | xfs_bmap_cancel(&free_list); |
| 2830 | cancel_flags |= XFS_TRANS_ABORT; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2831 | /* FALLTHROUGH */ |
| 2832 | |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 2833 | error_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | xfs_trans_cancel(tp, cancel_flags); |
| 2835 | goto std_return; |
| 2836 | } |
| 2837 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2838 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | xfs_symlink( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2840 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2841 | struct xfs_name *link_name, |
| 2842 | const char *target_path, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2843 | mode_t mode, |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2844 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | cred_t *credp) |
| 2846 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2847 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | xfs_inode_t *ip; |
| 2850 | int error; |
| 2851 | int pathlen; |
| 2852 | xfs_bmap_free_t free_list; |
| 2853 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2854 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | uint cancel_flags; |
| 2856 | int committed; |
| 2857 | xfs_fileoff_t first_fsb; |
| 2858 | xfs_filblks_t fs_blocks; |
| 2859 | int nmaps; |
| 2860 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 2861 | xfs_daddr_t d; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2862 | const char *cur_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | int byte_cnt; |
| 2864 | int n; |
| 2865 | xfs_buf_t *bp; |
| 2866 | xfs_prid_t prid; |
| 2867 | struct xfs_dquot *udqp, *gdqp; |
| 2868 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2870 | *ipp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | error = 0; |
| 2872 | ip = NULL; |
| 2873 | tp = NULL; |
| 2874 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2875 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | |
| 2877 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2878 | return XFS_ERROR(EIO); |
| 2879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | /* |
| 2881 | * Check component lengths of the target path name. |
| 2882 | */ |
| 2883 | pathlen = strlen(target_path); |
| 2884 | if (pathlen >= MAXPATHLEN) /* total string too long */ |
| 2885 | return XFS_ERROR(ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2887 | if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) { |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2888 | error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | DM_RIGHT_NULL, NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2890 | link_name->name, target_path, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | if (error) |
| 2892 | return error; |
| 2893 | } |
| 2894 | |
| 2895 | /* Return through std_return after this point. */ |
| 2896 | |
| 2897 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2898 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 2899 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | else |
| 2901 | prid = (xfs_prid_t)dfltprid; |
| 2902 | |
| 2903 | /* |
| 2904 | * Make sure that we have allocated dquot(s) on disk. |
| 2905 | */ |
| 2906 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2907 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 2909 | if (error) |
| 2910 | goto std_return; |
| 2911 | |
| 2912 | tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); |
| 2913 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2914 | /* |
| 2915 | * The symlink will fit into the inode data fork? |
| 2916 | * There can't be any attributes so we get the whole variable part. |
| 2917 | */ |
| 2918 | if (pathlen <= XFS_LITINO(mp)) |
| 2919 | fs_blocks = 0; |
| 2920 | else |
| 2921 | fs_blocks = XFS_B_TO_FSB(mp, pathlen); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2922 | resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0, |
| 2924 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 2925 | if (error == ENOSPC && fs_blocks == 0) { |
| 2926 | resblks = 0; |
| 2927 | error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, |
| 2928 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 2929 | } |
| 2930 | if (error) { |
| 2931 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | goto error_return; |
| 2933 | } |
| 2934 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2935 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2936 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | |
| 2938 | /* |
| 2939 | * Check whether the directory allows new symlinks or not. |
| 2940 | */ |
| 2941 | if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { |
| 2942 | error = XFS_ERROR(EPERM); |
| 2943 | goto error_return; |
| 2944 | } |
| 2945 | |
| 2946 | /* |
| 2947 | * Reserve disk quota : blocks and inode. |
| 2948 | */ |
| 2949 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 2950 | if (error) |
| 2951 | goto error_return; |
| 2952 | |
| 2953 | /* |
| 2954 | * Check for ability to enter directory entry, if no space reserved. |
| 2955 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2956 | error = xfs_dir_canenter(tp, dp, link_name, resblks); |
| 2957 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | goto error_return; |
| 2959 | /* |
| 2960 | * Initialize the bmap freelist prior to calling either |
| 2961 | * bmapi or the directory create code. |
| 2962 | */ |
| 2963 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2964 | |
| 2965 | /* |
| 2966 | * Allocate an inode for the symlink. |
| 2967 | */ |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2968 | error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | 1, 0, credp, prid, resblks > 0, &ip, NULL); |
| 2970 | if (error) { |
| 2971 | if (error == ENOSPC) |
| 2972 | goto error_return; |
| 2973 | goto error1; |
| 2974 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2975 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2977 | /* |
| 2978 | * An error after we've joined dp to the transaction will result in the |
| 2979 | * transaction cancel unlocking dp so don't do it explicitly in the |
| 2980 | * error path. |
| 2981 | */ |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 2982 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2984 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | |
| 2986 | /* |
| 2987 | * Also attach the dquot(s) to it, if applicable. |
| 2988 | */ |
| 2989 | XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp); |
| 2990 | |
| 2991 | if (resblks) |
| 2992 | resblks -= XFS_IALLOC_SPACE_RES(mp); |
| 2993 | /* |
| 2994 | * If the symlink will fit into the inode, write it inline. |
| 2995 | */ |
| 2996 | if (pathlen <= XFS_IFORK_DSIZE(ip)) { |
| 2997 | xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK); |
| 2998 | memcpy(ip->i_df.if_u1.if_data, target_path, pathlen); |
| 2999 | ip->i_d.di_size = pathlen; |
| 3000 | |
| 3001 | /* |
| 3002 | * The inode was initially created in extent format. |
| 3003 | */ |
| 3004 | ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); |
| 3005 | ip->i_df.if_flags |= XFS_IFINLINE; |
| 3006 | |
| 3007 | ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; |
| 3008 | xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); |
| 3009 | |
| 3010 | } else { |
| 3011 | first_fsb = 0; |
| 3012 | nmaps = SYMLINK_MAPS; |
| 3013 | |
| 3014 | error = xfs_bmapi(tp, ip, first_fsb, fs_blocks, |
| 3015 | XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, |
| 3016 | &first_block, resblks, mval, &nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3017 | &free_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | if (error) { |
| 3019 | goto error1; |
| 3020 | } |
| 3021 | |
| 3022 | if (resblks) |
| 3023 | resblks -= fs_blocks; |
| 3024 | ip->i_d.di_size = pathlen; |
| 3025 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3026 | |
| 3027 | cur_chunk = target_path; |
| 3028 | for (n = 0; n < nmaps; n++) { |
| 3029 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 3030 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 3031 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
| 3032 | BTOBB(byte_cnt), 0); |
| 3033 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); |
| 3034 | if (pathlen < byte_cnt) { |
| 3035 | byte_cnt = pathlen; |
| 3036 | } |
| 3037 | pathlen -= byte_cnt; |
| 3038 | |
| 3039 | memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt); |
| 3040 | cur_chunk += byte_cnt; |
| 3041 | |
| 3042 | xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1); |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | /* |
| 3047 | * Create the directory entry for the symlink. |
| 3048 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3049 | error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, |
| 3050 | &first_block, &free_list, resblks); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 3051 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 3054 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 3055 | |
| 3056 | /* |
| 3057 | * Bump the in memory version number of the parent directory |
| 3058 | * so that other processes accessing it will recognize that |
| 3059 | * the directory has changed. |
| 3060 | */ |
| 3061 | dp->i_gen++; |
| 3062 | |
| 3063 | /* |
| 3064 | * If this is a synchronous mount, make sure that the |
| 3065 | * symlink transaction goes to disk before returning to |
| 3066 | * the user. |
| 3067 | */ |
| 3068 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 3069 | xfs_trans_set_sync(tp); |
| 3070 | } |
| 3071 | |
| 3072 | /* |
| 3073 | * xfs_trans_commit normally decrements the vnode ref count |
| 3074 | * when it unlocks the inode. Since we want to return the |
| 3075 | * vnode to the caller, we bump the vnode ref count now. |
| 3076 | */ |
| 3077 | IHOLD(ip); |
| 3078 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3079 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | if (error) { |
| 3081 | goto error2; |
| 3082 | } |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3083 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | XFS_QM_DQRELE(mp, udqp); |
| 3085 | XFS_QM_DQRELE(mp, gdqp); |
| 3086 | |
| 3087 | /* Fall through to std_return with error = 0 or errno from |
| 3088 | * xfs_trans_commit */ |
| 3089 | std_return: |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3090 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3092 | dp, DM_RIGHT_NULL, |
| 3093 | error ? NULL : ip, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3094 | DM_RIGHT_NULL, link_name->name, |
| 3095 | target_path, 0, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 3098 | if (!error) |
| 3099 | *ipp = ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | return error; |
| 3101 | |
| 3102 | error2: |
| 3103 | IRELE(ip); |
| 3104 | error1: |
| 3105 | xfs_bmap_cancel(&free_list); |
| 3106 | cancel_flags |= XFS_TRANS_ABORT; |
| 3107 | error_return: |
| 3108 | xfs_trans_cancel(tp, cancel_flags); |
| 3109 | XFS_QM_DQRELE(mp, udqp); |
| 3110 | XFS_QM_DQRELE(mp, gdqp); |
| 3111 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3112 | if (unlock_dp_on_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
| 3115 | goto std_return; |
| 3116 | } |
| 3117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | xfs_inode_flush( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3120 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | int flags) |
| 3122 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3123 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | int error = 0; |
| 3125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3127 | return XFS_ERROR(EIO); |
| 3128 | |
| 3129 | /* |
| 3130 | * Bypass inodes which have already been cleaned by |
| 3131 | * the inode flush clustering code inside xfs_iflush |
| 3132 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 3133 | if (xfs_inode_clean(ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | return 0; |
| 3135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | /* |
| 3137 | * We make this non-blocking if the inode is contended, |
| 3138 | * return EAGAIN to indicate to the caller that they |
| 3139 | * did not succeed. This prevents the flush path from |
| 3140 | * blocking on inodes inside another operation right |
| 3141 | * now, they get caught later by xfs_sync. |
| 3142 | */ |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3143 | if (flags & FLUSH_SYNC) { |
| 3144 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 3145 | xfs_iflock(ip); |
| 3146 | } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { |
| 3147 | if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) { |
| 3148 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | return EAGAIN; |
| 3150 | } |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3151 | } else { |
| 3152 | return EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | } |
| 3154 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3155 | error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC |
| 3156 | : XFS_IFLUSH_ASYNC_NOBLOCK); |
| 3157 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 3158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | return error; |
| 3160 | } |
| 3161 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3164 | xfs_set_dmattrs( |
| 3165 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | u_int evmask, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3167 | u_int16_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3169 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | int error; |
| 3172 | |
| 3173 | if (!capable(CAP_SYS_ADMIN)) |
| 3174 | return XFS_ERROR(EPERM); |
| 3175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3177 | return XFS_ERROR(EIO); |
| 3178 | |
| 3179 | tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS); |
| 3180 | error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0); |
| 3181 | if (error) { |
| 3182 | xfs_trans_cancel(tp, 0); |
| 3183 | return error; |
| 3184 | } |
| 3185 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3186 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3187 | |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 3188 | ip->i_d.di_dmevmask = evmask; |
| 3189 | ip->i_d.di_dmstate = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | |
| 3191 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3192 | IHOLD(ip); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3193 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | |
| 3195 | return error; |
| 3196 | } |
| 3197 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3198 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | xfs_reclaim( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3200 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3202 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3204 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | |
| 3206 | ASSERT(!VN_MAPPED(vp)); |
| 3207 | |
| 3208 | /* bad inode, get out here ASAP */ |
| 3209 | if (VN_BAD(vp)) { |
| 3210 | xfs_ireclaim(ip); |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 3214 | vn_iowait(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame] | 3216 | ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 3218 | /* |
| 3219 | * Make sure the atime in the XFS inode is correct before freeing the |
| 3220 | * Linux inode. |
| 3221 | */ |
| 3222 | xfs_synchronize_atime(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3224 | /* |
| 3225 | * If we have nothing to flush with this inode then complete the |
| 3226 | * teardown now, otherwise break the link between the xfs inode and the |
| 3227 | * linux inode and clean up the xfs inode later. This avoids flushing |
| 3228 | * the inode to disk during the delete operation itself. |
| 3229 | * |
| 3230 | * When breaking the link, we need to set the XFS_IRECLAIMABLE flag |
| 3231 | * first to ensure that xfs_iunpin() will never see an xfs inode |
| 3232 | * that has a linux inode being reclaimed. Synchronisation is provided |
| 3233 | * by the i_flags_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | */ |
| 3235 | if (!ip->i_update_core && (ip->i_itemp == NULL)) { |
| 3236 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3237 | xfs_iflock(ip); |
| 3238 | return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC); |
| 3239 | } else { |
| 3240 | xfs_mount_t *mp = ip->i_mount; |
| 3241 | |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3242 | /* Protect sync and unpin from us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | XFS_MOUNT_ILOCK(mp); |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3244 | spin_lock(&ip->i_flags_lock); |
| 3245 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 3246 | vn_to_inode(vp)->i_private = NULL; |
| 3247 | ip->i_vnode = NULL; |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3248 | spin_unlock(&ip->i_flags_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | list_add_tail(&ip->i_reclaim, &mp->m_del_inodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | XFS_MOUNT_IUNLOCK(mp); |
| 3251 | } |
| 3252 | return 0; |
| 3253 | } |
| 3254 | |
| 3255 | int |
| 3256 | xfs_finish_reclaim( |
| 3257 | xfs_inode_t *ip, |
| 3258 | int locked, |
| 3259 | int sync_mode) |
| 3260 | { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3261 | xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 3262 | bhv_vnode_t *vp = XFS_ITOV_NULL(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | int error; |
| 3264 | |
| 3265 | if (vp && VN_BAD(vp)) |
| 3266 | goto reclaim; |
| 3267 | |
| 3268 | /* The hash lock here protects a thread in xfs_iget_core from |
| 3269 | * racing with us on linking the inode back with a vnode. |
| 3270 | * Once we have the XFS_IRECLAIM flag set it will not touch |
| 3271 | * us. |
| 3272 | */ |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3273 | write_lock(&pag->pag_ici_lock); |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3274 | spin_lock(&ip->i_flags_lock); |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 3275 | if (__xfs_iflags_test(ip, XFS_IRECLAIM) || |
| 3276 | (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) { |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3277 | spin_unlock(&ip->i_flags_lock); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3278 | write_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | if (locked) { |
| 3280 | xfs_ifunlock(ip); |
| 3281 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3282 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3283 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | } |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 3285 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3286 | spin_unlock(&ip->i_flags_lock); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3287 | write_unlock(&pag->pag_ici_lock); |
| 3288 | xfs_put_perag(ip->i_mount, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | |
| 3290 | /* |
| 3291 | * If the inode is still dirty, then flush it out. If the inode |
| 3292 | * is not in the AIL, then it will be OK to flush it delwri as |
| 3293 | * long as xfs_iflush() does not keep any references to the inode. |
| 3294 | * We leave that decision up to xfs_iflush() since it has the |
| 3295 | * knowledge of whether it's OK to simply do a delwri flush of |
| 3296 | * the inode or whether we need to wait until the inode is |
| 3297 | * pulled from the AIL. |
| 3298 | * We get the flush lock regardless, though, just to make sure |
| 3299 | * we don't free it while it is being flushed. |
| 3300 | */ |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3301 | if (!locked) { |
| 3302 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3303 | xfs_iflock(ip); |
| 3304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3306 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | if (ip->i_update_core || |
| 3308 | ((ip->i_itemp != NULL) && |
| 3309 | (ip->i_itemp->ili_format.ilf_fields != 0))) { |
| 3310 | error = xfs_iflush(ip, sync_mode); |
| 3311 | /* |
| 3312 | * If we hit an error, typically because of filesystem |
| 3313 | * shutdown, we don't need to let vn_reclaim to know |
| 3314 | * because we're gonna reclaim the inode anyway. |
| 3315 | */ |
| 3316 | if (error) { |
| 3317 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3318 | goto reclaim; |
| 3319 | } |
| 3320 | xfs_iflock(ip); /* synchronize with xfs_iflush_done */ |
| 3321 | } |
| 3322 | |
| 3323 | ASSERT(ip->i_update_core == 0); |
| 3324 | ASSERT(ip->i_itemp == NULL || |
| 3325 | ip->i_itemp->ili_format.ilf_fields == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
| 3327 | |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3328 | xfs_ifunlock(ip); |
| 3329 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | reclaim: |
| 3332 | xfs_ireclaim(ip); |
| 3333 | return 0; |
| 3334 | } |
| 3335 | |
| 3336 | int |
| 3337 | xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock) |
| 3338 | { |
| 3339 | int purged; |
| 3340 | xfs_inode_t *ip, *n; |
| 3341 | int done = 0; |
| 3342 | |
| 3343 | while (!done) { |
| 3344 | purged = 0; |
| 3345 | XFS_MOUNT_ILOCK(mp); |
| 3346 | list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) { |
| 3347 | if (noblock) { |
| 3348 | if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) |
| 3349 | continue; |
| 3350 | if (xfs_ipincount(ip) || |
| 3351 | !xfs_iflock_nowait(ip)) { |
| 3352 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3353 | continue; |
| 3354 | } |
| 3355 | } |
| 3356 | XFS_MOUNT_IUNLOCK(mp); |
Felix Blyakher | 6b2cf61 | 2005-11-25 16:42:13 +1100 | [diff] [blame] | 3357 | if (xfs_finish_reclaim(ip, noblock, |
| 3358 | XFS_IFLUSH_DELWRI_ELSE_ASYNC)) |
| 3359 | delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | purged = 1; |
| 3361 | break; |
| 3362 | } |
| 3363 | |
| 3364 | done = !purged; |
| 3365 | } |
| 3366 | |
| 3367 | XFS_MOUNT_IUNLOCK(mp); |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
| 3371 | /* |
| 3372 | * xfs_alloc_file_space() |
| 3373 | * This routine allocates disk space for the given file. |
| 3374 | * |
| 3375 | * If alloc_type == 0, this request is for an ALLOCSP type |
| 3376 | * request which will change the file size. In this case, no |
| 3377 | * DMAPI event will be generated by the call. A TRUNCATE event |
| 3378 | * will be generated later by xfs_setattr. |
| 3379 | * |
| 3380 | * If alloc_type != 0, this request is for a RESVSP type |
| 3381 | * request, and a DMAPI DM_EVENT_WRITE will be generated if the |
| 3382 | * lower block boundary byte address is less than the file's |
| 3383 | * length. |
| 3384 | * |
| 3385 | * RETURNS: |
| 3386 | * 0 on success |
| 3387 | * errno on error |
| 3388 | * |
| 3389 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 3390 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | xfs_alloc_file_space( |
| 3392 | xfs_inode_t *ip, |
| 3393 | xfs_off_t offset, |
| 3394 | xfs_off_t len, |
| 3395 | int alloc_type, |
| 3396 | int attr_flags) |
| 3397 | { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3398 | xfs_mount_t *mp = ip->i_mount; |
| 3399 | xfs_off_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | xfs_filblks_t allocated_fsb; |
| 3401 | xfs_filblks_t allocatesize_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3402 | xfs_extlen_t extsz, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | xfs_fileoff_t startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3404 | xfs_fsblock_t firstfsb; |
| 3405 | int nimaps; |
| 3406 | int bmapi_flag; |
| 3407 | int quota_flag; |
| 3408 | int rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | xfs_trans_t *tp; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3410 | xfs_bmbt_irec_t imaps[1], *imapp; |
| 3411 | xfs_bmap_free_t free_list; |
| 3412 | uint qblocks, resblks, resrtextents; |
| 3413 | int committed; |
| 3414 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3416 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
| 3418 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3419 | return XFS_ERROR(EIO); |
| 3420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
| 3422 | return error; |
| 3423 | |
| 3424 | if (len <= 0) |
| 3425 | return XFS_ERROR(EINVAL); |
| 3426 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3427 | rt = XFS_IS_REALTIME_INODE(ip); |
| 3428 | extsz = xfs_get_extsz_hint(ip); |
| 3429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | count = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | imapp = &imaps[0]; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3432 | nimaps = 1; |
| 3433 | bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 3435 | allocatesize_fsb = XFS_B_TO_FSB(mp, count); |
| 3436 | |
| 3437 | /* Generate a DMAPI event if needed. */ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3438 | if (alloc_type != 0 && offset < ip->i_size && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | (attr_flags&ATTR_DMI) == 0 && |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3440 | DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | xfs_off_t end_dmi_offset; |
| 3442 | |
| 3443 | end_dmi_offset = offset+len; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3444 | if (end_dmi_offset > ip->i_size) |
| 3445 | end_dmi_offset = ip->i_size; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3446 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset, |
| 3447 | end_dmi_offset - offset, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3449 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3453 | * Allocate file space until done or until there is an error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | */ |
| 3455 | retry: |
| 3456 | while (allocatesize_fsb && !error) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3457 | xfs_fileoff_t s, e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3459 | /* |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3460 | * Determine space reservations for data/realtime. |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3461 | */ |
| 3462 | if (unlikely(extsz)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | s = startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3464 | do_div(s, extsz); |
| 3465 | s *= extsz; |
| 3466 | e = startoffset_fsb + allocatesize_fsb; |
| 3467 | if ((temp = do_mod(startoffset_fsb, extsz))) |
| 3468 | e += temp; |
| 3469 | if ((temp = do_mod(e, extsz))) |
| 3470 | e += extsz - temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | } else { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3472 | s = 0; |
| 3473 | e = allocatesize_fsb; |
| 3474 | } |
| 3475 | |
| 3476 | if (unlikely(rt)) { |
| 3477 | resrtextents = qblocks = (uint)(e - s); |
| 3478 | resrtextents /= mp->m_sb.sb_rextsize; |
| 3479 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 3480 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
| 3481 | } else { |
| 3482 | resrtextents = 0; |
| 3483 | resblks = qblocks = \ |
| 3484 | XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s)); |
| 3485 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3489 | * Allocate and setup the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | */ |
| 3491 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3492 | error = xfs_trans_reserve(tp, resblks, |
| 3493 | XFS_WRITE_LOG_RES(mp), resrtextents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | XFS_TRANS_PERM_LOG_RES, |
| 3495 | XFS_WRITE_LOG_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3497 | * Check for running out of space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | */ |
| 3499 | if (error) { |
| 3500 | /* |
| 3501 | * Free the transaction structure. |
| 3502 | */ |
| 3503 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 3504 | xfs_trans_cancel(tp, 0); |
| 3505 | break; |
| 3506 | } |
| 3507 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3508 | error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, |
| 3509 | qblocks, 0, quota_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | if (error) |
| 3511 | goto error1; |
| 3512 | |
| 3513 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3514 | xfs_trans_ihold(tp, ip); |
| 3515 | |
| 3516 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3517 | * Issue the xfs_bmapi() call to allocate the blocks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | */ |
| 3519 | XFS_BMAP_INIT(&free_list, &firstfsb); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3520 | error = xfs_bmapi(tp, ip, startoffset_fsb, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3521 | allocatesize_fsb, bmapi_flag, |
| 3522 | &firstfsb, 0, imapp, &nimaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3523 | &free_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | if (error) { |
| 3525 | goto error0; |
| 3526 | } |
| 3527 | |
| 3528 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3529 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3531 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | if (error) { |
| 3533 | goto error0; |
| 3534 | } |
| 3535 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3536 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3538 | if (error) { |
| 3539 | break; |
| 3540 | } |
| 3541 | |
| 3542 | allocated_fsb = imapp->br_blockcount; |
| 3543 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3544 | if (nimaps == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | error = XFS_ERROR(ENOSPC); |
| 3546 | break; |
| 3547 | } |
| 3548 | |
| 3549 | startoffset_fsb += allocated_fsb; |
| 3550 | allocatesize_fsb -= allocated_fsb; |
| 3551 | } |
| 3552 | dmapi_enospc_check: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3553 | if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 && |
| 3554 | DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3556 | ip, DM_RIGHT_NULL, |
| 3557 | ip, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */ |
| 3559 | if (error == 0) |
| 3560 | goto retry; /* Maybe DMAPI app. has made space */ |
| 3561 | /* else fall through with error from XFS_SEND_DATA */ |
| 3562 | } |
| 3563 | |
| 3564 | return error; |
| 3565 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3566 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | xfs_bmap_cancel(&free_list); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3568 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag); |
| 3569 | |
| 3570 | error1: /* Just cancel transaction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 3572 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3573 | goto dmapi_enospc_check; |
| 3574 | } |
| 3575 | |
| 3576 | /* |
| 3577 | * Zero file bytes between startoff and endoff inclusive. |
| 3578 | * The iolock is held exclusive and no blocks are buffered. |
| 3579 | */ |
| 3580 | STATIC int |
| 3581 | xfs_zero_remaining_bytes( |
| 3582 | xfs_inode_t *ip, |
| 3583 | xfs_off_t startoff, |
| 3584 | xfs_off_t endoff) |
| 3585 | { |
| 3586 | xfs_bmbt_irec_t imap; |
| 3587 | xfs_fileoff_t offset_fsb; |
| 3588 | xfs_off_t lastoffset; |
| 3589 | xfs_off_t offset; |
| 3590 | xfs_buf_t *bp; |
| 3591 | xfs_mount_t *mp = ip->i_mount; |
| 3592 | int nimap; |
| 3593 | int error = 0; |
| 3594 | |
| 3595 | bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize, |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3596 | XFS_IS_REALTIME_INODE(ip) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | mp->m_rtdev_targp : mp->m_ddev_targp); |
| 3598 | |
| 3599 | for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { |
| 3600 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 3601 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3602 | error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3603 | NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | if (error || nimap < 1) |
| 3605 | break; |
| 3606 | ASSERT(imap.br_blockcount >= 1); |
| 3607 | ASSERT(imap.br_startoff == offset_fsb); |
| 3608 | lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; |
| 3609 | if (lastoffset > endoff) |
| 3610 | lastoffset = endoff; |
| 3611 | if (imap.br_startblock == HOLESTARTBLOCK) |
| 3612 | continue; |
| 3613 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3614 | if (imap.br_state == XFS_EXT_UNWRITTEN) |
| 3615 | continue; |
| 3616 | XFS_BUF_UNDONE(bp); |
| 3617 | XFS_BUF_UNWRITE(bp); |
| 3618 | XFS_BUF_READ(bp); |
| 3619 | XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock)); |
| 3620 | xfsbdstrat(mp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 3621 | error = xfs_iowait(bp); |
| 3622 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | xfs_ioerror_alert("xfs_zero_remaining_bytes(read)", |
| 3624 | mp, bp, XFS_BUF_ADDR(bp)); |
| 3625 | break; |
| 3626 | } |
| 3627 | memset(XFS_BUF_PTR(bp) + |
| 3628 | (offset - XFS_FSB_TO_B(mp, imap.br_startoff)), |
| 3629 | 0, lastoffset - offset + 1); |
| 3630 | XFS_BUF_UNDONE(bp); |
| 3631 | XFS_BUF_UNREAD(bp); |
| 3632 | XFS_BUF_WRITE(bp); |
| 3633 | xfsbdstrat(mp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 3634 | error = xfs_iowait(bp); |
| 3635 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | xfs_ioerror_alert("xfs_zero_remaining_bytes(write)", |
| 3637 | mp, bp, XFS_BUF_ADDR(bp)); |
| 3638 | break; |
| 3639 | } |
| 3640 | } |
| 3641 | xfs_buf_free(bp); |
| 3642 | return error; |
| 3643 | } |
| 3644 | |
| 3645 | /* |
| 3646 | * xfs_free_file_space() |
| 3647 | * This routine frees disk space for the given file. |
| 3648 | * |
| 3649 | * This routine is only called by xfs_change_file_space |
| 3650 | * for an UNRESVSP type call. |
| 3651 | * |
| 3652 | * RETURNS: |
| 3653 | * 0 on success |
| 3654 | * errno on error |
| 3655 | * |
| 3656 | */ |
| 3657 | STATIC int |
| 3658 | xfs_free_file_space( |
| 3659 | xfs_inode_t *ip, |
| 3660 | xfs_off_t offset, |
| 3661 | xfs_off_t len, |
| 3662 | int attr_flags) |
| 3663 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 3664 | bhv_vnode_t *vp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | int committed; |
| 3666 | int done; |
| 3667 | xfs_off_t end_dmi_offset; |
| 3668 | xfs_fileoff_t endoffset_fsb; |
| 3669 | int error; |
| 3670 | xfs_fsblock_t firstfsb; |
| 3671 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | xfs_bmbt_irec_t imap; |
| 3673 | xfs_off_t ioffset; |
| 3674 | xfs_extlen_t mod=0; |
| 3675 | xfs_mount_t *mp; |
| 3676 | int nimap; |
| 3677 | uint resblks; |
Nathan Scott | 673cdf5 | 2006-09-28 10:56:26 +1000 | [diff] [blame] | 3678 | uint rounding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | int rt; |
| 3680 | xfs_fileoff_t startoffset_fsb; |
| 3681 | xfs_trans_t *tp; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3682 | int need_iolock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3684 | vp = XFS_ITOV(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | mp = ip->i_mount; |
| 3686 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3687 | xfs_itrace_entry(ip); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
| 3690 | return error; |
| 3691 | |
| 3692 | error = 0; |
| 3693 | if (len <= 0) /* if nothing being freed */ |
| 3694 | return error; |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3695 | rt = XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | startoffset_fsb = XFS_B_TO_FSB(mp, offset); |
| 3697 | end_dmi_offset = offset + len; |
| 3698 | endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset); |
| 3699 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3700 | if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 && |
| 3701 | DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3702 | if (end_dmi_offset > ip->i_size) |
| 3703 | end_dmi_offset = ip->i_size; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3704 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | offset, end_dmi_offset - offset, |
| 3706 | AT_DELAY_FLAG(attr_flags), NULL); |
| 3707 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3708 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | } |
| 3710 | |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3711 | if (attr_flags & ATTR_NOLOCK) |
| 3712 | need_iolock = 0; |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 3713 | if (need_iolock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 3715 | vn_iowait(ip); /* wait for the completion of any pending DIOs */ |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 3716 | } |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3717 | |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3718 | 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] | 3719 | ioffset = offset & ~(rounding - 1); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3720 | |
| 3721 | if (VN_CACHED(vp) != 0) { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3722 | xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1); |
| 3723 | error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 3724 | if (error) |
| 3725 | goto out_unlock_iolock; |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3726 | } |
| 3727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | /* |
| 3729 | * Need to zero the stuff we're not freeing, on disk. |
| 3730 | * If its a realtime file & can't use unwritten extents then we |
| 3731 | * actually need to zero the extent edges. Otherwise xfs_bunmapi |
| 3732 | * will take care of it for us. |
| 3733 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3734 | if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3736 | error = xfs_bmapi(NULL, ip, startoffset_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3737 | 1, 0, NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | if (error) |
| 3739 | goto out_unlock_iolock; |
| 3740 | ASSERT(nimap == 0 || nimap == 1); |
| 3741 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 3742 | xfs_daddr_t block; |
| 3743 | |
| 3744 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3745 | block = imap.br_startblock; |
| 3746 | mod = do_div(block, mp->m_sb.sb_rextsize); |
| 3747 | if (mod) |
| 3748 | startoffset_fsb += mp->m_sb.sb_rextsize - mod; |
| 3749 | } |
| 3750 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3751 | error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3752 | 1, 0, NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | if (error) |
| 3754 | goto out_unlock_iolock; |
| 3755 | ASSERT(nimap == 0 || nimap == 1); |
| 3756 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 3757 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3758 | mod++; |
| 3759 | if (mod && (mod != mp->m_sb.sb_rextsize)) |
| 3760 | endoffset_fsb -= mod; |
| 3761 | } |
| 3762 | } |
| 3763 | if ((done = (endoffset_fsb <= startoffset_fsb))) |
| 3764 | /* |
| 3765 | * One contiguous piece to clear |
| 3766 | */ |
| 3767 | error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); |
| 3768 | else { |
| 3769 | /* |
| 3770 | * Some full blocks, possibly two pieces to clear |
| 3771 | */ |
| 3772 | if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) |
| 3773 | error = xfs_zero_remaining_bytes(ip, offset, |
| 3774 | XFS_FSB_TO_B(mp, startoffset_fsb) - 1); |
| 3775 | if (!error && |
| 3776 | XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) |
| 3777 | error = xfs_zero_remaining_bytes(ip, |
| 3778 | XFS_FSB_TO_B(mp, endoffset_fsb), |
| 3779 | offset + len - 1); |
| 3780 | } |
| 3781 | |
| 3782 | /* |
| 3783 | * free file space until done or until there is an error |
| 3784 | */ |
| 3785 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 3786 | while (!error && !done) { |
| 3787 | |
| 3788 | /* |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 3789 | * allocate and setup the transaction. Allow this |
| 3790 | * transaction to dip into the reserve blocks to ensure |
| 3791 | * the freeing of the space succeeds at ENOSPC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | */ |
| 3793 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 3794 | tp->t_flags |= XFS_TRANS_RESERVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | error = xfs_trans_reserve(tp, |
| 3796 | resblks, |
| 3797 | XFS_WRITE_LOG_RES(mp), |
| 3798 | 0, |
| 3799 | XFS_TRANS_PERM_LOG_RES, |
| 3800 | XFS_WRITE_LOG_COUNT); |
| 3801 | |
| 3802 | /* |
| 3803 | * check for running out of space |
| 3804 | */ |
| 3805 | if (error) { |
| 3806 | /* |
| 3807 | * Free the transaction structure. |
| 3808 | */ |
| 3809 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 3810 | xfs_trans_cancel(tp, 0); |
| 3811 | break; |
| 3812 | } |
| 3813 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3814 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3815 | ip->i_udquot, ip->i_gdquot, resblks, 0, |
| 3816 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | if (error) |
| 3818 | goto error1; |
| 3819 | |
| 3820 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3821 | xfs_trans_ihold(tp, ip); |
| 3822 | |
| 3823 | /* |
| 3824 | * issue the bunmapi() call to free the blocks |
| 3825 | */ |
| 3826 | XFS_BMAP_INIT(&free_list, &firstfsb); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3827 | error = xfs_bunmapi(tp, ip, startoffset_fsb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | endoffset_fsb - startoffset_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3829 | 0, 2, &firstfsb, &free_list, NULL, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | if (error) { |
| 3831 | goto error0; |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * complete the transaction |
| 3836 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3837 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | if (error) { |
| 3839 | goto error0; |
| 3840 | } |
| 3841 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3842 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3844 | } |
| 3845 | |
| 3846 | out_unlock_iolock: |
| 3847 | if (need_iolock) |
| 3848 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 3849 | return error; |
| 3850 | |
| 3851 | error0: |
| 3852 | xfs_bmap_cancel(&free_list); |
| 3853 | error1: |
| 3854 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 3855 | xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) : |
| 3856 | XFS_ILOCK_EXCL); |
| 3857 | return error; |
| 3858 | } |
| 3859 | |
| 3860 | /* |
| 3861 | * xfs_change_file_space() |
| 3862 | * This routine allocates or frees disk space for the given file. |
| 3863 | * The user specified parameters are checked for alignment and size |
| 3864 | * limitations. |
| 3865 | * |
| 3866 | * RETURNS: |
| 3867 | * 0 on success |
| 3868 | * errno on error |
| 3869 | * |
| 3870 | */ |
| 3871 | int |
| 3872 | xfs_change_file_space( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3873 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | int cmd, |
| 3875 | xfs_flock64_t *bf, |
| 3876 | xfs_off_t offset, |
| 3877 | cred_t *credp, |
| 3878 | int attr_flags) |
| 3879 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3880 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | int clrprealloc; |
| 3882 | int error; |
| 3883 | xfs_fsize_t fsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | int setprealloc; |
| 3885 | xfs_off_t startoffset; |
| 3886 | xfs_off_t llen; |
| 3887 | xfs_trans_t *tp; |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 3888 | bhv_vattr_t va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3890 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3892 | if (!S_ISREG(ip->i_d.di_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | return XFS_ERROR(EINVAL); |
| 3894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | switch (bf->l_whence) { |
| 3896 | case 0: /*SEEK_SET*/ |
| 3897 | break; |
| 3898 | case 1: /*SEEK_CUR*/ |
| 3899 | bf->l_start += offset; |
| 3900 | break; |
| 3901 | case 2: /*SEEK_END*/ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3902 | bf->l_start += ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | break; |
| 3904 | default: |
| 3905 | return XFS_ERROR(EINVAL); |
| 3906 | } |
| 3907 | |
| 3908 | llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len; |
| 3909 | |
| 3910 | if ( (bf->l_start < 0) |
| 3911 | || (bf->l_start > XFS_MAXIOFFSET(mp)) |
| 3912 | || (bf->l_start + llen < 0) |
| 3913 | || (bf->l_start + llen > XFS_MAXIOFFSET(mp))) |
| 3914 | return XFS_ERROR(EINVAL); |
| 3915 | |
| 3916 | bf->l_whence = 0; |
| 3917 | |
| 3918 | startoffset = bf->l_start; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3919 | fsize = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | |
| 3921 | /* |
| 3922 | * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve |
| 3923 | * file space. |
| 3924 | * These calls do NOT zero the data space allocated to the file, |
| 3925 | * nor do they change the file size. |
| 3926 | * |
| 3927 | * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file |
| 3928 | * space. |
| 3929 | * These calls cause the new file data to be zeroed and the file |
| 3930 | * size to be changed. |
| 3931 | */ |
| 3932 | setprealloc = clrprealloc = 0; |
| 3933 | |
| 3934 | switch (cmd) { |
| 3935 | case XFS_IOC_RESVSP: |
| 3936 | case XFS_IOC_RESVSP64: |
| 3937 | error = xfs_alloc_file_space(ip, startoffset, bf->l_len, |
| 3938 | 1, attr_flags); |
| 3939 | if (error) |
| 3940 | return error; |
| 3941 | setprealloc = 1; |
| 3942 | break; |
| 3943 | |
| 3944 | case XFS_IOC_UNRESVSP: |
| 3945 | case XFS_IOC_UNRESVSP64: |
| 3946 | if ((error = xfs_free_file_space(ip, startoffset, bf->l_len, |
| 3947 | attr_flags))) |
| 3948 | return error; |
| 3949 | break; |
| 3950 | |
| 3951 | case XFS_IOC_ALLOCSP: |
| 3952 | case XFS_IOC_ALLOCSP64: |
| 3953 | case XFS_IOC_FREESP: |
| 3954 | case XFS_IOC_FREESP64: |
| 3955 | if (startoffset > fsize) { |
| 3956 | error = xfs_alloc_file_space(ip, fsize, |
| 3957 | startoffset - fsize, 0, attr_flags); |
| 3958 | if (error) |
| 3959 | break; |
| 3960 | } |
| 3961 | |
| 3962 | va.va_mask = XFS_AT_SIZE; |
| 3963 | va.va_size = startoffset; |
| 3964 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3965 | error = xfs_setattr(ip, &va, attr_flags, credp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | |
| 3967 | if (error) |
| 3968 | return error; |
| 3969 | |
| 3970 | clrprealloc = 1; |
| 3971 | break; |
| 3972 | |
| 3973 | default: |
| 3974 | ASSERT(0); |
| 3975 | return XFS_ERROR(EINVAL); |
| 3976 | } |
| 3977 | |
| 3978 | /* |
| 3979 | * update the inode timestamp, mode, and prealloc flag bits |
| 3980 | */ |
| 3981 | tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID); |
| 3982 | |
| 3983 | if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp), |
| 3984 | 0, 0, 0))) { |
| 3985 | /* ASSERT(0); */ |
| 3986 | xfs_trans_cancel(tp, 0); |
| 3987 | return error; |
| 3988 | } |
| 3989 | |
| 3990 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3991 | |
| 3992 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3993 | xfs_trans_ihold(tp, ip); |
| 3994 | |
| 3995 | if ((attr_flags & ATTR_DMI) == 0) { |
| 3996 | ip->i_d.di_mode &= ~S_ISUID; |
| 3997 | |
| 3998 | /* |
| 3999 | * Note that we don't have to worry about mandatory |
| 4000 | * file locking being disabled here because we only |
| 4001 | * clear the S_ISGID bit if the Group execute bit is |
| 4002 | * on, but if it was on then mandatory locking wouldn't |
| 4003 | * have been enabled. |
| 4004 | */ |
| 4005 | if (ip->i_d.di_mode & S_IXGRP) |
| 4006 | ip->i_d.di_mode &= ~S_ISGID; |
| 4007 | |
| 4008 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 4009 | } |
| 4010 | if (setprealloc) |
| 4011 | ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; |
| 4012 | else if (clrprealloc) |
| 4013 | ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; |
| 4014 | |
| 4015 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 4016 | xfs_trans_set_sync(tp); |
| 4017 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4018 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | |
| 4020 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4021 | |
| 4022 | return error; |
| 4023 | } |