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