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