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