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