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