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