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