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. |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3 | * Copyright (c) 2016 Christoph Hellwig. |
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 | */ |
Christoph Hellwig | 3b3dce0 | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 19 | #include <linux/iomap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 22 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_format.h" |
| 24 | #include "xfs_log_format.h" |
| 25 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_btree.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 32 | #include "xfs_bmap_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_error.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 34 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_trans_space.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_iomap.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 38 | #include "xfs_icache.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_quota.h" |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 40 | #include "xfs_dquot_item.h" |
| 41 | #include "xfs_dquot.h" |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 42 | #include "xfs_reflink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \ |
| 46 | << mp->m_writeio_log) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 48 | void |
| 49 | xfs_bmbt_to_iomap( |
| 50 | struct xfs_inode *ip, |
| 51 | struct iomap *iomap, |
| 52 | struct xfs_bmbt_irec *imap) |
| 53 | { |
| 54 | struct xfs_mount *mp = ip->i_mount; |
| 55 | |
| 56 | if (imap->br_startblock == HOLESTARTBLOCK) { |
| 57 | iomap->blkno = IOMAP_NULL_BLOCK; |
| 58 | iomap->type = IOMAP_HOLE; |
| 59 | } else if (imap->br_startblock == DELAYSTARTBLOCK) { |
| 60 | iomap->blkno = IOMAP_NULL_BLOCK; |
| 61 | iomap->type = IOMAP_DELALLOC; |
| 62 | } else { |
| 63 | iomap->blkno = xfs_fsb_to_db(ip, imap->br_startblock); |
| 64 | if (imap->br_state == XFS_EXT_UNWRITTEN) |
| 65 | iomap->type = IOMAP_UNWRITTEN; |
| 66 | else |
| 67 | iomap->type = IOMAP_MAPPED; |
| 68 | } |
| 69 | iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff); |
| 70 | iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount); |
| 71 | iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip)); |
| 72 | } |
| 73 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 74 | xfs_extlen_t |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 75 | xfs_eof_alignment( |
| 76 | struct xfs_inode *ip, |
| 77 | xfs_extlen_t extsize) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 78 | { |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 79 | struct xfs_mount *mp = ip->i_mount; |
| 80 | xfs_extlen_t align = 0; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 81 | |
Christoph Hellwig | bf322d9 | 2011-12-18 20:00:05 +0000 | [diff] [blame] | 82 | if (!XFS_IS_REALTIME_INODE(ip)) { |
| 83 | /* |
| 84 | * Round up the allocation request to a stripe unit |
| 85 | * (m_dalign) boundary if the file size is >= stripe unit |
| 86 | * size, and we are allocating past the allocation eof. |
| 87 | * |
| 88 | * If mounted with the "-o swalloc" option the alignment is |
| 89 | * increased from the strip unit size to the stripe width. |
| 90 | */ |
| 91 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 92 | align = mp->m_swidth; |
| 93 | else if (mp->m_dalign) |
| 94 | align = mp->m_dalign; |
| 95 | |
Peter Watkins | 76b5730 | 2014-12-04 09:30:51 +1100 | [diff] [blame] | 96 | if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align)) |
| 97 | align = 0; |
Christoph Hellwig | bf322d9 | 2011-12-18 20:00:05 +0000 | [diff] [blame] | 98 | } |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Always round up the allocation request to an extent boundary |
| 102 | * (when file on a real-time subvolume or has di_extsize hint). |
| 103 | */ |
| 104 | if (extsize) { |
Peter Watkins | 76b5730 | 2014-12-04 09:30:51 +1100 | [diff] [blame] | 105 | if (align) |
| 106 | align = roundup_64(align, extsize); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 107 | else |
| 108 | align = extsize; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 109 | } |
| 110 | |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 111 | return align; |
| 112 | } |
| 113 | |
| 114 | STATIC int |
| 115 | xfs_iomap_eof_align_last_fsb( |
| 116 | struct xfs_inode *ip, |
| 117 | xfs_extlen_t extsize, |
| 118 | xfs_fileoff_t *last_fsb) |
| 119 | { |
| 120 | xfs_extlen_t align = xfs_eof_alignment(ip, extsize); |
| 121 | |
Peter Watkins | 76b5730 | 2014-12-04 09:30:51 +1100 | [diff] [blame] | 122 | if (align) { |
| 123 | xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align); |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 124 | int eof, error; |
| 125 | |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 126 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 127 | if (error) |
| 128 | return error; |
| 129 | if (eof) |
| 130 | *last_fsb = new_last_fsb; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | STATIC int |
Dave Chinner | 6d4a8ec | 2011-03-07 10:06:35 +1100 | [diff] [blame] | 136 | xfs_alert_fsblock_zero( |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 137 | xfs_inode_t *ip, |
| 138 | xfs_bmbt_irec_t *imap) |
| 139 | { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 140 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 141 | "Access to block zero in inode %llu " |
| 142 | "start_block: %llx start_off: %llx " |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 143 | "blkcnt: %llx extent-state: %x", |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 144 | (unsigned long long)ip->i_ino, |
| 145 | (unsigned long long)imap->br_startblock, |
| 146 | (unsigned long long)imap->br_startoff, |
| 147 | (unsigned long long)imap->br_blockcount, |
| 148 | imap->br_state); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 149 | return -EFSCORRUPTED; |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 150 | } |
| 151 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 152 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | xfs_iomap_write_direct( |
| 154 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 155 | xfs_off_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | size_t count, |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 157 | xfs_bmbt_irec_t *imap, |
Christoph Hellwig | 405f804 | 2010-12-10 08:42:19 +0000 | [diff] [blame] | 158 | int nmaps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | xfs_fileoff_t offset_fsb; |
| 162 | xfs_fileoff_t last_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 163 | xfs_filblks_t count_fsb, resaligned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | xfs_fsblock_t firstfsb; |
Christoph Hellwig | f13eb20 | 2017-02-06 10:42:26 -0800 | [diff] [blame] | 165 | xfs_extlen_t extsz; |
Eric Sandeen | 0116d93 | 2005-11-02 15:00:01 +1100 | [diff] [blame] | 166 | int nimaps; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 167 | int quota_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int rt; |
| 169 | xfs_trans_t *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 170 | struct xfs_defer_ops dfops; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 171 | uint qblocks, resblks, resrtextents; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 172 | int error; |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 173 | int lockmode; |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 174 | int bmapi_flags = XFS_BMAPI_PREALLOC; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 175 | uint tflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 177 | rt = XFS_IS_REALTIME_INODE(ip); |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 178 | extsz = xfs_get_extsz_hint(ip); |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 179 | lockmode = XFS_ILOCK_SHARED; /* locked by caller */ |
| 180 | |
| 181 | ASSERT(xfs_isilocked(ip, lockmode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 183 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 184 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 185 | if ((offset + count) > XFS_ISIZE(ip)) { |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 186 | /* |
| 187 | * Assert that the in-core extent list is present since this can |
| 188 | * call xfs_iread_extents() and we only have the ilock shared. |
| 189 | * This should be safe because the lock was held around a bmapi |
| 190 | * call in the caller and we only need it to access the in-core |
| 191 | * list. |
| 192 | */ |
| 193 | ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags & |
| 194 | XFS_IFEXTENTS); |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 195 | error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 196 | if (error) |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 197 | goto out_unlock; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 198 | } else { |
Christoph Hellwig | 405f804 | 2010-12-10 08:42:19 +0000 | [diff] [blame] | 199 | if (nmaps && (imap->br_startblock == HOLESTARTBLOCK)) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 200 | last_fsb = MIN(last_fsb, (xfs_fileoff_t) |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 201 | imap->br_blockcount + |
| 202 | imap->br_startoff); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 203 | } |
| 204 | count_fsb = last_fsb - offset_fsb; |
| 205 | ASSERT(count_fsb > 0); |
Christoph Hellwig | f13eb20 | 2017-02-06 10:42:26 -0800 | [diff] [blame] | 206 | resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 207 | |
| 208 | if (unlikely(rt)) { |
| 209 | resrtextents = qblocks = resaligned; |
| 210 | resrtextents /= mp->m_sb.sb_rextsize; |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 211 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 212 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
| 213 | } else { |
| 214 | resrtextents = 0; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 215 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 216 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
| 217 | } |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 220 | * Drop the shared lock acquired by the caller, attach the dquot if |
| 221 | * necessary and move on to transaction setup. |
| 222 | */ |
| 223 | xfs_iunlock(ip, lockmode); |
| 224 | error = xfs_qm_dqattach(ip, 0); |
| 225 | if (error) |
| 226 | return error; |
| 227 | |
| 228 | /* |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 229 | * For DAX, we do not allocate unwritten extents, but instead we zero |
| 230 | * the block before we commit the transaction. Ideally we'd like to do |
| 231 | * this outside the transaction context, but if we commit and then crash |
| 232 | * we may not have zeroed the blocks and this will be exposed on |
| 233 | * recovery of the allocation. Hence we must zero before commit. |
Dave Chinner | 3b0fe47 | 2016-01-04 16:22:45 +1100 | [diff] [blame] | 234 | * |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 235 | * Further, if we are mapping unwritten extents here, we need to zero |
| 236 | * and convert them to written so that we don't need an unwritten extent |
| 237 | * callback for DAX. This also means that we need to be able to dip into |
Dave Chinner | 3b0fe47 | 2016-01-04 16:22:45 +1100 | [diff] [blame] | 238 | * the reserve block pool for bmbt block allocation if there is no space |
| 239 | * left but we need to do unwritten extent conversion. |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 240 | */ |
| 241 | if (IS_DAX(VFS_I(ip))) { |
| 242 | bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO; |
Christoph Hellwig | 63fbb4c | 2017-03-28 14:53:36 -0700 | [diff] [blame] | 243 | if (imap->br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 244 | tflags |= XFS_TRANS_RESERVE; |
Dave Chinner | 3b0fe47 | 2016-01-04 16:22:45 +1100 | [diff] [blame] | 245 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; |
| 246 | } |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 247 | } |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 248 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents, |
| 249 | tflags, &tp); |
| 250 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 251 | return error; |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 252 | |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 253 | lockmode = XFS_ILOCK_EXCL; |
| 254 | xfs_ilock(ip, lockmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 256 | error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 257 | if (error) |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 258 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 260 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 263 | * From this point onwards we overwrite the imap pointer that the |
| 264 | * caller gave to us. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 266 | xfs_defer_init(&dfops, &firstfsb); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 267 | nimaps = 1; |
Christoph Hellwig | d531d91 | 2014-02-10 10:27:43 +1100 | [diff] [blame] | 268 | error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, |
Dave Chinner | 264e89a | 2015-11-03 13:28:41 +1100 | [diff] [blame] | 269 | bmapi_flags, &firstfsb, resblks, imap, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 270 | &nimaps, &dfops); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 271 | if (error) |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 272 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 275 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 277 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 278 | if (error) |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 279 | goto out_bmap_cancel; |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 280 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 281 | error = xfs_trans_commit(tp); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 282 | if (error) |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 283 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 285 | /* |
| 286 | * Copy any maps to caller's array and return any error. |
| 287 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | if (nimaps == 0) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 289 | error = -ENOSPC; |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 290 | goto out_unlock; |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 291 | } |
| 292 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 293 | if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip))) |
Dave Chinner | 6d4a8ec | 2011-03-07 10:06:35 +1100 | [diff] [blame] | 294 | error = xfs_alert_fsblock_zero(ip, imap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 296 | out_unlock: |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 297 | xfs_iunlock(ip, lockmode); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 298 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 300 | out_bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 301 | xfs_defer_cancel(&dfops); |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 302 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 303 | out_trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 304 | xfs_trans_cancel(tp); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 305 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 308 | STATIC bool |
| 309 | xfs_quota_need_throttle( |
| 310 | struct xfs_inode *ip, |
| 311 | int type, |
| 312 | xfs_fsblock_t alloc_blocks) |
| 313 | { |
| 314 | struct xfs_dquot *dq = xfs_inode_dquot(ip, type); |
| 315 | |
| 316 | if (!dq || !xfs_this_quota_on(ip->i_mount, type)) |
| 317 | return false; |
| 318 | |
| 319 | /* no hi watermark, no throttle */ |
| 320 | if (!dq->q_prealloc_hi_wmark) |
| 321 | return false; |
| 322 | |
| 323 | /* under the lo watermark, no throttle */ |
| 324 | if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark) |
| 325 | return false; |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | STATIC void |
| 331 | xfs_quota_calc_throttle( |
| 332 | struct xfs_inode *ip, |
| 333 | int type, |
| 334 | xfs_fsblock_t *qblocks, |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 335 | int *qshift, |
| 336 | int64_t *qfreesp) |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 337 | { |
| 338 | int64_t freesp; |
| 339 | int shift = 0; |
| 340 | struct xfs_dquot *dq = xfs_inode_dquot(ip, type); |
| 341 | |
Eric Sandeen | 5cca3f6 | 2014-10-02 09:27:09 +1000 | [diff] [blame] | 342 | /* no dq, or over hi wmark, squash the prealloc completely */ |
| 343 | if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) { |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 344 | *qblocks = 0; |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 345 | *qfreesp = 0; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
| 349 | freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount; |
| 350 | if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) { |
| 351 | shift = 2; |
| 352 | if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT]) |
| 353 | shift += 2; |
| 354 | if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT]) |
| 355 | shift += 2; |
| 356 | } |
| 357 | |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 358 | if (freesp < *qfreesp) |
| 359 | *qfreesp = freesp; |
| 360 | |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 361 | /* only overwrite the throttle values if we are more aggressive */ |
| 362 | if ((freesp >> shift) < (*qblocks >> *qshift)) { |
| 363 | *qblocks = freesp; |
| 364 | *qshift = shift; |
| 365 | } |
| 366 | } |
| 367 | |
Dave Chinner | a1e16c2 | 2013-02-11 16:05:01 +1100 | [diff] [blame] | 368 | /* |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 369 | * If we are doing a write at the end of the file and there are no allocations |
| 370 | * past this one, then extend the allocation out to the file system's write |
| 371 | * iosize. |
| 372 | * |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 373 | * If we don't have a user specified preallocation size, dynamically increase |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 374 | * the preallocation size as the size of the file grows. Cap the maximum size |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 375 | * at a single extent or less if the filesystem is near full. The closer the |
| 376 | * filesystem is to full, the smaller the maximum prealocation. |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 377 | * |
| 378 | * As an exception we don't do any preallocation at all if the file is smaller |
| 379 | * than the minimum preallocation and we are using the default dynamic |
| 380 | * preallocation scheme, as it is likely this is the only write to the file that |
| 381 | * is going to be done. |
| 382 | * |
| 383 | * We clean up any extra space left over when the file is closed in |
| 384 | * xfs_inactive(). |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 385 | */ |
| 386 | STATIC xfs_fsblock_t |
| 387 | xfs_iomap_prealloc_size( |
Dave Chinner | a1e16c2 | 2013-02-11 16:05:01 +1100 | [diff] [blame] | 388 | struct xfs_inode *ip, |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 389 | loff_t offset, |
| 390 | loff_t count, |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 391 | xfs_extnum_t idx) |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 392 | { |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 393 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 394 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 395 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 396 | struct xfs_bmbt_irec prev; |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 397 | int shift = 0; |
| 398 | int64_t freesp; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 399 | xfs_fsblock_t qblocks; |
| 400 | int qshift = 0; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 401 | xfs_fsblock_t alloc_blocks = 0; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 402 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 403 | if (offset + count <= XFS_ISIZE(ip)) |
| 404 | return 0; |
| 405 | |
| 406 | if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) && |
| 407 | (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks))) |
| 408 | return 0; |
| 409 | |
| 410 | /* |
| 411 | * If an explicit allocsize is set, the file is small, or we |
| 412 | * are writing behind a hole, then use the minimum prealloc: |
| 413 | */ |
| 414 | if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) || |
| 415 | XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) || |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 416 | !xfs_iext_get_extent(ifp, idx - 1, &prev) || |
| 417 | prev.br_startoff + prev.br_blockcount < offset_fsb) |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 418 | return mp->m_writeio_blocks; |
| 419 | |
| 420 | /* |
| 421 | * Determine the initial size of the preallocation. We are beyond the |
| 422 | * current EOF here, but we need to take into account whether this is |
| 423 | * a sparse write or an extending write when determining the |
| 424 | * preallocation size. Hence we need to look up the extent that ends |
| 425 | * at the current write offset and use the result to determine the |
| 426 | * preallocation size. |
| 427 | * |
| 428 | * If the extent is a hole, then preallocation is essentially disabled. |
| 429 | * Otherwise we take the size of the preceding data extent as the basis |
| 430 | * for the preallocation size. If the size of the extent is greater than |
| 431 | * half the maximum extent length, then use the current offset as the |
| 432 | * basis. This ensures that for large files the preallocation size |
| 433 | * always extends to MAXEXTLEN rather than falling short due to things |
| 434 | * like stripe unit/width alignment of real extents. |
| 435 | */ |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 436 | if (prev.br_blockcount <= (MAXEXTLEN >> 1)) |
| 437 | alloc_blocks = prev.br_blockcount << 1; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 438 | else |
| 439 | alloc_blocks = XFS_B_TO_FSB(mp, offset); |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 440 | if (!alloc_blocks) |
| 441 | goto check_writeio; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 442 | qblocks = alloc_blocks; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 443 | |
Brian Foster | c9bdbdc | 2013-03-18 10:51:44 -0400 | [diff] [blame] | 444 | /* |
| 445 | * MAXEXTLEN is not a power of two value but we round the prealloc down |
| 446 | * to the nearest power of two value after throttling. To prevent the |
| 447 | * round down from unconditionally reducing the maximum supported prealloc |
| 448 | * size, we round up first, apply appropriate throttling, round down and |
| 449 | * cap the value to MAXEXTLEN. |
| 450 | */ |
| 451 | alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN), |
| 452 | alloc_blocks); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 453 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 454 | freesp = percpu_counter_read_positive(&mp->m_fdblocks); |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 455 | if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) { |
| 456 | shift = 2; |
| 457 | if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT]) |
| 458 | shift++; |
| 459 | if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT]) |
| 460 | shift++; |
| 461 | if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT]) |
| 462 | shift++; |
| 463 | if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT]) |
| 464 | shift++; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 465 | } |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 466 | |
| 467 | /* |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 468 | * Check each quota to cap the prealloc size, provide a shift value to |
| 469 | * throttle with and adjust amount of available space. |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 470 | */ |
| 471 | if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks)) |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 472 | xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift, |
| 473 | &freesp); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 474 | if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks)) |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 475 | xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift, |
| 476 | &freesp); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 477 | if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks)) |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 478 | xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift, |
| 479 | &freesp); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * The final prealloc size is set to the minimum of free space available |
| 483 | * in each of the quotas and the overall filesystem. |
| 484 | * |
| 485 | * The shift throttle value is set to the maximum value as determined by |
| 486 | * the global low free space values and per-quota low free space values. |
| 487 | */ |
| 488 | alloc_blocks = MIN(alloc_blocks, qblocks); |
| 489 | shift = MAX(shift, qshift); |
| 490 | |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 491 | if (shift) |
| 492 | alloc_blocks >>= shift; |
Brian Foster | c9bdbdc | 2013-03-18 10:51:44 -0400 | [diff] [blame] | 493 | /* |
| 494 | * rounddown_pow_of_two() returns an undefined result if we pass in |
| 495 | * alloc_blocks = 0. |
| 496 | */ |
| 497 | if (alloc_blocks) |
| 498 | alloc_blocks = rounddown_pow_of_two(alloc_blocks); |
| 499 | if (alloc_blocks > MAXEXTLEN) |
| 500 | alloc_blocks = MAXEXTLEN; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 501 | |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 502 | /* |
| 503 | * If we are still trying to allocate more space than is |
| 504 | * available, squash the prealloc hard. This can happen if we |
| 505 | * have a large file on a small filesystem and the above |
| 506 | * lowspace thresholds are smaller than MAXEXTLEN. |
| 507 | */ |
| 508 | while (alloc_blocks && alloc_blocks >= freesp) |
| 509 | alloc_blocks >>= 4; |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 510 | check_writeio: |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 511 | if (alloc_blocks < mp->m_writeio_blocks) |
| 512 | alloc_blocks = mp->m_writeio_blocks; |
Brian Foster | 19cb7e3 | 2013-03-18 10:51:48 -0400 | [diff] [blame] | 513 | trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift, |
| 514 | mp->m_writeio_blocks); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 515 | return alloc_blocks; |
| 516 | } |
| 517 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 518 | static int |
| 519 | xfs_file_iomap_begin_delay( |
| 520 | struct inode *inode, |
| 521 | loff_t offset, |
| 522 | loff_t count, |
| 523 | unsigned flags, |
| 524 | struct iomap *iomap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 526 | struct xfs_inode *ip = XFS_I(inode); |
| 527 | struct xfs_mount *mp = ip->i_mount; |
| 528 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 529 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 530 | xfs_fileoff_t maxbytes_fsb = |
| 531 | XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 532 | xfs_fileoff_t end_fsb; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 533 | int error = 0, eof = 0; |
| 534 | struct xfs_bmbt_irec got; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 535 | xfs_extnum_t idx; |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 536 | xfs_fsblock_t prealloc_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 538 | ASSERT(!XFS_IS_REALTIME_INODE(ip)); |
| 539 | ASSERT(!xfs_get_extsz_hint(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 541 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 542 | |
| 543 | if (unlikely(XFS_TEST_ERROR( |
| 544 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 545 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 546 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 547 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 548 | error = -EFSCORRUPTED; |
| 549 | goto out_unlock; |
| 550 | } |
| 551 | |
| 552 | XFS_STATS_INC(mp, xs_blk_mapw); |
| 553 | |
| 554 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 555 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 556 | if (error) |
| 557 | goto out_unlock; |
| 558 | } |
| 559 | |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 560 | eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 561 | if (!eof && got.br_startoff <= offset_fsb) { |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 562 | if (xfs_is_reflink_inode(ip)) { |
| 563 | bool shared; |
| 564 | |
| 565 | end_fsb = min(XFS_B_TO_FSB(mp, offset + count), |
| 566 | maxbytes_fsb); |
| 567 | xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb); |
| 568 | error = xfs_reflink_reserve_cow(ip, &got, &shared); |
| 569 | if (error) |
| 570 | goto out_unlock; |
| 571 | } |
| 572 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 573 | trace_xfs_iomap_found(ip, offset, count, 0, &got); |
| 574 | goto done; |
| 575 | } |
| 576 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 577 | error = xfs_qm_dqattach_locked(ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (error) |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 579 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 581 | /* |
| 582 | * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages |
| 583 | * to keep the chunks of work done where somewhat symmetric with the |
| 584 | * work writeback does. This is a completely arbitrary number pulled |
| 585 | * out of thin air as a best guess for initial testing. |
| 586 | * |
| 587 | * Note that the values needs to be less than 32-bits wide until |
| 588 | * the lower level functions are updated. |
| 589 | */ |
| 590 | count = min_t(loff_t, count, 1024 * PAGE_SIZE); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 591 | end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 592 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 593 | if (eof) { |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 594 | prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count, idx); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 595 | if (prealloc_blocks) { |
| 596 | xfs_extlen_t align; |
| 597 | xfs_off_t end_offset; |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 598 | xfs_fileoff_t p_end_fsb; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 599 | |
| 600 | end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 601 | p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) + |
| 602 | prealloc_blocks; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 603 | |
| 604 | align = xfs_eof_alignment(ip, 0); |
| 605 | if (align) |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 606 | p_end_fsb = roundup_64(p_end_fsb, align); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 607 | |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 608 | p_end_fsb = min(p_end_fsb, maxbytes_fsb); |
| 609 | ASSERT(p_end_fsb > offset_fsb); |
| 610 | prealloc_blocks = p_end_fsb - end_fsb; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 611 | } |
| 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Dave Chinner | 8de2bf9 | 2009-04-06 18:49:12 +0200 | [diff] [blame] | 614 | retry: |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 615 | error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb, |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 616 | end_fsb - offset_fsb, prealloc_blocks, &got, &idx, eof); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 617 | switch (error) { |
| 618 | case 0: |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 619 | break; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 620 | case -ENOSPC: |
| 621 | case -EDQUOT: |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 622 | /* retry without any preallocation */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 623 | trace_xfs_delalloc_enospc(ip, offset, count); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 624 | if (prealloc_blocks) { |
| 625 | prealloc_blocks = 0; |
Dave Chinner | 9aa0500 | 2012-10-08 21:56:04 +1100 | [diff] [blame] | 626 | goto retry; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 627 | } |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 628 | /*FALLTHRU*/ |
| 629 | default: |
| 630 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 633 | /* |
| 634 | * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch |
| 635 | * them out if the write happens to fail. |
| 636 | */ |
| 637 | iomap->flags = IOMAP_F_NEW; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 638 | trace_xfs_iomap_alloc(ip, offset, count, 0, &got); |
| 639 | done: |
| 640 | if (isnullstartblock(got.br_startblock)) |
| 641 | got.br_startblock = DELAYSTARTBLOCK; |
| 642 | |
| 643 | if (!got.br_startblock) { |
| 644 | error = xfs_alert_fsblock_zero(ip, &got); |
| 645 | if (error) |
| 646 | goto out_unlock; |
| 647 | } |
| 648 | |
| 649 | xfs_bmbt_to_iomap(ip, iomap, &got); |
| 650 | |
| 651 | out_unlock: |
| 652 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 653 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Pass in a delayed allocate extent, convert it to real extents; |
| 658 | * return to the caller the extent we create which maps on top of |
| 659 | * the originating callers request. |
| 660 | * |
| 661 | * Called without a lock on the inode. |
David Chinner | e4143a1 | 2007-11-23 16:29:11 +1100 | [diff] [blame] | 662 | * |
| 663 | * We no longer bother to look at the incoming map - all we have to |
| 664 | * guarantee is that whatever we allocate fills the required range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | */ |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 666 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | xfs_iomap_write_allocate( |
| 668 | xfs_inode_t *ip, |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 669 | int whichfork, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 670 | xfs_off_t offset, |
Christoph Hellwig | 405f804 | 2010-12-10 08:42:19 +0000 | [diff] [blame] | 671 | xfs_bmbt_irec_t *imap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | xfs_fileoff_t offset_fsb, last_block; |
| 675 | xfs_fileoff_t end_fsb, map_start_fsb; |
| 676 | xfs_fsblock_t first_block; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 677 | struct xfs_defer_ops dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | xfs_filblks_t count_fsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | xfs_trans_t *tp; |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 680 | int nimaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | int error = 0; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 682 | int flags = XFS_BMAPI_DELALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | int nres; |
| 684 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 685 | if (whichfork == XFS_COW_FORK) |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 686 | flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Make sure that the dquots are there. |
| 690 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 691 | error = xfs_qm_dqattach(ip, 0); |
| 692 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 693 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 695 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 696 | count_fsb = imap->br_blockcount; |
| 697 | map_start_fsb = imap->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 699 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | while (count_fsb != 0) { |
| 702 | /* |
| 703 | * Set up a transaction with which to allocate the |
| 704 | * backing store for the file. Do allocations in a |
| 705 | * loop until we get some space in the range we are |
| 706 | * interested in. The other space that might be allocated |
| 707 | * is in the delayed allocation extent on which we sit |
| 708 | * but before our buffer starts. |
| 709 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | nimaps = 0; |
| 711 | while (nimaps == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK); |
Christoph Hellwig | 0af32fb | 2016-08-17 08:30:28 +1000 | [diff] [blame] | 713 | /* |
| 714 | * We have already reserved space for the extent and any |
| 715 | * indirect blocks when creating the delalloc extent, |
| 716 | * there is no need to reserve space in this transaction |
| 717 | * again. |
| 718 | */ |
| 719 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 720 | 0, XFS_TRANS_RESERVE, &tp); |
| 721 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 722 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 725 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 727 | xfs_defer_init(&dfops, &first_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
David Chinner | e4143a1 | 2007-11-23 16:29:11 +1100 | [diff] [blame] | 730 | * it is possible that the extents have changed since |
| 731 | * we did the read call as we dropped the ilock for a |
| 732 | * while. We have to be careful about truncates or hole |
| 733 | * punchs here - we are not allowed to allocate |
| 734 | * non-delalloc blocks here. |
| 735 | * |
| 736 | * The only protection against truncation is the pages |
| 737 | * for the range we are being asked to convert are |
| 738 | * locked and hence a truncate will block on them |
| 739 | * first. |
| 740 | * |
| 741 | * As a result, if we go beyond the range we really |
| 742 | * need and hit an delalloc extent boundary followed by |
| 743 | * a hole while we have excess blocks in the map, we |
| 744 | * will fill the hole incorrectly and overrun the |
| 745 | * transaction reservation. |
| 746 | * |
| 747 | * Using a single map prevents this as we are forced to |
| 748 | * check each map we look for overlap with the desired |
| 749 | * range and abort as soon as we find it. Also, given |
| 750 | * that we only return a single map, having one beyond |
| 751 | * what we can return is probably a bit silly. |
| 752 | * |
| 753 | * We also need to check that we don't go beyond EOF; |
| 754 | * this is a truncate optimisation as a truncate sets |
| 755 | * the new file size before block on the pages we |
| 756 | * currently have locked under writeback. Because they |
| 757 | * are about to be tossed, we don't need to write them |
| 758 | * back.... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | */ |
David Chinner | e4143a1 | 2007-11-23 16:29:11 +1100 | [diff] [blame] | 760 | nimaps = 1; |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 761 | end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); |
Eric Sandeen | 7fb2cd4 | 2014-04-14 18:58:05 +1000 | [diff] [blame] | 762 | error = xfs_bmap_last_offset(ip, &last_block, |
David Chinner | 7c9ef85 | 2008-04-10 12:21:59 +1000 | [diff] [blame] | 763 | XFS_DATA_FORK); |
| 764 | if (error) |
| 765 | goto trans_cancel; |
| 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | last_block = XFS_FILEOFF_MAX(last_block, end_fsb); |
| 768 | if ((map_start_fsb + count_fsb) > last_block) { |
| 769 | count_fsb = last_block - map_start_fsb; |
| 770 | if (count_fsb == 0) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 771 | error = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | goto trans_cancel; |
| 773 | } |
| 774 | } |
| 775 | |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 776 | /* |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 777 | * From this point onwards we overwrite the imap |
| 778 | * pointer that the caller gave to us. |
| 779 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 780 | error = xfs_bmapi_write(tp, ip, map_start_fsb, |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 781 | count_fsb, flags, &first_block, |
Brian Foster | dbd5c8c | 2015-10-12 16:04:13 +1100 | [diff] [blame] | 782 | nres, imap, &nimaps, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 783 | &dfops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (error) |
| 785 | goto trans_cancel; |
| 786 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 787 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (error) |
| 789 | goto trans_cancel; |
| 790 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 791 | error = xfs_trans_commit(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (error) |
| 793 | goto error0; |
| 794 | |
| 795 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * See if we were able to allocate an extent that |
| 800 | * covers at least part of the callers request |
| 801 | */ |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 802 | if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip))) |
Dave Chinner | 6d4a8ec | 2011-03-07 10:06:35 +1100 | [diff] [blame] | 803 | return xfs_alert_fsblock_zero(ip, imap); |
David Chinner | 86c4d62 | 2008-04-29 12:53:21 +1000 | [diff] [blame] | 804 | |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 805 | if ((offset_fsb >= imap->br_startoff) && |
| 806 | (offset_fsb < (imap->br_startoff + |
| 807 | imap->br_blockcount))) { |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 808 | XFS_STATS_INC(mp, xs_xstrat_quick); |
David Chinner | e4143a1 | 2007-11-23 16:29:11 +1100 | [diff] [blame] | 809 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
David Chinner | e4143a1 | 2007-11-23 16:29:11 +1100 | [diff] [blame] | 812 | /* |
| 813 | * So far we have not mapped the requested part of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | * file, just surrounding data, try again. |
| 815 | */ |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 816 | count_fsb -= imap->br_blockcount; |
| 817 | map_start_fsb = imap->br_startoff + imap->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | trans_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 821 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 822 | xfs_trans_cancel(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | error0: |
| 824 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 825 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | int |
| 829 | xfs_iomap_write_unwritten( |
| 830 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 831 | xfs_off_t offset, |
Christoph Hellwig | d32057f | 2015-01-09 10:48:12 +1100 | [diff] [blame] | 832 | xfs_off_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
| 834 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | xfs_fileoff_t offset_fsb; |
| 836 | xfs_filblks_t count_fsb; |
| 837 | xfs_filblks_t numblks_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 838 | xfs_fsblock_t firstfsb; |
| 839 | int nimaps; |
| 840 | xfs_trans_t *tp; |
| 841 | xfs_bmbt_irec_t imap; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 842 | struct xfs_defer_ops dfops; |
Christoph Hellwig | 84803fb | 2012-02-29 09:53:50 +0000 | [diff] [blame] | 843 | xfs_fsize_t i_size; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 844 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 847 | trace_xfs_unwritten_convert(ip, offset, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 850 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 851 | count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); |
| 852 | |
Lachlan McIlroy | 4ddd8bb | 2008-06-27 13:32:53 +1000 | [diff] [blame] | 853 | /* |
| 854 | * Reserve enough blocks in this transaction for two complete extent |
| 855 | * btree splits. We may be converting the middle part of an unwritten |
| 856 | * extent and in this case we will insert two new extents in the btree |
| 857 | * each of which could cause a full split. |
| 858 | * |
| 859 | * This reservation amount will be used in the first call to |
| 860 | * xfs_bmbt_split() to select an AG with enough space to satisfy the |
| 861 | * rest of the operation. |
| 862 | */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 863 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 865 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | /* |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 867 | * Set up a transaction to convert the range of extents |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | * from unwritten to real. Do allocations in a loop until |
| 869 | * we have covered the range passed in. |
Christoph Hellwig | 80641dc | 2009-10-19 04:00:03 +0000 | [diff] [blame] | 870 | * |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 871 | * Note that we can't risk to recursing back into the filesystem |
| 872 | * here as we might be asked to write out the same inode that we |
| 873 | * complete here and might deadlock on the iolock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | */ |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 875 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, |
| 876 | XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp); |
| 877 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 878 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
| 880 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 881 | xfs_trans_ijoin(tp, ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | /* |
| 884 | * Modify the unwritten extent state of the buffer. |
| 885 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 886 | xfs_defer_init(&dfops, &firstfsb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | nimaps = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 888 | error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, |
Brian Foster | dbd5c8c | 2015-10-12 16:04:13 +1100 | [diff] [blame] | 889 | XFS_BMAPI_CONVERT, &firstfsb, resblks, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 890 | &imap, &nimaps, &dfops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | if (error) |
| 892 | goto error_on_bmapi_transaction; |
| 893 | |
Christoph Hellwig | 84803fb | 2012-02-29 09:53:50 +0000 | [diff] [blame] | 894 | /* |
| 895 | * Log the updated inode size as we go. We have to be careful |
| 896 | * to only log it up to the actual write offset if it is |
| 897 | * halfway into a block. |
| 898 | */ |
| 899 | i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb); |
| 900 | if (i_size > offset + count) |
| 901 | i_size = offset + count; |
| 902 | |
| 903 | i_size = xfs_new_eof(ip, i_size); |
| 904 | if (i_size) { |
| 905 | ip->i_d.di_size = i_size; |
| 906 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 907 | } |
| 908 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 909 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (error) |
| 911 | goto error_on_bmapi_transaction; |
| 912 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 913 | error = xfs_trans_commit(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 915 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 916 | return error; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 917 | |
David Chinner | 86c4d62 | 2008-04-29 12:53:21 +1000 | [diff] [blame] | 918 | if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) |
Dave Chinner | 6d4a8ec | 2011-03-07 10:06:35 +1100 | [diff] [blame] | 919 | return xfs_alert_fsblock_zero(ip, &imap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
| 921 | if ((numblks_fsb = imap.br_blockcount) == 0) { |
| 922 | /* |
| 923 | * The numblks_fsb value should always get |
| 924 | * smaller, otherwise the loop is stuck. |
| 925 | */ |
| 926 | ASSERT(imap.br_blockcount); |
| 927 | break; |
| 928 | } |
| 929 | offset_fsb += numblks_fsb; |
| 930 | count_fsb -= numblks_fsb; |
| 931 | } while (count_fsb > 0); |
| 932 | |
| 933 | return 0; |
| 934 | |
| 935 | error_on_bmapi_transaction: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 936 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 937 | xfs_trans_cancel(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 939 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
Christoph Hellwig | 3b3dce0 | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 941 | |
Christoph Hellwig | 6c31f49 | 2016-09-19 11:28:38 +1000 | [diff] [blame] | 942 | static inline bool imap_needs_alloc(struct inode *inode, |
| 943 | struct xfs_bmbt_irec *imap, int nimaps) |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 944 | { |
| 945 | return !nimaps || |
| 946 | imap->br_startblock == HOLESTARTBLOCK || |
Christoph Hellwig | 6c31f49 | 2016-09-19 11:28:38 +1000 | [diff] [blame] | 947 | imap->br_startblock == DELAYSTARTBLOCK || |
Christoph Hellwig | 63fbb4c | 2017-03-28 14:53:36 -0700 | [diff] [blame] | 948 | (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 949 | } |
| 950 | |
Christoph Hellwig | acdda3a | 2016-11-30 14:37:15 +1100 | [diff] [blame] | 951 | static inline bool need_excl_ilock(struct xfs_inode *ip, unsigned flags) |
| 952 | { |
| 953 | /* |
| 954 | * COW writes will allocate delalloc space, so we need to make sure |
| 955 | * to take the lock exclusively here. |
| 956 | */ |
| 957 | if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO))) |
| 958 | return true; |
| 959 | if ((flags & IOMAP_DIRECT) && (flags & IOMAP_WRITE)) |
| 960 | return true; |
| 961 | return false; |
| 962 | } |
| 963 | |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 964 | static int |
| 965 | xfs_file_iomap_begin( |
| 966 | struct inode *inode, |
| 967 | loff_t offset, |
| 968 | loff_t length, |
| 969 | unsigned flags, |
| 970 | struct iomap *iomap) |
| 971 | { |
| 972 | struct xfs_inode *ip = XFS_I(inode); |
| 973 | struct xfs_mount *mp = ip->i_mount; |
| 974 | struct xfs_bmbt_irec imap; |
| 975 | xfs_fileoff_t offset_fsb, end_fsb; |
| 976 | int nimaps = 1, error = 0; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 977 | bool shared = false, trimmed = false; |
Christoph Hellwig | 66642c5 | 2016-09-19 11:26:39 +1000 | [diff] [blame] | 978 | unsigned lockmode; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 979 | |
| 980 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 981 | return -EIO; |
| 982 | |
Christoph Hellwig | acdda3a | 2016-11-30 14:37:15 +1100 | [diff] [blame] | 983 | if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) && |
| 984 | !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) { |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 985 | /* Reserve delalloc blocks for regular writeback. */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 986 | return xfs_file_iomap_begin_delay(inode, offset, length, flags, |
| 987 | iomap); |
| 988 | } |
| 989 | |
Christoph Hellwig | acdda3a | 2016-11-30 14:37:15 +1100 | [diff] [blame] | 990 | if (need_excl_ilock(ip, flags)) { |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 991 | lockmode = XFS_ILOCK_EXCL; |
| 992 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 993 | } else { |
| 994 | lockmode = xfs_ilock_data_map_shared(ip); |
| 995 | } |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 996 | |
| 997 | ASSERT(offset <= mp->m_super->s_maxbytes); |
| 998 | if ((xfs_fsize_t)offset + length > mp->m_super->s_maxbytes) |
| 999 | length = mp->m_super->s_maxbytes - offset; |
| 1000 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1001 | end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1002 | |
| 1003 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1004 | &nimaps, 0); |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 1005 | if (error) |
| 1006 | goto out_unlock; |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1007 | |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 1008 | if (flags & IOMAP_REPORT) { |
Christoph Hellwig | 5f9268c | 2016-10-20 15:53:32 +1100 | [diff] [blame] | 1009 | /* Trim the mapping to the nearest shared extent boundary. */ |
| 1010 | error = xfs_reflink_trim_around_shared(ip, &imap, &shared, |
| 1011 | &trimmed); |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 1012 | if (error) |
| 1013 | goto out_unlock; |
| 1014 | } |
| 1015 | |
| 1016 | if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) { |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 1017 | if (flags & IOMAP_DIRECT) { |
| 1018 | /* may drop and re-acquire the ilock */ |
| 1019 | error = xfs_reflink_allocate_cow(ip, &imap, &shared, |
| 1020 | &lockmode); |
| 1021 | if (error) |
| 1022 | goto out_unlock; |
| 1023 | } else { |
| 1024 | error = xfs_reflink_reserve_cow(ip, &imap, &shared); |
| 1025 | if (error) |
| 1026 | goto out_unlock; |
| 1027 | } |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 1028 | |
| 1029 | end_fsb = imap.br_startoff + imap.br_blockcount; |
| 1030 | length = XFS_FSB_TO_B(mp, end_fsb) - offset; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1031 | } |
| 1032 | |
Christoph Hellwig | 6c31f49 | 2016-09-19 11:28:38 +1000 | [diff] [blame] | 1033 | if ((flags & IOMAP_WRITE) && imap_needs_alloc(inode, &imap, nimaps)) { |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1034 | /* |
| 1035 | * We cap the maximum length we map here to MAX_WRITEBACK_PAGES |
| 1036 | * pages to keep the chunks of work done where somewhat symmetric |
| 1037 | * with the work writeback does. This is a completely arbitrary |
| 1038 | * number pulled out of thin air as a best guess for initial |
| 1039 | * testing. |
| 1040 | * |
| 1041 | * Note that the values needs to be less than 32-bits wide until |
| 1042 | * the lower level functions are updated. |
| 1043 | */ |
| 1044 | length = min_t(loff_t, length, 1024 * PAGE_SIZE); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1045 | /* |
| 1046 | * xfs_iomap_write_direct() expects the shared lock. It |
| 1047 | * is unlocked on return. |
| 1048 | */ |
Christoph Hellwig | 66642c5 | 2016-09-19 11:26:39 +1000 | [diff] [blame] | 1049 | if (lockmode == XFS_ILOCK_EXCL) |
| 1050 | xfs_ilock_demote(ip, lockmode); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1051 | error = xfs_iomap_write_direct(ip, offset, length, &imap, |
| 1052 | nimaps); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1053 | if (error) |
| 1054 | return error; |
| 1055 | |
Christoph Hellwig | ecd5072 | 2016-09-19 11:24:37 +1000 | [diff] [blame] | 1056 | iomap->flags = IOMAP_F_NEW; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1057 | trace_xfs_iomap_alloc(ip, offset, length, 0, &imap); |
Christoph Hellwig | b95a212 | 2016-08-17 08:44:52 +1000 | [diff] [blame] | 1058 | } else { |
| 1059 | ASSERT(nimaps); |
| 1060 | |
Christoph Hellwig | 66642c5 | 2016-09-19 11:26:39 +1000 | [diff] [blame] | 1061 | xfs_iunlock(ip, lockmode); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1062 | trace_xfs_iomap_found(ip, offset, length, 0, &imap); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1063 | } |
| 1064 | |
Christoph Hellwig | b95a212 | 2016-08-17 08:44:52 +1000 | [diff] [blame] | 1065 | xfs_bmbt_to_iomap(ip, iomap, &imap); |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1066 | if (shared) |
| 1067 | iomap->flags |= IOMAP_F_SHARED; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1068 | return 0; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 1069 | out_unlock: |
| 1070 | xfs_iunlock(ip, lockmode); |
| 1071 | return error; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static int |
| 1075 | xfs_file_iomap_end_delalloc( |
| 1076 | struct xfs_inode *ip, |
| 1077 | loff_t offset, |
| 1078 | loff_t length, |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1079 | ssize_t written, |
| 1080 | struct iomap *iomap) |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1081 | { |
| 1082 | struct xfs_mount *mp = ip->i_mount; |
| 1083 | xfs_fileoff_t start_fsb; |
| 1084 | xfs_fileoff_t end_fsb; |
| 1085 | int error = 0; |
| 1086 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1087 | /* |
| 1088 | * Behave as if the write failed if drop writes is enabled. Set the NEW |
| 1089 | * flag to force delalloc cleanup. |
| 1090 | */ |
| 1091 | if (xfs_mp_drop_writes(mp)) { |
| 1092 | iomap->flags |= IOMAP_F_NEW; |
Brian Foster | 9dbddd7 | 2017-02-13 22:48:17 -0800 | [diff] [blame] | 1093 | written = 0; |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1094 | } |
Brian Foster | 9dbddd7 | 2017-02-13 22:48:17 -0800 | [diff] [blame] | 1095 | |
Brian Foster | fa7f138 | 2017-02-16 17:19:12 -0800 | [diff] [blame] | 1096 | /* |
| 1097 | * start_fsb refers to the first unused block after a short write. If |
| 1098 | * nothing was written, round offset down to point at the first block in |
| 1099 | * the range. |
| 1100 | */ |
| 1101 | if (unlikely(!written)) |
| 1102 | start_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1103 | else |
| 1104 | start_fsb = XFS_B_TO_FSB(mp, offset + written); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1105 | end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1106 | |
| 1107 | /* |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1108 | * Trim delalloc blocks if they were allocated by this write and we |
| 1109 | * didn't manage to write the whole range. |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1110 | * |
| 1111 | * We don't need to care about racing delalloc as we hold i_mutex |
| 1112 | * across the reserve/allocate/unreserve calls. If there are delalloc |
| 1113 | * blocks in the range, they are ours. |
| 1114 | */ |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1115 | if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) { |
Brian Foster | fa7f138 | 2017-02-16 17:19:12 -0800 | [diff] [blame] | 1116 | truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb), |
| 1117 | XFS_FSB_TO_B(mp, end_fsb) - 1); |
| 1118 | |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1119 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1120 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 1121 | end_fsb - start_fsb); |
| 1122 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1123 | |
| 1124 | if (error && !XFS_FORCED_SHUTDOWN(mp)) { |
| 1125 | xfs_alert(mp, "%s: unable to clean up ino %lld", |
| 1126 | __func__, ip->i_ino); |
| 1127 | return error; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | static int |
| 1135 | xfs_file_iomap_end( |
| 1136 | struct inode *inode, |
| 1137 | loff_t offset, |
| 1138 | loff_t length, |
| 1139 | ssize_t written, |
| 1140 | unsigned flags, |
| 1141 | struct iomap *iomap) |
| 1142 | { |
| 1143 | if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC) |
| 1144 | return xfs_file_iomap_end_delalloc(XFS_I(inode), offset, |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1145 | length, written, iomap); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1149 | const struct iomap_ops xfs_iomap_ops = { |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1150 | .iomap_begin = xfs_file_iomap_begin, |
| 1151 | .iomap_end = xfs_file_iomap_end, |
| 1152 | }; |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1153 | |
| 1154 | static int |
| 1155 | xfs_xattr_iomap_begin( |
| 1156 | struct inode *inode, |
| 1157 | loff_t offset, |
| 1158 | loff_t length, |
| 1159 | unsigned flags, |
| 1160 | struct iomap *iomap) |
| 1161 | { |
| 1162 | struct xfs_inode *ip = XFS_I(inode); |
| 1163 | struct xfs_mount *mp = ip->i_mount; |
| 1164 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1165 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1166 | struct xfs_bmbt_irec imap; |
| 1167 | int nimaps = 1, error = 0; |
| 1168 | unsigned lockmode; |
| 1169 | |
| 1170 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1171 | return -EIO; |
| 1172 | |
| 1173 | lockmode = xfs_ilock_data_map_shared(ip); |
| 1174 | |
| 1175 | /* if there are no attribute fork or extents, return ENOENT */ |
| 1176 | if (XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) { |
| 1177 | error = -ENOENT; |
| 1178 | goto out_unlock; |
| 1179 | } |
| 1180 | |
| 1181 | ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL); |
| 1182 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, |
| 1183 | &nimaps, XFS_BMAPI_ENTIRE | XFS_BMAPI_ATTRFORK); |
| 1184 | out_unlock: |
| 1185 | xfs_iunlock(ip, lockmode); |
| 1186 | |
| 1187 | if (!error) { |
| 1188 | ASSERT(nimaps); |
| 1189 | xfs_bmbt_to_iomap(ip, iomap, &imap); |
| 1190 | } |
| 1191 | |
| 1192 | return error; |
| 1193 | } |
| 1194 | |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1195 | const struct iomap_ops xfs_xattr_iomap_ops = { |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1196 | .iomap_begin = xfs_xattr_iomap_begin, |
| 1197 | }; |