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