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