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