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. |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 3 | * Copyright (c) 2012 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
| 9 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_log.h" |
| 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_sb.h" |
| 27 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
| 32 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_dinode.h" |
| 34 | #include "xfs_inode.h" |
| 35 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_ialloc.h" |
| 38 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_bmap.h" |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 40 | #include "xfs_acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_attr.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_quota.h" |
| 44 | #include "xfs_utils.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 45 | #include "xfs_rtalloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_trans_space.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "xfs_log_priv.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 49 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 50 | #include "xfs_trace.h" |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 51 | #include "xfs_icache.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 52 | #include "xfs_symlink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 56 | * This is called by xfs_inactive to free any blocks beyond eof |
| 57 | * when the link count isn't zero and by xfs_dm_punch_hole() when |
| 58 | * punching a hole to EOF. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
Brian Foster | 40165e2 | 2012-11-06 09:50:41 -0500 | [diff] [blame] | 60 | int |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 61 | xfs_free_eofblocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | xfs_mount_t *mp, |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 63 | xfs_inode_t *ip, |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 64 | bool need_iolock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | xfs_trans_t *tp; |
| 67 | int error; |
| 68 | xfs_fileoff_t end_fsb; |
| 69 | xfs_fileoff_t last_fsb; |
| 70 | xfs_filblks_t map_len; |
| 71 | int nimaps; |
| 72 | xfs_bmbt_irec_t imap; |
| 73 | |
| 74 | /* |
| 75 | * Figure out if there are any blocks beyond the end |
| 76 | * of the file. If not, then there is nothing to do. |
| 77 | */ |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 78 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip)); |
Dave Chinner | 3297238 | 2012-06-08 15:44:54 +1000 | [diff] [blame] | 79 | last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
Kulikov Vasiliy | 3f34885 | 2010-07-20 17:54:28 +1000 | [diff] [blame] | 80 | if (last_fsb <= end_fsb) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 81 | return 0; |
Kulikov Vasiliy | 3f34885 | 2010-07-20 17:54:28 +1000 | [diff] [blame] | 82 | map_len = last_fsb - end_fsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | nimaps = 1; |
| 85 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 86 | error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 88 | |
| 89 | if (!error && (nimaps != 0) && |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 90 | (imap.br_startblock != HOLESTARTBLOCK || |
| 91 | ip->i_delayed_blks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Attach the dquots to the inode up front. |
| 94 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 95 | error = xfs_qm_dqattach(ip, 0); |
| 96 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 97 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * There are blocks after the end of file. |
| 101 | * Free them up now by truncating the file to |
| 102 | * its current size. |
| 103 | */ |
| 104 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 105 | |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 106 | if (need_iolock) { |
Christoph Hellwig | c56c9631 | 2009-10-19 04:03:46 +0000 | [diff] [blame] | 107 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
| 108 | xfs_trans_cancel(tp, 0); |
Brian Foster | 40165e2 | 2012-11-06 09:50:41 -0500 | [diff] [blame] | 109 | return EAGAIN; |
Christoph Hellwig | c56c9631 | 2009-10-19 04:03:46 +0000 | [diff] [blame] | 110 | } |
Christoph Hellwig | c56c9631 | 2009-10-19 04:03:46 +0000 | [diff] [blame] | 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | error = xfs_trans_reserve(tp, 0, |
| 114 | XFS_ITRUNCATE_LOG_RES(mp), |
| 115 | 0, XFS_TRANS_PERM_LOG_RES, |
| 116 | XFS_ITRUNCATE_LOG_COUNT); |
| 117 | if (error) { |
| 118 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 119 | xfs_trans_cancel(tp, 0); |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 120 | if (need_iolock) |
| 121 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 122 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 126 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 128 | /* |
| 129 | * Do not update the on-disk file size. If we update the |
| 130 | * on-disk file size and then the system crashes before the |
| 131 | * contents of the file are flushed to disk then the files |
| 132 | * may be full of holes (ie NULL files bug). |
| 133 | */ |
| 134 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 135 | XFS_ISIZE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (error) { |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 137 | /* |
| 138 | * If we get an error at this point we simply don't |
| 139 | * bother truncating the file. |
| 140 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | xfs_trans_cancel(tp, |
| 142 | (XFS_TRANS_RELEASE_LOG_RES | |
| 143 | XFS_TRANS_ABORT)); |
| 144 | } else { |
| 145 | error = xfs_trans_commit(tp, |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 146 | XFS_TRANS_RELEASE_LOG_RES); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 147 | if (!error) |
| 148 | xfs_inode_clear_eofblocks_tag(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 150 | |
| 151 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 152 | if (need_iolock) |
| 153 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 155 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 158 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | xfs_release( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 160 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 162 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | int error; |
| 164 | |
Christoph Hellwig | 42173f6 | 2008-04-22 17:33:25 +1000 | [diff] [blame] | 165 | 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] | 166 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | /* 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] | 169 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 172 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 173 | int truncated; |
| 174 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 175 | /* |
| 176 | * If we are using filestreams, and we have an unlinked |
| 177 | * file that we are processing the last close on, then nothing |
| 178 | * will be able to reopen and write to this file. Purge this |
| 179 | * inode from the filestreams cache so that it doesn't delay |
| 180 | * teardown of the inode. |
| 181 | */ |
| 182 | if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) |
| 183 | xfs_filestream_deassociate(ip); |
| 184 | |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 185 | /* |
| 186 | * If we previously truncated this file and removed old data |
| 187 | * in the process, we want to initiate "early" writeout on |
| 188 | * the last close. This is an attempt to combat the notorious |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 189 | * NULL files problem which is particularly noticeable from a |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 190 | * truncate down, buffered (re-)write (delalloc), followed by |
| 191 | * a crash. What we are effectively doing here is |
| 192 | * significantly reducing the time window where we'd otherwise |
| 193 | * be exposed to that problem. |
| 194 | */ |
Christoph Hellwig | 09262b43 | 2007-08-29 11:44:50 +1000 | [diff] [blame] | 195 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); |
Dave Chinner | df4368a | 2011-06-23 01:35:00 +0000 | [diff] [blame] | 196 | if (truncated) { |
| 197 | xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); |
Dave Chinner | 4bc1ea6 | 2012-11-12 22:53:56 +1100 | [diff] [blame] | 198 | if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) { |
| 199 | error = -filemap_flush(VFS_I(ip)->i_mapping); |
| 200 | if (error) |
| 201 | return error; |
| 202 | } |
Dave Chinner | df4368a | 2011-06-23 01:35:00 +0000 | [diff] [blame] | 203 | } |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 204 | } |
| 205 | |
Dave Chinner | 6e85756 | 2010-12-23 12:02:31 +1100 | [diff] [blame] | 206 | if (ip->i_d.di_nlink == 0) |
| 207 | return 0; |
Christoph Hellwig | c56c9631 | 2009-10-19 04:03:46 +0000 | [diff] [blame] | 208 | |
Brian Foster | 72b53ef | 2012-11-06 09:50:40 -0500 | [diff] [blame] | 209 | if (xfs_can_free_eofblocks(ip, false)) { |
Dave Chinner | 6e85756 | 2010-12-23 12:02:31 +1100 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * If we can't get the iolock just skip truncating the blocks |
| 213 | * past EOF because we could deadlock with the mmap_sem |
| 214 | * otherwise. We'll get another chance to drop them once the |
| 215 | * last reference to the inode is dropped, so we'll never leak |
| 216 | * blocks permanently. |
| 217 | * |
| 218 | * Further, check if the inode is being opened, written and |
| 219 | * closed frequently and we have delayed allocation blocks |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 220 | * outstanding (e.g. streaming writes from the NFS server), |
Dave Chinner | 6e85756 | 2010-12-23 12:02:31 +1100 | [diff] [blame] | 221 | * truncating the blocks past EOF will cause fragmentation to |
| 222 | * occur. |
| 223 | * |
| 224 | * In this case don't do the truncation, either, but we have to |
| 225 | * be careful how we detect this case. Blocks beyond EOF show |
| 226 | * up as i_delayed_blks even when the inode is clean, so we |
| 227 | * need to truncate them away first before checking for a dirty |
| 228 | * release. Hence on the first dirty close we will still remove |
| 229 | * the speculative allocation, but after that we will leave it |
| 230 | * in place. |
| 231 | */ |
| 232 | if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) |
| 233 | return 0; |
| 234 | |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 235 | error = xfs_free_eofblocks(mp, ip, true); |
Brian Foster | 40165e2 | 2012-11-06 09:50:41 -0500 | [diff] [blame] | 236 | if (error && error != EAGAIN) |
Dave Chinner | 6e85756 | 2010-12-23 12:02:31 +1100 | [diff] [blame] | 237 | return error; |
| 238 | |
| 239 | /* delalloc blocks after truncation means it really is dirty */ |
| 240 | if (ip->i_delayed_blks) |
| 241 | xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * xfs_inactive |
| 248 | * |
| 249 | * This is called when the vnode reference count for the vnode |
| 250 | * goes to zero. If the file has been unlinked, then it must |
| 251 | * now be truncated. Also, we clear all of the read-ahead state |
| 252 | * kept for the inode here since the file is now closed. |
| 253 | */ |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 254 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | xfs_inactive( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 256 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 258 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | xfs_fsblock_t first_block; |
| 260 | int committed; |
| 261 | xfs_trans_t *tp; |
| 262 | xfs_mount_t *mp; |
| 263 | int error; |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 264 | int truncate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* |
| 267 | * If the inode is already free, then there can be nothing |
| 268 | * to clean up here. |
| 269 | */ |
Christoph Hellwig | cb4c8cc | 2009-03-16 08:25:25 +0100 | [diff] [blame] | 270 | 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] | 271 | ASSERT(ip->i_df.if_real_bytes == 0); |
| 272 | ASSERT(ip->i_df.if_broot_bytes == 0); |
| 273 | return VN_INACTIVE_CACHE; |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | mp = ip->i_mount; |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | error = 0; |
| 279 | |
| 280 | /* 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] | 281 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | goto out; |
| 283 | |
| 284 | if (ip->i_d.di_nlink != 0) { |
Brian Foster | 72b53ef | 2012-11-06 09:50:40 -0500 | [diff] [blame] | 285 | /* |
| 286 | * force is true because we are evicting an inode from the |
| 287 | * cache. Post-eof blocks must be freed, lest we end up with |
| 288 | * broken free space accounting. |
| 289 | */ |
| 290 | if (xfs_can_free_eofblocks(ip, true)) { |
Christoph Hellwig | 5a15322 | 2012-07-04 11:13:32 -0400 | [diff] [blame] | 291 | error = xfs_free_eofblocks(mp, ip, false); |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 292 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 293 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | goto out; |
| 296 | } |
| 297 | |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 298 | if (S_ISREG(ip->i_d.di_mode) && |
| 299 | (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 || |
| 300 | ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0)) |
| 301 | truncate = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 303 | error = xfs_qm_dqattach(ip, 0); |
| 304 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 305 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 308 | error = xfs_trans_reserve(tp, 0, |
| 309 | (truncate || S_ISLNK(ip->i_d.di_mode)) ? |
| 310 | XFS_ITRUNCATE_LOG_RES(mp) : |
| 311 | XFS_IFREE_LOG_RES(mp), |
| 312 | 0, |
| 313 | XFS_TRANS_PERM_LOG_RES, |
| 314 | XFS_ITRUNCATE_LOG_COUNT); |
| 315 | if (error) { |
| 316 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 317 | xfs_trans_cancel(tp, 0); |
| 318 | return VN_INACTIVE_CACHE; |
| 319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 321 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 322 | xfs_trans_ijoin(tp, ip, 0); |
| 323 | |
| 324 | if (S_ISLNK(ip->i_d.di_mode)) { |
Mark Tinguely | 725eb1e | 2013-06-17 15:35:57 -0500 | [diff] [blame] | 325 | error = xfs_inactive_symlink(ip, &tp); |
| 326 | if (error) |
| 327 | goto out_cancel; |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 328 | } else if (truncate) { |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 329 | ip->i_d.di_size = 0; |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 330 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 331 | |
| 332 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 333 | if (error) |
| 334 | goto out_cancel; |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 335 | |
| 336 | ASSERT(ip->i_d.di_nextents == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 340 | * If there are attributes associated with the file then blow them away |
| 341 | * now. The code calls a routine that recursively deconstructs the |
| 342 | * attribute fork. We need to just commit the current transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * because we can't use it for xfs_attr_inactive(). |
| 344 | */ |
| 345 | if (ip->i_d.di_anextents > 0) { |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 346 | ASSERT(ip->i_d.di_forkoff != 0); |
| 347 | |
| 348 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 349 | if (error) |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 350 | goto out_unlock; |
| 351 | |
| 352 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 353 | |
| 354 | error = xfs_attr_inactive(ip); |
| 355 | if (error) |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 356 | goto out; |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 357 | |
| 358 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 359 | error = xfs_trans_reserve(tp, 0, |
| 360 | XFS_IFREE_LOG_RES(mp), |
| 361 | 0, XFS_TRANS_PERM_LOG_RES, |
| 362 | XFS_INACTIVE_LOG_COUNT); |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 363 | if (error) { |
| 364 | xfs_trans_cancel(tp, 0); |
| 365 | goto out; |
| 366 | } |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 367 | |
| 368 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 369 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Christoph Hellwig | fe67be0 | 2012-07-04 11:13:30 -0400 | [diff] [blame] | 372 | if (ip->i_afp) |
| 373 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 374 | |
| 375 | ASSERT(ip->i_d.di_anextents == 0); |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Free the inode. |
| 379 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 380 | xfs_bmap_init(&free_list, &first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | error = xfs_ifree(tp, ip, &free_list); |
| 382 | if (error) { |
| 383 | /* |
| 384 | * If we fail to free the inode, shut down. The cancel |
| 385 | * might do that, we need to make sure. Otherwise the |
| 386 | * inode might be lost for a long time or forever. |
| 387 | */ |
| 388 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 389 | xfs_notice(mp, "%s: xfs_ifree returned error %d", |
| 390 | __func__, error); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 391 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 394 | } else { |
| 395 | /* |
| 396 | * Credit the quota account(s). The inode is gone. |
| 397 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 398 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /* |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 401 | * Just ignore errors at this point. There is nothing we can |
| 402 | * do except to try to keep going. Make sure it's not a silent |
| 403 | * error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | */ |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 405 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 406 | if (error) |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 407 | xfs_notice(mp, "%s: xfs_bmap_finish returned error %d", |
| 408 | __func__, error); |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 409 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 410 | if (error) |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 411 | xfs_notice(mp, "%s: xfs_trans_commit returned error %d", |
| 412 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Release the dquots held by inode, if any. |
| 417 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 418 | xfs_qm_dqdetach(ip); |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 419 | out_unlock: |
| 420 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | b373e98 | 2012-07-04 11:13:29 -0400 | [diff] [blame] | 421 | out: |
| 422 | return VN_INACTIVE_CACHE; |
| 423 | out_cancel: |
| 424 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 425 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 428 | /* |
| 429 | * Lookups up an inode from "name". If ci_name is not NULL, then a CI match |
| 430 | * is allowed, otherwise it has to be an exact match. If a CI match is found, |
| 431 | * ci_name->name will point to a the actual name (caller must free) or |
| 432 | * will be set to NULL if an exact match is found. |
| 433 | */ |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 434 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | xfs_lookup( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 436 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 437 | struct xfs_name *name, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 438 | xfs_inode_t **ipp, |
| 439 | struct xfs_name *ci_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 441 | xfs_ino_t inum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | int error; |
| 443 | uint lock_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 445 | trace_xfs_lookup(dp, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 448 | return XFS_ERROR(EIO); |
| 449 | |
| 450 | lock_mode = xfs_ilock_map_shared(dp); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 451 | error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | xfs_iunlock_map_shared(dp, lock_mode); |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 453 | |
| 454 | if (error) |
| 455 | goto out; |
| 456 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 457 | error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 458 | if (error) |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 459 | goto out_free_name; |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 460 | |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 461 | return 0; |
| 462 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 463 | out_free_name: |
| 464 | if (ci_name) |
| 465 | kmem_free(ci_name->name); |
| 466 | out: |
Christoph Hellwig | eca450b | 2008-04-22 17:33:52 +1000 | [diff] [blame] | 467 | *ipp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return error; |
| 469 | } |
| 470 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 471 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | xfs_create( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 473 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 474 | struct xfs_name *name, |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 475 | umode_t mode, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 476 | xfs_dev_t rdev, |
Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 477 | xfs_inode_t **ipp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 479 | int is_dir = S_ISDIR(mode); |
| 480 | struct xfs_mount *mp = dp->i_mount; |
| 481 | struct xfs_inode *ip = NULL; |
| 482 | struct xfs_trans *tp = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 483 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | xfs_bmap_free_t free_list; |
| 485 | xfs_fsblock_t first_block; |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 486 | bool unlock_dp_on_error = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | uint cancel_flags; |
| 488 | int committed; |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 489 | prid_t prid; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 490 | struct xfs_dquot *udqp = NULL; |
| 491 | struct xfs_dquot *gdqp = NULL; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 492 | struct xfs_dquot *pdqp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | uint resblks; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 494 | uint log_res; |
| 495 | uint log_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 497 | trace_xfs_create(dp, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 499 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 500 | return XFS_ERROR(EIO); |
| 501 | |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 502 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 503 | prid = xfs_get_projid(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | else |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 505 | prid = XFS_PROJID_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * Make sure that we have allocated dquot(s) on disk. |
| 509 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 510 | error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 511 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, |
| 512 | &udqp, &gdqp, &pdqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (error) |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 514 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 516 | if (is_dir) { |
| 517 | rdev = 0; |
| 518 | resblks = XFS_MKDIR_SPACE_RES(mp, name->len); |
| 519 | log_res = XFS_MKDIR_LOG_RES(mp); |
| 520 | log_count = XFS_MKDIR_LOG_COUNT; |
| 521 | tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); |
| 522 | } else { |
| 523 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); |
| 524 | log_res = XFS_CREATE_LOG_RES(mp); |
| 525 | log_count = XFS_CREATE_LOG_COUNT; |
| 526 | tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); |
| 527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* |
| 532 | * Initially assume that the file does not exist and |
| 533 | * reserve the resources for that case. If that is not |
| 534 | * the case we'll drop the one we have and get a more |
| 535 | * appropriate transaction later. |
| 536 | */ |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 537 | error = xfs_trans_reserve(tp, resblks, log_res, 0, |
| 538 | XFS_TRANS_PERM_LOG_RES, log_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (error == ENOSPC) { |
Dave Chinner | 153fec4 | 2009-04-06 18:48:30 +0200 | [diff] [blame] | 540 | /* flush outstanding delalloc blocks and retry */ |
Dave Chinner | 9aa0500 | 2012-10-08 21:56:04 +1100 | [diff] [blame] | 541 | xfs_flush_inodes(mp); |
Christoph Hellwig | 4734d40 | 2009-09-09 18:19:02 -0500 | [diff] [blame] | 542 | error = xfs_trans_reserve(tp, resblks, log_res, 0, |
| 543 | XFS_TRANS_PERM_LOG_RES, log_count); |
Dave Chinner | 153fec4 | 2009-04-06 18:48:30 +0200 | [diff] [blame] | 544 | } |
| 545 | if (error == ENOSPC) { |
| 546 | /* No space at all so try a "no-allocation" reservation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | resblks = 0; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 548 | error = xfs_trans_reserve(tp, 0, log_res, 0, |
| 549 | XFS_TRANS_PERM_LOG_RES, log_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | if (error) { |
| 552 | cancel_flags = 0; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 553 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 556 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 557 | unlock_dp_on_error = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 559 | xfs_bmap_init(&free_list, &first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Reserve disk quota and the inode. |
| 563 | */ |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 564 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, |
| 565 | pdqp, resblks, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (error) |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 567 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 569 | error = xfs_dir_canenter(tp, dp, name, resblks); |
| 570 | if (error) |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 571 | goto out_trans_cancel; |
| 572 | |
| 573 | /* |
| 574 | * A newly created regular or special file just has one directory |
| 575 | * entry pointing to them, but a directory also the "." entry |
| 576 | * pointing to itself. |
| 577 | */ |
Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 578 | error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 579 | prid, resblks > 0, &ip, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (error) { |
| 581 | if (error == ENOSPC) |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 582 | goto out_trans_cancel; |
| 583 | goto out_trans_abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | /* |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 587 | * Now we join the directory inode to the transaction. We do not do it |
| 588 | * earlier because xfs_dir_ialloc might commit the previous transaction |
| 589 | * (and release all the locks). An error from here on will result in |
| 590 | * the transaction cancel unlocking dp so don't do it explicitly in the |
| 591 | * error path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 593 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 594 | unlock_dp_on_error = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 596 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 597 | &first_block, &free_list, resblks ? |
| 598 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (error) { |
| 600 | ASSERT(error != ENOSPC); |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 601 | goto out_trans_abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 603 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 605 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 606 | if (is_dir) { |
| 607 | error = xfs_dir_init(tp, ip, dp); |
| 608 | if (error) |
| 609 | goto out_bmap_cancel; |
| 610 | |
| 611 | error = xfs_bumplink(tp, dp); |
| 612 | if (error) |
| 613 | goto out_bmap_cancel; |
| 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* |
| 617 | * If this is a synchronous mount, make sure that the |
| 618 | * create transaction goes to disk before returning to |
| 619 | * the user. |
| 620 | */ |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 621 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | xfs_trans_set_sync(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | /* |
| 625 | * Attach the dquot(s) to the inodes and modify them incore. |
| 626 | * These ids of the inode couldn't have changed since the new |
| 627 | * inode has been locked ever since it was created. |
| 628 | */ |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 629 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 631 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 632 | if (error) |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 633 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 635 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 636 | if (error) |
| 637 | goto out_release_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 639 | xfs_qm_dqrele(udqp); |
| 640 | xfs_qm_dqrele(gdqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 641 | xfs_qm_dqrele(pdqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 643 | *ipp = ip; |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 644 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 646 | out_bmap_cancel: |
| 647 | xfs_bmap_cancel(&free_list); |
| 648 | out_trans_abort: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | cancel_flags |= XFS_TRANS_ABORT; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 650 | out_trans_cancel: |
| 651 | xfs_trans_cancel(tp, cancel_flags); |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 652 | out_release_inode: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /* |
| 654 | * Wait until after the current transaction is aborted to |
| 655 | * release the inode. This prevents recursive transactions |
| 656 | * and deadlocks from xfs_inactive. |
| 657 | */ |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 658 | if (ip) |
| 659 | IRELE(ip); |
| 660 | |
| 661 | xfs_qm_dqrele(udqp); |
| 662 | xfs_qm_dqrele(gdqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 663 | xfs_qm_dqrele(pdqp); |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 664 | |
| 665 | if (unlock_dp_on_error) |
| 666 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 667 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | int xfs_locked_n; |
| 672 | int xfs_small_retries; |
| 673 | int xfs_middle_retries; |
| 674 | int xfs_lots_retries; |
| 675 | int xfs_lock_delays; |
| 676 | #endif |
| 677 | |
| 678 | /* |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 679 | * Bump the subclass so xfs_lock_inodes() acquires each lock with |
| 680 | * a different value |
| 681 | */ |
| 682 | static inline int |
| 683 | xfs_lock_inumorder(int lock_mode, int subclass) |
| 684 | { |
| 685 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 686 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 687 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 688 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 689 | |
| 690 | return lock_mode; |
| 691 | } |
| 692 | |
| 693 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | * The following routine will lock n inodes in exclusive mode. |
| 695 | * We assume the caller calls us with the inodes in i_ino order. |
| 696 | * |
| 697 | * We need to detect deadlock where an inode that we lock |
| 698 | * is in the AIL and we start waiting for another inode that is locked |
| 699 | * by a thread in a long running transaction (such as truncate). This can |
| 700 | * result in deadlock since the long running trans might need to wait |
| 701 | * for the inode we just locked in order to push the tail and free space |
| 702 | * in the log. |
| 703 | */ |
| 704 | void |
| 705 | xfs_lock_inodes( |
| 706 | xfs_inode_t **ips, |
| 707 | int inodes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | uint lock_mode) |
| 709 | { |
| 710 | int attempts = 0, i, j, try_lock; |
| 711 | xfs_log_item_t *lp; |
| 712 | |
| 713 | ASSERT(ips && (inodes >= 2)); /* we need at least two */ |
| 714 | |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 715 | try_lock = 0; |
| 716 | i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | again: |
| 719 | for (; i < inodes; i++) { |
| 720 | ASSERT(ips[i]); |
| 721 | |
| 722 | if (i && (ips[i] == ips[i-1])) /* Already locked */ |
| 723 | continue; |
| 724 | |
| 725 | /* |
| 726 | * If try_lock is not set yet, make sure all locked inodes |
| 727 | * are not in the AIL. |
| 728 | * If any are, set try_lock to be used later. |
| 729 | */ |
| 730 | |
| 731 | if (!try_lock) { |
| 732 | for (j = (i - 1); j >= 0 && !try_lock; j--) { |
| 733 | lp = (xfs_log_item_t *)ips[j]->i_itemp; |
| 734 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 735 | try_lock++; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * If any of the previous locks we have locked is in the AIL, |
| 742 | * we must TRY to get the second and subsequent locks. If |
| 743 | * we can't get any, we must release all we have |
| 744 | * and try again. |
| 745 | */ |
| 746 | |
| 747 | if (try_lock) { |
| 748 | /* try_lock must be 0 if i is 0. */ |
| 749 | /* |
| 750 | * try_lock means we have an inode locked |
| 751 | * that is in the AIL. |
| 752 | */ |
| 753 | ASSERT(i != 0); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 754 | if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | attempts++; |
| 756 | |
| 757 | /* |
| 758 | * Unlock all previous guys and try again. |
| 759 | * xfs_iunlock will try to push the tail |
| 760 | * if the inode is in the AIL. |
| 761 | */ |
| 762 | |
| 763 | for(j = i - 1; j >= 0; j--) { |
| 764 | |
| 765 | /* |
| 766 | * Check to see if we've already |
| 767 | * unlocked this one. |
| 768 | * Not the first one going back, |
| 769 | * and the inode ptr is the same. |
| 770 | */ |
| 771 | if ((j != (i - 1)) && ips[j] == |
| 772 | ips[j+1]) |
| 773 | continue; |
| 774 | |
| 775 | xfs_iunlock(ips[j], lock_mode); |
| 776 | } |
| 777 | |
| 778 | if ((attempts % 5) == 0) { |
| 779 | delay(1); /* Don't just spin the CPU */ |
| 780 | #ifdef DEBUG |
| 781 | xfs_lock_delays++; |
| 782 | #endif |
| 783 | } |
| 784 | i = 0; |
| 785 | try_lock = 0; |
| 786 | goto again; |
| 787 | } |
| 788 | } else { |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 789 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | #ifdef DEBUG |
| 794 | if (attempts) { |
| 795 | if (attempts < 5) xfs_small_retries++; |
| 796 | else if (attempts < 100) xfs_middle_retries++; |
| 797 | else xfs_lots_retries++; |
| 798 | } else { |
| 799 | xfs_locked_n++; |
| 800 | } |
| 801 | #endif |
| 802 | } |
| 803 | |
David Chinner | f9114eb | 2008-09-17 16:51:21 +1000 | [diff] [blame] | 804 | /* |
| 805 | * xfs_lock_two_inodes() can only be used to lock one type of lock |
| 806 | * at a time - the iolock or the ilock, but not both at once. If |
| 807 | * we lock both at once, lockdep will report false positives saying |
| 808 | * we have violated locking orders. |
| 809 | */ |
Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 810 | void |
| 811 | xfs_lock_two_inodes( |
| 812 | xfs_inode_t *ip0, |
| 813 | xfs_inode_t *ip1, |
| 814 | uint lock_mode) |
| 815 | { |
| 816 | xfs_inode_t *temp; |
| 817 | int attempts = 0; |
| 818 | xfs_log_item_t *lp; |
| 819 | |
David Chinner | f9114eb | 2008-09-17 16:51:21 +1000 | [diff] [blame] | 820 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
| 821 | ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); |
Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 822 | ASSERT(ip0->i_ino != ip1->i_ino); |
| 823 | |
| 824 | if (ip0->i_ino > ip1->i_ino) { |
| 825 | temp = ip0; |
| 826 | ip0 = ip1; |
| 827 | ip1 = temp; |
| 828 | } |
| 829 | |
| 830 | again: |
| 831 | xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); |
| 832 | |
| 833 | /* |
| 834 | * If the first lock we have locked is in the AIL, we must TRY to get |
| 835 | * the second lock. If we can't get it, we must release the first one |
| 836 | * and try again. |
| 837 | */ |
| 838 | lp = (xfs_log_item_t *)ip0->i_itemp; |
| 839 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 840 | if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { |
| 841 | xfs_iunlock(ip0, lock_mode); |
| 842 | if ((++attempts % 5) == 0) |
| 843 | delay(1); /* Don't just spin the CPU */ |
| 844 | goto again; |
| 845 | } |
| 846 | } else { |
| 847 | xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); |
| 848 | } |
| 849 | } |
| 850 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 851 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | xfs_remove( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 853 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 854 | struct xfs_name *name, |
| 855 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 857 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | xfs_trans_t *tp = NULL; |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 859 | int is_dir = S_ISDIR(ip->i_d.di_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | int error = 0; |
| 861 | xfs_bmap_free_t free_list; |
| 862 | xfs_fsblock_t first_block; |
| 863 | int cancel_flags; |
| 864 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | int link_zero; |
| 866 | uint resblks; |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 867 | uint log_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 869 | trace_xfs_remove(dp, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 872 | return XFS_ERROR(EIO); |
| 873 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 874 | error = xfs_qm_dqattach(dp, 0); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 875 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | goto std_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 878 | error = xfs_qm_dqattach(ip, 0); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 879 | if (error) |
| 880 | goto std_return; |
| 881 | |
| 882 | if (is_dir) { |
| 883 | tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); |
| 884 | log_count = XFS_DEFAULT_LOG_COUNT; |
| 885 | } else { |
| 886 | tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); |
| 887 | log_count = XFS_REMOVE_LOG_COUNT; |
| 888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | /* |
| 892 | * We try to get the real space reservation first, |
| 893 | * allowing for directory btree deletion(s) implying |
| 894 | * possible bmap insert(s). If we can't get the space |
| 895 | * reservation then we use 0 instead, and avoid the bmap |
| 896 | * btree insert(s) in the directory code by, if the bmap |
| 897 | * insert tries to happen, instead trimming the LAST |
| 898 | * block from the directory. |
| 899 | */ |
| 900 | resblks = XFS_REMOVE_SPACE_RES(mp); |
| 901 | error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 902 | XFS_TRANS_PERM_LOG_RES, log_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (error == ENOSPC) { |
| 904 | resblks = 0; |
| 905 | error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 906 | XFS_TRANS_PERM_LOG_RES, log_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | if (error) { |
| 909 | ASSERT(error != ENOSPC); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 910 | cancel_flags = 0; |
| 911 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 914 | xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 916 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 917 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | /* |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 920 | * If we're removing a directory perform some additional validation. |
| 921 | */ |
| 922 | if (is_dir) { |
| 923 | ASSERT(ip->i_d.di_nlink >= 2); |
| 924 | if (ip->i_d.di_nlink != 2) { |
| 925 | error = XFS_ERROR(ENOTEMPTY); |
| 926 | goto out_trans_cancel; |
| 927 | } |
| 928 | if (!xfs_dir_isempty(ip)) { |
| 929 | error = XFS_ERROR(ENOTEMPTY); |
| 930 | goto out_trans_cancel; |
| 931 | } |
| 932 | } |
| 933 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 934 | xfs_bmap_init(&free_list, &first_block); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 935 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, |
Christoph Hellwig | d4377d8 | 2008-04-22 17:33:46 +1000 | [diff] [blame] | 936 | &first_block, &free_list, resblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (error) { |
| 938 | ASSERT(error != ENOENT); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 939 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 941 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 943 | if (is_dir) { |
| 944 | /* |
| 945 | * Drop the link from ip's "..". |
| 946 | */ |
| 947 | error = xfs_droplink(tp, dp); |
| 948 | if (error) |
| 949 | goto out_bmap_cancel; |
| 950 | |
| 951 | /* |
Christoph Hellwig | 2b7035f | 2008-10-30 17:55:18 +1100 | [diff] [blame] | 952 | * Drop the "." link from ip to self. |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 953 | */ |
| 954 | error = xfs_droplink(tp, ip); |
| 955 | if (error) |
| 956 | goto out_bmap_cancel; |
| 957 | } else { |
| 958 | /* |
| 959 | * When removing a non-directory we need to log the parent |
Dave Chinner | 26c5295 | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 960 | * inode here. For a directory this is done implicitly |
| 961 | * by the xfs_droplink call for the ".." entry. |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 962 | */ |
| 963 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 966 | /* |
Christoph Hellwig | 2b7035f | 2008-10-30 17:55:18 +1100 | [diff] [blame] | 967 | * Drop the link from dp to ip. |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 968 | */ |
| 969 | error = xfs_droplink(tp, ip); |
| 970 | if (error) |
| 971 | goto out_bmap_cancel; |
| 972 | |
| 973 | /* |
| 974 | * Determine if this is the last link while |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | * we are in the transaction. |
| 976 | */ |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 977 | link_zero = (ip->i_d.di_nlink == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * If this is a synchronous mount, make sure that the |
| 981 | * remove transaction goes to disk before returning to |
| 982 | * the user. |
| 983 | */ |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 984 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | xfs_trans_set_sync(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 987 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 988 | if (error) |
| 989 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 991 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Christoph Hellwig | 5df78e7 | 2008-04-22 17:34:24 +1000 | [diff] [blame] | 992 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | goto std_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 996 | * If we are using filestreams, kill the stream association. |
| 997 | * If the file is still open it may get a new one but that |
| 998 | * will get killed on last close in xfs_close() so we don't |
| 999 | * have to worry about that. |
| 1000 | */ |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1001 | if (!is_dir && link_zero && xfs_inode_is_filestream(ip)) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1002 | xfs_filestream_deassociate(ip); |
| 1003 | |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1004 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1006 | out_bmap_cancel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | xfs_bmap_cancel(&free_list); |
| 1008 | cancel_flags |= XFS_TRANS_ABORT; |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1009 | out_trans_cancel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | xfs_trans_cancel(tp, cancel_flags); |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1011 | std_return: |
| 1012 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1015 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | xfs_link( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1017 | xfs_inode_t *tdp, |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 1018 | xfs_inode_t *sip, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1019 | struct xfs_name *target_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1021 | xfs_mount_t *mp = tdp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | int error; |
| 1024 | xfs_bmap_free_t free_list; |
| 1025 | xfs_fsblock_t first_block; |
| 1026 | int cancel_flags; |
| 1027 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | int resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1030 | trace_xfs_link(tdp, target_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 1032 | ASSERT(!S_ISDIR(sip->i_d.di_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1035 | return XFS_ERROR(EIO); |
| 1036 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1037 | error = xfs_qm_dqattach(sip, 0); |
Christoph Hellwig | cb3f35b | 2009-02-04 09:34:20 +0100 | [diff] [blame] | 1038 | if (error) |
| 1039 | goto std_return; |
| 1040 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1041 | error = xfs_qm_dqattach(tdp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | if (error) |
| 1043 | goto std_return; |
| 1044 | |
| 1045 | tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); |
| 1046 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1047 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0, |
| 1049 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 1050 | if (error == ENOSPC) { |
| 1051 | resblks = 0; |
| 1052 | error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0, |
| 1053 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 1054 | } |
| 1055 | if (error) { |
| 1056 | cancel_flags = 0; |
| 1057 | goto error_return; |
| 1058 | } |
| 1059 | |
Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 1060 | xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1062 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); |
| 1063 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | /* |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1066 | * If we are using project inheritance, we only allow hard link |
| 1067 | * creation in our tree when the project IDs are the same; else |
| 1068 | * the tree quota mechanism could be circumvented. |
| 1069 | */ |
| 1070 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 1071 | (xfs_get_projid(tdp) != xfs_get_projid(sip)))) { |
Nathan Scott | b1ecdda | 2006-05-08 19:51:42 +1000 | [diff] [blame] | 1072 | error = XFS_ERROR(EXDEV); |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1073 | goto error_return; |
| 1074 | } |
| 1075 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1076 | error = xfs_dir_canenter(tp, tdp, target_name, resblks); |
| 1077 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | goto error_return; |
| 1079 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1080 | xfs_bmap_init(&free_list, &first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1082 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, |
| 1083 | &first_block, &free_list, resblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (error) |
| 1085 | goto abort_return; |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 1086 | xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); |
| 1088 | |
| 1089 | error = xfs_bumplink(tp, sip); |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 1090 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | goto abort_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* |
| 1094 | * If this is a synchronous mount, make sure that the |
| 1095 | * link transaction goes to disk before returning to |
| 1096 | * the user. |
| 1097 | */ |
| 1098 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 1099 | xfs_trans_set_sync(tp); |
| 1100 | } |
| 1101 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1102 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (error) { |
| 1104 | xfs_bmap_cancel(&free_list); |
| 1105 | goto abort_return; |
| 1106 | } |
| 1107 | |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1108 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
| 1110 | abort_return: |
| 1111 | cancel_flags |= XFS_TRANS_ABORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | error_return: |
| 1113 | xfs_trans_cancel(tp, cancel_flags); |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1114 | std_return: |
| 1115 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 1117 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1118 | int |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1119 | xfs_set_dmattrs( |
| 1120 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | u_int evmask, |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1122 | u_int16_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1124 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | int error; |
| 1127 | |
| 1128 | if (!capable(CAP_SYS_ADMIN)) |
| 1129 | return XFS_ERROR(EPERM); |
| 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1132 | return XFS_ERROR(EIO); |
| 1133 | |
| 1134 | tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS); |
| 1135 | error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0); |
| 1136 | if (error) { |
| 1137 | xfs_trans_cancel(tp, 0); |
| 1138 | return error; |
| 1139 | } |
| 1140 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1141 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 1143 | ip->i_d.di_dmevmask = evmask; |
| 1144 | ip->i_d.di_dmstate = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
| 1146 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1147 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | return error; |
| 1150 | } |
| 1151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * xfs_alloc_file_space() |
| 1154 | * This routine allocates disk space for the given file. |
| 1155 | * |
| 1156 | * If alloc_type == 0, this request is for an ALLOCSP type |
| 1157 | * request which will change the file size. In this case, no |
| 1158 | * DMAPI event will be generated by the call. A TRUNCATE event |
| 1159 | * will be generated later by xfs_setattr. |
| 1160 | * |
| 1161 | * If alloc_type != 0, this request is for a RESVSP type |
| 1162 | * request, and a DMAPI DM_EVENT_WRITE will be generated if the |
| 1163 | * lower block boundary byte address is less than the file's |
| 1164 | * length. |
| 1165 | * |
| 1166 | * RETURNS: |
| 1167 | * 0 on success |
| 1168 | * errno on error |
| 1169 | * |
| 1170 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1171 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | xfs_alloc_file_space( |
| 1173 | xfs_inode_t *ip, |
| 1174 | xfs_off_t offset, |
| 1175 | xfs_off_t len, |
| 1176 | int alloc_type, |
| 1177 | int attr_flags) |
| 1178 | { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1179 | xfs_mount_t *mp = ip->i_mount; |
| 1180 | xfs_off_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | xfs_filblks_t allocated_fsb; |
| 1182 | xfs_filblks_t allocatesize_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1183 | xfs_extlen_t extsz, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | xfs_fileoff_t startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1185 | xfs_fsblock_t firstfsb; |
| 1186 | int nimaps; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1187 | int quota_flag; |
| 1188 | int rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | xfs_trans_t *tp; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1190 | xfs_bmbt_irec_t imaps[1], *imapp; |
| 1191 | xfs_bmap_free_t free_list; |
| 1192 | uint qblocks, resblks, resrtextents; |
| 1193 | int committed; |
| 1194 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1196 | trace_xfs_alloc_file_space(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
| 1198 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1199 | return XFS_ERROR(EIO); |
| 1200 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1201 | error = xfs_qm_dqattach(ip, 0); |
| 1202 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | return error; |
| 1204 | |
| 1205 | if (len <= 0) |
| 1206 | return XFS_ERROR(EINVAL); |
| 1207 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 1208 | rt = XFS_IS_REALTIME_INODE(ip); |
| 1209 | extsz = xfs_get_extsz_hint(ip); |
| 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | count = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | imapp = &imaps[0]; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1213 | nimaps = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1215 | allocatesize_fsb = XFS_B_TO_FSB(mp, count); |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1218 | * Allocate file space until done or until there is an error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | while (allocatesize_fsb && !error) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1221 | xfs_fileoff_t s, e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1223 | /* |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 1224 | * Determine space reservations for data/realtime. |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1225 | */ |
| 1226 | if (unlikely(extsz)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | s = startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1228 | do_div(s, extsz); |
| 1229 | s *= extsz; |
| 1230 | e = startoffset_fsb + allocatesize_fsb; |
| 1231 | if ((temp = do_mod(startoffset_fsb, extsz))) |
| 1232 | e += temp; |
| 1233 | if ((temp = do_mod(e, extsz))) |
| 1234 | e += extsz - temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } else { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1236 | s = 0; |
| 1237 | e = allocatesize_fsb; |
| 1238 | } |
| 1239 | |
Dave Chinner | 72656c4 | 2010-09-03 12:19:33 +1000 | [diff] [blame] | 1240 | /* |
| 1241 | * The transaction reservation is limited to a 32-bit block |
| 1242 | * count, hence we need to limit the number of blocks we are |
| 1243 | * trying to reserve to avoid an overflow. We can't allocate |
| 1244 | * more than @nimaps extents, and an extent is limited on disk |
| 1245 | * to MAXEXTLEN (21 bits), so use that to enforce the limit. |
| 1246 | */ |
| 1247 | resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1248 | if (unlikely(rt)) { |
Dave Chinner | 72656c4 | 2010-09-03 12:19:33 +1000 | [diff] [blame] | 1249 | resrtextents = qblocks = resblks; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1250 | resrtextents /= mp->m_sb.sb_rextsize; |
| 1251 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 1252 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
| 1253 | } else { |
| 1254 | resrtextents = 0; |
Dave Chinner | 72656c4 | 2010-09-03 12:19:33 +1000 | [diff] [blame] | 1255 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1256 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1260 | * Allocate and setup the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | */ |
| 1262 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1263 | error = xfs_trans_reserve(tp, resblks, |
| 1264 | XFS_WRITE_LOG_RES(mp), resrtextents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | XFS_TRANS_PERM_LOG_RES, |
| 1266 | XFS_WRITE_LOG_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1268 | * Check for running out of space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | */ |
| 1270 | if (error) { |
| 1271 | /* |
| 1272 | * Free the transaction structure. |
| 1273 | */ |
| 1274 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 1275 | xfs_trans_cancel(tp, 0); |
| 1276 | break; |
| 1277 | } |
| 1278 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1279 | error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, |
| 1280 | 0, quota_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | if (error) |
| 1282 | goto error1; |
| 1283 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1284 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1286 | xfs_bmap_init(&free_list, &firstfsb); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 1287 | error = xfs_bmapi_write(tp, ip, startoffset_fsb, |
| 1288 | allocatesize_fsb, alloc_type, &firstfsb, |
| 1289 | 0, imapp, &nimaps, &free_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | if (error) { |
| 1291 | goto error0; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1295 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1297 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | if (error) { |
| 1299 | goto error0; |
| 1300 | } |
| 1301 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1302 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1304 | if (error) { |
| 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | allocated_fsb = imapp->br_blockcount; |
| 1309 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1310 | if (nimaps == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | error = XFS_ERROR(ENOSPC); |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | startoffset_fsb += allocated_fsb; |
| 1316 | allocatesize_fsb -= allocated_fsb; |
| 1317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | return error; |
| 1320 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1321 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | xfs_bmap_cancel(&free_list); |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 1323 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1324 | |
| 1325 | error1: /* Just cancel transaction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1327 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1328 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Zero file bytes between startoff and endoff inclusive. |
| 1333 | * The iolock is held exclusive and no blocks are buffered. |
Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 1334 | * |
| 1335 | * This function is used by xfs_free_file_space() to zero |
| 1336 | * partial blocks when the range to free is not block aligned. |
| 1337 | * When unreserving space with boundaries that are not block |
| 1338 | * aligned we round up the start and round down the end |
| 1339 | * boundaries and then use this function to zero the parts of |
| 1340 | * the blocks that got dropped during the rounding. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | */ |
| 1342 | STATIC int |
| 1343 | xfs_zero_remaining_bytes( |
| 1344 | xfs_inode_t *ip, |
| 1345 | xfs_off_t startoff, |
| 1346 | xfs_off_t endoff) |
| 1347 | { |
| 1348 | xfs_bmbt_irec_t imap; |
| 1349 | xfs_fileoff_t offset_fsb; |
| 1350 | xfs_off_t lastoffset; |
| 1351 | xfs_off_t offset; |
| 1352 | xfs_buf_t *bp; |
| 1353 | xfs_mount_t *mp = ip->i_mount; |
| 1354 | int nimap; |
| 1355 | int error = 0; |
| 1356 | |
Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 1357 | /* |
| 1358 | * Avoid doing I/O beyond eof - it's not necessary |
| 1359 | * since nothing can read beyond eof. The space will |
| 1360 | * be zeroed when the file is extended anyway. |
| 1361 | */ |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1362 | if (startoff >= XFS_ISIZE(ip)) |
Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 1363 | return 0; |
| 1364 | |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1365 | if (endoff > XFS_ISIZE(ip)) |
| 1366 | endoff = XFS_ISIZE(ip); |
Lachlan McIlroy | 2fd6f6e | 2008-09-17 16:52:50 +1000 | [diff] [blame] | 1367 | |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 1368 | bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ? |
| 1369 | mp->m_rtdev_targp : mp->m_ddev_targp, |
Dave Chinner | aa5c158 | 2012-04-23 15:58:56 +1000 | [diff] [blame] | 1370 | BTOBB(mp->m_sb.sb_blocksize), 0); |
Lachlan McIlroy | c642261 | 2008-12-05 13:16:15 +1100 | [diff] [blame] | 1371 | if (!bp) |
| 1372 | return XFS_ERROR(ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Christoph Hellwig | c8da0fa | 2011-07-08 14:36:25 +0200 | [diff] [blame] | 1374 | xfs_buf_unlock(bp); |
| 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { |
| 1377 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1378 | nimap = 1; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 1379 | error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | if (error || nimap < 1) |
| 1381 | break; |
| 1382 | ASSERT(imap.br_blockcount >= 1); |
| 1383 | ASSERT(imap.br_startoff == offset_fsb); |
| 1384 | lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; |
| 1385 | if (lastoffset > endoff) |
| 1386 | lastoffset = endoff; |
| 1387 | if (imap.br_startblock == HOLESTARTBLOCK) |
| 1388 | continue; |
| 1389 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 1390 | if (imap.br_state == XFS_EXT_UNWRITTEN) |
| 1391 | continue; |
| 1392 | XFS_BUF_UNDONE(bp); |
| 1393 | XFS_BUF_UNWRITE(bp); |
| 1394 | XFS_BUF_READ(bp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1395 | 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] | 1396 | xfsbdstrat(mp, bp); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1397 | error = xfs_buf_iowait(bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 1398 | if (error) { |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1399 | xfs_buf_ioerror_alert(bp, |
| 1400 | "xfs_zero_remaining_bytes(read)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | break; |
| 1402 | } |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 1403 | memset(bp->b_addr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | (offset - XFS_FSB_TO_B(mp, imap.br_startoff)), |
| 1405 | 0, lastoffset - offset + 1); |
| 1406 | XFS_BUF_UNDONE(bp); |
| 1407 | XFS_BUF_UNREAD(bp); |
| 1408 | XFS_BUF_WRITE(bp); |
| 1409 | xfsbdstrat(mp, bp); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1410 | error = xfs_buf_iowait(bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 1411 | if (error) { |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1412 | xfs_buf_ioerror_alert(bp, |
| 1413 | "xfs_zero_remaining_bytes(write)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | xfs_buf_free(bp); |
| 1418 | return error; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * xfs_free_file_space() |
| 1423 | * This routine frees disk space for the given file. |
| 1424 | * |
| 1425 | * This routine is only called by xfs_change_file_space |
| 1426 | * for an UNRESVSP type call. |
| 1427 | * |
| 1428 | * RETURNS: |
| 1429 | * 0 on success |
| 1430 | * errno on error |
| 1431 | * |
| 1432 | */ |
| 1433 | STATIC int |
| 1434 | xfs_free_file_space( |
| 1435 | xfs_inode_t *ip, |
| 1436 | xfs_off_t offset, |
| 1437 | xfs_off_t len, |
| 1438 | int attr_flags) |
| 1439 | { |
| 1440 | int committed; |
| 1441 | int done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | xfs_fileoff_t endoffset_fsb; |
| 1443 | int error; |
| 1444 | xfs_fsblock_t firstfsb; |
| 1445 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | xfs_bmbt_irec_t imap; |
| 1447 | xfs_off_t ioffset; |
| 1448 | xfs_extlen_t mod=0; |
| 1449 | xfs_mount_t *mp; |
| 1450 | int nimap; |
| 1451 | uint resblks; |
Dave Chinner | 28ca489 | 2013-05-20 09:51:09 +1000 | [diff] [blame] | 1452 | xfs_off_t rounding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | int rt; |
| 1454 | xfs_fileoff_t startoffset_fsb; |
| 1455 | xfs_trans_t *tp; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 1456 | int need_iolock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | mp = ip->i_mount; |
| 1459 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1460 | trace_xfs_free_file_space(ip); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 1461 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1462 | error = xfs_qm_dqattach(ip, 0); |
| 1463 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | return error; |
| 1465 | |
| 1466 | error = 0; |
| 1467 | if (len <= 0) /* if nothing being freed */ |
| 1468 | return error; |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 1469 | rt = XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | startoffset_fsb = XFS_B_TO_FSB(mp, offset); |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1471 | endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 1473 | if (attr_flags & XFS_ATTR_NOLOCK) |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 1474 | need_iolock = 0; |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 1475 | if (need_iolock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 1477 | /* wait for the completion of any pending DIOs */ |
Christoph Hellwig | 4a06fd2 | 2011-08-23 08:28:13 +0000 | [diff] [blame] | 1478 | inode_dio_wait(VFS_I(ip)); |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 1479 | } |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 1480 | |
Dave Chinner | 28ca489 | 2013-05-20 09:51:09 +1000 | [diff] [blame] | 1481 | rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | ioffset = offset & ~(rounding - 1); |
Dave Chinner | fb59581 | 2012-11-12 22:53:57 +1100 | [diff] [blame] | 1483 | error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping, |
| 1484 | ioffset, -1); |
| 1485 | if (error) |
| 1486 | goto out_unlock_iolock; |
| 1487 | truncate_pagecache_range(VFS_I(ip), ioffset, -1); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 1488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | /* |
| 1490 | * Need to zero the stuff we're not freeing, on disk. |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1491 | * 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] | 1492 | * actually need to zero the extent edges. Otherwise xfs_bunmapi |
| 1493 | * will take care of it for us. |
| 1494 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1495 | if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | nimap = 1; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 1497 | error = xfs_bmapi_read(ip, startoffset_fsb, 1, |
| 1498 | &imap, &nimap, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (error) |
| 1500 | goto out_unlock_iolock; |
| 1501 | ASSERT(nimap == 0 || nimap == 1); |
| 1502 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 1503 | xfs_daddr_t block; |
| 1504 | |
| 1505 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 1506 | block = imap.br_startblock; |
| 1507 | mod = do_div(block, mp->m_sb.sb_rextsize); |
| 1508 | if (mod) |
| 1509 | startoffset_fsb += mp->m_sb.sb_rextsize - mod; |
| 1510 | } |
| 1511 | nimap = 1; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 1512 | error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1, |
| 1513 | &imap, &nimap, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | if (error) |
| 1515 | goto out_unlock_iolock; |
| 1516 | ASSERT(nimap == 0 || nimap == 1); |
| 1517 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 1518 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 1519 | mod++; |
| 1520 | if (mod && (mod != mp->m_sb.sb_rextsize)) |
| 1521 | endoffset_fsb -= mod; |
| 1522 | } |
| 1523 | } |
| 1524 | if ((done = (endoffset_fsb <= startoffset_fsb))) |
| 1525 | /* |
| 1526 | * One contiguous piece to clear |
| 1527 | */ |
| 1528 | error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); |
| 1529 | else { |
| 1530 | /* |
| 1531 | * Some full blocks, possibly two pieces to clear |
| 1532 | */ |
| 1533 | if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) |
| 1534 | error = xfs_zero_remaining_bytes(ip, offset, |
| 1535 | XFS_FSB_TO_B(mp, startoffset_fsb) - 1); |
| 1536 | if (!error && |
| 1537 | XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) |
| 1538 | error = xfs_zero_remaining_bytes(ip, |
| 1539 | XFS_FSB_TO_B(mp, endoffset_fsb), |
| 1540 | offset + len - 1); |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * free file space until done or until there is an error |
| 1545 | */ |
| 1546 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 1547 | while (!error && !done) { |
| 1548 | |
| 1549 | /* |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 1550 | * allocate and setup the transaction. Allow this |
| 1551 | * transaction to dip into the reserve blocks to ensure |
| 1552 | * the freeing of the space succeeds at ENOSPC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | */ |
| 1554 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 1555 | tp->t_flags |= XFS_TRANS_RESERVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | error = xfs_trans_reserve(tp, |
| 1557 | resblks, |
| 1558 | XFS_WRITE_LOG_RES(mp), |
| 1559 | 0, |
| 1560 | XFS_TRANS_PERM_LOG_RES, |
| 1561 | XFS_WRITE_LOG_COUNT); |
| 1562 | |
| 1563 | /* |
| 1564 | * check for running out of space |
| 1565 | */ |
| 1566 | if (error) { |
| 1567 | /* |
| 1568 | * Free the transaction structure. |
| 1569 | */ |
| 1570 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 1571 | xfs_trans_cancel(tp, 0); |
| 1572 | break; |
| 1573 | } |
| 1574 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1575 | error = xfs_trans_reserve_quota(tp, mp, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1576 | ip->i_udquot, ip->i_gdquot, ip->i_pdquot, |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1577 | resblks, 0, XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | if (error) |
| 1579 | goto error1; |
| 1580 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1581 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
| 1583 | /* |
| 1584 | * issue the bunmapi() call to free the blocks |
| 1585 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1586 | xfs_bmap_init(&free_list, &firstfsb); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 1587 | error = xfs_bunmapi(tp, ip, startoffset_fsb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | endoffset_fsb - startoffset_fsb, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1589 | 0, 2, &firstfsb, &free_list, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | if (error) { |
| 1591 | goto error0; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * complete the transaction |
| 1596 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1597 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | if (error) { |
| 1599 | goto error0; |
| 1600 | } |
| 1601 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1602 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1604 | } |
| 1605 | |
| 1606 | out_unlock_iolock: |
| 1607 | if (need_iolock) |
| 1608 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1609 | return error; |
| 1610 | |
| 1611 | error0: |
| 1612 | xfs_bmap_cancel(&free_list); |
| 1613 | error1: |
| 1614 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1615 | xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) : |
| 1616 | XFS_ILOCK_EXCL); |
| 1617 | return error; |
| 1618 | } |
| 1619 | |
Dave Chinner | ef9d873 | 2012-11-29 15:26:33 +1100 | [diff] [blame] | 1620 | |
| 1621 | STATIC int |
| 1622 | xfs_zero_file_space( |
| 1623 | struct xfs_inode *ip, |
| 1624 | xfs_off_t offset, |
| 1625 | xfs_off_t len, |
| 1626 | int attr_flags) |
| 1627 | { |
| 1628 | struct xfs_mount *mp = ip->i_mount; |
| 1629 | uint granularity; |
| 1630 | xfs_off_t start_boundary; |
| 1631 | xfs_off_t end_boundary; |
| 1632 | int error; |
| 1633 | |
| 1634 | granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); |
| 1635 | |
| 1636 | /* |
| 1637 | * Round the range of extents we are going to convert inwards. If the |
| 1638 | * offset is aligned, then it doesn't get changed so we zero from the |
| 1639 | * start of the block offset points to. |
| 1640 | */ |
| 1641 | start_boundary = round_up(offset, granularity); |
| 1642 | end_boundary = round_down(offset + len, granularity); |
| 1643 | |
| 1644 | ASSERT(start_boundary >= offset); |
| 1645 | ASSERT(end_boundary <= offset + len); |
| 1646 | |
| 1647 | if (!(attr_flags & XFS_ATTR_NOLOCK)) |
| 1648 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 1649 | |
| 1650 | if (start_boundary < end_boundary - 1) { |
| 1651 | /* punch out the page cache over the conversion range */ |
| 1652 | truncate_pagecache_range(VFS_I(ip), start_boundary, |
| 1653 | end_boundary - 1); |
| 1654 | /* convert the blocks */ |
| 1655 | error = xfs_alloc_file_space(ip, start_boundary, |
| 1656 | end_boundary - start_boundary - 1, |
| 1657 | XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT, |
| 1658 | attr_flags); |
| 1659 | if (error) |
| 1660 | goto out_unlock; |
| 1661 | |
| 1662 | /* We've handled the interior of the range, now for the edges */ |
| 1663 | if (start_boundary != offset) |
| 1664 | error = xfs_iozero(ip, offset, start_boundary - offset); |
| 1665 | if (error) |
| 1666 | goto out_unlock; |
| 1667 | |
| 1668 | if (end_boundary != offset + len) |
| 1669 | error = xfs_iozero(ip, end_boundary, |
| 1670 | offset + len - end_boundary); |
| 1671 | |
| 1672 | } else { |
| 1673 | /* |
| 1674 | * It's either a sub-granularity range or the range spanned lies |
| 1675 | * partially across two adjacent blocks. |
| 1676 | */ |
| 1677 | error = xfs_iozero(ip, offset, len); |
| 1678 | } |
| 1679 | |
| 1680 | out_unlock: |
| 1681 | if (!(attr_flags & XFS_ATTR_NOLOCK)) |
| 1682 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1683 | return error; |
| 1684 | |
| 1685 | } |
| 1686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | /* |
| 1688 | * xfs_change_file_space() |
| 1689 | * This routine allocates or frees disk space for the given file. |
| 1690 | * The user specified parameters are checked for alignment and size |
| 1691 | * limitations. |
| 1692 | * |
| 1693 | * RETURNS: |
| 1694 | * 0 on success |
| 1695 | * errno on error |
| 1696 | * |
| 1697 | */ |
| 1698 | int |
| 1699 | xfs_change_file_space( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1700 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | int cmd, |
| 1702 | xfs_flock64_t *bf, |
| 1703 | xfs_off_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | int attr_flags) |
| 1705 | { |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1706 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | int clrprealloc; |
| 1708 | int error; |
| 1709 | xfs_fsize_t fsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | int setprealloc; |
| 1711 | xfs_off_t startoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | xfs_trans_t *tp; |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 1713 | struct iattr iattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1715 | if (!S_ISREG(ip->i_d.di_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | return XFS_ERROR(EINVAL); |
| 1717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | switch (bf->l_whence) { |
| 1719 | case 0: /*SEEK_SET*/ |
| 1720 | break; |
| 1721 | case 1: /*SEEK_CUR*/ |
| 1722 | bf->l_start += offset; |
| 1723 | break; |
| 1724 | case 2: /*SEEK_END*/ |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1725 | bf->l_start += XFS_ISIZE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | default: |
| 1728 | return XFS_ERROR(EINVAL); |
| 1729 | } |
| 1730 | |
Dave Chinner | f5b8911 | 2012-11-14 17:42:47 +1100 | [diff] [blame] | 1731 | /* |
| 1732 | * length of <= 0 for resv/unresv/zero is invalid. length for |
| 1733 | * alloc/free is ignored completely and we have no idea what userspace |
| 1734 | * might have set it to, so set it to zero to allow range |
| 1735 | * checks to pass. |
| 1736 | */ |
| 1737 | switch (cmd) { |
| 1738 | case XFS_IOC_ZERO_RANGE: |
| 1739 | case XFS_IOC_RESVSP: |
| 1740 | case XFS_IOC_RESVSP64: |
| 1741 | case XFS_IOC_UNRESVSP: |
| 1742 | case XFS_IOC_UNRESVSP64: |
| 1743 | if (bf->l_len <= 0) |
| 1744 | return XFS_ERROR(EINVAL); |
| 1745 | break; |
| 1746 | default: |
| 1747 | bf->l_len = 0; |
| 1748 | break; |
| 1749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
Dave Chinner | 3297238 | 2012-06-08 15:44:54 +1000 | [diff] [blame] | 1751 | if (bf->l_start < 0 || |
| 1752 | bf->l_start > mp->m_super->s_maxbytes || |
Dave Chinner | f5b8911 | 2012-11-14 17:42:47 +1100 | [diff] [blame] | 1753 | bf->l_start + bf->l_len < 0 || |
| 1754 | bf->l_start + bf->l_len >= mp->m_super->s_maxbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | return XFS_ERROR(EINVAL); |
| 1756 | |
| 1757 | bf->l_whence = 0; |
| 1758 | |
| 1759 | startoffset = bf->l_start; |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1760 | fsize = XFS_ISIZE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | setprealloc = clrprealloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | switch (cmd) { |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 1764 | case XFS_IOC_ZERO_RANGE: |
Dave Chinner | ef9d873 | 2012-11-29 15:26:33 +1100 | [diff] [blame] | 1765 | error = xfs_zero_file_space(ip, startoffset, bf->l_len, |
| 1766 | attr_flags); |
| 1767 | if (error) |
| 1768 | return error; |
| 1769 | setprealloc = 1; |
| 1770 | break; |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | case XFS_IOC_RESVSP: |
| 1773 | case XFS_IOC_RESVSP64: |
| 1774 | error = xfs_alloc_file_space(ip, startoffset, bf->l_len, |
Dave Chinner | ef9d873 | 2012-11-29 15:26:33 +1100 | [diff] [blame] | 1775 | XFS_BMAPI_PREALLOC, attr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | if (error) |
| 1777 | return error; |
| 1778 | setprealloc = 1; |
| 1779 | break; |
| 1780 | |
| 1781 | case XFS_IOC_UNRESVSP: |
| 1782 | case XFS_IOC_UNRESVSP64: |
| 1783 | if ((error = xfs_free_file_space(ip, startoffset, bf->l_len, |
| 1784 | attr_flags))) |
| 1785 | return error; |
| 1786 | break; |
| 1787 | |
| 1788 | case XFS_IOC_ALLOCSP: |
| 1789 | case XFS_IOC_ALLOCSP64: |
| 1790 | case XFS_IOC_FREESP: |
| 1791 | case XFS_IOC_FREESP64: |
Dave Chinner | bc4010ec | 2012-04-23 15:58:57 +1000 | [diff] [blame] | 1792 | /* |
| 1793 | * These operations actually do IO when extending the file, but |
| 1794 | * the allocation is done seperately to the zeroing that is |
| 1795 | * done. This set of operations need to be serialised against |
| 1796 | * other IO operations, such as truncate and buffered IO. We |
| 1797 | * need to take the IOLOCK here to serialise the allocation and |
| 1798 | * zeroing IO to prevent other IOLOCK holders (e.g. getbmap, |
| 1799 | * truncate, direct IO) from racing against the transient |
| 1800 | * allocated but not written state we can have here. |
| 1801 | */ |
| 1802 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | if (startoffset > fsize) { |
| 1804 | error = xfs_alloc_file_space(ip, fsize, |
Dave Chinner | bc4010ec | 2012-04-23 15:58:57 +1000 | [diff] [blame] | 1805 | startoffset - fsize, 0, |
| 1806 | attr_flags | XFS_ATTR_NOLOCK); |
| 1807 | if (error) { |
| 1808 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | break; |
Dave Chinner | bc4010ec | 2012-04-23 15:58:57 +1000 | [diff] [blame] | 1810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 1813 | iattr.ia_valid = ATTR_SIZE; |
| 1814 | iattr.ia_size = startoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
Dave Chinner | bc4010ec | 2012-04-23 15:58:57 +1000 | [diff] [blame] | 1816 | error = xfs_setattr_size(ip, &iattr, |
| 1817 | attr_flags | XFS_ATTR_NOLOCK); |
| 1818 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
| 1820 | if (error) |
| 1821 | return error; |
| 1822 | |
| 1823 | clrprealloc = 1; |
| 1824 | break; |
| 1825 | |
| 1826 | default: |
| 1827 | ASSERT(0); |
| 1828 | return XFS_ERROR(EINVAL); |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * update the inode timestamp, mode, and prealloc flag bits |
| 1833 | */ |
| 1834 | tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID); |
| 1835 | |
| 1836 | if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp), |
| 1837 | 0, 0, 0))) { |
| 1838 | /* ASSERT(0); */ |
| 1839 | xfs_trans_cancel(tp, 0); |
| 1840 | return error; |
| 1841 | } |
| 1842 | |
| 1843 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1844 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 1846 | if ((attr_flags & XFS_ATTR_DMI) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | ip->i_d.di_mode &= ~S_ISUID; |
| 1848 | |
| 1849 | /* |
| 1850 | * Note that we don't have to worry about mandatory |
| 1851 | * file locking being disabled here because we only |
| 1852 | * clear the S_ISGID bit if the Group execute bit is |
| 1853 | * on, but if it was on then mandatory locking wouldn't |
| 1854 | * have been enabled. |
| 1855 | */ |
| 1856 | if (ip->i_d.di_mode & S_IXGRP) |
| 1857 | ip->i_d.di_mode &= ~S_ISGID; |
| 1858 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 1859 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | } |
| 1861 | if (setprealloc) |
| 1862 | ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; |
| 1863 | else if (clrprealloc) |
| 1864 | ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; |
| 1865 | |
| 1866 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Dave Chinner | 8287889 | 2011-03-26 09:13:08 +1100 | [diff] [blame] | 1867 | if (attr_flags & XFS_ATTR_SYNC) |
| 1868 | xfs_trans_set_sync(tp); |
Christoph Hellwig | 23bb0be | 2011-09-18 20:47:51 +0000 | [diff] [blame] | 1869 | return xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |