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