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