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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 9 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 10 | #include "xfs_format.h" |
| 11 | #include "xfs_log_format.h" |
| 12 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 15 | #include "xfs_btree.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 16 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 18 | #include "xfs_bmap_util.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 19 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_error.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 21 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_trans_space.h" |
Christoph Hellwig | a39e596 | 2017-11-01 16:36:47 +0100 | [diff] [blame] | 23 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_iomap.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 25 | #include "xfs_trace.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 26 | #include "xfs_quota.h" |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 27 | #include "xfs_dquot_item.h" |
| 28 | #include "xfs_dquot.h" |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 29 | #include "xfs_reflink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 31 | #define XFS_ALLOC_ALIGN(mp, off) \ |
| 32 | (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 34 | static int |
| 35 | xfs_alert_fsblock_zero( |
| 36 | xfs_inode_t *ip, |
| 37 | xfs_bmbt_irec_t *imap) |
| 38 | { |
| 39 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
| 40 | "Access to block zero in inode %llu " |
| 41 | "start_block: %llx start_off: %llx " |
| 42 | "blkcnt: %llx extent-state: %x", |
| 43 | (unsigned long long)ip->i_ino, |
| 44 | (unsigned long long)imap->br_startblock, |
| 45 | (unsigned long long)imap->br_startoff, |
| 46 | (unsigned long long)imap->br_blockcount, |
| 47 | imap->br_state); |
| 48 | return -EFSCORRUPTED; |
| 49 | } |
| 50 | |
| 51 | int |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 52 | xfs_bmbt_to_iomap( |
| 53 | struct xfs_inode *ip, |
| 54 | struct iomap *iomap, |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 55 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 56 | unsigned int mapping_flags, |
| 57 | u16 iomap_flags) |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 58 | { |
| 59 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 60 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 61 | |
Christoph Hellwig | eb77b23 | 2019-09-03 08:13:13 -0700 | [diff] [blame] | 62 | if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 63 | return xfs_alert_fsblock_zero(ip, imap); |
| 64 | |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 65 | if (imap->br_startblock == HOLESTARTBLOCK) { |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 66 | iomap->addr = IOMAP_NULL_ADDR; |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 67 | iomap->type = IOMAP_HOLE; |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 68 | } else if (imap->br_startblock == DELAYSTARTBLOCK || |
| 69 | isnullstartblock(imap->br_startblock)) { |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 70 | iomap->addr = IOMAP_NULL_ADDR; |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 71 | iomap->type = IOMAP_DELALLOC; |
| 72 | } else { |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 73 | iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock)); |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 74 | if (imap->br_state == XFS_EXT_UNWRITTEN) |
| 75 | iomap->type = IOMAP_UNWRITTEN; |
| 76 | else |
| 77 | iomap->type = IOMAP_MAPPED; |
| 78 | } |
| 79 | iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff); |
| 80 | iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount); |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 81 | iomap->bdev = target->bt_bdev; |
| 82 | iomap->dax_dev = target->bt_daxdev; |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 83 | iomap->flags = iomap_flags; |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 84 | |
| 85 | if (xfs_ipincount(ip) && |
| 86 | (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP)) |
| 87 | iomap->flags |= IOMAP_F_DIRTY; |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 88 | return 0; |
Christoph Hellwig | e9c4973 | 2016-09-19 11:09:12 +1000 | [diff] [blame] | 89 | } |
| 90 | |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 91 | static void |
| 92 | xfs_hole_to_iomap( |
| 93 | struct xfs_inode *ip, |
| 94 | struct iomap *iomap, |
| 95 | xfs_fileoff_t offset_fsb, |
| 96 | xfs_fileoff_t end_fsb) |
| 97 | { |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 98 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); |
| 99 | |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 100 | iomap->addr = IOMAP_NULL_ADDR; |
| 101 | iomap->type = IOMAP_HOLE; |
| 102 | iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb); |
| 103 | iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb); |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 104 | iomap->bdev = target->bt_bdev; |
| 105 | iomap->dax_dev = target->bt_daxdev; |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 106 | } |
| 107 | |
Christoph Hellwig | 4356822 | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 108 | static inline xfs_fileoff_t |
| 109 | xfs_iomap_end_fsb( |
| 110 | struct xfs_mount *mp, |
| 111 | loff_t offset, |
| 112 | loff_t count) |
| 113 | { |
| 114 | ASSERT(offset <= mp->m_super->s_maxbytes); |
| 115 | return min(XFS_B_TO_FSB(mp, offset + count), |
| 116 | XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes)); |
| 117 | } |
| 118 | |
Christoph Hellwig | 49bbf8c | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 119 | static xfs_extlen_t |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 120 | xfs_eof_alignment( |
Christoph Hellwig | 57c4944 | 2019-10-30 12:24:58 -0700 | [diff] [blame] | 121 | struct xfs_inode *ip) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 122 | { |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 123 | struct xfs_mount *mp = ip->i_mount; |
| 124 | xfs_extlen_t align = 0; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 125 | |
Christoph Hellwig | bf322d9 | 2011-12-18 20:00:05 +0000 | [diff] [blame] | 126 | if (!XFS_IS_REALTIME_INODE(ip)) { |
| 127 | /* |
| 128 | * Round up the allocation request to a stripe unit |
| 129 | * (m_dalign) boundary if the file size is >= stripe unit |
| 130 | * size, and we are allocating past the allocation eof. |
| 131 | * |
| 132 | * If mounted with the "-o swalloc" option the alignment is |
| 133 | * increased from the strip unit size to the stripe width. |
| 134 | */ |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 135 | if (mp->m_swidth && xfs_has_swalloc(mp)) |
Christoph Hellwig | bf322d9 | 2011-12-18 20:00:05 +0000 | [diff] [blame] | 136 | align = mp->m_swidth; |
| 137 | else if (mp->m_dalign) |
| 138 | align = mp->m_dalign; |
| 139 | |
Peter Watkins | 76b5730 | 2014-12-04 09:30:51 +1100 | [diff] [blame] | 140 | if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align)) |
| 141 | align = 0; |
Christoph Hellwig | bf322d9 | 2011-12-18 20:00:05 +0000 | [diff] [blame] | 142 | } |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 143 | |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 144 | return align; |
| 145 | } |
| 146 | |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Check if last_fsb is outside the last extent, and if so grow it to the next |
| 149 | * stripe unit boundary. |
| 150 | */ |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 151 | xfs_fileoff_t |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 152 | xfs_iomap_eof_align_last_fsb( |
| 153 | struct xfs_inode *ip, |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 154 | xfs_fileoff_t end_fsb) |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 155 | { |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 156 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 157 | xfs_extlen_t extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | 57c4944 | 2019-10-30 12:24:58 -0700 | [diff] [blame] | 158 | xfs_extlen_t align = xfs_eof_alignment(ip); |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 159 | struct xfs_bmbt_irec irec; |
| 160 | struct xfs_iext_cursor icur; |
| 161 | |
Christoph Hellwig | b2197a3 | 2021-04-13 11:15:12 -0700 | [diff] [blame] | 162 | ASSERT(!xfs_need_iread_extents(ifp)); |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 163 | |
Christoph Hellwig | 57c4944 | 2019-10-30 12:24:58 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Always round up the allocation request to the extent hint boundary. |
| 166 | */ |
| 167 | if (extsz) { |
| 168 | if (align) |
| 169 | align = roundup_64(align, extsz); |
| 170 | else |
| 171 | align = extsz; |
| 172 | } |
| 173 | |
Peter Watkins | 76b5730 | 2014-12-04 09:30:51 +1100 | [diff] [blame] | 174 | if (align) { |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 175 | xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align); |
Christoph Hellwig | f8e3a82 | 2016-09-19 11:09:28 +1000 | [diff] [blame] | 176 | |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 177 | xfs_iext_last(ifp, &icur); |
| 178 | if (!xfs_iext_get_extent(ifp, &icur, &irec) || |
| 179 | aligned_end_fsb >= irec.br_startoff + irec.br_blockcount) |
| 180 | return aligned_end_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 181 | } |
Christoph Hellwig | ae7e403 | 2019-10-30 12:24:57 -0700 | [diff] [blame] | 182 | |
| 183 | return end_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 184 | } |
| 185 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 186 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | xfs_iomap_write_direct( |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 188 | struct xfs_inode *ip, |
| 189 | xfs_fileoff_t offset_fsb, |
| 190 | xfs_fileoff_t count_fsb, |
Christoph Hellwig | 952da06 | 2021-11-29 11:21:58 +0100 | [diff] [blame^] | 191 | unsigned int flags, |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 192 | struct xfs_bmbt_irec *imap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 194 | struct xfs_mount *mp = ip->i_mount; |
| 195 | struct xfs_trans *tp; |
| 196 | xfs_filblks_t resaligned; |
| 197 | int nimaps; |
Darrick J. Wong | 02b7ee4 | 2021-01-26 17:20:42 -0800 | [diff] [blame] | 198 | unsigned int dblocks, rblocks; |
Darrick J. Wong | 3de4eb1 | 2021-01-26 16:44:07 -0800 | [diff] [blame] | 199 | bool force = false; |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 200 | int error; |
| 201 | int bmapi_flags = XFS_BMAPI_PREALLOC; |
Chandan Babu R | 5147ef3 | 2021-03-25 11:48:18 -0700 | [diff] [blame] | 202 | int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 204 | ASSERT(count_fsb > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 205 | |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 206 | resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, |
| 207 | xfs_get_extsz_hint(ip)); |
| 208 | if (unlikely(XFS_IS_REALTIME_INODE(ip))) { |
Darrick J. Wong | 02b7ee4 | 2021-01-26 17:20:42 -0800 | [diff] [blame] | 209 | dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 210 | rblocks = resaligned; |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 211 | } else { |
Darrick J. Wong | 02b7ee4 | 2021-01-26 17:20:42 -0800 | [diff] [blame] | 212 | dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); |
| 213 | rblocks = 0; |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 214 | } |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 215 | |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 216 | error = xfs_qm_dqattach(ip); |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 217 | if (error) |
| 218 | return error; |
| 219 | |
| 220 | /* |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 221 | * For DAX, we do not allocate unwritten extents, but instead we zero |
| 222 | * the block before we commit the transaction. Ideally we'd like to do |
| 223 | * this outside the transaction context, but if we commit and then crash |
| 224 | * we may not have zeroed the blocks and this will be exposed on |
| 225 | * recovery of the allocation. Hence we must zero before commit. |
Dave Chinner | 3b0fe47 | 2016-01-04 16:22:45 +1100 | [diff] [blame] | 226 | * |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 227 | * Further, if we are mapping unwritten extents here, we need to zero |
| 228 | * and convert them to written so that we don't need an unwritten extent |
| 229 | * 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] | 230 | * the reserve block pool for bmbt block allocation if there is no space |
| 231 | * left but we need to do unwritten extent conversion. |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 232 | */ |
Christoph Hellwig | 952da06 | 2021-11-29 11:21:58 +0100 | [diff] [blame^] | 233 | if (flags & IOMAP_DAX) { |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 234 | bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO; |
Christoph Hellwig | 63fbb4c | 2017-03-28 14:53:36 -0700 | [diff] [blame] | 235 | if (imap->br_state == XFS_EXT_UNWRITTEN) { |
Darrick J. Wong | 02b7ee4 | 2021-01-26 17:20:42 -0800 | [diff] [blame] | 236 | force = true; |
Chandan Babu R | 5147ef3 | 2021-03-25 11:48:18 -0700 | [diff] [blame] | 237 | nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT; |
Darrick J. Wong | 02b7ee4 | 2021-01-26 17:20:42 -0800 | [diff] [blame] | 238 | dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; |
Dave Chinner | 3b0fe47 | 2016-01-04 16:22:45 +1100 | [diff] [blame] | 239 | } |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 240 | } |
Darrick J. Wong | 3de4eb1 | 2021-01-26 16:44:07 -0800 | [diff] [blame] | 241 | |
| 242 | error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks, |
| 243 | rblocks, force, &tp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 244 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 245 | return error; |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 246 | |
Chandan Babu R | 5147ef3 | 2021-03-25 11:48:18 -0700 | [diff] [blame] | 247 | error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | /* |
Christoph Hellwig | 3070451 | 2010-06-24 11:42:19 +1000 | [diff] [blame] | 252 | * From this point onwards we overwrite the imap pointer that the |
| 253 | * caller gave to us. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 255 | nimaps = 1; |
Brian Foster | da781e6 | 2019-10-21 09:26:48 -0700 | [diff] [blame] | 256 | error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0, |
| 257 | imap, &nimaps); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 258 | if (error) |
Darrick J. Wong | 35b11010 | 2021-01-26 17:23:30 -0800 | [diff] [blame] | 259 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 262 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | */ |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 264 | error = xfs_trans_commit(tp); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 265 | if (error) |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 266 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 268 | /* |
| 269 | * Copy any maps to caller's array and return any error. |
| 270 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (nimaps == 0) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 272 | error = -ENOSPC; |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 273 | goto out_unlock; |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 274 | } |
| 275 | |
Christoph Hellwig | eb77b23 | 2019-09-03 08:13:13 -0700 | [diff] [blame] | 276 | if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) |
Dave Chinner | 6d4a8ec | 2011-03-07 10:06:35 +1100 | [diff] [blame] | 277 | error = xfs_alert_fsblock_zero(ip, imap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 279 | out_unlock: |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 280 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 281 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 283 | out_trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 284 | xfs_trans_cancel(tp); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 285 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 288 | STATIC bool |
| 289 | xfs_quota_need_throttle( |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 290 | struct xfs_inode *ip, |
| 291 | xfs_dqtype_t type, |
| 292 | xfs_fsblock_t alloc_blocks) |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 293 | { |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 294 | struct xfs_dquot *dq = xfs_inode_dquot(ip, type); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 295 | |
| 296 | if (!dq || !xfs_this_quota_on(ip->i_mount, type)) |
| 297 | return false; |
| 298 | |
| 299 | /* no hi watermark, no throttle */ |
| 300 | if (!dq->q_prealloc_hi_wmark) |
| 301 | return false; |
| 302 | |
| 303 | /* under the lo watermark, no throttle */ |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 304 | if (dq->q_blk.reserved + alloc_blocks < dq->q_prealloc_lo_wmark) |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 305 | return false; |
| 306 | |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | STATIC void |
| 311 | xfs_quota_calc_throttle( |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 312 | struct xfs_inode *ip, |
| 313 | xfs_dqtype_t type, |
| 314 | xfs_fsblock_t *qblocks, |
| 315 | int *qshift, |
| 316 | int64_t *qfreesp) |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 317 | { |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 318 | struct xfs_dquot *dq = xfs_inode_dquot(ip, type); |
| 319 | int64_t freesp; |
| 320 | int shift = 0; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 321 | |
Eric Sandeen | 5cca3f6 | 2014-10-02 09:27:09 +1000 | [diff] [blame] | 322 | /* no dq, or over hi wmark, squash the prealloc completely */ |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 323 | if (!dq || dq->q_blk.reserved >= dq->q_prealloc_hi_wmark) { |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 324 | *qblocks = 0; |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 325 | *qfreesp = 0; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 329 | freesp = dq->q_prealloc_hi_wmark - dq->q_blk.reserved; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 330 | if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) { |
| 331 | shift = 2; |
| 332 | if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT]) |
| 333 | shift += 2; |
| 334 | if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT]) |
| 335 | shift += 2; |
| 336 | } |
| 337 | |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 338 | if (freesp < *qfreesp) |
| 339 | *qfreesp = freesp; |
| 340 | |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 341 | /* only overwrite the throttle values if we are more aggressive */ |
| 342 | if ((freesp >> shift) < (*qblocks >> *qshift)) { |
| 343 | *qblocks = freesp; |
| 344 | *qshift = shift; |
| 345 | } |
| 346 | } |
| 347 | |
Dave Chinner | a1e16c2 | 2013-02-11 16:05:01 +1100 | [diff] [blame] | 348 | /* |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 349 | * If we don't have a user specified preallocation size, dynamically increase |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 350 | * 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] | 351 | * at a single extent or less if the filesystem is near full. The closer the |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 352 | * filesystem is to being full, the smaller the maximum preallocation. |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 353 | */ |
| 354 | STATIC xfs_fsblock_t |
| 355 | xfs_iomap_prealloc_size( |
Dave Chinner | a1e16c2 | 2013-02-11 16:05:01 +1100 | [diff] [blame] | 356 | struct xfs_inode *ip, |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 357 | int whichfork, |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 358 | loff_t offset, |
| 359 | loff_t count, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 360 | struct xfs_iext_cursor *icur) |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 361 | { |
Darrick J. Wong | f0322c7 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 362 | struct xfs_iext_cursor ncur = *icur; |
| 363 | struct xfs_bmbt_irec prev, got; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 364 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 365 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 366 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 367 | int64_t freesp; |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 368 | xfs_fsblock_t qblocks; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 369 | xfs_fsblock_t alloc_blocks = 0; |
Darrick J. Wong | f0322c7 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 370 | xfs_extlen_t plen; |
| 371 | int shift = 0; |
| 372 | int qshift = 0; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 373 | |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 374 | /* |
| 375 | * As an exception we don't do any preallocation at all if the file is |
| 376 | * smaller than the minimum preallocation and we are using the default |
| 377 | * dynamic preallocation scheme, as it is likely this is the only write |
| 378 | * to the file that is going to be done. |
| 379 | */ |
| 380 | if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks)) |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 381 | return 0; |
| 382 | |
| 383 | /* |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 384 | * Use the minimum preallocation size for small files or if we are |
| 385 | * writing right after a hole. |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 386 | */ |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 387 | if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) || |
Darrick J. Wong | f0322c7 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 388 | !xfs_iext_prev_extent(ifp, &ncur, &prev) || |
Christoph Hellwig | 656152e | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 389 | prev.br_startoff + prev.br_blockcount < offset_fsb) |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 390 | return mp->m_allocsize_blocks; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 391 | |
| 392 | /* |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 393 | * Take the size of the preceding data extents as the basis for the |
| 394 | * preallocation size. Note that we don't care if the previous extents |
| 395 | * are written or not. |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 396 | */ |
Darrick J. Wong | f0322c7 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 397 | plen = prev.br_blockcount; |
| 398 | while (xfs_iext_prev_extent(ifp, &ncur, &got)) { |
| 399 | if (plen > MAXEXTLEN / 2 || |
| 400 | isnullstartblock(got.br_startblock) || |
| 401 | got.br_startoff + got.br_blockcount != prev.br_startoff || |
| 402 | got.br_startblock + got.br_blockcount != prev.br_startblock) |
| 403 | break; |
| 404 | plen += got.br_blockcount; |
| 405 | prev = got; |
| 406 | } |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * If the size of the extents is greater than half the maximum extent |
| 410 | * length, then use the current offset as the basis. This ensures that |
| 411 | * for large files the preallocation size always extends to MAXEXTLEN |
| 412 | * rather than falling short due to things like stripe unit/width |
| 413 | * alignment of real extents. |
| 414 | */ |
Darrick J. Wong | f0322c7 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 415 | alloc_blocks = plen * 2; |
| 416 | if (alloc_blocks > MAXEXTLEN) |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 417 | alloc_blocks = XFS_B_TO_FSB(mp, offset); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 418 | qblocks = alloc_blocks; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 419 | |
Brian Foster | c9bdbdc | 2013-03-18 10:51:44 -0400 | [diff] [blame] | 420 | /* |
| 421 | * MAXEXTLEN is not a power of two value but we round the prealloc down |
| 422 | * to the nearest power of two value after throttling. To prevent the |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 423 | * round down from unconditionally reducing the maximum supported |
| 424 | * prealloc size, we round up first, apply appropriate throttling, |
| 425 | * round down and cap the value to MAXEXTLEN. |
Brian Foster | c9bdbdc | 2013-03-18 10:51:44 -0400 | [diff] [blame] | 426 | */ |
| 427 | alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN), |
| 428 | alloc_blocks); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 429 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 430 | freesp = percpu_counter_read_positive(&mp->m_fdblocks); |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 431 | if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) { |
| 432 | shift = 2; |
| 433 | if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT]) |
| 434 | shift++; |
| 435 | if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT]) |
| 436 | shift++; |
| 437 | if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT]) |
| 438 | shift++; |
| 439 | if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT]) |
| 440 | shift++; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 441 | } |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 442 | |
| 443 | /* |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 444 | * Check each quota to cap the prealloc size, provide a shift value to |
| 445 | * throttle with and adjust amount of available space. |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 446 | */ |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 447 | if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks)) |
| 448 | xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift, |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 449 | &freesp); |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 450 | if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks)) |
| 451 | xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift, |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 452 | &freesp); |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 453 | if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks)) |
| 454 | xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift, |
Brian Foster | f074051 | 2014-07-24 19:56:08 +1000 | [diff] [blame] | 455 | &freesp); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * The final prealloc size is set to the minimum of free space available |
| 459 | * in each of the quotas and the overall filesystem. |
| 460 | * |
| 461 | * The shift throttle value is set to the maximum value as determined by |
| 462 | * the global low free space values and per-quota low free space values. |
| 463 | */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 464 | alloc_blocks = min(alloc_blocks, qblocks); |
| 465 | shift = max(shift, qshift); |
Brian Foster | 76a4202 | 2013-03-18 10:51:47 -0400 | [diff] [blame] | 466 | |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 467 | if (shift) |
| 468 | alloc_blocks >>= shift; |
Brian Foster | c9bdbdc | 2013-03-18 10:51:44 -0400 | [diff] [blame] | 469 | /* |
| 470 | * rounddown_pow_of_two() returns an undefined result if we pass in |
| 471 | * alloc_blocks = 0. |
| 472 | */ |
| 473 | if (alloc_blocks) |
| 474 | alloc_blocks = rounddown_pow_of_two(alloc_blocks); |
| 475 | if (alloc_blocks > MAXEXTLEN) |
| 476 | alloc_blocks = MAXEXTLEN; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 477 | |
Brian Foster | 3c58b5f | 2013-03-18 10:51:43 -0400 | [diff] [blame] | 478 | /* |
| 479 | * If we are still trying to allocate more space than is |
| 480 | * available, squash the prealloc hard. This can happen if we |
| 481 | * have a large file on a small filesystem and the above |
| 482 | * lowspace thresholds are smaller than MAXEXTLEN. |
| 483 | */ |
| 484 | while (alloc_blocks && alloc_blocks >= freesp) |
| 485 | alloc_blocks >>= 4; |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 486 | if (alloc_blocks < mp->m_allocsize_blocks) |
| 487 | alloc_blocks = mp->m_allocsize_blocks; |
Brian Foster | 19cb7e3 | 2013-03-18 10:51:48 -0400 | [diff] [blame] | 488 | trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift, |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 489 | mp->m_allocsize_blocks); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 490 | return alloc_blocks; |
| 491 | } |
| 492 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 493 | int |
| 494 | xfs_iomap_write_unwritten( |
| 495 | xfs_inode_t *ip, |
| 496 | xfs_off_t offset, |
| 497 | xfs_off_t count, |
| 498 | bool update_isize) |
| 499 | { |
| 500 | xfs_mount_t *mp = ip->i_mount; |
| 501 | xfs_fileoff_t offset_fsb; |
| 502 | xfs_filblks_t count_fsb; |
| 503 | xfs_filblks_t numblks_fsb; |
| 504 | int nimaps; |
| 505 | xfs_trans_t *tp; |
| 506 | xfs_bmbt_irec_t imap; |
| 507 | struct inode *inode = VFS_I(ip); |
| 508 | xfs_fsize_t i_size; |
| 509 | uint resblks; |
| 510 | int error; |
| 511 | |
| 512 | trace_xfs_unwritten_convert(ip, offset, count); |
| 513 | |
| 514 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 515 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 516 | count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); |
| 517 | |
| 518 | /* |
| 519 | * Reserve enough blocks in this transaction for two complete extent |
| 520 | * btree splits. We may be converting the middle part of an unwritten |
| 521 | * extent and in this case we will insert two new extents in the btree |
| 522 | * each of which could cause a full split. |
| 523 | * |
| 524 | * This reservation amount will be used in the first call to |
| 525 | * xfs_bmbt_split() to select an AG with enough space to satisfy the |
| 526 | * rest of the operation. |
| 527 | */ |
| 528 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; |
| 529 | |
Darrick J. Wong | 2815a16 | 2019-11-08 23:04:20 -0800 | [diff] [blame] | 530 | /* Attach dquots so that bmbt splits are accounted correctly. */ |
| 531 | error = xfs_qm_dqattach(ip); |
| 532 | if (error) |
| 533 | return error; |
| 534 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 535 | do { |
| 536 | /* |
| 537 | * Set up a transaction to convert the range of extents |
| 538 | * from unwritten to real. Do allocations in a loop until |
| 539 | * we have covered the range passed in. |
| 540 | * |
| 541 | * Note that we can't risk to recursing back into the filesystem |
| 542 | * here as we might be asked to write out the same inode that we |
| 543 | * complete here and might deadlock on the iolock. |
| 544 | */ |
Darrick J. Wong | 3a1af6c | 2021-01-26 16:33:29 -0800 | [diff] [blame] | 545 | error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, |
Darrick J. Wong | 3de4eb1 | 2021-01-26 16:44:07 -0800 | [diff] [blame] | 546 | 0, true, &tp); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 547 | if (error) |
| 548 | return error; |
| 549 | |
Chandan Babu R | c442f30 | 2021-01-22 16:48:14 -0800 | [diff] [blame] | 550 | error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, |
| 551 | XFS_IEXT_WRITE_UNWRITTEN_CNT); |
Darrick J. Wong | 2815a16 | 2019-11-08 23:04:20 -0800 | [diff] [blame] | 552 | if (error) |
| 553 | goto error_on_bmapi_transaction; |
| 554 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 555 | /* |
| 556 | * Modify the unwritten extent state of the buffer. |
| 557 | */ |
| 558 | nimaps = 1; |
| 559 | error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, |
| 560 | XFS_BMAPI_CONVERT, resblks, &imap, |
| 561 | &nimaps); |
| 562 | if (error) |
| 563 | goto error_on_bmapi_transaction; |
| 564 | |
| 565 | /* |
| 566 | * Log the updated inode size as we go. We have to be careful |
| 567 | * to only log it up to the actual write offset if it is |
| 568 | * halfway into a block. |
| 569 | */ |
| 570 | i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb); |
| 571 | if (i_size > offset + count) |
| 572 | i_size = offset + count; |
| 573 | if (update_isize && i_size > i_size_read(inode)) |
| 574 | i_size_write(inode, i_size); |
| 575 | i_size = xfs_new_eof(ip, i_size); |
| 576 | if (i_size) { |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 577 | ip->i_disk_size = i_size; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 578 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 579 | } |
| 580 | |
| 581 | error = xfs_trans_commit(tp); |
| 582 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 583 | if (error) |
| 584 | return error; |
| 585 | |
| 586 | if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock))) |
| 587 | return xfs_alert_fsblock_zero(ip, &imap); |
| 588 | |
| 589 | if ((numblks_fsb = imap.br_blockcount) == 0) { |
| 590 | /* |
| 591 | * The numblks_fsb value should always get |
| 592 | * smaller, otherwise the loop is stuck. |
| 593 | */ |
| 594 | ASSERT(imap.br_blockcount); |
| 595 | break; |
| 596 | } |
| 597 | offset_fsb += numblks_fsb; |
| 598 | count_fsb -= numblks_fsb; |
| 599 | } while (count_fsb > 0); |
| 600 | |
| 601 | return 0; |
| 602 | |
| 603 | error_on_bmapi_transaction: |
| 604 | xfs_trans_cancel(tp); |
| 605 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 606 | return error; |
| 607 | } |
| 608 | |
| 609 | static inline bool |
| 610 | imap_needs_alloc( |
| 611 | struct inode *inode, |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 612 | unsigned flags, |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 613 | struct xfs_bmbt_irec *imap, |
| 614 | int nimaps) |
| 615 | { |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 616 | /* don't allocate blocks when just zeroing */ |
| 617 | if (flags & IOMAP_ZERO) |
| 618 | return false; |
| 619 | if (!nimaps || |
| 620 | imap->br_startblock == HOLESTARTBLOCK || |
| 621 | imap->br_startblock == DELAYSTARTBLOCK) |
| 622 | return true; |
| 623 | /* we convert unwritten extents before copying the data for DAX */ |
Christoph Hellwig | 952da06 | 2021-11-29 11:21:58 +0100 | [diff] [blame^] | 624 | if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 625 | return true; |
| 626 | return false; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static inline bool |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 630 | imap_needs_cow( |
| 631 | struct xfs_inode *ip, |
| 632 | unsigned int flags, |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 633 | struct xfs_bmbt_irec *imap, |
| 634 | int nimaps) |
| 635 | { |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 636 | if (!xfs_is_cow_inode(ip)) |
| 637 | return false; |
| 638 | |
| 639 | /* when zeroing we don't have to COW holes or unwritten extents */ |
| 640 | if (flags & IOMAP_ZERO) { |
| 641 | if (!nimaps || |
| 642 | imap->br_startblock == HOLESTARTBLOCK || |
| 643 | imap->br_state == XFS_EXT_UNWRITTEN) |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | return true; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static int |
| 651 | xfs_ilock_for_iomap( |
| 652 | struct xfs_inode *ip, |
| 653 | unsigned flags, |
| 654 | unsigned *lockmode) |
| 655 | { |
| 656 | unsigned mode = XFS_ILOCK_SHARED; |
| 657 | bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO); |
| 658 | |
| 659 | /* |
| 660 | * COW writes may allocate delalloc space or convert unwritten COW |
| 661 | * extents, so we need to make sure to take the lock exclusively here. |
| 662 | */ |
Christoph Hellwig | 1e190f8 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 663 | if (xfs_is_cow_inode(ip) && is_write) |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 664 | mode = XFS_ILOCK_EXCL; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 665 | |
| 666 | /* |
| 667 | * Extents not yet cached requires exclusive access, don't block. This |
| 668 | * is an opencoded xfs_ilock_data_map_shared() call but with |
| 669 | * non-blocking behaviour. |
| 670 | */ |
Christoph Hellwig | b2197a3 | 2021-04-13 11:15:12 -0700 | [diff] [blame] | 671 | if (xfs_need_iread_extents(&ip->i_df)) { |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 672 | if (flags & IOMAP_NOWAIT) |
| 673 | return -EAGAIN; |
| 674 | mode = XFS_ILOCK_EXCL; |
| 675 | } |
| 676 | |
| 677 | relock: |
| 678 | if (flags & IOMAP_NOWAIT) { |
| 679 | if (!xfs_ilock_nowait(ip, mode)) |
| 680 | return -EAGAIN; |
| 681 | } else { |
| 682 | xfs_ilock(ip, mode); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * The reflink iflag could have changed since the earlier unlocked |
| 687 | * check, so if we got ILOCK_SHARED for a write and but we're now a |
| 688 | * reflink inode we have to switch to ILOCK_EXCL and relock. |
| 689 | */ |
| 690 | if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) { |
| 691 | xfs_iunlock(ip, mode); |
| 692 | mode = XFS_ILOCK_EXCL; |
| 693 | goto relock; |
| 694 | } |
| 695 | |
| 696 | *lockmode = mode; |
| 697 | return 0; |
| 698 | } |
| 699 | |
Dave Chinner | 883a790 | 2020-11-19 08:59:11 -0800 | [diff] [blame] | 700 | /* |
| 701 | * Check that the imap we are going to return to the caller spans the entire |
| 702 | * range that the caller requested for the IO. |
| 703 | */ |
| 704 | static bool |
| 705 | imap_spans_range( |
| 706 | struct xfs_bmbt_irec *imap, |
| 707 | xfs_fileoff_t offset_fsb, |
| 708 | xfs_fileoff_t end_fsb) |
| 709 | { |
| 710 | if (imap->br_startoff > offset_fsb) |
| 711 | return false; |
| 712 | if (imap->br_startoff + imap->br_blockcount < end_fsb) |
| 713 | return false; |
| 714 | return true; |
| 715 | } |
| 716 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 717 | static int |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 718 | xfs_direct_write_iomap_begin( |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 719 | struct inode *inode, |
| 720 | loff_t offset, |
| 721 | loff_t length, |
| 722 | unsigned flags, |
| 723 | struct iomap *iomap, |
| 724 | struct iomap *srcmap) |
| 725 | { |
| 726 | struct xfs_inode *ip = XFS_I(inode); |
| 727 | struct xfs_mount *mp = ip->i_mount; |
| 728 | struct xfs_bmbt_irec imap, cmap; |
| 729 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 730 | xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length); |
| 731 | int nimaps = 1, error = 0; |
| 732 | bool shared = false; |
| 733 | u16 iomap_flags = 0; |
| 734 | unsigned lockmode; |
| 735 | |
| 736 | ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO)); |
| 737 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 738 | if (xfs_is_shutdown(mp)) |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 739 | return -EIO; |
| 740 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 741 | /* |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 742 | * Writes that span EOF might trigger an IO size update on completion, |
| 743 | * so consider them to be dirty for the purposes of O_DSYNC even if |
| 744 | * there is no other metadata changes pending or have been made here. |
| 745 | */ |
| 746 | if (offset + length > i_size_read(inode)) |
| 747 | iomap_flags |= IOMAP_F_DIRTY; |
| 748 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 749 | error = xfs_ilock_for_iomap(ip, flags, &lockmode); |
| 750 | if (error) |
| 751 | return error; |
| 752 | |
| 753 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, |
| 754 | &nimaps, 0); |
| 755 | if (error) |
| 756 | goto out_unlock; |
| 757 | |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 758 | if (imap_needs_cow(ip, flags, &imap, nimaps)) { |
Christoph Hellwig | 1e190f8 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 759 | error = -EAGAIN; |
| 760 | if (flags & IOMAP_NOWAIT) |
| 761 | goto out_unlock; |
| 762 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 763 | /* may drop and re-acquire the ilock */ |
| 764 | error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared, |
| 765 | &lockmode, flags & IOMAP_DIRECT); |
| 766 | if (error) |
| 767 | goto out_unlock; |
| 768 | if (shared) |
| 769 | goto out_found_cow; |
| 770 | end_fsb = imap.br_startoff + imap.br_blockcount; |
| 771 | length = XFS_FSB_TO_B(mp, end_fsb) - offset; |
| 772 | } |
| 773 | |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 774 | if (imap_needs_alloc(inode, flags, &imap, nimaps)) |
| 775 | goto allocate_blocks; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 776 | |
Dave Chinner | 883a790 | 2020-11-19 08:59:11 -0800 | [diff] [blame] | 777 | /* |
Dave Chinner | ed1128c | 2021-01-23 10:06:31 -0800 | [diff] [blame] | 778 | * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with |
| 779 | * a single map so that we avoid partial IO failures due to the rest of |
| 780 | * the I/O range not covered by this map triggering an EAGAIN condition |
| 781 | * when it is subsequently mapped and aborting the I/O. |
Dave Chinner | 883a790 | 2020-11-19 08:59:11 -0800 | [diff] [blame] | 782 | */ |
Dave Chinner | ed1128c | 2021-01-23 10:06:31 -0800 | [diff] [blame] | 783 | if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) { |
Dave Chinner | 883a790 | 2020-11-19 08:59:11 -0800 | [diff] [blame] | 784 | error = -EAGAIN; |
Dave Chinner | ed1128c | 2021-01-23 10:06:31 -0800 | [diff] [blame] | 785 | if (!imap_spans_range(&imap, offset_fsb, end_fsb)) |
| 786 | goto out_unlock; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * For overwrite only I/O, we cannot convert unwritten extents without |
| 791 | * requiring sub-block zeroing. This can only be done under an |
| 792 | * exclusive IOLOCK, hence return -EAGAIN if this is not a written |
| 793 | * extent to tell the caller to try again. |
| 794 | */ |
| 795 | if (flags & IOMAP_OVERWRITE_ONLY) { |
| 796 | error = -EAGAIN; |
| 797 | if (imap.br_state != XFS_EXT_NORM && |
| 798 | ((offset | length) & mp->m_blockmask)) |
| 799 | goto out_unlock; |
Dave Chinner | 883a790 | 2020-11-19 08:59:11 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 802 | xfs_iunlock(ip, lockmode); |
| 803 | trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 804 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 805 | |
Christoph Hellwig | 5c5b6f7 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 806 | allocate_blocks: |
| 807 | error = -EAGAIN; |
Dave Chinner | ed1128c | 2021-01-23 10:06:31 -0800 | [diff] [blame] | 808 | if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 809 | goto out_unlock; |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 810 | |
| 811 | /* |
| 812 | * We cap the maximum length we map to a sane size to keep the chunks |
| 813 | * of work done where somewhat symmetric with the work writeback does. |
| 814 | * This is a completely arbitrary number pulled out of thin air as a |
| 815 | * best guess for initial testing. |
| 816 | * |
| 817 | * Note that the values needs to be less than 32-bits wide until the |
| 818 | * lower level functions are updated. |
| 819 | */ |
| 820 | length = min_t(loff_t, length, 1024 * PAGE_SIZE); |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 821 | end_fsb = xfs_iomap_end_fsb(mp, offset, length); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 822 | |
Christoph Hellwig | e696663 | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 823 | if (offset + length > XFS_ISIZE(ip)) |
| 824 | end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb); |
| 825 | else if (nimaps && imap.br_startblock == HOLESTARTBLOCK) |
| 826 | end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount); |
| 827 | xfs_iunlock(ip, lockmode); |
| 828 | |
| 829 | error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb, |
Christoph Hellwig | 952da06 | 2021-11-29 11:21:58 +0100 | [diff] [blame^] | 830 | flags, &imap); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 831 | if (error) |
| 832 | return error; |
| 833 | |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 834 | trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 835 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, |
| 836 | iomap_flags | IOMAP_F_NEW); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 837 | |
| 838 | out_found_cow: |
| 839 | xfs_iunlock(ip, lockmode); |
| 840 | length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount); |
| 841 | trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap); |
| 842 | if (imap.br_startblock != HOLESTARTBLOCK) { |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 843 | error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 844 | if (error) |
| 845 | return error; |
| 846 | } |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 847 | return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 848 | |
| 849 | out_unlock: |
Darrick J. Wong | f273387 | 2021-01-27 10:07:27 -0800 | [diff] [blame] | 850 | if (lockmode) |
| 851 | xfs_iunlock(ip, lockmode); |
Christoph Hellwig | a526c85 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 852 | return error; |
| 853 | } |
| 854 | |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 855 | const struct iomap_ops xfs_direct_write_iomap_ops = { |
| 856 | .iomap_begin = xfs_direct_write_iomap_begin, |
| 857 | }; |
| 858 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 859 | static int |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 860 | xfs_buffered_write_iomap_begin( |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 861 | struct inode *inode, |
| 862 | loff_t offset, |
| 863 | loff_t count, |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 864 | unsigned flags, |
Christoph Hellwig | 36adcba | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 865 | struct iomap *iomap, |
| 866 | struct iomap *srcmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 868 | struct xfs_inode *ip = XFS_I(inode); |
| 869 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 870 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Christoph Hellwig | 4356822 | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 871 | xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count); |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 872 | struct xfs_bmbt_irec imap, cmap; |
| 873 | struct xfs_iext_cursor icur, ccur; |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 874 | xfs_fsblock_t prealloc_blocks = 0; |
Christoph Hellwig | c4feb0b | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 875 | bool eof = false, cow_eof = false, shared = false; |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 876 | int allocfork = XFS_DATA_FORK; |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 877 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 879 | if (xfs_is_shutdown(mp)) |
Brian Foster | e482669 | 2021-02-10 17:27:20 -0800 | [diff] [blame] | 880 | return -EIO; |
| 881 | |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 882 | /* we can't use delayed allocations when using extent size hints */ |
| 883 | if (xfs_get_extsz_hint(ip)) |
| 884 | return xfs_direct_write_iomap_begin(inode, offset, count, |
| 885 | flags, iomap, srcmap); |
| 886 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 887 | ASSERT(!XFS_IS_REALTIME_INODE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 889 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 890 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 891 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 892 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 893 | error = -EFSCORRUPTED; |
| 894 | goto out_unlock; |
| 895 | } |
| 896 | |
| 897 | XFS_STATS_INC(mp, xs_blk_mapw); |
| 898 | |
Christoph Hellwig | 862a804 | 2021-04-13 11:15:09 -0700 | [diff] [blame] | 899 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 900 | if (error) |
| 901 | goto out_unlock; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 902 | |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 903 | /* |
Randy Dunlap | b63da6c | 2020-08-05 08:49:58 -0700 | [diff] [blame] | 904 | * Search the data fork first to look up our source mapping. We |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 905 | * always need the data fork map, as we have to return it to the |
| 906 | * iomap code so that the higher level write code can read data in to |
| 907 | * perform read-modify-write cycles for unaligned writes. |
| 908 | */ |
| 909 | eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap); |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 910 | if (eof) |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 911 | imap.br_startoff = end_fsb; /* fake hole until the end */ |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 912 | |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 913 | /* We never need to allocate blocks for zeroing a hole. */ |
| 914 | if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) { |
| 915 | xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff); |
| 916 | goto out_unlock; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Search the COW fork extent list even if we did not find a data fork |
| 921 | * extent. This serves two purposes: first this implements the |
| 922 | * speculative preallocation using cowextsize, so that we also unshare |
| 923 | * block adjacent to shared blocks instead of just the shared blocks |
| 924 | * themselves. Second the lookup in the extent list is generally faster |
| 925 | * than going out to the shared extent tree. |
| 926 | */ |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 927 | if (xfs_is_cow_inode(ip)) { |
| 928 | if (!ip->i_cowfp) { |
| 929 | ASSERT(!xfs_is_reflink_inode(ip)); |
| 930 | xfs_ifork_init_cow(ip); |
| 931 | } |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 932 | cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, |
| 933 | &ccur, &cmap); |
| 934 | if (!cow_eof && cmap.br_startoff <= offset_fsb) { |
| 935 | trace_xfs_reflink_cow_found(ip, &cmap); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 936 | goto found_cow; |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | if (imap.br_startoff <= offset_fsb) { |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 941 | /* |
| 942 | * For reflink files we may need a delalloc reservation when |
| 943 | * overwriting shared extents. This includes zeroing of |
| 944 | * existing extents that contain data. |
| 945 | */ |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 946 | if (!xfs_is_cow_inode(ip) || |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 947 | ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) { |
| 948 | trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK, |
| 949 | &imap); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 950 | goto found_imap; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 951 | } |
| 952 | |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 953 | xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 954 | |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 955 | /* Trim the mapping to the nearest shared extent boundary. */ |
zhengbin | aa12443 | 2020-01-20 14:34:47 -0800 | [diff] [blame] | 956 | error = xfs_bmap_trim_cow(ip, &imap, &shared); |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 957 | if (error) |
| 958 | goto out_unlock; |
| 959 | |
| 960 | /* Not shared? Just report the (potentially capped) extent. */ |
| 961 | if (!shared) { |
| 962 | trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK, |
| 963 | &imap); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 964 | goto found_imap; |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Fork all the shared blocks from our write offset until the |
| 969 | * end of the extent. |
| 970 | */ |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 971 | allocfork = XFS_COW_FORK; |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 972 | end_fsb = imap.br_startoff + imap.br_blockcount; |
| 973 | } else { |
| 974 | /* |
| 975 | * We cap the maximum length we map here to MAX_WRITEBACK_PAGES |
| 976 | * pages to keep the chunks of work done where somewhat |
| 977 | * symmetric with the work writeback does. This is a completely |
| 978 | * arbitrary number pulled out of thin air. |
| 979 | * |
| 980 | * Note that the values needs to be less than 32-bits wide until |
| 981 | * the lower level functions are updated. |
| 982 | */ |
| 983 | count = min_t(loff_t, count, 1024 * PAGE_SIZE); |
Christoph Hellwig | 4356822 | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 984 | end_fsb = xfs_iomap_end_fsb(mp, offset, count); |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 985 | |
| 986 | if (xfs_is_always_cow_inode(ip)) |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 987 | allocfork = XFS_COW_FORK; |
Christoph Hellwig | 0365c5d | 2018-10-18 17:19:26 +1100 | [diff] [blame] | 988 | } |
| 989 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 990 | error = xfs_qm_dqattach_locked(ip, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | if (error) |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 992 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 994 | if (eof && offset + count > XFS_ISIZE(ip)) { |
| 995 | /* |
| 996 | * Determine the initial size of the preallocation. |
| 997 | * We clean up any extra preallocation when the file is closed. |
| 998 | */ |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 999 | if (xfs_has_allocsize(mp)) |
Darrick J. Wong | 590b165 | 2020-05-23 09:43:30 -0700 | [diff] [blame] | 1000 | prealloc_blocks = mp->m_allocsize_blocks; |
| 1001 | else |
| 1002 | prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, |
| 1003 | offset, count, &icur); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1004 | if (prealloc_blocks) { |
| 1005 | xfs_extlen_t align; |
| 1006 | xfs_off_t end_offset; |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 1007 | xfs_fileoff_t p_end_fsb; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1008 | |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 1009 | end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 1010 | p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) + |
| 1011 | prealloc_blocks; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1012 | |
Christoph Hellwig | 57c4944 | 2019-10-30 12:24:58 -0700 | [diff] [blame] | 1013 | align = xfs_eof_alignment(ip); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1014 | if (align) |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 1015 | p_end_fsb = roundup_64(p_end_fsb, align); |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1016 | |
Christoph Hellwig | 4356822 | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 1017 | p_end_fsb = min(p_end_fsb, |
| 1018 | XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes)); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 1019 | ASSERT(p_end_fsb > offset_fsb); |
| 1020 | prealloc_blocks = p_end_fsb - end_fsb; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1021 | } |
| 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Dave Chinner | 8de2bf9 | 2009-04-06 18:49:12 +0200 | [diff] [blame] | 1024 | retry: |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 1025 | error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb, |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 1026 | end_fsb - offset_fsb, prealloc_blocks, |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 1027 | allocfork == XFS_DATA_FORK ? &imap : &cmap, |
| 1028 | allocfork == XFS_DATA_FORK ? &icur : &ccur, |
| 1029 | allocfork == XFS_DATA_FORK ? eof : cow_eof); |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 1030 | switch (error) { |
| 1031 | case 0: |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1032 | break; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1033 | case -ENOSPC: |
| 1034 | case -EDQUOT: |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1035 | /* retry without any preallocation */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1036 | trace_xfs_delalloc_enospc(ip, offset, count); |
Brian Foster | f782088 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 1037 | if (prealloc_blocks) { |
| 1038 | prealloc_blocks = 0; |
Dave Chinner | 9aa0500 | 2012-10-08 21:56:04 +1100 | [diff] [blame] | 1039 | goto retry; |
Dave Chinner | 055388a | 2011-01-04 11:35:03 +1100 | [diff] [blame] | 1040 | } |
Gustavo A. R. Silva | 53004ee | 2021-04-20 17:54:36 -0500 | [diff] [blame] | 1041 | fallthrough; |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1042 | default: |
| 1043 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 1046 | if (allocfork == XFS_COW_FORK) { |
| 1047 | trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 1048 | goto found_cow; |
| 1049 | } |
| 1050 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1051 | /* |
| 1052 | * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch |
| 1053 | * them out if the write happens to fail. |
| 1054 | */ |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 1055 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 12dfb58 | 2019-10-19 09:09:47 -0700 | [diff] [blame] | 1056 | trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1057 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 1058 | |
| 1059 | found_imap: |
| 1060 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1061 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 1062 | |
| 1063 | found_cow: |
| 1064 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1065 | if (imap.br_startoff <= offset_fsb) { |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1066 | error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0); |
Christoph Hellwig | 36adcba | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 1067 | if (error) |
| 1068 | return error; |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1069 | return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, |
| 1070 | IOMAP_F_SHARED); |
Christoph Hellwig | db46e60 | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 1071 | } |
Darrick J. Wong | 72a048c | 2021-08-20 14:42:39 -0700 | [diff] [blame] | 1072 | |
| 1073 | xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1074 | return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0); |
Christoph Hellwig | ae36b53 | 2019-10-19 09:09:43 -0700 | [diff] [blame] | 1075 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 1076 | out_unlock: |
| 1077 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1078 | return error; |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static int |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 1082 | xfs_buffered_write_iomap_end( |
| 1083 | struct inode *inode, |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1084 | loff_t offset, |
| 1085 | loff_t length, |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1086 | ssize_t written, |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 1087 | unsigned flags, |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1088 | struct iomap *iomap) |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1089 | { |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 1090 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1091 | struct xfs_mount *mp = ip->i_mount; |
| 1092 | xfs_fileoff_t start_fsb; |
| 1093 | xfs_fileoff_t end_fsb; |
| 1094 | int error = 0; |
| 1095 | |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 1096 | if (iomap->type != IOMAP_DELALLOC) |
| 1097 | return 0; |
| 1098 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1099 | /* |
| 1100 | * Behave as if the write failed if drop writes is enabled. Set the NEW |
| 1101 | * flag to force delalloc cleanup. |
| 1102 | */ |
Darrick J. Wong | f8c4725 | 2017-06-20 17:54:48 -0700 | [diff] [blame] | 1103 | if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) { |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1104 | iomap->flags |= IOMAP_F_NEW; |
Brian Foster | 9dbddd7 | 2017-02-13 22:48:17 -0800 | [diff] [blame] | 1105 | written = 0; |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1106 | } |
Brian Foster | 9dbddd7 | 2017-02-13 22:48:17 -0800 | [diff] [blame] | 1107 | |
Brian Foster | fa7f138 | 2017-02-16 17:19:12 -0800 | [diff] [blame] | 1108 | /* |
| 1109 | * start_fsb refers to the first unused block after a short write. If |
| 1110 | * nothing was written, round offset down to point at the first block in |
| 1111 | * the range. |
| 1112 | */ |
| 1113 | if (unlikely(!written)) |
| 1114 | start_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1115 | else |
| 1116 | start_fsb = XFS_B_TO_FSB(mp, offset + written); |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1117 | end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1118 | |
| 1119 | /* |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1120 | * Trim delalloc blocks if they were allocated by this write and we |
| 1121 | * didn't manage to write the whole range. |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1122 | * |
| 1123 | * We don't need to care about racing delalloc as we hold i_mutex |
| 1124 | * across the reserve/allocate/unreserve calls. If there are delalloc |
| 1125 | * blocks in the range, they are ours. |
| 1126 | */ |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 1127 | if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) { |
Brian Foster | fa7f138 | 2017-02-16 17:19:12 -0800 | [diff] [blame] | 1128 | truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb), |
| 1129 | XFS_FSB_TO_B(mp, end_fsb) - 1); |
| 1130 | |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1131 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 1132 | end_fsb - start_fsb); |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 1133 | if (error && !xfs_is_shutdown(mp)) { |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1134 | xfs_alert(mp, "%s: unable to clean up ino %lld", |
| 1135 | __func__, ip->i_ino); |
| 1136 | return error; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
Christoph Hellwig | f150b42 | 2019-10-19 09:09:46 -0700 | [diff] [blame] | 1143 | const struct iomap_ops xfs_buffered_write_iomap_ops = { |
| 1144 | .iomap_begin = xfs_buffered_write_iomap_begin, |
| 1145 | .iomap_end = xfs_buffered_write_iomap_end, |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 1146 | }; |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1147 | |
| 1148 | static int |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 1149 | xfs_read_iomap_begin( |
| 1150 | struct inode *inode, |
| 1151 | loff_t offset, |
| 1152 | loff_t length, |
| 1153 | unsigned flags, |
| 1154 | struct iomap *iomap, |
| 1155 | struct iomap *srcmap) |
| 1156 | { |
| 1157 | struct xfs_inode *ip = XFS_I(inode); |
| 1158 | struct xfs_mount *mp = ip->i_mount; |
| 1159 | struct xfs_bmbt_irec imap; |
| 1160 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1161 | xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length); |
| 1162 | int nimaps = 1, error = 0; |
| 1163 | bool shared = false; |
| 1164 | unsigned lockmode; |
| 1165 | |
| 1166 | ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO))); |
| 1167 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 1168 | if (xfs_is_shutdown(mp)) |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 1169 | return -EIO; |
| 1170 | |
| 1171 | error = xfs_ilock_for_iomap(ip, flags, &lockmode); |
| 1172 | if (error) |
| 1173 | return error; |
| 1174 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, |
| 1175 | &nimaps, 0); |
| 1176 | if (!error && (flags & IOMAP_REPORT)) |
| 1177 | error = xfs_reflink_trim_around_shared(ip, &imap, &shared); |
| 1178 | xfs_iunlock(ip, lockmode); |
| 1179 | |
| 1180 | if (error) |
| 1181 | return error; |
| 1182 | trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1183 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, |
| 1184 | shared ? IOMAP_F_SHARED : 0); |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | const struct iomap_ops xfs_read_iomap_ops = { |
| 1188 | .iomap_begin = xfs_read_iomap_begin, |
| 1189 | }; |
| 1190 | |
| 1191 | static int |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1192 | xfs_seek_iomap_begin( |
| 1193 | struct inode *inode, |
| 1194 | loff_t offset, |
| 1195 | loff_t length, |
| 1196 | unsigned flags, |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 1197 | struct iomap *iomap, |
| 1198 | struct iomap *srcmap) |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1199 | { |
| 1200 | struct xfs_inode *ip = XFS_I(inode); |
| 1201 | struct xfs_mount *mp = ip->i_mount; |
| 1202 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1203 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1204 | xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF; |
| 1205 | struct xfs_iext_cursor icur; |
| 1206 | struct xfs_bmbt_irec imap, cmap; |
| 1207 | int error = 0; |
| 1208 | unsigned lockmode; |
| 1209 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 1210 | if (xfs_is_shutdown(mp)) |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1211 | return -EIO; |
| 1212 | |
| 1213 | lockmode = xfs_ilock_data_map_shared(ip); |
Christoph Hellwig | 862a804 | 2021-04-13 11:15:09 -0700 | [diff] [blame] | 1214 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 1215 | if (error) |
| 1216 | goto out_unlock; |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1217 | |
| 1218 | if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) { |
| 1219 | /* |
| 1220 | * If we found a data extent we are done. |
| 1221 | */ |
| 1222 | if (imap.br_startoff <= offset_fsb) |
| 1223 | goto done; |
| 1224 | data_fsb = imap.br_startoff; |
| 1225 | } else { |
| 1226 | /* |
| 1227 | * Fake a hole until the end of the file. |
| 1228 | */ |
Christoph Hellwig | 4356822 | 2019-10-19 09:09:44 -0700 | [diff] [blame] | 1229 | data_fsb = xfs_iomap_end_fsb(mp, offset, length); |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * If a COW fork extent covers the hole, report it - capped to the next |
| 1234 | * data fork extent: |
| 1235 | */ |
| 1236 | if (xfs_inode_has_cow_data(ip) && |
| 1237 | xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap)) |
| 1238 | cow_fsb = cmap.br_startoff; |
| 1239 | if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) { |
| 1240 | if (data_fsb < cow_fsb + cmap.br_blockcount) |
| 1241 | end_fsb = min(end_fsb, data_fsb); |
| 1242 | xfs_trim_extent(&cmap, offset_fsb, end_fsb); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1243 | error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, |
| 1244 | IOMAP_F_SHARED); |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1245 | /* |
| 1246 | * This is a COW extent, so we must probe the page cache |
| 1247 | * because there could be dirty page cache being backed |
| 1248 | * by this extent. |
| 1249 | */ |
| 1250 | iomap->type = IOMAP_UNWRITTEN; |
| 1251 | goto out_unlock; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * Else report a hole, capped to the next found data or COW extent. |
| 1256 | */ |
| 1257 | if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb) |
| 1258 | imap.br_blockcount = cow_fsb - offset_fsb; |
| 1259 | else |
| 1260 | imap.br_blockcount = data_fsb - offset_fsb; |
| 1261 | imap.br_startoff = offset_fsb; |
| 1262 | imap.br_startblock = HOLESTARTBLOCK; |
| 1263 | imap.br_state = XFS_EXT_NORM; |
| 1264 | done: |
| 1265 | xfs_trim_extent(&imap, offset_fsb, end_fsb); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1266 | error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0); |
Christoph Hellwig | 60271ab7 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1267 | out_unlock: |
| 1268 | xfs_iunlock(ip, lockmode); |
| 1269 | return error; |
| 1270 | } |
| 1271 | |
| 1272 | const struct iomap_ops xfs_seek_iomap_ops = { |
| 1273 | .iomap_begin = xfs_seek_iomap_begin, |
| 1274 | }; |
| 1275 | |
| 1276 | static int |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1277 | xfs_xattr_iomap_begin( |
| 1278 | struct inode *inode, |
| 1279 | loff_t offset, |
| 1280 | loff_t length, |
| 1281 | unsigned flags, |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 1282 | struct iomap *iomap, |
| 1283 | struct iomap *srcmap) |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1284 | { |
| 1285 | struct xfs_inode *ip = XFS_I(inode); |
| 1286 | struct xfs_mount *mp = ip->i_mount; |
| 1287 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1288 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); |
| 1289 | struct xfs_bmbt_irec imap; |
| 1290 | int nimaps = 1, error = 0; |
| 1291 | unsigned lockmode; |
| 1292 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 1293 | if (xfs_is_shutdown(mp)) |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1294 | return -EIO; |
| 1295 | |
Darrick J. Wong | 84358536d | 2017-04-06 16:00:39 -0700 | [diff] [blame] | 1296 | lockmode = xfs_ilock_attr_map_shared(ip); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1297 | |
| 1298 | /* if there are no attribute fork or extents, return ENOENT */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1299 | if (!XFS_IFORK_Q(ip) || !ip->i_afp->if_nextents) { |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1300 | error = -ENOENT; |
| 1301 | goto out_unlock; |
| 1302 | } |
| 1303 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1304 | ASSERT(ip->i_afp->if_format != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1305 | 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] | 1306 | &nimaps, XFS_BMAPI_ATTRFORK); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1307 | out_unlock: |
| 1308 | xfs_iunlock(ip, lockmode); |
| 1309 | |
Christoph Hellwig | 16be143 | 2019-02-18 09:38:46 -0800 | [diff] [blame] | 1310 | if (error) |
| 1311 | return error; |
| 1312 | ASSERT(nimaps); |
Christoph Hellwig | 740fd67 | 2021-11-29 11:21:57 +0100 | [diff] [blame] | 1313 | return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1314 | } |
| 1315 | |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1316 | const struct iomap_ops xfs_xattr_iomap_ops = { |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1317 | .iomap_begin = xfs_xattr_iomap_begin, |
| 1318 | }; |
Shiyang Ruan | f1ba5fa | 2021-11-29 11:21:49 +0100 | [diff] [blame] | 1319 | |
| 1320 | int |
| 1321 | xfs_zero_range( |
| 1322 | struct xfs_inode *ip, |
| 1323 | loff_t pos, |
| 1324 | loff_t len, |
| 1325 | bool *did_zero) |
| 1326 | { |
| 1327 | struct inode *inode = VFS_I(ip); |
| 1328 | |
Christoph Hellwig | c6f4046 | 2021-11-29 11:21:52 +0100 | [diff] [blame] | 1329 | if (IS_DAX(inode)) |
| 1330 | return dax_zero_range(inode, pos, len, did_zero, |
Christoph Hellwig | a50f6ab | 2021-11-29 11:21:56 +0100 | [diff] [blame] | 1331 | &xfs_direct_write_iomap_ops); |
Shiyang Ruan | f1ba5fa | 2021-11-29 11:21:49 +0100 | [diff] [blame] | 1332 | return iomap_zero_range(inode, pos, len, did_zero, |
| 1333 | &xfs_buffered_write_iomap_ops); |
| 1334 | } |
| 1335 | |
| 1336 | int |
| 1337 | xfs_truncate_page( |
| 1338 | struct xfs_inode *ip, |
| 1339 | loff_t pos, |
| 1340 | bool *did_zero) |
| 1341 | { |
| 1342 | struct inode *inode = VFS_I(ip); |
| 1343 | |
Christoph Hellwig | c6f4046 | 2021-11-29 11:21:52 +0100 | [diff] [blame] | 1344 | if (IS_DAX(inode)) |
| 1345 | return dax_truncate_page(inode, pos, did_zero, |
Christoph Hellwig | a50f6ab | 2021-11-29 11:21:56 +0100 | [diff] [blame] | 1346 | &xfs_direct_write_iomap_ops); |
Shiyang Ruan | f1ba5fa | 2021-11-29 11:21:49 +0100 | [diff] [blame] | 1347 | return iomap_truncate_page(inode, pos, did_zero, |
| 1348 | &xfs_buffered_write_iomap_ops); |
| 1349 | } |